@tanstack/ai-bedrock 0.6.0 → 0.6.2

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.
@@ -133,12 +133,10 @@ function messageToBlocks(msg, docCounter, context) {
133
133
  }
134
134
  function toolUseBlock(call) {
135
135
  const rawArguments = call.function.arguments || "{}";
136
- let parsed;
136
+ let parsed = {};
137
137
  try {
138
138
  parsed = JSON.parse(rawArguments);
139
- } catch (error) {
140
- throw new Error(`Bedrock Converse: tool call "${call.function.name}" has malformed JSON arguments (${String(error)}). Raw: ${rawArguments}`);
141
- }
139
+ } catch {}
142
140
  if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error(`Bedrock Converse: tool call "${call.function.name}" arguments must be a JSON object, got ${Array.isArray(parsed) ? "array" : typeof parsed}.`);
143
141
  return { toolUse: {
144
142
  toolUseId: call.id,
@@ -1 +1 @@
1
- {"version":3,"file":"message-converter.js","names":[],"sources":["../../../src/converse/message-converter.ts"],"sourcesContent":["import {\n hashToolCallId,\n orderedAssistantBlocks,\n transformMessagesForReplay,\n} from '@tanstack/ai/adapter-internals'\nimport {\n isFileSource,\n normalizeSystemPrompts,\n unsupportedFileSourceError,\n} from '@tanstack/ai'\nimport type {\n ContentPart,\n ContentPartDataSource,\n DocumentPart,\n ImagePart,\n ModelMessage,\n SystemPrompt,\n TextPart,\n} from '@tanstack/ai'\nimport type {\n ContentBlock,\n Message,\n SystemContentBlock,\n ToolResultContentBlock,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { DocumentType } from '@smithy/types'\nimport type {\n BedrockSystemPromptMetadata,\n BedrockTextMetadata,\n} from '../message-types'\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nfunction base64ToBytes(b64: string): Uint8Array {\n return new Uint8Array(Buffer.from(b64, 'base64'))\n}\n\n// AWS ToolResultContentBlock.image: Nova and Claude 3/4 ids only.\n// ponytail: id substring, not the image-input catalog (Llama accepts user images).\nfunction supportsToolResultImage(model: string | undefined): boolean {\n return /amazon\\.nova|anthropic\\.claude-(?:3|(?:opus|sonnet|haiku)-4)/i.test(\n model ?? '',\n )\n}\n\nfunction imageFormat(mime: string): 'png' | 'jpeg' | 'gif' | 'webp' {\n switch (mime) {\n case 'image/png':\n return 'png'\n case 'image/jpeg':\n case 'image/jpg':\n return 'jpeg'\n case 'image/gif':\n return 'gif'\n case 'image/webp':\n return 'webp'\n default:\n throw new Error(\n `Bedrock Converse: unsupported image MIME type \"${mime}\". Supported types: image/png, image/jpeg, image/gif, image/webp.`,\n )\n }\n}\n\nfunction documentFormat(\n mime: string,\n): 'pdf' | 'csv' | 'doc' | 'docx' | 'xls' | 'xlsx' | 'html' | 'txt' | 'md' {\n switch (mime) {\n case 'application/pdf':\n return 'pdf'\n case 'text/csv':\n return 'csv'\n case 'application/msword':\n return 'doc'\n case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':\n return 'docx'\n case 'application/vnd.ms-excel':\n return 'xls'\n case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':\n return 'xlsx'\n case 'text/html':\n return 'html'\n case 'text/plain':\n return 'txt'\n case 'text/markdown':\n case 'text/x-markdown':\n return 'md'\n default:\n throw new Error(\n `Bedrock Converse: unsupported document MIME type \"${mime}\". Supported types: pdf, csv, doc, docx, xls, xlsx, html, txt, md.`,\n )\n }\n}\n\nfunction stringContent(\n content: string | null | undefined | Array<ContentPart>,\n): string {\n if (content === null || content === undefined) return ''\n if (typeof content === 'string') return content\n return content\n .filter((p): p is TextPart => p.type === 'text')\n .map((p) => p.content)\n .join('')\n}\n\nfunction isTextPart(p: ContentPart): p is TextPart {\n return p.type === 'text'\n}\n\nfunction isImagePart(p: ContentPart): p is ImagePart {\n return p.type === 'image'\n}\n\nfunction isDocumentPart(p: ContentPart): p is DocumentPart {\n return p.type === 'document'\n}\n\nfunction isDataSource(\n source: ImagePart['source'] | DocumentPart['source'],\n): source is ContentPartDataSource {\n return source.type === 'data'\n}\n\nfunction contentPartToBlock(part: ContentPart, docIndex: number): ContentBlock {\n if (isTextPart(part)) {\n return { text: part.content }\n }\n\n if (isImagePart(part)) {\n const { source } = part\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (!isDataSource(source)) {\n throw new Error(\n 'Bedrock Converse requires inline image bytes; URL image sources are not supported.',\n )\n }\n return {\n image: {\n format: imageFormat(source.mimeType),\n source: { bytes: base64ToBytes(source.value) },\n },\n }\n }\n\n if (isDocumentPart(part)) {\n const { source } = part\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (!isDataSource(source)) {\n throw new Error(\n 'Bedrock Converse requires inline document bytes; URL document sources are not supported.',\n )\n }\n return {\n document: {\n format: documentFormat(source.mimeType),\n name: `document-${docIndex}`,\n source: { bytes: base64ToBytes(source.value) },\n },\n }\n }\n\n // Fail loud for unsupported part types (audio, video, etc.)\n throw new Error(\n `Bedrock Converse does not support content part type \"${String(part.type)}\".`,\n )\n}\n\ninterface ConverseReplayContext {\n model: string\n provider?: string\n}\n\nfunction messageToBlocks(\n msg: ModelMessage,\n docCounter: { value: number },\n context?: ConverseReplayContext,\n): Array<ContentBlock> {\n const blocks: Array<ContentBlock> = []\n\n if (msg.role === 'tool') {\n if (!msg.toolCallId) {\n throw new Error(\n 'Bedrock Converse: tool message is missing toolCallId. Every tool result must reference the tool use ID it is responding to.',\n )\n }\n const textContent = stringContent(msg.content)\n const content: Array<ToolResultContentBlock> = textContent\n ? [{ text: textContent }]\n : []\n for (const part of Array.isArray(msg.content) ? msg.content : []) {\n // Inline bytes on Nova and Claude 3/4 only. Skip the rest, as before.\n if (\n !supportsToolResultImage(context?.model) ||\n !isImagePart(part) ||\n !isDataSource(part.source)\n )\n continue\n let image: ContentBlock['image']\n try {\n image = contentPartToBlock(part, 0).image\n } catch {\n continue\n }\n if (image) content.push({ image })\n }\n if (content.length === 0) content.push({ text: textContent })\n blocks.push({\n toolResult: {\n toolUseId: msg.toolCallId,\n content,\n status: msg.error !== undefined ? 'error' : 'success',\n },\n })\n return blocks\n }\n\n // Thinking goes back as reasoning blocks. Claude needs its signature, so\n // unsigned Claude thinking goes back as text.\n function appendThinking(\n thinking: NonNullable<ModelMessage['thinking']>[number],\n ): void {\n if (thinking.redacted) {\n if (thinking.signature)\n blocks.push({\n reasoningContent: {\n redactedContent: base64ToBytes(thinking.signature),\n },\n })\n return\n }\n const text = thinking.content\n if (!text) return\n const claude = /anthropic[./]claude/i.test(context?.model ?? '')\n if (claude && !thinking.signature) blocks.push({ text })\n else\n blocks.push({\n reasoningContent: {\n reasoningText: {\n text,\n ...(claude && thinking.signature\n ? { signature: thinking.signature }\n : {}),\n },\n },\n })\n }\n const ordered =\n msg.role === 'assistant' ? orderedAssistantBlocks(msg) : undefined\n if (ordered) {\n for (const block of ordered) {\n if (block.type === 'thinking') appendThinking(block.thinking)\n else if (block.type === 'tool-call')\n blocks.push(toolUseBlock(block.toolCall))\n else blocks.push({ text: block.text })\n }\n return blocks\n }\n if (msg.role === 'assistant')\n for (const thinking of msg.thinking ?? []) appendThinking(thinking)\n\n // Map content field to text/image/document blocks\n if (typeof msg.content === 'string') {\n if (msg.content !== '') {\n blocks.push({ text: msg.content })\n }\n } else if (Array.isArray(msg.content)) {\n for (const part of msg.content) {\n const docIndex = isDocumentPart(part) ? ++docCounter.value : 0\n blocks.push(contentPartToBlock(part, docIndex))\n if (isTextPart(part)) {\n const { cachePoint } = (part.metadata ?? {}) as BedrockTextMetadata\n if (cachePoint) blocks.push({ cachePoint })\n }\n }\n }\n // null → no text blocks\n\n // Append toolUse blocks for assistant tool calls. Malformed or non-object\n // arguments come from a prior assistant turn the engine already accepted, so\n // they signal a real upstream problem — throw rather than silently coercing to\n // `{}` and forwarding a corrupted tool call (the adapter's catch surfaces it\n // as a RUN_ERROR).\n if (msg.role === 'assistant' && msg.toolCalls) {\n for (const call of msg.toolCalls) blocks.push(toolUseBlock(call))\n }\n\n return blocks\n}\n\nfunction toolUseBlock(\n call: NonNullable<ModelMessage['toolCalls']>[number],\n): ContentBlock {\n const rawArguments = call.function.arguments || '{}'\n let parsed: unknown\n try {\n parsed = JSON.parse(rawArguments)\n } catch (error: unknown) {\n throw new Error(\n `Bedrock Converse: tool call \"${call.function.name}\" has malformed JSON arguments (${String(error)}). Raw: ${rawArguments}`,\n )\n }\n if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {\n throw new Error(\n `Bedrock Converse: tool call \"${call.function.name}\" arguments must be a JSON object, got ${Array.isArray(parsed) ? 'array' : typeof parsed}.`,\n )\n }\n return {\n toolUse: {\n toolUseId: call.id,\n name: call.function.name,\n input: parsed as DocumentType,\n },\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\n/**\n * Convert TanStack AI messages + system prompts into the Converse API format.\n *\n * - System prompts are lifted into `SystemContentBlock[]`; a prompt whose\n * `metadata.cachePoint` is set is followed by a `cachePoint` block.\n * - `tool` role messages are remapped to `user` role `toolResult` blocks.\n * - Consecutive messages with the same Converse role are merged (Converse\n * requires strict user/assistant alternation).\n */\nexport function toConverseMessages(\n messages: Array<ModelMessage>,\n systemPrompts?: Array<SystemPrompt>,\n context?: ConverseReplayContext,\n): { system: Array<SystemContentBlock>; messages: Array<Message> } {\n // Build system blocks (uses normalizeSystemPrompts for runtime validation)\n const system: Array<SystemContentBlock> =\n normalizeSystemPrompts<BedrockSystemPromptMetadata>(systemPrompts).flatMap(\n (p) =>\n p.metadata?.cachePoint\n ? [{ text: p.content }, { cachePoint: p.metadata.cachePoint }]\n : [{ text: p.content }],\n )\n\n // Convert each ModelMessage to a Converse Message, merging same-role pairs\n const converseMessages: Array<Message> = []\n // Global document counter: ensures every document block across all messages\n // gets a unique name, preventing Bedrock ValidationException for duplicate names.\n const docCounter = { value: 0 }\n\n // History from another model loses its signatures, and its tool IDs are\n // rewritten to the shape Converse accepts. Each call stays paired.\n const replay = transformMessagesForReplay(\n messages,\n context\n ? {\n provider: context.provider ?? 'amazon-bedrock',\n api: 'bedrock-converse-stream',\n model: context.model,\n }\n : undefined,\n (id, { foreign, attempt }) => {\n if (!foreign && attempt === 0) return id\n const clean = id.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64)\n return attempt === 0\n ? clean\n : clean.slice(0, 47) +\n '_' +\n hashToolCallId(id + ':' + attempt).slice(0, 16)\n },\n )\n for (const msg of replay.messages) {\n // Map TanStack roles to Converse roles\n const converseRole: 'user' | 'assistant' =\n msg.role === 'assistant' ? 'assistant' : 'user'\n\n const blocks = messageToBlocks(msg, docCounter, context)\n\n // Skip messages that produce no content blocks (e.g. assistant with\n // null content and no toolCalls). Pushing an empty-content message to\n // Converse triggers a ValidationException.\n if (blocks.length === 0) continue\n\n const last = converseMessages[converseMessages.length - 1]\n if (last && last.role === converseRole) {\n // Merge into the previous message's content array\n last.content = [...(last.content ?? []), ...blocks]\n } else {\n converseMessages.push({ role: converseRole, content: blocks })\n }\n }\n\n return { system, messages: converseMessages }\n}\n\n/**\n * Write the `toolUse` and `toolResult` blocks as text blocks. Bedrock rejects\n * these blocks in a request with no `toolConfig`, so a request with no tools\n * sends its tool history this way.\n */\nexport function toolBlocksToText(messages: Array<Message>): Array<Message> {\n return messages.map((message) => ({\n ...message,\n content: message.content?.flatMap((block): Array<ContentBlock> => {\n if (block.toolUse) {\n const { toolUseId, name, input } = block.toolUse\n return [\n {\n text: `[Tool call ${toolUseId} ${name}(${JSON.stringify(input)})]`,\n },\n ]\n }\n if (block.toolResult) {\n const { toolUseId, content = [] } = block.toolResult\n const text = content.map((part) => part.text ?? '').join('')\n // Keep the images of the result as image blocks after the text.\n const images = content.flatMap((part) =>\n part.image ? [{ image: part.image }] : [],\n )\n return [{ text: `[Tool result ${toolUseId}: ${text}]` }, ...images]\n }\n return [block]\n }),\n }))\n}\n"],"mappings":";;;AAmCA,SAAS,cAAc,KAAyB;CAC9C,OAAO,IAAI,WAAW,OAAO,KAAK,KAAK,QAAQ,CAAC;AAClD;AAIA,S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J"}
1
+ {"version":3,"file":"message-converter.js","names":[],"sources":["../../../src/converse/message-converter.ts"],"sourcesContent":["import {\n hashToolCallId,\n orderedAssistantBlocks,\n transformMessagesForReplay,\n} from '@tanstack/ai/adapter-internals'\nimport {\n isFileSource,\n normalizeSystemPrompts,\n unsupportedFileSourceError,\n} from '@tanstack/ai'\nimport type {\n ContentPart,\n ContentPartDataSource,\n DocumentPart,\n ImagePart,\n ModelMessage,\n SystemPrompt,\n TextPart,\n} from '@tanstack/ai'\nimport type {\n ContentBlock,\n Message,\n SystemContentBlock,\n ToolResultContentBlock,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { DocumentType } from '@smithy/types'\nimport type {\n BedrockSystemPromptMetadata,\n BedrockTextMetadata,\n} from '../message-types'\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nfunction base64ToBytes(b64: string): Uint8Array {\n return new Uint8Array(Buffer.from(b64, 'base64'))\n}\n\n// AWS ToolResultContentBlock.image: Nova and Claude 3/4 ids only.\n// ponytail: id substring, not the image-input catalog (Llama accepts user images).\nfunction supportsToolResultImage(model: string | undefined): boolean {\n return /amazon\\.nova|anthropic\\.claude-(?:3|(?:opus|sonnet|haiku)-4)/i.test(\n model ?? '',\n )\n}\n\nfunction imageFormat(mime: string): 'png' | 'jpeg' | 'gif' | 'webp' {\n switch (mime) {\n case 'image/png':\n return 'png'\n case 'image/jpeg':\n case 'image/jpg':\n return 'jpeg'\n case 'image/gif':\n return 'gif'\n case 'image/webp':\n return 'webp'\n default:\n throw new Error(\n `Bedrock Converse: unsupported image MIME type \"${mime}\". Supported types: image/png, image/jpeg, image/gif, image/webp.`,\n )\n }\n}\n\nfunction documentFormat(\n mime: string,\n): 'pdf' | 'csv' | 'doc' | 'docx' | 'xls' | 'xlsx' | 'html' | 'txt' | 'md' {\n switch (mime) {\n case 'application/pdf':\n return 'pdf'\n case 'text/csv':\n return 'csv'\n case 'application/msword':\n return 'doc'\n case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':\n return 'docx'\n case 'application/vnd.ms-excel':\n return 'xls'\n case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':\n return 'xlsx'\n case 'text/html':\n return 'html'\n case 'text/plain':\n return 'txt'\n case 'text/markdown':\n case 'text/x-markdown':\n return 'md'\n default:\n throw new Error(\n `Bedrock Converse: unsupported document MIME type \"${mime}\". Supported types: pdf, csv, doc, docx, xls, xlsx, html, txt, md.`,\n )\n }\n}\n\nfunction stringContent(\n content: string | null | undefined | Array<ContentPart>,\n): string {\n if (content === null || content === undefined) return ''\n if (typeof content === 'string') return content\n return content\n .filter((p): p is TextPart => p.type === 'text')\n .map((p) => p.content)\n .join('')\n}\n\nfunction isTextPart(p: ContentPart): p is TextPart {\n return p.type === 'text'\n}\n\nfunction isImagePart(p: ContentPart): p is ImagePart {\n return p.type === 'image'\n}\n\nfunction isDocumentPart(p: ContentPart): p is DocumentPart {\n return p.type === 'document'\n}\n\nfunction isDataSource(\n source: ImagePart['source'] | DocumentPart['source'],\n): source is ContentPartDataSource {\n return source.type === 'data'\n}\n\nfunction contentPartToBlock(part: ContentPart, docIndex: number): ContentBlock {\n if (isTextPart(part)) {\n return { text: part.content }\n }\n\n if (isImagePart(part)) {\n const { source } = part\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (!isDataSource(source)) {\n throw new Error(\n 'Bedrock Converse requires inline image bytes; URL image sources are not supported.',\n )\n }\n return {\n image: {\n format: imageFormat(source.mimeType),\n source: { bytes: base64ToBytes(source.value) },\n },\n }\n }\n\n if (isDocumentPart(part)) {\n const { source } = part\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (!isDataSource(source)) {\n throw new Error(\n 'Bedrock Converse requires inline document bytes; URL document sources are not supported.',\n )\n }\n return {\n document: {\n format: documentFormat(source.mimeType),\n name: `document-${docIndex}`,\n source: { bytes: base64ToBytes(source.value) },\n },\n }\n }\n\n // Fail loud for unsupported part types (audio, video, etc.)\n throw new Error(\n `Bedrock Converse does not support content part type \"${String(part.type)}\".`,\n )\n}\n\ninterface ConverseReplayContext {\n model: string\n provider?: string\n}\n\nfunction messageToBlocks(\n msg: ModelMessage,\n docCounter: { value: number },\n context?: ConverseReplayContext,\n): Array<ContentBlock> {\n const blocks: Array<ContentBlock> = []\n\n if (msg.role === 'tool') {\n if (!msg.toolCallId) {\n throw new Error(\n 'Bedrock Converse: tool message is missing toolCallId. Every tool result must reference the tool use ID it is responding to.',\n )\n }\n const textContent = stringContent(msg.content)\n const content: Array<ToolResultContentBlock> = textContent\n ? [{ text: textContent }]\n : []\n for (const part of Array.isArray(msg.content) ? msg.content : []) {\n // Inline bytes on Nova and Claude 3/4 only. Skip the rest, as before.\n if (\n !supportsToolResultImage(context?.model) ||\n !isImagePart(part) ||\n !isDataSource(part.source)\n )\n continue\n let image: ContentBlock['image']\n try {\n image = contentPartToBlock(part, 0).image\n } catch {\n continue\n }\n if (image) content.push({ image })\n }\n if (content.length === 0) content.push({ text: textContent })\n blocks.push({\n toolResult: {\n toolUseId: msg.toolCallId,\n content,\n status: msg.error !== undefined ? 'error' : 'success',\n },\n })\n return blocks\n }\n\n // Thinking goes back as reasoning blocks. Claude needs its signature, so\n // unsigned Claude thinking goes back as text.\n function appendThinking(\n thinking: NonNullable<ModelMessage['thinking']>[number],\n ): void {\n if (thinking.redacted) {\n if (thinking.signature)\n blocks.push({\n reasoningContent: {\n redactedContent: base64ToBytes(thinking.signature),\n },\n })\n return\n }\n const text = thinking.content\n if (!text) return\n const claude = /anthropic[./]claude/i.test(context?.model ?? '')\n if (claude && !thinking.signature) blocks.push({ text })\n else\n blocks.push({\n reasoningContent: {\n reasoningText: {\n text,\n ...(claude && thinking.signature\n ? { signature: thinking.signature }\n : {}),\n },\n },\n })\n }\n const ordered =\n msg.role === 'assistant' ? orderedAssistantBlocks(msg) : undefined\n if (ordered) {\n for (const block of ordered) {\n if (block.type === 'thinking') appendThinking(block.thinking)\n else if (block.type === 'tool-call')\n blocks.push(toolUseBlock(block.toolCall))\n else blocks.push({ text: block.text })\n }\n return blocks\n }\n if (msg.role === 'assistant')\n for (const thinking of msg.thinking ?? []) appendThinking(thinking)\n\n // Map content field to text/image/document blocks\n if (typeof msg.content === 'string') {\n if (msg.content !== '') {\n blocks.push({ text: msg.content })\n }\n } else if (Array.isArray(msg.content)) {\n for (const part of msg.content) {\n const docIndex = isDocumentPart(part) ? ++docCounter.value : 0\n blocks.push(contentPartToBlock(part, docIndex))\n if (isTextPart(part)) {\n const { cachePoint } = (part.metadata ?? {}) as BedrockTextMetadata\n if (cachePoint) blocks.push({ cachePoint })\n }\n }\n }\n // null → no text blocks\n\n // Append toolUse blocks for assistant tool calls. Malformed JSON stays in\n // the history next to its tool error, so it goes out as `{}`. Non-object\n // arguments signal a real upstream problem and throw (the adapter's catch\n // surfaces it as a RUN_ERROR).\n if (msg.role === 'assistant' && msg.toolCalls) {\n for (const call of msg.toolCalls) blocks.push(toolUseBlock(call))\n }\n\n return blocks\n}\n\nfunction toolUseBlock(\n call: NonNullable<ModelMessage['toolCalls']>[number],\n): ContentBlock {\n const rawArguments = call.function.arguments || '{}'\n let parsed: unknown = {}\n try {\n parsed = JSON.parse(rawArguments)\n } catch {\n // The tool result already told the model the arguments were malformed.\n }\n if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {\n throw new Error(\n `Bedrock Converse: tool call \"${call.function.name}\" arguments must be a JSON object, got ${Array.isArray(parsed) ? 'array' : typeof parsed}.`,\n )\n }\n return {\n toolUse: {\n toolUseId: call.id,\n name: call.function.name,\n input: parsed as DocumentType,\n },\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\n/**\n * Convert TanStack AI messages + system prompts into the Converse API format.\n *\n * - System prompts are lifted into `SystemContentBlock[]`; a prompt whose\n * `metadata.cachePoint` is set is followed by a `cachePoint` block.\n * - `tool` role messages are remapped to `user` role `toolResult` blocks.\n * - Consecutive messages with the same Converse role are merged (Converse\n * requires strict user/assistant alternation).\n */\nexport function toConverseMessages(\n messages: Array<ModelMessage>,\n systemPrompts?: Array<SystemPrompt>,\n context?: ConverseReplayContext,\n): { system: Array<SystemContentBlock>; messages: Array<Message> } {\n // Build system blocks (uses normalizeSystemPrompts for runtime validation)\n const system: Array<SystemContentBlock> =\n normalizeSystemPrompts<BedrockSystemPromptMetadata>(systemPrompts).flatMap(\n (p) =>\n p.metadata?.cachePoint\n ? [{ text: p.content }, { cachePoint: p.metadata.cachePoint }]\n : [{ text: p.content }],\n )\n\n // Convert each ModelMessage to a Converse Message, merging same-role pairs\n const converseMessages: Array<Message> = []\n // Global document counter: ensures every document block across all messages\n // gets a unique name, preventing Bedrock ValidationException for duplicate names.\n const docCounter = { value: 0 }\n\n // History from another model loses its signatures, and its tool IDs are\n // rewritten to the shape Converse accepts. Each call stays paired.\n const replay = transformMessagesForReplay(\n messages,\n context\n ? {\n provider: context.provider ?? 'amazon-bedrock',\n api: 'bedrock-converse-stream',\n model: context.model,\n }\n : undefined,\n (id, { foreign, attempt }) => {\n if (!foreign && attempt === 0) return id\n const clean = id.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64)\n return attempt === 0\n ? clean\n : clean.slice(0, 47) +\n '_' +\n hashToolCallId(id + ':' + attempt).slice(0, 16)\n },\n )\n for (const msg of replay.messages) {\n // Map TanStack roles to Converse roles\n const converseRole: 'user' | 'assistant' =\n msg.role === 'assistant' ? 'assistant' : 'user'\n\n const blocks = messageToBlocks(msg, docCounter, context)\n\n // Skip messages that produce no content blocks (e.g. assistant with\n // null content and no toolCalls). Pushing an empty-content message to\n // Converse triggers a ValidationException.\n if (blocks.length === 0) continue\n\n const last = converseMessages[converseMessages.length - 1]\n if (last && last.role === converseRole) {\n // Merge into the previous message's content array\n last.content = [...(last.content ?? []), ...blocks]\n } else {\n converseMessages.push({ role: converseRole, content: blocks })\n }\n }\n\n return { system, messages: converseMessages }\n}\n\n/**\n * Write the `toolUse` and `toolResult` blocks as text blocks. Bedrock rejects\n * these blocks in a request with no `toolConfig`, so a request with no tools\n * sends its tool history this way.\n */\nexport function toolBlocksToText(messages: Array<Message>): Array<Message> {\n return messages.map((message) => ({\n ...message,\n content: message.content?.flatMap((block): Array<ContentBlock> => {\n if (block.toolUse) {\n const { toolUseId, name, input } = block.toolUse\n return [\n {\n text: `[Tool call ${toolUseId} ${name}(${JSON.stringify(input)})]`,\n },\n ]\n }\n if (block.toolResult) {\n const { toolUseId, content = [] } = block.toolResult\n const text = content.map((part) => part.text ?? '').join('')\n // Keep the images of the result as image blocks after the text.\n const images = content.flatMap((part) =>\n part.image ? [{ image: part.image }] : [],\n )\n return [{ text: `[Tool result ${toolUseId}: ${text}]` }, ...images]\n }\n return [block]\n }),\n }))\n}\n"],"mappings":";;;AAmCA,SAAS,cAAc,KAAyB;CAC9C,OAAO,IAAI,WAAW,OAAO,KAAK,KAAK,QAAQ,CAAC;AAClD;AAIA,SAAS,wBAAwB,OAAoC;CACnE,OAAO,gEAAgE,KACrE,SAAS,EACX;AACF;AAEA,SAAS,YAAY,MAA+C;CAClE,QAAQ,MAAR;EACE,KAAK,aACH,OAAO;EACT,KAAK;EACL,KAAK,aACH,OAAO;EACT,KAAK,aACH,OAAO;EACT,KAAK,cACH,OAAO;EACT,SACE,MAAM,IAAI,MACR,kDAAkD,KAAK,kEACzD;CACJ;AACF;AAEA,SAAS,eACP,MACyE;CACzE,QAAQ,MAAR;EACE,KAAK,mBACH,OAAO;EACT,KAAK,YACH,OAAO;EACT,KAAK,sBACH,OAAO;EACT,KAAK,2EACH,OAAO;EACT,KAAK,4BACH,OAAO;EACT,KAAK,qEACH,OAAO;EACT,KAAK,aACH,OAAO;EACT,KAAK,cACH,OAAO;EACT,KAAK;EACL,KAAK,mBACH,OAAO;EACT,SACE,MAAM,IAAI,MACR,qDAAqD,KAAK,mEAC5D;CACJ;AACF;AAEA,SAAS,cACP,SACQ;CACR,IAAI,YAAY,QAAQ,YAAY,KAAA,GAAW,OAAO;CACtD,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AAM;IACzC,OAAO,CACL,EACE,MAAM,cAAc,UAAU,GAAG,KAAK,GAAG,KAAK,UAAU,KAAK,EAAE,IACjE,CACF;GACF;GACA,IAAI,MAAM,YAAY;IACpB,MAAM,EAAE,WAAW,UAAU,CAAC,MAAM,MAAM;IAC1C,MAAM,OAAO,QAAQ,KAAK,SAAS,KAAK,QAAQ,EAAE,CAAC,CAAC,KAAK,EAAE;IAE3D,MAAM,SAAS,QAAQ,SAAS,SAC9B,KAAK,QAAQ,CAAC,EAAE,OAAO,KAAK,MAAM,CAAC,IAAI,CAAC,CAC1C;IACA,OAAO,CAAC,EAAE,MAAM,gBAAgB,UAAU,IAAI,KAAK,GAAG,GAAG,GAAG,MAAM;GACpE;GACA,OAAO,CAAC,KAAK;EACf,CAAC;CACH,EAAE;AACJ"}
@@ -100,7 +100,8 @@ async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
100
100
  toolCallsByIndex.set(index, {
101
101
  id,
102
102
  name,
103
- started: true
103
+ started: true,
104
+ arguments: ""
104
105
  });
105
106
  yield {
106
107
  type: EventType.TOOL_CALL_START,
@@ -119,12 +120,15 @@ async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
119
120
  const index = block?.contentBlockIndex ?? 0;
120
121
  if (delta && "toolUse" in delta && delta.toolUse?.input !== void 0) {
121
122
  const toolCall = toolCallsByIndex.get(index);
122
- if (toolCall?.started) yield {
123
- type: EventType.TOOL_CALL_ARGS,
124
- toolCallId: toolCall.id,
125
- timestamp: Date.now(),
126
- delta: delta.toolUse.input
127
- };
123
+ if (toolCall?.started) {
124
+ toolCall.arguments += delta.toolUse.input;
125
+ yield {
126
+ type: EventType.TOOL_CALL_ARGS,
127
+ toolCallId: toolCall.id,
128
+ timestamp: Date.now(),
129
+ delta: delta.toolUse.input
130
+ };
131
+ }
128
132
  continue;
129
133
  }
130
134
  if (delta && "reasoningContent" in delta && delta.reasoningContent) {
@@ -205,6 +209,7 @@ async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
205
209
  yield {
206
210
  type: EventType.TOOL_CALL_END,
207
211
  toolCallId: toolCall.id,
212
+ args: toolCall.arguments,
208
213
  toolCallName: toolCall.name,
209
214
  toolName: toolCall.name,
210
215
  timestamp: Date.now()
@@ -230,6 +235,7 @@ async function* processConverseStream(stream, newMessageId, lifecycle = {}) {
230
235
  yield {
231
236
  type: EventType.TOOL_CALL_END,
232
237
  toolCallId: toolCall.id,
238
+ args: toolCall.arguments,
233
239
  toolCallName: toolCall.name,
234
240
  toolName: toolCall.name,
235
241
  timestamp: Date.now()
@@ -1 +1 @@
1
- {"version":3,"file":"stream-processor.js","names":[],"sources":["../../../src/converse/stream-processor.ts"],"sourcesContent":["import { EventType } from '@tanstack/ai'\nimport { buildConverseUsage } from './usage'\nimport type { AdapterYieldChunk, TokenUsage } from '@tanstack/ai'\nimport type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'\n\n/**\n * Converse delivers server-side failures — throttling, request validation,\n * mid-stream model faults, and service-unavailable — as in-band stream events\n * rather than thrown exceptions. If they were ignored the iterator would simply\n * end and the run would look like a clean, truncated success. Throw the\n * underlying exception (these SDK members extend `Error`) so the adapter's\n * `chatStream` / `structuredOutputStream` catch converts it into a `RUN_ERROR`.\n */\nexport function throwIfConverseStreamError(ev: ConverseStreamOutput): void {\n if ('internalServerException' in ev && ev.internalServerException) {\n throw ev.internalServerException\n }\n if ('modelStreamErrorException' in ev && ev.modelStreamErrorException) {\n throw ev.modelStreamErrorException\n }\n if ('validationException' in ev && ev.validationException) {\n throw ev.validationException\n }\n if ('throttlingException' in ev && ev.throttlingException) {\n throw ev.throttlingException\n }\n if ('serviceUnavailableException' in ev && ev.serviceUnavailableException) {\n throw ev.serviceUnavailableException\n }\n}\n\n/** Map Converse blocks to run, text, reasoning, and tool events.\n * Keep signatures and encrypted bytes separate for each contentBlockIndex.\n * Finish after trailing usage events. The adapter handles thrown errors.\n */\nexport async function* processConverseStream(\n stream: AsyncIterable<ConverseStreamOutput>,\n newMessageId: () => string,\n lifecycle: { threadId?: string; parentRunId?: string; model?: string } = {},\n): AsyncIterable<AdapterYieldChunk> {\n const runId = newMessageId()\n const threadId = lifecycle.threadId ?? newMessageId()\n const { parentRunId, model } = lifecycle\n const messageId = newMessageId()\n\n let hasEmittedRunStarted = false\n\n // Text lifecycle\n let accumulatedContent = ''\n let hasEmittedTextMessageStart = false\n\n // Reasoning lifecycle\n const reasoningByIndex = new Map<\n number,\n {\n id: string\n text: string\n signature: string\n bytes: Array<Uint8Array>\n redacted: boolean\n closed: boolean\n encrypted?: string\n }\n >()\n\n // Tool-call lifecycle, keyed by Converse contentBlockIndex. Converse opens a\n // tool-use block with `contentBlockStart`, streams arg fragments via\n // `contentBlockDelta`, and closes it with `contentBlockStop`.\n const toolCallsByIndex = new Map<\n number,\n {\n id: string\n name: string\n started: boolean\n }\n >()\n\n // Usage + finish-reason are captured during iteration and folded into the\n // single terminal RUN_FINISHED, matching openai-base's deferred-finish\n // contract (usage may arrive after the finish signal).\n let usage: TokenUsage | undefined\n let finishReason: NonNullable<AdapterYieldChunk['finishReason']> | undefined\n\n // Lazily emit RUN_STARTED exactly once, before the first content event.\n function* ensureRunStarted(): Generator<AdapterYieldChunk> {\n if (hasEmittedRunStarted) return\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n parentRunId,\n ...(model && { model }),\n timestamp: Date.now(),\n }\n }\n\n // Close an open reasoning message before text/tool content begins, mirroring\n // openai-base which always emits REASONING_MESSAGE_END before TEXT_MESSAGE_START.\n function* encryptedReasoning(index: number): Generator<AdapterYieldChunk> {\n const reasoning = reasoningByIndex.get(index)\n if (!reasoning) return\n const encrypted = reasoning.redacted\n ? Buffer.concat(reasoning.bytes).toString('base64')\n : reasoning.signature\n if (!encrypted || encrypted === reasoning.encrypted) return\n reasoning.encrypted = encrypted\n yield {\n type: EventType.REASONING_ENCRYPTED_VALUE,\n entityId: reasoning.id,\n subtype: 'message',\n encryptedValue: encrypted,\n ...(reasoning.redacted && {\n stepId: reasoning.id.startsWith('redacted_thinking-')\n ? reasoning.id\n : 'redacted_thinking-' + reasoning.id,\n }),\n timestamp: Date.now(),\n }\n }\n\n function* closeReasoning(index?: number): Generator<AdapterYieldChunk> {\n for (const [blockIndex, reasoning] of reasoningByIndex) {\n if (index !== undefined && index !== blockIndex) continue\n yield* encryptedReasoning(blockIndex)\n if (reasoning.closed) continue\n reasoning.closed = true\n yield {\n type: EventType.STEP_FINISHED,\n stepId: reasoning.id,\n stepName: reasoning.id,\n delta: '',\n content: reasoning.text,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n }\n }\n\n for await (const ev of stream) {\n yield* ensureRunStarted()\n\n // Surface in-band server/throttle/validation errors instead of dropping them.\n throwIfConverseStreamError(ev)\n\n // messageStart carries only the role; no AG-UI event maps to it.\n if ('messageStart' in ev) continue\n\n if ('contentBlockStart' in ev) {\n const start = ev.contentBlockStart\n const toolUse = start?.start?.toolUse\n if (start && toolUse) {\n yield* closeReasoning()\n const id = toolUse.toolUseId ?? newMessageId()\n const name = toolUse.name ?? ''\n const index = start.contentBlockIndex ?? 0\n toolCallsByIndex.set(index, {\n id,\n name,\n started: true,\n })\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: id,\n toolCallName: name,\n toolName: name,\n timestamp: Date.now(),\n index,\n }\n }\n continue\n }\n\n if ('contentBlockDelta' in ev) {\n const block = ev.contentBlockDelta\n const delta = block?.delta\n const index = block?.contentBlockIndex ?? 0\n\n // Tool-call argument fragments (partial JSON).\n if (delta && 'toolUse' in delta && delta.toolUse?.input !== undefined) {\n const toolCall = toolCallsByIndex.get(index)\n if (toolCall?.started) {\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: toolCall.id,\n timestamp: Date.now(),\n delta: delta.toolUse.input,\n }\n }\n continue\n }\n\n if (delta && 'reasoningContent' in delta && delta.reasoningContent) {\n const fragment = delta.reasoningContent\n let reasoning = reasoningByIndex.get(index)\n if (!reasoning) {\n const redacted =\n 'redactedContent' in fragment &&\n (fragment.redactedContent?.length ?? 0) > 0\n reasoning = {\n id: (redacted ? 'redacted_thinking-' : '') + newMessageId(),\n text: '',\n signature: '',\n bytes: [],\n redacted,\n closed: false,\n }\n reasoningByIndex.set(index, reasoning)\n yield {\n type: EventType.STEP_STARTED,\n stepId: reasoning.id,\n stepName: reasoning.id,\n stepType: 'thinking',\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoning.id,\n role: 'reasoning',\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'text' in fragment &&\n fragment.text !== undefined\n ) {\n reasoning.text += fragment.text\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoning.id,\n delta: fragment.text,\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'signature' in fragment &&\n fragment.signature !== undefined\n )\n reasoning.signature += fragment.signature\n if (\n 'redactedContent' in fragment &&\n fragment.redactedContent &&\n fragment.redactedContent.length > 0\n ) {\n if (!reasoning.redacted) reasoning.encrypted = undefined\n reasoning.redacted = true\n reasoning.text = ''\n reasoning.signature = ''\n reasoning.bytes.push(fragment.redactedContent)\n yield* encryptedReasoning(index)\n }\n continue\n }\n\n // Text content.\n if (delta && 'text' in delta && delta.text !== undefined) {\n yield* closeReasoning()\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n timestamp: Date.now(),\n }\n }\n accumulatedContent += delta.text\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: delta.text,\n content: accumulatedContent,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('contentBlockStop' in ev) {\n const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0\n yield* closeReasoning(stopIndex)\n const toolCall = toolCallsByIndex.get(stopIndex)\n if (toolCall?.started) {\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(stopIndex)\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // Map Converse stopReason to AG-UI's narrower finishReason vocabulary.\n finishReason =\n stopReason === 'tool_use'\n ? 'tool_calls'\n : stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n // Stream ended (possibly without any content) — still emit RUN_STARTED so\n // consumers always see a run lifecycle.\n yield* ensureRunStarted()\n\n // Drain any tool call that opened but never received contentBlockStop.\n for (const [index, toolCall] of toolCallsByIndex) {\n if (!toolCall.started) continue\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(index)\n }\n\n // Close the text message lifecycle if it was opened.\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n timestamp: Date.now(),\n }\n }\n\n // Close any reasoning lifecycle that text never closed.\n yield* closeReasoning()\n\n // Single terminal RUN_FINISHED. Conditional `usage` spread keeps the wire\n // shape spec-compliant (AG-UI's `usage` is optional with no `| undefined`).\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n timestamp: Date.now(),\n finishReason: finishReason ?? 'stop',\n ...(usage && { usage }),\n }\n}\n"],"mappings":";;;;;;;;;;;AAaA,SAAgB,2BAA2B,IAAgC;CACzE,IAAI,6BAA6B,MAAM,GAAG,yBACxC,MAAM,GAAG;CA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M;MACN,WAAW,KAAK,IAAI;KACtB;IACF;IACA,sBAAsB,MAAM;IAC5B,MAAM;KACJ,MAAM,UAAU;KAChB;KACA,OAAO,MAAM;KACb,SAAS;KACT,WAAW,KAAK,IAAI;IACtB;GACF;GACA;EACF;EAEA,IAAI,sBAAsB,IAAI;GAC5B,MAAM,YAAY,GAAG,kBAAkB,qBAAqB;GAC5D,OAAO,eAAe,SAAS;GAC/B,MAAM,WAAW,iBAAiB,IAAI,SAAS;GAC/C,IAAI,UAAU,SAAS;IACrB,MAAM;KACJ,MAAM,UAAU;KAChB,YAAY,SAAS;KACrB,cAAc,SAAS;KACvB,UAAU,SAAS;KACnB,WAAW,KAAK,IAAI;IACtB;IACA,iBAAiB,OAAO,SAAS;GACnC;GACA;EACF;EAEA,IAAI,iBAAiB,IAAI;GACvB,MAAM,aAAa,GAAG,aAAa;GAEnC,eACE,eAAe,aACX,eACA,eAAe,eACb,WACA,eAAe,qBACb,mBACA;GACV;EACF;EAEA,IAAI,cAAc,IAAI;GACpB,MAAM,IAAI,GAAG,UAAU;GACvB,IAAI,GACF,QAAQ,mBAAmB,CAAC;GAE9B;EACF;CACF;CAIA,OAAO,iBAAiB;CAGxB,KAAK,MAAM,CAAC,OAAO,aAAa,kBAAkB;EAChD,IAAI,CAAC,SAAS,SAAS;EACvB,MAAM;GACJ,MAAM,UAAU;GAChB,YAAY,SAAS;GACrB,cAAc,SAAS;GACvB,UAAU,SAAS;GACnB,WAAW,KAAK,IAAI;EACtB;EACA,iBAAiB,OAAO,KAAK;CAC/B;CAGA,IAAI,4BACF,MAAM;EACJ,MAAM,UAAU;EAChB;EACA,WAAW,KAAK,IAAI;CACtB;CAIF,OAAO,eAAe;CAItB,MAAM;EACJ,MAAM,UAAU;EAChB;EACA;EACA,WAAW,KAAK,IAAI;EACpB,cAAc,gBAAgB;EAC9B,GAAI,SAAS,EAAE,MAAM;CACvB;AACF"}
1
+ {"version":3,"file":"stream-processor.js","names":[],"sources":["../../../src/converse/stream-processor.ts"],"sourcesContent":["import { EventType } from '@tanstack/ai'\nimport { buildConverseUsage } from './usage'\nimport type { AdapterYieldChunk, TokenUsage } from '@tanstack/ai'\nimport type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'\n\n/**\n * Converse delivers server-side failures — throttling, request validation,\n * mid-stream model faults, and service-unavailable — as in-band stream events\n * rather than thrown exceptions. If they were ignored the iterator would simply\n * end and the run would look like a clean, truncated success. Throw the\n * underlying exception (these SDK members extend `Error`) so the adapter's\n * `chatStream` / `structuredOutputStream` catch converts it into a `RUN_ERROR`.\n */\nexport function throwIfConverseStreamError(ev: ConverseStreamOutput): void {\n if ('internalServerException' in ev && ev.internalServerException) {\n throw ev.internalServerException\n }\n if ('modelStreamErrorException' in ev && ev.modelStreamErrorException) {\n throw ev.modelStreamErrorException\n }\n if ('validationException' in ev && ev.validationException) {\n throw ev.validationException\n }\n if ('throttlingException' in ev && ev.throttlingException) {\n throw ev.throttlingException\n }\n if ('serviceUnavailableException' in ev && ev.serviceUnavailableException) {\n throw ev.serviceUnavailableException\n }\n}\n\n/** Map Converse blocks to run, text, reasoning, and tool events.\n * Keep signatures and encrypted bytes separate for each contentBlockIndex.\n * Finish after trailing usage events. The adapter handles thrown errors.\n */\nexport async function* processConverseStream(\n stream: AsyncIterable<ConverseStreamOutput>,\n newMessageId: () => string,\n lifecycle: { threadId?: string; parentRunId?: string; model?: string } = {},\n): AsyncIterable<AdapterYieldChunk> {\n const runId = newMessageId()\n const threadId = lifecycle.threadId ?? newMessageId()\n const { parentRunId, model } = lifecycle\n const messageId = newMessageId()\n\n let hasEmittedRunStarted = false\n\n // Text lifecycle\n let accumulatedContent = ''\n let hasEmittedTextMessageStart = false\n\n // Reasoning lifecycle\n const reasoningByIndex = new Map<\n number,\n {\n id: string\n text: string\n signature: string\n bytes: Array<Uint8Array>\n redacted: boolean\n closed: boolean\n encrypted?: string\n }\n >()\n\n // Tool-call lifecycle, keyed by Converse contentBlockIndex. Converse opens a\n // tool-use block with `contentBlockStart`, streams arg fragments via\n // `contentBlockDelta`, and closes it with `contentBlockStop`.\n const toolCallsByIndex = new Map<\n number,\n {\n id: string\n name: string\n started: boolean\n arguments: string\n }\n >()\n\n // Usage + finish-reason are captured during iteration and folded into the\n // single terminal RUN_FINISHED, matching openai-base's deferred-finish\n // contract (usage may arrive after the finish signal).\n let usage: TokenUsage | undefined\n let finishReason: NonNullable<AdapterYieldChunk['finishReason']> | undefined\n\n // Lazily emit RUN_STARTED exactly once, before the first content event.\n function* ensureRunStarted(): Generator<AdapterYieldChunk> {\n if (hasEmittedRunStarted) return\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n parentRunId,\n ...(model && { model }),\n timestamp: Date.now(),\n }\n }\n\n // Close an open reasoning message before text/tool content begins, mirroring\n // openai-base which always emits REASONING_MESSAGE_END before TEXT_MESSAGE_START.\n function* encryptedReasoning(index: number): Generator<AdapterYieldChunk> {\n const reasoning = reasoningByIndex.get(index)\n if (!reasoning) return\n const encrypted = reasoning.redacted\n ? Buffer.concat(reasoning.bytes).toString('base64')\n : reasoning.signature\n if (!encrypted || encrypted === reasoning.encrypted) return\n reasoning.encrypted = encrypted\n yield {\n type: EventType.REASONING_ENCRYPTED_VALUE,\n entityId: reasoning.id,\n subtype: 'message',\n encryptedValue: encrypted,\n ...(reasoning.redacted && {\n stepId: reasoning.id.startsWith('redacted_thinking-')\n ? reasoning.id\n : 'redacted_thinking-' + reasoning.id,\n }),\n timestamp: Date.now(),\n }\n }\n\n function* closeReasoning(index?: number): Generator<AdapterYieldChunk> {\n for (const [blockIndex, reasoning] of reasoningByIndex) {\n if (index !== undefined && index !== blockIndex) continue\n yield* encryptedReasoning(blockIndex)\n if (reasoning.closed) continue\n reasoning.closed = true\n yield {\n type: EventType.STEP_FINISHED,\n stepId: reasoning.id,\n stepName: reasoning.id,\n delta: '',\n content: reasoning.text,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n }\n }\n\n for await (const ev of stream) {\n yield* ensureRunStarted()\n\n // Surface in-band server/throttle/validation errors instead of dropping them.\n throwIfConverseStreamError(ev)\n\n // messageStart carries only the role; no AG-UI event maps to it.\n if ('messageStart' in ev) continue\n\n if ('contentBlockStart' in ev) {\n const start = ev.contentBlockStart\n const toolUse = start?.start?.toolUse\n if (start && toolUse) {\n yield* closeReasoning()\n const id = toolUse.toolUseId ?? newMessageId()\n const name = toolUse.name ?? ''\n const index = start.contentBlockIndex ?? 0\n toolCallsByIndex.set(index, {\n id,\n name,\n started: true,\n arguments: '',\n })\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: id,\n toolCallName: name,\n toolName: name,\n timestamp: Date.now(),\n index,\n }\n }\n continue\n }\n\n if ('contentBlockDelta' in ev) {\n const block = ev.contentBlockDelta\n const delta = block?.delta\n const index = block?.contentBlockIndex ?? 0\n\n // Tool-call argument fragments (partial JSON).\n if (delta && 'toolUse' in delta && delta.toolUse?.input !== undefined) {\n const toolCall = toolCallsByIndex.get(index)\n if (toolCall?.started) {\n toolCall.arguments += delta.toolUse.input\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: toolCall.id,\n timestamp: Date.now(),\n delta: delta.toolUse.input,\n }\n }\n continue\n }\n\n if (delta && 'reasoningContent' in delta && delta.reasoningContent) {\n const fragment = delta.reasoningContent\n let reasoning = reasoningByIndex.get(index)\n if (!reasoning) {\n const redacted =\n 'redactedContent' in fragment &&\n (fragment.redactedContent?.length ?? 0) > 0\n reasoning = {\n id: (redacted ? 'redacted_thinking-' : '') + newMessageId(),\n text: '',\n signature: '',\n bytes: [],\n redacted,\n closed: false,\n }\n reasoningByIndex.set(index, reasoning)\n yield {\n type: EventType.STEP_STARTED,\n stepId: reasoning.id,\n stepName: reasoning.id,\n stepType: 'thinking',\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoning.id,\n role: 'reasoning',\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'text' in fragment &&\n fragment.text !== undefined\n ) {\n reasoning.text += fragment.text\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoning.id,\n delta: fragment.text,\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'signature' in fragment &&\n fragment.signature !== undefined\n )\n reasoning.signature += fragment.signature\n if (\n 'redactedContent' in fragment &&\n fragment.redactedContent &&\n fragment.redactedContent.length > 0\n ) {\n if (!reasoning.redacted) reasoning.encrypted = undefined\n reasoning.redacted = true\n reasoning.text = ''\n reasoning.signature = ''\n reasoning.bytes.push(fragment.redactedContent)\n yield* encryptedReasoning(index)\n }\n continue\n }\n\n // Text content.\n if (delta && 'text' in delta && delta.text !== undefined) {\n yield* closeReasoning()\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n timestamp: Date.now(),\n }\n }\n accumulatedContent += delta.text\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: delta.text,\n content: accumulatedContent,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('contentBlockStop' in ev) {\n const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0\n yield* closeReasoning(stopIndex)\n const toolCall = toolCallsByIndex.get(stopIndex)\n if (toolCall?.started) {\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(stopIndex)\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // Map Converse stopReason to AG-UI's narrower finishReason vocabulary.\n finishReason =\n stopReason === 'tool_use'\n ? 'tool_calls'\n : stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n // Stream ended (possibly without any content) — still emit RUN_STARTED so\n // consumers always see a run lifecycle.\n yield* ensureRunStarted()\n\n // Drain any tool call that opened but never received contentBlockStop.\n for (const [index, toolCall] of toolCallsByIndex) {\n if (!toolCall.started) continue\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n toolName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(index)\n }\n\n // Close the text message lifecycle if it was opened.\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n timestamp: Date.now(),\n }\n }\n\n // Close any reasoning lifecycle that text never closed.\n yield* closeReasoning()\n\n // Single terminal RUN_FINISHED. Conditional `usage` spread keeps the wire\n // shape spec-compliant (AG-UI's `usage` is optional with no `| undefined`).\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n timestamp: Date.now(),\n finishReason: finishReason ?? 'stop',\n ...(usage && { usage }),\n }\n}\n"],"mappings":";;;;;;;;;;;AAaA,SAAgB,2BAA2B,IAAgC;CACzE,IAAI,6BAA6B,MAAM,GAAG,yBACxC,MAAM,GAAG;CAEX,IAAI,+BAA+B,MAAM,GAAG,2BAC1C,MAAM,GAAG;CAEX,IAAI,yBAAyB,MAAM,GAAG,qBACpC,MAAM,GAAG;CAEX,IAAI,yBAAyB,MAAM,GAAG,qBACpC,MAAM,GAAG;CAEX,IAAI,iCAAiC,MAAM,GAAG,6BAC5C,MAAM,GAAG;AAEb;;;;;AAMA,gBAAuB,sBACrB,QACA,cACA,YAAyE,CAAC,GACxC;CAClC,MAAM,QAAQ,aAAa;CAC3B,MAAM,WAAW,UAAU,YAAY,aAAa;CACpD,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,IAAI,SAAS;GAC/C,IAAI,UAAU,SAAS;IACrB,MAAM;KACJ,MAAM,UAAU;KAChB,YAAY,SAAS;KACrB,MAAM,SAAS;KACf,cAAc,SAAS;KACvB,UAAU,SAAS;KACnB,WAAW,KAAK,IAAI;IACtB;IACA,iBAAiB,OAAO,SAAS;GACnC;GACA;EACF;EAEA,IAAI,iBAAiB,IAAI;GACvB,MAAM,aAAa,GAAG,aAAa;GAEnC,eACE,eAAe,aACX,eACA,eAAe,eACb,WACA,eAAe,qBACb,mBACA;GACV;EACF;EAEA,IAAI,cAAc,IAAI;GACpB,MAAM,IAAI,GAAG,UAAU;GACvB,IAAI,GACF,QAAQ,mBAAmB,CAAC;GAE9B;EACF;CACF;CAIA,OAAO,iBAAiB;CAGxB,KAAK,MAAM,CAAC,OAAO,aAAa,kBAAkB;EAChD,IAAI,CAAC,SAAS,SAAS;EACvB,MAAM;GACJ,MAAM,UAAU;GAChB,YAAY,SAAS;GACrB,MAAM,SAAS;GACf,cAAc,SAAS;GACvB,UAAU,SAAS;GACnB,WAAW,KAAK,IAAI;EACtB;EACA,iBAAiB,OAAO,KAAK;CAC/B;CAGA,IAAI,4BACF,MAAM;EACJ,MAAM,UAAU;EAChB;EACA,WAAW,KAAK,IAAI;CACtB;CAIF,OAAO,eAAe;CAItB,MAAM;EACJ,MAAM,UAAU;EAChB;EACA;EACA,WAAW,KAAK,IAAI;EACpB,cAAc,gBAAgB;EAC9B,GAAI,SAAS,EAAE,MAAM;CACvB;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai-bedrock",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "type": "module",
5
5
  "description": "Amazon Bedrock adapter for TanStack AI — OpenAI-compatible chat, responses, tools, and reasoning.",
6
6
  "author": "",
@@ -43,7 +43,7 @@
43
43
  "vite": "^8.2.1"
44
44
  },
45
45
  "peerDependencies": {
46
- "@tanstack/ai": "^0.69.0"
46
+ "@tanstack/ai": "^0.71.0"
47
47
  },
48
48
  "dependencies": {
49
49
  "@aws-crypto/sha256-js": "^5.2.0",
@@ -52,7 +52,7 @@
52
52
  "@smithy/signature-v4": "^5.4.5",
53
53
  "@smithy/types": "^4.14.2",
54
54
  "openai": "^7.30.0",
55
- "@tanstack/openai-base": "^0.14.0"
55
+ "@tanstack/openai-base": "^0.16.0"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "vite build",
@@ -276,11 +276,10 @@ function messageToBlocks(
276
276
  }
277
277
  // null → no text blocks
278
278
 
279
- // Append toolUse blocks for assistant tool calls. Malformed or non-object
280
- // arguments come from a prior assistant turn the engine already accepted, so
281
- // they signal a real upstream problem — throw rather than silently coercing to
282
- // `{}` and forwarding a corrupted tool call (the adapter's catch surfaces it
283
- // as a RUN_ERROR).
279
+ // Append toolUse blocks for assistant tool calls. Malformed JSON stays in
280
+ // the history next to its tool error, so it goes out as `{}`. Non-object
281
+ // arguments signal a real upstream problem and throw (the adapter's catch
282
+ // surfaces it as a RUN_ERROR).
284
283
  if (msg.role === 'assistant' && msg.toolCalls) {
285
284
  for (const call of msg.toolCalls) blocks.push(toolUseBlock(call))
286
285
  }
@@ -292,13 +291,11 @@ function toolUseBlock(
292
291
  call: NonNullable<ModelMessage['toolCalls']>[number],
293
292
  ): ContentBlock {
294
293
  const rawArguments = call.function.arguments || '{}'
295
- let parsed: unknown
294
+ let parsed: unknown = {}
296
295
  try {
297
296
  parsed = JSON.parse(rawArguments)
298
- } catch (error: unknown) {
299
- throw new Error(
300
- `Bedrock Converse: tool call "${call.function.name}" has malformed JSON arguments (${String(error)}). Raw: ${rawArguments}`,
301
- )
297
+ } catch {
298
+ // The tool result already told the model the arguments were malformed.
302
299
  }
303
300
  if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
304
301
  throw new Error(
@@ -72,6 +72,7 @@ export async function* processConverseStream(
72
72
  id: string
73
73
  name: string
74
74
  started: boolean
75
+ arguments: string
75
76
  }
76
77
  >()
77
78
 
@@ -167,6 +168,7 @@ export async function* processConverseStream(
167
168
  id,
168
169
  name,
169
170
  started: true,
171
+ arguments: '',
170
172
  })
171
173
  yield {
172
174
  type: EventType.TOOL_CALL_START,
@@ -189,6 +191,7 @@ export async function* processConverseStream(
189
191
  if (delta && 'toolUse' in delta && delta.toolUse?.input !== undefined) {
190
192
  const toolCall = toolCallsByIndex.get(index)
191
193
  if (toolCall?.started) {
194
+ toolCall.arguments += delta.toolUse.input
192
195
  yield {
193
196
  type: EventType.TOOL_CALL_ARGS,
194
197
  toolCallId: toolCall.id,
@@ -295,6 +298,7 @@ export async function* processConverseStream(
295
298
  yield {
296
299
  type: EventType.TOOL_CALL_END,
297
300
  toolCallId: toolCall.id,
301
+ args: toolCall.arguments,
298
302
  toolCallName: toolCall.name,
299
303
  toolName: toolCall.name,
300
304
  timestamp: Date.now(),
@@ -337,6 +341,7 @@ export async function* processConverseStream(
337
341
  yield {
338
342
  type: EventType.TOOL_CALL_END,
339
343
  toolCallId: toolCall.id,
344
+ args: toolCall.arguments,
340
345
  toolCallName: toolCall.name,
341
346
  toolName: toolCall.name,
342
347
  timestamp: Date.now(),