@ai-sdk-tool/parser 3.1.3 → 3.2.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/dist/{chunk-3KQVEBKO.js → chunk-PIUBQRFC.js} +159 -705
- package/dist/chunk-PIUBQRFC.js.map +1 -0
- package/dist/community.cjs +160 -706
- package/dist/community.cjs.map +1 -1
- package/dist/community.js +1 -1
- package/dist/index.cjs +160 -715
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -64
- package/dist/index.d.ts +8 -64
- package/dist/index.js +1 -19
- package/package.json +4 -3
- package/dist/chunk-3KQVEBKO.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -31,17 +31,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
// src/index.ts
|
|
32
32
|
var src_exports = {};
|
|
33
33
|
__export(src_exports, {
|
|
34
|
-
applyHeuristicPipeline: () => applyHeuristicPipeline,
|
|
35
|
-
balanceTagsHeuristic: () => balanceTagsHeuristic,
|
|
36
34
|
createDynamicIfThenElseSchema: () => createDynamicIfThenElseSchema,
|
|
37
|
-
createIntermediateCall: () => createIntermediateCall,
|
|
38
35
|
createToolMiddleware: () => createToolMiddleware,
|
|
39
36
|
decodeOriginalTools: () => decodeOriginalTools,
|
|
40
|
-
dedupeShellStringTagsHeuristic: () => dedupeShellStringTagsHeuristic,
|
|
41
|
-
defaultPipelineConfig: () => defaultPipelineConfig,
|
|
42
37
|
encodeOriginalTools: () => encodeOriginalTools,
|
|
43
|
-
|
|
44
|
-
escapeRegExp: () => escapeRegExp2,
|
|
38
|
+
escapeRegExp: () => escapeRegExp,
|
|
45
39
|
extractOnErrorOption: () => extractOnErrorOption,
|
|
46
40
|
extractToolNamesFromOriginalTools: () => extractToolNamesFromOriginalTools,
|
|
47
41
|
getDebugLevel: () => getDebugLevel,
|
|
@@ -57,10 +51,7 @@ __export(src_exports, {
|
|
|
57
51
|
logParsedChunk: () => logParsedChunk,
|
|
58
52
|
logParsedSummary: () => logParsedSummary,
|
|
59
53
|
logRawChunk: () => logRawChunk,
|
|
60
|
-
mergePipelineConfigs: () => mergePipelineConfigs,
|
|
61
|
-
normalizeCloseTagsHeuristic: () => normalizeCloseTagsHeuristic,
|
|
62
54
|
originalToolsSchema: () => originalToolsSchema,
|
|
63
|
-
repairAgainstSchemaHeuristic: () => repairAgainstSchemaHeuristic,
|
|
64
55
|
toolChoiceStream: () => toolChoiceStream,
|
|
65
56
|
transformParams: () => transformParams,
|
|
66
57
|
wrapGenerate: () => wrapGenerate,
|
|
@@ -73,473 +64,6 @@ __export(src_exports, {
|
|
|
73
64
|
module.exports = __toCommonJS(src_exports);
|
|
74
65
|
__reExport(src_exports, require("@ai-sdk-tool/rjson"), module.exports);
|
|
75
66
|
|
|
76
|
-
// src/core/heuristics/engine.ts
|
|
77
|
-
function applyRawSegmentUpdate(current, result) {
|
|
78
|
-
if (result.rawSegment !== void 0) {
|
|
79
|
-
return { ...current, rawSegment: result.rawSegment };
|
|
80
|
-
}
|
|
81
|
-
return current;
|
|
82
|
-
}
|
|
83
|
-
function applyParsedUpdate(current, result) {
|
|
84
|
-
if (result.parsed !== void 0) {
|
|
85
|
-
return { ...current, parsed: result.parsed };
|
|
86
|
-
}
|
|
87
|
-
return current;
|
|
88
|
-
}
|
|
89
|
-
function applyWarningsUpdate(current, result) {
|
|
90
|
-
var _a, _b;
|
|
91
|
-
if (result.warnings && result.warnings.length > 0) {
|
|
92
|
-
const meta = (_a = current.meta) != null ? _a : {};
|
|
93
|
-
const existingWarnings = (_b = meta.warnings) != null ? _b : [];
|
|
94
|
-
return {
|
|
95
|
-
...current,
|
|
96
|
-
meta: { ...meta, warnings: [...existingWarnings, ...result.warnings] }
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
return current;
|
|
100
|
-
}
|
|
101
|
-
function attemptReparse(current, result, reparseCount, maxReparses, parse3) {
|
|
102
|
-
if (!result.reparse || result.rawSegment === void 0 || reparseCount >= maxReparses) {
|
|
103
|
-
return { state: current, newCount: reparseCount };
|
|
104
|
-
}
|
|
105
|
-
try {
|
|
106
|
-
const reparsed = parse3(result.rawSegment, current.schema);
|
|
107
|
-
return {
|
|
108
|
-
state: { ...current, parsed: reparsed, errors: [] },
|
|
109
|
-
newCount: reparseCount + 1
|
|
110
|
-
};
|
|
111
|
-
} catch (error) {
|
|
112
|
-
return {
|
|
113
|
-
state: { ...current, errors: [...current.errors, error] },
|
|
114
|
-
newCount: reparseCount + 1
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function executePhase(ctx, heuristics, options) {
|
|
119
|
-
var _a;
|
|
120
|
-
let current = ctx;
|
|
121
|
-
let reparseCount = 0;
|
|
122
|
-
const maxReparses = (_a = options.maxReparses) != null ? _a : 2;
|
|
123
|
-
for (const heuristic of heuristics) {
|
|
124
|
-
if (!heuristic.applies(current)) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
const result = heuristic.run(current);
|
|
128
|
-
current = applyRawSegmentUpdate(current, result);
|
|
129
|
-
current = applyParsedUpdate(current, result);
|
|
130
|
-
current = applyWarningsUpdate(current, result);
|
|
131
|
-
const reparseResult = attemptReparse(
|
|
132
|
-
current,
|
|
133
|
-
result,
|
|
134
|
-
reparseCount,
|
|
135
|
-
maxReparses,
|
|
136
|
-
options.parse
|
|
137
|
-
);
|
|
138
|
-
current = reparseResult.state;
|
|
139
|
-
reparseCount = reparseResult.newCount;
|
|
140
|
-
if (result.stop) {
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return current;
|
|
145
|
-
}
|
|
146
|
-
function applyHeuristicPipeline(ctx, config, options) {
|
|
147
|
-
let current = ctx;
|
|
148
|
-
if (config.preParse && config.preParse.length > 0) {
|
|
149
|
-
current = executePhase(current, config.preParse, options);
|
|
150
|
-
}
|
|
151
|
-
if (current.parsed === null && current.errors.length === 0) {
|
|
152
|
-
try {
|
|
153
|
-
const parsed = options.parse(current.rawSegment, current.schema);
|
|
154
|
-
current = { ...current, parsed, errors: [] };
|
|
155
|
-
} catch (error) {
|
|
156
|
-
current = { ...current, errors: [error] };
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
if (current.errors.length > 0 && config.fallbackReparse && config.fallbackReparse.length > 0) {
|
|
160
|
-
current = executePhase(current, config.fallbackReparse, options);
|
|
161
|
-
}
|
|
162
|
-
if (current.parsed !== null && config.postParse && config.postParse.length > 0) {
|
|
163
|
-
current = executePhase(current, config.postParse, options);
|
|
164
|
-
}
|
|
165
|
-
return current;
|
|
166
|
-
}
|
|
167
|
-
function createIntermediateCall(toolName, rawSegment, schema) {
|
|
168
|
-
return {
|
|
169
|
-
toolName,
|
|
170
|
-
schema,
|
|
171
|
-
rawSegment,
|
|
172
|
-
parsed: null,
|
|
173
|
-
errors: [],
|
|
174
|
-
meta: { originalContent: rawSegment }
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
function mergePipelineConfigs(...configs) {
|
|
178
|
-
var _a, _b, _c;
|
|
179
|
-
const result = {
|
|
180
|
-
preParse: [],
|
|
181
|
-
fallbackReparse: [],
|
|
182
|
-
postParse: []
|
|
183
|
-
};
|
|
184
|
-
for (const config of configs) {
|
|
185
|
-
if (config.preParse) {
|
|
186
|
-
result.preParse = [...(_a = result.preParse) != null ? _a : [], ...config.preParse];
|
|
187
|
-
}
|
|
188
|
-
if (config.fallbackReparse) {
|
|
189
|
-
result.fallbackReparse = [
|
|
190
|
-
...(_b = result.fallbackReparse) != null ? _b : [],
|
|
191
|
-
...config.fallbackReparse
|
|
192
|
-
];
|
|
193
|
-
}
|
|
194
|
-
if (config.postParse) {
|
|
195
|
-
result.postParse = [...(_c = result.postParse) != null ? _c : [], ...config.postParse];
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return result;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// src/core/heuristics/xml-defaults.ts
|
|
202
|
-
var import_rxml = require("@ai-sdk-tool/rxml");
|
|
203
|
-
var MALFORMED_CLOSE_RE_G = /<\/\s+([A-Za-z0-9_:-]+)\s*>/g;
|
|
204
|
-
var MALFORMED_CLOSE_RE = /<\/\s+([A-Za-z0-9_:-]+)\s*>/;
|
|
205
|
-
var STATUS_TO_STEP_BOUNDARY_RE = /<\/status>\s*<step>/g;
|
|
206
|
-
var WHITESPACE_REGEX = /\s/;
|
|
207
|
-
var NAME_CHAR_RE = /[A-Za-z0-9_:-]/;
|
|
208
|
-
var NAME_START_CHAR_RE = /[A-Za-z_:]/;
|
|
209
|
-
var STEP_TAG_RE = /<step>([\s\S]*?)<\/step>/i;
|
|
210
|
-
var STATUS_TAG_RE = /<status>([\s\S]*?)<\/status>/i;
|
|
211
|
-
var normalizeCloseTagsHeuristic = {
|
|
212
|
-
id: "normalize-close-tags",
|
|
213
|
-
phase: "pre-parse",
|
|
214
|
-
applies: () => true,
|
|
215
|
-
run: (ctx) => {
|
|
216
|
-
const normalized = ctx.rawSegment.replace(MALFORMED_CLOSE_RE_G, "</$1>");
|
|
217
|
-
if (normalized !== ctx.rawSegment) {
|
|
218
|
-
return { rawSegment: normalized };
|
|
219
|
-
}
|
|
220
|
-
return {};
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
var escapeInvalidLtHeuristic = {
|
|
224
|
-
id: "escape-invalid-lt",
|
|
225
|
-
phase: "pre-parse",
|
|
226
|
-
applies: () => true,
|
|
227
|
-
run: (ctx) => {
|
|
228
|
-
const escaped = escapeInvalidLt(ctx.rawSegment);
|
|
229
|
-
if (escaped !== ctx.rawSegment) {
|
|
230
|
-
return { rawSegment: escaped };
|
|
231
|
-
}
|
|
232
|
-
return {};
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
var balanceTagsHeuristic = {
|
|
236
|
-
id: "balance-tags",
|
|
237
|
-
phase: "fallback-reparse",
|
|
238
|
-
applies: (ctx) => {
|
|
239
|
-
var _a;
|
|
240
|
-
const original = ((_a = ctx.meta) == null ? void 0 : _a.originalContent) || ctx.rawSegment;
|
|
241
|
-
const normalized = original.replace(MALFORMED_CLOSE_RE_G, "</$1>");
|
|
242
|
-
const balanced = balanceTags(original);
|
|
243
|
-
const hasMalformedClose = MALFORMED_CLOSE_RE.test(original);
|
|
244
|
-
if (!hasMalformedClose && balanced.length > normalized.length) {
|
|
245
|
-
return false;
|
|
246
|
-
}
|
|
247
|
-
return balanced !== normalized;
|
|
248
|
-
},
|
|
249
|
-
run: (ctx) => {
|
|
250
|
-
var _a;
|
|
251
|
-
const original = ((_a = ctx.meta) == null ? void 0 : _a.originalContent) || ctx.rawSegment;
|
|
252
|
-
const balanced = balanceTags(original);
|
|
253
|
-
const escaped = escapeInvalidLt(balanced);
|
|
254
|
-
return { rawSegment: escaped, reparse: true };
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
var dedupeShellStringTagsHeuristic = {
|
|
258
|
-
id: "dedupe-shell-string-tags",
|
|
259
|
-
phase: "fallback-reparse",
|
|
260
|
-
applies: (ctx) => shouldDeduplicateStringTags(ctx.schema),
|
|
261
|
-
run: (ctx) => {
|
|
262
|
-
const names = getStringPropertyNames(ctx.schema);
|
|
263
|
-
let deduped = ctx.rawSegment;
|
|
264
|
-
for (const key of names) {
|
|
265
|
-
deduped = dedupeSingleTag(deduped, key);
|
|
266
|
-
}
|
|
267
|
-
if (deduped !== ctx.rawSegment) {
|
|
268
|
-
return { rawSegment: deduped, reparse: true };
|
|
269
|
-
}
|
|
270
|
-
return {};
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
var repairAgainstSchemaHeuristic = {
|
|
274
|
-
id: "repair-against-schema",
|
|
275
|
-
phase: "post-parse",
|
|
276
|
-
applies: (ctx) => ctx.parsed !== null && typeof ctx.parsed === "object",
|
|
277
|
-
run: (ctx) => {
|
|
278
|
-
const repaired = repairParsedAgainstSchema(ctx.parsed, ctx.schema);
|
|
279
|
-
if (repaired !== ctx.parsed) {
|
|
280
|
-
return { parsed: repaired };
|
|
281
|
-
}
|
|
282
|
-
return {};
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
var defaultPipelineConfig = {
|
|
286
|
-
preParse: [normalizeCloseTagsHeuristic, escapeInvalidLtHeuristic],
|
|
287
|
-
fallbackReparse: [balanceTagsHeuristic, dedupeShellStringTagsHeuristic],
|
|
288
|
-
postParse: [repairAgainstSchemaHeuristic]
|
|
289
|
-
};
|
|
290
|
-
var INDEX_TAG_RE = /^<(\d+)(?:>|\/?>)/;
|
|
291
|
-
function isIndexTagAt(xml, pos) {
|
|
292
|
-
const remaining = xml.slice(pos);
|
|
293
|
-
return INDEX_TAG_RE.test(remaining);
|
|
294
|
-
}
|
|
295
|
-
function escapeInvalidLt(xml) {
|
|
296
|
-
const len = xml.length;
|
|
297
|
-
let out = "";
|
|
298
|
-
for (let i = 0; i < len; i += 1) {
|
|
299
|
-
const ch = xml[i];
|
|
300
|
-
if (ch === "<") {
|
|
301
|
-
const next = i + 1 < len ? xml[i + 1] : "";
|
|
302
|
-
const isValidStart = NAME_START_CHAR_RE.test(next) || next === "/" || next === "!" || next === "?";
|
|
303
|
-
const isIndexTag = !isValidStart && isIndexTagAt(xml, i);
|
|
304
|
-
if (!(isValidStart || isIndexTag)) {
|
|
305
|
-
out += "<";
|
|
306
|
-
continue;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
out += ch;
|
|
310
|
-
}
|
|
311
|
-
return out;
|
|
312
|
-
}
|
|
313
|
-
function balanceTags(xml) {
|
|
314
|
-
const src = xml.replace(MALFORMED_CLOSE_RE_G, "</$1>").replace(STATUS_TO_STEP_BOUNDARY_RE, "</status></step><step>");
|
|
315
|
-
let i = 0;
|
|
316
|
-
const len = src.length;
|
|
317
|
-
const out = [];
|
|
318
|
-
const stack = [];
|
|
319
|
-
while (i < len) {
|
|
320
|
-
const lt = src.indexOf("<", i);
|
|
321
|
-
if (lt === -1) {
|
|
322
|
-
out.push(src.slice(i));
|
|
323
|
-
break;
|
|
324
|
-
}
|
|
325
|
-
out.push(src.slice(i, lt));
|
|
326
|
-
if (lt + 1 >= len) {
|
|
327
|
-
break;
|
|
328
|
-
}
|
|
329
|
-
const next = src[lt + 1];
|
|
330
|
-
if (next === "!" || next === "?") {
|
|
331
|
-
i = handleSpecialTagSegment(src, lt, out);
|
|
332
|
-
continue;
|
|
333
|
-
}
|
|
334
|
-
if (next === "/") {
|
|
335
|
-
i = handleClosingTagSegment(src, lt, out, stack);
|
|
336
|
-
continue;
|
|
337
|
-
}
|
|
338
|
-
i = handleOpeningTagSegment(src, lt, out, stack);
|
|
339
|
-
}
|
|
340
|
-
for (let k = stack.length - 1; k >= 0; k -= 1) {
|
|
341
|
-
out.push(`</${stack[k]}>`);
|
|
342
|
-
}
|
|
343
|
-
return out.join("");
|
|
344
|
-
}
|
|
345
|
-
function skipWs(s, p, len) {
|
|
346
|
-
let idx = p;
|
|
347
|
-
while (idx < len && WHITESPACE_REGEX.test(s[idx])) {
|
|
348
|
-
idx += 1;
|
|
349
|
-
}
|
|
350
|
-
return idx;
|
|
351
|
-
}
|
|
352
|
-
function parseTagNameAt(s, p, len) {
|
|
353
|
-
let idx = p;
|
|
354
|
-
const start = idx;
|
|
355
|
-
while (idx < len && NAME_CHAR_RE.test(s[idx])) {
|
|
356
|
-
idx += 1;
|
|
357
|
-
}
|
|
358
|
-
return { name: s.slice(start, idx), pos: idx };
|
|
359
|
-
}
|
|
360
|
-
function handleSpecialTagSegment(src, lt, out) {
|
|
361
|
-
const gt = src.indexOf(">", lt + 1);
|
|
362
|
-
if (gt === -1) {
|
|
363
|
-
out.push(src.slice(lt));
|
|
364
|
-
return src.length;
|
|
365
|
-
}
|
|
366
|
-
out.push(src.slice(lt, gt + 1));
|
|
367
|
-
return gt + 1;
|
|
368
|
-
}
|
|
369
|
-
function handleClosingTagSegment(src, lt, out, stack) {
|
|
370
|
-
const len = src.length;
|
|
371
|
-
let p = skipWs(src, lt + 2, len);
|
|
372
|
-
const { name, pos } = parseTagNameAt(src, p, len);
|
|
373
|
-
p = pos;
|
|
374
|
-
const gt = src.indexOf(">", p);
|
|
375
|
-
const closingText = gt === -1 ? src.slice(lt) : src.slice(lt, gt + 1);
|
|
376
|
-
const idx = stack.lastIndexOf(name);
|
|
377
|
-
if (idx !== -1) {
|
|
378
|
-
for (let k = stack.length - 1; k > idx; k -= 1) {
|
|
379
|
-
out.push(`</${stack[k]}>`);
|
|
380
|
-
stack.pop();
|
|
381
|
-
}
|
|
382
|
-
out.push(closingText);
|
|
383
|
-
stack.pop();
|
|
384
|
-
}
|
|
385
|
-
return gt === -1 ? len : gt + 1;
|
|
386
|
-
}
|
|
387
|
-
function handleOpeningTagSegment(src, lt, out, stack) {
|
|
388
|
-
const len = src.length;
|
|
389
|
-
let p = skipWs(src, lt + 1, len);
|
|
390
|
-
const nameStart = p;
|
|
391
|
-
const parsed = parseTagNameAt(src, p, len);
|
|
392
|
-
p = parsed.pos;
|
|
393
|
-
const name = src.slice(nameStart, p);
|
|
394
|
-
const q = src.indexOf(">", p);
|
|
395
|
-
if (q === -1) {
|
|
396
|
-
out.push(src.slice(lt));
|
|
397
|
-
return len;
|
|
398
|
-
}
|
|
399
|
-
let r = q - 1;
|
|
400
|
-
while (r >= nameStart && WHITESPACE_REGEX.test(src[r])) {
|
|
401
|
-
r -= 1;
|
|
402
|
-
}
|
|
403
|
-
const selfClosing = src[r] === "/";
|
|
404
|
-
out.push(src.slice(lt, q + 1));
|
|
405
|
-
if (!selfClosing && name) {
|
|
406
|
-
stack.push(name);
|
|
407
|
-
}
|
|
408
|
-
return q + 1;
|
|
409
|
-
}
|
|
410
|
-
function extractSchemaProperties(schema) {
|
|
411
|
-
const unwrapped = (0, import_rxml.unwrapJsonSchema)(schema);
|
|
412
|
-
if (!unwrapped || typeof unwrapped !== "object") {
|
|
413
|
-
return void 0;
|
|
414
|
-
}
|
|
415
|
-
return unwrapped.properties;
|
|
416
|
-
}
|
|
417
|
-
function shouldDeduplicateStringTags(schema) {
|
|
418
|
-
const props = extractSchemaProperties(schema);
|
|
419
|
-
if (!props) {
|
|
420
|
-
return false;
|
|
421
|
-
}
|
|
422
|
-
const commandRaw = props.command;
|
|
423
|
-
if (!commandRaw) {
|
|
424
|
-
return false;
|
|
425
|
-
}
|
|
426
|
-
const command = (0, import_rxml.unwrapJsonSchema)(commandRaw);
|
|
427
|
-
return (command == null ? void 0 : command.type) === "array";
|
|
428
|
-
}
|
|
429
|
-
function getStringPropertyNames(schema) {
|
|
430
|
-
const props = extractSchemaProperties(schema);
|
|
431
|
-
if (!props) {
|
|
432
|
-
return [];
|
|
433
|
-
}
|
|
434
|
-
const names = [];
|
|
435
|
-
for (const key of Object.keys(props)) {
|
|
436
|
-
const prop = (0, import_rxml.unwrapJsonSchema)(props[key]);
|
|
437
|
-
if ((prop == null ? void 0 : prop.type) === "string") {
|
|
438
|
-
names.push(key);
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return names;
|
|
442
|
-
}
|
|
443
|
-
function escapeRegExp(s) {
|
|
444
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
445
|
-
}
|
|
446
|
-
function dedupeSingleTag(xml, key) {
|
|
447
|
-
var _a, _b;
|
|
448
|
-
const escaped = escapeRegExp(key);
|
|
449
|
-
const re = new RegExp(`<${escaped}>([\\s\\S]*?)<\\/${escaped}>`, "g");
|
|
450
|
-
const matches = Array.from(xml.matchAll(re));
|
|
451
|
-
if (matches.length <= 1) {
|
|
452
|
-
return xml;
|
|
453
|
-
}
|
|
454
|
-
const last = matches.at(-1);
|
|
455
|
-
let result = "";
|
|
456
|
-
let cursor = 0;
|
|
457
|
-
for (const m of matches) {
|
|
458
|
-
const idx = (_a = m.index) != null ? _a : 0;
|
|
459
|
-
result += xml.slice(cursor, idx);
|
|
460
|
-
if (last && idx === ((_b = last.index) != null ? _b : -1)) {
|
|
461
|
-
result += m[0];
|
|
462
|
-
}
|
|
463
|
-
cursor = idx + m[0].length;
|
|
464
|
-
}
|
|
465
|
-
result += xml.slice(cursor);
|
|
466
|
-
return result;
|
|
467
|
-
}
|
|
468
|
-
function repairParsedAgainstSchema(input, schema) {
|
|
469
|
-
if (!input || typeof input !== "object") {
|
|
470
|
-
return input;
|
|
471
|
-
}
|
|
472
|
-
const properties = extractSchemaProperties(schema);
|
|
473
|
-
if (!properties) {
|
|
474
|
-
return input;
|
|
475
|
-
}
|
|
476
|
-
applySchemaProps(input, properties);
|
|
477
|
-
return input;
|
|
478
|
-
}
|
|
479
|
-
function applySchemaProps(obj, properties) {
|
|
480
|
-
for (const key of Object.keys(obj)) {
|
|
481
|
-
const propSchema = properties[key];
|
|
482
|
-
if (!propSchema) {
|
|
483
|
-
continue;
|
|
484
|
-
}
|
|
485
|
-
const prop = (0, import_rxml.unwrapJsonSchema)(propSchema);
|
|
486
|
-
if ((prop == null ? void 0 : prop.type) === "array" && prop.items) {
|
|
487
|
-
const itemSchema = (0, import_rxml.unwrapJsonSchema)(prop.items);
|
|
488
|
-
obj[key] = coerceArrayItems(obj[key], itemSchema);
|
|
489
|
-
continue;
|
|
490
|
-
}
|
|
491
|
-
if ((prop == null ? void 0 : prop.type) === "object") {
|
|
492
|
-
const val = obj[key];
|
|
493
|
-
if (val && typeof val === "object") {
|
|
494
|
-
obj[key] = repairParsedAgainstSchema(val, prop);
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
function coerceArrayItems(val, itemSchema) {
|
|
500
|
-
if (!Array.isArray(val)) {
|
|
501
|
-
return val;
|
|
502
|
-
}
|
|
503
|
-
return val.map((v) => coerceArrayItem(v, itemSchema));
|
|
504
|
-
}
|
|
505
|
-
function coerceArrayItem(v, itemSchema) {
|
|
506
|
-
const itemType = itemSchema == null ? void 0 : itemSchema.type;
|
|
507
|
-
if (typeof v === "string" && itemType === "object") {
|
|
508
|
-
const parsed = tryParseStringToSchemaObject(v, itemSchema);
|
|
509
|
-
if (parsed !== null) {
|
|
510
|
-
return parsed;
|
|
511
|
-
}
|
|
512
|
-
const fallback = extractStepStatusFromString(
|
|
513
|
-
v.replace(MALFORMED_CLOSE_RE_G, "</$1>")
|
|
514
|
-
);
|
|
515
|
-
if (fallback) {
|
|
516
|
-
return fallback;
|
|
517
|
-
}
|
|
518
|
-
return v;
|
|
519
|
-
}
|
|
520
|
-
if (v && typeof v === "object" && itemType === "object") {
|
|
521
|
-
return repairParsedAgainstSchema(v, itemSchema);
|
|
522
|
-
}
|
|
523
|
-
return v;
|
|
524
|
-
}
|
|
525
|
-
function tryParseStringToSchemaObject(xml, itemSchema) {
|
|
526
|
-
try {
|
|
527
|
-
const normalized = xml.replace(MALFORMED_CLOSE_RE_G, "</$1>");
|
|
528
|
-
const fixed = (0, import_rxml.parse)(normalized, itemSchema, { noChildNodes: [] });
|
|
529
|
-
return typeof fixed === "string" ? null : fixed;
|
|
530
|
-
} catch (e) {
|
|
531
|
-
return null;
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
function extractStepStatusFromString(normXml) {
|
|
535
|
-
const stepMatch = normXml.match(STEP_TAG_RE);
|
|
536
|
-
const statusMatch = normXml.match(STATUS_TAG_RE);
|
|
537
|
-
if (stepMatch && statusMatch) {
|
|
538
|
-
return { step: stepMatch[1], status: statusMatch[1] };
|
|
539
|
-
}
|
|
540
|
-
return null;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
67
|
// src/core/protocols/json-protocol.ts
|
|
544
68
|
var import_rjson = require("@ai-sdk-tool/rjson");
|
|
545
69
|
|
|
@@ -722,7 +246,7 @@ function generateId() {
|
|
|
722
246
|
}
|
|
723
247
|
|
|
724
248
|
// src/core/utils/regex.ts
|
|
725
|
-
function
|
|
249
|
+
function escapeRegExp(literal) {
|
|
726
250
|
return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
727
251
|
}
|
|
728
252
|
|
|
@@ -969,8 +493,8 @@ var jsonProtocol = ({
|
|
|
969
493
|
text,
|
|
970
494
|
options
|
|
971
495
|
}) {
|
|
972
|
-
const startEsc =
|
|
973
|
-
const endEsc =
|
|
496
|
+
const startEsc = escapeRegExp(toolCallStart);
|
|
497
|
+
const endEsc = escapeRegExp(toolCallEnd);
|
|
974
498
|
const toolCallRegex = new RegExp(
|
|
975
499
|
`${startEsc}([\0-\uFFFF]*?)${endEsc}`,
|
|
976
500
|
"gs"
|
|
@@ -1029,8 +553,8 @@ var jsonProtocol = ({
|
|
|
1029
553
|
});
|
|
1030
554
|
},
|
|
1031
555
|
extractToolCallSegments({ text }) {
|
|
1032
|
-
const startEsc =
|
|
1033
|
-
const endEsc =
|
|
556
|
+
const startEsc = escapeRegExp(toolCallStart);
|
|
557
|
+
const endEsc = escapeRegExp(toolCallEnd);
|
|
1034
558
|
const regex = new RegExp(`${startEsc}([\0-\uFFFF]*?)${endEsc}`, "gs");
|
|
1035
559
|
const segments = [];
|
|
1036
560
|
let m = regex.exec(text);
|
|
@@ -1051,93 +575,48 @@ function isTCMProtocolFactory(protocol) {
|
|
|
1051
575
|
}
|
|
1052
576
|
|
|
1053
577
|
// src/core/protocols/xml-protocol.ts
|
|
1054
|
-
var
|
|
1055
|
-
var
|
|
1056
|
-
var
|
|
1057
|
-
var
|
|
578
|
+
var import_rxml = require("@ai-sdk-tool/rxml");
|
|
579
|
+
var NAME_CHAR_RE = /[A-Za-z0-9_:-]/;
|
|
580
|
+
var WHITESPACE_REGEX = /\s/;
|
|
581
|
+
var REGEX_ESCAPE_RE = /[.*+?^${}()|[\]\\]/g;
|
|
582
|
+
function escapeRegExp2(value) {
|
|
583
|
+
return value.replace(REGEX_ESCAPE_RE, "\\$&");
|
|
584
|
+
}
|
|
1058
585
|
function getToolSchema(tools, toolName) {
|
|
1059
586
|
var _a;
|
|
1060
587
|
return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
1061
588
|
}
|
|
1062
|
-
function
|
|
1063
|
-
|
|
1064
|
-
}
|
|
1065
|
-
function tryParseSecondaryXml(content, toolSchema, options) {
|
|
1066
|
-
const balanced = balanceTags(content);
|
|
1067
|
-
try {
|
|
1068
|
-
let parsed = (0, import_rxml2.parse)(balanced, toolSchema, {
|
|
1069
|
-
onError: options == null ? void 0 : options.onError,
|
|
1070
|
-
noChildNodes: []
|
|
1071
|
-
});
|
|
1072
|
-
parsed = repairParsedAgainstSchema(parsed, toolSchema);
|
|
1073
|
-
return parsed;
|
|
1074
|
-
} catch (e) {
|
|
1075
|
-
if (shouldDeduplicateStringTags(toolSchema)) {
|
|
1076
|
-
const names = getStringPropertyNames(toolSchema);
|
|
1077
|
-
let deduped = balanced;
|
|
1078
|
-
for (const key of names) {
|
|
1079
|
-
deduped = dedupeSingleTag(deduped, key);
|
|
1080
|
-
}
|
|
1081
|
-
if (deduped !== balanced) {
|
|
1082
|
-
try {
|
|
1083
|
-
let reparsed = (0, import_rxml2.parse)(deduped, toolSchema, {
|
|
1084
|
-
onError: options == null ? void 0 : options.onError,
|
|
1085
|
-
noChildNodes: []
|
|
1086
|
-
});
|
|
1087
|
-
reparsed = repairParsedAgainstSchema(reparsed, toolSchema);
|
|
1088
|
-
return reparsed;
|
|
1089
|
-
} catch (e2) {
|
|
1090
|
-
return null;
|
|
1091
|
-
}
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
return null;
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
function processToolCallWithPipeline(params) {
|
|
1098
|
-
var _a;
|
|
1099
|
-
const {
|
|
1100
|
-
toolCall,
|
|
1101
|
-
tools,
|
|
1102
|
-
options,
|
|
1103
|
-
text,
|
|
1104
|
-
processedElements,
|
|
1105
|
-
pipelineConfig = defaultPipelineConfig2,
|
|
1106
|
-
maxReparses
|
|
1107
|
-
} = params;
|
|
589
|
+
function processToolCall(params) {
|
|
590
|
+
var _a, _b;
|
|
591
|
+
const { toolCall, tools, options, text, processedElements, parseOptions } = params;
|
|
1108
592
|
const toolSchema = getToolSchema(tools, toolCall.toolName);
|
|
1109
|
-
const
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
parse: (xml, schema) => (0, import_rxml2.parse)(xml, schema, { onError: options == null ? void 0 : options.onError, noChildNodes: [] }),
|
|
1116
|
-
onError: options == null ? void 0 : options.onError,
|
|
1117
|
-
maxReparses
|
|
1118
|
-
});
|
|
1119
|
-
if (result.parsed !== null) {
|
|
593
|
+
const parseConfig = {
|
|
594
|
+
...parseOptions != null ? parseOptions : {},
|
|
595
|
+
onError: (_a = options == null ? void 0 : options.onError) != null ? _a : parseOptions == null ? void 0 : parseOptions.onError
|
|
596
|
+
};
|
|
597
|
+
try {
|
|
598
|
+
const parsed = (0, import_rxml.parse)(toolCall.content, toolSchema, parseConfig);
|
|
1120
599
|
processedElements.push({
|
|
1121
600
|
type: "tool-call",
|
|
1122
601
|
toolCallId: generateId(),
|
|
1123
602
|
toolName: toolCall.toolName,
|
|
1124
|
-
input: JSON.stringify(
|
|
603
|
+
input: JSON.stringify(parsed)
|
|
1125
604
|
});
|
|
1126
|
-
}
|
|
605
|
+
} catch (error) {
|
|
1127
606
|
const originalCallText = text.substring(
|
|
1128
607
|
toolCall.startIndex,
|
|
1129
608
|
toolCall.endIndex
|
|
1130
609
|
);
|
|
1131
|
-
(
|
|
610
|
+
(_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
|
|
1132
611
|
options,
|
|
1133
612
|
`Could not process XML tool call: ${toolCall.toolName}`,
|
|
1134
|
-
{ toolCall: originalCallText, error
|
|
613
|
+
{ toolCall: originalCallText, error }
|
|
1135
614
|
);
|
|
1136
615
|
processedElements.push({ type: "text", text: originalCallText });
|
|
1137
616
|
}
|
|
1138
617
|
}
|
|
1139
618
|
function handleStreamingToolCallEnd(params) {
|
|
1140
|
-
var _a;
|
|
619
|
+
var _a, _b;
|
|
1141
620
|
const {
|
|
1142
621
|
toolContent,
|
|
1143
622
|
currentToolCall,
|
|
@@ -1145,51 +624,27 @@ function handleStreamingToolCallEnd(params) {
|
|
|
1145
624
|
options,
|
|
1146
625
|
ctrl,
|
|
1147
626
|
flushText,
|
|
1148
|
-
|
|
1149
|
-
maxReparses
|
|
627
|
+
parseOptions
|
|
1150
628
|
} = params;
|
|
1151
629
|
const toolSchema = getToolSchema(tools, currentToolCall.name);
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
toolContent,
|
|
1157
|
-
toolSchema
|
|
1158
|
-
);
|
|
1159
|
-
const result = applyHeuristicPipeline(ctx, pipelineConfig, {
|
|
1160
|
-
parse: (xml, schema) => (0, import_rxml2.parse)(xml, schema, { onError: options == null ? void 0 : options.onError, noChildNodes: [] }),
|
|
1161
|
-
onError: options == null ? void 0 : options.onError,
|
|
1162
|
-
maxReparses
|
|
1163
|
-
});
|
|
1164
|
-
parsedResult = result.parsed;
|
|
1165
|
-
} else {
|
|
1166
|
-
try {
|
|
1167
|
-
const primary = escapeInvalidLt(normalizeCloseTags(toolContent));
|
|
1168
|
-
const parsed = (0, import_rxml2.parse)(primary, toolSchema, {
|
|
1169
|
-
onError: options == null ? void 0 : options.onError,
|
|
1170
|
-
noChildNodes: []
|
|
1171
|
-
});
|
|
1172
|
-
parsedResult = repairParsedAgainstSchema(parsed, toolSchema);
|
|
1173
|
-
} catch (e) {
|
|
1174
|
-
parsedResult = tryParseSecondaryXml(
|
|
1175
|
-
toolContent,
|
|
1176
|
-
toolSchema,
|
|
1177
|
-
options != null ? options : {}
|
|
1178
|
-
);
|
|
1179
|
-
}
|
|
1180
|
-
}
|
|
630
|
+
const parseConfig = {
|
|
631
|
+
...parseOptions != null ? parseOptions : {},
|
|
632
|
+
onError: (_a = options == null ? void 0 : options.onError) != null ? _a : parseOptions == null ? void 0 : parseOptions.onError
|
|
633
|
+
};
|
|
1181
634
|
flushText(ctrl);
|
|
1182
|
-
|
|
635
|
+
try {
|
|
636
|
+
const parsedResult = (0, import_rxml.parse)(toolContent, toolSchema, parseConfig);
|
|
1183
637
|
ctrl.enqueue({
|
|
1184
638
|
type: "tool-call",
|
|
1185
639
|
toolCallId: generateId(),
|
|
1186
640
|
toolName: currentToolCall.name,
|
|
1187
641
|
input: JSON.stringify(parsedResult)
|
|
1188
642
|
});
|
|
1189
|
-
}
|
|
643
|
+
} catch (error) {
|
|
1190
644
|
const original = `<${currentToolCall.name}>${toolContent}</${currentToolCall.name}>`;
|
|
1191
|
-
(
|
|
1192
|
-
toolCall: original
|
|
645
|
+
(_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(options, "Could not process streaming XML tool call", {
|
|
646
|
+
toolCall: original,
|
|
647
|
+
error
|
|
1193
648
|
});
|
|
1194
649
|
flushText(ctrl, original);
|
|
1195
650
|
}
|
|
@@ -1228,11 +683,11 @@ function consumeClosingTag(text, lt) {
|
|
|
1228
683
|
}
|
|
1229
684
|
function consumeOpenTag(text, lt) {
|
|
1230
685
|
let p = lt + 1;
|
|
1231
|
-
while (p < text.length &&
|
|
686
|
+
while (p < text.length && WHITESPACE_REGEX.test(text[p])) {
|
|
1232
687
|
p += 1;
|
|
1233
688
|
}
|
|
1234
689
|
const nameStart = p;
|
|
1235
|
-
while (p < text.length &&
|
|
690
|
+
while (p < text.length && NAME_CHAR_RE.test(text.charAt(p))) {
|
|
1236
691
|
p += 1;
|
|
1237
692
|
}
|
|
1238
693
|
const name = text.slice(nameStart, p);
|
|
@@ -1241,7 +696,7 @@ function consumeOpenTag(text, lt) {
|
|
|
1241
696
|
return null;
|
|
1242
697
|
}
|
|
1243
698
|
let r = q - 1;
|
|
1244
|
-
while (r >= nameStart &&
|
|
699
|
+
while (r >= nameStart && WHITESPACE_REGEX.test(text[r])) {
|
|
1245
700
|
r -= 1;
|
|
1246
701
|
}
|
|
1247
702
|
const selfClosing = text[r] === "/";
|
|
@@ -1270,11 +725,11 @@ function nextTagToken(text, fromPos) {
|
|
|
1270
725
|
if (next === "/") {
|
|
1271
726
|
const closing = consumeClosingTag(text, lt);
|
|
1272
727
|
let p = lt + 2;
|
|
1273
|
-
while (p < text.length &&
|
|
728
|
+
while (p < text.length && WHITESPACE_REGEX.test(text[p])) {
|
|
1274
729
|
p += 1;
|
|
1275
730
|
}
|
|
1276
731
|
const nameStart = p;
|
|
1277
|
-
while (p < text.length &&
|
|
732
|
+
while (p < text.length && NAME_CHAR_RE.test(text.charAt(p))) {
|
|
1278
733
|
p += 1;
|
|
1279
734
|
}
|
|
1280
735
|
const name = text.slice(nameStart, p);
|
|
@@ -1291,49 +746,90 @@ function nextTagToken(text, fromPos) {
|
|
|
1291
746
|
nextPos: open.nextPos
|
|
1292
747
|
};
|
|
1293
748
|
}
|
|
749
|
+
function findNextToolTag(text, searchIndex, startTag, selfTag) {
|
|
750
|
+
const openIdx = text.indexOf(startTag, searchIndex);
|
|
751
|
+
const selfIdx = text.indexOf(selfTag, searchIndex);
|
|
752
|
+
if (openIdx === -1 && selfIdx === -1) {
|
|
753
|
+
return null;
|
|
754
|
+
}
|
|
755
|
+
const isSelfClosing = selfIdx !== -1 && (openIdx === -1 || selfIdx < openIdx);
|
|
756
|
+
return {
|
|
757
|
+
tagStart: isSelfClosing ? selfIdx : openIdx,
|
|
758
|
+
isSelfClosing
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
function findLastCloseTagStart(segment, toolName) {
|
|
762
|
+
const closeTagPattern = new RegExp(
|
|
763
|
+
`</\\s*${escapeRegExp2(toolName)}\\s*>`,
|
|
764
|
+
"g"
|
|
765
|
+
);
|
|
766
|
+
let closeTagStart = -1;
|
|
767
|
+
let match = closeTagPattern.exec(segment);
|
|
768
|
+
while (match !== null) {
|
|
769
|
+
closeTagStart = match.index;
|
|
770
|
+
match = closeTagPattern.exec(segment);
|
|
771
|
+
}
|
|
772
|
+
if (closeTagStart === -1) {
|
|
773
|
+
return segment.lastIndexOf("<");
|
|
774
|
+
}
|
|
775
|
+
return closeTagStart;
|
|
776
|
+
}
|
|
777
|
+
function pushSelfClosingToolCall(toolCalls, toolName, text, tagStart, selfTag) {
|
|
778
|
+
const endIndex = tagStart + selfTag.length;
|
|
779
|
+
toolCalls.push({
|
|
780
|
+
toolName,
|
|
781
|
+
startIndex: tagStart,
|
|
782
|
+
endIndex,
|
|
783
|
+
content: "",
|
|
784
|
+
segment: text.substring(tagStart, endIndex)
|
|
785
|
+
});
|
|
786
|
+
return endIndex;
|
|
787
|
+
}
|
|
788
|
+
function appendOpenToolCallIfComplete(toolCalls, text, toolName, tagStart, startTag) {
|
|
789
|
+
const contentStart = tagStart + startTag.length;
|
|
790
|
+
const fullTagEnd = findClosingTagEndFlexible(text, contentStart, toolName);
|
|
791
|
+
if (fullTagEnd === -1 || fullTagEnd <= contentStart) {
|
|
792
|
+
return contentStart;
|
|
793
|
+
}
|
|
794
|
+
const segment = text.substring(tagStart, fullTagEnd);
|
|
795
|
+
const closeTagStart = findLastCloseTagStart(segment, toolName);
|
|
796
|
+
const inner = closeTagStart === -1 ? segment.slice(startTag.length) : segment.slice(startTag.length, closeTagStart);
|
|
797
|
+
toolCalls.push({
|
|
798
|
+
toolName,
|
|
799
|
+
startIndex: tagStart,
|
|
800
|
+
endIndex: fullTagEnd,
|
|
801
|
+
content: inner,
|
|
802
|
+
segment
|
|
803
|
+
});
|
|
804
|
+
return fullTagEnd;
|
|
805
|
+
}
|
|
1294
806
|
function findToolCallsForName(text, toolName) {
|
|
1295
|
-
var _a;
|
|
1296
807
|
const toolCalls = [];
|
|
808
|
+
const startTag = `<${toolName}>`;
|
|
809
|
+
const selfTag = `<${toolName}/>`;
|
|
1297
810
|
let searchIndex = 0;
|
|
1298
811
|
while (searchIndex < text.length) {
|
|
1299
|
-
const
|
|
1300
|
-
|
|
1301
|
-
const openIdx = text.indexOf(startTag, searchIndex);
|
|
1302
|
-
const selfIdx = text.indexOf(selfTag, searchIndex);
|
|
1303
|
-
if (openIdx === -1 && selfIdx === -1) {
|
|
812
|
+
const match = findNextToolTag(text, searchIndex, startTag, selfTag);
|
|
813
|
+
if (match === null) {
|
|
1304
814
|
break;
|
|
1305
815
|
}
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
const endIndex = tagStart + selfTag.length;
|
|
1310
|
-
const segment = text.substring(tagStart, endIndex);
|
|
1311
|
-
toolCalls.push({
|
|
816
|
+
if (match.isSelfClosing) {
|
|
817
|
+
searchIndex = pushSelfClosingToolCall(
|
|
818
|
+
toolCalls,
|
|
1312
819
|
toolName,
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
});
|
|
1318
|
-
searchIndex = endIndex;
|
|
820
|
+
text,
|
|
821
|
+
match.tagStart,
|
|
822
|
+
selfTag
|
|
823
|
+
);
|
|
1319
824
|
continue;
|
|
1320
825
|
}
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
startIndex: tagStart,
|
|
1329
|
-
endIndex: fullTagEnd,
|
|
1330
|
-
content: inner,
|
|
1331
|
-
segment
|
|
1332
|
-
});
|
|
1333
|
-
searchIndex = fullTagEnd;
|
|
1334
|
-
} else {
|
|
1335
|
-
searchIndex = contentStart;
|
|
1336
|
-
}
|
|
826
|
+
searchIndex = appendOpenToolCallIfComplete(
|
|
827
|
+
toolCalls,
|
|
828
|
+
text,
|
|
829
|
+
toolName,
|
|
830
|
+
match.tagStart,
|
|
831
|
+
startTag
|
|
832
|
+
);
|
|
1337
833
|
}
|
|
1338
834
|
return toolCalls;
|
|
1339
835
|
}
|
|
@@ -1410,16 +906,20 @@ function processToolCallInBuffer(params) {
|
|
|
1410
906
|
controller,
|
|
1411
907
|
flushText,
|
|
1412
908
|
setBuffer,
|
|
1413
|
-
|
|
1414
|
-
maxReparses
|
|
909
|
+
parseOptions
|
|
1415
910
|
} = params;
|
|
1416
|
-
const
|
|
1417
|
-
|
|
1418
|
-
|
|
911
|
+
const endTagPattern = new RegExp(
|
|
912
|
+
`</\\s*${escapeRegExp2(currentToolCall.name)}\\s*>`
|
|
913
|
+
);
|
|
914
|
+
const endMatch = endTagPattern.exec(buffer);
|
|
915
|
+
if (!endMatch || endMatch.index === void 0) {
|
|
1419
916
|
return { buffer, currentToolCall, shouldBreak: true };
|
|
1420
917
|
}
|
|
918
|
+
const endIdx = endMatch.index;
|
|
919
|
+
const endPos = endIdx + endMatch[0].length;
|
|
1421
920
|
const content = buffer.substring(0, endIdx);
|
|
1422
|
-
|
|
921
|
+
const remainder = buffer.substring(endPos);
|
|
922
|
+
setBuffer(remainder);
|
|
1423
923
|
handleStreamingToolCallEnd({
|
|
1424
924
|
toolContent: content,
|
|
1425
925
|
currentToolCall,
|
|
@@ -1427,11 +927,10 @@ function processToolCallInBuffer(params) {
|
|
|
1427
927
|
options,
|
|
1428
928
|
ctrl: controller,
|
|
1429
929
|
flushText,
|
|
1430
|
-
|
|
1431
|
-
maxReparses
|
|
930
|
+
parseOptions
|
|
1432
931
|
});
|
|
1433
932
|
return {
|
|
1434
|
-
buffer:
|
|
933
|
+
buffer: remainder,
|
|
1435
934
|
currentToolCall: null,
|
|
1436
935
|
shouldBreak: false
|
|
1437
936
|
};
|
|
@@ -1444,8 +943,7 @@ function processNoToolCallInBuffer(params) {
|
|
|
1444
943
|
flushText,
|
|
1445
944
|
tools,
|
|
1446
945
|
options,
|
|
1447
|
-
|
|
1448
|
-
maxReparses,
|
|
946
|
+
parseOptions,
|
|
1449
947
|
setBuffer
|
|
1450
948
|
} = params;
|
|
1451
949
|
const {
|
|
@@ -1480,8 +978,7 @@ function processNoToolCallInBuffer(params) {
|
|
|
1480
978
|
options,
|
|
1481
979
|
ctrl: controller,
|
|
1482
980
|
flushText,
|
|
1483
|
-
|
|
1484
|
-
maxReparses
|
|
981
|
+
parseOptions
|
|
1485
982
|
});
|
|
1486
983
|
return {
|
|
1487
984
|
buffer: newBuffer2,
|
|
@@ -1500,7 +997,7 @@ function processNoToolCallInBuffer(params) {
|
|
|
1500
997
|
shouldContinue: true
|
|
1501
998
|
};
|
|
1502
999
|
}
|
|
1503
|
-
function createProcessBufferHandler(getBuffer, setBuffer, getCurrentToolCall, setCurrentToolCall, tools, options, toolNames, flushText,
|
|
1000
|
+
function createProcessBufferHandler(getBuffer, setBuffer, getCurrentToolCall, setCurrentToolCall, tools, options, toolNames, flushText, parseOptions) {
|
|
1504
1001
|
return (controller) => {
|
|
1505
1002
|
while (true) {
|
|
1506
1003
|
const currentToolCall = getCurrentToolCall();
|
|
@@ -1513,8 +1010,7 @@ function createProcessBufferHandler(getBuffer, setBuffer, getCurrentToolCall, se
|
|
|
1513
1010
|
controller,
|
|
1514
1011
|
flushText,
|
|
1515
1012
|
setBuffer,
|
|
1516
|
-
|
|
1517
|
-
maxReparses
|
|
1013
|
+
parseOptions
|
|
1518
1014
|
});
|
|
1519
1015
|
setBuffer(result.buffer);
|
|
1520
1016
|
setCurrentToolCall(result.currentToolCall);
|
|
@@ -1529,8 +1025,7 @@ function createProcessBufferHandler(getBuffer, setBuffer, getCurrentToolCall, se
|
|
|
1529
1025
|
flushText,
|
|
1530
1026
|
tools,
|
|
1531
1027
|
options,
|
|
1532
|
-
|
|
1533
|
-
maxReparses,
|
|
1028
|
+
parseOptions,
|
|
1534
1029
|
setBuffer
|
|
1535
1030
|
});
|
|
1536
1031
|
setBuffer(result.buffer);
|
|
@@ -1547,43 +1042,12 @@ function createProcessBufferHandler(getBuffer, setBuffer, getCurrentToolCall, se
|
|
|
1547
1042
|
};
|
|
1548
1043
|
}
|
|
1549
1044
|
var xmlProtocol = (protocolOptions) => {
|
|
1550
|
-
var _a
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
fallbackReparse: [],
|
|
1557
|
-
postParse: []
|
|
1558
|
-
};
|
|
1559
|
-
for (const h of protocolOptions.heuristics) {
|
|
1560
|
-
if (h.phase === "pre-parse") {
|
|
1561
|
-
(_a = heuristicsConfig.preParse) == null ? void 0 : _a.push(h);
|
|
1562
|
-
} else if (h.phase === "fallback-reparse") {
|
|
1563
|
-
(_b = heuristicsConfig.fallbackReparse) == null ? void 0 : _b.push(h);
|
|
1564
|
-
} else if (h.phase === "post-parse") {
|
|
1565
|
-
(_c = heuristicsConfig.postParse) == null ? void 0 : _c.push(h);
|
|
1566
|
-
}
|
|
1567
|
-
}
|
|
1568
|
-
if (pipelineConfig) {
|
|
1569
|
-
pipelineConfig = {
|
|
1570
|
-
preParse: [
|
|
1571
|
-
...(_d = pipelineConfig.preParse) != null ? _d : [],
|
|
1572
|
-
...(_e = heuristicsConfig.preParse) != null ? _e : []
|
|
1573
|
-
],
|
|
1574
|
-
fallbackReparse: [
|
|
1575
|
-
...(_f = pipelineConfig.fallbackReparse) != null ? _f : [],
|
|
1576
|
-
...(_g = heuristicsConfig.fallbackReparse) != null ? _g : []
|
|
1577
|
-
],
|
|
1578
|
-
postParse: [
|
|
1579
|
-
...(_h = pipelineConfig.postParse) != null ? _h : [],
|
|
1580
|
-
...(_i = heuristicsConfig.postParse) != null ? _i : []
|
|
1581
|
-
]
|
|
1582
|
-
};
|
|
1583
|
-
} else {
|
|
1584
|
-
pipelineConfig = heuristicsConfig;
|
|
1585
|
-
}
|
|
1586
|
-
}
|
|
1045
|
+
var _a;
|
|
1046
|
+
const parseOptions = {
|
|
1047
|
+
repair: true,
|
|
1048
|
+
noChildNodes: [],
|
|
1049
|
+
...(_a = protocolOptions == null ? void 0 : protocolOptions.parseOptions) != null ? _a : {}
|
|
1050
|
+
};
|
|
1587
1051
|
return {
|
|
1588
1052
|
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
1589
1053
|
return toolSystemPromptTemplate(tools || []);
|
|
@@ -1597,7 +1061,7 @@ var xmlProtocol = (protocolOptions) => {
|
|
|
1597
1061
|
args = toolCall.input;
|
|
1598
1062
|
}
|
|
1599
1063
|
}
|
|
1600
|
-
return (0,
|
|
1064
|
+
return (0, import_rxml.stringify)(toolCall.toolName, args, {
|
|
1601
1065
|
suppressEmptyNode: false,
|
|
1602
1066
|
format: true,
|
|
1603
1067
|
minimalEscaping: true
|
|
@@ -1618,14 +1082,13 @@ var xmlProtocol = (protocolOptions) => {
|
|
|
1618
1082
|
text: text.substring(currentIndex, tc.startIndex)
|
|
1619
1083
|
});
|
|
1620
1084
|
}
|
|
1621
|
-
|
|
1085
|
+
processToolCall({
|
|
1622
1086
|
toolCall: tc,
|
|
1623
1087
|
tools,
|
|
1624
1088
|
options,
|
|
1625
1089
|
text,
|
|
1626
1090
|
processedElements,
|
|
1627
|
-
|
|
1628
|
-
maxReparses
|
|
1091
|
+
parseOptions
|
|
1629
1092
|
});
|
|
1630
1093
|
currentIndex = tc.endIndex;
|
|
1631
1094
|
}
|
|
@@ -1666,8 +1129,7 @@ var xmlProtocol = (protocolOptions) => {
|
|
|
1666
1129
|
options,
|
|
1667
1130
|
toolNames,
|
|
1668
1131
|
flushText,
|
|
1669
|
-
|
|
1670
|
-
maxReparses
|
|
1132
|
+
parseOptions
|
|
1671
1133
|
);
|
|
1672
1134
|
return new TransformStream({
|
|
1673
1135
|
transform(chunk, controller) {
|
|
@@ -1717,8 +1179,8 @@ var xmlProtocol = (protocolOptions) => {
|
|
|
1717
1179
|
|
|
1718
1180
|
// src/core/protocols/yaml-protocol.ts
|
|
1719
1181
|
var import_yaml = __toESM(require("yaml"), 1);
|
|
1720
|
-
var
|
|
1721
|
-
var
|
|
1182
|
+
var NAME_CHAR_RE2 = /[A-Za-z0-9_:-]/;
|
|
1183
|
+
var WHITESPACE_REGEX2 = /\s/;
|
|
1722
1184
|
var LEADING_WHITESPACE_RE = /^(\s*)/;
|
|
1723
1185
|
function findClosingTagEnd(text, contentStart, toolName) {
|
|
1724
1186
|
let pos = contentStart;
|
|
@@ -1735,11 +1197,11 @@ function findClosingTagEnd(text, contentStart, toolName) {
|
|
|
1735
1197
|
break;
|
|
1736
1198
|
}
|
|
1737
1199
|
let p = ltIdx + 2;
|
|
1738
|
-
while (p < gtIdx &&
|
|
1200
|
+
while (p < gtIdx && WHITESPACE_REGEX2.test(text[p])) {
|
|
1739
1201
|
p++;
|
|
1740
1202
|
}
|
|
1741
1203
|
const nameStart = p;
|
|
1742
|
-
while (p < gtIdx &&
|
|
1204
|
+
while (p < gtIdx && NAME_CHAR_RE2.test(text.charAt(p))) {
|
|
1743
1205
|
p++;
|
|
1744
1206
|
}
|
|
1745
1207
|
const name = text.slice(nameStart, p);
|
|
@@ -1755,11 +1217,11 @@ function findClosingTagEnd(text, contentStart, toolName) {
|
|
|
1755
1217
|
pos = gtIdx === -1 ? text.length : gtIdx + 1;
|
|
1756
1218
|
} else {
|
|
1757
1219
|
let p = ltIdx + 1;
|
|
1758
|
-
while (p < text.length &&
|
|
1220
|
+
while (p < text.length && WHITESPACE_REGEX2.test(text[p])) {
|
|
1759
1221
|
p++;
|
|
1760
1222
|
}
|
|
1761
1223
|
const nameStart = p;
|
|
1762
|
-
while (p < text.length &&
|
|
1224
|
+
while (p < text.length && NAME_CHAR_RE2.test(text.charAt(p))) {
|
|
1763
1225
|
p++;
|
|
1764
1226
|
}
|
|
1765
1227
|
const name = text.slice(nameStart, p);
|
|
@@ -1768,7 +1230,7 @@ function findClosingTagEnd(text, contentStart, toolName) {
|
|
|
1768
1230
|
break;
|
|
1769
1231
|
}
|
|
1770
1232
|
let r = gtIdx - 1;
|
|
1771
|
-
while (r >= nameStart &&
|
|
1233
|
+
while (r >= nameStart && WHITESPACE_REGEX2.test(text[r])) {
|
|
1772
1234
|
r--;
|
|
1773
1235
|
}
|
|
1774
1236
|
const selfClosing = text[r] === "/";
|
|
@@ -2273,7 +1735,7 @@ function hasInputProperty(obj) {
|
|
|
2273
1735
|
|
|
2274
1736
|
// src/generate-handler.ts
|
|
2275
1737
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
2276
|
-
var
|
|
1738
|
+
var import_schema_coerce = require("@ai-sdk-tool/schema-coerce");
|
|
2277
1739
|
function parseToolChoiceJson(text, providerOptions) {
|
|
2278
1740
|
var _a;
|
|
2279
1741
|
try {
|
|
@@ -2435,7 +1897,7 @@ function fixToolCallWithSchema(part, tools) {
|
|
|
2435
1897
|
args = part.input;
|
|
2436
1898
|
}
|
|
2437
1899
|
const schema = (_a = tools.find((t) => t.name === part.toolName)) == null ? void 0 : _a.inputSchema;
|
|
2438
|
-
const coerced = (0,
|
|
1900
|
+
const coerced = (0, import_schema_coerce.coerceBySchema)(args, schema);
|
|
2439
1901
|
return {
|
|
2440
1902
|
...part,
|
|
2441
1903
|
input: JSON.stringify(coerced != null ? coerced : {})
|
|
@@ -3099,20 +2561,12 @@ function createToolMiddleware({
|
|
|
3099
2561
|
const resolvedProtocol = isTCMProtocolFactory(protocol) ? protocol() : protocol;
|
|
3100
2562
|
return {
|
|
3101
2563
|
specificationVersion: "v3",
|
|
3102
|
-
wrapStream: ({ doStream, doGenerate, params }) => {
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
}
|
|
3109
|
-
return wrapStream({
|
|
3110
|
-
protocol: resolvedProtocol,
|
|
3111
|
-
doStream,
|
|
3112
|
-
doGenerate,
|
|
3113
|
-
params
|
|
3114
|
-
});
|
|
3115
|
-
},
|
|
2564
|
+
wrapStream: ({ doStream, doGenerate, params }) => wrapStream({
|
|
2565
|
+
protocol: resolvedProtocol,
|
|
2566
|
+
doStream,
|
|
2567
|
+
doGenerate,
|
|
2568
|
+
params
|
|
2569
|
+
}),
|
|
3116
2570
|
wrapGenerate: async ({ doGenerate, params }) => wrapGenerate({
|
|
3117
2571
|
protocol: resolvedProtocol,
|
|
3118
2572
|
doGenerate,
|
|
@@ -3146,16 +2600,10 @@ var yamlToolMiddleware = createToolMiddleware({
|
|
|
3146
2600
|
});
|
|
3147
2601
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3148
2602
|
0 && (module.exports = {
|
|
3149
|
-
applyHeuristicPipeline,
|
|
3150
|
-
balanceTagsHeuristic,
|
|
3151
2603
|
createDynamicIfThenElseSchema,
|
|
3152
|
-
createIntermediateCall,
|
|
3153
2604
|
createToolMiddleware,
|
|
3154
2605
|
decodeOriginalTools,
|
|
3155
|
-
dedupeShellStringTagsHeuristic,
|
|
3156
|
-
defaultPipelineConfig,
|
|
3157
2606
|
encodeOriginalTools,
|
|
3158
|
-
escapeInvalidLtHeuristic,
|
|
3159
2607
|
escapeRegExp,
|
|
3160
2608
|
extractOnErrorOption,
|
|
3161
2609
|
extractToolNamesFromOriginalTools,
|
|
@@ -3172,10 +2620,7 @@ var yamlToolMiddleware = createToolMiddleware({
|
|
|
3172
2620
|
logParsedChunk,
|
|
3173
2621
|
logParsedSummary,
|
|
3174
2622
|
logRawChunk,
|
|
3175
|
-
mergePipelineConfigs,
|
|
3176
|
-
normalizeCloseTagsHeuristic,
|
|
3177
2623
|
originalToolsSchema,
|
|
3178
|
-
repairAgainstSchemaHeuristic,
|
|
3179
2624
|
toolChoiceStream,
|
|
3180
2625
|
transformParams,
|
|
3181
2626
|
wrapGenerate,
|