@langchain/anthropic 1.5.11 → 1.5.13

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 (70) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/chat_models.cjs +24 -13
  3. package/dist/chat_models.cjs.map +1 -1
  4. package/dist/chat_models.d.cts +12 -8
  5. package/dist/chat_models.d.cts.map +1 -1
  6. package/dist/chat_models.d.ts +12 -8
  7. package/dist/chat_models.d.ts.map +1 -1
  8. package/dist/chat_models.js +25 -14
  9. package/dist/chat_models.js.map +1 -1
  10. package/dist/profiles.cjs +87 -36
  11. package/dist/profiles.cjs.map +1 -1
  12. package/dist/profiles.js +87 -36
  13. package/dist/profiles.js.map +1 -1
  14. package/dist/tools/bash.d.cts +2 -3
  15. package/dist/tools/bash.d.cts.map +1 -1
  16. package/dist/tools/bash.d.ts +2 -3
  17. package/dist/tools/bash.d.ts.map +1 -1
  18. package/dist/tools/codeExecution.d.cts +2 -3
  19. package/dist/tools/codeExecution.d.cts.map +1 -1
  20. package/dist/tools/codeExecution.d.ts +2 -3
  21. package/dist/tools/codeExecution.d.ts.map +1 -1
  22. package/dist/tools/computer.d.cts +5 -6
  23. package/dist/tools/computer.d.cts.map +1 -1
  24. package/dist/tools/computer.d.ts +5 -6
  25. package/dist/tools/computer.d.ts.map +1 -1
  26. package/dist/tools/index.d.cts +2 -2
  27. package/dist/tools/index.d.cts.map +1 -1
  28. package/dist/tools/index.d.ts +2 -2
  29. package/dist/tools/index.d.ts.map +1 -1
  30. package/dist/tools/mcpToolset.d.cts +4 -5
  31. package/dist/tools/mcpToolset.d.cts.map +1 -1
  32. package/dist/tools/mcpToolset.d.ts +4 -5
  33. package/dist/tools/mcpToolset.d.ts.map +1 -1
  34. package/dist/tools/memory.d.cts +1 -2
  35. package/dist/tools/memory.d.cts.map +1 -1
  36. package/dist/tools/memory.d.ts +1 -2
  37. package/dist/tools/memory.d.ts.map +1 -1
  38. package/dist/tools/textEditor.d.cts +2 -3
  39. package/dist/tools/textEditor.d.cts.map +1 -1
  40. package/dist/tools/textEditor.d.ts +2 -3
  41. package/dist/tools/textEditor.d.ts.map +1 -1
  42. package/dist/tools/toolSearch.d.cts +3 -4
  43. package/dist/tools/toolSearch.d.cts.map +1 -1
  44. package/dist/tools/toolSearch.d.ts +3 -4
  45. package/dist/tools/toolSearch.d.ts.map +1 -1
  46. package/dist/tools/types.d.cts +68 -69
  47. package/dist/tools/types.d.cts.map +1 -1
  48. package/dist/tools/types.d.ts +68 -69
  49. package/dist/tools/types.d.ts.map +1 -1
  50. package/dist/tools/webFetch.d.cts +2 -3
  51. package/dist/tools/webFetch.d.cts.map +1 -1
  52. package/dist/tools/webFetch.d.ts +2 -3
  53. package/dist/tools/webFetch.d.ts.map +1 -1
  54. package/dist/tools/webSearch.d.cts +2 -3
  55. package/dist/tools/webSearch.d.cts.map +1 -1
  56. package/dist/tools/webSearch.d.ts +2 -3
  57. package/dist/tools/webSearch.d.ts.map +1 -1
  58. package/dist/types.d.cts +27 -28
  59. package/dist/types.d.cts.map +1 -1
  60. package/dist/types.d.ts +27 -28
  61. package/dist/types.d.ts.map +1 -1
  62. package/dist/utils/message_inputs.cjs +47 -10
  63. package/dist/utils/message_inputs.cjs.map +1 -1
  64. package/dist/utils/message_inputs.js +47 -11
  65. package/dist/utils/message_inputs.js.map +1 -1
  66. package/dist/utils/prompts.d.cts +1 -2
  67. package/dist/utils/prompts.d.cts.map +1 -1
  68. package/dist/utils/prompts.d.ts +1 -2
  69. package/dist/utils/prompts.d.ts.map +1 -1
  70. package/package.json +7 -7
@@ -191,23 +191,41 @@ function _formatContent(message, toolCalls) {
191
191
  if (typeof content === "string") return content;
192
192
  else return Array.from(_formatContentBlocks(content, toolCalls));
193
193
  }
194
+ const TOOL_CHANGE_BLOCK_TYPES = ["tool_addition", "tool_removal"];
195
+ /**
196
+ * Returns the provider payload a block carries: the `value` of a
197
+ * `non_standard` wrapper, or the block itself.
198
+ */
199
+ function _unwrapNonStandard(block) {
200
+ if (block.type === "non_standard" && "value" in block && typeof block.value === "object" && block.value !== null) return block.value;
201
+ return block;
202
+ }
194
203
  /**
195
204
  * Normalizes the content of a system message for the wire.
196
205
  *
197
206
  * Shared by the hoisted top-level `system` field and by in-place
198
207
  * `role: "system"` entries, so both strip the same framework-internal fields.
208
+ *
209
+ * Blocks wrapped in core's `non_standard` escape hatch are unwrapped first, so
210
+ * a provider-native block behaves the same whichever way it was written.
199
211
  */
200
212
  function _formatSystemContent(content) {
201
213
  if (typeof content === "string") return content;
202
214
  if (!Array.isArray(content)) return [];
203
- return content.map((block) => {
204
- if (block.type !== "text" || typeof block.text !== "string") return block;
205
- return {
215
+ return content.flatMap((rawBlock) => {
216
+ if (typeof rawBlock !== "object" || rawBlock === null) return [{
217
+ type: "text",
218
+ text: String(rawBlock)
219
+ }];
220
+ const block = _unwrapNonStandard(rawBlock);
221
+ if (TOOL_CHANGE_BLOCK_TYPES.includes(block.type)) return [block];
222
+ if (block.type === "text" && typeof block.text === "string") return [{
206
223
  type: "text",
207
224
  text: block.text,
208
225
  ..."cache_control" in block && block.cache_control ? { cache_control: block.cache_control } : {},
209
226
  ..."citations" in block && block.citations ? { citations: block.citations } : {}
210
- };
227
+ }];
228
+ return [];
211
229
  });
212
230
  }
213
231
  /**
@@ -229,6 +247,7 @@ function _convertMessagesToAnthropicPayload(messages) {
229
247
  text: content
230
248
  }] : content;
231
249
  });
250
+ if (Array.isArray(system) && system.length === 0) system = void 0;
232
251
  return {
233
252
  messages: mergeMessages(mergedMessages.slice(leadingSystemCount).map((message) => {
234
253
  let role;
@@ -267,18 +286,35 @@ function _convertMessagesToAnthropicPayload(messages) {
267
286
  content: [...formattedContentArr, ...missingToolCalls.map(_convertLangChainToolCallToAnthropic)]
268
287
  };
269
288
  }
270
- } else if (role === "system") return {
271
- role,
272
- content: _formatSystemContent(message.content)
273
- };
274
- else return {
289
+ } else if (role === "system") {
290
+ const content = _formatSystemContent(message.content);
291
+ return Array.isArray(content) && content.length === 0 ? void 0 : {
292
+ role,
293
+ content
294
+ };
295
+ } else return {
275
296
  role,
276
297
  content: _formatContent(message, AIMessage.isInstance(message) ? message.tool_calls : void 0)
277
298
  };
278
- })),
299
+ }).filter((message) => message !== void 0)),
279
300
  system
280
301
  };
281
302
  }
303
+ /**
304
+ * Describes the tool changes in the system content of a converted payload:
305
+ * `"inline"` if any `tool_addition` block defines a tool by value,
306
+ * `"reference"` if tool changes only name tools declared elsewhere, or
307
+ * `undefined` if there are none. The provider requires a different beta for
308
+ * each.
309
+ *
310
+ * Reads the converted payload rather than the input messages, so a block that
311
+ * was dropped during conversion never enables a beta.
312
+ */
313
+ function _getToolChangeKind(payload) {
314
+ const toolChanges = [payload.system, ...payload.messages.filter((message) => message.role === "system").map((message) => message.content)].flatMap((content) => Array.isArray(content) ? content.filter((block) => TOOL_CHANGE_BLOCK_TYPES.includes(block.type)) : []);
315
+ if (toolChanges.some((block) => block.tool?.type === "tool_definition")) return "inline";
316
+ return toolChanges.length > 0 ? "reference" : void 0;
317
+ }
282
318
  function mergeMessages(messages) {
283
319
  if (!messages || messages.length <= 1) return messages;
284
320
  const result = [];
@@ -310,6 +346,6 @@ function mergeMessages(messages) {
310
346
  return result;
311
347
  }
312
348
  //#endregion
313
- export { _convertLangChainToolCallToAnthropic, _convertMessagesToAnthropicPayload };
349
+ export { _convertLangChainToolCallToAnthropic, _convertMessagesToAnthropicPayload, _getToolChangeKind };
314
350
 
315
351
  //# sourceMappingURL=message_inputs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"message_inputs.js","names":[],"sources":["../../src/utils/message_inputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport {\n type BaseMessage,\n HumanMessage,\n ToolMessage,\n MessageContentComplex,\n isDataContentBlock,\n convertToProviderContentBlock,\n parseBase64DataUrl,\n ContentBlock,\n AIMessage,\n} from \"@langchain/core/messages\";\nimport { ToolCall } from \"@langchain/core/messages/tool\";\nimport {\n AnthropicMessageCreateParams,\n AnthropicTextBlockParam,\n AnthropicThinkingBlockParam,\n AnthropicRedactedThinkingBlockParam,\n AnthropicSearchResultBlockParam,\n AnthropicToolResponse,\n AnthropicContainerUploadBlockParam,\n AnthropicCompactionBlockParam,\n} from \"../types.js\";\nimport {\n _isAnthropicImageBlockParam,\n _isAnthropicRedactedThinkingBlock,\n _isAnthropicSearchResultBlock,\n _isAnthropicThinkingBlock,\n _isAnthropicCompactionBlock,\n standardContentBlockConverter,\n} from \"./content.js\";\nimport { _formatStandardContent } from \"./standard.js\";\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\nfunction _ensureMessageContents(messages: BaseMessage[]): BaseMessage[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const updatedMsgs = [];\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: (message as ToolMessage).tool_call_id,\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: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: \"tool_result\",\n // rare case: message.content could be undefined\n ...(message.content != null\n ? { content: _formatContent(message) }\n : {}),\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(message);\n }\n }\n return updatedMsgs;\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 return {\n type: \"tool_use\",\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\nfunction* _formatContentBlocks(\n content: ContentBlock[],\n toolCalls?: ToolCall[]\n): Generator<Anthropic.Beta.BetaContentBlockParam> {\n const toolTypes = [\n \"bash_code_execution_tool_result\",\n \"input_json_delta\",\n \"server_tool_use\",\n \"text_editor_code_execution_tool_result\",\n \"tool_result\",\n \"tool_search_tool_result\",\n \"tool_search_tool_bm25_tool_result\",\n \"tool_search_tool_regex_tool_result\",\n \"tool_use\",\n \"web_search_result\",\n \"web_search_tool_result\",\n ];\n const textTypes = [\"text\", \"text_delta\"];\n for (const contentPart of content) {\n if (isDataContentBlock(contentPart)) {\n yield convertToProviderContentBlock(\n contentPart,\n standardContentBlockConverter\n );\n }\n\n const cacheControl =\n \"cache_control\" in contentPart\n ? (contentPart.cache_control as Anthropic.Beta.BetaCacheControlEphemeral)\n : undefined;\n\n if (contentPart.type === \"image_url\") {\n let source;\n if (typeof contentPart.image_url === \"string\") {\n source = _formatImage(contentPart.image_url);\n } else if (\n typeof contentPart.image_url === \"object\" &&\n contentPart.image_url !== null &&\n \"url\" in contentPart.image_url &&\n typeof contentPart.image_url.url === \"string\"\n ) {\n source = _formatImage(contentPart.image_url.url);\n }\n if (source) {\n yield {\n type: \"image\" as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as Anthropic.Messages.ImageBlockParam;\n }\n } else if (_isAnthropicImageBlockParam(contentPart)) {\n yield contentPart;\n } else if (contentPart.type === \"image\") {\n // Handle new ContentBlock.Multimodal.Image format\n let source;\n\n if (\"url\" in contentPart && typeof contentPart.url === \"string\") {\n // URL-based image\n source = _formatImage(contentPart.url);\n } else if (\n \"data\" in contentPart &&\n (typeof contentPart.data === \"string\" ||\n // oxlint-disable-next-line no-instanceof/no-instanceof\n contentPart.data instanceof Uint8Array)\n ) {\n // Base64-based image\n const mimeType =\n \"mimeType\" in contentPart && typeof contentPart.mimeType === \"string\"\n ? contentPart.mimeType\n : \"image/jpeg\";\n const data =\n typeof contentPart.data === \"string\"\n ? contentPart.data\n : Buffer.from(contentPart.data).toString(\"base64\");\n source = {\n type: \"base64\" as const,\n media_type: mimeType as\n | \"image/jpeg\"\n | \"image/png\"\n | \"image/gif\"\n | \"image/webp\",\n data,\n };\n } else if (\n \"fileId\" in contentPart &&\n typeof contentPart.fileId === \"string\"\n ) {\n // File ID-based image\n // Note: Anthropic supports file IDs for images that have been uploaded\n // to their servers via the Files API\n source = {\n type: \"file\" as const,\n file_id: contentPart.fileId,\n };\n }\n\n if (source) {\n yield {\n type: \"image\" as const,\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as Anthropic.Messages.ImageBlockParam;\n }\n } else if (contentPart.type === \"file\") {\n // Handle new ContentBlock.Multimodal.File format\n let source:\n | { type: \"url\"; url: string }\n | { type: \"base64\"; media_type: string; data: string }\n | { type: \"file\"; file_id: string }\n | undefined;\n\n if (\"url\" in contentPart && typeof contentPart.url === \"string\") {\n // File with URL\n source = {\n type: \"url\" as const,\n url: contentPart.url,\n };\n } else if (\n \"data\" in contentPart &&\n (typeof contentPart.data === \"string\" ||\n // oxlint-disable-next-line no-instanceof/no-instanceof\n contentPart.data instanceof Uint8Array)\n ) {\n // File with base64 data (string or Uint8Array)\n const media_type =\n \"mimeType\" in contentPart && typeof contentPart.mimeType === \"string\"\n ? contentPart.mimeType\n : \"application/pdf\";\n const data =\n typeof contentPart.data === \"string\"\n ? contentPart.data\n : Buffer.from(contentPart.data).toString(\"base64\");\n\n source = {\n type: \"base64\" as const,\n media_type,\n data,\n };\n } else if (\n \"fileId\" in contentPart &&\n typeof contentPart.fileId === \"string\"\n ) {\n // File ID from Anthropic Files API\n // https://platform.claude.com/docs/en/build-with-claude/pdf-support#option-3-files-api\n source = {\n type: \"file\" as const,\n file_id: contentPart.fileId,\n };\n }\n\n if (source) {\n yield {\n type: \"document\" as const,\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as Anthropic.Messages.DocumentBlockParam;\n }\n } else if (contentPart.type === \"document\") {\n // PDF\n yield {\n ...contentPart,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as Anthropic.Messages.DocumentBlockParam;\n } else if (_isAnthropicThinkingBlock(contentPart)) {\n const block: AnthropicThinkingBlockParam = {\n type: \"thinking\" as const, // Explicitly setting the type as \"thinking\"\n thinking: contentPart.thinking,\n signature: contentPart.signature,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n yield block;\n } else if (_isAnthropicRedactedThinkingBlock(contentPart)) {\n const block: AnthropicRedactedThinkingBlockParam = {\n type: \"redacted_thinking\" as const, // Explicitly setting the type as \"redacted_thinking\"\n data: contentPart.data,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n yield block;\n } else if (_isAnthropicCompactionBlock(contentPart)) {\n const block: AnthropicCompactionBlockParam = {\n type: \"compaction\" as const,\n content: contentPart.content,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n yield block;\n } else if (_isAnthropicSearchResultBlock(contentPart)) {\n const block: AnthropicSearchResultBlockParam = {\n type: \"search_result\" as const, // Explicitly setting the type as \"search_result\"\n title: contentPart.title,\n source: contentPart.source,\n ...(\"cache_control\" in contentPart && contentPart.cache_control\n ? { cache_control: contentPart.cache_control }\n : {}),\n ...(\"citations\" in contentPart && contentPart.citations\n ? { citations: contentPart.citations }\n : {}),\n content: contentPart.content,\n };\n yield block as Anthropic.Beta.BetaSearchResultBlockParam;\n } else if (\n textTypes.find((t) => t === contentPart.type) &&\n \"text\" in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n yield {\n type: \"text\" as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n ...(\"citations\" in contentPart && contentPart.citations\n ? { citations: contentPart.citations }\n : {}),\n } as Anthropic.Messages.TextBlockParam;\n } else if (toolTypes.find((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n\n if (contentPartCopy.type === \"input_json_delta\") {\n // `input_json_delta` type only represents yielding partial tool inputs\n // and is not a valid type for Anthropic messages.\n // These blocks appear in streaming responses and should be skipped\n // as their input data is already captured in tool_calls.\n continue;\n }\n\n if (\n contentPartCopy.type === \"tool_use\" &&\n typeof contentPartCopy.input === \"string\"\n ) {\n // First, try to get the input from the corresponding tool_call.\n // This is the most reliable source since tool_calls are properly\n // consolidated from tool_call_chunks during streaming.\n const matchingToolCall = toolCalls?.find(\n (tc) => tc.id === contentPartCopy.id\n );\n if (matchingToolCall) {\n contentPartCopy.input = matchingToolCall.args;\n } else {\n // Fallback: `tool_use` content part may be followed by `input_json_delta`\n // content parts which are chunks of a stringified JSON input,\n // so we need to collect them and merge their inputs.\n const inputDeltas = content.filter(\n (nestedContentPart) =>\n nestedContentPart.index === contentPartCopy.index &&\n nestedContentPart.type === \"input_json_delta\" &&\n typeof nestedContentPart.input === \"string\"\n );\n // If no `input_json_delta` parts are found, this line will just\n // return `contentPartCopy.input`, so no additional check is needed\n contentPartCopy.input = inputDeltas.reduce(\n (accumulator, nestedContentPart) =>\n accumulator + nestedContentPart.input,\n contentPartCopy.input\n );\n }\n }\n\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 (\"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 // TODO: Fix when SDK types are fixed\n yield {\n ...contentPartCopy,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else if (contentPart.type === \"container_upload\") {\n yield {\n ...contentPart,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as AnthropicContainerUploadBlockParam;\n }\n\n // Note that we are intentionally dropping any blocks that we don't\n // recognize. This is to allow for cross-compatibility between different\n // providers that may have different block types. Ie if we take a message\n // output from OpenAI and send it to Anthropic, we want to drop any blocks\n // that Anthropic doesn't understand.\n }\n}\n\nfunction _formatContent(message: BaseMessage, toolCalls?: ToolCall[]) {\n const { content } = message;\n\n if (typeof content === \"string\") {\n return content;\n } else {\n return Array.from(_formatContentBlocks(content, toolCalls));\n }\n}\n\n/**\n * Normalizes the content of a system message for the wire.\n *\n * Shared by the hoisted top-level `system` field and by in-place\n * `role: \"system\"` entries, so both strip the same framework-internal fields.\n */\nfunction _formatSystemContent(\n content: BaseMessage[\"content\"]\n): string | Anthropic.Messages.ContentBlockParam[] {\n if (typeof content === \"string\") {\n return content;\n }\n if (!Array.isArray(content)) {\n // rare case: message.content could be undefined\n return [];\n }\n return content.map((block) => {\n if (block.type !== \"text\" || typeof block.text !== \"string\") {\n return block as Anthropic.Messages.ContentBlockParam;\n }\n return {\n type: \"text\" as const,\n text: block.text,\n ...(\"cache_control\" in block && block.cache_control\n ? {\n cache_control:\n block.cache_control as AnthropicTextBlockParam[\"cache_control\"],\n }\n : {}),\n ...(\"citations\" in block && block.citations\n ? {\n citations: block.citations as AnthropicTextBlockParam[\"citations\"],\n }\n : {}),\n };\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\n // The contiguous run of system messages starting at index 0 is hoisted into\n // the top-level `system` field. Every other system message keeps its\n // position and is sent as a `role: \"system\"` entry, which the provider\n // applies from that point in the conversation onwards. The run is identified\n // after tool-message folding, because that is the sequence the provider sees.\n let leadingSystemCount = 0;\n while (\n leadingSystemCount < mergedMessages.length &&\n mergedMessages[leadingSystemCount]._getType() === \"system\"\n ) {\n leadingSystemCount += 1;\n }\n\n let system: string | Anthropic.Messages.ContentBlockParam[] | undefined;\n if (leadingSystemCount === 1) {\n system = _formatSystemContent(mergedMessages[0].content);\n } else if (leadingSystemCount > 1) {\n system = mergedMessages.slice(0, leadingSystemCount).flatMap((message) => {\n const content = _formatSystemContent(message.content);\n return typeof content === \"string\"\n ? [{ type: \"text\" as const, text: content }]\n : content;\n });\n }\n\n const conversationMessages = mergedMessages.slice(leadingSystemCount);\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 role = \"system\" as const;\n } else {\n throw new Error(`Message type \"${message.type}\" is not supported.`);\n }\n if (\n AIMessage.isInstance(message) &&\n message.response_metadata?.output_version === \"v1\"\n ) {\n return {\n role,\n content: _formatStandardContent(message),\n };\n }\n if (AIMessage.isInstance(message) && !!message.tool_calls?.length) {\n if (typeof message.content === \"string\") {\n if (message.content === \"\") {\n return {\n role,\n content: message.tool_calls.map(\n _convertLangChainToolCallToAnthropic\n ),\n };\n } else {\n return {\n role,\n content: [\n { type: \"text\", text: message.content },\n ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const { content } = message;\n const formattedContent = _formatContent(message, message.tool_calls);\n const formattedContentArr = Array.isArray(formattedContent)\n ? formattedContent\n : [{ type: \"text\" as const, text: formattedContent }];\n const missingToolCalls = message.tool_calls.filter(\n (toolCall) =>\n !content.find(\n (contentPart) =>\n (contentPart.type === \"tool_use\" ||\n contentPart.type === \"input_json_delta\" ||\n contentPart.type === \"server_tool_use\") &&\n contentPart.id === toolCall.id\n )\n );\n return {\n role,\n content: [\n ...formattedContentArr,\n ...missingToolCalls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else if (role === \"system\") {\n return { role, content: _formatSystemContent(message.content) };\n } else {\n return {\n role,\n content: _formatContent(\n message,\n AIMessage.isInstance(message) ? message.tool_calls : undefined\n ),\n };\n }\n });\n return {\n messages: mergeMessages(\n formattedMessages as AnthropicMessageCreateParams[\"messages\"]\n ),\n system,\n } as AnthropicMessageCreateParams;\n}\n\nfunction mergeMessages(messages: AnthropicMessageCreateParams[\"messages\"]) {\n if (!messages || messages.length <= 1) {\n return messages;\n }\n\n const result: AnthropicMessageCreateParams[\"messages\"] = [];\n let currentMessage = messages[0];\n\n const normalizeContent = (\n content: string | Array<Anthropic.Messages.ContentBlockParam>\n ): Array<Anthropic.Messages.ContentBlockParam> => {\n if (typeof content === \"string\") {\n return [\n {\n type: \"text\",\n text: content,\n },\n ];\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":";;;;AAoCA,SAAS,aAAa,UAAkB;CACtC,MAAM,SAAS,mBAAmB,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,SAAS,uBAAuB,UAAwC;CAEtE,MAAM,cAAc,CAAC;CACrB,KAAK,MAAM,WAAW,UACpB,IAAI,QAAQ,SAAS,MAAM,QAAQ;EACjC,IAAI,OAAO,QAAQ,YAAY,UAAU;GACvC,MAAM,kBAAkB,YAAY,YAAY,SAAS;GACzD,IACE,iBAAiB,SAAS,MAAM,WAChC,MAAM,QAAQ,gBAAgB,OAAO,KACrC,UAAU,gBAAgB,QAAQ,MAClC,gBAAgB,QAAQ,EAAE,CAAC,SAAS,eAGpC,gBAAiB,QAAoC,KAAK;IACxD,MAAM;IACN,SAAS,QAAQ;IACjB,aAAc,QAA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../../src/utils/message_inputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport {\n type BaseMessage,\n HumanMessage,\n ToolMessage,\n MessageContentComplex,\n isDataContentBlock,\n convertToProviderContentBlock,\n parseBase64DataUrl,\n ContentBlock,\n AIMessage,\n} from \"@langchain/core/messages\";\nimport { ToolCall } from \"@langchain/core/messages/tool\";\nimport {\n AnthropicMessageCreateParams,\n AnthropicTextBlockParam,\n AnthropicThinkingBlockParam,\n AnthropicRedactedThinkingBlockParam,\n AnthropicSearchResultBlockParam,\n AnthropicToolResponse,\n AnthropicContainerUploadBlockParam,\n AnthropicCompactionBlockParam,\n} from \"../types.js\";\nimport {\n _isAnthropicImageBlockParam,\n _isAnthropicRedactedThinkingBlock,\n _isAnthropicSearchResultBlock,\n _isAnthropicThinkingBlock,\n _isAnthropicCompactionBlock,\n standardContentBlockConverter,\n} from \"./content.js\";\nimport { _formatStandardContent } from \"./standard.js\";\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\nfunction _ensureMessageContents(messages: BaseMessage[]): BaseMessage[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const updatedMsgs = [];\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: (message as ToolMessage).tool_call_id,\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: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: \"tool_result\",\n // rare case: message.content could be undefined\n ...(message.content != null\n ? { content: _formatContent(message) }\n : {}),\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(message);\n }\n }\n return updatedMsgs;\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 return {\n type: \"tool_use\",\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\nfunction* _formatContentBlocks(\n content: ContentBlock[],\n toolCalls?: ToolCall[]\n): Generator<Anthropic.Beta.BetaContentBlockParam> {\n const toolTypes = [\n \"bash_code_execution_tool_result\",\n \"input_json_delta\",\n \"server_tool_use\",\n \"text_editor_code_execution_tool_result\",\n \"tool_result\",\n \"tool_search_tool_result\",\n \"tool_search_tool_bm25_tool_result\",\n \"tool_search_tool_regex_tool_result\",\n \"tool_use\",\n \"web_search_result\",\n \"web_search_tool_result\",\n ];\n const textTypes = [\"text\", \"text_delta\"];\n for (const contentPart of content) {\n if (isDataContentBlock(contentPart)) {\n yield convertToProviderContentBlock(\n contentPart,\n standardContentBlockConverter\n );\n }\n\n const cacheControl =\n \"cache_control\" in contentPart\n ? (contentPart.cache_control as Anthropic.Beta.BetaCacheControlEphemeral)\n : undefined;\n\n if (contentPart.type === \"image_url\") {\n let source;\n if (typeof contentPart.image_url === \"string\") {\n source = _formatImage(contentPart.image_url);\n } else if (\n typeof contentPart.image_url === \"object\" &&\n contentPart.image_url !== null &&\n \"url\" in contentPart.image_url &&\n typeof contentPart.image_url.url === \"string\"\n ) {\n source = _formatImage(contentPart.image_url.url);\n }\n if (source) {\n yield {\n type: \"image\" as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as Anthropic.Messages.ImageBlockParam;\n }\n } else if (_isAnthropicImageBlockParam(contentPart)) {\n yield contentPart;\n } else if (contentPart.type === \"image\") {\n // Handle new ContentBlock.Multimodal.Image format\n let source;\n\n if (\"url\" in contentPart && typeof contentPart.url === \"string\") {\n // URL-based image\n source = _formatImage(contentPart.url);\n } else if (\n \"data\" in contentPart &&\n (typeof contentPart.data === \"string\" ||\n // oxlint-disable-next-line no-instanceof/no-instanceof\n contentPart.data instanceof Uint8Array)\n ) {\n // Base64-based image\n const mimeType =\n \"mimeType\" in contentPart && typeof contentPart.mimeType === \"string\"\n ? contentPart.mimeType\n : \"image/jpeg\";\n const data =\n typeof contentPart.data === \"string\"\n ? contentPart.data\n : Buffer.from(contentPart.data).toString(\"base64\");\n source = {\n type: \"base64\" as const,\n media_type: mimeType as\n | \"image/jpeg\"\n | \"image/png\"\n | \"image/gif\"\n | \"image/webp\",\n data,\n };\n } else if (\n \"fileId\" in contentPart &&\n typeof contentPart.fileId === \"string\"\n ) {\n // File ID-based image\n // Note: Anthropic supports file IDs for images that have been uploaded\n // to their servers via the Files API\n source = {\n type: \"file\" as const,\n file_id: contentPart.fileId,\n };\n }\n\n if (source) {\n yield {\n type: \"image\" as const,\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as Anthropic.Messages.ImageBlockParam;\n }\n } else if (contentPart.type === \"file\") {\n // Handle new ContentBlock.Multimodal.File format\n let source:\n | { type: \"url\"; url: string }\n | { type: \"base64\"; media_type: string; data: string }\n | { type: \"file\"; file_id: string }\n | undefined;\n\n if (\"url\" in contentPart && typeof contentPart.url === \"string\") {\n // File with URL\n source = {\n type: \"url\" as const,\n url: contentPart.url,\n };\n } else if (\n \"data\" in contentPart &&\n (typeof contentPart.data === \"string\" ||\n // oxlint-disable-next-line no-instanceof/no-instanceof\n contentPart.data instanceof Uint8Array)\n ) {\n // File with base64 data (string or Uint8Array)\n const media_type =\n \"mimeType\" in contentPart && typeof contentPart.mimeType === \"string\"\n ? contentPart.mimeType\n : \"application/pdf\";\n const data =\n typeof contentPart.data === \"string\"\n ? contentPart.data\n : Buffer.from(contentPart.data).toString(\"base64\");\n\n source = {\n type: \"base64\" as const,\n media_type,\n data,\n };\n } else if (\n \"fileId\" in contentPart &&\n typeof contentPart.fileId === \"string\"\n ) {\n // File ID from Anthropic Files API\n // https://platform.claude.com/docs/en/build-with-claude/pdf-support#option-3-files-api\n source = {\n type: \"file\" as const,\n file_id: contentPart.fileId,\n };\n }\n\n if (source) {\n yield {\n type: \"document\" as const,\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as Anthropic.Messages.DocumentBlockParam;\n }\n } else if (contentPart.type === \"document\") {\n // PDF\n yield {\n ...contentPart,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as Anthropic.Messages.DocumentBlockParam;\n } else if (_isAnthropicThinkingBlock(contentPart)) {\n const block: AnthropicThinkingBlockParam = {\n type: \"thinking\" as const, // Explicitly setting the type as \"thinking\"\n thinking: contentPart.thinking,\n signature: contentPart.signature,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n yield block;\n } else if (_isAnthropicRedactedThinkingBlock(contentPart)) {\n const block: AnthropicRedactedThinkingBlockParam = {\n type: \"redacted_thinking\" as const, // Explicitly setting the type as \"redacted_thinking\"\n data: contentPart.data,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n yield block;\n } else if (_isAnthropicCompactionBlock(contentPart)) {\n const block: AnthropicCompactionBlockParam = {\n type: \"compaction\" as const,\n content: contentPart.content,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n yield block;\n } else if (_isAnthropicSearchResultBlock(contentPart)) {\n const block: AnthropicSearchResultBlockParam = {\n type: \"search_result\" as const, // Explicitly setting the type as \"search_result\"\n title: contentPart.title,\n source: contentPart.source,\n ...(\"cache_control\" in contentPart && contentPart.cache_control\n ? { cache_control: contentPart.cache_control }\n : {}),\n ...(\"citations\" in contentPart && contentPart.citations\n ? { citations: contentPart.citations }\n : {}),\n content: contentPart.content,\n };\n yield block as Anthropic.Beta.BetaSearchResultBlockParam;\n } else if (\n textTypes.find((t) => t === contentPart.type) &&\n \"text\" in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n yield {\n type: \"text\" as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n ...(\"citations\" in contentPart && contentPart.citations\n ? { citations: contentPart.citations }\n : {}),\n } as Anthropic.Messages.TextBlockParam;\n } else if (toolTypes.find((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n\n if (contentPartCopy.type === \"input_json_delta\") {\n // `input_json_delta` type only represents yielding partial tool inputs\n // and is not a valid type for Anthropic messages.\n // These blocks appear in streaming responses and should be skipped\n // as their input data is already captured in tool_calls.\n continue;\n }\n\n if (\n contentPartCopy.type === \"tool_use\" &&\n typeof contentPartCopy.input === \"string\"\n ) {\n // First, try to get the input from the corresponding tool_call.\n // This is the most reliable source since tool_calls are properly\n // consolidated from tool_call_chunks during streaming.\n const matchingToolCall = toolCalls?.find(\n (tc) => tc.id === contentPartCopy.id\n );\n if (matchingToolCall) {\n contentPartCopy.input = matchingToolCall.args;\n } else {\n // Fallback: `tool_use` content part may be followed by `input_json_delta`\n // content parts which are chunks of a stringified JSON input,\n // so we need to collect them and merge their inputs.\n const inputDeltas = content.filter(\n (nestedContentPart) =>\n nestedContentPart.index === contentPartCopy.index &&\n nestedContentPart.type === \"input_json_delta\" &&\n typeof nestedContentPart.input === \"string\"\n );\n // If no `input_json_delta` parts are found, this line will just\n // return `contentPartCopy.input`, so no additional check is needed\n contentPartCopy.input = inputDeltas.reduce(\n (accumulator, nestedContentPart) =>\n accumulator + nestedContentPart.input,\n contentPartCopy.input\n );\n }\n }\n\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 (\"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 // TODO: Fix when SDK types are fixed\n yield {\n ...contentPartCopy,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else if (contentPart.type === \"container_upload\") {\n yield {\n ...contentPart,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n } as AnthropicContainerUploadBlockParam;\n }\n\n // Note that we are intentionally dropping any blocks that we don't\n // recognize. This is to allow for cross-compatibility between different\n // providers that may have different block types. Ie if we take a message\n // output from OpenAI and send it to Anthropic, we want to drop any blocks\n // that Anthropic doesn't understand.\n }\n}\n\nfunction _formatContent(message: BaseMessage, toolCalls?: ToolCall[]) {\n const { content } = message;\n\n if (typeof content === \"string\") {\n return content;\n } else {\n return Array.from(_formatContentBlocks(content, toolCalls));\n }\n}\n\ntype AnthropicToolChangeBlockParam =\n | Anthropic.Beta.BetaRequestToolAdditionBlock\n | Anthropic.Beta.BetaRequestToolRemovalBlock;\n\ntype AnthropicSystemContentBlockParam =\n | Anthropic.Messages.ContentBlockParam\n | AnthropicToolChangeBlockParam;\n\nconst TOOL_CHANGE_BLOCK_TYPES = [\"tool_addition\", \"tool_removal\"];\n\n/**\n * Returns the provider payload a block carries: the `value` of a\n * `non_standard` wrapper, or the block itself.\n */\nfunction _unwrapNonStandard(\n block: MessageContentComplex\n): MessageContentComplex {\n if (\n block.type === \"non_standard\" &&\n \"value\" in block &&\n typeof block.value === \"object\" &&\n block.value !== null\n ) {\n return block.value as MessageContentComplex;\n }\n return block;\n}\n\n/**\n * Normalizes the content of a system message for the wire.\n *\n * Shared by the hoisted top-level `system` field and by in-place\n * `role: \"system\"` entries, so both strip the same framework-internal fields.\n *\n * Blocks wrapped in core's `non_standard` escape hatch are unwrapped first, so\n * a provider-native block behaves the same whichever way it was written.\n */\nfunction _formatSystemContent(\n content: BaseMessage[\"content\"]\n): string | AnthropicSystemContentBlockParam[] {\n if (typeof content === \"string\") {\n return content;\n }\n if (!Array.isArray(content)) {\n // rare case: message.content could be undefined\n return [];\n }\n return content.flatMap((rawBlock): AnthropicSystemContentBlockParam[] => {\n if (typeof rawBlock !== \"object\" || rawBlock === null) {\n return [{ type: \"text\" as const, text: String(rawBlock) }];\n }\n const block = _unwrapNonStandard(rawBlock);\n if (TOOL_CHANGE_BLOCK_TYPES.includes(block.type as string)) {\n return [block as AnthropicToolChangeBlockParam];\n }\n if (block.type === \"text\" && typeof block.text === \"string\") {\n return [\n {\n type: \"text\" as const,\n text: block.text,\n ...(\"cache_control\" in block && block.cache_control\n ? {\n cache_control:\n block.cache_control as AnthropicTextBlockParam[\"cache_control\"],\n }\n : {}),\n ...(\"citations\" in block && block.citations\n ? {\n citations:\n block.citations as AnthropicTextBlockParam[\"citations\"],\n }\n : {}),\n },\n ];\n }\n // Anthropic accepts a closed set of system content blocks, and rejects\n // the whole request for anything else.\n // Consider warning about the dropped block once we have a proper logging solution\n return [];\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\n // The contiguous run of system messages starting at index 0 is hoisted into\n // the top-level `system` field. Every other system message keeps its\n // position and is sent as a `role: \"system\"` entry, which the provider\n // applies from that point in the conversation onwards. The run is identified\n // after tool-message folding, because that is the sequence the provider sees.\n let leadingSystemCount = 0;\n while (\n leadingSystemCount < mergedMessages.length &&\n mergedMessages[leadingSystemCount]._getType() === \"system\"\n ) {\n leadingSystemCount += 1;\n }\n\n let system: string | AnthropicSystemContentBlockParam[] | undefined;\n if (leadingSystemCount === 1) {\n system = _formatSystemContent(mergedMessages[0].content);\n } else if (leadingSystemCount > 1) {\n system = mergedMessages.slice(0, leadingSystemCount).flatMap((message) => {\n const content = _formatSystemContent(message.content);\n return typeof content === \"string\"\n ? [{ type: \"text\" as const, text: content }]\n : content;\n });\n }\n // The provider rejects empty system content, so system content that narrowed\n // to nothing is omitted rather than sent.\n if (Array.isArray(system) && system.length === 0) {\n system = undefined;\n }\n\n const conversationMessages = mergedMessages.slice(leadingSystemCount);\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 role = \"system\" as const;\n } else {\n throw new Error(`Message type \"${message.type}\" is not supported.`);\n }\n if (\n AIMessage.isInstance(message) &&\n message.response_metadata?.output_version === \"v1\"\n ) {\n return {\n role,\n content: _formatStandardContent(message),\n };\n }\n if (AIMessage.isInstance(message) && !!message.tool_calls?.length) {\n if (typeof message.content === \"string\") {\n if (message.content === \"\") {\n return {\n role,\n content: message.tool_calls.map(\n _convertLangChainToolCallToAnthropic\n ),\n };\n } else {\n return {\n role,\n content: [\n { type: \"text\", text: message.content },\n ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const { content } = message;\n const formattedContent = _formatContent(message, message.tool_calls);\n const formattedContentArr = Array.isArray(formattedContent)\n ? formattedContent\n : [{ type: \"text\" as const, text: formattedContent }];\n const missingToolCalls = message.tool_calls.filter(\n (toolCall) =>\n !content.find(\n (contentPart) =>\n (contentPart.type === \"tool_use\" ||\n contentPart.type === \"input_json_delta\" ||\n contentPart.type === \"server_tool_use\") &&\n contentPart.id === toolCall.id\n )\n );\n return {\n role,\n content: [\n ...formattedContentArr,\n ...missingToolCalls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else if (role === \"system\") {\n const content = _formatSystemContent(message.content);\n return Array.isArray(content) && content.length === 0\n ? undefined\n : { role, content };\n } else {\n return {\n role,\n content: _formatContent(\n message,\n AIMessage.isInstance(message) ? message.tool_calls : undefined\n ),\n };\n }\n });\n return {\n messages: mergeMessages(\n formattedMessages.filter(\n (message) => message !== undefined\n ) as AnthropicMessageCreateParams[\"messages\"]\n ),\n system,\n } as AnthropicMessageCreateParams;\n}\n\n/**\n * Describes the tool changes in the system content of a converted payload:\n * `\"inline\"` if any `tool_addition` block defines a tool by value,\n * `\"reference\"` if tool changes only name tools declared elsewhere, or\n * `undefined` if there are none. The provider requires a different beta for\n * each.\n *\n * Reads the converted payload rather than the input messages, so a block that\n * was dropped during conversion never enables a beta.\n */\nexport function _getToolChangeKind(\n payload: AnthropicMessageCreateParams\n): \"inline\" | \"reference\" | undefined {\n const systemContents: unknown[] = [\n payload.system,\n ...payload.messages\n .filter((message) => message.role === \"system\")\n .map((message) => message.content),\n ];\n const toolChanges = systemContents.flatMap((content) =>\n Array.isArray(content)\n ? content.filter((block) => TOOL_CHANGE_BLOCK_TYPES.includes(block.type))\n : []\n );\n if (toolChanges.some((block) => block.tool?.type === \"tool_definition\")) {\n return \"inline\";\n }\n return toolChanges.length > 0 ? \"reference\" : undefined;\n}\n\nfunction mergeMessages(messages: AnthropicMessageCreateParams[\"messages\"]) {\n if (!messages || messages.length <= 1) {\n return messages;\n }\n\n const result: AnthropicMessageCreateParams[\"messages\"] = [];\n let currentMessage = messages[0];\n\n const normalizeContent = (\n content: string | Array<Anthropic.Messages.ContentBlockParam>\n ): Array<Anthropic.Messages.ContentBlockParam> => {\n if (typeof content === \"string\") {\n return [\n {\n type: \"text\",\n text: content,\n },\n ];\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":";;;;AAoCA,SAAS,aAAa,UAAkB;CACtC,MAAM,SAAS,mBAAmB,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;EAC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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"}
@@ -37,7 +37,6 @@ import { BasePromptValue } from "@langchain/core/prompt_values";
37
37
  * @param formattedPrompt
38
38
  * @returns A partial Anthropic payload.
39
39
  */
40
- declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic$1.Messages.MessageCreateParams;
40
+ export declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic$1.Messages.MessageCreateParams;
41
41
  //#endregion
42
- export { convertPromptToAnthropic };
43
42
  //# sourceMappingURL=prompts.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"prompts.d.cts","names":[],"sources":["../../src/utils/prompts.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,yBACd,iBAAiB,kBAChB,YAAU,SAAS"}
1
+ {"version":3,"file":"prompts.d.cts","names":["Anthropic"],"sources":["../../src/utils/prompts.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAyCgB,yBACd,iBAAiB,kBAChBA,YAAU,SAAS"}
@@ -37,7 +37,6 @@ import { BasePromptValue } from "@langchain/core/prompt_values";
37
37
  * @param formattedPrompt
38
38
  * @returns A partial Anthropic payload.
39
39
  */
40
- declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic$1.Messages.MessageCreateParams;
40
+ export declare function convertPromptToAnthropic(formattedPrompt: BasePromptValue): Anthropic$1.Messages.MessageCreateParams;
41
41
  //#endregion
42
- export { convertPromptToAnthropic };
43
42
  //# sourceMappingURL=prompts.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"prompts.d.ts","names":[],"sources":["../../src/utils/prompts.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,yBACd,iBAAiB,kBAChB,YAAU,SAAS"}
1
+ {"version":3,"file":"prompts.d.ts","names":["Anthropic"],"sources":["../../src/utils/prompts.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAyCgB,yBACd,iBAAiB,kBAChBA,YAAU,SAAS"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/anthropic",
3
- "version": "1.5.11",
3
+ "version": "1.5.13",
4
4
  "description": "Anthropic integrations for LangChain.js",
5
5
  "author": "LangChain",
6
6
  "license": "MIT",
@@ -14,23 +14,23 @@
14
14
  },
15
15
  "homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/providers/langchain-anthropic/",
16
16
  "dependencies": {
17
- "@anthropic-ai/sdk": "^0.122.0",
17
+ "@anthropic-ai/sdk": "^0.129.0",
18
18
  "zod": "^3.25.76 || ^4"
19
19
  },
20
20
  "peerDependencies": {
21
- "@langchain/core": "^1.2.12"
21
+ "@langchain/core": "^1.2.20"
22
22
  },
23
23
  "devDependencies": {
24
- "@anthropic-ai/vertex-sdk": "^0.19.6",
24
+ "@anthropic-ai/vertex-sdk": "^0.20.0",
25
25
  "@cfworker/json-schema": "^4.1.1",
26
26
  "@tsconfig/recommended": "^1.0.10",
27
- "@vitest/coverage-v8": "^4.1.11",
27
+ "@vitest/coverage-v8": "^5.0.2",
28
28
  "dotenv": "^17.4.2",
29
29
  "dpdm": "^4.3.0",
30
30
  "rimraf": "^6.1.3",
31
31
  "typescript": "~7.0.2",
32
- "vitest": "^4.1.11",
33
- "@langchain/core": "^1.2.12",
32
+ "vitest": "^5.0.2",
33
+ "@langchain/core": "^1.2.20",
34
34
  "@langchain/standard-tests": "0.0.23",
35
35
  "@langchain/tsconfig": "0.0.1"
36
36
  },