@ai-sdk-tool/parser 2.0.12 → 2.0.13

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.js CHANGED
@@ -1,33 +1,11 @@
1
- "use strict";
2
1
  var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
2
  var __export = (target, all) => {
7
3
  for (var name in all)
8
4
  __defProp(target, name, { get: all[name], enumerable: true });
9
5
  };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- createToolMiddleware: () => createToolMiddleware,
24
- gemmaToolMiddleware: () => gemmaToolMiddleware,
25
- hermesToolMiddleware: () => hermesToolMiddleware
26
- });
27
- module.exports = __toCommonJS(index_exports);
28
6
 
29
7
  // src/tool-call-middleware.ts
30
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
8
+ import { generateId as generateId2 } from "@ai-sdk/provider-utils";
31
9
 
32
10
  // src/utils/conv-tool-prompt.ts
33
11
  function convertToolPrompt({
@@ -356,15 +334,15 @@ function makeTokenSpecs(relaxed) {
356
334
  { re: /^(?:true|false|null)/, f: fKeyword },
357
335
  // Keywords
358
336
  // Number: optional sign, digits, optional decimal part, optional exponent
359
- { re: /^\-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/, f: fNumber },
337
+ { re: /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/, f: fNumber },
360
338
  // String: double-quoted, handles escapes
361
- { re: /^"(?:[^"\\]|\\["bnrtf\\\/]|\\u[0-9a-fA-F]{4})*"/, f: fStringDouble }
339
+ { re: /^"(?:[^"\\]|\\["bnrtf\\/]|\\u[0-9a-fA-F]{4})*"/, f: fStringDouble }
362
340
  ];
363
341
  if (relaxed) {
364
342
  tokenSpecs = tokenSpecs.concat([
365
343
  // Single-quoted strings
366
344
  {
367
- re: /^'((?:[^'\\]|\\['bnrtf\\\/]|\\u[0-9a-fA-F]{4})*)'/,
345
+ re: /^'((?:[^'\\]|\\['bnrtf\\/]|\\u[0-9a-fA-F]{4})*)'/,
368
346
  f: fStringSingle
369
347
  },
370
348
  // Single-line comments (// ...)
@@ -373,7 +351,7 @@ function makeTokenSpecs(relaxed) {
373
351
  { re: /^\/\*[\s\S]*?\*\//, f: fComment },
374
352
  // Unquoted identifiers (treated as strings)
375
353
  // Allows letters, numbers, _, -, +, ., *, ?, !, |, &, %, ^, /, #, \
376
- { re: /^[$a-zA-Z0-9_\-+\.\*\?!\|&%\^\/#\\]+/, f: fIdentifier }
354
+ { re: /^[$a-zA-Z0-9_\-+.*?!|&%^/#\\]+/, f: fIdentifier }
377
355
  // Note: The order matters here. Identifiers are checked after keywords/numbers.
378
356
  ]);
379
357
  }
@@ -636,7 +614,7 @@ function parseMany(tokens, state, result, opts) {
636
614
  switch (token.type) {
637
615
  case opts.endSymbol:
638
616
  return result;
639
- case ",":
617
+ case ",": {
640
618
  const nextToken = tokens[state.pos];
641
619
  if (state.tolerant && nextToken && nextToken.type === opts.endSymbol) {
642
620
  raiseError(state, token, `Trailing comma before '${opts.endSymbol}'`);
@@ -645,6 +623,7 @@ function parseMany(tokens, state, result, opts) {
645
623
  }
646
624
  opts.elementParser(tokens, state, result);
647
625
  break;
626
+ }
648
627
  // Default case is only reachable in tolerant mode recovery above
649
628
  default:
650
629
  opts.elementParser(tokens, state, result);
@@ -713,6 +692,7 @@ function parseAny(tokens, state, end = false) {
713
692
  return ret;
714
693
  }
715
694
  function parse(text, optsOrReviver) {
695
+ var _a;
716
696
  let options = {};
717
697
  if (typeof optsOrReviver === "function") {
718
698
  options.reviver = optsOrReviver;
@@ -734,7 +714,7 @@ function parse(text, optsOrReviver) {
734
714
  }
735
715
  options.tolerant = options.tolerant || options.warnings || false;
736
716
  options.warnings = options.warnings || false;
737
- options.duplicate = options.duplicate || false;
717
+ options.duplicate = (_a = options.duplicate) != null ? _a : false;
738
718
  if (!options.relaxed && !options.warnings && !options.tolerant) {
739
719
  if (!options.duplicate) {
740
720
  } else {
@@ -752,8 +732,8 @@ function parse(text, optsOrReviver) {
752
732
  pos: 0,
753
733
  reviver: options.reviver,
754
734
  tolerant: options.tolerant,
755
- duplicate: !options.duplicate,
756
- // Internal state: true means *check* for duplicates
735
+ duplicate: options.duplicate,
736
+ // true = allow duplicate keys, false = reject duplicates
757
737
  warnings: []
758
738
  };
759
739
  return parseAny(tokens, state, true);
@@ -774,8 +754,8 @@ function parse(text, optsOrReviver) {
774
754
  reviver: options.reviver,
775
755
  tolerant: options.tolerant || false,
776
756
  // Ensure boolean
777
- duplicate: !options.duplicate,
778
- // true = check duplicates
757
+ duplicate: options.duplicate,
758
+ // true = allow duplicate keys, false = reject duplicates
779
759
  warnings: []
780
760
  };
781
761
  return parseAny(tokens, state, true);
@@ -812,12 +792,13 @@ function stringify(obj) {
812
792
  }
813
793
 
814
794
  // src/stream-handler.ts
815
- var import_provider_utils = require("@ai-sdk/provider-utils");
795
+ import { generateId } from "@ai-sdk/provider-utils";
816
796
  async function normalToolStream({
817
797
  doStream,
818
798
  toolCallTag,
819
799
  toolCallEndTag
820
800
  }) {
801
+ var _a;
821
802
  const { stream, ...rest } = await doStream();
822
803
  let isFirstToolCall = true;
823
804
  let isFirstText = true;
@@ -831,6 +812,25 @@ async function normalToolStream({
831
812
  const transformStream = new TransformStream({
832
813
  transform(chunk, controller) {
833
814
  if (chunk.type === "finish") {
815
+ if (isToolCall && (buffer.length > 0 || toolCallIndex >= 0 && toolCallBuffer[toolCallIndex])) {
816
+ if (!currentTextId) {
817
+ currentTextId = generateId();
818
+ controller.enqueue({
819
+ type: "text-start",
820
+ id: currentTextId
821
+ });
822
+ hasEmittedTextStart = true;
823
+ }
824
+ const incompleteContent = (toolCallBuffer[toolCallIndex] || "") + buffer;
825
+ controller.enqueue({
826
+ type: "text-delta",
827
+ id: currentTextId,
828
+ delta: toolCallTag + incompleteContent
829
+ });
830
+ if (toolCallIndex >= 0) {
831
+ toolCallBuffer = toolCallBuffer.slice(0, toolCallIndex);
832
+ }
833
+ }
834
834
  if (currentTextId && hasEmittedTextStart) {
835
835
  controller.enqueue({
836
836
  type: "text-end",
@@ -845,13 +845,13 @@ async function normalToolStream({
845
845
  const parsedToolCall = relaxed_json_exports.parse(toolCall);
846
846
  controller.enqueue({
847
847
  type: "tool-call",
848
- toolCallId: (0, import_provider_utils.generateId)(),
848
+ toolCallId: generateId(),
849
849
  toolName: parsedToolCall.name,
850
850
  input: JSON.stringify(parsedToolCall.arguments)
851
851
  });
852
852
  } catch (e) {
853
853
  console.error(`Error parsing tool call: ${toolCall}`, e);
854
- const errorId = (0, import_provider_utils.generateId)();
854
+ const errorId = generateId();
855
855
  controller.enqueue({
856
856
  type: "text-start",
857
857
  id: errorId
@@ -859,7 +859,7 @@ async function normalToolStream({
859
859
  controller.enqueue({
860
860
  type: "text-delta",
861
861
  id: errorId,
862
- delta: `Failed to parse tool call: ${e}`
862
+ delta: `${toolCallTag}${toolCall}${toolCallEndTag}`
863
863
  });
864
864
  controller.enqueue({
865
865
  type: "text-end",
@@ -876,7 +876,7 @@ async function normalToolStream({
876
876
  }
877
877
  buffer += chunk.delta;
878
878
  function publish(text) {
879
- if (text.length > 0) {
879
+ if (text.length > 0 || isToolCall) {
880
880
  const prefix = afterSwitch && (isToolCall ? !isFirstToolCall : !isFirstText) ? "\n" : "";
881
881
  if (isToolCall) {
882
882
  if (currentTextId && hasEmittedTextStart) {
@@ -891,9 +891,9 @@ async function normalToolStream({
891
891
  toolCallBuffer[toolCallIndex] = "";
892
892
  }
893
893
  toolCallBuffer[toolCallIndex] += text;
894
- } else {
894
+ } else if (text.length > 0) {
895
895
  if (!currentTextId) {
896
- currentTextId = (0, import_provider_utils.generateId)();
896
+ currentTextId = generateId();
897
897
  controller.enqueue({
898
898
  type: "text-start",
899
899
  id: currentTextId
@@ -936,24 +936,30 @@ async function normalToolStream({
936
936
  }
937
937
  });
938
938
  return {
939
- stream: stream.pipeThrough(transformStream),
939
+ stream: (_a = stream == null ? void 0 : stream.pipeThrough(transformStream)) != null ? _a : new ReadableStream(),
940
940
  ...rest
941
941
  };
942
942
  }
943
943
  async function toolChoiceStream({
944
944
  doGenerate
945
945
  }) {
946
+ var _a;
946
947
  const result = await doGenerate();
947
- const toolJson = result.content[0].type === "text" ? JSON.parse(result.content[0].text) : {};
948
+ const toolJson = (result == null ? void 0 : result.content) && result.content.length > 0 && ((_a = result.content[0]) == null ? void 0 : _a.type) === "text" ? JSON.parse(result.content[0].text) : {};
948
949
  const toolCallChunk = {
949
950
  type: "tool-call",
950
- toolCallId: (0, import_provider_utils.generateId)(),
951
- toolName: toolJson.name,
951
+ toolCallId: generateId(),
952
+ toolName: toolJson.name || "unknown",
952
953
  input: JSON.stringify(toolJson.arguments || {})
953
954
  };
954
955
  const finishChunk = {
955
956
  type: "finish",
956
- usage: result.usage,
957
+ usage: (result == null ? void 0 : result.usage) || // TODO: If possible, try to return a certain amount of LLM usage.
958
+ {
959
+ inputTokens: 0,
960
+ outputTokens: 0,
961
+ totalTokens: 0
962
+ },
957
963
  finishReason: "tool-calls"
958
964
  };
959
965
  const stream = new ReadableStream({
@@ -964,8 +970,8 @@ async function toolChoiceStream({
964
970
  }
965
971
  });
966
972
  return {
967
- request: result.request,
968
- response: result.response,
973
+ request: (result == null ? void 0 : result.request) || {},
974
+ response: (result == null ? void 0 : result.response) || {},
969
975
  stream
970
976
  };
971
977
  }
@@ -1011,8 +1017,8 @@ function createToolMiddleware({
1011
1017
  {
1012
1018
  type: "tool-call",
1013
1019
  toolCallType: "function",
1014
- toolCallId: (0, import_provider_utils2.generateId)(),
1015
- toolName: toolJson.name,
1020
+ toolCallId: generateId2(),
1021
+ toolName: toolJson.name || "unknown",
1016
1022
  input: JSON.stringify(toolJson.arguments || {})
1017
1023
  }
1018
1024
  ]
@@ -1043,7 +1049,7 @@ function createToolMiddleware({
1043
1049
  }
1044
1050
  return {
1045
1051
  type: "tool-call",
1046
- toolCallId: (0, import_provider_utils2.generateId)(),
1052
+ toolCallId: generateId2(),
1047
1053
  toolName: parsedToolCall.name,
1048
1054
  // Ensure args is always a JSON string
1049
1055
  input: typeof parsedToolCall.arguments === "string" ? parsedToolCall.arguments : JSON.stringify(parsedToolCall.arguments)
@@ -1121,7 +1127,7 @@ function createToolMiddleware({
1121
1127
  if (((_b = params.toolChoice) == null ? void 0 : _b.type) === "tool") {
1122
1128
  const selectedToolName = params.toolChoice.toolName;
1123
1129
  const selectedTool = (_c = params.tools) == null ? void 0 : _c.find(
1124
- (tool) => tool.name === selectedToolName
1130
+ (tool) => tool.type === "function" ? tool.name === selectedToolName : tool.id === selectedToolName
1125
1131
  );
1126
1132
  if (!selectedTool) {
1127
1133
  throw new Error(
@@ -1216,10 +1222,9 @@ For each function call return a json object with function name and arguments wit
1216
1222
  toolResponseTag: "<tool_response>",
1217
1223
  toolResponseEndTag: "</tool_response>"
1218
1224
  });
1219
- // Annotate the CommonJS export names for ESM import in node:
1220
- 0 && (module.exports = {
1225
+ export {
1221
1226
  createToolMiddleware,
1222
1227
  gemmaToolMiddleware,
1223
1228
  hermesToolMiddleware
1224
- });
1229
+ };
1225
1230
  //# sourceMappingURL=index.js.map