@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.
@@ -346,12 +346,15 @@ function handleOpeningTagSegment(src, lt, out, stack) {
346
346
  }
347
347
  return q + 1;
348
348
  }
349
- function shouldDeduplicateStringTags(schema) {
349
+ function extractSchemaProperties(schema) {
350
350
  const unwrapped = (0, import_rxml.unwrapJsonSchema)(schema);
351
351
  if (!unwrapped || typeof unwrapped !== "object") {
352
- return false;
352
+ return void 0;
353
353
  }
354
- const props = unwrapped.properties;
354
+ return unwrapped.properties;
355
+ }
356
+ function shouldDeduplicateStringTags(schema) {
357
+ const props = extractSchemaProperties(schema);
355
358
  if (!props) {
356
359
  return false;
357
360
  }
@@ -363,21 +366,14 @@ function shouldDeduplicateStringTags(schema) {
363
366
  return (command == null ? void 0 : command.type) === "array";
364
367
  }
365
368
  function getStringPropertyNames(schema) {
366
- const unwrapped = (0, import_rxml.unwrapJsonSchema)(schema);
367
- if (!unwrapped || typeof unwrapped !== "object") {
368
- return [];
369
- }
370
- const props = unwrapped.properties;
369
+ const props = extractSchemaProperties(schema);
371
370
  if (!props) {
372
371
  return [];
373
372
  }
374
373
  const names = [];
375
374
  for (const key of Object.keys(props)) {
376
- const prop = (0, import_rxml.unwrapJsonSchema)(
377
- props[key]
378
- );
379
- const type = prop.type;
380
- if (type === "string") {
375
+ const prop = (0, import_rxml.unwrapJsonSchema)(props[key]);
376
+ if ((prop == null ? void 0 : prop.type) === "string") {
381
377
  names.push(key);
382
378
  }
383
379
  }
@@ -412,11 +408,7 @@ function repairParsedAgainstSchema(input, schema) {
412
408
  if (!input || typeof input !== "object") {
413
409
  return input;
414
410
  }
415
- const unwrapped = (0, import_rxml.unwrapJsonSchema)(schema);
416
- if (!unwrapped || typeof unwrapped !== "object") {
417
- return input;
418
- }
419
- const properties = unwrapped.properties;
411
+ const properties = extractSchemaProperties(schema);
420
412
  if (!properties) {
421
413
  return input;
422
414
  }
@@ -430,14 +422,12 @@ function applySchemaProps(obj, properties) {
430
422
  continue;
431
423
  }
432
424
  const prop = (0, import_rxml.unwrapJsonSchema)(propSchema);
433
- const propType = prop.type;
434
- if (propType === "array" && prop.items) {
435
- const itemSchemaRaw = prop.items;
436
- const itemSchema = (0, import_rxml.unwrapJsonSchema)(itemSchemaRaw);
425
+ if ((prop == null ? void 0 : prop.type) === "array" && prop.items) {
426
+ const itemSchema = (0, import_rxml.unwrapJsonSchema)(prop.items);
437
427
  obj[key] = coerceArrayItems(obj[key], itemSchema);
438
428
  continue;
439
429
  }
440
- if (propType === "object") {
430
+ if ((prop == null ? void 0 : prop.type) === "object") {
441
431
  const val = obj[key];
442
432
  if (val && typeof val === "object") {
443
433
  obj[key] = repairParsedAgainstSchema(val, prop);
@@ -1536,10 +1526,12 @@ var jsonProtocol = ({
1536
1526
  },
1537
1527
  formatToolCall(toolCall) {
1538
1528
  let args = {};
1539
- try {
1540
- args = JSON.parse(toolCall.input);
1541
- } catch (e) {
1542
- args = toolCall.input;
1529
+ if (toolCall.input != null) {
1530
+ try {
1531
+ args = JSON.parse(toolCall.input);
1532
+ } catch (e) {
1533
+ args = toolCall.input;
1534
+ }
1543
1535
  }
1544
1536
  return `${toolCallStart}${JSON.stringify({
1545
1537
  name: toolCall.toolName,
@@ -2168,10 +2160,12 @@ var xmlProtocol = (protocolOptions) => {
2168
2160
  },
2169
2161
  formatToolCall(toolCall) {
2170
2162
  let args = {};
2171
- try {
2172
- args = JSON.parse(toolCall.input);
2173
- } catch (e) {
2174
- args = toolCall.input;
2163
+ if (toolCall.input != null) {
2164
+ try {
2165
+ args = JSON.parse(toolCall.input);
2166
+ } catch (e) {
2167
+ args = toolCall.input;
2168
+ }
2175
2169
  }
2176
2170
  return (0, import_rxml2.stringify)(toolCall.toolName, args, {
2177
2171
  suppressEmptyNode: false,
@@ -2565,10 +2559,12 @@ var yamlProtocol = (_protocolOptions) => {
2565
2559
  },
2566
2560
  formatToolCall(toolCall) {
2567
2561
  let args = {};
2568
- try {
2569
- args = JSON.parse(toolCall.input);
2570
- } catch (e) {
2571
- args = { value: toolCall.input };
2562
+ if (toolCall.input != null) {
2563
+ try {
2564
+ args = JSON.parse(toolCall.input);
2565
+ } catch (e) {
2566
+ args = { value: toolCall.input };
2567
+ }
2572
2568
  }
2573
2569
  const yamlContent = import_yaml.default.stringify(args);
2574
2570
  return `<${toolCall.toolName}>
@@ -2829,12 +2825,6 @@ function isToolChoiceActive(params) {
2829
2825
  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"));
2830
2826
  }
2831
2827
 
2832
- // src/core/utils/type-guards.ts
2833
- function isToolCallContent(content) {
2834
- return content.type === "tool-call" && typeof content.toolName === "string" && // input may be a JSON string or an already-parsed object depending on provider/runtime
2835
- (typeof content.input === "string" || typeof content.input === "object");
2836
- }
2837
-
2838
2828
  // src/generate-handler.ts
2839
2829
  var import_provider_utils = require("@ai-sdk/provider-utils");
2840
2830
  var import_rxml3 = require("@ai-sdk-tool/rxml");
@@ -2988,18 +2978,17 @@ function fixToolCallWithSchema(part, tools) {
2988
2978
  if (part.type !== "tool-call") {
2989
2979
  return part;
2990
2980
  }
2991
- const tc = part;
2992
2981
  let args = {};
2993
- if (typeof tc.input === "string") {
2982
+ if (typeof part.input === "string") {
2994
2983
  try {
2995
- args = JSON.parse(tc.input);
2984
+ args = JSON.parse(part.input);
2996
2985
  } catch (e) {
2997
2986
  return part;
2998
2987
  }
2999
- } else if (tc.input && typeof tc.input === "object") {
3000
- args = tc.input;
2988
+ } else if (part.input && typeof part.input === "object") {
2989
+ args = part.input;
3001
2990
  }
3002
- const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
2991
+ const schema = (_a = tools.find((t) => t.name === part.toolName)) == null ? void 0 : _a.inputSchema;
3003
2992
  const coerced = (0, import_rxml3.coerceBySchema)(args, schema);
3004
2993
  return {
3005
2994
  ...part,
@@ -3494,26 +3483,29 @@ function processAssistantContent(content, resolvedProtocol, providerOptions) {
3494
3483
  var _a;
3495
3484
  const newContent = [];
3496
3485
  for (const item of content) {
3497
- if (isToolCallContent(item)) {
3498
- newContent.push({
3499
- type: "text",
3500
- text: resolvedProtocol.formatToolCall(item)
3501
- });
3502
- } else if (item.type === "text") {
3503
- newContent.push(item);
3504
- } else if (item.type === "reasoning") {
3505
- newContent.push(item);
3506
- } else {
3507
- const options = extractOnErrorOption(providerOptions);
3508
- (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
3509
- options,
3510
- "tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
3511
- { content: item }
3512
- );
3513
- newContent.push({
3514
- type: "text",
3515
- text: JSON.stringify(item)
3516
- });
3486
+ switch (item.type) {
3487
+ case "tool-call":
3488
+ newContent.push({
3489
+ type: "text",
3490
+ text: resolvedProtocol.formatToolCall(item)
3491
+ });
3492
+ break;
3493
+ case "text":
3494
+ case "reasoning":
3495
+ newContent.push(item);
3496
+ break;
3497
+ default: {
3498
+ const options = extractOnErrorOption(providerOptions);
3499
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
3500
+ options,
3501
+ "tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
3502
+ { content: item }
3503
+ );
3504
+ newContent.push({
3505
+ type: "text",
3506
+ text: JSON.stringify(item)
3507
+ });
3508
+ }
3517
3509
  }
3518
3510
  }
3519
3511
  const onlyText = newContent.every((c) => c.type === "text");