Skip to content

feat: SSE raw passthrough for Claude-format relay - #5846

Closed
ITboynm wants to merge 1 commit into
QuantumNous:mainfrom
ITboynm:daw-custom
Closed

ITboynm wants to merge 1 commit into
QuantumNous:mainfrom
ITboynm:daw-custom

Conversation

@ITboynm

@ITboynm ITboynm commented Jul 2, 2026 •

Copy link
Copy Markdown
  • Add RawLineMode to transparently forward all SSE lines (event:, data:, empty) instead of filtering out event: lines, matching sub2api's passthrough design
  • Parse usage data as a side-effect without modifying the stream
  • Continue reading upstream after client disconnect to collect billing usage
  • Add Anthropic-native ping format (event: ping) for Claude relay
  • Change STREAMING_TIMEOUT default from 300s to 0 (disabled), matching sub2api
  • Fixes Agent tool-call disconnections and missing token statistics

AI-assisted: code generated with Claude Code

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

  • Bug Fixes
    • Improved Claude streaming reliability, including support for raw line streaming and cleaner handling of streamed events.
    • Fixed client-disconnect handling so responses can continue long enough to capture final usage details.
    • Updated streaming timeout behavior so no-timeout cases behave more predictably.
    • Added a lightweight ping response for streaming connections to help keep streams active.

- Add RawLineMode to transparently forward all SSE lines (event:, data:, empty)
  instead of filtering out event: lines, matching sub2api's passthrough design
- Parse usage data as a side-effect without modifying the stream
- Continue reading upstream after client disconnect to collect billing usage
- Add Anthropic-native ping format (event: ping) for Claude relay
- Change STREAMING_TIMEOUT default from 300s to 0 (disabled), matching sub2api
- Fixes Agent tool-call disconnections and missing token statistics

AI-assisted: code generated with Claude Code
@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f64b4df4-1fee-43a3-8ff2-146c51e08693

📥 Commits

Reviewing files that changed from the base of the PR and between 52858ad and c3a9e6f.

📒 Files selected for processing (5)
  • common/init.go
  • relay/channel/claude/relay-claude.go
  • relay/common/relay_info.go
  • relay/helper/common.go
  • relay/helper/stream_scanner.go

Walkthrough

Adds a raw SSE pass-through streaming mode for Claude relay. RelayInfo gains RawLineMode and ClientDisconnected fields; a new HandleRawLineStreamData handler and ClaudePingData helper support this mode; StreamScannerHandler is updated with mode-aware ticker, ping, and end-reason logic; and the default STREAMING_TIMEOUT fallback changes from 300 to 0.

Changes

Raw Line Streaming Mode

Layer / File(s) Summary
RelayInfo fields and timeout default
relay/common/relay_info.go, common/init.go
Adds RawLineMode and ClientDisconnected fields to RelayInfo; changes default STREAMING_TIMEOUT fallback from 300 to 0.
Claude raw line handler and dispatch
relay/channel/claude/relay-claude.go
Adds HandleRawLineStreamData to forward/parse raw SSE lines and update ClaudeResponseInfo; ClaudeStreamHandler enables RawLineMode and dispatches to the new handler for Claude relay format.
Raw mode ping and scanner control flow
relay/helper/common.go, relay/helper/stream_scanner.go
Adds ClaudePingData helper; updates StreamScannerHandler's ticker setup, ping dispatch, client-disconnect handling, data forwarding, and end-reason selection to be RawLineMode-aware.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ClaudeStreamHandler
    participant StreamScannerHandler
    participant HandleRawLineStreamData
    participant Upstream

    Client->>ClaudeStreamHandler: stream request (Claude format)
    ClaudeStreamHandler->>ClaudeStreamHandler: set RawLineMode = true
    ClaudeStreamHandler->>StreamScannerHandler: start scanning
    loop each scanned line
        Upstream-->>StreamScannerHandler: raw line
        alt RawLineMode
            StreamScannerHandler->>HandleRawLineStreamData: forward line
            HandleRawLineStreamData->>HandleRawLineStreamData: parse data:/update ClaudeResponseInfo
            HandleRawLineStreamData-->>Client: write line, flush on empty
        else legacy mode
            StreamScannerHandler->>StreamScannerHandler: HandleStreamResponseData
        end
    end
    alt client disconnected
        StreamScannerHandler->>StreamScannerHandler: mark ClientDisconnected, continue reading upstream
    end
Loading

Poem

A rabbit hops through streaming lines,
Raw and pure, no filter binds,
Ping goes out in Claude's own way,
Timeout set to zero — stay!
Thump thump, the data flows just right,
Carrots crunched, the code's tonight 🥕✨

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@ITboynm

ITboynm commented Jul 2, 2026

Copy link
Copy Markdown
Author
  • Add RawLineMode to transparently forward all SSE lines (event:, data:, empty) instead of filtering out event: lines, matching sub2api's passthrough design
  • Parse usage data as a side-effect without modifying the stream
  • Continue reading upstream after client disconnect to collect billing usage
  • Add Anthropic-native ping format (event: ping) for Claude relay
  • Change STREAMING_TIMEOUT default from 300s to 0 (disabled), matching sub2api
  • Fixes Agent tool-call disconnections and missing token statistics

AI-assisted: code generated with Claude Code

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

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.

1 participant