@ai-sdk-tool/parser 4.1.2 → 4.1.3

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.
@@ -3011,6 +3011,65 @@ function parse2(xml, schema, options = {}) {
3011
3011
  throw new RXMLParseError("Failed to parse XML with repair heuristics", error);
3012
3012
  }
3013
3013
 
3014
+ // src/core/prompts/shared/input-examples.ts
3015
+ function getToolInputExamples(tool) {
3016
+ const inputExamples = tool.inputExamples;
3017
+ if (!Array.isArray(inputExamples)) {
3018
+ return [];
3019
+ }
3020
+ return inputExamples.filter(
3021
+ (example) => typeof example === "object" && example !== null && "input" in example && example.input !== void 0
3022
+ );
3023
+ }
3024
+ function safeStringifyInputExample(input, sourceError) {
3025
+ try {
3026
+ const serialized = JSON.stringify(input);
3027
+ return serialized != null ? serialized : "null";
3028
+ } catch (stringifyError) {
3029
+ let reason = "";
3030
+ if (sourceError instanceof Error) {
3031
+ reason = sourceError.message;
3032
+ } else if (stringifyError instanceof Error) {
3033
+ reason = stringifyError.message;
3034
+ }
3035
+ return reason.length > 0 ? `[unserializable input: ${reason}]` : "[unserializable input]";
3036
+ }
3037
+ }
3038
+ function stringifyInputExampleAsJsonLiteral(input) {
3039
+ try {
3040
+ const serialized = JSON.stringify(input);
3041
+ return serialized != null ? serialized : "null";
3042
+ } catch (error) {
3043
+ const fallbackText = safeStringifyInputExample(input, error);
3044
+ return JSON.stringify(fallbackText);
3045
+ }
3046
+ }
3047
+ var INPUT_EXAMPLES_SECTION_HEADER = [
3048
+ "# Input Examples",
3049
+ "Treat these as canonical tool-call patterns.",
3050
+ "Reuse the closest structure and nesting, change only values, and do not invent parameters.",
3051
+ "Do not copy example values unless they match the user's request."
3052
+ ];
3053
+ function renderInputExamplesSection(options) {
3054
+ const renderedTools = options.tools.map((tool) => {
3055
+ const inputExamples = getToolInputExamples(tool);
3056
+ if (inputExamples.length === 0) {
3057
+ return "";
3058
+ }
3059
+ const renderedExamples = inputExamples.map((example, index) => {
3060
+ const rendered = options.renderExample(tool.name, example.input);
3061
+ return `Example ${index + 1}:
3062
+ ${rendered}`;
3063
+ }).join("\n\n");
3064
+ return `Tool: ${tool.name}
3065
+ ${renderedExamples}`;
3066
+ }).filter((text) => text.length > 0).join("\n\n");
3067
+ if (renderedTools.length === 0) {
3068
+ return "";
3069
+ }
3070
+ return [...INPUT_EXAMPLES_SECTION_HEADER, renderedTools].join("\n\n");
3071
+ }
3072
+
3014
3073
  // src/core/prompts/shared/text-part.ts
