@ai-sdk-tool/parser 2.1.4 → 2.1.6

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.cjs CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,24 +17,30 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
21
31
  var index_exports = {};
22
32
  __export(index_exports, {
23
33
  RJSON: () => robust_json_exports,
24
- coerceBySchema: () => coerceBySchema,
25
- coerceToolCallInput: () => coerceToolCallInput,
26
34
  createDynamicIfThenElseSchema: () => createDynamicIfThenElseSchema,
27
35
  createToolMiddleware: () => createToolMiddleware,
36
+ decodeOriginalTools: () => decodeOriginalTools,
37
+ encodeOriginalTools: () => encodeOriginalTools,
28
38
  escapeRegExp: () => escapeRegExp,
29
39
  extractOnErrorOption: () => extractOnErrorOption,
30
- fixToolCallWithSchema: () => fixToolCallWithSchema,
40
+ extractToolNamesFromOriginalTools: () => extractToolNamesFromOriginalTools,
31
41
  gemmaToolMiddleware: () => gemmaToolMiddleware,
32
42
  getDebugLevel: () => getDebugLevel,
33
- getFunctionTools: () => getFunctionTools,
34
43
  getPotentialStartIndex: () => getPotentialStartIndex,
35
- getSchemaType: () => getSchemaType,
36
44
  hasInputProperty: () => hasInputProperty,
37
45
  hermesToolMiddleware: () => hermesToolMiddleware,
38
46
  isToolCallContent: () => isToolCallContent,
@@ -43,7 +51,7 @@ __export(index_exports, {
43
51
  logParsedSummary: () => logParsedSummary,
44
52
  logRawChunk: () => logRawChunk,
45
53
  morphXmlProtocol: () => morphXmlProtocol,
46
- unwrapJsonSchema: () => unwrapJsonSchema,
54
+ originalToolsSchema: () => originalToolsSchema,
47
55
  xmlToolMiddleware: () => xmlToolMiddleware
48
56
  });
49
57
  module.exports = __toCommonJS(index_exports);
@@ -766,210 +774,6 @@ function stringify(obj) {
766
774
  return "null";
767
775
  }
768
776
 
769
- // src/utils/coercion.ts
770
- function unwrapJsonSchema(schema) {
771
- if (!schema || typeof schema !== "object") return schema;
772
- const s = schema;
773
- if (s.jsonSchema && typeof s.jsonSchema === "object") {
774
- return unwrapJsonSchema(s.jsonSchema);
775
- }
776
- return schema;
777
- }
778
- function getSchemaType(schema) {
779
- const unwrapped = unwrapJsonSchema(schema);
780
- if (!unwrapped || typeof unwrapped !== "object") return void 0;
781
- const t = unwrapped.type;
782
- if (typeof t === "string") return t;
783
- if (Array.isArray(t)) {
784
- const preferred = [
785
- "object",
786
- "array",
787
- "boolean",
788
- "number",
789
- "integer",
790
- "string"
791
- ];
792
- for (const p of preferred) if (t.includes(p)) return p;
793
- }
794
- const s = unwrapped;
795
- if (s && typeof s === "object" && (s.properties || s.additionalProperties)) {
796
- return "object";
797
- }
798
- if (s && typeof s === "object" && (s.items || s.prefixItems)) {
799
- return "array";
800
- }
801
- return void 0;
802
- }
803
- function coerceBySchema(value, schema) {
804
- const unwrapped = unwrapJsonSchema(schema);
805
- if (!unwrapped || typeof unwrapped !== "object") {
806
- if (typeof value === "string") {
807
- const s = value.trim();
808
- const lower = s.toLowerCase();
809
- if (lower === "true") return true;
810
- if (lower === "false") return false;
811
- if (/^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) {
812
- const num = Number(s);
813
- if (Number.isFinite(num)) return num;
814
- }
815
- if (s.startsWith("{") && s.endsWith("}") || s.startsWith("[") && s.endsWith("]")) {
816
- try {
817
- const parsed = JSON.parse(s);
818
- return coerceBySchema(parsed, void 0);
819
- } catch (e) {
820
- }
821
- }
822
- }
823
- return value;
824
- }
825
- const schemaType = getSchemaType(unwrapped);
826
- if (typeof value === "string") {
827
- const s = value.trim();
828
- if (schemaType === "object") {
829
- try {
830
- let normalized = s.replace(/'/g, '"');
831
- normalized = normalized.replace(/^\{\s*\}$/s, "{}");
832
- const obj = JSON.parse(normalized);
833
- if (obj && typeof obj === "object" && !Array.isArray(obj)) {
834
- const props = unwrapped.properties;
835
- const out = {};
836
- for (const [k, v] of Object.entries(obj)) {
837
- const propSchema = props ? props[k] : void 0;
838
- out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
839
- }
840
- return out;
841
- }
842
- } catch (e) {
843
- }
844
- }
845
- if (schemaType === "array") {
846
- try {
847
- const normalized = s.replace(/'/g, '"');
848
- const arr = JSON.parse(normalized);
849
- if (Array.isArray(arr)) {
850
- const u = unwrapped;
851
- const prefixItems = Array.isArray(
852
- u.prefixItems
853
- ) ? u.prefixItems : void 0;
854
- const itemsSchema = u.items;
855
- if (prefixItems && arr.length === prefixItems.length) {
856
- return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
857
- }
858
- return arr.map((v) => coerceBySchema(v, itemsSchema));
859
- }
860
- } catch (e) {
861
- const csv = s.includes("\n") ? s.split(/\n+/) : s.split(/,\s*/);
862
- const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);
863
- const u = unwrapped;
864
- const prefixItems = Array.isArray(
865
- u.prefixItems
866
- ) ? u.prefixItems : void 0;
867
- const itemsSchema = u.items;
868
- if (prefixItems && trimmed.length === prefixItems.length) {
869
- return trimmed.map((x, i) => coerceBySchema(x, prefixItems[i]));
870
- }
871
- return trimmed.map((x) => coerceBySchema(x, itemsSchema));
872
- }
873
- }
874
- }
875
- if (schemaType === "object" && value && typeof value === "object" && !Array.isArray(value)) {
876
- const out = {};
877
- const props = unwrapped.properties;
878
- for (const [k, v] of Object.entries(value)) {
879
- const propSchema = props ? props[k] : void 0;
880
- out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
881
- }
882
- return out;
883
- }
884
- if (schemaType === "array") {
885
- const u = unwrapped;
886
- const itemsSchema = u.items;
887
- const prefixItems = Array.isArray(
888
- u.prefixItems
889
- ) ? u.prefixItems : void 0;
890
- if (Array.isArray(value)) {
891
- if (prefixItems && value.length === prefixItems.length) {
892
- return value.map((v, i) => coerceBySchema(v, prefixItems[i]));
893
- }
894
- return value.map((v) => coerceBySchema(v, itemsSchema));
895
- }
896
- if (value && typeof value === "object") {
897
- const maybe = value;
898
- if (Object.prototype.hasOwnProperty.call(maybe, "item")) {
899
- const items = maybe.item;
900
- const arr = Array.isArray(items) ? items : [items];
901
- if (prefixItems && arr.length === prefixItems.length) {
902
- return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
903
- }
904
- return arr.map((v) => coerceBySchema(v, itemsSchema));
905
- }
906
- const keys = Object.keys(maybe);
907
- if (keys.length === 1) {
908
- const singleKey = keys[0];
909
- const singleValue = maybe[singleKey];
910
- if (Array.isArray(singleValue)) {
911
- const coercedArray = singleValue.map(
912
- (v) => coerceBySchema(v, itemsSchema)
913
- );
914
- return coercedArray;
915
- }
916
- }
917
- if (keys.length > 0 && keys.every((k) => /^\d+$/.test(k))) {
918
- const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
919
- if (prefixItems && arr.length === prefixItems.length) {
920
- return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
921
- }
922
- return arr.map((v) => coerceBySchema(v, itemsSchema));
923
- }
924
- }
925
- if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
926
- if (prefixItems && prefixItems.length > 0) {
927
- return [coerceBySchema(value, prefixItems[0])];
928
- }
929
- return [coerceBySchema(value, itemsSchema)];
930
- }
931
- }
932
- if (typeof value === "string") {
933
- const s = value.trim();
934
- if (schemaType === "boolean") {
935
- const lower = s.toLowerCase();
936
- if (lower === "true") return true;
937
- if (lower === "false") return false;
938
- }
939
- if (schemaType === "number" || schemaType === "integer") {
940
- if (/^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) {
941
- const num = Number(s);
942
- if (Number.isFinite(num)) return num;
943
- }
944
- }
945
- }
946
- return value;
947
- }
948
- function fixToolCallWithSchema(part, tools) {
949
- var _a;
950
- if (part.type !== "tool-call") return part;
951
- const tc = part;
952
- let args = {};
953
- if (typeof tc.input === "string") {
954
- try {
955
- args = JSON.parse(tc.input);
956
- } catch (e) {
957
- return part;
958
- }
959
- } else if (tc.input && typeof tc.input === "object") {
960
- args = tc.input;
961
- }
962
- const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
963
- const coerced = coerceBySchema(args, schema);
964
- return {
965
- ...part,
966
- input: JSON.stringify(coerced != null ? coerced : {})
967
- };
968
- }
969
- function coerceToolCallInput(part, tools) {
970
- return fixToolCallWithSchema(part, tools);
971
- }
972
-
973
777
  // src/utils/debug.ts
974
778
  function normalizeBooleanString(value) {
975
779
  const normalized = value.trim().toLowerCase();
@@ -1057,33 +861,34 @@ function extractOnErrorOption(providerOptions) {
1057
861
  return void 0;
1058
862
  }
1059
863
 
1060
- // src/utils/tools.ts
864
+ // src/utils/provider-options.ts
865
+ var originalToolsSchema = {
866
+ encode: encodeOriginalTools,
867
+ decode: decodeOriginalTools
868
+ };
869
+ function encodeOriginalTools(tools) {
870
+ return (tools == null ? void 0 : tools.map((t) => ({
871
+ name: t.name,
872
+ inputSchema: JSON.stringify(t.inputSchema)
873
+ }))) || [];
874
+ }
875
+ function decodeOriginalTools(originalTools) {
876
+ const tools = (originalTools == null ? void 0 : originalTools.map(
877
+ (t) => ({
878
+ name: t.name,
879
+ inputSchema: JSON.parse(t.inputSchema)
880
+ })
881
+ )) || [];
882
+ return tools;
883
+ }
884
+ function extractToolNamesFromOriginalTools(originalTools) {
885
+ return (originalTools == null ? void 0 : originalTools.map((t) => t.name)) || [];
886
+ }
1061
887
  function isToolChoiceActive(params) {
1062
888
  var _a, _b, _c;
1063
889
  const toolChoice = (_b = (_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware) == null ? void 0 : _b.toolChoice;
1064
890
  return !!(typeof params.providerOptions === "object" && params.providerOptions !== null && typeof ((_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware) === "object" && toolChoice && typeof toolChoice === "object" && (toolChoice.type === "tool" || toolChoice.type === "required"));
1065
891
  }
1066
- function getFunctionTools(params) {
1067
- var _a, _b;
1068
- const functionTools = ((_a = params.tools) != null ? _a : []).filter(
1069
- (t) => t.type === "function"
1070
- );
1071
- if (functionTools.length > 0) return functionTools;
1072
- const rawToolNames = params.providerOptions && typeof params.providerOptions === "object" && ((_b = params.providerOptions.toolCallMiddleware) == null ? void 0 : _b.toolNames) || [];
1073
- const toStringArray = (val) => Array.isArray(val) ? val.filter(
1074
- (item) => typeof item === "string"
1075
- ) : [];
1076
- const toolNames = toStringArray(rawToolNames);
1077
- if (toolNames.length > 0) {
1078
- return toolNames.map((name) => ({
1079
- type: "function",
1080
- name,
1081
- description: "",
1082
- inputSchema: { type: "object" }
1083
- }));
1084
- }
1085
- return [];
1086
- }
1087
892
 
1088
893
  // src/utils/type-guards.ts
1089
894
  function isToolCallContent(content) {
@@ -1338,57 +1143,13 @@ var jsonMixProtocol = ({
1338
1143
 
1339
1144
  // src/protocols/morph-xml-protocol.ts
1340
1145
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
1341
- var import_rxml = require("@ai-sdk-tool/rxml");
1342
- function getToolSchema(tools, originalSchemas, toolName) {
1343
- var _a;
1344
- const original = originalSchemas[toolName];
1345
- if (original) return original;
1346
- const fallback = (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
1347
- return fallback;
1348
- }
1349
- function findToolCalls(text, toolNames) {
1350
- const toolCalls = [];
1351
- for (const toolName of toolNames) {
1352
- let searchIndex = 0;
1353
- while (searchIndex < text.length) {
1354
- const startTag = `<${toolName}>`;
1355
- const tagStart = text.indexOf(startTag, searchIndex);
1356
- if (tagStart === -1) break;
1357
- const remainingText = text.substring(tagStart);
1358
- const range = (0, import_rxml.findFirstTopLevelRange)(remainingText, toolName);
1359
- if (range) {
1360
- const contentStart = tagStart + startTag.length;
1361
- const contentEnd = contentStart + (range.end - range.start);
1362
- let fullTagEnd = contentEnd + `</${toolName}>`.length;
1363
- const closeHead = text.indexOf(`</${toolName}`, contentEnd);
1364
- if (closeHead === contentEnd) {
1365
- let p = closeHead + 2 + toolName.length;
1366
- while (p < text.length && /\s/.test(text[p])) p++;
1367
- if (text[p] === ">") fullTagEnd = p + 1;
1368
- }
1369
- const toolContent = text.substring(contentStart, contentEnd);
1370
- const fullSegment = text.substring(tagStart, fullTagEnd);
1371
- toolCalls.push({
1372
- toolName,
1373
- startIndex: tagStart,
1374
- endIndex: fullTagEnd,
1375
- content: toolContent,
1376
- segment: fullSegment
1377
- });
1378
- searchIndex = fullTagEnd;
1379
- } else {
1380
- searchIndex = tagStart + startTag.length;
1381
- }
1382
- }
1383
- }
1384
- return toolCalls.sort((a, b) => a.startIndex - b.startIndex);
1385
- }
1146
+ var RXML = __toESM(require("@ai-sdk-tool/rxml"), 1);
1386
1147
  var morphXmlProtocol = () => ({
1387
1148
  formatTools({ tools, toolSystemPromptTemplate }) {
1388
1149
  const toolsForPrompt = (tools || []).map((tool) => ({
1389
1150
  name: tool.name,
1390
1151
  description: tool.description,
1391
- parameters: unwrapJsonSchema(tool.inputSchema)
1152
+ parameters: RXML.unwrapJsonSchema(tool.inputSchema)
1392
1153
  }));
1393
1154
  return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
1394
1155
  },
@@ -1404,20 +1165,19 @@ var morphXmlProtocol = () => ({
1404
1165
  } else {
1405
1166
  args = inputValue;
1406
1167
  }
1407
- return (0, import_rxml.stringify)(toolCall.toolName, args, {
1168
+ return RXML.stringify(toolCall.toolName, args, {
1408
1169
  suppressEmptyNode: false,
1409
1170
  format: false
1410
1171
  });
1411
1172
  },
1412
1173
  formatToolResponse(toolResult) {
1413
- return (0, import_rxml.stringify)("tool_response", {
1174
+ return RXML.stringify("tool_response", {
1414
1175
  tool_name: toolResult.toolName,
1415
1176
  result: toolResult.output
1416
1177
  });
1417
1178
  },
1418
1179
  parseGeneratedText({ text, tools, options }) {
1419
1180
  var _a;
1420
- const originalSchemas = (options == null ? void 0 : options.originalToolSchemas) || {};
1421
1181
  const toolNames = tools.map((t) => t.name).filter((name) => name != null);
1422
1182
  if (toolNames.length === 0) {
1423
1183
  return [{ type: "text", text }];
@@ -1433,13 +1193,11 @@ var morphXmlProtocol = () => ({
1433
1193
  }
1434
1194
  }
1435
1195
  try {
1436
- const toolSchema = getToolSchema(
1437
- tools,
1438
- originalSchemas,
1439
- toolCall.toolName
1440
- );
1441
- const parsed = (0, import_rxml.parse)(toolCall.content, toolSchema, {
1442
- onError: options == null ? void 0 : options.onError
1196
+ const toolSchema = getToolSchema(tools, toolCall.toolName);
1197
+ const parsed = RXML.parse(toolCall.content, toolSchema, {
1198
+ onError: options == null ? void 0 : options.onError,
1199
+ // Disable HTML self-closing tag behavior to allow base, meta, link etc. as regular tags
1200
+ noChildNodes: []
1443
1201
  });
1444
1202
  processedElements.push({
1445
1203
  type: "tool-call",
@@ -1471,8 +1229,8 @@ var morphXmlProtocol = () => ({
1471
1229
  return processedElements;
1472
1230
  },
1473
1231
  createStreamParser({ tools, options }) {
1474
- const originalSchemas = (options == null ? void 0 : options.originalToolSchemas) || {};
1475
1232
  const toolNames = tools.map((t) => t.name).filter((name) => name != null);
1233
+ const maxStartTagLen = toolNames.length ? Math.max(...toolNames.map((n) => `<${n}>`.length)) : 0;
1476
1234
  let buffer = "";
1477
1235
  let currentToolCall = null;
1478
1236
  let currentTextId = null;
@@ -1514,13 +1272,11 @@ var morphXmlProtocol = () => ({
1514
1272
  const toolContent = buffer.substring(0, endTagIndex);
1515
1273
  buffer = buffer.substring(endTagIndex + endTag.length);
1516
1274
  try {
1517
- const toolSchema = getToolSchema(
1518
- tools,
1519
- originalSchemas,
1520
- currentToolCall.name
1521
- );
1522
- const parsed = (0, import_rxml.parse)(toolContent, toolSchema, {
1523
- onError: options == null ? void 0 : options.onError
1275
+ const toolSchema = getToolSchema(tools, currentToolCall.name);
1276
+ const parsed = RXML.parse(toolContent, toolSchema, {
1277
+ onError: options == null ? void 0 : options.onError,
1278
+ // Disable HTML self-closing tag behavior to allow base, meta, link etc. as regular tags
1279
+ noChildNodes: []
1524
1280
  });
1525
1281
  flushText(controller);
1526
1282
  controller.enqueue({
@@ -1532,11 +1288,11 @@ var morphXmlProtocol = () => ({
1532
1288
  } catch (error) {
1533
1289
  const originalCallText = `<${currentToolCall.name}>${toolContent}${endTag}`;
1534
1290
  let message = "Could not process streaming XML tool call; emitting original text.";
1535
- if (error instanceof import_rxml.RXMLDuplicateStringTagError) {
1291
+ if (error instanceof RXML.RXMLDuplicateStringTagError) {
1536
1292
  message = `Duplicate string tags detected in streaming tool call '${currentToolCall.name}'; emitting original text.`;
1537
- } else if (error instanceof import_rxml.RXMLCoercionError) {
1293
+ } else if (error instanceof RXML.RXMLCoercionError) {
1538
1294
  message = `Failed to coerce arguments for streaming tool call '${currentToolCall.name}'; emitting original text.`;
1539
- } else if (error instanceof import_rxml.RXMLParseError) {
1295
+ } else if (error instanceof RXML.RXMLParseError) {
1540
1296
  message = `Failed to parse XML for streaming tool call '${currentToolCall.name}'; emitting original text.`;
1541
1297
  }
1542
1298
  (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, message, {
@@ -1572,6 +1328,14 @@ var morphXmlProtocol = () => ({
1572
1328
  );
1573
1329
  currentToolCall = { name: earliestToolName, content: "" };
1574
1330
  } else {
1331
+ const tail = Math.max(0, maxStartTagLen - 1);
1332
+ const safeLen = Math.max(0, buffer.length - tail);
1333
+ if (safeLen > 0) {
1334
+ const textToFlush = buffer.slice(0, safeLen);
1335
+ flushText(controller, textToFlush);
1336
+ buffer = buffer.slice(safeLen);
1337
+ continue;
1338
+ }
1575
1339
  break;
1576
1340
  }
1577
1341
  }
@@ -1596,6 +1360,48 @@ var morphXmlProtocol = () => ({
1596
1360
  return findToolCalls(text, toolNames).map((tc) => tc.segment);
1597
1361
  }
1598
1362
  });
1363
+ function getToolSchema(tools, toolName) {
1364
+ var _a;
1365
+ return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
1366
+ }
1367
+ function findToolCalls(text, toolNames) {
1368
+ var _a;
1369
+ const toolCalls = [];
1370
+ for (const toolName of toolNames) {
1371
+ let searchIndex = 0;
1372
+ while (searchIndex < text.length) {
1373
+ const startTag = `<${toolName}>`;
1374
+ const tagStart = text.indexOf(startTag, searchIndex);
1375
+ if (tagStart === -1) break;
1376
+ const remainingText = text.substring(tagStart);
1377
+ const range = RXML.findFirstTopLevelRange(remainingText, toolName);
1378
+ if (range) {
1379
+ const contentStart = tagStart + startTag.length;
1380
+ const contentEnd = contentStart + (range.end - range.start);
1381
+ let fullTagEnd = contentEnd + `</${toolName}>`.length;
1382
+ const closeHead = text.indexOf(`</${toolName}`, contentEnd);
1383
+ if (closeHead === contentEnd) {
1384
+ let p = closeHead + 2 + toolName.length;
1385
+ while (p < text.length && /\s/.test(text[p])) p++;
1386
+ if (text[p] === ">") fullTagEnd = p + 1;
1387
+ }
1388
+ const segment = text.substring(tagStart, fullTagEnd);
1389
+ const content = (_a = RXML.extractRawInner(segment, toolName)) != null ? _a : text.substring(contentStart, contentEnd);
1390
+ toolCalls.push({
1391
+ toolName,
1392
+ startIndex: tagStart,
1393
+ endIndex: fullTagEnd,
1394
+ content,
1395
+ segment
1396
+ });
1397
+ searchIndex = fullTagEnd;
1398
+ } else {
1399
+ searchIndex = tagStart + startTag.length;
1400
+ }
1401
+ }
1402
+ }
1403
+ return toolCalls.sort((a, b) => a.startIndex - b.startIndex);
1404
+ }
1599
1405
 
1600
1406
  // src/protocols/tool-call-protocol.ts
1601
1407
  function isProtocolFactory(protocol) {
@@ -1604,12 +1410,13 @@ function isProtocolFactory(protocol) {
1604
1410
 
1605
1411
  // src/generate-handler.ts
1606
1412
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
1413
+ var RXML2 = __toESM(require("@ai-sdk-tool/rxml"), 1);
1607
1414
  async function wrapGenerate({
1608
1415
  protocol,
1609
1416
  doGenerate,
1610
1417
  params
1611
1418
  }) {
1612
- var _a, _b;
1419
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1613
1420
  if (isToolChoiceActive(params)) {
1614
1421
  const result2 = await doGenerate();
1615
1422
  let parsed2 = {};
@@ -1642,7 +1449,16 @@ async function wrapGenerate({
1642
1449
  };
1643
1450
  const debugLevelToolChoice = getDebugLevel();
1644
1451
  const originText = first && first.type === "text" ? first.text : "";
1645
- if (debugLevelToolChoice === "parse") {
1452
+ const dbg2 = (_d = (_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware) == null ? void 0 : _d.debugSummary;
1453
+ if (dbg2) {
1454
+ dbg2.originalText = originText;
1455
+ try {
1456
+ dbg2.toolCalls = JSON.stringify([
1457
+ { toolName: toolCall.toolName, input: toolCall.input }
1458
+ ]);
1459
+ } catch (e) {
1460
+ }
1461
+ } else if (debugLevelToolChoice === "parse") {
1646
1462
  logParsedSummary({ toolCalls: [toolCall], originalText: originText });
1647
1463
  }
1648
1464
  return {
@@ -1650,6 +1466,9 @@ async function wrapGenerate({
1650
1466
  content: [toolCall]
1651
1467
  };
1652
1468
  }
1469
+ const tools = originalToolsSchema.decode(
1470
+ (_f = (_e = params.providerOptions) == null ? void 0 : _e.toolCallMiddleware) == null ? void 0 : _f.originalTools
1471
+ );
1653
1472
  const result = await doGenerate();
1654
1473
  if (result.content.length === 0) {
1655
1474
  return result;
@@ -1665,30 +1484,41 @@ async function wrapGenerate({
1665
1484
  }
1666
1485
  return protocol.parseGeneratedText({
1667
1486
  text: contentItem.text,
1668
- tools: getFunctionTools(params),
1487
+ tools,
1669
1488
  options: {
1670
1489
  ...extractOnErrorOption(params.providerOptions),
1671
1490
  ...(_a2 = params.providerOptions) == null ? void 0 : _a2.toolCallMiddleware
1672
1491
  }
1673
1492
  });
1674
1493
  });
1675
- const tools = getFunctionTools(params);
1676
1494
  const newContent = parsed.map(
1677
- (part) => coerceToolCallInput(part, tools)
1495
+ (part) => fixToolCallWithSchema(part, tools)
1678
1496
  );
1679
1497
  const debugLevel = getDebugLevel();
1680
1498
  if (debugLevel === "stream") {
1681
1499
  newContent.forEach((part) => logParsedChunk(part));
1682
1500
  }
1683
- if (debugLevel === "parse") {
1684
- const allText = result.content.filter(
1685
- (c) => c.type === "text"
1686
- ).map((c) => c.text).join("\n\n");
1687
- const segments = protocol.extractToolCallSegments ? protocol.extractToolCallSegments({ text: allText, tools }) : [];
1688
- const originalText = segments.join("\n\n");
1689
- const toolCalls = newContent.filter(
1690
- (p) => p.type === "tool-call"
1691
- );
1501
+ const allText = result.content.filter(
1502
+ (c) => c.type === "text"
1503
+ ).map((c) => c.text).join("\n\n");
1504
+ const segments = protocol.extractToolCallSegments ? protocol.extractToolCallSegments({ text: allText, tools }) : [];
1505
+ const originalText = segments.join("\n\n");
1506
+ const toolCalls = newContent.filter(
1507
+ (p) => p.type === "tool-call"
1508
+ );
1509
+ const dbg = (_h = (_g = params.providerOptions) == null ? void 0 : _g.toolCallMiddleware) == null ? void 0 : _h.debugSummary;
1510
+ if (dbg) {
1511
+ dbg.originalText = originalText;
1512
+ try {
1513
+ dbg.toolCalls = JSON.stringify(
1514
+ toolCalls.map((tc) => ({
1515
+ toolName: tc.toolName,
1516
+ input: tc.input
1517
+ }))
1518
+ );
1519
+ } catch (e) {
1520
+ }
1521
+ } else if (debugLevel === "parse") {
1692
1522
  logParsedSummary({ toolCalls, originalText });
1693
1523
  }
1694
1524
  return {
@@ -1696,6 +1526,27 @@ async function wrapGenerate({
1696
1526
  content: newContent
1697
1527
  };
1698
1528
  }
1529
+ function fixToolCallWithSchema(part, tools) {
1530
+ var _a;
1531
+ if (part.type !== "tool-call") return part;
1532
+ const tc = part;
1533
+ let args = {};
1534
+ if (typeof tc.input === "string") {
1535
+ try {
1536
+ args = JSON.parse(tc.input);
1537
+ } catch (e) {
1538
+ return part;
1539
+ }
1540
+ } else if (tc.input && typeof tc.input === "object") {
1541
+ args = tc.input;
1542
+ }
1543
+ const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
1544
+ const coerced = RXML2.coerceBySchema(args, schema);
1545
+ return {
1546
+ ...part,
1547
+ input: JSON.stringify(coerced != null ? coerced : {})
1548
+ };
1549
+ }
1699
1550
 
1700
1551
  // src/stream-handler.ts
1701
1552
  var import_provider_utils5 = require("@ai-sdk/provider-utils");
@@ -1705,7 +1556,7 @@ async function wrapStream({
1705
1556
  doGenerate,
1706
1557
  params
1707
1558
  }) {
1708
- var _a;
1559
+ var _a, _b, _c;
1709
1560
  if (isToolChoiceActive(params)) {
1710
1561
  return toolChoiceStream({
1711
1562
  doGenerate,
@@ -1714,10 +1565,12 @@ async function wrapStream({
1714
1565
  }
1715
1566
  const { stream, ...rest } = await doStream();
1716
1567
  const debugLevel = getDebugLevel();
1717
- const tools = getFunctionTools(params);
1568
+ const tools = originalToolsSchema.decode(
1569
+ (_b = (_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware) == null ? void 0 : _b.originalTools
1570
+ );
1718
1571
  const options = {
1719
1572
  ...extractOnErrorOption(params.providerOptions),
1720
- ...(_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware
1573
+ ...(_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware
1721
1574
  };
1722
1575
  if (debugLevel === "off") {
1723
1576
  return {
@@ -1787,6 +1640,7 @@ async function wrapStream({
1787
1640
  transform: /* @__PURE__ */ (() => {
1788
1641
  const parsedToolCalls = [];
1789
1642
  return (part, controller) => {
1643
+ var _a2, _b2;
1790
1644
  if (part.type === "tool-call") {
1791
1645
  parsedToolCalls.push(part);
1792
1646
  }
@@ -1797,10 +1651,27 @@ async function wrapStream({
1797
1651
  tools
1798
1652
  }) : [];
1799
1653
  const origin = segments.join("\n\n");
1800
- logParsedSummary({
1801
- toolCalls: parsedToolCalls,
1802
- originalText: origin
1803
- });
1654
+ const dbg = (_b2 = (_a2 = params.providerOptions) == null ? void 0 : _a2.toolCallMiddleware) == null ? void 0 : _b2.debugSummary;
1655
+ if (dbg) {
1656
+ dbg.originalText = origin;
1657
+ try {
1658
+ const toolCallParts = parsedToolCalls.filter(
1659
+ (p) => p.type === "tool-call"
1660
+ );
1661
+ dbg.toolCalls = JSON.stringify(
1662
+ toolCallParts.map((tc) => ({
1663
+ toolName: tc.toolName,
1664
+ input: tc.input
1665
+ }))
1666
+ );
1667
+ } catch (e) {
1668
+ }
1669
+ } else {
1670
+ logParsedSummary({
1671
+ toolCalls: parsedToolCalls,
1672
+ originalText: origin
1673
+ });
1674
+ }
1804
1675
  } catch (e) {
1805
1676
  }
1806
1677
  }
@@ -1926,10 +1797,10 @@ async function transformParams({
1926
1797
  ...params.providerOptions || {},
1927
1798
  toolCallMiddleware: {
1928
1799
  ...params.providerOptions && typeof params.providerOptions === "object" && params.providerOptions.toolCallMiddleware || {},
1929
- // INTERNAL: used by the middleware to propagate the names of custom
1930
- // function tools into downstream handlers (stream/generate) when
1931
- // providers strip or ignore `params.tools`. Not a stable public API.
1932
- toolNames: functionTools.map((t) => t.name)
1800
+ // INTERNAL: used by the middleware so downstream parsers can access
1801
+ // the original tool schemas even if providers strip `params.tools`.
1802
+ // Not a stable public API.
1803
+ originalTools: originalToolsSchema.encode(functionTools)
1933
1804
  }
1934
1805
  }
1935
1806
  };
@@ -2210,7 +2081,7 @@ When an argument is an array, write each item inside a single element on one lin
2210
2081
  Examples:
2211
2082
  <get_weather>
2212
2083
  <location>
2213
- San Fransisco
2084
+ San Francisco
2214
2085
  </location>
2215
2086
  </get_weather>`;
2216
2087
  }
@@ -2218,18 +2089,16 @@ San Fransisco
2218
2089
  // Annotate the CommonJS export names for ESM import in node:
2219
2090
  0 && (module.exports = {
2220
2091
  RJSON,
2221
- coerceBySchema,
2222
- coerceToolCallInput,
2223
2092
  createDynamicIfThenElseSchema,
2224
2093
  createToolMiddleware,
2094
+ decodeOriginalTools,
2095
+ encodeOriginalTools,
2225
2096
  escapeRegExp,
2226
2097
  extractOnErrorOption,
2227
- fixToolCallWithSchema,
2098
+ extractToolNamesFromOriginalTools,
2228
2099
  gemmaToolMiddleware,
2229
2100
  getDebugLevel,
2230
- getFunctionTools,
2231
2101
  getPotentialStartIndex,
2232
- getSchemaType,
2233
2102
  hasInputProperty,
2234
2103
  hermesToolMiddleware,
2235
2104
  isToolCallContent,
@@ -2240,7 +2109,7 @@ San Fransisco
2240
2109
  logParsedSummary,
2241
2110
  logRawChunk,
2242
2111
  morphXmlProtocol,
2243
- unwrapJsonSchema,
2112
+ originalToolsSchema,
2244
2113
  xmlToolMiddleware
2245
2114
  });
2246
2115
  //# sourceMappingURL=index.cjs.map