diff --git a/packages/ai/src/protocols/anthropic-messages.ts b/packages/ai/src/protocols/anthropic-messages.ts index f1646b1727c..532c69d152e 100644 --- a/packages/ai/src/protocols/anthropic-messages.ts +++ b/packages/ai/src/protocols/anthropic-messages.ts @@ -584,7 +584,7 @@ const serverToolResultType = (name: string): AnthropicServerToolResultType | und return undefined } -const lowerServerToolResult = Effect.fn("AnthropicMessages.lowerServerToolResult")(function* ( +const lowerServerToolResult = Effect.fnUntraced(function* ( part: ToolResultPart, providerMetadataKey: string, ) { @@ -657,7 +657,7 @@ const citationsFromMetadata = (metadata: MediaPart["metadata"]): AnthropicDocume const isHttpUrl = (value: string) => /^https?:\/\//i.test(value.trim()) -const lowerMedia = Effect.fn("AnthropicMessages.lowerMedia")(function* ( +const lowerMedia = Effect.fnUntraced(function* ( part: MediaPart, breakpoints?: Cache.Breakpoints, ) { @@ -847,7 +847,7 @@ const splitsLocalToolResults = (messages: LLMRequest["messages"], index: number) return pending.size > 0 } -const lowerNativeSystemUpdate = Effect.fn("AnthropicMessages.lowerNativeSystemUpdate")(function* ( +const lowerNativeSystemUpdate = Effect.fnUntraced(function* ( message: LLMRequest["messages"][number], breakpoints: Cache.Breakpoints, ) { @@ -862,7 +862,7 @@ const lowerNativeSystemUpdate = Effect.fn("AnthropicMessages.lowerNativeSystemUp } }) -const lowerMessages = Effect.fn("AnthropicMessages.lowerMessages")(function* ( +const lowerMessages = Effect.fnUntraced(function* ( request: LLMRequest, breakpoints: Cache.Breakpoints, ) { diff --git a/packages/ai/src/protocols/bedrock-converse.ts b/packages/ai/src/protocols/bedrock-converse.ts index 9e31442b03e..8d43542fb94 100644 --- a/packages/ai/src/protocols/bedrock-converse.ts +++ b/packages/ai/src/protocols/bedrock-converse.ts @@ -285,7 +285,7 @@ const lowerToolCall = (part: ToolCallPart, normalizeID: (id: string) => string): }, }) -const lowerToolResultContent = Effect.fn("BedrockConverse.lowerToolResultContent")(function* ( +const lowerToolResultContent = Effect.fnUntraced(function* ( part: ToolResultPart, documentNames: Set, ) { @@ -305,7 +305,7 @@ const lowerToolResultContent = Effect.fn("BedrockConverse.lowerToolResultContent return content }) -const lowerToolResult = Effect.fn("BedrockConverse.lowerToolResult")(function* ( +const lowerToolResult = Effect.fnUntraced(function* ( part: ToolResultPart, documentNames: Set, normalizeID: (id: string) => string, @@ -322,7 +322,7 @@ const lowerToolResult = Effect.fn("BedrockConverse.lowerToolResult")(function* ( // Keep Claude and Nova tool-result images inline; put other models' images beside the result. const keepToolImagesInline = (id: string) => id.includes("anthropic.claude-") || id.includes("amazon.nova-") -const lowerMessages = Effect.fn("BedrockConverse.lowerMessages")(function* ( +const lowerMessages = Effect.fnUntraced(function* ( request: LLMRequest, breakpoints: BedrockCache.Breakpoints, ) { diff --git a/packages/ai/src/protocols/gemini.ts b/packages/ai/src/protocols/gemini.ts index a4fbdb0e1ad..adc25fe5c87 100644 --- a/packages/ai/src/protocols/gemini.ts +++ b/packages/ai/src/protocols/gemini.ts @@ -283,7 +283,7 @@ const lowerToolConfig = (toolChoice: NonNullable) => tool: (name) => ({ functionCallingConfig: { mode: "ANY" as const, allowedFunctionNames: [name] } }), }) -const lowerContentPart = Effect.fn("Gemini.lowerContentPart")(function* (part: TextPart | MediaPart) { +const lowerContentPart = Effect.fnUntraced(function* (part: TextPart | MediaPart) { if (part.type === "text") return { text: part.text } return yield* GeminiGenerateContent.mediaPart("Gemini", part.media) }) @@ -302,7 +302,7 @@ const lowerToolCall = (part: ToolCallPart, omitIds: boolean, metadataKey: string thoughtSignature: thoughtSignature(part.providerMetadata, metadataKey), }) -const lowerMessages = Effect.fn("Gemini.lowerMessages")(function* (request: LLMRequest) { +const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest) { const contents: GeminiContent[] = [] const metadataKey = request.model.route.providerMetadataKey ?? String(request.model.provider) const omitCallIds = omitsFunctionCallIds(request.model.id) diff --git a/packages/ai/src/protocols/google-interactions.ts b/packages/ai/src/protocols/google-interactions.ts index 4e74f0758e1..6cc040defe4 100644 --- a/packages/ai/src/protocols/google-interactions.ts +++ b/packages/ai/src/protocols/google-interactions.ts @@ -182,7 +182,7 @@ type StepResult = readonly [ParserState, ReadonlyArray] // ============================================================================= // Request Body Construction // ============================================================================= -const mediaContent = Effect.fn("GoogleInteractions.mediaContent")(function* (asset: Media.Asset) { +const mediaContent = Effect.fnUntraced(function* (asset: Media.Asset) { if ( asset.kind !== "image" && asset.kind !== "audio" && @@ -208,7 +208,7 @@ const signature = (metadata: ProviderMetadata | undefined, key: string) => { : undefined } -const lowerMessages = Effect.fn("GoogleInteractions.lowerMessages")(function* (request: LLMRequest) { +const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest) { const steps: InputStep[] = [] const key = request.model.route.providerMetadataKey ?? String(request.model.provider) for (const message of request.messages) { @@ -275,7 +275,7 @@ const lowerMessages = Effect.fn("GoogleInteractions.lowerMessages")(function* (r return steps }) -const lowerToolResult = Effect.fn("GoogleInteractions.lowerToolResult")(function* (part: ToolResultPart) { +const lowerToolResult = Effect.fnUntraced(function* (part: ToolResultPart) { if (part.result.type === "json" && ProviderShared.isRecord(part.result.value)) return part.result.value if (part.result.type !== "content") return ProviderShared.toolResultText(part) diff --git a/packages/ai/src/protocols/mistral-chat.ts b/packages/ai/src/protocols/mistral-chat.ts index 46e16f8b52f..37a206beb87 100644 --- a/packages/ai/src/protocols/mistral-chat.ts +++ b/packages/ai/src/protocols/mistral-chat.ts @@ -226,7 +226,7 @@ const MistralEvent = Schema.StructWithRest( type MistralEvent = Schema.Schema.Type const MistralStreamEvent = Schema.Union([Schema.Literal(DONE), Protocol.jsonEvent(MistralEvent)]) -const lowerMedia = Effect.fn("MistralChat.lowerMedia")(function* (part: MediaPart) { +const lowerMedia = Effect.fnUntraced(function* (part: MediaPart) { const mime = part.media.mediaType.toLowerCase() const url = ProviderShared.mediaUrl(part.media) ?? @@ -236,7 +236,7 @@ const lowerMedia = Effect.fn("MistralChat.lowerMedia")(function* (part: MediaPar return yield* ProviderShared.invalidRequest(`Mistral Chat does not support media type ${part.media.mediaType}`) }) -const lowerUser = Effect.fn("MistralChat.lowerUser")(function* (message: LLMRequest["messages"][number]) { +const lowerUser = Effect.fnUntraced(function* (message: LLMRequest["messages"][number]) { const content: MistralUserContent[] = [] for (const part of message.content) { if (part.type === "text") { @@ -260,7 +260,7 @@ const lowerToolCall = (part: ToolCallPart, normalizeID: (id: string) => string): function: { name: part.name, arguments: ProviderShared.encodeJson(part.input) }, }) -const lowerAssistant = Effect.fn("MistralChat.lowerAssistant")(function* ( +const lowerAssistant = Effect.fnUntraced(function* ( message: LLMRequest["messages"][number], normalizeID: (id: string) => string, prefix: boolean, @@ -298,7 +298,7 @@ const lowerAssistant = Effect.fn("MistralChat.lowerAssistant")(function* ( } }) -const lowerToolResults = Effect.fn("MistralChat.lowerToolResults")(function* ( +const lowerToolResults = Effect.fnUntraced(function* ( message: LLMRequest["messages"][number], normalizeID: (id: string) => string, ) { @@ -335,7 +335,7 @@ const lowerToolResults = Effect.fn("MistralChat.lowerToolResults")(function* ( return output }) -const lowerMessages = Effect.fn("MistralChat.lowerMessages")(function* (request: LLMRequest) { +const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest) { const normalizeID = MistralToolID.normalizer(request) const messages: MistralMessage[] = request.system.length === 0 diff --git a/packages/ai/src/protocols/open-responses.ts b/packages/ai/src/protocols/open-responses.ts index 30a0c055271..5570b357082 100644 --- a/packages/ai/src/protocols/open-responses.ts +++ b/packages/ai/src/protocols/open-responses.ts @@ -442,7 +442,7 @@ interface ReasoningStreamItem { // ============================================================================= // Request Lowering // ============================================================================= -export const lowerTool = Effect.fn("OpenResponses.lowerTool")(function* (protocolName: string, tool: ToolDefinition) { +export const lowerTool = Effect.fnUntraced(function* (protocolName: string, tool: ToolDefinition) { if (tool.native !== undefined) return yield* ProviderShared.invalidRequest(`${protocolName} does not support provider-native tool ${tool.name}`) return { @@ -507,7 +507,10 @@ const lowerReasoning = (part: ReasoningPart, providerMetadataKey: string): OpenR } } -const lowerMedia = Effect.fn("OpenResponses.lowerMedia")(function* ( +const decodeImageDetail = ProviderShared.validateWith(Schema.decodeUnknownEffect(OpenResponsesInputImage.fields.detail)) +const decodeMessageMetadata = ProviderShared.validateWith(Schema.decodeUnknownEffect(MessageMetadata)) + +const lowerMedia = Effect.fnUntraced(function* ( part: MediaPart, request: LLMRequest, adapter: ProviderAdapter, @@ -516,9 +519,8 @@ const lowerMedia = Effect.fn("OpenResponses.lowerMedia")(function* ( const media = part.media.inline() const providerMedia = adapter.lowerMedia?.({ part, media, request }) if (providerMedia) return providerMedia - const detail = yield* ProviderShared.validateWith(Schema.decodeUnknownEffect(OpenResponsesInputImage.fields.detail))( - part.providerMetadata?.[metadataKey(request.model)]?.detail, - ) + const rawDetail = part.providerMetadata?.[metadataKey(request.model)]?.detail + const detail = rawDetail === undefined ? undefined : yield* decodeImageDetail(rawDetail) const mime = part.media.mediaType.toLowerCase() const url = ProviderShared.mediaUrl(part.media) const location = url ?? (yield* ProviderShared.requireInlineMedia(adapter.name, part.media)).dataUrl @@ -591,7 +593,7 @@ const lowerToolResultOutput = Effect.fnUntraced(function* ( const DEFAULT_EFFORT = "medium" -const lowerMessages = Effect.fn("OpenResponses.lowerMessages")(function* ( +const lowerMessages = Effect.fnUntraced(function* ( request: LLMRequest, adapter: ProviderAdapter, ) { @@ -599,9 +601,8 @@ const lowerMessages = Effect.fn("OpenResponses.lowerMessages")(function* ( const providerMetadataKey = metadataKey(request.model) for (const message of request.messages) { - const metadata = yield* ProviderShared.validateWith( - Schema.decodeUnknownEffect(Schema.UndefinedOr(MessageMetadata)), - )(message.providerMetadata?.[providerMetadataKey]) + const rawMetadata = message.providerMetadata?.[providerMetadataKey] + const metadata = rawMetadata === undefined ? undefined : yield* decodeMessageMetadata(rawMetadata) if (message.role === "system") { const update = effortUpdate(message) if (update) { @@ -755,7 +756,7 @@ const lowerMessages = Effect.fn("OpenResponses.lowerMessages")(function* ( return input }) -export const lowerConversation = Effect.fn("OpenResponses.lowerConversation")(function* ( +export const lowerConversation = Effect.fnUntraced(function* ( request: LLMRequest, adapter: ProviderAdapter, ) { diff --git a/packages/ai/src/protocols/openai-chat.ts b/packages/ai/src/protocols/openai-chat.ts index 5bc4fdcbddf..5448ad0fa8d 100644 --- a/packages/ai/src/protocols/openai-chat.ts +++ b/packages/ai/src/protocols/openai-chat.ts @@ -360,7 +360,7 @@ const lowerToolCall = ( extra_content: decodeExtraContent(part.providerMetadata?.[options.providerMetadataKey]?.extraContent), }) -const lowerMedia = Effect.fn("OpenAIChat.lowerMedia")(function* (part: MediaPart) { +const lowerMedia = Effect.fnUntraced(function* (part: MediaPart) { // Chat Completions accepts PDFs, and no other documents, as inline `file` parts; file URLs are not supported. if (part.media.mediaType.toLowerCase() === "application/pdf") return { @@ -406,7 +406,7 @@ const lowerReasoningDetail = (detail: ReasoningDetail) => { const isKimiDetail = (detail: { readonly type: string }) => detail.type === "summary" || detail.type === "encrypted" -const lowerUserMessage = Effect.fn("OpenAIChat.lowerUserMessage")(function* ( +const lowerUserMessage = Effect.fnUntraced(function* ( message: OpenAIChatRequestMessage, options: LoweringOptions, ) { @@ -430,7 +430,7 @@ const lowerUserMessage = Effect.fn("OpenAIChat.lowerUserMessage")(function* ( return { role: "user" as const, content } }) -const lowerAssistantMessage = Effect.fn("OpenAIChat.lowerAssistantMessage")(function* ( +const lowerAssistantMessage = Effect.fnUntraced(function* ( message: OpenAIChatRequestMessage, configuredField: string | undefined, requireReasoning: boolean, @@ -495,7 +495,7 @@ const lowerAssistantMessage = Effect.fn("OpenAIChat.lowerAssistantMessage")(func return { ...result, [field]: reasoningText } }) -const lowerToolMessages = Effect.fn("OpenAIChat.lowerToolMessages")(function* ( +const lowerToolMessages = Effect.fnUntraced(function* ( message: OpenAIChatRequestMessage, options: LoweringOptions, ) { @@ -532,7 +532,7 @@ const toolMessage = (toolCallID: string, text: string, cacheControl: OpenAIChatC content: cacheControl === undefined ? text : [{ type: "text" as const, text, cache_control: cacheControl }], }) -const lowerMessage = Effect.fn("OpenAIChat.lowerMessage")(function* ( +const lowerMessage = Effect.fnUntraced(function* ( message: OpenAIChatRequestMessage, reasoningField: string | undefined, requireReasoning: boolean, @@ -544,7 +544,7 @@ const lowerMessage = Effect.fn("OpenAIChat.lowerMessage")(function* ( return (yield* lowerToolMessages(message, options)).messages }) -const lowerMessages = Effect.fn("OpenAIChat.lowerMessages")(function* (request: LLMRequest, options: LoweringOptions) { +const lowerMessages = Effect.fnUntraced(function* (request: LLMRequest, options: LoweringOptions) { const system: OpenAIChatMessage[] = request.system.length === 0 ? [] diff --git a/packages/ai/src/protocols/openai-responses.ts b/packages/ai/src/protocols/openai-responses.ts index 60a76e47858..41436c8aa9d 100644 --- a/packages/ai/src/protocols/openai-responses.ts +++ b/packages/ai/src/protocols/openai-responses.ts @@ -157,7 +157,7 @@ const nativeImageTool = (tool: ToolDefinition) => { return Schema.is(OpenAIResponsesImageGenerationTool)(native) ? native : undefined } -const lowerTool = Effect.fn("OpenAIResponses.lowerTool")(function* (tool: ToolDefinition) { +const lowerTool = Effect.fnUntraced(function* (tool: ToolDefinition) { const native = nativeImageToolInput(tool) if (native !== undefined) { if (Schema.is(OpenAIResponsesImageGenerationTool)(native)) return native @@ -168,7 +168,7 @@ const lowerTool = Effect.fn("OpenAIResponses.lowerTool")(function* (tool: ToolDe // Native namespaces hold only function tools, so deeper levels flatten into // the leaf names the same way non-native protocols flatten the whole tree. -const lowerToolEntry = Effect.fn("OpenAIResponses.lowerToolEntry")(function* (tool: ToolEntry) { +const lowerToolEntry = Effect.fnUntraced(function* (tool: ToolEntry) { if (tool.type === "tool") return yield* lowerTool(tool) // OpenAI requires a namespace description; fall back to a generic one so a // missing description never blocks the request. diff --git a/packages/ai/src/protocols/shared.ts b/packages/ai/src/protocols/shared.ts index bd8d2538d6a..0cfd5f4bcac 100644 --- a/packages/ai/src/protocols/shared.ts +++ b/packages/ai/src/protocols/shared.ts @@ -154,7 +154,7 @@ export const wrapSystemUpdate = (parts: ReadonlyArray<{ readonly text: string }> * raw retrieved, tool, or web content into privileged updates: keep untrusted * data in ordinary user/tool messages instead. */ -export const systemUpdateText = Effect.fn("ProviderShared.systemUpdateText")(function* ( +export const systemUpdateText = Effect.fnUntraced(function* ( route: string, message: LLMRequest["messages"][number], ) { @@ -167,7 +167,7 @@ export const systemUpdateText = Effect.fn("ProviderShared.systemUpdateText")(fun }) /** Lower an unsupported privileged update into visible, in-order user text. */ -export const wrappedSystemUpdate = Effect.fn("ProviderShared.wrappedSystemUpdate")(function* ( +export const wrappedSystemUpdate = Effect.fnUntraced(function* ( route: string, message: LLMRequest["messages"][number], ) { diff --git a/packages/ai/src/protocols/utils/bedrock-media.ts b/packages/ai/src/protocols/utils/bedrock-media.ts index 9cc5730b8db..f93e3c86887 100644 --- a/packages/ai/src/protocols/utils/bedrock-media.ts +++ b/packages/ai/src/protocols/utils/bedrock-media.ts @@ -76,7 +76,7 @@ function documentName(filename: string | undefined, names: Set) { return name } -const mediaBase64 = Effect.fn("BedrockMedia.mediaBase64")(function* (part: MediaPart) { +const mediaBase64 = Effect.fnUntraced(function* (part: MediaPart) { const media = yield* ProviderShared.requireInlineMedia("Bedrock Converse", part.media) const bytes = yield* Effect.fromResult(Encoding.decodeBase64(media.base64)).pipe( Effect.mapError((cause) => @@ -91,7 +91,7 @@ const mediaBase64 = Effect.fn("BedrockMedia.mediaBase64")(function* (part: Media // document block. Image MIME types not in `IMAGE_FORMATS` (e.g. `image/svg+xml`) // get an image-specific error so the caller knows it's a format-support issue, // not a kind-detection issue. -export const lower = Effect.fn("BedrockMedia.lower")(function* (part: MediaPart, documentNames: Set) { +export const lower = Effect.fnUntraced(function* (part: MediaPart, documentNames: Set) { const mime = part.media.mediaType.toLowerCase() const imageFormat = IMAGE_FORMATS[mime as keyof typeof IMAGE_FORMATS] if (imageFormat) { diff --git a/packages/ai/src/route/framing.ts b/packages/ai/src/route/framing.ts index c53f0a68eee..f098d3bd564 100644 --- a/packages/ai/src/route/framing.ts +++ b/packages/ai/src/route/framing.ts @@ -1,5 +1,5 @@ import { Effect, Stream } from "effect" -import { makeParser, type Event } from "effect/unstable/encoding/Sse" +import { makeParser } from "effect/unstable/encoding/Sse" import { AIError, InvalidProviderOutputError } from "../schema/index.js" /** @@ -42,43 +42,39 @@ export const sseFraming = ( Stream.decodeText(), Stream.mapAccumEffect( () => { - const output: Event[] = [] + const output: string[] = [] return { output, parser: makeParser((event) => { - if (event._tag === "Event") output.push(event) + if ( + event._tag === "Event" && + (events === undefined || events.has(event.event)) && + event.data.length > 0 && + // Some OpenAI-compatible proxies serialize an empty flush as a bare + // `data: null`, between events or after `[DONE]`. No protocol has a + // null event, so it carries nothing and must not abort the stream. + event.data !== "null" && + // Vertex AI partner models (e.g. `xai/grok-4.6`) send their SSE + // keepalive comment as `data: : keepalive` while reasoning. + event.data !== ": keepalive" && + (event.data !== "[DONE]" || includeDone || (events !== undefined && event.event !== "message")) + ) + output.push(event.data) }), } }, - (state, chunk) => - Effect.gen(function* () { - const error = state.parser.feed(chunk) - if (error) - return yield* new AIError({ - reason: new InvalidProviderOutputError({ - route: "sse", - message: error.message, - body: chunk, - cause: error, - }), - }) - return [state, state.output.splice(0)] as const - }), + (state, chunk) => { + const error = state.parser.feed(chunk) + if (!error) return Effect.succeed([state, state.output.splice(0)] as const) + const reason = new InvalidProviderOutputError({ + route: "sse", + message: error.message, + body: chunk, + cause: error, + }) + return Effect.fail(new AIError({ reason })) + }, ), - Stream.filter( - (event) => - (events === undefined || events.has(event.event)) && - event.data.length > 0 && - // Some OpenAI-compatible proxies serialize an empty flush as a bare - // `data: null`, between events or after `[DONE]`. No protocol has a - // null event, so it carries nothing and must not abort the stream. - event.data !== "null" && - // Vertex AI partner models (e.g. `xai/grok-4.6`) send their SSE - // keepalive comment as `data: : keepalive` while reasoning. - event.data !== ": keepalive" && - (event.data !== "[DONE]" || includeDone || (events !== undefined && event.event !== "message")), - ), - Stream.map((event) => event.data), ) /** Server-Sent Events framing. Used by every JSON-streaming HTTP provider. */