@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.
package/dist/community.js CHANGED
@@ -1,20 +1,39 @@
1
1
  import {
2
2
  createToolMiddleware,
3
+ isValidXmlTagName,
3
4
  morphFormatToolResponseAsXml,
4
5
  morphXmlProtocol,
6
+ renderInputExamplesSection,
7
+ safeStringifyInputExample,
8
+ toSafeXmlTagName,
5
9
  uiTarsXmlProtocol
6
- } from "./chunk-X3NZ2YHV.js";
7
- import "./chunk-D4YULTAO.js";
10
+ } from "./chunk-ZIOUCBCX.js";
11
+ import {
12
+ escapeXmlMinimalAttr,
13
+ escapeXmlMinimalText,
14
+ stringify
15
+ } from "./chunk-D4YULTAO.js";
8
16
  import "./chunk-QBZNMO5C.js";
9
17
  import "./chunk-GMTE7BY5.js";
10
18
 
11
19
  // src/community/sijawara.ts
20
+ function hasInvalidXmlKeys(value) {
21
+ if (Array.isArray(value)) {
22
+ return value.some((entry) => hasInvalidXmlKeys(entry));
23
+ }
24
+ if (value && typeof value === "object") {
25
+ return Object.entries(value).some(
26
+ ([key, nested]) => !isValidXmlTagName(key) || hasInvalidXmlKeys(nested)
27
+ );
28
+ }
29
+ return false;
30
+ }
12
31
  var sijawaraDetailedXmlToolMiddleware = createToolMiddleware({
13
32
  protocol: morphXmlProtocol,
14
33
  toolResponsePromptTemplate: morphFormatToolResponseAsXml,
15
34
  toolSystemPromptTemplate(tools) {
16
35
  const toolsJson = JSON.stringify(tools);
17
- return `You have access to callable functions (tools).
36
+ const basePrompt = `You have access to callable functions (tools).
18
37
  Tool list/context:
19
38
  ${toolsJson}
20
39
 
@@ -65,6 +84,13 @@ var sijawaraDetailedXmlToolMiddleware = createToolMiddleware({
65
84
  - Include all required parameters. If info is missing, ask the user.
66
85
  - Do NOT use JSON\u2014use only the specified XML-like format for tool calls.
67
86
  - If no call is needed, reply without a tool call.`;
87
+ const inputExamplesText = renderSijawaraInputExamples(tools);
88
+ if (inputExamplesText.length === 0) {
89
+ return basePrompt;
90
+ }
91
+ return `${basePrompt}
92
+
93
+ ${inputExamplesText}`;
68
94
  }
69
95
  });
70
96
  var sijawaraConciseXmlToolMiddleware = createToolMiddleware({
@@ -72,7 +98,7 @@ var sijawaraConciseXmlToolMiddleware = createToolMiddleware({
72
98
  toolResponsePromptTemplate: morphFormatToolResponseAsXml,
73
99
  toolSystemPromptTemplate(tools) {
74
100
  const toolsJson = JSON.stringify(tools);
75
- return `You have access to callable functions (tools).
101
+ const basePrompt = `You have access to callable functions (tools).
76
102
  Tool list/context:
77
103
  ${toolsJson}
78
104
 
@@ -98,8 +124,40 @@ var sijawaraConciseXmlToolMiddleware = createToolMiddleware({
98
124
  San Francisco
99
125
  </location>
100
126
  </get_weather>`;
127
+ const inputExamplesText = renderSijawaraInputExamples(tools);
128
+ if (inputExamplesText.length === 0) {
129
+ return basePrompt;
130
+ }
131
+ return `${basePrompt}
132
+
133
+ ${inputExamplesText}`;
101
134
  }
102
135
  });
136
+ function renderSijawaraInputExamples(tools) {
137
+ return renderInputExamplesSection({
138
+ tools,
139
+ renderExample: renderSijawaraInputExample
140
+ });
141
+ }
142
+ function renderSijawaraInputExample(toolName, input) {
143
+ const safeToolName = toSafeXmlTagName(toolName);
144
+ if (hasInvalidXmlKeys(input)) {
145
+ const fallbackContent = safeStringifyInputExample(input);
146
+ const escapedFallback = escapeXmlMinimalText(fallbackContent);
147
+ return `<${safeToolName}>${escapedFallback}</${safeToolName}>`;
148
+ }
149
+ try {
150
+ return stringify(safeToolName, input, {
151
+ suppressEmptyNode: false,
152
+ format: true,
153
+ minimalEscaping: true
154
+ });
155
+ } catch (error) {
156
+ const fallbackContent = safeStringifyInputExample(input, error);
157
+ const escapedFallback = escapeXmlMinimalText(fallbackContent);
158
+ return `<${safeToolName}>${escapedFallback}</${safeToolName}>`;
159
+ }
160
+ }
103
161
 
104
162
  // src/community/ui-tars.ts
105
163
  var uiTarsToolMiddleware = createToolMiddleware({
@@ -107,7 +165,7 @@ var uiTarsToolMiddleware = createToolMiddleware({
107
165
  toolResponsePromptTemplate: morphFormatToolResponseAsXml,
108
166
  toolSystemPromptTemplate(tools) {
109
167
  const toolsJson = JSON.stringify(tools);
110
- return `You have access to callable functions (tools).
168
+ const basePrompt = `You have access to callable functions (tools).
111
169
  Tool list/context:
112
170
  ${toolsJson}
113
171
 
@@ -213,8 +271,57 @@ EXECUTION RULES
213
271
  - If you don't know a parameter value, ask the user
214
272
  - After calling a function, STOP and wait for the result
215
273
  - Do NOT add extra text before or after tool calls`;
274
+ const inputExamplesText = renderUiTarsInputExamples(tools);
275
+ if (inputExamplesText.length === 0) {
276
+ return basePrompt;
277
+ }
278
+ return `${basePrompt}
279
+
280
+ ${inputExamplesText}`;
216
281
  }
217
282
  });
283
+ function renderUiTarsInputExamples(tools) {
284
+ return renderInputExamplesSection({
285
+ tools,
286
+ renderExample: renderUiTarsInputExample
287
+ });
288
+ }
289
+ function renderUiTarsInputExample(toolName, input) {
290
+ const parameterBlocks = renderUiTarsParameters(input);
291
+ return `<tool_call>
292
+ <function=${escapeXmlMinimalAttr(toolName, '"')}>${parameterBlocks}
293
+ </function>
294
+ </tool_call>`;
295
+ }
296
+ function renderUiTarsParameters(input) {
297
+ if (isRecord(input)) {
298
+ const lines = Object.entries(input).map(([key, value]) => {
299
+ const content = renderUiTarsParameterValue(value);
300
+ return `<parameter=${escapeXmlMinimalAttr(key, '"')}>
301
+ ${content}
302
+ </parameter>`;
303
+ });
304
+ if (lines.length > 0) {
305
+ return `
306
+ ${lines.join("\n")}`;
307
+ }
308
+ }
309
+ const fallback = renderUiTarsParameterValue(input);
310
+ return `
311
+ <parameter=input>
312
+ ${fallback}
313
+ </parameter>`;
314
+ }
315
+ function renderUiTarsParameterValue(value) {
316
+ if (typeof value === "string") {
317
+ return escapeXmlMinimalText(value);
318
+ }
319
+ const serialized = safeStringifyInputExample(value);
320
+ return escapeXmlMinimalText(serialized);
321
+ }
322
+ function isRecord(value) {
323
+ return typeof value === "object" && value !== null && !Array.isArray(value);
324
+ }
218
325
  export {
219
326
  sijawaraConciseXmlToolMiddleware,
220
327
  sijawaraDetailedXmlToolMiddleware,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/community/sijawara.ts","../src/community/ui-tars.ts"],"sourcesContent":["import type { LanguageModelV3FunctionTool } from \"@ai-sdk/provider\";\nimport { morphFormatToolResponseAsXml } from \"../core/prompts/morph-xml-prompt\";\nimport { createToolMiddleware, morphXmlProtocol } from \"../index\";\n\nexport const sijawaraDetailedXmlToolMiddleware = createToolMiddleware({\n protocol: morphXmlProtocol,\n toolResponsePromptTemplate: morphFormatToolResponseAsXml,\n toolSystemPromptTemplate(tools: LanguageModelV3FunctionTool[]) {\n const toolsJson = JSON.stringify(tools);\n return `You have access to callable functions (tools).\n Tool list/context:\n ${toolsJson}\n\n ===============================\n TOOL CALLING FORMAT\n ===============================\n - Use the XML-like format for tool calls:\n <tool_name>\n <parameter_name>\n value\n </parameter_name>\n ...\n </tool_name>\n\n ===============================\n ARRAY PARAMETERS\n ===============================\n - For array/multiple values, repeat the parameter for each value.\n - Example:\n <send_messages>\n <recipient>\n alice@example.com\n </recipient>\n <recipient>\n bob@example.com\n </recipient>\n <message>\n Hello!\n </message>\n </send_messages>\n\n ===============================\n SINGLE VALUE PARAMETERS\n ===============================\n - For single values, use one parameter block.\n - Example:\n <get_weather>\n <location>\n San Francisco\n </location>\n </get_weather>\n\n ===============================\n GENERAL RULES\n ===============================\n - First line: tool (function) name in angle brackets.\n - Parameters: each on their own line, in angle brackets, with name and value.\n - Include all required parameters. If info is missing, ask the user.\n - Do NOT use JSON—use only the specified XML-like format for tool calls.\n - If no call is needed, reply without a tool call.`;\n },\n});\n\nexport const sijawaraConciseXmlToolMiddleware = createToolMiddleware({\n protocol: morphXmlProtocol,\n toolResponsePromptTemplate: morphFormatToolResponseAsXml,\n toolSystemPromptTemplate(tools: LanguageModelV3FunctionTool[]) {\n const toolsJson = JSON.stringify(tools);\n return `You have access to callable functions (tools).\n Tool list/context:\n ${toolsJson}\n\n STRICT CALLING RULES:\n - Use the XML-like format for tool calls:\n\n <tool_name>\n <parameter_name>\n value\n </parameter_name>\n ...\n </tool_name>\n\n - First line: the tool (function) name in angle brackets.\n - Parameters: each in their own angle brackets with name and value.\n - Include all required parameters. If info is missing, ask the user.\n - Do NOT use JSON. Use only the specified XML-like format.\n - If no call is needed, reply without a tool call.\n\n Example:\n <get_weather>\n <location>\n San Francisco\n </location>\n </get_weather>`;\n },\n});\n","import type { LanguageModelV3FunctionTool } from \"@ai-sdk/provider\";\nimport { morphFormatToolResponseAsXml } from \"../core/prompts/morph-xml-prompt\";\nimport { createToolMiddleware, uiTarsXmlProtocol } from \"../index\";\n\n/**\n * UI-TARS middleware using a custom protocol that handles <function=name> syntax (qwen3CoderProtocol)\n * This is a modified version of https://github.com/julien-blanchon/silo/blob/6ebbc24e9dcf21e502b65b07b86a1e76169d4e8c/src/lib/middleware/uiTars.ts\n */\nexport const uiTarsToolMiddleware = createToolMiddleware({\n protocol: uiTarsXmlProtocol,\n toolResponsePromptTemplate: morphFormatToolResponseAsXml,\n toolSystemPromptTemplate(tools: LanguageModelV3FunctionTool[]) {\n const toolsJson = JSON.stringify(tools);\n return `You have access to callable functions (tools).\nTool list/context:\n${toolsJson}\n\n===============================\nUI-TARS FUNCTION CALLING FORMAT\n===============================\nYou MUST use the EXACT format below for ALL function calls:\n\n<tool_call>\n<function=tool_name>\n<parameter=parameter_name>\nvalue\n</parameter>\n<parameter=another_parameter>\nvalue\n</parameter>\n</function>\n</tool_call>\n\n===============================\nCRITICAL SYNTAX RULES\n===============================\n1. Start with <tool_call>\n2. Next line must be <function=TOOL_NAME> (use the exact tool name from the list above)\n3. Each parameter MUST use opening/closing tags:\n <parameter=PARAM_NAME>\n VALUE\n </parameter>\n4. End the function with </function>\n5. End the tool call with </tool_call>\n6. NO quotes around tool names or parameter names\n7. NO extra spaces or characters\n8. NO JSON format - only use this XML-like format\n\n===============================\nCORRECT EXAMPLES\n===============================\nScreenshot:\n<tool_call>\n<function=computer>\n<parameter=action>\nscreenshot\n</parameter>\n</function>\n</tool_call>\n\nClick at coordinates:\n<tool_call>\n<function=computer>\n<parameter=action>\nleft_click\n</parameter>\n<parameter=coordinate>\n[100, 200]\n</parameter>\n</function>\n</tool_call>\n\nType text:\n<tool_call>\n<function=computer>\n<parameter=action>\ntype\n</parameter>\n<parameter=text>\nHello World\n</parameter>\n</function>\n</tool_call>\n\nScroll:\n<tool_call>\n<function=computer>\n<parameter=action>\nscroll\n</parameter>\n<parameter=coordinate>\n[500, 400]\n</parameter>\n<parameter=scroll_direction>\ndown\n</parameter>\n<parameter=scroll_amount>\n3\n</parameter>\n</function>\n</tool_call>\n\n===============================\nWRONG FORMATS (DO NOT USE)\n===============================\n❌ Missing <tool_call>...</tool_call> wrapper\n❌ <function=function='screenshot'> (no quotes or extra text)\n❌ <parameter=parameters> (must use actual parameter name)\n❌ JSON format like {\"action\": \"screenshot\"}\n❌ Any format other than the exact XML-like format above\n\n===============================\nEXECUTION RULES\n===============================\n- Use ONLY the tool names listed above\n- Include ALL required parameters for each tool\n- If you don't know a parameter value, ask the user\n- After calling a function, STOP and wait for the result\n- Do NOT add extra text before or after tool calls`;\n },\n});\n"],"mappings":";;;;;;;;;;;AAIO,IAAM,oCAAoC,qBAAqB;AAAA,EACpE,UAAU;AAAA,EACV,4BAA4B;AAAA,EAC5B,yBAAyB,OAAsC;AAC7D,UAAM,YAAY,KAAK,UAAU,KAAK;AACtC,WAAO;AAAA;AAAA,MAEL,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;A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
+ {"version":3,"sources":["../src/community/sijawara.ts","../src/community/ui-tars.ts"],"sourcesContent":["import type { JSONValue, LanguageModelV3FunctionTool } from \"@ai-sdk/provider\";\nimport { morphFormatToolResponseAsXml } from \"../core/prompts/morph-xml-prompt\";\nimport {\n renderInputExamplesSection,\n safeStringifyInputExample,\n} from \"../core/prompts/shared/input-examples\";\nimport {\n isValidXmlTagName,\n toSafeXmlTagName,\n} from \"../core/prompts/shared/xml-tag-name\";\nimport { createToolMiddleware, morphXmlProtocol } from \"../index\";\nimport { stringify } from \"../rxml\";\nimport { escapeXmlMinimalText } from \"../rxml/utils/helpers\";\n\nfunction hasInvalidXmlKeys(value: unknown): boolean {\n if (Array.isArray(value)) {\n return value.some((entry) => hasInvalidXmlKeys(entry));\n }\n\n if (value && typeof value === \"object\") {\n return Object.entries(value as Record<string, unknown>).some(\n ([key, nested]) => !isValidXmlTagName(key) || hasInvalidXmlKeys(nested)\n );\n }\n\n return false;\n}\n\nexport const sijawaraDetailedXmlToolMiddleware = createToolMiddleware({\n protocol: morphXmlProtocol,\n toolResponsePromptTemplate: morphFormatToolResponseAsXml,\n toolSystemPromptTemplate(tools: LanguageModelV3FunctionTool[]) {\n const toolsJson = JSON.stringify(tools);\n const basePrompt = `You have access to callable functions (tools).\n Tool list/context:\n ${toolsJson}\n\n ===============================\n TOOL CALLING FORMAT\n ===============================\n - Use the XML-like format for tool calls:\n <tool_name>\n <parameter_name>\n value\n </parameter_name>\n ...\n </tool_name>\n\n ===============================\n ARRAY PARAMETERS\n ===============================\n - For array/multiple values, repeat the parameter for each value.\n - Example:\n <send_messages>\n <recipient>\n alice@example.com\n </recipient>\n <recipient>\n bob@example.com\n </recipient>\n <message>\n Hello!\n </message>\n </send_messages>\n\n ===============================\n SINGLE VALUE PARAMETERS\n ===============================\n - For single values, use one parameter block.\n - Example:\n <get_weather>\n <location>\n San Francisco\n </location>\n </get_weather>\n\n ===============================\n GENERAL RULES\n ===============================\n - First line: tool (function) name in angle brackets.\n - Parameters: each on their own line, in angle brackets, with name and value.\n - Include all required parameters. If info is missing, ask the user.\n - Do NOT use JSON—use only the specified XML-like format for tool calls.\n - If no call is needed, reply without a tool call.`;\n\n const inputExamplesText = renderSijawaraInputExamples(tools);\n if (inputExamplesText.length === 0) {\n return basePrompt;\n }\n\n return `${basePrompt}\\n\\n${inputExamplesText}`;\n },\n});\n\nexport const sijawaraConciseXmlToolMiddleware = createToolMiddleware({\n protocol: morphXmlProtocol,\n toolResponsePromptTemplate: morphFormatToolResponseAsXml,\n toolSystemPromptTemplate(tools: LanguageModelV3FunctionTool[]) {\n const toolsJson = JSON.stringify(tools);\n const basePrompt = `You have access to callable functions (tools).\n Tool list/context:\n ${toolsJson}\n\n STRICT CALLING RULES:\n - Use the XML-like format for tool calls:\n\n <tool_name>\n <parameter_name>\n value\n </parameter_name>\n ...\n </tool_name>\n\n - First line: the tool (function) name in angle brackets.\n - Parameters: each in their own angle brackets with name and value.\n - Include all required parameters. If info is missing, ask the user.\n - Do NOT use JSON. Use only the specified XML-like format.\n - If no call is needed, reply without a tool call.\n\n Example:\n <get_weather>\n <location>\n San Francisco\n </location>\n </get_weather>`;\n\n const inputExamplesText = renderSijawaraInputExamples(tools);\n if (inputExamplesText.length === 0) {\n return basePrompt;\n }\n\n return `${basePrompt}\\n\\n${inputExamplesText}`;\n },\n});\n\nfunction renderSijawaraInputExamples(\n tools: LanguageModelV3FunctionTool[]\n): string {\n return renderInputExamplesSection({\n tools,\n renderExample: renderSijawaraInputExample,\n });\n}\n\nfunction renderSijawaraInputExample(toolName: string, input: unknown): string {\n const safeToolName = toSafeXmlTagName(toolName);\n\n if (hasInvalidXmlKeys(input)) {\n const fallbackContent = safeStringifyInputExample(input);\n const escapedFallback = escapeXmlMinimalText(fallbackContent);\n return `<${safeToolName}>${escapedFallback}</${safeToolName}>`;\n }\n\n try {\n return stringify(safeToolName, input as JSONValue, {\n suppressEmptyNode: false,\n format: true,\n minimalEscaping: true,\n });\n } catch (error) {\n const fallbackContent = safeStringifyInputExample(input, error);\n const escapedFallback = escapeXmlMinimalText(fallbackContent);\n return `<${safeToolName}>${escapedFallback}</${safeToolName}>`;\n }\n}\n","import type { LanguageModelV3FunctionTool } from \"@ai-sdk/provider\";\nimport { morphFormatToolResponseAsXml } from \"../core/prompts/morph-xml-prompt\";\nimport {\n renderInputExamplesSection,\n safeStringifyInputExample,\n} from \"../core/prompts/shared/input-examples\";\nimport { createToolMiddleware, uiTarsXmlProtocol } from \"../index\";\nimport {\n escapeXmlMinimalAttr,\n escapeXmlMinimalText,\n} from \"../rxml/utils/helpers\";\n\n/**\n * UI-TARS middleware using a custom protocol that handles <function=name> syntax (qwen3CoderProtocol)\n * This is a modified version of https://github.com/julien-blanchon/silo/blob/6ebbc24e9dcf21e502b65b07b86a1e76169d4e8c/src/lib/middleware/uiTars.ts\n */\nexport const uiTarsToolMiddleware = createToolMiddleware({\n protocol: uiTarsXmlProtocol,\n toolResponsePromptTemplate: morphFormatToolResponseAsXml,\n toolSystemPromptTemplate(tools: LanguageModelV3FunctionTool[]) {\n const toolsJson = JSON.stringify(tools);\n const basePrompt = `You have access to callable functions (tools).\nTool list/context:\n${toolsJson}\n\n===============================\nUI-TARS FUNCTION CALLING FORMAT\n===============================\nYou MUST use the EXACT format below for ALL function calls:\n\n<tool_call>\n<function=tool_name>\n<parameter=parameter_name>\nvalue\n</parameter>\n<parameter=another_parameter>\nvalue\n</parameter>\n</function>\n</tool_call>\n\n===============================\nCRITICAL SYNTAX RULES\n===============================\n1. Start with <tool_call>\n2. Next line must be <function=TOOL_NAME> (use the exact tool name from the list above)\n3. Each parameter MUST use opening/closing tags:\n <parameter=PARAM_NAME>\n VALUE\n </parameter>\n4. End the function with </function>\n5. End the tool call with </tool_call>\n6. NO quotes around tool names or parameter names\n7. NO extra spaces or characters\n8. NO JSON format - only use this XML-like format\n\n===============================\nCORRECT EXAMPLES\n===============================\nScreenshot:\n<tool_call>\n<function=computer>\n<parameter=action>\nscreenshot\n</parameter>\n</function>\n</tool_call>\n\nClick at coordinates:\n<tool_call>\n<function=computer>\n<parameter=action>\nleft_click\n</parameter>\n<parameter=coordinate>\n[100, 200]\n</parameter>\n</function>\n</tool_call>\n\nType text:\n<tool_call>\n<function=computer>\n<parameter=action>\ntype\n</parameter>\n<parameter=text>\nHello World\n</parameter>\n</function>\n</tool_call>\n\nScroll:\n<tool_call>\n<function=computer>\n<parameter=action>\nscroll\n</parameter>\n<parameter=coordinate>\n[500, 400]\n</parameter>\n<parameter=scroll_direction>\ndown\n</parameter>\n<parameter=scroll_amount>\n3\n</parameter>\n</function>\n</tool_call>\n\n===============================\nWRONG FORMATS (DO NOT USE)\n===============================\n❌ Missing <tool_call>...</tool_call> wrapper\n❌ <function=function='screenshot'> (no quotes or extra text)\n❌ <parameter=parameters> (must use actual parameter name)\n❌ JSON format like {\"action\": \"screenshot\"}\n❌ Any format other than the exact XML-like format above\n\n===============================\nEXECUTION RULES\n===============================\n- Use ONLY the tool names listed above\n- Include ALL required parameters for each tool\n- If you don't know a parameter value, ask the user\n- After calling a function, STOP and wait for the result\n- Do NOT add extra text before or after tool calls`;\n\n const inputExamplesText = renderUiTarsInputExamples(tools);\n if (inputExamplesText.length === 0) {\n return basePrompt;\n }\n\n return `${basePrompt}\\n\\n${inputExamplesText}`;\n },\n});\n\nfunction renderUiTarsInputExamples(\n tools: LanguageModelV3FunctionTool[]\n): string {\n return renderInputExamplesSection({\n tools,\n renderExample: renderUiTarsInputExample,\n });\n}\n\nfunction renderUiTarsInputExample(toolName: string, input: unknown): string {\n const parameterBlocks = renderUiTarsParameters(input);\n return `<tool_call>\\n<function=${escapeXmlMinimalAttr(toolName, '\"')}>${parameterBlocks}\\n</function>\\n</tool_call>`;\n}\n\nfunction renderUiTarsParameters(input: unknown): string {\n if (isRecord(input)) {\n const lines = Object.entries(input).map(([key, value]) => {\n const content = renderUiTarsParameterValue(value);\n return `<parameter=${escapeXmlMinimalAttr(key, '\"')}>\\n${content}\\n</parameter>`;\n });\n\n if (lines.length > 0) {\n return `\\n${lines.join(\"\\n\")}`;\n }\n }\n\n const fallback = renderUiTarsParameterValue(input);\n return `\\n<parameter=input>\\n${fallback}\\n</parameter>`;\n}\n\nfunction renderUiTarsParameterValue(value: unknown): string {\n if (typeof value === \"string\") {\n return escapeXmlMinimalText(value);\n }\n\n const serialized = safeStringifyInputExample(value);\n return escapeXmlMinimalText(serialized);\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAcA,SAAS,kBAAkB,OAAyB;AAClD,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,WAAO,MAAM,KAAK,CAAC,UAAU,kBAAkB,KAAK,CAAC;AAAA,EACvD;AAEA,MAAI,SAAS,OAAO,UAAU,UAAU;AACtC,WAAO,OAAO,QAAQ,KAAgC,EAAE;AAAA,MACtD,CAAC,CAAC,KAAK,MAAM,MAAM,CAAC,kBAAkB,GAAG,KAAK,kBAAkB,MAAM;AAAA,IACxE;AAAA,EACF;AAEA,SAAO;AACT;AAEO,IAAM,oCAAoC,qBAAqB;AAAA,EACpE,UAAU;AAAA,EACV,4BAA4B;AAAA,EAC5B,yBAAyB,OAAsC;AAC7D,UAAM,YAAY,KAAK,UAAU,KAAK;AACtC,UAAM,aAAa;AAAA;AAAA,MAEjB,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkDX,UAAM,oBAAoB,4BAA4B,KAAK;AAC3D,QAAI,kBAAkB,WAAW,GAAG;AAClC,aAA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,QAAQ,OAAO,QAAQ,KAAK,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AACxD,YAAM,UAAU,2BAA2B,KAAK;AAChD,aAAO,cAAc,qBAAqB,KAAK,GAAG,CAAC;AAAA,EAAM,OAAO;AAAA;AAAA,IAClE,CAAC;AAED,QAAI,MAAM,SAAS,GAAG;AACpB,aAAO;AAAA,EAAK,MAAM,KAAK,IAAI,CAAC;AAAA,IAC9B;AAAA,EACF;AAEA,QAAM,WAAW,2BAA2B,KAAK;AACjD,SAAO;AAAA;AAAA,EAAwB,QAAQ;AAAA;AACzC;AAEA,SAAS,2BAA2B,OAAwB;AAC1D,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO,qBAAqB,KAAK;AAAA,EACnC;AAEA,QAAM,aAAa,0BAA0B,KAAK;AAClD,SAAO,qBAAqB,UAAU;AACxC;AAEA,SAAS,SAAS,OAAkD;AAClE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC5E;","names":[]}
package/dist/index.cjs CHANGED
@@ -9496,6 +9496,65 @@ async function wrapGenerate({
9496
9496
  };
9497
9497
  }
9498
9498
 
9499
+ // src/core/prompts/shared/input-examples.ts
9500
+ function getToolInputExamples(tool) {
9501
+ const inputExamples = tool.inputExamples;
9502
+ if (!Array.isArray(inputExamples)) {
9503
+ return [];
9504
+ }
9505
+ return inputExamples.filter(
9506
+ (example) => typeof example === "object" && example !== null && "input" in example && example.input !== void 0
9507
+ );
9508
+ }
9509
+ function safeStringifyInputExample(input, sourceError) {
9510
+ try {
9511
+ const serialized = JSON.stringify(input);
9512
+ return serialized != null ? serialized : "null";
9513
+ } catch (stringifyError) {
9514
+ let reason = "";
9515
+ if (sourceError instanceof Error) {
9516
+ reason = sourceError.message;
9517
+ } else if (stringifyError instanceof Error) {
9518
+ reason = stringifyError.message;
9519
+ }
9520
+ return reason.length > 0 ? `[unserializable input: ${reason}]` : "[unserializable input]";
9521
+ }
9522
+ }
9523
+ function stringifyInputExampleAsJsonLiteral(input) {
9524
+ try {
9525
+ const serialized = JSON.stringify(input);
9526
+ return serialized != null ? serialized : "null";
9527
+ } catch (error) {
9528
+ const fallbackText = safeStringifyInputExample(input, error);
9529
+ return JSON.stringify(fallbackText);
9530
+ }
9531
+ }
9532
+ var INPUT_EXAMPLES_SECTION_HEADER = [
9533
+ "# Input Examples",
9534
+ "Treat these as canonical tool-call patterns.",
9535
+ "Reuse the closest structure and nesting, change only values, and do not invent parameters.",
9536
+ "Do not copy example values unless they match the user's request."
9537
+ ];
9538
+ function renderInputExamplesSection(options) {
9539
+ const renderedTools = options.tools.map((tool) => {
9540
+ const inputExamples = getToolInputExamples(tool);
9541
+ if (inputExamples.length === 0) {
9542
+ return "";
9543
+ }
9544
+ const renderedExamples = inputExamples.map((example, index) => {
9545
+ const rendered = options.renderExample(tool.name, example.input);
9546
+ return `Example ${index + 1}:
9547
+ ${rendered}`;
9548
+ }).join("\n\n");
9549
+ return `Tool: ${tool.name}
9550
+ ${renderedExamples}`;
9551
+ }).filter((text) => text.length > 0).join("\n\n");
9552
+ if (renderedTools.length === 0) {
9553
+ return "";
9554
+ }
9555
+ return [...INPUT_EXAMPLES_SECTION_HEADER, renderedTools].join("\n\n");
9556
+ }
9557
+
9499
9558
  // src/core/prompts/shared/text-part.ts
9500
9559
  function toTextPart(text, providerOptions) {
9501
9560
  if (providerOptions === void 0) {
@@ -9736,11 +9795,28 @@ function renderToolDefinition(tool) {
9736
9795
  }
9737
9796
  function hermesSystemPromptTemplate(tools) {
9738
9797
  const toolsRendered = tools.map(renderToolDefinition).join("\n");
9739
- 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>
9798
+ 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>
9740
9799
  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"}
9741
9800
  For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
9742
9801
  <tool_call>
9743
9802
  {"name": "<function-name>", "arguments": <args-dict>}
9803
+ </tool_call>`;
9804
+ const inputExamplesText = renderInputExamplesSection({
9805
+ tools,
9806
+ renderExample: renderHermesInputExample
9807
+ });
9808
+ if (inputExamplesText.length === 0) {
9809
+ return basePrompt;
9810
+ }
9811
+ return `${basePrompt}
9812
+
9813
+ ${inputExamplesText}`;
9814
+ }
9815
+ function renderHermesInputExample(toolName, input) {
9816
+ const argumentsLiteral = stringifyInputExampleAsJsonLiteral(input);
9817
+ const nameLiteral = JSON.stringify(toolName);
9818
+ return `<tool_call>
9819
+ {"name":${nameLiteral},"arguments":${argumentsLiteral}}
9744
9820
  </tool_call>`;
9745
9821
  }
9746
9822
 
@@ -9799,29 +9875,6 @@ function renderToolForXmlPrompt(tool) {
9799
9875
  lines.push(`schema: ${stringifySchema(normalizedSchema)}`);
9800
9876
  return lines.join("\n");
9801
9877
  }
9802
- function getToolInputExamples(tool) {
9803
- const inputExamples = tool.inputExamples;
9804
- if (!Array.isArray(inputExamples)) {
9805
- return [];
9806
- }
9807
- return inputExamples.filter(
9808
- (example) => typeof example === "object" && example !== null && "input" in example && example.input !== void 0
9809
- );
9810
- }
9811
- function safeStringifyInputExample(input, sourceError) {
9812
- try {
9813
- const serialized = JSON.stringify(input);
9814
- return serialized != null ? serialized : "null";
9815
- } catch (stringifyError) {
9816
- let reason = "";
9817
- if (sourceError instanceof Error) {
9818
- reason = sourceError.message;
9819
- } else if (stringifyError instanceof Error) {
9820
- reason = stringifyError.message;
9821
- }
9822
- return reason.length > 0 ? `[unserializable input: ${reason}]` : "[unserializable input]";
9823
- }
9824
- }
9825
9878
  function renderMorphXmlInputExample(toolName, input) {
9826
9879
  try {
9827
9880
  return stringify2(toolName, input, {
@@ -9836,29 +9889,10 @@ function renderMorphXmlInputExample(toolName, input) {
9836
9889
  }
9837
9890
  }
9838
9891
  function renderInputExamplesForXmlPrompt(tools) {
9839
- const renderedTools = tools.map((tool) => {
9840
- const inputExamples = getToolInputExamples(tool);
9841
- if (inputExamples.length === 0) {
9842
- return "";
9843
- }
9844
- const renderedExamples = inputExamples.map((example, index) => {
9845
- const xml = renderMorphXmlInputExample(tool.name, example.input);
9846
- return `Example ${index + 1}:
9847
- ${xml}`;
9848
- }).join("\n\n");
9849
- return `Tool: ${tool.name}
9850
- ${renderedExamples}`;
9851
- }).filter((text) => text.length > 0).join("\n\n");
9852
- if (renderedTools.length === 0) {
9853
- return "";
9854
- }
9855
- return [
9856
- "# Input Examples",
9857
- "Treat these as canonical tool-call patterns.",
9858
- "Reuse the closest structure and nesting, change only values, and do not invent parameters.",
9859
- "Do not copy example values unless they match the user's request.",
9860
- renderedTools
9861
- ].join("\n\n");
9892
+ return renderInputExamplesSection({
9893
+ tools,
9894
+ renderExample: renderMorphXmlInputExample
9895
+ });
9862
9896
  }
9863
9897
  function normalizeSchema(schema) {
9864
9898
  if (typeof schema === "string") {
@@ -10204,8 +10238,50 @@ function qwen3coderSystemPromptTemplate(tools) {
10204
10238
  }
10205
10239
  out += "\n</tools>";
10206
10240
  out += QWEN3CODER_TOOL_CALL_INSTRUCTIONS;
10241
+ const inputExamplesText = renderInputExamplesSection({
10242
+ tools,
10243
+ renderExample: renderQwen3CoderInputExample
10244
+ });
10245
+ if (inputExamplesText.length > 0) {
10246
+ out += `
10247
+
10248
+ ${inputExamplesText}`;
10249
+ }
10207
10250
  return out;
10208
10251
  }
10252
+ function renderQwen3CoderInputExample(toolName, input) {
10253
+ const parameterBlocks = renderQwen3CoderParameters(input);
10254
+ return `<tool_call>
10255
+ <function=${escapeXmlMinimalAttr(toolName, '"')}>${parameterBlocks}
10256
+ </function>
10257
+ </tool_call>`;
10258
+ }
10259
+ function renderQwen3CoderParameters(input) {
10260
+ if (isMapping2(input)) {
10261
+ const lines = Object.entries(input).map(([key, value]) => {
10262
+ const content = renderQwen3CoderParameterValue(value);
10263
+ return `<parameter=${escapeXmlMinimalAttr(key, '"')}>
10264
+ ${content}
10265
+ </parameter>`;
10266
+ });
10267
+ if (lines.length > 0) {
10268
+ return `
10269
+ ${lines.join("\n")}`;
10270
+ }
10271
+ }
10272
+ const fallback = renderQwen3CoderParameterValue(input);
10273
+ return `
10274
+ <parameter=input>
10275
+ ${fallback}
10276
+ </parameter>`;
10277
+ }
10278
+ function renderQwen3CoderParameterValue(value) {
10279
+ if (typeof value === "string") {
10280
+ return escapeXmlMinimalText(value);
10281
+ }
10282
+ const serialized = safeStringifyInputExample(value);
10283
+ return escapeXmlMinimalText(serialized);
10284
+ }
10209
10285
  function stringifyToolResponseContent(value) {
10210
10286
  if (typeof value === "string") {
10211
10287
  return value;
@@ -10226,9 +10302,25 @@ function formatToolResponseAsQwen3CoderXml(toolResult) {
10226
10302
  return formatToolResponseAsQwen3CoderXmlWithOptions(toolResult);
10227
10303
  }
10228
10304
 
10305
+ // src/core/prompts/yaml-xml-prompt.ts
10306
+ var import_yaml2 = __toESM(require("yaml"), 1);
10307
+
10308
+ // src/core/prompts/shared/xml-tag-name.ts
10309
+ var XML_TAG_NAME_REGEX = /^[A-Za-z_][A-Za-z0-9_.:-]*$/;
10310
+ function isValidXmlTagName(name) {
10311
+ return XML_TAG_NAME_REGEX.test(name);
10312
+ }
10313
+ function toSafeXmlTagName(name) {
10314
+ return isValidXmlTagName(name) ? name : "tool";
10315
+ }
10316
+
10229
10317
  // src/core/prompts/yaml-xml-prompt.ts
10230
10318
  function yamlXmlSystemPromptTemplate(tools, includeMultilineExample = true) {
10231
10319
  const toolsJson = JSON.stringify(tools);
10320
+ const inputExamplesText = renderInputExamplesSection({
10321
+ tools,
10322
+ renderExample: renderYamlXmlInputExample
10323
+ });
10232
10324
  const multilineExample = includeMultilineExample ? `
10233
10325
 
10234
10326
  For multiline values, use YAML's literal block syntax:
@@ -10239,7 +10331,7 @@ contents: |
10239
10331
  Second line
10240
10332
  Third line
10241
10333
  </write_file>` : "";
10242
- return `# Tools
10334
+ const basePrompt = `# Tools
10243
10335
 
10244
10336
  You may call one or more functions to assist with the user query.
10245
10337
 
@@ -10265,6 +10357,26 @@ unit: celsius
10265
10357
  - After calling a tool, you will receive a response. Use this result to answer the user.
10266
10358
  - Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
10267
10359
  - If a task requires multiple function calls, make ALL of them at once.`;
10360
+ if (inputExamplesText.length === 0) {
10361
+ return basePrompt;
10362
+ }
10363
+ return `${basePrompt}
10364
+
10365
+ ${inputExamplesText}`;
10366
+ }
10367
+ function renderYamlXmlInputExample(toolName, input) {
10368
+ const safeToolName = toSafeXmlTagName(toolName);
10369
+ let yamlBody = "null";
10370
+ try {
10371
+ const yaml = import_yaml2.default.stringify(input).trimEnd();
10372
+ yamlBody = yaml.length > 0 ? yaml : "null";
10373
+ } catch (error) {
10374
+ yamlBody = safeStringifyInputExample(input, error);
10375
+ }
10376
+ const escapedYamlBody = escapeXmlMinimalText(yamlBody);
10377
+ return `<${safeToolName}>
10378
+ ${escapedYamlBody}
10379
+ </${safeToolName}>`;
10268
10380
  }
10269
10381
  function formatToolResponseAsYaml(toolResult) {
10270
10382
  return morphFormatToolResponseAsXml(toolResult);