@ai-sdk-tool/parser 4.0.0 → 4.1.0
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 +109 -20
- package/dist/{chunk-2KK5BDZF.js → chunk-GMTE7BY5.js} +23 -6
- package/dist/chunk-GMTE7BY5.js.map +1 -0
- package/dist/{chunk-CXWS24JX.js → chunk-MHZC45AC.js} +5 -2
- package/dist/{chunk-CXWS24JX.js.map → chunk-MHZC45AC.js.map} +1 -1
- package/dist/{chunk-ERJKQKCR.js → chunk-O6NWVXQD.js} +2578 -379
- package/dist/chunk-O6NWVXQD.js.map +1 -0
- package/dist/{chunk-IX4FJELL.js → chunk-QBZNMO5C.js} +1 -1
- package/dist/chunk-QBZNMO5C.js.map +1 -0
- package/dist/community.cjs +7873 -5545
- package/dist/community.cjs.map +1 -1
- package/dist/community.d.cts +7 -1
- package/dist/community.d.ts +7 -1
- package/dist/community.js +128 -8
- package/dist/community.js.map +1 -1
- package/dist/index.cjs +2600 -384
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -24
- package/dist/index.d.ts +33 -24
- package/dist/index.js +22 -14
- package/dist/rjson.cjs.map +1 -1
- package/dist/rjson.d.cts +15 -15
- package/dist/rjson.d.ts +15 -15
- package/dist/rjson.js +1 -1
- package/dist/rxml.cjs +22 -5
- package/dist/rxml.cjs.map +1 -1
- package/dist/rxml.d.cts +23 -23
- package/dist/rxml.d.ts +23 -23
- package/dist/rxml.js +2 -2
- package/dist/schema-coerce.cjs +22 -5
- package/dist/schema-coerce.cjs.map +1 -1
- package/dist/schema-coerce.js +1 -1
- package/package.json +6 -6
- package/dist/chunk-2KK5BDZF.js.map +0 -1
- package/dist/chunk-ERJKQKCR.js.map +0 -1
- package/dist/chunk-IX4FJELL.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
+
Qwen3CoderToolParser: () => Qwen3CoderToolParser,
|
|
33
34
|
createDynamicIfThenElseSchema: () => createDynamicIfThenElseSchema,
|
|
34
35
|
createToolMiddleware: () => createToolMiddleware,
|
|
35
36
|
decodeOriginalTools: () => decodeOriginalTools,
|
|
@@ -40,28 +41,31 @@ __export(src_exports, {
|
|
|
40
41
|
getDebugLevel: () => getDebugLevel,
|
|
41
42
|
getPotentialStartIndex: () => getPotentialStartIndex,
|
|
42
43
|
hasInputProperty: () => hasInputProperty,
|
|
44
|
+
hermesProtocol: () => hermesProtocol,
|
|
43
45
|
hermesToolMiddleware: () => hermesToolMiddleware,
|
|
44
46
|
isProtocolFactory: () => isProtocolFactory,
|
|
45
47
|
isTCMProtocolFactory: () => isTCMProtocolFactory,
|
|
46
48
|
isToolChoiceActive: () => isToolChoiceActive,
|
|
47
49
|
isToolResultPart: () => isToolResultPart,
|
|
48
|
-
jsonProtocol: () => jsonProtocol,
|
|
49
50
|
logParseFailure: () => logParseFailure,
|
|
50
51
|
logParsedChunk: () => logParsedChunk,
|
|
51
52
|
logParsedSummary: () => logParsedSummary,
|
|
52
53
|
logRawChunk: () => logRawChunk,
|
|
54
|
+
morphXmlProtocol: () => morphXmlProtocol,
|
|
55
|
+
morphXmlToolMiddleware: () => morphXmlToolMiddleware,
|
|
53
56
|
originalToolsSchema: () => originalToolsSchema,
|
|
54
57
|
parse: () => parse,
|
|
58
|
+
qwen3CoderProtocol: () => qwen3CoderProtocol,
|
|
59
|
+
qwen3CoderToolMiddleware: () => qwen3CoderToolMiddleware,
|
|
55
60
|
stringify: () => stringify,
|
|
56
61
|
toolChoiceStream: () => toolChoiceStream,
|
|
57
62
|
transform: () => transform,
|
|
58
63
|
transformParams: () => transformParams,
|
|
64
|
+
uiTarsXmlProtocol: () => uiTarsXmlProtocol,
|
|
59
65
|
wrapGenerate: () => wrapGenerate,
|
|
60
66
|
wrapStream: () => wrapStream,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
yamlProtocol: () => yamlProtocol,
|
|
64
|
-
yamlToolMiddleware: () => yamlToolMiddleware
|
|
67
|
+
yamlXmlProtocol: () => yamlXmlProtocol,
|
|
68
|
+
yamlXmlToolMiddleware: () => yamlXmlToolMiddleware
|
|
65
69
|
});
|
|
66
70
|
module.exports = __toCommonJS(src_exports);
|
|
67
71
|
|
|
@@ -894,9 +898,19 @@ function getPotentialStartIndex(text, searchedText) {
|
|
|
894
898
|
if (directIndex !== -1) {
|
|
895
899
|
return directIndex;
|
|
896
900
|
}
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
901
|
+
const textLength = text.length;
|
|
902
|
+
const searchedTextLength = searchedText.length;
|
|
903
|
+
const startAt = Math.max(0, textLength - searchedTextLength + 1);
|
|
904
|
+
for (let i = startAt; i < textLength; i++) {
|
|
905
|
+
let match = true;
|
|
906
|
+
const currentSuffixLength = textLength - i;
|
|
907
|
+
for (let j = 0; j < currentSuffixLength; j++) {
|
|
908
|
+
if (text[i + j] !== searchedText[j]) {
|
|
909
|
+
match = false;
|
|
910
|
+
break;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
if (match) {
|
|
900
914
|
return i;
|
|
901
915
|
}
|
|
902
916
|
}
|
|
@@ -970,7 +984,7 @@ function escapeRegExp(literal) {
|
|
|
970
984
|
return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
971
985
|
}
|
|
972
986
|
|
|
973
|
-
// src/core/protocols/
|
|
987
|
+
// src/core/protocols/hermes-protocol.ts
|
|
974
988
|
function shouldEmitRawToolCallTextOnError(options) {
|
|
975
989
|
return (options == null ? void 0 : options.emitRawToolCallTextOnError) === true;
|
|
976
990
|
}
|
|
@@ -1513,7 +1527,7 @@ function handlePartialTag(state, controller, toolCallStart, toolCallEnd) {
|
|
|
1513
1527
|
state.buffer = "";
|
|
1514
1528
|
}
|
|
1515
1529
|
}
|
|
1516
|
-
var
|
|
1530
|
+
var hermesProtocol = ({
|
|
1517
1531
|
toolCallStart = "<tool_call>",
|
|
1518
1532
|
toolCallEnd = "</tool_call>"
|
|
1519
1533
|
} = {}) => ({
|
|
@@ -1615,14 +1629,6 @@ var jsonProtocol = ({
|
|
|
1615
1629
|
}
|
|
1616
1630
|
});
|
|
1617
1631
|
|
|
1618
|
-
// src/core/protocols/protocol-interface.ts
|
|
1619
|
-
function isProtocolFactory(protocol) {
|
|
1620
|
-
return typeof protocol === "function";
|
|
1621
|
-
}
|
|
1622
|
-
function isTCMProtocolFactory(protocol) {
|
|
1623
|
-
return typeof protocol === "function";
|
|
1624
|
-
}
|
|
1625
|
-
|
|
1626
1632
|
// src/rxml/errors/types.ts
|
|
1627
1633
|
var RXMLParseError = class extends Error {
|
|
1628
1634
|
constructor(message, cause, line, column) {
|
|
@@ -2184,13 +2190,20 @@ function coerceStringWithoutSchema(value) {
|
|
|
2184
2190
|
}
|
|
2185
2191
|
function coerceStringToObject(s, unwrapped) {
|
|
2186
2192
|
try {
|
|
2187
|
-
|
|
2188
|
-
normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
|
|
2189
|
-
const obj = JSON.parse(normalized);
|
|
2193
|
+
const obj = JSON.parse(s);
|
|
2190
2194
|
if (obj && typeof obj === "object" && !Array.isArray(obj)) {
|
|
2191
2195
|
return coerceObjectToObject(obj, unwrapped);
|
|
2192
2196
|
}
|
|
2193
2197
|
} catch (e) {
|
|
2198
|
+
try {
|
|
2199
|
+
let normalized = s.replace(/'/g, '"');
|
|
2200
|
+
normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
|
|
2201
|
+
const obj = JSON.parse(normalized);
|
|
2202
|
+
if (obj && typeof obj === "object" && !Array.isArray(obj)) {
|
|
2203
|
+
return coerceObjectToObject(obj, unwrapped);
|
|
2204
|
+
}
|
|
2205
|
+
} catch (e2) {
|
|
2206
|
+
}
|
|
2194
2207
|
}
|
|
2195
2208
|
return null;
|
|
2196
2209
|
}
|
|
@@ -2198,8 +2211,7 @@ function coerceStringToArray(s, unwrapped) {
|
|
|
2198
2211
|
const prefixItems = Array.isArray(unwrapped.prefixItems) ? unwrapped.prefixItems : void 0;
|
|
2199
2212
|
const itemsSchema = unwrapped.items;
|
|
2200
2213
|
try {
|
|
2201
|
-
const
|
|
2202
|
-
const arr = JSON.parse(normalized);
|
|
2214
|
+
const arr = JSON.parse(s);
|
|
2203
2215
|
if (Array.isArray(arr)) {
|
|
2204
2216
|
if (prefixItems && arr.length === prefixItems.length) {
|
|
2205
2217
|
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
@@ -2207,6 +2219,17 @@ function coerceStringToArray(s, unwrapped) {
|
|
|
2207
2219
|
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
2208
2220
|
}
|
|
2209
2221
|
} catch (e) {
|
|
2222
|
+
try {
|
|
2223
|
+
const normalized = s.replace(/'/g, '"');
|
|
2224
|
+
const arr = JSON.parse(normalized);
|
|
2225
|
+
if (Array.isArray(arr)) {
|
|
2226
|
+
if (prefixItems && arr.length === prefixItems.length) {
|
|
2227
|
+
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
2228
|
+
}
|
|
2229
|
+
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
2230
|
+
}
|
|
2231
|
+
} catch (e2) {
|
|
2232
|
+
}
|
|
2210
2233
|
const csv = s.includes("\n") ? s.split(NEWLINE_SPLIT_REGEX) : s.split(COMMA_SPLIT_REGEX);
|
|
2211
2234
|
const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);
|
|
2212
2235
|
if (prefixItems && trimmed.length === prefixItems.length) {
|
|
@@ -4675,7 +4698,7 @@ ${body}
|
|
|
4675
4698
|
</${rootTag}>`;
|
|
4676
4699
|
}
|
|
4677
4700
|
|
|
4678
|
-
// src/core/protocols/xml-protocol.ts
|
|
4701
|
+
// src/core/protocols/morph-xml-protocol.ts
|
|
4679
4702
|
function getToolSchema(tools, toolName) {
|
|
4680
4703
|
var _a;
|
|
4681
4704
|
return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
@@ -5651,7 +5674,7 @@ function findToolCallsWithFallbacks(text, toolNames) {
|
|
|
5651
5674
|
}
|
|
5652
5675
|
return { parseText, toolCalls };
|
|
5653
5676
|
}
|
|
5654
|
-
var
|
|
5677
|
+
var morphXmlProtocol = (protocolOptions) => {
|
|
5655
5678
|
var _a;
|
|
5656
5679
|
const parseOptions = {
|
|
5657
5680
|
repair: true,
|
|
@@ -5914,192 +5937,2062 @@ var xmlProtocol = (protocolOptions) => {
|
|
|
5914
5937
|
};
|
|
5915
5938
|
};
|
|
5916
5939
|
|
|
5917
|
-
// src/core/protocols/
|
|
5918
|
-
|
|
5919
|
-
|
|
5920
|
-
return (options == null ? void 0 : options.emitRawToolCallTextOnError) === true;
|
|
5940
|
+
// src/core/protocols/protocol-interface.ts
|
|
5941
|
+
function isProtocolFactory(protocol) {
|
|
5942
|
+
return typeof protocol === "function";
|
|
5921
5943
|
}
|
|
5922
|
-
|
|
5923
|
-
|
|
5924
|
-
let pattern = selfClosingTagCache2.get(toolName);
|
|
5925
|
-
if (!pattern) {
|
|
5926
|
-
pattern = new RegExp(`<\\s*${escapeRegExp(toolName)}\\s*/>`, "g");
|
|
5927
|
-
selfClosingTagCache2.set(toolName, pattern);
|
|
5928
|
-
}
|
|
5929
|
-
return pattern;
|
|
5944
|
+
function isTCMProtocolFactory(protocol) {
|
|
5945
|
+
return typeof protocol === "function";
|
|
5930
5946
|
}
|
|
5931
|
-
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
var
|
|
5935
|
-
|
|
5936
|
-
|
|
5937
|
-
|
|
5938
|
-
|
|
5939
|
-
|
|
5940
|
-
|
|
5947
|
+
|
|
5948
|
+
// src/core/utils/tool-call-coercion.ts
|
|
5949
|
+
function coerceToolCallInput(toolName, input, tools) {
|
|
5950
|
+
var _a;
|
|
5951
|
+
let args = {};
|
|
5952
|
+
if (typeof input === "string") {
|
|
5953
|
+
try {
|
|
5954
|
+
args = JSON.parse(input);
|
|
5955
|
+
} catch (e) {
|
|
5956
|
+
return;
|
|
5957
|
+
}
|
|
5958
|
+
} else if (input && typeof input === "object") {
|
|
5959
|
+
args = input;
|
|
5960
|
+
} else {
|
|
5961
|
+
return;
|
|
5941
5962
|
}
|
|
5942
|
-
const
|
|
5943
|
-
const
|
|
5944
|
-
|
|
5945
|
-
|
|
5963
|
+
const schema = (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
5964
|
+
const coerced = coerceBySchema(args, schema);
|
|
5965
|
+
return JSON.stringify(coerced != null ? coerced : {});
|
|
5966
|
+
}
|
|
5967
|
+
function coerceToolCallPart(part, tools) {
|
|
5968
|
+
const coercedInput = coerceToolCallInput(part.toolName, part.input, tools);
|
|
5969
|
+
if (coercedInput === void 0) {
|
|
5970
|
+
return part;
|
|
5946
5971
|
}
|
|
5947
|
-
|
|
5948
|
-
...
|
|
5949
|
-
|
|
5950
|
-
|
|
5951
|
-
|
|
5952
|
-
|
|
5953
|
-
|
|
5954
|
-
|
|
5972
|
+
return {
|
|
5973
|
+
...part,
|
|
5974
|
+
input: coercedInput
|
|
5975
|
+
};
|
|
5976
|
+
}
|
|
5977
|
+
|
|
5978
|
+
// src/core/protocols/qwen3coder-protocol.ts
|
|
5979
|
+
function shouldEmitRawToolCallTextOnError3(options) {
|
|
5980
|
+
return (options == null ? void 0 : options.emitRawToolCallTextOnError) === true;
|
|
5981
|
+
}
|
|
5982
|
+
var TOOL_CALL_OPEN_RE = /<tool_call\b[^>]*>/i;
|
|
5983
|
+
var TOOL_CALL_CLOSE_RE = /<\/tool_call\s*>/i;
|
|
5984
|
+
var TOOL_CALL_CLOSE_TRAILING_RE = /<\/tool_call\s*>\s*$/i;
|
|
5985
|
+
var TOOL_CALL_BLOCK_RE = /<tool_call\b[^>]*>[\s\S]*?<\/tool_call\s*>/gi;
|
|
5986
|
+
var LEADING_CALL_CLOSE_TAG_RE = /^\s*<\s*\/\s*(?:tool_call|function|call|tool|invoke)\s*>/i;
|
|
5987
|
+
var CALL_BLOCK_RE = /<(call|function|tool|invoke)\b[^>]*>[\s\S]*?<\/\1\s*>/gi;
|
|
5988
|
+
var QWEN3CODER_TOOL_PARSER_PARAM_TAG_NAMES = /* @__PURE__ */ new Set([
|
|
5989
|
+
"parameter",
|
|
5990
|
+
"param",
|
|
5991
|
+
"argument",
|
|
5992
|
+
"arg"
|
|
5993
|
+
]);
|
|
5994
|
+
var QWEN3CODER_TOOL_PARSER_CALL_TAG_NAMES = /* @__PURE__ */ new Set([
|
|
5995
|
+
"function",
|
|
5996
|
+
"call",
|
|
5997
|
+
"tool",
|
|
5998
|
+
"invoke",
|
|
5999
|
+
"tool_call"
|
|
6000
|
+
]);
|
|
6001
|
+
var CALL_SHORTHAND_VALUE_RE = /^<\s*(call|function|tool|invoke)\b\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>/]+))/i;
|
|
6002
|
+
var QWEN3CODER_TOOL_PARSER_STREAM_CALL_OPEN_START_RE = /<\s*(?!\/)\s*(call|function|tool|invoke)\b/i;
|
|
6003
|
+
var QWEN3CODER_TOOL_PARSER_STREAM_CALL_OPEN_TAG_RE = /<\s*(?!\/)\s*(call|function|tool|invoke)\b[^>]*>/i;
|
|
6004
|
+
var QWEN3CODER_TOOL_PARSER_STREAM_TOOL_CALL_CLOSE_TAG_RE = /<\s*\/\s*tool_call\s*>/i;
|
|
6005
|
+
var QWEN3CODER_TOOL_PARSER_STREAM_NAME_OR_PARAM_SIGNAL_RE = /<\s*(?!\/)\s*(name|tool_name|parameter|param|argument|arg)\b/i;
|
|
6006
|
+
var QWEN3CODER_TOOL_PARSER_STREAM_NAME_TAG_RE = /<\s*(name|tool_name)\b[^>]*>([\s\S]*?)<\s*\/\s*\1\s*>/i;
|
|
6007
|
+
var QWEN3CODER_TOOL_PARSER_STREAM_SELF_CLOSING_TAG_RE = /\/\s*>$/;
|
|
6008
|
+
function isAsciiWhitespace(ch) {
|
|
6009
|
+
return ch === " " || ch === "\n" || ch === "\r" || ch === " " || ch === "\f";
|
|
6010
|
+
}
|
|
6011
|
+
function skipAsciiWhitespace(text, index) {
|
|
6012
|
+
var _a;
|
|
6013
|
+
let i = index;
|
|
6014
|
+
while (i < text.length && isAsciiWhitespace((_a = text[i]) != null ? _a : "")) {
|
|
6015
|
+
i += 1;
|
|
5955
6016
|
}
|
|
5956
|
-
return
|
|
6017
|
+
return i;
|
|
5957
6018
|
}
|
|
5958
|
-
function
|
|
5959
|
-
|
|
5960
|
-
|
|
5961
|
-
const
|
|
5962
|
-
const
|
|
5963
|
-
|
|
5964
|
-
|
|
6019
|
+
function stripLeadingToolCallCloseTags(text) {
|
|
6020
|
+
let out = text;
|
|
6021
|
+
while (true) {
|
|
6022
|
+
const start = skipAsciiWhitespace(out, 0);
|
|
6023
|
+
const trimmed = out.slice(start);
|
|
6024
|
+
const match = TOOL_CALL_CLOSE_RE.exec(trimmed);
|
|
6025
|
+
if (!match || match.index !== 0 || !match[0]) {
|
|
6026
|
+
return out;
|
|
5965
6027
|
}
|
|
5966
|
-
|
|
5967
|
-
|
|
6028
|
+
out = out.slice(start + match[0].length);
|
|
6029
|
+
}
|
|
6030
|
+
}
|
|
6031
|
+
function stripTrailingToolCallCloseTags(text) {
|
|
6032
|
+
let out = text;
|
|
6033
|
+
while (true) {
|
|
6034
|
+
const next = out.replace(TOOL_CALL_CLOSE_TRAILING_RE, "");
|
|
6035
|
+
if (next === out) {
|
|
6036
|
+
return out;
|
|
5968
6037
|
}
|
|
5969
|
-
|
|
5970
|
-
} catch (error) {
|
|
5971
|
-
return {
|
|
5972
|
-
value: null,
|
|
5973
|
-
errors: [
|
|
5974
|
-
error instanceof Error ? error.message : "Unknown YAML parsing error"
|
|
5975
|
-
]
|
|
5976
|
-
};
|
|
6038
|
+
out = next;
|
|
5977
6039
|
}
|
|
5978
6040
|
}
|
|
5979
|
-
function
|
|
6041
|
+
function isTagBoundaryChar(ch) {
|
|
6042
|
+
return ch === "" || isAsciiWhitespace(ch) || ch === ">" || ch === "/";
|
|
6043
|
+
}
|
|
6044
|
+
function findTagEndIndex(text, startIndex) {
|
|
5980
6045
|
var _a;
|
|
5981
|
-
|
|
5982
|
-
let
|
|
5983
|
-
|
|
5984
|
-
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
|
|
5989
|
-
|
|
5990
|
-
|
|
5991
|
-
|
|
5992
|
-
|
|
6046
|
+
let quote = null;
|
|
6047
|
+
for (let i = startIndex; i < text.length; i += 1) {
|
|
6048
|
+
const ch = (_a = text[i]) != null ? _a : "";
|
|
6049
|
+
if (quote) {
|
|
6050
|
+
if (ch === quote) {
|
|
6051
|
+
quote = null;
|
|
6052
|
+
}
|
|
6053
|
+
continue;
|
|
6054
|
+
}
|
|
6055
|
+
if (ch === '"' || ch === "'") {
|
|
6056
|
+
quote = ch;
|
|
6057
|
+
continue;
|
|
6058
|
+
}
|
|
6059
|
+
if (ch === ">") {
|
|
6060
|
+
return i;
|
|
5993
6061
|
}
|
|
5994
|
-
index -= 1;
|
|
5995
6062
|
}
|
|
5996
6063
|
return null;
|
|
5997
6064
|
}
|
|
5998
|
-
function
|
|
5999
|
-
|
|
6000
|
-
|
|
6065
|
+
function parseShorthandValue(openTag, tagNameLower) {
|
|
6066
|
+
var _a, _b;
|
|
6067
|
+
let i = 1;
|
|
6068
|
+
i = skipAsciiWhitespace(openTag, i);
|
|
6069
|
+
if (!openTag.toLowerCase().startsWith(tagNameLower, i)) {
|
|
6001
6070
|
return null;
|
|
6002
6071
|
}
|
|
6003
|
-
|
|
6072
|
+
i += tagNameLower.length;
|
|
6073
|
+
i = skipAsciiWhitespace(openTag, i);
|
|
6074
|
+
if (openTag[i] !== "=") {
|
|
6075
|
+
return null;
|
|
6076
|
+
}
|
|
6077
|
+
i += 1;
|
|
6078
|
+
i = skipAsciiWhitespace(openTag, i);
|
|
6079
|
+
const quote = (_a = openTag[i]) != null ? _a : "";
|
|
6080
|
+
if (quote === '"' || quote === "'") {
|
|
6081
|
+
const end = openTag.indexOf(quote, i + 1);
|
|
6082
|
+
if (end === -1) {
|
|
6083
|
+
return null;
|
|
6084
|
+
}
|
|
6085
|
+
return openTag.slice(i + 1, end);
|
|
6086
|
+
}
|
|
6087
|
+
const start = i;
|
|
6088
|
+
while (i < openTag.length) {
|
|
6089
|
+
const ch = (_b = openTag[i]) != null ? _b : "";
|
|
6090
|
+
if (isAsciiWhitespace(ch) || ch === ">" || ch === "/") {
|
|
6091
|
+
break;
|
|
6092
|
+
}
|
|
6093
|
+
i += 1;
|
|
6094
|
+
}
|
|
6095
|
+
const value = openTag.slice(start, i);
|
|
6096
|
+
return value.length > 0 ? value : null;
|
|
6004
6097
|
}
|
|
6005
|
-
function
|
|
6006
|
-
const
|
|
6007
|
-
if (
|
|
6008
|
-
return
|
|
6098
|
+
function parseQwen3CoderToolParserParamName(openTag, tagNameLower) {
|
|
6099
|
+
const shorthand = parseShorthandValue(openTag, tagNameLower);
|
|
6100
|
+
if (shorthand != null) {
|
|
6101
|
+
return unescapeXml(shorthand);
|
|
6009
6102
|
}
|
|
6010
|
-
return
|
|
6103
|
+
return getAttributeValue(openTag, "name");
|
|
6011
6104
|
}
|
|
6012
|
-
function
|
|
6013
|
-
|
|
6014
|
-
|
|
6015
|
-
return false;
|
|
6105
|
+
function getCdataSectionNextIndex(textLower, startIndex) {
|
|
6106
|
+
if (!textLower.startsWith("<![cdata[", startIndex)) {
|
|
6107
|
+
return startIndex;
|
|
6016
6108
|
}
|
|
6017
|
-
|
|
6109
|
+
const cdataEnd = textLower.indexOf("]]>", startIndex + "<![cdata[".length);
|
|
6110
|
+
if (cdataEnd === -1) {
|
|
6111
|
+
return null;
|
|
6112
|
+
}
|
|
6113
|
+
return cdataEnd + 3;
|
|
6018
6114
|
}
|
|
6019
|
-
function
|
|
6115
|
+
function parseMatchingTagHeader(textLower, lt, tagNameLower) {
|
|
6020
6116
|
var _a;
|
|
6021
|
-
|
|
6022
|
-
|
|
6023
|
-
|
|
6117
|
+
let i = skipAsciiWhitespace(textLower, lt + 1);
|
|
6118
|
+
const isClosing = textLower[i] === "/";
|
|
6119
|
+
if (isClosing) {
|
|
6120
|
+
i += 1;
|
|
6121
|
+
i = skipAsciiWhitespace(textLower, i);
|
|
6024
6122
|
}
|
|
6025
|
-
|
|
6026
|
-
|
|
6027
|
-
return false;
|
|
6123
|
+
if (!textLower.startsWith(tagNameLower, i)) {
|
|
6124
|
+
return null;
|
|
6028
6125
|
}
|
|
6029
|
-
|
|
6030
|
-
|
|
6126
|
+
const afterName = i + tagNameLower.length;
|
|
6127
|
+
const boundary = (_a = textLower[afterName]) != null ? _a : "";
|
|
6128
|
+
const validBoundary = isClosing ? isTagBoundaryChar(boundary) : isTagBoundaryChar(boundary) || boundary === "=";
|
|
6129
|
+
if (boundary && !validBoundary) {
|
|
6130
|
+
return null;
|
|
6031
6131
|
}
|
|
6032
|
-
|
|
6033
|
-
|
|
6034
|
-
|
|
6035
|
-
|
|
6036
|
-
|
|
6037
|
-
|
|
6038
|
-
|
|
6132
|
+
return { isClosing, afterName };
|
|
6133
|
+
}
|
|
6134
|
+
function isSelfClosingXmlTag(textLower, lt, gt) {
|
|
6135
|
+
return textLower.slice(lt, gt + 1).trimEnd().endsWith("/>");
|
|
6136
|
+
}
|
|
6137
|
+
function findClosingTagEnd(textLower, startIndex, tagNameLower) {
|
|
6138
|
+
let depth = 1;
|
|
6139
|
+
let index = startIndex;
|
|
6140
|
+
while (true) {
|
|
6141
|
+
const lt = textLower.indexOf("<", index);
|
|
6142
|
+
if (lt === -1) {
|
|
6143
|
+
return null;
|
|
6144
|
+
}
|
|
6145
|
+
const cdataNextIndex = getCdataSectionNextIndex(textLower, lt);
|
|
6146
|
+
if (cdataNextIndex == null) {
|
|
6147
|
+
return null;
|
|
6148
|
+
}
|
|
6149
|
+
if (cdataNextIndex !== lt) {
|
|
6150
|
+
index = cdataNextIndex;
|
|
6039
6151
|
continue;
|
|
6040
6152
|
}
|
|
6041
|
-
const
|
|
6042
|
-
if (
|
|
6043
|
-
|
|
6153
|
+
const header = parseMatchingTagHeader(textLower, lt, tagNameLower);
|
|
6154
|
+
if (!header) {
|
|
6155
|
+
index = lt + 1;
|
|
6044
6156
|
continue;
|
|
6045
6157
|
}
|
|
6046
|
-
|
|
6047
|
-
|
|
6158
|
+
const gt = textLower.indexOf(">", header.afterName);
|
|
6159
|
+
if (gt === -1) {
|
|
6160
|
+
return null;
|
|
6048
6161
|
}
|
|
6049
|
-
if (
|
|
6050
|
-
|
|
6162
|
+
if (header.isClosing) {
|
|
6163
|
+
depth -= 1;
|
|
6164
|
+
if (depth === 0) {
|
|
6165
|
+
return { start: lt, end: gt + 1 };
|
|
6166
|
+
}
|
|
6167
|
+
index = gt + 1;
|
|
6168
|
+
continue;
|
|
6051
6169
|
}
|
|
6052
|
-
|
|
6170
|
+
const isSelfClosing = isSelfClosingXmlTag(textLower, lt, gt);
|
|
6171
|
+
if (!isSelfClosing) {
|
|
6172
|
+
depth += 1;
|
|
6173
|
+
}
|
|
6174
|
+
index = gt + 1;
|
|
6053
6175
|
}
|
|
6054
|
-
return false;
|
|
6055
6176
|
}
|
|
6056
|
-
function
|
|
6177
|
+
function findClosingTagStartWithBoundary(lowerText, valueStart, tagNameLower, allowEndOfStringBoundary) {
|
|
6057
6178
|
var _a;
|
|
6058
|
-
|
|
6059
|
-
|
|
6179
|
+
const needle = `</${tagNameLower}`;
|
|
6180
|
+
let searchIndex = valueStart;
|
|
6181
|
+
while (searchIndex < lowerText.length) {
|
|
6182
|
+
const found = lowerText.indexOf(needle, searchIndex);
|
|
6183
|
+
if (found === -1) {
|
|
6184
|
+
return -1;
|
|
6185
|
+
}
|
|
6186
|
+
const nextChar = (_a = lowerText[found + needle.length]) != null ? _a : "";
|
|
6187
|
+
if (nextChar === "" && !allowEndOfStringBoundary) {
|
|
6188
|
+
searchIndex = found + needle.length;
|
|
6189
|
+
continue;
|
|
6190
|
+
}
|
|
6191
|
+
if (isTagBoundaryChar(nextChar)) {
|
|
6192
|
+
return found;
|
|
6193
|
+
}
|
|
6194
|
+
searchIndex = found + needle.length;
|
|
6060
6195
|
}
|
|
6061
|
-
|
|
6062
|
-
|
|
6063
|
-
|
|
6064
|
-
|
|
6196
|
+
return -1;
|
|
6197
|
+
}
|
|
6198
|
+
function toSupportedCallEndTagName(tagNameLower) {
|
|
6199
|
+
var _a;
|
|
6200
|
+
const normalized = (_a = tagNameLower == null ? void 0 : tagNameLower.trim().toLowerCase()) != null ? _a : "";
|
|
6201
|
+
if (!normalized) {
|
|
6065
6202
|
return null;
|
|
6066
6203
|
}
|
|
6067
|
-
|
|
6068
|
-
|
|
6204
|
+
return QWEN3CODER_TOOL_PARSER_CALL_TAG_NAMES.has(normalized) ? normalized : null;
|
|
6205
|
+
}
|
|
6206
|
+
function findUnclosedParamBoundaryIndex(lowerText, valueStart, callEndTagNameLower, allowEndOfString) {
|
|
6207
|
+
const normalizedCallEndTag = toSupportedCallEndTagName(callEndTagNameLower);
|
|
6208
|
+
const callCloseIndex = normalizedCallEndTag ? findClosingTagStartWithBoundary(
|
|
6209
|
+
lowerText,
|
|
6210
|
+
valueStart,
|
|
6211
|
+
normalizedCallEndTag,
|
|
6212
|
+
allowEndOfString
|
|
6213
|
+
) : findClosingTagStartWithBoundary(
|
|
6214
|
+
lowerText,
|
|
6215
|
+
valueStart,
|
|
6216
|
+
"function",
|
|
6217
|
+
allowEndOfString
|
|
6218
|
+
);
|
|
6219
|
+
const indices = [
|
|
6220
|
+
lowerText.indexOf("<parameter", valueStart),
|
|
6221
|
+
lowerText.indexOf("<param", valueStart),
|
|
6222
|
+
lowerText.indexOf("<argument", valueStart),
|
|
6223
|
+
lowerText.indexOf("<arg", valueStart),
|
|
6224
|
+
callCloseIndex,
|
|
6225
|
+
findClosingTagStartWithBoundary(
|
|
6226
|
+
lowerText,
|
|
6227
|
+
valueStart,
|
|
6228
|
+
"tool_call",
|
|
6229
|
+
allowEndOfString
|
|
6230
|
+
),
|
|
6231
|
+
lowerText.indexOf("<function", valueStart)
|
|
6232
|
+
].filter((index) => index !== -1);
|
|
6233
|
+
if (indices.length === 0) {
|
|
6069
6234
|
return null;
|
|
6070
6235
|
}
|
|
6071
|
-
|
|
6072
|
-
|
|
6236
|
+
return Math.min(...indices);
|
|
6237
|
+
}
|
|
6238
|
+
function parseQwen3CoderToolParserParamTagNameLower(lowerText, startIndex) {
|
|
6239
|
+
var _a;
|
|
6240
|
+
let i = skipAsciiWhitespace(lowerText, startIndex + 1);
|
|
6241
|
+
if (i >= lowerText.length) {
|
|
6242
|
+
return { kind: "partial" };
|
|
6073
6243
|
}
|
|
6074
|
-
if (
|
|
6244
|
+
if (lowerText[i] === "/") {
|
|
6075
6245
|
return null;
|
|
6076
6246
|
}
|
|
6077
|
-
|
|
6078
|
-
|
|
6079
|
-
|
|
6080
|
-
|
|
6081
|
-
|
|
6247
|
+
const nameStart = i;
|
|
6248
|
+
while (i < lowerText.length) {
|
|
6249
|
+
const ch = (_a = lowerText[i]) != null ? _a : "";
|
|
6250
|
+
if (isAsciiWhitespace(ch) || ch === ">" || ch === "/" || ch === "=") {
|
|
6251
|
+
break;
|
|
6252
|
+
}
|
|
6253
|
+
i += 1;
|
|
6082
6254
|
}
|
|
6083
|
-
const
|
|
6084
|
-
if (!
|
|
6085
|
-
return
|
|
6255
|
+
const tagNameLower = lowerText.slice(nameStart, i);
|
|
6256
|
+
if (!QWEN3CODER_TOOL_PARSER_PARAM_TAG_NAMES.has(tagNameLower)) {
|
|
6257
|
+
return null;
|
|
6086
6258
|
}
|
|
6087
|
-
return
|
|
6088
|
-
}
|
|
6089
|
-
function hasUnstableProgressTail(normalized) {
|
|
6090
|
-
return hasIncompleteMappingTail(normalized) || hasIncompleteSequenceTail(normalized) || hasSplitNestedKeyTail(normalized) || hasUnterminatedPlainScalarTail(normalized);
|
|
6259
|
+
return { kind: "match", tagNameLower };
|
|
6091
6260
|
}
|
|
6092
|
-
function
|
|
6093
|
-
|
|
6094
|
-
|
|
6095
|
-
|
|
6261
|
+
function parseQwen3CoderToolParserUnclosedParamValue(options) {
|
|
6262
|
+
var _a;
|
|
6263
|
+
const valueStart = options.openEnd + 1;
|
|
6264
|
+
const boundaryIndex = findUnclosedParamBoundaryIndex(
|
|
6265
|
+
options.lowerText,
|
|
6266
|
+
valueStart,
|
|
6267
|
+
(_a = options.callEndTagNameLower) != null ? _a : null,
|
|
6268
|
+
options.allowEndOfString
|
|
6269
|
+
);
|
|
6270
|
+
if (boundaryIndex == null) {
|
|
6271
|
+
if (!options.allowEndOfString) {
|
|
6272
|
+
return {
|
|
6273
|
+
kind: "partial",
|
|
6274
|
+
start: options.startIndex,
|
|
6275
|
+
openEnd: options.openEnd
|
|
6276
|
+
};
|
|
6096
6277
|
}
|
|
6097
|
-
|
|
6098
|
-
|
|
6099
|
-
|
|
6100
|
-
|
|
6278
|
+
const rawValue2 = options.text.slice(valueStart);
|
|
6279
|
+
return {
|
|
6280
|
+
kind: "match",
|
|
6281
|
+
start: options.startIndex,
|
|
6282
|
+
end: options.text.length,
|
|
6283
|
+
name: options.paramName,
|
|
6284
|
+
value: rawValue2 ? normalizeXmlTextValue(rawValue2) : ""
|
|
6285
|
+
};
|
|
6101
6286
|
}
|
|
6102
|
-
|
|
6287
|
+
const rawValue = options.text.slice(valueStart, boundaryIndex);
|
|
6288
|
+
return {
|
|
6289
|
+
kind: "match",
|
|
6290
|
+
start: options.startIndex,
|
|
6291
|
+
end: boundaryIndex,
|
|
6292
|
+
name: options.paramName,
|
|
6293
|
+
value: rawValue ? normalizeXmlTextValue(rawValue) : ""
|
|
6294
|
+
};
|
|
6295
|
+
}
|
|
6296
|
+
function parseQwen3CoderToolParserParamTagAt(text, lowerText, startIndex, options) {
|
|
6297
|
+
var _a;
|
|
6298
|
+
const tagNameParse = parseQwen3CoderToolParserParamTagNameLower(
|
|
6299
|
+
lowerText,
|
|
6300
|
+
startIndex
|
|
6301
|
+
);
|
|
6302
|
+
if (!tagNameParse) {
|
|
6303
|
+
return null;
|
|
6304
|
+
}
|
|
6305
|
+
if (tagNameParse.kind === "partial") {
|
|
6306
|
+
return { kind: "partial", start: startIndex, openEnd: null };
|
|
6307
|
+
}
|
|
6308
|
+
const tagNameLower = tagNameParse.tagNameLower;
|
|
6309
|
+
const openEnd = findTagEndIndex(text, startIndex);
|
|
6310
|
+
if (openEnd == null) {
|
|
6311
|
+
return { kind: "partial", start: startIndex, openEnd: null };
|
|
6312
|
+
}
|
|
6313
|
+
const openTag = text.slice(startIndex, openEnd + 1);
|
|
6314
|
+
const paramNameRaw = parseQwen3CoderToolParserParamName(
|
|
6315
|
+
openTag,
|
|
6316
|
+
tagNameLower
|
|
6317
|
+
);
|
|
6318
|
+
const paramName = (_a = paramNameRaw == null ? void 0 : paramNameRaw.trim()) != null ? _a : "";
|
|
6319
|
+
if (paramName.length === 0) {
|
|
6320
|
+
return null;
|
|
6321
|
+
}
|
|
6322
|
+
const selfClosing = openTag.trimEnd().endsWith("/>");
|
|
6323
|
+
if (selfClosing) {
|
|
6324
|
+
return {
|
|
6325
|
+
kind: "match",
|
|
6326
|
+
start: startIndex,
|
|
6327
|
+
end: openEnd + 1,
|
|
6328
|
+
name: paramName,
|
|
6329
|
+
value: ""
|
|
6330
|
+
};
|
|
6331
|
+
}
|
|
6332
|
+
const valueStart = openEnd + 1;
|
|
6333
|
+
const close = findClosingTagEnd(lowerText, valueStart, tagNameLower);
|
|
6334
|
+
if (!close) {
|
|
6335
|
+
return parseQwen3CoderToolParserUnclosedParamValue({
|
|
6336
|
+
text,
|
|
6337
|
+
lowerText,
|
|
6338
|
+
startIndex,
|
|
6339
|
+
openEnd,
|
|
6340
|
+
paramName,
|
|
6341
|
+
allowEndOfString: (options == null ? void 0 : options.allowEndOfString) === true,
|
|
6342
|
+
callEndTagNameLower: options == null ? void 0 : options.callEndTagNameLower
|
|
6343
|
+
});
|
|
6344
|
+
}
|
|
6345
|
+
const rawValue = text.slice(openEnd + 1, close.start);
|
|
6346
|
+
return {
|
|
6347
|
+
kind: "match",
|
|
6348
|
+
start: startIndex,
|
|
6349
|
+
end: close.end,
|
|
6350
|
+
name: paramName,
|
|
6351
|
+
value: rawValue ? normalizeXmlTextValue(rawValue) : ""
|
|
6352
|
+
};
|
|
6353
|
+
}
|
|
6354
|
+
function normalizeXmlTextValue(raw) {
|
|
6355
|
+
let out = raw.trim();
|
|
6356
|
+
if (out.startsWith("<![CDATA[") && out.endsWith("]]>")) {
|
|
6357
|
+
out = out.slice("<![CDATA[".length, -"]]>".length).trim();
|
|
6358
|
+
}
|
|
6359
|
+
return unescapeXml(out);
|
|
6360
|
+
}
|
|
6361
|
+
function getOpeningTag(xml) {
|
|
6362
|
+
const gt = xml.indexOf(">");
|
|
6363
|
+
if (gt === -1) {
|
|
6364
|
+
return null;
|
|
6365
|
+
}
|
|
6366
|
+
return xml.slice(0, gt + 1);
|
|
6367
|
+
}
|
|
6368
|
+
var attrValueRegExpCache = /* @__PURE__ */ new Map();
|
|
6369
|
+
function getAttributeValue(openTag, attrName) {
|
|
6370
|
+
var _a;
|
|
6371
|
+
let re = attrValueRegExpCache.get(attrName);
|
|
6372
|
+
if (!re) {
|
|
6373
|
+
re = new RegExp(
|
|
6374
|
+
`\\b${escapeRegExp(attrName)}\\s*=\\s*(["'])([\\s\\S]*?)\\1`,
|
|
6375
|
+
"i"
|
|
6376
|
+
);
|
|
6377
|
+
attrValueRegExpCache.set(attrName, re);
|
|
6378
|
+
}
|
|
6379
|
+
const match = re.exec(openTag);
|
|
6380
|
+
if (!match) {
|
|
6381
|
+
return null;
|
|
6382
|
+
}
|
|
6383
|
+
return unescapeXml((_a = match[2]) != null ? _a : "");
|
|
6384
|
+
}
|
|
6385
|
+
function getShorthandValue(openTag) {
|
|
6386
|
+
var _a, _b;
|
|
6387
|
+
const match = CALL_SHORTHAND_VALUE_RE.exec(openTag);
|
|
6388
|
+
if (!match) {
|
|
6389
|
+
return null;
|
|
6390
|
+
}
|
|
6391
|
+
const value = (_b = (_a = match[2]) != null ? _a : match[3]) != null ? _b : match[4];
|
|
6392
|
+
if (!value) {
|
|
6393
|
+
return null;
|
|
6394
|
+
}
|
|
6395
|
+
return unescapeXml(value);
|
|
6396
|
+
}
|
|
6397
|
+
function extractFirstTagText(xml, tagName) {
|
|
6398
|
+
var _a;
|
|
6399
|
+
const lower = xml.toLowerCase();
|
|
6400
|
+
const tagLower = tagName.toLowerCase();
|
|
6401
|
+
let index = 0;
|
|
6402
|
+
while (true) {
|
|
6403
|
+
const lt = lower.indexOf("<", index);
|
|
6404
|
+
if (lt === -1) {
|
|
6405
|
+
return null;
|
|
6406
|
+
}
|
|
6407
|
+
const i = skipAsciiWhitespace(lower, lt + 1);
|
|
6408
|
+
if (i >= lower.length || lower[i] === "/") {
|
|
6409
|
+
index = lt + 1;
|
|
6410
|
+
continue;
|
|
6411
|
+
}
|
|
6412
|
+
if (!lower.startsWith(tagLower, i)) {
|
|
6413
|
+
index = lt + 1;
|
|
6414
|
+
continue;
|
|
6415
|
+
}
|
|
6416
|
+
const afterName = i + tagLower.length;
|
|
6417
|
+
const boundary = (_a = lower[afterName]) != null ? _a : "";
|
|
6418
|
+
if (boundary && !isTagBoundaryChar(boundary)) {
|
|
6419
|
+
index = lt + 1;
|
|
6420
|
+
continue;
|
|
6421
|
+
}
|
|
6422
|
+
const openEnd = findTagEndIndex(xml, lt);
|
|
6423
|
+
if (openEnd == null) {
|
|
6424
|
+
return null;
|
|
6425
|
+
}
|
|
6426
|
+
const contentStart = openEnd + 1;
|
|
6427
|
+
const close = findClosingTagEnd(lower, contentStart, tagLower);
|
|
6428
|
+
if (!close) {
|
|
6429
|
+
return null;
|
|
6430
|
+
}
|
|
6431
|
+
return normalizeXmlTextValue(xml.slice(contentStart, close.start));
|
|
6432
|
+
}
|
|
6433
|
+
}
|
|
6434
|
+
function extractToolCallInnerXml(segment) {
|
|
6435
|
+
const openMatch = TOOL_CALL_OPEN_RE.exec(segment);
|
|
6436
|
+
const closeMatch = TOOL_CALL_CLOSE_RE.exec(segment);
|
|
6437
|
+
if (!(openMatch && closeMatch)) {
|
|
6438
|
+
return null;
|
|
6439
|
+
}
|
|
6440
|
+
const openIndex = openMatch.index;
|
|
6441
|
+
const openTag = openMatch[0];
|
|
6442
|
+
const openEnd = openIndex + openTag.length;
|
|
6443
|
+
const closeIndex = segment.toLowerCase().lastIndexOf("</tool_call");
|
|
6444
|
+
if (closeIndex === -1) {
|
|
6445
|
+
return null;
|
|
6446
|
+
}
|
|
6447
|
+
const closeGt = segment.indexOf(">", closeIndex);
|
|
6448
|
+
if (closeGt === -1) {
|
|
6449
|
+
return null;
|
|
6450
|
+
}
|
|
6451
|
+
return {
|
|
6452
|
+
outerOpenTag: openTag,
|
|
6453
|
+
inner: segment.slice(openEnd, closeIndex)
|
|
6454
|
+
};
|
|
6455
|
+
}
|
|
6456
|
+
function mergeParamValue(args, key, value) {
|
|
6457
|
+
const existing = args[key];
|
|
6458
|
+
if (existing === void 0) {
|
|
6459
|
+
args[key] = value;
|
|
6460
|
+
return;
|
|
6461
|
+
}
|
|
6462
|
+
if (Array.isArray(existing)) {
|
|
6463
|
+
existing.push(value);
|
|
6464
|
+
return;
|
|
6465
|
+
}
|
|
6466
|
+
args[key] = [existing, value];
|
|
6467
|
+
}
|
|
6468
|
+
function extractParameters(xml, options) {
|
|
6469
|
+
var _a;
|
|
6470
|
+
const args = {};
|
|
6471
|
+
const lower = xml.toLowerCase();
|
|
6472
|
+
let index = 0;
|
|
6473
|
+
while (true) {
|
|
6474
|
+
const lt = lower.indexOf("<", index);
|
|
6475
|
+
if (lt === -1) {
|
|
6476
|
+
break;
|
|
6477
|
+
}
|
|
6478
|
+
const parsed = parseQwen3CoderToolParserParamTagAt(xml, lower, lt, {
|
|
6479
|
+
allowEndOfString: true,
|
|
6480
|
+
callEndTagNameLower: options == null ? void 0 : options.callEndTagNameLower
|
|
6481
|
+
});
|
|
6482
|
+
if (!parsed) {
|
|
6483
|
+
index = lt + 1;
|
|
6484
|
+
continue;
|
|
6485
|
+
}
|
|
6486
|
+
if (parsed.kind === "match") {
|
|
6487
|
+
mergeParamValue(args, parsed.name, parsed.value);
|
|
6488
|
+
index = parsed.end;
|
|
6489
|
+
continue;
|
|
6490
|
+
}
|
|
6491
|
+
index = ((_a = parsed.openEnd) != null ? _a : lt) + 1;
|
|
6492
|
+
}
|
|
6493
|
+
return args;
|
|
6494
|
+
}
|
|
6495
|
+
function parseSingleFunctionCallXml(xml, fallbackToolName) {
|
|
6496
|
+
var _a, _b, _c;
|
|
6497
|
+
const openingTag = getOpeningTag(xml);
|
|
6498
|
+
const toolNameAttr = openingTag ? getAttributeValue(openingTag, "name") : null;
|
|
6499
|
+
const shorthandName = openingTag ? getShorthandValue(openingTag) : null;
|
|
6500
|
+
const toolName = (_c = (_b = (_a = toolNameAttr != null ? toolNameAttr : shorthandName) != null ? _a : extractFirstTagText(xml, "name")) != null ? _b : extractFirstTagText(xml, "tool_name")) != null ? _c : fallbackToolName;
|
|
6501
|
+
const callEndTagNameLower = toSupportedCallEndTagName(
|
|
6502
|
+
openingTag ? getOpenTagNameLower(openingTag) : null
|
|
6503
|
+
);
|
|
6504
|
+
if (!toolName || toolName.trim().length === 0) {
|
|
6505
|
+
return null;
|
|
6506
|
+
}
|
|
6507
|
+
return {
|
|
6508
|
+
toolName,
|
|
6509
|
+
args: extractParameters(xml, { callEndTagNameLower })
|
|
6510
|
+
};
|
|
6511
|
+
}
|
|
6512
|
+
function findImplicitCallOpenIndices(lowerText) {
|
|
6513
|
+
var _a;
|
|
6514
|
+
const indices = [];
|
|
6515
|
+
let index = 0;
|
|
6516
|
+
while (true) {
|
|
6517
|
+
const lt = lowerText.indexOf("<", index);
|
|
6518
|
+
if (lt === -1) {
|
|
6519
|
+
break;
|
|
6520
|
+
}
|
|
6521
|
+
const i = skipAsciiWhitespace(lowerText, lt + 1);
|
|
6522
|
+
if (i >= lowerText.length) {
|
|
6523
|
+
break;
|
|
6524
|
+
}
|
|
6525
|
+
if (lowerText[i] === "/") {
|
|
6526
|
+
index = lt + 1;
|
|
6527
|
+
continue;
|
|
6528
|
+
}
|
|
6529
|
+
const tagNames = ["call", "function", "tool", "invoke"];
|
|
6530
|
+
for (const tagName of tagNames) {
|
|
6531
|
+
if (!lowerText.startsWith(tagName, i)) {
|
|
6532
|
+
continue;
|
|
6533
|
+
}
|
|
6534
|
+
const after = i + tagName.length;
|
|
6535
|
+
const boundary = (_a = lowerText[after]) != null ? _a : "";
|
|
6536
|
+
if (boundary && !isTagBoundaryChar(boundary) && boundary !== "=") {
|
|
6537
|
+
continue;
|
|
6538
|
+
}
|
|
6539
|
+
indices.push(lt);
|
|
6540
|
+
break;
|
|
6541
|
+
}
|
|
6542
|
+
index = lt + 1;
|
|
6543
|
+
}
|
|
6544
|
+
return indices;
|
|
6545
|
+
}
|
|
6546
|
+
function splitImplicitCallBlocks(xml) {
|
|
6547
|
+
var _a, _b;
|
|
6548
|
+
const lower = xml.toLowerCase();
|
|
6549
|
+
const starts = findImplicitCallOpenIndices(lower);
|
|
6550
|
+
if (starts.length === 0) {
|
|
6551
|
+
return [];
|
|
6552
|
+
}
|
|
6553
|
+
const blocks = [];
|
|
6554
|
+
for (let i = 0; i < starts.length; i += 1) {
|
|
6555
|
+
const start = (_a = starts[i]) != null ? _a : 0;
|
|
6556
|
+
const end = (_b = starts[i + 1]) != null ? _b : xml.length;
|
|
6557
|
+
blocks.push(xml.slice(start, end));
|
|
6558
|
+
}
|
|
6559
|
+
return blocks;
|
|
6560
|
+
}
|
|
6561
|
+
function stripLeadingCallCloseTags(text) {
|
|
6562
|
+
let out = text;
|
|
6563
|
+
while (true) {
|
|
6564
|
+
const match = LEADING_CALL_CLOSE_TAG_RE.exec(out);
|
|
6565
|
+
if (!match) {
|
|
6566
|
+
return out;
|
|
6567
|
+
}
|
|
6568
|
+
out = out.slice(match[0].length);
|
|
6569
|
+
}
|
|
6570
|
+
}
|
|
6571
|
+
function getOpenTagNameLower(openTag) {
|
|
6572
|
+
var _a;
|
|
6573
|
+
const lowerOpenTag = openTag.toLowerCase();
|
|
6574
|
+
const lt = lowerOpenTag.indexOf("<");
|
|
6575
|
+
if (lt === -1) {
|
|
6576
|
+
return null;
|
|
6577
|
+
}
|
|
6578
|
+
let i = skipAsciiWhitespace(lowerOpenTag, lt + 1);
|
|
6579
|
+
if (i >= lowerOpenTag.length || lowerOpenTag[i] === "/") {
|
|
6580
|
+
return null;
|
|
6581
|
+
}
|
|
6582
|
+
const start = i;
|
|
6583
|
+
while (i < lowerOpenTag.length) {
|
|
6584
|
+
const ch = (_a = lowerOpenTag[i]) != null ? _a : "";
|
|
6585
|
+
if (isAsciiWhitespace(ch) || ch === ">" || ch === "/" || ch === "=") {
|
|
6586
|
+
break;
|
|
6587
|
+
}
|
|
6588
|
+
i += 1;
|
|
6589
|
+
}
|
|
6590
|
+
const tagName = lowerOpenTag.slice(start, i);
|
|
6591
|
+
return tagName.length > 0 ? tagName : null;
|
|
6592
|
+
}
|
|
6593
|
+
function splitImplicitCallAndTail(callBlock) {
|
|
6594
|
+
var _a;
|
|
6595
|
+
const openingTag = getOpeningTag(callBlock);
|
|
6596
|
+
const openingTagName = toSupportedCallEndTagName(
|
|
6597
|
+
openingTag ? getOpenTagNameLower(openingTag) : null
|
|
6598
|
+
);
|
|
6599
|
+
const lowerCallBlock = callBlock.toLowerCase();
|
|
6600
|
+
let consumed = 0;
|
|
6601
|
+
if (openingTag) {
|
|
6602
|
+
consumed = openingTag.length;
|
|
6603
|
+
if (openingTagName) {
|
|
6604
|
+
const close = findClosingTagEnd(lowerCallBlock, consumed, openingTagName);
|
|
6605
|
+
if (close) {
|
|
6606
|
+
consumed = Math.max(consumed, close.end);
|
|
6607
|
+
}
|
|
6608
|
+
}
|
|
6609
|
+
}
|
|
6610
|
+
let index = 0;
|
|
6611
|
+
while (true) {
|
|
6612
|
+
const lt = lowerCallBlock.indexOf("<", index);
|
|
6613
|
+
if (lt === -1) {
|
|
6614
|
+
break;
|
|
6615
|
+
}
|
|
6616
|
+
const parsed = parseQwen3CoderToolParserParamTagAt(
|
|
6617
|
+
callBlock,
|
|
6618
|
+
lowerCallBlock,
|
|
6619
|
+
lt,
|
|
6620
|
+
{
|
|
6621
|
+
allowEndOfString: true,
|
|
6622
|
+
callEndTagNameLower: openingTagName
|
|
6623
|
+
}
|
|
6624
|
+
);
|
|
6625
|
+
if (!parsed) {
|
|
6626
|
+
index = lt + 1;
|
|
6627
|
+
continue;
|
|
6628
|
+
}
|
|
6629
|
+
if (parsed.kind === "partial") {
|
|
6630
|
+
index = ((_a = parsed.openEnd) != null ? _a : lt) + 1;
|
|
6631
|
+
continue;
|
|
6632
|
+
}
|
|
6633
|
+
consumed = Math.max(consumed, parsed.end);
|
|
6634
|
+
index = parsed.end;
|
|
6635
|
+
}
|
|
6636
|
+
const clamped = Math.max(0, Math.min(consumed, callBlock.length));
|
|
6637
|
+
return {
|
|
6638
|
+
callContent: callBlock.slice(0, clamped),
|
|
6639
|
+
trailingText: callBlock.slice(clamped)
|
|
6640
|
+
};
|
|
6641
|
+
}
|
|
6642
|
+
function parseQwen3CoderToolParserCallBlocks(blocks, outerNameAttr) {
|
|
6643
|
+
const calls = [];
|
|
6644
|
+
for (const block of blocks) {
|
|
6645
|
+
const parsed = parseSingleFunctionCallXml(block, outerNameAttr);
|
|
6646
|
+
if (!parsed) {
|
|
6647
|
+
return null;
|
|
6648
|
+
}
|
|
6649
|
+
calls.push(parsed);
|
|
6650
|
+
}
|
|
6651
|
+
return calls;
|
|
6652
|
+
}
|
|
6653
|
+
function parseQwen3CoderToolParserClosedMatches(inner, outerNameAttr) {
|
|
6654
|
+
var _a, _b;
|
|
6655
|
+
const callBlockMatches = Array.from(inner.matchAll(CALL_BLOCK_RE));
|
|
6656
|
+
if (callBlockMatches.length === 0) {
|
|
6657
|
+
return void 0;
|
|
6658
|
+
}
|
|
6659
|
+
const closedBlocks = [];
|
|
6660
|
+
let lastClosedEnd = 0;
|
|
6661
|
+
for (const match of callBlockMatches) {
|
|
6662
|
+
const callBlock = (_a = match[0]) != null ? _a : "";
|
|
6663
|
+
const startIndex = (_b = match.index) != null ? _b : -1;
|
|
6664
|
+
if (!callBlock || startIndex < 0) {
|
|
6665
|
+
continue;
|
|
6666
|
+
}
|
|
6667
|
+
closedBlocks.push(callBlock);
|
|
6668
|
+
lastClosedEnd = startIndex + callBlock.length;
|
|
6669
|
+
}
|
|
6670
|
+
const closedCalls = parseQwen3CoderToolParserCallBlocks(
|
|
6671
|
+
closedBlocks,
|
|
6672
|
+
outerNameAttr
|
|
6673
|
+
);
|
|
6674
|
+
if (!closedCalls) {
|
|
6675
|
+
return null;
|
|
6676
|
+
}
|
|
6677
|
+
const trailingInner = inner.slice(lastClosedEnd);
|
|
6678
|
+
if (trailingInner.trim().length === 0) {
|
|
6679
|
+
return closedCalls;
|
|
6680
|
+
}
|
|
6681
|
+
const trailingBlocks = splitImplicitCallBlocks(trailingInner).filter(
|
|
6682
|
+
(b) => b.trim().length > 0
|
|
6683
|
+
);
|
|
6684
|
+
if (trailingBlocks.length === 0) {
|
|
6685
|
+
return closedCalls;
|
|
6686
|
+
}
|
|
6687
|
+
const trailingCalls = parseQwen3CoderToolParserCallBlocks(
|
|
6688
|
+
trailingBlocks,
|
|
6689
|
+
outerNameAttr
|
|
6690
|
+
);
|
|
6691
|
+
if (!trailingCalls) {
|
|
6692
|
+
return closedCalls;
|
|
6693
|
+
}
|
|
6694
|
+
return closedCalls.concat(trailingCalls);
|
|
6695
|
+
}
|
|
6696
|
+
function parseQwen3CoderToolParserToolCallSegment(segment) {
|
|
6697
|
+
var _a;
|
|
6698
|
+
const extracted = extractToolCallInnerXml(segment);
|
|
6699
|
+
if (!extracted) {
|
|
6700
|
+
return null;
|
|
6701
|
+
}
|
|
6702
|
+
const { inner, outerOpenTag } = extracted;
|
|
6703
|
+
const outerNameAttr = getAttributeValue(outerOpenTag, "name");
|
|
6704
|
+
const closedCalls = parseQwen3CoderToolParserClosedMatches(
|
|
6705
|
+
inner,
|
|
6706
|
+
outerNameAttr
|
|
6707
|
+
);
|
|
6708
|
+
if (closedCalls) {
|
|
6709
|
+
return closedCalls;
|
|
6710
|
+
}
|
|
6711
|
+
if (closedCalls === null) {
|
|
6712
|
+
return null;
|
|
6713
|
+
}
|
|
6714
|
+
const implicitBlocks = splitImplicitCallBlocks(inner).filter(
|
|
6715
|
+
(b) => b.trim().length > 0
|
|
6716
|
+
);
|
|
6717
|
+
if (implicitBlocks.length > 0) {
|
|
6718
|
+
return parseQwen3CoderToolParserCallBlocks(implicitBlocks, outerNameAttr);
|
|
6719
|
+
}
|
|
6720
|
+
const single = (_a = parseSingleFunctionCallXml(inner, outerNameAttr)) != null ? _a : parseSingleFunctionCallXml(segment, outerNameAttr);
|
|
6721
|
+
if (!single) {
|
|
6722
|
+
return null;
|
|
6723
|
+
}
|
|
6724
|
+
return [single];
|
|
6725
|
+
}
|
|
6726
|
+
function parseToolCallInput(input) {
|
|
6727
|
+
if (input == null) {
|
|
6728
|
+
return {};
|
|
6729
|
+
}
|
|
6730
|
+
try {
|
|
6731
|
+
return JSON.parse(input);
|
|
6732
|
+
} catch (e) {
|
|
6733
|
+
return input;
|
|
6734
|
+
}
|
|
6735
|
+
}
|
|
6736
|
+
function stringifyToolInputWithSchema(options) {
|
|
6737
|
+
const coerced = coerceToolCallInput(
|
|
6738
|
+
options.toolName,
|
|
6739
|
+
options.args,
|
|
6740
|
+
options.tools
|
|
6741
|
+
);
|
|
6742
|
+
return coerced != null ? coerced : JSON.stringify(options.args);
|
|
6743
|
+
}
|
|
6744
|
+
function toQwen3CoderToolParserParamText(value) {
|
|
6745
|
+
if (typeof value === "string") {
|
|
6746
|
+
return value;
|
|
6747
|
+
}
|
|
6748
|
+
if (value === null) {
|
|
6749
|
+
return "None";
|
|
6750
|
+
}
|
|
6751
|
+
if (typeof value === "boolean") {
|
|
6752
|
+
return value ? "True" : "False";
|
|
6753
|
+
}
|
|
6754
|
+
if (value === void 0) {
|
|
6755
|
+
return "";
|
|
6756
|
+
}
|
|
6757
|
+
if (typeof value === "object") {
|
|
6758
|
+
return JSON.stringify(value);
|
|
6759
|
+
}
|
|
6760
|
+
return String(value);
|
|
6761
|
+
}
|
|
6762
|
+
function appendQwen3CoderToolParserParameter(lines, key, value) {
|
|
6763
|
+
const nameAttr = escapeXmlMinimalAttr(key, '"');
|
|
6764
|
+
const text = escapeXmlMinimalText(toQwen3CoderToolParserParamText(value));
|
|
6765
|
+
lines.push(` <parameter="${nameAttr}">${text}</parameter>`);
|
|
6766
|
+
}
|
|
6767
|
+
function appendQwen3CoderToolParserArgs(lines, args) {
|
|
6768
|
+
if (args && typeof args === "object" && !Array.isArray(args)) {
|
|
6769
|
+
for (const [key, value] of Object.entries(args)) {
|
|
6770
|
+
if (Array.isArray(value)) {
|
|
6771
|
+
for (const item of value) {
|
|
6772
|
+
appendQwen3CoderToolParserParameter(lines, key, item);
|
|
6773
|
+
}
|
|
6774
|
+
} else {
|
|
6775
|
+
appendQwen3CoderToolParserParameter(lines, key, value);
|
|
6776
|
+
}
|
|
6777
|
+
}
|
|
6778
|
+
return;
|
|
6779
|
+
}
|
|
6780
|
+
if (args !== void 0 && args !== null && args !== "") {
|
|
6781
|
+
appendQwen3CoderToolParserParameter(lines, "input", args);
|
|
6782
|
+
}
|
|
6783
|
+
}
|
|
6784
|
+
var qwen3CoderProtocol = () => ({
|
|
6785
|
+
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
6786
|
+
return toolSystemPromptTemplate(tools || []);
|
|
6787
|
+
},
|
|
6788
|
+
formatToolCall(toolCall) {
|
|
6789
|
+
const args = parseToolCallInput(toolCall.input);
|
|
6790
|
+
const lines = ["<tool_call>"];
|
|
6791
|
+
lines.push(
|
|
6792
|
+
` <function="${escapeXmlMinimalAttr(toolCall.toolName, '"')}">`
|
|
6793
|
+
);
|
|
6794
|
+
appendQwen3CoderToolParserArgs(lines, args);
|
|
6795
|
+
lines.push(" </function>");
|
|
6796
|
+
lines.push("</tool_call>");
|
|
6797
|
+
return lines.join("\n");
|
|
6798
|
+
},
|
|
6799
|
+
parseGeneratedText({ text, tools, options }) {
|
|
6800
|
+
const processedElements = [];
|
|
6801
|
+
const emitToolCalls = (calls) => {
|
|
6802
|
+
for (const call of calls) {
|
|
6803
|
+
processedElements.push({
|
|
6804
|
+
type: "tool-call",
|
|
6805
|
+
toolCallId: generateToolCallId(),
|
|
6806
|
+
toolName: call.toolName,
|
|
6807
|
+
input: stringifyToolInputWithSchema({
|
|
6808
|
+
tools,
|
|
6809
|
+
toolName: call.toolName,
|
|
6810
|
+
args: call.args
|
|
6811
|
+
})
|
|
6812
|
+
});
|
|
6813
|
+
}
|
|
6814
|
+
};
|
|
6815
|
+
const pushText = (value) => {
|
|
6816
|
+
if (value.length === 0) {
|
|
6817
|
+
return;
|
|
6818
|
+
}
|
|
6819
|
+
processedElements.push({ type: "text", text: value });
|
|
6820
|
+
};
|
|
6821
|
+
const tryEmitToolCallSegment = (segment, fallbackText = segment) => {
|
|
6822
|
+
var _a;
|
|
6823
|
+
const parsedCalls = parseQwen3CoderToolParserToolCallSegment(segment);
|
|
6824
|
+
if (!parsedCalls) {
|
|
6825
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
6826
|
+
options,
|
|
6827
|
+
"Could not process Qwen3CoderToolParser XML tool call; keeping original text.",
|
|
6828
|
+
{ toolCall: fallbackText }
|
|
6829
|
+
);
|
|
6830
|
+
processedElements.push({ type: "text", text: fallbackText });
|
|
6831
|
+
return false;
|
|
6832
|
+
}
|
|
6833
|
+
emitToolCalls(parsedCalls);
|
|
6834
|
+
return true;
|
|
6835
|
+
};
|
|
6836
|
+
const emitWrapperlessCallParseFailureAsText = (raw) => {
|
|
6837
|
+
var _a;
|
|
6838
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
6839
|
+
options,
|
|
6840
|
+
"Could not process Qwen3CoderToolParser <function> call; keeping original text.",
|
|
6841
|
+
{ toolCall: raw }
|
|
6842
|
+
);
|
|
6843
|
+
processedElements.push({ type: "text", text: raw });
|
|
6844
|
+
};
|
|
6845
|
+
const tryParseCallBlocksWithoutWrapperByImplicitStarts = (sourceText, starts) => {
|
|
6846
|
+
var _a, _b;
|
|
6847
|
+
let index = 0;
|
|
6848
|
+
for (let i = 0; i < starts.length; i += 1) {
|
|
6849
|
+
const startIndex = (_a = starts[i]) != null ? _a : -1;
|
|
6850
|
+
if (startIndex < 0) {
|
|
6851
|
+
continue;
|
|
6852
|
+
}
|
|
6853
|
+
const endIndex = (_b = starts[i + 1]) != null ? _b : sourceText.length;
|
|
6854
|
+
pushText(
|
|
6855
|
+
stripTrailingToolCallCloseTags(
|
|
6856
|
+
stripLeadingToolCallCloseTags(sourceText.slice(index, startIndex))
|
|
6857
|
+
)
|
|
6858
|
+
);
|
|
6859
|
+
const full = sourceText.slice(startIndex, endIndex);
|
|
6860
|
+
const { callContent, trailingText } = splitImplicitCallAndTail(full);
|
|
6861
|
+
const parsed = parseSingleFunctionCallXml(callContent, null);
|
|
6862
|
+
if (parsed) {
|
|
6863
|
+
emitToolCalls([parsed]);
|
|
6864
|
+
pushText(
|
|
6865
|
+
stripTrailingToolCallCloseTags(
|
|
6866
|
+
stripLeadingToolCallCloseTags(trailingText)
|
|
6867
|
+
)
|
|
6868
|
+
);
|
|
6869
|
+
} else {
|
|
6870
|
+
emitWrapperlessCallParseFailureAsText(full);
|
|
6871
|
+
}
|
|
6872
|
+
index = endIndex;
|
|
6873
|
+
}
|
|
6874
|
+
pushText(
|
|
6875
|
+
stripTrailingToolCallCloseTags(
|
|
6876
|
+
stripLeadingToolCallCloseTags(sourceText.slice(index))
|
|
6877
|
+
)
|
|
6878
|
+
);
|
|
6879
|
+
return true;
|
|
6880
|
+
};
|
|
6881
|
+
const tryParseCallBlocksWithoutWrapperByMatches = (sourceText, matches) => {
|
|
6882
|
+
var _a;
|
|
6883
|
+
let index = 0;
|
|
6884
|
+
for (const match of matches) {
|
|
6885
|
+
const full = match[0];
|
|
6886
|
+
const startIndex = (_a = match.index) != null ? _a : -1;
|
|
6887
|
+
if (!full || startIndex < 0) {
|
|
6888
|
+
continue;
|
|
6889
|
+
}
|
|
6890
|
+
pushText(
|
|
6891
|
+
stripTrailingToolCallCloseTags(
|
|
6892
|
+
stripLeadingToolCallCloseTags(sourceText.slice(index, startIndex))
|
|
6893
|
+
)
|
|
6894
|
+
);
|
|
6895
|
+
const parsed = parseSingleFunctionCallXml(full, null);
|
|
6896
|
+
if (parsed) {
|
|
6897
|
+
emitToolCalls([parsed]);
|
|
6898
|
+
} else {
|
|
6899
|
+
emitWrapperlessCallParseFailureAsText(full);
|
|
6900
|
+
}
|
|
6901
|
+
index = startIndex + full.length;
|
|
6902
|
+
}
|
|
6903
|
+
const trailing = sourceText.slice(index);
|
|
6904
|
+
const trailingStarts = findImplicitCallOpenIndices(
|
|
6905
|
+
trailing.toLowerCase()
|
|
6906
|
+
);
|
|
6907
|
+
if (trailingStarts.length > 0) {
|
|
6908
|
+
return tryParseCallBlocksWithoutWrapperByImplicitStarts(
|
|
6909
|
+
trailing,
|
|
6910
|
+
trailingStarts
|
|
6911
|
+
);
|
|
6912
|
+
}
|
|
6913
|
+
pushText(
|
|
6914
|
+
stripTrailingToolCallCloseTags(stripLeadingToolCallCloseTags(trailing))
|
|
6915
|
+
);
|
|
6916
|
+
return true;
|
|
6917
|
+
};
|
|
6918
|
+
const tryParseCallBlocksWithoutWrapperText = (sourceText) => {
|
|
6919
|
+
const matches = Array.from(sourceText.matchAll(CALL_BLOCK_RE));
|
|
6920
|
+
if (matches.length > 0) {
|
|
6921
|
+
return tryParseCallBlocksWithoutWrapperByMatches(sourceText, matches);
|
|
6922
|
+
}
|
|
6923
|
+
const starts = findImplicitCallOpenIndices(sourceText.toLowerCase());
|
|
6924
|
+
if (starts.length === 0) {
|
|
6925
|
+
return false;
|
|
6926
|
+
}
|
|
6927
|
+
return tryParseCallBlocksWithoutWrapperByImplicitStarts(
|
|
6928
|
+
sourceText,
|
|
6929
|
+
starts
|
|
6930
|
+
);
|
|
6931
|
+
};
|
|
6932
|
+
const pushTextOrParseWrapperlessCalls = (segment) => {
|
|
6933
|
+
if (segment.length === 0) {
|
|
6934
|
+
return;
|
|
6935
|
+
}
|
|
6936
|
+
if (!tryParseCallBlocksWithoutWrapperText(segment)) {
|
|
6937
|
+
pushText(segment);
|
|
6938
|
+
}
|
|
6939
|
+
};
|
|
6940
|
+
const handleCompleteToolCallRemainder = (remainder) => {
|
|
6941
|
+
if (!remainder) {
|
|
6942
|
+
return;
|
|
6943
|
+
}
|
|
6944
|
+
const lowerRemainder = remainder.toLowerCase();
|
|
6945
|
+
const trailingIndex = lowerRemainder.indexOf("<tool_call");
|
|
6946
|
+
if (trailingIndex === -1) {
|
|
6947
|
+
pushTextOrParseWrapperlessCalls(remainder);
|
|
6948
|
+
return;
|
|
6949
|
+
}
|
|
6950
|
+
pushTextOrParseWrapperlessCalls(remainder.slice(0, trailingIndex));
|
|
6951
|
+
const trailing = remainder.slice(trailingIndex);
|
|
6952
|
+
const synthetic = TOOL_CALL_CLOSE_RE.test(trailing) ? trailing : `${trailing}</tool_call>`;
|
|
6953
|
+
tryEmitToolCallSegment(synthetic, trailing);
|
|
6954
|
+
};
|
|
6955
|
+
const tryParseCompleteToolCallBlocks = () => {
|
|
6956
|
+
var _a;
|
|
6957
|
+
const matches = Array.from(text.matchAll(TOOL_CALL_BLOCK_RE));
|
|
6958
|
+
if (matches.length === 0) {
|
|
6959
|
+
return false;
|
|
6960
|
+
}
|
|
6961
|
+
let index = 0;
|
|
6962
|
+
for (const match of matches) {
|
|
6963
|
+
const full = match[0];
|
|
6964
|
+
const startIndex = (_a = match.index) != null ? _a : -1;
|
|
6965
|
+
if (!full || startIndex < 0) {
|
|
6966
|
+
continue;
|
|
6967
|
+
}
|
|
6968
|
+
pushTextOrParseWrapperlessCalls(text.slice(index, startIndex));
|
|
6969
|
+
tryEmitToolCallSegment(full);
|
|
6970
|
+
index = startIndex + full.length;
|
|
6971
|
+
}
|
|
6972
|
+
handleCompleteToolCallRemainder(text.slice(index));
|
|
6973
|
+
return true;
|
|
6974
|
+
};
|
|
6975
|
+
const tryParseIncompleteToolCall = () => {
|
|
6976
|
+
const lowerText = text.toLowerCase();
|
|
6977
|
+
const startIndex = lowerText.indexOf("<tool_call");
|
|
6978
|
+
if (startIndex === -1) {
|
|
6979
|
+
return false;
|
|
6980
|
+
}
|
|
6981
|
+
pushTextOrParseWrapperlessCalls(text.slice(0, startIndex));
|
|
6982
|
+
const trailing = text.slice(startIndex);
|
|
6983
|
+
const synthetic = TOOL_CALL_CLOSE_RE.test(trailing) ? trailing : `${trailing}</tool_call>`;
|
|
6984
|
+
tryEmitToolCallSegment(synthetic, trailing);
|
|
6985
|
+
return true;
|
|
6986
|
+
};
|
|
6987
|
+
const tryParseCallBlocksWithoutWrapper = () => {
|
|
6988
|
+
return tryParseCallBlocksWithoutWrapperText(text);
|
|
6989
|
+
};
|
|
6990
|
+
const tryParseSingleFunctionCall = () => {
|
|
6991
|
+
const lowerText = text.toLowerCase();
|
|
6992
|
+
const startIndex = lowerText.indexOf("<function");
|
|
6993
|
+
if (startIndex === -1) {
|
|
6994
|
+
return false;
|
|
6995
|
+
}
|
|
6996
|
+
pushText(stripTrailingToolCallCloseTags(text.slice(0, startIndex)));
|
|
6997
|
+
const trailing = stripLeadingToolCallCloseTags(text.slice(startIndex));
|
|
6998
|
+
const parsed = parseSingleFunctionCallXml(trailing, null);
|
|
6999
|
+
if (!parsed) {
|
|
7000
|
+
processedElements.push({ type: "text", text: trailing });
|
|
7001
|
+
return true;
|
|
7002
|
+
}
|
|
7003
|
+
emitToolCalls([parsed]);
|
|
7004
|
+
return true;
|
|
7005
|
+
};
|
|
7006
|
+
if (tryParseCompleteToolCallBlocks()) {
|
|
7007
|
+
return processedElements;
|
|
7008
|
+
}
|
|
7009
|
+
if (tryParseIncompleteToolCall()) {
|
|
7010
|
+
return processedElements;
|
|
7011
|
+
}
|
|
7012
|
+
if (tryParseCallBlocksWithoutWrapper()) {
|
|
7013
|
+
return processedElements;
|
|
7014
|
+
}
|
|
7015
|
+
if (tryParseSingleFunctionCall()) {
|
|
7016
|
+
return processedElements;
|
|
7017
|
+
}
|
|
7018
|
+
return [{ type: "text", text }];
|
|
7019
|
+
},
|
|
7020
|
+
extractToolCallSegments({ text }) {
|
|
7021
|
+
return Array.from(text.matchAll(TOOL_CALL_BLOCK_RE)).map((m) => m[0]).filter((s) => Boolean(s));
|
|
7022
|
+
},
|
|
7023
|
+
createStreamParser({ tools, options }) {
|
|
7024
|
+
const toolCallStartPrefixLower = "<tool_call";
|
|
7025
|
+
const implicitCallPrefixesLower = [
|
|
7026
|
+
"<function",
|
|
7027
|
+
"<call",
|
|
7028
|
+
"<tool",
|
|
7029
|
+
"<invoke"
|
|
7030
|
+
];
|
|
7031
|
+
let buffer = "";
|
|
7032
|
+
let toolCall = null;
|
|
7033
|
+
let implicitCall = null;
|
|
7034
|
+
let implicitCallOpenTag = null;
|
|
7035
|
+
let currentTextId = null;
|
|
7036
|
+
let hasEmittedTextStart = false;
|
|
7037
|
+
const flushText = createFlushTextHandler(
|
|
7038
|
+
() => currentTextId,
|
|
7039
|
+
(id) => {
|
|
7040
|
+
currentTextId = id;
|
|
7041
|
+
},
|
|
7042
|
+
() => hasEmittedTextStart,
|
|
7043
|
+
(value) => {
|
|
7044
|
+
hasEmittedTextStart = value;
|
|
7045
|
+
}
|
|
7046
|
+
);
|
|
7047
|
+
const removeSlice = (text, start, end) => text.slice(0, start) + text.slice(end);
|
|
7048
|
+
const maybeEmitToolInputStart = (controller, callState) => {
|
|
7049
|
+
if (callState.hasEmittedStart) {
|
|
7050
|
+
return;
|
|
7051
|
+
}
|
|
7052
|
+
const toolName = callState.toolName;
|
|
7053
|
+
if (!toolName || toolName.trim().length === 0) {
|
|
7054
|
+
return;
|
|
7055
|
+
}
|
|
7056
|
+
flushText(controller);
|
|
7057
|
+
controller.enqueue({
|
|
7058
|
+
type: "tool-input-start",
|
|
7059
|
+
id: callState.toolCallId,
|
|
7060
|
+
toolName
|
|
7061
|
+
});
|
|
7062
|
+
callState.hasEmittedStart = true;
|
|
7063
|
+
};
|
|
7064
|
+
const maybeEmitToolInputProgress = (controller, callState) => {
|
|
7065
|
+
if (!callState.hasEmittedStart) {
|
|
7066
|
+
return;
|
|
7067
|
+
}
|
|
7068
|
+
const toolName = callState.toolName;
|
|
7069
|
+
if (!toolName) {
|
|
7070
|
+
return;
|
|
7071
|
+
}
|
|
7072
|
+
const fullInput = stringifyToolInputWithSchema({
|
|
7073
|
+
tools,
|
|
7074
|
+
toolName,
|
|
7075
|
+
args: callState.args
|
|
7076
|
+
});
|
|
7077
|
+
if (fullInput === "{}") {
|
|
7078
|
+
return;
|
|
7079
|
+
}
|
|
7080
|
+
const prefixCandidate = toIncompleteJsonPrefix(fullInput);
|
|
7081
|
+
emitPrefixDelta({
|
|
7082
|
+
controller,
|
|
7083
|
+
id: callState.toolCallId,
|
|
7084
|
+
state: callState,
|
|
7085
|
+
candidate: prefixCandidate
|
|
7086
|
+
});
|
|
7087
|
+
};
|
|
7088
|
+
const finalizeCall = (controller, callState, fallbackToolName, rawToolCallText = null) => {
|
|
7089
|
+
var _a, _b;
|
|
7090
|
+
const resolvedToolName = (_a = callState.toolName) != null ? _a : fallbackToolName;
|
|
7091
|
+
if (!resolvedToolName || resolvedToolName.trim().length === 0) {
|
|
7092
|
+
const shouldEmitRaw = shouldEmitRawToolCallTextOnError3(options);
|
|
7093
|
+
(_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
|
|
7094
|
+
options,
|
|
7095
|
+
shouldEmitRaw && rawToolCallText ? "Could not resolve Qwen3CoderToolParser tool name for tool call; emitting original text." : "Could not resolve Qwen3CoderToolParser tool name for tool call",
|
|
7096
|
+
{
|
|
7097
|
+
toolCallId: callState.toolCallId,
|
|
7098
|
+
toolCall: rawToolCallText
|
|
7099
|
+
}
|
|
7100
|
+
);
|
|
7101
|
+
if (callState.hasEmittedStart) {
|
|
7102
|
+
controller.enqueue({
|
|
7103
|
+
type: "tool-input-end",
|
|
7104
|
+
id: callState.toolCallId
|
|
7105
|
+
});
|
|
7106
|
+
}
|
|
7107
|
+
if (shouldEmitRaw && rawToolCallText) {
|
|
7108
|
+
flushText(controller, rawToolCallText);
|
|
7109
|
+
}
|
|
7110
|
+
return false;
|
|
7111
|
+
}
|
|
7112
|
+
callState.toolName = resolvedToolName;
|
|
7113
|
+
maybeEmitToolInputStart(controller, callState);
|
|
7114
|
+
maybeEmitToolInputProgress(controller, callState);
|
|
7115
|
+
const finalInput = stringifyToolInputWithSchema({
|
|
7116
|
+
tools,
|
|
7117
|
+
toolName: resolvedToolName,
|
|
7118
|
+
args: callState.args
|
|
7119
|
+
});
|
|
7120
|
+
emitFinalRemainder({
|
|
7121
|
+
controller,
|
|
7122
|
+
id: callState.toolCallId,
|
|
7123
|
+
state: callState,
|
|
7124
|
+
finalFullJson: finalInput,
|
|
7125
|
+
onMismatch: options == null ? void 0 : options.onError
|
|
7126
|
+
});
|
|
7127
|
+
controller.enqueue({
|
|
7128
|
+
type: "tool-input-end",
|
|
7129
|
+
id: callState.toolCallId
|
|
7130
|
+
});
|
|
7131
|
+
controller.enqueue({
|
|
7132
|
+
type: "tool-call",
|
|
7133
|
+
toolCallId: callState.toolCallId,
|
|
7134
|
+
toolName: resolvedToolName,
|
|
7135
|
+
input: finalInput
|
|
7136
|
+
});
|
|
7137
|
+
return true;
|
|
7138
|
+
};
|
|
7139
|
+
const consumeToolNameTag = (controller, callState, work) => {
|
|
7140
|
+
var _a, _b, _c, _d;
|
|
7141
|
+
if (callState.toolName) {
|
|
7142
|
+
return work;
|
|
7143
|
+
}
|
|
7144
|
+
const match = QWEN3CODER_TOOL_PARSER_STREAM_NAME_TAG_RE.exec(work);
|
|
7145
|
+
if (!match) {
|
|
7146
|
+
return work;
|
|
7147
|
+
}
|
|
7148
|
+
const value = normalizeXmlTextValue((_a = match[2]) != null ? _a : "");
|
|
7149
|
+
if (value.trim().length > 0) {
|
|
7150
|
+
callState.toolName = value;
|
|
7151
|
+
}
|
|
7152
|
+
const start = (_b = match.index) != null ? _b : 0;
|
|
7153
|
+
const nextWork = removeSlice(
|
|
7154
|
+
work,
|
|
7155
|
+
start,
|
|
7156
|
+
start + ((_d = (_c = match[0]) == null ? void 0 : _c.length) != null ? _d : 0)
|
|
7157
|
+
);
|
|
7158
|
+
maybeEmitToolInputStart(controller, callState);
|
|
7159
|
+
return nextWork;
|
|
7160
|
+
};
|
|
7161
|
+
const consumeParamTags = (controller, callState, work, allowEndOfString) => {
|
|
7162
|
+
const lower = work.toLowerCase();
|
|
7163
|
+
let index = 0;
|
|
7164
|
+
let lastKept = 0;
|
|
7165
|
+
let pieces = null;
|
|
7166
|
+
while (true) {
|
|
7167
|
+
const lt = lower.indexOf("<", index);
|
|
7168
|
+
if (lt === -1) {
|
|
7169
|
+
break;
|
|
7170
|
+
}
|
|
7171
|
+
const parsed = parseQwen3CoderToolParserParamTagAt(work, lower, lt, {
|
|
7172
|
+
allowEndOfString,
|
|
7173
|
+
callEndTagNameLower: callState.endTagName
|
|
7174
|
+
});
|
|
7175
|
+
if (!parsed) {
|
|
7176
|
+
index = lt + 1;
|
|
7177
|
+
continue;
|
|
7178
|
+
}
|
|
7179
|
+
if (parsed.kind === "partial") {
|
|
7180
|
+
break;
|
|
7181
|
+
}
|
|
7182
|
+
mergeParamValue(callState.args, parsed.name, parsed.value);
|
|
7183
|
+
pieces != null ? pieces : pieces = [];
|
|
7184
|
+
pieces.push(work.slice(lastKept, parsed.start));
|
|
7185
|
+
lastKept = parsed.end;
|
|
7186
|
+
index = parsed.end;
|
|
7187
|
+
}
|
|
7188
|
+
maybeEmitToolInputStart(controller, callState);
|
|
7189
|
+
if (!pieces) {
|
|
7190
|
+
return work;
|
|
7191
|
+
}
|
|
7192
|
+
pieces.push(work.slice(lastKept));
|
|
7193
|
+
return pieces.join("");
|
|
7194
|
+
};
|
|
7195
|
+
const parseCallContent = (controller, callState, content, allowEndOfString) => {
|
|
7196
|
+
let work = content;
|
|
7197
|
+
work = consumeToolNameTag(controller, callState, work);
|
|
7198
|
+
work = consumeParamTags(controller, callState, work, allowEndOfString);
|
|
7199
|
+
maybeEmitToolInputStart(controller, callState);
|
|
7200
|
+
maybeEmitToolInputProgress(controller, callState);
|
|
7201
|
+
return work;
|
|
7202
|
+
};
|
|
7203
|
+
const closeTagCache = /* @__PURE__ */ new Map();
|
|
7204
|
+
const getCloseTagPattern = (endTagName) => {
|
|
7205
|
+
const cached = closeTagCache.get(endTagName);
|
|
7206
|
+
if (cached) {
|
|
7207
|
+
return cached;
|
|
7208
|
+
}
|
|
7209
|
+
const created = new RegExp(
|
|
7210
|
+
`<\\s*\\/\\s*${escapeRegExp(endTagName)}\\s*>`,
|
|
7211
|
+
"i"
|
|
7212
|
+
);
|
|
7213
|
+
closeTagCache.set(endTagName, created);
|
|
7214
|
+
return created;
|
|
7215
|
+
};
|
|
7216
|
+
const getNextCallStartInBuffer = (callState) => {
|
|
7217
|
+
var _a;
|
|
7218
|
+
if (callState.endTagName === "tool_call") {
|
|
7219
|
+
return -1;
|
|
7220
|
+
}
|
|
7221
|
+
const match = QWEN3CODER_TOOL_PARSER_STREAM_CALL_OPEN_TAG_RE.exec(
|
|
7222
|
+
callState.buffer
|
|
7223
|
+
);
|
|
7224
|
+
return (_a = match == null ? void 0 : match.index) != null ? _a : -1;
|
|
7225
|
+
};
|
|
7226
|
+
const finalizeStreamingCall = (controller, callState, fallbackToolName, remainder) => {
|
|
7227
|
+
const rawToolCallText = remainder.length > 0 && callState.raw.endsWith(remainder) ? callState.raw.slice(0, -remainder.length) : callState.raw;
|
|
7228
|
+
const ok = finalizeCall(
|
|
7229
|
+
controller,
|
|
7230
|
+
callState,
|
|
7231
|
+
fallbackToolName,
|
|
7232
|
+
rawToolCallText
|
|
7233
|
+
);
|
|
7234
|
+
if (ok && toolCall) {
|
|
7235
|
+
toolCall.emittedToolCallCount += 1;
|
|
7236
|
+
}
|
|
7237
|
+
};
|
|
7238
|
+
const consumeCallAtNextBoundary = (controller, callState, fallbackToolName, nextCallStart) => {
|
|
7239
|
+
const beforeNextCall = callState.buffer.slice(0, nextCallStart);
|
|
7240
|
+
const afterNextCall = callState.buffer.slice(nextCallStart);
|
|
7241
|
+
callState.buffer = parseCallContent(
|
|
7242
|
+
controller,
|
|
7243
|
+
callState,
|
|
7244
|
+
beforeNextCall,
|
|
7245
|
+
true
|
|
7246
|
+
);
|
|
7247
|
+
finalizeStreamingCall(
|
|
7248
|
+
controller,
|
|
7249
|
+
callState,
|
|
7250
|
+
fallbackToolName,
|
|
7251
|
+
afterNextCall
|
|
7252
|
+
);
|
|
7253
|
+
return { done: true, remainder: afterNextCall };
|
|
7254
|
+
};
|
|
7255
|
+
const consumeCall = (controller, callState, incoming, fallbackToolName) => {
|
|
7256
|
+
var _a, _b, _c;
|
|
7257
|
+
callState.buffer += incoming;
|
|
7258
|
+
callState.raw += incoming;
|
|
7259
|
+
const closeMatch = getCloseTagPattern(callState.endTagName).exec(
|
|
7260
|
+
callState.buffer
|
|
7261
|
+
);
|
|
7262
|
+
const closeStart = (_a = closeMatch == null ? void 0 : closeMatch.index) != null ? _a : -1;
|
|
7263
|
+
const nextCallStart = getNextCallStartInBuffer(callState);
|
|
7264
|
+
const shouldCloseAtNextBoundary = nextCallStart !== -1 && (closeStart === -1 || nextCallStart < closeStart);
|
|
7265
|
+
if (shouldCloseAtNextBoundary) {
|
|
7266
|
+
return consumeCallAtNextBoundary(
|
|
7267
|
+
controller,
|
|
7268
|
+
callState,
|
|
7269
|
+
fallbackToolName,
|
|
7270
|
+
nextCallStart
|
|
7271
|
+
);
|
|
7272
|
+
}
|
|
7273
|
+
if (!closeMatch) {
|
|
7274
|
+
callState.buffer = parseCallContent(
|
|
7275
|
+
controller,
|
|
7276
|
+
callState,
|
|
7277
|
+
callState.buffer,
|
|
7278
|
+
false
|
|
7279
|
+
);
|
|
7280
|
+
return { done: false, remainder: "" };
|
|
7281
|
+
}
|
|
7282
|
+
const closeEnd = closeStart + ((_c = (_b = closeMatch[0]) == null ? void 0 : _b.length) != null ? _c : 0);
|
|
7283
|
+
const beforeClose = callState.buffer.slice(0, closeStart);
|
|
7284
|
+
const afterClose = callState.buffer.slice(closeEnd);
|
|
7285
|
+
parseCallContent(controller, callState, beforeClose, true);
|
|
7286
|
+
callState.buffer = "";
|
|
7287
|
+
finalizeStreamingCall(
|
|
7288
|
+
controller,
|
|
7289
|
+
callState,
|
|
7290
|
+
fallbackToolName,
|
|
7291
|
+
afterClose
|
|
7292
|
+
);
|
|
7293
|
+
return { done: true, remainder: afterClose };
|
|
7294
|
+
};
|
|
7295
|
+
const finalizeCallAtFinish = (controller, callState, fallbackToolName) => {
|
|
7296
|
+
callState.buffer = parseCallContent(
|
|
7297
|
+
controller,
|
|
7298
|
+
callState,
|
|
7299
|
+
callState.buffer,
|
|
7300
|
+
true
|
|
7301
|
+
);
|
|
7302
|
+
const trailingText = stripLeadingCallCloseTags(callState.buffer);
|
|
7303
|
+
callState.buffer = "";
|
|
7304
|
+
const ok = finalizeCall(controller, callState, fallbackToolName, null);
|
|
7305
|
+
return {
|
|
7306
|
+
ok,
|
|
7307
|
+
trailingText
|
|
7308
|
+
};
|
|
7309
|
+
};
|
|
7310
|
+
const flushSafeTextPrefix = (controller) => {
|
|
7311
|
+
const lower = buffer.toLowerCase();
|
|
7312
|
+
const potentialIndices = [
|
|
7313
|
+
getPotentialStartIndex(lower, toolCallStartPrefixLower),
|
|
7314
|
+
...implicitCallPrefixesLower.map(
|
|
7315
|
+
(prefix) => getPotentialStartIndex(lower, prefix)
|
|
7316
|
+
)
|
|
7317
|
+
].filter((value) => value != null);
|
|
7318
|
+
const potentialIndex = potentialIndices.length > 0 ? Math.min(...potentialIndices) : null;
|
|
7319
|
+
if (potentialIndex == null) {
|
|
7320
|
+
if (buffer.length > 0) {
|
|
7321
|
+
flushText(controller, buffer);
|
|
7322
|
+
buffer = "";
|
|
7323
|
+
}
|
|
7324
|
+
return;
|
|
7325
|
+
}
|
|
7326
|
+
if (potentialIndex > 0) {
|
|
7327
|
+
flushText(controller, buffer.slice(0, potentialIndex));
|
|
7328
|
+
buffer = buffer.slice(potentialIndex);
|
|
7329
|
+
}
|
|
7330
|
+
};
|
|
7331
|
+
const stripLeadingToolCallCloseTagsFromBuffer = () => {
|
|
7332
|
+
if (!buffer) {
|
|
7333
|
+
return;
|
|
7334
|
+
}
|
|
7335
|
+
const stripped = stripLeadingToolCallCloseTags(buffer);
|
|
7336
|
+
if (stripped !== buffer) {
|
|
7337
|
+
buffer = stripped;
|
|
7338
|
+
}
|
|
7339
|
+
};
|
|
7340
|
+
const startToolCallIfPresent = (_controller) => {
|
|
7341
|
+
if (toolCall) {
|
|
7342
|
+
return;
|
|
7343
|
+
}
|
|
7344
|
+
if (implicitCall) {
|
|
7345
|
+
return;
|
|
7346
|
+
}
|
|
7347
|
+
const lower = buffer.toLowerCase();
|
|
7348
|
+
const startIndex = getPotentialStartIndex(
|
|
7349
|
+
lower,
|
|
7350
|
+
toolCallStartPrefixLower
|
|
7351
|
+
);
|
|
7352
|
+
if (startIndex == null || startIndex !== 0) {
|
|
7353
|
+
return;
|
|
7354
|
+
}
|
|
7355
|
+
const gtIndex = buffer.indexOf(">");
|
|
7356
|
+
if (gtIndex === -1) {
|
|
7357
|
+
return;
|
|
7358
|
+
}
|
|
7359
|
+
const openTag = buffer.slice(0, gtIndex + 1);
|
|
7360
|
+
if (!TOOL_CALL_OPEN_RE.test(openTag)) {
|
|
7361
|
+
return;
|
|
7362
|
+
}
|
|
7363
|
+
toolCall = {
|
|
7364
|
+
outerOpenTag: openTag,
|
|
7365
|
+
outerNameAttr: getAttributeValue(openTag, "name"),
|
|
7366
|
+
raw: openTag,
|
|
7367
|
+
mode: "unknown",
|
|
7368
|
+
innerBuffer: "",
|
|
7369
|
+
activeCall: null,
|
|
7370
|
+
emittedToolCallCount: 0
|
|
7371
|
+
};
|
|
7372
|
+
const remainder = buffer.slice(gtIndex + 1);
|
|
7373
|
+
buffer = "";
|
|
7374
|
+
if (remainder.length > 0) {
|
|
7375
|
+
toolCall.raw += remainder;
|
|
7376
|
+
toolCall.innerBuffer += remainder;
|
|
7377
|
+
}
|
|
7378
|
+
};
|
|
7379
|
+
const startImplicitCallIfPresent = (controller) => {
|
|
7380
|
+
var _a, _b, _c, _d;
|
|
7381
|
+
if (toolCall || implicitCall) {
|
|
7382
|
+
return;
|
|
7383
|
+
}
|
|
7384
|
+
const match = QWEN3CODER_TOOL_PARSER_STREAM_CALL_OPEN_TAG_RE.exec(buffer);
|
|
7385
|
+
const startIndex = (_a = match == null ? void 0 : match.index) != null ? _a : -1;
|
|
7386
|
+
const openTag = (_b = match == null ? void 0 : match[0]) != null ? _b : "";
|
|
7387
|
+
const callTagName = ((_c = match == null ? void 0 : match[1]) != null ? _c : "").toLowerCase();
|
|
7388
|
+
if (!match || startIndex !== 0 || !openTag || !callTagName) {
|
|
7389
|
+
return;
|
|
7390
|
+
}
|
|
7391
|
+
const inlineToolName = (_d = getAttributeValue(openTag, "name")) != null ? _d : getShorthandValue(openTag);
|
|
7392
|
+
if (!inlineToolName || inlineToolName.trim().length === 0) {
|
|
7393
|
+
return;
|
|
7394
|
+
}
|
|
7395
|
+
const selfClosing = QWEN3CODER_TOOL_PARSER_STREAM_SELF_CLOSING_TAG_RE.test(openTag);
|
|
7396
|
+
buffer = buffer.slice(openTag.length);
|
|
7397
|
+
const newCall = {
|
|
7398
|
+
endTagName: callTagName,
|
|
7399
|
+
toolCallId: generateToolCallId(),
|
|
7400
|
+
toolName: inlineToolName,
|
|
7401
|
+
hasEmittedStart: false,
|
|
7402
|
+
emittedInput: "",
|
|
7403
|
+
raw: openTag,
|
|
7404
|
+
args: {},
|
|
7405
|
+
buffer: ""
|
|
7406
|
+
};
|
|
7407
|
+
maybeEmitToolInputStart(controller, newCall);
|
|
7408
|
+
if (selfClosing) {
|
|
7409
|
+
finalizeCall(controller, newCall, inlineToolName, newCall.raw);
|
|
7410
|
+
return;
|
|
7411
|
+
}
|
|
7412
|
+
implicitCall = newCall;
|
|
7413
|
+
implicitCallOpenTag = openTag;
|
|
7414
|
+
};
|
|
7415
|
+
const processImplicitCall = (controller) => {
|
|
7416
|
+
while (implicitCall) {
|
|
7417
|
+
const callState = implicitCall;
|
|
7418
|
+
const { done, remainder } = consumeCall(
|
|
7419
|
+
controller,
|
|
7420
|
+
callState,
|
|
7421
|
+
buffer,
|
|
7422
|
+
null
|
|
7423
|
+
);
|
|
7424
|
+
buffer = "";
|
|
7425
|
+
if (!done) {
|
|
7426
|
+
return;
|
|
7427
|
+
}
|
|
7428
|
+
implicitCall = null;
|
|
7429
|
+
implicitCallOpenTag = null;
|
|
7430
|
+
if (remainder.length > 0) {
|
|
7431
|
+
buffer = remainder;
|
|
7432
|
+
}
|
|
7433
|
+
stripLeadingToolCallCloseTagsFromBuffer();
|
|
7434
|
+
flushSafeTextPrefix(controller);
|
|
7435
|
+
startToolCallIfPresent(controller);
|
|
7436
|
+
if (toolCall) {
|
|
7437
|
+
processToolCall2(controller);
|
|
7438
|
+
return;
|
|
7439
|
+
}
|
|
7440
|
+
startImplicitCallIfPresent(controller);
|
|
7441
|
+
}
|
|
7442
|
+
};
|
|
7443
|
+
const drainStarts = (controller) => {
|
|
7444
|
+
while (true) {
|
|
7445
|
+
if (toolCall || implicitCall) {
|
|
7446
|
+
return;
|
|
7447
|
+
}
|
|
7448
|
+
const before = buffer;
|
|
7449
|
+
startToolCallIfPresent(controller);
|
|
7450
|
+
if (toolCall) {
|
|
7451
|
+
processToolCall2(controller);
|
|
7452
|
+
return;
|
|
7453
|
+
}
|
|
7454
|
+
startImplicitCallIfPresent(controller);
|
|
7455
|
+
if (implicitCall) {
|
|
7456
|
+
processImplicitCall(controller);
|
|
7457
|
+
return;
|
|
7458
|
+
}
|
|
7459
|
+
if (buffer === before) {
|
|
7460
|
+
return;
|
|
7461
|
+
}
|
|
7462
|
+
stripLeadingToolCallCloseTagsFromBuffer();
|
|
7463
|
+
flushSafeTextPrefix(controller);
|
|
7464
|
+
}
|
|
7465
|
+
};
|
|
7466
|
+
const processToolCall2 = (controller) => {
|
|
7467
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
7468
|
+
while (toolCall) {
|
|
7469
|
+
if (toolCall.mode === "unknown") {
|
|
7470
|
+
const callMatch = QWEN3CODER_TOOL_PARSER_STREAM_CALL_OPEN_START_RE.exec(
|
|
7471
|
+
toolCall.innerBuffer
|
|
7472
|
+
);
|
|
7473
|
+
const signalMatch = QWEN3CODER_TOOL_PARSER_STREAM_NAME_OR_PARAM_SIGNAL_RE.exec(
|
|
7474
|
+
toolCall.innerBuffer
|
|
7475
|
+
);
|
|
7476
|
+
if (callMatch && (!signalMatch || ((_a = callMatch.index) != null ? _a : 0) < ((_b = signalMatch.index) != null ? _b : 0))) {
|
|
7477
|
+
toolCall.mode = "multi";
|
|
7478
|
+
} else if (signalMatch) {
|
|
7479
|
+
toolCall.mode = "single";
|
|
7480
|
+
const activeCall = {
|
|
7481
|
+
endTagName: "tool_call",
|
|
7482
|
+
toolCallId: generateToolCallId(),
|
|
7483
|
+
toolName: toolCall.outerNameAttr,
|
|
7484
|
+
hasEmittedStart: false,
|
|
7485
|
+
emittedInput: "",
|
|
7486
|
+
raw: toolCall.outerOpenTag,
|
|
7487
|
+
args: {},
|
|
7488
|
+
buffer: ""
|
|
7489
|
+
};
|
|
7490
|
+
toolCall.activeCall = activeCall;
|
|
7491
|
+
if (toolCall.outerNameAttr) {
|
|
7492
|
+
maybeEmitToolInputStart(controller, activeCall);
|
|
7493
|
+
}
|
|
7494
|
+
} else {
|
|
7495
|
+
return;
|
|
7496
|
+
}
|
|
7497
|
+
}
|
|
7498
|
+
if (toolCall.mode === "single") {
|
|
7499
|
+
const callState = toolCall.activeCall;
|
|
7500
|
+
if (!callState) {
|
|
7501
|
+
return;
|
|
7502
|
+
}
|
|
7503
|
+
const { done, remainder } = consumeCall(
|
|
7504
|
+
controller,
|
|
7505
|
+
callState,
|
|
7506
|
+
toolCall.innerBuffer,
|
|
7507
|
+
toolCall.outerNameAttr
|
|
7508
|
+
);
|
|
7509
|
+
toolCall.innerBuffer = "";
|
|
7510
|
+
if (!done) {
|
|
7511
|
+
return;
|
|
7512
|
+
}
|
|
7513
|
+
toolCall = null;
|
|
7514
|
+
if (remainder.length > 0) {
|
|
7515
|
+
buffer = remainder + buffer;
|
|
7516
|
+
}
|
|
7517
|
+
flushSafeTextPrefix(controller);
|
|
7518
|
+
startToolCallIfPresent(controller);
|
|
7519
|
+
continue;
|
|
7520
|
+
}
|
|
7521
|
+
if (toolCall.mode === "multi") {
|
|
7522
|
+
if (toolCall.activeCall) {
|
|
7523
|
+
const callState = toolCall.activeCall;
|
|
7524
|
+
const { done, remainder } = consumeCall(
|
|
7525
|
+
controller,
|
|
7526
|
+
callState,
|
|
7527
|
+
toolCall.innerBuffer,
|
|
7528
|
+
toolCall.outerNameAttr
|
|
7529
|
+
);
|
|
7530
|
+
toolCall.innerBuffer = "";
|
|
7531
|
+
if (!done) {
|
|
7532
|
+
return;
|
|
7533
|
+
}
|
|
7534
|
+
toolCall.activeCall = null;
|
|
7535
|
+
toolCall.innerBuffer = remainder;
|
|
7536
|
+
continue;
|
|
7537
|
+
}
|
|
7538
|
+
const closeMatch = QWEN3CODER_TOOL_PARSER_STREAM_TOOL_CALL_CLOSE_TAG_RE.exec(
|
|
7539
|
+
toolCall.innerBuffer
|
|
7540
|
+
);
|
|
7541
|
+
const callOpenMatch = QWEN3CODER_TOOL_PARSER_STREAM_CALL_OPEN_TAG_RE.exec(
|
|
7542
|
+
toolCall.innerBuffer
|
|
7543
|
+
);
|
|
7544
|
+
if (!(closeMatch || callOpenMatch)) {
|
|
7545
|
+
return;
|
|
7546
|
+
}
|
|
7547
|
+
const closeIndex = (_c = closeMatch == null ? void 0 : closeMatch.index) != null ? _c : -1;
|
|
7548
|
+
const callIndex = (_d = callOpenMatch == null ? void 0 : callOpenMatch.index) != null ? _d : -1;
|
|
7549
|
+
const hasClose = closeIndex !== -1;
|
|
7550
|
+
const hasCall = callIndex !== -1;
|
|
7551
|
+
const chooseClose = hasClose && (!hasCall || closeIndex < callIndex);
|
|
7552
|
+
const nextIndex = chooseClose ? closeIndex : callIndex;
|
|
7553
|
+
if (nextIndex > 0) {
|
|
7554
|
+
toolCall.innerBuffer = toolCall.innerBuffer.slice(nextIndex);
|
|
7555
|
+
}
|
|
7556
|
+
if (chooseClose) {
|
|
7557
|
+
const matchLen = (_f = (_e = closeMatch == null ? void 0 : closeMatch[0]) == null ? void 0 : _e.length) != null ? _f : 0;
|
|
7558
|
+
const remainder = toolCall.innerBuffer.slice(matchLen);
|
|
7559
|
+
toolCall = null;
|
|
7560
|
+
if (remainder.length > 0) {
|
|
7561
|
+
buffer = remainder + buffer;
|
|
7562
|
+
}
|
|
7563
|
+
flushSafeTextPrefix(controller);
|
|
7564
|
+
startToolCallIfPresent(controller);
|
|
7565
|
+
continue;
|
|
7566
|
+
}
|
|
7567
|
+
if (!callOpenMatch) {
|
|
7568
|
+
return;
|
|
7569
|
+
}
|
|
7570
|
+
const openTag = (_g = callOpenMatch[0]) != null ? _g : "";
|
|
7571
|
+
const callTagName = ((_h = callOpenMatch[1]) != null ? _h : "").toLowerCase();
|
|
7572
|
+
const rest = toolCall.innerBuffer.slice(openTag.length);
|
|
7573
|
+
const selfClosing = QWEN3CODER_TOOL_PARSER_STREAM_SELF_CLOSING_TAG_RE.test(openTag);
|
|
7574
|
+
if (selfClosing) {
|
|
7575
|
+
const toolNameAttr2 = (_j = (_i = getAttributeValue(openTag, "name")) != null ? _i : getShorthandValue(openTag)) != null ? _j : toolCall.outerNameAttr;
|
|
7576
|
+
const immediateCall = {
|
|
7577
|
+
endTagName: callTagName,
|
|
7578
|
+
toolCallId: generateToolCallId(),
|
|
7579
|
+
toolName: toolNameAttr2,
|
|
7580
|
+
hasEmittedStart: false,
|
|
7581
|
+
emittedInput: "",
|
|
7582
|
+
raw: openTag,
|
|
7583
|
+
args: {},
|
|
7584
|
+
buffer: ""
|
|
7585
|
+
};
|
|
7586
|
+
const ok = finalizeCall(
|
|
7587
|
+
controller,
|
|
7588
|
+
immediateCall,
|
|
7589
|
+
toolNameAttr2,
|
|
7590
|
+
immediateCall.raw
|
|
7591
|
+
);
|
|
7592
|
+
if (ok) {
|
|
7593
|
+
toolCall.emittedToolCallCount += 1;
|
|
7594
|
+
}
|
|
7595
|
+
toolCall.innerBuffer = rest;
|
|
7596
|
+
continue;
|
|
7597
|
+
}
|
|
7598
|
+
const toolNameAttr = (_k = getAttributeValue(openTag, "name")) != null ? _k : getShorthandValue(openTag);
|
|
7599
|
+
const newCall = {
|
|
7600
|
+
endTagName: callTagName,
|
|
7601
|
+
toolCallId: generateToolCallId(),
|
|
7602
|
+
toolName: toolNameAttr,
|
|
7603
|
+
hasEmittedStart: false,
|
|
7604
|
+
emittedInput: "",
|
|
7605
|
+
raw: openTag,
|
|
7606
|
+
args: {},
|
|
7607
|
+
buffer: ""
|
|
7608
|
+
};
|
|
7609
|
+
if (toolNameAttr) {
|
|
7610
|
+
maybeEmitToolInputStart(controller, newCall);
|
|
7611
|
+
}
|
|
7612
|
+
toolCall.activeCall = newCall;
|
|
7613
|
+
toolCall.innerBuffer = rest;
|
|
7614
|
+
}
|
|
7615
|
+
}
|
|
7616
|
+
};
|
|
7617
|
+
const reportUnfinishedToolCallAtFinish = (controller, rawToolCall) => {
|
|
7618
|
+
var _a;
|
|
7619
|
+
const shouldEmitRaw = shouldEmitRawToolCallTextOnError3(options);
|
|
7620
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
7621
|
+
options,
|
|
7622
|
+
shouldEmitRaw ? "Could not complete streaming Qwen3CoderToolParser XML tool call at finish; emitting original text." : "Could not complete streaming Qwen3CoderToolParser XML tool call at finish.",
|
|
7623
|
+
{ toolCall: rawToolCall }
|
|
7624
|
+
);
|
|
7625
|
+
if (shouldEmitRaw) {
|
|
7626
|
+
flushText(controller, rawToolCall);
|
|
7627
|
+
}
|
|
7628
|
+
};
|
|
7629
|
+
const reportUnfinishedImplicitCallAtFinish = (controller, rawCallText) => {
|
|
7630
|
+
var _a;
|
|
7631
|
+
const shouldEmitRaw = shouldEmitRawToolCallTextOnError3(options);
|
|
7632
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
7633
|
+
options,
|
|
7634
|
+
shouldEmitRaw ? "Could not complete streaming Qwen3CoderToolParser call block at finish; emitting original text." : "Could not complete streaming Qwen3CoderToolParser call block at finish.",
|
|
7635
|
+
{ toolCall: rawCallText }
|
|
7636
|
+
);
|
|
7637
|
+
if (shouldEmitRaw) {
|
|
7638
|
+
flushText(controller, rawCallText);
|
|
7639
|
+
}
|
|
7640
|
+
};
|
|
7641
|
+
const handleFinish = (controller) => {
|
|
7642
|
+
var _a, _b;
|
|
7643
|
+
if (toolCall) {
|
|
7644
|
+
processToolCall2(controller);
|
|
7645
|
+
if (toolCall) {
|
|
7646
|
+
if (toolCall.mode === "unknown") {
|
|
7647
|
+
const callMatch = QWEN3CODER_TOOL_PARSER_STREAM_CALL_OPEN_START_RE.exec(
|
|
7648
|
+
toolCall.innerBuffer
|
|
7649
|
+
);
|
|
7650
|
+
const signalMatch = QWEN3CODER_TOOL_PARSER_STREAM_NAME_OR_PARAM_SIGNAL_RE.exec(
|
|
7651
|
+
toolCall.innerBuffer
|
|
7652
|
+
);
|
|
7653
|
+
if (callMatch && (!signalMatch || ((_a = callMatch.index) != null ? _a : 0) < ((_b = signalMatch.index) != null ? _b : 0))) {
|
|
7654
|
+
toolCall.mode = "multi";
|
|
7655
|
+
} else if (signalMatch) {
|
|
7656
|
+
toolCall.mode = "single";
|
|
7657
|
+
toolCall.activeCall = {
|
|
7658
|
+
endTagName: "tool_call",
|
|
7659
|
+
toolCallId: generateToolCallId(),
|
|
7660
|
+
toolName: toolCall.outerNameAttr,
|
|
7661
|
+
hasEmittedStart: false,
|
|
7662
|
+
emittedInput: "",
|
|
7663
|
+
raw: toolCall.outerOpenTag,
|
|
7664
|
+
args: {},
|
|
7665
|
+
buffer: ""
|
|
7666
|
+
};
|
|
7667
|
+
}
|
|
7668
|
+
}
|
|
7669
|
+
if (toolCall.mode === "single" && toolCall.activeCall) {
|
|
7670
|
+
toolCall.activeCall.buffer += toolCall.innerBuffer;
|
|
7671
|
+
toolCall.innerBuffer = "";
|
|
7672
|
+
const result = finalizeCallAtFinish(
|
|
7673
|
+
controller,
|
|
7674
|
+
toolCall.activeCall,
|
|
7675
|
+
toolCall.outerNameAttr
|
|
7676
|
+
);
|
|
7677
|
+
if (result.ok) {
|
|
7678
|
+
toolCall.emittedToolCallCount += 1;
|
|
7679
|
+
}
|
|
7680
|
+
const shouldFlushTrailingText = result.ok || !shouldEmitRawToolCallTextOnError3(options);
|
|
7681
|
+
if (shouldFlushTrailingText && result.trailingText.length > 0) {
|
|
7682
|
+
flushText(controller, result.trailingText);
|
|
7683
|
+
}
|
|
7684
|
+
if (!result.ok && toolCall.emittedToolCallCount === 0) {
|
|
7685
|
+
reportUnfinishedToolCallAtFinish(controller, toolCall.raw);
|
|
7686
|
+
}
|
|
7687
|
+
} else if (toolCall.mode === "multi") {
|
|
7688
|
+
if (toolCall.activeCall) {
|
|
7689
|
+
const result = finalizeCallAtFinish(
|
|
7690
|
+
controller,
|
|
7691
|
+
toolCall.activeCall,
|
|
7692
|
+
toolCall.outerNameAttr
|
|
7693
|
+
);
|
|
7694
|
+
if (result.ok) {
|
|
7695
|
+
toolCall.emittedToolCallCount += 1;
|
|
7696
|
+
}
|
|
7697
|
+
const shouldFlushTrailingText = result.ok || !shouldEmitRawToolCallTextOnError3(options);
|
|
7698
|
+
if (shouldFlushTrailingText && result.trailingText.length > 0) {
|
|
7699
|
+
flushText(controller, result.trailingText);
|
|
7700
|
+
}
|
|
7701
|
+
if (!result.ok && toolCall.emittedToolCallCount === 0) {
|
|
7702
|
+
reportUnfinishedToolCallAtFinish(controller, toolCall.raw);
|
|
7703
|
+
}
|
|
7704
|
+
toolCall.activeCall = null;
|
|
7705
|
+
} else if (toolCall.emittedToolCallCount === 0) {
|
|
7706
|
+
reportUnfinishedToolCallAtFinish(controller, toolCall.raw);
|
|
7707
|
+
}
|
|
7708
|
+
} else {
|
|
7709
|
+
reportUnfinishedToolCallAtFinish(controller, toolCall.raw);
|
|
7710
|
+
}
|
|
7711
|
+
toolCall = null;
|
|
7712
|
+
}
|
|
7713
|
+
}
|
|
7714
|
+
if (implicitCall) {
|
|
7715
|
+
const callState = implicitCall;
|
|
7716
|
+
const openTag = implicitCallOpenTag;
|
|
7717
|
+
implicitCall = null;
|
|
7718
|
+
implicitCallOpenTag = null;
|
|
7719
|
+
const result = finalizeCallAtFinish(controller, callState, null);
|
|
7720
|
+
const shouldFlushTrailingText = result.ok || !shouldEmitRawToolCallTextOnError3(options);
|
|
7721
|
+
if (shouldFlushTrailingText && result.trailingText.length > 0) {
|
|
7722
|
+
flushText(controller, result.trailingText);
|
|
7723
|
+
}
|
|
7724
|
+
if (!result.ok && openTag) {
|
|
7725
|
+
reportUnfinishedImplicitCallAtFinish(
|
|
7726
|
+
controller,
|
|
7727
|
+
callState.raw || openTag + callState.buffer
|
|
7728
|
+
);
|
|
7729
|
+
}
|
|
7730
|
+
} else {
|
|
7731
|
+
stripLeadingToolCallCloseTagsFromBuffer();
|
|
7732
|
+
flushSafeTextPrefix(controller);
|
|
7733
|
+
drainStarts(controller);
|
|
7734
|
+
}
|
|
7735
|
+
if (buffer.length > 0) {
|
|
7736
|
+
flushText(controller, buffer);
|
|
7737
|
+
buffer = "";
|
|
7738
|
+
}
|
|
7739
|
+
flushText(controller);
|
|
7740
|
+
};
|
|
7741
|
+
const handlePassthroughChunk = (controller, chunk) => {
|
|
7742
|
+
if (!toolCall && buffer) {
|
|
7743
|
+
flushText(controller, buffer);
|
|
7744
|
+
buffer = "";
|
|
7745
|
+
}
|
|
7746
|
+
controller.enqueue(chunk);
|
|
7747
|
+
};
|
|
7748
|
+
const handleTextDeltaChunk = (controller, delta) => {
|
|
7749
|
+
if (toolCall) {
|
|
7750
|
+
toolCall.raw += delta;
|
|
7751
|
+
toolCall.innerBuffer += delta;
|
|
7752
|
+
processToolCall2(controller);
|
|
7753
|
+
return;
|
|
7754
|
+
}
|
|
7755
|
+
if (implicitCall) {
|
|
7756
|
+
const callState = implicitCall;
|
|
7757
|
+
const { done, remainder } = consumeCall(
|
|
7758
|
+
controller,
|
|
7759
|
+
callState,
|
|
7760
|
+
delta,
|
|
7761
|
+
null
|
|
7762
|
+
);
|
|
7763
|
+
if (!done) {
|
|
7764
|
+
return;
|
|
7765
|
+
}
|
|
7766
|
+
implicitCall = null;
|
|
7767
|
+
implicitCallOpenTag = null;
|
|
7768
|
+
if (remainder.length > 0) {
|
|
7769
|
+
buffer = remainder + buffer;
|
|
7770
|
+
}
|
|
7771
|
+
stripLeadingToolCallCloseTagsFromBuffer();
|
|
7772
|
+
flushSafeTextPrefix(controller);
|
|
7773
|
+
drainStarts(controller);
|
|
7774
|
+
return;
|
|
7775
|
+
}
|
|
7776
|
+
buffer += delta;
|
|
7777
|
+
stripLeadingToolCallCloseTagsFromBuffer();
|
|
7778
|
+
flushSafeTextPrefix(controller);
|
|
7779
|
+
drainStarts(controller);
|
|
7780
|
+
};
|
|
7781
|
+
const handleTransformChunk = (controller, chunk) => {
|
|
7782
|
+
if (chunk.type === "finish") {
|
|
7783
|
+
handleFinish(controller);
|
|
7784
|
+
controller.enqueue(chunk);
|
|
7785
|
+
return;
|
|
7786
|
+
}
|
|
7787
|
+
if (chunk.type !== "text-delta") {
|
|
7788
|
+
handlePassthroughChunk(controller, chunk);
|
|
7789
|
+
return;
|
|
7790
|
+
}
|
|
7791
|
+
const delta = chunk.delta;
|
|
7792
|
+
if (!delta) {
|
|
7793
|
+
return;
|
|
7794
|
+
}
|
|
7795
|
+
handleTextDeltaChunk(controller, delta);
|
|
7796
|
+
};
|
|
7797
|
+
return new TransformStream({
|
|
7798
|
+
transform(chunk, controller) {
|
|
7799
|
+
handleTransformChunk(controller, chunk);
|
|
7800
|
+
},
|
|
7801
|
+
flush(controller) {
|
|
7802
|
+
handleFinish(controller);
|
|
7803
|
+
}
|
|
7804
|
+
});
|
|
7805
|
+
}
|
|
7806
|
+
});
|
|
7807
|
+
var uiTarsXmlProtocol = qwen3CoderProtocol;
|
|
7808
|
+
var Qwen3CoderToolParser = qwen3CoderProtocol;
|
|
7809
|
+
|
|
7810
|
+
// src/core/protocols/yaml-xml-protocol.ts
|
|
7811
|
+
var import_yaml = __toESM(require("yaml"), 1);
|
|
7812
|
+
function shouldEmitRawToolCallTextOnError4(options) {
|
|
7813
|
+
return (options == null ? void 0 : options.emitRawToolCallTextOnError) === true;
|
|
7814
|
+
}
|
|
7815
|
+
var selfClosingTagCache2 = /* @__PURE__ */ new Map();
|
|
7816
|
+
function getSelfClosingTagPattern2(toolName) {
|
|
7817
|
+
let pattern = selfClosingTagCache2.get(toolName);
|
|
7818
|
+
if (!pattern) {
|
|
7819
|
+
pattern = new RegExp(`<\\s*${escapeRegExp(toolName)}\\s*/>`, "g");
|
|
7820
|
+
selfClosingTagCache2.set(toolName, pattern);
|
|
7821
|
+
}
|
|
7822
|
+
return pattern;
|
|
7823
|
+
}
|
|
7824
|
+
var LEADING_WHITESPACE_RE = /^(\s*)/;
|
|
7825
|
+
var INCOMPLETE_MAPPING_TAIL_RE = /^[^:[\]{}-][^:]*:\s*$/;
|
|
7826
|
+
var INCOMPLETE_SEQUENCE_TAIL_RE = /^-\s*$/;
|
|
7827
|
+
var BLOCK_SCALAR_KEY_RE = /:\s*[|>][-+0-9]*\s*$/;
|
|
7828
|
+
var PLAIN_MAPPING_VALUE_RE = /^[^:[\]{}-][^:]*:\s*(.+)$/;
|
|
7829
|
+
var PLAIN_SEQUENCE_VALUE_RE = /^-\s+(.+)$/;
|
|
7830
|
+
function normalizeYamlContent(yamlContent) {
|
|
7831
|
+
let normalized = yamlContent;
|
|
7832
|
+
if (normalized.startsWith("\n")) {
|
|
7833
|
+
normalized = normalized.slice(1);
|
|
7834
|
+
}
|
|
7835
|
+
const lines = normalized.split("\n");
|
|
7836
|
+
const nonEmptyLines = lines.filter((line) => line.trim().length > 0);
|
|
7837
|
+
if (nonEmptyLines.length === 0) {
|
|
7838
|
+
return { normalized: "", nonEmptyLines };
|
|
7839
|
+
}
|
|
7840
|
+
const minIndent = Math.min(
|
|
7841
|
+
...nonEmptyLines.map((line) => {
|
|
7842
|
+
const match = line.match(LEADING_WHITESPACE_RE);
|
|
7843
|
+
return match ? match[1].length : 0;
|
|
7844
|
+
})
|
|
7845
|
+
);
|
|
7846
|
+
if (minIndent > 0) {
|
|
7847
|
+
normalized = lines.map((line) => line.slice(minIndent)).join("\n");
|
|
7848
|
+
}
|
|
7849
|
+
return { normalized, nonEmptyLines };
|
|
7850
|
+
}
|
|
7851
|
+
function parseYamlDocumentAsMapping(normalized) {
|
|
7852
|
+
try {
|
|
7853
|
+
const doc = import_yaml.default.parseDocument(normalized);
|
|
7854
|
+
const errors = doc.errors.map((e) => e.message);
|
|
7855
|
+
const result = doc.toJSON();
|
|
7856
|
+
if (result === null) {
|
|
7857
|
+
return { value: {}, errors };
|
|
7858
|
+
}
|
|
7859
|
+
if (typeof result !== "object" || Array.isArray(result)) {
|
|
7860
|
+
return { value: null, errors };
|
|
7861
|
+
}
|
|
7862
|
+
return { value: result, errors };
|
|
7863
|
+
} catch (error) {
|
|
7864
|
+
return {
|
|
7865
|
+
value: null,
|
|
7866
|
+
errors: [
|
|
7867
|
+
error instanceof Error ? error.message : "Unknown YAML parsing error"
|
|
7868
|
+
]
|
|
7869
|
+
};
|
|
7870
|
+
}
|
|
7871
|
+
}
|
|
7872
|
+
function getLastMeaningfulLineInfo(input) {
|
|
7873
|
+
var _a;
|
|
7874
|
+
const lines = input.split("\n");
|
|
7875
|
+
let index = lines.length - 1;
|
|
7876
|
+
while (index >= 0) {
|
|
7877
|
+
const raw = (_a = lines[index]) != null ? _a : "";
|
|
7878
|
+
const trimmed = raw.trim();
|
|
7879
|
+
if (trimmed.length > 0 && !trimmed.startsWith("#")) {
|
|
7880
|
+
return {
|
|
7881
|
+
index,
|
|
7882
|
+
raw,
|
|
7883
|
+
trimmed,
|
|
7884
|
+
indent: raw.length - raw.trimStart().length
|
|
7885
|
+
};
|
|
7886
|
+
}
|
|
7887
|
+
index -= 1;
|
|
7888
|
+
}
|
|
7889
|
+
return null;
|
|
7890
|
+
}
|
|
7891
|
+
function dropLastMeaningfulLine(input) {
|
|
7892
|
+
const lineInfo = getLastMeaningfulLineInfo(input);
|
|
7893
|
+
if (!lineInfo) {
|
|
7894
|
+
return null;
|
|
7895
|
+
}
|
|
7896
|
+
return input.split("\n").slice(0, lineInfo.index).join("\n").trimEnd();
|
|
7897
|
+
}
|
|
7898
|
+
function hasIncompleteMappingTail(normalized) {
|
|
7899
|
+
const lineInfo = getLastMeaningfulLineInfo(normalized);
|
|
7900
|
+
if (!lineInfo) {
|
|
7901
|
+
return false;
|
|
7902
|
+
}
|
|
7903
|
+
return INCOMPLETE_MAPPING_TAIL_RE.test(lineInfo.trimmed);
|
|
7904
|
+
}
|
|
7905
|
+
function hasIncompleteSequenceTail(normalized) {
|
|
7906
|
+
const lineInfo = getLastMeaningfulLineInfo(normalized);
|
|
7907
|
+
if (!lineInfo) {
|
|
7908
|
+
return false;
|
|
7909
|
+
}
|
|
7910
|
+
return INCOMPLETE_SEQUENCE_TAIL_RE.test(lineInfo.trimmed);
|
|
7911
|
+
}
|
|
7912
|
+
function hasSplitNestedKeyTail(normalized) {
|
|
7913
|
+
var _a;
|
|
7914
|
+
const lineInfo = getLastMeaningfulLineInfo(normalized);
|
|
7915
|
+
if (!lineInfo) {
|
|
7916
|
+
return false;
|
|
7917
|
+
}
|
|
7918
|
+
const { trimmed, indent, index } = lineInfo;
|
|
7919
|
+
if (indent === 0) {
|
|
7920
|
+
return false;
|
|
7921
|
+
}
|
|
7922
|
+
if (trimmed.startsWith("#") || trimmed.startsWith("-") || trimmed.includes(":")) {
|
|
7923
|
+
return false;
|
|
7924
|
+
}
|
|
7925
|
+
const lines = normalized.split("\n");
|
|
7926
|
+
let parentIndex = index - 1;
|
|
7927
|
+
while (parentIndex >= 0) {
|
|
7928
|
+
const parentRaw = (_a = lines[parentIndex]) != null ? _a : "";
|
|
7929
|
+
const parentTrimmed = parentRaw.trim();
|
|
7930
|
+
if (parentTrimmed.length === 0 || parentTrimmed.startsWith("#")) {
|
|
7931
|
+
parentIndex -= 1;
|
|
7932
|
+
continue;
|
|
7933
|
+
}
|
|
7934
|
+
const parentIndent = parentRaw.length - parentRaw.trimStart().length;
|
|
7935
|
+
if (parentIndent >= indent) {
|
|
7936
|
+
parentIndex -= 1;
|
|
7937
|
+
continue;
|
|
7938
|
+
}
|
|
7939
|
+
if (!parentTrimmed.endsWith(":")) {
|
|
7940
|
+
return false;
|
|
7941
|
+
}
|
|
7942
|
+
if (BLOCK_SCALAR_KEY_RE.test(parentTrimmed)) {
|
|
7943
|
+
return false;
|
|
7944
|
+
}
|
|
7945
|
+
return true;
|
|
7946
|
+
}
|
|
7947
|
+
return false;
|
|
7948
|
+
}
|
|
7949
|
+
function extractTrailingPlainScalarValue(line) {
|
|
7950
|
+
var _a;
|
|
7951
|
+
if (BLOCK_SCALAR_KEY_RE.test(line)) {
|
|
7952
|
+
return null;
|
|
7953
|
+
}
|
|
7954
|
+
const mappingMatch = line.match(PLAIN_MAPPING_VALUE_RE);
|
|
7955
|
+
const sequenceMatch = line.match(PLAIN_SEQUENCE_VALUE_RE);
|
|
7956
|
+
const value = (_a = mappingMatch == null ? void 0 : mappingMatch[1]) != null ? _a : sequenceMatch == null ? void 0 : sequenceMatch[1];
|
|
7957
|
+
if (!value) {
|
|
7958
|
+
return null;
|
|
7959
|
+
}
|
|
7960
|
+
const trimmedValue = value.trim();
|
|
7961
|
+
if (trimmedValue.length === 0) {
|
|
7962
|
+
return null;
|
|
7963
|
+
}
|
|
7964
|
+
if (trimmedValue.startsWith('"') || trimmedValue.startsWith("'")) {
|
|
7965
|
+
return null;
|
|
7966
|
+
}
|
|
7967
|
+
if (trimmedValue.startsWith("{") || trimmedValue.startsWith("[") || trimmedValue.startsWith("|") || trimmedValue.startsWith(">")) {
|
|
7968
|
+
return null;
|
|
7969
|
+
}
|
|
7970
|
+
return trimmedValue;
|
|
7971
|
+
}
|
|
7972
|
+
function hasUnterminatedPlainScalarTail(normalized) {
|
|
7973
|
+
if (normalized.endsWith("\n")) {
|
|
7974
|
+
return false;
|
|
7975
|
+
}
|
|
7976
|
+
const lineInfo = getLastMeaningfulLineInfo(normalized);
|
|
7977
|
+
if (!lineInfo) {
|
|
7978
|
+
return false;
|
|
7979
|
+
}
|
|
7980
|
+
return extractTrailingPlainScalarValue(lineInfo.trimmed) != null;
|
|
7981
|
+
}
|
|
7982
|
+
function hasUnstableProgressTail(normalized) {
|
|
7983
|
+
return hasIncompleteMappingTail(normalized) || hasIncompleteSequenceTail(normalized) || hasSplitNestedKeyTail(normalized) || hasUnterminatedPlainScalarTail(normalized);
|
|
7984
|
+
}
|
|
7985
|
+
function trimTrailingNewlineInUnknown(value) {
|
|
7986
|
+
if (typeof value === "string") {
|
|
7987
|
+
if (value.endsWith("\n")) {
|
|
7988
|
+
return value.slice(0, -1);
|
|
7989
|
+
}
|
|
7990
|
+
return value;
|
|
7991
|
+
}
|
|
7992
|
+
if (Array.isArray(value)) {
|
|
7993
|
+
return value.map((item) => trimTrailingNewlineInUnknown(item));
|
|
7994
|
+
}
|
|
7995
|
+
if (value && typeof value === "object") {
|
|
6103
7996
|
return Object.fromEntries(
|
|
6104
7997
|
Object.entries(value).map(([key, item]) => [
|
|
6105
7998
|
key,
|
|
@@ -6115,7 +8008,7 @@ function stabilizeParsedValueForStreamProgress(value, source) {
|
|
|
6115
8008
|
}
|
|
6116
8009
|
return trimTrailingNewlineInUnknown(value);
|
|
6117
8010
|
}
|
|
6118
|
-
function
|
|
8011
|
+
function findClosingTagEnd2(text, contentStart, toolName) {
|
|
6119
8012
|
let pos = contentStart;
|
|
6120
8013
|
let depth = 1;
|
|
6121
8014
|
while (pos < text.length) {
|
|
@@ -6186,9 +8079,9 @@ function findEarliestTagPosition(openIdx, selfIdx) {
|
|
|
6186
8079
|
function collectToolCallsForName(text, toolName) {
|
|
6187
8080
|
const toolCalls = [];
|
|
6188
8081
|
let searchIndex = 0;
|
|
8082
|
+
const startTag = `<${toolName}>`;
|
|
6189
8083
|
const selfTagRegex = getSelfClosingTagPattern2(toolName);
|
|
6190
8084
|
while (searchIndex < text.length) {
|
|
6191
|
-
const startTag = `<${toolName}>`;
|
|
6192
8085
|
const openIdx = text.indexOf(startTag, searchIndex);
|
|
6193
8086
|
selfTagRegex.lastIndex = searchIndex;
|
|
6194
8087
|
const selfMatch = selfTagRegex.exec(text);
|
|
@@ -6213,7 +8106,7 @@ function collectToolCallsForName(text, toolName) {
|
|
|
6213
8106
|
continue;
|
|
6214
8107
|
}
|
|
6215
8108
|
const contentStart = tagStart + startTag.length;
|
|
6216
|
-
const fullTagEnd =
|
|
8109
|
+
const fullTagEnd = findClosingTagEnd2(text, contentStart, toolName);
|
|
6217
8110
|
if (fullTagEnd !== -1 && fullTagEnd > contentStart) {
|
|
6218
8111
|
const endTag = `</${toolName}>`;
|
|
6219
8112
|
const endTagStart = fullTagEnd - endTag.length;
|
|
@@ -6363,7 +8256,7 @@ function stripTrailingPartialCloseTag(content, toolName) {
|
|
|
6363
8256
|
)}${preservedLeadingWhitespace}`;
|
|
6364
8257
|
return contentWithoutPartial.trimEnd();
|
|
6365
8258
|
}
|
|
6366
|
-
var
|
|
8259
|
+
var yamlXmlProtocol = (_protocolOptions) => {
|
|
6367
8260
|
return {
|
|
6368
8261
|
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
6369
8262
|
return toolSystemPromptTemplate(tools || []);
|
|
@@ -6487,7 +8380,7 @@ ${yamlContent}</${toolCall.toolName}>`;
|
|
|
6487
8380
|
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse streaming YAML tool call", {
|
|
6488
8381
|
toolCall: original
|
|
6489
8382
|
});
|
|
6490
|
-
if (
|
|
8383
|
+
if (shouldEmitRawToolCallTextOnError4(options)) {
|
|
6491
8384
|
flushText(controller, original);
|
|
6492
8385
|
}
|
|
6493
8386
|
}
|
|
@@ -6532,7 +8425,7 @@ ${yamlContent}</${toolCall.toolName}>`;
|
|
|
6532
8425
|
"Could not complete streaming YAML tool call at finish.",
|
|
6533
8426
|
{ toolCall: unfinishedContent }
|
|
6534
8427
|
);
|
|
6535
|
-
if (
|
|
8428
|
+
if (shouldEmitRawToolCallTextOnError4(options)) {
|
|
6536
8429
|
flushText(controller, unfinishedContent);
|
|
6537
8430
|
}
|
|
6538
8431
|
}
|
|
@@ -7073,36 +8966,6 @@ function recoverToolCallFromJsonCandidates(text, tools) {
|
|
|
7073
8966
|
return null;
|
|
7074
8967
|
}
|
|
7075
8968
|
|
|
7076
|
-
// src/core/utils/tool-call-coercion.ts
|
|
7077
|
-
function coerceToolCallInput(toolName, input, tools) {
|
|
7078
|
-
var _a;
|
|
7079
|
-
let args = {};
|
|
7080
|
-
if (typeof input === "string") {
|
|
7081
|
-
try {
|
|
7082
|
-
args = JSON.parse(input);
|
|
7083
|
-
} catch (e) {
|
|
7084
|
-
return;
|
|
7085
|
-
}
|
|
7086
|
-
} else if (input && typeof input === "object") {
|
|
7087
|
-
args = input;
|
|
7088
|
-
} else {
|
|
7089
|
-
return;
|
|
7090
|
-
}
|
|
7091
|
-
const schema = (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
7092
|
-
const coerced = coerceBySchema(args, schema);
|
|
7093
|
-
return JSON.stringify(coerced != null ? coerced : {});
|
|
7094
|
-
}
|
|
7095
|
-
function coerceToolCallPart(part, tools) {
|
|
7096
|
-
const coercedInput = coerceToolCallInput(part.toolName, part.input, tools);
|
|
7097
|
-
if (coercedInput === void 0) {
|
|
7098
|
-
return part;
|
|
7099
|
-
}
|
|
7100
|
-
return {
|
|
7101
|
-
...part,
|
|
7102
|
-
input: coercedInput
|
|
7103
|
-
};
|
|
7104
|
-
}
|
|
7105
|
-
|
|
7106
8969
|
// src/core/utils/tool-choice.ts
|
|
7107
8970
|
function ensureNonEmptyToolName(name) {
|
|
7108
8971
|
if (typeof name !== "string") {
|
|
@@ -7310,23 +9173,127 @@ async function wrapGenerate({
|
|
|
7310
9173
|
};
|
|
7311
9174
|
}
|
|
7312
9175
|
|
|
7313
|
-
// src/core/prompts/
|
|
7314
|
-
function
|
|
7315
|
-
|
|
7316
|
-
return `You are a function calling AI model.
|
|
7317
|
-
You are provided with function signatures within <tools></tools> XML tags.
|
|
7318
|
-
You may call one or more functions to assist with the user query.
|
|
7319
|
-
Don't make assumptions about what values to plug into functions.
|
|
7320
|
-
Here are the available tools: <tools>${toolsJson}</tools>
|
|
7321
|
-
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"]}
|
|
7322
|
-
For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
|
|
7323
|
-
<tool_call>
|
|
7324
|
-
{"name": "<function-name>", "arguments": <args-dict>}
|
|
7325
|
-
</tool_call>`;
|
|
9176
|
+
// src/core/prompts/shared/tool-result-normalizer.ts
|
|
9177
|
+
function isMapping(value) {
|
|
9178
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7326
9179
|
}
|
|
7327
|
-
|
|
7328
|
-
|
|
7329
|
-
|
|
9180
|
+
function getMediaKindFromMediaType(mediaType) {
|
|
9181
|
+
if (mediaType.startsWith("image/")) {
|
|
9182
|
+
return "image";
|
|
9183
|
+
}
|
|
9184
|
+
if (mediaType.startsWith("audio/")) {
|
|
9185
|
+
return "audio";
|
|
9186
|
+
}
|
|
9187
|
+
if (mediaType.startsWith("video/")) {
|
|
9188
|
+
return "video";
|
|
9189
|
+
}
|
|
9190
|
+
return "file";
|
|
9191
|
+
}
|
|
9192
|
+
function getContentPartMediaKind(part) {
|
|
9193
|
+
const contentPart = isMapping(part) ? part : void 0;
|
|
9194
|
+
const type = contentPart == null ? void 0 : contentPart.type;
|
|
9195
|
+
switch (type) {
|
|
9196
|
+
case "image-data":
|
|
9197
|
+
case "image-url":
|
|
9198
|
+
case "image-file-id":
|
|
9199
|
+
return "image";
|
|
9200
|
+
case "file-data":
|
|
9201
|
+
case "file-url":
|
|
9202
|
+
case "file-id": {
|
|
9203
|
+
const mediaType = contentPart == null ? void 0 : contentPart.mediaType;
|
|
9204
|
+
if (typeof mediaType === "string") {
|
|
9205
|
+
return getMediaKindFromMediaType(mediaType);
|
|
9206
|
+
}
|
|
9207
|
+
return "file";
|
|
9208
|
+
}
|
|
9209
|
+
case "media": {
|
|
9210
|
+
const mediaType = contentPart == null ? void 0 : contentPart.mediaType;
|
|
9211
|
+
if (typeof mediaType === "string") {
|
|
9212
|
+
return getMediaKindFromMediaType(mediaType);
|
|
9213
|
+
}
|
|
9214
|
+
return "file";
|
|
9215
|
+
}
|
|
9216
|
+
default:
|
|
9217
|
+
return null;
|
|
9218
|
+
}
|
|
9219
|
+
}
|
|
9220
|
+
function shouldPassRawByStrategy(mediaKind, strategy) {
|
|
9221
|
+
var _a, _b;
|
|
9222
|
+
const mode = (_a = strategy == null ? void 0 : strategy.mode) != null ? _a : "placeholder";
|
|
9223
|
+
if (mode === "raw") {
|
|
9224
|
+
return true;
|
|
9225
|
+
}
|
|
9226
|
+
if (mode === "placeholder") {
|
|
9227
|
+
return false;
|
|
9228
|
+
}
|
|
9229
|
+
if (mode === "model") {
|
|
9230
|
+
return false;
|
|
9231
|
+
}
|
|
9232
|
+
return ((_b = strategy == null ? void 0 : strategy.capabilities) == null ? void 0 : _b[mediaKind]) === true;
|
|
9233
|
+
}
|
|
9234
|
+
function shouldPassRawContent(contentParts, strategy) {
|
|
9235
|
+
var _a;
|
|
9236
|
+
const mode = (_a = strategy == null ? void 0 : strategy.mode) != null ? _a : "placeholder";
|
|
9237
|
+
if (mode === "raw") {
|
|
9238
|
+
return true;
|
|
9239
|
+
}
|
|
9240
|
+
if (mode === "placeholder") {
|
|
9241
|
+
return false;
|
|
9242
|
+
}
|
|
9243
|
+
if (mode === "model") {
|
|
9244
|
+
return false;
|
|
9245
|
+
}
|
|
9246
|
+
let hasSupportedMediaContent = false;
|
|
9247
|
+
for (const part of contentParts) {
|
|
9248
|
+
const mediaKind = getContentPartMediaKind(part);
|
|
9249
|
+
if (!mediaKind) {
|
|
9250
|
+
continue;
|
|
9251
|
+
}
|
|
9252
|
+
hasSupportedMediaContent = true;
|
|
9253
|
+
if (!shouldPassRawByStrategy(mediaKind, strategy)) {
|
|
9254
|
+
return false;
|
|
9255
|
+
}
|
|
9256
|
+
}
|
|
9257
|
+
return hasSupportedMediaContent;
|
|
9258
|
+
}
|
|
9259
|
+
function formatContentPartPlaceholder(part) {
|
|
9260
|
+
var _a;
|
|
9261
|
+
const contentPart = part;
|
|
9262
|
+
switch (contentPart.type) {
|
|
9263
|
+
case "text":
|
|
9264
|
+
return (_a = contentPart.text) != null ? _a : "";
|
|
9265
|
+
case "image-data":
|
|
9266
|
+
return `[Image: ${contentPart.mediaType}]`;
|
|
9267
|
+
case "image-url":
|
|
9268
|
+
return `[Image URL: ${contentPart.url}]`;
|
|
9269
|
+
case "image-file-id": {
|
|
9270
|
+
const fileId = contentPart.fileId;
|
|
9271
|
+
const displayId = typeof fileId === "string" ? fileId : JSON.stringify(fileId);
|
|
9272
|
+
return `[Image ID: ${displayId}]`;
|
|
9273
|
+
}
|
|
9274
|
+
case "file-data": {
|
|
9275
|
+
const filePart = contentPart;
|
|
9276
|
+
if (filePart.filename) {
|
|
9277
|
+
return `[File: ${filePart.filename} (${filePart.mediaType})]`;
|
|
9278
|
+
}
|
|
9279
|
+
return `[File: ${filePart.mediaType}]`;
|
|
9280
|
+
}
|
|
9281
|
+
case "file-url":
|
|
9282
|
+
return `[File URL: ${contentPart.url}]`;
|
|
9283
|
+
case "file-id": {
|
|
9284
|
+
const fileId = contentPart.fileId;
|
|
9285
|
+
const displayId = typeof fileId === "string" ? fileId : JSON.stringify(fileId);
|
|
9286
|
+
return `[File ID: ${displayId}]`;
|
|
9287
|
+
}
|
|
9288
|
+
case "media":
|
|
9289
|
+
return `[Media: ${contentPart.mediaType}]`;
|
|
9290
|
+
case "custom":
|
|
9291
|
+
return "[Custom content]";
|
|
9292
|
+
default:
|
|
9293
|
+
return "[Unknown content]";
|
|
9294
|
+
}
|
|
9295
|
+
}
|
|
9296
|
+
function unwrapToolResult(result, mediaStrategy) {
|
|
7330
9297
|
var _a, _b;
|
|
7331
9298
|
switch (result.type) {
|
|
7332
9299
|
case "text":
|
|
@@ -7339,46 +9306,14 @@ function unwrapToolResult(result) {
|
|
|
7339
9306
|
}
|
|
7340
9307
|
case "error-text":
|
|
7341
9308
|
return `[Error: ${(_b = result.value) != null ? _b : ""}]`;
|
|
7342
|
-
case "error-json":
|
|
7343
|
-
return `[Error: ${JSON.stringify(result.value)}]`;
|
|
7344
|
-
case "content": {
|
|
7345
|
-
|
|
7346
|
-
|
|
7347
|
-
|
|
7348
|
-
|
|
7349
|
-
|
|
7350
|
-
return (_a2 = contentPart.text) != null ? _a2 : "";
|
|
7351
|
-
case "image-data":
|
|
7352
|
-
return `[Image: ${contentPart.mediaType}]`;
|
|
7353
|
-
case "image-url":
|
|
7354
|
-
return `[Image URL: ${contentPart.url}]`;
|
|
7355
|
-
case "image-file-id": {
|
|
7356
|
-
const fileId = contentPart.fileId;
|
|
7357
|
-
const displayId = typeof fileId === "string" ? fileId : JSON.stringify(fileId);
|
|
7358
|
-
return `[Image ID: ${displayId}]`;
|
|
7359
|
-
}
|
|
7360
|
-
case "file-data": {
|
|
7361
|
-
const filePart = contentPart;
|
|
7362
|
-
if (filePart.filename) {
|
|
7363
|
-
return `[File: ${filePart.filename} (${filePart.mediaType})]`;
|
|
7364
|
-
}
|
|
7365
|
-
return `[File: ${filePart.mediaType}]`;
|
|
7366
|
-
}
|
|
7367
|
-
case "file-url":
|
|
7368
|
-
return `[File URL: ${contentPart.url}]`;
|
|
7369
|
-
case "file-id": {
|
|
7370
|
-
const fileId = contentPart.fileId;
|
|
7371
|
-
const displayId = typeof fileId === "string" ? fileId : JSON.stringify(fileId);
|
|
7372
|
-
return `[File ID: ${displayId}]`;
|
|
7373
|
-
}
|
|
7374
|
-
case "media":
|
|
7375
|
-
return `[Media: ${contentPart.mediaType}]`;
|
|
7376
|
-
case "custom":
|
|
7377
|
-
return "[Custom content]";
|
|
7378
|
-
default:
|
|
7379
|
-
return "[Unknown content]";
|
|
7380
|
-
}
|
|
7381
|
-
}).join("\n");
|
|
9309
|
+
case "error-json":
|
|
9310
|
+
return `[Error: ${JSON.stringify(result.value)}]`;
|
|
9311
|
+
case "content": {
|
|
9312
|
+
const parts = result.value;
|
|
9313
|
+
if (shouldPassRawContent(parts, mediaStrategy)) {
|
|
9314
|
+
return parts;
|
|
9315
|
+
}
|
|
9316
|
+
return parts.map(formatContentPartPlaceholder).join("\n");
|
|
7382
9317
|
}
|
|
7383
9318
|
default: {
|
|
7384
9319
|
const _exhaustive = result;
|
|
@@ -7386,58 +9321,89 @@ function unwrapToolResult(result) {
|
|
|
7386
9321
|
}
|
|
7387
9322
|
}
|
|
7388
9323
|
}
|
|
7389
|
-
|
|
7390
|
-
|
|
9324
|
+
|
|
9325
|
+
// src/core/prompts/hermes-prompt.ts
|
|
9326
|
+
function formatToolResponseAsHermesWithOptions(toolResult, options) {
|
|
9327
|
+
const unwrappedResult = unwrapToolResult(
|
|
9328
|
+
toolResult.output,
|
|
9329
|
+
options == null ? void 0 : options.mediaStrategy
|
|
9330
|
+
);
|
|
7391
9331
|
return `<tool_response>${JSON.stringify({
|
|
7392
|
-
|
|
7393
|
-
|
|
9332
|
+
name: toolResult.toolName,
|
|
9333
|
+
content: unwrappedResult
|
|
7394
9334
|
})}</tool_response>`;
|
|
7395
9335
|
}
|
|
7396
|
-
function
|
|
7397
|
-
|
|
7398
|
-
const toolNameXml = `<tool_name>${toolResult.toolName}</tool_name>`;
|
|
7399
|
-
const resultLines = formatXmlNode("result", unwrappedResult, 1);
|
|
7400
|
-
return [
|
|
7401
|
-
"<tool_response>",
|
|
7402
|
-
` ${toolNameXml}`,
|
|
7403
|
-
...resultLines,
|
|
7404
|
-
"</tool_response>"
|
|
7405
|
-
].join("\n");
|
|
9336
|
+
function formatToolResponseAsHermes(toolResult) {
|
|
9337
|
+
return formatToolResponseAsHermesWithOptions(toolResult);
|
|
7406
9338
|
}
|
|
7407
|
-
function
|
|
7408
|
-
const
|
|
7409
|
-
if (
|
|
7410
|
-
return
|
|
9339
|
+
function jsonSchemaToPythonType(schema) {
|
|
9340
|
+
const type = schema.type;
|
|
9341
|
+
if (type === "string") {
|
|
9342
|
+
return "str";
|
|
7411
9343
|
}
|
|
7412
|
-
if (
|
|
7413
|
-
return
|
|
9344
|
+
if (type === "number") {
|
|
9345
|
+
return "float";
|
|
7414
9346
|
}
|
|
7415
|
-
if (
|
|
7416
|
-
|
|
7417
|
-
|
|
9347
|
+
if (type === "integer") {
|
|
9348
|
+
return "int";
|
|
9349
|
+
}
|
|
9350
|
+
if (type === "boolean") {
|
|
9351
|
+
return "bool";
|
|
9352
|
+
}
|
|
9353
|
+
if (type === "array") {
|
|
9354
|
+
const items = schema.items;
|
|
9355
|
+
if (items) {
|
|
9356
|
+
return `list[${jsonSchemaToPythonType(items)}]`;
|
|
7418
9357
|
}
|
|
7419
|
-
|
|
7420
|
-
|
|
7421
|
-
|
|
9358
|
+
return "list[Any]";
|
|
9359
|
+
}
|
|
9360
|
+
if (type === "object") {
|
|
9361
|
+
const additionalProperties = schema.additionalProperties;
|
|
9362
|
+
if (additionalProperties) {
|
|
9363
|
+
return `dict[str, ${jsonSchemaToPythonType(additionalProperties)}]`;
|
|
7422
9364
|
}
|
|
7423
|
-
|
|
7424
|
-
return lines2;
|
|
9365
|
+
return "dict";
|
|
7425
9366
|
}
|
|
7426
|
-
|
|
7427
|
-
|
|
7428
|
-
return [`${indent}<${tagName}></${tagName}>`];
|
|
9367
|
+
if (Array.isArray(type)) {
|
|
9368
|
+
return `Union[${type.map((t) => jsonSchemaToPythonType({ type: t })).join(",")}]`;
|
|
7429
9369
|
}
|
|
7430
|
-
|
|
7431
|
-
|
|
7432
|
-
|
|
9370
|
+
return "Any";
|
|
9371
|
+
}
|
|
9372
|
+
function renderToolDefinition(tool) {
|
|
9373
|
+
var _a, _b;
|
|
9374
|
+
const schema = tool.inputSchema;
|
|
9375
|
+
const properties = schema.properties;
|
|
9376
|
+
const paramSignature = properties ? Object.entries(properties).map(([name, field]) => `${name}: ${jsonSchemaToPythonType(field)}`).join(", ") : "";
|
|
9377
|
+
const desc = (_a = tool.description) != null ? _a : "";
|
|
9378
|
+
let description = `${tool.name}(${paramSignature}) - ${desc}
|
|
9379
|
+
|
|
9380
|
+
`;
|
|
9381
|
+
if (properties && Object.keys(properties).length > 0) {
|
|
9382
|
+
description += " Args:\n";
|
|
9383
|
+
for (const [paramName, paramFields] of Object.entries(properties)) {
|
|
9384
|
+
const paramDesc = (_b = paramFields.description) != null ? _b : "";
|
|
9385
|
+
description += ` ${paramName}(${jsonSchemaToPythonType(paramFields)}): ${paramDesc.trim()}
|
|
9386
|
+
`;
|
|
9387
|
+
}
|
|
7433
9388
|
}
|
|
7434
|
-
|
|
7435
|
-
|
|
9389
|
+
const parametersJson = JSON.stringify(schema);
|
|
9390
|
+
const descJson = JSON.stringify(description);
|
|
9391
|
+
const nameJson = JSON.stringify(tool.name);
|
|
9392
|
+
return `{"type": "function", "function": {"name": ${nameJson}, "description": ${descJson}, "parameters": ${parametersJson}}}`;
|
|
9393
|
+
}
|
|
9394
|
+
function hermesSystemPromptTemplate(tools) {
|
|
9395
|
+
const toolsRendered = tools.map(renderToolDefinition).join("\n");
|
|
9396
|
+
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>
|
|
9397
|
+
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"}
|
|
9398
|
+
For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
|
|
9399
|
+
<tool_call>
|
|
9400
|
+
{"name": "<function-name>", "arguments": <args-dict>}
|
|
9401
|
+
</tool_call>`;
|
|
7436
9402
|
}
|
|
7437
9403
|
|
|
7438
|
-
// src/core/prompts/xml-
|
|
9404
|
+
// src/core/prompts/morph-xml-prompt.ts
|
|
7439
9405
|
var import_dedent = __toESM(require("dedent"), 1);
|
|
7440
|
-
function
|
|
9406
|
+
function morphXmlSystemPromptTemplate(tools) {
|
|
7441
9407
|
const toolsText = renderToolsForXmlPrompt(tools);
|
|
7442
9408
|
const header = import_dedent.default`
|
|
7443
9409
|
# Tools
|
|
@@ -7661,9 +9627,241 @@ function stripSchemaKeys(value) {
|
|
|
7661
9627
|
}
|
|
7662
9628
|
return value;
|
|
7663
9629
|
}
|
|
9630
|
+
function formatXmlNode(tagName, value, depth) {
|
|
9631
|
+
const indent = " ".repeat(depth);
|
|
9632
|
+
if (value === null || value === void 0) {
|
|
9633
|
+
return [`${indent}<${tagName}></${tagName}>`];
|
|
9634
|
+
}
|
|
9635
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
9636
|
+
return [`${indent}<${tagName}>${String(value)}</${tagName}>`];
|
|
9637
|
+
}
|
|
9638
|
+
if (Array.isArray(value)) {
|
|
9639
|
+
if (value.length === 0) {
|
|
9640
|
+
return [`${indent}<${tagName}></${tagName}>`];
|
|
9641
|
+
}
|
|
9642
|
+
const lines2 = [`${indent}<${tagName}>`];
|
|
9643
|
+
for (const item of value) {
|
|
9644
|
+
lines2.push(...formatXmlNode("item", item, depth + 1));
|
|
9645
|
+
}
|
|
9646
|
+
lines2.push(`${indent}</${tagName}>`);
|
|
9647
|
+
return lines2;
|
|
9648
|
+
}
|
|
9649
|
+
const entries = Object.entries(value);
|
|
9650
|
+
if (entries.length === 0) {
|
|
9651
|
+
return [`${indent}<${tagName}></${tagName}>`];
|
|
9652
|
+
}
|
|
9653
|
+
const lines = [`${indent}<${tagName}>`];
|
|
9654
|
+
for (const [key, entryValue] of entries) {
|
|
9655
|
+
lines.push(...formatXmlNode(key, entryValue, depth + 1));
|
|
9656
|
+
}
|
|
9657
|
+
lines.push(`${indent}</${tagName}>`);
|
|
9658
|
+
return lines;
|
|
9659
|
+
}
|
|
9660
|
+
function morphFormatToolResponseAsXmlWithOptions(toolResult, options) {
|
|
9661
|
+
const unwrappedResult = unwrapToolResult(
|
|
9662
|
+
toolResult.output,
|
|
9663
|
+
options == null ? void 0 : options.mediaStrategy
|
|
9664
|
+
);
|
|
9665
|
+
const toolNameXml = `<tool_name>${toolResult.toolName}</tool_name>`;
|
|
9666
|
+
const resultLines = formatXmlNode("result", unwrappedResult, 1);
|
|
9667
|
+
return [
|
|
9668
|
+
"<tool_response>",
|
|
9669
|
+
` ${toolNameXml}`,
|
|
9670
|
+
...resultLines,
|
|
9671
|
+
"</tool_response>"
|
|
9672
|
+
].join("\n");
|
|
9673
|
+
}
|
|
9674
|
+
function morphFormatToolResponseAsXml(toolResult) {
|
|
9675
|
+
return morphFormatToolResponseAsXmlWithOptions(toolResult);
|
|
9676
|
+
}
|
|
9677
|
+
|
|
9678
|
+
// src/core/prompts/shared/assistant-tool-calls-to-text.ts
|
|
9679
|
+
function assistantToolCallsToTextContent(options) {
|
|
9680
|
+
var _a, _b;
|
|
9681
|
+
const newContent = [];
|
|
9682
|
+
for (const item of options.content) {
|
|
9683
|
+
switch (item.type) {
|
|
9684
|
+
case "tool-call":
|
|
9685
|
+
newContent.push({
|
|
9686
|
+
type: "text",
|
|
9687
|
+
text: options.protocol.formatToolCall(item)
|
|
9688
|
+
});
|
|
9689
|
+
break;
|
|
9690
|
+
case "text":
|
|
9691
|
+
case "reasoning":
|
|
9692
|
+
newContent.push(item);
|
|
9693
|
+
break;
|
|
9694
|
+
default:
|
|
9695
|
+
(_b = (_a = options.conversionOptions) == null ? void 0 : _a.onError) == null ? void 0 : _b.call(
|
|
9696
|
+
_a,
|
|
9697
|
+
"tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
|
|
9698
|
+
{ content: item }
|
|
9699
|
+
);
|
|
9700
|
+
newContent.push({
|
|
9701
|
+
type: "text",
|
|
9702
|
+
text: JSON.stringify(item)
|
|
9703
|
+
});
|
|
9704
|
+
}
|
|
9705
|
+
}
|
|
9706
|
+
if (!newContent.every((entry) => entry.type === "text")) {
|
|
9707
|
+
return newContent;
|
|
9708
|
+
}
|
|
9709
|
+
return [
|
|
9710
|
+
{
|
|
9711
|
+
type: "text",
|
|
9712
|
+
text: newContent.map((entry) => entry.text).join("\n")
|
|
9713
|
+
}
|
|
9714
|
+
];
|
|
9715
|
+
}
|
|
9716
|
+
|
|
9717
|
+
// src/core/prompts/qwen3coder-prompt.ts
|
|
9718
|
+
var QWEN3CODER_TOOL_HEADER = "# Tools\n\nYou have access to the following functions:\n\n";
|
|
9719
|
+
var QWEN3CODER_TOOL_CALL_INSTRUCTIONS = "\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>";
|
|
9720
|
+
var XML_PROMPT_TAG_NAME_RE = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
|
|
9721
|
+
function isMapping2(value) {
|
|
9722
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
9723
|
+
}
|
|
9724
|
+
function isSequence(value) {
|
|
9725
|
+
return Array.isArray(value);
|
|
9726
|
+
}
|
|
9727
|
+
function toJinjaString(value) {
|
|
9728
|
+
if (value === void 0) {
|
|
9729
|
+
return "";
|
|
9730
|
+
}
|
|
9731
|
+
if (value === null) {
|
|
9732
|
+
return "None";
|
|
9733
|
+
}
|
|
9734
|
+
if (typeof value === "boolean") {
|
|
9735
|
+
return value ? "True" : "False";
|
|
9736
|
+
}
|
|
9737
|
+
return String(value);
|
|
9738
|
+
}
|
|
9739
|
+
function toJinjaTrimmedString(value) {
|
|
9740
|
+
return toJinjaString(value).trim();
|
|
9741
|
+
}
|
|
9742
|
+
function toEscapedXmlText(value) {
|
|
9743
|
+
return escapeXmlMinimalText(toJinjaString(value));
|
|
9744
|
+
}
|
|
9745
|
+
function toEscapedTrimmedXmlText(value) {
|
|
9746
|
+
return escapeXmlMinimalText(toJinjaTrimmedString(value));
|
|
9747
|
+
}
|
|
9748
|
+
function renderXmlPromptField(key, escapedValue) {
|
|
9749
|
+
if (XML_PROMPT_TAG_NAME_RE.test(key)) {
|
|
9750
|
+
return `
|
|
9751
|
+
<${key}>${escapedValue}</${key}>`;
|
|
9752
|
+
}
|
|
9753
|
+
return `
|
|
9754
|
+
<property name="${escapeXmlMinimalAttr(
|
|
9755
|
+
key,
|
|
9756
|
+
'"'
|
|
9757
|
+
)}">${escapedValue}</property>`;
|
|
9758
|
+
}
|
|
9759
|
+
function renderExtraKeys(jsonDict, handledKeys) {
|
|
9760
|
+
if (!isMapping2(jsonDict)) {
|
|
9761
|
+
return "";
|
|
9762
|
+
}
|
|
9763
|
+
const handled = new Set(handledKeys);
|
|
9764
|
+
let out = "";
|
|
9765
|
+
for (const [jsonKey, jsonValue] of Object.entries(jsonDict)) {
|
|
9766
|
+
if (handled.has(jsonKey)) {
|
|
9767
|
+
continue;
|
|
9768
|
+
}
|
|
9769
|
+
const renderedValue = isMapping2(jsonValue) || isSequence(jsonValue) ? JSON.stringify(jsonValue) : toJinjaString(jsonValue);
|
|
9770
|
+
out += renderXmlPromptField(jsonKey, escapeXmlMinimalText(renderedValue));
|
|
9771
|
+
}
|
|
9772
|
+
return out;
|
|
9773
|
+
}
|
|
9774
|
+
function normalizeInputSchema(inputSchema) {
|
|
9775
|
+
if (typeof inputSchema !== "string") {
|
|
9776
|
+
return inputSchema;
|
|
9777
|
+
}
|
|
9778
|
+
try {
|
|
9779
|
+
return JSON.parse(inputSchema);
|
|
9780
|
+
} catch (e) {
|
|
9781
|
+
return inputSchema;
|
|
9782
|
+
}
|
|
9783
|
+
}
|
|
9784
|
+
function normalizeTool(rawTool) {
|
|
9785
|
+
return {
|
|
9786
|
+
name: rawTool.name,
|
|
9787
|
+
description: rawTool.description,
|
|
9788
|
+
parameters: normalizeInputSchema(rawTool.inputSchema)
|
|
9789
|
+
};
|
|
9790
|
+
}
|
|
9791
|
+
function renderParameter(paramName, paramFieldsRaw) {
|
|
9792
|
+
const paramFields = isMapping2(paramFieldsRaw) ? paramFieldsRaw : void 0;
|
|
9793
|
+
let out = "\n<parameter>";
|
|
9794
|
+
out += `
|
|
9795
|
+
<name>${toEscapedXmlText(paramName)}</name>`;
|
|
9796
|
+
if ((paramFields == null ? void 0 : paramFields.type) !== void 0) {
|
|
9797
|
+
out += `
|
|
9798
|
+
<type>${toEscapedXmlText(paramFields.type)}</type>`;
|
|
9799
|
+
}
|
|
9800
|
+
if ((paramFields == null ? void 0 : paramFields.description) !== void 0) {
|
|
9801
|
+
out += `
|
|
9802
|
+
<description>${toEscapedTrimmedXmlText(paramFields.description)}</description>`;
|
|
9803
|
+
}
|
|
9804
|
+
out += renderExtraKeys(paramFieldsRaw, ["name", "type", "description"]);
|
|
9805
|
+
out += "\n</parameter>";
|
|
9806
|
+
return out;
|
|
9807
|
+
}
|
|
9808
|
+
function renderTool(tool) {
|
|
9809
|
+
let out = `
|
|
9810
|
+
<function>
|
|
9811
|
+
<name>${toEscapedXmlText(tool.name)}</name>`;
|
|
9812
|
+
if (tool.description !== void 0) {
|
|
9813
|
+
out += `
|
|
9814
|
+
<description>${toEscapedTrimmedXmlText(tool.description)}</description>`;
|
|
9815
|
+
}
|
|
9816
|
+
out += "\n<parameters>";
|
|
9817
|
+
const parameters = tool.parameters;
|
|
9818
|
+
if (isMapping2(parameters) && isMapping2(parameters.properties)) {
|
|
9819
|
+
for (const [paramName, paramFieldsRaw] of Object.entries(
|
|
9820
|
+
parameters.properties
|
|
9821
|
+
)) {
|
|
9822
|
+
out += renderParameter(paramName, paramFieldsRaw);
|
|
9823
|
+
}
|
|
9824
|
+
}
|
|
9825
|
+
out += renderExtraKeys(parameters, ["type", "properties"]);
|
|
9826
|
+
out += "\n</parameters>";
|
|
9827
|
+
out += renderExtraKeys(tool, ["type", "name", "description", "parameters"]);
|
|
9828
|
+
out += "\n</function>";
|
|
9829
|
+
return out;
|
|
9830
|
+
}
|
|
9831
|
+
function qwen3coderSystemPromptTemplate(tools) {
|
|
9832
|
+
if (!tools.length) {
|
|
9833
|
+
return "";
|
|
9834
|
+
}
|
|
9835
|
+
let out = `${QWEN3CODER_TOOL_HEADER}<tools>`;
|
|
9836
|
+
for (const tool of tools) {
|
|
9837
|
+
out += renderTool(normalizeTool(tool));
|
|
9838
|
+
}
|
|
9839
|
+
out += "\n</tools>";
|
|
9840
|
+
out += QWEN3CODER_TOOL_CALL_INSTRUCTIONS;
|
|
9841
|
+
return out;
|
|
9842
|
+
}
|
|
9843
|
+
function stringifyToolResponseContent(value) {
|
|
9844
|
+
if (typeof value === "string") {
|
|
9845
|
+
return value;
|
|
9846
|
+
}
|
|
9847
|
+
return JSON.stringify(value);
|
|
9848
|
+
}
|
|
9849
|
+
function formatToolResponseAsQwen3CoderXmlWithOptions(toolResult, options) {
|
|
9850
|
+
const unwrappedResult = unwrapToolResult(
|
|
9851
|
+
toolResult.output,
|
|
9852
|
+
options == null ? void 0 : options.mediaStrategy
|
|
9853
|
+
);
|
|
9854
|
+
const content = stringifyToolResponseContent(unwrappedResult);
|
|
9855
|
+
return `<tool_response>
|
|
9856
|
+
${content}
|
|
9857
|
+
</tool_response>`;
|
|
9858
|
+
}
|
|
9859
|
+
function formatToolResponseAsQwen3CoderXml(toolResult) {
|
|
9860
|
+
return formatToolResponseAsQwen3CoderXmlWithOptions(toolResult);
|
|
9861
|
+
}
|
|
7664
9862
|
|
|
7665
|
-
// src/core/prompts/yaml-
|
|
7666
|
-
function
|
|
9863
|
+
// src/core/prompts/yaml-xml-prompt.ts
|
|
9864
|
+
function yamlXmlSystemPromptTemplate(tools, includeMultilineExample = true) {
|
|
7667
9865
|
const toolsJson = JSON.stringify(tools);
|
|
7668
9866
|
const multilineExample = includeMultilineExample ? `
|
|
7669
9867
|
|
|
@@ -7702,6 +9900,9 @@ unit: celsius
|
|
|
7702
9900
|
- Do NOT ask clarifying questions. Use reasonable defaults for optional parameters.
|
|
7703
9901
|
- If a task requires multiple function calls, make ALL of them at once.`;
|
|
7704
9902
|
}
|
|
9903
|
+
function formatToolResponseAsYaml(toolResult) {
|
|
9904
|
+
return morphFormatToolResponseAsXml(toolResult);
|
|
9905
|
+
}
|
|
7705
9906
|
|
|
7706
9907
|
// src/stream-handler.ts
|
|
7707
9908
|
async function wrapStream({
|
|
@@ -7866,8 +10067,75 @@ function normalizeUsage(usage) {
|
|
|
7866
10067
|
return ZERO_USAGE;
|
|
7867
10068
|
}
|
|
7868
10069
|
|
|
10070
|
+
// src/core/prompts/shared/tool-role-to-user-message.ts
|
|
10071
|
+
function formatApprovalResponse(part) {
|
|
10072
|
+
const status = part.approved ? "Approved" : "Denied";
|
|
10073
|
+
const reason = part.reason ? `: ${part.reason}` : "";
|
|
10074
|
+
return `[Tool Approval ${status}${reason}]`;
|
|
10075
|
+
}
|
|
10076
|
+
function toTextPart(text) {
|
|
10077
|
+
return {
|
|
10078
|
+
type: "text",
|
|
10079
|
+
text
|
|
10080
|
+
};
|
|
10081
|
+
}
|
|
10082
|
+
function normalizeTemplateResult(result) {
|
|
10083
|
+
if (typeof result === "string") {
|
|
10084
|
+
return [toTextPart(result)];
|
|
10085
|
+
}
|
|
10086
|
+
return result;
|
|
10087
|
+
}
|
|
10088
|
+
function appendSection(target, section) {
|
|
10089
|
+
if (section.length === 0) {
|
|
10090
|
+
return;
|
|
10091
|
+
}
|
|
10092
|
+
if (target.length > 0) {
|
|
10093
|
+
target.push(toTextPart("\n"));
|
|
10094
|
+
}
|
|
10095
|
+
target.push(...section);
|
|
10096
|
+
}
|
|
10097
|
+
function mergeAdjacentTextParts(parts) {
|
|
10098
|
+
const merged = [];
|
|
10099
|
+
for (const part of parts) {
|
|
10100
|
+
const last = merged.at(-1);
|
|
10101
|
+
const canMergeTextParts = part.type === "text" && (last == null ? void 0 : last.type) === "text" && part.providerOptions === void 0 && last.providerOptions === void 0;
|
|
10102
|
+
if (canMergeTextParts) {
|
|
10103
|
+
last.text += part.text;
|
|
10104
|
+
continue;
|
|
10105
|
+
}
|
|
10106
|
+
merged.push(part);
|
|
10107
|
+
}
|
|
10108
|
+
return merged;
|
|
10109
|
+
}
|
|
10110
|
+
function toolRoleContentToUserTextMessage(options) {
|
|
10111
|
+
const toolResultParts = options.toolContent.filter(
|
|
10112
|
+
(part) => part.type === "tool-result"
|
|
10113
|
+
);
|
|
10114
|
+
const approvalResponseParts = options.toolContent.filter(
|
|
10115
|
+
(part) => part.type === "tool-approval-response"
|
|
10116
|
+
);
|
|
10117
|
+
const sections = [];
|
|
10118
|
+
for (const toolResult of toolResultParts) {
|
|
10119
|
+
const result = options.toolResponsePromptTemplate(toolResult);
|
|
10120
|
+
appendSection(sections, normalizeTemplateResult(result));
|
|
10121
|
+
}
|
|
10122
|
+
for (const approvalResponse of approvalResponseParts) {
|
|
10123
|
+
appendSection(sections, [
|
|
10124
|
+
toTextPart(formatApprovalResponse(approvalResponse))
|
|
10125
|
+
]);
|
|
10126
|
+
}
|
|
10127
|
+
const normalizedSections = sections.length > 0 ? sections : [toTextPart("")];
|
|
10128
|
+
return {
|
|
10129
|
+
role: "user",
|
|
10130
|
+
content: mergeAdjacentTextParts(normalizedSections)
|
|
10131
|
+
};
|
|
10132
|
+
}
|
|
10133
|
+
|
|
7869
10134
|
// src/transform-handler.ts
|
|
7870
10135
|
function buildFinalPrompt(systemPrompt, processedPrompt, placement) {
|
|
10136
|
+
if (systemPrompt.trim().length === 0) {
|
|
10137
|
+
return processedPrompt;
|
|
10138
|
+
}
|
|
7871
10139
|
const systemIndex = processedPrompt.findIndex((m) => m.role === "system");
|
|
7872
10140
|
if (systemIndex !== -1) {
|
|
7873
10141
|
const existing = processedPrompt[systemIndex].content;
|
|
@@ -8063,94 +10331,30 @@ function transformParams({
|
|
|
8063
10331
|
}
|
|
8064
10332
|
return baseReturnParams;
|
|
8065
10333
|
}
|
|
8066
|
-
function processAssistantContent(content, resolvedProtocol, providerOptions) {
|
|
8067
|
-
var _a;
|
|
8068
|
-
const newContent = [];
|
|
8069
|
-
for (const item of content) {
|
|
8070
|
-
switch (item.type) {
|
|
8071
|
-
case "tool-call":
|
|
8072
|
-
newContent.push({
|
|
8073
|
-
type: "text",
|
|
8074
|
-
text: resolvedProtocol.formatToolCall(item)
|
|
8075
|
-
});
|
|
8076
|
-
break;
|
|
8077
|
-
case "text":
|
|
8078
|
-
case "reasoning":
|
|
8079
|
-
newContent.push(item);
|
|
8080
|
-
break;
|
|
8081
|
-
default: {
|
|
8082
|
-
const options = extractOnErrorOption(providerOptions);
|
|
8083
|
-
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
8084
|
-
options,
|
|
8085
|
-
"tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
|
|
8086
|
-
{ content: item }
|
|
8087
|
-
);
|
|
8088
|
-
newContent.push({
|
|
8089
|
-
type: "text",
|
|
8090
|
-
text: JSON.stringify(item)
|
|
8091
|
-
});
|
|
8092
|
-
}
|
|
8093
|
-
}
|
|
8094
|
-
}
|
|
8095
|
-
const onlyText = newContent.every((c) => c.type === "text");
|
|
8096
|
-
return onlyText ? [
|
|
8097
|
-
{
|
|
8098
|
-
type: "text",
|
|
8099
|
-
text: newContent.map((c) => c.text).join("\n")
|
|
8100
|
-
}
|
|
8101
|
-
] : newContent;
|
|
8102
|
-
}
|
|
8103
|
-
function formatApprovalResponse(part) {
|
|
8104
|
-
const status = part.approved ? "Approved" : "Denied";
|
|
8105
|
-
const reason = part.reason ? `: ${part.reason}` : "";
|
|
8106
|
-
return `[Tool Approval ${status}${reason}]`;
|
|
8107
|
-
}
|
|
8108
|
-
function processToolMessage(toolResults, approvalResponses, toolResponsePromptTemplate) {
|
|
8109
|
-
const resultTexts = toolResults.map((toolResult) => {
|
|
8110
|
-
return toolResponsePromptTemplate(toolResult);
|
|
8111
|
-
});
|
|
8112
|
-
const approvalTexts = approvalResponses.map(formatApprovalResponse);
|
|
8113
|
-
const allTexts = [...resultTexts, ...approvalTexts];
|
|
8114
|
-
return {
|
|
8115
|
-
role: "user",
|
|
8116
|
-
content: [
|
|
8117
|
-
{
|
|
8118
|
-
type: "text",
|
|
8119
|
-
text: allTexts.join("\n")
|
|
8120
|
-
}
|
|
8121
|
-
]
|
|
8122
|
-
};
|
|
8123
|
-
}
|
|
8124
10334
|
function processMessage(message, resolvedProtocol, providerOptions, toolResponsePromptTemplate) {
|
|
8125
10335
|
if (message.role === "assistant") {
|
|
8126
|
-
const condensedContent =
|
|
8127
|
-
message.content,
|
|
8128
|
-
resolvedProtocol,
|
|
8129
|
-
|
|
8130
|
-
|
|
10336
|
+
const condensedContent = assistantToolCallsToTextContent({
|
|
10337
|
+
content: message.content,
|
|
10338
|
+
protocol: resolvedProtocol,
|
|
10339
|
+
conversionOptions: {
|
|
10340
|
+
onError: providerOptions == null ? void 0 : providerOptions.onError
|
|
10341
|
+
}
|
|
10342
|
+
});
|
|
8131
10343
|
return {
|
|
8132
10344
|
role: "assistant",
|
|
8133
10345
|
content: condensedContent
|
|
8134
10346
|
};
|
|
8135
10347
|
}
|
|
8136
10348
|
if (message.role === "tool") {
|
|
8137
|
-
const toolContent = message.content;
|
|
8138
|
-
const toolResultParts = toolContent.filter(
|
|
8139
|
-
(part) => part.type === "tool-result"
|
|
8140
|
-
);
|
|
8141
|
-
const approvalResponseParts = toolContent.filter(
|
|
8142
|
-
(part) => part.type === "tool-approval-response"
|
|
8143
|
-
);
|
|
8144
10349
|
if (!toolResponsePromptTemplate) {
|
|
8145
10350
|
throw new Error(
|
|
8146
10351
|
'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.'
|
|
8147
10352
|
);
|
|
8148
10353
|
}
|
|
8149
|
-
return
|
|
8150
|
-
|
|
8151
|
-
approvalResponseParts,
|
|
10354
|
+
return toolRoleContentToUserTextMessage({
|
|
10355
|
+
toolContent: message.content,
|
|
8152
10356
|
toolResponsePromptTemplate
|
|
8153
|
-
);
|
|
10357
|
+
});
|
|
8154
10358
|
}
|
|
8155
10359
|
return message;
|
|
8156
10360
|
}
|
|
@@ -8201,6 +10405,9 @@ function mergeConsecutiveUserMessages(processedPrompt) {
|
|
|
8201
10405
|
const current = processedPrompt[i];
|
|
8202
10406
|
const prev = processedPrompt[i - 1];
|
|
8203
10407
|
if (current.role === "user" && prev.role === "user") {
|
|
10408
|
+
if (!(isAllTextContent(prev.content) && isAllTextContent(current.content))) {
|
|
10409
|
+
continue;
|
|
10410
|
+
}
|
|
8204
10411
|
const prevContent = prev.content.map((c) => c.type === "text" ? c.text : "").join("\n");
|
|
8205
10412
|
const currentContent = current.content.map((c) => c.type === "text" ? c.text : "").join("\n");
|
|
8206
10413
|
processedPrompt[i - 1] = {
|
|
@@ -8260,22 +10467,28 @@ function createToolMiddleware({
|
|
|
8260
10467
|
|
|
8261
10468
|
// src/preconfigured-middleware.ts
|
|
8262
10469
|
var hermesToolMiddleware = createToolMiddleware({
|
|
8263
|
-
protocol:
|
|
10470
|
+
protocol: hermesProtocol(),
|
|
8264
10471
|
toolSystemPromptTemplate: hermesSystemPromptTemplate,
|
|
8265
|
-
toolResponsePromptTemplate:
|
|
10472
|
+
toolResponsePromptTemplate: formatToolResponseAsHermes
|
|
10473
|
+
});
|
|
10474
|
+
var qwen3CoderToolMiddleware = createToolMiddleware({
|
|
10475
|
+
protocol: qwen3CoderProtocol,
|
|
10476
|
+
toolSystemPromptTemplate: qwen3coderSystemPromptTemplate,
|
|
10477
|
+
toolResponsePromptTemplate: formatToolResponseAsQwen3CoderXml
|
|
8266
10478
|
});
|
|
8267
|
-
var
|
|
8268
|
-
protocol:
|
|
8269
|
-
toolSystemPromptTemplate:
|
|
8270
|
-
toolResponsePromptTemplate:
|
|
10479
|
+
var morphXmlToolMiddleware = createToolMiddleware({
|
|
10480
|
+
protocol: morphXmlProtocol({}),
|
|
10481
|
+
toolSystemPromptTemplate: morphXmlSystemPromptTemplate,
|
|
10482
|
+
toolResponsePromptTemplate: morphFormatToolResponseAsXml
|
|
8271
10483
|
});
|
|
8272
|
-
var
|
|
8273
|
-
protocol:
|
|
8274
|
-
toolSystemPromptTemplate:
|
|
8275
|
-
toolResponsePromptTemplate:
|
|
10484
|
+
var yamlXmlToolMiddleware = createToolMiddleware({
|
|
10485
|
+
protocol: yamlXmlProtocol({}),
|
|
10486
|
+
toolSystemPromptTemplate: yamlXmlSystemPromptTemplate,
|
|
10487
|
+
toolResponsePromptTemplate: formatToolResponseAsYaml
|
|
8276
10488
|
});
|
|
8277
10489
|
// Annotate the CommonJS export names for ESM import in node:
|
|
8278
10490
|
0 && (module.exports = {
|
|
10491
|
+
Qwen3CoderToolParser,
|
|
8279
10492
|
createDynamicIfThenElseSchema,
|
|
8280
10493
|
createToolMiddleware,
|
|
8281
10494
|
decodeOriginalTools,
|
|
@@ -8286,27 +10499,30 @@ var yamlToolMiddleware = createToolMiddleware({
|
|
|
8286
10499
|
getDebugLevel,
|
|
8287
10500
|
getPotentialStartIndex,
|
|
8288
10501
|
hasInputProperty,
|
|
10502
|
+
hermesProtocol,
|
|
8289
10503
|
hermesToolMiddleware,
|
|
8290
10504
|
isProtocolFactory,
|
|
8291
10505
|
isTCMProtocolFactory,
|
|
8292
10506
|
isToolChoiceActive,
|
|
8293
10507
|
isToolResultPart,
|
|
8294
|
-
jsonProtocol,
|
|
8295
10508
|
logParseFailure,
|
|
8296
10509
|
logParsedChunk,
|
|
8297
10510
|
logParsedSummary,
|
|
8298
10511
|
logRawChunk,
|
|
10512
|
+
morphXmlProtocol,
|
|
10513
|
+
morphXmlToolMiddleware,
|
|
8299
10514
|
originalToolsSchema,
|
|
8300
10515
|
parse,
|
|
10516
|
+
qwen3CoderProtocol,
|
|
10517
|
+
qwen3CoderToolMiddleware,
|
|
8301
10518
|
stringify,
|
|
8302
10519
|
toolChoiceStream,
|
|
8303
10520
|
transform,
|
|
8304
10521
|
transformParams,
|
|
10522
|
+
uiTarsXmlProtocol,
|
|
8305
10523
|
wrapGenerate,
|
|
8306
10524
|
wrapStream,
|
|
8307
|
-
|
|
8308
|
-
|
|
8309
|
-
yamlProtocol,
|
|
8310
|
-
yamlToolMiddleware
|
|
10525
|
+
yamlXmlProtocol,
|
|
10526
|
+
yamlXmlToolMiddleware
|
|
8311
10527
|
});
|
|
8312
10528
|
//# sourceMappingURL=index.cjs.map
|