@ai-sdk-tool/parser 4.1.2 → 4.1.4

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.
@@ -5802,6 +5802,65 @@ async function wrapGenerate({
5802
5802
  };
5803
5803
  }
5804
5804
 
5805
+ // src/core/prompts/shared/input-examples.ts
5806
+ function getToolInputExamples(tool) {
5807
+ const inputExamples = tool.inputExamples;
5808
+ if (!Array.isArray(inputExamples)) {
5809
+ return [];
5810
+ }
5811
+ return inputExamples.filter(
5812
+ (example) => typeof example === "object" && example !== null && "input" in example && example.input !== void 0
5813
+ );
5814
+ }
5815
+ function safeStringifyInputExample(input, sourceError) {
5816
+ try {
5817
+ const serialized = JSON.stringify(input);
5818
+ return serialized != null ? serialized : "null";
5819
+ } catch (stringifyError) {
5820
+ let reason = "";
5821
+ if (sourceError instanceof Error) {
5822
+ reason = sourceError.message;
5823
+ } else if (stringifyError instanceof Error) {
5824
+ reason = stringifyError.message;
5825
+ }
5826
+ return reason.length > 0 ? `[unserializable input: ${reason}]` : "[unserializable input]";
5827
+ }
5828
+ }
5829
+ function stringifyInputExampleAsJsonLiteral(input) {
5830
+ try {
5831
+ const serialized = JSON.stringify(input);
5832
+ return serialized != null ? serialized : "null";
5833
+ } catch (error) {
5834
+ const fallbackText = safeStringifyInputExample(input, error);
5835
+ return JSON.stringify(fallbackText);
5836
+ }
5837
+ }
5838
+ var INPUT_EXAMPLES_SECTION_HEADER = [
5839
+ "# Input Examples",
5840
+ "Treat these as canonical tool-call patterns.",
5841
+ "Reuse the closest structure and nesting, change only values, and do not invent parameters.",
5842
+ "Do not copy example values unless they match the user's request."
5843
+ ];
5844
+ function renderInputExamplesSection(options) {
5845
+ const renderedTools = options.tools.map((tool) => {
5846
+ const inputExamples = getToolInputExamples(tool);
5847
+ if (inputExamples.length === 0) {
5848
+ return "";
5849
+ }
5850
+ const renderedExamples = inputExamples.map((example, index) => {
5851
+ const rendered = options.renderExample(tool.name, example.input);
5852
+ return `Example ${index + 1}:
5853
+ ${rendered}`;
5854
+ }).join("\n\n");
5855
+ return `Tool: ${tool.name}
5856
+ ${renderedExamples}`;
5857
+ }).filter((text) => text.length > 0).join("\n\n");
5858
+ if (renderedTools.length === 0) {
5859
+ return "";
5860
+ }
5861
+ return [...INPUT_EXAMPLES_SECTION_HEADER, renderedTools].join("\n\n");
5862
+ }
5863
+
5805
5864
  // src/core/prompts/shared/text-part.ts
