Skip to content

WIP: feat: Add ClaudeMessages relay support for volcengine doubao#287

Open
gkzhb wants to merge 1 commit intoLaisky:mainfrom
gkzhb:fix/messages-api-thinking
Open

WIP: feat: Add ClaudeMessages relay support for volcengine doubao#287
gkzhb wants to merge 1 commit intoLaisky:mainfrom
gkzhb:fix/messages-api-thinking

Conversation

@gkzhb
Copy link

@gkzhb gkzhb commented Jan 5, 2026

TODOs:

  • In OpenCode, request oneapi /v1/messages (using doubao provider)with tool calls failed with error
Bad Request: {"error":{"message":"The parameter `tool_choice.type` specified in the request are not valid: expected 'function', but got `auto` instead. Request id: 0217676361346139b682021c37db203d3df9ca3c30e9969579fd8 (request id: 2026010602021435833600021693409)","type":"BadRequest","param":"tool_choice.type","code":"InvalidParameter"}}

Summary by CodeRabbit

  • New Features

    • Extended Doubao adaptor with support for Claude Messages, enabling additional chat completion endpoints based on model configuration.
  • Bug Fixes

    • Enhanced stream processing for thinking content handling—now intelligently filters and emits only non-empty thinking blocks with intelligent fallback to alternative content sources.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 5, 2026

📝 Walkthrough

Walkthrough

Two files modified: one extends the Doubao adaptor to route Claude Messages requests to appropriate endpoints based on model name, the other refines thinking content handling in Claude SSE stream conversion using preference and fallback logic.

Changes

Cohort / File(s) Change Summary
Doubao Claude Messages routing
relay/adaptor/doubao/main.go
Added relaymode.ClaudeMessages case in GetRequestURL; routes to bots/chat/completions for model names starting with "bot", otherwise chat/completions
Claude SSE thinking content handling
relay/adaptor/openai_compatible/claude_convert.go
Modified thinking content resolution to prefer delta.Thinking with fallback to delta.ReasoningContent; only emits when non-empty

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A rabbit hops with glee so bright,
Claude's thoughts now flow just right,
Doubao routes with care,
New pathways to spare—
Messages leap through the night! 🌙

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title mentions 'ClaudeMessages relay support for volcengine doubao' which aligns with the main change adding Claude Messages support to the Doubao adaptor, but the WIP prefix and slightly different framing than the full objective description make it partially aligned.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gkzhb gkzhb changed the title fix: anthropic /v1/messages sse not returning thinking_delta, and add ClaudeMessages for doubao fix: anthropic /v1/messages sse not returning thinking_delta, and add ClaudeMessages relay support for volcengine doubao Jan 5, 2026
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
relay/adaptor/doubao/main.go (1)

22-27: Consider refactoring to eliminate code duplication.

The ClaudeMessages case duplicates the exact logic from the ChatCompletions case (lines 16-19). If the routing logic needs to change in the future, both cases must be updated, increasing maintenance burden.

