Repository navigation
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughStreamScannerHandler now returns a retriable-aware error; multiple channel stream handlers were updated to capture and propagate that scanner error (returning early on non-nil). StreamStatus gained IsRetriable to classify retriable end reasons. Tests and HTTP client timeout handling were also adjusted. Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client
participant Relay as RelayHandler
participant Upstream as Upstream
participant Scanner as StreamScanner
participant Service as Service
Client->>Relay: send streaming request
Relay->>Upstream: forward request
Upstream-->>Relay: streaming HTTP response (resp)
Relay->>Scanner: StreamScannerHandler(resp, info, dataHandler)
Scanner->>Relay: invoke dataHandler per chunk
Scanner-->>Relay: returns error or nil
alt streamErr != nil
Relay->>Service: CloseResponseBodyGracefully(resp)
Relay-->>Client: return error (streamErr)
else streamErr == nil
Relay->>Service: extract usage / finalize
Relay-->>Client: return success + usage
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
relay/channel/openai/audio.go (1)
38-53:⚠️ Potential issue | 🟡 MinorOpenaiTTSHandler should return an error like all other channel handlers.
Unlike other handlers (Claude, Gemini, STT, chat, Rerank, etc.),
OpenaiTTSHandlerhas a signature that returns only*dto.Usagewith no error. This means it cannot propagate the error fromStreamScannerHandlerto the caller at line 619, preventing the retry loop from triggering for transient failures.All comparable handlers return
(*dto.Usage, *types.NewAPIError)or(*types.NewAPIError, *dto.Usage)and are called with error capture (e.g.,err, usage = OpenaiSTTHandler(...)at line 623). The TTS handler should follow this same pattern.Change the function signature to
func OpenaiTTSHandler(...) (*dto.Usage, *types.NewAPIError)to align with other handlers and allow proper error propagation.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@relay/channel/openai/audio.go` around lines 38 - 53, OpenaiTTSHandler currently returns only *dto.Usage and swallows streaming errors; change its signature to func OpenaiTTSHandler(...) (*dto.Usage, *types.NewAPIError) and propagate any errors from helper.StreamScannerHandler back to the caller as a *types.NewAPIError (create/convert from the sr.Error or returned error in the StreamScannerHandler callback). Ensure the callback passed to helper.StreamScannerHandler sets/collects the error and that OpenaiTTSHandler returns (nil, newAPIErr) when an error occurred, otherwise return (usage, nil) so callers that expect (err, usage) can trigger retries.
🧹 Nitpick comments (1)
relay/helper/stream_scanner_test.go (1)
533-533: Add assertions for the new return-value contract.At Line 533 (and similarly across this file), the returned error is discarded. Please add explicit tests for:
- abnormal end with zero chunks ⇒ non-nil
channel:*retriable error,- abnormal end after at least one chunk ⇒ nil error.
This protects the new retry behavior from regressions.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@relay/helper/stream_scanner_test.go` at line 533, Update tests that call StreamScannerHandler (e.g., the call at the line using StreamScannerHandler(c, resp, info, func(data string, sr *StreamResult) {})) to capture its returned error instead of discarding it, and add two assertions: simulate an abnormal end with zero chunks and assert the returned error is a non-nil retriable channel error (match error type or message like "channel:*"), and simulate an abnormal end after at least one chunk and assert the returned error is nil; use the same StreamScannerHandler invocation and StreamResult hook to produce the chunk/no-chunk scenarios so the new retry contract is covered across the file.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@relay/helper/stream_scanner.go`:
- Around line 304-308: The returned channel error in the
types.NewErrorWithStatusCode call currently uses http.StatusGatewayTimeout (504)
which prevents the orchestration from treating it as a retriable channel error;
update the status code to http.StatusBadGateway (502) in the
types.NewErrorWithStatusCode invocation (the one that wraps fmt.Errorf("stream
failed without response: %s", info.StreamStatus.Summary())) in stream_scanner.go
so the error emits a 502 and matches the existing retry/fallback contract.
---
Outside diff comments:
In `@relay/channel/openai/audio.go`:
- Around line 38-53: OpenaiTTSHandler currently returns only *dto.Usage and
swallows streaming errors; change its signature to func OpenaiTTSHandler(...)
(*dto.Usage, *types.NewAPIError) and propagate any errors from
helper.StreamScannerHandler back to the caller as a *types.NewAPIError
(create/convert from the sr.Error or returned error in the StreamScannerHandler
callback). Ensure the callback passed to helper.StreamScannerHandler
sets/collects the error and that OpenaiTTSHandler returns (nil, newAPIErr) when
an error occurred, otherwise return (usage, nil) so callers that expect (err,
usage) can trigger retries.
---
Nitpick comments:
In `@relay/helper/stream_scanner_test.go`:
- Line 533: Update tests that call StreamScannerHandler (e.g., the call at the
line using StreamScannerHandler(c, resp, info, func(data string, sr
*StreamResult) {})) to capture its returned error instead of discarding it, and
add two assertions: simulate an abnormal end with zero chunks and assert the
returned error is a non-nil retriable channel error (match error type or message
like "channel:*"), and simulate an abnormal end after at least one chunk and
assert the returned error is nil; use the same StreamScannerHandler invocation
and StreamResult hook to produce the chunk/no-chunk scenarios so the new retry
contract is covered across the file.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: b1f81ca0-ed9d-485e-bdf5-f54ed39a3512
📒 Files selected for processing (12)
relay/channel/baidu/relay-baidu.gorelay/channel/claude/relay-claude.gorelay/channel/dify/relay-dify.gorelay/channel/gemini/relay-gemini.gorelay/channel/openai/audio.gorelay/channel/openai/chat_via_responses.gorelay/channel/openai/relay-openai.gorelay/channel/openai/relay_responses.gorelay/channel/xai/text.gorelay/common/stream_status.gorelay/helper/stream_scanner.gorelay/helper/stream_scanner_test.go
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@relay/channel/claude/relay-claude.go`:
- Around line 890-897: The early return on stream error in the
StreamScannerHandler call can leak the HTTP response body (resp); add explicit
cleanup by calling service.CloseResponseBodyGracefully(resp) before returning
the streamErr, or preferably add a defer
service.CloseResponseBodyGracefully(resp) at the start of the surrounding
function so resp is always closed; update the block that calls
helper.StreamScannerHandler(c, resp, info, ...) to ensure resp is closed on the
new error path (reference resp, helper.StreamScannerHandler,
HandleStreamResponseData, and service.CloseResponseBodyGracefully).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 84f2cbf8-7bc7-46d5-83bb-3a630ffb3847
📒 Files selected for processing (12)
relay/channel/baidu/relay-baidu.gorelay/channel/claude/relay-claude.gorelay/channel/dify/relay-dify.gorelay/channel/gemini/relay-gemini.gorelay/channel/openai/audio.gorelay/channel/openai/chat_via_responses.gorelay/channel/openai/relay-openai.gorelay/channel/openai/relay_responses.gorelay/channel/xai/text.gorelay/common/stream_status.gorelay/helper/stream_scanner.gorelay/helper/stream_scanner_test.go
✅ Files skipped from review due to trivial changes (3)
- relay/channel/dify/relay-dify.go
- relay/channel/openai/audio.go
- relay/helper/stream_scanner_test.go
🚧 Files skipped from review as they are similar to previous changes (7)
- relay/channel/openai/chat_via_responses.go
- relay/channel/openai/relay-openai.go
- relay/common/stream_status.go
- relay/channel/gemini/relay-gemini.go
- relay/channel/xai/text.go
- relay/helper/stream_scanner.go
- relay/channel/baidu/relay-baidu.go
…sponse When an upstream provider accepts a streaming connection (HTTP 200) but never sends any tokens, the stream eventually times out. Previously this was silently treated as success with zero tokens billed, and the retry mechanism never triggered. Now StreamScannerHandler returns a retriable channel error (502) when the stream ends abnormally (timeout, scanner error, ping failure) and no tokens were received. The existing retry loop picks it up and tries the next available channel automatically. Closes #2989, addresses #357, #1562
|
Closing: no longer maintaining these against upstream. |
📝 变更描述 / Description
When an upstream provider accepts a streaming connection (HTTP 200) but never sends any tokens, the stream eventually times out via
STREAMING_TIMEOUT. Previously this was silently treated as success (zero tokens, $0 billed) becauseStreamScannerHandlerreturned no error andshouldRetrynever triggered. Users saw a hung response, no retry, and no error log.Fix.
StreamScannerHandlernow returns a*types.NewAPIErrorwhen the stream ends with a retriable reason (timeout, scanner error, ping failure, or panic) and zero tokens were sent to the client (ReceivedResponseCount == 0). The error carries achannel:prefix and HTTP 502, whichIsChannelError/shouldRetryrecognize so the retry loop tries the next available channel automatically.If any tokens were already streamed (
ReceivedResponseCount > 0), HTTP headers and partial data are flushed and retry is impossible, so no error is returned and the request settles as-is. 502 is intentional: 504 is inAlwaysSkipRetryStatusCodes, while 502 is semantically correct for upstream-not-responding and goes through the retry path.The fix is applied uniformly across every adaptor that uses
StreamScannerHandler(OpenAI / Claude / Gemini / Baidu / Dify / xAI, plus OpenAI audio and responses paths). Tests are added inrelay/helper/stream_scanner_test.gocovering the new error contract.🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
📸 运行证明 / Proof of Work
Backend:
go build -o /dev/null .passes.relay/helper/stream_scanner_test.gopasses (covers the new error contract).Frontend:
cd web/default && bun run buildpasses (no frontend changes in this PR).Functional verification: