@langchain/core 1.1.36 → 1.1.38

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 (44) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/language_models/base.cjs +1 -1
  3. package/dist/language_models/base.cjs.map +1 -1
  4. package/dist/language_models/base.d.cts +1 -1
  5. package/dist/language_models/base.d.cts.map +1 -1
  6. package/dist/language_models/base.d.ts +1 -1
  7. package/dist/language_models/base.d.ts.map +1 -1
  8. package/dist/language_models/base.js +1 -1
  9. package/dist/language_models/base.js.map +1 -1
  10. package/dist/language_models/chat_models.cjs +6 -21
  11. package/dist/language_models/chat_models.cjs.map +1 -1
  12. package/dist/language_models/chat_models.d.cts.map +1 -1
  13. package/dist/language_models/chat_models.d.ts.map +1 -1
  14. package/dist/language_models/chat_models.js +7 -22
  15. package/dist/language_models/chat_models.js.map +1 -1
  16. package/dist/language_models/llms.cjs +6 -22
  17. package/dist/language_models/llms.cjs.map +1 -1
  18. package/dist/language_models/llms.d.cts.map +1 -1
  19. package/dist/language_models/llms.d.ts.map +1 -1
  20. package/dist/language_models/llms.js +6 -22
  21. package/dist/language_models/llms.js.map +1 -1
  22. package/dist/language_models/utils.cjs +0 -4
  23. package/dist/language_models/utils.cjs.map +1 -1
  24. package/dist/language_models/utils.js +1 -4
  25. package/dist/language_models/utils.js.map +1 -1
  26. package/dist/messages/base.cjs +19 -3
  27. package/dist/messages/base.cjs.map +1 -1
  28. package/dist/messages/base.d.cts.map +1 -1
  29. package/dist/messages/base.d.ts.map +1 -1
  30. package/dist/messages/base.js +19 -3
  31. package/dist/messages/base.js.map +1 -1
  32. package/dist/messages/block_translators/openai.cjs +6 -1
  33. package/dist/messages/block_translators/openai.cjs.map +1 -1
  34. package/dist/messages/block_translators/openai.js +6 -1
  35. package/dist/messages/block_translators/openai.js.map +1 -1
  36. package/dist/utils/json_schema.cjs +31 -7
  37. package/dist/utils/json_schema.cjs.map +1 -1
  38. package/dist/utils/json_schema.d.cts +1 -0
  39. package/dist/utils/json_schema.d.cts.map +1 -1
  40. package/dist/utils/json_schema.d.ts +1 -0
  41. package/dist/utils/json_schema.d.ts.map +1 -1
  42. package/dist/utils/json_schema.js +31 -7
  43. package/dist/utils/json_schema.js.map +1 -1
  44. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"openai.js","names":[],"sources":["../../../src/messages/block_translators/openai.ts"],"sourcesContent":["import type { ContentBlock } from \"../content/index.js\";\nimport type { AIMessageChunk, AIMessage } from \"../ai.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { convertToV1FromOpenAIDataBlock, isOpenAIDataBlock } from \"./data.js\";\nimport {\n _isArray,\n _isContentBlock,\n _isObject,\n _isString,\n iife,\n} from \"./utils.js\";\n\n/**\n * Converts a ChatOpenAICompletions message to an array of v1 standard content blocks.\n *\n * This function processes an AI message from ChatOpenAICompletions API format\n * and converts it to the standardized v1 content block format. It handles both\n * string content and structured content blocks, as well as tool calls.\n *\n * @param message - The AI message containing ChatOpenAICompletions formatted content\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const message = new AIMessage(\"Hello world\");\n * const standardBlocks = convertToV1FromChatCompletions(message);\n * // Returns: [{ type: \"text\", text: \"Hello world\" }]\n * ```\n *\n * @example\n * ```typescript\n * const message = new AIMessage([\n * { type: \"text\", text: \"Hello\" },\n * { type: \"image_url\", image_url: { url: \"https://example.com/image.png\" } }\n * ]);\n * message.tool_calls = [\n * { id: \"call_123\", name: \"calculator\", args: { a: 1, b: 2 } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletions(message);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello\" },\n * // { type: \"image\", url: \"https://example.com/image.png\" },\n * // { type: \"tool_call\", id: \"call_123\", name: \"calculator\", args: { a: 1, b: 2 } }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletions(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n if (typeof message.content === \"string\") {\n // Only add text block if content is non-empty\n if (message.content.length > 0) {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n }\n } else {\n blocks.push(...convertToV1FromChatCompletionsInput(message.content));\n }\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n return blocks;\n}\n\n/**\n * Converts a ChatOpenAICompletions message chunk to an array of v1 standard content blocks.\n *\n * This function processes an AI message chunk from OpenAI's chat completions API and converts\n * it to the standardized v1 content block format. It handles both string and array content,\n * as well as tool calls that may be present in the chunk.\n *\n * @param message - The AI message chunk containing OpenAI-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const chunk = new AIMessage(\"Hello\");\n * const standardBlocks = convertToV1FromChatCompletionsChunk(chunk);\n * // Returns: [{ type: \"text\", text: \"Hello\" }]\n * ```\n *\n * @example\n * ```typescript\n * const chunk = new AIMessage([\n * { type: \"text\", text: \"Processing...\" }\n * ]);\n * chunk.tool_calls = [\n * { id: \"call_456\", name: \"search\", args: { query: \"test\" } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletionsChunk(chunk);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Processing...\" },\n * // { type: \"tool_call\", id: \"call_456\", name: \"search\", args: { query: \"test\" } }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletionsChunk(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n if (typeof message.content === \"string\") {\n // Only add text block if content is non-empty\n if (message.content.length > 0) {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n }\n } else {\n blocks.push(...convertToV1FromChatCompletionsInput(message.content));\n }\n\n // TODO: parse chunk position information\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n return blocks;\n}\n\n/**\n * Converts an array of ChatOpenAICompletions content blocks to v1 standard content blocks.\n *\n * This function processes content blocks from OpenAI's Chat Completions API format\n * and converts them to the standardized v1 content block format. It handles both\n * OpenAI-specific data blocks (which require conversion) and standard blocks\n * (which are passed through with type assertion).\n *\n * @param blocks - Array of content blocks in ChatOpenAICompletions format\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const openaiBlocks = [\n * { type: \"text\", text: \"Hello world\" },\n * { type: \"image_url\", image_url: { url: \"https://example.com/image.png\" } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletionsInput(openaiBlocks);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello world\" },\n * // { type: \"image\", url: \"https://example.com/image.png\" }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletionsInput(\n blocks: Array<ContentBlock>\n): Array<ContentBlock.Standard> {\n const convertedBlocks: Array<ContentBlock.Standard> = [];\n for (const block of blocks) {\n if (isOpenAIDataBlock(block)) {\n convertedBlocks.push(convertToV1FromOpenAIDataBlock(block));\n } else {\n convertedBlocks.push(block as ContentBlock.Standard);\n }\n }\n return convertedBlocks;\n}\n\nfunction convertResponsesAnnotation(\n annotation: ContentBlock\n): ContentBlock | ContentBlock.Citation {\n if (annotation.type === \"url_citation\") {\n const { url, title, start_index, end_index } = annotation;\n return {\n type: \"citation\",\n url,\n title,\n startIndex: start_index,\n endIndex: end_index,\n };\n }\n if (annotation.type === \"file_citation\") {\n const { file_id, filename, index } = annotation;\n return {\n type: \"citation\",\n title: filename,\n startIndex: index,\n endIndex: index,\n fileId: file_id,\n };\n }\n return annotation;\n}\n\n/**\n * Converts a ChatOpenAIResponses message to an array of v1 standard content blocks.\n *\n * This function processes an AI message containing OpenAI Responses-specific content blocks\n * and converts them to the standardized v1 content block format. It handles reasoning summaries,\n * text content with annotations, tool calls, and various tool outputs including code interpreter,\n * web search, file search, computer calls, and MCP-related blocks.\n *\n * @param message - The AI message containing OpenAI Responses-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const message = new AIMessage({\n * content: [{ type: \"text\", text: \"Hello world\", annotations: [] }],\n * tool_calls: [{ id: \"123\", name: \"calculator\", args: { a: 1, b: 2 } }],\n * additional_kwargs: {\n * reasoning: { summary: [{ text: \"Let me calculate this...\" }] },\n * tool_outputs: [\n * {\n * type: \"code_interpreter_call\",\n * code: \"print('hello')\",\n * outputs: [{ type: \"logs\", logs: \"hello\" }]\n * }\n * ]\n * }\n * });\n *\n * const standardBlocks = convertToV1FromResponses(message);\n * // Returns:\n * // [\n * // { type: \"reasoning\", reasoning: \"Let me calculate this...\" },\n * // { type: \"text\", text: \"Hello world\", annotations: [] },\n * // { type: \"tool_call\", id: \"123\", name: \"calculator\", args: { a: 1, b: 2 } },\n * // { type: \"code_interpreter_call\", code: \"print('hello')\" },\n * // { type: \"code_interpreter_result\", output: [{ type: \"code_interpreter_output\", returnCode: 0, stdout: \"hello\" }] }\n * // ]\n * ```\n */\nexport function convertToV1FromResponses(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n if (\n _isObject(message.additional_kwargs?.reasoning) &&\n _isArray(message.additional_kwargs.reasoning.summary)\n ) {\n const summary =\n message.additional_kwargs.reasoning.summary.reduce<string>(\n (acc, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return `${acc}${item.text}`;\n }\n return acc;\n },\n \"\"\n );\n yield {\n type: \"reasoning\",\n reasoning: summary,\n };\n }\n const content =\n typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n for (const block of content) {\n if (_isContentBlock(block, \"text\")) {\n const { text, annotations, ...rest } = block;\n if (Array.isArray(annotations)) {\n yield {\n ...rest,\n type: \"text\",\n text: String(text),\n annotations: annotations.map(convertResponsesAnnotation),\n };\n } else {\n yield {\n ...rest,\n type: \"text\",\n text: String(text),\n };\n }\n }\n }\n for (const toolCall of message.tool_calls ?? []) {\n yield {\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n };\n }\n if (\n _isObject(message.additional_kwargs) &&\n _isArray(message.additional_kwargs.tool_outputs)\n ) {\n for (const toolOutput of message.additional_kwargs.tool_outputs) {\n if (_isContentBlock(toolOutput, \"web_search_call\")) {\n /**\n * Build args from available action data.\n * The ResponseFunctionWebSearch base type only has id, status, type.\n * The action field (with query, sources, etc.) may be present at\n * runtime when the `include` parameter includes \"web_search_call.action.sources\".\n */\n const webSearchArgs: Record<string, unknown> = {};\n if (\n _isObject(toolOutput.action) &&\n _isString(toolOutput.action.query)\n ) {\n webSearchArgs.query = toolOutput.action.query;\n }\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"web_search\",\n args: webSearchArgs,\n };\n // Emit a server_tool_call_result when the search has completed or failed\n if (\n toolOutput.status === \"completed\" ||\n toolOutput.status === \"failed\"\n ) {\n const output: Record<string, unknown> = {};\n if (_isObject(toolOutput.action)) {\n output.action = toolOutput.action;\n }\n yield {\n type: \"server_tool_call_result\",\n toolCallId: _isString(toolOutput.id) ? toolOutput.id : \"\",\n status: toolOutput.status === \"completed\" ? \"success\" : \"error\",\n output,\n };\n }\n continue;\n } else if (_isContentBlock(toolOutput, \"file_search_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"file_search\",\n args: {\n queries: _isArray(toolOutput.queries) ? toolOutput.queries : [],\n },\n };\n // Emit a server_tool_call_result when results are available\n if (\n toolOutput.status === \"completed\" ||\n toolOutput.status === \"failed\"\n ) {\n yield {\n type: \"server_tool_call_result\",\n toolCallId: _isString(toolOutput.id) ? toolOutput.id : \"\",\n status: toolOutput.status === \"completed\" ? \"success\" : \"error\",\n output: _isArray(toolOutput.results)\n ? { results: toolOutput.results }\n : {},\n };\n }\n continue;\n } else if (_isContentBlock(toolOutput, \"computer_call\")) {\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n } else if (_isContentBlock(toolOutput, \"code_interpreter_call\")) {\n if (_isString(toolOutput.code)) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"code_interpreter\",\n args: { code: toolOutput.code },\n };\n }\n if (_isArray(toolOutput.outputs)) {\n const returnCode = iife(() => {\n if (toolOutput.status === \"in_progress\") return undefined;\n if (toolOutput.status === \"completed\") return 0;\n if (toolOutput.status === \"incomplete\") return 127;\n if (toolOutput.status === \"interpreting\") return undefined;\n if (toolOutput.status === \"failed\") return 1;\n return undefined;\n });\n for (const output of toolOutput.outputs) {\n if (_isContentBlock(output, \"logs\")) {\n yield {\n type: \"server_tool_call_result\",\n toolCallId: toolOutput.id ?? \"\",\n status: \"success\",\n output: {\n type: \"code_interpreter_output\",\n returnCode: returnCode ?? 0,\n stderr: [0, undefined].includes(returnCode)\n ? undefined\n : String(output.logs),\n stdout: [0, undefined].includes(returnCode)\n ? String(output.logs)\n : undefined,\n },\n };\n continue;\n }\n }\n }\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"mcp_call\",\n args: toolOutput.input,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_list_tools\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"mcp_list_tools\",\n args: toolOutput.input,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_approval_request\")) {\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n } else if (_isContentBlock(toolOutput, \"tool_search_call\")) {\n const toolSearchArgs: Record<string, unknown> = {};\n if (_isObject(toolOutput.arguments)) {\n Object.assign(toolSearchArgs, toolOutput.arguments);\n }\n const toolSearchCallExtras: Record<string, unknown> = {};\n if (_isString(toolOutput.execution)) {\n toolSearchCallExtras.execution = toolOutput.execution;\n }\n if (_isString(toolOutput.status)) {\n toolSearchCallExtras.status = toolOutput.status;\n }\n if (_isString(toolOutput.call_id)) {\n toolSearchCallExtras.call_id = toolOutput.call_id;\n }\n yield {\n id: _isString(toolOutput.id) ? toolOutput.id : \"\",\n type: \"server_tool_call\",\n name: \"tool_search\",\n args: toolSearchArgs,\n ...(Object.keys(toolSearchCallExtras).length > 0\n ? { extras: toolSearchCallExtras }\n : {}),\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"tool_search_output\")) {\n const toolSearchOutputExtras: Record<string, unknown> = {\n name: \"tool_search\",\n };\n if (_isString(toolOutput.execution)) {\n toolSearchOutputExtras.execution = toolOutput.execution;\n }\n yield {\n type: \"server_tool_call_result\",\n toolCallId: _isString(toolOutput.id) ? toolOutput.id : \"\",\n status:\n toolOutput.status === \"completed\"\n ? \"success\"\n : toolOutput.status === \"failed\"\n ? \"error\"\n : \"success\",\n output: {\n tools: _isArray(toolOutput.tools) ? toolOutput.tools : [],\n },\n extras: toolSearchOutputExtras,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"image_generation_call\")) {\n // Convert image_generation_call to proper image content block if result is available\n if (_isString(toolOutput.result)) {\n yield {\n type: \"image\",\n mimeType: \"image/png\",\n data: toolOutput.result,\n id: _isString(toolOutput.id) ? toolOutput.id : undefined,\n metadata: {\n status: _isString(toolOutput.status)\n ? toolOutput.status\n : undefined,\n },\n };\n }\n // Also yield as non_standard for backwards compatibility\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n }\n if (_isObject(toolOutput)) {\n yield { type: \"non_standard\", value: toolOutput };\n }\n }\n }\n }\n return Array.from(iterateContent());\n}\n\n/**\n * Converts a ChatOpenAIResponses message chunk to an array of v1 standard content blocks.\n *\n * This function processes an AI message chunk containing OpenAI-specific content blocks\n * and converts them to the standardized v1 content block format. It handles both the\n * regular message content and tool call chunks that are specific to streaming responses.\n *\n * @param message - The AI message chunk containing OpenAI-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const messageChunk = new AIMessageChunk({\n * content: [{ type: \"text\", text: \"Hello\" }],\n * tool_call_chunks: [\n * { id: \"call_123\", name: \"calculator\", args: '{\"a\": 1' }\n * ]\n * });\n *\n * const standardBlocks = convertToV1FromResponsesChunk(messageChunk);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello\" },\n * // { type: \"tool_call_chunk\", id: \"call_123\", name: \"calculator\", args: '{\"a\": 1' }\n * // ]\n * ```\n */\nexport function convertToV1FromResponsesChunk(\n message: AIMessageChunk\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n yield* convertToV1FromResponses(message);\n for (const toolCallChunk of message.tool_call_chunks ?? []) {\n yield {\n type: \"tool_call_chunk\",\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n args: toolCallChunk.args,\n };\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatOpenAITranslator: StandardContentBlockTranslator = {\n translateContent: (message) => {\n if (typeof message.content === \"string\") {\n return convertToV1FromChatCompletions(message);\n }\n return convertToV1FromResponses(message);\n },\n translateContentChunk: (message) => {\n if (typeof message.content === \"string\") {\n return convertToV1FromChatCompletionsChunk(message);\n }\n return convertToV1FromResponsesChunk(message);\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,+BACd,SAC8B;CAC9B,MAAM,SAAuC,EAAE;AAC/C,KAAI,OAAO,QAAQ,YAAY;MAEzB,QAAQ,QAAQ,SAAS,EAC3B,QAAO,KAAK;GACV,MAAM;GACN,MAAM,QAAQ;GACf,CAAC;OAGJ,QAAO,KAAK,GAAG,oCAAoC,QAAQ,QAAQ,CAAC;AAEtE,MAAK,MAAM,YAAY,QAAQ,cAAc,EAAE,CAC7C,QAAO,KAAK;EACV,MAAM;EACN,IAAI,SAAS;EACb,MAAM,SAAS;EACf,MAAM,SAAS;EAChB,CAAC;AAEJ,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCT,SAAgB,oCACd,SAC8B;CAC9B,MAAM,SAAuC,EAAE;AAC/C,KAAI,OAAO,QAAQ,YAA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UAAU,WAAW,GAAG,GAAG,WAAW,KAAK,KAAA;KAC/C,UAAU,EACR,QAAQ,UAAU,WAAW,OAAO,GAChC,WAAW,SACX,KAAA,GACL;KACF;AAGH,UAAM;KAAE,MAAM;KAAgB,OAAO;KAAY;AACjD;;AAEF,OAAI,UAAU,WAAW,CACvB,OAAM;IAAE,MAAM;IAAgB,OAAO;IAAY;;;AAKzD,QAAO,MAAM,KAAK,gBAAgB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BrC,SAAgB,8BACd,SAC8B;CAC9B,UAAU,iBAAkD;AAC1D,SAAO,yBAAyB,QAAQ;AACxC,OAAK,MAAM,iBAAiB,QAAQ,oBAAoB,EAAE,CACxD,OAAM;GACJ,MAAM;GACN,IAAI,cAAc;GAClB,MAAM,cAAc;GACpB,MAAM,cAAc;GACrB;;AAGL,QAAO,MAAM,KAAK,gBAAgB,CAAC;;AAGrC,MAAa,uBAAuD;CAClE,mBAAmB,YAAY;AAC7B,MAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,+BAA+B,QAAQ;AAEhD,SAAO,yBAAyB,QAAQ;;CAE1C,wBAAwB,YAAY;AAClC,MAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,oCAAoC,QAAQ;AAErD,SAAO,8BAA8B,QAAQ;;CAEhD"}
