Repository navigation
fix(core): stream chat-compression side-query to survive gateway timeout #5865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
374f994
7de4967
91e364a
d7a4ca1
0a15181
4ef0402
eccb533
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,14 @@ export interface GenerateTextOptions { | |
| * The maximum number of attempts for the request. | ||
| */ | ||
| maxAttempts?: number; | ||
| /** | ||
|
doudouOUC marked this conversation as resolved.
Resolved
|
||
| * Stream the response instead of awaiting the whole non-streaming body. | ||
| * Defaults to `false` (unchanged non-streaming behavior). Opt in to keep the | ||
| * HTTP connection alive against BFF gateways whose `proxy_read_timeout` would | ||
| * otherwise kill a slow inference before the first byte arrives. The streamed | ||
| * deltas are collected into the same `{ text, usage }` result. | ||
| */ | ||
| stream?: boolean; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -325,6 +333,7 @@ export class BaseLlmClient { | |
| systemInstruction, | ||
| promptId, | ||
| maxAttempts, | ||
| stream, | ||
| } = options; | ||
|
|
||
| const requestConfig: GenerateContentConfig = { | ||
|
|
@@ -341,15 +350,46 @@ export class BaseLlmClient { | |
| } = await this.resolveForModel(model); | ||
|
|
||
| try { | ||
| const apiCall = () => | ||
| contentGenerator.generateContent( | ||
| { | ||
| model: requestModel, | ||
| config: requestConfig, | ||
| contents, | ||
| }, | ||
| promptId ?? '', | ||
| ); | ||
| const request = { | ||
| model: requestModel, | ||
| config: requestConfig, | ||
| contents, | ||
| }; | ||
|
|
||
| // Both branches resolve to the same `{ text, usage }` shape so a single | ||
| // retryWithBackoff governs the whole request (a mid-stream failure retries | ||
| // the entire call — side queries are idempotent). Streaming keeps the HTTP | ||
| // connection alive so a slow inference can't be killed by a gateway's | ||
| // `proxy_read_timeout`; non-streaming is the unchanged default. | ||
| const apiCall: () => Promise<GenerateTextResult> = stream | ||
| ? async () => { | ||
| const responseStream = await contentGenerator.generateContentStream( | ||
| request, | ||
| promptId ?? '', | ||
| ); | ||
| // Chunks are deltas, not cumulative snapshots, so concatenate. | ||
| // getResponseText already drops thought parts; usageMetadata rides | ||
| // the final chunk (last one wins), matching the non-streaming read. | ||
| let text = ''; | ||
| let usage: GenerateContentResponseUsageMetadata | undefined; | ||
| for await (const chunk of responseStream) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks — I checked this carefully. Two parts of the observation are correct, but the severity premise isn't, so I've corrected the PR description rather than adding the wrapper. Correct: But the motivating path is not Gemini. The DataWorks ACP / DQ_AGENT BFF is OpenAI-compatible — issue #5861's own trace shows For native Changed: corrected the PR description's risk note, which overclaimed a universal 120s watchdog — it's now scoped to the OpenAI/Qwen pipeline, with the Gemini/Anthropic contract spelled out. Not changed: I didn't wrap 🤖 Addressed by Qwen Code |
||
| text += getResponseText(chunk) ?? ''; | ||
| if (chunk.usageMetadata) { | ||
| usage = chunk.usageMetadata; | ||
| } | ||
| } | ||
| return { text, usage }; | ||
| } | ||
| : async () => { | ||
| const result = await contentGenerator.generateContent( | ||
| request, | ||
| promptId ?? '', | ||
| ); | ||
| return { | ||
| text: getResponseText(result) ?? '', | ||
| usage: result.usageMetadata, | ||
| }; | ||
| }; | ||
|
|
||
| const result = await retryWithBackoff(apiCall, { | ||
| maxAttempts: maxAttempts ?? DEFAULT_MAX_ATTEMPTS, | ||
|
|
@@ -379,8 +419,8 @@ export class BaseLlmClient { | |
| }); | ||
|
|
||
| return { | ||
| text: (getResponseText(result) ?? '').trim(), | ||
| usage: result.usageMetadata, | ||
| text: result.text.trim(), | ||
| usage: result.usage, | ||
| }; | ||
| } catch (error) { | ||
| if (abortSignal.aborted) { | ||
|
|
@@ -389,7 +429,9 @@ export class BaseLlmClient { | |
|
|
||
| await reportError( | ||
| error, | ||
| 'Error generating text content via API.', | ||
| // Mark streaming failures so an oncall can tell a mid-stream error | ||
| // apart from the original non-streaming gateway timeout (#5861). | ||
| `Error generating text content via API${stream ? ' [streaming]' : ''}.`, | ||
| contents, | ||
| 'generateText-api', | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.