@librechat/agents 3.2.38 → 3.2.39

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.
Files changed (78) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +25 -8
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +7 -4
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +20 -4
  6. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  7. package/dist/cjs/llm/bedrock/index.cjs +7 -1
  8. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  9. package/dist/cjs/llm/bedrock/toolCache.cjs +5 -4
  10. package/dist/cjs/llm/bedrock/toolCache.cjs.map +1 -1
  11. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +34 -17
  12. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
  13. package/dist/cjs/llm/openrouter/index.cjs +1 -0
  14. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  15. package/dist/cjs/llm/openrouter/toolCache.cjs +18 -5
  16. package/dist/cjs/llm/openrouter/toolCache.cjs.map +1 -1
  17. package/dist/cjs/main.cjs +4 -0
  18. package/dist/cjs/messages/anthropicToolCache.cjs +75 -13
  19. package/dist/cjs/messages/anthropicToolCache.cjs.map +1 -1
  20. package/dist/cjs/messages/cache.cjs +91 -35
  21. package/dist/cjs/messages/cache.cjs.map +1 -1
  22. package/dist/cjs/summarization/node.cjs +3 -2
  23. package/dist/cjs/summarization/node.cjs.map +1 -1
  24. package/dist/esm/agents/AgentContext.mjs +26 -9
  25. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  26. package/dist/esm/graphs/Graph.mjs +8 -5
  27. package/dist/esm/graphs/Graph.mjs.map +1 -1
  28. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +20 -4
  29. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  30. package/dist/esm/llm/bedrock/index.mjs +7 -1
  31. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  32. package/dist/esm/llm/bedrock/toolCache.mjs +5 -4
  33. package/dist/esm/llm/bedrock/toolCache.mjs.map +1 -1
  34. package/dist/esm/llm/bedrock/utils/message_inputs.mjs +34 -17
  35. package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
  36. package/dist/esm/llm/openrouter/index.mjs +1 -0
  37. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  38. package/dist/esm/llm/openrouter/toolCache.mjs +18 -5
  39. package/dist/esm/llm/openrouter/toolCache.mjs.map +1 -1
  40. package/dist/esm/main.mjs +2 -2
  41. package/dist/esm/messages/anthropicToolCache.mjs +75 -13
  42. package/dist/esm/messages/anthropicToolCache.mjs.map +1 -1
  43. package/dist/esm/messages/cache.mjs +88 -36
  44. package/dist/esm/messages/cache.mjs.map +1 -1
  45. package/dist/esm/summarization/node.mjs +4 -3
  46. package/dist/esm/summarization/node.mjs.map +1 -1
  47. package/dist/types/agents/AgentContext.d.ts +11 -0
  48. package/dist/types/agents/__tests__/promptCacheLiveHelpers.d.ts +2 -0
  49. package/dist/types/llm/bedrock/index.d.ts +13 -0
  50. package/dist/types/llm/bedrock/toolCache.d.ts +2 -1
  51. package/dist/types/llm/openrouter/index.d.ts +8 -0
  52. package/dist/types/llm/openrouter/toolCache.d.ts +2 -1
  53. package/dist/types/messages/anthropicToolCache.d.ts +2 -1
  54. package/dist/types/messages/cache.d.ts +49 -5
  55. package/dist/types/types/llm.d.ts +14 -0
  56. package/package.json +1 -1
  57. package/src/agents/AgentContext.ts +64 -17
  58. package/src/agents/__tests__/AgentContext.anthropic.live.test.ts +6 -2
  59. package/src/agents/__tests__/AgentContext.bedrock.live.test.ts +7 -5
  60. package/src/agents/__tests__/AgentContext.openrouter.live.test.ts +1 -1
  61. package/src/agents/__tests__/AgentContext.test.ts +31 -19
  62. package/src/agents/__tests__/promptCacheLiveHelpers.ts +6 -2
  63. package/src/graphs/Graph.ts +40 -4
  64. package/src/llm/anthropic/utils/message_inputs.ts +33 -6
  65. package/src/llm/bedrock/index.ts +21 -1
  66. package/src/llm/bedrock/llm.spec.ts +61 -0
  67. package/src/llm/bedrock/toolCache.test.ts +24 -0
  68. package/src/llm/bedrock/toolCache.ts +12 -7
  69. package/src/llm/bedrock/utils/message_inputs.ts +57 -40
  70. package/src/llm/openrouter/index.ts +9 -0
  71. package/src/llm/openrouter/toolCache.test.ts +52 -1
  72. package/src/llm/openrouter/toolCache.ts +40 -6
  73. package/src/messages/__tests__/anthropicToolCache.test.ts +168 -0
  74. package/src/messages/anthropicToolCache.ts +118 -15
  75. package/src/messages/cache.test.ts +175 -0
  76. package/src/messages/cache.ts +133 -48
  77. package/src/summarization/node.ts +21 -2
  78. package/src/types/llm.ts +14 -0