🔎 Proposed refactor to reduce duplication
 func GetRequestURL(meta *meta.Meta) (string, error) {
 	switch meta.Mode {
-	case relaymode.ChatCompletions:
-		if strings.HasPrefix(meta.ActualModelName, "bot") {
-			return fmt.Sprintf("%s/api/v3/bots/chat/completions", meta.BaseURL), nil
-		}
-		return fmt.Sprintf("%s/api/v3/chat/completions", meta.BaseURL), nil
+	case relaymode.ChatCompletions, relaymode.ClaudeMessages:
+		// 豆包支持 Claude Messages API 格式,转换为 ChatCompletions 处理
+		if strings.HasPrefix(meta.ActualModelName, "bot") {
+			return fmt.Sprintf("%s/api/v3/bots/chat/completions", meta.BaseURL), nil
+		}
+		return fmt.Sprintf("%s/api/v3/chat/completions", meta.BaseURL), nil
 	case relaymode.Embeddings:
 		return fmt.Sprintf("%s/api/v3/embeddings", meta.BaseURL), nil
-	case relaymode.ClaudeMessages:
-		// 豆包支持 Claude Messages API 格式,转换为 ChatCompletions 处理
-		if strings.HasPrefix(meta.ActualModelName, "bot") {
-			return fmt.Sprintf("%s/api/v3/bots/chat/completions", meta.BaseURL), nil
-		}
-		return fmt.Sprintf("%s/api/v3/chat/completions", meta.BaseURL), nil
 	default:
 	}
 	return "", errors.Errorf("unsupported relay mode %d for doubao", meta.Mode)
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 81d10fd and 4c22202.

📒 Files selected for processing (2)
  • relay/adaptor/doubao/main.go
  • relay/adaptor/openai_compatible/claude_convert.go
🧰 Additional context used
🧠 Learnings (7)
📓 Common learnings
Learnt from: CR
Repo: Laisky/one-api PR: 0
File: .github/instructions/memory.instructions.md:0-0
Timestamp: 2025-10-27T15:08:58.269Z
Learning: Applies to relay/adaptor/openai_compatible/claude_messages.go : Use ctxkey.ClaudeMessagesConversion and ctxkey.OriginalClaudeRequest to keep streaming, quota reconciliation, and logging aligned during Claude Messages conversions
Learnt from: CR
Repo: Laisky/one-api PR: 0
File: .github/instructions/memory.instructions.md:0-0
Timestamp: 2025-10-27T15:08:58.269Z
Learning: Applies to relay/adaptor/openai_compatible/claude_messages.go : Implement ConvertClaudeRequest to map Claude Messages to OpenAI ChatCompletion and HandleClaudeMessagesResponse to reverse-map responses
📚 Learning: 2025-10-27T15:08:58.269Z
Learnt from: CR
Repo: Laisky/one-api PR: 0
File: .github/instructions/memory.instructions.md:0-0
Timestamp: 2025-10-27T15:08:58.269Z
Learning: Applies to relay/adaptor/openai_compatible/claude_messages.go : Implement ConvertClaudeRequest to map Claude Messages to OpenAI ChatCompletion and HandleClaudeMessagesResponse to reverse-map responses

Applied to files:

  • relay/adaptor/doubao/main.go
  • relay/adaptor/openai_compatible/claude_convert.go
📚 Learning: 2025-10-27T15:08:58.269Z
Learnt from: CR
Repo: Laisky/one-api PR: 0
File: .github/instructions/memory.instructions.md:0-0
Timestamp: 2025-10-27T15:08:58.269Z
Learning: Applies to relay/adaptor/openai_compatible/claude_messages.go : Use ctxkey.ClaudeMessagesConversion and ctxkey.OriginalClaudeRequest to keep streaming, quota reconciliation, and logging aligned during Claude Messages conversions

Applied to files:

  • relay/adaptor/doubao/main.go
  • relay/adaptor/openai_compatible/claude_convert.go
📚 Learning: 2025-10-27T15:08:58.269Z
Learnt from: CR
Repo: Laisky/one-api PR: 0
File: .github/instructions/memory.instructions.md:0-0
Timestamp: 2025-10-27T15:08:58.269Z
Learning: Applies to relay/adaptor/openai_compatible/claude_messages.go : Consider gating structured-output promotion per provider/model to avoid empty or missing structured outputs for specific upstreams (e.g., Azure gpt-5-nano, OpenAI gpt-5-mini streaming Claude, DeepSeek Response structured)

Applied to files:

  • relay/adaptor/doubao/main.go
  • relay/adaptor/openai_compatible/claude_convert.go
📚 Learning: 2025-10-27T15:08:58.269Z
Learnt from: CR
Repo: Laisky/one-api PR: 0
File: .github/instructions/memory.instructions.md:0-0
Timestamp: 2025-10-27T15:08:58.269Z
Learning: Applies to relay/adaptor/openai_compatible/claude_messages.go : Structured-output promotion to response_format=json_schema only when all conditions pass: one tool with additionalProperties=false; no existing tool_use/tool_result; tool_choice matches name case-insensitively; description or text references JSON/structured/schema semantics. When promoted, drop tools/tool_choice, set strict=true, and treat as structured output

Applied to files:

  • relay/adaptor/doubao/main.go
📚 Learning: 2025-10-27T15:08:58.269Z
Learnt from: CR
Repo: Laisky/one-api PR: 0
File: .github/instructions/memory.instructions.md:0-0
Timestamp: 2025-10-27T15:08:58.269Z
Learning: Applies to relay/adaptor/openai_compatible/*_test.go : Maintain regression tests for structured-output promotion: TestConvertClaudeRequest_StructuredToolPromoted and TestConvertClaudeRequest_ToolNotPromoted

Applied to files:

  • relay/adaptor/doubao/main.go
  • relay/adaptor/openai_compatible/claude_convert.go
📚 Learning: 2025-10-27T15:08:58.269Z
Learnt from: CR
Repo: Laisky/one-api PR: 0
File: .github/instructions/memory.instructions.md:0-0
Timestamp: 2025-10-27T15:08:58.269Z
Learning: Applies to relay/controller/response.go : ConvertResponseAPIToChatCompletionRequest for non-OpenAI channels, execute as ChatCompletions, then rewrap via ctxkey.ResponseRewriteHandler; ensure text-only collapses to a single string message and multimodal stays structured; tool definitions, reasoning config, and JSON schema must round-trip

Applied to files:

  • relay/adaptor/doubao/main.go
🧬 Code graph analysis (2)
relay/adaptor/doubao/main.go (1)
common/ctxkey/key.go (1)
  • BaseURL (144-144)
relay/adaptor/openai_compatible/claude_convert.go (2)
relay/adaptor/anthropic/model.go (1)
  • Delta (116-124)
relay/model/general.go (1)
  • Thinking (179-182)
🔇 Additional comments (1)
relay/adaptor/openai_compatible/claude_convert.go (1)

264-302: LGTM! Thinking content fallback logic correctly addresses the PR objective.

The change implements a proper preference-and-fallback pattern:

  1. Prefers Delta.Thinking when available and non-empty
  2. Falls back to Delta.ReasoningContent when Thinking is absent
  3. Only emits thinking blocks when content is non-empty

This correctly addresses the issue mentioned in the PR title where thinking_delta was not being returned. The resolved thinkingContent variable is safely dereferenced on line 289 because the guard on line 272 ensures it's non-nil and non-empty.

@gkzhb gkzhb changed the title fix: anthropic /v1/messages sse not returning thinking_delta, and add ClaudeMessages relay support for volcengine doubao WIP: fix: anthropic /v1/messages sse not returning thinking_delta, and add ClaudeMessages relay support for volcengine doubao Jan 5, 2026
@Laisky
Copy link
Owner

Laisky commented Jan 6, 2026

Are you still working on it, or do you need my help?

@gkzhb
Copy link
Author

gkzhb commented Jan 6, 2026

I'm still working on this pr, and any guidance would be welcome.

@Laisky
Copy link
Owner

Laisky commented Jan 6, 2026

I'm still working on this pr, and any guidance would be welcome.

Could you provide a request body that will cause the error? This would be very helpful for fixing the issue and for testing.

@gkzhb
Copy link
Author

gkzhb commented Jan 7, 2026

Request path is /v1/messages, and the simplified request body:

{
  "model": "kimi-k2-thinking-251104",
  "max_tokens": 32000,
  "system": [],
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "hi",
          "cache_control": {
            "type": "ephemeral"
          }
        }
      ]
    }
  ],
  "tools": [
    {
      "name": "bash",
      "description": "Executes a given bash command in a persistent shell session with optional timeout, ensuring proper handling and security measures.\n\nAll commands run in /data00/home/zhanghaibin.zhb/gitrep/one-api by default. Use the `workdir` parameter if you need to run a command in a different directory. AVOID using `cd <directory> && <command>` patterns - use `workdir` instead.\n\nIMPORTANT: This tool is for terminal operations like git, npm, docker, etc. DO NOT use it for file operations (reading, writing, editing, searching, finding files) - use the specialized tools for this instead.\n\nBefore executing the command, please follow these steps:\n\n1. Directory Verification:\n   - If the command will create new directories or files, first use `ls` to verify the parent directory exists and is the correct location\n   - For example, before running \"mkdir foo/bar\", first use `ls foo` to check that \"foo\" exists and is the intended parent directory\n\n2. Command Execution:\n   - Always quote file paths that contain spaces with double quotes (e.g., rm \"path with spaces/file.txt\")\n   - Examples of proper quoting:\n     - mkdir \"/Users/name/My Documents\" (correct)\n     - mkdir /Users/name/My Documents (incorrect - will fail)\n     - python \"/path/with spaces/script.py\" (correct)\n     - python /path/with spaces/script.py (incorrect - will fail)\n   - After ensuring proper quoting, execute the command.\n   - Capture the output of the command.\n\nUsage notes:\n  - The command argument is required.\n  - You can specify an optional timeout in milliseconds (up to 600000ms / 10 minutes). If not specified, commands will time out after 120000ms (2 minutes).\n  - It is very helpful if you write a clear, concise description of what this command does in 5-10 words.\n  - If the output exceeds 30000 characters, output will be truncated before being returned to you.\n  - You can use the `run_in_background` parameter to run the command in the background, which allows you to continue working while the command runs. You can monitor the output using the Bash tool as it becomes available. You do not need to use '&' at the end of the command when using this parameter.\n\n  - Avoid using Bash with the `find`, `grep`, `cat`, `head`, `tail`, `sed`, `awk`, or `echo` commands, unless explicitly instructed or when these commands are truly necessary for the task. Instead, always prefer using the dedicated tools for these commands:\n    - File search: Use Glob (NOT find or ls)\n    - Content search: Use Grep (NOT grep or rg)\n    - Read files: Use Read (NOT cat/head/tail)\n    - Edit files: Use Edit (NOT sed/awk)\n    - Write files: Use Write (NOT echo >/cat <<EOF)\n    - Communication: Output text directly (NOT echo/printf)\n  - When issuing multiple commands:\n    - If the commands are independent and can run in parallel, make multiple Bash tool calls in a single message. For example, if you need to run \"git status\" and \"git diff\", send a single message with two Bash tool calls in parallel.\n    - If the commands depend on each other and must run sequentially, use a single Bash call with '&&' to chain them together (e.g., `git add . && git commit -m \"message\" && git push`). For instance, if one operation must complete before another starts (like mkdir before cp, Write before Bash for git operations, or git add before git commit), run these operations sequentially instead.\n    - Use ';' only when you need to run commands sequentially but don't care if earlier commands fail\n    - DO NOT use newlines to separate commands (newlines are ok in quoted strings)\n  - AVOID using `cd <directory> && <command>`. Use the `workdir` parameter to change directories instead.\n    <good-example>\n    Use workdir=\"/foo/bar\" with command: pytest tests\n    </good-example>\n    <bad-example>\n    cd /foo/bar && pytest tests\n    </bad-example>\n\n# Committing changes with git\n\nOnly create commits when requested by the user. If unclear, ask first. When the user asks you to create a new git commit, follow these steps carefully:\n\nGit Safety Protocol:\n- NEVER update the git config\n- NEVER run destructive/irreversible git commands (like push --force, hard reset, etc) unless the user explicitly requests them\n- NEVER skip hooks (--no-verify, --no-gpg-sign, etc) unless the user explicitly requests it\n- NEVER run force push to main/master, warn the user if they request it\n- Avoid git commit --amend. ONLY use --amend when ALL conditions are met:\n  (1) User explicitly requested amend, OR commit SUCCEEDED but pre-commit hook auto-modified files that need including\n  (2) HEAD commit was created by you in this conversation (verify: git log -1 --format='%an %ae')\n  (3) Commit has NOT been pushed to remote (verify: git status shows \"Your branch is ahead\")\n- CRITICAL: If commit FAILED or was REJECTED by hook, NEVER amend - fix the issue and create a NEW commit\n- CRITICAL: If you already pushed to remote, NEVER amend unless user explicitly requests it (requires force push)\n- NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.\n\n1. You can call multiple tools in a single response. When multiple independent pieces of information are requested and all commands are likely to succeed, run multiple tool calls in parallel for optimal performance. run the following bash commands in parallel, each using the Bash tool:\n  - Run a git status command to see all untracked files.\n  - Run a git diff command to see both staged and unstaged changes that will be committed.\n  - Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.\n2. Analyze all staged changes (both previously staged and newly added) and draft a commit message:\n  - Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.). Ensure the message accurately reflects the changes and their purpose (i.e. \"add\" means a wholly new feature, \"update\" means an enhancement to an existing feature, \"fix\" means a bug fix, etc.).\n  - Do not commit files that likely contain secrets (.env, credentials.json, etc.). Warn the user if they specifically request to commit those files\n  - Draft a concise (1-2 sentences) commit message that focuses on the \"why\" rather than the \"what\"\n  - Ensure it accurately reflects the changes and their purpose\n3. You can call multiple tools in a single response. When multiple independent pieces of information are requested and all commands are likely to succeed, run multiple tool calls in parallel for optimal performance. run the following commands:\n   - Add relevant untracked files to the staging area.\n   - Create the commit with a message\n   - Run git status after the commit completes to verify success.\n   Note: git status depends on the commit completing, so run it sequentially after the commit.\n4. If the commit fails due to pre-commit hook, fix the issue and create a NEW commit (see amend rules above)\n\nImportant notes:\n- NEVER run additional commands to read or explore code, besides git bash commands\n- NEVER use the TodoWrite or Task tools\n- DO NOT push to the remote repository unless the user explicitly asks you to do so\n- IMPORTANT: Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported.\n- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit\n\n# Creating pull requests\nUse the gh command via the Bash tool for ALL GitHub-related tasks including working with issues, pull requests, checks, and releases. If given a Github URL use the gh command to get the information needed.\n\nIMPORTANT: When the user asks you to create a pull request, follow these steps carefully:\n\n1. You can call multiple tools in a single response. When multiple independent pieces of information are requested and all commands are likely to succeed, run multiple tool calls in parallel for optimal performance. run the following bash commands in parallel using the Bash tool, in order to understand the current state of the branch since it diverged from the main branch:\n   - Run a git status command to see all untracked files\n   - Run a git diff command to see both staged and unstaged changes that will be committed\n   - Check if the current branch tracks a remote branch and is up to date with the remote, so you know if you need to push to the remote\n   - Run a git log command and `git diff [base-branch]...HEAD` to understand the full commit history for the current branch (from the time it diverged from the base branch)\n2. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (NOT just the latest commit, but ALL commits that will be included in the pull request!!!), and draft a pull request summary\n3. You can call multiple tools in a single response. When multiple independent pieces of information are requested and all commands are likely to succeed, run multiple tool calls in parallel for optimal performance. run the following commands in parallel:\n   - Create new branch if needed\n   - Push to remote with -u flag if needed\n   - Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.\n<example>\ngh pr create --title \"the pr title\" --body \"$(cat <<'EOF'\n## Summary\n<1-3 bullet points>\n</example>\n\nImportant:\n- DO NOT use the TodoWrite or Task tools\n- Return the PR URL when you're done, so the user can see it\n\n# Other common operations\n- View comments on a Github PR: gh api repos/foo/bar/pulls/123/comments\n",
      "input_schema": {
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "type": "object",
        "properties": {
          "command": {
            "description": "The command to execute",
            "type": "string"
          },
          "timeout": {
            "description": "Optional timeout in milliseconds",
            "type": "number"
          },
          "workdir": {
            "description": "The working directory to run the command in. Defaults to /data00/home/zhanghaibin.zhb/gitrep/one-api. Use this instead of 'cd' commands.",
            "type": "string"
          },
          "description": {
            "description": "Clear, concise description of what this command does in 5-10 words. Examples:\nInput: ls\nOutput: Lists files in current directory\n\nInput: git status\nOutput: Shows working tree status\n\nInput: npm install\nOutput: Installs package dependencies\n\nInput: mkdir foo\nOutput: Creates directory 'foo'",
            "type": "string"
          }
        },
        "required": [
          "command",
          "description"
        ],
        "additionalProperties": false
      }
    }
  ],
  "tool_choice": {
    "type": "auto"
  },
  "stream": true
}

@gkzhb
Copy link
Author

gkzhb commented Jan 7, 2026

I may need to split this PR into two separate ones:

@gkzhb gkzhb changed the title WIP: fix: anthropic /v1/messages sse not returning thinking_delta, and add ClaudeMessages relay support for volcengine doubao WIP: feat: Add ClaudeMessages relay support for volcengine doubao Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments