@tanstack/openai-base 0.6.0 → 0.7.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.
@@ -826,13 +826,6 @@ class OpenAIBaseChatCompletionsTextAdapter extends BaseTextAdapter {
826
826
  ...modelOptions,
827
827
  model: options.model,
828
828
  messages,
829
- ...options.temperature !== void 0 && {
830
- temperature: options.temperature
831
- },
832
- ...options.maxTokens !== void 0 && {
833
- max_tokens: options.maxTokens
834
- },
835
- ...options.topP !== void 0 && { top_p: options.topP },
836
829
  // Conditional spread: `tools: undefined` would clobber any
837
830
  // modelOptions.tools the caller set above.
838
831
  ...tools && tools.length > 0 && {
@@ -1 +1 @@
1
- {"version":3,"file":"chat-completions-text.js","sources":["../../../src/adapters/chat-completions-text.ts"],"sourcesContent":["import { EventType, normalizeSystemPrompts } from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport {\n toRunErrorPayload,\n toRunErrorRawEvent,\n} from '@tanstack/ai/adapter-internals'\nimport { generateId, transformNullsToUndefined } from '@tanstack/ai-utils'\nimport { extractRequestOptions } from '../utils/request-options'\nimport { makeStructuredOutputCompatible } from '../utils/schema-converter'\nimport { buildChatCompletionsUsage } from '../usage'\nimport { convertToolsToChatCompletionsFormat } from './chat-completions-tool-converter'\nimport type OpenAI from 'openai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type {\n ChatCompletionChunk,\n ChatCompletionContentPart,\n ChatCompletionCreateParamsStreaming,\n ChatCompletionMessageParam,\n} from 'openai/resources/chat/completions/completions'\nimport type {\n ContentPart,\n DefaultMessageMetadataByModality,\n Modality,\n ModelMessage,\n RunFinishedEvent,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\n\n/**\n * Shared implementation of the OpenAI Chat Completions API. Holds the\n * stream-accumulator + AG-UI lifecycle logic and calls the OpenAI SDK\n * directly. Subclasses (ai-openai, ai-grok, ai-groq) construct an OpenAI\n * client with their provider-specific `baseURL` / headers and pass it in.\n */\nexport abstract class OpenAIBaseChatCompletionsTextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, unknown> = Record<string, unknown>,\n TInputModalities extends ReadonlyArray<Modality> = ReadonlyArray<Modality>,\n TMessageMetadata extends DefaultMessageMetadataByModality =\n DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n TMessageMetadata,\n TToolCapabilities\n> {\n override readonly kind = 'text' as const\n readonly name: string\n protected client: OpenAI\n\n constructor(model: TModel, name: string, client: OpenAI) {\n super({}, model)\n this.name = name\n this.client = client\n }\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n // AG-UI lifecycle tracking (mutable state object for ESLint compatibility)\n const aguiState = {\n runId: generateId(this.name),\n threadId: options.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n hasEmittedRunStarted: false,\n }\n\n try {\n // mapOptionsToRequest can throw (e.g. fail-loud guards in convertMessage\n // for empty content or unsupported parts). Keep it inside the try so\n // those failures surface as a single RUN_ERROR event, matching every\n // other failure mode here — callers iterating chatStream then only need\n // one error-handling path instead of both a try/catch around iteration\n // and a RUN_ERROR handler.\n const requestParams = this.mapOptionsToRequest(options)\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const stream = await this.client.chat.completions.create(\n {\n ...requestParams,\n stream: true,\n stream_options: { include_usage: true },\n },\n extractRequestOptions(options.request),\n )\n\n yield* this.processStreamChunks(stream, options, aguiState)\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n const rawEvent = toRunErrorRawEvent(error)\n\n // Emit RUN_STARTED if not yet emitted\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: options.model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n // Emit AG-UI RUN_ERROR. Conditional `code` spread keeps the wire\n // shape spec-compliant under `exactOptionalPropertyTypes`: AG-UI's\n // `RunErrorEvent.code` is `string?` (absent vs explicit `undefined`\n // matter), so we omit the key when there's no code.\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n code: errorPayload.code,\n // Forward the provider's structured error body so consumers can recover\n // the upstream detail the `{ message, code }` payload drops. Omitted\n // when the error carried no provider body (see toRunErrorRawEvent).\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n code: errorPayload.code,\n },\n }\n\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n }\n }\n\n /**\n * Generate structured output using the provider's JSON Schema response format.\n * Uses stream: false to get the complete response in one call.\n *\n * OpenAI-compatible APIs have strict requirements for structured output:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for all objects\n *\n * The outputSchema is already JSON Schema (converted in the ai layer).\n * We apply provider-specific transformations for structured output compatibility.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapOptionsToRequest(chatOptions)\n\n const jsonSchema = this.makeStructuredOutputCompatible(\n outputSchema,\n outputSchema.required,\n )\n\n try {\n // Strip stream_options which is only valid for streaming calls\n const {\n stream_options: _,\n stream: __,\n ...cleanParams\n } = requestParams as any\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const response = await this.client.chat.completions.create(\n {\n ...cleanParams,\n stream: false,\n response_format: {\n type: 'json_schema',\n json_schema: {\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n },\n extractRequestOptions(chatOptions.request),\n )\n\n // Extract text content from the response. Fail loud on empty content\n // rather than letting it cascade into a JSON-parse error on '' — the\n // root cause (the model returned no content for the structured request)\n // is then visible in logs.\n const rawText = response.choices[0]?.message.content\n if (typeof rawText !== 'string' || rawText.length === 0) {\n throw new Error(\n `${this.name}.structuredOutput: response contained no content`,\n )\n }\n\n // Parse the JSON response\n let parsed: unknown\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to parse structured output as JSON. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n\n // Transform null values to undefined to match original Zod schema expectations\n // Provider returns null for optional fields we made nullable in the schema.\n // Subclasses can override `transformStructuredOutput` to skip this — e.g.\n // OpenRouter historically passed nulls through unchanged.\n const transformed = this.transformStructuredOutput(parsed)\n\n return {\n data: transformed,\n rawText,\n }\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Stream structured output. Single Chat Completions request with\n * `response_format: json_schema` + `stream: true`. Emits the standard\n * AG-UI lifecycle (`RUN_STARTED` → `REASONING_*?` → `TEXT_MESSAGE_*`\n * carrying raw JSON deltas → terminal `CUSTOM 'structured-output.complete'`\n * → `RUN_FINISHED`). Subclasses use the same SDK-call / reasoning /\n * structured-output-transform hooks as `chatStream` / `structuredOutput` —\n * no per-subclass override should be needed.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapOptionsToRequest(chatOptions)\n\n const jsonSchema = this.makeStructuredOutputCompatible(\n outputSchema,\n outputSchema.required,\n )\n\n const timestamp = Date.now()\n const aguiState = {\n runId: generateId(this.name),\n threadId: chatOptions.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n timestamp,\n hasEmittedRunStarted: false,\n }\n\n let accumulatedContent = ''\n let accumulatedReasoning = ''\n let hasEmittedTextMessageStart = false\n let reasoningMessageId: string | undefined\n let hasClosedReasoning = false\n let stepId: string | undefined\n let lastModel: string | undefined\n let lastUsage:\n | OpenAI.Chat.Completions.ChatCompletionChunk['usage']\n | undefined\n\n const closeReasoningLifecycle = function* (this: {\n name: string\n }): Generator<StreamChunk> {\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model: lastModel || chatOptions.model,\n timestamp,\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model: lastModel || chatOptions.model,\n timestamp,\n }\n if (stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model: lastModel || chatOptions.model,\n timestamp,\n content: accumulatedReasoning,\n }\n }\n }\n }.bind(this)\n\n try {\n // Strip stream_options + tools from the base request. Structured output\n // sends `response_format: json_schema` and doesn't carry tools — keeping\n // them in the request can confuse strict-mode validation upstream.\n const {\n stream_options: _so,\n stream: _s,\n tools: _t,\n ...cleanParams\n } = requestParams\n\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n\n const stream = await this.client.chat.completions.create(\n {\n ...cleanParams,\n stream: true,\n stream_options: { include_usage: true },\n response_format: {\n type: 'json_schema',\n json_schema: {\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n },\n extractRequestOptions(chatOptions.request),\n )\n\n for await (const chunk of stream) {\n const choiceForLog = chunk.choices[0]\n chatOptions.logger.provider(\n `provider=${this.name} finish_reason=${choiceForLog?.finish_reason ?? 'none'} hasContent=${!!choiceForLog?.delta.content} hasUsage=${!!chunk.usage}`,\n { provider: this.name, model: chunk.model },\n )\n\n if (chunk.model) lastModel = chunk.model\n\n // Usage may arrive on a chunk with empty `choices` (OpenAI's\n // include_usage terminal chunk) or piggybacked on a finish chunk\n // (`x_groq.usage` on Groq). Capture from either independent of\n // choices[0].\n const usage =\n chunk.usage ??\n (chunk as { x_groq?: { usage?: typeof chunk.usage } }).x_groq?.usage\n if (usage) lastUsage = usage\n\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chunk.model || chatOptions.model,\n timestamp,\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n // Reasoning (via the extractReasoning hook — same hook as chatStream).\n const reasoning = this.extractReasoning(chunk)\n if (reasoning && reasoning.text) {\n if (!reasoningMessageId) {\n reasoningMessageId = generateId(this.name)\n stepId = generateId(this.name)\n yield {\n type: EventType.REASONING_START,\n messageId: reasoningMessageId,\n model: chunk.model || chatOptions.model,\n timestamp,\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning' as const,\n model: chunk.model || chatOptions.model,\n timestamp,\n }\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: chunk.model || chatOptions.model,\n timestamp,\n stepType: 'thinking',\n }\n }\n accumulatedReasoning += reasoning.text\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta: reasoning.text,\n model: chunk.model || chatOptions.model,\n timestamp,\n }\n }\n\n const choice = chunk.choices[0]\n if (!choice) continue\n\n const deltaContent = choice.delta.content\n if (deltaContent) {\n yield* closeReasoningLifecycle()\n\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: chunk.model || chatOptions.model,\n timestamp,\n role: 'assistant',\n }\n }\n\n accumulatedContent += deltaContent\n\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model: chunk.model || chatOptions.model,\n timestamp,\n delta: deltaContent,\n content: accumulatedContent,\n }\n }\n }\n\n // Finalisation: close any open lifecycle, parse + validate, emit\n // terminal events. This block always runs unless the loop threw — abort\n // and SDK errors land in the catch block below.\n yield* closeReasoningLifecycle()\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: lastModel || chatOptions.model,\n timestamp,\n }\n }\n\n if (accumulatedContent.length === 0) {\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model: lastModel || chatOptions.model,\n timestamp,\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n error: {\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n },\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedContent)\n } catch {\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model: lastModel || chatOptions.model,\n timestamp,\n message: `Failed to parse structured output as JSON. Content: ${accumulatedContent.slice(0, 200)}${accumulatedContent.length > 200 ? '...' : ''}`,\n code: 'parse-error',\n error: {\n message: 'Failed to parse structured output as JSON',\n code: 'parse-error',\n },\n }\n return\n }\n\n const transformed = this.transformStructuredOutput(parsed)\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: transformed,\n raw: accumulatedContent,\n ...(accumulatedReasoning ? { reasoning: accumulatedReasoning } : {}),\n },\n model: lastModel || chatOptions.model,\n timestamp,\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: lastModel || chatOptions.model,\n timestamp,\n finishReason: 'stop',\n ...(lastUsage && {\n usage: buildChatCompletionsUsage(lastUsage),\n }),\n }\n } catch (error: unknown) {\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chatOptions.model,\n timestamp,\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n const isAbort = this.isAbortError(error)\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes`: AG-UI's `RunErrorEvent.code` is `string?`\n // (absent vs explicit `undefined` matter).\n const resolvedCode = isAbort ? 'aborted' : errorPayload.code\n const rawEvent = isAbort ? undefined : toRunErrorRawEvent(error)\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model: lastModel || chatOptions.model,\n timestamp,\n message: errorPayload.message,\n ...(resolvedCode !== undefined && { code: resolvedCode }),\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n ...(resolvedCode !== undefined && { code: resolvedCode }),\n },\n }\n\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n }\n }\n\n /**\n * Cross-SDK abort detection for `structuredOutputStream`. Default duck-types\n * on `name === 'APIUserAbortError'` (OpenAI SDK), `code === 'ERR_CANCELED'`,\n * and standard `AbortError`s. Subclasses with proprietary error types (e.g.\n * `@openrouter/sdk`'s `RequestAbortedError`) override to extend the check.\n */\n protected isAbortError(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false\n const e = error as { name?: unknown; code?: unknown }\n return (\n e.name === 'APIUserAbortError' ||\n e.name === 'AbortError' ||\n e.code === 'ERR_CANCELED'\n )\n }\n\n /**\n * Applies provider-specific transformations for structured output compatibility.\n * Override this in subclasses to handle provider-specific quirks.\n */\n protected makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n ): Record<string, any> {\n return makeStructuredOutputCompatible(schema, originalRequired)\n }\n\n /**\n * Extract reasoning content from a stream chunk. Default returns\n * `undefined` because the OpenAI Chat Completions chunk shape doesn't\n * carry reasoning. The chunk param is typed `unknown` so an override can\n * narrow to its own SDK chunk type without an `as` dance — the base only\n * passes through `processStreamChunks`'s structurally-iterated chunk.\n */\n protected extractReasoning(_chunk: unknown): { text: string } | undefined {\n return undefined\n }\n\n /**\n * Final shaping pass applied to parsed structured-output JSON before it is\n * returned to the caller. Default converts `null` values to `undefined` so\n * the result aligns with the original Zod schema's optional-field\n * semantics. Subclasses with different conventions (OpenRouter historically\n * preserves nulls) can override.\n */\n protected transformStructuredOutput(parsed: unknown): unknown {\n return transformNullsToUndefined(parsed)\n }\n\n /**\n * Processes streamed chunks from the Chat Completions API and yields AG-UI events.\n * Override this in subclasses to handle provider-specific stream behavior.\n */\n protected async *processStreamChunks(\n stream: AsyncIterable<ChatCompletionChunk>,\n options: TextOptions,\n aguiState: {\n runId: string\n threadId: string\n messageId: string\n hasEmittedRunStarted: boolean\n },\n ): AsyncIterable<StreamChunk> {\n let accumulatedContent = ''\n let hasEmittedTextMessageStart = false\n let lastModel: string | undefined\n // Track usage from any chunk that carries it. With\n // `stream_options: { include_usage: true }` OpenAI emits a terminal chunk\n // whose `choices` is `[]` and only the `usage` field is populated; the\n // earlier `finish_reason` chunk does NOT include token counts. We must\n // therefore defer RUN_FINISHED until the iterator is exhausted so we can\n // pick up usage from the trailing chunk regardless of arrival order.\n let lastUsage: ChatCompletionChunk['usage'] | undefined\n let pendingFinishReason:\n | ChatCompletionChunk['choices'][number]['finish_reason']\n | undefined\n\n // Track tool calls being streamed (arguments come in chunks)\n const toolCallsInProgress = new Map<\n number,\n {\n id: string\n name: string\n arguments: string\n started: boolean // Track if TOOL_CALL_START has been emitted\n }\n >()\n\n // Reasoning lifecycle (driven by extractReasoning() hook — see method\n // docs). The base wire format (OpenAI Chat Completions) has no reasoning,\n // so these stay unused for openai/grok/groq. OpenRouter etc. opt in.\n let reasoningMessageId: string | undefined\n let hasClosedReasoning = false\n // Legacy STEP_STARTED/STEP_FINISHED pair emitted alongside REASONING_*\n // for back-compat with consumers (UI, devtools) that haven't migrated\n // to the spec REASONING_* events yet.\n let stepId: string | undefined\n let accumulatedReasoning = ''\n // Track whether ANY tool call lifecycle was actually completed across the\n // entire stream. Lets us downgrade a `tool_calls` finish_reason to `stop`\n // when the upstream signalled tool calls but never produced a complete\n // start/end pair — emitting RUN_FINISHED { finishReason: 'tool_calls' }\n // with no matching TOOL_CALL_END would leave consumers waiting for tool\n // results that never arrive.\n let emittedAnyToolCallEnd = false\n\n try {\n for await (const chunk of stream) {\n const choiceForLog = chunk.choices[0]\n options.logger.provider(\n `provider=${this.name} finish_reason=${choiceForLog?.finish_reason ?? 'none'} hasContent=${!!choiceForLog?.delta.content} hasToolCalls=${!!choiceForLog?.delta.tool_calls} hasUsage=${!!chunk.usage}`,\n { provider: this.name, model: chunk.model },\n )\n\n // Capture usage from any chunk (including the terminal usage-only\n // chunk emitted when `stream_options.include_usage` is on).\n if (chunk.usage) {\n lastUsage = chunk.usage\n }\n if (chunk.model) {\n lastModel = chunk.model\n }\n\n // Emit RUN_STARTED on the first chunk of any kind so callers see a\n // run lifecycle even on streams that arrive entirely as usage-only\n // (no choices). Without this, a usage-first stream would skip\n // RUN_STARTED via `if (!choice) continue` below and the post-loop\n // synthetic block would also skip RUN_FINISHED (it gates on\n // `hasEmittedRunStarted`).\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n // Reasoning content (extractReasoning() hook). Run before reading\n // choice/delta so reasoning-only chunks (no `choices`) still drive\n // the REASONING_* lifecycle on providers that send reasoning out of\n // band. The base default returns undefined.\n const reasoning = this.extractReasoning(chunk)\n if (reasoning && reasoning.text) {\n if (!reasoningMessageId) {\n reasoningMessageId = generateId(this.name)\n stepId = generateId(this.name)\n yield {\n type: EventType.REASONING_START,\n messageId: reasoningMessageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning' as const,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n // Legacy STEP_STARTED (single emission, paired with the\n // STEP_FINISHED below when reasoning closes).\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n accumulatedReasoning += reasoning.text\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta: reasoning.text,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n }\n\n const choice = chunk.choices[0]\n\n if (!choice) continue\n\n const delta = choice.delta\n const deltaContent = delta.content\n const deltaToolCalls = delta.tool_calls\n\n // Handle content delta\n if (deltaContent) {\n // Close reasoning before text starts so consumers see a clean\n // REASONING_END before any TEXT_MESSAGE_START.\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n if (stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n content: accumulatedReasoning,\n }\n }\n }\n\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n }\n\n accumulatedContent += deltaContent\n\n // Emit AG-UI TEXT_MESSAGE_CONTENT\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n delta: deltaContent,\n content: accumulatedContent,\n }\n }\n\n // Handle tool calls - they come in as deltas\n if (deltaToolCalls) {\n for (const toolCallDelta of deltaToolCalls) {\n const index = toolCallDelta.index\n\n // Initialize or update the tool call in progress\n let toolCall = toolCallsInProgress.get(index)\n if (!toolCall) {\n toolCall = {\n id: toolCallDelta.id || '',\n name: toolCallDelta.function?.name || '',\n arguments: '',\n started: false,\n }\n toolCallsInProgress.set(index, toolCall)\n }\n\n // Update with any new data from the delta\n if (toolCallDelta.id) {\n toolCall.id = toolCallDelta.id\n }\n if (toolCallDelta.function?.name) {\n toolCall.name = toolCallDelta.function.name\n }\n if (toolCallDelta.function?.arguments) {\n toolCall.arguments += toolCallDelta.function.arguments\n }\n\n // Emit TOOL_CALL_START when we have id and name\n if (toolCall.id && toolCall.name && !toolCall.started) {\n toolCall.started = true\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n index,\n }\n }\n\n // Emit TOOL_CALL_ARGS for argument deltas\n if (toolCallDelta.function?.arguments && toolCall.started) {\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: toolCall.id,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n delta: toolCallDelta.function.arguments,\n }\n }\n }\n }\n\n // Handle finish reason. We DO emit TOOL_CALL_END and TEXT_MESSAGE_END\n // here because the corresponding _START events have already fired,\n // and tool execution downstream wants to begin as soon as possible.\n // RUN_FINISHED is deferred until the iterator is fully exhausted so\n // we can capture the trailing usage chunk that arrives AFTER this\n // chunk when stream_options.include_usage is on.\n if (choice.finish_reason) {\n if (\n choice.finish_reason === 'tool_calls' ||\n toolCallsInProgress.size > 0\n ) {\n for (const [, toolCall] of toolCallsInProgress) {\n // Skip tool calls that never emitted TOOL_CALL_START — emitting\n // a stray TOOL_CALL_END here would violate AG-UI lifecycle\n // (END without matching START) for partial deltas where the\n // upstream never sent both id and name.\n if (!toolCall.started) continue\n\n // Parse arguments for TOOL_CALL_END. Surface parse failures via\n // the logger so a model emitting malformed JSON for tool args\n // is debuggable instead of silently invoking the tool with {}.\n // Non-object JSON (e.g. a bare string or number) is also coerced\n // to {} so downstream tool execution doesn't receive a primitive\n // input, mirroring the Responses adapter's guard.\n let parsedInput: unknown = {}\n if (toolCall.arguments) {\n try {\n const parsed: unknown = JSON.parse(toolCall.arguments)\n parsedInput =\n parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${toolCall.name} (${toolCall.id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: toolCall.id,\n toolName: toolCall.name,\n rawArguments: toolCall.arguments,\n },\n )\n parsedInput = {}\n }\n }\n\n // Emit AG-UI TOOL_CALL_END\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n emittedAnyToolCallEnd = true\n }\n // Clear tool-call state after emission so a subsequent\n // `finish_reason: 'stop'` chunk (or the post-loop synthetic\n // block) doesn't see lingering entries and misreport the finish.\n toolCallsInProgress.clear()\n }\n\n // Emit TEXT_MESSAGE_END if we had text content\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n hasEmittedTextMessageStart = false\n }\n\n // Remember the upstream finish_reason; RUN_FINISHED is emitted at\n // end-of-stream so we pick up the trailing usage-only chunk too.\n pendingFinishReason = choice.finish_reason\n }\n }\n\n // Emit a single terminal RUN_FINISHED after the iterator is exhausted.\n // This both delivers accurate token counts (the trailing usage chunk\n // may arrive AFTER the finish_reason chunk) and gives consumers a\n // guaranteed terminal event even when the upstream cuts off mid-stream\n // (no finish_reason chunk ever arrives).\n if (aguiState.hasEmittedRunStarted) {\n // Close any started tool calls that never got finish_reason. A\n // truncated stream that emitted TOOL_CALL_START but never reached\n // finish_reason would otherwise leave consumers with an unbalanced\n // start. Skip non-started entries (no matching START to close).\n let pendingToolCount = 0\n for (const [, toolCall] of toolCallsInProgress) {\n if (!toolCall.started) continue\n let parsedInput: unknown = {}\n if (toolCall.arguments) {\n try {\n const parsed: unknown = JSON.parse(toolCall.arguments)\n parsedInput = parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n // Mirror the finish_reason path's logger call — a truncated\n // stream emitting malformed tool-call JSON would otherwise\n // silently invoke the tool with `{}`, the exact failure the\n // finish_reason logger was added to prevent.\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed (drain)`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${toolCall.name} (${toolCall.id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: toolCall.id,\n toolName: toolCall.name,\n rawArguments: toolCall.arguments,\n },\n )\n parsedInput = {}\n }\n }\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: lastModel || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n pendingToolCount += 1\n emittedAnyToolCallEnd = true\n }\n toolCallsInProgress.clear()\n\n // Make sure the text message lifecycle is closed even on early\n // termination paths where finish_reason never arrives.\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n }\n }\n\n // Close any reasoning lifecycle that text never closed (no text\n // content arrived, or the stream cut off before text started).\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n }\n if (stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n content: accumulatedReasoning,\n }\n }\n }\n\n // Map upstream finish_reason to AG-UI's narrower vocabulary.\n // Collapsing length / content_filter to 'stop' would hide why the\n // run terminated — surface it instead. Use `tool_calls` only when\n // a TOOL_CALL_END was actually emitted: an upstream that signalled\n // `tool_calls` but never produced a started/ended pair must NOT\n // surface `tool_calls` here, since downstream consumers wait for\n // tool results that would never arrive. OpenAI's legacy\n // `function_call` value (from the v1 function-calling API) is\n // normalized to `tool_calls` — semantically the same termination.\n const finishReason: NonNullable<RunFinishedEvent['finishReason']> =\n emittedAnyToolCallEnd\n ? 'tool_calls'\n : pendingFinishReason === 'tool_calls'\n ? 'stop'\n : pendingFinishReason === 'function_call'\n ? 'tool_calls'\n : (pendingFinishReason ?? 'stop')\n\n // Conditional `usage` spread: AG-UI's `RunFinishedEvent.usage` is\n // optional with no `| undefined`; omit the key entirely when no usage\n // arrived rather than emitting `usage: undefined`.\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n ...(lastUsage && {\n usage: buildChatCompletionsUsage(lastUsage),\n }),\n finishReason,\n }\n }\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.processStreamChunks failed`,\n )\n const rawEvent = toRunErrorRawEvent(error)\n options.logger.errors(`${this.name}.processStreamChunks fatal`, {\n error: errorPayload,\n source: `${this.name}.processStreamChunks`,\n })\n\n // Emit AG-UI RUN_ERROR with conditional `code` spread (see chatStream's\n // catch block for the rationale). `rawEvent` carries the provider's\n // structured error body when present.\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Maps common TextOptions to Chat Completions API request format.\n * Override this in subclasses to add provider-specific options.\n */\n protected mapOptionsToRequest(\n options: TextOptions,\n ): ChatCompletionCreateParamsStreaming {\n const tools = options.tools\n ? convertToolsToChatCompletionsFormat(\n options.tools,\n this.makeStructuredOutputCompatible.bind(this),\n )\n : undefined\n\n // Build messages array with system prompts\n const messages: Array<ChatCompletionMessageParam> = []\n\n // Add system prompts first\n const systemPrompts = normalizeSystemPrompts(options.systemPrompts)\n if (systemPrompts.length > 0) {\n messages.push({\n role: 'system',\n content: systemPrompts.map((p) => p.content).join('\\n'),\n })\n }\n\n // Convert messages\n for (const message of options.messages) {\n messages.push(this.convertMessage(message))\n }\n\n const modelOptions = options.modelOptions\n\n // Native combined mode (issue #605): when the engine threads\n // `outputSchema` through TextOptions, the adapter declared\n // `supportsCombinedToolsAndSchema` and the schema is already JSON Schema\n // (pre-converted at the activity boundary). Wire it into\n // `response_format` alongside any `tools`. Modern OpenAI-compatible\n // Chat Completions accepts both together and emits the schema-\n // constrained text on the natural final turn.\n const combinedSchema = options.outputSchema as\n | Record<string, unknown>\n | undefined\n const responseFormat = combinedSchema\n ? {\n response_format: {\n type: 'json_schema' as const,\n json_schema: {\n name: 'structured_output',\n schema: this.makeStructuredOutputCompatible(\n combinedSchema,\n Array.isArray(combinedSchema.required)\n ? (combinedSchema.required as Array<string>)\n : undefined,\n ),\n strict: true,\n },\n },\n }\n : undefined\n\n // Build the request so explicit top-level options win over modelOptions\n // when set, but `undefined` top-level options do NOT clobber values the\n // caller put in modelOptions. Keeping the merge nullish-aware fixes the\n // silent regression where a `modelOptions: { temperature: 0.7 }` setting\n // was overwritten with `temperature: undefined`.\n return {\n ...modelOptions,\n model: options.model,\n messages,\n ...(options.temperature !== undefined && {\n temperature: options.temperature,\n }),\n ...(options.maxTokens !== undefined && {\n max_tokens: options.maxTokens,\n }),\n ...(options.topP !== undefined && { top_p: options.topP }),\n // Conditional spread: `tools: undefined` would clobber any\n // modelOptions.tools the caller set above.\n ...(tools &&\n tools.length > 0 && {\n tools,\n }),\n ...(responseFormat ?? {}),\n stream: true,\n }\n }\n\n /**\n * Modern OpenAI-compatible Chat Completions APIs support `tools` and\n * `response_format: json_schema` together in a single streaming request\n * (per issue #605). Subclasses can override — Groq, for instance, must\n * return `false` because its API rejects schema + tools + stream with a\n * 400.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return true\n }\n\n /**\n * Converts a single ModelMessage to the Chat Completions API message format.\n * Override this in subclasses to handle provider-specific message formats.\n */\n protected convertMessage(message: ModelMessage): ChatCompletionMessageParam {\n // Handle tool messages\n if (message.role === 'tool') {\n // The Chat Completions API has no multimodal `tool` message support\n // (unlike the Responses API's `function_call_output`). A tool that\n // returns an `Array<ContentPart>` is therefore stringified here — the\n // documented fallback for providers on the chat-completions path\n // (Groq, Ollama, Grok, OpenRouter chat). Multimodal tool results are\n // only delivered structurally via the Responses adapter.\n return {\n role: 'tool',\n tool_call_id: message.toolCallId || '',\n content:\n typeof message.content === 'string'\n ? message.content\n : JSON.stringify(message.content),\n }\n }\n\n // Handle assistant messages\n if (message.role === 'assistant') {\n const toolCalls = message.toolCalls?.map((tc) => ({\n id: tc.id,\n type: 'function' as const,\n function: {\n name: tc.function.name,\n arguments:\n typeof tc.function.arguments === 'string'\n ? tc.function.arguments\n : JSON.stringify(tc.function.arguments),\n },\n }))\n const hasToolCalls = !!toolCalls && toolCalls.length > 0\n const textContent = this.extractTextContent(message.content)\n\n // Per the OpenAI Chat Completions contract, an assistant message that\n // only carries tool_calls should have `content: null` (or omit content)\n // rather than `content: ''`. Empty-string content interacts oddly with\n // tokenization on some backends; null is the documented shape.\n return {\n role: 'assistant',\n content: hasToolCalls && !textContent ? null : textContent,\n ...(hasToolCalls ? { tool_calls: toolCalls } : {}),\n }\n }\n\n // Handle user messages - support multimodal content\n const contentParts = this.normalizeContent(message.content)\n\n // If only text, use simple string format\n if (contentParts.length === 1 && contentParts[0]?.type === 'text') {\n const text = contentParts[0].content\n if (text.length === 0) {\n // Single empty text part is the same fail-loud condition as below —\n // an empty paid request mask a real intent (caller passed `null`/'',\n // or an upstream step normalised everything to an empty string).\n throw new Error(\n `User message for ${this.name} has empty text content. ` +\n `Empty user messages would produce a paid request with no input; ` +\n `provide non-empty content or omit the message.`,\n )\n }\n return {\n role: 'user',\n content: text,\n }\n }\n\n // Otherwise, use array format for multimodal. Fail fast on unsupported\n // content parts rather than silently dropping them — a message of all\n // unsupported parts would otherwise turn into an empty user prompt and\n // mask a real capability mismatch.\n const parts: Array<ChatCompletionContentPart> = []\n for (const part of contentParts) {\n const converted = this.convertContentPart(part)\n if (!converted) {\n throw new Error(\n `Unsupported content part type for ${this.name}: ${part.type}. ` +\n `Override convertContentPart() in a subclass to handle this type, ` +\n `or remove it from the message.`,\n )\n }\n parts.push(converted)\n }\n\n if (parts.length === 0) {\n // The original message had no content parts at all (e.g. content was\n // explicitly null or []). Sending an empty user message to OpenAI\n // produces a paid request with no signal — fail loud instead.\n throw new Error(\n `User message for ${this.name} has no content parts. ` +\n `Empty user messages would produce a paid request with no input; ` +\n `provide at least one text/image/audio part or omit the message.`,\n )\n }\n\n return {\n role: 'user',\n content: parts,\n }\n }\n\n /**\n * Converts a single ContentPart to the Chat Completions API content part format.\n * Override this in subclasses to handle additional content types or provider-specific metadata.\n */\n protected convertContentPart(\n part: ContentPart,\n ): ChatCompletionContentPart | null {\n if (part.type === 'text') {\n return { type: 'text', text: part.content }\n }\n\n if (part.type === 'image') {\n const imageMetadata = part.metadata as\n | { detail?: 'auto' | 'low' | 'high' }\n | undefined\n\n // For base64 data, construct a data URI using the mimeType from source.\n // Default to a generic octet-stream MIME if the source didn't provide\n // one — interpolating `undefined` into the URI (\"data:undefined;base64,\n // ...\") would produce an invalid URI the API rejects.\n const imageValue = part.source.value\n const imageMime = part.source.mimeType || 'application/octet-stream'\n const imageUrl =\n part.source.type === 'data' && !imageValue.startsWith('data:')\n ? `data:${imageMime};base64,${imageValue}`\n : imageValue\n\n return {\n type: 'image_url',\n image_url: {\n url: imageUrl,\n detail: imageMetadata?.detail || 'auto',\n },\n }\n }\n\n // Unsupported content type — subclasses can override to handle more types\n return null\n }\n\n /**\n * Normalizes message content to an array of ContentPart.\n * Handles backward compatibility with string content.\n */\n protected normalizeContent(\n content: string | null | Array<ContentPart>,\n ): Array<ContentPart> {\n if (content === null) {\n return []\n }\n if (typeof content === 'string') {\n return [{ type: 'text', content: content }]\n }\n return content\n }\n\n /**\n * Extracts text content from a content value that may be string, null, or ContentPart array.\n */\n protected extractTextContent(\n content: string | null | Array<ContentPart>,\n ): string {\n if (content === null) {\n return ''\n }\n if (typeof content === 'string') {\n return content\n }\n // It's an array of ContentPart\n return content\n .filter((p) => p.type === 'text')\n .map((p) => p.content)\n .join('')\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAsCO,MAAe,6CAOZ,gBAMR;AAAA,EACkB,OAAO;AAAA,EAChB;AAAA,EACC;AAAA,EAEV,YAAY,OAAe,MAAc,QAAgB;AACvD,UAAM,CAAA,GAAI,KAAK;AACf,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,OAAO,WACL,SAC4B;AAE5B,UAAM,YAAY;AAAA,MAChB,OAAO,WAAW,KAAK,IAAI;AAAA,MAC3B,UAAU,QAAQ,YAAY,WAAW,KAAK,IAAI;AAAA,MAClD,WAAW,WAAW,KAAK,IAAI;AAAA,MAC/B,sBAAsB;AAAA,IAAA;AAGxB,QAAI;AAOF,YAAM,gBAAgB,KAAK,oBAAoB,OAAO;AAC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AAI,KAAK,SAAS,SAAS;AACzB,YAAM,gBAAgB,KAAK;AAQ3B,YAAM,aAAa,KAAK,OAAO;AAC/B,YAAM,YAAY,KAAK,OAAO,YAAY;AAC1C,YAAM,WACJ,KAAK,OAAO,SAAS,UAAU,CAAC,WAAW,WAAW,OAAO,IACzD,QAAQ,SAAS,WAAW,UAAU,KACtC;AAEN,aAAO;AAAA,QACL,MAAM;AAAA,QACN,WAAW;AAAA,UACT,KAAK;AAAA,UACL,QAAQ,eAAe,UAAU;AAAA,QAAA;AAAA,MACnC;AAAA,IAEJ;AAGA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,iBACR,SACoB;AACpB,QAAI,YAAY,MAAM;AACpB,aAAO,CAAA;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO,CAAC,EAAE,MAAM,QAAQ,SAAkB;AAAA,IAC5C;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKU,mBACR,SACQ;AACR,QAAI,YAAY,MAAM;AACpB,aAAO;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO;AAAA,IACT;AAEA,WAAO,QACJ,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAC/B,IAAI,CAAC,MAAM,EAAE,OAAO,EACpB,KAAK,EAAE;AAAA,EACZ;AACF;"}
1
+ {"version":3,"file":"chat-completions-text.js","sources":["../../../src/adapters/chat-completions-text.ts"],"sourcesContent":["import { EventType, normalizeSystemPrompts } from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport {\n toRunErrorPayload,\n toRunErrorRawEvent,\n} from '@tanstack/ai/adapter-internals'\nimport { generateId, transformNullsToUndefined } from '@tanstack/ai-utils'\nimport { extractRequestOptions } from '../utils/request-options'\nimport { makeStructuredOutputCompatible } from '../utils/schema-converter'\nimport { buildChatCompletionsUsage } from '../usage'\nimport { convertToolsToChatCompletionsFormat } from './chat-completions-tool-converter'\nimport type OpenAI from 'openai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type {\n ChatCompletionChunk,\n ChatCompletionContentPart,\n ChatCompletionCreateParamsStreaming,\n ChatCompletionMessageParam,\n} from 'openai/resources/chat/completions/completions'\nimport type {\n ContentPart,\n DefaultMessageMetadataByModality,\n Modality,\n ModelMessage,\n RunFinishedEvent,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\n\n/**\n * Shared implementation of the OpenAI Chat Completions API. Holds the\n * stream-accumulator + AG-UI lifecycle logic and calls the OpenAI SDK\n * directly. Subclasses (ai-openai, ai-grok, ai-groq) construct an OpenAI\n * client with their provider-specific `baseURL` / headers and pass it in.\n */\nexport abstract class OpenAIBaseChatCompletionsTextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, unknown> = Record<string, unknown>,\n TInputModalities extends ReadonlyArray<Modality> = ReadonlyArray<Modality>,\n TMessageMetadata extends DefaultMessageMetadataByModality =\n DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n TMessageMetadata,\n TToolCapabilities\n> {\n override readonly kind = 'text' as const\n readonly name: string\n protected client: OpenAI\n\n constructor(model: TModel, name: string, client: OpenAI) {\n super({}, model)\n this.name = name\n this.client = client\n }\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n // AG-UI lifecycle tracking (mutable state object for ESLint compatibility)\n const aguiState = {\n runId: generateId(this.name),\n threadId: options.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n hasEmittedRunStarted: false,\n }\n\n try {\n // mapOptionsToRequest can throw (e.g. fail-loud guards in convertMessage\n // for empty content or unsupported parts). Keep it inside the try so\n // those failures surface as a single RUN_ERROR event, matching every\n // other failure mode here — callers iterating chatStream then only need\n // one error-handling path instead of both a try/catch around iteration\n // and a RUN_ERROR handler.\n const requestParams = this.mapOptionsToRequest(options)\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const stream = await this.client.chat.completions.create(\n {\n ...requestParams,\n stream: true,\n stream_options: { include_usage: true },\n },\n extractRequestOptions(options.request),\n )\n\n yield* this.processStreamChunks(stream, options, aguiState)\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n const rawEvent = toRunErrorRawEvent(error)\n\n // Emit RUN_STARTED if not yet emitted\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: options.model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n // Emit AG-UI RUN_ERROR. Conditional `code` spread keeps the wire\n // shape spec-compliant under `exactOptionalPropertyTypes`: AG-UI's\n // `RunErrorEvent.code` is `string?` (absent vs explicit `undefined`\n // matter), so we omit the key when there's no code.\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n code: errorPayload.code,\n // Forward the provider's structured error body so consumers can recover\n // the upstream detail the `{ message, code }` payload drops. Omitted\n // when the error carried no provider body (see toRunErrorRawEvent).\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n code: errorPayload.code,\n },\n }\n\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n }\n }\n\n /**\n * Generate structured output using the provider's JSON Schema response format.\n * Uses stream: false to get the complete response in one call.\n *\n * OpenAI-compatible APIs have strict requirements for structured output:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for all objects\n *\n * The outputSchema is already JSON Schema (converted in the ai layer).\n * We apply provider-specific transformations for structured output compatibility.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapOptionsToRequest(chatOptions)\n\n const jsonSchema = this.makeStructuredOutputCompatible(\n outputSchema,\n outputSchema.required,\n )\n\n try {\n // Strip stream_options which is only valid for streaming calls\n const {\n stream_options: _,\n stream: __,\n ...cleanParams\n } = requestParams as any\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const response = await this.client.chat.completions.create(\n {\n ...cleanParams,\n stream: false,\n response_format: {\n type: 'json_schema',\n json_schema: {\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n },\n extractRequestOptions(chatOptions.request),\n )\n\n // Extract text content from the response. Fail loud on empty content\n // rather than letting it cascade into a JSON-parse error on '' — the\n // root cause (the model returned no content for the structured request)\n // is then visible in logs.\n const rawText = response.choices[0]?.message.content\n if (typeof rawText !== 'string' || rawText.length === 0) {\n throw new Error(\n `${this.name}.structuredOutput: response contained no content`,\n )\n }\n\n // Parse the JSON response\n let parsed: unknown\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to parse structured output as JSON. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n\n // Transform null values to undefined to match original Zod schema expectations\n // Provider returns null for optional fields we made nullable in the schema.\n // Subclasses can override `transformStructuredOutput` to skip this — e.g.\n // OpenRouter historically passed nulls through unchanged.\n const transformed = this.transformStructuredOutput(parsed)\n\n return {\n data: transformed,\n rawText,\n }\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Stream structured output. Single Chat Completions request with\n * `response_format: json_schema` + `stream: true`. Emits the standard\n * AG-UI lifecycle (`RUN_STARTED` → `REASONING_*?` → `TEXT_MESSAGE_*`\n * carrying raw JSON deltas → terminal `CUSTOM 'structured-output.complete'`\n * → `RUN_FINISHED`). Subclasses use the same SDK-call / reasoning /\n * structured-output-transform hooks as `chatStream` / `structuredOutput` —\n * no per-subclass override should be needed.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapOptionsToRequest(chatOptions)\n\n const jsonSchema = this.makeStructuredOutputCompatible(\n outputSchema,\n outputSchema.required,\n )\n\n const timestamp = Date.now()\n const aguiState = {\n runId: generateId(this.name),\n threadId: chatOptions.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n timestamp,\n hasEmittedRunStarted: false,\n }\n\n let accumulatedContent = ''\n let accumulatedReasoning = ''\n let hasEmittedTextMessageStart = false\n let reasoningMessageId: string | undefined\n let hasClosedReasoning = false\n let stepId: string | undefined\n let lastModel: string | undefined\n let lastUsage:\n | OpenAI.Chat.Completions.ChatCompletionChunk['usage']\n | undefined\n\n const closeReasoningLifecycle = function* (this: {\n name: string\n }): Generator<StreamChunk> {\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model: lastModel || chatOptions.model,\n timestamp,\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model: lastModel || chatOptions.model,\n timestamp,\n }\n if (stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model: lastModel || chatOptions.model,\n timestamp,\n content: accumulatedReasoning,\n }\n }\n }\n }.bind(this)\n\n try {\n // Strip stream_options + tools from the base request. Structured output\n // sends `response_format: json_schema` and doesn't carry tools — keeping\n // them in the request can confuse strict-mode validation upstream.\n const {\n stream_options: _so,\n stream: _s,\n tools: _t,\n ...cleanParams\n } = requestParams\n\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n\n const stream = await this.client.chat.completions.create(\n {\n ...cleanParams,\n stream: true,\n stream_options: { include_usage: true },\n response_format: {\n type: 'json_schema',\n json_schema: {\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n },\n extractRequestOptions(chatOptions.request),\n )\n\n for await (const chunk of stream) {\n const choiceForLog = chunk.choices[0]\n chatOptions.logger.provider(\n `provider=${this.name} finish_reason=${choiceForLog?.finish_reason ?? 'none'} hasContent=${!!choiceForLog?.delta.content} hasUsage=${!!chunk.usage}`,\n { provider: this.name, model: chunk.model },\n )\n\n if (chunk.model) lastModel = chunk.model\n\n // Usage may arrive on a chunk with empty `choices` (OpenAI's\n // include_usage terminal chunk) or piggybacked on a finish chunk\n // (`x_groq.usage` on Groq). Capture from either independent of\n // choices[0].\n const usage =\n chunk.usage ??\n (chunk as { x_groq?: { usage?: typeof chunk.usage } }).x_groq?.usage\n if (usage) lastUsage = usage\n\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chunk.model || chatOptions.model,\n timestamp,\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n // Reasoning (via the extractReasoning hook — same hook as chatStream).\n const reasoning = this.extractReasoning(chunk)\n if (reasoning && reasoning.text) {\n if (!reasoningMessageId) {\n reasoningMessageId = generateId(this.name)\n stepId = generateId(this.name)\n yield {\n type: EventType.REASONING_START,\n messageId: reasoningMessageId,\n model: chunk.model || chatOptions.model,\n timestamp,\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning' as const,\n model: chunk.model || chatOptions.model,\n timestamp,\n }\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: chunk.model || chatOptions.model,\n timestamp,\n stepType: 'thinking',\n }\n }\n accumulatedReasoning += reasoning.text\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta: reasoning.text,\n model: chunk.model || chatOptions.model,\n timestamp,\n }\n }\n\n const choice = chunk.choices[0]\n if (!choice) continue\n\n const deltaContent = choice.delta.content\n if (deltaContent) {\n yield* closeReasoningLifecycle()\n\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: chunk.model || chatOptions.model,\n timestamp,\n role: 'assistant',\n }\n }\n\n accumulatedContent += deltaContent\n\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model: chunk.model || chatOptions.model,\n timestamp,\n delta: deltaContent,\n content: accumulatedContent,\n }\n }\n }\n\n // Finalisation: close any open lifecycle, parse + validate, emit\n // terminal events. This block always runs unless the loop threw — abort\n // and SDK errors land in the catch block below.\n yield* closeReasoningLifecycle()\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: lastModel || chatOptions.model,\n timestamp,\n }\n }\n\n if (accumulatedContent.length === 0) {\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model: lastModel || chatOptions.model,\n timestamp,\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n error: {\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n },\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedContent)\n } catch {\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model: lastModel || chatOptions.model,\n timestamp,\n message: `Failed to parse structured output as JSON. Content: ${accumulatedContent.slice(0, 200)}${accumulatedContent.length > 200 ? '...' : ''}`,\n code: 'parse-error',\n error: {\n message: 'Failed to parse structured output as JSON',\n code: 'parse-error',\n },\n }\n return\n }\n\n const transformed = this.transformStructuredOutput(parsed)\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: transformed,\n raw: accumulatedContent,\n ...(accumulatedReasoning ? { reasoning: accumulatedReasoning } : {}),\n },\n model: lastModel || chatOptions.model,\n timestamp,\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: lastModel || chatOptions.model,\n timestamp,\n finishReason: 'stop',\n ...(lastUsage && {\n usage: buildChatCompletionsUsage(lastUsage),\n }),\n }\n } catch (error: unknown) {\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chatOptions.model,\n timestamp,\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n const isAbort = this.isAbortError(error)\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes`: AG-UI's `RunErrorEvent.code` is `string?`\n // (absent vs explicit `undefined` matter).\n const resolvedCode = isAbort ? 'aborted' : errorPayload.code\n const rawEvent = isAbort ? undefined : toRunErrorRawEvent(error)\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model: lastModel || chatOptions.model,\n timestamp,\n message: errorPayload.message,\n ...(resolvedCode !== undefined && { code: resolvedCode }),\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n ...(resolvedCode !== undefined && { code: resolvedCode }),\n },\n }\n\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n }\n }\n\n /**\n * Cross-SDK abort detection for `structuredOutputStream`. Default duck-types\n * on `name === 'APIUserAbortError'` (OpenAI SDK), `code === 'ERR_CANCELED'`,\n * and standard `AbortError`s. Subclasses with proprietary error types (e.g.\n * `@openrouter/sdk`'s `RequestAbortedError`) override to extend the check.\n */\n protected isAbortError(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false\n const e = error as { name?: unknown; code?: unknown }\n return (\n e.name === 'APIUserAbortError' ||\n e.name === 'AbortError' ||\n e.code === 'ERR_CANCELED'\n )\n }\n\n /**\n * Applies provider-specific transformations for structured output compatibility.\n * Override this in subclasses to handle provider-specific quirks.\n */\n protected makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n ): Record<string, any> {\n return makeStructuredOutputCompatible(schema, originalRequired)\n }\n\n /**\n * Extract reasoning content from a stream chunk. Default returns\n * `undefined` because the OpenAI Chat Completions chunk shape doesn't\n * carry reasoning. The chunk param is typed `unknown` so an override can\n * narrow to its own SDK chunk type without an `as` dance — the base only\n * passes through `processStreamChunks`'s structurally-iterated chunk.\n */\n protected extractReasoning(_chunk: unknown): { text: string } | undefined {\n return undefined\n }\n\n /**\n * Final shaping pass applied to parsed structured-output JSON before it is\n * returned to the caller. Default converts `null` values to `undefined` so\n * the result aligns with the original Zod schema's optional-field\n * semantics. Subclasses with different conventions (OpenRouter historically\n * preserves nulls) can override.\n */\n protected transformStructuredOutput(parsed: unknown): unknown {\n return transformNullsToUndefined(parsed)\n }\n\n /**\n * Processes streamed chunks from the Chat Completions API and yields AG-UI events.\n * Override this in subclasses to handle provider-specific stream behavior.\n */\n protected async *processStreamChunks(\n stream: AsyncIterable<ChatCompletionChunk>,\n options: TextOptions,\n aguiState: {\n runId: string\n threadId: string\n messageId: string\n hasEmittedRunStarted: boolean\n },\n ): AsyncIterable<StreamChunk> {\n let accumulatedContent = ''\n let hasEmittedTextMessageStart = false\n let lastModel: string | undefined\n // Track usage from any chunk that carries it. With\n // `stream_options: { include_usage: true }` OpenAI emits a terminal chunk\n // whose `choices` is `[]` and only the `usage` field is populated; the\n // earlier `finish_reason` chunk does NOT include token counts. We must\n // therefore defer RUN_FINISHED until the iterator is exhausted so we can\n // pick up usage from the trailing chunk regardless of arrival order.\n let lastUsage: ChatCompletionChunk['usage'] | undefined\n let pendingFinishReason:\n | ChatCompletionChunk['choices'][number]['finish_reason']\n | undefined\n\n // Track tool calls being streamed (arguments come in chunks)\n const toolCallsInProgress = new Map<\n number,\n {\n id: string\n name: string\n arguments: string\n started: boolean // Track if TOOL_CALL_START has been emitted\n }\n >()\n\n // Reasoning lifecycle (driven by extractReasoning() hook — see method\n // docs). The base wire format (OpenAI Chat Completions) has no reasoning,\n // so these stay unused for openai/grok/groq. OpenRouter etc. opt in.\n let reasoningMessageId: string | undefined\n let hasClosedReasoning = false\n // Legacy STEP_STARTED/STEP_FINISHED pair emitted alongside REASONING_*\n // for back-compat with consumers (UI, devtools) that haven't migrated\n // to the spec REASONING_* events yet.\n let stepId: string | undefined\n let accumulatedReasoning = ''\n // Track whether ANY tool call lifecycle was actually completed across the\n // entire stream. Lets us downgrade a `tool_calls` finish_reason to `stop`\n // when the upstream signalled tool calls but never produced a complete\n // start/end pair — emitting RUN_FINISHED { finishReason: 'tool_calls' }\n // with no matching TOOL_CALL_END would leave consumers waiting for tool\n // results that never arrive.\n let emittedAnyToolCallEnd = false\n\n try {\n for await (const chunk of stream) {\n const choiceForLog = chunk.choices[0]\n options.logger.provider(\n `provider=${this.name} finish_reason=${choiceForLog?.finish_reason ?? 'none'} hasContent=${!!choiceForLog?.delta.content} hasToolCalls=${!!choiceForLog?.delta.tool_calls} hasUsage=${!!chunk.usage}`,\n { provider: this.name, model: chunk.model },\n )\n\n // Capture usage from any chunk (including the terminal usage-only\n // chunk emitted when `stream_options.include_usage` is on).\n if (chunk.usage) {\n lastUsage = chunk.usage\n }\n if (chunk.model) {\n lastModel = chunk.model\n }\n\n // Emit RUN_STARTED on the first chunk of any kind so callers see a\n // run lifecycle even on streams that arrive entirely as usage-only\n // (no choices). Without this, a usage-first stream would skip\n // RUN_STARTED via `if (!choice) continue` below and the post-loop\n // synthetic block would also skip RUN_FINISHED (it gates on\n // `hasEmittedRunStarted`).\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n // Reasoning content (extractReasoning() hook). Run before reading\n // choice/delta so reasoning-only chunks (no `choices`) still drive\n // the REASONING_* lifecycle on providers that send reasoning out of\n // band. The base default returns undefined.\n const reasoning = this.extractReasoning(chunk)\n if (reasoning && reasoning.text) {\n if (!reasoningMessageId) {\n reasoningMessageId = generateId(this.name)\n stepId = generateId(this.name)\n yield {\n type: EventType.REASONING_START,\n messageId: reasoningMessageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning' as const,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n // Legacy STEP_STARTED (single emission, paired with the\n // STEP_FINISHED below when reasoning closes).\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n accumulatedReasoning += reasoning.text\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta: reasoning.text,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n }\n\n const choice = chunk.choices[0]\n\n if (!choice) continue\n\n const delta = choice.delta\n const deltaContent = delta.content\n const deltaToolCalls = delta.tool_calls\n\n // Handle content delta\n if (deltaContent) {\n // Close reasoning before text starts so consumers see a clean\n // REASONING_END before any TEXT_MESSAGE_START.\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n if (stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n content: accumulatedReasoning,\n }\n }\n }\n\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n }\n\n accumulatedContent += deltaContent\n\n // Emit AG-UI TEXT_MESSAGE_CONTENT\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n delta: deltaContent,\n content: accumulatedContent,\n }\n }\n\n // Handle tool calls - they come in as deltas\n if (deltaToolCalls) {\n for (const toolCallDelta of deltaToolCalls) {\n const index = toolCallDelta.index\n\n // Initialize or update the tool call in progress\n let toolCall = toolCallsInProgress.get(index)\n if (!toolCall) {\n toolCall = {\n id: toolCallDelta.id || '',\n name: toolCallDelta.function?.name || '',\n arguments: '',\n started: false,\n }\n toolCallsInProgress.set(index, toolCall)\n }\n\n // Update with any new data from the delta\n if (toolCallDelta.id) {\n toolCall.id = toolCallDelta.id\n }\n if (toolCallDelta.function?.name) {\n toolCall.name = toolCallDelta.function.name\n }\n if (toolCallDelta.function?.arguments) {\n toolCall.arguments += toolCallDelta.function.arguments\n }\n\n // Emit TOOL_CALL_START when we have id and name\n if (toolCall.id && toolCall.name && !toolCall.started) {\n toolCall.started = true\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n index,\n }\n }\n\n // Emit TOOL_CALL_ARGS for argument deltas\n if (toolCallDelta.function?.arguments && toolCall.started) {\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: toolCall.id,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n delta: toolCallDelta.function.arguments,\n }\n }\n }\n }\n\n // Handle finish reason. We DO emit TOOL_CALL_END and TEXT_MESSAGE_END\n // here because the corresponding _START events have already fired,\n // and tool execution downstream wants to begin as soon as possible.\n // RUN_FINISHED is deferred until the iterator is fully exhausted so\n // we can capture the trailing usage chunk that arrives AFTER this\n // chunk when stream_options.include_usage is on.\n if (choice.finish_reason) {\n if (\n choice.finish_reason === 'tool_calls' ||\n toolCallsInProgress.size > 0\n ) {\n for (const [, toolCall] of toolCallsInProgress) {\n // Skip tool calls that never emitted TOOL_CALL_START — emitting\n // a stray TOOL_CALL_END here would violate AG-UI lifecycle\n // (END without matching START) for partial deltas where the\n // upstream never sent both id and name.\n if (!toolCall.started) continue\n\n // Parse arguments for TOOL_CALL_END. Surface parse failures via\n // the logger so a model emitting malformed JSON for tool args\n // is debuggable instead of silently invoking the tool with {}.\n // Non-object JSON (e.g. a bare string or number) is also coerced\n // to {} so downstream tool execution doesn't receive a primitive\n // input, mirroring the Responses adapter's guard.\n let parsedInput: unknown = {}\n if (toolCall.arguments) {\n try {\n const parsed: unknown = JSON.parse(toolCall.arguments)\n parsedInput =\n parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${toolCall.name} (${toolCall.id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: toolCall.id,\n toolName: toolCall.name,\n rawArguments: toolCall.arguments,\n },\n )\n parsedInput = {}\n }\n }\n\n // Emit AG-UI TOOL_CALL_END\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n emittedAnyToolCallEnd = true\n }\n // Clear tool-call state after emission so a subsequent\n // `finish_reason: 'stop'` chunk (or the post-loop synthetic\n // block) doesn't see lingering entries and misreport the finish.\n toolCallsInProgress.clear()\n }\n\n // Emit TEXT_MESSAGE_END if we had text content\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: chunk.model || options.model,\n timestamp: Date.now(),\n }\n hasEmittedTextMessageStart = false\n }\n\n // Remember the upstream finish_reason; RUN_FINISHED is emitted at\n // end-of-stream so we pick up the trailing usage-only chunk too.\n pendingFinishReason = choice.finish_reason\n }\n }\n\n // Emit a single terminal RUN_FINISHED after the iterator is exhausted.\n // This both delivers accurate token counts (the trailing usage chunk\n // may arrive AFTER the finish_reason chunk) and gives consumers a\n // guaranteed terminal event even when the upstream cuts off mid-stream\n // (no finish_reason chunk ever arrives).\n if (aguiState.hasEmittedRunStarted) {\n // Close any started tool calls that never got finish_reason. A\n // truncated stream that emitted TOOL_CALL_START but never reached\n // finish_reason would otherwise leave consumers with an unbalanced\n // start. Skip non-started entries (no matching START to close).\n let pendingToolCount = 0\n for (const [, toolCall] of toolCallsInProgress) {\n if (!toolCall.started) continue\n let parsedInput: unknown = {}\n if (toolCall.arguments) {\n try {\n const parsed: unknown = JSON.parse(toolCall.arguments)\n parsedInput = parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n // Mirror the finish_reason path's logger call — a truncated\n // stream emitting malformed tool-call JSON would otherwise\n // silently invoke the tool with `{}`, the exact failure the\n // finish_reason logger was added to prevent.\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed (drain)`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${toolCall.name} (${toolCall.id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: toolCall.id,\n toolName: toolCall.name,\n rawArguments: toolCall.arguments,\n },\n )\n parsedInput = {}\n }\n }\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n model: lastModel || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n pendingToolCount += 1\n emittedAnyToolCallEnd = true\n }\n toolCallsInProgress.clear()\n\n // Make sure the text message lifecycle is closed even on early\n // termination paths where finish_reason never arrives.\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n }\n }\n\n // Close any reasoning lifecycle that text never closed (no text\n // content arrived, or the stream cut off before text started).\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n }\n if (stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n content: accumulatedReasoning,\n }\n }\n }\n\n // Map upstream finish_reason to AG-UI's narrower vocabulary.\n // Collapsing length / content_filter to 'stop' would hide why the\n // run terminated — surface it instead. Use `tool_calls` only when\n // a TOOL_CALL_END was actually emitted: an upstream that signalled\n // `tool_calls` but never produced a started/ended pair must NOT\n // surface `tool_calls` here, since downstream consumers wait for\n // tool results that would never arrive. OpenAI's legacy\n // `function_call` value (from the v1 function-calling API) is\n // normalized to `tool_calls` — semantically the same termination.\n const finishReason: NonNullable<RunFinishedEvent['finishReason']> =\n emittedAnyToolCallEnd\n ? 'tool_calls'\n : pendingFinishReason === 'tool_calls'\n ? 'stop'\n : pendingFinishReason === 'function_call'\n ? 'tool_calls'\n : (pendingFinishReason ?? 'stop')\n\n // Conditional `usage` spread: AG-UI's `RunFinishedEvent.usage` is\n // optional with no `| undefined`; omit the key entirely when no usage\n // arrived rather than emitting `usage: undefined`.\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: lastModel || options.model,\n timestamp: Date.now(),\n ...(lastUsage && {\n usage: buildChatCompletionsUsage(lastUsage),\n }),\n finishReason,\n }\n }\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.processStreamChunks failed`,\n )\n const rawEvent = toRunErrorRawEvent(error)\n options.logger.errors(`${this.name}.processStreamChunks fatal`, {\n error: errorPayload,\n source: `${this.name}.processStreamChunks`,\n })\n\n // Emit AG-UI RUN_ERROR with conditional `code` spread (see chatStream's\n // catch block for the rationale). `rawEvent` carries the provider's\n // structured error body when present.\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Maps common TextOptions to Chat Completions API request format.\n * Override this in subclasses to add provider-specific options.\n */\n protected mapOptionsToRequest(\n options: TextOptions,\n ): ChatCompletionCreateParamsStreaming {\n const tools = options.tools\n ? convertToolsToChatCompletionsFormat(\n options.tools,\n this.makeStructuredOutputCompatible.bind(this),\n )\n : undefined\n\n // Build messages array with system prompts\n const messages: Array<ChatCompletionMessageParam> = []\n\n // Add system prompts first\n const systemPrompts = normalizeSystemPrompts(options.systemPrompts)\n if (systemPrompts.length > 0) {\n messages.push({\n role: 'system',\n content: systemPrompts.map((p) => p.content).join('\\n'),\n })\n }\n\n // Convert messages\n for (const message of options.messages) {\n messages.push(this.convertMessage(message))\n }\n\n const modelOptions = options.modelOptions\n\n // Native combined mode (issue #605): when the engine threads\n // `outputSchema` through TextOptions, the adapter declared\n // `supportsCombinedToolsAndSchema` and the schema is already JSON Schema\n // (pre-converted at the activity boundary). Wire it into\n // `response_format` alongside any `tools`. Modern OpenAI-compatible\n // Chat Completions accepts both together and emits the schema-\n // constrained text on the natural final turn.\n const combinedSchema = options.outputSchema as\n | Record<string, unknown>\n | undefined\n const responseFormat = combinedSchema\n ? {\n response_format: {\n type: 'json_schema' as const,\n json_schema: {\n name: 'structured_output',\n schema: this.makeStructuredOutputCompatible(\n combinedSchema,\n Array.isArray(combinedSchema.required)\n ? (combinedSchema.required as Array<string>)\n : undefined,\n ),\n strict: true,\n },\n },\n }\n : undefined\n\n // `modelOptions` is the sole sampling surface: callers set provider-native\n // wire names (`temperature`, `top_p`, `max_tokens`/`max_completion_tokens`)\n // there and they flow through the spread below. The root\n // `temperature`/`topP`/`maxTokens` fields are intentionally NOT read here.\n return {\n ...modelOptions,\n model: options.model,\n messages,\n // Conditional spread: `tools: undefined` would clobber any\n // modelOptions.tools the caller set above.\n ...(tools &&\n tools.length > 0 && {\n tools,\n }),\n ...(responseFormat ?? {}),\n stream: true,\n }\n }\n\n /**\n * Modern OpenAI-compatible Chat Completions APIs support `tools` and\n * `response_format: json_schema` together in a single streaming request\n * (per issue #605). Subclasses can override — Groq, for instance, must\n * return `false` because its API rejects schema + tools + stream with a\n * 400.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return true\n }\n\n /**\n * Converts a single ModelMessage to the Chat Completions API message format.\n * Override this in subclasses to handle provider-specific message formats.\n */\n protected convertMessage(message: ModelMessage): ChatCompletionMessageParam {\n // Handle tool messages\n if (message.role === 'tool') {\n // The Chat Completions API has no multimodal `tool` message support\n // (unlike the Responses API's `function_call_output`). A tool that\n // returns an `Array<ContentPart>` is therefore stringified here — the\n // documented fallback for providers on the chat-completions path\n // (Groq, Ollama, Grok, OpenRouter chat). Multimodal tool results are\n // only delivered structurally via the Responses adapter.\n return {\n role: 'tool',\n tool_call_id: message.toolCallId || '',\n content:\n typeof message.content === 'string'\n ? message.content\n : JSON.stringify(message.content),\n }\n }\n\n // Handle assistant messages\n if (message.role === 'assistant') {\n const toolCalls = message.toolCalls?.map((tc) => ({\n id: tc.id,\n type: 'function' as const,\n function: {\n name: tc.function.name,\n arguments:\n typeof tc.function.arguments === 'string'\n ? tc.function.arguments\n : JSON.stringify(tc.function.arguments),\n },\n }))\n const hasToolCalls = !!toolCalls && toolCalls.length > 0\n const textContent = this.extractTextContent(message.content)\n\n // Per the OpenAI Chat Completions contract, an assistant message that\n // only carries tool_calls should have `content: null` (or omit content)\n // rather than `content: ''`. Empty-string content interacts oddly with\n // tokenization on some backends; null is the documented shape.\n return {\n role: 'assistant',\n content: hasToolCalls && !textContent ? null : textContent,\n ...(hasToolCalls ? { tool_calls: toolCalls } : {}),\n }\n }\n\n // Handle user messages - support multimodal content\n const contentParts = this.normalizeContent(message.content)\n\n // If only text, use simple string format\n if (contentParts.length === 1 && contentParts[0]?.type === 'text') {\n const text = contentParts[0].content\n if (text.length === 0) {\n // Single empty text part is the same fail-loud condition as below —\n // an empty paid request mask a real intent (caller passed `null`/'',\n // or an upstream step normalised everything to an empty string).\n throw new Error(\n `User message for ${this.name} has empty text content. ` +\n `Empty user messages would produce a paid request with no input; ` +\n `provide non-empty content or omit the message.`,\n )\n }\n return {\n role: 'user',\n content: text,\n }\n }\n\n // Otherwise, use array format for multimodal. Fail fast on unsupported\n // content parts rather than silently dropping them — a message of all\n // unsupported parts would otherwise turn into an empty user prompt and\n // mask a real capability mismatch.\n const parts: Array<ChatCompletionContentPart> = []\n for (const part of contentParts) {\n const converted = this.convertContentPart(part)\n if (!converted) {\n throw new Error(\n `Unsupported content part type for ${this.name}: ${part.type}. ` +\n `Override convertContentPart() in a subclass to handle this type, ` +\n `or remove it from the message.`,\n )\n }\n parts.push(converted)\n }\n\n if (parts.length === 0) {\n // The original message had no content parts at all (e.g. content was\n // explicitly null or []). Sending an empty user message to OpenAI\n // produces a paid request with no signal — fail loud instead.\n throw new Error(\n `User message for ${this.name} has no content parts. ` +\n `Empty user messages would produce a paid request with no input; ` +\n `provide at least one text/image/audio part or omit the message.`,\n )\n }\n\n return {\n role: 'user',\n content: parts,\n }\n }\n\n /**\n * Converts a single ContentPart to the Chat Completions API content part format.\n * Override this in subclasses to handle additional content types or provider-specific metadata.\n */\n protected convertContentPart(\n part: ContentPart,\n ): ChatCompletionContentPart | null {\n if (part.type === 'text') {\n return { type: 'text', text: part.content }\n }\n\n if (part.type === 'image') {\n const imageMetadata = part.metadata as\n | { detail?: 'auto' | 'low' | 'high' }\n | undefined\n\n // For base64 data, construct a data URI using the mimeType from source.\n // Default to a generic octet-stream MIME if the source didn't provide\n // one — interpolating `undefined` into the URI (\"data:undefined;base64,\n // ...\") would produce an invalid URI the API rejects.\n const imageValue = part.source.value\n const imageMime = part.source.mimeType || 'application/octet-stream'\n const imageUrl =\n part.source.type === 'data' && !imageValue.startsWith('data:')\n ? `data:${imageMime};base64,${imageValue}`\n : imageValue\n\n return {\n type: 'image_url',\n image_url: {\n url: imageUrl,\n detail: imageMetadata?.detail || 'auto',\n },\n }\n }\n\n // Unsupported content type — subclasses can override to handle more types\n return null\n }\n\n /**\n * Normalizes message content to an array of ContentPart.\n * Handles backward compatibility with string content.\n */\n protected normalizeContent(\n content: string | null | Array<ContentPart>,\n ): Array<ContentPart> {\n if (content === null) {\n return []\n }\n if (typeof content === 'string') {\n return [{ type: 'text', content: content }]\n }\n return content\n }\n\n /**\n * Extracts text content from a content value that may be string, null, or ContentPart array.\n */\n protected extractTextContent(\n content: string | null | Array<ContentPart>,\n ): string {\n if (content === null) {\n return ''\n }\n if (typeof content === 'string') {\n return content\n }\n // It's an array of ContentPart\n return content\n .filter((p) => p.type === 'text')\n .map((p) => p.content)\n .join('')\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAsCO,MAAe,6CAOZ,gBAMR;AAAA,EACkB,OAAO;AAAA,EAChB;AAAA,EACC;AAAA,EAEV,YAAY,OAAe,MAAc,QAAgB;AACvD,UAAM,CAAA,GAAI,KAAK;AACf,SAAK,OAAO;AACZ,S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AACxB,aAAO,EAAE,MAAM,QAAQ,MAAM,KAAK,QAAA;AAAA,IACpC;AAEA,QAAI,KAAK,SAAS,SAAS;AACzB,YAAM,gBAAgB,KAAK;AAQ3B,YAAM,aAAa,KAAK,OAAO;AAC/B,YAAM,YAAY,KAAK,OAAO,YAAY;AAC1C,YAAM,WACJ,KAAK,OAAO,SAAS,UAAU,CAAC,WAAW,WAAW,OAAO,IACzD,QAAQ,SAAS,WAAW,UAAU,KACtC;AAEN,aAAO;AAAA,QACL,MAAM;AAAA,QACN,WAAW;AAAA,UACT,KAAK;AAAA,UACL,QAAQ,eAAe,UAAU;AAAA,QAAA;AAAA,MACnC;AAAA,IAEJ;AAGA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,iBACR,SACoB;AACpB,QAAI,YAAY,MAAM;AACpB,aAAO,CAAA;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO,CAAC,EAAE,MAAM,QAAQ,SAAkB;AAAA,IAC5C;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKU,mBACR,SACQ;AACR,QAAI,YAAY,MAAM;AACpB,aAAO;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO;AAAA,IACT;AAEA,WAAO,QACJ,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAC/B,IAAI,CAAC,MAAM,EAAE,OAAO,EACpB,KAAK,EAAE;AAAA,EACZ;AACF;"}
@@ -1150,13 +1150,6 @@ class OpenAIBaseResponsesTextAdapter extends BaseTextAdapter {
1150
1150
  return {
1151
1151
  ...modelOptions,
1152
1152
  model: options.model,
1153
- ...options.temperature !== void 0 && {
1154
- temperature: options.temperature
1155
- },
1156
- ...options.maxTokens !== void 0 && {
1157
- max_output_tokens: options.maxTokens
1158
- },
1159
- ...options.topP !== void 0 && { top_p: options.topP },
1160
1153
  ...options.metadata !== void 0 && { metadata: options.metadata },
1161
1154
  ...(() => {
1162
1155
  const prompts = normalizeSystemPrompts(options.systemPrompts);
@@ -1 +1 @@
1
- {"version":3,"file":"responses-text.js","sources":["../../../src/adapters/responses-text.ts"],"sourcesContent":["import { EventType, normalizeSystemPrompts } from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport {\n toRunErrorPayload,\n toRunErrorRawEvent,\n} from '@tanstack/ai/adapter-internals'\nimport { generateId, transformNullsToUndefined } from '@tanstack/ai-utils'\nimport { extractRequestOptions } from '../utils/request-options'\nimport { makeStructuredOutputCompatible } from '../utils/schema-converter'\nimport { buildResponsesUsage } from '../usage'\nimport { convertToolsToResponsesFormat } from './responses-tool-converter'\nimport type OpenAI from 'openai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type {\n Response,\n ResponseCreateParams,\n ResponseFunctionCallOutputItem,\n ResponseInput,\n ResponseInputContent,\n ResponseStreamEvent,\n} from 'openai/resources/responses/responses'\nimport type {\n ContentPart,\n DefaultMessageMetadataByModality,\n Modality,\n ModelMessage,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\n\n/**\n * Shared implementation of the OpenAI Responses API. Holds the stream-event\n * accumulator + AG-UI lifecycle and calls the OpenAI SDK directly. Subclasses\n * (today: ai-openai) construct an OpenAI client with their provider-specific\n * `baseURL` / headers and pass it in.\n */\nexport abstract class OpenAIBaseResponsesTextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, unknown> = Record<string, unknown>,\n TInputModalities extends ReadonlyArray<Modality> = ReadonlyArray<Modality>,\n TMessageMetadata extends DefaultMessageMetadataByModality =\n DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n TMessageMetadata,\n TToolCapabilities\n> {\n override readonly kind = 'text' as const\n readonly name: string\n protected client: OpenAI\n\n constructor(model: TModel, name: string, client: OpenAI) {\n super({}, model)\n this.name = name\n this.client = client\n }\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n // Track tool call metadata by unique ID\n // Responses API streams tool calls with deltas — first chunk has ID/name,\n // subsequent chunks only have args.\n // We assign our own indices as we encounter unique tool call IDs.\n const toolCallMetadata = new Map<\n string,\n {\n index: number\n name: string\n started: boolean\n // Set once TOOL_CALL_END has been emitted (via args.done or the\n // output_item.done backfill) so the two paths don't double-emit.\n ended?: boolean\n // Set when args.done arrives before TOOL_CALL_START could fire\n // (output_item.added lacked a name). output_item.done picks these\n // up to emit the missing END. Allow explicit `undefined` so the\n // emission paths can re-clear the slot after handing it off.\n pendingArguments?: string\n }\n >()\n\n // AG-UI lifecycle tracking\n const aguiState = {\n runId: generateId(this.name),\n threadId: options.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n hasEmittedRunStarted: false,\n }\n\n try {\n // mapOptionsToRequest can throw on caller-side validation failures\n // (empty user content, unsupported parts, webSearchTool() rejection in\n // the OpenRouter override). Keep it inside the try so those failures\n // surface as RUN_ERROR events instead of iterator throws.\n const requestParams = this.mapOptionsToRequest(options)\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const response = await this.client.responses.create(\n {\n ...requestParams,\n stream: true,\n },\n extractRequestOptions(options.request),\n )\n\n yield* this.processStreamChunks(\n response,\n toolCallMetadata,\n options,\n aguiState,\n )\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n const rawEvent = toRunErrorRawEvent(error)\n\n // Emit RUN_STARTED if not yet emitted\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: options.model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n // Emit AG-UI RUN_ERROR. Conditional `code` spread keeps the wire\n // shape spec-compliant under `exactOptionalPropertyTypes`: AG-UI's\n // `RunErrorEvent.code` is `string?` (absent vs explicit `undefined`\n // matter), so we omit the key when there's no code.\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n code: errorPayload.code,\n // Forward the provider's structured error body when present (see\n // toRunErrorRawEvent); omitted otherwise.\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n code: errorPayload.code,\n },\n }\n\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n }\n }\n\n /**\n * Generate structured output using the provider's native JSON Schema response format.\n * Uses stream: false to get the complete response in one call.\n *\n * OpenAI-compatible Responses APIs have strict requirements for structured output:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for all objects\n *\n * The outputSchema is already JSON Schema (converted in the ai layer).\n * We apply provider-specific transformations for structured output compatibility.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapOptionsToRequest(chatOptions)\n\n // Apply provider-specific transformations for structured output compatibility\n const jsonSchema = this.makeStructuredOutputCompatible(\n outputSchema,\n outputSchema.required,\n )\n\n try {\n // Strip streaming-only fields a subclass override of mapOptionsToRequest\n // might have returned (parallel to chat-completions's structuredOutput\n // cleanup) — sending stream_options to a non-streaming call is a 4xx.\n const {\n stream: _stream,\n stream_options: _streamOptions,\n ...cleanParams\n } = requestParams as Record<string, unknown>\n void _stream\n void _streamOptions\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const response = await this.client.responses.create(\n {\n ...(cleanParams as Omit<ResponseCreateParams, 'stream'>),\n stream: false,\n // Configure structured output via text.format\n text: {\n format: {\n type: 'json_schema',\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n },\n extractRequestOptions(chatOptions.request),\n )\n\n // Extract text content from the response. `stream: false` narrows the\n // SDK return type to `Response`, but the explicit annotation makes\n // that contract local rather than relying on inference through the\n // overloaded `client.responses.create` signature.\n const rawText = this.extractTextFromResponse(response satisfies Response)\n\n // Fail loud on empty content rather than letting it cascade into a\n // confusing \"Failed to parse JSON. Content: \" error — the root cause\n // (the model returned no text content for the structured request) is\n // then visible in logs. Mirrors the chat-completions sibling.\n if (rawText.length === 0) {\n throw new Error(\n `${this.name}.structuredOutput: response contained no content`,\n )\n }\n\n // Parse the JSON response\n let parsed: unknown\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to parse structured output as JSON. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n\n // Apply the provider-specific post-parse shaping (default: null →\n // undefined to align with the original Zod schema's optional-field\n // semantics; subclasses with different conventions can override\n // `transformStructuredOutput`, mirroring the chat-completions base's\n // hook so OpenRouter and other providers that preserve nulls in\n // structured output can opt out without forking `structuredOutput`).\n const transformed = this.transformStructuredOutput(parsed)\n\n return {\n data: transformed,\n rawText,\n }\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Stream structured output via the Responses API: single request with\n * `text.format: json_schema` + `stream: true`. Consumes Responses-API\n * events (`response.output_text.delta`, `response.reasoning_text.delta`,\n * `response.reasoning_summary_text.delta`, `response.refusal.delta`,\n * `response.completed`, `response.failed`) and re-emits the standard AG-UI\n * lifecycle ending with `CUSTOM 'structured-output.complete'`.\n *\n * Tools are stripped (structured output is mutually exclusive with tool\n * calls in this path). Reasoning text is accumulated and surfaced both as\n * REASONING_* lifecycle events during the stream and on the terminal\n * CUSTOM event's `value.reasoning`.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapOptionsToRequest(chatOptions)\n\n const jsonSchema = this.makeStructuredOutputCompatible(\n outputSchema,\n outputSchema.required,\n )\n\n const timestamp = Date.now()\n const aguiState = {\n runId: generateId(this.name),\n threadId: chatOptions.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n timestamp,\n hasEmittedRunStarted: false,\n }\n\n let accumulatedContent = ''\n let accumulatedReasoning = ''\n let hasEmittedTextMessageStart = false\n let reasoningMessageId: string | undefined\n let stepId: string | undefined\n let hasClosedReasoning = false\n let model: string = chatOptions.model\n let usage: OpenAI.Responses.Response['usage'] | undefined\n\n const closeReasoning = function* (this: {\n name: string\n }): Generator<StreamChunk> {\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model,\n timestamp,\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model,\n timestamp,\n }\n if (stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model,\n timestamp,\n content: accumulatedReasoning,\n }\n }\n }\n }.bind(this)\n\n const openReasoning = function* (this: {\n name: string\n }): Generator<StreamChunk> {\n if (reasoningMessageId) return\n reasoningMessageId = generateId(this.name)\n stepId = generateId(this.name)\n yield {\n type: EventType.REASONING_START,\n messageId: reasoningMessageId,\n model,\n timestamp,\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning' as const,\n model,\n timestamp,\n }\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model,\n timestamp,\n stepType: 'thinking',\n }\n }.bind(this)\n\n try {\n const { tools: _tools, ...cleanParams } = requestParams\n void _tools\n\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n\n const stream = await this.client.responses.create(\n {\n ...cleanParams,\n stream: true,\n text: {\n format: {\n type: 'json_schema',\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n },\n extractRequestOptions(chatOptions.request),\n )\n\n for await (const chunk of stream) {\n chatOptions.logger.provider(\n `provider=${this.name} type=${chunk.type}`,\n { provider: this.name, type: chunk.type },\n )\n\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model,\n timestamp,\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n if (\n chunk.type === 'response.created' ||\n chunk.type === 'response.in_progress'\n ) {\n const responseModel = (chunk as { response?: { model?: string } })\n .response?.model\n if (responseModel) model = responseModel\n continue\n }\n\n if (chunk.type === 'response.refusal.delta') {\n const delta =\n typeof (chunk as { delta?: unknown }).delta === 'string'\n ? (chunk as { delta: string }).delta\n : ''\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message: `Model refused: ${delta}`,\n code: 'refusal',\n error: { message: `Model refused: ${delta}`, code: 'refusal' },\n }\n return\n }\n\n if (\n chunk.type === 'response.reasoning_text.delta' ||\n chunk.type === 'response.reasoning_summary_text.delta'\n ) {\n const raw = (chunk as { delta?: unknown }).delta\n const reasoningDelta = Array.isArray(raw)\n ? raw.join('')\n : typeof raw === 'string'\n ? raw\n : ''\n if (!reasoningDelta) continue\n yield* openReasoning()\n // openReasoning() guarantees reasoningMessageId is set on first call;\n // TS can't see through the generator side-effect.\n if (!reasoningMessageId) continue\n accumulatedReasoning += reasoningDelta\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta: reasoningDelta,\n model,\n timestamp,\n }\n continue\n }\n\n if (chunk.type === 'response.output_text.delta') {\n const raw = (chunk as { delta?: unknown }).delta\n const textDelta = Array.isArray(raw)\n ? raw.join('')\n : typeof raw === 'string'\n ? raw\n : ''\n if (!textDelta) continue\n\n yield* closeReasoning()\n\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model,\n timestamp,\n role: 'assistant',\n }\n }\n accumulatedContent += textDelta\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model,\n timestamp,\n delta: textDelta,\n content: accumulatedContent,\n }\n continue\n }\n\n if (chunk.type === 'response.completed') {\n const response = chunk.response\n if (response.usage) usage = response.usage\n if (response.model) model = response.model\n continue\n }\n\n if (chunk.type === 'response.failed') {\n const response = (\n chunk as {\n response?: { error?: { message?: string; code?: string } }\n }\n ).response\n const message =\n response?.error?.message || 'Responses API stream failed'\n const code = response?.error?.code\n // Conditional `code` spread keeps the wire shape spec-compliant\n // under `exactOptionalPropertyTypes` (see chatStream catch).\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message,\n ...(code !== undefined && { code }),\n error: { message, ...(code !== undefined && { code }) },\n }\n return\n }\n }\n\n yield* closeReasoning()\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model,\n timestamp,\n }\n }\n\n if (accumulatedContent.length === 0) {\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n error: {\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n },\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedContent)\n } catch {\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message: `Failed to parse structured output as JSON. Content: ${accumulatedContent.slice(0, 200)}${accumulatedContent.length > 200 ? '...' : ''}`,\n code: 'parse-error',\n error: {\n message: 'Failed to parse structured output as JSON',\n code: 'parse-error',\n },\n }\n return\n }\n\n const transformed = transformNullsToUndefined(parsed)\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: transformed,\n raw: accumulatedContent,\n ...(accumulatedReasoning ? { reasoning: accumulatedReasoning } : {}),\n },\n model,\n timestamp,\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model,\n timestamp,\n finishReason: 'stop',\n ...(usage && {\n usage: buildResponsesUsage(usage),\n }),\n }\n } catch (error: unknown) {\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model,\n timestamp,\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n const isAbort = this.isAbortError(error)\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes` (see chatStream catch).\n const resolvedCode = isAbort ? 'aborted' : errorPayload.code\n const rawEvent = isAbort ? undefined : toRunErrorRawEvent(error)\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message: errorPayload.message,\n ...(resolvedCode !== undefined && { code: resolvedCode }),\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n ...(resolvedCode !== undefined && { code: resolvedCode }),\n },\n }\n\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n }\n }\n\n /**\n * Cross-SDK abort detection for `structuredOutputStream`. Mirrors the\n * Chat Completions base; subclasses with proprietary error types override.\n */\n protected isAbortError(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false\n const e = error as { name?: unknown; code?: unknown }\n return (\n e.name === 'APIUserAbortError' ||\n e.name === 'AbortError' ||\n e.code === 'ERR_CANCELED'\n )\n }\n\n /**\n * Applies provider-specific transformations for structured output compatibility.\n * Override this in subclasses to handle provider-specific quirks.\n */\n protected makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n ): Record<string, any> {\n return makeStructuredOutputCompatible(schema, originalRequired)\n }\n\n /**\n * Final shaping pass applied to parsed structured-output JSON before it is\n * returned to the caller. Default converts `null` values to `undefined` so\n * the result aligns with the original Zod schema's optional-field\n * semantics. Subclasses with different conventions (OpenRouter historically\n * preserves nulls) can override — mirrors the chat-completions base's hook\n * so a subclass that opts out of null-stripping doesn't have to fork the\n * whole `structuredOutput` method.\n */\n protected transformStructuredOutput(parsed: unknown): unknown {\n return transformNullsToUndefined(parsed)\n }\n\n /**\n * Extract text content from a non-streaming Responses API response.\n * Override this in subclasses for provider-specific response shapes.\n */\n protected extractTextFromResponse(response: Response): string {\n let textContent = ''\n let refusal: string | undefined\n let sawMessageItem = false\n const observedItemTypes = new Set<string>()\n\n for (const item of response.output) {\n observedItemTypes.add(item.type)\n if (item.type === 'message') {\n sawMessageItem = true\n for (const part of item.content) {\n // Cast off the discriminated union before the type discrimination\n // so future SDK variants (e.g. `output_audio`, `output_image`) hit\n // the explicit error path rather than being misreported as refusals\n // when they get added to the union. Mirrors the streaming side's\n // handleContentPart.\n const partType = (part as { type: string }).type\n if (partType === 'output_text') {\n textContent += (part as { text?: string }).text ?? ''\n } else if (partType === 'refusal') {\n const refusalText = (part as { refusal?: string }).refusal\n refusal = refusalText || refusal || 'Refused without explanation'\n } else {\n throw new Error(\n `${this.name}.extractTextFromResponse: unsupported message content part type \"${partType}\"`,\n )\n }\n }\n }\n }\n\n // Surface refusals as an explicit error so callers don't see a generic\n // \"Failed to parse structured output as JSON. Content: \" when the model\n // refused for safety / content-policy reasons.\n if (!textContent && refusal !== undefined) {\n const err = new Error(`Model refused to respond: ${refusal}`)\n ;(err as Error & { code?: string }).code = 'refusal'\n throw err\n }\n\n // Response had items but none carried message text (e.g. only\n // function_call or reasoning items). Surface that explicitly so a\n // downstream structured-output caller doesn't see a misleading\n // \"Failed to parse JSON. Content: \" from an empty string.\n if (!textContent && response.output.length > 0 && !sawMessageItem) {\n throw new Error(\n `${this.name}.extractTextFromResponse: response.output contained items of type(s) [${[...observedItemTypes].sort().join(', ')}] but no message text — the model returned a non-text response`,\n )\n }\n\n return textContent\n }\n\n /**\n * Processes streamed chunks from the Responses API and yields AG-UI events.\n * Override this in subclasses to handle provider-specific stream behavior.\n *\n * Handles the following event types:\n * - response.created / response.incomplete / response.failed\n * - response.output_text.delta\n * - response.reasoning_text.delta\n * - response.reasoning_summary_text.delta\n * - response.content_part.added / response.content_part.done\n * - response.output_item.added\n * - response.function_call_arguments.delta / response.function_call_arguments.done\n * - response.completed\n * - error\n */\n protected async *processStreamChunks(\n stream: AsyncIterable<ResponseStreamEvent>,\n toolCallMetadata: Map<\n string,\n {\n index: number\n name: string\n started: boolean\n ended?: boolean\n pendingArguments?: string | undefined\n }\n >,\n options: TextOptions<TProviderOptions>,\n aguiState: {\n runId: string\n threadId: string\n messageId: string\n hasEmittedRunStarted: boolean\n },\n ): AsyncIterable<StreamChunk> {\n let accumulatedContent = ''\n let accumulatedReasoning = ''\n\n // Track if we've been streaming deltas to avoid duplicating content from done events\n let hasStreamedContentDeltas = false\n let hasStreamedReasoningDeltas = false\n\n // Preserve response metadata across events\n let model: string = options.model\n\n // AG-UI lifecycle tracking\n let stepId: string | null = null\n let hasEmittedTextMessageStart = false\n let hasEmittedStepStarted = false\n // Track whether we've emitted a terminal RUN_FINISHED so the\n // end-of-stream fallback below knows to synthesise one when the upstream\n // cuts off without a response.completed event.\n let runFinishedEmitted = false\n\n try {\n for await (const chunk of stream) {\n options.logger.provider(`provider=${this.name} type=${chunk.type}`, {\n provider: this.name,\n type: chunk.type,\n })\n\n // Emit RUN_STARTED on first chunk\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: model || options.model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n const handleContentPart = (contentPart: {\n type: string\n text?: string\n refusal?: string\n }): StreamChunk => {\n if (contentPart.type === 'output_text') {\n accumulatedContent += contentPart.text || ''\n return {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: contentPart.text || '',\n content: accumulatedContent,\n }\n }\n\n if (contentPart.type === 'reasoning_text') {\n accumulatedReasoning += contentPart.text || ''\n // Cache the fallback stepId rather than generating a fresh one\n // on every call — otherwise multiple reasoning chunks arriving\n // before STEP_STARTED was emitted (e.g. via response.content_part.done\n // alone) would each get a different stepId and break correlation.\n if (!stepId) {\n stepId = generateId(this.name)\n }\n return {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: contentPart.text || '',\n content: accumulatedReasoning,\n }\n }\n // Either a real refusal or an unknown content_part type. Surface\n // the part type in the error so unknown parts are debuggable\n // instead of being misreported as \"Unknown refusal\".\n const isRefusal = contentPart.type === 'refusal'\n const message = isRefusal\n ? contentPart.refusal || 'Refused without explanation'\n : `Unsupported response content_part type: ${contentPart.type}`\n const code = isRefusal ? 'refusal' : contentPart.type\n return {\n type: EventType.RUN_ERROR,\n model: model || options.model,\n timestamp: Date.now(),\n message,\n code,\n error: { message, code },\n }\n }\n\n // Capture model metadata from any of these events (created starts\n // the run; failed/incomplete signal terminal failure).\n if (\n chunk.type === 'response.created' ||\n chunk.type === 'response.incomplete' ||\n chunk.type === 'response.failed'\n ) {\n model = chunk.response.model\n }\n\n // response.created marks the start of a fresh run — safe to reset\n // the per-run accumulators here.\n if (chunk.type === 'response.created') {\n hasStreamedContentDeltas = false\n hasStreamedReasoningDeltas = false\n hasEmittedTextMessageStart = false\n hasEmittedStepStarted = false\n accumulatedContent = ''\n accumulatedReasoning = ''\n }\n\n // response.failed and response.incomplete are TERMINAL events for\n // the current response. Close any open AG-UI message lifecycle FIRST\n // so consumers tracking start/end pairs don't see an unbalanced\n // TEXT_MESSAGE_START. Then surface the error and mark the run as\n // finished so the post-loop synthetic terminal block doesn't emit\n // a duplicate RUN_FINISHED on top of RUN_ERROR.\n if (\n chunk.type === 'response.failed' ||\n chunk.type === 'response.incomplete'\n ) {\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: chunk.response.model,\n timestamp: Date.now(),\n }\n hasEmittedTextMessageStart = false\n }\n // Coalesce error + incomplete_details into a single RUN_ERROR\n // payload — emitting two distinct events for one terminal upstream\n // event would force consumers to handle a non-existent ordering.\n const errorMessage =\n chunk.response.error?.message ||\n chunk.response.incomplete_details?.reason ||\n (chunk.type === 'response.failed'\n ? 'Response failed'\n : 'Response ended incomplete')\n const errorCode =\n chunk.response.error?.code ??\n (chunk.response.incomplete_details ? 'incomplete' : undefined) ??\n undefined\n // Always emit RUN_ERROR for terminal failure events, even when the\n // upstream omitted both `error` and `incomplete_details`. Skipping\n // emission on a `response.incomplete` with no detail would let the\n // post-loop synthetic block silently coerce the run to a clean\n // `RUN_FINISHED { finishReason: 'stop' }` — masking the failure.\n yield {\n type: EventType.RUN_ERROR,\n model: chunk.response.model,\n timestamp: Date.now(),\n message: errorMessage,\n ...(errorCode !== undefined && { code: errorCode }),\n error: {\n message: errorMessage,\n ...(errorCode !== undefined && { code: errorCode }),\n },\n }\n // RUN_ERROR is the terminal event for this run; stop processing\n // any further chunks the iterator might still deliver.\n runFinishedEmitted = true\n return\n }\n\n // Handle output text deltas (token-by-token streaming)\n // response.output_text.delta provides incremental text updates\n if (chunk.type === 'response.output_text.delta' && chunk.delta) {\n // Delta can be an array of strings or a single string\n const textDelta = Array.isArray(chunk.delta)\n ? chunk.delta.join('')\n : typeof chunk.delta === 'string'\n ? chunk.delta\n : ''\n\n if (textDelta) {\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n }\n\n accumulatedContent += textDelta\n hasStreamedContentDeltas = true\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: textDelta,\n content: accumulatedContent,\n }\n }\n }\n\n // Handle reasoning deltas (token-by-token thinking/reasoning streaming)\n // response.reasoning_text.delta provides incremental reasoning updates\n if (chunk.type === 'response.reasoning_text.delta' && chunk.delta) {\n // Delta can be an array of strings or a single string\n const reasoningDelta = Array.isArray(chunk.delta)\n ? chunk.delta.join('')\n : typeof chunk.delta === 'string'\n ? chunk.delta\n : ''\n\n if (reasoningDelta) {\n // Emit STEP_STARTED on first reasoning content\n if (!hasEmittedStepStarted) {\n hasEmittedStepStarted = true\n stepId = generateId(this.name)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n\n accumulatedReasoning += reasoningDelta\n hasStreamedReasoningDeltas = true\n const fallbackStepId = stepId || generateId(this.name)\n yield {\n type: EventType.STEP_FINISHED,\n stepName: fallbackStepId,\n stepId: fallbackStepId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: reasoningDelta,\n content: accumulatedReasoning,\n }\n }\n }\n\n // Handle reasoning summary deltas (when using reasoning.summary option)\n // response.reasoning_summary_text.delta provides incremental summary updates\n if (\n chunk.type === 'response.reasoning_summary_text.delta' &&\n chunk.delta\n ) {\n const summaryDelta =\n typeof chunk.delta === 'string' ? chunk.delta : ''\n\n if (summaryDelta) {\n // Emit STEP_STARTED on first reasoning content\n if (!hasEmittedStepStarted) {\n hasEmittedStepStarted = true\n stepId = generateId(this.name)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n\n accumulatedReasoning += summaryDelta\n hasStreamedReasoningDeltas = true\n const fallbackStepId = stepId || generateId(this.name)\n yield {\n type: EventType.STEP_FINISHED,\n stepName: fallbackStepId,\n stepId: fallbackStepId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: summaryDelta,\n content: accumulatedReasoning,\n }\n }\n }\n\n // handle content_part added events for text, reasoning and refusals\n if (chunk.type === 'response.content_part.added') {\n const contentPart = chunk.part\n // Emit TEXT_MESSAGE_START if this is text content\n if (\n contentPart.type === 'output_text' &&\n !hasEmittedTextMessageStart\n ) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n }\n // Emit STEP_STARTED if this is reasoning content\n if (contentPart.type === 'reasoning_text' && !hasEmittedStepStarted) {\n hasEmittedStepStarted = true\n stepId = generateId(this.name)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n // Mark whichever stream we just emitted into so a subsequent\n // `content_part.done` doesn't duplicate the same text. Without\n // this flag, an `added` event carrying the full text followed by\n // a matching `done` event would emit TEXT_MESSAGE_CONTENT twice.\n if (contentPart.type === 'output_text') {\n hasStreamedContentDeltas = true\n } else if (contentPart.type === 'reasoning_text') {\n hasStreamedReasoningDeltas = true\n }\n const partChunk = handleContentPart(contentPart)\n yield partChunk\n // handleContentPart returns RUN_ERROR for refusals / unknown\n // content_part types — those are terminal events. Don't keep\n // processing more chunks (and don't let the post-loop synthetic\n // block emit a second terminal event).\n if (partChunk.type === 'RUN_ERROR') {\n runFinishedEmitted = true\n return\n }\n }\n\n if (chunk.type === 'response.content_part.done') {\n const contentPart = chunk.part\n\n // Skip emitting chunks for content parts that we've already streamed via deltas\n // The done event is just a completion marker, not new content\n if (contentPart.type === 'output_text' && hasStreamedContentDeltas) {\n // Content already accumulated from deltas, skip\n continue\n }\n if (\n contentPart.type === 'reasoning_text' &&\n hasStreamedReasoningDeltas\n ) {\n // Reasoning already accumulated from deltas, skip\n continue\n }\n\n // Upstreams that emit `content_part.done` without any preceding\n // deltas (or `content_part.added`) still need a START event before\n // CONTENT — otherwise consumers tracking start/end pairs see content\n // without a start and never see an end. Emit the lifecycle opener\n // for whichever stream this content_part belongs to before yielding\n // the CONTENT chunk; the post-loop block emits the matching END.\n if (\n contentPart.type === 'output_text' &&\n !hasEmittedTextMessageStart\n ) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n } else if (\n contentPart.type === 'reasoning_text' &&\n !hasEmittedStepStarted\n ) {\n hasEmittedStepStarted = true\n stepId = generateId(this.name)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n\n // Only emit if we haven't been streaming deltas (e.g., for non-streaming responses)\n const doneChunk = handleContentPart(contentPart)\n yield doneChunk\n if (doneChunk.type === 'RUN_ERROR') {\n runFinishedEmitted = true\n return\n }\n }\n\n // handle output_item.added to capture function call metadata (name)\n if (chunk.type === 'response.output_item.added') {\n const item = chunk.item\n if (item.type === 'function_call' && item.id) {\n // Track the item as soon as we see it so subsequent arg deltas\n // aren't logged as orphans, but only emit TOOL_CALL_START when\n // both id AND name are populated. Emitting START with an empty\n // name would propagate into TOOL_CALL_END (which reads the same\n // metadata) and route the tool call to whatever name happens to\n // match `''` downstream — a silent misroute.\n let metadata = toolCallMetadata.get(item.id)\n if (!metadata) {\n metadata = {\n index: chunk.output_index,\n name: item.name || '',\n started: false,\n }\n toolCallMetadata.set(item.id, metadata)\n } else if (!metadata.name && item.name) {\n // A later output_item.added for the same id finally carries\n // the name. Update so the gated emission below can fire.\n metadata.name = item.name\n }\n if (!metadata.started && metadata.name) {\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: item.id,\n toolCallName: metadata.name,\n toolName: metadata.name,\n model: model || options.model,\n timestamp: Date.now(),\n index: chunk.output_index,\n }\n metadata.started = true\n }\n }\n }\n\n // Handle function call arguments delta (streaming). Drop the\n // previously-emitted `args` field — it had inverted polarity\n // (populated only when metadata was MISSING, i.e. when the\n // matching TOOL_CALL_START hadn't fired) and the chat-completions\n // adapter never emitted it, so it leaked partial deltas as\n // pseudo-args only on the orphan path. Consumers should accumulate\n // `delta` themselves.\n //\n // Guard with `metadata?.started`: the matching TOOL_CALL_START fires\n // from `output_item.added`, and emitting TOOL_CALL_ARGS before that\n // would violate the AG-UI lifecycle (ARGS without START). The .done\n // handler below applies the same guard.\n if (\n chunk.type === 'response.function_call_arguments.delta' &&\n chunk.delta\n ) {\n const metadata = toolCallMetadata.get(chunk.item_id)\n if (!metadata?.started) {\n options.logger.errors(\n `${this.name}.processStreamChunks orphan function_call_arguments.delta`,\n {\n source: `${this.name}.processStreamChunks`,\n toolCallId: chunk.item_id,\n rawDelta: chunk.delta,\n },\n )\n continue\n }\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: chunk.item_id,\n model: model || options.model,\n timestamp: Date.now(),\n delta: chunk.delta,\n }\n }\n\n if (chunk.type === 'response.function_call_arguments.done') {\n const { item_id } = chunk\n\n // Get the function name from metadata (captured in output_item.added)\n const metadata = toolCallMetadata.get(item_id)\n // If the matching START was never emitted (the upstream sent an\n // `output_item.added` without a name and no later event has filled\n // it in yet), defer END until `output_item.done` or\n // `response.completed` can backfill the name. We stash the raw\n // arguments so the late emission has them. Emitting END without\n // START would produce an unbalanced AG-UI lifecycle event\n // downstream consumers can't pair.\n if (!metadata?.started) {\n if (metadata) {\n metadata.pendingArguments = chunk.arguments\n }\n options.logger.errors(\n `${this.name}.processStreamChunks deferring function_call_arguments.done — TOOL_CALL_START not yet emitted (waiting for name)`,\n {\n source: `${this.name}.processStreamChunks`,\n toolCallId: item_id,\n rawArguments: chunk.arguments,\n },\n )\n continue\n }\n // The output_item.done backstop may have already emitted END (when\n // it arrived before args.done with a populated item.arguments).\n // Skip so we never produce a duplicate close for the same id.\n if (metadata.ended) continue\n const name = metadata.name || ''\n metadata.ended = true\n\n // Parse arguments. Surface parse failures via the logger so a\n // model emitting malformed JSON is debuggable instead of silently\n // invoking the tool with {}.\n let parsedInput: unknown = {}\n if (chunk.arguments) {\n try {\n const parsed = JSON.parse(chunk.arguments)\n parsedInput = parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${name} (${item_id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: item_id,\n toolName: name,\n rawArguments: chunk.arguments,\n },\n )\n parsedInput = {}\n }\n }\n\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: item_id,\n toolCallName: name,\n toolName: name,\n model: model || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n }\n\n // `output_item.done` is the last point at which a function_call's\n // name is guaranteed to be on the wire — it carries the fully-formed\n // ResponseFunctionToolCall. Use it as a backstop to recover any\n // tool call whose name was missing from `output_item.added` (and\n // whose START + END therefore never fired).\n if (chunk.type === 'response.output_item.done') {\n const item = chunk.item\n if (item.type === 'function_call' && item.id) {\n const metadata = toolCallMetadata.get(item.id) ?? {\n index: chunk.output_index,\n name: item.name || '',\n started: false,\n }\n if (!toolCallMetadata.has(item.id)) {\n toolCallMetadata.set(item.id, metadata)\n } else if (!metadata.name && item.name) {\n metadata.name = item.name\n }\n // Emit gated START if we now have a name and never started.\n if (!metadata.started && metadata.name) {\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: item.id,\n toolCallName: metadata.name,\n toolName: metadata.name,\n model: model || options.model,\n timestamp: Date.now(),\n index: metadata.index,\n }\n metadata.started = true\n }\n // Emit END if we have args (either from a previously-deferred\n // args.done OR from item.arguments) and haven't already ended.\n const rawArgs =\n typeof item.arguments === 'string' && item.arguments.length > 0\n ? item.arguments\n : metadata.pendingArguments\n if (metadata.started && !metadata.ended && rawArgs !== undefined) {\n const name = metadata.name || ''\n let parsedInput: unknown = {}\n if (rawArgs) {\n try {\n const parsed = JSON.parse(rawArgs)\n parsedInput =\n parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed (output_item.done backfill)`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${name} (${item.id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: item.id,\n toolName: name,\n rawArguments: rawArgs,\n },\n )\n parsedInput = {}\n }\n }\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: item.id,\n toolCallName: name,\n toolName: name,\n model: model || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n metadata.ended = true\n metadata.pendingArguments = undefined\n }\n }\n }\n\n if (chunk.type === 'response.completed') {\n // Final backstop for function_call lifecycle: if a function_call\n // appears in `response.output[]` but was never matched by an\n // output_item.added/done with a name, recover the missing START\n // (and END if args were pending). Without this, a tool call could\n // be silently dropped from the AG-UI stream while `hasFunctionCalls`\n // below still routes the run's finishReason to 'tool_calls' —\n // leaving consumers waiting for tool results they never saw start.\n for (const item of chunk.response.output) {\n if (item.type !== 'function_call' || !item.id) continue\n const metadata = toolCallMetadata.get(item.id) ?? {\n index: 0,\n name: item.name || '',\n started: false,\n }\n if (!toolCallMetadata.has(item.id)) {\n toolCallMetadata.set(item.id, metadata)\n } else if (!metadata.name && item.name) {\n metadata.name = item.name\n }\n if (!metadata.started && metadata.name) {\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: item.id,\n toolCallName: metadata.name,\n toolName: metadata.name,\n model: model || options.model,\n timestamp: Date.now(),\n index: metadata.index,\n }\n metadata.started = true\n }\n const rawArgs =\n typeof item.arguments === 'string' && item.arguments.length > 0\n ? item.arguments\n : metadata.pendingArguments\n if (metadata.started && !metadata.ended) {\n const name = metadata.name || ''\n let parsedInput: unknown = {}\n if (rawArgs) {\n try {\n const parsed = JSON.parse(rawArgs)\n parsedInput =\n parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed (response.completed backfill)`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${name} (${item.id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: item.id,\n toolName: name,\n rawArguments: rawArgs,\n },\n )\n parsedInput = {}\n }\n }\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: item.id,\n toolCallName: name,\n toolName: name,\n model: model || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n metadata.ended = true\n metadata.pendingArguments = undefined\n }\n }\n\n // Emit TEXT_MESSAGE_END if we had text content\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n }\n hasEmittedTextMessageStart = false\n }\n\n // Determine finish reason. Function-call output → tool_calls.\n // Otherwise surface incomplete_details.reason when present so\n // callers can distinguish length-limit / content-filter cutoffs\n // from a clean stop, mirroring the chat-completions adapter.\n // The Responses API's incomplete_details.reason ('max_output_tokens'\n // | 'content_filter') maps to the AG-UI finishReason vocabulary:\n // max_output_tokens → 'length', content_filter → 'content_filter'.\n const hasFunctionCalls = chunk.response.output.some(\n (item: unknown) =>\n (item as { type: string }).type === 'function_call',\n )\n const incompleteReason = chunk.response.incomplete_details?.reason\n const finishReason:\n | 'tool_calls'\n | 'length'\n | 'content_filter'\n | 'stop' = hasFunctionCalls\n ? 'tool_calls'\n : incompleteReason === 'max_output_tokens'\n ? 'length'\n : incompleteReason === 'content_filter'\n ? 'content_filter'\n : 'stop'\n\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: model || options.model,\n timestamp: Date.now(),\n // Omit usage entirely when the provider reported none rather than\n // emitting fabricated zeros (also satisfies exactOptionalPropertyTypes).\n ...(chunk.response.usage && {\n usage: buildResponsesUsage(chunk.response.usage),\n }),\n finishReason,\n }\n runFinishedEmitted = true\n }\n\n if (chunk.type === 'error') {\n // Conditional `code` spread keeps the wire shape spec-compliant\n // under `exactOptionalPropertyTypes` (see chatStream catch).\n const code = chunk.code ?? undefined\n yield {\n type: EventType.RUN_ERROR,\n model: model || options.model,\n timestamp: Date.now(),\n message: chunk.message,\n ...(code !== undefined && { code }),\n error: {\n message: chunk.message,\n ...(code !== undefined && { code }),\n },\n }\n // RUN_ERROR is terminal — don't let the synthetic RUN_FINISHED\n // block fire after a top-level stream error event, and stop\n // processing further chunks so no in-flight lifecycle events\n // (TEXT_MESSAGE_CONTENT, TOOL_CALL_*) leak past the terminal\n // error. Mirrors the `response.failed` / `response.incomplete`\n // branches above which return after their RUN_ERROR emission.\n runFinishedEmitted = true\n return\n }\n }\n\n // Synthetic terminal RUN_FINISHED if the stream ended without a\n // response.completed event (e.g. truncated upstream connection). This\n // mirrors the chat-completions adapter's behavior so consumers always\n // see a terminal event for every started run.\n if (!runFinishedEmitted && aguiState.hasEmittedRunStarted) {\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n }\n }\n // Omit `usage` entirely (vs `usage: undefined`) — the synthetic\n // RUN_FINISHED for truncated streams has no usage data, and AG-UI's\n // `RunFinishedEvent.usage` is optional without `| undefined` under\n // `exactOptionalPropertyTypes`.\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: model || options.model,\n timestamp: Date.now(),\n finishReason: toolCallMetadata.size > 0 ? 'tool_calls' : 'stop',\n }\n }\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.processStreamChunks failed`,\n )\n const rawEvent = toRunErrorRawEvent(error)\n options.logger.errors(`${this.name}.processStreamChunks fatal`, {\n error: errorPayload,\n source: `${this.name}.processStreamChunks`,\n })\n // Emit AG-UI RUN_ERROR with conditional `code` spread (see chatStream\n // catch for the rationale). `rawEvent` carries the provider's structured\n // error body when present.\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Maps common TextOptions to Responses API request format.\n * Override this in subclasses to add provider-specific options.\n */\n protected mapOptionsToRequest(\n options: TextOptions<TProviderOptions>,\n ): Omit<ResponseCreateParams, 'stream'> {\n const input = this.convertMessagesToInput(options.messages)\n\n const tools = options.tools\n ? convertToolsToResponsesFormat(\n options.tools,\n this.makeStructuredOutputCompatible.bind(this),\n )\n : undefined\n\n const modelOptions = options.modelOptions\n\n // Native combined mode (issue #605): when the engine threads\n // `outputSchema` through TextOptions, the adapter declared\n // `supportsCombinedToolsAndSchema` and the schema is already JSON Schema\n // (pre-converted at the activity boundary). Wire it into `text.format`\n // alongside any `tools` — the Responses API supports both together and\n // emits the schema-constrained text on the natural final turn.\n const combinedSchema = options.outputSchema as\n | Record<string, unknown>\n | undefined\n const textFormat = combinedSchema\n ? {\n text: {\n format: {\n type: 'json_schema' as const,\n name: 'structured_output',\n schema: this.makeStructuredOutputCompatible(\n combinedSchema,\n Array.isArray(combinedSchema.required)\n ? (combinedSchema.required as Array<string>)\n : undefined,\n ),\n strict: true,\n },\n },\n }\n : undefined\n\n // Spread modelOptions first, then explicit top-level options when set.\n // Mirrors the chat-completions base adapter's precedence so callers\n // tuning either backend get identical behaviour. Leaving `modelOptions`\n // last (its previous behavior) silently shadowed the canonical\n // `options.temperature`/`maxTokens` fields, while spreading first\n // without nullish-aware merge would clobber `modelOptions.temperature`\n // with `undefined` whenever the caller didn't set the top-level option.\n return {\n ...modelOptions,\n model: options.model,\n ...(options.temperature !== undefined && {\n temperature: options.temperature,\n }),\n ...(options.maxTokens !== undefined && {\n max_output_tokens: options.maxTokens,\n }),\n ...(options.topP !== undefined && { top_p: options.topP }),\n ...(options.metadata !== undefined && { metadata: options.metadata }),\n ...(() => {\n const prompts = normalizeSystemPrompts(options.systemPrompts)\n if (prompts.length === 0) return {}\n return { instructions: prompts.map((p) => p.content).join('\\n') }\n })(),\n input,\n // Conditional spread: `tools: undefined` would clobber any\n // modelOptions.tools the caller set above.\n ...(tools && tools.length > 0 && { tools }),\n ...(textFormat ?? {}),\n }\n }\n\n /**\n * The OpenAI Responses API supports `tools` and `text.format: json_schema`\n * together in a single streaming request (per issue #605). Subclasses\n * that route to providers without this capability should override.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return true\n }\n\n /**\n * Converts ModelMessage[] to Responses API ResponseInput format.\n * Override this in subclasses for provider-specific message format quirks.\n *\n * Key differences from Chat Completions:\n * - Tool results use `function_call_output` type (not `tool` role)\n * - Assistant tool calls are `function_call` objects (not nested in `tool_calls`)\n * - User content uses `input_text`, `input_image`, `input_file` types\n * - System prompts go in `instructions`, not as messages\n */\n protected convertMessagesToInput(\n messages: Array<ModelMessage>,\n ): ResponseInput {\n const result: ResponseInput = []\n\n for (const message of messages) {\n // Handle tool messages - convert to FunctionToolCallOutput\n if (message.role === 'tool') {\n const toolContent = message.content\n const output: string | Array<ResponseFunctionCallOutputItem> =\n Array.isArray(toolContent)\n ? toolContent.map((part) => this.convertContentPartToInput(part))\n : typeof toolContent === 'string'\n ? toolContent\n : JSON.stringify(toolContent)\n result.push({\n type: 'function_call_output',\n call_id: message.toolCallId || '',\n output,\n })\n continue\n }\n\n // Handle assistant messages\n if (message.role === 'assistant') {\n // If the assistant message has tool calls, add them as FunctionToolCall objects\n // Responses API expects arguments as a string (JSON string)\n if (message.toolCalls && message.toolCalls.length > 0) {\n for (const toolCall of message.toolCalls) {\n // Keep arguments as string for Responses API\n const argumentsString =\n typeof toolCall.function.arguments === 'string'\n ? toolCall.function.arguments\n : JSON.stringify(toolCall.function.arguments)\n\n result.push({\n type: 'function_call',\n call_id: toolCall.id,\n name: toolCall.function.name,\n arguments: argumentsString,\n })\n }\n }\n\n // Add the assistant's text message if there is content\n if (message.content) {\n const contentStr = this.extractTextContent(message.content)\n if (contentStr) {\n result.push({\n type: 'message',\n role: 'assistant',\n content: contentStr,\n })\n }\n }\n\n continue\n }\n\n // Handle user messages (default case) — support multimodal content\n const contentParts = this.normalizeContent(message.content)\n const inputContent: Array<ResponseInputContent> = []\n\n for (const part of contentParts) {\n inputContent.push(this.convertContentPartToInput(part))\n }\n\n if (inputContent.length === 0) {\n // Fail loud rather than silently sending an empty user message —\n // mirrors the chat-completions adapter, where a paid-but-empty\n // request would mask the real intent (caller passed `null` content\n // or a normalize step dropped everything).\n throw new Error(\n `User message for ${this.name} has no content parts. ` +\n `Empty user messages would produce a paid request with no input; ` +\n `provide at least one text/image/audio part or omit the message.`,\n )\n }\n\n result.push({\n type: 'message',\n role: 'user',\n content: inputContent,\n })\n }\n\n return result\n }\n\n /**\n * Converts a ContentPart to Responses API input content item.\n * Handles text, image, and audio content parts.\n * Override this in subclasses for additional content types or provider-specific metadata.\n */\n protected convertContentPartToInput(part: ContentPart): ResponseInputContent {\n switch (part.type) {\n case 'text':\n return {\n type: 'input_text',\n text: part.content,\n }\n case 'image': {\n const imageMetadata = part.metadata as\n | { detail?: 'auto' | 'low' | 'high' }\n | undefined\n if (part.source.type === 'url') {\n return {\n type: 'input_image',\n image_url: part.source.value,\n detail: imageMetadata?.detail || 'auto',\n }\n }\n // For base64 data, construct a data URI using the mimeType from\n // source. Default to a generic octet-stream MIME if the source\n // didn't supply one — letting `undefined` interpolate would produce\n // an invalid URI like \"data:undefined;base64,...\".\n const imageValue = part.source.value\n const imageMime = part.source.mimeType || 'application/octet-stream'\n const imageUrl = imageValue.startsWith('data:')\n ? imageValue\n : `data:${imageMime};base64,${imageValue}`\n return {\n type: 'input_image',\n image_url: imageUrl,\n detail: imageMetadata?.detail || 'auto',\n }\n }\n case 'audio': {\n if (part.source.type === 'url') {\n return {\n type: 'input_file',\n file_url: part.source.value,\n }\n }\n // Wrap raw base64 in a data URL — `input_file` rejects bare base64\n // payloads (matches the image branch above which already does this).\n // Default the MIME if missing so we never interpolate `undefined`.\n const audioValue = part.source.value\n const audioMime = part.source.mimeType || 'application/octet-stream'\n const audioFileData = audioValue.startsWith('data:')\n ? audioValue\n : `data:${audioMime};base64,${audioValue}`\n return {\n type: 'input_file',\n file_data: audioFileData,\n }\n }\n\n case 'video':\n case 'document':\n default:\n // OpenAI Responses API doesn't accept native video/document parts on\n // this path — surface as explicit unsupported error so callers see\n // the same message regardless of which content type leaked through.\n throw new Error(`Unsupported content part type: ${part.type}`)\n }\n }\n\n /**\n * Normalizes message content to an array of ContentPart.\n * Handles backward compatibility with string content.\n */\n protected normalizeContent(\n content: string | null | Array<ContentPart>,\n ): Array<ContentPart> {\n if (content === null) {\n return []\n }\n if (typeof content === 'string') {\n return [{ type: 'text', content: content }]\n }\n return content\n }\n\n /**\n * Extracts text content from a content value that may be string, null, or ContentPart array.\n */\n protected extractTextContent(\n content: string | null | Array<ContentPart>,\n ): string {\n if (content === null) {\n return ''\n }\n if (typeof content === 'string') {\n return content\n }\n // It's an array of ContentPart\n return content\n .filter((p) => p.type === 'text')\n .map((p) => p.content)\n .join('')\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAuCO,MAAe,uCAOZ,gBAMR;AAAA,EACkB,OAAO;AAAA,EAChB;AAAA,EACC;AAAA,E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AAAA,MACL;AAIE,cAAM,IAAI,MAAM,kCAAkC,KAAK,IAAI,EAAE;AAAA,IAAA;AAAA,EAEnE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,iBACR,SACoB;AACpB,QAAI,YAAY,MAAM;AACpB,aAAO,CAAA;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO,CAAC,EAAE,MAAM,QAAQ,SAAkB;AAAA,IAC5C;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKU,mBACR,SACQ;AACR,QAAI,YAAY,MAAM;AACpB,aAAO;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO;AAAA,IACT;AAEA,WAAO,QACJ,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAC/B,IAAI,CAAC,MAAM,EAAE,OAAO,EACpB,KAAK,EAAE;AAAA,EACZ;AACF;"}
1
+ {"version":3,"file":"responses-text.js","sources":["../../../src/adapters/responses-text.ts"],"sourcesContent":["import { EventType, normalizeSystemPrompts } from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport {\n toRunErrorPayload,\n toRunErrorRawEvent,\n} from '@tanstack/ai/adapter-internals'\nimport { generateId, transformNullsToUndefined } from '@tanstack/ai-utils'\nimport { extractRequestOptions } from '../utils/request-options'\nimport { makeStructuredOutputCompatible } from '../utils/schema-converter'\nimport { buildResponsesUsage } from '../usage'\nimport { convertToolsToResponsesFormat } from './responses-tool-converter'\nimport type OpenAI from 'openai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type {\n Response,\n ResponseCreateParams,\n ResponseFunctionCallOutputItem,\n ResponseInput,\n ResponseInputContent,\n ResponseStreamEvent,\n} from 'openai/resources/responses/responses'\nimport type {\n ContentPart,\n DefaultMessageMetadataByModality,\n Modality,\n ModelMessage,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\n\n/**\n * Shared implementation of the OpenAI Responses API. Holds the stream-event\n * accumulator + AG-UI lifecycle and calls the OpenAI SDK directly. Subclasses\n * (today: ai-openai) construct an OpenAI client with their provider-specific\n * `baseURL` / headers and pass it in.\n */\nexport abstract class OpenAIBaseResponsesTextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, unknown> = Record<string, unknown>,\n TInputModalities extends ReadonlyArray<Modality> = ReadonlyArray<Modality>,\n TMessageMetadata extends DefaultMessageMetadataByModality =\n DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n TMessageMetadata,\n TToolCapabilities\n> {\n override readonly kind = 'text' as const\n readonly name: string\n protected client: OpenAI\n\n constructor(model: TModel, name: string, client: OpenAI) {\n super({}, model)\n this.name = name\n this.client = client\n }\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n // Track tool call metadata by unique ID\n // Responses API streams tool calls with deltas — first chunk has ID/name,\n // subsequent chunks only have args.\n // We assign our own indices as we encounter unique tool call IDs.\n const toolCallMetadata = new Map<\n string,\n {\n index: number\n name: string\n started: boolean\n // Set once TOOL_CALL_END has been emitted (via args.done or the\n // output_item.done backfill) so the two paths don't double-emit.\n ended?: boolean\n // Set when args.done arrives before TOOL_CALL_START could fire\n // (output_item.added lacked a name). output_item.done picks these\n // up to emit the missing END. Allow explicit `undefined` so the\n // emission paths can re-clear the slot after handing it off.\n pendingArguments?: string\n }\n >()\n\n // AG-UI lifecycle tracking\n const aguiState = {\n runId: generateId(this.name),\n threadId: options.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n hasEmittedRunStarted: false,\n }\n\n try {\n // mapOptionsToRequest can throw on caller-side validation failures\n // (empty user content, unsupported parts, webSearchTool() rejection in\n // the OpenRouter override). Keep it inside the try so those failures\n // surface as RUN_ERROR events instead of iterator throws.\n const requestParams = this.mapOptionsToRequest(options)\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const response = await this.client.responses.create(\n {\n ...requestParams,\n stream: true,\n },\n extractRequestOptions(options.request),\n )\n\n yield* this.processStreamChunks(\n response,\n toolCallMetadata,\n options,\n aguiState,\n )\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n const rawEvent = toRunErrorRawEvent(error)\n\n // Emit RUN_STARTED if not yet emitted\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: options.model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n // Emit AG-UI RUN_ERROR. Conditional `code` spread keeps the wire\n // shape spec-compliant under `exactOptionalPropertyTypes`: AG-UI's\n // `RunErrorEvent.code` is `string?` (absent vs explicit `undefined`\n // matter), so we omit the key when there's no code.\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n code: errorPayload.code,\n // Forward the provider's structured error body when present (see\n // toRunErrorRawEvent); omitted otherwise.\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n code: errorPayload.code,\n },\n }\n\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n }\n }\n\n /**\n * Generate structured output using the provider's native JSON Schema response format.\n * Uses stream: false to get the complete response in one call.\n *\n * OpenAI-compatible Responses APIs have strict requirements for structured output:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for all objects\n *\n * The outputSchema is already JSON Schema (converted in the ai layer).\n * We apply provider-specific transformations for structured output compatibility.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapOptionsToRequest(chatOptions)\n\n // Apply provider-specific transformations for structured output compatibility\n const jsonSchema = this.makeStructuredOutputCompatible(\n outputSchema,\n outputSchema.required,\n )\n\n try {\n // Strip streaming-only fields a subclass override of mapOptionsToRequest\n // might have returned (parallel to chat-completions's structuredOutput\n // cleanup) — sending stream_options to a non-streaming call is a 4xx.\n const {\n stream: _stream,\n stream_options: _streamOptions,\n ...cleanParams\n } = requestParams as Record<string, unknown>\n void _stream\n void _streamOptions\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const response = await this.client.responses.create(\n {\n ...(cleanParams as Omit<ResponseCreateParams, 'stream'>),\n stream: false,\n // Configure structured output via text.format\n text: {\n format: {\n type: 'json_schema',\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n },\n extractRequestOptions(chatOptions.request),\n )\n\n // Extract text content from the response. `stream: false` narrows the\n // SDK return type to `Response`, but the explicit annotation makes\n // that contract local rather than relying on inference through the\n // overloaded `client.responses.create` signature.\n const rawText = this.extractTextFromResponse(response satisfies Response)\n\n // Fail loud on empty content rather than letting it cascade into a\n // confusing \"Failed to parse JSON. Content: \" error — the root cause\n // (the model returned no text content for the structured request) is\n // then visible in logs. Mirrors the chat-completions sibling.\n if (rawText.length === 0) {\n throw new Error(\n `${this.name}.structuredOutput: response contained no content`,\n )\n }\n\n // Parse the JSON response\n let parsed: unknown\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to parse structured output as JSON. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n\n // Apply the provider-specific post-parse shaping (default: null →\n // undefined to align with the original Zod schema's optional-field\n // semantics; subclasses with different conventions can override\n // `transformStructuredOutput`, mirroring the chat-completions base's\n // hook so OpenRouter and other providers that preserve nulls in\n // structured output can opt out without forking `structuredOutput`).\n const transformed = this.transformStructuredOutput(parsed)\n\n return {\n data: transformed,\n rawText,\n }\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Stream structured output via the Responses API: single request with\n * `text.format: json_schema` + `stream: true`. Consumes Responses-API\n * events (`response.output_text.delta`, `response.reasoning_text.delta`,\n * `response.reasoning_summary_text.delta`, `response.refusal.delta`,\n * `response.completed`, `response.failed`) and re-emits the standard AG-UI\n * lifecycle ending with `CUSTOM 'structured-output.complete'`.\n *\n * Tools are stripped (structured output is mutually exclusive with tool\n * calls in this path). Reasoning text is accumulated and surfaced both as\n * REASONING_* lifecycle events during the stream and on the terminal\n * CUSTOM event's `value.reasoning`.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n const { chatOptions, outputSchema } = options\n const requestParams = this.mapOptionsToRequest(chatOptions)\n\n const jsonSchema = this.makeStructuredOutputCompatible(\n outputSchema,\n outputSchema.required,\n )\n\n const timestamp = Date.now()\n const aguiState = {\n runId: generateId(this.name),\n threadId: chatOptions.threadId ?? generateId(this.name),\n messageId: generateId(this.name),\n timestamp,\n hasEmittedRunStarted: false,\n }\n\n let accumulatedContent = ''\n let accumulatedReasoning = ''\n let hasEmittedTextMessageStart = false\n let reasoningMessageId: string | undefined\n let stepId: string | undefined\n let hasClosedReasoning = false\n let model: string = chatOptions.model\n let usage: OpenAI.Responses.Response['usage'] | undefined\n\n const closeReasoning = function* (this: {\n name: string\n }): Generator<StreamChunk> {\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model,\n timestamp,\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model,\n timestamp,\n }\n if (stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model,\n timestamp,\n content: accumulatedReasoning,\n }\n }\n }\n }.bind(this)\n\n const openReasoning = function* (this: {\n name: string\n }): Generator<StreamChunk> {\n if (reasoningMessageId) return\n reasoningMessageId = generateId(this.name)\n stepId = generateId(this.name)\n yield {\n type: EventType.REASONING_START,\n messageId: reasoningMessageId,\n model,\n timestamp,\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning' as const,\n model,\n timestamp,\n }\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model,\n timestamp,\n stepType: 'thinking',\n }\n }.bind(this)\n\n try {\n const { tools: _tools, ...cleanParams } = requestParams\n void _tools\n\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n\n const stream = await this.client.responses.create(\n {\n ...cleanParams,\n stream: true,\n text: {\n format: {\n type: 'json_schema',\n name: 'structured_output',\n schema: jsonSchema,\n strict: true,\n },\n },\n },\n extractRequestOptions(chatOptions.request),\n )\n\n for await (const chunk of stream) {\n chatOptions.logger.provider(\n `provider=${this.name} type=${chunk.type}`,\n { provider: this.name, type: chunk.type },\n )\n\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model,\n timestamp,\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n if (\n chunk.type === 'response.created' ||\n chunk.type === 'response.in_progress'\n ) {\n const responseModel = (chunk as { response?: { model?: string } })\n .response?.model\n if (responseModel) model = responseModel\n continue\n }\n\n if (chunk.type === 'response.refusal.delta') {\n const delta =\n typeof (chunk as { delta?: unknown }).delta === 'string'\n ? (chunk as { delta: string }).delta\n : ''\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message: `Model refused: ${delta}`,\n code: 'refusal',\n error: { message: `Model refused: ${delta}`, code: 'refusal' },\n }\n return\n }\n\n if (\n chunk.type === 'response.reasoning_text.delta' ||\n chunk.type === 'response.reasoning_summary_text.delta'\n ) {\n const raw = (chunk as { delta?: unknown }).delta\n const reasoningDelta = Array.isArray(raw)\n ? raw.join('')\n : typeof raw === 'string'\n ? raw\n : ''\n if (!reasoningDelta) continue\n yield* openReasoning()\n // openReasoning() guarantees reasoningMessageId is set on first call;\n // TS can't see through the generator side-effect.\n if (!reasoningMessageId) continue\n accumulatedReasoning += reasoningDelta\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta: reasoningDelta,\n model,\n timestamp,\n }\n continue\n }\n\n if (chunk.type === 'response.output_text.delta') {\n const raw = (chunk as { delta?: unknown }).delta\n const textDelta = Array.isArray(raw)\n ? raw.join('')\n : typeof raw === 'string'\n ? raw\n : ''\n if (!textDelta) continue\n\n yield* closeReasoning()\n\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model,\n timestamp,\n role: 'assistant',\n }\n }\n accumulatedContent += textDelta\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model,\n timestamp,\n delta: textDelta,\n content: accumulatedContent,\n }\n continue\n }\n\n if (chunk.type === 'response.completed') {\n const response = chunk.response\n if (response.usage) usage = response.usage\n if (response.model) model = response.model\n continue\n }\n\n if (chunk.type === 'response.failed') {\n const response = (\n chunk as {\n response?: { error?: { message?: string; code?: string } }\n }\n ).response\n const message =\n response?.error?.message || 'Responses API stream failed'\n const code = response?.error?.code\n // Conditional `code` spread keeps the wire shape spec-compliant\n // under `exactOptionalPropertyTypes` (see chatStream catch).\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message,\n ...(code !== undefined && { code }),\n error: { message, ...(code !== undefined && { code }) },\n }\n return\n }\n }\n\n yield* closeReasoning()\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model,\n timestamp,\n }\n }\n\n if (accumulatedContent.length === 0) {\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n error: {\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n },\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedContent)\n } catch {\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message: `Failed to parse structured output as JSON. Content: ${accumulatedContent.slice(0, 200)}${accumulatedContent.length > 200 ? '...' : ''}`,\n code: 'parse-error',\n error: {\n message: 'Failed to parse structured output as JSON',\n code: 'parse-error',\n },\n }\n return\n }\n\n const transformed = transformNullsToUndefined(parsed)\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: transformed,\n raw: accumulatedContent,\n ...(accumulatedReasoning ? { reasoning: accumulatedReasoning } : {}),\n },\n model,\n timestamp,\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model,\n timestamp,\n finishReason: 'stop',\n ...(usage && {\n usage: buildResponsesUsage(usage),\n }),\n }\n } catch (error: unknown) {\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model,\n timestamp,\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n const isAbort = this.isAbortError(error)\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes` (see chatStream catch).\n const resolvedCode = isAbort ? 'aborted' : errorPayload.code\n const rawEvent = isAbort ? undefined : toRunErrorRawEvent(error)\n yield {\n type: EventType.RUN_ERROR,\n runId: aguiState.runId,\n model,\n timestamp,\n message: errorPayload.message,\n ...(resolvedCode !== undefined && { code: resolvedCode }),\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n ...(resolvedCode !== undefined && { code: resolvedCode }),\n },\n }\n\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n }\n }\n\n /**\n * Cross-SDK abort detection for `structuredOutputStream`. Mirrors the\n * Chat Completions base; subclasses with proprietary error types override.\n */\n protected isAbortError(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false\n const e = error as { name?: unknown; code?: unknown }\n return (\n e.name === 'APIUserAbortError' ||\n e.name === 'AbortError' ||\n e.code === 'ERR_CANCELED'\n )\n }\n\n /**\n * Applies provider-specific transformations for structured output compatibility.\n * Override this in subclasses to handle provider-specific quirks.\n */\n protected makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n ): Record<string, any> {\n return makeStructuredOutputCompatible(schema, originalRequired)\n }\n\n /**\n * Final shaping pass applied to parsed structured-output JSON before it is\n * returned to the caller. Default converts `null` values to `undefined` so\n * the result aligns with the original Zod schema's optional-field\n * semantics. Subclasses with different conventions (OpenRouter historically\n * preserves nulls) can override — mirrors the chat-completions base's hook\n * so a subclass that opts out of null-stripping doesn't have to fork the\n * whole `structuredOutput` method.\n */\n protected transformStructuredOutput(parsed: unknown): unknown {\n return transformNullsToUndefined(parsed)\n }\n\n /**\n * Extract text content from a non-streaming Responses API response.\n * Override this in subclasses for provider-specific response shapes.\n */\n protected extractTextFromResponse(response: Response): string {\n let textContent = ''\n let refusal: string | undefined\n let sawMessageItem = false\n const observedItemTypes = new Set<string>()\n\n for (const item of response.output) {\n observedItemTypes.add(item.type)\n if (item.type === 'message') {\n sawMessageItem = true\n for (const part of item.content) {\n // Cast off the discriminated union before the type discrimination\n // so future SDK variants (e.g. `output_audio`, `output_image`) hit\n // the explicit error path rather than being misreported as refusals\n // when they get added to the union. Mirrors the streaming side's\n // handleContentPart.\n const partType = (part as { type: string }).type\n if (partType === 'output_text') {\n textContent += (part as { text?: string }).text ?? ''\n } else if (partType === 'refusal') {\n const refusalText = (part as { refusal?: string }).refusal\n refusal = refusalText || refusal || 'Refused without explanation'\n } else {\n throw new Error(\n `${this.name}.extractTextFromResponse: unsupported message content part type \"${partType}\"`,\n )\n }\n }\n }\n }\n\n // Surface refusals as an explicit error so callers don't see a generic\n // \"Failed to parse structured output as JSON. Content: \" when the model\n // refused for safety / content-policy reasons.\n if (!textContent && refusal !== undefined) {\n const err = new Error(`Model refused to respond: ${refusal}`)\n ;(err as Error & { code?: string }).code = 'refusal'\n throw err\n }\n\n // Response had items but none carried message text (e.g. only\n // function_call or reasoning items). Surface that explicitly so a\n // downstream structured-output caller doesn't see a misleading\n // \"Failed to parse JSON. Content: \" from an empty string.\n if (!textContent && response.output.length > 0 && !sawMessageItem) {\n throw new Error(\n `${this.name}.extractTextFromResponse: response.output contained items of type(s) [${[...observedItemTypes].sort().join(', ')}] but no message text — the model returned a non-text response`,\n )\n }\n\n return textContent\n }\n\n /**\n * Processes streamed chunks from the Responses API and yields AG-UI events.\n * Override this in subclasses to handle provider-specific stream behavior.\n *\n * Handles the following event types:\n * - response.created / response.incomplete / response.failed\n * - response.output_text.delta\n * - response.reasoning_text.delta\n * - response.reasoning_summary_text.delta\n * - response.content_part.added / response.content_part.done\n * - response.output_item.added\n * - response.function_call_arguments.delta / response.function_call_arguments.done\n * - response.completed\n * - error\n */\n protected async *processStreamChunks(\n stream: AsyncIterable<ResponseStreamEvent>,\n toolCallMetadata: Map<\n string,\n {\n index: number\n name: string\n started: boolean\n ended?: boolean\n pendingArguments?: string | undefined\n }\n >,\n options: TextOptions<TProviderOptions>,\n aguiState: {\n runId: string\n threadId: string\n messageId: string\n hasEmittedRunStarted: boolean\n },\n ): AsyncIterable<StreamChunk> {\n let accumulatedContent = ''\n let accumulatedReasoning = ''\n\n // Track if we've been streaming deltas to avoid duplicating content from done events\n let hasStreamedContentDeltas = false\n let hasStreamedReasoningDeltas = false\n\n // Preserve response metadata across events\n let model: string = options.model\n\n // AG-UI lifecycle tracking\n let stepId: string | null = null\n let hasEmittedTextMessageStart = false\n let hasEmittedStepStarted = false\n // Track whether we've emitted a terminal RUN_FINISHED so the\n // end-of-stream fallback below knows to synthesise one when the upstream\n // cuts off without a response.completed event.\n let runFinishedEmitted = false\n\n try {\n for await (const chunk of stream) {\n options.logger.provider(`provider=${this.name} type=${chunk.type}`, {\n provider: this.name,\n type: chunk.type,\n })\n\n // Emit RUN_STARTED on first chunk\n if (!aguiState.hasEmittedRunStarted) {\n aguiState.hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: model || options.model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n const handleContentPart = (contentPart: {\n type: string\n text?: string\n refusal?: string\n }): StreamChunk => {\n if (contentPart.type === 'output_text') {\n accumulatedContent += contentPart.text || ''\n return {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: contentPart.text || '',\n content: accumulatedContent,\n }\n }\n\n if (contentPart.type === 'reasoning_text') {\n accumulatedReasoning += contentPart.text || ''\n // Cache the fallback stepId rather than generating a fresh one\n // on every call — otherwise multiple reasoning chunks arriving\n // before STEP_STARTED was emitted (e.g. via response.content_part.done\n // alone) would each get a different stepId and break correlation.\n if (!stepId) {\n stepId = generateId(this.name)\n }\n return {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: contentPart.text || '',\n content: accumulatedReasoning,\n }\n }\n // Either a real refusal or an unknown content_part type. Surface\n // the part type in the error so unknown parts are debuggable\n // instead of being misreported as \"Unknown refusal\".\n const isRefusal = contentPart.type === 'refusal'\n const message = isRefusal\n ? contentPart.refusal || 'Refused without explanation'\n : `Unsupported response content_part type: ${contentPart.type}`\n const code = isRefusal ? 'refusal' : contentPart.type\n return {\n type: EventType.RUN_ERROR,\n model: model || options.model,\n timestamp: Date.now(),\n message,\n code,\n error: { message, code },\n }\n }\n\n // Capture model metadata from any of these events (created starts\n // the run; failed/incomplete signal terminal failure).\n if (\n chunk.type === 'response.created' ||\n chunk.type === 'response.incomplete' ||\n chunk.type === 'response.failed'\n ) {\n model = chunk.response.model\n }\n\n // response.created marks the start of a fresh run — safe to reset\n // the per-run accumulators here.\n if (chunk.type === 'response.created') {\n hasStreamedContentDeltas = false\n hasStreamedReasoningDeltas = false\n hasEmittedTextMessageStart = false\n hasEmittedStepStarted = false\n accumulatedContent = ''\n accumulatedReasoning = ''\n }\n\n // response.failed and response.incomplete are TERMINAL events for\n // the current response. Close any open AG-UI message lifecycle FIRST\n // so consumers tracking start/end pairs don't see an unbalanced\n // TEXT_MESSAGE_START. Then surface the error and mark the run as\n // finished so the post-loop synthetic terminal block doesn't emit\n // a duplicate RUN_FINISHED on top of RUN_ERROR.\n if (\n chunk.type === 'response.failed' ||\n chunk.type === 'response.incomplete'\n ) {\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: chunk.response.model,\n timestamp: Date.now(),\n }\n hasEmittedTextMessageStart = false\n }\n // Coalesce error + incomplete_details into a single RUN_ERROR\n // payload — emitting two distinct events for one terminal upstream\n // event would force consumers to handle a non-existent ordering.\n const errorMessage =\n chunk.response.error?.message ||\n chunk.response.incomplete_details?.reason ||\n (chunk.type === 'response.failed'\n ? 'Response failed'\n : 'Response ended incomplete')\n const errorCode =\n chunk.response.error?.code ??\n (chunk.response.incomplete_details ? 'incomplete' : undefined) ??\n undefined\n // Always emit RUN_ERROR for terminal failure events, even when the\n // upstream omitted both `error` and `incomplete_details`. Skipping\n // emission on a `response.incomplete` with no detail would let the\n // post-loop synthetic block silently coerce the run to a clean\n // `RUN_FINISHED { finishReason: 'stop' }` — masking the failure.\n yield {\n type: EventType.RUN_ERROR,\n model: chunk.response.model,\n timestamp: Date.now(),\n message: errorMessage,\n ...(errorCode !== undefined && { code: errorCode }),\n error: {\n message: errorMessage,\n ...(errorCode !== undefined && { code: errorCode }),\n },\n }\n // RUN_ERROR is the terminal event for this run; stop processing\n // any further chunks the iterator might still deliver.\n runFinishedEmitted = true\n return\n }\n\n // Handle output text deltas (token-by-token streaming)\n // response.output_text.delta provides incremental text updates\n if (chunk.type === 'response.output_text.delta' && chunk.delta) {\n // Delta can be an array of strings or a single string\n const textDelta = Array.isArray(chunk.delta)\n ? chunk.delta.join('')\n : typeof chunk.delta === 'string'\n ? chunk.delta\n : ''\n\n if (textDelta) {\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n }\n\n accumulatedContent += textDelta\n hasStreamedContentDeltas = true\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: textDelta,\n content: accumulatedContent,\n }\n }\n }\n\n // Handle reasoning deltas (token-by-token thinking/reasoning streaming)\n // response.reasoning_text.delta provides incremental reasoning updates\n if (chunk.type === 'response.reasoning_text.delta' && chunk.delta) {\n // Delta can be an array of strings or a single string\n const reasoningDelta = Array.isArray(chunk.delta)\n ? chunk.delta.join('')\n : typeof chunk.delta === 'string'\n ? chunk.delta\n : ''\n\n if (reasoningDelta) {\n // Emit STEP_STARTED on first reasoning content\n if (!hasEmittedStepStarted) {\n hasEmittedStepStarted = true\n stepId = generateId(this.name)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n\n accumulatedReasoning += reasoningDelta\n hasStreamedReasoningDeltas = true\n const fallbackStepId = stepId || generateId(this.name)\n yield {\n type: EventType.STEP_FINISHED,\n stepName: fallbackStepId,\n stepId: fallbackStepId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: reasoningDelta,\n content: accumulatedReasoning,\n }\n }\n }\n\n // Handle reasoning summary deltas (when using reasoning.summary option)\n // response.reasoning_summary_text.delta provides incremental summary updates\n if (\n chunk.type === 'response.reasoning_summary_text.delta' &&\n chunk.delta\n ) {\n const summaryDelta =\n typeof chunk.delta === 'string' ? chunk.delta : ''\n\n if (summaryDelta) {\n // Emit STEP_STARTED on first reasoning content\n if (!hasEmittedStepStarted) {\n hasEmittedStepStarted = true\n stepId = generateId(this.name)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n\n accumulatedReasoning += summaryDelta\n hasStreamedReasoningDeltas = true\n const fallbackStepId = stepId || generateId(this.name)\n yield {\n type: EventType.STEP_FINISHED,\n stepName: fallbackStepId,\n stepId: fallbackStepId,\n model: model || options.model,\n timestamp: Date.now(),\n delta: summaryDelta,\n content: accumulatedReasoning,\n }\n }\n }\n\n // handle content_part added events for text, reasoning and refusals\n if (chunk.type === 'response.content_part.added') {\n const contentPart = chunk.part\n // Emit TEXT_MESSAGE_START if this is text content\n if (\n contentPart.type === 'output_text' &&\n !hasEmittedTextMessageStart\n ) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n }\n // Emit STEP_STARTED if this is reasoning content\n if (contentPart.type === 'reasoning_text' && !hasEmittedStepStarted) {\n hasEmittedStepStarted = true\n stepId = generateId(this.name)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n // Mark whichever stream we just emitted into so a subsequent\n // `content_part.done` doesn't duplicate the same text. Without\n // this flag, an `added` event carrying the full text followed by\n // a matching `done` event would emit TEXT_MESSAGE_CONTENT twice.\n if (contentPart.type === 'output_text') {\n hasStreamedContentDeltas = true\n } else if (contentPart.type === 'reasoning_text') {\n hasStreamedReasoningDeltas = true\n }\n const partChunk = handleContentPart(contentPart)\n yield partChunk\n // handleContentPart returns RUN_ERROR for refusals / unknown\n // content_part types — those are terminal events. Don't keep\n // processing more chunks (and don't let the post-loop synthetic\n // block emit a second terminal event).\n if (partChunk.type === 'RUN_ERROR') {\n runFinishedEmitted = true\n return\n }\n }\n\n if (chunk.type === 'response.content_part.done') {\n const contentPart = chunk.part\n\n // Skip emitting chunks for content parts that we've already streamed via deltas\n // The done event is just a completion marker, not new content\n if (contentPart.type === 'output_text' && hasStreamedContentDeltas) {\n // Content already accumulated from deltas, skip\n continue\n }\n if (\n contentPart.type === 'reasoning_text' &&\n hasStreamedReasoningDeltas\n ) {\n // Reasoning already accumulated from deltas, skip\n continue\n }\n\n // Upstreams that emit `content_part.done` without any preceding\n // deltas (or `content_part.added`) still need a START event before\n // CONTENT — otherwise consumers tracking start/end pairs see content\n // without a start and never see an end. Emit the lifecycle opener\n // for whichever stream this content_part belongs to before yielding\n // the CONTENT chunk; the post-loop block emits the matching END.\n if (\n contentPart.type === 'output_text' &&\n !hasEmittedTextMessageStart\n ) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n } else if (\n contentPart.type === 'reasoning_text' &&\n !hasEmittedStepStarted\n ) {\n hasEmittedStepStarted = true\n stepId = generateId(this.name)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model: model || options.model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n\n // Only emit if we haven't been streaming deltas (e.g., for non-streaming responses)\n const doneChunk = handleContentPart(contentPart)\n yield doneChunk\n if (doneChunk.type === 'RUN_ERROR') {\n runFinishedEmitted = true\n return\n }\n }\n\n // handle output_item.added to capture function call metadata (name)\n if (chunk.type === 'response.output_item.added') {\n const item = chunk.item\n if (item.type === 'function_call' && item.id) {\n // Track the item as soon as we see it so subsequent arg deltas\n // aren't logged as orphans, but only emit TOOL_CALL_START when\n // both id AND name are populated. Emitting START with an empty\n // name would propagate into TOOL_CALL_END (which reads the same\n // metadata) and route the tool call to whatever name happens to\n // match `''` downstream — a silent misroute.\n let metadata = toolCallMetadata.get(item.id)\n if (!metadata) {\n metadata = {\n index: chunk.output_index,\n name: item.name || '',\n started: false,\n }\n toolCallMetadata.set(item.id, metadata)\n } else if (!metadata.name && item.name) {\n // A later output_item.added for the same id finally carries\n // the name. Update so the gated emission below can fire.\n metadata.name = item.name\n }\n if (!metadata.started && metadata.name) {\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: item.id,\n toolCallName: metadata.name,\n toolName: metadata.name,\n model: model || options.model,\n timestamp: Date.now(),\n index: chunk.output_index,\n }\n metadata.started = true\n }\n }\n }\n\n // Handle function call arguments delta (streaming). Drop the\n // previously-emitted `args` field — it had inverted polarity\n // (populated only when metadata was MISSING, i.e. when the\n // matching TOOL_CALL_START hadn't fired) and the chat-completions\n // adapter never emitted it, so it leaked partial deltas as\n // pseudo-args only on the orphan path. Consumers should accumulate\n // `delta` themselves.\n //\n // Guard with `metadata?.started`: the matching TOOL_CALL_START fires\n // from `output_item.added`, and emitting TOOL_CALL_ARGS before that\n // would violate the AG-UI lifecycle (ARGS without START). The .done\n // handler below applies the same guard.\n if (\n chunk.type === 'response.function_call_arguments.delta' &&\n chunk.delta\n ) {\n const metadata = toolCallMetadata.get(chunk.item_id)\n if (!metadata?.started) {\n options.logger.errors(\n `${this.name}.processStreamChunks orphan function_call_arguments.delta`,\n {\n source: `${this.name}.processStreamChunks`,\n toolCallId: chunk.item_id,\n rawDelta: chunk.delta,\n },\n )\n continue\n }\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: chunk.item_id,\n model: model || options.model,\n timestamp: Date.now(),\n delta: chunk.delta,\n }\n }\n\n if (chunk.type === 'response.function_call_arguments.done') {\n const { item_id } = chunk\n\n // Get the function name from metadata (captured in output_item.added)\n const metadata = toolCallMetadata.get(item_id)\n // If the matching START was never emitted (the upstream sent an\n // `output_item.added` without a name and no later event has filled\n // it in yet), defer END until `output_item.done` or\n // `response.completed` can backfill the name. We stash the raw\n // arguments so the late emission has them. Emitting END without\n // START would produce an unbalanced AG-UI lifecycle event\n // downstream consumers can't pair.\n if (!metadata?.started) {\n if (metadata) {\n metadata.pendingArguments = chunk.arguments\n }\n options.logger.errors(\n `${this.name}.processStreamChunks deferring function_call_arguments.done — TOOL_CALL_START not yet emitted (waiting for name)`,\n {\n source: `${this.name}.processStreamChunks`,\n toolCallId: item_id,\n rawArguments: chunk.arguments,\n },\n )\n continue\n }\n // The output_item.done backstop may have already emitted END (when\n // it arrived before args.done with a populated item.arguments).\n // Skip so we never produce a duplicate close for the same id.\n if (metadata.ended) continue\n const name = metadata.name || ''\n metadata.ended = true\n\n // Parse arguments. Surface parse failures via the logger so a\n // model emitting malformed JSON is debuggable instead of silently\n // invoking the tool with {}.\n let parsedInput: unknown = {}\n if (chunk.arguments) {\n try {\n const parsed = JSON.parse(chunk.arguments)\n parsedInput = parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${name} (${item_id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: item_id,\n toolName: name,\n rawArguments: chunk.arguments,\n },\n )\n parsedInput = {}\n }\n }\n\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: item_id,\n toolCallName: name,\n toolName: name,\n model: model || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n }\n\n // `output_item.done` is the last point at which a function_call's\n // name is guaranteed to be on the wire — it carries the fully-formed\n // ResponseFunctionToolCall. Use it as a backstop to recover any\n // tool call whose name was missing from `output_item.added` (and\n // whose START + END therefore never fired).\n if (chunk.type === 'response.output_item.done') {\n const item = chunk.item\n if (item.type === 'function_call' && item.id) {\n const metadata = toolCallMetadata.get(item.id) ?? {\n index: chunk.output_index,\n name: item.name || '',\n started: false,\n }\n if (!toolCallMetadata.has(item.id)) {\n toolCallMetadata.set(item.id, metadata)\n } else if (!metadata.name && item.name) {\n metadata.name = item.name\n }\n // Emit gated START if we now have a name and never started.\n if (!metadata.started && metadata.name) {\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: item.id,\n toolCallName: metadata.name,\n toolName: metadata.name,\n model: model || options.model,\n timestamp: Date.now(),\n index: metadata.index,\n }\n metadata.started = true\n }\n // Emit END if we have args (either from a previously-deferred\n // args.done OR from item.arguments) and haven't already ended.\n const rawArgs =\n typeof item.arguments === 'string' && item.arguments.length > 0\n ? item.arguments\n : metadata.pendingArguments\n if (metadata.started && !metadata.ended && rawArgs !== undefined) {\n const name = metadata.name || ''\n let parsedInput: unknown = {}\n if (rawArgs) {\n try {\n const parsed = JSON.parse(rawArgs)\n parsedInput =\n parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed (output_item.done backfill)`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${name} (${item.id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: item.id,\n toolName: name,\n rawArguments: rawArgs,\n },\n )\n parsedInput = {}\n }\n }\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: item.id,\n toolCallName: name,\n toolName: name,\n model: model || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n metadata.ended = true\n metadata.pendingArguments = undefined\n }\n }\n }\n\n if (chunk.type === 'response.completed') {\n // Final backstop for function_call lifecycle: if a function_call\n // appears in `response.output[]` but was never matched by an\n // output_item.added/done with a name, recover the missing START\n // (and END if args were pending). Without this, a tool call could\n // be silently dropped from the AG-UI stream while `hasFunctionCalls`\n // below still routes the run's finishReason to 'tool_calls' —\n // leaving consumers waiting for tool results they never saw start.\n for (const item of chunk.response.output) {\n if (item.type !== 'function_call' || !item.id) continue\n const metadata = toolCallMetadata.get(item.id) ?? {\n index: 0,\n name: item.name || '',\n started: false,\n }\n if (!toolCallMetadata.has(item.id)) {\n toolCallMetadata.set(item.id, metadata)\n } else if (!metadata.name && item.name) {\n metadata.name = item.name\n }\n if (!metadata.started && metadata.name) {\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: item.id,\n toolCallName: metadata.name,\n toolName: metadata.name,\n model: model || options.model,\n timestamp: Date.now(),\n index: metadata.index,\n }\n metadata.started = true\n }\n const rawArgs =\n typeof item.arguments === 'string' && item.arguments.length > 0\n ? item.arguments\n : metadata.pendingArguments\n if (metadata.started && !metadata.ended) {\n const name = metadata.name || ''\n let parsedInput: unknown = {}\n if (rawArgs) {\n try {\n const parsed = JSON.parse(rawArgs)\n parsedInput =\n parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n options.logger.errors(\n `${this.name}.processStreamChunks tool-args JSON parse failed (response.completed backfill)`,\n {\n error: toRunErrorPayload(\n parseError,\n `tool ${name} (${item.id}) returned malformed JSON arguments`,\n ),\n source: `${this.name}.processStreamChunks`,\n toolCallId: item.id,\n toolName: name,\n rawArguments: rawArgs,\n },\n )\n parsedInput = {}\n }\n }\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: item.id,\n toolCallName: name,\n toolName: name,\n model: model || options.model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n metadata.ended = true\n metadata.pendingArguments = undefined\n }\n }\n\n // Emit TEXT_MESSAGE_END if we had text content\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n }\n hasEmittedTextMessageStart = false\n }\n\n // Determine finish reason. Function-call output → tool_calls.\n // Otherwise surface incomplete_details.reason when present so\n // callers can distinguish length-limit / content-filter cutoffs\n // from a clean stop, mirroring the chat-completions adapter.\n // The Responses API's incomplete_details.reason ('max_output_tokens'\n // | 'content_filter') maps to the AG-UI finishReason vocabulary:\n // max_output_tokens → 'length', content_filter → 'content_filter'.\n const hasFunctionCalls = chunk.response.output.some(\n (item: unknown) =>\n (item as { type: string }).type === 'function_call',\n )\n const incompleteReason = chunk.response.incomplete_details?.reason\n const finishReason:\n | 'tool_calls'\n | 'length'\n | 'content_filter'\n | 'stop' = hasFunctionCalls\n ? 'tool_calls'\n : incompleteReason === 'max_output_tokens'\n ? 'length'\n : incompleteReason === 'content_filter'\n ? 'content_filter'\n : 'stop'\n\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: model || options.model,\n timestamp: Date.now(),\n // Omit usage entirely when the provider reported none rather than\n // emitting fabricated zeros (also satisfies exactOptionalPropertyTypes).\n ...(chunk.response.usage && {\n usage: buildResponsesUsage(chunk.response.usage),\n }),\n finishReason,\n }\n runFinishedEmitted = true\n }\n\n if (chunk.type === 'error') {\n // Conditional `code` spread keeps the wire shape spec-compliant\n // under `exactOptionalPropertyTypes` (see chatStream catch).\n const code = chunk.code ?? undefined\n yield {\n type: EventType.RUN_ERROR,\n model: model || options.model,\n timestamp: Date.now(),\n message: chunk.message,\n ...(code !== undefined && { code }),\n error: {\n message: chunk.message,\n ...(code !== undefined && { code }),\n },\n }\n // RUN_ERROR is terminal — don't let the synthetic RUN_FINISHED\n // block fire after a top-level stream error event, and stop\n // processing further chunks so no in-flight lifecycle events\n // (TEXT_MESSAGE_CONTENT, TOOL_CALL_*) leak past the terminal\n // error. Mirrors the `response.failed` / `response.incomplete`\n // branches above which return after their RUN_ERROR emission.\n runFinishedEmitted = true\n return\n }\n }\n\n // Synthetic terminal RUN_FINISHED if the stream ended without a\n // response.completed event (e.g. truncated upstream connection). This\n // mirrors the chat-completions adapter's behavior so consumers always\n // see a terminal event for every started run.\n if (!runFinishedEmitted && aguiState.hasEmittedRunStarted) {\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId: aguiState.messageId,\n model: model || options.model,\n timestamp: Date.now(),\n }\n }\n // Omit `usage` entirely (vs `usage: undefined`) — the synthetic\n // RUN_FINISHED for truncated streams has no usage data, and AG-UI's\n // `RunFinishedEvent.usage` is optional without `| undefined` under\n // `exactOptionalPropertyTypes`.\n yield {\n type: EventType.RUN_FINISHED,\n runId: aguiState.runId,\n threadId: aguiState.threadId,\n model: model || options.model,\n timestamp: Date.now(),\n finishReason: toolCallMetadata.size > 0 ? 'tool_calls' : 'stop',\n }\n }\n } catch (error: unknown) {\n // Narrow before logging: raw SDK errors can carry request metadata\n // (including auth headers) which we must never surface to user loggers.\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.processStreamChunks failed`,\n )\n const rawEvent = toRunErrorRawEvent(error)\n options.logger.errors(`${this.name}.processStreamChunks fatal`, {\n error: errorPayload,\n source: `${this.name}.processStreamChunks`,\n })\n // Emit AG-UI RUN_ERROR with conditional `code` spread (see chatStream\n // catch for the rationale). `rawEvent` carries the provider's structured\n // error body when present.\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Maps common TextOptions to Responses API request format.\n * Override this in subclasses to add provider-specific options.\n */\n protected mapOptionsToRequest(\n options: TextOptions<TProviderOptions>,\n ): Omit<ResponseCreateParams, 'stream'> {\n const input = this.convertMessagesToInput(options.messages)\n\n const tools = options.tools\n ? convertToolsToResponsesFormat(\n options.tools,\n this.makeStructuredOutputCompatible.bind(this),\n )\n : undefined\n\n const modelOptions = options.modelOptions\n\n // Native combined mode (issue #605): when the engine threads\n // `outputSchema` through TextOptions, the adapter declared\n // `supportsCombinedToolsAndSchema` and the schema is already JSON Schema\n // (pre-converted at the activity boundary). Wire it into `text.format`\n // alongside any `tools` — the Responses API supports both together and\n // emits the schema-constrained text on the natural final turn.\n const combinedSchema = options.outputSchema as\n | Record<string, unknown>\n | undefined\n const textFormat = combinedSchema\n ? {\n text: {\n format: {\n type: 'json_schema' as const,\n name: 'structured_output',\n schema: this.makeStructuredOutputCompatible(\n combinedSchema,\n Array.isArray(combinedSchema.required)\n ? (combinedSchema.required as Array<string>)\n : undefined,\n ),\n strict: true,\n },\n },\n }\n : undefined\n\n // `modelOptions` is the sole sampling surface: `temperature`, `top_p`, and\n // `max_output_tokens` live there (typed via OpenAISamplingOptions) and are\n // spread first. Engine-managed fields (`model`, `metadata`, `instructions`,\n // `input`, `tools`, `textFormat`) are layered on top afterward so they\n // always win over any same-named key a caller happened to put in\n // `modelOptions`.\n return {\n ...modelOptions,\n model: options.model,\n ...(options.metadata !== undefined && { metadata: options.metadata }),\n ...(() => {\n const prompts = normalizeSystemPrompts(options.systemPrompts)\n if (prompts.length === 0) return {}\n return { instructions: prompts.map((p) => p.content).join('\\n') }\n })(),\n input,\n // Conditional spread: `tools: undefined` would clobber any\n // modelOptions.tools the caller set above.\n ...(tools && tools.length > 0 && { tools }),\n ...(textFormat ?? {}),\n }\n }\n\n /**\n * The OpenAI Responses API supports `tools` and `text.format: json_schema`\n * together in a single streaming request (per issue #605). Subclasses\n * that route to providers without this capability should override.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return true\n }\n\n /**\n * Converts ModelMessage[] to Responses API ResponseInput format.\n * Override this in subclasses for provider-specific message format quirks.\n *\n * Key differences from Chat Completions:\n * - Tool results use `function_call_output` type (not `tool` role)\n * - Assistant tool calls are `function_call` objects (not nested in `tool_calls`)\n * - User content uses `input_text`, `input_image`, `input_file` types\n * - System prompts go in `instructions`, not as messages\n */\n protected convertMessagesToInput(\n messages: Array<ModelMessage>,\n ): ResponseInput {\n const result: ResponseInput = []\n\n for (const message of messages) {\n // Handle tool messages - convert to FunctionToolCallOutput\n if (message.role === 'tool') {\n const toolContent = message.content\n const output: string | Array<ResponseFunctionCallOutputItem> =\n Array.isArray(toolContent)\n ? toolContent.map((part) => this.convertContentPartToInput(part))\n : typeof toolContent === 'string'\n ? toolContent\n : JSON.stringify(toolContent)\n result.push({\n type: 'function_call_output',\n call_id: message.toolCallId || '',\n output,\n })\n continue\n }\n\n // Handle assistant messages\n if (message.role === 'assistant') {\n // If the assistant message has tool calls, add them as FunctionToolCall objects\n // Responses API expects arguments as a string (JSON string)\n if (message.toolCalls && message.toolCalls.length > 0) {\n for (const toolCall of message.toolCalls) {\n // Keep arguments as string for Responses API\n const argumentsString =\n typeof toolCall.function.arguments === 'string'\n ? toolCall.function.arguments\n : JSON.stringify(toolCall.function.arguments)\n\n result.push({\n type: 'function_call',\n call_id: toolCall.id,\n name: toolCall.function.name,\n arguments: argumentsString,\n })\n }\n }\n\n // Add the assistant's text message if there is content\n if (message.content) {\n const contentStr = this.extractTextContent(message.content)\n if (contentStr) {\n result.push({\n type: 'message',\n role: 'assistant',\n content: contentStr,\n })\n }\n }\n\n continue\n }\n\n // Handle user messages (default case) — support multimodal content\n const contentParts = this.normalizeContent(message.content)\n const inputContent: Array<ResponseInputContent> = []\n\n for (const part of contentParts) {\n inputContent.push(this.convertContentPartToInput(part))\n }\n\n if (inputContent.length === 0) {\n // Fail loud rather than silently sending an empty user message —\n // mirrors the chat-completions adapter, where a paid-but-empty\n // request would mask the real intent (caller passed `null` content\n // or a normalize step dropped everything).\n throw new Error(\n `User message for ${this.name} has no content parts. ` +\n `Empty user messages would produce a paid request with no input; ` +\n `provide at least one text/image/audio part or omit the message.`,\n )\n }\n\n result.push({\n type: 'message',\n role: 'user',\n content: inputContent,\n })\n }\n\n return result\n }\n\n /**\n * Converts a ContentPart to Responses API input content item.\n * Handles text, image, and audio content parts.\n * Override this in subclasses for additional content types or provider-specific metadata.\n */\n protected convertContentPartToInput(part: ContentPart): ResponseInputContent {\n switch (part.type) {\n case 'text':\n return {\n type: 'input_text',\n text: part.content,\n }\n case 'image': {\n const imageMetadata = part.metadata as\n | { detail?: 'auto' | 'low' | 'high' }\n | undefined\n if (part.source.type === 'url') {\n return {\n type: 'input_image',\n image_url: part.source.value,\n detail: imageMetadata?.detail || 'auto',\n }\n }\n // For base64 data, construct a data URI using the mimeType from\n // source. Default to a generic octet-stream MIME if the source\n // didn't supply one — letting `undefined` interpolate would produce\n // an invalid URI like \"data:undefined;base64,...\".\n const imageValue = part.source.value\n const imageMime = part.source.mimeType || 'application/octet-stream'\n const imageUrl = imageValue.startsWith('data:')\n ? imageValue\n : `data:${imageMime};base64,${imageValue}`\n return {\n type: 'input_image',\n image_url: imageUrl,\n detail: imageMetadata?.detail || 'auto',\n }\n }\n case 'audio': {\n if (part.source.type === 'url') {\n return {\n type: 'input_file',\n file_url: part.source.value,\n }\n }\n // Wrap raw base64 in a data URL — `input_file` rejects bare base64\n // payloads (matches the image branch above which already does this).\n // Default the MIME if missing so we never interpolate `undefined`.\n const audioValue = part.source.value\n const audioMime = part.source.mimeType || 'application/octet-stream'\n const audioFileData = audioValue.startsWith('data:')\n ? audioValue\n : `data:${audioMime};base64,${audioValue}`\n return {\n type: 'input_file',\n file_data: audioFileData,\n }\n }\n\n case 'video':\n case 'document':\n default:\n // OpenAI Responses API doesn't accept native video/document parts on\n // this path — surface as explicit unsupported error so callers see\n // the same message regardless of which content type leaked through.\n throw new Error(`Unsupported content part type: ${part.type}`)\n }\n }\n\n /**\n * Normalizes message content to an array of ContentPart.\n * Handles backward compatibility with string content.\n */\n protected normalizeContent(\n content: string | null | Array<ContentPart>,\n ): Array<ContentPart> {\n if (content === null) {\n return []\n }\n if (typeof content === 'string') {\n return [{ type: 'text', content: content }]\n }\n return content\n }\n\n /**\n * Extracts text content from a content value that may be string, null, or ContentPart array.\n */\n protected extractTextContent(\n content: string | null | Array<ContentPart>,\n ): string {\n if (content === null) {\n return ''\n }\n if (typeof content === 'string') {\n return content\n }\n // It's an array of ContentPart\n return content\n .filter((p) => p.type === 'text')\n .map((p) => p.content)\n .join('')\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAuCO,MAAe,uCAOZ,gBAMR;AAAA,EACkB,OAAO;AAAA,EAChB;AAAA,EACC;AAAA,EAEV,YAAY,OAAe,MAAc,QAAgB;AACvD,UAAM,CAAA,GAAI,KAAK;AACf,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,OAAO,WACL,SAC4B;AAK5B,UAAM,uCAAuB,IAAA;AAkB7B,UAAM,YAAY;AAAA,MAChB,OAAO,WAAW,KAAK,IAAI;AAAA,MAC3B,UAAU,QAAQ,YAAY,WAAW,KAAK,IAAI;AAAA,MAClD,WAAW,WAAW,KAAK,IAAI;AAAA,MAC/B,sBAAsB;AAAA,IAAA;AAGxB,QAAI;AAKF,YAAM,gBAAgB,KAAK,oBAAoB,OAAO;AACtD,cAAQ,OAAO;AAAA,QACb,0BAA0B,KAAK,IAAI,UAAU,KAAK,KAAK,aAAa,QAAQ,SAAS,MAAM,UAAU,QAAQ,OAAO,UAAU,CAAC;AAAA,QAC/H,EAAE,UAAU,KAAK,MAAM,OAAO,KAAK,MAAA;AAAA,MAAM;AAE3C,YAAM,WAAW,MAAM,KAAK,OAAO,UAAU;AAAA,QAC3C;AAAA,UACE,GAAG;AAAA,UACH,QAAQ;AAAA,QAAA;AAAA,QAEV,sBAAsB,QAAQ,OAAO;AAAA,MAAA;AAGvC,aAAO,KAAK;AAAA,QACV;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MAAA;AAAA,IAEJ,SAAS,OAAgB;AAGvB,YAAM,eAAe;AAAA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W,OAAO,IAC/C,aACA,QAAQ,SAAS,WAAW,UAAU;AAC1C,eAAO;AAAA,UACL,MAAM;AAAA,UACN,WAAW;AAAA,QAAA;AAAA,MAEf;AAAA,MAEA,KAAK;AAAA,MACL,KAAK;AAAA,MACL;AAIE,cAAM,IAAI,MAAM,kCAAkC,KAAK,IAAI,EAAE;AAAA,IAAA;AAAA,EAEnE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,iBACR,SACoB;AACpB,QAAI,YAAY,MAAM;AACpB,aAAO,CAAA;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO,CAAC,EAAE,MAAM,QAAQ,SAAkB;AAAA,IAC5C;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKU,mBACR,SACQ;AACR,QAAI,YAAY,MAAM;AACpB,aAAO;AAAA,IACT;AACA,QAAI,OAAO,YAAY,UAAU;AAC/B,aAAO;AAAA,IACT;AAEA,WAAO,QACJ,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAC/B,IAAI,CAAC,MAAM,EAAE,OAAO,EACpB,KAAK,EAAE;AAAA,EACZ;AACF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/openai-base",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Shared OpenAI SDK base adapters for TanStack AI providers using Chat Completions and Responses APIs.",
5
5
  "author": "",
6
6
  "license": "MIT",
@@ -39,13 +39,13 @@
39
39
  "@tanstack/ai-utils": "0.2.1"
40
40
  },
41
41
  "peerDependencies": {
42
- "@tanstack/ai": "^0.25.0"
42
+ "@tanstack/ai": "^0.27.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@vitest/coverage-v8": "4.0.14",
46
46
  "vite": "^7.3.3",
47
47
  "zod": "^4.2.0",
48
- "@tanstack/ai": "0.25.0"
48
+ "@tanstack/ai": "0.27.0"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "vite build",
@@ -1157,22 +1157,14 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter<
1157
1157
  }
1158
1158
  : undefined
1159
1159
 
1160
- // Build the request so explicit top-level options win over modelOptions
1161
- // when set, but `undefined` top-level options do NOT clobber values the
1162
- // caller put in modelOptions. Keeping the merge nullish-aware fixes the
1163
- // silent regression where a `modelOptions: { temperature: 0.7 }` setting
1164
- // was overwritten with `temperature: undefined`.
1160
+ // `modelOptions` is the sole sampling surface: callers set provider-native
1161
+ // wire names (`temperature`, `top_p`, `max_tokens`/`max_completion_tokens`)
1162
+ // there and they flow through the spread below. The root
1163
+ // `temperature`/`topP`/`maxTokens` fields are intentionally NOT read here.
1165
1164
  return {
1166
1165
  ...modelOptions,
1167
1166
  model: options.model,
1168
1167
  messages,
1169
- ...(options.temperature !== undefined && {
1170
- temperature: options.temperature,
1171
- }),
1172
- ...(options.maxTokens !== undefined && {
1173
- max_tokens: options.maxTokens,
1174
- }),
1175
- ...(options.topP !== undefined && { top_p: options.topP }),
1176
1168
  // Conditional spread: `tools: undefined` would clobber any
1177
1169
  // modelOptions.tools the caller set above.
1178
1170
  ...(tools &&
@@ -1645,23 +1645,15 @@ export abstract class OpenAIBaseResponsesTextAdapter<
1645
1645
  }
1646
1646
  : undefined
1647
1647
 
1648
- // Spread modelOptions first, then explicit top-level options when set.
1649
- // Mirrors the chat-completions base adapter's precedence so callers
1650
- // tuning either backend get identical behaviour. Leaving `modelOptions`
1651
- // last (its previous behavior) silently shadowed the canonical
1652
- // `options.temperature`/`maxTokens` fields, while spreading first
1653
- // without nullish-aware merge would clobber `modelOptions.temperature`
1654
- // with `undefined` whenever the caller didn't set the top-level option.
1648
+ // `modelOptions` is the sole sampling surface: `temperature`, `top_p`, and
1649
+ // `max_output_tokens` live there (typed via OpenAISamplingOptions) and are
1650
+ // spread first. Engine-managed fields (`model`, `metadata`, `instructions`,
1651
+ // `input`, `tools`, `textFormat`) are layered on top afterward so they
1652
+ // always win over any same-named key a caller happened to put in
1653
+ // `modelOptions`.
1655
1654
  return {
1656
1655
  ...modelOptions,
1657
1656
  model: options.model,
1658
- ...(options.temperature !== undefined && {
1659
- temperature: options.temperature,
1660
- }),
1661
- ...(options.maxTokens !== undefined && {
1662
- max_output_tokens: options.maxTokens,
1663
- }),
1664
- ...(options.topP !== undefined && { top_p: options.topP }),
1665
1657
  ...(options.metadata !== undefined && { metadata: options.metadata }),
1666
1658
  ...(() => {
1667
1659
  const prompts = normalizeSystemPrompts(options.systemPrompts)