1
+ {"version":3,"file":"openai.js","names":[],"sources":["../../../src/messages/block_translators/openai.ts"],"sourcesContent":["import type { ContentBlock } from \"../content/index.js\";\nimport type { AIMessageChunk, AIMessage } from \"../ai.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { convertToV1FromOpenAIDataBlock, isOpenAIDataBlock } from \"./data.js\";\nimport {\n _isArray,\n _isContentBlock,\n _isObject,\n _isString,\n iife,\n} from \"./utils.js\";\n\n/**\n * Converts a ChatOpenAICompletions message to an array of v1 standard content blocks.\n *\n * This function processes an AI message from ChatOpenAICompletions API format\n * and converts it to the standardized v1 content block format. It handles both\n * string content and structured content blocks, as well as tool calls.\n *\n * @param message - The AI message containing ChatOpenAICompletions formatted content\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const message = new AIMessage(\"Hello world\");\n * const standardBlocks = convertToV1FromChatCompletions(message);\n * // Returns: [{ type: \"text\", text: \"Hello world\" }]\n * ```\n *\n * @example\n * ```typescript\n * const message = new AIMessage([\n * { type: \"text\", text: \"Hello\" },\n * { type: \"image_url\", image_url: { url: \"https://example.com/image.png\" } }\n * ]);\n * message.tool_calls = [\n * { id: \"call_123\", name: \"calculator\", args: { a: 1, b: 2 } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletions(message);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello\" },\n * // { type: \"image\", url: \"https://example.com/image.png\" },\n * // { type: \"tool_call\", id: \"call_123\", name: \"calculator\", args: { a: 1, b: 2 } }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletions(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n if (typeof message.content === \"string\") {\n // Only add text block if content is non-empty\n if (message.content.length > 0) {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n }\n } else {\n blocks.push(...convertToV1FromChatCompletionsInput(message.content));\n }\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n return blocks;\n}\n\n/**\n * Converts a ChatOpenAICompletions message chunk to an array of v1 standard content blocks.\n *\n * This function processes an AI message chunk from OpenAI's chat completions API and converts\n * it to the standardized v1 content block format. It handles both string and array content,\n * as well as tool calls that may be present in the chunk.\n *\n * @param message - The AI message chunk containing OpenAI-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const chunk = new AIMessage(\"Hello\");\n * const standardBlocks = convertToV1FromChatCompletionsChunk(chunk);\n * // Returns: [{ type: \"text\", text: \"Hello\" }]\n * ```\n *\n * @example\n * ```typescript\n * const chunk = new AIMessage([\n * { type: \"text\", text: \"Processing...\" }\n * ]);\n * chunk.tool_calls = [\n * { id: \"call_456\", name: \"search\", args: { query: \"test\" } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletionsChunk(chunk);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Processing...\" },\n * // { type: \"tool_call\", id: \"call_456\", name: \"search\", args: { query: \"test\" } }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletionsChunk(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n if (typeof message.content === \"string\") {\n // Only add text block if content is non-empty\n if (message.content.length > 0) {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n }\n } else {\n blocks.push(...convertToV1FromChatCompletionsInput(message.content));\n }\n\n // TODO: parse chunk position information\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n return blocks;\n}\n\n/**\n * Converts an array of ChatOpenAICompletions content blocks to v1 standard content blocks.\n *\n * This function processes content blocks from OpenAI's Chat Completions API format\n * and converts them to the standardized v1 content block format. It handles both\n * OpenAI-specific data blocks (which require conversion) and standard blocks\n * (which are passed through with type assertion).\n *\n * @param blocks - Array of content blocks in ChatOpenAICompletions format\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const openaiBlocks = [\n * { type: \"text\", text: \"Hello world\" },\n * { type: \"image_url\", image_url: { url: \"https://example.com/image.png\" } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletionsInput(openaiBlocks);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello world\" },\n * // { type: \"image\", url: \"https://example.com/image.png\" }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletionsInput(\n blocks: Array<ContentBlock>\n): Array<ContentBlock.Standard> {\n const convertedBlocks: Array<ContentBlock.Standard> = [];\n for (const block of blocks) {\n if (isOpenAIDataBlock(block)) {\n convertedBlocks.push(convertToV1FromOpenAIDataBlock(block));\n } else {\n convertedBlocks.push(block as ContentBlock.Standard);\n }\n }\n return convertedBlocks;\n}\n\nfunction convertResponsesAnnotation(\n annotation: ContentBlock\n): ContentBlock | ContentBlock.Citation {\n if (annotation.type === \"url_citation\") {\n const { url, title, start_index, end_index } = annotation;\n return {\n type: \"citation\",\n url,\n title,\n startIndex: start_index,\n endIndex: end_index,\n };\n }\n if (annotation.type === \"file_citation\") {\n const { file_id, filename, index } = annotation;\n return {\n type: \"citation\",\n title: filename,\n startIndex: index,\n endIndex: index,\n fileId: file_id,\n };\n }\n return annotation;\n}\n\n/**\n * Converts a ChatOpenAIResponses message to an array of v1 standard content blocks.\n *\n * This function processes an AI message containing OpenAI Responses-specific content blocks\n * and converts them to the standardized v1 content block format. It handles reasoning summaries,\n * text content with annotations, tool calls, and various tool outputs including code interpreter,\n * web search, file search, computer calls, and MCP-related blocks.\n *\n * @param message - The AI message containing OpenAI Responses-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const message = new AIMessage({\n * content: [{ type: \"text\", text: \"Hello world\", annotations: [] }],\n * tool_calls: [{ id: \"123\", name: \"calculator\", args: { a: 1, b: 2 } }],\n * additional_kwargs: {\n * reasoning: { summary: [{ text: \"Let me calculate this...\" }] },\n * tool_outputs: [\n * {\n * type: \"code_interpreter_call\",\n * code: \"print('hello')\",\n * outputs: [{ type: \"logs\", logs: \"hello\" }]\n * }\n * ]\n * }\n * });\n *\n * const standardBlocks = convertToV1FromResponses(message);\n * // Returns:\n * // [\n * // { type: \"reasoning\", reasoning: \"Let me calculate this...\" },\n * // { type: \"text\", text: \"Hello world\", annotations: [] },\n * // { type: \"tool_call\", id: \"123\", name: \"calculator\", args: { a: 1, b: 2 } },\n * // { type: \"code_interpreter_call\", code: \"print('hello')\" },\n * // { type: \"code_interpreter_result\", output: [{ type: \"code_interpreter_output\", returnCode: 0, stdout: \"hello\" }] }\n * // ]\n * ```\n */\nexport function convertToV1FromResponses(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n if (\n _isObject(message.additional_kwargs?.reasoning) &&\n _isArray(message.additional_kwargs.reasoning.summary)\n ) {\n const summary =\n message.additional_kwargs.reasoning.summary.reduce<string>(\n (acc, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return `${acc}${item.text}`;\n }\n return acc;\n },\n \"\"\n );\n yield {\n type: \"reasoning\",\n reasoning: summary,\n };\n }\n const content =\n typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n for (const block of content) {\n if (_isContentBlock(block, \"text\")) {\n const {\n text,\n annotations,\n phase,\n extras: existingExtras,\n ...rest\n } = block;\n const extras: Record<string, unknown> = _isObject(existingExtras)\n ? { ...(existingExtras as Record<string, unknown>) }\n : {};\n if (_isString(phase)) {\n extras.phase = phase;\n }\n const extrasSpread = Object.keys(extras).length > 0 ? { extras } : {};\n if (Array.isArray(annotations)) {\n yield {\n ...rest,\n ...extrasSpread,\n type: \"text\",\n text: String(text),\n annotations: annotations.map(convertResponsesAnnotation),\n };\n } else {\n yield {\n ...rest,\n ...extrasSpread,\n type: \"text\",\n text: String(text),\n };\n }\n }\n }\n for (const toolCall of message.tool_calls ?? []) {\n yield {\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n };\n }\n if (\n _isObject(message.additional_kwargs) &&\n _isArray(message.additional_kwargs.tool_outputs)\n ) {\n for (const toolOutput of message.additional_kwargs.tool_outputs) {\n if (_isContentBlock(toolOutput, \"web_search_call\")) {\n /**\n * Build args from available action data.\n * The ResponseFunctionWebSearch base type only has id, status, type.\n * The action field (with query, sources, etc.) may be present at\n * runtime when the `include` parameter includes \"web_search_call.action.sources\".\n */\n const webSearchArgs: Record<string, unknown> = {};\n if (\n _isObject(toolOutput.action) &&\n _isString(toolOutput.action.query)\n ) {\n webSearchArgs.query = toolOutput.action.query;\n }\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"web_search\",\n args: webSearchArgs,\n };\n // Emit a server_tool_call_result when the search has completed or failed\n if (\n toolOutput.status === \"completed\" ||\n toolOutput.status === \"failed\"\n ) {\n const output: Record<string, unknown> = {};\n if (_isObject(toolOutput.action)) {\n output.action = toolOutput.action;\n }\n yield {\n type: \"server_tool_call_result\",\n toolCallId: _isString(toolOutput.id) ? toolOutput.id : \"\",\n status: toolOutput.status === \"completed\" ? \"success\" : \"error\",\n output,\n };\n }\n continue;\n } else if (_isContentBlock(toolOutput, \"file_search_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"file_search\",\n args: {\n queries: _isArray(toolOutput.queries) ? toolOutput.queries : [],\n },\n };\n // Emit a server_tool_call_result when results are available\n if (\n toolOutput.status === \"completed\" ||\n toolOutput.status === \"failed\"\n ) {\n yield {\n type: \"server_tool_call_result\",\n toolCallId: _isString(toolOutput.id) ? toolOutput.id : \"\",\n status: toolOutput.status === \"completed\" ? \"success\" : \"error\",\n output: _isArray(toolOutput.results)\n ? { results: toolOutput.results }\n : {},\n };\n }\n continue;\n } else if (_isContentBlock(toolOutput, \"computer_call\")) {\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n } else if (_isContentBlock(toolOutput, \"code_interpreter_call\")) {\n if (_isString(toolOutput.code)) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"code_interpreter\",\n args: { code: toolOutput.code },\n };\n }\n if (_isArray(toolOutput.outputs)) {\n const returnCode = iife(() => {\n if (toolOutput.status === \"in_progress\") return undefined;\n if (toolOutput.status === \"completed\") return 0;\n if (toolOutput.status === \"incomplete\") return 127;\n if (toolOutput.status === \"interpreting\") return undefined;\n if (toolOutput.status === \"failed\") return 1;\n return undefined;\n });\n for (const output of toolOutput.outputs) {\n if (_isContentBlock(output, \"logs\")) {\n yield {\n type: \"server_tool_call_result\",\n toolCallId: toolOutput.id ?? \"\",\n status: \"success\",\n output: {\n type: \"code_interpreter_output\",\n returnCode: returnCode ?? 0,\n stderr: [0, undefined].includes(returnCode)\n ? undefined\n : String(output.logs),\n stdout: [0, undefined].includes(returnCode)\n ? String(output.logs)\n : undefined,\n },\n };\n continue;\n }\n }\n }\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"mcp_call\",\n args: toolOutput.input,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_list_tools\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"mcp_list_tools\",\n args: toolOutput.input,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_approval_request\")) {\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n } else if (_isContentBlock(toolOutput, \"tool_search_call\")) {\n const toolSearchArgs: Record<string, unknown> = {};\n if (_isObject(toolOutput.arguments)) {\n Object.assign(toolSearchArgs, toolOutput.arguments);\n }\n const toolSearchCallExtras: Record<string, unknown> = {};\n if (_isString(toolOutput.execution)) {\n toolSearchCallExtras.execution = toolOutput.execution;\n }\n if (_isString(toolOutput.status)) {\n toolSearchCallExtras.status = toolOutput.status;\n }\n if (_isString(toolOutput.call_id)) {\n toolSearchCallExtras.call_id = toolOutput.call_id;\n }\n yield {\n id: _isString(toolOutput.id) ? toolOutput.id : \"\",\n type: \"server_tool_call\",\n name: \"tool_search\",\n args: toolSearchArgs,\n ...(Object.keys(toolSearchCallExtras).length > 0\n ? { extras: toolSearchCallExtras }\n : {}),\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"tool_search_output\")) {\n const toolSearchOutputExtras: Record<string, unknown> = {\n name: \"tool_search\",\n };\n if (_isString(toolOutput.execution)) {\n toolSearchOutputExtras.execution = toolOutput.execution;\n }\n yield {\n type: \"server_tool_call_result\",\n toolCallId: _isString(toolOutput.id) ? toolOutput.id : \"\",\n status:\n toolOutput.status === \"completed\"\n ? \"success\"\n : toolOutput.status === \"failed\"\n ? \"error\"\n : \"success\",\n output: {\n tools: _isArray(toolOutput.tools) ? toolOutput.tools : [],\n },\n extras: toolSearchOutputExtras,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"image_generation_call\")) {\n // Convert image_generation_call to proper image content block if result is available\n if (_isString(toolOutput.result)) {\n yield {\n type: \"image\",\n mimeType: \"image/png\",\n data: toolOutput.result,\n id: _isString(toolOutput.id) ? toolOutput.id : undefined,\n metadata: {\n status: _isString(toolOutput.status)\n ? toolOutput.status\n : undefined,\n },\n };\n }\n // Also yield as non_standard for backwards compatibility\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n }\n if (_isObject(toolOutput)) {\n yield { type: \"non_standard\", value: toolOutput };\n }\n }\n }\n }\n return Array.from(iterateContent());\n}\n\n/**\n * Converts a ChatOpenAIResponses message chunk to an array of v1 standard content blocks.\n *\n * This function processes an AI message chunk containing OpenAI-specific content blocks\n * and converts them to the standardized v1 content block format. It handles both the\n * regular message content and tool call chunks that are specific to streaming responses.\n *\n * @param message - The AI message chunk containing OpenAI-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const messageChunk = new AIMessageChunk({\n * content: [{ type: \"text\", text: \"Hello\" }],\n * tool_call_chunks: [\n * { id: \"call_123\", name: \"calculator\", args: '{\"a\": 1' }\n * ]\n * });\n *\n * const standardBlocks = convertToV1FromResponsesChunk(messageChunk);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello\" },\n * // { type: \"tool_call_chunk\", id: \"call_123\", name: \"calculator\", args: '{\"a\": 1' }\n * // ]\n * ```\n */\nexport function convertToV1FromResponsesChunk(\n message: AIMessageChunk\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n yield* convertToV1FromResponses(message);\n for (const toolCallChunk of message.tool_call_chunks ?? []) {\n yield {\n type: \"tool_call_chunk\",\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n args: toolCallChunk.args,\n };\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatOpenAITranslator: StandardContentBlockTranslator = {\n translateContent: (message) => {\n if (typeof message.content === \"string\") {\n return convertToV1FromChatCompletions(message);\n }\n return convertToV1FromResponses(message);\n },\n translateContentChunk: (message) => {\n if (typeof message.content === \"string\") {\n return convertToV1FromChatCompletionsChunk(message);\n }\n return convertToV1FromResponsesChunk(message);\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,+BACd,SAC8B;CAC9B,MAAM,SAAuC,EAAE;AAC/C,KAAI,OAAO,QAAQ,YAAY;MAEzB,QAAQ,QAAQ,SAAS,EAC3B,QAAO,KAAK;GACV,MAAM;GACN,MAAM,QAAQ;GACf,CAAC;OAGJ,QAAO,KAAK,GAAG,oCAAoC,QAAQ,QAAQ,CAAC;AAEtE,MAAK,MAAM,YAAY,QAAQ,cAAc,EAAE,CAC7C,QAAO,KAAK;EACV,MAAM;EACN,IAAI,SAAS;EACb,MAAM,SAAS;EACf,MAAM,SAAS;EAChB,CAAC;AAEJ,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCT,SAAgB,oCACd,SAC8B;CAC9B,MAAM,SAAuC,EAAE;AAC/C,KAAI,OAAO,QAAQ,YAAY;MAEzB,QAAQ,QAAQ,SAAS,EAC3B,QAAO,KAAK;GACV,MAAM;GACN,MAAM,QAAQ;GACf,CAAC;OAGJ,QAAO,KAAK,GAAG,oCAAoC,QAAQ,QAAQ,CAAC;AAItE,MAAK,MAAM,YAAY,QAAQ,cAAc,EAAE,CAC7C,QAAO,KAAK;EACV,MAAM;EACN,IAAI,SAAS;EACb,MAAM,SAAS;EACf,MAAM,SAAS;EAChB,CAAC;AAEJ,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BT,SAAgB,oCACd,QAC8B;CAC9B,MAAM,kBAAgD,EAAE;AACxD,MAAK,MAAM,SAAS,OAClB,KAAI,kBAAkB,MAAM,CAC1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AAE,MAAM;IAAgB,OAAO;IAAY;;;AAKzD,QAAO,MAAM,KAAK,gBAAgB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BrC,SAAgB,8BACd,SAC8B;CAC9B,UAAU,iBAAkD;AAC1D,SAAO,yBAAyB,QAAQ;AACxC,OAAK,MAAM,iBAAiB,QAAQ,oBAAoB,EAAE,CACxD,OAAM;GACJ,MAAM;GACN,IAAI,cAAc;GAClB,MAAM,cAAc;GACpB,MAAM,cAAc;GACrB;;AAGL,QAAO,MAAM,KAAK,gBAAgB,CAAC;;AAGrC,MAAa,uBAAuD;CAClE,mBAAmB,YAAY;AAC7B,MAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,+BAA+B,QAAQ;AAEhD,SAAO,yBAAyB,QAAQ;;CAE1C,wBAAwB,YAAY;AAClC,MAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,oCAAoC,QAAQ;AAErD,SAAO,8BAA8B,QAAQ;;CAEhD"}
@@ -14,20 +14,44 @@ var json_schema_exports = /* @__PURE__ */ require_runtime.__exportAll({
14
14
  validatesOnlyStrings: () => validatesOnlyStrings
15
15
  });
16
16
  /**
17
+ * WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.
18
+ *
19
+ * Keyed on the schema object reference. Since Zod schemas are immutable and
20
+ * the same `tool.schema` reference is passed on every LLM call, this
21
+ * eliminates redundant serializations. For example, an agent with 6 tools
22
+ * doing 15 steps across 3 parallel subagents would otherwise run 270
23
+ * identical conversions per invocation.
24
+ *
25
+ * Only used when no custom `params` are passed (the common case for tool
26
+ * binding). WeakMap ensures cached entries are GC'd when the schema goes
27
+ * out of scope.
28
+ *
29
+ * @internal
30
+ */
31
+ const _jsonSchemaCache = /* @__PURE__ */ new WeakMap();
32
+ /**
17
33
  * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
34
+ * Results are cached by schema reference when no custom params are passed.
18
35
  * @param schema - The schema to convert.
19
36
  * @param params - The parameters to pass to the toJSONSchema function.
20
37
  * @returns The converted schema.
21
38
  */
22
39
  function toJsonSchema(schema, params) {
23
- if (require_utils_standard_schema.isStandardJsonSchema(schema) && !require_zod.isZodSchemaV4(schema)) return schema["~standard"].jsonSchema.input({ target: "draft-07" });
24
- if (require_zod.isZodSchemaV4(schema)) {
25
- const inputSchema = require_zod.interopZodTransformInputSchema(schema, true);
26
- if (require_zod.isZodObjectV4(inputSchema)) return (0, zod_v4_core.toJSONSchema)(require_zod.interopZodObjectStrict(inputSchema, true), params);
27
- else return (0, zod_v4_core.toJSONSchema)(schema, params);
40
+ const canCache = !params && schema != null && typeof schema === "object";
41
+ if (canCache) {
42
+ const cached = _jsonSchemaCache.get(schema);
43
+ if (cached) return cached;
28
44
  }
29
- if (require_zod.isZodSchemaV3(schema)) return require_zodToJsonSchema.zodToJsonSchema(schema);
30
- return schema;
45
+ let result;
46
+ if (require_utils_standard_schema.isStandardJsonSchema(schema) && !require_zod.isZodSchemaV4(schema)) result = schema["~standard"].jsonSchema.input({ target: "draft-07" });
47
+ else if (require_zod.isZodSchemaV4(schema)) {
48
+ const inputSchema = require_zod.interopZodTransformInputSchema(schema, true);
49
+ if (require_zod.isZodObjectV4(inputSchema)) result = (0, zod_v4_core.toJSONSchema)(require_zod.interopZodObjectStrict(inputSchema, true), params);
50
+ else result = (0, zod_v4_core.toJSONSchema)(schema, params);
51
+ } else if (require_zod.isZodSchemaV3(schema)) result = require_zodToJsonSchema.zodToJsonSchema(schema);
52
+ else result = schema;
53
+ if (canCache && result != null && typeof result === "object") _jsonSchemaCache.set(schema, result);
54
+ return result;
31
55
  }
32
56
  /**
33
57
  * Validates if a JSON schema validates only strings. May return false negatives in some edge cases
@@ -1 +1 @@
1
- {"version":3,"file":"json_schema.cjs","names":["isStandardJsonSchema","isZodSchemaV4","interopZodTransformInputSchema","isZodObjectV4","interopZodObjectStrict","isZodSchemaV3","zodToJsonSchema"],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-07\",\n }) as JSONSchema;\n }\n if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(inputSchema, true);\n return toJSONSchema(strictSchema as unknown as $ZodType, params);\n } else {\n return toJSONSchema(schema as unknown as $ZodType, params);\n }\n }\n if (isZodSchemaV3(schema)) {\n return zodToJsonSchema(schema as never);\n }\n return schema as JSONSchema;\n}\n\n/**\n * Validates if a JSON schema validates only strings. May return false negatives in some edge cases\n * (like recursive or unresolvable refs).\n *\n * @param schema - The schema to validate.\n * @returns `true` if the schema validates only strings, `false` otherwise.\n */\nexport function validatesOnlyStrings(schema: unknown): boolean {\n // Null, undefined, or empty schema\n if (\n !schema ||\n typeof schema !== \"object\" ||\n Object.keys(schema).length === 0 ||\n Array.isArray(schema)\n ) {\n return false; // Validates anything, not just strings\n }\n\n // Explicit type constraint\n if (\"type\" in schema) {\n if (typeof schema.type === \"string\") {\n return schema.type === \"string\";\n }\n\n if (Array.isArray(schema.type)) {\n // not sure why someone would do `\"type\": [\"string\"]` or especially `\"type\": [\"string\",\n // \"string\", \"string\", ...]` but we're not here to judge\n return schema.type.every((t) => t === \"string\");\n }\n return false; // Invalid or non-string type\n }\n\n // Enum with only string values\n if (\"enum\" in schema) {\n return (\n Array.isArray(schema.enum) &&\n schema.enum.length > 0 &&\n schema.enum.every((val) => typeof val === \"string\")\n );\n }\n\n // String constant\n if (\"const\" in schema) {\n return typeof schema.const === \"string\";\n }\n\n // Schema combinations\n if (\"allOf\" in schema && Array.isArray(schema.allOf)) {\n // If any subschema validates only strings, then the overall schema validates only strings\n return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));\n }\n\n if (\n (\"anyOf\" in schema && Array.isArray(schema.anyOf)) ||\n (\"oneOf\" in schema && Array.isArray(schema.oneOf))\n ) {\n const subschemas = (\n \"anyOf\" in schema ? schema.anyOf : schema.oneOf\n ) as unknown[];\n\n // All subschemas must validate only strings\n return (\n subschemas.length > 0 &&\n subschemas.every((subschema) => validatesOnlyStrings(subschema))\n );\n }\n\n // We're not going to try on this one, it's too complex - we just assume if it has a \"not\" key and hasn't matched one of the above checks, it's not a string schema.\n if (\"not\" in schema) {\n return false; // The not case can validate non-strings\n }\n\n if (\"$ref\" in schema && typeof schema.$ref === \"string\") {\n const ref = schema.$ref as string;\n const resolved = dereference(schema as Schema);\n if (resolved[ref]) {\n return validatesOnlyStrings(resolved[ref]);\n }\n return false;\n }\n\n // ignore recursive refs and other cases where type is omitted for now\n // ignore other cases for now where type is omitted\n\n return false;\n}\n\n// Re-export of the types used throughout langchain for json schema serialization.\n// The plan is to eventually nix zod-to-json-schema altogether in place for\n// zod v4 / a more standardized way of serializing validated inputs, so its re-exported\n// here to remove the dependency on zod-to-json-schema in downstream packages until\n// a determination is made.\n\nexport {\n type JsonSchema7Type,\n type JsonSchema7Type as JSONSchema,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"./zod-to-json-schema/index.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,aACd,QACA,QACY;AACZ,KAAIA,8BAAAA,qBAAqB,OAAO,IAAI,CAACC,YAAAA,cAAc,OAAO,CACxD,QAAO,OAAO,aAAa,WAAW,MAAM,EAC1C,QAAQ,YACT,CAAC;AAEJ,KAAIA,YAAAA,cAAc,OAAO,EAAE;EACzB,MAAM,cAAcC,YAAAA,+BAA+B,QAAQ,KAAK;AAChE,MAAIC,YAAAA,cAAc,YAAY,CAE5B,SAAA,GAAA,YAAA,cADqBC,YAAAA,uBAAuB,aAAa,KAAK,EACL,OAAO;MAEhE,SAAA,GAAA,YAAA,cAAoB,QAA+B,OAAO;;AAG9D,KAAIC,YAAAA,cAAc,OAAO,CACvB,QAAOC,wBAAAA,gBAAgB,OAAgB;AAEzC,QAAO;;;;;;;;;AAUT,SAAgB,qBAAqB,QAA0B;AAE7D,KACE,CAAC,UACD,OAAO,WAAW,YAClB,OAAO,KAAK,OAAO,CAAC,WAAW,KAC/B,MAAM,QAAQ,OAAO,CAErB,QAAO;AAIT,KAAI,UAAU,QAAQ;AACpB,MAAI,OAAO,OAAO,SAAS,SACzB,QAAO,OAAO,SAAS;AAGzB,MAAI,MAAM,QAAQ,OAAO,KAAK,CAG5B,QAAO,OAAO,KAAK,OAAO,MAAM,MAAM,SAAS;AAEjD,SAAO;;AAIT,KAAI,UAAU,OACZ,QACE,MAAM,QAAQ,OAAO,KAAK,IAC1B,OAAO,KAAK,SAAS,KACrB,OAAO,KAAK,OAAO,QAAQ,OAAO,QAAQ,SAAS;AAKvD,KAAI,WAAW,OACb,QAAO,OAAO,OAAO,UAAU;AAIjC,KAAI,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,CAElD,QAAO,OAAO,MAAM,MAAM,cAAc,qBAAqB,UAAU,CAAC;AAG1E,KACG,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,IAChD,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,EACjD;EACA,MAAM,aACJ,WAAW,SAAS,OAAO,QAAQ,OAAO;AAI5C,SACE,WAAW,SAAS,KACpB,WAAW,OAAO,cAAc,qBAAqB,UAAU,CAAC;;AAKpE,KAAI,SAAS,OACX,QAAO;AAGT,KAAI,UAAU,UAAU,OAAO,OAAO,SAAS,UAAU;EACvD,MAAM,MAAM,OAAO;EACnB,MAAM,YAAA,GAAA,sBAAA,aAAuB,OAAiB;AAC9C,MAAI,SAAS,KACX,QAAO,qBAAqB,SAAS,KAAK;AAE5C,SAAO;;AAMT,QAAO"}
1
+ {"version":3,"file":"json_schema.cjs","names":["isStandardJsonSchema","isZodSchemaV4","interopZodTransformInputSchema","isZodObjectV4","interopZodObjectStrict","isZodSchemaV3","zodToJsonSchema"],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.\n *\n * Keyed on the schema object reference. Since Zod schemas are immutable and\n * the same `tool.schema` reference is passed on every LLM call, this\n * eliminates redundant serializations. For example, an agent with 6 tools\n * doing 15 steps across 3 parallel subagents would otherwise run 270\n * identical conversions per invocation.\n *\n * Only used when no custom `params` are passed (the common case for tool\n * binding). WeakMap ensures cached entries are GC'd when the schema goes\n * out of scope.\n *\n * @internal\n */\nconst _jsonSchemaCache = new WeakMap<object, JSONSchema>();\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * Results are cached by schema reference when no custom params are passed.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n // Cache when no custom params are passed (the default for tool binding).\n // Params can change the output (e.g., different target draft), so we\n // bypass the cache when they're provided.\n const canCache = !params && schema != null && typeof schema === \"object\";\n\n if (canCache) {\n const cached = _jsonSchemaCache.get(schema);\n if (cached) return cached;\n }\n\n let result: JSONSchema;\n\n if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n result = schema[\"~standard\"].jsonSchema.input({\n target: \"draft-07\",\n }) as JSONSchema;\n } else if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(inputSchema, true);\n result = toJSONSchema(strictSchema as unknown as $ZodType, params);\n } else {\n result = toJSONSchema(schema as unknown as $ZodType, params);\n }\n } else if (isZodSchemaV3(schema)) {\n result = zodToJsonSchema(schema as never);\n } else {\n result = schema as JSONSchema;\n }\n\n if (canCache && result != null && typeof result === \"object\") {\n _jsonSchemaCache.set(schema, result);\n }\n\n return result;\n}\n\n/**\n * Validates if a JSON schema validates only strings. May return false negatives in some edge cases\n * (like recursive or unresolvable refs).\n *\n * @param schema - The schema to validate.\n * @returns `true` if the schema validates only strings, `false` otherwise.\n */\nexport function validatesOnlyStrings(schema: unknown): boolean {\n // Null, undefined, or empty schema\n if (\n !schema ||\n typeof schema !== \"object\" ||\n Object.keys(schema).length === 0 ||\n Array.isArray(schema)\n ) {\n return false; // Validates anything, not just strings\n }\n\n // Explicit type constraint\n if (\"type\" in schema) {\n if (typeof schema.type === \"string\") {\n return schema.type === \"string\";\n }\n\n if (Array.isArray(schema.type)) {\n // not sure why someone would do `\"type\": [\"string\"]` or especially `\"type\": [\"string\",\n // \"string\", \"string\", ...]` but we're not here to judge\n return schema.type.every((t) => t === \"string\");\n }\n return false; // Invalid or non-string type\n }\n\n // Enum with only string values\n if (\"enum\" in schema) {\n return (\n Array.isArray(schema.enum) &&\n schema.enum.length > 0 &&\n schema.enum.every((val) => typeof val === \"string\")\n );\n }\n\n // String constant\n if (\"const\" in schema) {\n return typeof schema.const === \"string\";\n }\n\n // Schema combinations\n if (\"allOf\" in schema && Array.isArray(schema.allOf)) {\n // If any subschema validates only strings, then the overall schema validates only strings\n return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));\n }\n\n if (\n (\"anyOf\" in schema && Array.isArray(schema.anyOf)) ||\n (\"oneOf\" in schema && Array.isArray(schema.oneOf))\n ) {\n const subschemas = (\n \"anyOf\" in schema ? schema.anyOf : schema.oneOf\n ) as unknown[];\n\n // All subschemas must validate only strings\n return (\n subschemas.length > 0 &&\n subschemas.every((subschema) => validatesOnlyStrings(subschema))\n );\n }\n\n // We're not going to try on this one, it's too complex - we just assume if it has a \"not\" key and hasn't matched one of the above checks, it's not a string schema.\n if (\"not\" in schema) {\n return false; // The not case can validate non-strings\n }\n\n if (\"$ref\" in schema && typeof schema.$ref === \"string\") {\n const ref = schema.$ref as string;\n const resolved = dereference(schema as Schema);\n if (resolved[ref]) {\n return validatesOnlyStrings(resolved[ref]);\n }\n return false;\n }\n\n // ignore recursive refs and other cases where type is omitted for now\n // ignore other cases for now where type is omitted\n\n return false;\n}\n\n// Re-export of the types used throughout langchain for json schema serialization.\n// The plan is to eventually nix zod-to-json-schema altogether in place for\n// zod v4 / a more standardized way of serializing validated inputs, so its re-exported\n// here to remove the dependency on zod-to-json-schema in downstream packages until\n// a determination is made.\n\nexport {\n type JsonSchema7Type,\n type JsonSchema7Type as JSONSchema,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"./zod-to-json-schema/index.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,MAAM,mCAAmB,IAAI,SAA6B;;;;;;;;AAS1D,SAAgB,aACd,QACA,QACY;CAIZ,MAAM,WAAW,CAAC,UAAU,UAAU,QAAQ,OAAO,WAAW;AAEhE,KAAI,UAAU;EACZ,MAAM,SAAS,iBAAiB,IAAI,OAAO;AAC3C,MAAI,OAAQ,QAAO;;CAGrB,IAAI;AAEJ,KAAIA,8BAAAA,qBAAqB,OAAO,IAAI,CAACC,YAAAA,cAAc,OAAO,CACxD,UAAS,OAAO,aAAa,WAAW,MAAM,EAC5C,QAAQ,YACT,CAAC;UACOA,YAAAA,cAAc,OAAO,EAAE;EAChC,MAAM,cAAcC,YAAAA,+BAA+B,QAAQ,KAAK;AAChE,MAAIC,YAAAA,cAAc,YAAY,CAE5B,WAAA,GAAA,YAAA,cADqBC,YAAAA,uBAAuB,aAAa,KAAK,EACH,OAAO;MAElE,WAAA,GAAA,YAAA,cAAsB,QAA+B,OAAO;YAErDC,YAAAA,cAAc,OAAO,CAC9B,UAASC,wBAAAA,gBAAgB,OAAgB;KAEzC,UAAS;AAGX,KAAI,YAAY,UAAU,QAAQ,OAAO,WAAW,SAClD,kBAAiB,IAAI,QAAQ,OAAO;AAGtC,QAAO;;;;;;;;;AAUT,SAAgB,qBAAqB,QAA0B;AAE7D,KACE,CAAC,UACD,OAAO,WAAW,YAClB,OAAO,KAAK,OAAO,CAAC,WAAW,KAC/B,MAAM,QAAQ,OAAO,CAErB,QAAO;AAIT,KAAI,UAAU,QAAQ;AACpB,MAAI,OAAO,OAAO,SAAS,SACzB,QAAO,OAAO,SAAS;AAGzB,MAAI,MAAM,QAAQ,OAAO,KAAK,CAG5B,QAAO,OAAO,KAAK,OAAO,MAAM,MAAM,SAAS;AAEjD,SAAO;;AAIT,KAAI,UAAU,OACZ,QACE,MAAM,QAAQ,OAAO,KAAK,IAC1B,OAAO,KAAK,SAAS,KACrB,OAAO,KAAK,OAAO,QAAQ,OAAO,QAAQ,SAAS;AAKvD,KAAI,WAAW,OACb,QAAO,OAAO,OAAO,UAAU;AAIjC,KAAI,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,CAElD,QAAO,OAAO,MAAM,MAAM,cAAc,qBAAqB,UAAU,CAAC;AAG1E,KACG,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,IAChD,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,EACjD;EACA,MAAM,aACJ,WAAW,SAAS,OAAO,QAAQ,OAAO;AAI5C,SACE,WAAW,SAAS,KACpB,WAAW,OAAO,cAAc,qBAAqB,UAAU,CAAC;;AAKpE,KAAI,SAAS,OACX,QAAO;AAGT,KAAI,UAAU,UAAU,OAAO,OAAO,SAAS,UAAU;EACvD,MAAM,MAAM,OAAO;EACnB,MAAM,YAAA,GAAA,sBAAA,aAAuB,OAAiB;AAC9C,MAAI,SAAS,KACX,QAAO,qBAAqB,SAAS,KAAK;AAE5C,SAAO;;AAMT,QAAO"}
@@ -13,6 +13,7 @@ import { Validator, deepCompareStrict } from "@cfworker/json-schema";
13
13
  type ToJSONSchemaParams = NonNullable<Parameters<typeof toJSONSchema>[1]>;
14
14
  /**
15
15
  * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
16
+ * Results are cached by schema reference when no custom params are passed.
16
17
  * @param schema - The schema to convert.
17
18
  * @param params - The parameters to pass to the toJSONSchema function.
18
19
  * @returns The converted schema.
@@ -1 +1 @@
1
- {"version":3,"file":"json_schema.d.cts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAmBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;iBASJ,YAAA,CACd,MAAA,EAAQ,oBAAA,GAAuB,cAAA,GAAiB,eAAA,EAChD,MAAA,GAAS,kBAAA,GACR,eAAA;;;;;;;;iBA4Ba,oBAAA,CAAqB,MAAA"}
1
+ {"version":3,"file":"json_schema.d.cts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAmBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;AADpB;iBA4BgB,YAAA,CACd,MAAA,EAAQ,oBAAA,GAAuB,cAAA,GAAiB,eAAA,EAChD,MAAA,GAAS,kBAAA,GACR,eAAA;;;;;;;;iBA6Ca,oBAAA,CAAqB,MAAA"}
@@ -13,6 +13,7 @@ import { StandardJSONSchemaV1 } from "@standard-schema/spec";
13
13
  type ToJSONSchemaParams = NonNullable<Parameters<typeof toJSONSchema>[1]>;
14
14
  /**
15
15
  * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
16
+ * Results are cached by schema reference when no custom params are passed.
16
17
  * @param schema - The schema to convert.
17
18
  * @param params - The parameters to pass to the toJSONSchema function.
18
19
  * @returns The converted schema.
@@ -1 +1 @@
1
- {"version":3,"file":"json_schema.d.ts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAmBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;iBASJ,YAAA,CACd,MAAA,EAAQ,oBAAA,GAAuB,cAAA,GAAiB,eAAA,EAChD,MAAA,GAAS,kBAAA,GACR,eAAA;AAbH;;;;;;;AAAA,iBAyCgB,oBAAA,CAAqB,MAAA"}
1
+ {"version":3,"file":"json_schema.d.ts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAmBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;;iBA2BJ,YAAA,CACd,MAAA,EAAQ,oBAAA,GAAuB,cAAA,GAAiB,eAAA,EAChD,MAAA,GAAS,kBAAA,GACR,eAAA;;;;;;;;iBA6Ca,oBAAA,CAAqB,MAAA"}
@@ -13,20 +13,44 @@ var json_schema_exports = /* @__PURE__ */ __exportAll({
13
13
  validatesOnlyStrings: () => validatesOnlyStrings
14
14
  });
15
15
  /**
16
+ * WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.
17
+ *
18
+ * Keyed on the schema object reference. Since Zod schemas are immutable and
19
+ * the same `tool.schema` reference is passed on every LLM call, this
20
+ * eliminates redundant serializations. For example, an agent with 6 tools
21
+ * doing 15 steps across 3 parallel subagents would otherwise run 270
22
+ * identical conversions per invocation.
23
+ *
24
+ * Only used when no custom `params` are passed (the common case for tool
25
+ * binding). WeakMap ensures cached entries are GC'd when the schema goes
26
+ * out of scope.
27
+ *
28
+ * @internal
29
+ */
30
+ const _jsonSchemaCache = /* @__PURE__ */ new WeakMap();
31
+ /**
16
32
  * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
33
+ * Results are cached by schema reference when no custom params are passed.
17
34
  * @param schema - The schema to convert.
18
35
  * @param params - The parameters to pass to the toJSONSchema function.
19
36
  * @returns The converted schema.
20
37
  */
21
38
  function toJsonSchema(schema, params) {
22
- if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) return schema["~standard"].jsonSchema.input({ target: "draft-07" });
23
- if (isZodSchemaV4(schema)) {
24
- const inputSchema = interopZodTransformInputSchema(schema, true);
25
- if (isZodObjectV4(inputSchema)) return toJSONSchema(interopZodObjectStrict(inputSchema, true), params);
26
- else return toJSONSchema(schema, params);
39
+ const canCache = !params && schema != null && typeof schema === "object";
40
+ if (canCache) {
41
+ const cached = _jsonSchemaCache.get(schema);
42
+ if (cached) return cached;
27
43
  }
28
- if (isZodSchemaV3(schema)) return zodToJsonSchema(schema);
29
- return schema;
44
+ let result;
45
+ if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) result = schema["~standard"].jsonSchema.input({ target: "draft-07" });
46
+ else if (isZodSchemaV4(schema)) {
47
+ const inputSchema = interopZodTransformInputSchema(schema, true);
48
+ if (isZodObjectV4(inputSchema)) result = toJSONSchema(interopZodObjectStrict(inputSchema, true), params);
49
+ else result = toJSONSchema(schema, params);
50
+ } else if (isZodSchemaV3(schema)) result = zodToJsonSchema(schema);
51
+ else result = schema;
52
+ if (canCache && result != null && typeof result === "object") _jsonSchemaCache.set(schema, result);
53
+ return result;
30
54
  }
31
55
  /**
32
56
  * Validates if a JSON schema validates only strings. May return false negatives in some edge cases
@@ -1 +1 @@
1
- {"version":3,"file":"json_schema.js","names":[],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-07\",\n }) as JSONSchema;\n }\n if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(inputSchema, true);\n return toJSONSchema(strictSchema as unknown as $ZodType, params);\n } else {\n return toJSONSchema(schema as unknown as $ZodType, params);\n }\n }\n if (isZodSchemaV3(schema)) {\n return zodToJsonSchema(schema as never);\n }\n return schema as JSONSchema;\n}\n\n/**\n * Validates if a JSON schema validates only strings. May return false negatives in some edge cases\n * (like recursive or unresolvable refs).\n *\n * @param schema - The schema to validate.\n * @returns `true` if the schema validates only strings, `false` otherwise.\n */\nexport function validatesOnlyStrings(schema: unknown): boolean {\n // Null, undefined, or empty schema\n if (\n !schema ||\n typeof schema !== \"object\" ||\n Object.keys(schema).length === 0 ||\n Array.isArray(schema)\n ) {\n return false; // Validates anything, not just strings\n }\n\n // Explicit type constraint\n if (\"type\" in schema) {\n if (typeof schema.type === \"string\") {\n return schema.type === \"string\";\n }\n\n if (Array.isArray(schema.type)) {\n // not sure why someone would do `\"type\": [\"string\"]` or especially `\"type\": [\"string\",\n // \"string\", \"string\", ...]` but we're not here to judge\n return schema.type.every((t) => t === \"string\");\n }\n return false; // Invalid or non-string type\n }\n\n // Enum with only string values\n if (\"enum\" in schema) {\n return (\n Array.isArray(schema.enum) &&\n schema.enum.length > 0 &&\n schema.enum.every((val) => typeof val === \"string\")\n );\n }\n\n // String constant\n if (\"const\" in schema) {\n return typeof schema.const === \"string\";\n }\n\n // Schema combinations\n if (\"allOf\" in schema && Array.isArray(schema.allOf)) {\n // If any subschema validates only strings, then the overall schema validates only strings\n return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));\n }\n\n if (\n (\"anyOf\" in schema && Array.isArray(schema.anyOf)) ||\n (\"oneOf\" in schema && Array.isArray(schema.oneOf))\n ) {\n const subschemas = (\n \"anyOf\" in schema ? schema.anyOf : schema.oneOf\n ) as unknown[];\n\n // All subschemas must validate only strings\n return (\n subschemas.length > 0 &&\n subschemas.every((subschema) => validatesOnlyStrings(subschema))\n );\n }\n\n // We're not going to try on this one, it's too complex - we just assume if it has a \"not\" key and hasn't matched one of the above checks, it's not a string schema.\n if (\"not\" in schema) {\n return false; // The not case can validate non-strings\n }\n\n if (\"$ref\" in schema && typeof schema.$ref === \"string\") {\n const ref = schema.$ref as string;\n const resolved = dereference(schema as Schema);\n if (resolved[ref]) {\n return validatesOnlyStrings(resolved[ref]);\n }\n return false;\n }\n\n // ignore recursive refs and other cases where type is omitted for now\n // ignore other cases for now where type is omitted\n\n return false;\n}\n\n// Re-export of the types used throughout langchain for json schema serialization.\n// The plan is to eventually nix zod-to-json-schema altogether in place for\n// zod v4 / a more standardized way of serializing validated inputs, so its re-exported\n// here to remove the dependency on zod-to-json-schema in downstream packages until\n// a determination is made.\n\nexport {\n type JsonSchema7Type,\n type JsonSchema7Type as JSONSchema,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"./zod-to-json-schema/index.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,aACd,QACA,QACY;AACZ,KAAI,qBAAqB,OAAO,IAAI,CAAC,cAAc,OAAO,CACxD,QAAO,OAAO,aAAa,WAAW,MAAM,EAC1C,QAAQ,YACT,CAAC;AAEJ,KAAI,cAAc,OAAO,EAAE;EACzB,MAAM,cAAc,+BAA+B,QAAQ,KAAK;AAChE,MAAI,cAAc,YAAY,CAE5B,QAAO,aADc,uBAAuB,aAAa,KAAK,EACL,OAAO;MAEhE,QAAO,aAAa,QAA+B,OAAO;;AAG9D,KAAI,cAAc,OAAO,CACvB,QAAO,gBAAgB,OAAgB;AAEzC,QAAO;;;;;;;;;AAUT,SAAgB,qBAAqB,QAA0B;AAE7D,KACE,CAAC,UACD,OAAO,WAAW,YAClB,OAAO,KAAK,OAAO,CAAC,WAAW,KAC/B,MAAM,QAAQ,OAAO,CAErB,QAAO;AAIT,KAAI,UAAU,QAAQ;AACpB,MAAI,OAAO,OAAO,SAAS,SACzB,QAAO,OAAO,SAAS;AAGzB,MAAI,MAAM,QAAQ,OAAO,KAAK,CAG5B,QAAO,OAAO,KAAK,OAAO,MAAM,MAAM,SAAS;AAEjD,SAAO;;AAIT,KAAI,UAAU,OACZ,QACE,MAAM,QAAQ,OAAO,KAAK,IAC1B,OAAO,KAAK,SAAS,KACrB,OAAO,KAAK,OAAO,QAAQ,OAAO,QAAQ,SAAS;AAKvD,KAAI,WAAW,OACb,QAAO,OAAO,OAAO,UAAU;AAIjC,KAAI,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,CAElD,QAAO,OAAO,MAAM,MAAM,cAAc,qBAAqB,UAAU,CAAC;AAG1E,KACG,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,IAChD,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,EACjD;EACA,MAAM,aACJ,WAAW,SAAS,OAAO,QAAQ,OAAO;AAI5C,SACE,WAAW,SAAS,KACpB,WAAW,OAAO,cAAc,qBAAqB,UAAU,CAAC;;AAKpE,KAAI,SAAS,OACX,QAAO;AAGT,KAAI,UAAU,UAAU,OAAO,OAAO,SAAS,UAAU;EACvD,MAAM,MAAM,OAAO;EACnB,MAAM,WAAW,YAAY,OAAiB;AAC9C,MAAI,SAAS,KACX,QAAO,qBAAqB,SAAS,KAAK;AAE5C,SAAO;;AAMT,QAAO"}
1
+ {"version":3,"file":"json_schema.js","names":[],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.\n *\n * Keyed on the schema object reference. Since Zod schemas are immutable and\n * the same `tool.schema` reference is passed on every LLM call, this\n * eliminates redundant serializations. For example, an agent with 6 tools\n * doing 15 steps across 3 parallel subagents would otherwise run 270\n * identical conversions per invocation.\n *\n * Only used when no custom `params` are passed (the common case for tool\n * binding). WeakMap ensures cached entries are GC'd when the schema goes\n * out of scope.\n *\n * @internal\n */\nconst _jsonSchemaCache = new WeakMap<object, JSONSchema>();\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * Results are cached by schema reference when no custom params are passed.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n // Cache when no custom params are passed (the default for tool binding).\n // Params can change the output (e.g., different target draft), so we\n // bypass the cache when they're provided.\n const canCache = !params && schema != null && typeof schema === \"object\";\n\n if (canCache) {\n const cached = _jsonSchemaCache.get(schema);\n if (cached) return cached;\n }\n\n let result: JSONSchema;\n\n if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n result = schema[\"~standard\"].jsonSchema.input({\n target: \"draft-07\",\n }) as JSONSchema;\n } else if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(inputSchema, true);\n result = toJSONSchema(strictSchema as unknown as $ZodType, params);\n } else {\n result = toJSONSchema(schema as unknown as $ZodType, params);\n }\n } else if (isZodSchemaV3(schema)) {\n result = zodToJsonSchema(schema as never);\n } else {\n result = schema as JSONSchema;\n }\n\n if (canCache && result != null && typeof result === \"object\") {\n _jsonSchemaCache.set(schema, result);\n }\n\n return result;\n}\n\n/**\n * Validates if a JSON schema validates only strings. May return false negatives in some edge cases\n * (like recursive or unresolvable refs).\n *\n * @param schema - The schema to validate.\n * @returns `true` if the schema validates only strings, `false` otherwise.\n */\nexport function validatesOnlyStrings(schema: unknown): boolean {\n // Null, undefined, or empty schema\n if (\n !schema ||\n typeof schema !== \"object\" ||\n Object.keys(schema).length === 0 ||\n Array.isArray(schema)\n ) {\n return false; // Validates anything, not just strings\n }\n\n // Explicit type constraint\n if (\"type\" in schema) {\n if (typeof schema.type === \"string\") {\n return schema.type === \"string\";\n }\n\n if (Array.isArray(schema.type)) {\n // not sure why someone would do `\"type\": [\"string\"]` or especially `\"type\": [\"string\",\n // \"string\", \"string\", ...]` but we're not here to judge\n return schema.type.every((t) => t === \"string\");\n }\n return false; // Invalid or non-string type\n }\n\n // Enum with only string values\n if (\"enum\" in schema) {\n return (\n Array.isArray(schema.enum) &&\n schema.enum.length > 0 &&\n schema.enum.every((val) => typeof val === \"string\")\n );\n }\n\n // String constant\n if (\"const\" in schema) {\n return typeof schema.const === \"string\";\n }\n\n // Schema combinations\n if (\"allOf\" in schema && Array.isArray(schema.allOf)) {\n // If any subschema validates only strings, then the overall schema validates only strings\n return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));\n }\n\n if (\n (\"anyOf\" in schema && Array.isArray(schema.anyOf)) ||\n (\"oneOf\" in schema && Array.isArray(schema.oneOf))\n ) {\n const subschemas = (\n \"anyOf\" in schema ? schema.anyOf : schema.oneOf\n ) as unknown[];\n\n // All subschemas must validate only strings\n return (\n subschemas.length > 0 &&\n subschemas.every((subschema) => validatesOnlyStrings(subschema))\n );\n }\n\n // We're not going to try on this one, it's too complex - we just assume if it has a \"not\" key and hasn't matched one of the above checks, it's not a string schema.\n if (\"not\" in schema) {\n return false; // The not case can validate non-strings\n }\n\n if (\"$ref\" in schema && typeof schema.$ref === \"string\") {\n const ref = schema.$ref as string;\n const resolved = dereference(schema as Schema);\n if (resolved[ref]) {\n return validatesOnlyStrings(resolved[ref]);\n }\n return false;\n }\n\n // ignore recursive refs and other cases where type is omitted for now\n // ignore other cases for now where type is omitted\n\n return false;\n}\n\n// Re-export of the types used throughout langchain for json schema serialization.\n// The plan is to eventually nix zod-to-json-schema altogether in place for\n// zod v4 / a more standardized way of serializing validated inputs, so its re-exported\n// here to remove the dependency on zod-to-json-schema in downstream packages until\n// a determination is made.\n\nexport {\n type JsonSchema7Type,\n type JsonSchema7Type as JSONSchema,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"./zod-to-json-schema/index.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,MAAM,mCAAmB,IAAI,SAA6B;;;;;;;;AAS1D,SAAgB,aACd,QACA,QACY;CAIZ,MAAM,WAAW,CAAC,UAAU,UAAU,QAAQ,OAAO,WAAW;AAEhE,KAAI,UAAU;EACZ,MAAM,SAAS,iBAAiB,IAAI,OAAO;AAC3C,MAAI,OAAQ,QAAO;;CAGrB,IAAI;AAEJ,KAAI,qBAAqB,OAAO,IAAI,CAAC,cAAc,OAAO,CACxD,UAAS,OAAO,aAAa,WAAW,MAAM,EAC5C,QAAQ,YACT,CAAC;UACO,cAAc,OAAO,EAAE;EAChC,MAAM,cAAc,+BAA+B,QAAQ,KAAK;AAChE,MAAI,cAAc,YAAY,CAE5B,UAAS,aADY,uBAAuB,aAAa,KAAK,EACH,OAAO;MAElE,UAAS,aAAa,QAA+B,OAAO;YAErD,cAAc,OAAO,CAC9B,UAAS,gBAAgB,OAAgB;KAEzC,UAAS;AAGX,KAAI,YAAY,UAAU,QAAQ,OAAO,WAAW,SAClD,kBAAiB,IAAI,QAAQ,OAAO;AAGtC,QAAO;;;;;;;;;AAUT,SAAgB,qBAAqB,QAA0B;AAE7D,KACE,CAAC,UACD,OAAO,WAAW,YAClB,OAAO,KAAK,OAAO,CAAC,WAAW,KAC/B,MAAM,QAAQ,OAAO,CAErB,QAAO;AAIT,KAAI,UAAU,QAAQ;AACpB,MAAI,OAAO,OAAO,SAAS,SACzB,QAAO,OAAO,SAAS;AAGzB,MAAI,MAAM,QAAQ,OAAO,KAAK,CAG5B,QAAO,OAAO,KAAK,OAAO,MAAM,MAAM,SAAS;AAEjD,SAAO;;AAIT,KAAI,UAAU,OACZ,QACE,MAAM,QAAQ,OAAO,KAAK,IAC1B,OAAO,KAAK,SAAS,KACrB,OAAO,KAAK,OAAO,QAAQ,OAAO,QAAQ,SAAS;AAKvD,KAAI,WAAW,OACb,QAAO,OAAO,OAAO,UAAU;AAIjC,KAAI,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,CAElD,QAAO,OAAO,MAAM,MAAM,cAAc,qBAAqB,UAAU,CAAC;AAG1E,KACG,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,IAChD,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,EACjD;EACA,MAAM,aACJ,WAAW,SAAS,OAAO,QAAQ,OAAO;AAI5C,SACE,WAAW,SAAS,KACpB,WAAW,OAAO,cAAc,qBAAqB,UAAU,CAAC;;AAKpE,KAAI,SAAS,OACX,QAAO;AAGT,KAAI,UAAU,UAAU,OAAO,OAAO,SAAS,UAAU;EACvD,MAAM,MAAM,OAAO;EACnB,MAAM,WAAW,YAAY,OAAiB;AAC9C,MAAI,SAAS,KACX,QAAO,qBAAqB,SAAS,KAAK;AAE5C,SAAO;;AAMT,QAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "1.1.36",
3
+ "version": "1.1.38",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {