From f08c2348903fcfa4dcc5c5c2617c9c47c7755dd8 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Tue, 25 Aug 2026 18:17:45 -0500 Subject: [PATCH] fix(ai): ignore SSE retry directives without ending streams (#45093) --- packages/ai/src/protocols/shared.ts | 27 +++++++++++++++++------- packages/ai/test/schema.test.ts | 32 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/packages/ai/src/protocols/shared.ts b/packages/ai/src/protocols/shared.ts index 1041624af39..757eaa472e6 100644 --- a/packages/ai/src/protocols/shared.ts +++ b/packages/ai/src/protocols/shared.ts @@ -210,10 +210,9 @@ export const errorText = (error: unknown) => { * `framing` step for Server-Sent Events. Decodes UTF-8, runs the SSE channel * decoder, optionally filters named events, and drops empty / `[DONE]` * keep-alive events so the protocol event schema sees one JSON string per - * element. The SSE channel emits a - * `Retry` control event on its error channel; we drop it here (we don't - * implement client-driven retries). Decoder failures become provider output - * errors so the public error channel stays `AIError`. + * element. Retry control events are ignored without interrupting the stream. + * Decoder failures become provider output errors so the public error channel + * stays `AIError`. */ export const sseFraming = ( bytes: Stream.Stream, @@ -221,9 +220,23 @@ export const sseFraming = ( ): Stream.Stream => bytes.pipe( Stream.decodeText(), - Stream.pipeThroughChannel(Sse.decode()), - Stream.catchTag("Retry", () => Stream.empty), - Stream.catchTag("SseError", (error) => Stream.fail(eventError("sse", error.message))), + Stream.mapAccumEffect( + () => { + const output: Sse.Event[] = [] + return { + output, + parser: Sse.makeParser((event) => { + if (event._tag === "Event") output.push(event) + }), + } + }, + (state, chunk) => + Effect.gen(function* () { + const error = state.parser.feed(chunk) + if (error) return yield* eventError("sse", error.message) + return [state, state.output.splice(0)] as const + }), + ), Stream.filter( (event) => (events === undefined || events.has(event.event)) && diff --git a/packages/ai/test/schema.test.ts b/packages/ai/test/schema.test.ts index bcade58e4e4..f0097f3a88d 100644 --- a/packages/ai/test/schema.test.ts +++ b/packages/ai/test/schema.test.ts @@ -102,6 +102,38 @@ describe("AI.Usage", () => { expect(error.reason._tag).toBe("InvalidProviderOutput") }) + test("sseFraming ignores retry directives without ending the stream", async () => { + const encoder = new TextEncoder() + const frames = await Effect.runPromise( + ProviderShared.sseFraming( + Stream.make( + encoder.encode("retry: 1000\n\n"), + encoder.encode('data: {"first":true}\n\n'), + encoder.encode("retry: 2000\n\n"), + encoder.encode('data: {"second":true}\n\n'), + ).pipe(Stream.rechunk(1)), + ).pipe(Stream.runCollect), + ) + + expect(Array.from(frames)).toEqual(['{"first":true}', '{"second":true}']) + }) + + test("sseFraming preserves event data around retry directives", async () => { + const encoder = new TextEncoder() + const frames = await Effect.runPromise( + ProviderShared.sseFraming( + Stream.make( + encoder.encode("event: update\ndata: first\n"), + encoder.encode("retry: 1000\n"), + encoder.encode("data: second\n\n"), + ).pipe(Stream.rechunk(1)), + new Set(["update"]), + ).pipe(Stream.runCollect), + ) + + expect(Array.from(frames)).toEqual(["first\nsecond"]) + }) + test("visibleOutputTokens clamps reasoning > output to zero", () => { expect(new Usage({ outputTokens: 10, reasoningTokens: 4 }).visibleOutputTokens).toBe(6) expect(new Usage({ outputTokens: 10 }).visibleOutputTokens).toBe(10)