@librechat/agents 3.1.34 → 3.1.35

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message_outputs.mjs","sources":["../../../../../src/llm/bedrock/utils/message_outputs.ts"],"sourcesContent":["/**\n * Utility functions for converting Bedrock Converse responses to LangChain messages.\n * Ported from @langchain/aws common.js\n */\nimport { AIMessage, AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type {\n BedrockMessage,\n ConverseResponse,\n ContentBlockDeltaEvent,\n ConverseStreamMetadataEvent,\n ContentBlockStartEvent,\n ReasoningContentBlock,\n ReasoningContentBlockDelta,\n MessageContentReasoningBlock,\n MessageContentReasoningBlockReasoningTextPartial,\n MessageContentReasoningBlockRedacted,\n} from '../types';\n\n/**\n * Convert a Bedrock reasoning block delta to a LangChain partial reasoning block.\n */\nexport function bedrockReasoningDeltaToLangchainPartialReasoningBlock(\n reasoningContent: ReasoningContentBlockDelta\n):\n | MessageContentReasoningBlockReasoningTextPartial\n | MessageContentReasoningBlockRedacted {\n const { text, redactedContent, signature } =\n reasoningContent as ReasoningContentBlockDelta & {\n text?: string;\n redactedContent?: Uint8Array;\n signature?: string;\n };\n\n if (typeof text === 'string') {\n return {\n type: 'reasoning_content',\n reasoningText: { text },\n };\n }\n if (signature != null) {\n return {\n type: 'reasoning_content',\n reasoningText: { signature },\n };\n }\n if (redactedContent != null) {\n return {\n type: 'reasoning_content',\n redactedContent: Buffer.from(redactedContent).toString('base64'),\n };\n }\n throw new Error('Invalid reasoning content');\n}\n\n/**\n * Convert a Bedrock reasoning block to a LangChain reasoning block.\n */\nexport function bedrockReasoningBlockToLangchainReasoningBlock(\n reasoningContent: ReasoningContentBlock\n): MessageContentReasoningBlock {\n const { reasoningText, redactedContent } =\n reasoningContent as ReasoningContentBlock & {\n reasoningText?: { text?: string; signature?: string };\n redactedContent?: Uint8Array;\n };\n\n if (reasoningText != null) {\n return {\n type: 'reasoning_content',\n reasoningText: reasoningText,\n };\n }\n if (redactedContent != null) {\n return {\n type: 'reasoning_content',\n redactedContent: Buffer.from(redactedContent).toString('base64'),\n };\n }\n throw new Error('Invalid reasoning content');\n}\n\n/**\n * Convert a Bedrock Converse message to a LangChain message.\n */\nexport function convertConverseMessageToLangChainMessage(\n message: BedrockMessage,\n responseMetadata: Omit<ConverseResponse, 'output'>\n): AIMessage {\n if (message.content == null) {\n throw new Error('No message content found in response.');\n }\n if (message.role !== 'assistant') {\n throw new Error(\n `Unsupported message role received in ChatBedrockConverse response: ${message.role}`\n );\n }\n\n let requestId: string | undefined;\n if (\n '$metadata' in responseMetadata &&\n responseMetadata.$metadata != null &&\n typeof responseMetadata.$metadata === 'object' &&\n 'requestId' in responseMetadata.$metadata\n ) {\n requestId = responseMetadata.$metadata.requestId as string;\n }\n\n let tokenUsage:\n | { input_tokens: number; output_tokens: number; total_tokens: number }\n | undefined;\n if (responseMetadata.usage != null) {\n const input_tokens = responseMetadata.usage.inputTokens ?? 0;\n const output_tokens = responseMetadata.usage.outputTokens ?? 0;\n tokenUsage = {\n input_tokens,\n output_tokens,\n total_tokens:\n responseMetadata.usage.totalTokens ?? input_tokens + output_tokens,\n };\n }\n\n if (\n message.content.length === 1 &&\n 'text' in message.content[0] &&\n typeof message.content[0].text === 'string'\n ) {\n return new AIMessage({\n content: message.content[0].text,\n response_metadata: responseMetadata,\n usage_metadata: tokenUsage,\n id: requestId,\n });\n } else {\n const toolCalls: Array<{\n id?: string;\n name: string;\n args: Record<string, unknown>;\n type: 'tool_call';\n }> = [];\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const content: any[] = [];\n\n message.content.forEach((c) => {\n if (\n 'toolUse' in c &&\n c.toolUse != null &&\n c.toolUse.name != null &&\n c.toolUse.name !== '' &&\n c.toolUse.input != null &&\n typeof c.toolUse.input === 'object'\n ) {\n toolCalls.push({\n id: c.toolUse.toolUseId,\n name: c.toolUse.name,\n args: c.toolUse.input as Record<string, unknown>,\n type: 'tool_call',\n });\n } else if ('text' in c && typeof c.text === 'string') {\n content.push({ type: 'text', text: c.text });\n } else if ('reasoningContent' in c && c.reasoningContent != null) {\n content.push(\n bedrockReasoningBlockToLangchainReasoningBlock(c.reasoningContent)\n );\n } else {\n content.push(c);\n }\n });\n\n return new AIMessage({\n content: content.length ? content : '',\n tool_calls: toolCalls.length ? toolCalls : undefined,\n response_metadata: responseMetadata,\n usage_metadata: tokenUsage,\n id: requestId,\n });\n }\n}\n\n/**\n * Handle a content block delta event from Bedrock Converse stream.\n */\nexport function handleConverseStreamContentBlockDelta(\n contentBlockDelta: ContentBlockDeltaEvent\n): ChatGenerationChunk {\n if (contentBlockDelta.delta == null) {\n throw new Error('No delta found in content block.');\n }\n\n if (typeof contentBlockDelta.delta.text === 'string') {\n return new ChatGenerationChunk({\n text: contentBlockDelta.delta.text,\n message: new AIMessageChunk({\n content: contentBlockDelta.delta.text,\n response_metadata: {\n contentBlockIndex: contentBlockDelta.contentBlockIndex,\n },\n }),\n });\n } else if (contentBlockDelta.delta.toolUse != null) {\n const index = contentBlockDelta.contentBlockIndex;\n return new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n tool_call_chunks: [\n {\n args: contentBlockDelta.delta.toolUse.input as string,\n index,\n type: 'tool_call_chunk',\n },\n ],\n response_metadata: {\n contentBlockIndex: contentBlockDelta.contentBlockIndex,\n },\n }),\n });\n } else if (contentBlockDelta.delta.reasoningContent != null) {\n const reasoningBlock =\n bedrockReasoningDeltaToLangchainPartialReasoningBlock(\n contentBlockDelta.delta.reasoningContent\n );\n // Extract the text for additional_kwargs.reasoning_content (for stream handler compatibility)\n const reasoningText =\n 'reasoningText' in reasoningBlock\n ? (reasoningBlock.reasoningText.text ??\n reasoningBlock.reasoningText.signature ??\n ('redactedContent' in reasoningBlock\n ? reasoningBlock.redactedContent\n : ''))\n : '';\n return new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: [reasoningBlock],\n additional_kwargs: {\n // Set reasoning_content for stream handler to detect reasoning mode\n reasoning_content: reasoningText,\n },\n response_metadata: {\n contentBlockIndex: contentBlockDelta.contentBlockIndex,\n },\n }),\n });\n } else {\n throw new Error(\n `Unsupported content block type(s): ${JSON.stringify(contentBlockDelta.delta, null, 2)}`\n );\n }\n}\n\n/**\n * Handle a content block start event from Bedrock Converse stream.\n */\nexport function handleConverseStreamContentBlockStart(\n contentBlockStart: ContentBlockStartEvent\n): ChatGenerationChunk | null {\n const index = contentBlockStart.contentBlockIndex;\n\n if (contentBlockStart.start?.toolUse != null) {\n return new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n tool_call_chunks: [\n {\n name: contentBlockStart.start.toolUse.name,\n id: contentBlockStart.start.toolUse.toolUseId,\n index,\n type: 'tool_call_chunk',\n },\n ],\n response_metadata: {\n contentBlockIndex: index,\n },\n }),\n });\n }\n\n // Return null for non-tool content block starts (text blocks don't need special handling)\n return null;\n}\n\n/**\n * Handle a metadata event from Bedrock Converse stream.\n */\nexport function handleConverseStreamMetadata(\n metadata: ConverseStreamMetadataEvent,\n extra: { streamUsage: boolean }\n): ChatGenerationChunk {\n const inputTokens = metadata.usage?.inputTokens ?? 0;\n const outputTokens = metadata.usage?.outputTokens ?? 0;\n const usage_metadata = {\n input_tokens: inputTokens,\n output_tokens: outputTokens,\n total_tokens: metadata.usage?.totalTokens ?? inputTokens + outputTokens,\n };\n\n return new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n usage_metadata: extra.streamUsage ? usage_metadata : undefined,\n response_metadata: {\n // Use the same key as returned from the Converse API\n metadata,\n },\n }),\n });\n}\n"],"names":[],"mappings":";;;AAAA;;;AAGG;AAgBH;;AAEG;AACG,SAAU,qDAAqD,CACnE,gBAA4C,EAAA;IAI5C,MAAM,EAAE,IAAI,EAAE,eAAe,EAAE,SAAS,EAAE,GACxC,gBAIC;AAEH,IAAA,IAA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,CAAC;AACJ;;;;"}
@@ -102,6 +102,9 @@ function modifyDeltaProperties(provider, obj) {
102
102
  }
103
103
  if (obj.lc_kwargs &&
104
104
  Array.isArray(obj.lc_kwargs.content)) {
105
+ if (provider === Providers.BEDROCK) {
106
+ obj.lc_kwargs.content = reduceBlocks(obj.lc_kwargs.content);
107
+ }
105
108
  obj.lc_kwargs.content = modifyContent({
106
109
  provider,
107
110
  messageType,
@@ -1 +1 @@
1
- {"version":3,"file":"core.mjs","sources":["../../../src/messages/core.ts"],"sourcesContent":["// src/messages.ts\nimport {\n AIMessageChunk,\n HumanMessage,\n ToolMessage,\n AIMessage,\n BaseMessage,\n} from '@langchain/core/messages';\nimport type { ToolCall } from '@langchain/core/messages/tool';\nimport type * as t from '@/types';\nimport { Providers } from '@/common';\n\nexport function getConverseOverrideMessage({\n userMessage,\n lastMessageX,\n lastMessageY,\n}: {\n userMessage: string[];\n lastMessageX: AIMessageChunk | null;\n lastMessageY: ToolMessage;\n}): HumanMessage {\n const content = `\nUser: ${userMessage[1]}\n\n---\n# YOU HAVE ALREADY RESPONDED TO THE LATEST USER MESSAGE:\n\n# Observations:\n- ${lastMessageX?.content}\n\n# Tool Calls:\n- ${lastMessageX?.tool_calls?.join('\\n- ')}\n\n# Tool Responses:\n- ${lastMessageY.content}\n`;\n\n return new HumanMessage(content);\n}\n\nconst _allowedTypes = ['image_url', 'text', 'tool_use', 'tool_result'];\nconst allowedTypesByProvider: Record<string, string[]> = {\n default: _allowedTypes,\n [Providers.ANTHROPIC]: [..._allowedTypes, 'thinking', 'redacted_thinking'],\n [Providers.BEDROCK]: [..._allowedTypes, 'reasoning_content'],\n [Providers.OPENAI]: _allowedTypes,\n};\n\nconst modifyContent = ({\n provider,\n messageType,\n content,\n}: {\n provider: Providers;\n messageType: string;\n content: t.ExtendedMessageContent[];\n}): t.ExtendedMessageContent[] => {\n const allowedTypes =\n allowedTypesByProvider[provider] ?? allowedTypesByProvider.default;\n return content.map((item) => {\n if (\n item &&\n typeof item === 'object' &&\n 'type' in item &&\n item.type != null &&\n item.type\n ) {\n let newType = item.type;\n if (newType.endsWith('_delta')) {\n newType = newType.replace('_delta', '');\n }\n if (!allowedTypes.includes(newType)) {\n newType = 'text';\n }\n\n /* Handle the edge case for empty object 'tool_use' input in AI messages */\n if (\n messageType === 'ai' &&\n newType === 'tool_use' &&\n 'input' in item &&\n item.input === ''\n ) {\n return { ...item, type: newType, input: '{}' };\n }\n\n return { ...item, type: newType };\n }\n return item;\n });\n};\n\ntype ContentBlock =\n | Partial<t.BedrockReasoningContentText>\n | t.MessageDeltaUpdate;\n\nfunction reduceBlocks(blocks: ContentBlock[]): ContentBlock[] {\n const reduced: ContentBlock[] = [];\n\n for (const block of blocks) {\n const lastBlock = reduced[reduced.length - 1] as ContentBlock | undefined;\n\n // Merge consecutive 'reasoning_content'\n if (\n block.type === 'reasoning_content' &&\n lastBlock?.type === 'reasoning_content'\n ) {\n // append text if exists\n if (block.reasoningText?.text != null && block.reasoningText.text) {\n (\n lastBlock.reasoningText as t.BedrockReasoningContentText['reasoningText']\n ).text =\n (lastBlock.reasoningText?.text ?? '') + block.reasoningText.text;\n }\n // preserve the signature if exists\n if (\n block.reasoningText?.signature != null &&\n block.reasoningText.signature\n ) {\n (\n lastBlock.reasoningText as t.BedrockReasoningContentText['reasoningText']\n ).signature = block.reasoningText.signature;\n }\n }\n // Merge consecutive 'text'\n else if (block.type === 'text' && lastBlock?.type === 'text') {\n lastBlock.text += block.text;\n }\n // add a new block as it's a different type or first element\n else {\n // deep copy to avoid mutation of original\n reduced.push(JSON.parse(JSON.stringify(block)));\n }\n }\n\n return reduced;\n}\n\nexport function modifyDeltaProperties(\n provider: Providers,\n obj?: AIMessageChunk\n): AIMessageChunk | undefined {\n if (!obj || typeof obj !== 'object') return obj;\n\n const messageType = (obj as Partial<AIMessageChunk>)._getType\n ? obj._getType()\n : '';\n\n if (provider === Providers.BEDROCK && Array.isArray(obj.content)) {\n obj.content = reduceBlocks(obj.content as ContentBlock[]);\n }\n if (Array.isArray(obj.content)) {\n obj.content = modifyContent({\n provider,\n messageType,\n content: obj.content,\n });\n }\n if (\n (obj as Partial<AIMessageChunk>).lc_kwargs &&\n Array.isArray(obj.lc_kwargs.content)\n ) {\n obj.lc_kwargs.content = modifyContent({\n provider,\n messageType,\n content: obj.lc_kwargs.content,\n });\n }\n return obj;\n}\n\nexport function formatAnthropicMessage(message: AIMessageChunk): AIMessage {\n if (!message.tool_calls || message.tool_calls.length === 0) {\n return new AIMessage({ content: message.content });\n }\n\n const toolCallMap = new Map(message.tool_calls.map((tc) => [tc.id, tc]));\n let formattedContent: string | t.ExtendedMessageContent[];\n\n if (Array.isArray(message.content)) {\n formattedContent = message.content.reduce<t.ExtendedMessageContent[]>(\n (acc, item) => {\n if (typeof item === 'object') {\n const extendedItem = item as t.ExtendedMessageContent;\n if (\n extendedItem.type === 'text' &&\n extendedItem.text != null &&\n extendedItem.text\n ) {\n acc.push({ type: 'text', text: extendedItem.text });\n } else if (\n extendedItem.type === 'tool_use' &&\n extendedItem.id != null &&\n extendedItem.id\n ) {\n const toolCall = toolCallMap.get(extendedItem.id);\n if (toolCall) {\n acc.push({\n type: 'tool_use',\n id: extendedItem.id,\n name: toolCall.name,\n input: toolCall.args as unknown as string,\n });\n }\n } else if (\n 'input' in extendedItem &&\n extendedItem.input != null &&\n extendedItem.input\n ) {\n try {\n const parsedInput = JSON.parse(extendedItem.input);\n const toolCall = message.tool_calls?.find(\n (tc) => tc.args.input === parsedInput.input\n );\n if (toolCall) {\n acc.push({\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args as unknown as string,\n });\n }\n } catch {\n if (extendedItem.input) {\n acc.push({ type: 'text', text: extendedItem.input });\n }\n }\n }\n } else if (typeof item === 'string') {\n acc.push({ type: 'text', text: item });\n }\n return acc;\n },\n []\n );\n } else if (typeof message.content === 'string') {\n formattedContent = message.content;\n } else {\n formattedContent = [];\n }\n\n // const formattedToolCalls: ToolCall[] = message.tool_calls.map(toolCall => ({\n // id: toolCall.id ?? '',\n // name: toolCall.name,\n // args: toolCall.args,\n // type: 'tool_call',\n // }));\n\n const formattedToolCalls: t.AgentToolCall[] = message.tool_calls.map(\n (toolCall) => ({\n id: toolCall.id ?? '',\n type: 'function',\n function: {\n name: toolCall.name,\n arguments: toolCall.args,\n },\n })\n );\n\n return new AIMessage({\n content: formattedContent,\n tool_calls: formattedToolCalls as ToolCall[],\n additional_kwargs: {\n ...message.additional_kwargs,\n },\n });\n}\n\nexport function convertMessagesToContent(\n messages: BaseMessage[]\n): t.MessageContentComplex[] {\n const processedContent: t.MessageContentComplex[] = [];\n\n const addContentPart = (message: BaseMessage | null): void => {\n const content =\n message?.lc_kwargs.content != null\n ? message.lc_kwargs.content\n : message?.content;\n if (content === undefined) {\n return;\n }\n if (typeof content === 'string') {\n processedContent.push({\n type: 'text',\n text: content,\n });\n } else if (Array.isArray(content)) {\n const filteredContent = content.filter(\n (item) => item != null && item.type !== 'tool_use'\n );\n processedContent.push(...filteredContent);\n }\n };\n\n let currentAIMessageIndex = -1;\n const toolCallMap = new Map<string, t.CustomToolCall>();\n\n for (let i = 0; i < messages.length; i++) {\n const message = messages[i] as BaseMessage | null;\n const messageType = message?._getType();\n\n if (\n messageType === 'ai' &&\n ((message as AIMessage).tool_calls?.length ?? 0) > 0\n ) {\n const tool_calls = (message as AIMessage).tool_calls || [];\n for (const tool_call of tool_calls) {\n if (tool_call.id == null || !tool_call.id) {\n continue;\n }\n\n toolCallMap.set(tool_call.id, tool_call);\n }\n\n addContentPart(message);\n currentAIMessageIndex = processedContent.length - 1;\n continue;\n } else if (\n messageType === 'tool' &&\n (message as ToolMessage).tool_call_id\n ) {\n const id = (message as ToolMessage).tool_call_id;\n const output = (message as ToolMessage).content;\n const tool_call = toolCallMap.get(id);\n if (currentAIMessageIndex === -1) {\n processedContent.push({ type: 'text', text: '' });\n currentAIMessageIndex = processedContent.length - 1;\n }\n const contentPart = processedContent[currentAIMessageIndex];\n processedContent.push({\n type: 'tool_call',\n tool_call: Object.assign({}, tool_call, { output }),\n });\n const tool_call_ids = contentPart.tool_call_ids || [];\n tool_call_ids.push(id);\n contentPart.tool_call_ids = tool_call_ids;\n continue;\n } else if (messageType !== 'ai') {\n continue;\n }\n\n addContentPart(message);\n }\n\n return processedContent;\n}\n\nexport function formatAnthropicArtifactContent(messages: BaseMessage[]): void {\n const lastMessage = messages[messages.length - 1];\n if (!(lastMessage instanceof ToolMessage)) return;\n\n // Find the latest AIMessage with tool_calls that this tool message belongs to\n const latestAIParentIndex = findLastIndex(\n messages,\n (msg) =>\n (msg instanceof AIMessageChunk &&\n (msg.tool_calls?.length ?? 0) > 0 &&\n msg.tool_calls?.some((tc) => tc.id === lastMessage.tool_call_id)) ??\n false\n );\n\n if (latestAIParentIndex === -1) return;\n\n // Check if any tool message after the AI message has array artifact content\n const hasArtifactContent = messages.some(\n (msg, i) =>\n i > latestAIParentIndex &&\n msg instanceof ToolMessage &&\n msg.artifact != null &&\n msg.artifact?.content != null &&\n Array.isArray(msg.artifact.content)\n );\n\n if (!hasArtifactContent) return;\n\n const message = messages[latestAIParentIndex] as AIMessageChunk;\n const toolCallIds = message.tool_calls?.map((tc) => tc.id) ?? [];\n\n for (let j = latestAIParentIndex + 1; j < messages.length; j++) {\n const msg = messages[j];\n if (\n msg instanceof ToolMessage &&\n toolCallIds.includes(msg.tool_call_id) &&\n msg.artifact != null &&\n Array.isArray(msg.artifact?.content) &&\n Array.isArray(msg.content)\n ) {\n msg.content = msg.content.concat(msg.artifact.content);\n }\n }\n}\n\nexport function formatArtifactPayload(messages: BaseMessage[]): void {\n const lastMessageY = messages[messages.length - 1];\n if (!(lastMessageY instanceof ToolMessage)) return;\n\n // Find the latest AIMessage with tool_calls that this tool message belongs to\n const latestAIParentIndex = findLastIndex(\n messages,\n (msg) =>\n (msg instanceof AIMessageChunk &&\n (msg.tool_calls?.length ?? 0) > 0 &&\n msg.tool_calls?.some((tc) => tc.id === lastMessageY.tool_call_id)) ??\n false\n );\n\n if (latestAIParentIndex === -1) return;\n\n // Check if any tool message after the AI message has array artifact content\n const hasArtifactContent = messages.some(\n (msg, i) =>\n i > latestAIParentIndex &&\n msg instanceof ToolMessage &&\n msg.artifact != null &&\n msg.artifact?.content != null &&\n Array.isArray(msg.artifact.content)\n );\n\n if (!hasArtifactContent) return;\n\n // Collect all relevant tool messages and their artifacts\n const relevantMessages = messages\n .slice(latestAIParentIndex + 1)\n .filter((msg) => msg instanceof ToolMessage) as ToolMessage[];\n\n // Aggregate all content and artifacts\n const aggregatedContent: t.MessageContentComplex[] = [];\n\n relevantMessages.forEach((msg) => {\n if (!Array.isArray(msg.artifact?.content)) {\n return;\n }\n let currentContent = msg.content;\n if (!Array.isArray(currentContent)) {\n currentContent = [\n {\n type: 'text',\n text: msg.content,\n },\n ];\n }\n aggregatedContent.push(...currentContent);\n msg.content =\n 'Tool response is included in the next message as a Human message';\n aggregatedContent.push(...msg.artifact.content);\n });\n\n // Add single HumanMessage with all aggregated content\n if (aggregatedContent.length > 0) {\n messages.push(new HumanMessage({ content: aggregatedContent }));\n }\n}\n\nexport function findLastIndex<T>(\n array: T[],\n predicate: (value: T) => boolean\n): number {\n for (let i = array.length - 1; i >= 0; i--) {\n if (predicate(array[i])) {\n return i;\n }\n }\n return -1;\n}\n"],"names":[],"mappings":";;;AAAA;AAYM,SAAU,0BAA0B,CAAC,EACzC,WAAW,EACX,YAAY,EACZ,YAAY,GAKb,EAAA;AACC,IAAA,MAAM,OAAO,GAAG;QACV,WAAW,CAAC,CAAC,CAAC;;;;;;AAMlB,EAAA,EAAA,YAAY,EAAE,OAAO;;;AAGrB,EAAA,EAAA,YAAY,EAAE,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC;;;AAGtC,EAAA,EAAA,YAAY,CAAC,OAAO;CACvB;AAEC,IAAA,OAAO,IAAI,YAAY,CAAC,OAAO,CAAC;AAClC;AAEA,MAAM,aAAa,GAAG,CAAC,WAAW,EAAE,MAAM,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M,iBAAiB,GAA8B,EAAE;AAEvD,IAAA,gBAAgB,CAAC,OAAO,CAAC,CAAC,GAAG,KAAI;AAC/B,QAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,EAAE;YACzC;;AAEF,QAAA,IAAI,cAAc,GAAG,GAAG,CAAC,OAAO;QAChC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;AAClC,YAAA,cAAc,GAAG;AACf,gBAAA;AACE,oBAAA,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,GAAG,CAAC,OAAO;AAClB,iBAAA;aACF;;AAEH,QAAA,iBAAiB,CAAC,IAAI,CAAC,GAAG,cAAc,CAAC;AACzC,QAAA,GAAG,CAAC,OAAO;AACT,YAAA,kEAAkE;QACpE,iBAAiB,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC;AACjD,KAAC,CAAC;;AAGF,IAAA,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;AAChC,QAAA,QAAQ,CAAC,IAAI,CAAC,IAAI,YAAY,CAAC,EAAE,OAAO,EAAE,iBAAiB,EAAE,CAAC,CAAC;;AAEnE;AAEgB,SAAA,aAAa,CAC3B,KAAU,EACV,SAAgC,EAAA;AAEhC,IAAA,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;QAC1C,IAAI,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;AACvB,YAAA,OAAO,CAAC;;;IAGZ,OAAO,EAAE;AACX;;;;"}
1
+ {"version":3,"file":"core.mjs","sources":["../../../src/messages/core.ts"],"sourcesContent":["// src/messages.ts\nimport {\n AIMessageChunk,\n HumanMessage,\n ToolMessage,\n AIMessage,\n BaseMessage,\n} from '@langchain/core/messages';\nimport type { ToolCall } from '@langchain/core/messages/tool';\nimport type * as t from '@/types';\nimport { Providers } from '@/common';\n\nexport function getConverseOverrideMessage({\n userMessage,\n lastMessageX,\n lastMessageY,\n}: {\n userMessage: string[];\n lastMessageX: AIMessageChunk | null;\n lastMessageY: ToolMessage;\n}): HumanMessage {\n const content = `\nUser: ${userMessage[1]}\n\n---\n# YOU HAVE ALREADY RESPONDED TO THE LATEST USER MESSAGE:\n\n# Observations:\n- ${lastMessageX?.content}\n\n# Tool Calls:\n- ${lastMessageX?.tool_calls?.join('\\n- ')}\n\n# Tool Responses:\n- ${lastMessageY.content}\n`;\n\n return new HumanMessage(content);\n}\n\nconst _allowedTypes = ['image_url', 'text', 'tool_use', 'tool_result'];\nconst allowedTypesByProvider: Record<string, string[]> = {\n default: _allowedTypes,\n [Providers.ANTHROPIC]: [..._allowedTypes, 'thinking', 'redacted_thinking'],\n [Providers.BEDROCK]: [..._allowedTypes, 'reasoning_content'],\n [Providers.OPENAI]: _allowedTypes,\n};\n\nconst modifyContent = ({\n provider,\n messageType,\n content,\n}: {\n provider: Providers;\n messageType: string;\n content: t.ExtendedMessageContent[];\n}): t.ExtendedMessageContent[] => {\n const allowedTypes =\n allowedTypesByProvider[provider] ?? allowedTypesByProvider.default;\n return content.map((item) => {\n if (\n item &&\n typeof item === 'object' &&\n 'type' in item &&\n item.type != null &&\n item.type\n ) {\n let newType = item.type;\n if (newType.endsWith('_delta')) {\n newType = newType.replace('_delta', '');\n }\n if (!allowedTypes.includes(newType)) {\n newType = 'text';\n }\n\n /* Handle the edge case for empty object 'tool_use' input in AI messages */\n if (\n messageType === 'ai' &&\n newType === 'tool_use' &&\n 'input' in item &&\n item.input === ''\n ) {\n return { ...item, type: newType, input: '{}' };\n }\n\n return { ...item, type: newType };\n }\n return item;\n });\n};\n\ntype ContentBlock =\n | Partial<t.BedrockReasoningContentText>\n | t.MessageDeltaUpdate;\n\nfunction reduceBlocks(blocks: ContentBlock[]): ContentBlock[] {\n const reduced: ContentBlock[] = [];\n\n for (const block of blocks) {\n const lastBlock = reduced[reduced.length - 1] as ContentBlock | undefined;\n\n // Merge consecutive 'reasoning_content'\n if (\n block.type === 'reasoning_content' &&\n lastBlock?.type === 'reasoning_content'\n ) {\n // append text if exists\n if (block.reasoningText?.text != null && block.reasoningText.text) {\n (\n lastBlock.reasoningText as t.BedrockReasoningContentText['reasoningText']\n ).text =\n (lastBlock.reasoningText?.text ?? '') + block.reasoningText.text;\n }\n // preserve the signature if exists\n if (\n block.reasoningText?.signature != null &&\n block.reasoningText.signature\n ) {\n (\n lastBlock.reasoningText as t.BedrockReasoningContentText['reasoningText']\n ).signature = block.reasoningText.signature;\n }\n }\n // Merge consecutive 'text'\n else if (block.type === 'text' && lastBlock?.type === 'text') {\n lastBlock.text += block.text;\n }\n // add a new block as it's a different type or first element\n else {\n // deep copy to avoid mutation of original\n reduced.push(JSON.parse(JSON.stringify(block)));\n }\n }\n\n return reduced;\n}\n\nexport function modifyDeltaProperties(\n provider: Providers,\n obj?: AIMessageChunk\n): AIMessageChunk | undefined {\n if (!obj || typeof obj !== 'object') return obj;\n\n const messageType = (obj as Partial<AIMessageChunk>)._getType\n ? obj._getType()\n : '';\n\n if (provider === Providers.BEDROCK && Array.isArray(obj.content)) {\n obj.content = reduceBlocks(obj.content as ContentBlock[]);\n }\n if (Array.isArray(obj.content)) {\n obj.content = modifyContent({\n provider,\n messageType,\n content: obj.content,\n });\n }\n if (\n (obj as Partial<AIMessageChunk>).lc_kwargs &&\n Array.isArray(obj.lc_kwargs.content)\n ) {\n if (provider === Providers.BEDROCK) {\n obj.lc_kwargs.content = reduceBlocks(\n obj.lc_kwargs.content as ContentBlock[]\n );\n }\n obj.lc_kwargs.content = modifyContent({\n provider,\n messageType,\n content: obj.lc_kwargs.content,\n });\n }\n return obj;\n}\n\nexport function formatAnthropicMessage(message: AIMessageChunk): AIMessage {\n if (!message.tool_calls || message.tool_calls.length === 0) {\n return new AIMessage({ content: message.content });\n }\n\n const toolCallMap = new Map(message.tool_calls.map((tc) => [tc.id, tc]));\n let formattedContent: string | t.ExtendedMessageContent[];\n\n if (Array.isArray(message.content)) {\n formattedContent = message.content.reduce<t.ExtendedMessageContent[]>(\n (acc, item) => {\n if (typeof item === 'object') {\n const extendedItem = item as t.ExtendedMessageContent;\n if (\n extendedItem.type === 'text' &&\n extendedItem.text != null &&\n extendedItem.text\n ) {\n acc.push({ type: 'text', text: extendedItem.text });\n } else if (\n extendedItem.type === 'tool_use' &&\n extendedItem.id != null &&\n extendedItem.id\n ) {\n const toolCall = toolCallMap.get(extendedItem.id);\n if (toolCall) {\n acc.push({\n type: 'tool_use',\n id: extendedItem.id,\n name: toolCall.name,\n input: toolCall.args as unknown as string,\n });\n }\n } else if (\n 'input' in extendedItem &&\n extendedItem.input != null &&\n extendedItem.input\n ) {\n try {\n const parsedInput = JSON.parse(extendedItem.input);\n const toolCall = message.tool_calls?.find(\n (tc) => tc.args.input === parsedInput.input\n );\n if (toolCall) {\n acc.push({\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args as unknown as string,\n });\n }\n } catch {\n if (extendedItem.input) {\n acc.push({ type: 'text', text: extendedItem.input });\n }\n }\n }\n } else if (typeof item === 'string') {\n acc.push({ type: 'text', text: item });\n }\n return acc;\n },\n []\n );\n } else if (typeof message.content === 'string') {\n formattedContent = message.content;\n } else {\n formattedContent = [];\n }\n\n // const formattedToolCalls: ToolCall[] = message.tool_calls.map(toolCall => ({\n // id: toolCall.id ?? '',\n // name: toolCall.name,\n // args: toolCall.args,\n // type: 'tool_call',\n // }));\n\n const formattedToolCalls: t.AgentToolCall[] = message.tool_calls.map(\n (toolCall) => ({\n id: toolCall.id ?? '',\n type: 'function',\n function: {\n name: toolCall.name,\n arguments: toolCall.args,\n },\n })\n );\n\n return new AIMessage({\n content: formattedContent,\n tool_calls: formattedToolCalls as ToolCall[],\n additional_kwargs: {\n ...message.additional_kwargs,\n },\n });\n}\n\nexport function convertMessagesToContent(\n messages: BaseMessage[]\n): t.MessageContentComplex[] {\n const processedContent: t.MessageContentComplex[] = [];\n\n const addContentPart = (message: BaseMessage | null): void => {\n const content =\n message?.lc_kwargs.content != null\n ? message.lc_kwargs.content\n : message?.content;\n if (content === undefined) {\n return;\n }\n if (typeof content === 'string') {\n processedContent.push({\n type: 'text',\n text: content,\n });\n } else if (Array.isArray(content)) {\n const filteredContent = content.filter(\n (item) => item != null && item.type !== 'tool_use'\n );\n processedContent.push(...filteredContent);\n }\n };\n\n let currentAIMessageIndex = -1;\n const toolCallMap = new Map<string, t.CustomToolCall>();\n\n for (let i = 0; i < messages.length; i++) {\n const message = messages[i] as BaseMessage | null;\n const messageType = message?._getType();\n\n if (\n messageType === 'ai' &&\n ((message as AIMessage).tool_calls?.length ?? 0) > 0\n ) {\n const tool_calls = (message as AIMessage).tool_calls || [];\n for (const tool_call of tool_calls) {\n if (tool_call.id == null || !tool_call.id) {\n continue;\n }\n\n toolCallMap.set(tool_call.id, tool_call);\n }\n\n addContentPart(message);\n currentAIMessageIndex = processedContent.length - 1;\n continue;\n } else if (\n messageType === 'tool' &&\n (message as ToolMessage).tool_call_id\n ) {\n const id = (message as ToolMessage).tool_call_id;\n const output = (message as ToolMessage).content;\n const tool_call = toolCallMap.get(id);\n if (currentAIMessageIndex === -1) {\n processedContent.push({ type: 'text', text: '' });\n currentAIMessageIndex = processedContent.length - 1;\n }\n const contentPart = processedContent[currentAIMessageIndex];\n processedContent.push({\n type: 'tool_call',\n tool_call: Object.assign({}, tool_call, { output }),\n });\n const tool_call_ids = contentPart.tool_call_ids || [];\n tool_call_ids.push(id);\n contentPart.tool_call_ids = tool_call_ids;\n continue;\n } else if (messageType !== 'ai') {\n continue;\n }\n\n addContentPart(message);\n }\n\n return processedContent;\n}\n\nexport function formatAnthropicArtifactContent(messages: BaseMessage[]): void {\n const lastMessage = messages[messages.length - 1];\n if (!(lastMessage instanceof ToolMessage)) return;\n\n // Find the latest AIMessage with tool_calls that this tool message belongs to\n const latestAIParentIndex = findLastIndex(\n messages,\n (msg) =>\n (msg instanceof AIMessageChunk &&\n (msg.tool_calls?.length ?? 0) > 0 &&\n msg.tool_calls?.some((tc) => tc.id === lastMessage.tool_call_id)) ??\n false\n );\n\n if (latestAIParentIndex === -1) return;\n\n // Check if any tool message after the AI message has array artifact content\n const hasArtifactContent = messages.some(\n (msg, i) =>\n i > latestAIParentIndex &&\n msg instanceof ToolMessage &&\n msg.artifact != null &&\n msg.artifact?.content != null &&\n Array.isArray(msg.artifact.content)\n );\n\n if (!hasArtifactContent) return;\n\n const message = messages[latestAIParentIndex] as AIMessageChunk;\n const toolCallIds = message.tool_calls?.map((tc) => tc.id) ?? [];\n\n for (let j = latestAIParentIndex + 1; j < messages.length; j++) {\n const msg = messages[j];\n if (\n msg instanceof ToolMessage &&\n toolCallIds.includes(msg.tool_call_id) &&\n msg.artifact != null &&\n Array.isArray(msg.artifact?.content) &&\n Array.isArray(msg.content)\n ) {\n msg.content = msg.content.concat(msg.artifact.content);\n }\n }\n}\n\nexport function formatArtifactPayload(messages: BaseMessage[]): void {\n const lastMessageY = messages[messages.length - 1];\n if (!(lastMessageY instanceof ToolMessage)) return;\n\n // Find the latest AIMessage with tool_calls that this tool message belongs to\n const latestAIParentIndex = findLastIndex(\n messages,\n (msg) =>\n (msg instanceof AIMessageChunk &&\n (msg.tool_calls?.length ?? 0) > 0 &&\n msg.tool_calls?.some((tc) => tc.id === lastMessageY.tool_call_id)) ??\n false\n );\n\n if (latestAIParentIndex === -1) return;\n\n // Check if any tool message after the AI message has array artifact content\n const hasArtifactContent = messages.some(\n (msg, i) =>\n i > latestAIParentIndex &&\n msg instanceof ToolMessage &&\n msg.artifact != null &&\n msg.artifact?.content != null &&\n Array.isArray(msg.artifact.content)\n );\n\n if (!hasArtifactContent) return;\n\n // Collect all relevant tool messages and their artifacts\n const relevantMessages = messages\n .slice(latestAIParentIndex + 1)\n .filter((msg) => msg instanceof ToolMessage) as ToolMessage[];\n\n // Aggregate all content and artifacts\n const aggregatedContent: t.MessageContentComplex[] = [];\n\n relevantMessages.forEach((msg) => {\n if (!Array.isArray(msg.artifact?.content)) {\n return;\n }\n let currentContent = msg.content;\n if (!Array.isArray(currentContent)) {\n currentContent = [\n {\n type: 'text',\n text: msg.content,\n },\n ];\n }\n aggregatedContent.push(...currentContent);\n msg.content =\n 'Tool response is included in the next message as a Human message';\n aggregatedContent.push(...msg.artifact.content);\n });\n\n // Add single HumanMessage with all aggregated content\n if (aggregatedContent.length > 0) {\n messages.push(new HumanMessage({ content: aggregatedContent }));\n }\n}\n\nexport function findLastIndex<T>(\n array: T[],\n predicate: (value: T) => boolean\n): number {\n for (let i = array.length - 1; i >= 0; i--) {\n if (predicate(array[i])) {\n return i;\n }\n }\n return -1;\n}\n"],"names":[],"mappings":";;;AAAA;AAYM,SAAU,0BAA0B,CAAC,EACzC,WAAW,EACX,YAAY,EACZ,YAAY,GAKb,EAAA;AACC,IAAA,MAAM,OAAO,GAAG;QACV,WAAW,CAAC,CAAC,CAAC;;;;;;AAMlB,EAAA,EAAA,YAAY,EAAE,OAAO;;;AAGrB,EAAA,EAAA,YAAY,EAAE,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC;;;AAGtC,EAAA,EAAA,YAAY,CAAC,OAAO;CACvB;AAEC,IAAA,OAAO,IAAI,YAAY,CAAC,OAAO,CAAC;AAClC;AAEA,MAAM,aAAa,GAAG,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,CAAC;AACtE,MAAM,sBAAsB,GAA6B;AACvD,IAAA,OAAO,EAAE,aAAa;AACtB,IAAA,CAAC,SAAS,CAAC,SAAS,GAAG,CAAC,GAAG,aAAa,EAAE,UAAU,EAAE,mBAAmB,CAAC;IAC1E,CAAC,SAAS,CAAC,OAAO,GAAG,CAAC,GAAG,aAAa,EAAE,mBAAmB,CAAC;AAC5D,IAAA,CAAC,SAAS,CAAC,MAAM,GAAG,aAAa;CAClC;AAED,MAAM,aAAa,GAAG,CAAC,EACrB,QAAQ,EACR,W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GAAG,GAAG,CAAC,OAAO;QAChC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;AAClC,YAAA,cAAc,GAAG;AACf,gBAAA;AACE,oBAAA,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,GAAG,CAAC,OAAO;AAClB,iBAAA;aACF;;AAEH,QAAA,iBAAiB,CAAC,IAAI,CAAC,GAAG,cAAc,CAAC;AACzC,QAAA,GAAG,CAAC,OAAO;AACT,YAAA,kEAAkE;QACpE,iBAAiB,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC;AACjD,KAAC,CAAC;;AAGF,IAAA,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;AAChC,QAAA,QAAQ,CAAC,IAAI,CAAC,IAAI,YAAY,CAAC,EAAE,OAAO,EAAE,iBAAiB,EAAE,CAAC,CAAC;;AAEnE;AAEgB,SAAA,aAAa,CAC3B,KAAU,EACV,SAAgC,EAAA;AAEhC,IAAA,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;QAC1C,IAAI,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;AACvB,YAAA,OAAO,CAAC;;;IAGZ,OAAO,EAAE;AACX;;;;"}
@@ -1,18 +1,24 @@
1
1
  /**
2
- * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
3
- * and adds support for latest @langchain/aws features:
2
+ * Optimized ChatBedrockConverse wrapper that fixes content block merging for
3
+ * streaming responses and adds support for latest @langchain/aws features:
4
4
  *
5
5
  * - Application Inference Profiles (PR #9129)
6
6
  * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+
7
7
  *
8
- * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
9
- * causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
10
- * errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid
11
- * the conflict.
8
+ * Bedrock's `@langchain/aws` library does not include an `index` property on content
9
+ * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append
10
+ * each streaming chunk as a separate array entry instead of merging by index.
12
11
  *
13
- * The contentBlockIndex field is only used internally by Bedrock's streaming protocol
14
- * and isn't needed by application logic - the index field on tool_call_chunks serves
15
- * the purpose of tracking tool call ordering.
12
+ * This wrapper takes full ownership of the stream by directly interfacing with the
13
+ * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that
14
+ * include `contentBlockIndex` in response_metadata for every delta type. It then
15
+ * promotes `contentBlockIndex` to an `index` property on each content block
16
+ * (mirroring Anthropic's pattern) and strips it from metadata to avoid
17
+ * `_mergeDicts` conflicts.
18
+ *
19
+ * When multiple content block types are present (e.g. reasoning + text), text deltas
20
+ * are promoted from strings to array form with `index` so they merge correctly once
21
+ * the accumulated content is already an array.
16
22
  */
17
23
  import { ChatBedrockConverse } from '@langchain/aws';
18
24
  import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
@@ -91,25 +97,27 @@ export declare class CustomChatBedrockConverse extends ChatBedrockConverse {
91
97
  */
92
98
  _generateNonStreaming(messages: BaseMessage[], options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions, runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
93
99
  /**
94
- * Override _streamResponseChunks to:
95
- * 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)
96
- * 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts
100
+ * Own the stream end-to-end so we have direct access to every
101
+ * `contentBlockDelta.contentBlockIndex` from the AWS SDK.
97
102
  *
98
- * Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's
99
- * internal chunk handling which correctly preserves array content for reasoning blocks.
103
+ * This replaces the parent's implementation which strips contentBlockIndex
104
+ * from text and reasoning deltas, making it impossible to merge correctly.
100
105
  */
101
106
  _streamResponseChunks(messages: BaseMessage[], options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions, runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
102
107
  /**
103
- * Clean a chunk by removing contentBlockIndex from response_metadata.
104
- */
105
- private cleanChunk;
106
- /**
107
- * Check if contentBlockIndex exists at any level in the object
108
+ * Inject `index` on content blocks for proper merge behaviour, then strip
109
+ * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.
110
+ *
111
+ * Text string content is promoted to array form only when the stream contains
112
+ * multiple content block indices (e.g. reasoning at index 0, text at index 1),
113
+ * ensuring text merges correctly with the already-array accumulated content.
108
114
  */
109
- private hasContentBlockIndex;
115
+ private enrichChunk;
110
116
  /**
111
- * Recursively remove contentBlockIndex from all levels of an object
117
+ * Extract `contentBlockIndex` from the top level of response_metadata.
118
+ * Our custom handlers always place it at the top level.
112
119
  */
120
+ private extractContentBlockIndex;
113
121
  private removeContentBlockIndex;
114
122
  }
115
123
  export type { ChatBedrockConverseInput };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@librechat/agents",
3
- "version": "3.1.34",
3
+ "version": "3.1.35",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -1,26 +1,39 @@
1
1
  /**
2
- * Optimized ChatBedrockConverse wrapper that fixes contentBlockIndex conflicts
3
- * and adds support for latest @langchain/aws features:
2
+ * Optimized ChatBedrockConverse wrapper that fixes content block merging for
3
+ * streaming responses and adds support for latest @langchain/aws features:
4
4
  *
5
5
  * - Application Inference Profiles (PR #9129)
6
6
  * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+
7
7
  *
8
- * Bedrock sends the same contentBlockIndex for both text and tool_use content blocks,
9
- * causing LangChain's merge logic to fail with "field[contentBlockIndex] already exists"
10
- * errors. This wrapper simply strips contentBlockIndex from response_metadata to avoid
11
- * the conflict.
8
+ * Bedrock's `@langchain/aws` library does not include an `index` property on content
9
+ * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append
10
+ * each streaming chunk as a separate array entry instead of merging by index.
12
11
  *
13
- * The contentBlockIndex field is only used internally by Bedrock's streaming protocol
14
- * and isn't needed by application logic - the index field on tool_call_chunks serves
15
- * the purpose of tracking tool call ordering.
12
+ * This wrapper takes full ownership of the stream by directly interfacing with the
13
+ * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that
14
+ * include `contentBlockIndex` in response_metadata for every delta type. It then
15
+ * promotes `contentBlockIndex` to an `index` property on each content block
16
+ * (mirroring Anthropic's pattern) and strips it from metadata to avoid
17
+ * `_mergeDicts` conflicts.
18
+ *
19
+ * When multiple content block types are present (e.g. reasoning + text), text deltas
20
+ * are promoted from strings to array form with `index` so they merge correctly once
21
+ * the accumulated content is already an array.
16
22
  */
17
23
 
18
24
  import { ChatBedrockConverse } from '@langchain/aws';
25
+ import { ConverseStreamCommand } from '@aws-sdk/client-bedrock-runtime';
19
26
  import { AIMessageChunk } from '@langchain/core/messages';
20
27
  import { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';
21
28
  import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
22
29
  import type { ChatBedrockConverseInput } from '@langchain/aws';
23
30
  import type { BaseMessage } from '@langchain/core/messages';
31
+ import {
32
+ convertToConverseMessages,
33
+ handleConverseStreamContentBlockStart,
34
+ handleConverseStreamContentBlockDelta,
35
+ handleConverseStreamMetadata,
36
+ } from './utils';
24
37
 
25
38
  /**
26
39
  * Service tier type for Bedrock invocations.
@@ -126,7 +139,6 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
126
139
  options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
127
140
  runManager?: CallbackManagerForLLMRun
128
141
  ): Promise<ChatResult> {
129
- // Temporarily swap model for applicationInferenceProfile support
130
142
  const originalModel = this.model;
131
143
  if (
132
144
  this.applicationInferenceProfile != null &&
@@ -138,75 +150,155 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
138
150
  try {
139
151
  return await super._generateNonStreaming(messages, options, runManager);
140
152
  } finally {
141
- // Restore original model
142
153
  this.model = originalModel;
143
154
  }
144
155
  }
145
156
 
146
157
  /**
147
- * Override _streamResponseChunks to:
148
- * 1. Use applicationInferenceProfile as modelId (by temporarily swapping this.model)
149
- * 2. Strip contentBlockIndex from response_metadata to prevent merge conflicts
158
+ * Own the stream end-to-end so we have direct access to every
159
+ * `contentBlockDelta.contentBlockIndex` from the AWS SDK.
150
160
  *
151
- * Note: We delegate to super._streamResponseChunks() to preserve @langchain/aws's
152
- * internal chunk handling which correctly preserves array content for reasoning blocks.
161
+ * This replaces the parent's implementation which strips contentBlockIndex
162
+ * from text and reasoning deltas, making it impossible to merge correctly.
153
163
  */
154
164
  override async *_streamResponseChunks(
155
165
  messages: BaseMessage[],
156
166
  options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,
157
167
  runManager?: CallbackManagerForLLMRun
158
168
  ): AsyncGenerator<ChatGenerationChunk> {
159
- // Temporarily swap model for applicationInferenceProfile support
160
- const originalModel = this.model;
161
- if (
162
- this.applicationInferenceProfile != null &&
163
- this.applicationInferenceProfile !== ''
164
- ) {
165
- this.model = this.applicationInferenceProfile;
169
+ const { converseMessages, converseSystem } =
170
+ convertToConverseMessages(messages);
171
+ const params = this.invocationParams(options);
172
+
173
+ let { streamUsage } = this;
174
+ if ((options as Record<string, unknown>).streamUsage !== undefined) {
175
+ streamUsage = (options as Record<string, unknown>).streamUsage as boolean;
166
176
  }
167
177
 
168
- try {
169
- // Use parent's streaming logic which correctly handles reasoning content
170
- const baseStream = super._streamResponseChunks(
171
- messages,
172
- options,
173
- runManager
174
- );
178
+ const modelId = this.getModelId();
179
+
180
+ const command = new ConverseStreamCommand({
181
+ modelId,
182
+ messages: converseMessages,
183
+ system: converseSystem,
184
+ ...(params as Record<string, unknown>),
185
+ });
186
+
187
+ const response = await this.client.send(command, {
188
+ abortSignal: options.signal,
189
+ });
190
+
191
+ if (!response.stream) {
192
+ return;
193
+ }
194
+
195
+ const seenBlockIndices = new Set<number>();
196
+
197
+ for await (const event of response.stream) {
198
+ if (event.contentBlockStart != null) {
199
+ const startChunk = handleConverseStreamContentBlockStart(
200
+ event.contentBlockStart
201
+ );
202
+ if (startChunk != null) {
203
+ const idx = event.contentBlockStart.contentBlockIndex;
204
+ if (idx != null) {
205
+ seenBlockIndices.add(idx);
206
+ }
207
+ yield this.enrichChunk(startChunk, seenBlockIndices);
208
+ }
209
+ } else if (event.contentBlockDelta != null) {
210
+ const deltaChunk = handleConverseStreamContentBlockDelta(
211
+ event.contentBlockDelta
212
+ );
213
+
214
+ const idx = event.contentBlockDelta.contentBlockIndex;
215
+ if (idx != null) {
216
+ seenBlockIndices.add(idx);
217
+ }
218
+
219
+ yield this.enrichChunk(deltaChunk, seenBlockIndices);
175
220
 
176
- for await (const chunk of baseStream) {
177
- // Clean contentBlockIndex from response_metadata to prevent merge conflicts
178
- yield this.cleanChunk(chunk);
221
+ await runManager?.handleLLMNewToken(
222
+ deltaChunk.text,
223
+ undefined,
224
+ undefined,
225
+ undefined,
226
+ undefined,
227
+ { chunk: deltaChunk }
228
+ );
229
+ } else if (event.metadata != null) {
230
+ yield handleConverseStreamMetadata(event.metadata, { streamUsage });
231
+ } else if (event.contentBlockStop != null) {
232
+ const stopIdx = event.contentBlockStop.contentBlockIndex;
233
+ if (stopIdx != null) {
234
+ seenBlockIndices.add(stopIdx);
235
+ }
236
+ } else {
237
+ yield new ChatGenerationChunk({
238
+ text: '',
239
+ message: new AIMessageChunk({
240
+ content: '',
241
+ response_metadata: event,
242
+ }),
243
+ });
179
244
  }
180
- } finally {
181
- // Restore original model
182
- this.model = originalModel;
183
245
  }
184
246
  }
185
247
 
186
248
  /**
187
- * Clean a chunk by removing contentBlockIndex from response_metadata.
249
+ * Inject `index` on content blocks for proper merge behaviour, then strip
250
+ * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.
251
+ *
252
+ * Text string content is promoted to array form only when the stream contains
253
+ * multiple content block indices (e.g. reasoning at index 0, text at index 1),
254
+ * ensuring text merges correctly with the already-array accumulated content.
188
255
  */
189
- private cleanChunk(chunk: ChatGenerationChunk): ChatGenerationChunk {
256
+ private enrichChunk(
257
+ chunk: ChatGenerationChunk,
258
+ seenBlockIndices: Set<number>
259
+ ): ChatGenerationChunk {
190
260
  const message = chunk.message;
191
261
  if (!(message instanceof AIMessageChunk)) {
192
262
  return chunk;
193
263
  }
194
264
 
195
265
  const metadata = message.response_metadata as Record<string, unknown>;
196
- const hasContentBlockIndex = this.hasContentBlockIndex(metadata);
197
- if (!hasContentBlockIndex) {
266
+ const blockIndex = this.extractContentBlockIndex(metadata);
267
+ const hasMetadataIndex = blockIndex != null;
268
+
269
+ let content: AIMessageChunk['content'] = message.content;
270
+ let contentModified = false;
271
+
272
+ if (Array.isArray(content) && blockIndex != null) {
273
+ content = content.map((block) =>
274
+ typeof block === 'object' && !('index' in block)
275
+ ? { ...block, index: blockIndex }
276
+ : block
277
+ );
278
+ contentModified = true;
279
+ } else if (
280
+ typeof content === 'string' &&
281
+ content !== '' &&
282
+ blockIndex != null &&
283
+ seenBlockIndices.size > 1
284
+ ) {
285
+ content = [{ type: 'text', text: content, index: blockIndex }];
286
+ contentModified = true;
287
+ }
288
+
289
+ if (!contentModified && !hasMetadataIndex) {
198
290
  return chunk;
199
291
  }
200
292
 
201
- const cleanedMetadata = this.removeContentBlockIndex(metadata) as Record<
202
- string,
203
- unknown
204
- >;
293
+ const cleanedMetadata = hasMetadataIndex
294
+ ? (this.removeContentBlockIndex(metadata) as Record<string, unknown>)
295
+ : metadata;
205
296
 
206
297
  return new ChatGenerationChunk({
207
298
  text: chunk.text,
208
299
  message: new AIMessageChunk({
209
300
  ...message,
301
+ content,
210
302
  response_metadata: cleanedMetadata,
211
303
  }),
212
304
  generationInfo: chunk.generationInfo,
@@ -214,31 +306,21 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
214
306
  }
215
307
 
216
308
  /**
217
- * Check if contentBlockIndex exists at any level in the object
309
+ * Extract `contentBlockIndex` from the top level of response_metadata.
310
+ * Our custom handlers always place it at the top level.
218
311
  */
219
- private hasContentBlockIndex(obj: unknown): boolean {
220
- if (obj === null || obj === undefined || typeof obj !== 'object') {
221
- return false;
222
- }
223
-
224
- if ('contentBlockIndex' in obj) {
225
- return true;
226
- }
227
-
228
- for (const value of Object.values(obj)) {
229
- if (typeof value === 'object' && value !== null) {
230
- if (this.hasContentBlockIndex(value)) {
231
- return true;
232
- }
233
- }
312
+ private extractContentBlockIndex(
313
+ metadata: Record<string, unknown>
314
+ ): number | undefined {
315
+ if (
316
+ 'contentBlockIndex' in metadata &&
317
+ typeof metadata.contentBlockIndex === 'number'
318
+ ) {
319
+ return metadata.contentBlockIndex;
234
320
  }
235
-
236
- return false;
321
+ return undefined;
237
322
  }
238
323
 
239
- /**
240
- * Recursively remove contentBlockIndex from all levels of an object
241
- */
242
324
  private removeContentBlockIndex(obj: unknown): unknown {
243
325
  if (obj === null || obj === undefined) {
244
326
  return obj;