diff --git a/packages/ai/AGENTS.md b/packages/ai/AGENTS.md index d5a33c4875e..95e53b27a79 100644 --- a/packages/ai/AGENTS.md +++ b/packages/ai/AGENTS.md @@ -3,6 +3,7 @@ ## Principles - **Forward compatibility.** Providers ship new models, options, and values constantly. Accept unknown pass-through values (`Known | (string & {})` for string options); use closed literals only when OpenCode branches on the value. +- **Tolerate unknowns.** Unknown provider events, statuses, and enum values must not fail a request; ignore them or pass them through. Fail only on errors in the provider's documented shape, malformed known types, or a missing terminal event. - **One canonical model.** The Schema classes in `src/schema/` are the data model. Do not add a parallel shape or a second construction path. - **Independent of Session.** Auth, permissions, plugins, and orchestration belong in Core. `LLM.stream`/`LLM.generate` run exactly one model call. - **Reuse protocols.** Providers compose existing protocols with `Route.make(...)` instead of cloning them. diff --git a/packages/ai/src/protocols/openai-images.ts b/packages/ai/src/protocols/openai-images.ts index 26794d6ce85..a38add77761 100644 --- a/packages/ai/src/protocols/openai-images.ts +++ b/packages/ai/src/protocols/openai-images.ts @@ -1,4 +1,4 @@ -import { Effect, Schema, Stream } from "effect" +import { Effect, Schema, SchemaGetter, Stream } from "effect" import { ImageFinishEvent, ImageModel, @@ -73,6 +73,22 @@ const OpenAIImageResponse = Schema.Struct({ // 3. Streaming event schema // --------------------------------------------------------------------------- +const EVENT_TYPES = new Set([ + "image_generation.partial_image", + "image_edit.partial_image", + "image_generation.completed", + "image_edit.completed", + "error", +]) +/** OpenAI may add stream event types; frames outside `EVENT_TYPES` are ignored, while a malformed known type still fails decoding. */ +const UnknownEvent = Schema.Struct({ + type: Schema.String.check(Schema.makeFilter((type) => !EVENT_TYPES.has(type))), +}).pipe( + Schema.decodeTo(Schema.Struct({ type: Schema.Literal("unknown"), rawType: Schema.String }), { + decode: SchemaGetter.transform((event) => ({ type: "unknown" as const, rawType: event.type })), + encode: SchemaGetter.forbidden(() => "Unknown OpenAI Images stream events are decode-only"), + }), +) const StreamEvent = Schema.Union([ Schema.Struct({ type: Schema.Literals(["image_generation.partial_image", "image_edit.partial_image"]), @@ -92,6 +108,7 @@ const StreamEvent = Schema.Union([ type: Schema.Literal("error"), error: Schema.optional(Schema.Struct({ message: Schema.optional(Schema.String) })), }), + UnknownEvent, ]) const decodeEvent = route.decodeFrame(StreamEvent) @@ -254,6 +271,7 @@ const eventImage = (frame: string, label: string, data: string, format: string, const onEvent = Effect.fnUntraced(function* (state: State, frame: string) { const event = yield* decodeEvent(frame) + if (event.type === "unknown") return [state, []] as const if (event.type === "error") return yield* new AIError({ reason: classifyProviderFailure({ diff --git a/packages/ai/test/image.test.ts b/packages/ai/test/image.test.ts index 1152a4c145b..998bd37150e 100644 --- a/packages/ai/test/image.test.ts +++ b/packages/ai/test/image.test.ts @@ -201,6 +201,36 @@ describe("Image", () => { ), ) + it.effect("ignores unknown OpenAI image stream events", () => + Effect.gen(function* () { + const events = yield* Image.stream({ + model: OpenAI.configure({ apiKey: "test", baseURL: "https://openai.test/v1" }).image("gpt-image-1-mini"), + prompt: "A lighthouse", + }).pipe(Stream.runCollect) + expect(Array.from(events).map((event) => event.type)).toEqual(["image", "finish"]) + }).pipe( + Effect.provide( + layer((input) => + Effect.succeed( + input.respond( + [ + { type: "image_generation.in_progress", sequence_number: 0 }, + { + type: "image_generation.completed", + b64_json: Buffer.from([0x89, 0x50, 0x4e, 0x47]).toString("base64"), + output_format: "png", + }, + ] + .map((event) => `data: ${JSON.stringify(event)}\n\n`) + .join(""), + { headers: { "content-type": "text/event-stream" } }, + ), + ), + ), + ), + ), + ) + it.effect("lowers aspect to the nearest OpenAI size and reports the substitution", () => Effect.gen(function* () { const openai = OpenAI.configure({ apiKey: "test", baseURL: "https://openai.test/v1" })