@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.
- package/README.md +7 -9
- package/dist/{v6.cjs → chunk-WX5U7G6L.js} +1902 -1825
- package/dist/chunk-WX5U7G6L.js.map +1 -0
- package/dist/community.cjs +367 -351
- package/dist/community.cjs.map +1 -1
- package/dist/community.d.cts +0 -14
- package/dist/community.d.ts +0 -14
- package/dist/community.js +9 -10
- package/dist/community.js.map +1 -1
- package/dist/index.cjs +388 -362
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +152 -19
- package/dist/index.d.ts +152 -19
- package/dist/index.js +24 -23
- package/package.json +6 -20
- package/dist/chunk-DFOXAWP6.js +0 -1
- package/dist/chunk-DFOXAWP6.js.map +0 -1
- package/dist/chunk-NOYHOQOL.js +0 -2489
- package/dist/chunk-NOYHOQOL.js.map +0 -1
- package/dist/chunk-QPJA5CS6.js +0 -1289
- package/dist/chunk-QPJA5CS6.js.map +0 -1
- package/dist/tool-call-protocol-Bn2CBcD-.d.cts +0 -84
- package/dist/tool-call-protocol-Bn2CBcD-.d.ts +0 -84
- package/dist/v5.cjs +0 -2816
- package/dist/v5.cjs.map +0 -1
- package/dist/v5.d.cts +0 -14
- package/dist/v5.d.ts +0 -14
- package/dist/v5.js +0 -476
- package/dist/v5.js.map +0 -1
- package/dist/v6.cjs.map +0 -1
- package/dist/v6.d.cts +0 -16
- package/dist/v6.d.ts +0 -16
- package/dist/v6.js +0 -16
- package/dist/v6.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -43,32 +43,34 @@ __export(src_exports, {
|
|
|
43
43
|
escapeRegExp: () => escapeRegExp2,
|
|
44
44
|
extractOnErrorOption: () => extractOnErrorOption,
|
|
45
45
|
extractToolNamesFromOriginalTools: () => extractToolNamesFromOriginalTools,
|
|
46
|
-
gemmaToolMiddleware: () => gemmaToolMiddleware,
|
|
47
46
|
getDebugLevel: () => getDebugLevel,
|
|
48
47
|
getPotentialStartIndex: () => getPotentialStartIndex,
|
|
49
48
|
hasInputProperty: () => hasInputProperty,
|
|
50
49
|
hermesToolMiddleware: () => hermesToolMiddleware,
|
|
51
50
|
isProtocolFactory: () => isProtocolFactory,
|
|
52
|
-
|
|
51
|
+
isTCMProtocolFactory: () => isTCMProtocolFactory,
|
|
53
52
|
isToolChoiceActive: () => isToolChoiceActive,
|
|
54
53
|
isToolResultPart: () => isToolResultPart,
|
|
55
|
-
|
|
54
|
+
jsonProtocol: () => jsonProtocol,
|
|
56
55
|
logParseFailure: () => logParseFailure,
|
|
57
56
|
logParsedChunk: () => logParsedChunk,
|
|
58
57
|
logParsedSummary: () => logParsedSummary,
|
|
59
58
|
logRawChunk: () => logRawChunk,
|
|
60
59
|
mergePipelineConfigs: () => mergePipelineConfigs,
|
|
61
|
-
morphXmlProtocol: () => morphXmlProtocol,
|
|
62
|
-
morphXmlToolMiddleware: () => morphXmlToolMiddleware,
|
|
63
60
|
normalizeCloseTagsHeuristic: () => normalizeCloseTagsHeuristic,
|
|
64
|
-
orchestratorSystemPromptTemplate: () => orchestratorSystemPromptTemplate,
|
|
65
|
-
orchestratorToolMiddleware: () => orchestratorToolMiddleware,
|
|
66
61
|
originalToolsSchema: () => originalToolsSchema,
|
|
67
62
|
parse: () => parse2,
|
|
68
63
|
repairAgainstSchemaHeuristic: () => repairAgainstSchemaHeuristic,
|
|
69
64
|
stringify: () => stringify,
|
|
65
|
+
toolChoiceStream: () => toolChoiceStream,
|
|
70
66
|
transform: () => transform,
|
|
71
|
-
|
|
67
|
+
transformParams: () => transformParams,
|
|
68
|
+
wrapGenerate: () => wrapGenerate,
|
|
69
|
+
wrapStream: () => wrapStream,
|
|
70
|
+
xmlProtocol: () => xmlProtocol,
|
|
71
|
+
xmlToolMiddleware: () => xmlToolMiddleware,
|
|
72
|
+
yamlProtocol: () => yamlProtocol,
|
|
73
|
+
yamlToolMiddleware: () => yamlToolMiddleware
|
|
72
74
|
});
|
|
73
75
|
module.exports = __toCommonJS(src_exports);
|
|
74
76
|
|
|
@@ -406,12 +408,15 @@ function handleOpeningTagSegment(src, lt, out, stack) {
|
|
|
406
408
|
}
|
|
407
409
|
return q + 1;
|
|
408
410
|
}
|
|
409
|
-
function
|
|
411
|
+
function extractSchemaProperties(schema) {
|
|
410
412
|
const unwrapped = (0, import_rxml.unwrapJsonSchema)(schema);
|
|
411
413
|
if (!unwrapped || typeof unwrapped !== "object") {
|
|
412
|
-
return
|
|
414
|
+
return void 0;
|
|
413
415
|
}
|
|
414
|
-
|
|
416
|
+
return unwrapped.properties;
|
|
417
|
+
}
|
|
418
|
+
function shouldDeduplicateStringTags(schema) {
|
|
419
|
+
const props = extractSchemaProperties(schema);
|
|
415
420
|
if (!props) {
|
|
416
421
|
return false;
|
|
417
422
|
}
|
|
@@ -423,21 +428,14 @@ function shouldDeduplicateStringTags(schema) {
|
|
|
423
428
|
return (command == null ? void 0 : command.type) === "array";
|
|
424
429
|
}
|
|
425
430
|
function getStringPropertyNames(schema) {
|
|
426
|
-
const
|
|
427
|
-
if (!unwrapped || typeof unwrapped !== "object") {
|
|
428
|
-
return [];
|
|
429
|
-
}
|
|
430
|
-
const props = unwrapped.properties;
|
|
431
|
+
const props = extractSchemaProperties(schema);
|
|
431
432
|
if (!props) {
|
|
432
433
|
return [];
|
|
433
434
|
}
|
|
434
435
|
const names = [];
|
|
435
436
|
for (const key of Object.keys(props)) {
|
|
436
|
-
const prop = (0, import_rxml.unwrapJsonSchema)(
|
|
437
|
-
|
|
438
|
-
);
|
|
439
|
-
const type = prop.type;
|
|
440
|
-
if (type === "string") {
|
|
437
|
+
const prop = (0, import_rxml.unwrapJsonSchema)(props[key]);
|
|
438
|
+
if ((prop == null ? void 0 : prop.type) === "string") {
|
|
441
439
|
names.push(key);
|
|
442
440
|
}
|
|
443
441
|
}
|
|
@@ -472,11 +470,7 @@ function repairParsedAgainstSchema(input, schema) {
|
|
|
472
470
|
if (!input || typeof input !== "object") {
|
|
473
471
|
return input;
|
|
474
472
|
}
|
|
475
|
-
const
|
|
476
|
-
if (!unwrapped || typeof unwrapped !== "object") {
|
|
477
|
-
return input;
|
|
478
|
-
}
|
|
479
|
-
const properties = unwrapped.properties;
|
|
473
|
+
const properties = extractSchemaProperties(schema);
|
|
480
474
|
if (!properties) {
|
|
481
475
|
return input;
|
|
482
476
|
}
|
|
@@ -490,14 +484,12 @@ function applySchemaProps(obj, properties) {
|
|
|
490
484
|
continue;
|
|
491
485
|
}
|
|
492
486
|
const prop = (0, import_rxml.unwrapJsonSchema)(propSchema);
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
const itemSchemaRaw = prop.items;
|
|
496
|
-
const itemSchema = (0, import_rxml.unwrapJsonSchema)(itemSchemaRaw);
|
|
487
|
+
if ((prop == null ? void 0 : prop.type) === "array" && prop.items) {
|
|
488
|
+
const itemSchema = (0, import_rxml.unwrapJsonSchema)(prop.items);
|
|
497
489
|
obj[key] = coerceArrayItems(obj[key], itemSchema);
|
|
498
490
|
continue;
|
|
499
491
|
}
|
|
500
|
-
if (
|
|
492
|
+
if ((prop == null ? void 0 : prop.type) === "object") {
|
|
501
493
|
const val = obj[key];
|
|
502
494
|
if (val && typeof val === "object") {
|
|
503
495
|
obj[key] = repairParsedAgainstSchema(val, prop);
|
|
@@ -1397,7 +1389,7 @@ function stringify(obj) {
|
|
|
1397
1389
|
return "null";
|
|
1398
1390
|
}
|
|
1399
1391
|
|
|
1400
|
-
// src/core/protocols/json-
|
|
1392
|
+
// src/core/protocols/json-protocol.ts
|
|
1401
1393
|
function processToolCallJson(toolCallJson, fullMatch, processedElements, options) {
|
|
1402
1394
|
var _a, _b;
|
|
1403
1395
|
try {
|
|
@@ -1457,7 +1449,6 @@ function flushBuffer(state, controller, toolCallStart) {
|
|
|
1457
1449
|
controller.enqueue({
|
|
1458
1450
|
type: "text-delta",
|
|
1459
1451
|
id: state.currentTextId,
|
|
1460
|
-
textDelta: deltaContent,
|
|
1461
1452
|
delta: deltaContent
|
|
1462
1453
|
});
|
|
1463
1454
|
state.buffer = "";
|
|
@@ -1490,7 +1481,6 @@ function emitIncompleteToolCall(state, controller, toolCallStart) {
|
|
|
1490
1481
|
controller.enqueue({
|
|
1491
1482
|
type: "text-delta",
|
|
1492
1483
|
id: errorId,
|
|
1493
|
-
textDelta: errorContent,
|
|
1494
1484
|
delta: errorContent
|
|
1495
1485
|
});
|
|
1496
1486
|
controller.enqueue({
|
|
@@ -1523,7 +1513,6 @@ function publishText(text, state, controller) {
|
|
|
1523
1513
|
controller.enqueue({
|
|
1524
1514
|
type: "text-delta",
|
|
1525
1515
|
id: state.currentTextId,
|
|
1526
|
-
textDelta: text,
|
|
1527
1516
|
delta: text
|
|
1528
1517
|
});
|
|
1529
1518
|
}
|
|
@@ -1556,7 +1545,6 @@ function emitToolCall(context) {
|
|
|
1556
1545
|
controller.enqueue({
|
|
1557
1546
|
type: "text-delta",
|
|
1558
1547
|
id: errorId,
|
|
1559
|
-
textDelta: errorContent,
|
|
1560
1548
|
delta: errorContent
|
|
1561
1549
|
});
|
|
1562
1550
|
controller.enqueue({
|
|
@@ -1616,39 +1604,34 @@ function handlePartialTag(state, controller, toolCallStart) {
|
|
|
1616
1604
|
state.buffer = "";
|
|
1617
1605
|
}
|
|
1618
1606
|
}
|
|
1619
|
-
var
|
|
1607
|
+
var jsonProtocol = ({
|
|
1620
1608
|
toolCallStart = "<tool_call>",
|
|
1621
|
-
toolCallEnd = "</tool_call>"
|
|
1622
|
-
toolResponseStart = "<tool_response>",
|
|
1623
|
-
toolResponseEnd = "</tool_response>"
|
|
1609
|
+
toolCallEnd = "</tool_call>"
|
|
1624
1610
|
} = {}) => ({
|
|
1625
|
-
formatTools({
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
}));
|
|
1631
|
-
return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
|
|
1611
|
+
formatTools({
|
|
1612
|
+
tools,
|
|
1613
|
+
toolSystemPromptTemplate
|
|
1614
|
+
}) {
|
|
1615
|
+
return toolSystemPromptTemplate(tools || []);
|
|
1632
1616
|
},
|
|
1633
1617
|
formatToolCall(toolCall) {
|
|
1634
1618
|
let args = {};
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1619
|
+
if (toolCall.input != null) {
|
|
1620
|
+
try {
|
|
1621
|
+
args = JSON.parse(toolCall.input);
|
|
1622
|
+
} catch (e) {
|
|
1623
|
+
args = toolCall.input;
|
|
1624
|
+
}
|
|
1639
1625
|
}
|
|
1640
1626
|
return `${toolCallStart}${JSON.stringify({
|
|
1641
1627
|
name: toolCall.toolName,
|
|
1642
1628
|
arguments: args
|
|
1643
1629
|
})}${toolCallEnd}`;
|
|
1644
1630
|
},
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
})}${toolResponseEnd}`;
|
|
1650
|
-
},
|
|
1651
|
-
parseGeneratedText({ text, options }) {
|
|
1631
|
+
parseGeneratedText({
|
|
1632
|
+
text,
|
|
1633
|
+
options
|
|
1634
|
+
}) {
|
|
1652
1635
|
const startEsc = escapeRegExp2(toolCallStart);
|
|
1653
1636
|
const endEsc = escapeRegExp2(toolCallEnd);
|
|
1654
1637
|
const toolCallRegex = new RegExp(
|
|
@@ -1674,7 +1657,9 @@ var jsonMixProtocol = ({
|
|
|
1674
1657
|
}
|
|
1675
1658
|
return processedElements;
|
|
1676
1659
|
},
|
|
1677
|
-
createStreamParser({
|
|
1660
|
+
createStreamParser({
|
|
1661
|
+
options
|
|
1662
|
+
}) {
|
|
1678
1663
|
const state = {
|
|
1679
1664
|
isInsideToolCall: false,
|
|
1680
1665
|
buffer: "",
|
|
@@ -1684,7 +1669,7 @@ var jsonMixProtocol = ({
|
|
|
1684
1669
|
};
|
|
1685
1670
|
return new TransformStream({
|
|
1686
1671
|
transform(chunk, controller) {
|
|
1687
|
-
var _a
|
|
1672
|
+
var _a;
|
|
1688
1673
|
if (chunk.type === "finish") {
|
|
1689
1674
|
handleFinishChunk(state, controller, toolCallStart, chunk);
|
|
1690
1675
|
return;
|
|
@@ -1693,7 +1678,7 @@ var jsonMixProtocol = ({
|
|
|
1693
1678
|
controller.enqueue(chunk);
|
|
1694
1679
|
return;
|
|
1695
1680
|
}
|
|
1696
|
-
const textContent = (
|
|
1681
|
+
const textContent = (_a = chunk.delta) != null ? _a : "";
|
|
1697
1682
|
state.buffer += textContent;
|
|
1698
1683
|
processBufferTags({
|
|
1699
1684
|
state,
|
|
@@ -1720,7 +1705,15 @@ var jsonMixProtocol = ({
|
|
|
1720
1705
|
}
|
|
1721
1706
|
});
|
|
1722
1707
|
|
|
1723
|
-
// src/core/protocols/
|
|
1708
|
+
// src/core/protocols/protocol-interface.ts
|
|
1709
|
+
function isProtocolFactory(protocol) {
|
|
1710
|
+
return typeof protocol === "function";
|
|
1711
|
+
}
|
|
1712
|
+
function isTCMProtocolFactory(protocol) {
|
|
1713
|
+
return typeof protocol === "function";
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
// src/core/protocols/xml-protocol.ts
|
|
1724
1717
|
var import_rxml2 = require("@ai-sdk-tool/rxml");
|
|
1725
1718
|
var defaultPipelineConfig2 = defaultPipelineConfig;
|
|
1726
1719
|
var NAME_CHAR_RE2 = /[A-Za-z0-9_:-]/;
|
|
@@ -2055,7 +2048,6 @@ function createFlushTextHandler(getCurrentTextId, setCurrentTextId, getHasEmitte
|
|
|
2055
2048
|
controller.enqueue({
|
|
2056
2049
|
type: "text-delta",
|
|
2057
2050
|
id: getCurrentTextId(),
|
|
2058
|
-
textDelta: content,
|
|
2059
2051
|
delta: content
|
|
2060
2052
|
});
|
|
2061
2053
|
}
|
|
@@ -2217,7 +2209,7 @@ function createProcessBufferHandler(getBuffer, setBuffer, getCurrentToolCall, se
|
|
|
2217
2209
|
}
|
|
2218
2210
|
};
|
|
2219
2211
|
}
|
|
2220
|
-
var
|
|
2212
|
+
var xmlProtocol = (protocolOptions) => {
|
|
2221
2213
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
2222
2214
|
let pipelineConfig = protocolOptions == null ? void 0 : protocolOptions.pipeline;
|
|
2223
2215
|
const maxReparses = protocolOptions == null ? void 0 : protocolOptions.maxReparses;
|
|
@@ -2257,40 +2249,22 @@ var morphXmlProtocol = (protocolOptions) => {
|
|
|
2257
2249
|
}
|
|
2258
2250
|
return {
|
|
2259
2251
|
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
2260
|
-
|
|
2261
|
-
name: tool.name,
|
|
2262
|
-
description: tool.description,
|
|
2263
|
-
parameters: (0, import_rxml2.unwrapJsonSchema)(tool.inputSchema)
|
|
2264
|
-
}));
|
|
2265
|
-
return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
|
|
2252
|
+
return toolSystemPromptTemplate(tools || []);
|
|
2266
2253
|
},
|
|
2267
2254
|
formatToolCall(toolCall) {
|
|
2268
2255
|
let args = {};
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2256
|
+
if (toolCall.input != null) {
|
|
2257
|
+
try {
|
|
2258
|
+
args = JSON.parse(toolCall.input);
|
|
2259
|
+
} catch (e) {
|
|
2260
|
+
args = toolCall.input;
|
|
2261
|
+
}
|
|
2273
2262
|
}
|
|
2274
2263
|
return (0, import_rxml2.stringify)(toolCall.toolName, args, {
|
|
2275
2264
|
suppressEmptyNode: false,
|
|
2276
2265
|
format: false
|
|
2277
2266
|
});
|
|
2278
2267
|
},
|
|
2279
|
-
formatToolResponse(toolResult) {
|
|
2280
|
-
let result = toolResult.result;
|
|
2281
|
-
if (result && typeof result === "object" && "type" in result && result.type === "json" && "value" in result) {
|
|
2282
|
-
result = result.value;
|
|
2283
|
-
}
|
|
2284
|
-
const xml = (0, import_rxml2.stringify)(
|
|
2285
|
-
"tool_response",
|
|
2286
|
-
{
|
|
2287
|
-
tool_name: toolResult.toolName,
|
|
2288
|
-
result
|
|
2289
|
-
},
|
|
2290
|
-
{ declaration: false }
|
|
2291
|
-
);
|
|
2292
|
-
return xml;
|
|
2293
|
-
},
|
|
2294
2268
|
parseGeneratedText({ text, tools, options }) {
|
|
2295
2269
|
const toolNames = tools.map((t) => t.name).filter(Boolean);
|
|
2296
2270
|
if (toolNames.length === 0) {
|
|
@@ -2359,7 +2333,7 @@ var morphXmlProtocol = (protocolOptions) => {
|
|
|
2359
2333
|
);
|
|
2360
2334
|
return new TransformStream({
|
|
2361
2335
|
transform(chunk, controller) {
|
|
2362
|
-
var _a2
|
|
2336
|
+
var _a2;
|
|
2363
2337
|
if (chunk.type !== "text-delta") {
|
|
2364
2338
|
if (buffer) {
|
|
2365
2339
|
flushText(controller, buffer);
|
|
@@ -2368,7 +2342,7 @@ var morphXmlProtocol = (protocolOptions) => {
|
|
|
2368
2342
|
controller.enqueue(chunk);
|
|
2369
2343
|
return;
|
|
2370
2344
|
}
|
|
2371
|
-
const textContent = (
|
|
2345
|
+
const textContent = (_a2 = chunk.delta) != null ? _a2 : "";
|
|
2372
2346
|
buffer += textContent;
|
|
2373
2347
|
processBuffer(controller);
|
|
2374
2348
|
},
|
|
@@ -2403,13 +2377,7 @@ var morphXmlProtocol = (protocolOptions) => {
|
|
|
2403
2377
|
};
|
|
2404
2378
|
};
|
|
2405
2379
|
|
|
2406
|
-
// src/core/protocols/
|
|
2407
|
-
function isProtocolFactory(protocol) {
|
|
2408
|
-
return typeof protocol === "function";
|
|
2409
|
-
}
|
|
2410
|
-
|
|
2411
|
-
// src/core/protocols/yaml-xml-protocol.ts
|
|
2412
|
-
var import_rxml3 = require("@ai-sdk-tool/rxml");
|
|
2380
|
+
// src/core/protocols/yaml-protocol.ts
|
|
2413
2381
|
var import_yaml = __toESM(require("yaml"), 1);
|
|
2414
2382
|
var NAME_CHAR_RE3 = /[A-Za-z0-9_:-]/;
|
|
2415
2383
|
var WHITESPACE_REGEX4 = /\s/;
|
|
@@ -2630,7 +2598,6 @@ function createFlushTextHandler2(getCurrentTextId, setCurrentTextId, getHasEmitt
|
|
|
2630
2598
|
controller.enqueue({
|
|
2631
2599
|
type: "text-delta",
|
|
2632
2600
|
id: getCurrentTextId(),
|
|
2633
|
-
textDelta: content,
|
|
2634
2601
|
delta: content
|
|
2635
2602
|
});
|
|
2636
2603
|
}
|
|
@@ -2678,42 +2645,24 @@ function findEarliestToolTag2(buffer, toolNames) {
|
|
|
2678
2645
|
tagLength: bestTagLength
|
|
2679
2646
|
};
|
|
2680
2647
|
}
|
|
2681
|
-
var
|
|
2648
|
+
var yamlProtocol = (_protocolOptions) => {
|
|
2682
2649
|
return {
|
|
2683
2650
|
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
2684
|
-
|
|
2685
|
-
name: tool.name,
|
|
2686
|
-
description: tool.description,
|
|
2687
|
-
parameters: (0, import_rxml3.unwrapJsonSchema)(tool.inputSchema)
|
|
2688
|
-
}));
|
|
2689
|
-
return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
|
|
2651
|
+
return toolSystemPromptTemplate(tools || []);
|
|
2690
2652
|
},
|
|
2691
2653
|
formatToolCall(toolCall) {
|
|
2692
2654
|
let args = {};
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2655
|
+
if (toolCall.input != null) {
|
|
2656
|
+
try {
|
|
2657
|
+
args = JSON.parse(toolCall.input);
|
|
2658
|
+
} catch (e) {
|
|
2659
|
+
args = { value: toolCall.input };
|
|
2660
|
+
}
|
|
2697
2661
|
}
|
|
2698
2662
|
const yamlContent = import_yaml.default.stringify(args);
|
|
2699
2663
|
return `<${toolCall.toolName}>
|
|
2700
2664
|
${yamlContent}</${toolCall.toolName}>`;
|
|
2701
2665
|
},
|
|
2702
|
-
formatToolResponse(toolResult) {
|
|
2703
|
-
let result = toolResult.result;
|
|
2704
|
-
if (result && typeof result === "object" && "type" in result && result.type === "json" && "value" in result) {
|
|
2705
|
-
result = result.value;
|
|
2706
|
-
}
|
|
2707
|
-
const xml = (0, import_rxml3.stringify)(
|
|
2708
|
-
"tool_response",
|
|
2709
|
-
{
|
|
2710
|
-
tool_name: toolResult.toolName,
|
|
2711
|
-
result
|
|
2712
|
-
},
|
|
2713
|
-
{ declaration: false }
|
|
2714
|
-
);
|
|
2715
|
-
return xml;
|
|
2716
|
-
},
|
|
2717
2666
|
parseGeneratedText({ text, tools, options }) {
|
|
2718
2667
|
const toolNames = tools.map((t) => t.name).filter(Boolean);
|
|
2719
2668
|
if (toolNames.length === 0) {
|
|
@@ -2827,7 +2776,7 @@ ${yamlContent}</${toolCall.toolName}>`;
|
|
|
2827
2776
|
};
|
|
2828
2777
|
return new TransformStream({
|
|
2829
2778
|
transform(chunk, controller) {
|
|
2830
|
-
var _a
|
|
2779
|
+
var _a;
|
|
2831
2780
|
if (chunk.type !== "text-delta") {
|
|
2832
2781
|
if (buffer) {
|
|
2833
2782
|
flushText(controller, buffer);
|
|
@@ -2836,7 +2785,7 @@ ${yamlContent}</${toolCall.toolName}>`;
|
|
|
2836
2785
|
controller.enqueue(chunk);
|
|
2837
2786
|
return;
|
|
2838
2787
|
}
|
|
2839
|
-
const textContent = (
|
|
2788
|
+
const textContent = (_a = chunk.delta) != null ? _a : "";
|
|
2840
2789
|
buffer += textContent;
|
|
2841
2790
|
processBuffer(controller);
|
|
2842
2791
|
},
|
|
@@ -2872,44 +2821,6 @@ ${yamlContent}</${toolCall.toolName}>`;
|
|
|
2872
2821
|
}
|
|
2873
2822
|
};
|
|
2874
2823
|
};
|
|
2875
|
-
function orchestratorSystemPromptTemplate(tools, includeMultilineExample = true) {
|
|
2876
|
-
const multilineExample = includeMultilineExample ? `
|
|
2877
|
-
|
|
2878
|
-
For multiline values, use YAML's literal block syntax:
|
|
2879
|
-
<write_file>
|
|
2880
|
-
file_path: /tmp/example.txt
|
|
2881
|
-
contents: |
|
|
2882
|
-
First line
|
|
2883
|
-
Second line
|
|
2884
|
-
Third line
|
|
2885
|
-
</write_file>` : "";
|
|
2886
|
-
return `# Tools
|
|
2887
|
-
|
|
2888
|
-
You may call one or more functions to assist with the user query.
|
|
2889
|
-
|
|
2890
|
-
You are provided with function signatures within <tools></tools> XML tags:
|
|
2891
|
-
<tools>${tools}</tools>
|
|
2892
|
-
|
|
2893
|
-
# Format
|
|
2894
|
-
|
|
2895
|
-
Use exactly one XML element whose tag name is the function name.
|
|
2896
|
-
Inside the XML element, specify parameters using YAML syntax (key: value pairs).
|
|
2897
|
-
|
|
2898
|
-
# Example
|
|
2899
|
-
<get_weather>
|
|
2900
|
-
location: New York
|
|
2901
|
-
unit: celsius
|
|
2902
|
-
</get_weather>${multilineExample}
|
|
2903
|
-
|
|
2904
|
-
# Rules
|
|
2905
|
-
- Parameter names and values must follow the schema exactly.
|
|
2906
|
-
- Use proper YAML syntax for values (strings, numbers, booleans, arrays, objects).
|
|
2907
|
-
- Each required parameter must appear once.
|
|
2908
|
-
- Do not add functions or parameters not in the schema.
|
|
2909
|
-
- After calling a tool, you will receive a response. Use this result to answer the user.
|
|
2910
|
-
- Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
|
|
2911
|
-
- If a task requires multiple function calls, make ALL of them at once.`;
|
|
2912
|
-
}
|
|
2913
2824
|
|
|
2914
2825
|
// src/core/utils/dynamic-tool-schema.ts
|
|
2915
2826
|
function createDynamicIfThenElseSchema(tools) {
|
|
@@ -3011,21 +2922,20 @@ function isToolChoiceActive(params) {
|
|
|
3011
2922
|
}
|
|
3012
2923
|
|
|
3013
2924
|
// src/core/utils/type-guards.ts
|
|
3014
|
-
function isToolCallContent(content) {
|
|
3015
|
-
return content.type === "tool-call" && typeof content.toolName === "string" && // input may be a JSON string or an already-parsed object depending on provider/runtime
|
|
3016
|
-
(typeof content.input === "string" || typeof content.input === "object");
|
|
3017
|
-
}
|
|
3018
2925
|
function isToolResultPart(content) {
|
|
2926
|
+
if (!content || typeof content !== "object") {
|
|
2927
|
+
return false;
|
|
2928
|
+
}
|
|
3019
2929
|
const c = content;
|
|
3020
|
-
return
|
|
2930
|
+
return c.type === "tool-result" && typeof c.toolName === "string" && typeof c.toolCallId === "string" && "output" in c;
|
|
3021
2931
|
}
|
|
3022
2932
|
function hasInputProperty(obj) {
|
|
3023
2933
|
return typeof obj === "object" && obj !== null && "input" in obj;
|
|
3024
2934
|
}
|
|
3025
2935
|
|
|
3026
|
-
// src/
|
|
2936
|
+
// src/generate-handler.ts
|
|
3027
2937
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
3028
|
-
var
|
|
2938
|
+
var import_rxml3 = require("@ai-sdk-tool/rxml");
|
|
3029
2939
|
function parseToolChoiceJson(text, providerOptions) {
|
|
3030
2940
|
var _a;
|
|
3031
2941
|
try {
|
|
@@ -3176,92 +3086,218 @@ function fixToolCallWithSchema(part, tools) {
|
|
|
3176
3086
|
if (part.type !== "tool-call") {
|
|
3177
3087
|
return part;
|
|
3178
3088
|
}
|
|
3179
|
-
const tc = part;
|
|
3180
3089
|
let args = {};
|
|
3181
|
-
if (typeof
|
|
3090
|
+
if (typeof part.input === "string") {
|
|
3182
3091
|
try {
|
|
3183
|
-
args = JSON.parse(
|
|
3092
|
+
args = JSON.parse(part.input);
|
|
3184
3093
|
} catch (e) {
|
|
3185
3094
|
return part;
|
|
3186
3095
|
}
|
|
3187
|
-
} else if (
|
|
3188
|
-
args =
|
|
3096
|
+
} else if (part.input && typeof part.input === "object") {
|
|
3097
|
+
args = part.input;
|
|
3189
3098
|
}
|
|
3190
|
-
const schema = (_a = tools.find((t) => t.name ===
|
|
3191
|
-
const coerced = (0,
|
|
3099
|
+
const schema = (_a = tools.find((t) => t.name === part.toolName)) == null ? void 0 : _a.inputSchema;
|
|
3100
|
+
const coerced = (0, import_rxml3.coerceBySchema)(args, schema);
|
|
3192
3101
|
return {
|
|
3193
3102
|
...part,
|
|
3194
3103
|
input: JSON.stringify(coerced != null ? coerced : {})
|
|
3195
3104
|
};
|
|
3196
3105
|
}
|
|
3197
3106
|
|
|
3198
|
-
// src/
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
return {
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3107
|
+
// src/core/prompts/hermes-system-prompt.ts
|
|
3108
|
+
function hermesSystemPromptTemplate(tools) {
|
|
3109
|
+
const toolsJson = JSON.stringify(tools);
|
|
3110
|
+
return `You are a function calling AI model.
|
|
3111
|
+
You are provided with function signatures within <tools></tools> XML tags.
|
|
3112
|
+
You may call one or more functions to assist with the user query.
|
|
3113
|
+
Don't make assumptions about what values to plug into functions.
|
|
3114
|
+
Here are the available tools: <tools>${toolsJson}</tools>
|
|
3115
|
+
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"]}
|
|
3116
|
+
For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
|
|
3117
|
+
<tool_call>
|
|
3118
|
+
{"name": "<function-name>", "arguments": <args-dict>}
|
|
3119
|
+
</tool_call>`;
|
|
3120
|
+
}
|
|
3121
|
+
|
|
3122
|
+
// src/core/prompts/tool-response.ts
|
|
3123
|
+
function unwrapToolResult(result) {
|
|
3124
|
+
var _a, _b;
|
|
3125
|
+
switch (result.type) {
|
|
3126
|
+
case "text":
|
|
3127
|
+
return (_a = result.value) != null ? _a : "";
|
|
3128
|
+
case "json":
|
|
3129
|
+
return result.value;
|
|
3130
|
+
case "execution-denied": {
|
|
3131
|
+
const reason = result.reason;
|
|
3132
|
+
return reason ? `[Execution Denied: ${reason}]` : "[Execution Denied]";
|
|
3133
|
+
}
|
|
3134
|
+
case "error-text":
|
|
3135
|
+
return `[Error: ${(_b = result.value) != null ? _b : ""}]`;
|
|
3136
|
+
case "error-json":
|
|
3137
|
+
return `[Error: ${JSON.stringify(result.value)}]`;
|
|
3138
|
+
case "content": {
|
|
3139
|
+
return result.value.map((part) => {
|
|
3140
|
+
var _a2;
|
|
3141
|
+
const contentPart = part;
|
|
3142
|
+
switch (contentPart.type) {
|
|
3143
|
+
case "text":
|
|
3144
|
+
return (_a2 = contentPart.text) != null ? _a2 : "";
|
|
3145
|
+
case "image-data":
|
|
3146
|
+
return `[Image: ${contentPart.mediaType}]`;
|
|
3147
|
+
case "image-url":
|
|
3148
|
+
return `[Image URL: ${contentPart.url}]`;
|
|
3149
|
+
case "image-file-id": {
|
|
3150
|
+
const fileId = contentPart.fileId;
|
|
3151
|
+
const displayId = typeof fileId === "string" ? fileId : JSON.stringify(fileId);
|
|
3152
|
+
return `[Image ID: ${displayId}]`;
|
|
3153
|
+
}
|
|
3154
|
+
case "file-data": {
|
|
3155
|
+
const filePart = contentPart;
|
|
3156
|
+
if (filePart.filename) {
|
|
3157
|
+
return `[File: ${filePart.filename} (${filePart.mediaType})]`;
|
|
3158
|
+
}
|
|
3159
|
+
return `[File: ${filePart.mediaType}]`;
|
|
3160
|
+
}
|
|
3161
|
+
case "file-url":
|
|
3162
|
+
return `[File URL: ${contentPart.url}]`;
|
|
3163
|
+
case "file-id": {
|
|
3164
|
+
const fileId = contentPart.fileId;
|
|
3165
|
+
const displayId = typeof fileId === "string" ? fileId : JSON.stringify(fileId);
|
|
3166
|
+
return `[File ID: ${displayId}]`;
|
|
3167
|
+
}
|
|
3168
|
+
case "media":
|
|
3169
|
+
return `[Media: ${contentPart.mediaType}]`;
|
|
3170
|
+
case "custom":
|
|
3171
|
+
return "[Custom content]";
|
|
3172
|
+
default:
|
|
3173
|
+
return "[Unknown content]";
|
|
3174
|
+
}
|
|
3175
|
+
}).join("\n");
|
|
3176
|
+
}
|
|
3177
|
+
default: {
|
|
3178
|
+
const _exhaustive = result;
|
|
3179
|
+
return _exhaustive;
|
|
3180
|
+
}
|
|
3235
3181
|
}
|
|
3236
3182
|
}
|
|
3237
|
-
function
|
|
3238
|
-
const
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3183
|
+
function formatToolResponseAsJsonInXml(toolResult) {
|
|
3184
|
+
const unwrappedResult = unwrapToolResult(toolResult.output);
|
|
3185
|
+
return `<tool_response>${JSON.stringify({
|
|
3186
|
+
toolName: toolResult.toolName,
|
|
3187
|
+
result: unwrappedResult
|
|
3188
|
+
})}</tool_response>`;
|
|
3189
|
+
}
|
|
3190
|
+
function formatToolResponseAsXml(toolResult) {
|
|
3191
|
+
const unwrappedResult = unwrapToolResult(toolResult.output);
|
|
3192
|
+
const toolNameXml = `<tool_name>${toolResult.toolName}</tool_name>`;
|
|
3193
|
+
const resultLines = formatXmlNode("result", unwrappedResult, 1);
|
|
3194
|
+
return [
|
|
3195
|
+
"<tool_response>",
|
|
3196
|
+
` ${toolNameXml}`,
|
|
3197
|
+
...resultLines,
|
|
3198
|
+
"</tool_response>"
|
|
3199
|
+
].join("\n");
|
|
3200
|
+
}
|
|
3201
|
+
function formatXmlNode(tagName, value, depth) {
|
|
3202
|
+
const indent = " ".repeat(depth);
|
|
3203
|
+
if (value === null || value === void 0) {
|
|
3204
|
+
return [`${indent}<${tagName}></${tagName}>`];
|
|
3205
|
+
}
|
|
3206
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
3207
|
+
return [`${indent}<${tagName}>${String(value)}</${tagName}>`];
|
|
3208
|
+
}
|
|
3209
|
+
if (Array.isArray(value)) {
|
|
3210
|
+
if (value.length === 0) {
|
|
3211
|
+
return [`${indent}<${tagName}></${tagName}>`];
|
|
3260
3212
|
}
|
|
3261
|
-
|
|
3262
|
-
|
|
3213
|
+
const lines2 = [`${indent}<${tagName}>`];
|
|
3214
|
+
for (const item of value) {
|
|
3215
|
+
lines2.push(...formatXmlNode("item", item, depth + 1));
|
|
3216
|
+
}
|
|
3217
|
+
lines2.push(`${indent}</${tagName}>`);
|
|
3218
|
+
return lines2;
|
|
3263
3219
|
}
|
|
3220
|
+
const entries = Object.entries(value);
|
|
3221
|
+
if (entries.length === 0) {
|
|
3222
|
+
return [`${indent}<${tagName}></${tagName}>`];
|
|
3223
|
+
}
|
|
3224
|
+
const lines = [`${indent}<${tagName}>`];
|
|
3225
|
+
for (const [key, entryValue] of entries) {
|
|
3226
|
+
lines.push(...formatXmlNode(key, entryValue, depth + 1));
|
|
3227
|
+
}
|
|
3228
|
+
lines.push(`${indent}</${tagName}>`);
|
|
3229
|
+
return lines;
|
|
3230
|
+
}
|
|
3231
|
+
|
|
3232
|
+
// src/core/prompts/xml-system-prompt.ts
|
|
3233
|
+
function xmlSystemPromptTemplate(tools) {
|
|
3234
|
+
const toolsJson = JSON.stringify(tools);
|
|
3235
|
+
return `# Tools
|
|
3236
|
+
|
|
3237
|
+
You may call one or more functions to assist with the user query.
|
|
3238
|
+
|
|
3239
|
+
You are provided with function signatures within <tools></tools> XML tags:
|
|
3240
|
+
<tools>${toolsJson}</tools>
|
|
3241
|
+
|
|
3242
|
+
# Rules
|
|
3243
|
+
- Use exactly one XML element whose tag name is the function name.
|
|
3244
|
+
- Put each parameter as a child element.
|
|
3245
|
+
- Values must follow the schema exactly (numbers, arrays, objects, enums \u2192 copy as-is).
|
|
3246
|
+
- Do not add or remove functions or parameters.
|
|
3247
|
+
- Each required parameter must appear once.
|
|
3248
|
+
- Output nothing before or after the function call.
|
|
3249
|
+
- 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.
|
|
3250
|
+
|
|
3251
|
+
# Example
|
|
3252
|
+
<get_weather>
|
|
3253
|
+
<location>New York</location>
|
|
3254
|
+
<unit>celsius</unit>
|
|
3255
|
+
</get_weather>`;
|
|
3264
3256
|
}
|
|
3257
|
+
|
|
3258
|
+
// src/core/prompts/yaml-system-prompt.ts
|
|
3259
|
+
function yamlSystemPromptTemplate(tools, includeMultilineExample = true) {
|
|
3260
|
+
const toolsJson = JSON.stringify(tools);
|
|
3261
|
+
const multilineExample = includeMultilineExample ? `
|
|
3262
|
+
|
|
3263
|
+
For multiline values, use YAML's literal block syntax:
|
|
3264
|
+
<write_file>
|
|
3265
|
+
file_path: /tmp/example.txt
|
|
3266
|
+
contents: |
|
|
3267
|
+
First line
|
|
3268
|
+
Second line
|
|
3269
|
+
Third line
|
|
3270
|
+
</write_file>` : "";
|
|
3271
|
+
return `# Tools
|
|
3272
|
+
|
|
3273
|
+
You may call one or more functions to assist with the user query.
|
|
3274
|
+
|
|
3275
|
+
You are provided with function signatures within <tools></tools> XML tags:
|
|
3276
|
+
<tools>${toolsJson}</tools>
|
|
3277
|
+
|
|
3278
|
+
# Format
|
|
3279
|
+
|
|
3280
|
+
Use exactly one XML element whose tag name is the function name.
|
|
3281
|
+
Inside the XML element, specify parameters using YAML syntax (key: value pairs).
|
|
3282
|
+
|
|
3283
|
+
# Example
|
|
3284
|
+
<get_weather>
|
|
3285
|
+
location: New York
|
|
3286
|
+
unit: celsius
|
|
3287
|
+
</get_weather>${multilineExample}
|
|
3288
|
+
|
|
3289
|
+
# Rules
|
|
3290
|
+
- Parameter names and values must follow the schema exactly.
|
|
3291
|
+
- Use proper YAML syntax for values (strings, numbers, booleans, arrays, objects).
|
|
3292
|
+
- Each required parameter must appear once.
|
|
3293
|
+
- Do not add functions or parameters not in the schema.
|
|
3294
|
+
- After calling a tool, you will receive a response. Use this result to answer the user.
|
|
3295
|
+
- Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
|
|
3296
|
+
- If a task requires multiple function calls, make ALL of them at once.`;
|
|
3297
|
+
}
|
|
3298
|
+
|
|
3299
|
+
// src/stream-handler.ts
|
|
3300
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
3265
3301
|
async function wrapStream({
|
|
3266
3302
|
protocol,
|
|
3267
3303
|
doStream,
|
|
@@ -3285,23 +3321,24 @@ async function wrapStream({
|
|
|
3285
3321
|
...((_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware) || {}
|
|
3286
3322
|
};
|
|
3287
3323
|
const coreStream = stream.pipeThrough(
|
|
3288
|
-
new TransformStream(
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3324
|
+
new TransformStream(
|
|
3325
|
+
{
|
|
3326
|
+
transform(part, controller) {
|
|
3327
|
+
if (debugLevel === "stream") {
|
|
3328
|
+
logRawChunk(part);
|
|
3329
|
+
}
|
|
3330
|
+
controller.enqueue(part);
|
|
3292
3331
|
}
|
|
3293
|
-
controller.enqueue(mapV3PartToCore(part));
|
|
3294
3332
|
}
|
|
3295
|
-
|
|
3333
|
+
)
|
|
3296
3334
|
).pipeThrough(protocol.createStreamParser({ tools, options }));
|
|
3297
3335
|
const v3Stream = coreStream.pipeThrough(
|
|
3298
3336
|
new TransformStream({
|
|
3299
3337
|
transform(part, controller) {
|
|
3300
|
-
const v3Part = mapCorePartToV3(part);
|
|
3301
3338
|
if (debugLevel === "stream") {
|
|
3302
|
-
logParsedChunk(
|
|
3339
|
+
logParsedChunk(part);
|
|
3303
3340
|
}
|
|
3304
|
-
controller.enqueue(
|
|
3341
|
+
controller.enqueue(part);
|
|
3305
3342
|
}
|
|
3306
3343
|
})
|
|
3307
3344
|
);
|
|
@@ -3358,7 +3395,7 @@ async function toolChoiceStream({
|
|
|
3358
3395
|
};
|
|
3359
3396
|
}
|
|
3360
3397
|
|
|
3361
|
-
// src/
|
|
3398
|
+
// src/transform-handler.ts
|
|
3362
3399
|
function buildFinalPrompt(systemPrompt, processedPrompt, placement) {
|
|
3363
3400
|
const systemIndex = processedPrompt.findIndex((m) => m.role === "system");
|
|
3364
3401
|
if (systemIndex !== -1) {
|
|
@@ -3501,10 +3538,11 @@ function transformParams({
|
|
|
3501
3538
|
params,
|
|
3502
3539
|
protocol,
|
|
3503
3540
|
toolSystemPromptTemplate,
|
|
3541
|
+
toolResponsePromptTemplate,
|
|
3504
3542
|
placement = "first"
|
|
3505
3543
|
}) {
|
|
3506
|
-
var _a, _b, _c, _d
|
|
3507
|
-
const resolvedProtocol =
|
|
3544
|
+
var _a, _b, _c, _d;
|
|
3545
|
+
const resolvedProtocol = isTCMProtocolFactory(protocol) ? protocol() : protocol;
|
|
3508
3546
|
const functionTools = ((_a = params.tools) != null ? _a : []).filter(
|
|
3509
3547
|
(t) => t.type === "function"
|
|
3510
3548
|
);
|
|
@@ -3512,9 +3550,18 @@ function transformParams({
|
|
|
3512
3550
|
tools: functionTools,
|
|
3513
3551
|
toolSystemPromptTemplate
|
|
3514
3552
|
});
|
|
3553
|
+
let normalizedPrompt;
|
|
3554
|
+
if (Array.isArray(params.prompt)) {
|
|
3555
|
+
normalizedPrompt = params.prompt;
|
|
3556
|
+
} else if (params.prompt) {
|
|
3557
|
+
normalizedPrompt = [params.prompt];
|
|
3558
|
+
} else {
|
|
3559
|
+
normalizedPrompt = [];
|
|
3560
|
+
}
|
|
3515
3561
|
const processedPrompt = convertToolPrompt(
|
|
3516
|
-
|
|
3562
|
+
normalizedPrompt,
|
|
3517
3563
|
resolvedProtocol,
|
|
3564
|
+
toolResponsePromptTemplate,
|
|
3518
3565
|
extractOnErrorOption(params.providerOptions)
|
|
3519
3566
|
);
|
|
3520
3567
|
const finalPrompt = buildFinalPrompt(
|
|
@@ -3527,15 +3574,15 @@ function transformParams({
|
|
|
3527
3574
|
finalPrompt,
|
|
3528
3575
|
functionTools
|
|
3529
3576
|
);
|
|
3530
|
-
if (((
|
|
3577
|
+
if (((_b = params.toolChoice) == null ? void 0 : _b.type) === "none") {
|
|
3531
3578
|
throw new Error(
|
|
3532
3579
|
"The 'none' toolChoice type is not supported by this middleware. Please use 'auto', 'required', or specify a tool name."
|
|
3533
3580
|
);
|
|
3534
3581
|
}
|
|
3535
|
-
if (((
|
|
3582
|
+
if (((_c = params.toolChoice) == null ? void 0 : _c.type) === "tool") {
|
|
3536
3583
|
return handleToolChoiceTool(params, baseReturnParams);
|
|
3537
3584
|
}
|
|
3538
|
-
if (((
|
|
3585
|
+
if (((_d = params.toolChoice) == null ? void 0 : _d.type) === "required") {
|
|
3539
3586
|
return handleToolChoiceRequired(params, baseReturnParams, functionTools);
|
|
3540
3587
|
}
|
|
3541
3588
|
return baseReturnParams;
|
|
@@ -3544,26 +3591,29 @@ function processAssistantContent(content, resolvedProtocol, providerOptions) {
|
|
|
3544
3591
|
var _a;
|
|
3545
3592
|
const newContent = [];
|
|
3546
3593
|
for (const item of content) {
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
options
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3594
|
+
switch (item.type) {
|
|
3595
|
+
case "tool-call":
|
|
3596
|
+
newContent.push({
|
|
3597
|
+
type: "text",
|
|
3598
|
+
text: resolvedProtocol.formatToolCall(item)
|
|
3599
|
+
});
|
|
3600
|
+
break;
|
|
3601
|
+
case "text":
|
|
3602
|
+
case "reasoning":
|
|
3603
|
+
newContent.push(item);
|
|
3604
|
+
break;
|
|
3605
|
+
default: {
|
|
3606
|
+
const options = extractOnErrorOption(providerOptions);
|
|
3607
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
3608
|
+
options,
|
|
3609
|
+
"tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
|
|
3610
|
+
{ content: item }
|
|
3611
|
+
);
|
|
3612
|
+
newContent.push({
|
|
3613
|
+
type: "text",
|
|
3614
|
+
text: JSON.stringify(item)
|
|
3615
|
+
});
|
|
3616
|
+
}
|
|
3567
3617
|
}
|
|
3568
3618
|
}
|
|
3569
3619
|
const onlyText = newContent.every((c) => c.type === "text");
|
|
@@ -3574,25 +3624,28 @@ function processAssistantContent(content, resolvedProtocol, providerOptions) {
|
|
|
3574
3624
|
}
|
|
3575
3625
|
] : newContent;
|
|
3576
3626
|
}
|
|
3577
|
-
function
|
|
3627
|
+
function formatApprovalResponse(part) {
|
|
3628
|
+
const status = part.approved ? "Approved" : "Denied";
|
|
3629
|
+
const reason = part.reason ? `: ${part.reason}` : "";
|
|
3630
|
+
return `[Tool Approval ${status}${reason}]`;
|
|
3631
|
+
}
|
|
3632
|
+
function processToolMessage(toolResults, approvalResponses, toolResponsePromptTemplate) {
|
|
3633
|
+
const resultTexts = toolResults.map((toolResult) => {
|
|
3634
|
+
return toolResponsePromptTemplate(toolResult);
|
|
3635
|
+
});
|
|
3636
|
+
const approvalTexts = approvalResponses.map(formatApprovalResponse);
|
|
3637
|
+
const allTexts = [...resultTexts, ...approvalTexts];
|
|
3578
3638
|
return {
|
|
3579
3639
|
role: "user",
|
|
3580
3640
|
content: [
|
|
3581
3641
|
{
|
|
3582
3642
|
type: "text",
|
|
3583
|
-
text:
|
|
3584
|
-
var _a, _b;
|
|
3585
|
-
const tr = toolResult;
|
|
3586
|
-
return resolvedProtocol.formatToolResponse({
|
|
3587
|
-
...toolResult,
|
|
3588
|
-
result: (_b = (_a = tr.result) != null ? _a : tr.content) != null ? _b : tr.output
|
|
3589
|
-
});
|
|
3590
|
-
}).join("\n")
|
|
3643
|
+
text: allTexts.join("\n")
|
|
3591
3644
|
}
|
|
3592
3645
|
]
|
|
3593
3646
|
};
|
|
3594
3647
|
}
|
|
3595
|
-
function processMessage(message, resolvedProtocol, providerOptions) {
|
|
3648
|
+
function processMessage(message, resolvedProtocol, providerOptions, toolResponsePromptTemplate) {
|
|
3596
3649
|
if (message.role === "assistant") {
|
|
3597
3650
|
const condensedContent = processAssistantContent(
|
|
3598
3651
|
message.content,
|
|
@@ -3605,10 +3658,23 @@ function processMessage(message, resolvedProtocol, providerOptions) {
|
|
|
3605
3658
|
};
|
|
3606
3659
|
}
|
|
3607
3660
|
if (message.role === "tool") {
|
|
3608
|
-
const
|
|
3661
|
+
const toolContent = message.content;
|
|
3662
|
+
const toolResultParts = toolContent.filter(
|
|
3609
3663
|
(part) => part.type === "tool-result"
|
|
3610
3664
|
);
|
|
3611
|
-
|
|
3665
|
+
const approvalResponseParts = toolContent.filter(
|
|
3666
|
+
(part) => part.type === "tool-approval-response"
|
|
3667
|
+
);
|
|
3668
|
+
if (!toolResponsePromptTemplate) {
|
|
3669
|
+
throw new Error(
|
|
3670
|
+
'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.'
|
|
3671
|
+
);
|
|
3672
|
+
}
|
|
3673
|
+
return processToolMessage(
|
|
3674
|
+
toolResultParts,
|
|
3675
|
+
approvalResponseParts,
|
|
3676
|
+
toolResponsePromptTemplate
|
|
3677
|
+
);
|
|
3612
3678
|
}
|
|
3613
3679
|
return message;
|
|
3614
3680
|
}
|
|
@@ -3671,22 +3737,28 @@ ${currentContent}` }]
|
|
|
3671
3737
|
}
|
|
3672
3738
|
return processedPrompt;
|
|
3673
3739
|
}
|
|
3674
|
-
function convertToolPrompt(prompt, resolvedProtocol, providerOptions) {
|
|
3740
|
+
function convertToolPrompt(prompt, resolvedProtocol, toolResponsePromptTemplate, providerOptions) {
|
|
3675
3741
|
let processedPrompt = prompt.map(
|
|
3676
|
-
(message) => processMessage(
|
|
3742
|
+
(message) => processMessage(
|
|
3743
|
+
message,
|
|
3744
|
+
resolvedProtocol,
|
|
3745
|
+
providerOptions,
|
|
3746
|
+
toolResponsePromptTemplate
|
|
3747
|
+
)
|
|
3677
3748
|
);
|
|
3678
3749
|
processedPrompt = condenseTextContent(processedPrompt);
|
|
3679
3750
|
processedPrompt = mergeConsecutiveUserMessages(processedPrompt);
|
|
3680
3751
|
return processedPrompt;
|
|
3681
3752
|
}
|
|
3682
3753
|
|
|
3683
|
-
// src/
|
|
3754
|
+
// src/tool-call-middleware.ts
|
|
3684
3755
|
function createToolMiddleware({
|
|
3685
3756
|
protocol,
|
|
3686
3757
|
toolSystemPromptTemplate,
|
|
3758
|
+
toolResponsePromptTemplate,
|
|
3687
3759
|
placement = "last"
|
|
3688
3760
|
}) {
|
|
3689
|
-
const resolvedProtocol =
|
|
3761
|
+
const resolvedProtocol = isTCMProtocolFactory(protocol) ? protocol() : protocol;
|
|
3690
3762
|
return {
|
|
3691
3763
|
specificationVersion: "v3",
|
|
3692
3764
|
wrapStream: ({ doStream, doGenerate, params }) => {
|
|
@@ -3711,76 +3783,28 @@ function createToolMiddleware({
|
|
|
3711
3783
|
transformParams: async ({ params }) => transformParams({
|
|
3712
3784
|
protocol: resolvedProtocol,
|
|
3713
3785
|
toolSystemPromptTemplate,
|
|
3786
|
+
toolResponsePromptTemplate,
|
|
3714
3787
|
placement,
|
|
3715
3788
|
params
|
|
3716
3789
|
})
|
|
3717
3790
|
};
|
|
3718
3791
|
}
|
|
3719
3792
|
|
|
3720
|
-
// src/
|
|
3721
|
-
var gemmaToolMiddleware = createToolMiddleware({
|
|
3722
|
-
protocol: jsonMixProtocol({
|
|
3723
|
-
toolCallStart: "```tool_call\n",
|
|
3724
|
-
toolCallEnd: "\n```",
|
|
3725
|
-
toolResponseStart: "```tool_response\n",
|
|
3726
|
-
toolResponseEnd: "\n```"
|
|
3727
|
-
}),
|
|
3728
|
-
toolSystemPromptTemplate(tools) {
|
|
3729
|
-
return `You have access to functions. If you decide to invoke any of the function(s),
|
|
3730
|
-
you MUST put it in the format of markdown code fence block with the language name of tool_call , e.g.
|
|
3731
|
-
\`\`\`tool_call
|
|
3732
|
-
{'name': <function-name>, 'arguments': <args-dict>}
|
|
3733
|
-
\`\`\`
|
|
3734
|
-
You SHOULD NOT include any other text in the response if you call a function
|
|
3735
|
-
${tools}`;
|
|
3736
|
-
}
|
|
3737
|
-
});
|
|
3793
|
+
// src/preconfigured-middleware.ts
|
|
3738
3794
|
var hermesToolMiddleware = createToolMiddleware({
|
|
3739
|
-
protocol:
|
|
3740
|
-
toolSystemPromptTemplate
|
|
3741
|
-
|
|
3742
|
-
You are provided with function signatures within <tools></tools> XML tags.
|
|
3743
|
-
You may call one or more functions to assist with the user query.
|
|
3744
|
-
Don't make assumptions about what values to plug into functions.
|
|
3745
|
-
Here are the available tools: <tools>${tools}</tools>
|
|
3746
|
-
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"]}
|
|
3747
|
-
For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
|
|
3748
|
-
<tool_call>
|
|
3749
|
-
{"name": "<function-name>", "arguments": <args-dict>}
|
|
3750
|
-
</tool_call>`;
|
|
3751
|
-
}
|
|
3795
|
+
protocol: jsonProtocol({}),
|
|
3796
|
+
toolSystemPromptTemplate: hermesSystemPromptTemplate,
|
|
3797
|
+
toolResponsePromptTemplate: formatToolResponseAsJsonInXml
|
|
3752
3798
|
});
|
|
3753
|
-
var
|
|
3754
|
-
protocol:
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
return `# Tools
|
|
3758
|
-
|
|
3759
|
-
You may call one or more functions to assist with the user query.
|
|
3760
|
-
|
|
3761
|
-
You are provided with function signatures within <tools></tools> XML tags:
|
|
3762
|
-
<tools>${tools}</tools>
|
|
3763
|
-
|
|
3764
|
-
# Rules
|
|
3765
|
-
- Use exactly one XML element whose tag name is the function name.
|
|
3766
|
-
- Put each parameter as a child element.
|
|
3767
|
-
- Values must follow the schema exactly (numbers, arrays, objects, enums \u2192 copy as-is).
|
|
3768
|
-
- Do not add or remove functions or parameters.
|
|
3769
|
-
- Each required parameter must appear once.
|
|
3770
|
-
- Output nothing before or after the function call.
|
|
3771
|
-
- 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.
|
|
3772
|
-
|
|
3773
|
-
# Example
|
|
3774
|
-
<get_weather>
|
|
3775
|
-
<location>New York</location>
|
|
3776
|
-
<unit>celsius</unit>
|
|
3777
|
-
</get_weather>`;
|
|
3778
|
-
}
|
|
3799
|
+
var xmlToolMiddleware = createToolMiddleware({
|
|
3800
|
+
protocol: xmlProtocol({}),
|
|
3801
|
+
toolSystemPromptTemplate: xmlSystemPromptTemplate,
|
|
3802
|
+
toolResponsePromptTemplate: formatToolResponseAsXml
|
|
3779
3803
|
});
|
|
3780
|
-
var
|
|
3781
|
-
protocol:
|
|
3782
|
-
|
|
3783
|
-
|
|
3804
|
+
var yamlToolMiddleware = createToolMiddleware({
|
|
3805
|
+
protocol: yamlProtocol({}),
|
|
3806
|
+
toolSystemPromptTemplate: yamlSystemPromptTemplate,
|
|
3807
|
+
toolResponsePromptTemplate: formatToolResponseAsXml
|
|
3784
3808
|
});
|
|
3785
3809
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3786
3810
|
0 && (module.exports = {
|
|
@@ -3797,31 +3821,33 @@ var orchestratorToolMiddleware = createToolMiddleware({
|
|
|
3797
3821
|
escapeRegExp,
|
|
3798
3822
|
extractOnErrorOption,
|
|
3799
3823
|
extractToolNamesFromOriginalTools,
|
|
3800
|
-
gemmaToolMiddleware,
|
|
3801
3824
|
getDebugLevel,
|
|
3802
3825
|
getPotentialStartIndex,
|
|
3803
3826
|
hasInputProperty,
|
|
3804
3827
|
hermesToolMiddleware,
|
|
3805
3828
|
isProtocolFactory,
|
|
3806
|
-
|
|
3829
|
+
isTCMProtocolFactory,
|
|
3807
3830
|
isToolChoiceActive,
|
|
3808
3831
|
isToolResultPart,
|
|
3809
|
-
|
|
3832
|
+
jsonProtocol,
|
|
3810
3833
|
logParseFailure,
|
|
3811
3834
|
logParsedChunk,
|
|
3812
3835
|
logParsedSummary,
|
|
3813
3836
|
logRawChunk,
|
|
3814
3837
|
mergePipelineConfigs,
|
|
3815
|
-
morphXmlProtocol,
|
|
3816
|
-
morphXmlToolMiddleware,
|
|
3817
3838
|
normalizeCloseTagsHeuristic,
|
|
3818
|
-
orchestratorSystemPromptTemplate,
|
|
3819
|
-
orchestratorToolMiddleware,
|
|
3820
3839
|
originalToolsSchema,
|
|
3821
3840
|
parse,
|
|
3822
3841
|
repairAgainstSchemaHeuristic,
|
|
3823
3842
|
stringify,
|
|
3843
|
+
toolChoiceStream,
|
|
3824
3844
|
transform,
|
|
3825
|
-
|
|
3845
|
+
transformParams,
|
|
3846
|
+
wrapGenerate,
|
|
3847
|
+
wrapStream,
|
|
3848
|
+
xmlProtocol,
|
|
3849
|
+
xmlToolMiddleware,
|
|
3850
|
+
yamlProtocol,
|
|
3851
|
+
yamlToolMiddleware
|
|
3826
3852
|
});
|
|
3827
3853
|
//# sourceMappingURL=index.cjs.map
|