@ai-sdk-tool/parser 3.1.0 → 3.1.1

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.
@@ -332,12 +332,15 @@ function handleOpeningTagSegment(src, lt, out, stack) {
332
332
  }
333
333
  return q + 1;
334
334
  }
335
- function shouldDeduplicateStringTags(schema) {
335
+ function extractSchemaProperties(schema) {
336
336
  const unwrapped = unwrapJsonSchema(schema);
337
337
  if (!unwrapped || typeof unwrapped !== "object") {
338
- return false;
338
+ return void 0;
339
339
  }
340
- const props = unwrapped.properties;
340
+ return unwrapped.properties;
341
+ }
342
+ function shouldDeduplicateStringTags(schema) {
343
+ const props = extractSchemaProperties(schema);
341
344
  if (!props) {
342
345
  return false;
343
346
  }
@@ -349,21 +352,14 @@ function shouldDeduplicateStringTags(schema) {
349
352
  return (command == null ? void 0 : command.type) === "array";
350
353
  }
351
354
  function getStringPropertyNames(schema) {
352
- const unwrapped = unwrapJsonSchema(schema);
353
- if (!unwrapped || typeof unwrapped !== "object") {
354
- return [];
355
- }
356
- const props = unwrapped.properties;
355
+ const props = extractSchemaProperties(schema);
357
356
  if (!props) {
358
357
  return [];
359
358
  }
360
359
  const names = [];
361
360
  for (const key of Object.keys(props)) {
362
- const prop = unwrapJsonSchema(
363
- props[key]
364
- );
365
- const type = prop.type;
366
- if (type === "string") {
361
+ const prop = unwrapJsonSchema(props[key]);
362
+ if ((prop == null ? void 0 : prop.type) === "string") {
367
363
  names.push(key);
368
364
  }
369
365
  }
@@ -398,11 +394,7 @@ function repairParsedAgainstSchema(input, schema) {
398
394
  if (!input || typeof input !== "object") {
399
395
  return input;
400
396
  }
401
- const unwrapped = unwrapJsonSchema(schema);
402
- if (!unwrapped || typeof unwrapped !== "object") {
403
- return input;
404
- }
405
- const properties = unwrapped.properties;
397
+ const properties = extractSchemaProperties(schema);
406
398
  if (!properties) {
407
399
  return input;
408
400
  }
@@ -416,14 +408,12 @@ function applySchemaProps(obj, properties) {
416
408
  continue;
417
409
  }
418
410
  const prop = unwrapJsonSchema(propSchema);
419
- const propType = prop.type;
420
- if (propType === "array" && prop.items) {
421
- const itemSchemaRaw = prop.items;
422
- const itemSchema = unwrapJsonSchema(itemSchemaRaw);
411
+ if ((prop == null ? void 0 : prop.type) === "array" && prop.items) {
412
+ const itemSchema = unwrapJsonSchema(prop.items);
423
413
  obj[key] = coerceArrayItems(obj[key], itemSchema);
424
414
  continue;
425
415
  }
426
- if (propType === "object") {
416
+ if ((prop == null ? void 0 : prop.type) === "object") {
427
417
  const val = obj[key];
428
418
  if (val && typeof val === "object") {
429
419
  obj[key] = repairParsedAgainstSchema(val, prop);
@@ -1550,10 +1540,12 @@ var jsonProtocol = ({
1550
1540
  },
1551
1541
  formatToolCall(toolCall) {
1552
1542
  let args = {};
1553
- try {
1554
- args = JSON.parse(toolCall.input);
1555
- } catch (e) {
1556
- args = toolCall.input;
1543
+ if (toolCall.input != null) {
1544
+ try {
1545
+ args = JSON.parse(toolCall.input);
1546
+ } catch (e) {
1547
+ args = toolCall.input;
1548
+ }
1557
1549
  }
1558
1550
  return `${toolCallStart}${JSON.stringify({
1559
1551
  name: toolCall.toolName,
@@ -2185,10 +2177,12 @@ var xmlProtocol = (protocolOptions) => {
2185
2177
  },
2186
2178
  formatToolCall(toolCall) {
2187
2179
  let args = {};
2188
- try {
2189
- args = JSON.parse(toolCall.input);
2190
- } catch (e) {
2191
- args = toolCall.input;
2180
+ if (toolCall.input != null) {
2181
+ try {
2182
+ args = JSON.parse(toolCall.input);
2183
+ } catch (e) {
2184
+ args = toolCall.input;
2185
+ }
2192
2186
  }
2193
2187
  return stringify2(toolCall.toolName, args, {
2194
2188
  suppressEmptyNode: false,
@@ -2582,10 +2576,12 @@ var yamlProtocol = (_protocolOptions) => {
2582
2576
  },
2583
2577
  formatToolCall(toolCall) {
2584
2578
  let args = {};
2585
- try {
2586
- args = JSON.parse(toolCall.input);
2587
- } catch (e) {
2588
- args = { value: toolCall.input };
2579
+ if (toolCall.input != null) {
2580
+ try {
2581
+ args = JSON.parse(toolCall.input);
2582
+ } catch (e) {
2583
+ args = { value: toolCall.input };
2584
+ }
2589
2585
  }
2590
2586
  const yamlContent = YAML.stringify(args);
2591
2587
  return `<${toolCall.toolName}>
@@ -2850,13 +2846,12 @@ function isToolChoiceActive(params) {
2850
2846
  }
2851
2847
 
2852
2848
  // src/core/utils/type-guards.ts
2853
- function isToolCallContent(content) {
2854
- return content.type === "tool-call" && typeof content.toolName === "string" && // input may be a JSON string or an already-parsed object depending on provider/runtime
2855
- (typeof content.input === "string" || typeof content.input === "object");
2856
- }
2857
2849
  function isToolResultPart(content) {
2850
+ if (!content || typeof content !== "object") {
2851
+ return false;
2852
+ }
2858
2853
  const c = content;
2859
- return !!c && c.type === "tool-result" && typeof c.toolName === "string" && typeof c.toolCallId === "string" && "output" in c;
2854
+ return c.type === "tool-result" && typeof c.toolName === "string" && typeof c.toolCallId === "string" && "output" in c;
2860
2855
  }
2861
2856
  function hasInputProperty(obj) {
2862
2857
  return typeof obj === "object" && obj !== null && "input" in obj;
@@ -3015,18 +3010,17 @@ function fixToolCallWithSchema(part, tools) {
3015
3010
  if (part.type !== "tool-call") {
3016
3011
  return part;
3017
3012
  }
3018
- const tc = part;
3019
3013
  let args = {};
3020
- if (typeof tc.input === "string") {
3014
+ if (typeof part.input === "string") {
3021
3015
  try {
3022
- args = JSON.parse(tc.input);
3016
+ args = JSON.parse(part.input);
3023
3017
  } catch (e) {
3024
3018
  return part;
3025
3019
  }
3026
- } else if (tc.input && typeof tc.input === "object") {
3027
- args = tc.input;
3020
+ } else if (part.input && typeof part.input === "object") {
3021
+ args = part.input;
3028
3022
  }
3029
- const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
3023
+ const schema = (_a = tools.find((t) => t.name === part.toolName)) == null ? void 0 : _a.inputSchema;
3030
3024
  const coerced = coerceBySchema(args, schema);
3031
3025
  return {
3032
3026
  ...part,
@@ -3521,26 +3515,29 @@ function processAssistantContent(content, resolvedProtocol, providerOptions) {
3521
3515
  var _a;
3522
3516
  const newContent = [];
3523
3517
  for (const item of content) {
3524
- if (isToolCallContent(item)) {
3525
- newContent.push({
3526
- type: "text",
3527
- text: resolvedProtocol.formatToolCall(item)
3528
- });
3529
- } else if (item.type === "text") {
3530
- newContent.push(item);
3531
- } else if (item.type === "reasoning") {
3532
- newContent.push(item);
3533
- } else {
3534
- const options = extractOnErrorOption(providerOptions);
3535
- (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
3536
- options,
3537
- "tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
3538
- { content: item }
3539
- );
3540
- newContent.push({
3541
- type: "text",
3542
- text: JSON.stringify(item)
3543
- });
3518
+ switch (item.type) {
3519
+ case "tool-call":
3520
+ newContent.push({
3521
+ type: "text",
3522
+ text: resolvedProtocol.formatToolCall(item)
3523
+ });
3524
+ break;
3525
+ case "text":
3526
+ case "reasoning":
3527
+ newContent.push(item);
3528
+ break;
3529
+ default: {
3530
+ const options = extractOnErrorOption(providerOptions);
3531
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
3532
+ options,
3533
+ "tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
3534
+ { content: item }
3535
+ );
3536
+ newContent.push({
3537
+ type: "text",
3538
+ text: JSON.stringify(item)
3539
+ });
3540
+ }
3544
3541
  }
3545
3542
  }
3546
3543
  const onlyText = newContent.every((c) => c.type === "text");
@@ -3766,7 +3763,6 @@ export {
3766
3763
  decodeOriginalTools,
3767
3764
  extractToolNamesFromOriginalTools,
3768
3765
  isToolChoiceActive,
3769
- isToolCallContent,
3770
3766
  isToolResultPart,
3771
3767
  hasInputProperty,
3772
3768
  wrapGenerate,
@@ -3778,4 +3774,4 @@ export {
3778
3774
  xmlToolMiddleware,
3779
3775
  yamlToolMiddleware
3780
3776
  };
3781
- //# sourceMappingURL=chunk-JVQVEA3K.js.map
3777
+ //# sourceMappingURL=chunk-WX5U7G6L.js.map