@effect-uai/ai-sdk 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,5 @@
1
1
  import * as Items from "@effect-uai/core/Items";
2
2
  import { Schema } from "effect";
3
-
4
- //#region src/Messages.d.ts
5
3
  declare namespace Messages_d_exports {
6
4
  export { FilePart, TextPart, ToolPart, UIMessage, UIPart, decodeMessages };
7
5
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Messages.d.mts","names":[],"sources":["../src/Messages.ts"],"mappings":";;;;;;;cAmBM,QAAA,EAAQ,MAAA,CAAA,MAAA;EAAA;;;KAIF,QAAA,UAAkB,QAAA,CAAS,IAAA;AAAA,cAEjC,QAAA,EAAQ,MAAA,CAAA,MAAA;EAAA;;;;KAKF,QAAA,UAAkB,QAAA,CAAS,IAAA;AAAA,cAEjC,QAAA,EAAQ,MAAA,CAAA,MAAA;EAAA;;;;;;;KAQF,QAAA,UAAkB,QAAA,CAAS,IAAA;AAAA,KAS3B,MAAA;EAAA,SACD,IAAA;EAAA,UACC,GAAA;AAAA;AAAA,KAGA,SAAA;EAAA,SACD,EAAA;EAAA,SACA,IAAA;EAAA,SACA,KAAA,EAAO,aAAA,CAAc,MAAA;AAAA;AAAA,cA8DnB,cAAA,GACX,QAAA,EAAU,aAAA,CAAc,SAAA,MACvB,aAAA,CAAc,KAAA,CAAM,WAAA"}
1
+ {"version":3,"file":"Messages.d.mts","names":[],"sources":["../src/Messages.ts"],"mappings":";;;;;cAmBM,UAAQ,OAAA;;;;KAIF,kBAAkB,SAAS;cAEjC,UAAQ,OAAA;;;;;KAKF,kBAAkB,SAAS;cAEjC,UAAQ,OAAA;;;;;;;;KAQF,kBAAkB,SAAS;KAS3B;WACD;YACC;;KAGA;WACD;WACA;WACA,OAAO,cAAc;;cA8DnB,iBAAc,UACf,cAAc,eACvB,cAAc,MAAM"}
package/dist/Messages.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
2
  import * as Image from "@effect-uai/core/Image";
3
3
  import * as Items from "@effect-uai/core/Items";
4
4
  import { Match, Schema } from "effect";
@@ -1 +1 @@
1
- {"version":3,"file":"Messages.mjs","names":[],"sources":["../src/Messages.ts"],"sourcesContent":["/**\n * Decode the `messages` array a `useChat` client POSTs (Vercel's `UIMessage`\n * shape) back into effect-uai `HistoryItem`s, so an existing loop can run\n * against an unchanged frontend.\n *\n * One `UIMessage` can expand to several history items: an assistant message\n * carrying tool calls becomes an assistant `Message` followed by the\n * `ToolCall` / `ToolCallOutput` items the model needs to see. Text and image\n * (`file` with an `image/*` media type) parts fold into a message's content;\n * non-image files and unresolved tool states are dropped.\n */\nimport * as Image from \"@effect-uai/core/Image\"\nimport * as Items from \"@effect-uai/core/Items\"\nimport { Match, Schema } from \"effect\"\n\n// The inbound part variants we understand. Tool parts are identified\n// structurally by `toolCallId` - their wire `type` is a dynamic `tool-<name>`\n// (or `dynamic-tool`) rather than a fixed literal - so their schema keeps\n// `type` open and reads the name off it.\nconst TextPart = Schema.Struct({\n type: Schema.Literal(\"text\"),\n text: Schema.String,\n})\nexport type TextPart = typeof TextPart.Type\n\nconst FilePart = Schema.Struct({\n type: Schema.Literal(\"file\"),\n mediaType: Schema.String,\n url: Schema.String,\n})\nexport type FilePart = typeof FilePart.Type\n\nconst ToolPart = Schema.Struct({\n type: Schema.String,\n toolCallId: Schema.String,\n toolName: Schema.optional(Schema.String),\n state: Schema.optional(Schema.String),\n input: Schema.optional(Schema.Unknown),\n output: Schema.optional(Schema.Unknown),\n})\nexport type ToolPart = typeof ToolPart.Type\n\nconst isText = Schema.is(TextPart)\nconst isFile = Schema.is(FilePart)\nconst isTool = Schema.is(ToolPart)\nconst isImageFile = (part: FilePart): boolean => part.mediaType.startsWith(\"image\")\n\n// A raw inbound part: a discriminating `type` plus whatever else the client\n// attached. Kept open so the schema guards do the narrowing.\nexport type UIPart = {\n readonly type: string\n readonly [key: string]: unknown\n}\n\nexport type UIMessage = {\n readonly id?: string\n readonly role: \"user\" | \"assistant\" | \"system\"\n readonly parts: ReadonlyArray<UIPart>\n}\n\nconst textOf = (message: UIMessage): string =>\n message.parts\n .filter(isText)\n .map((part) => part.text)\n .join(\"\")\n\nconst imageBlocks = (message: UIMessage): ReadonlyArray<Items.InputImage> =>\n message.parts\n .filter(isFile)\n .filter(isImageFile)\n .map((part) => ({ type: \"input_image\", source: Image.imageUrl(part.url, part.mediaType) }))\n\n// A user turn: one joined text block (if any) followed by one image block per\n// image file, preserving the model's expectation of text-then-media.\nconst userMessage = (message: UIMessage): Items.Message => {\n const text = textOf(message)\n return {\n type: \"message\",\n role: \"user\",\n content: [\n ...(text.length > 0 ? [{ type: \"input_text\", text } as const] : []),\n ...imageBlocks(message),\n ],\n }\n}\n\nconst toolName = (part: ToolPart): string =>\n part.type === \"dynamic-tool\" ? (part.toolName ?? \"\") : part.type.slice(\"tool-\".length)\n\n// A resolved tool call becomes a `function_call` item, plus a\n// `function_call_output` once the client carries its output.\nconst toolItems = (part: ToolPart): ReadonlyArray<Items.HistoryItem> => {\n const call: Items.ToolCall = {\n type: \"function_call\",\n call_id: part.toolCallId,\n name: toolName(part),\n arguments: JSON.stringify(part.input ?? {}),\n }\n if (part.state !== \"output-available\") return [call]\n const output = typeof part.output === \"string\" ? part.output : JSON.stringify(part.output)\n return [call, Items.toolCallOutput(part.toolCallId, output)]\n}\n\nconst assistantItems = (message: UIMessage): ReadonlyArray<Items.HistoryItem> => {\n const text = textOf(message)\n return [\n ...(text.length > 0 ? [Items.assistantText(text)] : []),\n ...message.parts.filter(isTool).flatMap(toolItems),\n ]\n}\n\nconst decodeMessage = (message: UIMessage): ReadonlyArray<Items.HistoryItem> =>\n Match.value(message.role).pipe(\n Match.when(\"user\", () => [userMessage(message)]),\n Match.when(\"system\", () => [Items.systemText(textOf(message))]),\n Match.when(\"assistant\", () => assistantItems(message)),\n Match.exhaustive,\n )\n\nexport const decodeMessages = (\n messages: ReadonlyArray<UIMessage>,\n): ReadonlyArray<Items.HistoryItem> => messages.flatMap(decodeMessage)\n"],"mappings":";;;;;;;;;;;;;;;;;AAmBA,MAAM,WAAW,OAAO,OAAO;CAC7B,MAAM,OAAO,QAAQ,OAAO;CAC5B,MAAM,OAAO;CACd,CAAC;AAGF,MAAM,WAAW,OAAO,OAAO;CAC7B,MAAM,OAAO,QAAQ,OAAO;CAC5B,WAAW,OAAO;CAClB,KAAK,OAAO;CACb,CAAC;AAGF,MAAM,WAAW,OAAO,OAAO;CAC7B,MAAM,OAAO;CACb,YAAY,OAAO;CACnB,UAAU,OAAO,SAAS,OAAO,OAAO;CACxC,OAAO,OAAO,SAAS,OAAO,OAAO;CACrC,OAAO,OAAO,SAAS,OAAO,QAAQ;CACtC,QAAQ,OAAO,SAAS,OAAO,QAAQ;CACxC,CAAC;AAGF,MAAM,SAAS,OAAO,GAAG,SAAS;AAClC,MAAM,SAAS,OAAO,GAAG,SAAS;AAClC,MAAM,SAAS,OAAO,GAAG,SAAS;AAClC,MAAM,eAAe,SAA4B,KAAK,UAAU,WAAW,QAAQ;AAenF,MAAM,UAAU,YACd,QAAQ,MACL,OAAO,OAAO,CACd,KAAK,SAAS,KAAK,KAAK,CACxB,KAAK,GAAG;AAEb,MAAM,eAAe,YACnB,QAAQ,MACL,OAAO,OAAO,CACd,OAAO,YAAY,CACnB,KAAK,UAAU;CAAE,MAAM;CAAe,QAAQ,MAAM,SAAS,KAAK,KAAK,KAAK,UAAU;CAAE,EAAE;AAI/F,MAAM,eAAe,YAAsC;CACzD,MAAM,OAAO,OAAO,QAAQ;AAC5B,QAAO;EACL,MAAM;EACN,MAAM;EACN,SAAS,CACP,GAAI,KAAK,SAAS,IAAI,CAAC;GAAE,MAAM;GAAc;GAAM,CAAU,GAAG,EAAE,EAClE,GAAG,YAAY,QAAQ,CACxB;EACF;;AAGH,MAAM,YAAY,SAChB,KAAK,SAAS,iBAAkB,KAAK,YAAY,KAAM,KAAK,KAAK,MAAM,EAAe;AAIxF,MAAM,aAAa,SAAqD;CACtE,MAAM,OAAuB;EAC3B,MAAM;EACN,SAAS,KAAK;EACd,MAAM,SAAS,KAAK;EACpB,WAAW,KAAK,UAAU,KAAK,SAAS,EAAE,CAAC;EAC5C;AACD,KAAI,KAAK,UAAU,mBAAoB,QAAO,CAAC,KAAK;CACpD,MAAM,SAAS,OAAO,KAAK,WAAW,WAAW,KAAK,SAAS,KAAK,UAAU,KAAK,OAAO;AAC1F,QAAO,CAAC,MAAM,MAAM,eAAe,KAAK,YAAY,OAAO,CAAC;;AAG9D,MAAM,kBAAkB,YAAyD;CAC/E,MAAM,OAAO,OAAO,QAAQ;AAC5B,QAAO,CACL,GAAI,KAAK,SAAS,IAAI,CAAC,MAAM,cAAc,KAAK,CAAC,GAAG,EAAE,EACtD,GAAG,QAAQ,MAAM,OAAO,OAAO,CAAC,QAAQ,UAAU,CACnD;;AAGH,MAAM,iBAAiB,YACrB,MAAM,MAAM,QAAQ,KAAK,CAAC,KACxB,MAAM,KAAK,cAAc,CAAC,YAAY,QAAQ,CAAC,CAAC,EAChD,MAAM,KAAK,gBAAgB,CAAC,MAAM,WAAW,OAAO,QAAQ,CAAC,CAAC,CAAC,EAC/D,MAAM,KAAK,mBAAmB,eAAe,QAAQ,CAAC,EACtD,MAAM,WACP;AAEH,MAAa,kBACX,aACqC,SAAS,QAAQ,cAAc"}
1
+ {"version":3,"file":"Messages.mjs","names":[],"sources":["../src/Messages.ts"],"sourcesContent":["/**\n * Decode the `messages` array a `useChat` client POSTs (Vercel's `UIMessage`\n * shape) back into effect-uai `HistoryItem`s, so an existing loop can run\n * against an unchanged frontend.\n *\n * One `UIMessage` can expand to several history items: an assistant message\n * carrying tool calls becomes an assistant `Message` followed by the\n * `ToolCall` / `ToolCallOutput` items the model needs to see. Text and image\n * (`file` with an `image/*` media type) parts fold into a message's content;\n * non-image files and unresolved tool states are dropped.\n */\nimport * as Image from \"@effect-uai/core/Image\"\nimport * as Items from \"@effect-uai/core/Items\"\nimport { Match, Schema } from \"effect\"\n\n// The inbound part variants we understand. Tool parts are identified\n// structurally by `toolCallId` - their wire `type` is a dynamic `tool-<name>`\n// (or `dynamic-tool`) rather than a fixed literal - so their schema keeps\n// `type` open and reads the name off it.\nconst TextPart = Schema.Struct({\n type: Schema.Literal(\"text\"),\n text: Schema.String,\n})\nexport type TextPart = typeof TextPart.Type\n\nconst FilePart = Schema.Struct({\n type: Schema.Literal(\"file\"),\n mediaType: Schema.String,\n url: Schema.String,\n})\nexport type FilePart = typeof FilePart.Type\n\nconst ToolPart = Schema.Struct({\n type: Schema.String,\n toolCallId: Schema.String,\n toolName: Schema.optional(Schema.String),\n state: Schema.optional(Schema.String),\n input: Schema.optional(Schema.Unknown),\n output: Schema.optional(Schema.Unknown),\n})\nexport type ToolPart = typeof ToolPart.Type\n\nconst isText = Schema.is(TextPart)\nconst isFile = Schema.is(FilePart)\nconst isTool = Schema.is(ToolPart)\nconst isImageFile = (part: FilePart): boolean => part.mediaType.startsWith(\"image\")\n\n// A raw inbound part: a discriminating `type` plus whatever else the client\n// attached. Kept open so the schema guards do the narrowing.\nexport type UIPart = {\n readonly type: string\n readonly [key: string]: unknown\n}\n\nexport type UIMessage = {\n readonly id?: string\n readonly role: \"user\" | \"assistant\" | \"system\"\n readonly parts: ReadonlyArray<UIPart>\n}\n\nconst textOf = (message: UIMessage): string =>\n message.parts\n .filter(isText)\n .map((part) => part.text)\n .join(\"\")\n\nconst imageBlocks = (message: UIMessage): ReadonlyArray<Items.InputImage> =>\n message.parts\n .filter(isFile)\n .filter(isImageFile)\n .map((part) => ({ type: \"input_image\", source: Image.imageUrl(part.url, part.mediaType) }))\n\n// A user turn: one joined text block (if any) followed by one image block per\n// image file, preserving the model's expectation of text-then-media.\nconst userMessage = (message: UIMessage): Items.Message => {\n const text = textOf(message)\n return {\n type: \"message\",\n role: \"user\",\n content: [\n ...(text.length > 0 ? [{ type: \"input_text\", text } as const] : []),\n ...imageBlocks(message),\n ],\n }\n}\n\nconst toolName = (part: ToolPart): string =>\n part.type === \"dynamic-tool\" ? (part.toolName ?? \"\") : part.type.slice(\"tool-\".length)\n\n// A resolved tool call becomes a `function_call` item, plus a\n// `function_call_output` once the client carries its output.\nconst toolItems = (part: ToolPart): ReadonlyArray<Items.HistoryItem> => {\n const call: Items.ToolCall = {\n type: \"function_call\",\n call_id: part.toolCallId,\n name: toolName(part),\n arguments: JSON.stringify(part.input ?? {}),\n }\n if (part.state !== \"output-available\") return [call]\n const output = typeof part.output === \"string\" ? part.output : JSON.stringify(part.output)\n return [call, Items.toolCallOutput(part.toolCallId, output)]\n}\n\nconst assistantItems = (message: UIMessage): ReadonlyArray<Items.HistoryItem> => {\n const text = textOf(message)\n return [\n ...(text.length > 0 ? [Items.assistantText(text)] : []),\n ...message.parts.filter(isTool).flatMap(toolItems),\n ]\n}\n\nconst decodeMessage = (message: UIMessage): ReadonlyArray<Items.HistoryItem> =>\n Match.value(message.role).pipe(\n Match.when(\"user\", () => [userMessage(message)]),\n Match.when(\"system\", () => [Items.systemText(textOf(message))]),\n Match.when(\"assistant\", () => assistantItems(message)),\n Match.exhaustive,\n )\n\nexport const decodeMessages = (\n messages: ReadonlyArray<UIMessage>,\n): ReadonlyArray<Items.HistoryItem> => messages.flatMap(decodeMessage)\n"],"mappings":";;;;;;;;;;;;;;;;;AAmBA,MAAM,WAAW,OAAO,OAAO;CAC7B,MAAM,OAAO,QAAQ,MAAM;CAC3B,MAAM,OAAO;AACf,CAAC;AAGD,MAAM,WAAW,OAAO,OAAO;CAC7B,MAAM,OAAO,QAAQ,MAAM;CAC3B,WAAW,OAAO;CAClB,KAAK,OAAO;AACd,CAAC;AAGD,MAAM,WAAW,OAAO,OAAO;CAC7B,MAAM,OAAO;CACb,YAAY,OAAO;CACnB,UAAU,OAAO,SAAS,OAAO,MAAM;CACvC,OAAO,OAAO,SAAS,OAAO,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AO,OAAO;CAC3B,OAAO,CACL,GAAI,KAAK,SAAS,IAAI,CAAC,MAAM,cAAc,IAAI,CAAC,IAAI,CAAC,GACrD,GAAG,QAAQ,MAAM,OAAO,MAAM,CAAC,CAAC,QAAQ,SAAS,CACnD;AACF;AAEA,MAAM,iBAAiB,YACrB,MAAM,MAAM,QAAQ,IAAI,CAAC,CAAC,KACxB,MAAM,KAAK,cAAc,CAAC,YAAY,OAAO,CAAC,CAAC,GAC/C,MAAM,KAAK,gBAAgB,CAAC,MAAM,WAAW,OAAO,OAAO,CAAC,CAAC,CAAC,GAC9D,MAAM,KAAK,mBAAmB,eAAe,OAAO,CAAC,GACrD,MAAM,UACR;AAEF,MAAa,kBACX,aACqC,SAAS,QAAQ,aAAa"}
@@ -1,8 +1,6 @@
1
1
  import { Stream } from "effect";
2
2
  import * as Turn from "@effect-uai/core/Turn";
3
3
  import * as SSE from "@effect-uai/core/SSE";
4
-
5
- //#region src/UIMessageStream.d.ts
6
4
  declare namespace UIMessageStream_d_exports {
7
5
  export { DataEmission, Emission, MetadataEmission, Part, dataPart, messageMetadata, responseHeaders, toUIMessageStream };
8
6
  }
@@ -1 +1 @@
1
- {"version":3,"file":"UIMessageStream.d.mts","names":[],"sources":["../src/UIMessageStream.ts"],"mappings":";;;;;;;;;;;;KAyBY,IAAA;EAAA,SACG,IAAA;EAAA,SAAwB,SAAA;AAAA;EAAA,SACxB,IAAA;EAAA,SAA6B,EAAA;AAAA;EAAA,SAC7B,IAAA;EAAA,SAA6B,EAAA;EAAA,SAAqB,KAAA;AAAA;EAAA,SAClD,IAAA;EAAA,SAA2B,EAAA;AAAA;EAAA,SAC3B,IAAA;EAAA,SAAkC,EAAA;AAAA;EAAA,SAClC,IAAA;EAAA,SAAkC,EAAA;EAAA,SAAqB,KAAA;AAAA;EAAA,SACvD,IAAA;EAAA,SAAgC,EAAA;AAAA;EAAA,SAChC,IAAA;EAAA,SAAmC,UAAA;EAAA,SAA6B,QAAA;AAAA;EAAA,SA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AGA;WACA;WACA;WACA;;WAGA;WACA;WACA;;WAGA;WACA;WACA;WACA;;WAEA;WAAmC;;WACnC;WAAwB;;WACxB;;;;;;;KAOH;WACD;WACA;WACA;WACA;WACA;;;KAIC;WACD;WACA;;;;;;KAOC,WAAW,KAAK,mBAAmB,eAAe;;cAGjD,WAAQ,cACP,eACC;WACQ;WAAsB;MAC1C;;cAGU,kBAAe,sBAAwB;;;;;;;;cAYvC,iBAAiB;;;;;;cA+GjB,oBAAiB,uBAE3B,GAAG,GAAC,MAAQ,OAAO,OAAO,UAAU,GAAG,OAAK,OAAO,OAAO,IAAI,OAAO,GAAG"}
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
2
  import { Match, Option, Stream } from "effect";
3
3
  import * as Turn from "@effect-uai/core/Turn";
4
4
  //#region src/UIMessageStream.ts
@@ -1 +1 @@
1
- {"version":3,"file":"UIMessageStream.mjs","names":[],"sources":["../src/UIMessageStream.ts"],"sourcesContent":["/**\n * Project an effect-uai loop onto the Vercel AI SDK \"UI Message Stream\"\n * protocol (the `v1` wire format, stable across AI SDK v5–v7). The output is a\n * `Stream<SSE.Event>` ready for `SSE.toBytes`; a `useChat` client on the other\n * end consumes it with no frontend changes.\n *\n * The encoder input is an `Emission`: every `InteractionEvent` the loop\n * produces, plus optional `dataPart` and `messageMetadata` emissions a caller\n * interleaves to stream custom typed data (e.g. live metrics) and settled\n * per-message metadata (e.g. usage). A plain `Stream<InteractionEvent>` is a\n * valid `Stream<Emission>`, so existing callers are unaffected.\n *\n * The protocol wants a stable id and explicit start/end lifecycle around each\n * text and reasoning block, which effect-uai's delta events don't carry. This\n * module synthesizes them: one block id per turn, opened on the first delta\n * and closed on `TurnComplete`.\n */\nimport type * as SSE from \"@effect-uai/core/SSE\"\nimport * as Turn from \"@effect-uai/core/Turn\"\nimport { Match, Option, Stream } from \"effect\"\n\n/**\n * The stream parts `useChat` consumes. Names and shapes are the wire\n * contract with the AI SDK client; keep them literal.\n */\nexport type Part =\n | { readonly type: \"start\"; readonly messageId: string }\n | { readonly type: \"text-start\"; readonly id: string }\n | { readonly type: \"text-delta\"; readonly id: string; readonly delta: string }\n | { readonly type: \"text-end\"; readonly id: string }\n | { readonly type: \"reasoning-start\"; readonly id: string }\n | { readonly type: \"reasoning-delta\"; readonly id: string; readonly delta: string }\n | { readonly type: \"reasoning-end\"; readonly id: string }\n | { readonly type: \"tool-input-start\"; readonly toolCallId: string; readonly toolName: string }\n | {\n readonly type: \"tool-input-delta\"\n readonly toolCallId: string\n readonly inputTextDelta: string\n }\n | {\n readonly type: \"tool-input-available\"\n readonly toolCallId: string\n readonly toolName: string\n readonly input: unknown\n }\n | {\n readonly type: \"tool-output-available\"\n readonly toolCallId: string\n readonly output: unknown\n }\n | {\n readonly type: `data-${string}`\n readonly id?: string\n readonly data: unknown\n readonly transient?: boolean\n }\n | { readonly type: \"message-metadata\"; readonly messageMetadata: unknown }\n | { readonly type: \"error\"; readonly errorText: string }\n | { readonly type: \"finish\" }\n\n/**\n * A custom typed data part (`data-<name>`). `transient: true` delivers it to\n * the client's `onData` callback only (not persisted into `message.parts`);\n * reuse an `id` across emissions to reconcile/update one part in place.\n */\nexport type DataEmission = {\n readonly kind: \"data\"\n readonly name: string\n readonly data: unknown\n readonly id?: string\n readonly transient?: boolean\n}\n\n/** Settled per-message metadata, surfaced as `message.metadata` on the client. */\nexport type MetadataEmission = {\n readonly kind: \"metadata\"\n readonly metadata: unknown\n}\n\n/**\n * What `toUIMessageStream` encodes: loop `InteractionEvent`s plus optional\n * data / metadata emissions a caller interleaves into the stream.\n */\nexport type Emission = Turn.InteractionEvent | DataEmission | MetadataEmission\n\n/** Construct a `data-<name>` emission. See {@link DataEmission}. */\nexport const dataPart = (\n name: string,\n data: unknown,\n options?: { readonly id?: string; readonly transient?: boolean },\n): DataEmission => ({ kind: \"data\", name, data, ...options })\n\n/** Construct a message-metadata emission. See {@link MetadataEmission}. */\nexport const messageMetadata = (metadata: unknown): MetadataEmission => ({\n kind: \"metadata\",\n metadata,\n})\n\n/**\n * Headers the UI Message Stream protocol requires on the HTTP response.\n * `x-vercel-ai-ui-message-stream: v1` is mandatory; without it the client\n * falls back to the plain text-stream protocol and ignores non-text parts.\n * Pure data, so it composes with any server (web `Response`, `@effect/platform`\n * `HttpServerResponse`, raw Node) - the package deliberately owns no HTTP layer.\n */\nexport const responseHeaders: Record<string, string> = {\n \"content-type\": \"text/event-stream\",\n \"cache-control\": \"no-cache\",\n connection: \"keep-alive\",\n \"x-vercel-ai-ui-message-stream\": \"v1\",\n}\n\nconst parseJson = Option.liftThrowable(JSON.parse)\n\n// Tool arguments/output cross effect-uai as JSON strings; hand the client the\n// decoded value, falling back to the raw string when it isn't JSON.\nconst decode = (raw: string): unknown => Option.getOrElse(parseJson(raw), () => raw)\n\nconst event = (part: Part | \"[DONE]\"): SSE.Event => ({\n data: part === \"[DONE]\" ? \"[DONE]\" : JSON.stringify(part),\n})\n\ntype State = {\n readonly messageId: string\n readonly textId: string | null\n readonly reasoningId: string | null\n readonly seq: number\n}\n\ntype Step = readonly [State, ReadonlyArray<Part>]\n\nconst toDataPart = (d: DataEmission): Part => {\n const base: Part = { type: `data-${d.name}`, data: d.data }\n const withId = d.id !== undefined ? { ...base, id: d.id } : base\n return d.transient !== undefined ? { ...withId, transient: d.transient } : withId\n}\n\n// One emission fans out to zero or more parts (a first TextDelta opens a block\n// *and* emits its delta), so each step yields an array.\nconst step = (s: State, ev: Emission): Step =>\n Match.value(ev).pipe(\n Match.tags({\n TextDelta: (e): Step => {\n if (s.textId !== null) return [s, [{ type: \"text-delta\", id: s.textId, delta: e.text }]]\n const id = `${s.messageId}:t${s.seq}`\n return [\n { ...s, textId: id, seq: s.seq + 1 },\n [\n { type: \"text-start\", id },\n { type: \"text-delta\", id, delta: e.text },\n ],\n ]\n },\n ReasoningDelta: (e): Step => {\n if (s.reasoningId !== null)\n return [s, [{ type: \"reasoning-delta\", id: s.reasoningId, delta: e.text }]]\n const id = `${s.messageId}:r${s.seq}`\n return [\n { ...s, reasoningId: id, seq: s.seq + 1 },\n [\n { type: \"reasoning-start\", id },\n { type: \"reasoning-delta\", id, delta: e.text },\n ],\n ]\n },\n ToolCallStart: (e): Step => [\n s,\n [{ type: \"tool-input-start\", toolCallId: e.call_id, toolName: e.name }],\n ],\n ToolCallArgsDelta: (e): Step => [\n s,\n [{ type: \"tool-input-delta\", toolCallId: e.call_id, inputTextDelta: e.delta }],\n ],\n RefusalDelta: (e): Step => [s, [{ type: \"error\", errorText: e.text }]],\n UsageUpdate: (): Step => [s, []],\n // Grounding progress and streamed citations have no UI Message Stream\n // part; the citations still arrive on `TurnComplete.turn`.\n WebSearchCall: (): Step => [s, []],\n CitationAdded: (): Step => [s, []],\n TurnComplete: (e): Step => {\n const closing: ReadonlyArray<Part> = [\n ...(s.textId !== null ? [{ type: \"text-end\", id: s.textId } as const] : []),\n ...(s.reasoningId !== null\n ? [{ type: \"reasoning-end\", id: s.reasoningId } as const]\n : []),\n ...Turn.getToolCalls(e.turn).map(\n (call): Part => ({\n type: \"tool-input-available\",\n toolCallId: call.call_id,\n toolName: call.name,\n input: decode(call.arguments),\n }),\n ),\n ]\n // Reset block ids; the loop may run further turns after tools resolve.\n return [{ ...s, textId: null, reasoningId: null }, closing]\n },\n }),\n // The non-`_tag` arm of InteractionEvent: a resolved tool result the loop\n // appended after running the tool.\n Match.when(\n { type: \"function_call_output\" },\n (o): Step => [\n s,\n [{ type: \"tool-output-available\", toolCallId: o.call_id, output: decode(o.output) }],\n ],\n ),\n // Caller-interleaved emissions.\n Match.when({ kind: \"data\" }, (d): Step => [s, [toDataPart(d)]]),\n Match.when(\n { kind: \"metadata\" },\n (m): Step => [s, [{ type: \"message-metadata\", messageMetadata: m.metadata }]],\n ),\n Match.exhaustive,\n )\n\n/**\n * Encode an `InteractionEvent` stream as UI Message Stream `SSE.Event`s,\n * bracketed by the protocol's `start` and terminal `finish` / `[DONE]`\n * markers. `messageId` identifies the assistant message on the client.\n */\nexport const toUIMessageStream =\n (messageId: string) =>\n <E, R>(self: Stream.Stream<Emission, E, R>): Stream.Stream<SSE.Event, E, R> =>\n Stream.make(event({ type: \"start\", messageId })).pipe(\n Stream.concat(\n Stream.mapAccum(\n self,\n (): State => ({ messageId, textId: null, reasoningId: null, seq: 0 }),\n (s, ev: Emission): readonly [State, ReadonlyArray<SSE.Event>] => {\n const [next, parts] = step(s, ev)\n return [next, parts.map(event)]\n },\n ),\n ),\n Stream.concat(Stream.make(event({ type: \"finish\" }), event(\"[DONE]\"))),\n )\n"],"mappings":";;;;;;;;;;;AAsFA,MAAa,YACX,MACA,MACA,aACkB;CAAE,MAAM;CAAQ;CAAM;CAAM,GAAG;CAAS;;AAG5D,MAAa,mBAAmB,cAAyC;CACvE,MAAM;CACN;CACD;;;;;;;;AASD,MAAa,kBAA0C;CACrD,gBAAgB;CAChB,iBAAiB;CACjB,YAAY;CACZ,iCAAiC;CAClC;AAED,MAAM,YAAY,OAAO,cAAc,KAAK,MAAM;AAIlD,MAAM,UAAU,QAAyB,OAAO,UAAU,UAAU,IAAI,QAAQ,IAAI;AAEpF,MAAM,SAAS,UAAsC,EACnD,MAAM,SAAS,WAAW,WAAW,KAAK,UAAU,KAAK,EAC1D;AAWD,MAAM,cAAc,MAA0B;CAC5C,MAAM,OAAa;EAAE,MAAM,QAAQ,EAAE;EAAQ,MAAM,EAAE;EAAM;CAC3D,MAAM,SAAS,EAAE,OAAO,KAAA,IAAY;EAAE,GAAG;EAAM,IAAI,EAAE;EAAI,GAAG;AAC5D,QAAO,EAAE,cAAc,KAAA,IAAY;EAAE,GAAG;EAAQ,WAAW,EAAE;EAAW,GAAG;;AAK7E,MAAM,QAAQ,GAAU,OACtB,MAAM,MAAM,GAAG,CAAC,KACd,MAAM,KAAK;CACT,YAAY,MAAY;AACtB,MAAI,EAAE,WAAW,KAAM,QAAO,CAAC,GAAG,CAAC;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;IAAE,MAAM;IAAiB,IAAI,EAAE;IAAa,CAAU,GACvD,EAAE;GACN,GAAG,KAAK,aAAa,EAAE,KAAK,CAAC,KAC1B,UAAgB;IACf,MAAM;IACN,YAAY,KAAK;IACjB,UAAU,KAAK;IACf,OAAO,OAAO,KAAK,UAAU;IAC9B,EACF;GACF;AAED,SAAO,CAAC;GAAE,GAAG;GAAG,QAAQ;GAAM,aAAa;GAAM,EAAE,QAAQ;;CAE9D,CAAC,EAGF,MAAM,KACJ,EAAE,MAAM,wBAAwB,GAC/B,MAAY,CACX,GACA,CAAC;CAAE,MAAM;CAAyB,YAAY,EAAE;CAAS,QAAQ,OAAO,EAAE,OAAO;CAAE,CAAC,CACrF,CACF,EAED,MAAM,KAAK,EAAE,MAAM,QAAQ,GAAG,MAAY,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAC/D,MAAM,KACJ,EAAE,MAAM,YAAY,GACnB,MAAY,CAAC,GAAG,CAAC;CAAE,MAAM;CAAoB,iBAAiB,EAAE;CAAU,CAAC,CAAC,CAC9E,EACD,MAAM,WACP;;;;;;AAOH,MAAa,qBACV,eACM,SACL,OAAO,KAAK,MAAM;CAAE,MAAM;CAAS;CAAW,CAAC,CAAC,CAAC,KAC/C,OAAO,OACL,OAAO,SACL,aACc;CAAE;CAAW,QAAQ;CAAM,aAAa;CAAM,KAAK;CAAG,IACnE,GAAG,OAA6D;CAC/D,MAAM,CAAC,MAAM,SAAS,KAAK,GAAG,GAAG;AACjC,QAAO,CAAC,MAAM,MAAM,IAAI,MAAM,CAAC;EAElC,CACF,EACD,OAAO,OAAO,OAAO,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC,EAAE,MAAM,SAAS,CAAC,CAAC,CACvE"}
1
+ {"version":3,"file":"UIMessageStream.mjs","names":[],"sources":["../src/UIMessageStream.ts"],"sourcesContent":["/**\n * Project an effect-uai loop onto the Vercel AI SDK \"UI Message Stream\"\n * protocol (the `v1` wire format, stable across AI SDK v5–v7). The output is a\n * `Stream<SSE.Event>` ready for `SSE.toBytes`; a `useChat` client on the other\n * end consumes it with no frontend changes.\n *\n * The encoder input is an `Emission`: every `InteractionEvent` the loop\n * produces, plus optional `dataPart` and `messageMetadata` emissions a caller\n * interleaves to stream custom typed data (e.g. live metrics) and settled\n * per-message metadata (e.g. usage). A plain `Stream<InteractionEvent>` is a\n * valid `Stream<Emission>`, so existing callers are unaffected.\n *\n * The protocol wants a stable id and explicit start/end lifecycle around each\n * text and reasoning block, which effect-uai's delta events don't carry. This\n * module synthesizes them: one block id per turn, opened on the first delta\n * and closed on `TurnComplete`.\n */\nimport type * as SSE from \"@effect-uai/core/SSE\"\nimport * as Turn from \"@effect-uai/core/Turn\"\nimport { Match, Option, Stream } from \"effect\"\n\n/**\n * The stream parts `useChat` consumes. Names and shapes are the wire\n * contract with the AI SDK client; keep them literal.\n */\nexport type Part =\n | { readonly type: \"start\"; readonly messageId: string }\n | { readonly type: \"text-start\"; readonly id: string }\n | { readonly type: \"text-delta\"; readonly id: string; readonly delta: string }\n | { readonly type: \"text-end\"; readonly id: string }\n | { readonly type: \"reasoning-start\"; readonly id: string }\n | { readonly type: \"reasoning-delta\"; readonly id: string; readonly delta: string }\n | { readonly type: \"reasoning-end\"; readonly id: string }\n | { readonly type: \"tool-input-start\"; readonly toolCallId: string; readonly toolName: string }\n | {\n readonly type: \"tool-input-delta\"\n readonly toolCallId: string\n readonly inputTextDelta: string\n }\n | {\n readonly type: \"tool-input-available\"\n readonly toolCallId: string\n readonly toolName: string\n readonly input: unknown\n }\n | {\n readonly type: \"tool-output-available\"\n readonly toolCallId: string\n readonly output: unknown\n }\n | {\n readonly type: `data-${string}`\n readonly id?: string\n readonly data: unknown\n readonly transient?: boolean\n }\n | { readonly type: \"message-metadata\"; readonly messageMetadata: unknown }\n | { readonly type: \"error\"; readonly errorText: string }\n | { readonly type: \"finish\" }\n\n/**\n * A custom typed data part (`data-<name>`). `transient: true` delivers it to\n * the client's `onData` callback only (not persisted into `message.parts`);\n * reuse an `id` across emissions to reconcile/update one part in place.\n */\nexport type DataEmission = {\n readonly kind: \"data\"\n readonly name: string\n readonly data: unknown\n readonly id?: string\n readonly transient?: boolean\n}\n\n/** Settled per-message metadata, surfaced as `message.metadata` on the client. */\nexport type MetadataEmission = {\n readonly kind: \"metadata\"\n readonly metadata: unknown\n}\n\n/**\n * What `toUIMessageStream` encodes: loop `InteractionEvent`s plus optional\n * data / metadata emissions a caller interleaves into the stream.\n */\nexport type Emission = Turn.InteractionEvent | DataEmission | MetadataEmission\n\n/** Construct a `data-<name>` emission. See {@link DataEmission}. */\nexport const dataPart = (\n name: string,\n data: unknown,\n options?: { readonly id?: string; readonly transient?: boolean },\n): DataEmission => ({ kind: \"data\", name, data, ...options })\n\n/** Construct a message-metadata emission. See {@link MetadataEmission}. */\nexport const messageMetadata = (metadata: unknown): MetadataEmission => ({\n kind: \"metadata\",\n metadata,\n})\n\n/**\n * Headers the UI Message Stream protocol requires on the HTTP response.\n * `x-vercel-ai-ui-message-stream: v1` is mandatory; without it the client\n * falls back to the plain text-stream protocol and ignores non-text parts.\n * Pure data, so it composes with any server (web `Response`, `@effect/platform`\n * `HttpServerResponse`, raw Node) - the package deliberately owns no HTTP layer.\n */\nexport const responseHeaders: Record<string, string> = {\n \"content-type\": \"text/event-stream\",\n \"cache-control\": \"no-cache\",\n connection: \"keep-alive\",\n \"x-vercel-ai-ui-message-stream\": \"v1\",\n}\n\nconst parseJson = Option.liftThrowable(JSON.parse)\n\n// Tool arguments/output cross effect-uai as JSON strings; hand the client the\n// decoded value, falling back to the raw string when it isn't JSON.\nconst decode = (raw: string): unknown => Option.getOrElse(parseJson(raw), () => raw)\n\nconst event = (part: Part | \"[DONE]\"): SSE.Event => ({\n data: part === \"[DONE]\" ? \"[DONE]\" : JSON.stringify(part),\n})\n\ntype State = {\n readonly messageId: string\n readonly textId: string | null\n readonly reasoningId: string | null\n readonly seq: number\n}\n\ntype Step = readonly [State, ReadonlyArray<Part>]\n\nconst toDataPart = (d: DataEmission): Part => {\n const base: Part = { type: `data-${d.name}`, data: d.data }\n const withId = d.id !== undefined ? { ...base, id: d.id } : base\n return d.transient !== undefined ? { ...withId, transient: d.transient } : withId\n}\n\n// One emission fans out to zero or more parts (a first TextDelta opens a block\n// *and* emits its delta), so each step yields an array.\nconst step = (s: State, ev: Emission): Step =>\n Match.value(ev).pipe(\n Match.tags({\n TextDelta: (e): Step => {\n if (s.textId !== null) return [s, [{ type: \"text-delta\", id: s.textId, delta: e.text }]]\n const id = `${s.messageId}:t${s.seq}`\n return [\n { ...s, textId: id, seq: s.seq + 1 },\n [\n { type: \"text-start\", id },\n { type: \"text-delta\", id, delta: e.text },\n ],\n ]\n },\n ReasoningDelta: (e): Step => {\n if (s.reasoningId !== null)\n return [s, [{ type: \"reasoning-delta\", id: s.reasoningId, delta: e.text }]]\n const id = `${s.messageId}:r${s.seq}`\n return [\n { ...s, reasoningId: id, seq: s.seq + 1 },\n [\n { type: \"reasoning-start\", id },\n { type: \"reasoning-delta\", id, delta: e.text },\n ],\n ]\n },\n ToolCallStart: (e): Step => [\n s,\n [{ type: \"tool-input-start\", toolCallId: e.call_id, toolName: e.name }],\n ],\n ToolCallArgsDelta: (e): Step => [\n s,\n [{ type: \"tool-input-delta\", toolCallId: e.call_id, inputTextDelta: e.delta }],\n ],\n RefusalDelta: (e): Step => [s, [{ type: \"error\", errorText: e.text }]],\n UsageUpdate: (): Step => [s, []],\n // Grounding progress and streamed citations have no UI Message Stream\n // part; the citations still arrive on `TurnComplete.turn`.\n WebSearchCall: (): Step => [s, []],\n CitationAdded: (): Step => [s, []],\n TurnComplete: (e): Step => {\n const closing: ReadonlyArray<Part> = [\n ...(s.textId !== null ? [{ type: \"text-end\", id: s.textId } as const] : []),\n ...(s.reasoningId !== null\n ? [{ type: \"reasoning-end\", id: s.reasoningId } as const]\n : []),\n ...Turn.getToolCalls(e.turn).map((call): Part => ({\n type: \"tool-input-available\",\n toolCallId: call.call_id,\n toolName: call.name,\n input: decode(call.arguments),\n })),\n ]\n // Reset block ids; the loop may run further turns after tools resolve.\n return [{ ...s, textId: null, reasoningId: null }, closing]\n },\n }),\n // The non-`_tag` arm of InteractionEvent: a resolved tool result the loop\n // appended after running the tool.\n Match.when({ type: \"function_call_output\" }, (o): Step => [\n s,\n [{ type: \"tool-output-available\", toolCallId: o.call_id, output: decode(o.output) }],\n ]),\n // Caller-interleaved emissions.\n Match.when({ kind: \"data\" }, (d): Step => [s, [toDataPart(d)]]),\n Match.when({ kind: \"metadata\" }, (m): Step => [\n s,\n [{ type: \"message-metadata\", messageMetadata: m.metadata }],\n ]),\n Match.exhaustive,\n )\n\n/**\n * Encode an `InteractionEvent` stream as UI Message Stream `SSE.Event`s,\n * bracketed by the protocol's `start` and terminal `finish` / `[DONE]`\n * markers. `messageId` identifies the assistant message on the client.\n */\nexport const toUIMessageStream =\n (messageId: string) =>\n <E, R>(self: Stream.Stream<Emission, E, R>): Stream.Stream<SSE.Event, E, R> =>\n Stream.make(event({ type: \"start\", messageId })).pipe(\n Stream.concat(\n Stream.mapAccum(\n self,\n (): State => ({ messageId, textId: null, reasoningId: null, seq: 0 }),\n (s, ev: Emission): readonly [State, ReadonlyArray<SSE.Event>] => {\n const [next, parts] = step(s, ev)\n return [next, parts.map(event)]\n },\n ),\n ),\n Stream.concat(Stream.make(event({ type: \"finish\" }), event(\"[DONE]\"))),\n )\n"],"mappings":";;;;;;;;;;;AAsFA,MAAa,YACX,MACA,MACA,aACkB;CAAE,MAAM;CAAQ;CAAM;CAAM,GAAG;AAAQ;;AAG3D,MAAa,mBAAmB,cAAyC;CACvE,MAAM;CACN;AACF;;;;;;;;AASA,MAAa,kBAA0C;CACrD,gBAAgB;CAChB,iBAAiB;CACjB,YAAY;CACZ,iCAAiC;AACnC;AAEA,MAAM,YAAY,OAAO,cAAc,KAAK,KAAK;AAIjD,MAAM,UAAU,QAAyB,OAAO,UAAU,UAAU,GAAG,SAAS,GAAG;AAEnF,MAAM,SAAS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E,MAAM;CAAS;AAAU,CAAC,CAAC,CAAC,CAAC,KAC/C,OAAO,OACL,OAAO,SACL,aACc;CAAE;CAAW,QAAQ;CAAM,aAAa;CAAM,KAAK;AAAE,KAClE,GAAG,OAA6D;CAC/D,MAAM,CAAC,MAAM,SAAS,KAAK,GAAG,EAAE;CAChC,OAAO,CAAC,MAAM,MAAM,IAAI,KAAK,CAAC;AAChC,CACF,CACF,GACA,OAAO,OAAO,OAAO,KAAK,MAAM,EAAE,MAAM,SAAS,CAAC,GAAG,MAAM,QAAQ,CAAC,CAAC,CACvE"}
@@ -1 +1 @@
1
- export { };
1
+ export {}
@@ -1,5 +1,5 @@
1
1
  import { decodeMessages } from "./Messages.mjs";
2
- import { n as globalExpect, r as describe, t as it } from "./dist-DDciKZ0w.mjs";
2
+ import { n as globalExpect, r as describe, t as it } from "./dist-CGDqr3r6.mjs";
3
3
  import { dataPart, messageMetadata, toUIMessageStream } from "./UIMessageStream.mjs";
4
4
  import * as Image from "@effect-uai/core/Image";
5
5
  import * as Items from "@effect-uai/core/Items";
@@ -125,7 +125,7 @@ describe("toUIMessageStream", () => {
125
125
  });
126
126
  describe("decodeMessages", () => {
127
127
  it("folds UIMessage text parts back into HistoryItems by role", () => {
128
- globalExpect(decodeMessages([
128
+ const items = decodeMessages([
129
129
  {
130
130
  role: "system",
131
131
  parts: [{
@@ -150,14 +150,15 @@ describe("decodeMessages", () => {
150
150
  text: "hello"
151
151
  }]
152
152
  }
153
- ])).toEqual([
153
+ ]);
154
+ globalExpect(items).toEqual([
154
155
  Items.systemText("be terse"),
155
156
  Items.userText("hi there"),
156
157
  Items.assistantText("hello")
157
158
  ]);
158
159
  });
159
160
  it("folds an image file part into an input_image block and drops non-image files", () => {
160
- globalExpect(decodeMessages([{
161
+ const items = decodeMessages([{
161
162
  role: "user",
162
163
  parts: [
163
164
  {
@@ -177,7 +178,8 @@ describe("decodeMessages", () => {
177
178
  url: "https://x/doc.pdf"
178
179
  }
179
180
  ]
180
- }])).toEqual([{
181
+ }]);
182
+ globalExpect(items).toEqual([{
181
183
  type: "message",
182
184
  role: "user",
183
185
  content: [{
@@ -190,7 +192,7 @@ describe("decodeMessages", () => {
190
192
  }]);
191
193
  });
192
194
  it("expands an assistant tool call into function_call + function_call_output", () => {
193
- globalExpect(decodeMessages([{
195
+ const items = decodeMessages([{
194
196
  role: "assistant",
195
197
  parts: [{
196
198
  type: "text",
@@ -204,7 +206,8 @@ describe("decodeMessages", () => {
204
206
  output: { hits: 3 },
205
207
  providerExecuted: false
206
208
  }]
207
- }])).toEqual([
209
+ }]);
210
+ globalExpect(items).toEqual([
208
211
  Items.assistantText("Here"),
209
212
  {
210
213
  type: "function_call",
@@ -216,7 +219,7 @@ describe("decodeMessages", () => {
216
219
  ]);
217
220
  });
218
221
  it("emits only function_call for a dynamic tool with no resolved output", () => {
219
- globalExpect(decodeMessages([{
222
+ const items = decodeMessages([{
220
223
  role: "assistant",
221
224
  parts: [{
222
225
  type: "dynamic-tool",
@@ -225,7 +228,8 @@ describe("decodeMessages", () => {
225
228
  state: "input-available",
226
229
  input: { id: 7 }
227
230
  }]
228
- }])).toEqual([{
231
+ }]);
232
+ globalExpect(items).toEqual([{
229
233
  type: "function_call",
230
234
  call_id: "c9",
231
235
  name: "lookup",
@@ -1 +1 @@
1
- {"version":3,"file":"UIMessageStream.test.mjs","names":[],"sources":["../src/UIMessageStream.test.ts"],"sourcesContent":["import * as Image from \"@effect-uai/core/Image\"\nimport * as Items from \"@effect-uai/core/Items\"\nimport * as Turn from \"@effect-uai/core/Turn\"\nimport { describe, it } from \"@effect/vitest\"\nimport { Effect, Stream } from \"effect\"\nimport { expect } from \"vitest\"\nimport { decodeMessages } from \"./Messages.js\"\nimport { dataPart, type Emission, messageMetadata, toUIMessageStream } from \"./UIMessageStream.js\"\n\nconst turn = (items: ReadonlyArray<Items.HistoryItem>, stop: Items.StopReason): Turn.Turn => ({\n items,\n usage: {},\n stop_reason: stop,\n})\n\n// Run an InteractionEvent script through the encoder and return the decoded\n// parts (the `data:` payload of each SSE event, JSON-parsed except `[DONE]`).\nconst encode = (events: ReadonlyArray<Emission>): Effect.Effect<ReadonlyArray<unknown>> =>\n Stream.fromIterable(events).pipe(\n toUIMessageStream(\"m1\"),\n Stream.runCollect,\n Effect.map((sse) => sse.map((e) => (e.data === \"[DONE]\" ? \"[DONE]\" : JSON.parse(e.data)))),\n )\n\nconst types = (parts: ReadonlyArray<unknown>): ReadonlyArray<string> =>\n parts.map((p) => (p === \"[DONE]\" ? \"[DONE]\" : (p as { type: string }).type))\n\ndescribe(\"toUIMessageStream\", () => {\n it.effect(\"brackets text deltas with start / text-start / text-end / finish / [DONE]\", () =>\n Effect.gen(function* () {\n const parts = yield* encode([\n Turn.TurnEvent.TextDelta({ text: \"Hel\" }),\n Turn.TurnEvent.TextDelta({ text: \"lo\" }),\n Turn.TurnEvent.TurnComplete({ turn: turn([Items.assistantText(\"Hello\")], \"stop\") }),\n ])\n\n expect(types(parts)).toEqual([\n \"start\",\n \"text-start\",\n \"text-delta\",\n \"text-delta\",\n \"text-end\",\n \"finish\",\n \"[DONE]\",\n ])\n expect(parts[0]).toEqual({ type: \"start\", messageId: \"m1\" })\n // Both deltas share the one synthesized block id.\n const id = (parts[1] as { id: string }).id\n expect((parts[2] as { id: string }).id).toBe(id)\n expect((parts[4] as { id: string }).id).toBe(id)\n }),\n )\n\n it.effect(\"maps a tool round-trip to input-start/delta/available then output-available\", () =>\n Effect.gen(function* () {\n const call: Items.ToolCall = {\n type: \"function_call\",\n call_id: \"c1\",\n name: \"search\",\n arguments: `{\"q\":\"lisbon\"}`,\n }\n const parts = yield* encode([\n Turn.TurnEvent.ToolCallStart({ call_id: \"c1\", name: \"search\" }),\n Turn.TurnEvent.ToolCallArgsDelta({ call_id: \"c1\", delta: `{\"q\":\"lisbon\"}` }),\n Turn.TurnEvent.TurnComplete({ turn: turn([call], \"tool_calls\") }),\n Items.toolCallOutput(\"c1\", `{\"hits\":3}`),\n ])\n\n expect(types(parts)).toEqual([\n \"start\",\n \"tool-input-start\",\n \"tool-input-delta\",\n \"tool-input-available\",\n \"tool-output-available\",\n \"finish\",\n \"[DONE]\",\n ])\n // JSON payloads are decoded, not passed through as strings.\n expect(parts[3]).toEqual({\n type: \"tool-input-available\",\n toolCallId: \"c1\",\n toolName: \"search\",\n input: { q: \"lisbon\" },\n })\n expect(parts[4]).toEqual({\n type: \"tool-output-available\",\n toolCallId: \"c1\",\n output: { hits: 3 },\n })\n }),\n )\n\n it.effect(\"surfaces a refusal as an error part\", () =>\n Effect.gen(function* () {\n const parts = yield* encode([Turn.TurnEvent.RefusalDelta({ text: \"cannot help\" })])\n expect(types(parts)).toEqual([\"start\", \"error\", \"finish\", \"[DONE]\"])\n expect(parts[1]).toEqual({ type: \"error\", errorText: \"cannot help\" })\n }),\n )\n\n it.effect(\"interleaves data-part and message-metadata emissions\", () =>\n Effect.gen(function* () {\n const parts = yield* encode([\n dataPart(\"metrics\", { tokps: 42 }, { transient: true }),\n Turn.TurnEvent.TextDelta({ text: \"hi\" }),\n messageMetadata({ model: \"gpt-5.4-mini\", usage: { output_tokens: 7 } }),\n Turn.TurnEvent.TurnComplete({ turn: turn([Items.assistantText(\"hi\")], \"stop\") }),\n ])\n\n expect(types(parts)).toEqual([\n \"start\",\n \"data-metrics\",\n \"text-start\",\n \"text-delta\",\n \"message-metadata\",\n \"text-end\",\n \"finish\",\n \"[DONE]\",\n ])\n expect(parts[1]).toEqual({ type: \"data-metrics\", data: { tokps: 42 }, transient: true })\n expect(parts[4]).toEqual({\n type: \"message-metadata\",\n messageMetadata: { model: \"gpt-5.4-mini\", usage: { output_tokens: 7 } },\n })\n }),\n )\n})\n\ndescribe(\"decodeMessages\", () => {\n it(\"folds UIMessage text parts back into HistoryItems by role\", () => {\n const items = decodeMessages([\n { role: \"system\", parts: [{ type: \"text\", text: \"be terse\" }] },\n {\n role: \"user\",\n parts: [\n { type: \"text\", text: \"hi \" },\n { type: \"text\", text: \"there\" },\n ],\n },\n { role: \"assistant\", parts: [{ type: \"text\", text: \"hello\" }] },\n ])\n\n expect(items).toEqual([\n Items.systemText(\"be terse\"),\n Items.userText(\"hi there\"),\n Items.assistantText(\"hello\"),\n ])\n })\n\n it(\"folds an image file part into an input_image block and drops non-image files\", () => {\n const items = decodeMessages([\n {\n role: \"user\",\n parts: [\n { type: \"text\", text: \"look\", state: \"done\" },\n { type: \"file\", mediaType: \"image/png\", url: \"https://x/i.png\", filename: \"i.png\" },\n { type: \"file\", mediaType: \"application/pdf\", url: \"https://x/doc.pdf\" },\n ],\n },\n ])\n\n expect(items).toEqual([\n {\n type: \"message\",\n role: \"user\",\n content: [\n { type: \"input_text\", text: \"look\" },\n { type: \"input_image\", source: Image.imageUrl(\"https://x/i.png\", \"image/png\") },\n ],\n },\n ])\n })\n\n it(\"expands an assistant tool call into function_call + function_call_output\", () => {\n const items = decodeMessages([\n {\n role: \"assistant\",\n parts: [\n { type: \"text\", text: \"Here\", state: \"done\" },\n {\n type: \"tool-search\",\n toolCallId: \"c1\",\n state: \"output-available\",\n input: { q: \"lisbon\" },\n output: { hits: 3 },\n providerExecuted: false,\n },\n ],\n },\n ])\n\n expect(items).toEqual([\n Items.assistantText(\"Here\"),\n { type: \"function_call\", call_id: \"c1\", name: \"search\", arguments: `{\"q\":\"lisbon\"}` },\n Items.toolCallOutput(\"c1\", `{\"hits\":3}`),\n ])\n })\n\n it(\"emits only function_call for a dynamic tool with no resolved output\", () => {\n const items = decodeMessages([\n {\n role: \"assistant\",\n parts: [\n {\n type: \"dynamic-tool\",\n toolName: \"lookup\",\n toolCallId: \"c9\",\n state: \"input-available\",\n input: { id: 7 },\n },\n ],\n },\n ])\n\n expect(items).toEqual([\n { type: \"function_call\", call_id: \"c9\", name: \"lookup\", arguments: `{\"id\":7}` },\n ])\n })\n})\n"],"mappings":";;;;;;;;AASA,MAAM,QAAQ,OAAyC,UAAuC;CAC5F;CACA,OAAO,EAAE;CACT,aAAa;CACd;AAID,MAAM,UAAU,WAC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AEW,CAAC,CAAC,QAAQ;GACpB,MAAM,cAAc,OAAO;GAC3B;IAAE,MAAM;IAAiB,SAAS;IAAM,MAAM;IAAU,WAAW;IAAkB;GACrF,MAAM,eAAe,MAAM,aAAa;GACzC,CAAC;GACF;AAEF,IAAG,6EAA6E;AAgB9E,eAfc,eAAe,CAC3B;GACE,MAAM;GACN,OAAO,CACL;IACE,MAAM;IACN,UAAU;IACV,YAAY;IACZ,OAAO;IACP,OAAO,EAAE,IAAI,GAAG;IACjB,CACF;GACF,CACF,CAEW,CAAC,CAAC,QAAQ,CACpB;GAAE,MAAM;GAAiB,SAAS;GAAM,MAAM;GAAU,WAAW;GAAY,CAChF,CAAC;GACF;EACF"}
1
+ {"version":3,"file":"UIMessageStream.test.mjs","names":[],"sources":["../src/UIMessageStream.test.ts"],"sourcesContent":["import * as Image from \"@effect-uai/core/Image\"\nimport * as Items from \"@effect-uai/core/Items\"\nimport * as Turn from \"@effect-uai/core/Turn\"\nimport { describe, it } from \"@effect/vitest\"\nimport { Effect, Stream } from \"effect\"\nimport { expect } from \"vitest\"\nimport { decodeMessages } from \"./Messages.js\"\nimport { dataPart, type Emission, messageMetadata, toUIMessageStream } from \"./UIMessageStream.js\"\n\nconst turn = (items: ReadonlyArray<Items.HistoryItem>, stop: Items.StopReason): Turn.Turn => ({\n items,\n usage: {},\n stop_reason: stop,\n})\n\n// Run an InteractionEvent script through the encoder and return the decoded\n// parts (the `data:` payload of each SSE event, JSON-parsed except `[DONE]`).\nconst encode = (events: ReadonlyArray<Emission>): Effect.Effect<ReadonlyArray<unknown>> =>\n Stream.fromIterable(events).pipe(\n toUIMessageStream(\"m1\"),\n Stream.runCollect,\n Effect.map((sse) => sse.map((e) => (e.data === \"[DONE]\" ? \"[DONE]\" : JSON.parse(e.data)))),\n )\n\nconst types = (parts: ReadonlyArray<unknown>): ReadonlyArray<string> =>\n parts.map((p) => (p === \"[DONE]\" ? \"[DONE]\" : (p as { type: string }).type))\n\ndescribe(\"toUIMessageStream\", () => {\n it.effect(\"brackets text deltas with start / text-start / text-end / finish / [DONE]\", () =>\n Effect.gen(function* () {\n const parts = yield* encode([\n Turn.TurnEvent.TextDelta({ text: \"Hel\" }),\n Turn.TurnEvent.TextDelta({ text: \"lo\" }),\n Turn.TurnEvent.TurnComplete({ turn: turn([Items.assistantText(\"Hello\")], \"stop\") }),\n ])\n\n expect(types(parts)).toEqual([\n \"start\",\n \"text-start\",\n \"text-delta\",\n \"text-delta\",\n \"text-end\",\n \"finish\",\n \"[DONE]\",\n ])\n expect(parts[0]).toEqual({ type: \"start\", messageId: \"m1\" })\n // Both deltas share the one synthesized block id.\n const id = (parts[1] as { id: string }).id\n expect((parts[2] as { id: string }).id).toBe(id)\n expect((parts[4] as { id: string }).id).toBe(id)\n }),\n )\n\n it.effect(\"maps a tool round-trip to input-start/delta/available then output-available\", () =>\n Effect.gen(function* () {\n const call: Items.ToolCall = {\n type: \"function_call\",\n call_id: \"c1\",\n name: \"search\",\n arguments: `{\"q\":\"lisbon\"}`,\n }\n const parts = yield* encode([\n Turn.TurnEvent.ToolCallStart({ call_id: \"c1\", name: \"search\" }),\n Turn.TurnEvent.ToolCallArgsDelta({ call_id: \"c1\", delta: `{\"q\":\"lisbon\"}` }),\n Turn.TurnEvent.TurnComplete({ turn: turn([call], \"tool_calls\") }),\n Items.toolCallOutput(\"c1\", `{\"hits\":3}`),\n ])\n\n expect(types(parts)).toEqual([\n \"start\",\n \"tool-input-start\",\n \"tool-input-delta\",\n \"tool-input-available\",\n \"tool-output-available\",\n \"finish\",\n \"[DONE]\",\n ])\n // JSON payloads are decoded, not passed through as strings.\n expect(parts[3]).toEqual({\n type: \"tool-input-available\",\n toolCallId: \"c1\",\n toolName: \"search\",\n input: { q: \"lisbon\" },\n })\n expect(parts[4]).toEqual({\n type: \"tool-output-available\",\n toolCallId: \"c1\",\n output: { hits: 3 },\n })\n }),\n )\n\n it.effect(\"surfaces a refusal as an error part\", () =>\n Effect.gen(function* () {\n const parts = yield* encode([Turn.TurnEvent.RefusalDelta({ text: \"cannot help\" })])\n expect(types(parts)).toEqual([\"start\", \"error\", \"finish\", \"[DONE]\"])\n expect(parts[1]).toEqual({ type: \"error\", errorText: \"cannot help\" })\n }),\n )\n\n it.effect(\"interleaves data-part and message-metadata emissions\", () =>\n Effect.gen(function* () {\n const parts = yield* encode([\n dataPart(\"metrics\", { tokps: 42 }, { transient: true }),\n Turn.TurnEvent.TextDelta({ text: \"hi\" }),\n messageMetadata({ model: \"gpt-5.4-mini\", usage: { output_tokens: 7 } }),\n Turn.TurnEvent.TurnComplete({ turn: turn([Items.assistantText(\"hi\")], \"stop\") }),\n ])\n\n expect(types(parts)).toEqual([\n \"start\",\n \"data-metrics\",\n \"text-start\",\n \"text-delta\",\n \"message-metadata\",\n \"text-end\",\n \"finish\",\n \"[DONE]\",\n ])\n expect(parts[1]).toEqual({ type: \"data-metrics\", data: { tokps: 42 }, transient: true })\n expect(parts[4]).toEqual({\n type: \"message-metadata\",\n messageMetadata: { model: \"gpt-5.4-mini\", usage: { output_tokens: 7 } },\n })\n }),\n )\n})\n\ndescribe(\"decodeMessages\", () => {\n it(\"folds UIMessage text parts back into HistoryItems by role\", () => {\n const items = decodeMessages([\n { role: \"system\", parts: [{ type: \"text\", text: \"be terse\" }] },\n {\n role: \"user\",\n parts: [\n { type: \"text\", text: \"hi \" },\n { type: \"text\", text: \"there\" },\n ],\n },\n { role: \"assistant\", parts: [{ type: \"text\", text: \"hello\" }] },\n ])\n\n expect(items).toEqual([\n Items.systemText(\"be terse\"),\n Items.userText(\"hi there\"),\n Items.assistantText(\"hello\"),\n ])\n })\n\n it(\"folds an image file part into an input_image block and drops non-image files\", () => {\n const items = decodeMessages([\n {\n role: \"user\",\n parts: [\n { type: \"text\", text: \"look\", state: \"done\" },\n { type: \"file\", mediaType: \"image/png\", url: \"https://x/i.png\", filename: \"i.png\" },\n { type: \"file\", mediaType: \"application/pdf\", url: \"https://x/doc.pdf\" },\n ],\n },\n ])\n\n expect(items).toEqual([\n {\n type: \"message\",\n role: \"user\",\n content: [\n { type: \"input_text\", text: \"look\" },\n { type: \"input_image\", source: Image.imageUrl(\"https://x/i.png\", \"image/png\") },\n ],\n },\n ])\n })\n\n it(\"expands an assistant tool call into function_call + function_call_output\", () => {\n const items = decodeMessages([\n {\n role: \"assistant\",\n parts: [\n { type: \"text\", text: \"Here\", state: \"done\" },\n {\n type: \"tool-search\",\n toolCallId: \"c1\",\n state: \"output-available\",\n input: { q: \"lisbon\" },\n output: { hits: 3 },\n providerExecuted: false,\n },\n ],\n },\n ])\n\n expect(items).toEqual([\n Items.assistantText(\"Here\"),\n { type: \"function_call\", call_id: \"c1\", name: \"search\", arguments: `{\"q\":\"lisbon\"}` },\n Items.toolCallOutput(\"c1\", `{\"hits\":3}`),\n ])\n })\n\n it(\"emits only function_call for a dynamic tool with no resolved output\", () => {\n const items = decodeMessages([\n {\n role: \"assistant\",\n parts: [\n {\n type: \"dynamic-tool\",\n toolName: \"lookup\",\n toolCallId: \"c9\",\n state: \"input-available\",\n input: { id: 7 },\n },\n ],\n },\n ])\n\n expect(items).toEqual([\n { type: \"function_call\", call_id: \"c9\", name: \"lookup\", arguments: `{\"id\":7}` },\n ])\n })\n})\n"],"mappings":";;;;;;;;AASA,MAAM,QAAQ,OAAyC,UAAuC;CAC5F;CACA,OAAO,CAAC;CACR,aAAa;AACf;AAIA,MAAM,UAAU,WACd,OAAO,aAAa,MAAM,CAAC,CAAC,KAC1B,kBAAkB,IAAI,GACtB,OAAO,YACP,OAAO,KAAK,QAAQ,IAAI,KAAK,MAAO,EAAE,SAAS,WAAW,WAAW,KAAK,MAAM,EAAE,IAAI,CAAE,CAAC,CAC3F;AAEF,MAAM,SAAS,UACb,MAAM,KAAK,MAAO,MAAM,WAAW,WAAY,EAAuB,IAAK;AAE7E,SAAS,2BAA2B;CAClC,GAAG,OAAO,mFACR,OAAO,IAAI,aAAa;EACtB,MAAM,QAAQ,OAAO,OAAO;GAC1B,KAAK,UAAU,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