5806
5865
  function toTextPart(text, providerOptions) {
5807
5866
  if (providerOptions === void 0) {
@@ -6042,11 +6101,28 @@ function renderToolDefinition(tool) {
6042
6101
  }
6043
6102
  function hermesSystemPromptTemplate(tools) {
6044
6103
  const toolsRendered = tools.map(renderToolDefinition).join("\n");
6045
- return `You are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. Here are the available tools: <tools> ${toolsRendered} </tools>
6104
+ const basePrompt = `You are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. Here are the available tools: <tools> ${toolsRendered} </tools>
6046
6105
  Use the following pydantic model json schema for each tool call you will make: {"properties": {"name": {"title": "Name", "type": "string"}, "arguments": {"title": "Arguments", "type": "object"}}, "required": ["name", "arguments"], "title": "FunctionCall", "type": "object"}
6047
6106
  For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
6048
6107
  <tool_call>
6049
6108
  {"name": "<function-name>", "arguments": <args-dict>}
6109
+ </tool_call>`;
6110
+ const inputExamplesText = renderInputExamplesSection({
6111
+ tools,
6112
+ renderExample: renderHermesInputExample
6113
+ });
6114
+ if (inputExamplesText.length === 0) {
6115
+ return basePrompt;
6116
+ }
6117
+ return `${basePrompt}
6118
+
6119
+ ${inputExamplesText}`;
6120
+ }
6121
+ function renderHermesInputExample(toolName, input) {
6122
+ const argumentsLiteral = stringifyInputExampleAsJsonLiteral(input);
6123
+ const nameLiteral = JSON.stringify(toolName);
6124
+ return `<tool_call>
6125
+ {"name":${nameLiteral},"arguments":${argumentsLiteral}}
6050
6126
  </tool_call>`;
6051
6127
  }
6052
6128
 
@@ -6105,29 +6181,6 @@ function renderToolForXmlPrompt(tool) {
6105
6181
  lines.push(`schema: ${stringifySchema(normalizedSchema)}`);
6106
6182
  return lines.join("\n");
6107
6183
  }
6108
- function getToolInputExamples(tool) {
6109
- const inputExamples = tool.inputExamples;
6110
- if (!Array.isArray(inputExamples)) {
6111
- return [];
6112
- }
6113
- return inputExamples.filter(
6114
- (example) => typeof example === "object" && example !== null && "input" in example && example.input !== void 0
6115
- );
6116
- }
6117
- function safeStringifyInputExample(input, sourceError) {
6118
- try {
6119
- const serialized = JSON.stringify(input);
6120
- return serialized != null ? serialized : "null";
6121
- } catch (stringifyError) {
6122
- let reason = "";
6123
- if (sourceError instanceof Error) {
6124
- reason = sourceError.message;
6125
- } else if (stringifyError instanceof Error) {
6126
- reason = stringifyError.message;
6127
- }
6128
- return reason.length > 0 ? `[unserializable input: ${reason}]` : "[unserializable input]";
6129
- }
6130
- }
6131
6184
  function renderMorphXmlInputExample(toolName, input) {
6132
6185
  try {
6133
6186
  return stringify(toolName, input, {
@@ -6142,29 +6195,10 @@ function renderMorphXmlInputExample(toolName, input) {
6142
6195
  }
6143
6196
  }
6144
6197
  function renderInputExamplesForXmlPrompt(tools) {
6145
- const renderedTools = tools.map((tool) => {
6146
- const inputExamples = getToolInputExamples(tool);
6147
- if (inputExamples.length === 0) {
6148
- return "";
6149
- }
6150
- const renderedExamples = inputExamples.map((example, index) => {
6151
- const xml = renderMorphXmlInputExample(tool.name, example.input);
6152
- return `Example ${index + 1}:
6153
- ${xml}`;
6154
- }).join("\n\n");
6155
- return `Tool: ${tool.name}
6156
- ${renderedExamples}`;
6157
- }).filter((text) => text.length > 0).join("\n\n");
6158
- if (renderedTools.length === 0) {
6159
- return "";
6160
- }
6161
- return [
6162
- "# Input Examples",
6163
- "Treat these as canonical tool-call patterns.",
6164
- "Reuse the closest structure and nesting, change only values, and do not invent parameters.",
6165
- "Do not copy example values unless they match the user's request.",
6166
- renderedTools
6167
- ].join("\n\n");
6198
+ return renderInputExamplesSection({
6199
+ tools,
6200
+ renderExample: renderMorphXmlInputExample
6201
+ });
6168
6202
  }
6169
6203
  function normalizeSchema(schema) {
6170
6204
  if (typeof schema === "string") {
@@ -6510,8 +6544,50 @@ function qwen3coderSystemPromptTemplate(tools) {
6510
6544
  }
6511
6545
  out += "\n</tools>";
6512
6546
  out += QWEN3CODER_TOOL_CALL_INSTRUCTIONS;
6547
+ const inputExamplesText = renderInputExamplesSection({
6548
+ tools,
6549
+ renderExample: renderQwen3CoderInputExample
6550
+ });
6551
+ if (inputExamplesText.length > 0) {
6552
+ out += `
6553
+
6554
+ ${inputExamplesText}`;
6555
+ }
6513
6556
  return out;
6514
6557
  }
6558
+ function renderQwen3CoderInputExample(toolName, input) {
6559
+ const parameterBlocks = renderQwen3CoderParameters(input);
6560
+ return `<tool_call>
6561
+ <function=${escapeXmlMinimalAttr(toolName, '"')}>${parameterBlocks}
6562
+ </function>
6563
+ </tool_call>`;
6564
+ }
6565
+ function renderQwen3CoderParameters(input) {
6566
+ if (isMapping2(input)) {
6567
+ const lines = Object.entries(input).map(([key, value]) => {
6568
+ const content = renderQwen3CoderParameterValue(value);
6569
+ return `<parameter=${escapeXmlMinimalAttr(key, '"')}>
6570
+ ${content}
6571
+ </parameter>`;
6572
+ });
6573
+ if (lines.length > 0) {
6574
+ return `
6575
+ ${lines.join("\n")}`;
6576
+ }
6577
+ }
6578
+ const fallback = renderQwen3CoderParameterValue(input);
6579
+ return `
6580
+ <parameter=input>
6581
+ ${fallback}
6582
+ </parameter>`;
6583
+ }
6584
+ function renderQwen3CoderParameterValue(value) {
6585
+ if (typeof value === "string") {
6586
+ return escapeXmlMinimalText(value);
6587
+ }
6588
+ const serialized = safeStringifyInputExample(value);
6589
+ return escapeXmlMinimalText(serialized);
6590
+ }
6515
6591
  function stringifyToolResponseContent(value) {
6516
6592
  if (typeof value === "string") {
6517
6593
  return value;
@@ -6532,9 +6608,25 @@ function formatToolResponseAsQwen3CoderXml(toolResult) {
6532
6608
  return formatToolResponseAsQwen3CoderXmlWithOptions(toolResult);
6533
6609
  }
6534
6610
 
6611
+ // src/core/prompts/yaml-xml-prompt.ts
6612
+ import YAML2 from "yaml";
6613
+
6614
+ // src/core/prompts/shared/xml-tag-name.ts
6615
+ var XML_TAG_NAME_REGEX = /^[A-Za-z_][A-Za-z0-9_.:-]*$/;
6616
+ function isValidXmlTagName(name) {
6617
+ return XML_TAG_NAME_REGEX.test(name);
6618
+ }
6619
+ function toSafeXmlTagName(name) {
6620
+ return isValidXmlTagName(name) ? name : "tool";
6621
+ }
6622
+
6535
6623
  // src/core/prompts/yaml-xml-prompt.ts
6536
6624
  function yamlXmlSystemPromptTemplate(tools, includeMultilineExample = true) {
6537
6625
  const toolsJson = JSON.stringify(tools);
6626
+ const inputExamplesText = renderInputExamplesSection({
6627
+ tools,
6628
+ renderExample: renderYamlXmlInputExample
6629
+ });
6538
6630
  const multilineExample = includeMultilineExample ? `
6539
6631
 
6540
6632
  For multiline values, use YAML's literal block syntax:
@@ -6545,7 +6637,7 @@ contents: |
6545
6637
  Second line
6546
6638
  Third line
6547
6639
  </write_file>` : "";
6548
- return `# Tools
6640
+ const basePrompt = `# Tools
6549
6641
 
6550
6642
  You may call one or more functions to assist with the user query.
6551
6643
 
@@ -6571,6 +6663,26 @@ unit: celsius
6571
6663
  - After calling a tool, you will receive a response. Use this result to answer the user.
6572
6664
  - Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
6573
6665
  - If a task requires multiple function calls, make ALL of them at once.`;
6666
+ if (inputExamplesText.length === 0) {
6667
+ return basePrompt;
6668
+ }
6669
+ return `${basePrompt}
6670
+
6671
+ ${inputExamplesText}`;
6672
+ }
6673
+ function renderYamlXmlInputExample(toolName, input) {
6674
+ const safeToolName = toSafeXmlTagName(toolName);
6675
+ let yamlBody = "null";
6676
+ try {
6677
+ const yaml = YAML2.stringify(input).trimEnd();
6678
+ yamlBody = yaml.length > 0 ? yaml : "null";
6679
+ } catch (error) {
6680
+ yamlBody = safeStringifyInputExample(input, error);
6681
+ }
6682
+ const escapedYamlBody = escapeXmlMinimalText(yamlBody);
6683
+ return `<${safeToolName}>
6684
+ ${escapedYamlBody}
6685
+ </${safeToolName}>`;
6574
6686
  }
6575
6687
  function formatToolResponseAsYaml(toolResult) {
6576
6688
  return morphFormatToolResponseAsXml(toolResult);
@@ -7207,7 +7319,11 @@ export {
7207
7319
  isToolResultPart,
7208
7320
  hasInputProperty,
7209
7321
  wrapGenerate,
7322
+ safeStringifyInputExample,
7323
+ renderInputExamplesSection,
7210
7324
  morphFormatToolResponseAsXml,
7325
+ isValidXmlTagName,
7326
+ toSafeXmlTagName,
7211
7327
  wrapStream,
7212
7328
  toolChoiceStream,
7213
7329
  transformParams,
@@ -7217,4 +7333,4 @@ export {
7217
7333
  morphXmlToolMiddleware,
7218
7334
  yamlXmlToolMiddleware
7219
7335
  };
7220
- //# sourceMappingURL=chunk-X3NZ2YHV.js.map
7336
+ //# sourceMappingURL=chunk-ZIOUCBCX.js.map