@ai-sdk-tool/parser 4.1.1 → 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.
- package/dist/{chunk-722D5BGD.js → chunk-ZIOUCBCX.js} +183 -5
- package/dist/chunk-ZIOUCBCX.js.map +1 -0
- package/dist/community.cjs +4806 -4535
- package/dist/community.cjs.map +1 -1
- package/dist/community.js +112 -5
- package/dist/community.js.map +1 -1
- package/dist/index.cjs +178 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -3
- package/dist/chunk-722D5BGD.js.map +0 -1
|
@@ -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
|
-
|
|
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
|
|
|
@@ -6054,6 +6130,7 @@ For each function call return a json object with function name and arguments wit
|
|
|
6054
6130
|
import dedent from "dedent";
|
|
6055
6131
|
function morphXmlSystemPromptTemplate(tools) {
|
|
6056
6132
|
const toolsText = renderToolsForXmlPrompt(tools);
|
|
6133
|
+
const inputExamplesText = renderInputExamplesForXmlPrompt(tools);
|
|
6057
6134
|
const header = dedent`
|
|
6058
6135
|
# Tools
|
|
6059
6136
|
You may call one or more functions to assist with the user query.
|
|
@@ -6068,7 +6145,7 @@ function morphXmlSystemPromptTemplate(tools) {
|
|
|
6068
6145
|
<rules>
|
|
6069
6146
|
- Use exactly one XML element whose tag name is the function name.
|
|
6070
6147
|
- Put each parameter as a child element.
|
|
6071
|
-
- Values must follow the schema exactly (numbers, arrays, objects, enums
|
|
6148
|
+
- Values must follow the schema exactly (numbers, arrays, objects, enums -> copy as-is).
|
|
6072
6149
|
- Do not add or remove functions or parameters.
|
|
6073
6150
|
- Each required parameter must appear once.
|
|
6074
6151
|
- Output nothing before or after the function call.
|
|
@@ -6084,7 +6161,7 @@ function morphXmlSystemPromptTemplate(tools) {
|
|
|
6084
6161
|
multiple lines</example_parameter_2>
|
|
6085
6162
|
</example_function_name>
|
|
6086
6163
|
`;
|
|
6087
|
-
return [header, definitions, rules, examples].join("\n\n");
|
|
6164
|
+
return [header, definitions, rules, examples, inputExamplesText].filter((section) => section.trim().length > 0).join("\n\n");
|
|
6088
6165
|
}
|
|
6089
6166
|
var INDENT = " ";
|
|
6090
6167
|
function renderToolsForXmlPrompt(tools) {
|
|
@@ -6104,6 +6181,25 @@ function renderToolForXmlPrompt(tool) {
|
|
|
6104
6181
|
lines.push(`schema: ${stringifySchema(normalizedSchema)}`);
|
|
6105
6182
|
return lines.join("\n");
|
|
6106
6183
|
}
|
|
6184
|
+
function renderMorphXmlInputExample(toolName, input) {
|
|
6185
|
+
try {
|
|
6186
|
+
return stringify(toolName, input, {
|
|
6187
|
+
suppressEmptyNode: false,
|
|
6188
|
+
format: true,
|
|
6189
|
+
minimalEscaping: true
|
|
6190
|
+
});
|
|
6191
|
+
} catch (error) {
|
|
6192
|
+
const fallbackContent = safeStringifyInputExample(input, error);
|
|
6193
|
+
const escapedFallback = escapeXmlMinimalText(fallbackContent);
|
|
6194
|
+
return `<${toolName}>${escapedFallback}</${toolName}>`;
|
|
6195
|
+
}
|
|
6196
|
+
}
|
|
6197
|
+
function renderInputExamplesForXmlPrompt(tools) {
|
|
6198
|
+
return renderInputExamplesSection({
|
|
6199
|
+
tools,
|
|
6200
|
+
renderExample: renderMorphXmlInputExample
|
|
6201
|
+
});
|
|
6202
|
+
}
|
|
6107
6203
|
function normalizeSchema(schema) {
|
|
6108
6204
|
if (typeof schema === "string") {
|
|
6109
6205
|
try {
|
|
@@ -6448,8 +6544,50 @@ function qwen3coderSystemPromptTemplate(tools) {
|
|
|
6448
6544
|
}
|
|
6449
6545
|
out += "\n</tools>";
|
|
6450
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
|
+
}
|
|
6451
6556
|
return out;
|
|
6452
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
|
+
}
|
|
6453
6591
|
function stringifyToolResponseContent(value) {
|
|
6454
6592
|
if (typeof value === "string") {
|
|
6455
6593
|
return value;
|
|
@@ -6470,9 +6608,25 @@ function formatToolResponseAsQwen3CoderXml(toolResult) {
|
|
|
6470
6608
|
return formatToolResponseAsQwen3CoderXmlWithOptions(toolResult);
|
|
6471
6609
|
}
|
|
6472
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
|
+
|
|
6473
6623
|
// src/core/prompts/yaml-xml-prompt.ts
|
|
6474
6624
|
function yamlXmlSystemPromptTemplate(tools, includeMultilineExample = true) {
|
|
6475
6625
|
const toolsJson = JSON.stringify(tools);
|
|
6626
|
+
const inputExamplesText = renderInputExamplesSection({
|
|
6627
|
+
tools,
|
|
6628
|
+
renderExample: renderYamlXmlInputExample
|
|
6629
|
+
});
|
|
6476
6630
|
const multilineExample = includeMultilineExample ? `
|
|
6477
6631
|
|
|
6478
6632
|
For multiline values, use YAML's literal block syntax:
|
|
@@ -6483,7 +6637,7 @@ contents: |
|
|
|
6483
6637
|
Second line
|
|
6484
6638
|
Third line
|
|
6485
6639
|
</write_file>` : "";
|
|
6486
|
-
|
|
6640
|
+
const basePrompt = `# Tools
|
|
6487
6641
|
|
|
6488
6642
|
You may call one or more functions to assist with the user query.
|
|
6489
6643
|
|
|
@@ -6509,6 +6663,26 @@ unit: celsius
|
|
|
6509
6663
|
- After calling a tool, you will receive a response. Use this result to answer the user.
|
|
6510
6664
|
- Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
|
|
6511
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}>`;
|
|
6512
6686
|
}
|
|
6513
6687
|
function formatToolResponseAsYaml(toolResult) {
|
|
6514
6688
|
return morphFormatToolResponseAsXml(toolResult);
|
|
@@ -7145,7 +7319,11 @@ export {
|
|
|
7145
7319
|
isToolResultPart,
|
|
7146
7320
|
hasInputProperty,
|
|
7147
7321
|
wrapGenerate,
|
|
7322
|
+
safeStringifyInputExample,
|
|
7323
|
+
renderInputExamplesSection,
|
|
7148
7324
|
morphFormatToolResponseAsXml,
|
|
7325
|
+
isValidXmlTagName,
|
|
7326
|
+
toSafeXmlTagName,
|
|
7149
7327
|
wrapStream,
|
|
7150
7328
|
toolChoiceStream,
|
|
7151
7329
|
transformParams,
|
|
@@ -7155,4 +7333,4 @@ export {
|
|
|
7155
7333
|
morphXmlToolMiddleware,
|
|
7156
7334
|
yamlXmlToolMiddleware
|
|
7157
7335
|
};
|
|
7158
|
-
//# sourceMappingURL=chunk-
|
|
7336
|
+
//# sourceMappingURL=chunk-ZIOUCBCX.js.map
|