@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.
@@ -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,14 +2160,17 @@ 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,
2178
- format: false
2172
+ format: true,
2173
+ minimalEscaping: true
2179
2174
  });
2180
2175
  },
2181
2176
  parseGeneratedText({ text, tools, options }) {
@@ -2565,10 +2560,12 @@ var yamlProtocol = (_protocolOptions) => {
2565
2560
  },
2566
2561
  formatToolCall(toolCall) {
2567
2562
  let args = {};
2568
- try {
2569
- args = JSON.parse(toolCall.input);
2570
- } catch (e) {
2571
- args = { value: toolCall.input };
2563
+ if (toolCall.input != null) {
2564
+ try {
2565
+ args = JSON.parse(toolCall.input);
2566
+ } catch (e) {
2567
+ args = { value: toolCall.input };
2568
+ }
2572
2569
  }
2573
2570
  const yamlContent = import_yaml.default.stringify(args);
2574
2571
  return `<${toolCall.toolName}>
@@ -2829,12 +2826,6 @@ function isToolChoiceActive(params) {
2829
2826
  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
2827
  }
2831
2828
 
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
2829
  // src/generate-handler.ts
2839
2830
  var import_provider_utils = require("@ai-sdk/provider-utils");
2840
2831
  var import_rxml3 = require("@ai-sdk-tool/rxml");
@@ -2988,18 +2979,17 @@ function fixToolCallWithSchema(part, tools) {
2988
2979
  if (part.type !== "tool-call") {
2989
2980
  return part;
2990
2981
  }
2991
- const tc = part;
2992
2982
  let args = {};
2993
- if (typeof tc.input === "string") {
2983
+ if (typeof part.input === "string") {
2994
2984
  try {
2995
- args = JSON.parse(tc.input);
2985
+ args = JSON.parse(part.input);
2996
2986
  } catch (e) {
2997
2987
  return part;
2998
2988
  }
2999
- } else if (tc.input && typeof tc.input === "object") {
3000
- args = tc.input;
2989
+ } else if (part.input && typeof part.input === "object") {
2990
+ args = part.input;
3001
2991
  }
3002
- const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
2992
+ const schema = (_a = tools.find((t) => t.name === part.toolName)) == null ? void 0 : _a.inputSchema;
3003
2993
  const coerced = (0, import_rxml3.coerceBySchema)(args, schema);
3004
2994
  return {
3005
2995
  ...part,
@@ -3494,26 +3484,29 @@ function processAssistantContent(content, resolvedProtocol, providerOptions) {
3494
3484
  var _a;
3495
3485
  const newContent = [];
3496
3486
  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
- });
3487
+ switch (item.type) {
3488
+ case "tool-call":
3489
+ newContent.push({
3490
+ type: "text",
3491
+ text: resolvedProtocol.formatToolCall(item)
3492
+ });
3493
+ break;
3494
+ case "text":
3495
+ case "reasoning":
3496
+ newContent.push(item);
3497
+ break;
3498
+ default: {
3499
+ const options = extractOnErrorOption(providerOptions);
3500
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
3501
+ options,
3502
+ "tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
3503
+ { content: item }
3504
+ );
3505
+ newContent.push({
3506
+ type: "text",
3507
+ text: JSON.stringify(item)
3508
+ });
3509
+ }
3517
3510
  }
3518
3511
  }
3519
3512
  const onlyText = newContent.every((c) => c.type === "text");