Skip to content

Commit d2ed3f1

Browse files
committed
fix(google): clear gemini first-response deadline
1 parent 9235289 commit d2ed3f1

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

‎extensions/google/transport-stream.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ function buildSseResponse(events: unknown[]): Response {
8686
});
8787
}
8888

89+
function buildDelayedSecondSseResponse(params: {
90+
first: unknown;
91+
second: unknown;
92+
delayMs: number;
93+
}): Response {
94+
const encoder = new TextEncoder();
95+
const first = `data: ${JSON.stringify(params.first)}\n\n`;
96+
const second = `data: ${JSON.stringify(params.second)}\n\ndata: [DONE]\n\n`;
97+
let timeout: ReturnType<typeof setTimeout> | undefined;
98+
const body = new ReadableStream<Uint8Array>({
99+
start(controller) {
100+
controller.enqueue(encoder.encode(first));
101+
timeout = setTimeout(() => {
102+
controller.enqueue(encoder.encode(second));
103+
controller.close();
104+
}, params.delayMs);
105+
},
106+
cancel() {
107+
if (timeout) {
108+
clearTimeout(timeout);
109+
}
110+
},
111+
});
112+
return new Response(body, {
113+
status: 200,
114+
headers: { "content-type": "text/event-stream" },
115+
});
116+
}
117+
89118
describe("google transport stream", () => {
90119
beforeAll(async () => {
91120
({
@@ -334,6 +363,40 @@ describe("google transport stream", () => {
334363
expect(retryBody.tools).toEqual(firstBody.tools);
335364
});
336365

366+
it("keeps streaming after the first Gemini 3 chunk arrives before the retry deadline", async () => {
367+
vi.stubEnv("OPENCLAW_GOOGLE_GEMINI_FIRST_RESPONSE_RETRY_MS", "10");
368+
guardedFetchMock.mockResolvedValueOnce(
369+
buildDelayedSecondSseResponse({
370+
first: {
371+
candidates: [{ content: { parts: [{ text: "first " }] } }],
372+
},
373+
second: {
374+
candidates: [{ content: { parts: [{ text: "second" }] }, finishReason: "STOP" }],
375+
},
376+
delayMs: 25,
377+
}),
378+
);
379+
380+
const model = buildGeminiModel({
381+
id: "gemini-3.1-pro-preview",
382+
name: "Gemini 3.1 Pro Preview",
383+
});
384+
const streamFn = createGoogleGenerativeAiTransportStreamFn();
385+
const stream = await Promise.resolve(
386+
streamFn(
387+
model,
388+
{
389+
messages: [{ role: "user", content: "hello", timestamp: 0 }],
390+
} as never,
391+
{ reasoning: "high" } as never,
392+
),
393+
);
394+
const result = await stream.result();
395+
396+
expect(result.content).toEqual([{ type: "text", text: "first second" }]);
397+
expect(guardedFetchMock).toHaveBeenCalledTimes(1);
398+
});
399+
337400
it("uses bearer auth when the Google api key is an OAuth JSON payload", async () => {
338401
guardedFetchMock.mockResolvedValueOnce(buildSseResponse([]));
339402

‎extensions/google/transport-stream.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,13 +752,18 @@ function createChildSignal(parent: AbortSignal | undefined, timeoutMs: number) {
752752
}, timeoutMs);
753753
timeout.unref?.();
754754
}
755+
const clearDeadline = () => {
756+
if (timeout) {
757+
clearTimeout(timeout);
758+
timeout = undefined;
759+
}
760+
};
755761
return {
756762
signal: controller.signal,
757763
timedOut: () => timedOut,
764+
clearDeadline,
758765
cleanup: () => {
759-
if (timeout) {
760-
clearTimeout(timeout);
761-
}
766+
clearDeadline();
762767
parent?.removeEventListener("abort", abortFromParent);
763768
},
764769
};
@@ -819,6 +824,7 @@ async function openGoogleSseAttempt(params: {
819824
const chunks = parseGoogleSseChunks(response, signal);
820825
const iterator = chunks[Symbol.asyncIterator]();
821826
const first = await iterator.next();
827+
attemptSignal?.clearDeadline();
822828
if (first.done) {
823829
return {
824830
type: "ready",

0 commit comments

Comments
 (0)