@@ -487,8 +487,12 @@ const NON_CACHEABLE_PAYLOAD_BLOCK_TYPES = new Set(["thinking", "redacted_thinkin
487
487
  * input_json_delta) are already gone — only native thinking blocks must be
488
488
  * skipped. Returns a new array only when it actually places a marker.
489
489
  */
490
- function reanchorTailCacheControl(messages) {
490
+ function reanchorTailCacheControl(messages, ttl) {
491
491
  if (messages.length === 0) return messages;
492
+ const cacheControl = ttl === "1h" ? {
493
+ type: "ephemeral",
494
+ ttl: "1h"
495
+ } : { type: "ephemeral" };
492
496
  const lastIndex = messages.length - 1;
493
497
  const tail = messages[lastIndex];
494
498
  const content = tail.content;
@@ -500,7 +504,7 @@ function reanchorTailCacheControl(messages) {
500
504
  content: [{
501
505
  type: "text",
502
506
  text: content,
503
- cache_control: { type: "ephemeral" }
507
+ cache_control: cacheControl
504
508
  }]
505
509
  };
506
510
  return next;
@@ -519,11 +523,23 @@ function reanchorTailCacheControl(messages) {
519
523
  ...tail,
520
524
  content: content.map((block, i) => i === anchor ? {
521
525
  ...block,
522
- cache_control: { type: "ephemeral" }
526
+ cache_control: cacheControl
523
527
  } : block)
524
528
  };
525
529
  return next;
526
530
  }
531
+ /**
532
+ * Find the extended-cache TTL (`'1h'`) carried by an existing `cache_control`
533
+ * breakpoint, so {@link reanchorTailCacheControl} can re-apply the same TTL the
534
+ * stripped prefill had. Returns `undefined` for the legacy 5-minute default
535
+ * (no `ttl`), keeping that path byte-identical to before.
536
+ */
537
+ function findCacheControlTtl(messages) {
538
+ for (const message of messages) {
539
+ if (!Array.isArray(message.content)) continue;
540
+ for (const block of message.content) if (block.cache_control?.ttl === "1h") return "1h";
541
+ }
542
+ }
527
543
  function stripUnsupportedAssistantPrefill(request) {
528
544
  if (!modelDisallowsAssistantPrefill(request.model)) return request;
529
545
  const messages = request.messages;
@@ -536,7 +552,7 @@ function stripUnsupportedAssistantPrefill(request) {
536
552
  * message caching for this request. Re-anchor the breakpoint on the new tail
537
553
  * (only when one was actually lost, so caching-off requests stay untouched).
538
554
  */
539
- const reanchored = messagesHaveCacheControl(messages) && !messagesHaveCacheControl(nextMessages) ? reanchorTailCacheControl(nextMessages) : nextMessages;
555
+ const reanchored = messagesHaveCacheControl(messages) && !messagesHaveCacheControl(nextMessages) ? reanchorTailCacheControl(nextMessages, findCacheControlTtl(messages)) : nextMessages;
540
556
  return {
541
557
  ...request,
542
558
  messages: reanchored
@@ -1 +1 @@
1
- {"version":3,"file":"message_inputs.cjs","names":["HumanMessage","isAnthropicImageBlockParam","redactedPart","compactionPart"],"sources":["../../../../../src/llm/anthropic/utils/message_inputs.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\n/* eslint-disable no-console */\n/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport { createHash } from 'node:crypto';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport {\n type BaseMessage,\n type SystemMessage,\n HumanMessage,\n type AIMessage,\n type ToolMessage,\n isAIMessage,\n type Data,\n type StandardContentBlockConverter,\n MessageContentComplex,\n isDataContentBlock,\n convertToProviderContentBlock,\n parseBase64DataUrl,\n} from '@langchain/core/messages';\nimport {\n AnthropicImageBlockParam,\n AnthropicMessageCreateParams,\n AnthropicTextBlockParam,\n AnthropicDocumentBlockParam,\n AnthropicThinkingBlockParam,\n AnthropicRedactedThinkingBlockParam,\n AnthropicServerToolUseBlockParam,\n AnthropicWebSearchToolResultBlockParam,\n isAnthropicImageBlockParam,\n AnthropicSearchResultBlockParam,\n AnthropicCompactionBlockParam,\n AnthropicToolResponse,\n} from '../types';\nimport { Constants } from '@/common';\n\ntype StandardTextBlock = Data.StandardTextBlock;\ntype StandardImageBlock = Data.StandardImageBlock;\ntype StandardFileBlock = Data.StandardFileBlock;\ntype ImageUrlContentBlock = MessageContentComplex & {\n image_url: string | { url: string };\n};\ntype GoogleFunctionCallBlock = MessageContentComplex & {\n functionCall: {\n name: string;\n args: Record<string, unknown>;\n };\n};\n\nconst ANTHROPIC_EMPTY_TEXT_PLACEHOLDER = '_';\nconst CLAUDE_4_RELEASE_DATE_MODEL_PATTERN =\n /claude-(?:opus|sonnet|haiku)-4-\\d{8}(?:[-.@]|$)/i;\nconst CLAUDE_4_MINOR_MODEL_PATTERN =\n /claude-(?:opus|sonnet|haiku)-4[-.](\\d+)(?:[-.@]|$)/i;\n\nfunction _formatImage(imageUrl: string) {\n const parsed = parseBase64DataUrl({ dataUrl: imageUrl });\n if (parsed) {\n return {\n type: 'base64',\n media_type: parsed.mime_type,\n data: parsed.data,\n };\n }\n let parsedUrl: URL;\n\n try {\n parsedUrl = new URL(imageUrl);\n } catch {\n throw new Error(\n [\n `Malformed image URL: ${JSON.stringify(\n imageUrl\n )}. Content blocks of type 'image_url' must be a valid http, https, or base64-encoded data URL.`,\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n 'Example: https://example.com/image.jpg',\n ].join('\\n\\n')\n );\n }\n\n if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') {\n return {\n type: 'url',\n url: imageUrl,\n };\n }\n\n throw new Error(\n [\n `Invalid image URL protocol: ${JSON.stringify(\n parsedUrl.protocol\n )}. Anthropic only supports images as http, https, or base64-encoded data URLs on 'image_url' content blocks.`,\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n 'Example: https://example.com/image.jpg',\n ].join('\\n\\n')\n );\n}\n\nconst ANTHROPIC_TOOL_USE_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;\nconst ANTHROPIC_TOOL_USE_ID_MAX_LENGTH = 64;\nconst ANTHROPIC_TOOL_USE_ID_HASH_LENGTH = 10;\n\n/**\n * Normalize a tool-call ID to satisfy Anthropic's `^[a-zA-Z0-9_-]+$` and 64-char\n * constraints. Pure and deterministic — same input always yields the same output,\n * so paired `tool_use.id` and `tool_result.tool_use_id` stay matched without\n * needing a session map. IDs that already comply pass through unchanged.\n *\n * For non-compliant inputs we sanitize then append a short SHA-256 prefix of\n * the original ID to preserve uniqueness when truncation would otherwise\n * collapse distinct IDs to the same value (e.g. two long Responses-style IDs\n * sharing a 64-char prefix). The hash is computed against the raw input so\n * inputs that differ only after the truncation cutoff still produce distinct\n * outputs.\n */\nexport function normalizeAnthropicToolCallId(id: string): string;\nexport function normalizeAnthropicToolCallId(\n id: string | undefined\n): string | undefined;\nexport function normalizeAnthropicToolCallId(\n id: string | undefined\n): string | undefined {\n if (id == null) {\n return id;\n }\n if (\n id.length <= ANTHROPIC_TOOL_USE_ID_MAX_LENGTH &&\n ANTHROPIC_TOOL_USE_ID_PATTERN.test(id)\n ) {\n return id;\n }\n const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, '_');\n const hash = createHash('sha256')\n .update(id)\n .digest('hex')\n .slice(0, ANTHROPIC_TOOL_USE_ID_HASH_LENGTH);\n const prefixMaxLength =\n ANTHROPIC_TOOL_USE_ID_MAX_LENGTH - ANTHROPIC_TOOL_USE_ID_HASH_LENGTH - 1;\n return `${sanitized.slice(0, prefixMaxLength)}_${hash}`;\n}\n\n/**\n * Lift any `cache_control` off the inner blocks of a tool result onto the\n * `tool_result` block itself. Anthropic documents the top-level\n * `messages.content` block as the cacheable position and does not document\n * caching of sub-content blocks; the API currently honors a nested marker, but\n * anchoring on the documented position keeps the single tail breakpoint robust\n * (and mirrors the Bedrock cachePoint hoist). The first marker found wins; it is\n * stripped from every inner block so exactly one survives, on the outer block.\n */\nfunction hoistToolResultCacheControl(\n content: string | MessageContentComplex[]\n): { content: string | MessageContentComplex[]; cacheControl: unknown } {\n if (!Array.isArray(content)) {\n return { content, cacheControl: undefined };\n }\n let cacheControl: unknown;\n const stripped = content.map((block) => {\n if ('cache_control' in block) {\n cacheControl ??= (block as Record<string, unknown>).cache_control;\n const clone = { ...(block as Record<string, unknown>) };\n delete clone.cache_control;\n return clone as MessageContentComplex;\n }\n return block;\n });\n // `stripped` is element-equal to `content` when no marker was present.\n return { content: stripped, cacheControl };\n}\n\nfunction _ensureMessageContents(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const updatedMsgs: BaseMessage[] = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = updatedMsgs[updatedMsgs.length - 1];\n if (\n previousMessage._getType() === 'human' &&\n Array.isArray(previousMessage.content) &&\n 'type' in previousMessage.content[0] &&\n previousMessage.content[0].type === 'tool_result'\n ) {\n // If the previous message was a tool result, we merge this tool message into it.\n (previousMessage.content as MessageContentComplex[]).push({\n type: 'tool_result',\n content: message.content,\n tool_use_id: normalizeAnthropicToolCallId(\n (message as ToolMessage).tool_call_id\n ),\n });\n } else {\n // If not, we create a new human message with the tool result.\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: message.content,\n tool_use_id: normalizeAnthropicToolCallId(\n (message as ToolMessage).tool_call_id\n ),\n },\n ],\n })\n );\n }\n } else {\n const toolMessageContent = (\n message as { content?: BaseMessage['content'] | null }\n ).content;\n // Hoist a tail cache_control off the inner content onto the\n // tool_result block itself (the documented cacheable position).\n const { content: hoistedContent, cacheControl } =\n toolMessageContent != null\n ? hoistToolResultCacheControl(_formatContent(message))\n : { content: undefined, cacheControl: undefined };\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n ...(hoistedContent != null ? { content: hoistedContent } : {}),\n ...(cacheControl != null\n ? { cache_control: cacheControl as { type: 'ephemeral' } }\n : {}),\n tool_use_id: normalizeAnthropicToolCallId(\n (message as ToolMessage).tool_call_id\n ),\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(message);\n }\n }\n return updatedMsgs as (SystemMessage | HumanMessage | AIMessage)[];\n}\n\nexport function _convertLangChainToolCallToAnthropic(\n toolCall: ToolCall\n): AnthropicToolResponse {\n if (toolCall.id === undefined) {\n throw new Error('Anthropic requires all tool calls to have an \"id\".');\n }\n const isServerTool = toolCall.id.startsWith(\n Constants.ANTHROPIC_SERVER_TOOL_PREFIX\n );\n return {\n type: isServerTool ? 'server_tool_use' : 'tool_use',\n id: isServerTool ? toolCall.id : normalizeAnthropicToolCallId(toolCall.id),\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\nconst standardContentBlockConverter: StandardContentBlockConverter<{\n text: AnthropicTextBlockParam;\n image: AnthropicImageBlockParam;\n file: AnthropicDocumentBlockParam;\n}> = {\n providerName: 'anthropic',\n\n fromStandardTextBlock(block: StandardTextBlock): AnthropicTextBlockParam {\n return {\n type: 'text',\n text: block.text,\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as AnthropicTextBlockParam;\n },\n\n fromStandardImageBlock(block: StandardImageBlock): AnthropicImageBlockParam {\n if (block.source_type === 'url') {\n const data = parseBase64DataUrl({\n dataUrl: block.url,\n asTypedArray: false,\n });\n if (data) {\n return {\n type: 'image',\n source: {\n type: 'base64',\n data: data.data,\n media_type: data.mime_type,\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as AnthropicImageBlockParam;\n } else {\n return {\n type: 'image',\n source: {\n type: 'url',\n url: block.url,\n media_type: block.mime_type ?? '',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as AnthropicImageBlockParam;\n }\n } else {\n if (block.source_type === 'base64') {\n return {\n type: 'image',\n source: {\n type: 'base64',\n data: block.data,\n media_type: block.mime_type ?? '',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as AnthropicImageBlockParam;\n } else {\n throw new Error(`Unsupported image source type: ${block.source_type}`);\n }\n }\n },\n\n fromStandardFileBlock(block: StandardFileBlock): AnthropicDocumentBlockParam {\n const mime_type = (block.mime_type ?? '').split(';')[0];\n\n if (block.source_type === 'url') {\n if (mime_type === 'application/pdf' || mime_type === '') {\n return {\n type: 'document',\n source: {\n type: 'url',\n url: block.url,\n media_type: block.mime_type ?? '',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('context' in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...('title' in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as AnthropicDocumentBlockParam;\n }\n throw new Error(\n `Unsupported file mime type for file url source: ${block.mime_type}`\n );\n } else if (block.source_type === 'text') {\n if (mime_type === 'text/plain' || mime_type === '') {\n return {\n type: 'document',\n source: {\n type: 'text',\n data: block.text,\n media_type: block.mime_type ?? '',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('context' in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...('title' in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as AnthropicDocumentBlockParam;\n } else {\n throw new Error(\n `Unsupported file mime type for file text source: ${block.mime_type}`\n );\n }\n } else if (block.source_type === 'base64') {\n if (mime_type === 'application/pdf' || mime_type === '') {\n return {\n type: 'document',\n source: {\n type: 'base64',\n data: block.data,\n media_type: 'application/pdf',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('context' in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...('title' in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as AnthropicDocumentBlockParam;\n } else if (\n ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].includes(\n mime_type\n )\n ) {\n return {\n type: 'document',\n source: {\n type: 'content',\n content: [\n {\n type: 'image',\n source: {\n type: 'base64',\n data: block.data,\n media_type: mime_type as\n | 'image/jpeg'\n | 'image/png'\n | 'image/gif'\n | 'image/webp',\n },\n },\n ],\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('context' in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...('title' in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as AnthropicDocumentBlockParam;\n } else {\n throw new Error(\n `Unsupported file mime type for file base64 source: ${block.mime_type}`\n );\n }\n } else {\n throw new Error(`Unsupported file source type: ${block.source_type}`);\n }\n },\n};\n\nfunction _formatContent(message: BaseMessage) {\n const toolTypes = [\n 'tool_use',\n 'tool_result',\n 'input_json_delta',\n 'server_tool_use',\n 'web_search_tool_result',\n 'web_search_result',\n ];\n const textTypes = ['text', 'text_delta'];\n /**\n * Reasoning blocks emitted by other providers — Bedrock's `reasoning_content`,\n * Google's `reasoning`, and LibreChat's `think`. Their signatures are\n * provider-specific and cannot be validated by Anthropic, so on a\n * cross-provider handoff (e.g. Bedrock → Anthropic) we drop them rather than\n * forwarding an unusable block. The receiving model produces its own thinking.\n */\n const foreignReasoningTypes = ['reasoning_content', 'reasoning', 'think'];\n const { content } = message;\n\n if (typeof content === 'string') {\n return content;\n } else {\n const contentParts = content as MessageContentComplex[];\n const contentBlocks = contentParts.map((contentPart) => {\n /**\n * Normalize server_tool_use blocks into a clean shape the API accepts.\n * These blocks may arrive with the correct type (server_tool_use) or mislabeled\n * as text/tool_use after chunk concatenation or state serialization.\n * Regardless of current type, if the id starts with 'srvtoolu_' we rebuild\n * a clean block with only the properties the API expects.\n */\n if (\n 'id' in contentPart &&\n typeof (contentPart as Record<string, unknown>).id === 'string' &&\n ((contentPart as Record<string, unknown>).id as string).startsWith(\n Constants.ANTHROPIC_SERVER_TOOL_PREFIX\n ) &&\n 'name' in contentPart\n ) {\n const rawPart = contentPart as Record<string, unknown>;\n let input = rawPart.input;\n if (typeof input === 'string') {\n try {\n input = JSON.parse(input);\n } catch {\n input = {};\n }\n }\n const corrected: AnthropicServerToolUseBlockParam = {\n type: 'server_tool_use',\n id: rawPart.id as string,\n name: (rawPart.name ?? 'web_search') as 'web_search',\n input: (input ?? {}) as Record<string, unknown>,\n };\n return corrected;\n }\n\n /**\n * Normalize web_search_tool_result blocks into a clean shape.\n * Same rationale as above — the block may carry extra properties from\n * streaming (input, index, etc.) that the API rejects. Rebuild cleanly.\n */\n if (\n 'tool_use_id' in contentPart &&\n typeof (contentPart as Record<string, unknown>).tool_use_id ===\n 'string' &&\n (\n (contentPart as Record<string, unknown>).tool_use_id as string\n ).startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) &&\n 'content' in contentPart\n ) {\n const rawPart = contentPart as Record<string, unknown>;\n const content = rawPart.content;\n const isValidContent =\n Array.isArray(content) ||\n (content != null &&\n typeof content === 'object' &&\n 'type' in content &&\n (content as Record<string, unknown>).type ===\n 'web_search_tool_result_error');\n\n if (isValidContent) {\n const corrected: AnthropicWebSearchToolResultBlockParam = {\n type: 'web_search_tool_result',\n tool_use_id: rawPart.tool_use_id as string,\n content:\n content as AnthropicWebSearchToolResultBlockParam['content'],\n };\n return corrected;\n }\n return null;\n }\n\n /**\n * Skip non-server malformed blocks that have tool fields mixed with text type.\n */\n if (\n 'id' in contentPart &&\n 'name' in contentPart &&\n 'input' in contentPart &&\n contentPart.type === 'text'\n ) {\n return null;\n }\n if (\n 'tool_use_id' in contentPart &&\n 'content' in contentPart &&\n contentPart.type === 'text'\n ) {\n return null;\n }\n\n // Core's v1 streaming aggregation can leave a partial tool-input delta as a\n // standalone block typed `text` carrying `input` but no `text`. The assembled\n // input is restored on the tool_use block from `message.tool_calls`, so drop it.\n if (\n contentPart.type === 'text' &&\n 'input' in contentPart &&\n !('text' in contentPart)\n ) {\n return null;\n }\n\n if (isDataContentBlock(contentPart)) {\n return convertToProviderContentBlock(\n contentPart,\n standardContentBlockConverter\n );\n }\n\n const cacheControl =\n 'cache_control' in contentPart ? contentPart.cache_control : undefined;\n\n if (contentPart.type === 'image_url') {\n let source;\n const imageUrl = (contentPart as ImageUrlContentBlock).image_url;\n if (typeof imageUrl === 'string') {\n source = _formatImage(imageUrl);\n } else {\n source = _formatImage(imageUrl.url);\n }\n return {\n type: 'image' as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n } else if (isAnthropicImageBlockParam(contentPart)) {\n return contentPart;\n } else if (contentPart.type === 'document') {\n // PDF\n return {\n ...contentPart,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n } else if (contentPart.type === 'thinking') {\n const thinkingPart = contentPart as AnthropicThinkingBlockParam;\n // Google thinking-enabled output reuses `type: 'thinking'` but carries\n // no Anthropic signature. Anthropic rejects an unsigned thinking block,\n // so on an assistant turn treat it as foreign reasoning and drop it\n // rather than forward an unusable block. Signed (Anthropic-native)\n // thinking is forwarded as before.\n const signature = (thinkingPart as { signature?: string }).signature;\n if (isAIMessage(message) && (signature == null || signature === '')) {\n return null;\n }\n const block: AnthropicThinkingBlockParam = {\n type: 'thinking' as const, // Explicitly setting the type as \"thinking\"\n thinking: thinkingPart.thinking,\n signature: thinkingPart.signature,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n return block;\n } else if (contentPart.type === 'redacted_thinking') {\n const redactedPart = contentPart as AnthropicRedactedThinkingBlockParam;\n const block: AnthropicRedactedThinkingBlockParam = {\n type: 'redacted_thinking' as const, // Explicitly setting the type as \"redacted_thinking\"\n data: redactedPart.data,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n return block;\n } else if (contentPart.type === 'search_result') {\n const searchResultPart = contentPart as AnthropicSearchResultBlockParam;\n const block: AnthropicSearchResultBlockParam = {\n type: 'search_result' as const,\n title: searchResultPart.title,\n source: searchResultPart.source,\n ...('cache_control' in contentPart &&\n contentPart.cache_control != null\n ? { cache_control: contentPart.cache_control }\n : {}),\n ...('citations' in contentPart && contentPart.citations != null\n ? { citations: contentPart.citations }\n : {}),\n content: searchResultPart.content,\n };\n return block;\n } else if (contentPart.type === 'compaction') {\n const compactionPart = contentPart as AnthropicCompactionBlockParam;\n const block: AnthropicCompactionBlockParam = {\n type: 'compaction' as const,\n content: compactionPart.content,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n return block;\n } else if (\n textTypes.some((t) => t === contentPart.type) &&\n 'text' in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n return {\n type: 'text' as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n ...('citations' in contentPart && contentPart.citations != null\n ? { citations: contentPart.citations }\n : {}),\n };\n } else if (toolTypes.some((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n if ('index' in contentPartCopy) {\n // Anthropic does not support passing the index field here, so we remove it.\n delete contentPartCopy.index;\n }\n\n if (contentPartCopy.type === 'input_json_delta') {\n // Orphaned partial tool-input delta with no id of its own. The assembled\n // input is restored on the tool_use block from `message.tool_calls`; drop it.\n return null;\n }\n\n if (\n contentPartCopy.type === 'tool_use' &&\n 'id' in contentPartCopy &&\n typeof contentPartCopy.id === 'string' &&\n contentPartCopy.id.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)\n ) {\n contentPartCopy.type = 'server_tool_use';\n }\n\n // Core's streaming aggregation can leave the inline tool_use input empty\n // (the assembled arguments live in `message.tool_calls` or, for persisted\n // messages, in sibling input_json_delta blocks). Restore it when missing.\n if (\n contentPartCopy.type === 'tool_use' &&\n typeof contentPartCopy.id === 'string' &&\n (contentPartCopy.input === '' || contentPartCopy.input == null)\n ) {\n const matchingToolCall = isAIMessage(message)\n ? message.tool_calls?.find(\n (toolCall) => toolCall.id === contentPartCopy.id\n )\n : undefined;\n if (matchingToolCall) {\n contentPartCopy.input = matchingToolCall.args;\n } else {\n const blockIndex = (contentPart as Record<string, unknown>).index;\n const merged = contentParts\n .filter((part) => {\n const p = part as Record<string, unknown>;\n return (\n p.type === 'input_json_delta' &&\n p.index === blockIndex &&\n typeof p.input === 'string'\n );\n })\n .reduce(\n (acc, part) => acc + (part as Record<string, unknown>).input,\n ''\n );\n if (merged !== '') {\n contentPartCopy.input = merged;\n }\n }\n }\n\n if ('input' in contentPartCopy) {\n // Anthropic tool use inputs should be valid objects, when applicable.\n if (typeof contentPartCopy.input === 'string') {\n try {\n contentPartCopy.input = JSON.parse(contentPartCopy.input);\n } catch {\n contentPartCopy.input = {};\n }\n }\n }\n\n /**\n * For multi-turn conversations with citations, we must preserve ALL blocks\n * including server_tool_use, web_search_tool_result, and web_search_result.\n * Citations reference search results by index, so filtering changes indices and breaks references.\n *\n * The ToolNode already handles skipping server tool invocations via the srvtoolu_ prefix check.\n */\n\n // TODO: Fix when SDK types are fixed\n return {\n ...contentPartCopy,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else if (\n 'functionCall' in contentPart &&\n contentPart.functionCall != null &&\n typeof contentPart.functionCall === 'object' &&\n isAIMessage(message)\n ) {\n const functionCallPart = contentPart as GoogleFunctionCallBlock;\n const correspondingToolCall = message.tool_calls?.find(\n (toolCall) => toolCall.name === functionCallPart.functionCall.name\n );\n if (!correspondingToolCall) {\n throw new Error(\n `Could not find tool call for function call ${functionCallPart.functionCall.name}`\n );\n }\n // Google GenAI models include a `functionCall` object inside content. We should ignore it as Anthropic will not support it.\n return {\n id: correspondingToolCall.id,\n type: 'tool_use',\n name: correspondingToolCall.name,\n input: functionCallPart.functionCall.args,\n };\n } else if (\n isAIMessage(message) &&\n foreignReasoningTypes.some((t) => t === contentPart.type)\n ) {\n // Foreign reasoning on an ASSISTANT turn (Bedrock `reasoning_content`,\n // Google `reasoning`, LibreChat `think`) carries provider-specific\n // signatures Anthropic cannot validate; drop it so a cross-provider\n // handoff doesn't crash. The same types on a user/tool turn are real\n // input and fall through to the throw below rather than being silently\n // dropped — as does any other unknown block (user media, Google\n // code-execution), which must be surfaced, not discarded.\n return null;\n } else {\n console.error(\n 'Unsupported content part:',\n JSON.stringify(contentPart, null, 2)\n );\n throw new Error('Unsupported message content format');\n }\n });\n const filteredContentBlocks = contentBlocks.filter(\n (block) =>\n block !== null &&\n !(\n block.type === 'text' &&\n 'text' in block &&\n typeof block.text === 'string' &&\n block.text.trim() === ''\n )\n );\n return filteredContentBlocks.length > 0\n ? filteredContentBlocks\n : [{ type: 'text' as const, text: ANTHROPIC_EMPTY_TEXT_PLACEHOLDER }];\n }\n}\n\n/**\n * Formats messages as a prompt for the model.\n * Used in LangSmith, export is important here.\n * @param messages The base messages to format as a prompt.\n * @returns The formatted prompt.\n */\nexport function _convertMessagesToAnthropicPayload(\n messages: BaseMessage[]\n): AnthropicMessageCreateParams {\n const mergedMessages = _ensureMessageContents(messages);\n let system;\n if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {\n system = messages[0].content;\n }\n const conversationMessages =\n system !== undefined ? mergedMessages.slice(1) : mergedMessages;\n const formattedMessages = conversationMessages.map((message) => {\n let role;\n if (message._getType() === 'human') {\n role = 'user' as const;\n } else if (message._getType() === 'ai') {\n role = 'assistant' as const;\n } else if (message._getType() === 'tool') {\n role = 'user' as const;\n } else if (message._getType() === 'system') {\n throw new Error(\n 'System messages are only permitted as the first passed message.'\n );\n } else {\n throw new Error(`Message type \"${message._getType()}\" is not supported.`);\n }\n const isAI = isAIMessage(message);\n const toolCalls = isAI ? (message.tool_calls ?? []) : [];\n if (isAI && toolCalls.length > 0) {\n if (typeof message.content === 'string') {\n const clientToolCalls = toolCalls.filter(\n (tc) =>\n !(\n tc.id?.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) ?? false\n )\n );\n if (message.content === '') {\n return {\n role,\n content:\n clientToolCalls.length > 0\n ? clientToolCalls.map(_convertLangChainToolCallToAnthropic)\n : [\n {\n type: 'text' as const,\n text: ANTHROPIC_EMPTY_TEXT_PLACEHOLDER,\n },\n ],\n };\n } else {\n return {\n role,\n content: [\n { type: 'text' as const, text: message.content },\n ...clientToolCalls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const formattedContent = _formatContent(message);\n const formattedBlocks = Array.isArray(formattedContent)\n ? formattedContent\n : [];\n // Tool calls already materialized as content blocks by `_formatContent`.\n // Derived from the FORMATTED output (not the raw content by type) so\n // that Google `functionCall` parts — which `_formatContent` converts\n // into `tool_use` — count as represented and are not appended twice.\n const representedToolIds = new Set(\n formattedBlocks\n .filter(\n (block) =>\n block != null &&\n (block.type === 'tool_use' || block.type === 'server_tool_use')\n )\n .map((block) => (block as { id?: string }).id)\n );\n // Client tool calls present in `tool_calls` but absent from the\n // formatted content — e.g. a Bedrock extended-thinking turn records the\n // tool only on `tool_calls` and leaves `content` as just the reasoning\n // block. Without materializing them, dropping that reasoning block\n // silently loses the (handoff) tool call instead of forwarding it.\n const unrepresentedToolCalls = toolCalls.filter(\n (toolCall) =>\n !(\n toolCall.id?.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) ??\n false\n ) && !representedToolIds.has(toolCall.id)\n );\n if (unrepresentedToolCalls.length === 0) {\n return { role, content: formattedContent };\n }\n const existingBlocks = formattedBlocks.filter(\n (block) =>\n !(\n block != null &&\n block.type === 'text' &&\n 'text' in block &&\n block.text === ANTHROPIC_EMPTY_TEXT_PLACEHOLDER\n )\n );\n return {\n role,\n content: [\n ...existingBlocks,\n ...unrepresentedToolCalls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n return {\n role,\n content: _formatContent(message),\n };\n }\n });\n return {\n messages: mergeMessages(formattedMessages),\n system,\n } as AnthropicMessageCreateParams;\n}\n\nexport function modelDisallowsAssistantPrefill(model?: string): boolean {\n const modelId = model ?? '';\n if (CLAUDE_4_RELEASE_DATE_MODEL_PATTERN.test(modelId)) {\n return false;\n }\n\n const match = CLAUDE_4_MINOR_MODEL_PATTERN.exec(modelId);\n if (!match) {\n return false;\n }\n return Number(match[1]) >= 6;\n}\n\nfunction messagesHaveCacheControl(\n messages: AnthropicMessageCreateParams['messages']\n): boolean {\n return messages.some(\n (message) =>\n Array.isArray(message.content) &&\n message.content.some((block) => 'cache_control' in block)\n );\n}\n\n/** Anthropic rejects cache_control on these reasoning blocks. */\nconst NON_CACHEABLE_PAYLOAD_BLOCK_TYPES = new Set([\n 'thinking',\n 'redacted_thinking',\n]);\n\n/**\n * Place one ephemeral `cache_control` on the last cacheable block of the final\n * message of an already-converted Anthropic payload. Used to re-anchor the tail\n * breakpoint after a trailing assistant prefill is stripped. Operates on the\n * post-conversion payload, where blocks the converter drops (foreign reasoning,\n * input_json_delta) are already gone — only native thinking blocks must be\n * skipped. Returns a new array only when it actually places a marker.\n */\nfunction reanchorTailCacheControl(\n messages: AnthropicMessageCreateParams['messages']\n): AnthropicMessageCreateParams['messages'] {\n if (messages.length === 0) {\n return messages;\n }\n const lastIndex = messages.length - 1;\n const tail = messages[lastIndex];\n const content = tail.content;\n\n if (typeof content === 'string') {\n if (content.trim() === '') {\n return messages;\n }\n const next = [...messages];\n next[lastIndex] = {\n ...tail,\n content: [\n { type: 'text', text: content, cache_control: { type: 'ephemeral' } },\n ],\n } as (typeof messages)[number];\n return next;\n }\n\n if (!Array.isArray(content)) {\n return messages;\n }\n\n let anchor = -1;\n for (let i = 0; i < content.length; i++) {\n const type = (content[i] as { type?: string }).type;\n if (type == null || NON_CACHEABLE_PAYLOAD_BLOCK_TYPES.has(type)) {\n continue;\n }\n if (\n type === 'text' &&\n ((content[i] as { text?: string }).text ?? '').trim() === ''\n ) {\n continue;\n }\n anchor = i;\n }\n if (anchor < 0) {\n return messages;\n }\n\n const next = [...messages];\n next[lastIndex] = {\n ...tail,\n content: content.map((block, i) =>\n i === anchor ? { ...block, cache_control: { type: 'ephemeral' } } : block\n ),\n } as (typeof messages)[number];\n return next;\n}\n\nexport function stripUnsupportedAssistantPrefill<\n T extends Pick<AnthropicMessageCreateParams, 'messages'> & { model?: string },\n>(request: T): T {\n if (!modelDisallowsAssistantPrefill(request.model)) {\n return request;\n }\n\n const messages = request.messages;\n if (\n messages.length <= 1 ||\n messages[messages.length - 1]?.role !== 'assistant'\n ) {\n return request;\n }\n\n const nextMessages = [...messages];\n while (\n nextMessages.length > 1 &&\n nextMessages[nextMessages.length - 1]?.role === 'assistant'\n ) {\n nextMessages.pop();\n }\n\n /**\n * If a single tail prompt-cache breakpoint rode the stripped assistant\n * prefill, the survivors may now carry no `cache_control` at all, dropping\n * message caching for this request. Re-anchor the breakpoint on the new tail\n * (only when one was actually lost, so caching-off requests stay untouched).\n */\n const reanchored =\n messagesHaveCacheControl(messages) &&\n !messagesHaveCacheControl(nextMessages)\n ? reanchorTailCacheControl(nextMessages)\n : nextMessages;\n\n return {\n ...request,\n messages: reanchored,\n };\n}\n\nfunction mergeMessages(messages: AnthropicMessageCreateParams['messages']) {\n if (messages.length <= 1) {\n return messages;\n }\n\n const result: AnthropicMessageCreateParams['messages'] = [];\n let currentMessage = messages[0];\n\n type ContentBlocks = Exclude<\n AnthropicMessageCreateParams['messages'][number]['content'],\n string\n >;\n const normalizeContent = (\n content: AnthropicMessageCreateParams['messages'][number]['content']\n ): ContentBlocks => {\n if (typeof content === 'string') {\n return [{ type: 'text', text: content }];\n }\n return content;\n };\n\n const isToolResultMessage = (msg: (typeof messages)[0]) => {\n if (msg.role !== 'user') return false;\n\n if (typeof msg.content === 'string') {\n return false;\n }\n\n return (\n Array.isArray(msg.content) &&\n msg.content.every((item) => item.type === 'tool_result')\n );\n };\n\n for (let i = 1; i < messages.length; i += 1) {\n const nextMessage = messages[i];\n\n if (\n isToolResultMessage(currentMessage) &&\n isToolResultMessage(nextMessage)\n ) {\n // Merge the messages by combining their content arrays\n currentMessage = {\n ...currentMessage,\n content: [\n ...normalizeContent(currentMessage.content),\n ...normalizeContent(nextMessage.content),\n ],\n };\n } else {\n result.push(currentMessage);\n currentMessage = nextMessage;\n }\n }\n\n result.push(currentMessage);\n return result;\n}\n"],"mappings":";;;;;;;;;AAkDA,MAAM,mCAAmC;AACzC,MAAM,sCACJ;AACF,MAAM,+BACJ;AAEF,SAAS,aAAa,UAAkB;CACtC,MAAM,UAAA,GAAA,yBAAA,mBAAA,CAA4B,EAAE,SAAS,SAAS,CAAC;CACvD,IAAI,QACF,OAAO;EACL,MAAM;EACN,YAAY,OAAO;EACnB,MAAM,OAAO;CACf;CAEF,IAAI;CAEJ,IAAI;EACF,YAAY,IAAI,IAAI,QAAQ;CAC9B,QAAQ;EACN,MAAM,IAAI,MACR;GACE,wBAAwB,KAAK,UAC3B,QACF,EAAE;GACF;GACA;EACF,CAAC,CAAC,KAAK,MAAM,CACf;CACF;CAEA,IAAI,UAAU,aAAa,WAAW,UAAU,aAAa,UAC3D,OAAO;EACL,MAAM;EACN,KAAK;CACP;CAGF,MAAM,IAAI,MACR;EACE,+BAA+B,KAAK,UAClC,UAAU,QACZ,EAAE;EACF;EACA;CACF,CAAC,CAAC,KAAK,MAAM,CACf;AACF;AAEA,MAAM,gCAAgC;AACtC,MAAM,mCAAmC;AACzC,MA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AAG;EACH,UAAU;CACZ;AACF;AAEA,SAAS,cAAc,UAAoD;CACzE,IAAI,SAAS,UAAU,GACrB,OAAO;CAGT,MAAM,SAAmD,CAAC;CAC1D,IAAI,iBAAiB,SAAS;CAM9B,MAAM,oBACJ,YACkB;EAClB,IAAI,OAAO,YAAY,UACrB,OAAO,CAAC;GAAE,MAAM;GAAQ,MAAM;EAAQ,CAAC;EAEzC,OAAO;CACT;CAEA,MAAM,uBAAuB,QAA8B;EACzD,IAAI,IAAI,SAAS,QAAQ,OAAO;EAEhC,IAAI,OAAO,IAAI,YAAY,UACzB,OAAO;EAGT,OACE,MAAM,QAAQ,IAAI,OAAO,KACzB,IAAI,QAAQ,OAAO,SAAS,KAAK,SAAS,aAAa;CAE3D;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK,GAAG;EAC3C,MAAM,cAAc,SAAS;EAE7B,IACE,oBAAoB,cAAc,KAClC,oBAAoB,WAAW,GAG/B,iBAAiB;GACf,GAAG;GACH,SAAS,CACP,GAAG,iBAAiB,eAAe,OAAO,GAC1C,GAAG,iBAAiB,YAAY,OAAO,CACzC;EACF;OACK;GACL,OAAO,KAAK,cAAc;GAC1B,iBAAiB;EACnB;CACF;CAEA,OAAO,KAAK,cAAc;CAC1B,OAAO;AACT"}
1
+ {"version":3,"file":"message_inputs.cjs","names":["HumanMessage","isAnthropicImageBlockParam","redactedPart","compactionPart"],"sources":["../../../../../src/llm/anthropic/utils/message_inputs.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\n/* eslint-disable no-console */\n/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport { createHash } from 'node:crypto';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport {\n type BaseMessage,\n type SystemMessage,\n HumanMessage,\n type AIMessage,\n type ToolMessage,\n isAIMessage,\n type Data,\n type StandardContentBlockConverter,\n MessageContentComplex,\n isDataContentBlock,\n convertToProviderContentBlock,\n parseBase64DataUrl,\n} from '@langchain/core/messages';\nimport {\n AnthropicImageBlockParam,\n AnthropicMessageCreateParams,\n AnthropicTextBlockParam,\n AnthropicDocumentBlockParam,\n AnthropicThinkingBlockParam,\n AnthropicRedactedThinkingBlockParam,\n AnthropicServerToolUseBlockParam,\n AnthropicWebSearchToolResultBlockParam,\n isAnthropicImageBlockParam,\n AnthropicSearchResultBlockParam,\n AnthropicCompactionBlockParam,\n AnthropicToolResponse,\n} from '../types';\nimport { Constants } from '@/common';\n\ntype StandardTextBlock = Data.StandardTextBlock;\ntype StandardImageBlock = Data.StandardImageBlock;\ntype StandardFileBlock = Data.StandardFileBlock;\ntype ImageUrlContentBlock = MessageContentComplex & {\n image_url: string | { url: string };\n};\ntype GoogleFunctionCallBlock = MessageContentComplex & {\n functionCall: {\n name: string;\n args: Record<string, unknown>;\n };\n};\n\nconst ANTHROPIC_EMPTY_TEXT_PLACEHOLDER = '_';\nconst CLAUDE_4_RELEASE_DATE_MODEL_PATTERN =\n /claude-(?:opus|sonnet|haiku)-4-\\d{8}(?:[-.@]|$)/i;\nconst CLAUDE_4_MINOR_MODEL_PATTERN =\n /claude-(?:opus|sonnet|haiku)-4[-.](\\d+)(?:[-.@]|$)/i;\n\nfunction _formatImage(imageUrl: string) {\n const parsed = parseBase64DataUrl({ dataUrl: imageUrl });\n if (parsed) {\n return {\n type: 'base64',\n media_type: parsed.mime_type,\n data: parsed.data,\n };\n }\n let parsedUrl: URL;\n\n try {\n parsedUrl = new URL(imageUrl);\n } catch {\n throw new Error(\n [\n `Malformed image URL: ${JSON.stringify(\n imageUrl\n )}. Content blocks of type 'image_url' must be a valid http, https, or base64-encoded data URL.`,\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n 'Example: https://example.com/image.jpg',\n ].join('\\n\\n')\n );\n }\n\n if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') {\n return {\n type: 'url',\n url: imageUrl,\n };\n }\n\n throw new Error(\n [\n `Invalid image URL protocol: ${JSON.stringify(\n parsedUrl.protocol\n )}. Anthropic only supports images as http, https, or base64-encoded data URLs on 'image_url' content blocks.`,\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n 'Example: https://example.com/image.jpg',\n ].join('\\n\\n')\n );\n}\n\nconst ANTHROPIC_TOOL_USE_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;\nconst ANTHROPIC_TOOL_USE_ID_MAX_LENGTH = 64;\nconst ANTHROPIC_TOOL_USE_ID_HASH_LENGTH = 10;\n\n/**\n * Normalize a tool-call ID to satisfy Anthropic's `^[a-zA-Z0-9_-]+$` and 64-char\n * constraints. Pure and deterministic — same input always yields the same output,\n * so paired `tool_use.id` and `tool_result.tool_use_id` stay matched without\n * needing a session map. IDs that already comply pass through unchanged.\n *\n * For non-compliant inputs we sanitize then append a short SHA-256 prefix of\n * the original ID to preserve uniqueness when truncation would otherwise\n * collapse distinct IDs to the same value (e.g. two long Responses-style IDs\n * sharing a 64-char prefix). The hash is computed against the raw input so\n * inputs that differ only after the truncation cutoff still produce distinct\n * outputs.\n */\nexport function normalizeAnthropicToolCallId(id: string): string;\nexport function normalizeAnthropicToolCallId(\n id: string | undefined\n): string | undefined;\nexport function normalizeAnthropicToolCallId(\n id: string | undefined\n): string | undefined {\n if (id == null) {\n return id;\n }\n if (\n id.length <= ANTHROPIC_TOOL_USE_ID_MAX_LENGTH &&\n ANTHROPIC_TOOL_USE_ID_PATTERN.test(id)\n ) {\n return id;\n }\n const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, '_');\n const hash = createHash('sha256')\n .update(id)\n .digest('hex')\n .slice(0, ANTHROPIC_TOOL_USE_ID_HASH_LENGTH);\n const prefixMaxLength =\n ANTHROPIC_TOOL_USE_ID_MAX_LENGTH - ANTHROPIC_TOOL_USE_ID_HASH_LENGTH - 1;\n return `${sanitized.slice(0, prefixMaxLength)}_${hash}`;\n}\n\n/**\n * Lift any `cache_control` off the inner blocks of a tool result onto the\n * `tool_result` block itself. Anthropic documents the top-level\n * `messages.content` block as the cacheable position and does not document\n * caching of sub-content blocks; the API currently honors a nested marker, but\n * anchoring on the documented position keeps the single tail breakpoint robust\n * (and mirrors the Bedrock cachePoint hoist). The first marker found wins; it is\n * stripped from every inner block so exactly one survives, on the outer block.\n */\nfunction hoistToolResultCacheControl(\n content: string | MessageContentComplex[]\n): { content: string | MessageContentComplex[]; cacheControl: unknown } {\n if (!Array.isArray(content)) {\n return { content, cacheControl: undefined };\n }\n let cacheControl: unknown;\n const stripped = content.map((block) => {\n if ('cache_control' in block) {\n cacheControl ??= (block as Record<string, unknown>).cache_control;\n const clone = { ...(block as Record<string, unknown>) };\n delete clone.cache_control;\n return clone as MessageContentComplex;\n }\n return block;\n });\n // `stripped` is element-equal to `content` when no marker was present.\n return { content: stripped, cacheControl };\n}\n\nfunction _ensureMessageContents(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const updatedMsgs: BaseMessage[] = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = updatedMsgs[updatedMsgs.length - 1];\n if (\n previousMessage._getType() === 'human' &&\n Array.isArray(previousMessage.content) &&\n 'type' in previousMessage.content[0] &&\n previousMessage.content[0].type === 'tool_result'\n ) {\n // If the previous message was a tool result, we merge this tool message into it.\n (previousMessage.content as MessageContentComplex[]).push({\n type: 'tool_result',\n content: message.content,\n tool_use_id: normalizeAnthropicToolCallId(\n (message as ToolMessage).tool_call_id\n ),\n });\n } else {\n // If not, we create a new human message with the tool result.\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: message.content,\n tool_use_id: normalizeAnthropicToolCallId(\n (message as ToolMessage).tool_call_id\n ),\n },\n ],\n })\n );\n }\n } else {\n const toolMessageContent = (\n message as { content?: BaseMessage['content'] | null }\n ).content;\n // Hoist a tail cache_control off the inner content onto the\n // tool_result block itself (the documented cacheable position).\n const { content: hoistedContent, cacheControl } =\n toolMessageContent != null\n ? hoistToolResultCacheControl(_formatContent(message))\n : { content: undefined, cacheControl: undefined };\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n ...(hoistedContent != null ? { content: hoistedContent } : {}),\n ...(cacheControl != null\n ? { cache_control: cacheControl as { type: 'ephemeral' } }\n : {}),\n tool_use_id: normalizeAnthropicToolCallId(\n (message as ToolMessage).tool_call_id\n ),\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(message);\n }\n }\n return updatedMsgs as (SystemMessage | HumanMessage | AIMessage)[];\n}\n\nexport function _convertLangChainToolCallToAnthropic(\n toolCall: ToolCall\n): AnthropicToolResponse {\n if (toolCall.id === undefined) {\n throw new Error('Anthropic requires all tool calls to have an \"id\".');\n }\n const isServerTool = toolCall.id.startsWith(\n Constants.ANTHROPIC_SERVER_TOOL_PREFIX\n );\n return {\n type: isServerTool ? 'server_tool_use' : 'tool_use',\n id: isServerTool ? toolCall.id : normalizeAnthropicToolCallId(toolCall.id),\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\nconst standardContentBlockConverter: StandardContentBlockConverter<{\n text: AnthropicTextBlockParam;\n image: AnthropicImageBlockParam;\n file: AnthropicDocumentBlockParam;\n}> = {\n providerName: 'anthropic',\n\n fromStandardTextBlock(block: StandardTextBlock): AnthropicTextBlockParam {\n return {\n type: 'text',\n text: block.text,\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as AnthropicTextBlockParam;\n },\n\n fromStandardImageBlock(block: StandardImageBlock): AnthropicImageBlockParam {\n if (block.source_type === 'url') {\n const data = parseBase64DataUrl({\n dataUrl: block.url,\n asTypedArray: false,\n });\n if (data) {\n return {\n type: 'image',\n source: {\n type: 'base64',\n data: data.data,\n media_type: data.mime_type,\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as AnthropicImageBlockParam;\n } else {\n return {\n type: 'image',\n source: {\n type: 'url',\n url: block.url,\n media_type: block.mime_type ?? '',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as AnthropicImageBlockParam;\n }\n } else {\n if (block.source_type === 'base64') {\n return {\n type: 'image',\n source: {\n type: 'base64',\n data: block.data,\n media_type: block.mime_type ?? '',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as AnthropicImageBlockParam;\n } else {\n throw new Error(`Unsupported image source type: ${block.source_type}`);\n }\n }\n },\n\n fromStandardFileBlock(block: StandardFileBlock): AnthropicDocumentBlockParam {\n const mime_type = (block.mime_type ?? '').split(';')[0];\n\n if (block.source_type === 'url') {\n if (mime_type === 'application/pdf' || mime_type === '') {\n return {\n type: 'document',\n source: {\n type: 'url',\n url: block.url,\n media_type: block.mime_type ?? '',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('context' in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...('title' in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as AnthropicDocumentBlockParam;\n }\n throw new Error(\n `Unsupported file mime type for file url source: ${block.mime_type}`\n );\n } else if (block.source_type === 'text') {\n if (mime_type === 'text/plain' || mime_type === '') {\n return {\n type: 'document',\n source: {\n type: 'text',\n data: block.text,\n media_type: block.mime_type ?? '',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('context' in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...('title' in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as AnthropicDocumentBlockParam;\n } else {\n throw new Error(\n `Unsupported file mime type for file text source: ${block.mime_type}`\n );\n }\n } else if (block.source_type === 'base64') {\n if (mime_type === 'application/pdf' || mime_type === '') {\n return {\n type: 'document',\n source: {\n type: 'base64',\n data: block.data,\n media_type: 'application/pdf',\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('context' in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...('title' in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as AnthropicDocumentBlockParam;\n } else if (\n ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].includes(\n mime_type\n )\n ) {\n return {\n type: 'document',\n source: {\n type: 'content',\n content: [\n {\n type: 'image',\n source: {\n type: 'base64',\n data: block.data,\n media_type: mime_type as\n | 'image/jpeg'\n | 'image/png'\n | 'image/gif'\n | 'image/webp',\n },\n },\n ],\n },\n ...('cache_control' in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...('citations' in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...('context' in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...('title' in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as AnthropicDocumentBlockParam;\n } else {\n throw new Error(\n `Unsupported file mime type for file base64 source: ${block.mime_type}`\n );\n }\n } else {\n throw new Error(`Unsupported file source type: ${block.source_type}`);\n }\n },\n};\n\nfunction _formatContent(message: BaseMessage) {\n const toolTypes = [\n 'tool_use',\n 'tool_result',\n 'input_json_delta',\n 'server_tool_use',\n 'web_search_tool_result',\n 'web_search_result',\n ];\n const textTypes = ['text', 'text_delta'];\n /**\n * Reasoning blocks emitted by other providers — Bedrock's `reasoning_content`,\n * Google's `reasoning`, and LibreChat's `think`. Their signatures are\n * provider-specific and cannot be validated by Anthropic, so on a\n * cross-provider handoff (e.g. Bedrock → Anthropic) we drop them rather than\n * forwarding an unusable block. The receiving model produces its own thinking.\n */\n const foreignReasoningTypes = ['reasoning_content', 'reasoning', 'think'];\n const { content } = message;\n\n if (typeof content === 'string') {\n return content;\n } else {\n const contentParts = content as MessageContentComplex[];\n const contentBlocks = contentParts.map((contentPart) => {\n /**\n * Normalize server_tool_use blocks into a clean shape the API accepts.\n * These blocks may arrive with the correct type (server_tool_use) or mislabeled\n * as text/tool_use after chunk concatenation or state serialization.\n * Regardless of current type, if the id starts with 'srvtoolu_' we rebuild\n * a clean block with only the properties the API expects.\n */\n if (\n 'id' in contentPart &&\n typeof (contentPart as Record<string, unknown>).id === 'string' &&\n ((contentPart as Record<string, unknown>).id as string).startsWith(\n Constants.ANTHROPIC_SERVER_TOOL_PREFIX\n ) &&\n 'name' in contentPart\n ) {\n const rawPart = contentPart as Record<string, unknown>;\n let input = rawPart.input;\n if (typeof input === 'string') {\n try {\n input = JSON.parse(input);\n } catch {\n input = {};\n }\n }\n const corrected: AnthropicServerToolUseBlockParam = {\n type: 'server_tool_use',\n id: rawPart.id as string,\n name: (rawPart.name ?? 'web_search') as 'web_search',\n input: (input ?? {}) as Record<string, unknown>,\n };\n return corrected;\n }\n\n /**\n * Normalize web_search_tool_result blocks into a clean shape.\n * Same rationale as above — the block may carry extra properties from\n * streaming (input, index, etc.) that the API rejects. Rebuild cleanly.\n */\n if (\n 'tool_use_id' in contentPart &&\n typeof (contentPart as Record<string, unknown>).tool_use_id ===\n 'string' &&\n (\n (contentPart as Record<string, unknown>).tool_use_id as string\n ).startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) &&\n 'content' in contentPart\n ) {\n const rawPart = contentPart as Record<string, unknown>;\n const content = rawPart.content;\n const isValidContent =\n Array.isArray(content) ||\n (content != null &&\n typeof content === 'object' &&\n 'type' in content &&\n (content as Record<string, unknown>).type ===\n 'web_search_tool_result_error');\n\n if (isValidContent) {\n const corrected: AnthropicWebSearchToolResultBlockParam = {\n type: 'web_search_tool_result',\n tool_use_id: rawPart.tool_use_id as string,\n content:\n content as AnthropicWebSearchToolResultBlockParam['content'],\n };\n return corrected;\n }\n return null;\n }\n\n /**\n * Skip non-server malformed blocks that have tool fields mixed with text type.\n */\n if (\n 'id' in contentPart &&\n 'name' in contentPart &&\n 'input' in contentPart &&\n contentPart.type === 'text'\n ) {\n return null;\n }\n if (\n 'tool_use_id' in contentPart &&\n 'content' in contentPart &&\n contentPart.type === 'text'\n ) {\n return null;\n }\n\n // Core's v1 streaming aggregation can leave a partial tool-input delta as a\n // standalone block typed `text` carrying `input` but no `text`. The assembled\n // input is restored on the tool_use block from `message.tool_calls`, so drop it.\n if (\n contentPart.type === 'text' &&\n 'input' in contentPart &&\n !('text' in contentPart)\n ) {\n return null;\n }\n\n if (isDataContentBlock(contentPart)) {\n return convertToProviderContentBlock(\n contentPart,\n standardContentBlockConverter\n );\n }\n\n const cacheControl =\n 'cache_control' in contentPart ? contentPart.cache_control : undefined;\n\n if (contentPart.type === 'image_url') {\n let source;\n const imageUrl = (contentPart as ImageUrlContentBlock).image_url;\n if (typeof imageUrl === 'string') {\n source = _formatImage(imageUrl);\n } else {\n source = _formatImage(imageUrl.url);\n }\n return {\n type: 'image' as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n } else if (isAnthropicImageBlockParam(contentPart)) {\n return contentPart;\n } else if (contentPart.type === 'document') {\n // PDF\n return {\n ...contentPart,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n } else if (contentPart.type === 'thinking') {\n const thinkingPart = contentPart as AnthropicThinkingBlockParam;\n // Google thinking-enabled output reuses `type: 'thinking'` but carries\n // no Anthropic signature. Anthropic rejects an unsigned thinking block,\n // so on an assistant turn treat it as foreign reasoning and drop it\n // rather than forward an unusable block. Signed (Anthropic-native)\n // thinking is forwarded as before.\n const signature = (thinkingPart as { signature?: string }).signature;\n if (isAIMessage(message) && (signature == null || signature === '')) {\n return null;\n }\n const block: AnthropicThinkingBlockParam = {\n type: 'thinking' as const, // Explicitly setting the type as \"thinking\"\n thinking: thinkingPart.thinking,\n signature: thinkingPart.signature,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n return block;\n } else if (contentPart.type === 'redacted_thinking') {\n const redactedPart = contentPart as AnthropicRedactedThinkingBlockParam;\n const block: AnthropicRedactedThinkingBlockParam = {\n type: 'redacted_thinking' as const, // Explicitly setting the type as \"redacted_thinking\"\n data: redactedPart.data,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n return block;\n } else if (contentPart.type === 'search_result') {\n const searchResultPart = contentPart as AnthropicSearchResultBlockParam;\n const block: AnthropicSearchResultBlockParam = {\n type: 'search_result' as const,\n title: searchResultPart.title,\n source: searchResultPart.source,\n ...('cache_control' in contentPart &&\n contentPart.cache_control != null\n ? { cache_control: contentPart.cache_control }\n : {}),\n ...('citations' in contentPart && contentPart.citations != null\n ? { citations: contentPart.citations }\n : {}),\n content: searchResultPart.content,\n };\n return block;\n } else if (contentPart.type === 'compaction') {\n const compactionPart = contentPart as AnthropicCompactionBlockParam;\n const block: AnthropicCompactionBlockParam = {\n type: 'compaction' as const,\n content: compactionPart.content,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n };\n return block;\n } else if (\n textTypes.some((t) => t === contentPart.type) &&\n 'text' in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n return {\n type: 'text' as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n ...('citations' in contentPart && contentPart.citations != null\n ? { citations: contentPart.citations }\n : {}),\n };\n } else if (toolTypes.some((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n if ('index' in contentPartCopy) {\n // Anthropic does not support passing the index field here, so we remove it.\n delete contentPartCopy.index;\n }\n\n if (contentPartCopy.type === 'input_json_delta') {\n // Orphaned partial tool-input delta with no id of its own. The assembled\n // input is restored on the tool_use block from `message.tool_calls`; drop it.\n return null;\n }\n\n if (\n contentPartCopy.type === 'tool_use' &&\n 'id' in contentPartCopy &&\n typeof contentPartCopy.id === 'string' &&\n contentPartCopy.id.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)\n ) {\n contentPartCopy.type = 'server_tool_use';\n }\n\n // Core's streaming aggregation can leave the inline tool_use input empty\n // (the assembled arguments live in `message.tool_calls` or, for persisted\n // messages, in sibling input_json_delta blocks). Restore it when missing.\n if (\n contentPartCopy.type === 'tool_use' &&\n typeof contentPartCopy.id === 'string' &&\n (contentPartCopy.input === '' || contentPartCopy.input == null)\n ) {\n const matchingToolCall = isAIMessage(message)\n ? message.tool_calls?.find(\n (toolCall) => toolCall.id === contentPartCopy.id\n )\n : undefined;\n if (matchingToolCall) {\n contentPartCopy.input = matchingToolCall.args;\n } else {\n const blockIndex = (contentPart as Record<string, unknown>).index;\n const merged = contentParts\n .filter((part) => {\n const p = part as Record<string, unknown>;\n return (\n p.type === 'input_json_delta' &&\n p.index === blockIndex &&\n typeof p.input === 'string'\n );\n })\n .reduce(\n (acc, part) => acc + (part as Record<string, unknown>).input,\n ''\n );\n if (merged !== '') {\n contentPartCopy.input = merged;\n }\n }\n }\n\n if ('input' in contentPartCopy) {\n // Anthropic tool use inputs should be valid objects, when applicable.\n if (typeof contentPartCopy.input === 'string') {\n try {\n contentPartCopy.input = JSON.parse(contentPartCopy.input);\n } catch {\n contentPartCopy.input = {};\n }\n }\n }\n\n /**\n * For multi-turn conversations with citations, we must preserve ALL blocks\n * including server_tool_use, web_search_tool_result, and web_search_result.\n * Citations reference search results by index, so filtering changes indices and breaks references.\n *\n * The ToolNode already handles skipping server tool invocations via the srvtoolu_ prefix check.\n */\n\n // TODO: Fix when SDK types are fixed\n return {\n ...contentPartCopy,\n ...(cacheControl != null ? { cache_control: cacheControl } : {}),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else if (\n 'functionCall' in contentPart &&\n contentPart.functionCall != null &&\n typeof contentPart.functionCall === 'object' &&\n isAIMessage(message)\n ) {\n const functionCallPart = contentPart as GoogleFunctionCallBlock;\n const correspondingToolCall = message.tool_calls?.find(\n (toolCall) => toolCall.name === functionCallPart.functionCall.name\n );\n if (!correspondingToolCall) {\n throw new Error(\n `Could not find tool call for function call ${functionCallPart.functionCall.name}`\n );\n }\n // Google GenAI models include a `functionCall` object inside content. We should ignore it as Anthropic will not support it.\n return {\n id: correspondingToolCall.id,\n type: 'tool_use',\n name: correspondingToolCall.name,\n input: functionCallPart.functionCall.args,\n };\n } else if (\n isAIMessage(message) &&\n foreignReasoningTypes.some((t) => t === contentPart.type)\n ) {\n // Foreign reasoning on an ASSISTANT turn (Bedrock `reasoning_content`,\n // Google `reasoning`, LibreChat `think`) carries provider-specific\n // signatures Anthropic cannot validate; drop it so a cross-provider\n // handoff doesn't crash. The same types on a user/tool turn are real\n // input and fall through to the throw below rather than being silently\n // dropped — as does any other unknown block (user media, Google\n // code-execution), which must be surfaced, not discarded.\n return null;\n } else {\n console.error(\n 'Unsupported content part:',\n JSON.stringify(contentPart, null, 2)\n );\n throw new Error('Unsupported message content format');\n }\n });\n const filteredContentBlocks = contentBlocks.filter(\n (block) =>\n block !== null &&\n !(\n block.type === 'text' &&\n 'text' in block &&\n typeof block.text === 'string' &&\n block.text.trim() === ''\n )\n );\n return filteredContentBlocks.length > 0\n ? filteredContentBlocks\n : [{ type: 'text' as const, text: ANTHROPIC_EMPTY_TEXT_PLACEHOLDER }];\n }\n}\n\n/**\n * Formats messages as a prompt for the model.\n * Used in LangSmith, export is important here.\n * @param messages The base messages to format as a prompt.\n * @returns The formatted prompt.\n */\nexport function _convertMessagesToAnthropicPayload(\n messages: BaseMessage[]\n): AnthropicMessageCreateParams {\n const mergedMessages = _ensureMessageContents(messages);\n let system;\n if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {\n system = messages[0].content;\n }\n const conversationMessages =\n system !== undefined ? mergedMessages.slice(1) : mergedMessages;\n const formattedMessages = conversationMessages.map((message) => {\n let role;\n if (message._getType() === 'human') {\n role = 'user' as const;\n } else if (message._getType() === 'ai') {\n role = 'assistant' as const;\n } else if (message._getType() === 'tool') {\n role = 'user' as const;\n } else if (message._getType() === 'system') {\n throw new Error(\n 'System messages are only permitted as the first passed message.'\n );\n } else {\n throw new Error(`Message type \"${message._getType()}\" is not supported.`);\n }\n const isAI = isAIMessage(message);\n const toolCalls = isAI ? (message.tool_calls ?? []) : [];\n if (isAI && toolCalls.length > 0) {\n if (typeof message.content === 'string') {\n const clientToolCalls = toolCalls.filter(\n (tc) =>\n !(\n tc.id?.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) ?? false\n )\n );\n if (message.content === '') {\n return {\n role,\n content:\n clientToolCalls.length > 0\n ? clientToolCalls.map(_convertLangChainToolCallToAnthropic)\n : [\n {\n type: 'text' as const,\n text: ANTHROPIC_EMPTY_TEXT_PLACEHOLDER,\n },\n ],\n };\n } else {\n return {\n role,\n content: [\n { type: 'text' as const, text: message.content },\n ...clientToolCalls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const formattedContent = _formatContent(message);\n const formattedBlocks = Array.isArray(formattedContent)\n ? formattedContent\n : [];\n // Tool calls already materialized as content blocks by `_formatContent`.\n // Derived from the FORMATTED output (not the raw content by type) so\n // that Google `functionCall` parts — which `_formatContent` converts\n // into `tool_use` — count as represented and are not appended twice.\n const representedToolIds = new Set(\n formattedBlocks\n .filter(\n (block) =>\n block != null &&\n (block.type === 'tool_use' || block.type === 'server_tool_use')\n )\n .map((block) => (block as { id?: string }).id)\n );\n // Client tool calls present in `tool_calls` but absent from the\n // formatted content — e.g. a Bedrock extended-thinking turn records the\n // tool only on `tool_calls` and leaves `content` as just the reasoning\n // block. Without materializing them, dropping that reasoning block\n // silently loses the (handoff) tool call instead of forwarding it.\n const unrepresentedToolCalls = toolCalls.filter(\n (toolCall) =>\n !(\n toolCall.id?.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) ??\n false\n ) && !representedToolIds.has(toolCall.id)\n );\n if (unrepresentedToolCalls.length === 0) {\n return { role, content: formattedContent };\n }\n const existingBlocks = formattedBlocks.filter(\n (block) =>\n !(\n block != null &&\n block.type === 'text' &&\n 'text' in block &&\n block.text === ANTHROPIC_EMPTY_TEXT_PLACEHOLDER\n )\n );\n return {\n role,\n content: [\n ...existingBlocks,\n ...unrepresentedToolCalls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n return {\n role,\n content: _formatContent(message),\n };\n }\n });\n return {\n messages: mergeMessages(formattedMessages),\n system,\n } as AnthropicMessageCreateParams;\n}\n\nexport function modelDisallowsAssistantPrefill(model?: string): boolean {\n const modelId = model ?? '';\n if (CLAUDE_4_RELEASE_DATE_MODEL_PATTERN.test(modelId)) {\n return false;\n }\n\n const match = CLAUDE_4_MINOR_MODEL_PATTERN.exec(modelId);\n if (!match) {\n return false;\n }\n return Number(match[1]) >= 6;\n}\n\nfunction messagesHaveCacheControl(\n messages: AnthropicMessageCreateParams['messages']\n): boolean {\n return messages.some(\n (message) =>\n Array.isArray(message.content) &&\n message.content.some((block) => 'cache_control' in block)\n );\n}\n\n/** Anthropic rejects cache_control on these reasoning blocks. */\nconst NON_CACHEABLE_PAYLOAD_BLOCK_TYPES = new Set([\n 'thinking',\n 'redacted_thinking',\n]);\n\n/**\n * Place one ephemeral `cache_control` on the last cacheable block of the final\n * message of an already-converted Anthropic payload. Used to re-anchor the tail\n * breakpoint after a trailing assistant prefill is stripped. Operates on the\n * post-conversion payload, where blocks the converter drops (foreign reasoning,\n * input_json_delta) are already gone — only native thinking blocks must be\n * skipped. Returns a new array only when it actually places a marker.\n */\nfunction reanchorTailCacheControl(\n messages: AnthropicMessageCreateParams['messages'],\n ttl?: '1h'\n): AnthropicMessageCreateParams['messages'] {\n if (messages.length === 0) {\n return messages;\n }\n const cacheControl =\n ttl === '1h'\n ? ({ type: 'ephemeral', ttl: '1h' } as const)\n : ({ type: 'ephemeral' } as const);\n const lastIndex = messages.length - 1;\n const tail = messages[lastIndex];\n const content = tail.content;\n\n if (typeof content === 'string') {\n if (content.trim() === '') {\n return messages;\n }\n const next = [...messages];\n next[lastIndex] = {\n ...tail,\n content: [{ type: 'text', text: content, cache_control: cacheControl }],\n } as (typeof messages)[number];\n return next;\n }\n\n if (!Array.isArray(content)) {\n return messages;\n }\n\n let anchor = -1;\n for (let i = 0; i < content.length; i++) {\n const type = (content[i] as { type?: string }).type;\n if (type == null || NON_CACHEABLE_PAYLOAD_BLOCK_TYPES.has(type)) {\n continue;\n }\n if (\n type === 'text' &&\n ((content[i] as { text?: string }).text ?? '').trim() === ''\n ) {\n continue;\n }\n anchor = i;\n }\n if (anchor < 0) {\n return messages;\n }\n\n const next = [...messages];\n next[lastIndex] = {\n ...tail,\n content: content.map((block, i) =>\n i === anchor ? { ...block, cache_control: cacheControl } : block\n ),\n } as (typeof messages)[number];\n return next;\n}\n\n/**\n * Find the extended-cache TTL (`'1h'`) carried by an existing `cache_control`\n * breakpoint, so {@link reanchorTailCacheControl} can re-apply the same TTL the\n * stripped prefill had. Returns `undefined` for the legacy 5-minute default\n * (no `ttl`), keeping that path byte-identical to before.\n */\nfunction findCacheControlTtl(\n messages: AnthropicMessageCreateParams['messages']\n): '1h' | undefined {\n for (const message of messages) {\n if (!Array.isArray(message.content)) {\n continue;\n }\n for (const block of message.content) {\n const cacheControl = (block as { cache_control?: { ttl?: unknown } })\n .cache_control;\n if (cacheControl?.ttl === '1h') {\n return '1h';\n }\n }\n }\n return undefined;\n}\n\nexport function stripUnsupportedAssistantPrefill<\n T extends Pick<AnthropicMessageCreateParams, 'messages'> & { model?: string },\n>(request: T): T {\n if (!modelDisallowsAssistantPrefill(request.model)) {\n return request;\n }\n\n const messages = request.messages;\n if (\n messages.length <= 1 ||\n messages[messages.length - 1]?.role !== 'assistant'\n ) {\n return request;\n }\n\n const nextMessages = [...messages];\n while (\n nextMessages.length > 1 &&\n nextMessages[nextMessages.length - 1]?.role === 'assistant'\n ) {\n nextMessages.pop();\n }\n\n /**\n * If a single tail prompt-cache breakpoint rode the stripped assistant\n * prefill, the survivors may now carry no `cache_control` at all, dropping\n * message caching for this request. Re-anchor the breakpoint on the new tail\n * (only when one was actually lost, so caching-off requests stay untouched).\n */\n const reanchored =\n messagesHaveCacheControl(messages) &&\n !messagesHaveCacheControl(nextMessages)\n ? reanchorTailCacheControl(nextMessages, findCacheControlTtl(messages))\n : nextMessages;\n\n return {\n ...request,\n messages: reanchored,\n };\n}\n\nfunction mergeMessages(messages: AnthropicMessageCreateParams['messages']) {\n if (messages.length <= 1) {\n return messages;\n }\n\n const result: AnthropicMessageCreateParams['messages'] = [];\n let currentMessage = messages[0];\n\n type ContentBlocks = Exclude<\n AnthropicMessageCreateParams['messages'][number]['content'],\n string\n >;\n const normalizeContent = (\n content: AnthropicMessageCreateParams['messages'][number]['content']\n ): ContentBlocks => {\n if (typeof content === 'string') {\n return [{ type: 'text', text: content }];\n }\n return content;\n };\n\n const isToolResultMessage = (msg: (typeof messages)[0]) => {\n if (msg.role !== 'user') return false;\n\n if (typeof msg.content === 'string') {\n return false;\n }\n\n return (\n Array.isArray(msg.content) &&\n msg.content.every((item) => item.type === 'tool_result')\n );\n };\n\n for (let i = 1; i < messages.length; i += 1) {\n const nextMessage = messages[i];\n\n if (\n isToolResultMessage(currentMessage) &&\n isToolResultMessage(nextMessage)\n ) {\n // Merge the messages by combining their content arrays\n currentMessage = {\n ...currentMessage,\n content: [\n ...normalizeContent(currentMessage.content),\n ...normalizeContent(nextMessage.content),\n ],\n };\n } else {\n result.push(currentMessage);\n currentMessage = nextMessage;\n }\n }\n\n result.push(currentMessage);\n return result;\n}\n"],"mappings":";;;;;;;;;AAkDA,MAAM,mCAAmC;AACzC,MAAM,sCACJ;AACF,MAAM,+BACJ;AAEF,SAAS,aAAa,UAAkB;CACtC,MAAM,UAAA,GAAA,yBAAA,mBAAA,CAA4B,EAAE,SAAS,SAAS,CAAC;CACvD,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AE3D;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK,GAAG;EAC3C,MAAM,cAAc,SAAS;EAE7B,IACE,oBAAoB,cAAc,KAClC,oBAAoB,WAAW,GAG/B,iBAAiB;GACf,GAAG;GACH,SAAS,CACP,GAAG,iBAAiB,eAAe,OAAO,GAC1C,GAAG,iBAAiB,YAAY,OAAO,CACzC;EACF;OACK;GACL,OAAO,KAAK,cAAc;GAC1B,iBAAiB;EACnB;CACF;CAEA,OAAO,KAAK,cAAc;CAC1B,OAAO;AACT"}
@@ -1,3 +1,4 @@
1
+ const require_cache = require("../../messages/cache.cjs");
1
2
  const require_toolCache = require("./toolCache.cjs");
2
3
  const require_message_inputs = require("./utils/message_inputs.cjs");
3
4
  const require_message_outputs = require("./utils/message_outputs.cjs");
@@ -35,6 +36,10 @@ var CustomChatBedrockConverse = class extends _langchain_aws.ChatBedrockConverse
35
36
  */
36
37
  promptCache;
37
38
  /**
39
+ * Prompt-cache checkpoint TTL (`'5m'` legacy or `'1h'` extended cache).
40
+ */
41
+ promptCacheTtl;
42
+ /**
38
43
  * Application Inference Profile ARN to use instead of model ID.
39
44
  */
40
45
  applicationInferenceProfile;
@@ -45,6 +50,7 @@ var CustomChatBedrockConverse = class extends _langchain_aws.ChatBedrockConverse
45
50
  constructor(fields) {
46
51
  super(fields);
47
52
  this.promptCache = fields?.promptCache;
53
+ this.promptCacheTtl = fields?.promptCacheTtl;
48
54
  this.applicationInferenceProfile = fields?.applicationInferenceProfile;
49
55
  this.serviceTier = fields?.serviceTier;
50
56
  }
@@ -63,7 +69,7 @@ var CustomChatBedrockConverse = class extends _langchain_aws.ChatBedrockConverse
63
69
  */
64
70
  invocationParams(options) {
65
71
  const baseParams = super.invocationParams(options);
66
- const toolConfig = this.promptCache === true ? require_toolCache.insertBedrockToolCachePoint(baseParams.toolConfig, true) : baseParams.toolConfig;
72
+ const toolConfig = this.promptCache === true ? require_toolCache.insertBedrockToolCachePoint(baseParams.toolConfig, true, require_cache.resolvePromptCacheTtl(this.promptCacheTtl)) : baseParams.toolConfig;
67
73
  /** Service tier from options or fall back to class-level setting */
68
74
  const serviceTierType = options?.serviceTier ?? this.serviceTier;
69
75
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["ChatBedrockConverse","insertBedrockToolCachePoint","convertToConverseMessages","ConverseStreamCommand","handleConverseStreamContentBlockStart","handleConverseStreamContentBlockDelta","handleConverseStreamMetadata","createConverseToolUseStopChunk","ChatGenerationChunk","AIMessageChunk"],"sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes content block merging for\n * streaming responses and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock's `@langchain/aws` library does not include an `index` property on content\n * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append\n * each streaming chunk as a separate array entry instead of merging by index.\n *\n * This wrapper takes full ownership of the stream by directly interfacing with the\n * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that\n * include `contentBlockIndex` in response_metadata for every delta type. It then\n * promotes `contentBlockIndex` to an `index` property on each content block\n * (mirroring Anthropic's pattern) and strips it from metadata to avoid\n * `_mergeDicts` conflicts.\n *\n * When multiple content block types are present (e.g. reasoning + text), text deltas\n * are promoted from strings to array form with `index` so they merge correctly once\n * the accumulated content is already an array.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport {\n ConverseStreamCommand,\n type GuardrailConfiguration,\n type GuardrailStreamConfiguration,\n} from '@aws-sdk/client-bedrock-runtime';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, ResponseMetadata } from '@langchain/core/messages';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport {\n convertToConverseMessages,\n createConverseToolUseStopChunk,\n handleConverseStreamContentBlockStart,\n handleConverseStreamContentBlockDelta,\n handleConverseStreamMetadata,\n} from './utils';\nimport { insertBedrockToolCachePoint } from './toolCache';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\nexport type CustomGuardrailConfiguration = GuardrailConfiguration &\n Pick<GuardrailStreamConfiguration, 'streamProcessingMode'>;\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Enables Bedrock prompt cache checkpoints for message and tool prefixes.\n */\n promptCache?: boolean;\n\n /**\n * Guardrail configuration for Converse and ConverseStream invocations.\n * `streamProcessingMode` is only used by ConverseStream.\n */\n guardrailConfig?: CustomGuardrailConfiguration;\n\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n guardrailConfig?: CustomGuardrailConfiguration;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Whether to insert Bedrock prompt cache checkpoints when available.\n */\n promptCache?: boolean;\n\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.promptCache = fields?.promptCache;\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n const toolConfig =\n this.promptCache === true\n ? insertBedrockToolCachePoint(baseParams.toolConfig, true)\n : baseParams.toolConfig;\n\n /** Service tier from options or fall back to class-level setting */\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n toolConfig,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n * Uses the same model-swapping pattern as streaming for consistency.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n return await super._generateNonStreaming(messages, options, runManager);\n } finally {\n this.model = originalModel;\n }\n }\n\n /**\n * Own the stream end-to-end so we have direct access to every\n * `contentBlockDelta.contentBlockIndex` from the AWS SDK.\n *\n * This replaces the parent's implementation which strips contentBlockIndex\n * from text and reasoning deltas, making it impossible to merge correctly.\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n let { streamUsage } = this;\n if ((options as Record<string, unknown>).streamUsage !== undefined) {\n streamUsage = (options as Record<string, unknown>).streamUsage as boolean;\n }\n\n const modelId = this.getModelId();\n\n const command = new ConverseStreamCommand({\n modelId,\n messages: converseMessages,\n system: converseSystem,\n ...(params as Record<string, unknown>),\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n if (!response.stream) {\n return;\n }\n\n const seenBlockIndices = new Set<number>();\n const toolUseBlockIndices = new Set<number>();\n /**\n * Guardrails can reject an already-streamed toolUse block at\n * `messageStop` (`guardrail_intervened`), after `contentBlockStop` has\n * passed. Only emit eager-execution seals when no guardrails are\n * configured, so a later intervention can't race an eagerly started tool.\n */\n const sealToolUseOnStop =\n options.guardrailConfig == null && this.guardrailConfig == null;\n\n for await (const event of response.stream) {\n if (event.contentBlockStart != null) {\n const startChunk = handleConverseStreamContentBlockStart(\n event.contentBlockStart\n );\n if (startChunk != null) {\n const idx = event.contentBlockStart.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n if (event.contentBlockStart.start?.toolUse != null) {\n toolUseBlockIndices.add(idx);\n }\n }\n yield this.enrichChunk(startChunk, seenBlockIndices);\n\n // Registered stream handlers receive chunks through callback\n // events, not the yielded generator — dispatch the start chunk so\n // they see the tool call's id/name (eager chunk state needs both).\n await runManager?.handleLLMNewToken(\n startChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: startChunk }\n );\n }\n } else if (event.contentBlockDelta != null) {\n const deltaChunk = handleConverseStreamContentBlockDelta(\n event.contentBlockDelta\n );\n\n const idx = event.contentBlockDelta.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n }\n\n yield this.enrichChunk(deltaChunk, seenBlockIndices);\n\n await runManager?.handleLLMNewToken(\n deltaChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: deltaChunk }\n );\n } else if (event.metadata != null) {\n yield handleConverseStreamMetadata(event.metadata, { streamUsage });\n } else if (event.contentBlockStop != null) {\n const stopIdx = event.contentBlockStop.contentBlockIndex;\n if (stopIdx != null) {\n seenBlockIndices.add(stopIdx);\n if (sealToolUseOnStop && toolUseBlockIndices.has(stopIdx)) {\n // Converse guarantees the block's input is complete at stop, so\n // emit an explicit seal chunk for eager tool execution — through\n // the callback path too, for registered stream handlers.\n const sealChunk = createConverseToolUseStopChunk(stopIdx);\n yield sealChunk;\n await runManager?.handleLLMNewToken(\n sealChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: sealChunk }\n );\n }\n }\n } else {\n yield new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n response_metadata: { ...event } as ResponseMetadata,\n }),\n });\n }\n }\n }\n\n /**\n * Inject `index` on content blocks for proper merge behaviour, then strip\n * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.\n *\n * Text string content is promoted to array form only when the stream contains\n * multiple content block indices (e.g. reasoning at index 0, text at index 1),\n * ensuring text merges correctly with the already-array accumulated content.\n */\n private enrichChunk(\n chunk: ChatGenerationChunk,\n seenBlockIndices: Set<number>\n ): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const blockIndex = this.extractContentBlockIndex(metadata);\n const hasMetadataIndex = blockIndex != null;\n\n let content: AIMessageChunk['content'] = message.content;\n let contentModified = false;\n\n if (Array.isArray(content) && blockIndex != null) {\n content = content.map((block) =>\n typeof block === 'object' && !('index' in block)\n ? { ...block, index: blockIndex }\n : block\n );\n contentModified = true;\n } else if (\n typeof content === 'string' &&\n content !== '' &&\n blockIndex != null &&\n seenBlockIndices.size > 1\n ) {\n content = [{ type: 'text', text: content, index: blockIndex }];\n contentModified = true;\n }\n\n if (!contentModified && !hasMetadataIndex) {\n return chunk;\n }\n\n const cleanedMetadata = hasMetadataIndex\n ? (this.removeContentBlockIndex(metadata) as Record<string, unknown>)\n : metadata;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n content,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Extract `contentBlockIndex` from the top level of response_metadata.\n * Our custom handlers always place it at the top level.\n */\n private extractContentBlockIndex(\n metadata: Record<string, unknown>\n ): number | undefined {\n if (\n 'contentBlockIndex' in metadata &&\n typeof metadata.contentBlockIndex === 'number'\n ) {\n return metadata.contentBlockIndex;\n }\n return undefined;\n }\n\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyGA,IAAa,4BAAb,cAA+CA,eAAAA,oBAAoB;;;;CAIjE;;;;CAKA;;;;CAKA;CAEA,YAAY,QAAyC;EACnD,MAAM,MAAM;EACZ,KA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AG7D,IAAI,OAAO,QAAQ,UAAU;GAC3B,MAAM,UAAmC,CAAC;GAC1C,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,GAAG,GAC3C,IAAI,QAAQ,qBACV,QAAQ,OAAO,KAAK,wBAAwB,KAAK;GAGrD,OAAO;EACT;EAEA,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"index.cjs","names":["ChatBedrockConverse","insertBedrockToolCachePoint","resolvePromptCacheTtl","convertToConverseMessages","ConverseStreamCommand","handleConverseStreamContentBlockStart","handleConverseStreamContentBlockDelta","handleConverseStreamMetadata","createConverseToolUseStopChunk","ChatGenerationChunk","AIMessageChunk"],"sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes content block merging for\n * streaming responses and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock's `@langchain/aws` library does not include an `index` property on content\n * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append\n * each streaming chunk as a separate array entry instead of merging by index.\n *\n * This wrapper takes full ownership of the stream by directly interfacing with the\n * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that\n * include `contentBlockIndex` in response_metadata for every delta type. It then\n * promotes `contentBlockIndex` to an `index` property on each content block\n * (mirroring Anthropic's pattern) and strips it from metadata to avoid\n * `_mergeDicts` conflicts.\n *\n * When multiple content block types are present (e.g. reasoning + text), text deltas\n * are promoted from strings to array form with `index` so they merge correctly once\n * the accumulated content is already an array.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport {\n ConverseStreamCommand,\n type GuardrailConfiguration,\n type GuardrailStreamConfiguration,\n} from '@aws-sdk/client-bedrock-runtime';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, ResponseMetadata } from '@langchain/core/messages';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport {\n convertToConverseMessages,\n createConverseToolUseStopChunk,\n handleConverseStreamContentBlockStart,\n handleConverseStreamContentBlockDelta,\n handleConverseStreamMetadata,\n} from './utils';\nimport { resolvePromptCacheTtl, type PromptCacheTtl } from '@/messages/cache';\nimport { insertBedrockToolCachePoint } from './toolCache';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\nexport type CustomGuardrailConfiguration = GuardrailConfiguration &\n Pick<GuardrailStreamConfiguration, 'streamProcessingMode'>;\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Enables Bedrock prompt cache checkpoints for message and tool prefixes.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL. Defaults to `'1h'` (extended cache) when\n * `promptCache` is enabled; set `'5m'` for the legacy 5-minute behavior.\n * Bedrock models that don't support the 1-hour TTL downgrade to 5m\n * server-side (verified on Sonnet/Opus 4.6), so the default is safe to leave\n * on; use `'5m'` for any model that rejects it.\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Guardrail configuration for Converse and ConverseStream invocations.\n * `streamProcessingMode` is only used by ConverseStream.\n */\n guardrailConfig?: CustomGuardrailConfiguration;\n\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n guardrailConfig?: CustomGuardrailConfiguration;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Whether to insert Bedrock prompt cache checkpoints when available.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL (`'5m'` legacy or `'1h'` extended cache).\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.promptCache = fields?.promptCache;\n this.promptCacheTtl = fields?.promptCacheTtl;\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n const toolConfig =\n this.promptCache === true\n ? insertBedrockToolCachePoint(\n baseParams.toolConfig,\n true,\n resolvePromptCacheTtl(this.promptCacheTtl)\n )\n : baseParams.toolConfig;\n\n /** Service tier from options or fall back to class-level setting */\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n toolConfig,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n * Uses the same model-swapping pattern as streaming for consistency.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n return await super._generateNonStreaming(messages, options, runManager);\n } finally {\n this.model = originalModel;\n }\n }\n\n /**\n * Own the stream end-to-end so we have direct access to every\n * `contentBlockDelta.contentBlockIndex` from the AWS SDK.\n *\n * This replaces the parent's implementation which strips contentBlockIndex\n * from text and reasoning deltas, making it impossible to merge correctly.\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n let { streamUsage } = this;\n if ((options as Record<string, unknown>).streamUsage !== undefined) {\n streamUsage = (options as Record<string, unknown>).streamUsage as boolean;\n }\n\n const modelId = this.getModelId();\n\n const command = new ConverseStreamCommand({\n modelId,\n messages: converseMessages,\n system: converseSystem,\n ...(params as Record<string, unknown>),\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n if (!response.stream) {\n return;\n }\n\n const seenBlockIndices = new Set<number>();\n const toolUseBlockIndices = new Set<number>();\n /**\n * Guardrails can reject an already-streamed toolUse block at\n * `messageStop` (`guardrail_intervened`), after `contentBlockStop` has\n * passed. Only emit eager-execution seals when no guardrails are\n * configured, so a later intervention can't race an eagerly started tool.\n */\n const sealToolUseOnStop =\n options.guardrailConfig == null && this.guardrailConfig == null;\n\n for await (const event of response.stream) {\n if (event.contentBlockStart != null) {\n const startChunk = handleConverseStreamContentBlockStart(\n event.contentBlockStart\n );\n if (startChunk != null) {\n const idx = event.contentBlockStart.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n if (event.contentBlockStart.start?.toolUse != null) {\n toolUseBlockIndices.add(idx);\n }\n }\n yield this.enrichChunk(startChunk, seenBlockIndices);\n\n // Registered stream handlers receive chunks through callback\n // events, not the yielded generator — dispatch the start chunk so\n // they see the tool call's id/name (eager chunk state needs both).\n await runManager?.handleLLMNewToken(\n startChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: startChunk }\n );\n }\n } else if (event.contentBlockDelta != null) {\n const deltaChunk = handleConverseStreamContentBlockDelta(\n event.contentBlockDelta\n );\n\n const idx = event.contentBlockDelta.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n }\n\n yield this.enrichChunk(deltaChunk, seenBlockIndices);\n\n await runManager?.handleLLMNewToken(\n deltaChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: deltaChunk }\n );\n } else if (event.metadata != null) {\n yield handleConverseStreamMetadata(event.metadata, { streamUsage });\n } else if (event.contentBlockStop != null) {\n const stopIdx = event.contentBlockStop.contentBlockIndex;\n if (stopIdx != null) {\n seenBlockIndices.add(stopIdx);\n if (sealToolUseOnStop && toolUseBlockIndices.has(stopIdx)) {\n // Converse guarantees the block's input is complete at stop, so\n // emit an explicit seal chunk for eager tool execution — through\n // the callback path too, for registered stream handlers.\n const sealChunk = createConverseToolUseStopChunk(stopIdx);\n yield sealChunk;\n await runManager?.handleLLMNewToken(\n sealChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: sealChunk }\n );\n }\n }\n } else {\n yield new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n response_metadata: { ...event } as ResponseMetadata,\n }),\n });\n }\n }\n }\n\n /**\n * Inject `index` on content blocks for proper merge behaviour, then strip\n * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.\n *\n * Text string content is promoted to array form only when the stream contains\n * multiple content block indices (e.g. reasoning at index 0, text at index 1),\n * ensuring text merges correctly with the already-array accumulated content.\n */\n private enrichChunk(\n chunk: ChatGenerationChunk,\n seenBlockIndices: Set<number>\n ): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const blockIndex = this.extractContentBlockIndex(metadata);\n const hasMetadataIndex = blockIndex != null;\n\n let content: AIMessageChunk['content'] = message.content;\n let contentModified = false;\n\n if (Array.isArray(content) && blockIndex != null) {\n content = content.map((block) =>\n typeof block === 'object' && !('index' in block)\n ? { ...block, index: blockIndex }\n : block\n );\n contentModified = true;\n } else if (\n typeof content === 'string' &&\n content !== '' &&\n blockIndex != null &&\n seenBlockIndices.size > 1\n ) {\n content = [{ type: 'text', text: content, index: blockIndex }];\n contentModified = true;\n }\n\n if (!contentModified && !hasMetadataIndex) {\n return chunk;\n }\n\n const cleanedMetadata = hasMetadataIndex\n ? (this.removeContentBlockIndex(metadata) as Record<string, unknown>)\n : metadata;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n content,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Extract `contentBlockIndex` from the top level of response_metadata.\n * Our custom handlers always place it at the top level.\n */\n private extractContentBlockIndex(\n metadata: Record<string, unknown>\n ): number | undefined {\n if (\n 'contentBlockIndex' in metadata &&\n typeof metadata.contentBlockIndex === 'number'\n ) {\n return metadata.contentBlockIndex;\n }\n return undefined;\n }\n\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmHA,IAAa,4BAAb,cAA+CA,eAAAA,oBAAoB;;;;CAIjE;;;;CAKA;;;;CAKA;;;;CAKA;CAEA,YAAY,QAAyC;EACnD,MAAM,MAAM;EACZ,KAAK,cAAc,QAAQ;EAC3B,KAAK,iBAAiB,QAAQ;EAC9B,KAAK,8BAA8B,QAAQ;EAC3C,KAAK,cAAc,QAAQ;CAC7B;CAEA,OAAO,UAAkB;EACvB,OAAO;CACT;;;;;CAMA,aAA+B;EAC7B,OAAO,KAAK,+BAA+B,KAAK;CAClD;;;;CAKA,iBACE,SAGA;EACA,MAAM,aAAa,MAAM,iBAAiB,OAAO;EACjD,MAAM,aACJ,KAAK,gBAAgB,OACjBC,kBAAAA,4BACA,WAAW,YACX,MACAC,cAAAA,sBAAsB,KAAK,cAAc,CAC3C,IACE,WAAW;;EAGjB,MAAM,kBAAkB,SAAS,eAAe,KAAK;EAErD,OAAO;GACL,GAAG;GACH;GACA,aAAa,kBAAkB,EAAE,MAAM,gBAAgB,IAAI,KAAA;EAC7D;CACF;;;;;CAMA,MAAe,sBACb,UACA,SACA,YACqB;EACrB,MAAM,gBAAgB,KAAK;EAC3B,IACE,KAAK,+BAA+B,QACpC,KAAK,gCAAgC,IAErC,KAAK,QAAQ,KAAK;EAGpB,IAAI;GACF,OAAO,MAAM,MAAM,sBAAsB,UAAU,SAAS,UAAU;EACx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AM;IAAS,OAAO;GAAW,CAAC;GAC7D,kBAAkB;EACpB;EAEA,IAAI,CAAC,mBAAmB,CAAC,kBACvB,OAAO;EAGT,MAAM,kBAAkB,mBACnB,KAAK,wBAAwB,QAAQ,IACtC;EAEJ,OAAO,IAAID,wBAAAA,oBAAoB;GAC7B,MAAM,MAAM;GACZ,SAAS,IAAIC,yBAAAA,eAAe;IAC1B,GAAG;IACH;IACA,mBAAmB;GACrB,CAAC;GACD,gBAAgB,MAAM;EACxB,CAAC;CACH;;;;;CAMA,yBACE,UACoB;EACpB,IACE,uBAAuB,YACvB,OAAO,SAAS,sBAAsB,UAEtC,OAAO,SAAS;CAGpB;CAEA,wBAAgC,KAAuB;EACrD,IAAI,QAAQ,QAAQ,QAAQ,KAAA,GAC1B,OAAO;EAGT,IAAI,MAAM,QAAQ,GAAG,GACnB,OAAO,IAAI,KAAK,SAAS,KAAK,wBAAwB,IAAI,CAAC;EAG7D,IAAI,OAAO,QAAQ,UAAU;GAC3B,MAAM,UAAmC,CAAC;GAC1C,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,GAAG,GAC3C,IAAI,QAAQ,qBACV,QAAQ,OAAO,KAAK,wBAAwB,KAAK;GAGrD,OAAO;EACT;EAEA,OAAO;CACT;AACF"}
@@ -1,6 +1,6 @@
1
+ const require_cache = require("../../messages/cache.cjs");
1
2
  const require_index = require("../openai/index.cjs");
2
3
  //#region src/llm/bedrock/toolCache.ts
3
- const CACHE_POINT = { cachePoint: { type: "default" } };
4
4
  const BEDROCK_TOOL_CACHE_MARKER = "__lc_bedrock_cache_point_after";
5
5
  const BEDROCK_TOOL_CACHE_DISABLED_MARKER = "__lc_bedrock_skip_tool_cache";
6
6
  function isBedrockToolSpec(tool) {
@@ -63,9 +63,10 @@ function partitionAndMarkBedrockToolCache(tools, isDeferred) {
63
63
  staticTools[staticTools.length - 1] = markCachePointAfter(staticTools[staticTools.length - 1]);
64
64
  return [...staticTools, ...deferredTools];
65
65
  }
66
- function insertBedrockToolCachePoint(toolConfig, fallbackToEnd) {
66
+ function insertBedrockToolCachePoint(toolConfig, fallbackToEnd, ttl) {
67
67
  const tools = toolConfig?.tools;
68
68
  if (tools == null || tools.length === 0) return toolConfig;
69
+ const cachePoint = { cachePoint: require_cache.buildBedrockCachePoint(ttl) };
69
70
  let markerIndex = -1;
70
71
  let hasCachePoint = false;
71
72
  let hasDisabledMarker = false;
@@ -74,7 +75,7 @@ function insertBedrockToolCachePoint(toolConfig, fallbackToEnd) {
74
75
  const tool = tools[i];
75
76
  if (isBedrockCachePoint(tool)) {
76
77
  hasCachePoint = true;
77
- cleanedTools.push(tool);
78
+ cleanedTools.push(cachePoint);
78
79
  continue;
79
80
  }
80
81
  if (tool[BEDROCK_TOOL_CACHE_MARKER] === true) markerIndex = cleanedTools.length;
@@ -94,7 +95,7 @@ function insertBedrockToolCachePoint(toolConfig, fallbackToEnd) {
94
95
  ...toolConfig,
95
96
  tools: [
96
97
  ...cleanedTools.slice(0, insertionIndex + 1),
97
- CACHE_POINT,
98
+ cachePoint,
98
99
  ...cleanedTools.slice(insertionIndex + 1)
99
100
  ]
100
101
  };
@@ -1 +1 @@
1
- {"version":3,"file":"toolCache.cjs","names":["_convertToOpenAITool"],"sources":["../../../../src/llm/bedrock/toolCache.ts"],"sourcesContent":["import type { BindToolsInput } from '@langchain/core/language_models/chat_models';\nimport type { Tool, ToolConfiguration } from '@aws-sdk/client-bedrock-runtime';\nimport type { OpenAIClient } from '@langchain/openai';\nimport type { DocumentType } from '@smithy/types';\nimport type { GraphTools } from '@/types';\nimport { _convertToOpenAITool } from '@/llm/openai';\n\nconst CACHE_POINT: Tool.CachePointMember = {\n cachePoint: { type: 'default' },\n};\n\nconst BEDROCK_TOOL_CACHE_MARKER = '__lc_bedrock_cache_point_after';\nconst BEDROCK_TOOL_CACHE_DISABLED_MARKER = '__lc_bedrock_skip_tool_cache';\n\ntype BedrockToolWithCacheMarker = Tool & {\n [BEDROCK_TOOL_CACHE_MARKER]?: true;\n [BEDROCK_TOOL_CACHE_DISABLED_MARKER]?: true;\n};\n\ntype OpenAIFunctionTool = Extract<\n OpenAIClient.ChatCompletionTool,\n { type: 'function' }\n>;\n\ntype ToolNameCandidate = {\n name?: unknown;\n function?: {\n name?: unknown;\n };\n toolSpec?: {\n name?: unknown;\n };\n};\n\nfunction isBedrockToolSpec(tool: unknown): tool is Tool.ToolSpecMember {\n return (\n typeof tool === 'object' &&\n tool != null &&\n 'toolSpec' in tool &&\n typeof (tool as ToolNameCandidate).toolSpec?.name === 'string'\n );\n}\n\nfunction isBedrockCachePoint(tool: Tool): tool is Tool.CachePointMember {\n return 'cachePoint' in tool && tool.cachePoint != null;\n}\n\nfunction getToolName(tool: unknown): string | undefined {\n const candidate = tool as ToolNameCandidate;\n if (typeof candidate.toolSpec?.name === 'string') {\n return candidate.toolSpec.name;\n }\n if (typeof candidate.name === 'string') {\n return candidate.name;\n }\n if (typeof candidate.function?.name === 'string') {\n return candidate.function.name;\n }\n return undefined;\n}\n\nfunction openAIToBedrockTool(tool: OpenAIFunctionTool): Tool.ToolSpecMember {\n const toolSpec: NonNullable<Tool.ToolSpecMember['toolSpec']> = {\n name: tool.function.name,\n inputSchema: { json: tool.function.parameters as DocumentType },\n };\n\n if (tool.function.description != null && tool.function.description !== '') {\n toolSpec.description = tool.function.description;\n }\n\n return {\n toolSpec,\n };\n}\n\nfunction toBedrockTool(tool: unknown): BedrockToolWithCacheMarker {\n if (isBedrockToolSpec(tool)) {\n return { ...tool };\n }\n\n return openAIToBedrockTool(\n _convertToOpenAITool(tool as BindToolsInput) as OpenAIFunctionTool\n ) as BedrockToolWithCacheMarker;\n}\n\nfunction markCachePointAfter(\n tool: BedrockToolWithCacheMarker\n): BedrockToolWithCacheMarker {\n return {\n ...tool,\n [BEDROCK_TOOL_CACHE_MARKER]: true,\n };\n}\n\nfunction markToolCacheDisabled(\n tool: BedrockToolWithCacheMarker\n): BedrockToolWithCacheMarker {\n return {\n ...tool,\n [BEDROCK_TOOL_CACHE_DISABLED_MARKER]: true,\n };\n}\n\nfunction stripCachePointMarker(tool: BedrockToolWithCacheMarker): Tool {\n const {\n [BEDROCK_TOOL_CACHE_MARKER]: _marker,\n [BEDROCK_TOOL_CACHE_DISABLED_MARKER]: _disabled,\n ...rest\n } = tool;\n return rest as Tool;\n}\n\nexport function partitionAndMarkBedrockToolCache(\n tools: GraphTools | undefined,\n isDeferred: (toolName: string) => boolean\n): GraphTools | undefined {\n if (tools == null || tools.length === 0) {\n return tools;\n }\n\n const staticTools: BedrockToolWithCacheMarker[] = [];\n const deferredTools: BedrockToolWithCacheMarker[] = [];\n\n for (const tool of tools as readonly unknown[]) {\n const converted = toBedrockTool(tool);\n const name = getToolName(converted) ?? getToolName(tool);\n\n if (name != null && isDeferred(name)) {\n deferredTools.push(converted);\n continue;\n }\n\n staticTools.push(converted);\n }\n\n if (staticTools.length === 0) {\n deferredTools[0] = markToolCacheDisabled(deferredTools[0]);\n return [...deferredTools] as GraphTools;\n }\n\n staticTools[staticTools.length - 1] = markCachePointAfter(\n staticTools[staticTools.length - 1]\n );\n\n return [...staticTools, ...deferredTools] as GraphTools;\n}\n\nexport function insertBedrockToolCachePoint(\n toolConfig: ToolConfiguration | undefined,\n fallbackToEnd: boolean\n): ToolConfiguration | undefined {\n const tools = toolConfig?.tools as BedrockToolWithCacheMarker[] | undefined;\n if (tools == null || tools.length === 0) {\n return toolConfig;\n }\n\n let markerIndex = -1;\n let hasCachePoint = false;\n let hasDisabledMarker = false;\n const cleanedTools: Tool[] = [];\n\n for (let i = 0; i < tools.length; i++) {\n const tool = tools[i];\n if (isBedrockCachePoint(tool)) {\n hasCachePoint = true;\n cleanedTools.push(tool);\n continue;\n }\n if (tool[BEDROCK_TOOL_CACHE_MARKER] === true) {\n markerIndex = cleanedTools.length;\n }\n if (tool[BEDROCK_TOOL_CACHE_DISABLED_MARKER] === true) {\n hasDisabledMarker = true;\n }\n cleanedTools.push(stripCachePointMarker(tool));\n }\n\n if (hasCachePoint || hasDisabledMarker) {\n return { ...toolConfig, tools: cleanedTools };\n }\n\n const insertionIndex = markerIndex >= 0 ? markerIndex : tools.length - 1;\n if (markerIndex < 0 && !fallbackToEnd) {\n return { ...toolConfig, tools: cleanedTools };\n }\n\n return {\n ...toolConfig,\n tools: [\n ...cleanedTools.slice(0, insertionIndex + 1),\n CACHE_POINT,\n ...cleanedTools.slice(insertionIndex + 1),\n ],\n };\n}\n"],"mappings":";;AAOA,MAAM,cAAqC,EACzC,YAAY,EAAE,MAAM,UAAU,EAChC;AAEA,MAAM,4BAA4B;AAClC,MAAM,qCAAqC;AAsB3C,SAAS,kBAAkB,MAA4C;CACrE,OACE,OAAO,SAAS,YAChB,QAAQ,QACR,cAAc,QACd,OAAQ,KAA2B,UAAU,SAAS;AAE1D;AAEA,SAAS,oBAAoB,MAA2C;CACtE,OAAO,gBAAgB,QAAQ,KAAK,cAAc;AACpD;AAEA,SAAS,YAAY,MAAmC;CACtD,MAAM,YAAY;CAClB,IAAI,OAAO,UAAU,UAAU,SAAS,UACtC,OAAO,UAAU,SAAS;CAE5B,IAAI,OAAO,UAAU,SAAS,UAC5B,OAAO,UAAU;CAEnB,IAAI,OAAO,UAAU,UAAU,SAAS,UACtC,OAAO,UAAU,SAAS;AAG9B;AAEA,SAAS,oBAAoB,MAA+C;CAC1E,MAAM,WAAyD;EAC7D,MAAM,KAAK,SAAS;EACpB,aAAa,EAAE,MAAM,KAAK,SAAS,WAA2B;CAChE;CAEA,IAAI,KAAK,SAAS,eAAe,QAAQ,KAAK,SAAS,gBAAgB,IACrE,SAAS,cAAc,KAAK,SAAS;CAGvC,OAAO,EACL,SACF;AACF;AAEA,SAAS,cAAc,MAA2C;CAChE,IAAI,kBAAkB,IAAI,GACxB,OAAO,EAAE,GAAG,KAAK;CAGnB,OAAO,oBACLA,cAAAA,qBAAqB,IAAsB,CAC7C;AACF;AAEA,SAAS,oBACP,MAC4B;CAC5B,OAAO;EACL,GAAG;GACF,4BAA4B;CAC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I,CAAC;CAC/C;CAEA,IAAI,iBAAiB,mBACnB,OAAO;EAAE,GAAG;EAAY,OAAO;CAAa;CAG9C,MAAM,iBAAiB,eAAe,IAAI,cAAc,MAAM,SAAS;CACvE,IAAI,cAAc,KAAK,CAAC,eACtB,OAAO;EAAE,GAAG;EAAY,OAAO;CAAa;CAG9C,OAAO;EACL,GAAG;EACH,OAAO;GACL,GAAG,aAAa,MAAM,GAAG,iBAAiB,CAAC;GAC3C;GACA,GAAG,aAAa,MAAM,iBAAiB,CAAC;EAC1C;CACF;AACF"}
1
+ {"version":3,"file":"toolCache.cjs","names":["_convertToOpenAITool","buildBedrockCachePoint"],"sources":["../../../../src/llm/bedrock/toolCache.ts"],"sourcesContent":["import type { BindToolsInput } from '@langchain/core/language_models/chat_models';\nimport type { Tool, ToolConfiguration } from '@aws-sdk/client-bedrock-runtime';\nimport type { OpenAIClient } from '@langchain/openai';\nimport type { DocumentType } from '@smithy/types';\nimport type { GraphTools } from '@/types';\nimport { buildBedrockCachePoint, type PromptCacheTtl } from '@/messages/cache';\nimport { _convertToOpenAITool } from '@/llm/openai';\n\nconst BEDROCK_TOOL_CACHE_MARKER = '__lc_bedrock_cache_point_after';\nconst BEDROCK_TOOL_CACHE_DISABLED_MARKER = '__lc_bedrock_skip_tool_cache';\n\ntype BedrockToolWithCacheMarker = Tool & {\n [BEDROCK_TOOL_CACHE_MARKER]?: true;\n [BEDROCK_TOOL_CACHE_DISABLED_MARKER]?: true;\n};\n\ntype OpenAIFunctionTool = Extract<\n OpenAIClient.ChatCompletionTool,\n { type: 'function' }\n>;\n\ntype ToolNameCandidate = {\n name?: unknown;\n function?: {\n name?: unknown;\n };\n toolSpec?: {\n name?: unknown;\n };\n};\n\nfunction isBedrockToolSpec(tool: unknown): tool is Tool.ToolSpecMember {\n return (\n typeof tool === 'object' &&\n tool != null &&\n 'toolSpec' in tool &&\n typeof (tool as ToolNameCandidate).toolSpec?.name === 'string'\n );\n}\n\nfunction isBedrockCachePoint(tool: Tool): tool is Tool.CachePointMember {\n return 'cachePoint' in tool && tool.cachePoint != null;\n}\n\nfunction getToolName(tool: unknown): string | undefined {\n const candidate = tool as ToolNameCandidate;\n if (typeof candidate.toolSpec?.name === 'string') {\n return candidate.toolSpec.name;\n }\n if (typeof candidate.name === 'string') {\n return candidate.name;\n }\n if (typeof candidate.function?.name === 'string') {\n return candidate.function.name;\n }\n return undefined;\n}\n\nfunction openAIToBedrockTool(tool: OpenAIFunctionTool): Tool.ToolSpecMember {\n const toolSpec: NonNullable<Tool.ToolSpecMember['toolSpec']> = {\n name: tool.function.name,\n inputSchema: { json: tool.function.parameters as DocumentType },\n };\n\n if (tool.function.description != null && tool.function.description !== '') {\n toolSpec.description = tool.function.description;\n }\n\n return {\n toolSpec,\n };\n}\n\nfunction toBedrockTool(tool: unknown): BedrockToolWithCacheMarker {\n if (isBedrockToolSpec(tool)) {\n return { ...tool };\n }\n\n return openAIToBedrockTool(\n _convertToOpenAITool(tool as BindToolsInput) as OpenAIFunctionTool\n ) as BedrockToolWithCacheMarker;\n}\n\nfunction markCachePointAfter(\n tool: BedrockToolWithCacheMarker\n): BedrockToolWithCacheMarker {\n return {\n ...tool,\n [BEDROCK_TOOL_CACHE_MARKER]: true,\n };\n}\n\nfunction markToolCacheDisabled(\n tool: BedrockToolWithCacheMarker\n): BedrockToolWithCacheMarker {\n return {\n ...tool,\n [BEDROCK_TOOL_CACHE_DISABLED_MARKER]: true,\n };\n}\n\nfunction stripCachePointMarker(tool: BedrockToolWithCacheMarker): Tool {\n const {\n [BEDROCK_TOOL_CACHE_MARKER]: _marker,\n [BEDROCK_TOOL_CACHE_DISABLED_MARKER]: _disabled,\n ...rest\n } = tool;\n return rest as Tool;\n}\n\nexport function partitionAndMarkBedrockToolCache(\n tools: GraphTools | undefined,\n isDeferred: (toolName: string) => boolean\n): GraphTools | undefined {\n if (tools == null || tools.length === 0) {\n return tools;\n }\n\n const staticTools: BedrockToolWithCacheMarker[] = [];\n const deferredTools: BedrockToolWithCacheMarker[] = [];\n\n for (const tool of tools as readonly unknown[]) {\n const converted = toBedrockTool(tool);\n const name = getToolName(converted) ?? getToolName(tool);\n\n if (name != null && isDeferred(name)) {\n deferredTools.push(converted);\n continue;\n }\n\n staticTools.push(converted);\n }\n\n if (staticTools.length === 0) {\n deferredTools[0] = markToolCacheDisabled(deferredTools[0]);\n return [...deferredTools] as GraphTools;\n }\n\n staticTools[staticTools.length - 1] = markCachePointAfter(\n staticTools[staticTools.length - 1]\n );\n\n return [...staticTools, ...deferredTools] as GraphTools;\n}\n\nexport function insertBedrockToolCachePoint(\n toolConfig: ToolConfiguration | undefined,\n fallbackToEnd: boolean,\n ttl?: PromptCacheTtl\n): ToolConfiguration | undefined {\n const tools = toolConfig?.tools as BedrockToolWithCacheMarker[] | undefined;\n if (tools == null || tools.length === 0) {\n return toolConfig;\n }\n\n const cachePoint: Tool.CachePointMember = {\n cachePoint: buildBedrockCachePoint(ttl),\n };\n\n let markerIndex = -1;\n let hasCachePoint = false;\n let hasDisabledMarker = false;\n const cleanedTools: Tool[] = [];\n\n for (let i = 0; i < tools.length; i++) {\n const tool = tools[i];\n if (isBedrockCachePoint(tool)) {\n // Normalize an existing cache point to the resolved TTL so a stale\n // 5-minute tool breakpoint never precedes the new 1-hour system/message\n // breakpoints (Bedrock requires longer-TTL entries to appear first).\n hasCachePoint = true;\n cleanedTools.push(cachePoint);\n continue;\n }\n if (tool[BEDROCK_TOOL_CACHE_MARKER] === true) {\n markerIndex = cleanedTools.length;\n }\n if (tool[BEDROCK_TOOL_CACHE_DISABLED_MARKER] === true) {\n hasDisabledMarker = true;\n }\n cleanedTools.push(stripCachePointMarker(tool));\n }\n\n if (hasCachePoint || hasDisabledMarker) {\n return { ...toolConfig, tools: cleanedTools };\n }\n\n const insertionIndex = markerIndex >= 0 ? markerIndex : tools.length - 1;\n if (markerIndex < 0 && !fallbackToEnd) {\n return { ...toolConfig, tools: cleanedTools };\n }\n\n return {\n ...toolConfig,\n tools: [\n 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