@ai-sdk-tool/parser 3.1.0 → 3.1.2

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,14 +2177,17 @@ 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,
2195
- format: false
2189
+ format: true,
2190
+ minimalEscaping: true
2196
2191
  });
2197
2192
  },
2198
2193
  parseGeneratedText({ text, tools, options }) {
@@ -2582,10 +2577,12 @@ var yamlProtocol = (_protocolOptions) => {
2582
2577
  },
2583
2578
  formatToolCall(toolCall) {
2584
2579
  let args = {};
2585
- try {
2586
- args = JSON.parse(toolCall.input);
2587
- } catch (e) {
2588
- args = { value: toolCall.input };
2580
+ if (toolCall.input != null) {
2581
+ try {
2582
+ args = JSON.parse(toolCall.input);
2583
+ } catch (e) {
2584
+ args = { value: toolCall.input };
2585
+ }
2589
2586
  }
2590
2587
  const yamlContent = YAML.stringify(args);
2591
2588
  return `<${toolCall.toolName}>
@@ -2850,13 +2847,12 @@ function isToolChoiceActive(params) {
2850
2847
  }
2851
2848
 
2852
2849
  // 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
2850
  function isToolResultPart(content) {
2851
+ if (!content || typeof content !== "object") {
2852
+ return false;
2853
+ }
2858
2854
  const c = content;
2859
- return !!c && c.type === "tool-result" && typeof c.toolName === "string" && typeof c.toolCallId === "string" && "output" in c;
2855
+ return c.type === "tool-result" && typeof c.toolName === "string" && typeof c.toolCallId === "string" && "output" in c;
2860
2856
  }
2861
2857
  function hasInputProperty(obj) {
2862
2858
  return typeof obj === "object" && obj !== null && "input" in obj;
@@ -3015,18 +3011,17 @@ function fixToolCallWithSchema(part, tools) {
3015
3011
  if (part.type !== "tool-call") {
3016
3012
  return part;
3017
3013
  }
3018
- const tc = part;
3019
3014
  let args = {};
3020
- if (typeof tc.input === "string") {
3015
+ if (typeof part.input === "string") {
3021
3016
  try {
3022
- args = JSON.parse(tc.input);
3017
+ args = JSON.parse(part.input);
3023
3018
  } catch (e) {
3024
3019
  return part;
3025
3020
  }
3026
- } else if (tc.input && typeof tc.input === "object") {
3027
- args = tc.input;
3021
+ } else if (part.input && typeof part.input === "object") {
3022
+ args = part.input;
3028
3023
  }
3029
- const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
3024
+ const schema = (_a = tools.find((t) => t.name === part.toolName)) == null ? void 0 : _a.inputSchema;
3030
3025
  const coerced = coerceBySchema(args, schema);
3031
3026
  return {
3032
3027
  ...part,
@@ -3521,26 +3516,29 @@ function processAssistantContent(content, resolvedProtocol, providerOptions) {
3521
3516
  var _a;
3522
3517
  const newContent = [];
3523
3518
  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
- });
3519
+ switch (item.type) {
3520
+ case "tool-call":
3521
+ newContent.push({
3522
+ type: "text",
3523
+ text: resolvedProtocol.formatToolCall(item)
3524
+ });
3525
+ break;
3526
+ case "text":
3527
+ case "reasoning":
3528
+ newContent.push(item);
3529
+ break;
3530
+ default: {
3531
+ const options = extractOnErrorOption(providerOptions);
3532
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
3533
+ options,
3534
+ "tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
3535
+ { content: item }
3536
+ );
3537
+ newContent.push({
3538
+ type: "text",
3539
+ text: JSON.stringify(item)
3540
+ });
3541
+ }
3544
3542
  }
3545
3543
  }
3546
3544
  const onlyText = newContent.every((c) => c.type === "text");
@@ -3766,7 +3764,6 @@ export {
3766
3764
  decodeOriginalTools,
3767
3765
  extractToolNamesFromOriginalTools,
3768
3766
  isToolChoiceActive,
3769
- isToolCallContent,
3770
3767
  isToolResultPart,
3771
3768
  hasInputProperty,
3772
3769
  wrapGenerate,
@@ -3778,4 +3775,4 @@ export {
3778
3775
  xmlToolMiddleware,
3779
3776
  yamlToolMiddleware
3780
3777
  };
3781
- //# sourceMappingURL=chunk-JVQVEA3K.js.map
3778
+ //# sourceMappingURL=chunk-TQT6XSP7.js.map