3015
3074
  function toTextPart(text, providerOptions) {
3016
3075
  if (providerOptions === void 0) {
@@ -3234,29 +3293,6 @@ function renderToolForXmlPrompt(tool) {
3234
3293
  lines.push(`schema: ${stringifySchema(normalizedSchema)}`);
3235
3294
  return lines.join("\n");
3236
3295
  }
3237
- function getToolInputExamples(tool) {
3238
- const inputExamples = tool.inputExamples;
3239
- if (!Array.isArray(inputExamples)) {
3240
- return [];
3241
- }
3242
- return inputExamples.filter(
3243
- (example) => typeof example === "object" && example !== null && "input" in example && example.input !== void 0
3244
- );
3245
- }
3246
- function safeStringifyInputExample(input, sourceError) {
3247
- try {
3248
- const serialized = JSON.stringify(input);
3249
- return serialized != null ? serialized : "null";
3250
- } catch (stringifyError) {
3251
- let reason = "";
3252
- if (sourceError instanceof Error) {
3253
- reason = sourceError.message;
3254
- } else if (stringifyError instanceof Error) {
3255
- reason = stringifyError.message;
3256
- }
3257
- return reason.length > 0 ? `[unserializable input: ${reason}]` : "[unserializable input]";
3258
- }
3259
- }
3260
3296
  function renderMorphXmlInputExample(toolName, input) {
3261
3297
  try {
3262
3298
  return stringify(toolName, input, {
@@ -3271,29 +3307,10 @@ function renderMorphXmlInputExample(toolName, input) {
3271
3307
  }
3272
3308
  }
3273
3309
  function renderInputExamplesForXmlPrompt(tools) {
3274
- const renderedTools = tools.map((tool) => {
3275
- const inputExamples = getToolInputExamples(tool);
3276
- if (inputExamples.length === 0) {
3277
- return "";
3278
- }
3279
- const renderedExamples = inputExamples.map((example, index) => {
3280
- const xml = renderMorphXmlInputExample(tool.name, example.input);
3281
- return `Example ${index + 1}:
3282
- ${xml}`;
3283
- }).join("\n\n");
3284
- return `Tool: ${tool.name}
3285
- ${renderedExamples}`;
3286
- }).filter((text) => text.length > 0).join("\n\n");
3287
- if (renderedTools.length === 0) {
3288
- return "";
3289
- }
3290
- return [
3291
- "# Input Examples",
3292
- "Treat these as canonical tool-call patterns.",
3293
- "Reuse the closest structure and nesting, change only values, and do not invent parameters.",
3294
- "Do not copy example values unless they match the user's request.",
3295
- renderedTools
3296
- ].join("\n\n");
3310
+ return renderInputExamplesSection({
3311
+ tools,
3312
+ renderExample: renderMorphXmlInputExample
3313
+ });
3297
3314
  }
3298
3315
  function normalizeSchema(schema) {
3299
3316
  if (typeof schema === "string") {
@@ -3515,6 +3532,15 @@ function morphFormatToolResponseAsXml(toolResult) {
3515
3532
  return morphFormatToolResponseAsXmlWithOptions(toolResult);
3516
3533
  }
3517
3534
 
3535
+ // src/core/prompts/shared/xml-tag-name.ts
3536
+ var XML_TAG_NAME_REGEX = /^[A-Za-z_][A-Za-z0-9_.:-]*$/;
3537
+ function isValidXmlTagName(name) {
3538
+ return XML_TAG_NAME_REGEX.test(name);
3539
+ }
3540
+ function toSafeXmlTagName(name) {
3541
+ return isValidXmlTagName(name) ? name : "tool";
3542
+ }
3543
+
3518
3544
  // src/rjson/index.ts
3519
3545
  var WHITESPACE_TEST_REGEX = /\s/;
3520
3546
  var WHITESPACE_REGEX4 = /^\s+/;
@@ -9994,11 +10020,28 @@ function renderToolDefinition(tool) {
9994
10020
  }
9995
10021
  function hermesSystemPromptTemplate(tools) {
9996
10022
  const toolsRendered = tools.map(renderToolDefinition).join("\n");
9997
- 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>
10023
+ 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>
9998
10024
  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"}
9999
10025
  For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
10000
10026
  <tool_call>
10001
10027
  {"name": "<function-name>", "arguments": <args-dict>}
10028
+ </tool_call>`;
10029
+ const inputExamplesText = renderInputExamplesSection({
10030
+ tools,
10031
+ renderExample: renderHermesInputExample
10032
+ });
10033
+ if (inputExamplesText.length === 0) {
10034
+ return basePrompt;
10035
+ }
10036
+ return `${basePrompt}
10037
+
10038
+ ${inputExamplesText}`;
10039
+ }
10040
+ function renderHermesInputExample(toolName, input) {
10041
+ const argumentsLiteral = stringifyInputExampleAsJsonLiteral(input);
10042
+ const nameLiteral = JSON.stringify(toolName);
10043
+ return `<tool_call>
10044
+ {"name":${nameLiteral},"arguments":${argumentsLiteral}}
10002
10045
  </tool_call>`;
10003
10046
  }
10004
10047
 
@@ -10126,8 +10169,50 @@ function qwen3coderSystemPromptTemplate(tools) {
10126
10169
  }
10127
10170
  out += "\n</tools>";
10128
10171
  out += QWEN3CODER_TOOL_CALL_INSTRUCTIONS;
10172
+ const inputExamplesText = renderInputExamplesSection({
10173
+ tools,
10174
+ renderExample: renderQwen3CoderInputExample
10175
+ });
10176
+ if (inputExamplesText.length > 0) {
10177
+ out += `
10178
+
10179
+ ${inputExamplesText}`;
10180
+ }
10129
10181
  return out;
10130
10182
  }
10183
+ function renderQwen3CoderInputExample(toolName, input) {
10184
+ const parameterBlocks = renderQwen3CoderParameters(input);
10185
+ return `<tool_call>
10186
+ <function=${escapeXmlMinimalAttr(toolName, '"')}>${parameterBlocks}
10187
+ </function>
10188
+ </tool_call>`;
10189
+ }
10190
+ function renderQwen3CoderParameters(input) {
10191
+ if (isMapping2(input)) {
10192
+ const lines = Object.entries(input).map(([key, value]) => {
10193
+ const content = renderQwen3CoderParameterValue(value);
10194
+ return `<parameter=${escapeXmlMinimalAttr(key, '"')}>
10195
+ ${content}
10196
+ </parameter>`;
10197
+ });
10198
+ if (lines.length > 0) {
10199
+ return `
10200
+ ${lines.join("\n")}`;
10201
+ }
10202
+ }
10203
+ const fallback = renderQwen3CoderParameterValue(input);
10204
+ return `
10205
+ <parameter=input>
10206
+ ${fallback}
10207
+ </parameter>`;
10208
+ }
10209
+ function renderQwen3CoderParameterValue(value) {
10210
+ if (typeof value === "string") {
10211
+ return escapeXmlMinimalText(value);
10212
+ }
10213
+ const serialized = safeStringifyInputExample(value);
10214
+ return escapeXmlMinimalText(serialized);
10215
+ }
10131
10216
  function stringifyToolResponseContent(value) {
10132
10217
  if (typeof value === "string") {
10133
10218
  return value;
@@ -10149,8 +10234,13 @@ function formatToolResponseAsQwen3CoderXml(toolResult) {
10149
10234
  }
10150
10235
 
10151
10236
  // src/core/prompts/yaml-xml-prompt.ts
10237
+ var import_yaml2 = __toESM(require("yaml"), 1);
10152
10238
  function yamlXmlSystemPromptTemplate(tools, includeMultilineExample = true) {
10153
10239
  const toolsJson = JSON.stringify(tools);
10240
+ const inputExamplesText = renderInputExamplesSection({
10241
+ tools,
10242
+ renderExample: renderYamlXmlInputExample
10243
+ });
10154
10244
  const multilineExample = includeMultilineExample ? `
10155
10245
 
10156
10246
  For multiline values, use YAML's literal block syntax:
@@ -10161,7 +10251,7 @@ contents: |
10161
10251
  Second line
10162
10252
  Third line
10163
10253
  </write_file>` : "";
10164
- return `# Tools
10254
+ const basePrompt = `# Tools
10165
10255
 
10166
10256
  You may call one or more functions to assist with the user query.
10167
10257
 
@@ -10187,6 +10277,26 @@ unit: celsius
10187
10277
  - After calling a tool, you will receive a response. Use this result to answer the user.
10188
10278
  - Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
10189
10279
  - If a task requires multiple function calls, make ALL of them at once.`;
10280
+ if (inputExamplesText.length === 0) {
10281
+ return basePrompt;
10282
+ }
10283
+ return `${basePrompt}
10284
+
10285
+ ${inputExamplesText}`;
10286
+ }
10287
+ function renderYamlXmlInputExample(toolName, input) {
10288
+ const safeToolName = toSafeXmlTagName(toolName);
10289
+ let yamlBody = "null";
10290
+ try {
10291
+ const yaml = import_yaml2.default.stringify(input).trimEnd();
10292
+ yamlBody = yaml.length > 0 ? yaml : "null";
10293
+ } catch (error) {
10294
+ yamlBody = safeStringifyInputExample(input, error);
10295
+ }
10296
+ const escapedYamlBody = escapeXmlMinimalText(yamlBody);
10297
+ return `<${safeToolName}>
10298
+ ${escapedYamlBody}
10299
+ </${safeToolName}>`;
10190
10300
  }
10191
10301
  function formatToolResponseAsYaml(toolResult) {
10192
10302
  return morphFormatToolResponseAsXml(toolResult);
@@ -10796,12 +10906,23 @@ var yamlXmlToolMiddleware = createToolMiddleware({
10796
10906
  });
10797
10907
 
10798
10908
  // src/community/sijawara.ts
10909
+ function hasInvalidXmlKeys(value) {
10910
+ if (Array.isArray(value)) {
10911
+ return value.some((entry) => hasInvalidXmlKeys(entry));
10912
+ }
10913
+ if (value && typeof value === "object") {
10914
+ return Object.entries(value).some(
10915
+ ([key, nested]) => !isValidXmlTagName(key) || hasInvalidXmlKeys(nested)
10916
+ );
10917
+ }
10918
+ return false;
10919
+ }
10799
10920
  var sijawaraDetailedXmlToolMiddleware = createToolMiddleware({
10800
10921
  protocol: morphXmlProtocol,
10801
10922
  toolResponsePromptTemplate: morphFormatToolResponseAsXml,
10802
10923
  toolSystemPromptTemplate(tools) {
10803
10924
  const toolsJson = JSON.stringify(tools);
10804
- return `You have access to callable functions (tools).
10925
+ const basePrompt = `You have access to callable functions (tools).
10805
10926
  Tool list/context:
10806
10927
  ${toolsJson}
10807
10928
 
@@ -10852,6 +10973,13 @@ var sijawaraDetailedXmlToolMiddleware = createToolMiddleware({
10852
10973
  - Include all required parameters. If info is missing, ask the user.
10853
10974
  - Do NOT use JSON\u2014use only the specified XML-like format for tool calls.
10854
10975
  - If no call is needed, reply without a tool call.`;
10976
+ const inputExamplesText = renderSijawaraInputExamples(tools);
10977
+ if (inputExamplesText.length === 0) {
10978
+ return basePrompt;
10979
+ }
10980
+ return `${basePrompt}
10981
+
10982
+ ${inputExamplesText}`;
10855
10983
  }
10856
10984
  });
10857
10985
  var sijawaraConciseXmlToolMiddleware = createToolMiddleware({
@@ -10859,7 +10987,7 @@ var sijawaraConciseXmlToolMiddleware = createToolMiddleware({
10859
10987
  toolResponsePromptTemplate: morphFormatToolResponseAsXml,
10860
10988
  toolSystemPromptTemplate(tools) {
10861
10989
  const toolsJson = JSON.stringify(tools);
10862
- return `You have access to callable functions (tools).
10990
+ const basePrompt = `You have access to callable functions (tools).
10863
10991
  Tool list/context:
10864
10992
  ${toolsJson}
10865
10993
 
@@ -10885,8 +11013,40 @@ var sijawaraConciseXmlToolMiddleware = createToolMiddleware({
10885
11013
  San Francisco
10886
11014
  </location>
10887
11015
  </get_weather>`;
11016
+ const inputExamplesText = renderSijawaraInputExamples(tools);
11017
+ if (inputExamplesText.length === 0) {
11018
+ return basePrompt;
11019
+ }
11020
+ return `${basePrompt}
11021
+
11022
+ ${inputExamplesText}`;
10888
11023
  }
10889
11024
  });
11025
+ function renderSijawaraInputExamples(tools) {
11026
+ return renderInputExamplesSection({
11027
+ tools,
11028
+ renderExample: renderSijawaraInputExample
11029
+ });
11030
+ }
11031
+ function renderSijawaraInputExample(toolName, input) {
11032
+ const safeToolName = toSafeXmlTagName(toolName);
11033
+ if (hasInvalidXmlKeys(input)) {
11034
+ const fallbackContent = safeStringifyInputExample(input);
11035
+ const escapedFallback = escapeXmlMinimalText(fallbackContent);
11036
+ return `<${safeToolName}>${escapedFallback}</${safeToolName}>`;
11037
+ }
11038
+ try {
11039
+ return stringify(safeToolName, input, {
11040
+ suppressEmptyNode: false,
11041
+ format: true,
11042
+ minimalEscaping: true
11043
+ });
11044
+ } catch (error) {
11045
+ const fallbackContent = safeStringifyInputExample(input, error);
11046
+ const escapedFallback = escapeXmlMinimalText(fallbackContent);
11047
+ return `<${safeToolName}>${escapedFallback}</${safeToolName}>`;
11048
+ }
11049
+ }
10890
11050
 
10891
11051
  // src/community/ui-tars.ts
10892
11052
  var uiTarsToolMiddleware = createToolMiddleware({
@@ -10894,7 +11054,7 @@ var uiTarsToolMiddleware = createToolMiddleware({
10894
11054
  toolResponsePromptTemplate: morphFormatToolResponseAsXml,
10895
11055
  toolSystemPromptTemplate(tools) {
10896
11056
  const toolsJson = JSON.stringify(tools);
10897
- return `You have access to callable functions (tools).
11057
+ const basePrompt = `You have access to callable functions (tools).
10898
11058
  Tool list/context:
10899
11059
  ${toolsJson}
10900
11060
 
@@ -11000,8 +11160,57 @@ EXECUTION RULES
11000
11160
  - If you don't know a parameter value, ask the user
11001
11161
  - After calling a function, STOP and wait for the result
11002
11162
  - Do NOT add extra text before or after tool calls`;
11163
+ const inputExamplesText = renderUiTarsInputExamples(tools);
11164
+ if (inputExamplesText.length === 0) {
11165
+ return basePrompt;
11166
+ }
11167
+ return `${basePrompt}
11168
+
11169
+ ${inputExamplesText}`;
11003
11170
  }
11004
11171
  });
11172
+ function renderUiTarsInputExamples(tools) {
11173
+ return renderInputExamplesSection({
11174
+ tools,
11175
+ renderExample: renderUiTarsInputExample
11176
+ });
11177
+ }
11178
+ function renderUiTarsInputExample(toolName, input) {
11179
+ const parameterBlocks = renderUiTarsParameters(input);
11180
+ return `<tool_call>
11181
+ <function=${escapeXmlMinimalAttr(toolName, '"')}>${parameterBlocks}
11182
+ </function>
11183
+ </tool_call>`;
11184
+ }
11185
+ function renderUiTarsParameters(input) {
11186
+ if (isRecord3(input)) {
11187
+ const lines = Object.entries(input).map(([key, value]) => {
11188
+ const content = renderUiTarsParameterValue(value);
11189
+ return `<parameter=${escapeXmlMinimalAttr(key, '"')}>
11190
+ ${content}
11191
+ </parameter>`;
11192
+ });
11193
+ if (lines.length > 0) {
11194
+ return `
11195
+ ${lines.join("\n")}`;
11196
+ }
11197
+ }
11198
+ const fallback = renderUiTarsParameterValue(input);
11199
+ return `
11200
+ <parameter=input>
11201
+ ${fallback}
11202
+ </parameter>`;
11203
+ }
11204
+ function renderUiTarsParameterValue(value) {
11205
+ if (typeof value === "string") {
11206
+ return escapeXmlMinimalText(value);
11207
+ }
11208
+ const serialized = safeStringifyInputExample(value);
11209
+ return escapeXmlMinimalText(serialized);
11210
+ }
11211
+ function isRecord3(value) {
11212
+ return typeof value === "object" && value !== null && !Array.isArray(value);
11213
+ }
11005
11214
  // Annotate the CommonJS export names for ESM import in node:
11006
11215
  0 && (module.exports = {
11007
11216
  sijawaraConciseXmlToolMiddleware,