@ai-sdk-tool/parser 3.0.0 → 3.1.1

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.
@@ -346,12 +346,15 @@ function handleOpeningTagSegment(src, lt, out, stack) {
346
346
  }
347
347
  return q + 1;
348
348
  }
349
- function shouldDeduplicateStringTags(schema) {
349
+ function extractSchemaProperties(schema) {
350
350
  const unwrapped = (0, import_rxml.unwrapJsonSchema)(schema);
351
351
  if (!unwrapped || typeof unwrapped !== "object") {
352
- return false;
352
+ return void 0;
353
353
  }
354
- const props = unwrapped.properties;
354
+ return unwrapped.properties;
355
+ }
356
+ function shouldDeduplicateStringTags(schema) {
357
+ const props = extractSchemaProperties(schema);
355
358
  if (!props) {
356
359
  return false;
357
360
  }
@@ -363,21 +366,14 @@ function shouldDeduplicateStringTags(schema) {
363
366
  return (command == null ? void 0 : command.type) === "array";
364
367
  }
365
368
  function getStringPropertyNames(schema) {
366
- const unwrapped = (0, import_rxml.unwrapJsonSchema)(schema);
367
- if (!unwrapped || typeof unwrapped !== "object") {
368
- return [];
369
- }
370
- const props = unwrapped.properties;
369
+ const props = extractSchemaProperties(schema);
371
370
  if (!props) {
372
371
  return [];
373
372
  }
374
373
  const names = [];
375
374
  for (const key of Object.keys(props)) {
376
- const prop = (0, import_rxml.unwrapJsonSchema)(
377
- props[key]
378
- );
379
- const type = prop.type;
380
- if (type === "string") {
375
+ const prop = (0, import_rxml.unwrapJsonSchema)(props[key]);
376
+ if ((prop == null ? void 0 : prop.type) === "string") {
381
377
  names.push(key);
382
378
  }
383
379
  }
@@ -412,11 +408,7 @@ function repairParsedAgainstSchema(input, schema) {
412
408
  if (!input || typeof input !== "object") {
413
409
  return input;
414
410
  }
415
- const unwrapped = (0, import_rxml.unwrapJsonSchema)(schema);
416
- if (!unwrapped || typeof unwrapped !== "object") {
417
- return input;
418
- }
419
- const properties = unwrapped.properties;
411
+ const properties = extractSchemaProperties(schema);
420
412
  if (!properties) {
421
413
  return input;
422
414
  }
@@ -430,14 +422,12 @@ function applySchemaProps(obj, properties) {
430
422
  continue;
431
423
  }
432
424
  const prop = (0, import_rxml.unwrapJsonSchema)(propSchema);
433
- const propType = prop.type;
434
- if (propType === "array" && prop.items) {
435
- const itemSchemaRaw = prop.items;
436
- const itemSchema = (0, import_rxml.unwrapJsonSchema)(itemSchemaRaw);
425
+ if ((prop == null ? void 0 : prop.type) === "array" && prop.items) {
426
+ const itemSchema = (0, import_rxml.unwrapJsonSchema)(prop.items);
437
427
  obj[key] = coerceArrayItems(obj[key], itemSchema);
438
428
  continue;
439
429
  }
440
- if (propType === "object") {
430
+ if ((prop == null ? void 0 : prop.type) === "object") {
441
431
  const val = obj[key];
442
432
  if (val && typeof val === "object") {
443
433
  obj[key] = repairParsedAgainstSchema(val, prop);
@@ -1309,7 +1299,7 @@ function parse2(text, optsOrReviver) {
1309
1299
  return parseWithTransform(text, options);
1310
1300
  }
1311
1301
 
1312
- // src/core/protocols/json-mix-protocol.ts
1302
+ // src/core/protocols/json-protocol.ts
1313
1303
  function processToolCallJson(toolCallJson, fullMatch, processedElements, options) {
1314
1304
  var _a, _b;
1315
1305
  try {
@@ -1369,7 +1359,6 @@ function flushBuffer(state, controller, toolCallStart) {
1369
1359
  controller.enqueue({
1370
1360
  type: "text-delta",
1371
1361
  id: state.currentTextId,
1372
- textDelta: deltaContent,
1373
1362
  delta: deltaContent
1374
1363
  });
1375
1364
  state.buffer = "";
@@ -1402,7 +1391,6 @@ function emitIncompleteToolCall(state, controller, toolCallStart) {
1402
1391
  controller.enqueue({
1403
1392
  type: "text-delta",
1404
1393
  id: errorId,
1405
- textDelta: errorContent,
1406
1394
  delta: errorContent
1407
1395
  });
1408
1396
  controller.enqueue({
@@ -1435,7 +1423,6 @@ function publishText(text, state, controller) {
1435
1423
  controller.enqueue({
1436
1424
  type: "text-delta",
1437
1425
  id: state.currentTextId,
1438
- textDelta: text,
1439
1426
  delta: text
1440
1427
  });
1441
1428
  }
@@ -1468,7 +1455,6 @@ function emitToolCall(context) {
1468
1455
  controller.enqueue({
1469
1456
  type: "text-delta",
1470
1457
  id: errorId,
1471
- textDelta: errorContent,
1472
1458
  delta: errorContent
1473
1459
  });
1474
1460
  controller.enqueue({
@@ -1528,39 +1514,34 @@ function handlePartialTag(state, controller, toolCallStart) {
1528
1514
  state.buffer = "";
1529
1515
  }
1530
1516
  }
1531
- var jsonMixProtocol = ({
1517
+ var jsonProtocol = ({
1532
1518
  toolCallStart = "<tool_call>",
1533
- toolCallEnd = "</tool_call>",
1534
- toolResponseStart = "<tool_response>",
1535
- toolResponseEnd = "</tool_response>"
1519
+ toolCallEnd = "</tool_call>"
1536
1520
  } = {}) => ({
1537
- formatTools({ tools, toolSystemPromptTemplate }) {
1538
- const toolsForPrompt = (tools || []).filter((tool) => tool.type === "function").map((tool) => ({
1539
- name: tool.name,
1540
- description: tool.type === "function" && typeof tool.description === "string" ? tool.description : void 0,
1541
- parameters: tool.inputSchema
1542
- }));
1543
- return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
1521
+ formatTools({
1522
+ tools,
1523
+ toolSystemPromptTemplate
1524
+ }) {
1525
+ return toolSystemPromptTemplate(tools || []);
1544
1526
  },
1545
1527
  formatToolCall(toolCall) {
1546
1528
  let args = {};
1547
- try {
1548
- args = JSON.parse(toolCall.input);
1549
- } catch (e) {
1550
- args = toolCall.input;
1529
+ if (toolCall.input != null) {
1530
+ try {
1531
+ args = JSON.parse(toolCall.input);
1532
+ } catch (e) {
1533
+ args = toolCall.input;
1534
+ }
1551
1535
  }
1552
1536
  return `${toolCallStart}${JSON.stringify({
1553
1537
  name: toolCall.toolName,
1554
1538
  arguments: args
1555
1539
  })}${toolCallEnd}`;
1556
1540
  },
1557
- formatToolResponse(toolResult) {
1558
- return `${toolResponseStart}${JSON.stringify({
1559
- toolName: toolResult.toolName,
1560
- result: toolResult.result
1561
- })}${toolResponseEnd}`;
1562
- },
1563
- parseGeneratedText({ text, options }) {
1541
+ parseGeneratedText({
1542
+ text,
1543
+ options
1544
+ }) {
1564
1545
  const startEsc = escapeRegExp2(toolCallStart);
1565
1546
  const endEsc = escapeRegExp2(toolCallEnd);
1566
1547
  const toolCallRegex = new RegExp(
@@ -1586,7 +1567,9 @@ var jsonMixProtocol = ({
1586
1567
  }
1587
1568
  return processedElements;
1588
1569
  },
1589
- createStreamParser({ options }) {
1570
+ createStreamParser({
1571
+ options
1572
+ }) {
1590
1573
  const state = {
1591
1574
  isInsideToolCall: false,
1592
1575
  buffer: "",
@@ -1596,7 +1579,7 @@ var jsonMixProtocol = ({
1596
1579
  };
1597
1580
  return new TransformStream({
1598
1581
  transform(chunk, controller) {
1599
- var _a, _b;
1582
+ var _a;
1600
1583
  if (chunk.type === "finish") {
1601
1584
  handleFinishChunk(state, controller, toolCallStart, chunk);
1602
1585
  return;
@@ -1605,7 +1588,7 @@ var jsonMixProtocol = ({
1605
1588
  controller.enqueue(chunk);
1606
1589
  return;
1607
1590
  }
1608
- const textContent = (_b = (_a = chunk.textDelta) != null ? _a : chunk.delta) != null ? _b : "";
1591
+ const textContent = (_a = chunk.delta) != null ? _a : "";
1609
1592
  state.buffer += textContent;
1610
1593
  processBufferTags({
1611
1594
  state,
@@ -1632,7 +1615,12 @@ var jsonMixProtocol = ({
1632
1615
  }
1633
1616
  });
1634
1617
 
1635
- // src/core/protocols/morph-xml-protocol.ts
1618
+ // src/core/protocols/protocol-interface.ts
1619
+ function isTCMProtocolFactory(protocol) {
1620
+ return typeof protocol === "function";
1621
+ }
1622
+
1623
+ // src/core/protocols/xml-protocol.ts
1636
1624
  var import_rxml2 = require("@ai-sdk-tool/rxml");
1637
1625
  var defaultPipelineConfig2 = defaultPipelineConfig;
1638
1626
  var NAME_CHAR_RE2 = /[A-Za-z0-9_:-]/;
@@ -1967,7 +1955,6 @@ function createFlushTextHandler(getCurrentTextId, setCurrentTextId, getHasEmitte
1967
1955
  controller.enqueue({
1968
1956
  type: "text-delta",
1969
1957
  id: getCurrentTextId(),
1970
- textDelta: content,
1971
1958
  delta: content
1972
1959
  });
1973
1960
  }
@@ -2129,7 +2116,7 @@ function createProcessBufferHandler(getBuffer, setBuffer, getCurrentToolCall, se
2129
2116
  }
2130
2117
  };
2131
2118
  }
2132
- var morphXmlProtocol = (protocolOptions) => {
2119
+ var xmlProtocol = (protocolOptions) => {
2133
2120
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2134
2121
  let pipelineConfig = protocolOptions == null ? void 0 : protocolOptions.pipeline;
2135
2122
  const maxReparses = protocolOptions == null ? void 0 : protocolOptions.maxReparses;
@@ -2169,40 +2156,22 @@ var morphXmlProtocol = (protocolOptions) => {
2169
2156
  }
2170
2157
  return {
2171
2158
  formatTools({ tools, toolSystemPromptTemplate }) {
2172
- const toolsForPrompt = (tools || []).map((tool) => ({
2173
- name: tool.name,
2174
- description: tool.description,
2175
- parameters: (0, import_rxml2.unwrapJsonSchema)(tool.inputSchema)
2176
- }));
2177
- return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
2159
+ return toolSystemPromptTemplate(tools || []);
2178
2160
  },
2179
2161
  formatToolCall(toolCall) {
2180
2162
  let args = {};
2181
- try {
2182
- args = JSON.parse(toolCall.input);
2183
- } catch (e) {
2184
- args = toolCall.input;
2163
+ if (toolCall.input != null) {
2164
+ try {
2165
+ args = JSON.parse(toolCall.input);
2166
+ } catch (e) {
2167
+ args = toolCall.input;
2168
+ }
2185
2169
  }
2186
2170
  return (0, import_rxml2.stringify)(toolCall.toolName, args, {
2187
2171
  suppressEmptyNode: false,
2188
2172
  format: false
2189
2173
  });
2190
2174
  },
2191
- formatToolResponse(toolResult) {
2192
- let result = toolResult.result;
2193
- if (result && typeof result === "object" && "type" in result && result.type === "json" && "value" in result) {
2194
- result = result.value;
2195
- }
2196
- const xml = (0, import_rxml2.stringify)(
2197
- "tool_response",
2198
- {
2199
- tool_name: toolResult.toolName,
2200
- result
2201
- },
2202
- { declaration: false }
2203
- );
2204
- return xml;
2205
- },
2206
2175
  parseGeneratedText({ text, tools, options }) {
2207
2176
  const toolNames = tools.map((t) => t.name).filter(Boolean);
2208
2177
  if (toolNames.length === 0) {
@@ -2271,7 +2240,7 @@ var morphXmlProtocol = (protocolOptions) => {
2271
2240
  );
2272
2241
  return new TransformStream({
2273
2242
  transform(chunk, controller) {
2274
- var _a2, _b2;
2243
+ var _a2;
2275
2244
  if (chunk.type !== "text-delta") {
2276
2245
  if (buffer) {
2277
2246
  flushText(controller, buffer);
@@ -2280,7 +2249,7 @@ var morphXmlProtocol = (protocolOptions) => {
2280
2249
  controller.enqueue(chunk);
2281
2250
  return;
2282
2251
  }
2283
- const textContent = (_b2 = (_a2 = chunk.textDelta) != null ? _a2 : chunk.delta) != null ? _b2 : "";
2252
+ const textContent = (_a2 = chunk.delta) != null ? _a2 : "";
2284
2253
  buffer += textContent;
2285
2254
  processBuffer(controller);
2286
2255
  },
@@ -2315,13 +2284,7 @@ var morphXmlProtocol = (protocolOptions) => {
2315
2284
  };
2316
2285
  };
2317
2286
 
2318
- // src/core/protocols/tool-call-protocol.ts
2319
- function isProtocolFactory(protocol) {
2320
- return typeof protocol === "function";
2321
- }
2322
-
2323
- // src/core/protocols/yaml-xml-protocol.ts
2324
- var import_rxml3 = require("@ai-sdk-tool/rxml");
2287
+ // src/core/protocols/yaml-protocol.ts
2325
2288
  var import_yaml = __toESM(require("yaml"), 1);
2326
2289
  var NAME_CHAR_RE3 = /[A-Za-z0-9_:-]/;
2327
2290
  var WHITESPACE_REGEX4 = /\s/;
@@ -2542,7 +2505,6 @@ function createFlushTextHandler2(getCurrentTextId, setCurrentTextId, getHasEmitt
2542
2505
  controller.enqueue({
2543
2506
  type: "text-delta",
2544
2507
  id: getCurrentTextId(),
2545
- textDelta: content,
2546
2508
  delta: content
2547
2509
  });
2548
2510
  }
@@ -2590,42 +2552,24 @@ function findEarliestToolTag2(buffer, toolNames) {
2590
2552
  tagLength: bestTagLength
2591
2553
  };
2592
2554
  }
2593
- var yamlXmlProtocol = (_protocolOptions) => {
2555
+ var yamlProtocol = (_protocolOptions) => {
2594
2556
  return {
2595
2557
  formatTools({ tools, toolSystemPromptTemplate }) {
2596
- const toolsForPrompt = (tools || []).map((tool) => ({
2597
- name: tool.name,
2598
- description: tool.description,
2599
- parameters: (0, import_rxml3.unwrapJsonSchema)(tool.inputSchema)
2600
- }));
2601
- return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
2558
+ return toolSystemPromptTemplate(tools || []);
2602
2559
  },
2603
2560
  formatToolCall(toolCall) {
2604
2561
  let args = {};
2605
- try {
2606
- args = JSON.parse(toolCall.input);
2607
- } catch (e) {
2608
- args = { value: toolCall.input };
2562
+ if (toolCall.input != null) {
2563
+ try {
2564
+ args = JSON.parse(toolCall.input);
2565
+ } catch (e) {
2566
+ args = { value: toolCall.input };
2567
+ }
2609
2568
  }
2610
2569
  const yamlContent = import_yaml.default.stringify(args);
2611
2570
  return `<${toolCall.toolName}>
2612
2571
  ${yamlContent}</${toolCall.toolName}>`;
2613
2572
  },
2614
- formatToolResponse(toolResult) {
2615
- let result = toolResult.result;
2616
- if (result && typeof result === "object" && "type" in result && result.type === "json" && "value" in result) {
2617
- result = result.value;
2618
- }
2619
- const xml = (0, import_rxml3.stringify)(
2620
- "tool_response",
2621
- {
2622
- tool_name: toolResult.toolName,
2623
- result
2624
- },
2625
- { declaration: false }
2626
- );
2627
- return xml;
2628
- },
2629
2573
  parseGeneratedText({ text, tools, options }) {
2630
2574
  const toolNames = tools.map((t) => t.name).filter(Boolean);
2631
2575
  if (toolNames.length === 0) {
@@ -2739,7 +2683,7 @@ ${yamlContent}</${toolCall.toolName}>`;
2739
2683
  };
2740
2684
  return new TransformStream({
2741
2685
  transform(chunk, controller) {
2742
- var _a, _b;
2686
+ var _a;
2743
2687
  if (chunk.type !== "text-delta") {
2744
2688
  if (buffer) {
2745
2689
  flushText(controller, buffer);
@@ -2748,7 +2692,7 @@ ${yamlContent}</${toolCall.toolName}>`;
2748
2692
  controller.enqueue(chunk);
2749
2693
  return;
2750
2694
  }
2751
- const textContent = (_b = (_a = chunk.textDelta) != null ? _a : chunk.delta) != null ? _b : "";
2695
+ const textContent = (_a = chunk.delta) != null ? _a : "";
2752
2696
  buffer += textContent;
2753
2697
  processBuffer(controller);
2754
2698
  },
@@ -2784,44 +2728,6 @@ ${yamlContent}</${toolCall.toolName}>`;
2784
2728
  }
2785
2729
  };
2786
2730
  };
2787
- function orchestratorSystemPromptTemplate(tools, includeMultilineExample = true) {
2788
- const multilineExample = includeMultilineExample ? `
2789
-
2790
- For multiline values, use YAML's literal block syntax:
2791
- <write_file>
2792
- file_path: /tmp/example.txt
2793
- contents: |
2794
- First line
2795
- Second line
2796
- Third line
2797
- </write_file>` : "";
2798
- return `# Tools
2799
-
2800
- You may call one or more functions to assist with the user query.
2801
-
2802
- You are provided with function signatures within <tools></tools> XML tags:
2803
- <tools>${tools}</tools>
2804
-
2805
- # Format
2806
-
2807
- Use exactly one XML element whose tag name is the function name.
2808
- Inside the XML element, specify parameters using YAML syntax (key: value pairs).
2809
-
2810
- # Example
2811
- <get_weather>
2812
- location: New York
2813
- unit: celsius
2814
- </get_weather>${multilineExample}
2815
-
2816
- # Rules
2817
- - Parameter names and values must follow the schema exactly.
2818
- - Use proper YAML syntax for values (strings, numbers, booleans, arrays, objects).
2819
- - Each required parameter must appear once.
2820
- - Do not add functions or parameters not in the schema.
2821
- - After calling a tool, you will receive a response. Use this result to answer the user.
2822
- - Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
2823
- - If a task requires multiple function calls, make ALL of them at once.`;
2824
- }
2825
2731
 
2826
2732
  // src/core/utils/dynamic-tool-schema.ts
2827
2733
  function createDynamicIfThenElseSchema(tools) {
@@ -2919,15 +2825,9 @@ function isToolChoiceActive(params) {
2919
2825
  return !!(typeof params.providerOptions === "object" && params.providerOptions !== null && typeof ((_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware) === "object" && toolChoice && typeof toolChoice === "object" && (toolChoice.type === "tool" || toolChoice.type === "required"));
2920
2826
  }
2921
2827
 
2922
- // src/core/utils/type-guards.ts
2923
- function isToolCallContent(content) {
2924
- return content.type === "tool-call" && typeof content.toolName === "string" && // input may be a JSON string or an already-parsed object depending on provider/runtime
2925
- (typeof content.input === "string" || typeof content.input === "object");
2926
- }
2927
-
2928
- // src/v6/generate-handler.ts
2828
+ // src/generate-handler.ts
2929
2829
  var import_provider_utils = require("@ai-sdk/provider-utils");
2930
- var import_rxml4 = require("@ai-sdk-tool/rxml");
2830
+ var import_rxml3 = require("@ai-sdk-tool/rxml");
2931
2831
  function parseToolChoiceJson(text, providerOptions) {
2932
2832
  var _a;
2933
2833
  try {
@@ -3078,92 +2978,218 @@ function fixToolCallWithSchema(part, tools) {
3078
2978
  if (part.type !== "tool-call") {
3079
2979
  return part;
3080
2980
  }
3081
- const tc = part;
3082
2981
  let args = {};
3083
- if (typeof tc.input === "string") {
2982
+ if (typeof part.input === "string") {
3084
2983
  try {
3085
- args = JSON.parse(tc.input);
2984
+ args = JSON.parse(part.input);
3086
2985
  } catch (e) {
3087
2986
  return part;
3088
2987
  }
3089
- } else if (tc.input && typeof tc.input === "object") {
3090
- args = tc.input;
2988
+ } else if (part.input && typeof part.input === "object") {
2989
+ args = part.input;
3091
2990
  }
3092
- const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
3093
- const coerced = (0, import_rxml4.coerceBySchema)(args, schema);
2991
+ const schema = (_a = tools.find((t) => t.name === part.toolName)) == null ? void 0 : _a.inputSchema;
2992
+ const coerced = (0, import_rxml3.coerceBySchema)(args, schema);
3094
2993
  return {
3095
2994
  ...part,
3096
2995
  input: JSON.stringify(coerced != null ? coerced : {})
3097
2996
  };
3098
2997
  }
3099
2998
 
3100
- // src/v6/stream-handler.ts
3101
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
3102
- function mapCorePartToV3(part) {
3103
- switch (part.type) {
3104
- case "text-delta":
3105
- return {
3106
- type: "text-delta",
3107
- id: part.id || (0, import_provider_utils2.generateId)(),
3108
- delta: part.textDelta
3109
- };
3110
- case "tool-call":
3111
- return {
3112
- type: "tool-call",
3113
- toolCallId: part.toolCallId,
3114
- toolName: part.toolName,
3115
- input: part.input
3116
- };
3117
- case "tool-call-delta":
3118
- return {
3119
- type: "tool-call-delta",
3120
- toolCallId: part.toolCallId,
3121
- toolName: part.toolName,
3122
- argsTextDelta: part.argsTextDelta
3123
- };
3124
- case "finish":
3125
- return {
3126
- type: "finish",
3127
- finishReason: part.finishReason,
3128
- usage: part.usage
3129
- };
3130
- case "error":
3131
- return {
3132
- type: "error",
3133
- error: part.error
3134
- };
3135
- default:
3136
- return part;
2999
+ // src/core/prompts/hermes-system-prompt.ts
3000
+ function hermesSystemPromptTemplate(tools) {
3001
+ const toolsJson = JSON.stringify(tools);
3002
+ return `You are a function calling AI model.
3003
+ You are provided with function signatures within <tools></tools> XML tags.
3004
+ You may call one or more functions to assist with the user query.
3005
+ Don't make assumptions about what values to plug into functions.
3006
+ Here are the available tools: <tools>${toolsJson}</tools>
3007
+ Use the following pydantic model json schema for each tool call you will make: {"title": "FunctionCall", "type": "object", "properties": {"arguments": {"title": "Arguments", "type": "object"}, "name": {"title": "Name", "type": "string"}}, "required": ["arguments", "name"]}
3008
+ For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
3009
+ <tool_call>
3010
+ {"name": "<function-name>", "arguments": <args-dict>}
3011
+ </tool_call>`;
3012
+ }
3013
+
3014
+ // src/core/prompts/tool-response.ts
3015
+ function unwrapToolResult(result) {
3016
+ var _a, _b;
3017
+ switch (result.type) {
3018
+ case "text":
3019
+ return (_a = result.value) != null ? _a : "";
3020
+ case "json":
3021
+ return result.value;
3022
+ case "execution-denied": {
3023
+ const reason = result.reason;
3024
+ return reason ? `[Execution Denied: ${reason}]` : "[Execution Denied]";
3025
+ }
3026
+ case "error-text":
3027
+ return `[Error: ${(_b = result.value) != null ? _b : ""}]`;
3028
+ case "error-json":
3029
+ return `[Error: ${JSON.stringify(result.value)}]`;
3030
+ case "content": {
3031
+ return result.value.map((part) => {
3032
+ var _a2;
3033
+ const contentPart = part;
3034
+ switch (contentPart.type) {
3035
+ case "text":
3036
+ return (_a2 = contentPart.text) != null ? _a2 : "";
3037
+ case "image-data":
3038
+ return `[Image: ${contentPart.mediaType}]`;
3039
+ case "image-url":
3040
+ return `[Image URL: ${contentPart.url}]`;
3041
+ case "image-file-id": {
3042
+ const fileId = contentPart.fileId;
3043
+ const displayId = typeof fileId === "string" ? fileId : JSON.stringify(fileId);
3044
+ return `[Image ID: ${displayId}]`;
3045
+ }
3046
+ case "file-data": {
3047
+ const filePart = contentPart;
3048
+ if (filePart.filename) {
3049
+ return `[File: ${filePart.filename} (${filePart.mediaType})]`;
3050
+ }
3051
+ return `[File: ${filePart.mediaType}]`;
3052
+ }
3053
+ case "file-url":
3054
+ return `[File URL: ${contentPart.url}]`;
3055
+ case "file-id": {
3056
+ const fileId = contentPart.fileId;
3057
+ const displayId = typeof fileId === "string" ? fileId : JSON.stringify(fileId);
3058
+ return `[File ID: ${displayId}]`;
3059
+ }
3060
+ case "media":
3061
+ return `[Media: ${contentPart.mediaType}]`;
3062
+ case "custom":
3063
+ return "[Custom content]";
3064
+ default:
3065
+ return "[Unknown content]";
3066
+ }
3067
+ }).join("\n");
3068
+ }
3069
+ default: {
3070
+ const _exhaustive = result;
3071
+ return _exhaustive;
3072
+ }
3137
3073
  }
3138
3074
  }
3139
- function mapV3PartToCore(part) {
3140
- const p = part;
3141
- switch (p.type) {
3142
- case "text-delta":
3143
- return {
3144
- type: "text-delta",
3145
- id: p.id,
3146
- textDelta: p.delta || p.textDelta || ""
3147
- };
3148
- case "tool-call":
3149
- return {
3150
- type: "tool-call",
3151
- toolCallId: p.toolCallId,
3152
- toolName: p.toolName,
3153
- input: p.input
3154
- };
3155
- case "finish": {
3156
- const finishReason = p.finishReason;
3157
- return {
3158
- type: "finish",
3159
- finishReason: (typeof finishReason === "object" ? finishReason == null ? void 0 : finishReason.unified : finishReason) || "stop",
3160
- usage: p.usage
3161
- };
3075
+ function formatToolResponseAsJsonInXml(toolResult) {
3076
+ const unwrappedResult = unwrapToolResult(toolResult.output);
3077
+ return `<tool_response>${JSON.stringify({
3078
+ toolName: toolResult.toolName,
3079
+ result: unwrappedResult
3080
+ })}</tool_response>`;
3081
+ }
3082
+ function formatToolResponseAsXml(toolResult) {
3083
+ const unwrappedResult = unwrapToolResult(toolResult.output);
3084
+ const toolNameXml = `<tool_name>${toolResult.toolName}</tool_name>`;
3085
+ const resultLines = formatXmlNode("result", unwrappedResult, 1);
3086
+ return [
3087
+ "<tool_response>",
3088
+ ` ${toolNameXml}`,
3089
+ ...resultLines,
3090
+ "</tool_response>"
3091
+ ].join("\n");
3092
+ }
3093
+ function formatXmlNode(tagName, value, depth) {
3094
+ const indent = " ".repeat(depth);
3095
+ if (value === null || value === void 0) {
3096
+ return [`${indent}<${tagName}></${tagName}>`];
3097
+ }
3098
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
3099
+ return [`${indent}<${tagName}>${String(value)}</${tagName}>`];
3100
+ }
3101
+ if (Array.isArray(value)) {
3102
+ if (value.length === 0) {
3103
+ return [`${indent}<${tagName}></${tagName}>`];
3162
3104
  }
3163
- default:
3164
- return p;
3105
+ const lines2 = [`${indent}<${tagName}>`];
3106
+ for (const item of value) {
3107
+ lines2.push(...formatXmlNode("item", item, depth + 1));
3108
+ }
3109
+ lines2.push(`${indent}</${tagName}>`);
3110
+ return lines2;
3111
+ }
3112
+ const entries = Object.entries(value);
3113
+ if (entries.length === 0) {
3114
+ return [`${indent}<${tagName}></${tagName}>`];
3115
+ }
3116
+ const lines = [`${indent}<${tagName}>`];
3117
+ for (const [key, entryValue] of entries) {
3118
+ lines.push(...formatXmlNode(key, entryValue, depth + 1));
3165
3119
  }
3120
+ lines.push(`${indent}</${tagName}>`);
3121
+ return lines;
3122
+ }
3123
+
3124
+ // src/core/prompts/xml-system-prompt.ts
3125
+ function xmlSystemPromptTemplate(tools) {
3126
+ const toolsJson = JSON.stringify(tools);
3127
+ return `# Tools
3128
+
3129
+ You may call one or more functions to assist with the user query.
3130
+
3131
+ You are provided with function signatures within <tools></tools> XML tags:
3132
+ <tools>${toolsJson}</tools>
3133
+
3134
+ # Rules
3135
+ - Use exactly one XML element whose tag name is the function name.
3136
+ - Put each parameter as a child element.
3137
+ - Values must follow the schema exactly (numbers, arrays, objects, enums \u2192 copy as-is).
3138
+ - Do not add or remove functions or parameters.
3139
+ - Each required parameter must appear once.
3140
+ - Output nothing before or after the function call.
3141
+ - After calling a tool, you will receive a response in the format: <tool_response><tool_name>NAME</tool_name><result>RESULT</result></tool_response>. Use this result to answer the user.
3142
+
3143
+ # Example
3144
+ <get_weather>
3145
+ <location>New York</location>
3146
+ <unit>celsius</unit>
3147
+ </get_weather>`;
3148
+ }
3149
+
3150
+ // src/core/prompts/yaml-system-prompt.ts
3151
+ function yamlSystemPromptTemplate(tools, includeMultilineExample = true) {
3152
+ const toolsJson = JSON.stringify(tools);
3153
+ const multilineExample = includeMultilineExample ? `
3154
+
3155
+ For multiline values, use YAML's literal block syntax:
3156
+ <write_file>
3157
+ file_path: /tmp/example.txt
3158
+ contents: |
3159
+ First line
3160
+ Second line
3161
+ Third line
3162
+ </write_file>` : "";
3163
+ return `# Tools
3164
+
3165
+ You may call one or more functions to assist with the user query.
3166
+
3167
+ You are provided with function signatures within <tools></tools> XML tags:
3168
+ <tools>${toolsJson}</tools>
3169
+
3170
+ # Format
3171
+
3172
+ Use exactly one XML element whose tag name is the function name.
3173
+ Inside the XML element, specify parameters using YAML syntax (key: value pairs).
3174
+
3175
+ # Example
3176
+ <get_weather>
3177
+ location: New York
3178
+ unit: celsius
3179
+ </get_weather>${multilineExample}
3180
+
3181
+ # Rules
3182
+ - Parameter names and values must follow the schema exactly.
3183
+ - Use proper YAML syntax for values (strings, numbers, booleans, arrays, objects).
3184
+ - Each required parameter must appear once.
3185
+ - Do not add functions or parameters not in the schema.
3186
+ - After calling a tool, you will receive a response. Use this result to answer the user.
3187
+ - Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
3188
+ - If a task requires multiple function calls, make ALL of them at once.`;
3166
3189
  }
3190
+
3191
+ // src/stream-handler.ts
3192
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
3167
3193
  async function wrapStream({
3168
3194
  protocol,
3169
3195
  doStream,
@@ -3187,23 +3213,24 @@ async function wrapStream({
3187
3213
  ...((_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware) || {}
3188
3214
  };
3189
3215
  const coreStream = stream.pipeThrough(
3190
- new TransformStream({
3191
- transform(part, controller) {
3192
- if (debugLevel === "stream") {
3193
- logRawChunk(part);
3216
+ new TransformStream(
3217
+ {
3218
+ transform(part, controller) {
3219
+ if (debugLevel === "stream") {
3220
+ logRawChunk(part);
3221
+ }
3222
+ controller.enqueue(part);
3194
3223
  }
3195
- controller.enqueue(mapV3PartToCore(part));
3196
3224
  }
3197
- })
3225
+ )
3198
3226
  ).pipeThrough(protocol.createStreamParser({ tools, options }));
3199
3227
  const v3Stream = coreStream.pipeThrough(
3200
3228
  new TransformStream({
3201
3229
  transform(part, controller) {
3202
- const v3Part = mapCorePartToV3(part);
3203
3230
  if (debugLevel === "stream") {
3204
- logParsedChunk(v3Part);
3231
+ logParsedChunk(part);
3205
3232
  }
3206
- controller.enqueue(v3Part);
3233
+ controller.enqueue(part);
3207
3234
  }
3208
3235
  })
3209
3236
  );
@@ -3260,7 +3287,7 @@ async function toolChoiceStream({
3260
3287
  };
3261
3288
  }
3262
3289
 
3263
- // src/v6/transform-handler.ts
3290
+ // src/transform-handler.ts
3264
3291
  function buildFinalPrompt(systemPrompt, processedPrompt, placement) {
3265
3292
  const systemIndex = processedPrompt.findIndex((m) => m.role === "system");
3266
3293
  if (systemIndex !== -1) {
@@ -3403,10 +3430,11 @@ function transformParams({
3403
3430
  params,
3404
3431
  protocol,
3405
3432
  toolSystemPromptTemplate,
3433
+ toolResponsePromptTemplate,
3406
3434
  placement = "first"
3407
3435
  }) {
3408
- var _a, _b, _c, _d, _e;
3409
- const resolvedProtocol = isProtocolFactory(protocol) ? protocol() : protocol;
3436
+ var _a, _b, _c, _d;
3437
+ const resolvedProtocol = isTCMProtocolFactory(protocol) ? protocol() : protocol;
3410
3438
  const functionTools = ((_a = params.tools) != null ? _a : []).filter(
3411
3439
  (t) => t.type === "function"
3412
3440
  );
@@ -3414,9 +3442,18 @@ function transformParams({
3414
3442
  tools: functionTools,
3415
3443
  toolSystemPromptTemplate
3416
3444
  });
3445
+ let normalizedPrompt;
3446
+ if (Array.isArray(params.prompt)) {
3447
+ normalizedPrompt = params.prompt;
3448
+ } else if (params.prompt) {
3449
+ normalizedPrompt = [params.prompt];
3450
+ } else {
3451
+ normalizedPrompt = [];
3452
+ }
3417
3453
  const processedPrompt = convertToolPrompt(
3418
- (_b = params.prompt) != null ? _b : [],
3454
+ normalizedPrompt,
3419
3455
  resolvedProtocol,
3456
+ toolResponsePromptTemplate,
3420
3457
  extractOnErrorOption(params.providerOptions)
3421
3458
  );
3422
3459
  const finalPrompt = buildFinalPrompt(
@@ -3429,15 +3466,15 @@ function transformParams({
3429
3466
  finalPrompt,
3430
3467
  functionTools
3431
3468
  );
3432
- if (((_c = params.toolChoice) == null ? void 0 : _c.type) === "none") {
3469
+ if (((_b = params.toolChoice) == null ? void 0 : _b.type) === "none") {
3433
3470
  throw new Error(
3434
3471
  "The 'none' toolChoice type is not supported by this middleware. Please use 'auto', 'required', or specify a tool name."
3435
3472
  );
3436
3473
  }
3437
- if (((_d = params.toolChoice) == null ? void 0 : _d.type) === "tool") {
3474
+ if (((_c = params.toolChoice) == null ? void 0 : _c.type) === "tool") {
3438
3475
  return handleToolChoiceTool(params, baseReturnParams);
3439
3476
  }
3440
- if (((_e = params.toolChoice) == null ? void 0 : _e.type) === "required") {
3477
+ if (((_d = params.toolChoice) == null ? void 0 : _d.type) === "required") {
3441
3478
  return handleToolChoiceRequired(params, baseReturnParams, functionTools);
3442
3479
  }
3443
3480
  return baseReturnParams;
@@ -3446,26 +3483,29 @@ function processAssistantContent(content, resolvedProtocol, providerOptions) {
3446
3483
  var _a;
3447
3484
  const newContent = [];
3448
3485
  for (const item of content) {
3449
- if (isToolCallContent(item)) {
3450
- newContent.push({
3451
- type: "text",
3452
- text: resolvedProtocol.formatToolCall(item)
3453
- });
3454
- } else if (item.type === "text") {
3455
- newContent.push(item);
3456
- } else if (item.type === "reasoning") {
3457
- newContent.push(item);
3458
- } else {
3459
- const options = extractOnErrorOption(providerOptions);
3460
- (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
3461
- options,
3462
- "tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
3463
- { content: item }
3464
- );
3465
- newContent.push({
3466
- type: "text",
3467
- text: JSON.stringify(item)
3468
- });
3486
+ switch (item.type) {
3487
+ case "tool-call":
3488
+ newContent.push({
3489
+ type: "text",
3490
+ text: resolvedProtocol.formatToolCall(item)
3491
+ });
3492
+ break;
3493
+ case "text":
3494
+ case "reasoning":
3495
+ newContent.push(item);
3496
+ break;
3497
+ default: {
3498
+ const options = extractOnErrorOption(providerOptions);
3499
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
3500
+ options,
3501
+ "tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
3502
+ { content: item }
3503
+ );
3504
+ newContent.push({
3505
+ type: "text",
3506
+ text: JSON.stringify(item)
3507
+ });
3508
+ }
3469
3509
  }
3470
3510
  }
3471
3511
  const onlyText = newContent.every((c) => c.type === "text");
@@ -3476,25 +3516,28 @@ function processAssistantContent(content, resolvedProtocol, providerOptions) {
3476
3516
  }
3477
3517
  ] : newContent;
3478
3518
  }
3479
- function processToolMessage(content, resolvedProtocol) {
3519
+ function formatApprovalResponse(part) {
3520
+ const status = part.approved ? "Approved" : "Denied";
3521
+ const reason = part.reason ? `: ${part.reason}` : "";
3522
+ return `[Tool Approval ${status}${reason}]`;
3523
+ }
3524
+ function processToolMessage(toolResults, approvalResponses, toolResponsePromptTemplate) {
3525
+ const resultTexts = toolResults.map((toolResult) => {
3526
+ return toolResponsePromptTemplate(toolResult);
3527
+ });
3528
+ const approvalTexts = approvalResponses.map(formatApprovalResponse);
3529
+ const allTexts = [...resultTexts, ...approvalTexts];
3480
3530
  return {
3481
3531
  role: "user",
3482
3532
  content: [
3483
3533
  {
3484
3534
  type: "text",
3485
- text: content.map((toolResult) => {
3486
- var _a, _b;
3487
- const tr = toolResult;
3488
- return resolvedProtocol.formatToolResponse({
3489
- ...toolResult,
3490
- result: (_b = (_a = tr.result) != null ? _a : tr.content) != null ? _b : tr.output
3491
- });
3492
- }).join("\n")
3535
+ text: allTexts.join("\n")
3493
3536
  }
3494
3537
  ]
3495
3538
  };
3496
3539
  }
3497
- function processMessage(message, resolvedProtocol, providerOptions) {
3540
+ function processMessage(message, resolvedProtocol, providerOptions, toolResponsePromptTemplate) {
3498
3541
  if (message.role === "assistant") {
3499
3542
  const condensedContent = processAssistantContent(
3500
3543
  message.content,
@@ -3507,10 +3550,23 @@ function processMessage(message, resolvedProtocol, providerOptions) {
3507
3550
  };
3508
3551
  }
3509
3552
  if (message.role === "tool") {
3510
- const toolResultParts = message.content.filter(
3553
+ const toolContent = message.content;
3554
+ const toolResultParts = toolContent.filter(
3511
3555
  (part) => part.type === "tool-result"
3512
3556
  );
3513
- return processToolMessage(toolResultParts, resolvedProtocol);
3557
+ const approvalResponseParts = toolContent.filter(
3558
+ (part) => part.type === "tool-approval-response"
3559
+ );
3560
+ if (!toolResponsePromptTemplate) {
3561
+ throw new Error(
3562
+ 'toolResponsePromptTemplate is required when processing messages with role "tool". This parameter is optional for other roles but is required here so tool-result content can be converted into a prompt. Ensure your middleware or transform configuration passes a toolResponsePromptTemplate when tool message processing is enabled.'
3563
+ );
3564
+ }
3565
+ return processToolMessage(
3566
+ toolResultParts,
3567
+ approvalResponseParts,
3568
+ toolResponsePromptTemplate
3569
+ );
3514
3570
  }
3515
3571
  return message;
3516
3572
  }
@@ -3573,22 +3629,28 @@ ${currentContent}` }]
3573
3629
  }
3574
3630
  return processedPrompt;
3575
3631
  }
3576
- function convertToolPrompt(prompt, resolvedProtocol, providerOptions) {
3632
+ function convertToolPrompt(prompt, resolvedProtocol, toolResponsePromptTemplate, providerOptions) {
3577
3633
  let processedPrompt = prompt.map(
3578
- (message) => processMessage(message, resolvedProtocol, providerOptions)
3634
+ (message) => processMessage(
3635
+ message,
3636
+ resolvedProtocol,
3637
+ providerOptions,
3638
+ toolResponsePromptTemplate
3639
+ )
3579
3640
  );
3580
3641
  processedPrompt = condenseTextContent(processedPrompt);
3581
3642
  processedPrompt = mergeConsecutiveUserMessages(processedPrompt);
3582
3643
  return processedPrompt;
3583
3644
  }
3584
3645
 
3585
- // src/v6/tool-call-middleware.ts
3646
+ // src/tool-call-middleware.ts
3586
3647
  function createToolMiddleware({
3587
3648
  protocol,
3588
3649
  toolSystemPromptTemplate,
3650
+ toolResponsePromptTemplate,
3589
3651
  placement = "last"
3590
3652
  }) {
3591
- const resolvedProtocol = isProtocolFactory(protocol) ? protocol() : protocol;
3653
+ const resolvedProtocol = isTCMProtocolFactory(protocol) ? protocol() : protocol;
3592
3654
  return {
3593
3655
  specificationVersion: "v3",
3594
3656
  wrapStream: ({ doStream, doGenerate, params }) => {
@@ -3613,85 +3675,38 @@ function createToolMiddleware({
3613
3675
  transformParams: async ({ params }) => transformParams({
3614
3676
  protocol: resolvedProtocol,
3615
3677
  toolSystemPromptTemplate,
3678
+ toolResponsePromptTemplate,
3616
3679
  placement,
3617
3680
  params
3618
3681
  })
3619
3682
  };
3620
3683
  }
3621
3684
 
3622
- // src/v6/index.ts
3623
- var gemmaToolMiddleware = createToolMiddleware({
3624
- protocol: jsonMixProtocol({
3625
- toolCallStart: "```tool_call\n",
3626
- toolCallEnd: "\n```",
3627
- toolResponseStart: "```tool_response\n",
3628
- toolResponseEnd: "\n```"
3629
- }),
3630
- toolSystemPromptTemplate(tools) {
3631
- return `You have access to functions. If you decide to invoke any of the function(s),
3632
- you MUST put it in the format of markdown code fence block with the language name of tool_call , e.g.
3633
- \`\`\`tool_call
3634
- {'name': <function-name>, 'arguments': <args-dict>}
3635
- \`\`\`
3636
- You SHOULD NOT include any other text in the response if you call a function
3637
- ${tools}`;
3638
- }
3639
- });
3685
+ // src/preconfigured-middleware.ts
3640
3686
  var hermesToolMiddleware = createToolMiddleware({
3641
- protocol: jsonMixProtocol,
3642
- toolSystemPromptTemplate(tools) {
3643
- return `You are a function calling AI model.
3644
- You are provided with function signatures within <tools></tools> XML tags.
3645
- You may call one or more functions to assist with the user query.
3646
- Don't make assumptions about what values to plug into functions.
3647
- Here are the available tools: <tools>${tools}</tools>
3648
- Use the following pydantic model json schema for each tool call you will make: {"title": "FunctionCall", "type": "object", "properties": {"arguments": {"title": "Arguments", "type": "object"}, "name": {"title": "Name", "type": "string"}}, "required": ["arguments", "name"]}
3649
- For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
3650
- <tool_call>
3651
- {"name": "<function-name>", "arguments": <args-dict>}
3652
- </tool_call>`;
3653
- }
3687
+ protocol: jsonProtocol({}),
3688
+ toolSystemPromptTemplate: hermesSystemPromptTemplate,
3689
+ toolResponsePromptTemplate: formatToolResponseAsJsonInXml
3654
3690
  });
3655
- var morphXmlToolMiddleware = createToolMiddleware({
3656
- protocol: morphXmlProtocol,
3657
- placement: "first",
3658
- toolSystemPromptTemplate(tools) {
3659
- return `# Tools
3660
-
3661
- You may call one or more functions to assist with the user query.
3662
-
3663
- You are provided with function signatures within <tools></tools> XML tags:
3664
- <tools>${tools}</tools>
3665
-
3666
- # Rules
3667
- - Use exactly one XML element whose tag name is the function name.
3668
- - Put each parameter as a child element.
3669
- - Values must follow the schema exactly (numbers, arrays, objects, enums \u2192 copy as-is).
3670
- - Do not add or remove functions or parameters.
3671
- - Each required parameter must appear once.
3672
- - Output nothing before or after the function call.
3673
- - After calling a tool, you will receive a response in the format: <tool_response><tool_name>NAME</tool_name><result>RESULT</result></tool_response>. Use this result to answer the user.
3674
-
3675
- # Example
3676
- <get_weather>
3677
- <location>New York</location>
3678
- <unit>celsius</unit>
3679
- </get_weather>`;
3680
- }
3691
+ var xmlToolMiddleware = createToolMiddleware({
3692
+ protocol: xmlProtocol({}),
3693
+ toolSystemPromptTemplate: xmlSystemPromptTemplate,
3694
+ toolResponsePromptTemplate: formatToolResponseAsXml
3681
3695
  });
3682
- var orchestratorToolMiddleware = createToolMiddleware({
3683
- protocol: yamlXmlProtocol(),
3684
- placement: "first",
3685
- toolSystemPromptTemplate: orchestratorSystemPromptTemplate
3696
+ var yamlToolMiddleware = createToolMiddleware({
3697
+ protocol: yamlProtocol({}),
3698
+ toolSystemPromptTemplate: yamlSystemPromptTemplate,
3699
+ toolResponsePromptTemplate: formatToolResponseAsXml
3686
3700
  });
3687
3701
 
3688
3702
  // src/community/sijawara.ts
3689
3703
  var sijawaraDetailedXmlToolMiddleware = createToolMiddleware({
3690
- protocol: morphXmlProtocol,
3704
+ protocol: xmlProtocol,
3691
3705
  toolSystemPromptTemplate(tools) {
3706
+ const toolsJson = JSON.stringify(tools);
3692
3707
  return `You have access to callable functions (tools).
3693
3708
  Tool list/context:
3694
- ${tools}
3709
+ ${toolsJson}
3695
3710
 
3696
3711
  ===============================
3697
3712
  TOOL CALLING FORMAT
@@ -3743,11 +3758,12 @@ var sijawaraDetailedXmlToolMiddleware = createToolMiddleware({
3743
3758
  }
3744
3759
  });
3745
3760
  var sijawaraConciseXmlToolMiddleware = createToolMiddleware({
3746
- protocol: morphXmlProtocol,
3761
+ protocol: xmlProtocol,
3747
3762
  toolSystemPromptTemplate(tools) {
3763
+ const toolsJson = JSON.stringify(tools);
3748
3764
  return `You have access to callable functions (tools).
3749
3765
  Tool list/context:
3750
- ${tools}
3766
+ ${toolsJson}
3751
3767
 
3752
3768
  STRICT CALLING RULES:
3753
3769
  - Use the XML-like format for tool calls: