Skip to content

fix: Claude stream block index/type transitions - #2854

Merged
Calcium-Ion merged 2 commits into
QuantumNous:mainfrom
seefs001:fix/claude-tool-index
Feb 7, 2026
Merged

fix: Claude stream block index/type transitions#2854
Calcium-Ion merged 2 commits into
QuantumNous:mainfrom
seefs001:fix/claude-tool-index

Conversation

@seefs001

@seefs001 seefs001 commented Feb 5, 2026

Copy link
Copy Markdown
Collaborator

fix #2695
claude的vscode插件会依赖返回信息的index,在交错思考情况下不符合插件预期。
attach文件再请求即可复现,终端使用不受影响。
另外VsCode插件上传文件没有被读取到,原因为OpenAI转Claude没有兼容文件类型,在另外的PR(#2689
)中有处理。

tips:不建议在有条件用claude原生格式的情况下使用转换,并非所有模型都有同样的交错思考的概念,未必能完美兼容,对性能也或许有一定的损耗。

@coderabbitai

coderabbitai Bot commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Two files are modified to improve tool-call indexing in Claude-to-OpenAI streaming conversion. New fields are added to the ClaudeConvertInfo struct to track tool-call indices, while service/convert.go implements helper functions and logic to properly manage content block state transitions and index propagation during tool-call streaming.

Changes

Cohort / File(s) Summary
Struct State Tracking
relay/common/relay_info.go
Added two integer fields (ToolCallBaseIndex, ToolCallMaxIndexOffset) to ClaudeConvertInfo struct for tracking tool-call indexing state.
Streaming Conversion Logic
service/convert.go
Introduced stopOpenBlocks and stopOpenBlocksAndAdvance helpers to manage content block termination across text/thinking/tools transitions. Added tool-call tracking state initialization, replaced direct Index references with local variables for precise block positioning, and updated tool_call handling to compute and propagate correct block indices for nested tool_use blocks with corresponding content_block events.

Sequence Diagram

sequenceDiagram
    participant Stream as Streaming Handler
    participant State as ClaudeConvertInfo State
    participant Blocks as Content Block Manager
    participant Events as Event Emitter

    Stream->>State: Detect tool_call in stream
    Stream->>State: Initialize ToolCallBaseIndex & ToolCallMaxIndexOffset
    
    alt Open Block Exists
        Stream->>Blocks: stopOpenBlocks()
        Blocks->>Events: Emit content_block_stop (previous block)
    end
    
    Stream->>Blocks: Compute correct block index
    Blocks->>Events: Emit content_block_start (tool_use)
    
    loop For Each Tool Call Delta
        Stream->>Blocks: Emit content_block_delta with correct Index
        Blocks->>Events: Stream delta event
    end
    
    Stream->>Blocks: Finalize block state
    Blocks->>Events: Emit content_block_stop (tool_use)
    Events->>Events: Report usage + finish_reason
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Poem

🐰 Hippity-hop through blocks we go,
Tool calls dancing to and fro,
Index tracking, state so true,
Content blocks in perfect queue! 🎯

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main purpose of the changes: fixing how Claude handles stream block indices and type transitions during tool call processing.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@Calcium-Ion
Calcium-Ion merged commit dd5610d into QuantumNous:main Feb 7, 2026
1 check passed
@coderabbitai coderabbitai Bot mentioned this pull request Mar 2, 2026
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
fix: Claude stream block index/type transitions
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.

claude code 的新版vscode插件(>2.0.56),配置new-api(0.10.5)的大模型,请求报错

2 participants