@ai-sdk-tool/parser 2.0.12 → 2.0.14

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({
@@ -92,9 +70,12 @@ function convertToolPrompt({
92
70
  }
93
71
  return message;
94
72
  });
95
- const HermesPrompt = toolSystemPromptTemplate(
96
- JSON.stringify(Object.entries(paramsTools || {}))
97
- );
73
+ const toolsForPrompt = (paramsTools || []).filter((tool) => tool.type === "function").map((tool) => ({
74
+ name: tool.name,
75
+ description: tool.type === "function" && typeof tool.description === "string" ? tool.description : void 0,
76
+ parameters: tool.inputSchema
77
+ }));
78
+ const HermesPrompt = toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
98
79
  const toolSystemPrompt = processedPrompt[0].role === "system" ? [
99
80
  {
100
81
  role: "system",
@@ -356,15 +337,15 @@ function makeTokenSpecs(relaxed) {
356
337
  { re: /^(?:true|false|null)/, f: fKeyword },
357
338
  // Keywords
358
339
  // Number: optional sign, digits, optional decimal part, optional exponent
359
- { re: /^\-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/, f: fNumber },
340
+ { re: /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/, f: fNumber },
360
341
  // String: double-quoted, handles escapes
361
- { re: /^"(?:[^"\\]|\\["bnrtf\\\/]|\\u[0-9a-fA-F]{4})*"/, f: fStringDouble }
342
+ { re: /^"(?:[^"\\]|\\["bnrtf\\/]|\\u[0-9a-fA-F]{4})*"/, f: fStringDouble }
362
343
  ];
363
344
  if (relaxed) {
364
345
  tokenSpecs = tokenSpecs.concat([
365
346
  // Single-quoted strings
366
347
  {
367
- re: /^'((?:[^'\\]|\\['bnrtf\\\/]|\\u[0-9a-fA-F]{4})*)'/,
348
+ re: /^'((?:[^'\\]|\\['bnrtf\\/]|\\u[0-9a-fA-F]{4})*)'/,
368
349
  f: fStringSingle
369
350
  },
370
351
  // Single-line comments (// ...)
@@ -373,7 +354,7 @@ function makeTokenSpecs(relaxed) {
373
354
  { re: /^\/\*[\s\S]*?\*\//, f: fComment },
374
355
  // Unquoted identifiers (treated as strings)
375
356
  // Allows letters, numbers, _, -, +, ., *, ?, !, |, &, %, ^, /, #, \
376
- { re: /^[$a-zA-Z0-9_\-+\.\*\?!\|&%\^\/#\\]+/, f: fIdentifier }
357
+ { re: /^[$a-zA-Z0-9_\-+.*?!|&%^/#\\]+/, f: fIdentifier }
377
358
  // Note: The order matters here. Identifiers are checked after keywords/numbers.
378
359
  ]);
379
360
  }
@@ -636,7 +617,7 @@ function parseMany(tokens, state, result, opts) {
636
617
  switch (token.type) {
637
618
  case opts.endSymbol:
638
619
  return result;
639
- case ",":
620
+ case ",": {
640
621
  const nextToken = tokens[state.pos];
641
622
  if (state.tolerant && nextToken && nextToken.type === opts.endSymbol) {
642
623
  raiseError(state, token, `Trailing comma before '${opts.endSymbol}'`);
@@ -645,6 +626,7 @@ function parseMany(tokens, state, result, opts) {
645
626
  }
646
627
  opts.elementParser(tokens, state, result);
647
628
  break;
629
+ }
648
630
  // Default case is only reachable in tolerant mode recovery above
649
631
  default:
650
632
  opts.elementParser(tokens, state, result);
@@ -713,6 +695,7 @@ function parseAny(tokens, state, end = false) {
713
695
  return ret;
714
696
  }
715
697
  function parse(text, optsOrReviver) {
698
+ var _a;
716
699
  let options = {};
717
700
  if (typeof optsOrReviver === "function") {
718
701
  options.reviver = optsOrReviver;
@@ -734,7 +717,7 @@ function parse(text, optsOrReviver) {
734
717
  }
735
718
  options.tolerant = options.tolerant || options.warnings || false;
736
719
  options.warnings = options.warnings || false;
737
- options.duplicate = options.duplicate || false;
720
+ options.duplicate = (_a = options.duplicate) != null ? _a : false;
738
721
  if (!options.relaxed && !options.warnings && !options.tolerant) {
739
722
  if (!options.duplicate) {
740
723
  } else {
@@ -752,8 +735,8 @@ function parse(text, optsOrReviver) {
752
735
  pos: 0,
753
736
  reviver: options.reviver,
754
737
  tolerant: options.tolerant,
755
- duplicate: !options.duplicate,
756
- // Internal state: true means *check* for duplicates
738
+ duplicate: options.duplicate,
739
+ // true = allow duplicate keys, false = reject duplicates
757
740
  warnings: []
758
741
  };
759
742
  return parseAny(tokens, state, true);
@@ -774,8 +757,8 @@ function parse(text, optsOrReviver) {
774
757
  reviver: options.reviver,
775
758
  tolerant: options.tolerant || false,
776
759
  // Ensure boolean
777
- duplicate: !options.duplicate,
778
- // true = check duplicates
760
+ duplicate: options.duplicate,
761
+ // true = allow duplicate keys, false = reject duplicates
779
762
  warnings: []
780
763
  };
781
764
  return parseAny(tokens, state, true);
@@ -812,12 +795,13 @@ function stringify(obj) {
812
795
  }
813
796
 
814
797
  // src/stream-handler.ts
815
- var import_provider_utils = require("@ai-sdk/provider-utils");
798
+ import { generateId } from "@ai-sdk/provider-utils";
816
799
  async function normalToolStream({
817
800
  doStream,
818
801
  toolCallTag,
819
802
  toolCallEndTag
820
803
  }) {
804
+ var _a;
821
805
  const { stream, ...rest } = await doStream();
822
806
  let isFirstToolCall = true;
823
807
  let isFirstText = true;
@@ -831,6 +815,25 @@ async function normalToolStream({
831
815
  const transformStream = new TransformStream({
832
816
  transform(chunk, controller) {
833
817
  if (chunk.type === "finish") {
818
+ if (isToolCall && (buffer.length > 0 || toolCallIndex >= 0 && toolCallBuffer[toolCallIndex])) {
819
+ if (!currentTextId) {
820
+ currentTextId = generateId();
821
+ controller.enqueue({
822
+ type: "text-start",
823
+ id: currentTextId
824
+ });
825
+ hasEmittedTextStart = true;
826
+ }
827
+ const incompleteContent = (toolCallBuffer[toolCallIndex] || "") + buffer;
828
+ controller.enqueue({
829
+ type: "text-delta",
830
+ id: currentTextId,
831
+ delta: toolCallTag + incompleteContent
832
+ });
833
+ if (toolCallIndex >= 0) {
834
+ toolCallBuffer = toolCallBuffer.slice(0, toolCallIndex);
835
+ }
836
+ }
834
837
  if (currentTextId && hasEmittedTextStart) {
835
838
  controller.enqueue({
836
839
  type: "text-end",
@@ -845,13 +848,13 @@ async function normalToolStream({
845
848
  const parsedToolCall = relaxed_json_exports.parse(toolCall);
846
849
  controller.enqueue({
847
850
  type: "tool-call",
848
- toolCallId: (0, import_provider_utils.generateId)(),
851
+ toolCallId: generateId(),
849
852
  toolName: parsedToolCall.name,
850
853
  input: JSON.stringify(parsedToolCall.arguments)
851
854
  });
852
855
  } catch (e) {
853
856
  console.error(`Error parsing tool call: ${toolCall}`, e);
854
- const errorId = (0, import_provider_utils.generateId)();
857
+ const errorId = generateId();
855
858
  controller.enqueue({
856
859
  type: "text-start",
857
860
  id: errorId
@@ -859,7 +862,7 @@ async function normalToolStream({
859
862
  controller.enqueue({
860
863
  type: "text-delta",
861
864
  id: errorId,
862
- delta: `Failed to parse tool call: ${e}`
865
+ delta: `${toolCallTag}${toolCall}${toolCallEndTag}`
863
866
  });
864
867
  controller.enqueue({
865
868
  type: "text-end",
@@ -876,7 +879,7 @@ async function normalToolStream({
876
879
  }
877
880
  buffer += chunk.delta;
878
881
  function publish(text) {
879
- if (text.length > 0) {
882
+ if (text.length > 0 || isToolCall) {
880
883
  const prefix = afterSwitch && (isToolCall ? !isFirstToolCall : !isFirstText) ? "\n" : "";
881
884
  if (isToolCall) {
882
885
  if (currentTextId && hasEmittedTextStart) {
@@ -891,9 +894,9 @@ async function normalToolStream({
891
894
  toolCallBuffer[toolCallIndex] = "";
892
895
  }
893
896
  toolCallBuffer[toolCallIndex] += text;
894
- } else {
897
+ } else if (text.length > 0) {
895
898
  if (!currentTextId) {
896
- currentTextId = (0, import_provider_utils.generateId)();
899
+ currentTextId = generateId();
897
900
  controller.enqueue({
898
901
  type: "text-start",
899
902
  id: currentTextId
@@ -936,24 +939,30 @@ async function normalToolStream({
936
939
  }
937
940
  });
938
941
  return {
939
- stream: stream.pipeThrough(transformStream),
942
+ stream: (_a = stream == null ? void 0 : stream.pipeThrough(transformStream)) != null ? _a : new ReadableStream(),
940
943
  ...rest
941
944
  };
942
945
  }
943
946
  async function toolChoiceStream({
944
947
  doGenerate
945
948
  }) {
949
+ var _a;
946
950
  const result = await doGenerate();
947
- const toolJson = result.content[0].type === "text" ? JSON.parse(result.content[0].text) : {};
951
+ 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
952
  const toolCallChunk = {
949
953
  type: "tool-call",
950
- toolCallId: (0, import_provider_utils.generateId)(),
951
- toolName: toolJson.name,
954
+ toolCallId: generateId(),
955
+ toolName: toolJson.name || "unknown",
952
956
  input: JSON.stringify(toolJson.arguments || {})
953
957
  };
954
958
  const finishChunk = {
955
959
  type: "finish",
956
- usage: result.usage,
960
+ usage: (result == null ? void 0 : result.usage) || // TODO: If possible, try to return a certain amount of LLM usage.
961
+ {
962
+ inputTokens: 0,
963
+ outputTokens: 0,
964
+ totalTokens: 0
965
+ },
957
966
  finishReason: "tool-calls"
958
967
  };
959
968
  const stream = new ReadableStream({
@@ -964,8 +973,8 @@ async function toolChoiceStream({
964
973
  }
965
974
  });
966
975
  return {
967
- request: result.request,
968
- response: result.response,
976
+ request: (result == null ? void 0 : result.request) || {},
977
+ response: (result == null ? void 0 : result.response) || {},
969
978
  stream
970
979
  };
971
980
  }
@@ -1011,8 +1020,8 @@ function createToolMiddleware({
1011
1020
  {
1012
1021
  type: "tool-call",
1013
1022
  toolCallType: "function",
1014
- toolCallId: (0, import_provider_utils2.generateId)(),
1015
- toolName: toolJson.name,
1023
+ toolCallId: generateId2(),
1024
+ toolName: toolJson.name || "unknown",
1016
1025
  input: JSON.stringify(toolJson.arguments || {})
1017
1026
  }
1018
1027
  ]
@@ -1043,7 +1052,7 @@ function createToolMiddleware({
1043
1052
  }
1044
1053
  return {
1045
1054
  type: "tool-call",
1046
- toolCallId: (0, import_provider_utils2.generateId)(),
1055
+ toolCallId: generateId2(),
1047
1056
  toolName: parsedToolCall.name,
1048
1057
  // Ensure args is always a JSON string
1049
1058
  input: typeof parsedToolCall.arguments === "string" ? parsedToolCall.arguments : JSON.stringify(parsedToolCall.arguments)
@@ -1121,7 +1130,7 @@ function createToolMiddleware({
1121
1130
  if (((_b = params.toolChoice) == null ? void 0 : _b.type) === "tool") {
1122
1131
  const selectedToolName = params.toolChoice.toolName;
1123
1132
  const selectedTool = (_c = params.tools) == null ? void 0 : _c.find(
1124
- (tool) => tool.name === selectedToolName
1133
+ (tool) => tool.type === "function" ? tool.name === selectedToolName : tool.id === selectedToolName
1125
1134
  );
1126
1135
  if (!selectedTool) {
1127
1136
  throw new Error(
@@ -1216,10 +1225,9 @@ For each function call return a json object with function name and arguments wit
1216
1225
  toolResponseTag: "<tool_response>",
1217
1226
  toolResponseEndTag: "</tool_response>"
1218
1227
  });
1219
- // Annotate the CommonJS export names for ESM import in node:
1220
- 0 && (module.exports = {
1228
+ export {
1221
1229
  createToolMiddleware,
1222
1230
  gemmaToolMiddleware,
1223
1231
  hermesToolMiddleware
1224
- });
1232
+ };
1225
1233
  //# sourceMappingURL=index.js.map