@ai-sdk-tool/parser 3.3.0 → 3.3.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.
package/dist/index.d.cts CHANGED
@@ -3,6 +3,12 @@ import { LanguageModelV3FunctionTool, LanguageModelV3ToolCall, LanguageModelV3Co
3
3
  import { ToolResultPart } from '@ai-sdk/provider-utils';
4
4
  export { ParseOptions, parse, stringify, transform } from './rjson.cjs';
5
5
 
6
+ /**
7
+ * Options for parsing tool calls and handling errors
8
+ */
9
+ interface ParserOptions {
10
+ onError?: (message: string, metadata?: Record<string, unknown>) => void;
11
+ }
6
12
  interface TCMProtocol {
7
13
  formatTools({ tools, toolSystemPromptTemplate, }: {
8
14
  tools: LanguageModelV3FunctionTool[];
@@ -261,4 +267,4 @@ declare function transformParams({ params, protocol, toolSystemPromptTemplate, t
261
267
  toolChoice: undefined;
262
268
  };
263
269
 
264
- export { type DebugLevel, type OnErrorFn, type TCMCoreProtocol, type TCMProtocol, type ToolCallMiddlewareProviderOptions, type XmlProtocolOptions, type YamlProtocolOptions, createDynamicIfThenElseSchema, createToolMiddleware, decodeOriginalTools, encodeOriginalTools, escapeRegExp, extractOnErrorOption, extractToolNamesFromOriginalTools, getDebugLevel, getPotentialStartIndex, hasInputProperty, hermesToolMiddleware, isProtocolFactory, isTCMProtocolFactory, isToolChoiceActive, isToolResultPart, jsonProtocol, logParseFailure, logParsedChunk, logParsedSummary, logRawChunk, originalToolsSchema, toolChoiceStream, transformParams, wrapGenerate, wrapStream, xmlProtocol, xmlToolMiddleware, yamlProtocol, yamlToolMiddleware };
270
+ export { type DebugLevel, type OnErrorFn, type ParserOptions, type TCMCoreProtocol, type TCMProtocol, type ToolCallMiddlewareProviderOptions, type XmlProtocolOptions, type YamlProtocolOptions, createDynamicIfThenElseSchema, createToolMiddleware, decodeOriginalTools, encodeOriginalTools, escapeRegExp, extractOnErrorOption, extractToolNamesFromOriginalTools, getDebugLevel, getPotentialStartIndex, hasInputProperty, hermesToolMiddleware, isProtocolFactory, isTCMProtocolFactory, isToolChoiceActive, isToolResultPart, jsonProtocol, logParseFailure, logParsedChunk, logParsedSummary, logRawChunk, originalToolsSchema, toolChoiceStream, transformParams, wrapGenerate, wrapStream, xmlProtocol, xmlToolMiddleware, yamlProtocol, yamlToolMiddleware };
package/dist/index.d.ts CHANGED
@@ -3,6 +3,12 @@ import { LanguageModelV3FunctionTool, LanguageModelV3ToolCall, LanguageModelV3Co
3
3
  import { ToolResultPart } from '@ai-sdk/provider-utils';
4
4
  export { ParseOptions, parse, stringify, transform } from './rjson.js';
5
5
 
6
+ /**
7
+ * Options for parsing tool calls and handling errors
8
+ */
9
+ interface ParserOptions {
10
+ onError?: (message: string, metadata?: Record<string, unknown>) => void;
11
+ }
6
12
  interface TCMProtocol {
7
13
  formatTools({ tools, toolSystemPromptTemplate, }: {
8
14
  tools: LanguageModelV3FunctionTool[];
@@ -261,4 +267,4 @@ declare function transformParams({ params, protocol, toolSystemPromptTemplate, t
261
267
  toolChoice: undefined;
262
268
  };
263
269
 
264
- export { type DebugLevel, type OnErrorFn, type TCMCoreProtocol, type TCMProtocol, type ToolCallMiddlewareProviderOptions, type XmlProtocolOptions, type YamlProtocolOptions, createDynamicIfThenElseSchema, createToolMiddleware, decodeOriginalTools, encodeOriginalTools, escapeRegExp, extractOnErrorOption, extractToolNamesFromOriginalTools, getDebugLevel, getPotentialStartIndex, hasInputProperty, hermesToolMiddleware, isProtocolFactory, isTCMProtocolFactory, isToolChoiceActive, isToolResultPart, jsonProtocol, logParseFailure, logParsedChunk, logParsedSummary, logRawChunk, originalToolsSchema, toolChoiceStream, transformParams, wrapGenerate, wrapStream, xmlProtocol, xmlToolMiddleware, yamlProtocol, yamlToolMiddleware };
270
+ export { type DebugLevel, type OnErrorFn, type ParserOptions, type TCMCoreProtocol, type TCMProtocol, type ToolCallMiddlewareProviderOptions, type XmlProtocolOptions, type YamlProtocolOptions, createDynamicIfThenElseSchema, createToolMiddleware, decodeOriginalTools, encodeOriginalTools, escapeRegExp, extractOnErrorOption, extractToolNamesFromOriginalTools, getDebugLevel, getPotentialStartIndex, hasInputProperty, hermesToolMiddleware, isProtocolFactory, isTCMProtocolFactory, isToolChoiceActive, isToolResultPart, jsonProtocol, logParseFailure, logParsedChunk, logParsedSummary, logRawChunk, originalToolsSchema, toolChoiceStream, transformParams, wrapGenerate, wrapStream, xmlProtocol, xmlToolMiddleware, yamlProtocol, yamlToolMiddleware };
package/dist/index.js CHANGED
@@ -3,7 +3,6 @@ import {
3
3
  createToolMiddleware,
4
4
  decodeOriginalTools,
5
5
  encodeOriginalTools,
6
- escapeRegExp,
7
6
  extractOnErrorOption,
8
7
  extractToolNamesFromOriginalTools,
9
8
  getDebugLevel,
@@ -28,14 +27,16 @@ import {
28
27
  xmlToolMiddleware,
29
28
  yamlProtocol,
30
29
  yamlToolMiddleware
31
- } from "./chunk-3QJVNEHE.js";
32
- import "./chunk-TR2ARLIF.js";
30
+ } from "./chunk-5WKXBBCU.js";
31
+ import {
32
+ escapeRegExp
33
+ } from "./chunk-ZDBNJWLY.js";
33
34
  import {
34
35
  parse,
35
36
  stringify,
36
37
  transform
37
38
  } from "./chunk-IX4FJELL.js";
38
- import "./chunk-DZB6Y354.js";
39
+ import "./chunk-OUGMLYAW.js";
39
40
  export {
40
41
  createDynamicIfThenElseSchema,
41
42
  createToolMiddleware,
package/dist/rxml.cjs CHANGED
@@ -406,6 +406,153 @@ function getSchemaType(schema) {
406
406
  }
407
407
  return;
408
408
  }
409
+ function schemaAllowsPropertyViaCombinators(s, key, depth) {
410
+ const anyOfValues = s.anyOf;
411
+ const oneOfValues = s.oneOf;
412
+ const allOfValues = s.allOf;
413
+ let hasCombinator = false;
414
+ let anyOfAllows = true;
415
+ let oneOfAllows = true;
416
+ let allOfAllows = true;
417
+ if (Array.isArray(anyOfValues)) {
418
+ hasCombinator = true;
419
+ anyOfAllows = anyOfValues.some(
420
+ (sub) => schemaHasProperty(sub, key, depth + 1)
421
+ );
422
+ }
423
+ if (Array.isArray(oneOfValues)) {
424
+ hasCombinator = true;
425
+ oneOfAllows = oneOfValues.some(
426
+ (sub) => schemaHasProperty(sub, key, depth + 1)
427
+ );
428
+ }
429
+ if (Array.isArray(allOfValues)) {
430
+ hasCombinator = true;
431
+ allOfAllows = allOfValues.every(
432
+ (sub) => schemaHasProperty(sub, key, depth + 1)
433
+ );
434
+ }
435
+ if (!hasCombinator) {
436
+ return false;
437
+ }
438
+ return anyOfAllows && oneOfAllows && allOfAllows;
439
+ }
440
+ function schemaHasPropertyDirectly(s, key) {
441
+ const props = s.properties;
442
+ if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] !== false) {
443
+ return true;
444
+ }
445
+ const required = s.required;
446
+ if (Array.isArray(required) && required.includes(key)) {
447
+ return true;
448
+ }
449
+ const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
450
+ return patternSchemas.some((schema) => schema !== false);
451
+ }
452
+ function schemaHasPropertyViaAdditional(s) {
453
+ const additional = s.additionalProperties;
454
+ if (additional === true || additional && typeof additional === "object" && !Array.isArray(additional)) {
455
+ return true;
456
+ }
457
+ if (Object.hasOwn(s, "additionalProperties")) {
458
+ return false;
459
+ }
460
+ const type = s.type;
461
+ const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
462
+ const hasObjectKeywords = s.properties && typeof s.properties === "object" && !Array.isArray(s.properties) || s.patternProperties && typeof s.patternProperties === "object" && !Array.isArray(s.patternProperties) || Array.isArray(s.required) && s.required.length > 0;
463
+ return !!(isObjectType || hasObjectKeywords);
464
+ }
465
+ function schemaDisallowsPropertyDirectly(s, key) {
466
+ const props = s.properties;
467
+ if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] === false) {
468
+ return true;
469
+ }
470
+ const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
471
+ return patternSchemas.some((schema) => schema === false);
472
+ }
473
+ function schemaHasProperty(schema, key, depth = 0) {
474
+ if (depth > 5) {
475
+ return true;
476
+ }
477
+ const unwrapped = unwrapJsonSchema(schema);
478
+ if (schemaIsUnconstrained(unwrapped)) {
479
+ return true;
480
+ }
481
+ if (!unwrapped || typeof unwrapped !== "object") {
482
+ return false;
483
+ }
484
+ const s = unwrapped;
485
+ if (schemaDisallowsPropertyDirectly(s, key)) {
486
+ return false;
487
+ }
488
+ if (schemaHasPropertyDirectly(s, key)) {
489
+ return true;
490
+ }
491
+ if (schemaHasPropertyViaAdditional(s)) {
492
+ return true;
493
+ }
494
+ return schemaAllowsPropertyViaCombinators(s, key, depth);
495
+ }
496
+ function schemaIsUnconstrained(schema) {
497
+ const unwrapped = unwrapJsonSchema(schema);
498
+ if (unwrapped == null || unwrapped === true) {
499
+ return true;
500
+ }
501
+ if (typeof unwrapped !== "object" || Array.isArray(unwrapped)) {
502
+ return false;
503
+ }
504
+ return Object.keys(unwrapped).length === 0;
505
+ }
506
+ function getPatternSchemasForKey(patternProperties, key) {
507
+ if (!patternProperties || typeof patternProperties !== "object" || Array.isArray(patternProperties)) {
508
+ return [];
509
+ }
510
+ const schemas = [];
511
+ for (const [pattern, schema] of Object.entries(
512
+ patternProperties
513
+ )) {
514
+ try {
515
+ const regex = new RegExp(pattern);
516
+ if (regex.test(key)) {
517
+ schemas.push(schema);
518
+ }
519
+ } catch (e) {
520
+ }
521
+ }
522
+ return schemas;
523
+ }
524
+ function coerceValueForKey(value, key, unwrapped) {
525
+ const schemas = [];
526
+ const props = unwrapped.properties;
527
+ if (props && Object.hasOwn(props, key)) {
528
+ schemas.push(props[key]);
529
+ }
530
+ const patternSchemas = getPatternSchemasForKey(
531
+ unwrapped.patternProperties,
532
+ key
533
+ );
534
+ if (patternSchemas.length > 0) {
535
+ schemas.push(...patternSchemas);
536
+ }
537
+ if (schemas.length > 0) {
538
+ let out = value;
539
+ for (const schema of schemas) {
540
+ if (typeof schema === "boolean") {
541
+ continue;
542
+ }
543
+ out = coerceBySchema(out, schema);
544
+ }
545
+ return out;
546
+ }
547
+ const additional = unwrapped.additionalProperties;
548
+ if (additional && typeof additional === "object" && !Array.isArray(additional)) {
549
+ return coerceBySchema(value, additional);
550
+ }
551
+ if (additional === true || additional === false) {
552
+ return value;
553
+ }
554
+ return coerceBySchema(value, void 0);
555
+ }
409
556
  function coerceStringWithoutSchema(value) {
410
557
  const s = value.trim();
411
558
  const lower = s.toLowerCase();
@@ -436,13 +583,7 @@ function coerceStringToObject(s, unwrapped) {
436
583
  normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
437
584
  const obj = JSON.parse(normalized);
438
585
  if (obj && typeof obj === "object" && !Array.isArray(obj)) {
439
- const props = unwrapped.properties;
440
- const out = {};
441
- for (const [k, v] of Object.entries(obj)) {
442
- const propSchema = props ? props[k] : void 0;
443
- out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
444
- }
445
- return out;
586
+ return coerceObjectToObject(obj, unwrapped);
446
587
  }
447
588
  } catch (e) {
448
589
  }
@@ -472,10 +613,8 @@ function coerceStringToArray(s, unwrapped) {
472
613
  }
473
614
  function coerceObjectToObject(value, unwrapped) {
474
615
  const out = {};
475
- const props = unwrapped.properties;
476
616
  for (const [k, v] of Object.entries(value)) {
477
- const propSchema = props ? props[k] : void 0;
478
- out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
617
+ out[k] = coerceValueForKey(v, k, unwrapped);
479
618
  }
480
619
  return out;
481
620
  }
@@ -492,16 +631,22 @@ function coerceObjectToArray(maybe, prefixItems, itemsSchema) {
492
631
  return coerceArrayToArray(arr, prefixItems, itemsSchema);
493
632
  }
494
633
  const keys = Object.keys(maybe);
495
- if (keys.length === 1) {
496
- const singleValue = maybe[keys[0]];
497
- if (Array.isArray(singleValue)) {
498
- return singleValue.map((v) => coerceBySchema(v, itemsSchema));
499
- }
500
- }
501
634
  if (keys.length > 0 && keys.every((k) => DIGIT_KEY_REGEX.test(k))) {
502
635
  const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
503
636
  return coerceArrayToArray(arr, prefixItems, itemsSchema);
504
637
  }
638
+ if (keys.length === 1) {
639
+ const singleKey = keys[0];
640
+ if (!(schemaIsUnconstrained(itemsSchema) || schemaHasProperty(itemsSchema, singleKey))) {
641
+ const singleValue = maybe[singleKey];
642
+ if (Array.isArray(singleValue)) {
643
+ return singleValue.map((v) => coerceBySchema(v, itemsSchema));
644
+ }
645
+ if (singleValue && typeof singleValue === "object") {
646
+ return [coerceBySchema(singleValue, itemsSchema)];
647
+ }
648
+ }
649
+ }
505
650
  return null;
506
651
  }
507
652
  function coercePrimitiveToArray(value, prefixItems, itemsSchema) {
@@ -561,11 +706,15 @@ function coerceArrayValue(value, prefixItems, itemsSchema) {
561
706
  if (result !== null) {
562
707
  return result;
563
708
  }
709
+ if (getSchemaType(itemsSchema) === "array") {
710
+ return [value];
711
+ }
712
+ return [coerceBySchema(value, itemsSchema)];
564
713
  }
565
714
  if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
566
715
  return coercePrimitiveToArray(value, prefixItems, itemsSchema);
567
716
  }
568
- return value;
717
+ return [value];
569
718
  }
570
719
  function coerceBySchema(value, schema) {
571
720
  const unwrapped = unwrapJsonSchema(schema);
@@ -2101,6 +2250,11 @@ function createIntermediateCall(toolName, rawSegment, schema) {
2101
2250
  };
2102
2251
  }
2103
2252
 
2253
+ // src/core/utils/regex.ts
2254
+ function escapeRegExp(literal) {
2255
+ return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2256
+ }
2257
+
2104
2258
  // src/rxml/heuristics/xml-defaults.ts
2105
2259
  var MALFORMED_CLOSE_RE_G = /<\/\s+([A-Za-z0-9_:-]+)\s*>/g;
2106
2260
  var MALFORMED_CLOSE_RE = /<\/\s+([A-Za-z0-9_:-]+)\s*>/;
@@ -2342,9 +2496,6 @@ function getStringPropertyNames(schema) {
2342
2496
  }
2343
2497
  return names;
2344
2498
  }
2345
- function escapeRegExp(s) {
2346
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2347
- }
2348
2499
  function dedupeSingleTag(xml, key) {
2349
2500
  var _a, _b;
2350
2501
  const escaped = escapeRegExp(key);