@ai-sdk-tool/parser 2.1.6 → 2.1.7
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 +2 -2
- package/dist/chunk-GSD5HDOQ.js +2068 -0
- package/dist/chunk-GSD5HDOQ.js.map +1 -0
- package/dist/community.cjs +2164 -0
- package/dist/community.cjs.map +1 -0
- package/dist/community.d.cts +20 -0
- package/dist/community.d.ts +20 -0
- package/dist/community.js +98 -0
- package/dist/community.js.map +1 -0
- package/dist/index.cjs +23 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +27 -2038
- package/dist/index.js.map +1 -1
- package/package.json +13 -1
|
@@ -0,0 +1,2068 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/protocols/dummy-protocol.ts
|
|
8
|
+
import { generateId } from "@ai-sdk/provider-utils";
|
|
9
|
+
|
|
10
|
+
// src/protocols/json-mix-protocol.ts
|
|
11
|
+
import { generateId as generateId2 } from "@ai-sdk/provider-utils";
|
|
12
|
+
|
|
13
|
+
// src/utils/dynamic-tool-schema.ts
|
|
14
|
+
function createDynamicIfThenElseSchema(tools) {
|
|
15
|
+
let currentSchema = {};
|
|
16
|
+
const toolNames = [];
|
|
17
|
+
for (let i = tools.length - 1; i >= 0; i--) {
|
|
18
|
+
const tool = tools[i];
|
|
19
|
+
if (tool.type === "provider-defined") {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"Provider-defined tools are not supported by this middleware. Please use custom tools."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
toolNames.unshift(tool.name);
|
|
25
|
+
const toolCondition = {
|
|
26
|
+
if: {
|
|
27
|
+
properties: {
|
|
28
|
+
name: {
|
|
29
|
+
const: tool.name
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
required: ["name"]
|
|
33
|
+
},
|
|
34
|
+
then: {
|
|
35
|
+
properties: {
|
|
36
|
+
name: {
|
|
37
|
+
const: tool.name
|
|
38
|
+
},
|
|
39
|
+
arguments: tool.inputSchema
|
|
40
|
+
},
|
|
41
|
+
required: ["name", "arguments"]
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
if (Object.keys(currentSchema).length > 0) {
|
|
45
|
+
toolCondition.else = currentSchema;
|
|
46
|
+
}
|
|
47
|
+
currentSchema = toolCondition;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
type: "object",
|
|
51
|
+
// Explicitly specify type as "object"
|
|
52
|
+
properties: {
|
|
53
|
+
name: {
|
|
54
|
+
type: "string",
|
|
55
|
+
description: "Name of the tool to call",
|
|
56
|
+
enum: toolNames
|
|
57
|
+
},
|
|
58
|
+
arguments: {
|
|
59
|
+
type: "object",
|
|
60
|
+
// By default, arguments is also specified as object type
|
|
61
|
+
description: "Argument object to be passed to the tool"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
required: ["name", "arguments"],
|
|
65
|
+
...currentSchema
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/utils/get-potential-start-index.ts
|
|
70
|
+
function getPotentialStartIndex(text, searchedText) {
|
|
71
|
+
if (searchedText.length === 0) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const directIndex = text.indexOf(searchedText);
|
|
75
|
+
if (directIndex !== -1) {
|
|
76
|
+
return directIndex;
|
|
77
|
+
}
|
|
78
|
+
for (let i = text.length - 1; i >= 0; i--) {
|
|
79
|
+
const suffix = text.substring(i);
|
|
80
|
+
if (searchedText.startsWith(suffix)) {
|
|
81
|
+
return i;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/utils/regex.ts
|
|
88
|
+
function escapeRegExp(literal) {
|
|
89
|
+
return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/utils/robust-json.ts
|
|
93
|
+
var robust_json_exports = {};
|
|
94
|
+
__export(robust_json_exports, {
|
|
95
|
+
parse: () => parse,
|
|
96
|
+
stringify: () => stringify,
|
|
97
|
+
transform: () => transform
|
|
98
|
+
});
|
|
99
|
+
function some(array, f) {
|
|
100
|
+
let acc = false;
|
|
101
|
+
for (let i = 0; i < array.length; i++) {
|
|
102
|
+
const result = f(array[i], i, array);
|
|
103
|
+
acc = result === void 0 ? false : result;
|
|
104
|
+
if (acc) {
|
|
105
|
+
return acc;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return acc;
|
|
109
|
+
}
|
|
110
|
+
function makeLexer(tokenSpecs) {
|
|
111
|
+
return function(contents) {
|
|
112
|
+
const tokens = [];
|
|
113
|
+
let line = 1;
|
|
114
|
+
function findToken() {
|
|
115
|
+
const result = some(tokenSpecs, (tokenSpec) => {
|
|
116
|
+
const m = tokenSpec.re.exec(contents);
|
|
117
|
+
if (m) {
|
|
118
|
+
const raw = m[0];
|
|
119
|
+
contents = contents.slice(raw.length);
|
|
120
|
+
return {
|
|
121
|
+
raw,
|
|
122
|
+
matched: tokenSpec.f(m)
|
|
123
|
+
// Process the match using the spec's function
|
|
124
|
+
};
|
|
125
|
+
} else {
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return result === false ? void 0 : result;
|
|
130
|
+
}
|
|
131
|
+
while (contents !== "") {
|
|
132
|
+
const matched = findToken();
|
|
133
|
+
if (!matched) {
|
|
134
|
+
const err = new SyntaxError(
|
|
135
|
+
`Unexpected character: ${contents[0]}; input: ${contents.substr(
|
|
136
|
+
0,
|
|
137
|
+
100
|
|
138
|
+
)}`
|
|
139
|
+
);
|
|
140
|
+
err.line = line;
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
const tokenWithLine = matched.matched;
|
|
144
|
+
tokenWithLine.line = line;
|
|
145
|
+
line += matched.raw.replace(/[^\n]/g, "").length;
|
|
146
|
+
tokens.push(tokenWithLine);
|
|
147
|
+
}
|
|
148
|
+
return tokens;
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function fStringSingle(m) {
|
|
152
|
+
const content = m[1].replace(
|
|
153
|
+
/([^'\\]|\\['bnrtf\\]|\\u[0-9a-fA-F]{4})/g,
|
|
154
|
+
(mm) => {
|
|
155
|
+
if (mm === '"') {
|
|
156
|
+
return '\\"';
|
|
157
|
+
} else if (mm === "\\'") {
|
|
158
|
+
return "'";
|
|
159
|
+
} else {
|
|
160
|
+
return mm;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
const match = `"${content}"`;
|
|
165
|
+
return {
|
|
166
|
+
type: "string",
|
|
167
|
+
match,
|
|
168
|
+
// The transformed, double-quoted string representation
|
|
169
|
+
// Use JSON.parse on the transformed string to handle escape sequences correctly
|
|
170
|
+
value: JSON.parse(match)
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function fStringDouble(m) {
|
|
174
|
+
return {
|
|
175
|
+
type: "string",
|
|
176
|
+
match: m[0],
|
|
177
|
+
// The raw matched string (including quotes)
|
|
178
|
+
value: JSON.parse(m[0])
|
|
179
|
+
// Use JSON.parse to handle escapes and get the value
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function fIdentifier(m) {
|
|
183
|
+
const value = m[0];
|
|
184
|
+
const match = '"' + value.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + // Escape backslashes and quotes
|
|
185
|
+
'"';
|
|
186
|
+
return {
|
|
187
|
+
type: "string",
|
|
188
|
+
// Treat identifiers as strings
|
|
189
|
+
value,
|
|
190
|
+
// The original identifier name
|
|
191
|
+
match
|
|
192
|
+
// The double-quoted string representation
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function fComment(m) {
|
|
196
|
+
const match = m[0].replace(/./g, (c) => /\s/.test(c) ? c : " ");
|
|
197
|
+
return {
|
|
198
|
+
type: " ",
|
|
199
|
+
// Represent comments as whitespace tokens
|
|
200
|
+
match,
|
|
201
|
+
// String containing original newlines and spaces for other chars
|
|
202
|
+
value: void 0
|
|
203
|
+
// Comments don't have a semantic value
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function fNumber(m) {
|
|
207
|
+
return {
|
|
208
|
+
type: "number",
|
|
209
|
+
match: m[0],
|
|
210
|
+
// The raw matched number string
|
|
211
|
+
value: parseFloat(m[0])
|
|
212
|
+
// Convert string to number
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function fKeyword(m) {
|
|
216
|
+
let value;
|
|
217
|
+
switch (m[0]) {
|
|
218
|
+
case "null":
|
|
219
|
+
value = null;
|
|
220
|
+
break;
|
|
221
|
+
case "true":
|
|
222
|
+
value = true;
|
|
223
|
+
break;
|
|
224
|
+
case "false":
|
|
225
|
+
value = false;
|
|
226
|
+
break;
|
|
227
|
+
default:
|
|
228
|
+
throw new Error(`Unexpected keyword: ${m[0]}`);
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
type: "atom",
|
|
232
|
+
// Use 'atom' type for these literals
|
|
233
|
+
match: m[0],
|
|
234
|
+
// The raw matched keyword
|
|
235
|
+
value
|
|
236
|
+
// The corresponding JavaScript value
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function makeTokenSpecs(relaxed) {
|
|
240
|
+
function f(type) {
|
|
241
|
+
return function(m) {
|
|
242
|
+
return { type, match: m[0], value: void 0 };
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
let tokenSpecs = [
|
|
246
|
+
{ re: /^\s+/, f: f(" ") },
|
|
247
|
+
// Whitespace
|
|
248
|
+
{ re: /^\{/, f: f("{") },
|
|
249
|
+
// Object start
|
|
250
|
+
{ re: /^\}/, f: f("}") },
|
|
251
|
+
// Object end
|
|
252
|
+
{ re: /^\[/, f: f("[") },
|
|
253
|
+
// Array start
|
|
254
|
+
{ re: /^\]/, f: f("]") },
|
|
255
|
+
// Array end
|
|
256
|
+
{ re: /^,/, f: f(",") },
|
|
257
|
+
// Comma separator
|
|
258
|
+
{ re: /^:/, f: f(":") },
|
|
259
|
+
// Key-value separator
|
|
260
|
+
{ re: /^(?:true|false|null)/, f: fKeyword },
|
|
261
|
+
// Keywords
|
|
262
|
+
// Number: optional sign, digits, optional decimal part, optional exponent
|
|
263
|
+
{ re: /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/, f: fNumber },
|
|
264
|
+
// String: double-quoted, handles escapes
|
|
265
|
+
{ re: /^"(?:[^"\\]|\\["bnrtf\\/]|\\u[0-9a-fA-F]{4})*"/, f: fStringDouble }
|
|
266
|
+
];
|
|
267
|
+
if (relaxed) {
|
|
268
|
+
tokenSpecs = tokenSpecs.concat([
|
|
269
|
+
// Single-quoted strings
|
|
270
|
+
{
|
|
271
|
+
re: /^'((?:[^'\\]|\\['bnrtf\\/]|\\u[0-9a-fA-F]{4})*)'/,
|
|
272
|
+
f: fStringSingle
|
|
273
|
+
},
|
|
274
|
+
// Single-line comments (// ...)
|
|
275
|
+
{ re: /^\/\/.*?(?:\r\n|\r|\n)/, f: fComment },
|
|
276
|
+
// Multi-line comments (/* ... */)
|
|
277
|
+
{ re: /^\/\*[\s\S]*?\*\//, f: fComment },
|
|
278
|
+
// Unquoted identifiers (treated as strings)
|
|
279
|
+
// Allows letters, numbers, _, -, +, ., *, ?, !, |, &, %, ^, /, #, \
|
|
280
|
+
{ re: /^[$a-zA-Z0-9_\-+.*?!|&%^/#\\]+/, f: fIdentifier }
|
|
281
|
+
// Note: The order matters here. Identifiers are checked after keywords/numbers.
|
|
282
|
+
]);
|
|
283
|
+
}
|
|
284
|
+
return tokenSpecs;
|
|
285
|
+
}
|
|
286
|
+
var lexer = makeLexer(makeTokenSpecs(true));
|
|
287
|
+
var strictLexer = makeLexer(makeTokenSpecs(false));
|
|
288
|
+
function previousNWSToken(tokens, index) {
|
|
289
|
+
for (; index >= 0; index--) {
|
|
290
|
+
if (tokens[index].type !== " ") {
|
|
291
|
+
return index;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return void 0;
|
|
295
|
+
}
|
|
296
|
+
function stripTrailingComma(tokens) {
|
|
297
|
+
const res = [];
|
|
298
|
+
tokens.forEach((token, index) => {
|
|
299
|
+
if (index > 0 && (token.type === "]" || token.type === "}")) {
|
|
300
|
+
const prevNWSTokenIndex = previousNWSToken(res, res.length - 1);
|
|
301
|
+
if (prevNWSTokenIndex !== void 0 && res[prevNWSTokenIndex].type === ",") {
|
|
302
|
+
const preCommaIndex = previousNWSToken(res, prevNWSTokenIndex - 1);
|
|
303
|
+
if (preCommaIndex !== void 0 && res[preCommaIndex].type !== "[" && res[preCommaIndex].type !== "{") {
|
|
304
|
+
res[prevNWSTokenIndex] = {
|
|
305
|
+
type: " ",
|
|
306
|
+
match: " ",
|
|
307
|
+
// Represent as a single space
|
|
308
|
+
value: void 0,
|
|
309
|
+
// Whitespace has no value
|
|
310
|
+
line: res[prevNWSTokenIndex].line
|
|
311
|
+
// Preserve original line number
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
res.push(token);
|
|
317
|
+
});
|
|
318
|
+
return res;
|
|
319
|
+
}
|
|
320
|
+
function transform(text) {
|
|
321
|
+
let tokens = lexer(text);
|
|
322
|
+
tokens = stripTrailingComma(tokens);
|
|
323
|
+
return tokens.reduce((str, token) => {
|
|
324
|
+
return str + token.match;
|
|
325
|
+
}, "");
|
|
326
|
+
}
|
|
327
|
+
function popToken(tokens, state) {
|
|
328
|
+
const token = tokens[state.pos];
|
|
329
|
+
state.pos += 1;
|
|
330
|
+
if (!token) {
|
|
331
|
+
const lastLine = tokens.length !== 0 ? tokens[tokens.length - 1].line : 1;
|
|
332
|
+
return { type: "eof", match: "", value: void 0, line: lastLine };
|
|
333
|
+
}
|
|
334
|
+
return token;
|
|
335
|
+
}
|
|
336
|
+
function strToken(token) {
|
|
337
|
+
switch (token.type) {
|
|
338
|
+
case "atom":
|
|
339
|
+
case "string":
|
|
340
|
+
case "number":
|
|
341
|
+
return `${token.type} ${token.match}`;
|
|
342
|
+
case "eof":
|
|
343
|
+
return "end-of-file";
|
|
344
|
+
default:
|
|
345
|
+
return `'${token.type}'`;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
function skipColon(tokens, state) {
|
|
349
|
+
const colon = popToken(tokens, state);
|
|
350
|
+
if (colon.type !== ":") {
|
|
351
|
+
const message = `Unexpected token: ${strToken(colon)}, expected ':'`;
|
|
352
|
+
if (state.tolerant) {
|
|
353
|
+
state.warnings.push({
|
|
354
|
+
message,
|
|
355
|
+
line: colon.line
|
|
356
|
+
});
|
|
357
|
+
state.pos -= 1;
|
|
358
|
+
} else {
|
|
359
|
+
const err = new SyntaxError(message);
|
|
360
|
+
err.line = colon.line;
|
|
361
|
+
throw err;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
function skipPunctuation(tokens, state, valid) {
|
|
366
|
+
const punctuation = [",", ":", "]", "}"];
|
|
367
|
+
let token = popToken(tokens, state);
|
|
368
|
+
while (true) {
|
|
369
|
+
if (valid && valid.includes(token.type)) {
|
|
370
|
+
return token;
|
|
371
|
+
} else if (token.type === "eof") {
|
|
372
|
+
return token;
|
|
373
|
+
} else if (punctuation.includes(token.type)) {
|
|
374
|
+
const message = `Unexpected token: ${strToken(
|
|
375
|
+
token
|
|
376
|
+
)}, expected '[', '{', number, string or atom`;
|
|
377
|
+
if (state.tolerant) {
|
|
378
|
+
state.warnings.push({
|
|
379
|
+
message,
|
|
380
|
+
line: token.line
|
|
381
|
+
});
|
|
382
|
+
token = popToken(tokens, state);
|
|
383
|
+
} else {
|
|
384
|
+
const err = new SyntaxError(message);
|
|
385
|
+
err.line = token.line;
|
|
386
|
+
throw err;
|
|
387
|
+
}
|
|
388
|
+
} else {
|
|
389
|
+
return token;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
function raiseError(state, token, message) {
|
|
394
|
+
if (state.tolerant) {
|
|
395
|
+
state.warnings.push({
|
|
396
|
+
message,
|
|
397
|
+
line: token.line
|
|
398
|
+
});
|
|
399
|
+
} else {
|
|
400
|
+
const err = new SyntaxError(message);
|
|
401
|
+
err.line = token.line;
|
|
402
|
+
throw err;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function raiseUnexpected(state, token, expected) {
|
|
406
|
+
raiseError(
|
|
407
|
+
state,
|
|
408
|
+
token,
|
|
409
|
+
`Unexpected token: ${strToken(token)}, expected ${expected}`
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
function checkDuplicates(state, obj, token) {
|
|
413
|
+
const key = String(token.value);
|
|
414
|
+
if (!state.duplicate && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
415
|
+
raiseError(state, token, `Duplicate key: ${key}`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function appendPair(state, obj, key, value) {
|
|
419
|
+
const finalValue = state.reviver ? state.reviver(key, value) : value;
|
|
420
|
+
if (finalValue !== void 0) {
|
|
421
|
+
obj[key] = finalValue;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
function parsePair(tokens, state, obj) {
|
|
425
|
+
let token = skipPunctuation(tokens, state, [":", "string", "number", "atom"]);
|
|
426
|
+
let value;
|
|
427
|
+
if (token.type !== "string") {
|
|
428
|
+
raiseUnexpected(state, token, "string key");
|
|
429
|
+
if (state.tolerant) {
|
|
430
|
+
switch (token.type) {
|
|
431
|
+
case ":":
|
|
432
|
+
token = {
|
|
433
|
+
type: "string",
|
|
434
|
+
value: "null",
|
|
435
|
+
match: '"null"',
|
|
436
|
+
line: token.line
|
|
437
|
+
};
|
|
438
|
+
state.pos -= 1;
|
|
439
|
+
break;
|
|
440
|
+
case "number":
|
|
441
|
+
// Use number as string key
|
|
442
|
+
case "atom":
|
|
443
|
+
token = {
|
|
444
|
+
type: "string",
|
|
445
|
+
value: String(token.value),
|
|
446
|
+
match: `"${token.value}"`,
|
|
447
|
+
line: token.line
|
|
448
|
+
};
|
|
449
|
+
break;
|
|
450
|
+
case "[":
|
|
451
|
+
// Assume missing key before an array
|
|
452
|
+
case "{":
|
|
453
|
+
state.pos -= 1;
|
|
454
|
+
value = parseAny(tokens, state);
|
|
455
|
+
checkDuplicates(state, obj, {
|
|
456
|
+
type: "string",
|
|
457
|
+
value: "null",
|
|
458
|
+
match: '"null"',
|
|
459
|
+
line: token.line
|
|
460
|
+
});
|
|
461
|
+
appendPair(state, obj, "null", value);
|
|
462
|
+
return;
|
|
463
|
+
// Finished parsing this "pair"
|
|
464
|
+
case "eof":
|
|
465
|
+
return;
|
|
466
|
+
// Cannot recover
|
|
467
|
+
default:
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
} else {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
checkDuplicates(state, obj, token);
|
|
475
|
+
const key = String(token.value);
|
|
476
|
+
skipColon(tokens, state);
|
|
477
|
+
value = parseAny(tokens, state);
|
|
478
|
+
appendPair(state, obj, key, value);
|
|
479
|
+
}
|
|
480
|
+
function parseElement(tokens, state, arr) {
|
|
481
|
+
const key = arr.length;
|
|
482
|
+
const value = parseAny(tokens, state);
|
|
483
|
+
arr[key] = state.reviver ? state.reviver(String(key), value) : value;
|
|
484
|
+
}
|
|
485
|
+
function parseObject(tokens, state) {
|
|
486
|
+
const obj = {};
|
|
487
|
+
return parseMany(tokens, state, obj, {
|
|
488
|
+
skip: [":", "}"],
|
|
489
|
+
// Initially skip over colon or closing brace (for empty/tolerant cases)
|
|
490
|
+
elementParser: parsePair,
|
|
491
|
+
// Use parsePair to parse each key-value element
|
|
492
|
+
elementName: "string key",
|
|
493
|
+
// Expected element type for errors
|
|
494
|
+
endSymbol: "}"
|
|
495
|
+
// The closing token for an object
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
function parseArray(tokens, state) {
|
|
499
|
+
const arr = [];
|
|
500
|
+
return parseMany(tokens, state, arr, {
|
|
501
|
+
skip: ["]"],
|
|
502
|
+
// Initially skip over closing bracket (for empty/tolerant cases)
|
|
503
|
+
elementParser: parseElement,
|
|
504
|
+
// Use parseElement to parse each array item
|
|
505
|
+
elementName: "json value",
|
|
506
|
+
// Expected element type for errors
|
|
507
|
+
endSymbol: "]"
|
|
508
|
+
// The closing token for an array
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
function parseMany(tokens, state, result, opts) {
|
|
512
|
+
let token = skipPunctuation(tokens, state, opts.skip);
|
|
513
|
+
if (token.type === "eof") {
|
|
514
|
+
raiseUnexpected(state, token, `'${opts.endSymbol}' or ${opts.elementName}`);
|
|
515
|
+
if (state.tolerant) {
|
|
516
|
+
return result;
|
|
517
|
+
} else {
|
|
518
|
+
return result;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if (token.type === opts.endSymbol) {
|
|
522
|
+
return result;
|
|
523
|
+
}
|
|
524
|
+
state.pos -= 1;
|
|
525
|
+
opts.elementParser(tokens, state, result);
|
|
526
|
+
while (true) {
|
|
527
|
+
token = popToken(tokens, state);
|
|
528
|
+
if (token.type !== opts.endSymbol && token.type !== ",") {
|
|
529
|
+
raiseUnexpected(state, token, `',' or '${opts.endSymbol}'`);
|
|
530
|
+
if (state.tolerant) {
|
|
531
|
+
if (token.type === "eof") {
|
|
532
|
+
return result;
|
|
533
|
+
}
|
|
534
|
+
state.pos -= 1;
|
|
535
|
+
} else {
|
|
536
|
+
return result;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
switch (token.type) {
|
|
540
|
+
case opts.endSymbol:
|
|
541
|
+
return result;
|
|
542
|
+
case ",": {
|
|
543
|
+
const nextToken = tokens[state.pos];
|
|
544
|
+
if (state.tolerant && nextToken && nextToken.type === opts.endSymbol) {
|
|
545
|
+
raiseError(state, token, `Trailing comma before '${opts.endSymbol}'`);
|
|
546
|
+
popToken(tokens, state);
|
|
547
|
+
return result;
|
|
548
|
+
}
|
|
549
|
+
opts.elementParser(tokens, state, result);
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
// Default case is only reachable in tolerant mode recovery above
|
|
553
|
+
default:
|
|
554
|
+
opts.elementParser(tokens, state, result);
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
function endChecks(tokens, state, ret) {
|
|
560
|
+
if (state.pos < tokens.length) {
|
|
561
|
+
if (state.tolerant) {
|
|
562
|
+
skipPunctuation(tokens, state);
|
|
563
|
+
}
|
|
564
|
+
if (state.pos < tokens.length) {
|
|
565
|
+
raiseError(
|
|
566
|
+
state,
|
|
567
|
+
tokens[state.pos],
|
|
568
|
+
`Unexpected token: ${strToken(tokens[state.pos])}, expected end-of-input`
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (state.tolerant && state.warnings.length > 0) {
|
|
573
|
+
const message = state.warnings.length === 1 ? state.warnings[0].message : `${state.warnings.length} parse warnings`;
|
|
574
|
+
const err = new SyntaxError(message);
|
|
575
|
+
err.line = state.warnings[0].line;
|
|
576
|
+
err.warnings = state.warnings;
|
|
577
|
+
err.obj = ret;
|
|
578
|
+
throw err;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
function parseAny(tokens, state, end = false) {
|
|
582
|
+
const token = skipPunctuation(tokens, state);
|
|
583
|
+
let ret;
|
|
584
|
+
if (token.type === "eof") {
|
|
585
|
+
if (end) {
|
|
586
|
+
raiseUnexpected(state, token, "json value");
|
|
587
|
+
}
|
|
588
|
+
raiseUnexpected(state, token, "json value");
|
|
589
|
+
return void 0;
|
|
590
|
+
}
|
|
591
|
+
switch (token.type) {
|
|
592
|
+
case "{":
|
|
593
|
+
ret = parseObject(tokens, state);
|
|
594
|
+
break;
|
|
595
|
+
case "[":
|
|
596
|
+
ret = parseArray(tokens, state);
|
|
597
|
+
break;
|
|
598
|
+
case "string":
|
|
599
|
+
// String literal
|
|
600
|
+
case "number":
|
|
601
|
+
// Number literal
|
|
602
|
+
case "atom":
|
|
603
|
+
ret = token.value;
|
|
604
|
+
break;
|
|
605
|
+
default:
|
|
606
|
+
raiseUnexpected(state, token, "json value");
|
|
607
|
+
if (state.tolerant) {
|
|
608
|
+
ret = null;
|
|
609
|
+
} else {
|
|
610
|
+
return void 0;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
if (end) {
|
|
614
|
+
ret = state.reviver ? state.reviver("", ret) : ret;
|
|
615
|
+
endChecks(tokens, state, ret);
|
|
616
|
+
}
|
|
617
|
+
return ret;
|
|
618
|
+
}
|
|
619
|
+
function parse(text, optsOrReviver) {
|
|
620
|
+
var _a;
|
|
621
|
+
let options = {};
|
|
622
|
+
if (typeof optsOrReviver === "function") {
|
|
623
|
+
options.reviver = optsOrReviver;
|
|
624
|
+
} else if (optsOrReviver !== null && typeof optsOrReviver === "object") {
|
|
625
|
+
options = { ...optsOrReviver };
|
|
626
|
+
} else if (optsOrReviver !== void 0) {
|
|
627
|
+
throw new TypeError(
|
|
628
|
+
"Second argument must be a reviver function or an options object."
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
if (options.relaxed === void 0) {
|
|
632
|
+
if (options.warnings === true || options.tolerant === true) {
|
|
633
|
+
options.relaxed = true;
|
|
634
|
+
} else if (options.warnings === false && options.tolerant === false) {
|
|
635
|
+
options.relaxed = false;
|
|
636
|
+
} else {
|
|
637
|
+
options.relaxed = true;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
options.tolerant = options.tolerant || options.warnings || false;
|
|
641
|
+
options.warnings = options.warnings || false;
|
|
642
|
+
options.duplicate = (_a = options.duplicate) != null ? _a : false;
|
|
643
|
+
if (!options.relaxed && !options.warnings && !options.tolerant) {
|
|
644
|
+
if (!options.duplicate) {
|
|
645
|
+
} else {
|
|
646
|
+
return JSON.parse(
|
|
647
|
+
text,
|
|
648
|
+
options.reviver
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
const lexerToUse = options.relaxed ? lexer : strictLexer;
|
|
653
|
+
let tokens = lexerToUse(text);
|
|
654
|
+
if (options.relaxed) {
|
|
655
|
+
tokens = stripTrailingComma(tokens);
|
|
656
|
+
}
|
|
657
|
+
if (options.warnings || options.tolerant) {
|
|
658
|
+
tokens = tokens.filter((token) => token.type !== " ");
|
|
659
|
+
const state = {
|
|
660
|
+
pos: 0,
|
|
661
|
+
reviver: options.reviver,
|
|
662
|
+
tolerant: options.tolerant,
|
|
663
|
+
duplicate: options.duplicate,
|
|
664
|
+
// true = allow duplicate keys, false = reject duplicates
|
|
665
|
+
warnings: []
|
|
666
|
+
};
|
|
667
|
+
return parseAny(tokens, state, true);
|
|
668
|
+
} else {
|
|
669
|
+
tokens.reduce((str, token) => {
|
|
670
|
+
return str + token.match;
|
|
671
|
+
}, "");
|
|
672
|
+
if (!options.relaxed && !options.warnings && !options.tolerant && options.duplicate) {
|
|
673
|
+
return JSON.parse(text, options.reviver);
|
|
674
|
+
} else if (options.warnings || options.tolerant || !options.duplicate) {
|
|
675
|
+
tokens = lexerToUse(text);
|
|
676
|
+
if (options.relaxed) {
|
|
677
|
+
tokens = stripTrailingComma(tokens);
|
|
678
|
+
}
|
|
679
|
+
tokens = tokens.filter((token) => token.type !== " ");
|
|
680
|
+
const state = {
|
|
681
|
+
pos: 0,
|
|
682
|
+
reviver: options.reviver,
|
|
683
|
+
tolerant: options.tolerant || false,
|
|
684
|
+
// Ensure boolean
|
|
685
|
+
duplicate: options.duplicate,
|
|
686
|
+
// true = allow duplicate keys, false = reject duplicates
|
|
687
|
+
warnings: []
|
|
688
|
+
};
|
|
689
|
+
return parseAny(tokens, state, true);
|
|
690
|
+
} else {
|
|
691
|
+
tokens = lexer(text);
|
|
692
|
+
tokens = stripTrailingComma(tokens);
|
|
693
|
+
const newtext = tokens.reduce((str, token) => str + token.match, "");
|
|
694
|
+
return JSON.parse(
|
|
695
|
+
newtext,
|
|
696
|
+
options.reviver
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
function stringifyPair(obj, key) {
|
|
702
|
+
return JSON.stringify(key) + ":" + stringify(obj[key]);
|
|
703
|
+
}
|
|
704
|
+
function stringify(obj) {
|
|
705
|
+
const type = typeof obj;
|
|
706
|
+
if (type === "string" || type === "number" || type === "boolean" || obj === null) {
|
|
707
|
+
return JSON.stringify(obj);
|
|
708
|
+
}
|
|
709
|
+
if (type === "undefined") {
|
|
710
|
+
return "null";
|
|
711
|
+
}
|
|
712
|
+
if (Array.isArray(obj)) {
|
|
713
|
+
const elements = obj.map(stringify).join(",");
|
|
714
|
+
return "[" + elements + "]";
|
|
715
|
+
}
|
|
716
|
+
if (type === "object") {
|
|
717
|
+
const keys = Object.keys(obj);
|
|
718
|
+
keys.sort();
|
|
719
|
+
const pairs = keys.map((key) => stringifyPair(obj, key)).join(",");
|
|
720
|
+
return "{" + pairs + "}";
|
|
721
|
+
}
|
|
722
|
+
return "null";
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// src/utils/debug.ts
|
|
726
|
+
function normalizeBooleanString(value) {
|
|
727
|
+
const normalized = value.trim().toLowerCase();
|
|
728
|
+
if (normalized === "1" || normalized === "true" || normalized === "yes") {
|
|
729
|
+
return true;
|
|
730
|
+
}
|
|
731
|
+
if (normalized === "0" || normalized === "false" || normalized === "no") {
|
|
732
|
+
return false;
|
|
733
|
+
}
|
|
734
|
+
return void 0;
|
|
735
|
+
}
|
|
736
|
+
function getDebugLevel() {
|
|
737
|
+
const envVal = typeof process !== "undefined" && process.env && process.env.DEBUG_PARSER_MW || "off";
|
|
738
|
+
const envLower = String(envVal).toLowerCase();
|
|
739
|
+
if (envLower === "stream" || envLower === "parse" || envLower === "off") {
|
|
740
|
+
return envLower;
|
|
741
|
+
}
|
|
742
|
+
const boolEnv = normalizeBooleanString(envLower);
|
|
743
|
+
if (boolEnv === true) return "stream";
|
|
744
|
+
if (envLower === "2") return "parse";
|
|
745
|
+
return "off";
|
|
746
|
+
}
|
|
747
|
+
function color(code) {
|
|
748
|
+
return (text) => `\x1B[${code}m${text}\x1B[0m`;
|
|
749
|
+
}
|
|
750
|
+
var cGray = color(90);
|
|
751
|
+
var cYellow = color(33);
|
|
752
|
+
var cCyan = color(36);
|
|
753
|
+
var cBgBlue = color(44);
|
|
754
|
+
var cBgGreen = color(42);
|
|
755
|
+
var cInverse = color(7);
|
|
756
|
+
var cUnderline = color(4);
|
|
757
|
+
var cBold = color(1);
|
|
758
|
+
function safeStringify(value) {
|
|
759
|
+
try {
|
|
760
|
+
return `
|
|
761
|
+
${typeof value === "string" ? value : JSON.stringify(value, null, 2)}`;
|
|
762
|
+
} catch (e) {
|
|
763
|
+
return String(value);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
function logRawChunk(part) {
|
|
767
|
+
console.log(cGray("[debug:mw:raw]"), cYellow(safeStringify(part)));
|
|
768
|
+
}
|
|
769
|
+
function logParsedChunk(part) {
|
|
770
|
+
console.log(cGray("[debug:mw:out]"), cCyan(safeStringify(part)));
|
|
771
|
+
}
|
|
772
|
+
function logParsedSummary({
|
|
773
|
+
toolCalls,
|
|
774
|
+
originalText
|
|
775
|
+
}) {
|
|
776
|
+
if (originalText) {
|
|
777
|
+
const style = (() => {
|
|
778
|
+
const envVal = typeof process !== "undefined" && process.env && process.env.DEBUG_PARSER_MW_STYLE || "bg";
|
|
779
|
+
const normalized = String(envVal).trim().toLowerCase();
|
|
780
|
+
if (normalized === "inverse" || normalized === "invert")
|
|
781
|
+
return "inverse";
|
|
782
|
+
if (normalized === "underline" || normalized === "ul")
|
|
783
|
+
return "underline";
|
|
784
|
+
if (normalized === "bold") return "bold";
|
|
785
|
+
if (normalized === "bg" || normalized === "background")
|
|
786
|
+
return "bg";
|
|
787
|
+
const asBool = normalizeBooleanString(normalized);
|
|
788
|
+
if (asBool === true) return "bg";
|
|
789
|
+
return "bg";
|
|
790
|
+
})();
|
|
791
|
+
const highlight = style === "inverse" ? cInverse : style === "underline" ? cUnderline : style === "bold" ? cBold : style === "bg" ? cBgGreen : cYellow;
|
|
792
|
+
const rendered = style === "bg" || style === "inverse" || style === "underline" || style === "bold" ? originalText.split(/\r?\n/).map((line) => line.length ? highlight(line) : line).join("\n") : highlight(originalText);
|
|
793
|
+
console.log(cGray("[debug:mw:origin]"), `
|
|
794
|
+
${rendered}`);
|
|
795
|
+
}
|
|
796
|
+
if (toolCalls.length > 0) {
|
|
797
|
+
const styledSummary = safeStringify(toolCalls).split(/\r?\n/).map((line) => line.length ? cBgBlue(line) : line).join("\n");
|
|
798
|
+
console.log(cGray("[debug:mw:summary]"), styledSummary);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
// src/utils/on-error.ts
|
|
803
|
+
function extractOnErrorOption(providerOptions) {
|
|
804
|
+
var _a;
|
|
805
|
+
if (providerOptions && typeof providerOptions === "object") {
|
|
806
|
+
const onError = (_a = providerOptions.toolCallMiddleware) == null ? void 0 : _a.onError;
|
|
807
|
+
return onError ? { onError } : void 0;
|
|
808
|
+
}
|
|
809
|
+
return void 0;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// src/utils/provider-options.ts
|
|
813
|
+
var originalToolsSchema = {
|
|
814
|
+
encode: encodeOriginalTools,
|
|
815
|
+
decode: decodeOriginalTools
|
|
816
|
+
};
|
|
817
|
+
function encodeOriginalTools(tools) {
|
|
818
|
+
return (tools == null ? void 0 : tools.map((t) => ({
|
|
819
|
+
name: t.name,
|
|
820
|
+
inputSchema: JSON.stringify(t.inputSchema)
|
|
821
|
+
}))) || [];
|
|
822
|
+
}
|
|
823
|
+
function decodeOriginalTools(originalTools) {
|
|
824
|
+
const tools = (originalTools == null ? void 0 : originalTools.map(
|
|
825
|
+
(t) => ({
|
|
826
|
+
name: t.name,
|
|
827
|
+
inputSchema: JSON.parse(t.inputSchema)
|
|
828
|
+
})
|
|
829
|
+
)) || [];
|
|
830
|
+
return tools;
|
|
831
|
+
}
|
|
832
|
+
function extractToolNamesFromOriginalTools(originalTools) {
|
|
833
|
+
return (originalTools == null ? void 0 : originalTools.map((t) => t.name)) || [];
|
|
834
|
+
}
|
|
835
|
+
function isToolChoiceActive(params) {
|
|
836
|
+
var _a, _b, _c;
|
|
837
|
+
const toolChoice = (_b = (_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware) == null ? void 0 : _b.toolChoice;
|
|
838
|
+
return !!(typeof params.providerOptions === "object" && params.providerOptions !== null && typeof ((_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware) === "object" && toolChoice && typeof toolChoice === "object" && (toolChoice.type === "tool" || toolChoice.type === "required"));
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
// src/utils/type-guards.ts
|
|
842
|
+
function isToolCallContent(content) {
|
|
843
|
+
return content.type === "tool-call" && typeof content.toolName === "string" && // input may be a JSON string or an already-parsed object depending on provider/runtime
|
|
844
|
+
(typeof content.input === "string" || typeof content.input === "object");
|
|
845
|
+
}
|
|
846
|
+
function isToolResultPart(content) {
|
|
847
|
+
const c = content;
|
|
848
|
+
return !!c && c.type === "tool-result" && typeof c.toolName === "string" && typeof c.toolCallId === "string" && "output" in c;
|
|
849
|
+
}
|
|
850
|
+
function hasInputProperty(obj) {
|
|
851
|
+
return typeof obj === "object" && obj !== null && "input" in obj;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// src/protocols/json-mix-protocol.ts
|
|
855
|
+
var jsonMixProtocol = ({
|
|
856
|
+
toolCallStart = "<tool_call>",
|
|
857
|
+
toolCallEnd = "</tool_call>",
|
|
858
|
+
toolResponseStart = "<tool_response>",
|
|
859
|
+
toolResponseEnd = "</tool_response>"
|
|
860
|
+
} = {}) => ({
|
|
861
|
+
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
862
|
+
const toolsForPrompt = (tools || []).filter((tool) => tool.type === "function").map((tool) => ({
|
|
863
|
+
name: tool.name,
|
|
864
|
+
description: tool.type === "function" && typeof tool.description === "string" ? tool.description : void 0,
|
|
865
|
+
parameters: tool.inputSchema
|
|
866
|
+
}));
|
|
867
|
+
return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
|
|
868
|
+
},
|
|
869
|
+
formatToolCall(toolCall) {
|
|
870
|
+
let args = {};
|
|
871
|
+
try {
|
|
872
|
+
args = JSON.parse(toolCall.input);
|
|
873
|
+
} catch (e) {
|
|
874
|
+
args = toolCall.input;
|
|
875
|
+
}
|
|
876
|
+
return `${toolCallStart}${JSON.stringify({
|
|
877
|
+
name: toolCall.toolName,
|
|
878
|
+
arguments: args
|
|
879
|
+
})}${toolCallEnd}`;
|
|
880
|
+
},
|
|
881
|
+
formatToolResponse(toolResult) {
|
|
882
|
+
return `${toolResponseStart}${JSON.stringify({
|
|
883
|
+
toolName: toolResult.toolName,
|
|
884
|
+
result: toolResult.output
|
|
885
|
+
})}${toolResponseEnd}`;
|
|
886
|
+
},
|
|
887
|
+
parseGeneratedText({ text, options }) {
|
|
888
|
+
var _a;
|
|
889
|
+
const startEsc = escapeRegExp(toolCallStart);
|
|
890
|
+
const endEsc = escapeRegExp(toolCallEnd);
|
|
891
|
+
const toolCallRegex = new RegExp(
|
|
892
|
+
`${startEsc}([\0-\uFFFF]*?)${endEsc}`,
|
|
893
|
+
"gs"
|
|
894
|
+
);
|
|
895
|
+
const processedElements = [];
|
|
896
|
+
let currentIndex = 0;
|
|
897
|
+
let match;
|
|
898
|
+
while ((match = toolCallRegex.exec(text)) !== null) {
|
|
899
|
+
const startIndex = match.index;
|
|
900
|
+
const toolCallJson = match[1];
|
|
901
|
+
if (startIndex > currentIndex) {
|
|
902
|
+
const textSegment = text.substring(currentIndex, startIndex);
|
|
903
|
+
if (textSegment.trim()) {
|
|
904
|
+
processedElements.push({ type: "text", text: textSegment });
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
if (toolCallJson) {
|
|
908
|
+
try {
|
|
909
|
+
const parsedToolCall = robust_json_exports.parse(toolCallJson);
|
|
910
|
+
processedElements.push({
|
|
911
|
+
type: "tool-call",
|
|
912
|
+
toolCallId: generateId2(),
|
|
913
|
+
toolName: parsedToolCall.name,
|
|
914
|
+
input: JSON.stringify((_a = parsedToolCall.arguments) != null ? _a : {})
|
|
915
|
+
});
|
|
916
|
+
} catch (error) {
|
|
917
|
+
if (options == null ? void 0 : options.onError) {
|
|
918
|
+
options.onError(
|
|
919
|
+
"Could not process JSON tool call, keeping original text.",
|
|
920
|
+
{ toolCall: match[0], error }
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
processedElements.push({ type: "text", text: match[0] });
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
currentIndex = startIndex + match[0].length;
|
|
927
|
+
}
|
|
928
|
+
if (currentIndex < text.length) {
|
|
929
|
+
const remainingText = text.substring(currentIndex);
|
|
930
|
+
if (remainingText.trim()) {
|
|
931
|
+
processedElements.push({ type: "text", text: remainingText });
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return processedElements;
|
|
935
|
+
},
|
|
936
|
+
createStreamParser({ tools: _tools, options } = { tools: [] }) {
|
|
937
|
+
let isInsideToolCall = false;
|
|
938
|
+
let buffer = "";
|
|
939
|
+
let currentToolCallJson = "";
|
|
940
|
+
let currentTextId = null;
|
|
941
|
+
let hasEmittedTextStart = false;
|
|
942
|
+
return new TransformStream({
|
|
943
|
+
transform(chunk, controller) {
|
|
944
|
+
var _a;
|
|
945
|
+
if (chunk.type === "finish") {
|
|
946
|
+
if (isInsideToolCall && buffer.length > 0) {
|
|
947
|
+
if (!currentTextId) {
|
|
948
|
+
currentTextId = generateId2();
|
|
949
|
+
controller.enqueue({ type: "text-start", id: currentTextId });
|
|
950
|
+
hasEmittedTextStart = true;
|
|
951
|
+
}
|
|
952
|
+
controller.enqueue({
|
|
953
|
+
type: "text-delta",
|
|
954
|
+
id: currentTextId,
|
|
955
|
+
delta: `${toolCallStart}${buffer}`
|
|
956
|
+
});
|
|
957
|
+
buffer = "";
|
|
958
|
+
} else if (!isInsideToolCall && buffer.length > 0) {
|
|
959
|
+
if (!currentTextId) {
|
|
960
|
+
currentTextId = generateId2();
|
|
961
|
+
controller.enqueue({ type: "text-start", id: currentTextId });
|
|
962
|
+
hasEmittedTextStart = true;
|
|
963
|
+
}
|
|
964
|
+
controller.enqueue({
|
|
965
|
+
type: "text-delta",
|
|
966
|
+
id: currentTextId,
|
|
967
|
+
delta: buffer
|
|
968
|
+
});
|
|
969
|
+
buffer = "";
|
|
970
|
+
}
|
|
971
|
+
if (currentTextId && hasEmittedTextStart) {
|
|
972
|
+
controller.enqueue({ type: "text-end", id: currentTextId });
|
|
973
|
+
currentTextId = null;
|
|
974
|
+
hasEmittedTextStart = false;
|
|
975
|
+
}
|
|
976
|
+
if (currentToolCallJson) {
|
|
977
|
+
const errorId = generateId2();
|
|
978
|
+
controller.enqueue({ type: "text-start", id: errorId });
|
|
979
|
+
controller.enqueue({
|
|
980
|
+
type: "text-delta",
|
|
981
|
+
id: errorId,
|
|
982
|
+
delta: `${toolCallStart}${currentToolCallJson}`
|
|
983
|
+
});
|
|
984
|
+
controller.enqueue({ type: "text-end", id: errorId });
|
|
985
|
+
currentToolCallJson = "";
|
|
986
|
+
}
|
|
987
|
+
controller.enqueue(chunk);
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
if (chunk.type !== "text-delta") {
|
|
991
|
+
controller.enqueue(chunk);
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
buffer += chunk.delta;
|
|
995
|
+
const publish = (text) => {
|
|
996
|
+
if (isInsideToolCall) {
|
|
997
|
+
if (currentTextId && hasEmittedTextStart) {
|
|
998
|
+
controller.enqueue({ type: "text-end", id: currentTextId });
|
|
999
|
+
currentTextId = null;
|
|
1000
|
+
hasEmittedTextStart = false;
|
|
1001
|
+
}
|
|
1002
|
+
currentToolCallJson += text;
|
|
1003
|
+
} else if (text.length > 0) {
|
|
1004
|
+
if (!currentTextId) {
|
|
1005
|
+
currentTextId = generateId2();
|
|
1006
|
+
controller.enqueue({ type: "text-start", id: currentTextId });
|
|
1007
|
+
hasEmittedTextStart = true;
|
|
1008
|
+
}
|
|
1009
|
+
controller.enqueue({
|
|
1010
|
+
type: "text-delta",
|
|
1011
|
+
id: currentTextId,
|
|
1012
|
+
delta: text
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
};
|
|
1016
|
+
let startIndex;
|
|
1017
|
+
while ((startIndex = getPotentialStartIndex(
|
|
1018
|
+
buffer,
|
|
1019
|
+
isInsideToolCall ? toolCallEnd : toolCallStart
|
|
1020
|
+
)) != null) {
|
|
1021
|
+
const tag = isInsideToolCall ? toolCallEnd : toolCallStart;
|
|
1022
|
+
if (startIndex + tag.length > buffer.length) {
|
|
1023
|
+
break;
|
|
1024
|
+
}
|
|
1025
|
+
publish(buffer.slice(0, startIndex));
|
|
1026
|
+
buffer = buffer.slice(startIndex + tag.length);
|
|
1027
|
+
if (!isInsideToolCall) {
|
|
1028
|
+
currentToolCallJson = "";
|
|
1029
|
+
isInsideToolCall = true;
|
|
1030
|
+
} else {
|
|
1031
|
+
try {
|
|
1032
|
+
const parsedToolCall = robust_json_exports.parse(currentToolCallJson);
|
|
1033
|
+
if (currentTextId && hasEmittedTextStart) {
|
|
1034
|
+
controller.enqueue({ type: "text-end", id: currentTextId });
|
|
1035
|
+
currentTextId = null;
|
|
1036
|
+
hasEmittedTextStart = false;
|
|
1037
|
+
}
|
|
1038
|
+
controller.enqueue({
|
|
1039
|
+
type: "tool-call",
|
|
1040
|
+
toolCallId: generateId2(),
|
|
1041
|
+
toolName: parsedToolCall.name,
|
|
1042
|
+
input: JSON.stringify((_a = parsedToolCall.arguments) != null ? _a : {})
|
|
1043
|
+
});
|
|
1044
|
+
} catch (e) {
|
|
1045
|
+
const errorId = generateId2();
|
|
1046
|
+
controller.enqueue({ type: "text-start", id: errorId });
|
|
1047
|
+
controller.enqueue({
|
|
1048
|
+
type: "text-delta",
|
|
1049
|
+
id: errorId,
|
|
1050
|
+
delta: `${toolCallStart}${currentToolCallJson}${toolCallEnd}`
|
|
1051
|
+
});
|
|
1052
|
+
controller.enqueue({ type: "text-end", id: errorId });
|
|
1053
|
+
if (options == null ? void 0 : options.onError) {
|
|
1054
|
+
options.onError(
|
|
1055
|
+
"Could not process streaming JSON tool call; emitting original text.",
|
|
1056
|
+
{
|
|
1057
|
+
toolCall: `${toolCallStart}${currentToolCallJson}${toolCallEnd}`
|
|
1058
|
+
}
|
|
1059
|
+
);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
currentToolCallJson = "";
|
|
1063
|
+
isInsideToolCall = false;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
if (!isInsideToolCall) {
|
|
1067
|
+
const potentialIndex = getPotentialStartIndex(buffer, toolCallStart);
|
|
1068
|
+
if (potentialIndex != null && potentialIndex + toolCallStart.length > buffer.length) {
|
|
1069
|
+
publish(buffer.slice(0, potentialIndex));
|
|
1070
|
+
buffer = buffer.slice(potentialIndex);
|
|
1071
|
+
} else {
|
|
1072
|
+
publish(buffer);
|
|
1073
|
+
buffer = "";
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
});
|
|
1078
|
+
},
|
|
1079
|
+
extractToolCallSegments({ text }) {
|
|
1080
|
+
const startEsc = escapeRegExp(toolCallStart);
|
|
1081
|
+
const endEsc = escapeRegExp(toolCallEnd);
|
|
1082
|
+
const regex = new RegExp(`${startEsc}([\0-\uFFFF]*?)${endEsc}`, "gs");
|
|
1083
|
+
const segments = [];
|
|
1084
|
+
let m;
|
|
1085
|
+
while ((m = regex.exec(text)) != null) {
|
|
1086
|
+
segments.push(m[0]);
|
|
1087
|
+
}
|
|
1088
|
+
return segments;
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
|
|
1092
|
+
// src/protocols/morph-xml-protocol.ts
|
|
1093
|
+
import { generateId as generateId3 } from "@ai-sdk/provider-utils";
|
|
1094
|
+
import * as RXML from "@ai-sdk-tool/rxml";
|
|
1095
|
+
var morphXmlProtocol = () => ({
|
|
1096
|
+
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
1097
|
+
const toolsForPrompt = (tools || []).map((tool) => ({
|
|
1098
|
+
name: tool.name,
|
|
1099
|
+
description: tool.description,
|
|
1100
|
+
parameters: RXML.unwrapJsonSchema(tool.inputSchema)
|
|
1101
|
+
}));
|
|
1102
|
+
return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
|
|
1103
|
+
},
|
|
1104
|
+
formatToolCall(toolCall) {
|
|
1105
|
+
let args = {};
|
|
1106
|
+
const inputValue = hasInputProperty(toolCall) ? toolCall.input : void 0;
|
|
1107
|
+
if (typeof inputValue === "string") {
|
|
1108
|
+
try {
|
|
1109
|
+
args = JSON.parse(inputValue);
|
|
1110
|
+
} catch (e) {
|
|
1111
|
+
args = inputValue;
|
|
1112
|
+
}
|
|
1113
|
+
} else {
|
|
1114
|
+
args = inputValue;
|
|
1115
|
+
}
|
|
1116
|
+
return RXML.stringify(toolCall.toolName, args, {
|
|
1117
|
+
suppressEmptyNode: false,
|
|
1118
|
+
format: false
|
|
1119
|
+
});
|
|
1120
|
+
},
|
|
1121
|
+
formatToolResponse(toolResult) {
|
|
1122
|
+
return RXML.stringify("tool_response", {
|
|
1123
|
+
tool_name: toolResult.toolName,
|
|
1124
|
+
result: toolResult.output
|
|
1125
|
+
});
|
|
1126
|
+
},
|
|
1127
|
+
parseGeneratedText({ text, tools, options }) {
|
|
1128
|
+
var _a;
|
|
1129
|
+
const toolNames = tools.map((t) => t.name).filter((name) => name != null);
|
|
1130
|
+
if (toolNames.length === 0) {
|
|
1131
|
+
return [{ type: "text", text }];
|
|
1132
|
+
}
|
|
1133
|
+
const processedElements = [];
|
|
1134
|
+
let currentIndex = 0;
|
|
1135
|
+
const toolCalls = findToolCalls(text, toolNames);
|
|
1136
|
+
for (const toolCall of toolCalls) {
|
|
1137
|
+
if (toolCall.startIndex > currentIndex) {
|
|
1138
|
+
const textSegment = text.substring(currentIndex, toolCall.startIndex);
|
|
1139
|
+
if (textSegment.trim()) {
|
|
1140
|
+
processedElements.push({ type: "text", text: textSegment });
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
try {
|
|
1144
|
+
const toolSchema = getToolSchema(tools, toolCall.toolName);
|
|
1145
|
+
const parsed = RXML.parse(toolCall.content, toolSchema, {
|
|
1146
|
+
onError: options == null ? void 0 : options.onError,
|
|
1147
|
+
// Disable HTML self-closing tag behavior to allow base, meta, link etc. as regular tags
|
|
1148
|
+
noChildNodes: []
|
|
1149
|
+
});
|
|
1150
|
+
processedElements.push({
|
|
1151
|
+
type: "tool-call",
|
|
1152
|
+
toolCallId: generateId3(),
|
|
1153
|
+
toolName: toolCall.toolName,
|
|
1154
|
+
input: JSON.stringify(parsed)
|
|
1155
|
+
});
|
|
1156
|
+
} catch (error) {
|
|
1157
|
+
const originalCallText = text.substring(
|
|
1158
|
+
toolCall.startIndex,
|
|
1159
|
+
toolCall.endIndex
|
|
1160
|
+
);
|
|
1161
|
+
const message = `Could not process XML tool call, keeping original text: ${originalCallText}`;
|
|
1162
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, message, {
|
|
1163
|
+
toolCall: originalCallText,
|
|
1164
|
+
toolName: toolCall.toolName,
|
|
1165
|
+
error
|
|
1166
|
+
});
|
|
1167
|
+
processedElements.push({ type: "text", text: originalCallText });
|
|
1168
|
+
}
|
|
1169
|
+
currentIndex = toolCall.endIndex;
|
|
1170
|
+
}
|
|
1171
|
+
if (currentIndex < text.length) {
|
|
1172
|
+
const remainingText = text.substring(currentIndex);
|
|
1173
|
+
if (remainingText.trim()) {
|
|
1174
|
+
processedElements.push({ type: "text", text: remainingText });
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
return processedElements;
|
|
1178
|
+
},
|
|
1179
|
+
createStreamParser({ tools, options }) {
|
|
1180
|
+
const toolNames = tools.map((t) => t.name).filter((name) => name != null);
|
|
1181
|
+
const maxStartTagLen = toolNames.length ? Math.max(...toolNames.map((n) => `<${n}>`.length)) : 0;
|
|
1182
|
+
let buffer = "";
|
|
1183
|
+
let currentToolCall = null;
|
|
1184
|
+
let currentTextId = null;
|
|
1185
|
+
const flushText = (controller, text) => {
|
|
1186
|
+
const content = text != null ? text : buffer;
|
|
1187
|
+
if (content) {
|
|
1188
|
+
if (!currentTextId) {
|
|
1189
|
+
currentTextId = generateId3();
|
|
1190
|
+
controller.enqueue({ type: "text-start", id: currentTextId });
|
|
1191
|
+
}
|
|
1192
|
+
controller.enqueue({
|
|
1193
|
+
type: "text-delta",
|
|
1194
|
+
id: currentTextId,
|
|
1195
|
+
delta: content
|
|
1196
|
+
});
|
|
1197
|
+
if (text === void 0) {
|
|
1198
|
+
buffer = "";
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
if (currentTextId && !text) {
|
|
1202
|
+
controller.enqueue({ type: "text-end", id: currentTextId });
|
|
1203
|
+
currentTextId = null;
|
|
1204
|
+
}
|
|
1205
|
+
};
|
|
1206
|
+
return new TransformStream({
|
|
1207
|
+
transform(chunk, controller) {
|
|
1208
|
+
var _a;
|
|
1209
|
+
if (chunk.type !== "text-delta") {
|
|
1210
|
+
if (buffer) flushText(controller);
|
|
1211
|
+
controller.enqueue(chunk);
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
buffer += chunk.delta;
|
|
1215
|
+
while (true) {
|
|
1216
|
+
if (currentToolCall) {
|
|
1217
|
+
const endTag = `</${currentToolCall.name}>`;
|
|
1218
|
+
const endTagIndex = buffer.indexOf(endTag);
|
|
1219
|
+
if (endTagIndex !== -1) {
|
|
1220
|
+
const toolContent = buffer.substring(0, endTagIndex);
|
|
1221
|
+
buffer = buffer.substring(endTagIndex + endTag.length);
|
|
1222
|
+
try {
|
|
1223
|
+
const toolSchema = getToolSchema(tools, currentToolCall.name);
|
|
1224
|
+
const parsed = RXML.parse(toolContent, toolSchema, {
|
|
1225
|
+
onError: options == null ? void 0 : options.onError,
|
|
1226
|
+
// Disable HTML self-closing tag behavior to allow base, meta, link etc. as regular tags
|
|
1227
|
+
noChildNodes: []
|
|
1228
|
+
});
|
|
1229
|
+
flushText(controller);
|
|
1230
|
+
controller.enqueue({
|
|
1231
|
+
type: "tool-call",
|
|
1232
|
+
toolCallId: generateId3(),
|
|
1233
|
+
toolName: currentToolCall.name,
|
|
1234
|
+
input: JSON.stringify(parsed)
|
|
1235
|
+
});
|
|
1236
|
+
} catch (error) {
|
|
1237
|
+
const originalCallText = `<${currentToolCall.name}>${toolContent}${endTag}`;
|
|
1238
|
+
let message = "Could not process streaming XML tool call; emitting original text.";
|
|
1239
|
+
if (error instanceof RXML.RXMLDuplicateStringTagError) {
|
|
1240
|
+
message = `Duplicate string tags detected in streaming tool call '${currentToolCall.name}'; emitting original text.`;
|
|
1241
|
+
} else if (error instanceof RXML.RXMLCoercionError) {
|
|
1242
|
+
message = `Failed to coerce arguments for streaming tool call '${currentToolCall.name}'; emitting original text.`;
|
|
1243
|
+
} else if (error instanceof RXML.RXMLParseError) {
|
|
1244
|
+
message = `Failed to parse XML for streaming tool call '${currentToolCall.name}'; emitting original text.`;
|
|
1245
|
+
}
|
|
1246
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, message, {
|
|
1247
|
+
toolCall: originalCallText,
|
|
1248
|
+
toolName: currentToolCall.name,
|
|
1249
|
+
error
|
|
1250
|
+
});
|
|
1251
|
+
flushText(controller, originalCallText);
|
|
1252
|
+
}
|
|
1253
|
+
currentToolCall = null;
|
|
1254
|
+
} else {
|
|
1255
|
+
break;
|
|
1256
|
+
}
|
|
1257
|
+
} else {
|
|
1258
|
+
let earliestStartTagIndex = -1;
|
|
1259
|
+
let earliestToolName = "";
|
|
1260
|
+
if (toolNames.length > 0) {
|
|
1261
|
+
for (const name of toolNames) {
|
|
1262
|
+
const startTag = `<${name}>`;
|
|
1263
|
+
const index = buffer.indexOf(startTag);
|
|
1264
|
+
if (index !== -1 && (earliestStartTagIndex === -1 || index < earliestStartTagIndex)) {
|
|
1265
|
+
earliestStartTagIndex = index;
|
|
1266
|
+
earliestToolName = name;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
if (earliestStartTagIndex !== -1) {
|
|
1271
|
+
const textBeforeTag = buffer.substring(0, earliestStartTagIndex);
|
|
1272
|
+
flushText(controller, textBeforeTag);
|
|
1273
|
+
const startTag = `<${earliestToolName}>`;
|
|
1274
|
+
buffer = buffer.substring(
|
|
1275
|
+
earliestStartTagIndex + startTag.length
|
|
1276
|
+
);
|
|
1277
|
+
currentToolCall = { name: earliestToolName, content: "" };
|
|
1278
|
+
} else {
|
|
1279
|
+
const tail = Math.max(0, maxStartTagLen - 1);
|
|
1280
|
+
const safeLen = Math.max(0, buffer.length - tail);
|
|
1281
|
+
if (safeLen > 0) {
|
|
1282
|
+
const textToFlush = buffer.slice(0, safeLen);
|
|
1283
|
+
flushText(controller, textToFlush);
|
|
1284
|
+
buffer = buffer.slice(safeLen);
|
|
1285
|
+
continue;
|
|
1286
|
+
}
|
|
1287
|
+
break;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
},
|
|
1292
|
+
flush(controller) {
|
|
1293
|
+
if (currentToolCall) {
|
|
1294
|
+
const unfinishedCall = `<${currentToolCall.name}>${buffer}`;
|
|
1295
|
+
flushText(controller, unfinishedCall);
|
|
1296
|
+
} else if (buffer) {
|
|
1297
|
+
flushText(controller);
|
|
1298
|
+
}
|
|
1299
|
+
if (currentTextId) {
|
|
1300
|
+
controller.enqueue({ type: "text-end", id: currentTextId });
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
});
|
|
1304
|
+
},
|
|
1305
|
+
extractToolCallSegments({ text, tools }) {
|
|
1306
|
+
const toolNames = tools.map((t) => t.name).filter(Boolean);
|
|
1307
|
+
if (toolNames.length === 0) return [];
|
|
1308
|
+
return findToolCalls(text, toolNames).map((tc) => tc.segment);
|
|
1309
|
+
}
|
|
1310
|
+
});
|
|
1311
|
+
function getToolSchema(tools, toolName) {
|
|
1312
|
+
var _a;
|
|
1313
|
+
return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
1314
|
+
}
|
|
1315
|
+
function findToolCalls(text, toolNames) {
|
|
1316
|
+
var _a;
|
|
1317
|
+
const toolCalls = [];
|
|
1318
|
+
for (const toolName of toolNames) {
|
|
1319
|
+
let searchIndex = 0;
|
|
1320
|
+
while (searchIndex < text.length) {
|
|
1321
|
+
const startTag = `<${toolName}>`;
|
|
1322
|
+
const tagStart = text.indexOf(startTag, searchIndex);
|
|
1323
|
+
if (tagStart === -1) break;
|
|
1324
|
+
const remainingText = text.substring(tagStart);
|
|
1325
|
+
const range = RXML.findFirstTopLevelRange(remainingText, toolName);
|
|
1326
|
+
if (range) {
|
|
1327
|
+
const contentStart = tagStart + startTag.length;
|
|
1328
|
+
const contentEnd = contentStart + (range.end - range.start);
|
|
1329
|
+
let fullTagEnd = contentEnd + `</${toolName}>`.length;
|
|
1330
|
+
const closeHead = text.indexOf(`</${toolName}`, contentEnd);
|
|
1331
|
+
if (closeHead === contentEnd) {
|
|
1332
|
+
let p = closeHead + 2 + toolName.length;
|
|
1333
|
+
while (p < text.length && /\s/.test(text[p])) p++;
|
|
1334
|
+
if (text[p] === ">") fullTagEnd = p + 1;
|
|
1335
|
+
}
|
|
1336
|
+
const segment = text.substring(tagStart, fullTagEnd);
|
|
1337
|
+
const content = (_a = RXML.extractRawInner(segment, toolName)) != null ? _a : text.substring(contentStart, contentEnd);
|
|
1338
|
+
toolCalls.push({
|
|
1339
|
+
toolName,
|
|
1340
|
+
startIndex: tagStart,
|
|
1341
|
+
endIndex: fullTagEnd,
|
|
1342
|
+
content,
|
|
1343
|
+
segment
|
|
1344
|
+
});
|
|
1345
|
+
searchIndex = fullTagEnd;
|
|
1346
|
+
} else {
|
|
1347
|
+
searchIndex = tagStart + startTag.length;
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
return toolCalls.sort((a, b) => a.startIndex - b.startIndex);
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
// src/protocols/tool-call-protocol.ts
|
|
1355
|
+
function isProtocolFactory(protocol) {
|
|
1356
|
+
return typeof protocol === "function";
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
// src/generate-handler.ts
|
|
1360
|
+
import { generateId as generateId4 } from "@ai-sdk/provider-utils";
|
|
1361
|
+
import * as RXML2 from "@ai-sdk-tool/rxml";
|
|
1362
|
+
async function wrapGenerate({
|
|
1363
|
+
protocol,
|
|
1364
|
+
doGenerate,
|
|
1365
|
+
params
|
|
1366
|
+
}) {
|
|
1367
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1368
|
+
if (isToolChoiceActive(params)) {
|
|
1369
|
+
const result2 = await doGenerate();
|
|
1370
|
+
let parsed2 = {};
|
|
1371
|
+
const first = (_a = result2.content) == null ? void 0 : _a[0];
|
|
1372
|
+
if (first && first.type === "text") {
|
|
1373
|
+
const debugLevel2 = getDebugLevel();
|
|
1374
|
+
if (debugLevel2 === "parse") {
|
|
1375
|
+
logRawChunk(first.text);
|
|
1376
|
+
}
|
|
1377
|
+
try {
|
|
1378
|
+
parsed2 = JSON.parse(first.text);
|
|
1379
|
+
} catch (error) {
|
|
1380
|
+
const options = extractOnErrorOption(params.providerOptions);
|
|
1381
|
+
(_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
|
|
1382
|
+
options,
|
|
1383
|
+
"Failed to parse toolChoice JSON from generated model output",
|
|
1384
|
+
{
|
|
1385
|
+
text: first.text,
|
|
1386
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1387
|
+
}
|
|
1388
|
+
);
|
|
1389
|
+
parsed2 = {};
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
const toolCall = {
|
|
1393
|
+
type: "tool-call",
|
|
1394
|
+
toolCallId: generateId4(),
|
|
1395
|
+
toolName: parsed2.name || "unknown",
|
|
1396
|
+
input: JSON.stringify(parsed2.arguments || {})
|
|
1397
|
+
};
|
|
1398
|
+
const debugLevelToolChoice = getDebugLevel();
|
|
1399
|
+
const originText = first && first.type === "text" ? first.text : "";
|
|
1400
|
+
const dbg2 = (_d = (_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware) == null ? void 0 : _d.debugSummary;
|
|
1401
|
+
if (dbg2) {
|
|
1402
|
+
dbg2.originalText = originText;
|
|
1403
|
+
try {
|
|
1404
|
+
dbg2.toolCalls = JSON.stringify([
|
|
1405
|
+
{ toolName: toolCall.toolName, input: toolCall.input }
|
|
1406
|
+
]);
|
|
1407
|
+
} catch (e) {
|
|
1408
|
+
}
|
|
1409
|
+
} else if (debugLevelToolChoice === "parse") {
|
|
1410
|
+
logParsedSummary({ toolCalls: [toolCall], originalText: originText });
|
|
1411
|
+
}
|
|
1412
|
+
return {
|
|
1413
|
+
...result2,
|
|
1414
|
+
content: [toolCall]
|
|
1415
|
+
};
|
|
1416
|
+
}
|
|
1417
|
+
const tools = originalToolsSchema.decode(
|
|
1418
|
+
(_f = (_e = params.providerOptions) == null ? void 0 : _e.toolCallMiddleware) == null ? void 0 : _f.originalTools
|
|
1419
|
+
);
|
|
1420
|
+
const result = await doGenerate();
|
|
1421
|
+
if (result.content.length === 0) {
|
|
1422
|
+
return result;
|
|
1423
|
+
}
|
|
1424
|
+
const parsed = result.content.flatMap((contentItem) => {
|
|
1425
|
+
var _a2;
|
|
1426
|
+
if (contentItem.type !== "text") {
|
|
1427
|
+
return [contentItem];
|
|
1428
|
+
}
|
|
1429
|
+
const debugLevel2 = getDebugLevel();
|
|
1430
|
+
if (debugLevel2 === "stream") {
|
|
1431
|
+
logRawChunk(contentItem.text);
|
|
1432
|
+
}
|
|
1433
|
+
return protocol.parseGeneratedText({
|
|
1434
|
+
text: contentItem.text,
|
|
1435
|
+
tools,
|
|
1436
|
+
options: {
|
|
1437
|
+
...extractOnErrorOption(params.providerOptions),
|
|
1438
|
+
...(_a2 = params.providerOptions) == null ? void 0 : _a2.toolCallMiddleware
|
|
1439
|
+
}
|
|
1440
|
+
});
|
|
1441
|
+
});
|
|
1442
|
+
const newContent = parsed.map(
|
|
1443
|
+
(part) => fixToolCallWithSchema(part, tools)
|
|
1444
|
+
);
|
|
1445
|
+
const debugLevel = getDebugLevel();
|
|
1446
|
+
if (debugLevel === "stream") {
|
|
1447
|
+
newContent.forEach((part) => logParsedChunk(part));
|
|
1448
|
+
}
|
|
1449
|
+
const allText = result.content.filter(
|
|
1450
|
+
(c) => c.type === "text"
|
|
1451
|
+
).map((c) => c.text).join("\n\n");
|
|
1452
|
+
const segments = protocol.extractToolCallSegments ? protocol.extractToolCallSegments({ text: allText, tools }) : [];
|
|
1453
|
+
const originalText = segments.join("\n\n");
|
|
1454
|
+
const toolCalls = newContent.filter(
|
|
1455
|
+
(p) => p.type === "tool-call"
|
|
1456
|
+
);
|
|
1457
|
+
const dbg = (_h = (_g = params.providerOptions) == null ? void 0 : _g.toolCallMiddleware) == null ? void 0 : _h.debugSummary;
|
|
1458
|
+
if (dbg) {
|
|
1459
|
+
dbg.originalText = originalText;
|
|
1460
|
+
try {
|
|
1461
|
+
dbg.toolCalls = JSON.stringify(
|
|
1462
|
+
toolCalls.map((tc) => ({
|
|
1463
|
+
toolName: tc.toolName,
|
|
1464
|
+
input: tc.input
|
|
1465
|
+
}))
|
|
1466
|
+
);
|
|
1467
|
+
} catch (e) {
|
|
1468
|
+
}
|
|
1469
|
+
} else if (debugLevel === "parse") {
|
|
1470
|
+
logParsedSummary({ toolCalls, originalText });
|
|
1471
|
+
}
|
|
1472
|
+
return {
|
|
1473
|
+
...result,
|
|
1474
|
+
content: newContent
|
|
1475
|
+
};
|
|
1476
|
+
}
|
|
1477
|
+
function fixToolCallWithSchema(part, tools) {
|
|
1478
|
+
var _a;
|
|
1479
|
+
if (part.type !== "tool-call") return part;
|
|
1480
|
+
const tc = part;
|
|
1481
|
+
let args = {};
|
|
1482
|
+
if (typeof tc.input === "string") {
|
|
1483
|
+
try {
|
|
1484
|
+
args = JSON.parse(tc.input);
|
|
1485
|
+
} catch (e) {
|
|
1486
|
+
return part;
|
|
1487
|
+
}
|
|
1488
|
+
} else if (tc.input && typeof tc.input === "object") {
|
|
1489
|
+
args = tc.input;
|
|
1490
|
+
}
|
|
1491
|
+
const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
|
|
1492
|
+
const coerced = RXML2.coerceBySchema(args, schema);
|
|
1493
|
+
return {
|
|
1494
|
+
...part,
|
|
1495
|
+
input: JSON.stringify(coerced != null ? coerced : {})
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
// src/stream-handler.ts
|
|
1500
|
+
import { generateId as generateId5 } from "@ai-sdk/provider-utils";
|
|
1501
|
+
async function wrapStream({
|
|
1502
|
+
protocol,
|
|
1503
|
+
doStream,
|
|
1504
|
+
doGenerate,
|
|
1505
|
+
params
|
|
1506
|
+
}) {
|
|
1507
|
+
var _a, _b, _c;
|
|
1508
|
+
if (isToolChoiceActive(params)) {
|
|
1509
|
+
return toolChoiceStream({
|
|
1510
|
+
doGenerate,
|
|
1511
|
+
options: extractOnErrorOption(params.providerOptions)
|
|
1512
|
+
});
|
|
1513
|
+
}
|
|
1514
|
+
const { stream, ...rest } = await doStream();
|
|
1515
|
+
const debugLevel = getDebugLevel();
|
|
1516
|
+
const tools = originalToolsSchema.decode(
|
|
1517
|
+
(_b = (_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware) == null ? void 0 : _b.originalTools
|
|
1518
|
+
);
|
|
1519
|
+
const options = {
|
|
1520
|
+
...extractOnErrorOption(params.providerOptions),
|
|
1521
|
+
...(_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware
|
|
1522
|
+
};
|
|
1523
|
+
if (debugLevel === "off") {
|
|
1524
|
+
return {
|
|
1525
|
+
stream: stream.pipeThrough(
|
|
1526
|
+
protocol.createStreamParser({
|
|
1527
|
+
tools,
|
|
1528
|
+
options
|
|
1529
|
+
})
|
|
1530
|
+
),
|
|
1531
|
+
...rest
|
|
1532
|
+
};
|
|
1533
|
+
}
|
|
1534
|
+
if (debugLevel === "stream") {
|
|
1535
|
+
const withRawTap2 = stream.pipeThrough(
|
|
1536
|
+
new TransformStream(
|
|
1537
|
+
{
|
|
1538
|
+
transform(part, controller) {
|
|
1539
|
+
logRawChunk(part);
|
|
1540
|
+
controller.enqueue(part);
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
)
|
|
1544
|
+
);
|
|
1545
|
+
const parsed2 = withRawTap2.pipeThrough(
|
|
1546
|
+
protocol.createStreamParser({
|
|
1547
|
+
tools,
|
|
1548
|
+
options
|
|
1549
|
+
})
|
|
1550
|
+
);
|
|
1551
|
+
const withParsedTap = parsed2.pipeThrough(
|
|
1552
|
+
new TransformStream(
|
|
1553
|
+
{
|
|
1554
|
+
transform(part, controller) {
|
|
1555
|
+
logParsedChunk(part);
|
|
1556
|
+
controller.enqueue(part);
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
)
|
|
1560
|
+
);
|
|
1561
|
+
return {
|
|
1562
|
+
stream: withParsedTap,
|
|
1563
|
+
...rest
|
|
1564
|
+
};
|
|
1565
|
+
}
|
|
1566
|
+
let fullRawText = "";
|
|
1567
|
+
const withRawTap = stream.pipeThrough(
|
|
1568
|
+
new TransformStream({
|
|
1569
|
+
transform(part, controller) {
|
|
1570
|
+
if (part.type === "text-delta") {
|
|
1571
|
+
const delta = part.delta;
|
|
1572
|
+
if (typeof delta === "string" && delta.length > 0) {
|
|
1573
|
+
fullRawText += delta;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
controller.enqueue(part);
|
|
1577
|
+
}
|
|
1578
|
+
})
|
|
1579
|
+
);
|
|
1580
|
+
const parsed = withRawTap.pipeThrough(
|
|
1581
|
+
protocol.createStreamParser({
|
|
1582
|
+
tools,
|
|
1583
|
+
options
|
|
1584
|
+
})
|
|
1585
|
+
);
|
|
1586
|
+
const withSummary = parsed.pipeThrough(
|
|
1587
|
+
new TransformStream({
|
|
1588
|
+
transform: /* @__PURE__ */ (() => {
|
|
1589
|
+
const parsedToolCalls = [];
|
|
1590
|
+
return (part, controller) => {
|
|
1591
|
+
var _a2, _b2;
|
|
1592
|
+
if (part.type === "tool-call") {
|
|
1593
|
+
parsedToolCalls.push(part);
|
|
1594
|
+
}
|
|
1595
|
+
if (part.type === "finish") {
|
|
1596
|
+
try {
|
|
1597
|
+
const segments = protocol.extractToolCallSegments ? protocol.extractToolCallSegments({
|
|
1598
|
+
text: fullRawText,
|
|
1599
|
+
tools
|
|
1600
|
+
}) : [];
|
|
1601
|
+
const origin = segments.join("\n\n");
|
|
1602
|
+
const dbg = (_b2 = (_a2 = params.providerOptions) == null ? void 0 : _a2.toolCallMiddleware) == null ? void 0 : _b2.debugSummary;
|
|
1603
|
+
if (dbg) {
|
|
1604
|
+
dbg.originalText = origin;
|
|
1605
|
+
try {
|
|
1606
|
+
const toolCallParts = parsedToolCalls.filter(
|
|
1607
|
+
(p) => p.type === "tool-call"
|
|
1608
|
+
);
|
|
1609
|
+
dbg.toolCalls = JSON.stringify(
|
|
1610
|
+
toolCallParts.map((tc) => ({
|
|
1611
|
+
toolName: tc.toolName,
|
|
1612
|
+
input: tc.input
|
|
1613
|
+
}))
|
|
1614
|
+
);
|
|
1615
|
+
} catch (e) {
|
|
1616
|
+
}
|
|
1617
|
+
} else {
|
|
1618
|
+
logParsedSummary({
|
|
1619
|
+
toolCalls: parsedToolCalls,
|
|
1620
|
+
originalText: origin
|
|
1621
|
+
});
|
|
1622
|
+
}
|
|
1623
|
+
} catch (e) {
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
controller.enqueue(part);
|
|
1627
|
+
};
|
|
1628
|
+
})()
|
|
1629
|
+
})
|
|
1630
|
+
);
|
|
1631
|
+
return {
|
|
1632
|
+
stream: withSummary,
|
|
1633
|
+
...rest
|
|
1634
|
+
};
|
|
1635
|
+
}
|
|
1636
|
+
async function toolChoiceStream({
|
|
1637
|
+
doGenerate,
|
|
1638
|
+
options
|
|
1639
|
+
}) {
|
|
1640
|
+
var _a, _b;
|
|
1641
|
+
const result = await doGenerate();
|
|
1642
|
+
let toolJson = {};
|
|
1643
|
+
if ((result == null ? void 0 : result.content) && result.content.length > 0 && ((_a = result.content[0]) == null ? void 0 : _a.type) === "text") {
|
|
1644
|
+
try {
|
|
1645
|
+
toolJson = JSON.parse(result.content[0].text);
|
|
1646
|
+
} catch (error) {
|
|
1647
|
+
(_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
|
|
1648
|
+
options,
|
|
1649
|
+
"Failed to parse toolChoice JSON from streamed model output",
|
|
1650
|
+
{
|
|
1651
|
+
text: result.content[0].text,
|
|
1652
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1653
|
+
}
|
|
1654
|
+
);
|
|
1655
|
+
toolJson = {};
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
const toolCallChunk = {
|
|
1659
|
+
type: "tool-call",
|
|
1660
|
+
toolCallId: generateId5(),
|
|
1661
|
+
toolName: toolJson.name || "unknown",
|
|
1662
|
+
input: JSON.stringify(toolJson.arguments || {})
|
|
1663
|
+
};
|
|
1664
|
+
const finishChunk = {
|
|
1665
|
+
type: "finish",
|
|
1666
|
+
usage: (result == null ? void 0 : result.usage) || // TODO: If possible, try to return a certain amount of LLM usage.
|
|
1667
|
+
{
|
|
1668
|
+
inputTokens: 0,
|
|
1669
|
+
outputTokens: 0,
|
|
1670
|
+
totalTokens: 0
|
|
1671
|
+
},
|
|
1672
|
+
finishReason: "tool-calls"
|
|
1673
|
+
};
|
|
1674
|
+
const stream = new ReadableStream({
|
|
1675
|
+
start(controller) {
|
|
1676
|
+
controller.enqueue(toolCallChunk);
|
|
1677
|
+
controller.enqueue(finishChunk);
|
|
1678
|
+
controller.close();
|
|
1679
|
+
}
|
|
1680
|
+
});
|
|
1681
|
+
const debugLevel = getDebugLevel();
|
|
1682
|
+
const firstText = (result == null ? void 0 : result.content) && result.content[0] && result.content[0].type === "text" && result.content[0].text || "";
|
|
1683
|
+
const streamWithSummary = debugLevel === "parse" ? stream.pipeThrough(
|
|
1684
|
+
new TransformStream({
|
|
1685
|
+
transform(part, controller) {
|
|
1686
|
+
if (part.type === "finish") {
|
|
1687
|
+
try {
|
|
1688
|
+
logParsedSummary({
|
|
1689
|
+
toolCalls: [toolCallChunk],
|
|
1690
|
+
originalText: typeof firstText === "string" ? firstText : ""
|
|
1691
|
+
});
|
|
1692
|
+
} catch (e) {
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
controller.enqueue(part);
|
|
1696
|
+
}
|
|
1697
|
+
})
|
|
1698
|
+
) : stream;
|
|
1699
|
+
return {
|
|
1700
|
+
request: (result == null ? void 0 : result.request) || {},
|
|
1701
|
+
response: (result == null ? void 0 : result.response) || {},
|
|
1702
|
+
stream: streamWithSummary
|
|
1703
|
+
};
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
// src/transform-handler.ts
|
|
1707
|
+
async function transformParams({
|
|
1708
|
+
params,
|
|
1709
|
+
protocol,
|
|
1710
|
+
toolSystemPromptTemplate
|
|
1711
|
+
}) {
|
|
1712
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1713
|
+
const resolvedProtocol = isProtocolFactory(protocol) ? protocol() : protocol;
|
|
1714
|
+
const functionTools = ((_a = params.tools) != null ? _a : []).filter(
|
|
1715
|
+
(t) => t.type === "function"
|
|
1716
|
+
);
|
|
1717
|
+
const systemPrompt = resolvedProtocol.formatTools({
|
|
1718
|
+
tools: functionTools,
|
|
1719
|
+
toolSystemPromptTemplate
|
|
1720
|
+
});
|
|
1721
|
+
const processedPrompt = convertToolPrompt(
|
|
1722
|
+
(_b = params.prompt) != null ? _b : [],
|
|
1723
|
+
resolvedProtocol,
|
|
1724
|
+
extractOnErrorOption(params.providerOptions)
|
|
1725
|
+
);
|
|
1726
|
+
const finalPrompt = ((_c = processedPrompt[0]) == null ? void 0 : _c.role) === "system" ? [
|
|
1727
|
+
{
|
|
1728
|
+
role: "system",
|
|
1729
|
+
content: systemPrompt + "\n\n" + processedPrompt[0].content
|
|
1730
|
+
},
|
|
1731
|
+
...processedPrompt.slice(1)
|
|
1732
|
+
] : [
|
|
1733
|
+
{
|
|
1734
|
+
role: "system",
|
|
1735
|
+
content: systemPrompt
|
|
1736
|
+
},
|
|
1737
|
+
...processedPrompt
|
|
1738
|
+
];
|
|
1739
|
+
const baseReturnParams = {
|
|
1740
|
+
...params,
|
|
1741
|
+
prompt: finalPrompt,
|
|
1742
|
+
tools: [],
|
|
1743
|
+
toolChoice: void 0,
|
|
1744
|
+
providerOptions: {
|
|
1745
|
+
...params.providerOptions || {},
|
|
1746
|
+
toolCallMiddleware: {
|
|
1747
|
+
...params.providerOptions && typeof params.providerOptions === "object" && params.providerOptions.toolCallMiddleware || {},
|
|
1748
|
+
// INTERNAL: used by the middleware so downstream parsers can access
|
|
1749
|
+
// the original tool schemas even if providers strip `params.tools`.
|
|
1750
|
+
// Not a stable public API.
|
|
1751
|
+
originalTools: originalToolsSchema.encode(functionTools)
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
};
|
|
1755
|
+
if (((_d = params.toolChoice) == null ? void 0 : _d.type) === "none") {
|
|
1756
|
+
throw new Error(
|
|
1757
|
+
"The 'none' toolChoice type is not supported by this middleware. Please use 'auto', 'required', or specify a tool name."
|
|
1758
|
+
);
|
|
1759
|
+
}
|
|
1760
|
+
if (((_e = params.toolChoice) == null ? void 0 : _e.type) === "tool") {
|
|
1761
|
+
const selectedToolName = params.toolChoice.toolName;
|
|
1762
|
+
const providerDefinedMatch = ((_f = params.tools) != null ? _f : []).find((t) => {
|
|
1763
|
+
if (t.type === "function") return false;
|
|
1764
|
+
const anyTool = t;
|
|
1765
|
+
return anyTool.id === selectedToolName || anyTool.name === selectedToolName;
|
|
1766
|
+
});
|
|
1767
|
+
if (providerDefinedMatch) {
|
|
1768
|
+
throw new Error(
|
|
1769
|
+
"Provider-defined tools are not supported by this middleware. Please use custom tools."
|
|
1770
|
+
);
|
|
1771
|
+
}
|
|
1772
|
+
const selectedTool = ((_g = params.tools) != null ? _g : []).find(
|
|
1773
|
+
(t) => t.type === "function" && t.name === selectedToolName
|
|
1774
|
+
);
|
|
1775
|
+
if (!selectedTool) {
|
|
1776
|
+
throw new Error(
|
|
1777
|
+
`Tool with name '${selectedToolName}' not found in params.tools.`
|
|
1778
|
+
);
|
|
1779
|
+
}
|
|
1780
|
+
return {
|
|
1781
|
+
...baseReturnParams,
|
|
1782
|
+
responseFormat: {
|
|
1783
|
+
type: "json",
|
|
1784
|
+
schema: {
|
|
1785
|
+
type: "object",
|
|
1786
|
+
properties: {
|
|
1787
|
+
name: {
|
|
1788
|
+
const: selectedTool.name
|
|
1789
|
+
},
|
|
1790
|
+
arguments: selectedTool.inputSchema
|
|
1791
|
+
},
|
|
1792
|
+
required: ["name", "arguments"]
|
|
1793
|
+
},
|
|
1794
|
+
name: selectedTool.name,
|
|
1795
|
+
description: typeof selectedTool.description === "string" ? selectedTool.description : void 0
|
|
1796
|
+
},
|
|
1797
|
+
providerOptions: {
|
|
1798
|
+
...baseReturnParams.providerOptions || {},
|
|
1799
|
+
toolCallMiddleware: {
|
|
1800
|
+
...baseReturnParams.providerOptions && typeof baseReturnParams.providerOptions === "object" && baseReturnParams.providerOptions.toolCallMiddleware || {},
|
|
1801
|
+
// INTERNAL: used by the middleware to activate the tool-choice
|
|
1802
|
+
// fast-path in handlers. Not a stable public API.
|
|
1803
|
+
toolChoice: params.toolChoice
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
};
|
|
1807
|
+
}
|
|
1808
|
+
if (((_h = params.toolChoice) == null ? void 0 : _h.type) === "required") {
|
|
1809
|
+
if (!params.tools || params.tools.length === 0) {
|
|
1810
|
+
throw new Error(
|
|
1811
|
+
"Tool choice type 'required' is set, but no tools are provided in params.tools."
|
|
1812
|
+
);
|
|
1813
|
+
}
|
|
1814
|
+
return {
|
|
1815
|
+
...baseReturnParams,
|
|
1816
|
+
responseFormat: {
|
|
1817
|
+
type: "json",
|
|
1818
|
+
schema: createDynamicIfThenElseSchema(functionTools)
|
|
1819
|
+
},
|
|
1820
|
+
providerOptions: {
|
|
1821
|
+
...baseReturnParams.providerOptions || {},
|
|
1822
|
+
toolCallMiddleware: {
|
|
1823
|
+
...baseReturnParams.providerOptions && typeof baseReturnParams.providerOptions === "object" && baseReturnParams.providerOptions.toolCallMiddleware || {},
|
|
1824
|
+
// INTERNAL: used by the middleware to activate the tool-choice
|
|
1825
|
+
// fast-path in handlers. Not a stable public API.
|
|
1826
|
+
toolChoice: { type: "required" }
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
};
|
|
1830
|
+
}
|
|
1831
|
+
return baseReturnParams;
|
|
1832
|
+
}
|
|
1833
|
+
function convertToolPrompt(prompt, resolvedProtocol, providerOptions) {
|
|
1834
|
+
const processedPrompt = prompt.map((message) => {
|
|
1835
|
+
var _a;
|
|
1836
|
+
if (message.role === "assistant") {
|
|
1837
|
+
const newContent = [];
|
|
1838
|
+
for (const content of message.content) {
|
|
1839
|
+
if (isToolCallContent(content)) {
|
|
1840
|
+
newContent.push({
|
|
1841
|
+
type: "text",
|
|
1842
|
+
text: resolvedProtocol.formatToolCall(content)
|
|
1843
|
+
});
|
|
1844
|
+
} else if (content.type === "text") {
|
|
1845
|
+
newContent.push(content);
|
|
1846
|
+
} else if (content.type === "reasoning") {
|
|
1847
|
+
newContent.push(content);
|
|
1848
|
+
} else {
|
|
1849
|
+
const options = extractOnErrorOption(providerOptions);
|
|
1850
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
1851
|
+
options,
|
|
1852
|
+
"tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
|
|
1853
|
+
{ content }
|
|
1854
|
+
);
|
|
1855
|
+
newContent.push({
|
|
1856
|
+
type: "text",
|
|
1857
|
+
text: JSON.stringify(content)
|
|
1858
|
+
});
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
const onlyText = newContent.every((c) => c.type === "text");
|
|
1862
|
+
const condensedAssistant = onlyText ? [
|
|
1863
|
+
{
|
|
1864
|
+
type: "text",
|
|
1865
|
+
text: newContent.map((c) => c.text).join("\n")
|
|
1866
|
+
}
|
|
1867
|
+
] : newContent;
|
|
1868
|
+
return { role: "assistant", content: condensedAssistant };
|
|
1869
|
+
}
|
|
1870
|
+
if (message.role === "tool") {
|
|
1871
|
+
return {
|
|
1872
|
+
role: "user",
|
|
1873
|
+
// Map tool results to text response blocks, then condense into a single text block
|
|
1874
|
+
content: [
|
|
1875
|
+
{
|
|
1876
|
+
type: "text",
|
|
1877
|
+
text: message.content.map(
|
|
1878
|
+
(toolResult) => isToolResultPart(toolResult) ? resolvedProtocol.formatToolResponse(toolResult) : resolvedProtocol.formatToolResponse(
|
|
1879
|
+
toolResult
|
|
1880
|
+
)
|
|
1881
|
+
).join("\n")
|
|
1882
|
+
}
|
|
1883
|
+
]
|
|
1884
|
+
};
|
|
1885
|
+
}
|
|
1886
|
+
return message;
|
|
1887
|
+
});
|
|
1888
|
+
for (let i = 0; i < processedPrompt.length; i++) {
|
|
1889
|
+
const msg = processedPrompt[i];
|
|
1890
|
+
if (Array.isArray(msg.content)) {
|
|
1891
|
+
const allText = msg.content.every(
|
|
1892
|
+
(c) => (c == null ? void 0 : c.type) === "text"
|
|
1893
|
+
);
|
|
1894
|
+
if (allText && msg.content.length > 1) {
|
|
1895
|
+
const joinedText = msg.content.map((c) => c.text).join("\n");
|
|
1896
|
+
if (msg.role === "system") {
|
|
1897
|
+
processedPrompt[i] = {
|
|
1898
|
+
role: "system",
|
|
1899
|
+
content: joinedText
|
|
1900
|
+
};
|
|
1901
|
+
} else if (msg.role === "assistant") {
|
|
1902
|
+
processedPrompt[i] = {
|
|
1903
|
+
role: "assistant",
|
|
1904
|
+
content: [
|
|
1905
|
+
{
|
|
1906
|
+
type: "text",
|
|
1907
|
+
text: joinedText
|
|
1908
|
+
}
|
|
1909
|
+
]
|
|
1910
|
+
};
|
|
1911
|
+
} else {
|
|
1912
|
+
processedPrompt[i] = {
|
|
1913
|
+
role: "user",
|
|
1914
|
+
content: [
|
|
1915
|
+
{
|
|
1916
|
+
type: "text",
|
|
1917
|
+
text: joinedText
|
|
1918
|
+
}
|
|
1919
|
+
]
|
|
1920
|
+
};
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
for (let i = processedPrompt.length - 1; i > 0; i--) {
|
|
1926
|
+
const current = processedPrompt[i];
|
|
1927
|
+
const prev = processedPrompt[i - 1];
|
|
1928
|
+
if (current.role === "user" && prev.role === "user") {
|
|
1929
|
+
const prevContent = prev.content.map((c) => c.type === "text" ? c.text : "").join("\n");
|
|
1930
|
+
const currentContent = current.content.map((c) => c.type === "text" ? c.text : "").join("\n");
|
|
1931
|
+
processedPrompt[i - 1] = {
|
|
1932
|
+
role: "user",
|
|
1933
|
+
content: [{ type: "text", text: prevContent + "\n" + currentContent }]
|
|
1934
|
+
};
|
|
1935
|
+
processedPrompt.splice(i, 1);
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
return processedPrompt;
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
// src/tool-call-middleware.ts
|
|
1942
|
+
function createToolMiddleware({
|
|
1943
|
+
protocol,
|
|
1944
|
+
toolSystemPromptTemplate
|
|
1945
|
+
}) {
|
|
1946
|
+
const resolvedProtocol = isProtocolFactory(protocol) ? protocol() : protocol;
|
|
1947
|
+
return {
|
|
1948
|
+
middlewareVersion: "v2",
|
|
1949
|
+
wrapStream: async ({ doStream, doGenerate, params }) => {
|
|
1950
|
+
if (isToolChoiceActive(params)) {
|
|
1951
|
+
return toolChoiceStream({
|
|
1952
|
+
doGenerate,
|
|
1953
|
+
options: extractOnErrorOption(params.providerOptions)
|
|
1954
|
+
});
|
|
1955
|
+
} else {
|
|
1956
|
+
return wrapStream({
|
|
1957
|
+
protocol: resolvedProtocol,
|
|
1958
|
+
doStream,
|
|
1959
|
+
doGenerate,
|
|
1960
|
+
params
|
|
1961
|
+
});
|
|
1962
|
+
}
|
|
1963
|
+
},
|
|
1964
|
+
wrapGenerate: async ({ doGenerate, params }) => wrapGenerate({
|
|
1965
|
+
protocol: resolvedProtocol,
|
|
1966
|
+
doGenerate,
|
|
1967
|
+
params
|
|
1968
|
+
}),
|
|
1969
|
+
transformParams: async ({
|
|
1970
|
+
params
|
|
1971
|
+
}) => {
|
|
1972
|
+
return transformParams({
|
|
1973
|
+
protocol: resolvedProtocol,
|
|
1974
|
+
toolSystemPromptTemplate,
|
|
1975
|
+
params
|
|
1976
|
+
});
|
|
1977
|
+
}
|
|
1978
|
+
};
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
// src/index.ts
|
|
1982
|
+
var gemmaToolMiddleware = createToolMiddleware({
|
|
1983
|
+
protocol: jsonMixProtocol(
|
|
1984
|
+
// Customize the tool call delimiters to use markdown code fences
|
|
1985
|
+
{
|
|
1986
|
+
toolCallStart: "```tool_call\n",
|
|
1987
|
+
// TODO: Support specifying multiple possible tags,
|
|
1988
|
+
// e.g., for gemma, it would be nice to be able to set both `` and ``` at the same time.
|
|
1989
|
+
toolCallEnd: "\n```",
|
|
1990
|
+
toolResponseStart: "```tool_response\n",
|
|
1991
|
+
toolResponseEnd: "\n```"
|
|
1992
|
+
}
|
|
1993
|
+
),
|
|
1994
|
+
toolSystemPromptTemplate(tools) {
|
|
1995
|
+
return `You have access to functions. If you decide to invoke any of the function(s),
|
|
1996
|
+
you MUST put it in the format of markdown code fence block with the language name of tool_call , e.g.
|
|
1997
|
+
\`\`\`tool_call
|
|
1998
|
+
{'name': <function-name>, 'arguments': <args-dict>}
|
|
1999
|
+
\`\`\`
|
|
2000
|
+
You SHOULD NOT include any other text in the response if you call a function
|
|
2001
|
+
${tools}`;
|
|
2002
|
+
}
|
|
2003
|
+
});
|
|
2004
|
+
var hermesToolMiddleware = createToolMiddleware({
|
|
2005
|
+
protocol: jsonMixProtocol,
|
|
2006
|
+
toolSystemPromptTemplate(tools) {
|
|
2007
|
+
return `You are a function calling AI model.
|
|
2008
|
+
You are provided with function signatures within <tools></tools> XML tags.
|
|
2009
|
+
You may call one or more functions to assist with the user query.
|
|
2010
|
+
Don't make assumptions about what values to plug into functions.
|
|
2011
|
+
Here are the available tools: <tools>${tools}</tools>
|
|
2012
|
+
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"]}
|
|
2013
|
+
For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
|
|
2014
|
+
<tool_call>
|
|
2015
|
+
{"name": "<function-name>", "arguments": <args-dict>}
|
|
2016
|
+
</tool_call>`;
|
|
2017
|
+
}
|
|
2018
|
+
});
|
|
2019
|
+
var morphXmlToolMiddleware = createToolMiddleware({
|
|
2020
|
+
protocol: morphXmlProtocol,
|
|
2021
|
+
toolSystemPromptTemplate(tools) {
|
|
2022
|
+
return `You are a function calling AI model.
|
|
2023
|
+
|
|
2024
|
+
Available functions are listed inside <tools></tools>.
|
|
2025
|
+
<tools>${tools}</tools>
|
|
2026
|
+
|
|
2027
|
+
# Rules
|
|
2028
|
+
- Use exactly one XML element whose tag name is the function name.
|
|
2029
|
+
- Put each parameter as a child element.
|
|
2030
|
+
- Values must follow the schema exactly (numbers, arrays, objects, enums \u2192 copy as-is).
|
|
2031
|
+
- Do not add or remove functions or parameters.
|
|
2032
|
+
- Each required parameter must appear once.
|
|
2033
|
+
- Output nothing before or after the function call.
|
|
2034
|
+
|
|
2035
|
+
# Example
|
|
2036
|
+
<get_weather>
|
|
2037
|
+
<location>New York</location>
|
|
2038
|
+
<unit>celsius</unit>
|
|
2039
|
+
</get_weather>`;
|
|
2040
|
+
}
|
|
2041
|
+
});
|
|
2042
|
+
|
|
2043
|
+
export {
|
|
2044
|
+
createDynamicIfThenElseSchema,
|
|
2045
|
+
getPotentialStartIndex,
|
|
2046
|
+
escapeRegExp,
|
|
2047
|
+
robust_json_exports,
|
|
2048
|
+
getDebugLevel,
|
|
2049
|
+
logRawChunk,
|
|
2050
|
+
logParsedChunk,
|
|
2051
|
+
logParsedSummary,
|
|
2052
|
+
extractOnErrorOption,
|
|
2053
|
+
originalToolsSchema,
|
|
2054
|
+
encodeOriginalTools,
|
|
2055
|
+
decodeOriginalTools,
|
|
2056
|
+
extractToolNamesFromOriginalTools,
|
|
2057
|
+
isToolChoiceActive,
|
|
2058
|
+
isToolCallContent,
|
|
2059
|
+
isToolResultPart,
|
|
2060
|
+
hasInputProperty,
|
|
2061
|
+
jsonMixProtocol,
|
|
2062
|
+
morphXmlProtocol,
|
|
2063
|
+
createToolMiddleware,
|
|
2064
|
+
gemmaToolMiddleware,
|
|
2065
|
+
hermesToolMiddleware,
|
|
2066
|
+
morphXmlToolMiddleware
|
|
2067
|
+
};
|
|
2068
|
+
//# sourceMappingURL=chunk-GSD5HDOQ.js.map
|