Skip to content

Commit 4a53028

Browse files
authored
fix(ai): tolerate unknown and untyped stream events (#56286)
1 parent 1cb7bca commit 4a53028

10 files changed

Lines changed: 178 additions & 31 deletions

‎packages/ai/src/protocols/anthropic-messages.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
type ToolDefinition,
2626
type ToolResultPart,
2727
} from "../schema/index.js"
28-
import { JsonObject, knownString, optionalArray, optionalNull, ProviderShared } from "./shared.js"
28+
import { JsonObject, knownString, optionalArray, optionalNull, ProviderShared, withUntypedFrames } from "./shared.js"
2929
import { classifyProviderFailure } from "../provider-error.js"
3030
import { effortUpdate, resolveEffortUpdates } from "../effort-updates.js"
3131
import * as Cache from "./utils/cache.js"
@@ -467,7 +467,7 @@ const AnthropicStreamDelta = Schema.StructWithRest(
467467
type AnthropicStreamDelta = Schema.Schema.Type<typeof AnthropicStreamDelta>
468468
const decodeAnthropicStreamDelta = Schema.decodeUnknownOption(AnthropicStreamDelta)
469469

470-
const AnthropicEvent = Schema.StructWithRest(
470+
const TypedAnthropicEvent = Schema.StructWithRest(
471471
Schema.Struct({
472472
type: Schema.String,
473473
index: Schema.optional(Schema.Number),
@@ -490,6 +490,7 @@ const AnthropicEvent = Schema.StructWithRest(
490490
}),
491491
[JsonObject],
492492
)
493+
const AnthropicEvent = withUntypedFrames(TypedAnthropicEvent)
493494
type AnthropicEvent = Schema.Schema.Type<typeof AnthropicEvent>
494495

495496
interface ParserState {

‎packages/ai/src/protocols/cohere-chat.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Effect, Schema } from "effect"
1+
import { Effect, Schema, SchemaGetter } from "effect"
22
import { Route } from "../route/client.js"
33
import { Endpoint } from "../route/endpoint.js"
44
import { Framing } from "../route/framing.js"
@@ -75,6 +75,28 @@ const NativeUsage = Schema.Struct({
7575
billed_units: Schema.optional(TokenCounts),
7676
cached_tokens: Schema.optional(Schema.Number),
7777
})
78+
const KNOWN_EVENT_TYPES = new Set([
79+
"message-start",
80+
"content-start",
81+
"content-delta",
82+
"content-end",
83+
"tool-plan-delta",
84+
"tool-call-start",
85+
"tool-call-delta",
86+
"tool-call-end",
87+
"message-end",
88+
"citation-start",
89+
"citation-end",
90+
])
91+
// Cohere may add stream event types. Unknown types are ignored, while a malformed known type still fails decoding.
92+
const UnknownEvent = Schema.Struct({
93+
type: Schema.String.check(Schema.makeFilter((type) => !KNOWN_EVENT_TYPES.has(type))),
94+
}).pipe(
95+
Schema.decodeTo(Schema.Struct({ type: Schema.Literal("unknown") }), {
96+
decode: SchemaGetter.transform(() => ({ type: "unknown" as const })),
97+
encode: SchemaGetter.forbidden(() => "Unknown Cohere stream events are decode-only"),
98+
}),
99+
)
78100
const Event = Schema.Union([
79101
Schema.Struct({ type: Schema.Literal("message-start") }),
80102
Schema.Struct({
@@ -110,6 +132,7 @@ const Event = Schema.Union([
110132
}),
111133
// Citation output is outside this basic chat surface.
112134
Schema.Struct({ type: Schema.Literals(["citation-start", "citation-end"]) }),
135+
UnknownEvent,
113136
])
114137
type Event = typeof Event.Type
115138
type State = {

‎packages/ai/src/protocols/google-interactions.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Effect, Schema } from "effect"
1+
import { Effect, Schema, SchemaGetter } from "effect"
22
import { Route } from "../route/client.js"
33
import { Auth } from "../route/auth.js"
44
import { Endpoint } from "../route/endpoint.js"
@@ -148,6 +148,24 @@ const Interaction = Schema.StructWithRest(
148148
}),
149149
[Schema.Record(Schema.String, Schema.Unknown)],
150150
)
151+
const KNOWN_EVENT_TYPES = new Set([
152+
"step.start",
153+
"step.delta",
154+
"step.stop",
155+
"interaction.created",
156+
"interaction.status_update",
157+
"interaction.completed",
158+
"error",
159+
])
160+
// Google may add stream event types. Unknown types are ignored, while a malformed known type still fails decoding.
161+
const UnknownEvent = Schema.Struct({
162+
event_type: Schema.String.check(Schema.makeFilter((type) => !KNOWN_EVENT_TYPES.has(type))),
163+
}).pipe(
164+
Schema.decodeTo(Schema.Struct({ event_type: Schema.Literal("unknown") }), {
165+
decode: SchemaGetter.transform(() => ({ event_type: "unknown" as const })),
166+
encode: SchemaGetter.forbidden(() => "Unknown Google Interactions stream events are decode-only"),
167+
}),
168+
)
151169
const Event = Schema.Union([
152170
Schema.Struct({ event_type: Schema.Literal("step.start"), index: Schema.Number, step: OutputStep }),
153171
Schema.Struct({ event_type: Schema.Literal("step.delta"), index: Schema.Number, delta: Delta }),
@@ -163,6 +181,7 @@ const Event = Schema.Union([
163181
}),
164182
Schema.Struct({ event_type: Schema.Literal("interaction.completed"), interaction: Interaction }),
165183
Schema.Struct({ event_type: Schema.Literal("error"), error: Schema.Unknown }),
184+
UnknownEvent,
166185
])
167186
type Event = typeof Event.Type
168187

@@ -475,6 +494,7 @@ const step = Effect.fnUntraced(function* (state: ParserState, event: Event) {
475494
return yield* onStop(state, event.index)
476495
case "interaction.created":
477496
case "interaction.status_update":
497+
case "unknown":
478498
return [state, []] satisfies StepResult
479499
case "error":
480500
return yield* new AIError({

‎packages/ai/src/protocols/open-responses.ts‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
type ToolResultPart,
1919
} from "../schema/index.js"
2020
import type { Media } from "../media.js"
21-
import { JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared.js"
21+
import { JsonObject, optionalArray, optionalNull, ProviderShared, withUntypedFrames } from "./shared.js"
2222
import { classifyProviderFailure } from "../provider-error.js"
2323
import { effortUpdate } from "../effort-updates.js"
2424
import { OpenResponsesOptions } from "./utils/open-responses-options.js"
@@ -351,7 +351,7 @@ export type OutputItem = StreamItem & { readonly id: string; readonly namespace?
351351
const asText = (value: unknown) =>
352352
typeof value === "string" && value.length > 0 ? value : typeof value === "number" ? String(value) : undefined
353353

354-
export const Event = Schema.StructWithRest(
354+
const TypedEvent = Schema.StructWithRest(
355355
Schema.Struct({
356356
type: Schema.String,
357357
delta: Schema.optional(Schema.String),
@@ -383,27 +383,12 @@ export const Event = Schema.StructWithRest(
383383
}),
384384
[Schema.Record(Schema.String, Schema.Unknown)],
385385
)
386+
export const Event = withUntypedFrames(TypedEvent)
386387
export type Event = Schema.Schema.Type<typeof Event>
387388
export type NormalizedEvent = Event & { readonly item?: OutputItem | null }
388389

389-
const decodeEventValue = Schema.decodeUnknownEffect(Event)
390-
const decodeFrame = Schema.decodeUnknownEffect(ProviderShared.Json)
391-
392-
/**
393-
* Decodes one WebSocket frame. Some providers and gateways answer a rejected `response.create` with a bare
394-
* `{ "error": ... }` envelope and no event type; that reads as an error event so it classifies instead of
395-
* failing decoding.
396-
*/
397-
export const decodeChannelEvent = (frame: string) =>
398-
decodeFrame(frame).pipe(
399-
Effect.flatMap((value) =>
400-
decodeEventValue(
401-
ProviderShared.isRecord(value) && value.type === undefined && value.error != null
402-
? { ...value, type: "error" }
403-
: value,
404-
),
405-
),
406-
)
390+
/** Decodes one WebSocket frame. */
391+
export const decodeChannelEvent = Schema.decodeUnknownEffect(Protocol.jsonEvent(Event))
407392

408393
export interface ProviderAdapter {
409394
readonly id: string

‎packages/ai/src/protocols/shared.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Tool } from "@opencode/schema/tool"
2-
import { Effect, Option, Schema } from "effect"
2+
import { Effect, Option, Schema, SchemaGetter } from "effect"
33
import { Headers, HttpClientRequest } from "effect/http"
44
import { Media } from "../media.js"
55
import {
@@ -32,6 +32,22 @@ export const lenient = <const S extends Schema.Top>(schema: S) =>
3232
Schema.optionalKey(
3333
Schema.UndefinedOr(schema).pipe(Schema.catchDecoding(() => Effect.succeed(Option.some(undefined)))),
3434
)
35+
/**
36+
* Accepts stream frames that carry no `type`, alongside the typed event schema. Gateways inject such frames as
37+
* keep-alives, so they decode as an ignored event; one with an `error` still decodes as an error event.
38+
*/
39+
export const withUntypedFrames = <const S extends Schema.Codec<{ readonly type: string }, { readonly type: string }>>(
40+
event: S,
41+
) =>
42+
Schema.Union([
43+
event,
44+
JsonObject.check(Schema.makeFilter((frame) => !("type" in frame))).pipe(
45+
Schema.decodeTo(event, {
46+
decode: SchemaGetter.transform((frame) => ({ ...frame, type: frame.error != null ? "error" : "ignored" })),
47+
encode: SchemaGetter.forbidden(() => "Untyped stream frames are decode-only"),
48+
}),
49+
),
50+
])
3551
/** Provider-defined string enum: known values for autocomplete, any string accepted at runtime. */
3652
export const knownString = <Known extends string>() =>
3753
Schema.declare<OpenString<Known>>((value): value is OpenString<Known> => typeof value === "string", {

‎packages/ai/test/provider/anthropic-messages.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,34 @@ describe("Anthropic Messages route", () => {
19661966
}),
19671967
)
19681968

1969+
it.effect("ignores untyped stream frames but reads untyped error envelopes as errors", () =>
1970+
Effect.gen(function* () {
1971+
const response = yield* LLMClient.generate(request).pipe(
1972+
Effect.provide(
1973+
fixedResponse(
1974+
sseEvents(
1975+
{},
1976+
{ type: "message_start", message: { usage: { input_tokens: 5 } } },
1977+
{ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
1978+
{ gateway: "keep-alive" },
1979+
{ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "OK" } },
1980+
{ type: "content_block_stop", index: 0 },
1981+
{ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } },
1982+
{ type: "message_stop" },
1983+
),
1984+
),
1985+
),
1986+
)
1987+
expect(response.text).toBe("OK")
1988+
1989+
const error = yield* LLMClient.generate(request).pipe(
1990+
Effect.provide(fixedResponse(sseEvents({ error: { type: "overloaded_error", message: "Overloaded" } }))),
1991+
Effect.flip,
1992+
)
1993+
expect(error).toMatchObject({ reason: { _tag: "ProviderInternal" }, message: "overloaded_error: Overloaded" })
1994+
}),
1995+
)
1996+
19691997
it.effect("classifies prompt-too-long provider errors", () =>
19701998
Effect.gen(function* () {
19711999
const error = yield* LLMClient.generate(request).pipe(

‎packages/ai/test/provider/cohere.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,30 @@ it.effect("Cohere rejects incomplete streams", () =>
133133
}),
134134
)
135135

136+
it.effect("Cohere ignores unknown stream events but rejects malformed known ones", () =>
137+
Effect.gen(function* () {
138+
const generate = (...events: ReadonlyArray<unknown>) =>
139+
LLMClient.generate(LLM.request({ model: cohere.model("command-a-03-2025"), prompt: "Hi" })).pipe(
140+
Effect.provide(fixedResponse(sseEvents(...events))),
141+
)
142+
const response = yield* generate(
143+
{ type: "message-start" },
144+
{ type: "debug", detail: "new event type" },
145+
{ type: "content-delta", index: 0, delta: { message: { content: { text: "OK" } } } },
146+
{ type: "content-end", index: 0 },
147+
{ type: "message-end", delta: { finish_reason: "COMPLETE" } },
148+
)
149+
expect(response.text).toBe("OK")
150+
151+
const error = yield* generate(
152+
{ type: "message-start" },
153+
{ type: "content-delta", delta: { message: { content: { text: "OK" } } } },
154+
{ type: "message-end", delta: { finish_reason: "COMPLETE" } },
155+
).pipe(Effect.flip)
156+
expect(error.reason._tag).toBe("InvalidProviderOutput")
157+
}),
158+
)
159+
136160
it.effect("Cohere preserves native tool plans in continued history", () =>
137161
Effect.gen(function* () {
138162
const response = yield* LLMClient.generate(

‎packages/ai/test/provider/google-vertex-interactions.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ describe("Google Vertex Interactions", () => {
163163
)
164164
})
165165

166+
it.effect("ignores unknown stream events but rejects malformed known ones", () =>
167+
Effect.gen(function* () {
168+
const model = GoogleVertexInteractions.configure({ apiKey: "vertex-key" }).model("gemini-3.8-flash")
169+
const generate = (...events: ReadonlyArray<unknown>) =>
170+
LLMClient.generate(LLM.request({ model, prompt: "Hi" })).pipe(
171+
Effect.provide(fixedResponse(sseEvents(...events))),
172+
)
173+
const response = yield* generate(
174+
{ event_type: "step.start", index: 0, step: { type: "model_output" } },
175+
{ event_type: "interaction.heartbeat" },
176+
{ event_type: "step.delta", index: 0, delta: { type: "text", text: "OK" } },
177+
{ event_type: "step.stop", index: 0 },
178+
{ event_type: "interaction.completed", interaction: { status: "completed" } },
179+
)
180+
expect(response.text).toBe("OK")
181+
182+
const error = yield* generate(
183+
{ event_type: "step.delta", delta: { type: "text", text: "OK" } },
184+
{ event_type: "interaction.completed", interaction: { status: "completed" } },
185+
).pipe(Effect.flip)
186+
expect(error.reason._tag).toBe("InvalidProviderOutput")
187+
}),
188+
)
189+
166190
it.effect("round-trips tool signatures under Vertex metadata without enabling storage", () =>
167191
Effect.gen(function* () {
168192
const model = GoogleVertexInteractions.configure({

‎packages/ai/test/provider/open-responses-errors.test.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ it.effect("decodes error frames verbatim in shared SSE and WebSocket decoding",
3535
}),
3636
)
3737

38-
it.effect("reads bare WebSocket error envelopes as error events", () =>
38+
it.effect("reads untyped error envelopes as error events and ignores other untyped frames", () =>
3939
Effect.gen(function* () {
4040
const message = "gRPC error: Response with id=resp_missing not found"
41-
for (const error of [{ type: "api_error", message }, message, 42]) {
42-
expect(yield* OpenResponses.decodeChannelEvent(JSON.stringify({ error }))).toEqual({ type: "error", error })
43-
}
44-
for (const frame of [{ error: null }, { message }]) {
45-
expect(yield* OpenResponses.decodeChannelEvent(JSON.stringify(frame)).pipe(Effect.flip)).toBeDefined()
41+
for (const decode of [decodeEvent, OpenResponses.decodeChannelEvent]) {
42+
for (const error of [{ type: "api_error", message }, message, 42]) {
43+
expect(yield* decode(JSON.stringify({ error }))).toEqual({ type: "error", error })
44+
}
45+
for (const frame of [{}, { error: null }, { message }]) {
46+
expect(yield* decode(JSON.stringify(frame))).toEqual({ ...frame, type: "ignored" })
47+
}
4648
}
4749
}),
4850
)

‎packages/ai/test/provider/openai-responses.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,30 @@ describe("OpenAI Responses route", () => {
608608
}),
609609
)
610610

611+
it.effect("ignores untyped SSE frames", () =>
612+
Effect.gen(function* () {
613+
const response = yield* LLMClient.generate(request).pipe(
614+
Effect.provide(
615+
fixedResponse(
616+
sseEvents(
617+
{},
618+
{ type: "response.created", response: { id: "resp_1" } },
619+
{ type: "response.output_item.added", item: { type: "message", id: "msg_1" } },
620+
{ type: "response.output_text.delta", item_id: "msg_1", delta: "PO" },
621+
{ gateway: "keep-alive" },
622+
{ type: "response.output_text.delta", item_id: "msg_1", delta: "NG" },
623+
{ type: "response.output_item.done", item: { type: "message", id: "msg_1" } },
624+
{ type: "response.completed", response: { id: "resp_1" } },
625+
),
626+
),
627+
),
628+
)
629+
630+
expect(response.text).toBe("PONG")
631+
expect(response.finishReason?.normalized).toBe("stop")
632+
}),
633+
)
634+
611635
it.effect("continues a streamed tool call with only the new tool output", () =>
612636
Effect.forEach([undefined, []], (output) =>
613637
Effect.gen(function* () {

0 commit comments

Comments
 (0)