@ai-sdk-tool/parser 1.0.0 → 2.0.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/index.mjs ADDED
@@ -0,0 +1,846 @@
1
+ // src/tool-call-middleware.ts
2
+ import { generateId } from "@ai-sdk/provider-utils";
3
+
4
+ // src/utils/get-potential-start-index.ts
5
+ function getPotentialStartIndex(text, searchedText) {
6
+ if (searchedText.length === 0) {
7
+ return null;
8
+ }
9
+ const directIndex = text.indexOf(searchedText);
10
+ if (directIndex !== -1) {
11
+ return directIndex;
12
+ }
13
+ for (let i = text.length - 1; i >= 0; i--) {
14
+ const suffix = text.substring(i);
15
+ if (searchedText.startsWith(suffix)) {
16
+ return i;
17
+ }
18
+ }
19
+ return null;
20
+ }
21
+
22
+ // src/utils/relaxed-json.ts
23
+ function some(array, f) {
24
+ let acc = false;
25
+ for (let i = 0; i < array.length; i++) {
26
+ const result = f(array[i], i, array);
27
+ acc = result === void 0 ? false : result;
28
+ if (acc) {
29
+ return acc;
30
+ }
31
+ }
32
+ return acc;
33
+ }
34
+ function makeLexer(tokenSpecs) {
35
+ return function(contents) {
36
+ const tokens = [];
37
+ let line = 1;
38
+ function findToken() {
39
+ const result = some(tokenSpecs, (tokenSpec) => {
40
+ const m = tokenSpec.re.exec(contents);
41
+ if (m) {
42
+ const raw = m[0];
43
+ contents = contents.slice(raw.length);
44
+ return {
45
+ raw,
46
+ matched: tokenSpec.f(m)
47
+ // Process the match using the spec's function
48
+ };
49
+ } else {
50
+ return void 0;
51
+ }
52
+ });
53
+ return result === false ? void 0 : result;
54
+ }
55
+ while (contents !== "") {
56
+ const matched = findToken();
57
+ if (!matched) {
58
+ const err = new SyntaxError(
59
+ `Unexpected character: ${contents[0]}; input: ${contents.substr(
60
+ 0,
61
+ 100
62
+ )}`
63
+ );
64
+ err.line = line;
65
+ throw err;
66
+ }
67
+ const tokenWithLine = matched.matched;
68
+ tokenWithLine.line = line;
69
+ line += matched.raw.replace(/[^\n]/g, "").length;
70
+ tokens.push(tokenWithLine);
71
+ }
72
+ return tokens;
73
+ };
74
+ }
75
+ function fStringSingle(m) {
76
+ const content = m[1].replace(
77
+ /([^'\\]|\\['bnrtf\\]|\\u[0-9a-fA-F]{4})/g,
78
+ (mm) => {
79
+ if (mm === '"') {
80
+ return '\\"';
81
+ } else if (mm === "\\'") {
82
+ return "'";
83
+ } else {
84
+ return mm;
85
+ }
86
+ }
87
+ );
88
+ const match = `"${content}"`;
89
+ return {
90
+ type: "string",
91
+ match,
92
+ // The transformed, double-quoted string representation
93
+ // Use JSON.parse on the transformed string to handle escape sequences correctly
94
+ value: JSON.parse(match)
95
+ };
96
+ }
97
+ function fStringDouble(m) {
98
+ return {
99
+ type: "string",
100
+ match: m[0],
101
+ // The raw matched string (including quotes)
102
+ value: JSON.parse(m[0])
103
+ // Use JSON.parse to handle escapes and get the value
104
+ };
105
+ }
106
+ function fIdentifier(m) {
107
+ const value = m[0];
108
+ const match = '"' + value.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + // Escape backslashes and quotes
109
+ '"';
110
+ return {
111
+ type: "string",
112
+ // Treat identifiers as strings
113
+ value,
114
+ // The original identifier name
115
+ match
116
+ // The double-quoted string representation
117
+ };
118
+ }
119
+ function fComment(m) {
120
+ const match = m[0].replace(/./g, (c) => /\s/.test(c) ? c : " ");
121
+ return {
122
+ type: " ",
123
+ // Represent comments as whitespace tokens
124
+ match,
125
+ // String containing original newlines and spaces for other chars
126
+ value: void 0
127
+ // Comments don't have a semantic value
128
+ };
129
+ }
130
+ function fNumber(m) {
131
+ return {
132
+ type: "number",
133
+ match: m[0],
134
+ // The raw matched number string
135
+ value: parseFloat(m[0])
136
+ // Convert string to number
137
+ };
138
+ }
139
+ function fKeyword(m) {
140
+ let value;
141
+ switch (m[0]) {
142
+ case "null":
143
+ value = null;
144
+ break;
145
+ case "true":
146
+ value = true;
147
+ break;
148
+ case "false":
149
+ value = false;
150
+ break;
151
+ default:
152
+ throw new Error(`Unexpected keyword: ${m[0]}`);
153
+ }
154
+ return {
155
+ type: "atom",
156
+ // Use 'atom' type for these literals
157
+ match: m[0],
158
+ // The raw matched keyword
159
+ value
160
+ // The corresponding JavaScript value
161
+ };
162
+ }
163
+ function makeTokenSpecs(relaxed) {
164
+ function f(type) {
165
+ return function(m) {
166
+ return { type, match: m[0], value: void 0 };
167
+ };
168
+ }
169
+ let tokenSpecs = [
170
+ { re: /^\s+/, f: f(" ") },
171
+ // Whitespace
172
+ { re: /^\{/, f: f("{") },
173
+ // Object start
174
+ { re: /^\}/, f: f("}") },
175
+ // Object end
176
+ { re: /^\[/, f: f("[") },
177
+ // Array start
178
+ { re: /^\]/, f: f("]") },
179
+ // Array end
180
+ { re: /^,/, f: f(",") },
181
+ // Comma separator
182
+ { re: /^:/, f: f(":") },
183
+ // Key-value separator
184
+ { re: /^(?:true|false|null)/, f: fKeyword },
185
+ // Keywords
186
+ // Number: optional sign, digits, optional decimal part, optional exponent
187
+ { re: /^\-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/, f: fNumber },
188
+ // String: double-quoted, handles escapes
189
+ { re: /^"(?:[^"\\]|\\["bnrtf\\\/]|\\u[0-9a-fA-F]{4})*"/, f: fStringDouble }
190
+ ];
191
+ if (relaxed) {
192
+ tokenSpecs = tokenSpecs.concat([
193
+ // Single-quoted strings
194
+ {
195
+ re: /^'((?:[^'\\]|\\['bnrtf\\\/]|\\u[0-9a-fA-F]{4})*)'/,
196
+ f: fStringSingle
197
+ },
198
+ // Single-line comments (// ...)
199
+ { re: /^\/\/.*?(?:\r\n|\r|\n)/, f: fComment },
200
+ // Multi-line comments (/* ... */)
201
+ { re: /^\/\*[\s\S]*?\*\//, f: fComment },
202
+ // Unquoted identifiers (treated as strings)
203
+ // Allows letters, numbers, _, -, +, ., *, ?, !, |, &, %, ^, /, #, \
204
+ { re: /^[$a-zA-Z0-9_\-+\.\*\?!\|&%\^\/#\\]+/, f: fIdentifier }
205
+ // Note: The order matters here. Identifiers are checked after keywords/numbers.
206
+ ]);
207
+ }
208
+ return tokenSpecs;
209
+ }
210
+ var lexer = makeLexer(makeTokenSpecs(true));
211
+ var strictLexer = makeLexer(makeTokenSpecs(false));
212
+ function previousNWSToken(tokens, index) {
213
+ for (; index >= 0; index--) {
214
+ if (tokens[index].type !== " ") {
215
+ return index;
216
+ }
217
+ }
218
+ return void 0;
219
+ }
220
+ function stripTrailingComma(tokens) {
221
+ const res = [];
222
+ tokens.forEach((token, index) => {
223
+ if (index > 0 && (token.type === "]" || token.type === "}")) {
224
+ const prevNWSTokenIndex = previousNWSToken(res, res.length - 1);
225
+ if (prevNWSTokenIndex !== void 0 && res[prevNWSTokenIndex].type === ",") {
226
+ const preCommaIndex = previousNWSToken(res, prevNWSTokenIndex - 1);
227
+ if (preCommaIndex !== void 0 && res[preCommaIndex].type !== "[" && res[preCommaIndex].type !== "{") {
228
+ res[prevNWSTokenIndex] = {
229
+ type: " ",
230
+ match: " ",
231
+ // Represent as a single space
232
+ value: void 0,
233
+ // Whitespace has no value
234
+ line: res[prevNWSTokenIndex].line
235
+ // Preserve original line number
236
+ };
237
+ }
238
+ }
239
+ }
240
+ res.push(token);
241
+ });
242
+ return res;
243
+ }
244
+ function popToken(tokens, state) {
245
+ const token = tokens[state.pos];
246
+ state.pos += 1;
247
+ if (!token) {
248
+ const lastLine = tokens.length !== 0 ? tokens[tokens.length - 1].line : 1;
249
+ return { type: "eof", match: "", value: void 0, line: lastLine };
250
+ }
251
+ return token;
252
+ }
253
+ function strToken(token) {
254
+ switch (token.type) {
255
+ case "atom":
256
+ case "string":
257
+ case "number":
258
+ return `${token.type} ${token.match}`;
259
+ case "eof":
260
+ return "end-of-file";
261
+ default:
262
+ return `'${token.type}'`;
263
+ }
264
+ }
265
+ function skipColon(tokens, state) {
266
+ const colon = popToken(tokens, state);
267
+ if (colon.type !== ":") {
268
+ const message = `Unexpected token: ${strToken(colon)}, expected ':'`;
269
+ if (state.tolerant) {
270
+ state.warnings.push({
271
+ message,
272
+ line: colon.line
273
+ });
274
+ state.pos -= 1;
275
+ } else {
276
+ const err = new SyntaxError(message);
277
+ err.line = colon.line;
278
+ throw err;
279
+ }
280
+ }
281
+ }
282
+ function skipPunctuation(tokens, state, valid) {
283
+ const punctuation = [",", ":", "]", "}"];
284
+ let token = popToken(tokens, state);
285
+ while (true) {
286
+ if (valid && valid.includes(token.type)) {
287
+ return token;
288
+ } else if (token.type === "eof") {
289
+ return token;
290
+ } else if (punctuation.includes(token.type)) {
291
+ const message = `Unexpected token: ${strToken(
292
+ token
293
+ )}, expected '[', '{', number, string or atom`;
294
+ if (state.tolerant) {
295
+ state.warnings.push({
296
+ message,
297
+ line: token.line
298
+ });
299
+ token = popToken(tokens, state);
300
+ } else {
301
+ const err = new SyntaxError(message);
302
+ err.line = token.line;
303
+ throw err;
304
+ }
305
+ } else {
306
+ return token;
307
+ }
308
+ }
309
+ }
310
+ function raiseError(state, token, message) {
311
+ if (state.tolerant) {
312
+ state.warnings.push({
313
+ message,
314
+ line: token.line
315
+ });
316
+ } else {
317
+ const err = new SyntaxError(message);
318
+ err.line = token.line;
319
+ throw err;
320
+ }
321
+ }
322
+ function raiseUnexpected(state, token, expected) {
323
+ raiseError(
324
+ state,
325
+ token,
326
+ `Unexpected token: ${strToken(token)}, expected ${expected}`
327
+ );
328
+ }
329
+ function checkDuplicates(state, obj, token) {
330
+ const key = String(token.value);
331
+ if (!state.duplicate && Object.prototype.hasOwnProperty.call(obj, key)) {
332
+ raiseError(state, token, `Duplicate key: ${key}`);
333
+ }
334
+ }
335
+ function appendPair(state, obj, key, value) {
336
+ const finalValue = state.reviver ? state.reviver(key, value) : value;
337
+ if (finalValue !== void 0) {
338
+ obj[key] = finalValue;
339
+ }
340
+ }
341
+ function parsePair(tokens, state, obj) {
342
+ let token = skipPunctuation(tokens, state, [":", "string", "number", "atom"]);
343
+ let key;
344
+ let value;
345
+ if (token.type !== "string") {
346
+ raiseUnexpected(state, token, "string key");
347
+ if (state.tolerant) {
348
+ switch (token.type) {
349
+ case ":":
350
+ token = {
351
+ type: "string",
352
+ value: "null",
353
+ match: '"null"',
354
+ line: token.line
355
+ };
356
+ state.pos -= 1;
357
+ break;
358
+ case "number":
359
+ // Use number as string key
360
+ case "atom":
361
+ token = {
362
+ type: "string",
363
+ value: String(token.value),
364
+ match: `"${token.value}"`,
365
+ line: token.line
366
+ };
367
+ break;
368
+ case "[":
369
+ // Assume missing key before an array
370
+ case "{":
371
+ state.pos -= 1;
372
+ value = parseAny(tokens, state);
373
+ checkDuplicates(state, obj, {
374
+ type: "string",
375
+ value: "null",
376
+ match: '"null"',
377
+ line: token.line
378
+ });
379
+ appendPair(state, obj, "null", value);
380
+ return;
381
+ // Finished parsing this "pair"
382
+ case "eof":
383
+ return;
384
+ // Cannot recover
385
+ default:
386
+ return;
387
+ }
388
+ } else {
389
+ return;
390
+ }
391
+ }
392
+ checkDuplicates(state, obj, token);
393
+ key = String(token.value);
394
+ skipColon(tokens, state);
395
+ value = parseAny(tokens, state);
396
+ appendPair(state, obj, key, value);
397
+ }
398
+ function parseElement(tokens, state, arr) {
399
+ const key = arr.length;
400
+ const value = parseAny(tokens, state);
401
+ arr[key] = state.reviver ? state.reviver(String(key), value) : value;
402
+ }
403
+ function parseObject(tokens, state) {
404
+ const obj = {};
405
+ return parseMany(tokens, state, obj, {
406
+ skip: [":", "}"],
407
+ // Initially skip over colon or closing brace (for empty/tolerant cases)
408
+ elementParser: parsePair,
409
+ // Use parsePair to parse each key-value element
410
+ elementName: "string key",
411
+ // Expected element type for errors
412
+ endSymbol: "}"
413
+ // The closing token for an object
414
+ });
415
+ }
416
+ function parseArray(tokens, state) {
417
+ const arr = [];
418
+ return parseMany(tokens, state, arr, {
419
+ skip: ["]"],
420
+ // Initially skip over closing bracket (for empty/tolerant cases)
421
+ elementParser: parseElement,
422
+ // Use parseElement to parse each array item
423
+ elementName: "json value",
424
+ // Expected element type for errors
425
+ endSymbol: "]"
426
+ // The closing token for an array
427
+ });
428
+ }
429
+ function parseMany(tokens, state, result, opts) {
430
+ let token = skipPunctuation(tokens, state, opts.skip);
431
+ if (token.type === "eof") {
432
+ raiseUnexpected(state, token, `'${opts.endSymbol}' or ${opts.elementName}`);
433
+ if (state.tolerant) {
434
+ return result;
435
+ } else {
436
+ return result;
437
+ }
438
+ }
439
+ if (token.type === opts.endSymbol) {
440
+ return result;
441
+ }
442
+ state.pos -= 1;
443
+ opts.elementParser(tokens, state, result);
444
+ while (true) {
445
+ token = popToken(tokens, state);
446
+ if (token.type !== opts.endSymbol && token.type !== ",") {
447
+ raiseUnexpected(state, token, `',' or '${opts.endSymbol}'`);
448
+ if (state.tolerant) {
449
+ if (token.type === "eof") {
450
+ return result;
451
+ }
452
+ state.pos -= 1;
453
+ } else {
454
+ return result;
455
+ }
456
+ }
457
+ switch (token.type) {
458
+ case opts.endSymbol:
459
+ return result;
460
+ case ",":
461
+ const nextToken = tokens[state.pos];
462
+ if (state.tolerant && nextToken && nextToken.type === opts.endSymbol) {
463
+ raiseError(state, token, `Trailing comma before '${opts.endSymbol}'`);
464
+ popToken(tokens, state);
465
+ return result;
466
+ }
467
+ opts.elementParser(tokens, state, result);
468
+ break;
469
+ // Default case is only reachable in tolerant mode recovery above
470
+ default:
471
+ opts.elementParser(tokens, state, result);
472
+ break;
473
+ }
474
+ }
475
+ }
476
+ function endChecks(tokens, state, ret) {
477
+ if (state.pos < tokens.length) {
478
+ if (state.tolerant) {
479
+ skipPunctuation(tokens, state);
480
+ }
481
+ if (state.pos < tokens.length) {
482
+ raiseError(
483
+ state,
484
+ tokens[state.pos],
485
+ `Unexpected token: ${strToken(tokens[state.pos])}, expected end-of-input`
486
+ );
487
+ }
488
+ }
489
+ if (state.tolerant && state.warnings.length > 0) {
490
+ const message = state.warnings.length === 1 ? state.warnings[0].message : `${state.warnings.length} parse warnings`;
491
+ const err = new SyntaxError(message);
492
+ err.line = state.warnings[0].line;
493
+ err.warnings = state.warnings;
494
+ err.obj = ret;
495
+ throw err;
496
+ }
497
+ }
498
+ function parseAny(tokens, state, end = false) {
499
+ const token = skipPunctuation(tokens, state);
500
+ let ret;
501
+ if (token.type === "eof") {
502
+ if (end) {
503
+ raiseUnexpected(state, token, "json value");
504
+ }
505
+ raiseUnexpected(state, token, "json value");
506
+ return void 0;
507
+ }
508
+ switch (token.type) {
509
+ case "{":
510
+ ret = parseObject(tokens, state);
511
+ break;
512
+ case "[":
513
+ ret = parseArray(tokens, state);
514
+ break;
515
+ case "string":
516
+ // String literal
517
+ case "number":
518
+ // Number literal
519
+ case "atom":
520
+ ret = token.value;
521
+ break;
522
+ default:
523
+ raiseUnexpected(state, token, "json value");
524
+ if (state.tolerant) {
525
+ ret = null;
526
+ } else {
527
+ return void 0;
528
+ }
529
+ }
530
+ if (end) {
531
+ ret = state.reviver ? state.reviver("", ret) : ret;
532
+ endChecks(tokens, state, ret);
533
+ }
534
+ return ret;
535
+ }
536
+ function parse(text, optsOrReviver) {
537
+ let options = {};
538
+ if (typeof optsOrReviver === "function") {
539
+ options.reviver = optsOrReviver;
540
+ } else if (optsOrReviver !== null && typeof optsOrReviver === "object") {
541
+ options = { ...optsOrReviver };
542
+ } else if (optsOrReviver !== void 0) {
543
+ throw new TypeError(
544
+ "Second argument must be a reviver function or an options object."
545
+ );
546
+ }
547
+ if (options.relaxed === void 0) {
548
+ if (options.warnings === true || options.tolerant === true) {
549
+ options.relaxed = true;
550
+ } else if (options.warnings === false && options.tolerant === false) {
551
+ options.relaxed = false;
552
+ } else {
553
+ options.relaxed = true;
554
+ }
555
+ }
556
+ options.tolerant = options.tolerant || options.warnings || false;
557
+ options.warnings = options.warnings || false;
558
+ options.duplicate = options.duplicate || false;
559
+ if (!options.relaxed && !options.warnings && !options.tolerant) {
560
+ if (!options.duplicate) {
561
+ } else {
562
+ return JSON.parse(text, options.reviver);
563
+ }
564
+ }
565
+ const lexerToUse = options.relaxed ? lexer : strictLexer;
566
+ let tokens = lexerToUse(text);
567
+ if (options.relaxed) {
568
+ tokens = stripTrailingComma(tokens);
569
+ }
570
+ if (options.warnings || options.tolerant) {
571
+ tokens = tokens.filter((token) => token.type !== " ");
572
+ const state = {
573
+ pos: 0,
574
+ reviver: options.reviver,
575
+ tolerant: options.tolerant,
576
+ duplicate: !options.duplicate,
577
+ // Internal state: true means *check* for duplicates
578
+ warnings: []
579
+ };
580
+ return parseAny(tokens, state, true);
581
+ } else {
582
+ const newtext = tokens.reduce((str, token) => {
583
+ return str + token.match;
584
+ }, "");
585
+ if (!options.relaxed && !options.warnings && !options.tolerant && options.duplicate) {
586
+ return JSON.parse(text, options.reviver);
587
+ } else if (options.warnings || options.tolerant || !options.duplicate) {
588
+ tokens = lexerToUse(text);
589
+ if (options.relaxed) {
590
+ tokens = stripTrailingComma(tokens);
591
+ }
592
+ tokens = tokens.filter((token) => token.type !== " ");
593
+ const state = {
594
+ pos: 0,
595
+ reviver: options.reviver,
596
+ tolerant: options.tolerant || false,
597
+ // Ensure boolean
598
+ duplicate: !options.duplicate,
599
+ // true = check duplicates
600
+ warnings: []
601
+ };
602
+ return parseAny(tokens, state, true);
603
+ } else {
604
+ tokens = lexer(text);
605
+ tokens = stripTrailingComma(tokens);
606
+ const newtext2 = tokens.reduce((str, token) => str + token.match, "");
607
+ return JSON.parse(newtext2, options.reviver);
608
+ }
609
+ }
610
+ }
611
+
612
+ // src/tool-call-middleware.ts
613
+ function createToolMiddleware({
614
+ toolCallTag,
615
+ toolCallEndTag,
616
+ toolResponseTag,
617
+ toolResponseEndTag,
618
+ toolSystemPromptTemplate
619
+ }) {
620
+ return {
621
+ middlewareVersion: "v2",
622
+ wrapStream: async ({ doStream }) => {
623
+ const { stream, ...rest } = await doStream();
624
+ let isFirstToolCall = true;
625
+ let isFirstText = true;
626
+ let afterSwitch = false;
627
+ let isToolCall = false;
628
+ let buffer = "";
629
+ let toolCallIndex = -1;
630
+ let toolCallBuffer = [];
631
+ const transformStream = new TransformStream({
632
+ transform(chunk, controller) {
633
+ if (chunk.type === "finish") {
634
+ if (toolCallBuffer.length > 0) {
635
+ toolCallBuffer.forEach((toolCall) => {
636
+ try {
637
+ const parsedToolCall = parse(toolCall);
638
+ controller.enqueue({
639
+ type: "tool-call",
640
+ toolCallType: "function",
641
+ toolCallId: generateId(),
642
+ toolName: parsedToolCall.name,
643
+ args: JSON.stringify(parsedToolCall.arguments)
644
+ });
645
+ } catch (e) {
646
+ console.error(`Error parsing tool call: ${toolCall}`, e);
647
+ controller.enqueue({
648
+ type: "text",
649
+ text: `Failed to parse tool call: ${e}`
650
+ });
651
+ }
652
+ });
653
+ }
654
+ controller.enqueue(chunk);
655
+ return;
656
+ } else if (chunk.type !== "text") {
657
+ controller.enqueue(chunk);
658
+ return;
659
+ }
660
+ buffer += chunk.text;
661
+ function publish(text) {
662
+ if (text.length > 0) {
663
+ const prefix = afterSwitch && (isToolCall ? !isFirstToolCall : !isFirstText) ? "\n" : "";
664
+ if (isToolCall) {
665
+ if (!toolCallBuffer[toolCallIndex]) {
666
+ toolCallBuffer[toolCallIndex] = "";
667
+ }
668
+ toolCallBuffer[toolCallIndex] += text;
669
+ } else {
670
+ controller.enqueue({
671
+ type: "text",
672
+ text: prefix + text
673
+ });
674
+ }
675
+ afterSwitch = false;
676
+ if (isToolCall) {
677
+ isFirstToolCall = false;
678
+ } else {
679
+ isFirstText = false;
680
+ }
681
+ }
682
+ }
683
+ do {
684
+ const nextTag = isToolCall ? toolCallEndTag : toolCallTag;
685
+ const startIndex = getPotentialStartIndex(buffer, nextTag);
686
+ if (startIndex == null) {
687
+ publish(buffer);
688
+ buffer = "";
689
+ break;
690
+ }
691
+ publish(buffer.slice(0, startIndex));
692
+ const foundFullMatch = startIndex + nextTag.length <= buffer.length;
693
+ if (foundFullMatch) {
694
+ buffer = buffer.slice(startIndex + nextTag.length);
695
+ toolCallIndex++;
696
+ isToolCall = !isToolCall;
697
+ afterSwitch = true;
698
+ } else {
699
+ buffer = buffer.slice(startIndex);
700
+ break;
701
+ }
702
+ } while (true);
703
+ }
704
+ });
705
+ return {
706
+ stream: stream.pipeThrough(transformStream),
707
+ ...rest
708
+ };
709
+ },
710
+ wrapGenerate: async ({ doGenerate }) => {
711
+ const result = await doGenerate();
712
+ if (result.content.length !== 1) {
713
+ return result;
714
+ }
715
+ if (result.content[0].type === "text" && !result.content[0].text.includes(toolCallTag)) {
716
+ return result;
717
+ }
718
+ const toolCallRegex = new RegExp(
719
+ `${toolCallTag}(.*?)(?:${toolCallEndTag}|$)`,
720
+ "gs"
721
+ );
722
+ const matches = result.content[0].type === "text" ? Array.from(result.content[0].text.matchAll(toolCallRegex)) : [];
723
+ const function_call_tuples = matches.map((match) => match[1] || match[2]);
724
+ const tool_calls = function_call_tuples.map(
725
+ (toolCall) => {
726
+ const parsedToolCall = parse(toolCall);
727
+ const toolName = parsedToolCall.name;
728
+ const args = parsedToolCall.arguments;
729
+ return {
730
+ type: "tool-call",
731
+ toolCallType: "function",
732
+ toolCallId: generateId(),
733
+ toolName,
734
+ args: JSON.stringify(args)
735
+ };
736
+ }
737
+ );
738
+ return {
739
+ ...result,
740
+ // TODO: Return the remaining value after extracting the tool call tag.
741
+ content: [...tool_calls]
742
+ };
743
+ },
744
+ transformParams: async ({ params }) => {
745
+ const processedPrompt = params.prompt.map((message) => {
746
+ if (message.role === "assistant") {
747
+ return {
748
+ role: "assistant",
749
+ content: message.content.map((content) => {
750
+ if (content.type === "tool-call") {
751
+ return {
752
+ type: "text",
753
+ text: `${toolCallTag}${JSON.stringify({
754
+ arguments: content.args,
755
+ name: content.toolName
756
+ })}${toolCallEndTag}`
757
+ };
758
+ }
759
+ return content;
760
+ })
761
+ };
762
+ } else if (message.role === "tool") {
763
+ return {
764
+ role: "user",
765
+ content: [
766
+ {
767
+ type: "text",
768
+ text: message.content.map(
769
+ (content) => `${toolResponseTag}${JSON.stringify({
770
+ toolName: content.toolName,
771
+ result: content.result
772
+ })}${toolResponseEndTag}`
773
+ ).join("\n")
774
+ }
775
+ ]
776
+ };
777
+ }
778
+ return message;
779
+ });
780
+ const HermesPrompt = toolSystemPromptTemplate(
781
+ JSON.stringify(Object.entries(params.tools || {}))
782
+ );
783
+ const toolSystemPrompt = processedPrompt[0].role === "system" ? [
784
+ {
785
+ role: "system",
786
+ content: HermesPrompt + "\n\n" + processedPrompt[0].content
787
+ },
788
+ ...processedPrompt.slice(1)
789
+ ] : [
790
+ {
791
+ role: "system",
792
+ content: HermesPrompt
793
+ },
794
+ ...processedPrompt
795
+ ];
796
+ return {
797
+ ...params,
798
+ prompt: toolSystemPrompt,
799
+ // set the mode back to regular and remove the default tools.
800
+ tools: [],
801
+ toolChoice: void 0
802
+ };
803
+ }
804
+ };
805
+ }
806
+
807
+ // src/index.ts
808
+ var gemmaToolMiddleware = createToolMiddleware({
809
+ toolSystemPromptTemplate(tools) {
810
+ return `You have access to functions. If you decide to invoke any of the function(s),
811
+ you MUST put it in the format of
812
+ \`\`\`tool_call
813
+ {'name': <function-name>, 'arguments': <args-dict>}
814
+ \`\`\`
815
+ You SHOULD NOT include any other text in the response if you call a function
816
+ ${tools}`;
817
+ },
818
+ toolCallTag: "```tool_call\n",
819
+ toolCallEndTag: "```",
820
+ toolResponseTag: "```tool_response\n",
821
+ toolResponseEndTag: "\n```"
822
+ });
823
+ var hermesToolMiddleware = createToolMiddleware({
824
+ toolSystemPromptTemplate(tools) {
825
+ return `You are a function calling AI model.
826
+ You are provided with function signatures within <tools></tools> XML tags.
827
+ You may call one or more functions to assist with the user query.
828
+ Don't make assumptions about what values to plug into functions.
829
+ Here are the available tools: <tools>${tools}</tools>
830
+ 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']}
831
+ For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
832
+ <tool_call>
833
+ {'arguments': <args-dict>, 'name': <function-name>}
834
+ </tool_call>`;
835
+ },
836
+ toolCallTag: "<tool_call>",
837
+ toolCallEndTag: "</tool_call>",
838
+ toolResponseTag: "<tool_response>",
839
+ toolResponseEndTag: "</tool_response>"
840
+ });
841
+ export {
842
+ createToolMiddleware,
843
+ gemmaToolMiddleware,
844
+ hermesToolMiddleware
845
+ };
846
+ //# sourceMappingURL=index.mjs.map