feat(agentic-server): relay streaming chat completions and meter their usage - #1768
Merged
Conversation
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The gateway assumed every successful chat response was JSON (
await upstream.json()), so any client asking forstream: truegotSyntaxError: Unexpected token 'd', "data: {"id".... That is what a real harness does by default — found while pointing DeepSeek Harness atagentic-serveras its OpenAI-compatible provider, where every turn failed./v1/chat/completionsnow branches onreq.body.stream === true:Three things worth knowing:
UsageScannerbuffers across chunk boundaries, splits on\n, skips[DONE]/keep-alives/unparseable frames, and keeps the lastusageobject — so metering still fires on a streamed turn.stream_options: { include_usage: true }is added only when the caller expressed no preference.501, not silently downgraded.resolveUpstreamUrlrewritesollama/anthropicto their native endpoints, whose stream frames are not OpenAI chunks; answering a whole JSON body to a client that asked for a stream would be worse than failing loud.res.on('close'), notreq. The request stream'sclosefires as soon as the body is consumed, so aborting on it kills every stream mid-flight (AbortError: This operation was aborted— observed before the fix). The error path alsoend()s instead of writing JSON onceres.headersSent.Verified end to end beyond the unit tests: DeepSeek Harness, running Constructive's 18 neutral
@agentic-kit/db-toolsthrough a local adapter, completed a tool-calling turn routed through this gateway, with the injected sink recordingpromptTokens/completionTokensfor the streamed turn.Link to Devin session: https://app.devin.ai/sessions/2d292e43fd2e4f34bb84605b5aa2a250
Requested by: @pyramation