You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor free-model SSE rewriting into a backpressure-aware byte stream shared by chat, messages, and responses APIs.
Preserve split UTF-8 input, final unterminated events, event IDs, cancellation, and upstream errors while preventing proxy transformation of event streams.
Clone responses inside metrics and request-logging helpers so concurrent background drains do not disturb the client response.
Verification
Not manually tested; the change targets low-level Response stream behavior exercised in isolation.
Visual Changes
N/A
Reviewer Notes
Focus on stream cancellation/error propagation and response-clone timing around usage, metrics, and request logging.
The new commit (add112a6) addresses both previously reported issues: the backpressure violation (multiple enqueues per pull()) and the redundant outputController closure variable. The refactored pull() loop now correctly dequeues exactly one chunk per invocation and uses the controller parameter directly.
Previously Reported Issues — All Resolved
File
Line
Issue
Status
apps/web/src/lib/rewriteModelResponse.ts
—
WARNING: Backpressure bypass — multiple events enqueued per pull()
Other Observations (pre-existing, not introduced by this PR)
captureProxyError still clones inside after() (apps/web/src/lib/ai-gateway/llm-proxy-helpers.ts:363):response.clone() is called inside an after() callback. For error responses the body is typically small and non-streaming, so this rarely causes an issue in practice — but a streaming error body could silently fail. Pre-existing and unchanged by this PR.
Test coverage for _ChatCompletions and _Messages streaming paths: The createRewrittenSseStream shared helper is exercised by the _Responses tests, but stream-specific rewrites in the other two variants lack parallel integration tests for UTF-8 chunking, cancellation, error propagation, and final-event flush. Pre-existing and unchanged.
Files Reviewed (8 files)
apps/web/src/app/api/openrouter/[...path]/route.ts — no issues
apps/web/src/app/api/openrouter/audio/transcriptions/route.ts — no issues
apps/web/src/app/api/openrouter/embeddings/route.ts — no issues
apps/web/src/lib/ai-gateway/handleRequestLogging.ts — no issues
apps/web/src/lib/ai-gateway/llm-proxy-helpers.ts — no issues
apps/web/src/lib/ai-gateway/o11y/api-metrics.server.ts — no issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Verification
Responsestream behavior exercised in isolation.Visual Changes
N/A
Reviewer Notes
Focus on stream cancellation/error propagation and response-clone timing around usage, metrics, and request logging.