@ai-sdk-tool/parser 3.3.1 → 3.3.3

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[];
@@ -94,11 +100,12 @@ declare function createDynamicIfThenElseSchema(tools: (LanguageModelV3FunctionTo
94
100
  */
95
101
  declare function getPotentialStartIndex(text: string, searchedText: string): number | null;
96
102
 
97
- type OnErrorFn = (message: string, metadata?: Record<string, unknown>) => void;
103
+ type OnErrorFn$1 = (message: string, metadata?: Record<string, unknown>) => void;
98
104
  declare function extractOnErrorOption(providerOptions?: unknown): {
99
- onError?: OnErrorFn;
105
+ onError?: OnErrorFn$1;
100
106
  } | undefined;
101
107
 
108
+ type OnErrorFn = (message: string, metadata?: Record<string, unknown>) => void;
102
109
  interface ToolCallMiddlewareProviderOptions {
103
110
  toolCallMiddleware?: {
104
111
  debugSummary?: {
@@ -119,14 +126,18 @@ declare const originalToolsSchema: {
119
126
  encode: typeof encodeOriginalTools;
120
127
  decode: typeof decodeOriginalTools;
121
128
  };
122
- declare function encodeOriginalTools(tools: LanguageModelV3FunctionTool[] | undefined): Array<{
129
+ interface EncodedOriginalTool {
123
130
  name: string;
124
131
  inputSchema: string;
125
- }>;
126
- declare function decodeOriginalTools(originalTools: Array<{
132
+ }
133
+ interface DecodeOriginalToolsOptions {
134
+ onError?: OnErrorFn;
135
+ }
136
+ declare function encodeOriginalTools(tools: LanguageModelV3FunctionTool[] | undefined): Array<{
127
137
  name: string;
128
138
  inputSchema: string;
129
- }> | undefined): LanguageModelV3FunctionTool[];
139
+ }>;
140
+ declare function decodeOriginalTools(originalTools: EncodedOriginalTool[] | undefined, options?: DecodeOriginalToolsOptions): LanguageModelV3FunctionTool[];
130
141
  declare function extractToolNamesFromOriginalTools(originalTools: Array<{
131
142
  name: string;
132
143
  inputSchema: string;
@@ -176,8 +187,9 @@ declare function wrapStream({ protocol, doStream, doGenerate, params, }: {
176
187
  headers?: _ai_sdk_provider.SharedV3Headers;
177
188
  };
178
189
  }>;
179
- declare function toolChoiceStream({ doGenerate, options, }: {
190
+ declare function toolChoiceStream({ doGenerate, tools, options, }: {
180
191
  doGenerate: () => ReturnType<LanguageModelV3["doGenerate"]>;
192
+ tools?: LanguageModelV3FunctionTool[];
181
193
  options?: {
182
194
  onError?: (message: string, metadata?: Record<string, unknown>) => void;
183
195
  };
@@ -261,4 +273,4 @@ declare function transformParams({ params, protocol, toolSystemPromptTemplate, t
261
273
  toolChoice: undefined;
262
274
  };
263
275
 
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 };
276
+ export { type DebugLevel, type OnErrorFn$1 as 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[];
@@ -94,11 +100,12 @@ declare function createDynamicIfThenElseSchema(tools: (LanguageModelV3FunctionTo
94
100
  */
95
101
  declare function getPotentialStartIndex(text: string, searchedText: string): number | null;
96
102
 
97
- type OnErrorFn = (message: string, metadata?: Record<string, unknown>) => void;
103
+ type OnErrorFn$1 = (message: string, metadata?: Record<string, unknown>) => void;
98
104
  declare function extractOnErrorOption(providerOptions?: unknown): {
99
- onError?: OnErrorFn;
105
+ onError?: OnErrorFn$1;
100
106
  } | undefined;
101
107
 
108
+ type OnErrorFn = (message: string, metadata?: Record<string, unknown>) => void;
102
109
  interface ToolCallMiddlewareProviderOptions {
103
110
  toolCallMiddleware?: {
104
111
  debugSummary?: {
@@ -119,14 +126,18 @@ declare const originalToolsSchema: {
119
126
  encode: typeof encodeOriginalTools;
120
127
  decode: typeof decodeOriginalTools;
121
128
  };
122
- declare function encodeOriginalTools(tools: LanguageModelV3FunctionTool[] | undefined): Array<{
129
+ interface EncodedOriginalTool {
123
130
  name: string;
124
131
  inputSchema: string;
125
- }>;
126
- declare function decodeOriginalTools(originalTools: Array<{
132
+ }
133
+ interface DecodeOriginalToolsOptions {
134
+ onError?: OnErrorFn;
135
+ }
136
+ declare function encodeOriginalTools(tools: LanguageModelV3FunctionTool[] | undefined): Array<{
127
137
  name: string;
128
138
  inputSchema: string;
129
- }> | undefined): LanguageModelV3FunctionTool[];
139
+ }>;
140
+ declare function decodeOriginalTools(originalTools: EncodedOriginalTool[] | undefined, options?: DecodeOriginalToolsOptions): LanguageModelV3FunctionTool[];
130
141
  declare function extractToolNamesFromOriginalTools(originalTools: Array<{
131
142
  name: string;
132
143
  inputSchema: string;
@@ -176,8 +187,9 @@ declare function wrapStream({ protocol, doStream, doGenerate, params, }: {
176
187
  headers?: _ai_sdk_provider.SharedV3Headers;
177
188
  };
178
189
  }>;
179
- declare function toolChoiceStream({ doGenerate, options, }: {
190
+ declare function toolChoiceStream({ doGenerate, tools, options, }: {
180
191
  doGenerate: () => ReturnType<LanguageModelV3["doGenerate"]>;
192
+ tools?: LanguageModelV3FunctionTool[];
181
193
  options?: {
182
194
  onError?: (message: string, metadata?: Record<string, unknown>) => void;
183
195
  };
@@ -261,4 +273,4 @@ declare function transformParams({ params, protocol, toolSystemPromptTemplate, t
261
273
  toolChoice: undefined;
262
274
  };
263
275
 
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 };
276
+ export { type DebugLevel, type OnErrorFn$1 as 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-7E6UFDFQ.js";
32
- import "./chunk-EW3A6Y7O.js";
30
+ } from "./chunk-NAQSTPDQ.js";
31
+ import {
32
+ escapeRegExp
33
+ } from "./chunk-CXWS24JX.js";
33
34
  import {
34
35
  parse,
35
36
  stringify,
36
37
  transform
37
38
  } from "./chunk-IX4FJELL.js";
38
- import "./chunk-OUGMLYAW.js";
39
+ import "./chunk-2KK5BDZF.js";
39
40
  export {
40
41
  createDynamicIfThenElseSchema,
41
42
  createToolMiddleware,
package/dist/rxml.cjs CHANGED
@@ -363,6 +363,13 @@ var EMPTY_OBJECT_REGEX = /^\{\s*\}$/s;
363
363
  var NEWLINE_SPLIT_REGEX = /\n+/;
364
364
  var COMMA_SPLIT_REGEX = /,\s*/;
365
365
  var DIGIT_KEY_REGEX = /^\d+$/;
366
+ var WHITESPACE_REGEX = /\s+/g;
367
+ var HAS_WHITESPACE_REGEX = /\s/;
368
+ var SINGLE_QUOTE = "'";
369
+ var DOUBLE_QUOTE = '"';
370
+ var SNAKE_SEGMENT_REGEX = /_([a-zA-Z0-9])/g;
371
+ var CAMEL_BOUNDARY_REGEX = /([a-z0-9])([A-Z])/g;
372
+ var LEADING_UNDERSCORES_REGEX = /^_+/;
366
373
  function unwrapJsonSchema(schema) {
367
374
  if (!schema || typeof schema !== "object") {
368
375
  return schema;
@@ -611,9 +618,158 @@ function coerceStringToArray(s, unwrapped) {
611
618
  }
612
619
  return null;
613
620
  }
621
+ function getStrictObjectSchemaInfo(unwrapped) {
622
+ if (getSchemaType(unwrapped) !== "object") {
623
+ return null;
624
+ }
625
+ if (unwrapped.additionalProperties !== false) {
626
+ return null;
627
+ }
628
+ const properties = unwrapped.properties;
629
+ if (!properties || typeof properties !== "object" || Array.isArray(properties)) {
630
+ return null;
631
+ }
632
+ const propertyMap = properties;
633
+ const required = Array.isArray(unwrapped.required) ? unwrapped.required.filter(
634
+ (value) => typeof value === "string" && value.length > 0
635
+ ) : [];
636
+ const patternProps = unwrapped.patternProperties;
637
+ const patternProperties = patternProps && typeof patternProps === "object" && !Array.isArray(patternProps) ? patternProps : void 0;
638
+ return {
639
+ properties: propertyMap,
640
+ required,
641
+ patternProperties
642
+ };
643
+ }
644
+ function isSingularPluralPair(left, right) {
645
+ return left.length > 1 && right.length > 1 && (left === `${right}s` || right === `${left}s`);
646
+ }
647
+ function snakeToCamel(value) {
648
+ const trimmed = value.replace(LEADING_UNDERSCORES_REGEX, "");
649
+ if (trimmed.length === 0) {
650
+ return value;
651
+ }
652
+ const camelized = trimmed.replace(
653
+ SNAKE_SEGMENT_REGEX,
654
+ (_, c) => c.toUpperCase()
655
+ );
656
+ return camelized.charAt(0).toLowerCase() + camelized.slice(1);
657
+ }
658
+ function camelToSnake(value) {
659
+ return value.replace(CAMEL_BOUNDARY_REGEX, "$1_$2").toLowerCase();
660
+ }
661
+ function isCaseStylePair(targetKey, sourceKey) {
662
+ if (targetKey === sourceKey) {
663
+ return false;
664
+ }
665
+ const sourceLooksSnake = sourceKey.includes("_");
666
+ const targetLooksSnake = targetKey.includes("_");
667
+ if (sourceLooksSnake && snakeToCamel(sourceKey) === targetKey) {
668
+ return true;
669
+ }
670
+ if (!sourceLooksSnake && targetLooksSnake && camelToSnake(sourceKey) === targetKey) {
671
+ return true;
672
+ }
673
+ return false;
674
+ }
675
+ function isUnexpectedKey(key, schemaInfo) {
676
+ if (Object.hasOwn(schemaInfo.properties, key)) {
677
+ return false;
678
+ }
679
+ const patternSchemas = getPatternSchemasForKey(
680
+ schemaInfo.patternProperties,
681
+ key
682
+ );
683
+ if (patternSchemas.length > 0) {
684
+ return patternSchemas.every((schema) => schema === false);
685
+ }
686
+ return true;
687
+ }
688
+ function computeMissingAndUnexpectedKeys(input, schemaInfo) {
689
+ const missingRequired = schemaInfo.required.filter(
690
+ (key) => !Object.hasOwn(input, key)
691
+ );
692
+ const unexpectedKeys = Object.keys(input).filter(
693
+ (key) => isUnexpectedKey(key, schemaInfo)
694
+ );
695
+ return { missingRequired, unexpectedKeys };
696
+ }
697
+ function applySingularPluralRequiredKeyRename(input, schemaInfo) {
698
+ const { missingRequired, unexpectedKeys } = computeMissingAndUnexpectedKeys(
699
+ input,
700
+ schemaInfo
701
+ );
702
+ if (missingRequired.length !== 1 || unexpectedKeys.length !== 1) {
703
+ return null;
704
+ }
705
+ const targetKey = missingRequired[0];
706
+ const sourceKey = unexpectedKeys[0];
707
+ if (!Object.hasOwn(schemaInfo.properties, targetKey)) {
708
+ return null;
709
+ }
710
+ if (!isSingularPluralPair(targetKey, sourceKey)) {
711
+ return null;
712
+ }
713
+ if (getSchemaType(schemaInfo.properties[targetKey]) !== "array") {
714
+ return null;
715
+ }
716
+ if (!Array.isArray(input[sourceKey])) {
717
+ return null;
718
+ }
719
+ if (!Object.hasOwn(input, sourceKey) || Object.hasOwn(input, targetKey)) {
720
+ return null;
721
+ }
722
+ const output = { ...input };
723
+ output[targetKey] = output[sourceKey];
724
+ delete output[sourceKey];
725
+ return output;
726
+ }
727
+ function applyCaseStyleRequiredKeyRename(input, schemaInfo) {
728
+ const { missingRequired, unexpectedKeys } = computeMissingAndUnexpectedKeys(
729
+ input,
730
+ schemaInfo
731
+ );
732
+ if (missingRequired.length !== 1 || unexpectedKeys.length !== 1) {
733
+ return null;
734
+ }
735
+ const targetKey = missingRequired[0];
736
+ const sourceKey = unexpectedKeys[0];
737
+ if (!Object.hasOwn(schemaInfo.properties, targetKey)) {
738
+ return null;
739
+ }
740
+ if (!isCaseStylePair(targetKey, sourceKey)) {
741
+ return null;
742
+ }
743
+ if (!Object.hasOwn(input, sourceKey) || Object.hasOwn(input, targetKey)) {
744
+ return null;
745
+ }
746
+ const output = { ...input };
747
+ output[targetKey] = output[sourceKey];
748
+ delete output[sourceKey];
749
+ return output;
750
+ }
751
+ function applyStrictRequiredKeyRename(input, unwrapped) {
752
+ const schemaInfo = getStrictObjectSchemaInfo(unwrapped);
753
+ if (!schemaInfo) {
754
+ return input;
755
+ }
756
+ const singularPlural = applySingularPluralRequiredKeyRename(
757
+ input,
758
+ schemaInfo
759
+ );
760
+ if (singularPlural) {
761
+ return singularPlural;
762
+ }
763
+ const caseStyle = applyCaseStyleRequiredKeyRename(input, schemaInfo);
764
+ if (caseStyle) {
765
+ return caseStyle;
766
+ }
767
+ return input;
768
+ }
614
769
  function coerceObjectToObject(value, unwrapped) {
770
+ const normalizedInput = applyStrictRequiredKeyRename(value, unwrapped);
615
771
  const out = {};
616
- for (const [k, v] of Object.entries(value)) {
772
+ for (const [k, v] of Object.entries(normalizedInput)) {
617
773
  out[k] = coerceValueForKey(v, k, unwrapped);
618
774
  }
619
775
  return out;
@@ -624,6 +780,109 @@ function coerceArrayToArray(value, prefixItems, itemsSchema) {
624
780
  }
625
781
  return value.map((v) => coerceBySchema(v, itemsSchema));
626
782
  }
783
+ function isPrimitiveSchemaType(schemaType) {
784
+ return schemaType === "string" || schemaType === "number" || schemaType === "integer" || schemaType === "boolean";
785
+ }
786
+ function isPrimitiveMatchForSchemaType(value, schemaType) {
787
+ if (schemaType === "string") {
788
+ return typeof value === "string";
789
+ }
790
+ if (schemaType === "number") {
791
+ return typeof value === "number" && Number.isFinite(value);
792
+ }
793
+ if (schemaType === "integer") {
794
+ return typeof value === "number" && Number.isFinite(value) && Number.isInteger(value);
795
+ }
796
+ return typeof value === "boolean";
797
+ }
798
+ function coercePrimitiveWrappedObject(value, itemsSchema) {
799
+ const schemaType = getSchemaType(itemsSchema);
800
+ if (!isPrimitiveSchemaType(schemaType)) {
801
+ return null;
802
+ }
803
+ const keys = Object.keys(value);
804
+ if (keys.length !== 1) {
805
+ return null;
806
+ }
807
+ const singleValue = value[keys[0]];
808
+ if (singleValue && typeof singleValue === "object") {
809
+ return null;
810
+ }
811
+ const coerced = coerceBySchema(singleValue, itemsSchema);
812
+ return isPrimitiveMatchForSchemaType(coerced, schemaType) ? coerced : null;
813
+ }
814
+ function coerceParallelArraysObjectToArray(maybe, prefixItems, itemsSchema) {
815
+ if (prefixItems && prefixItems.length > 0) {
816
+ return null;
817
+ }
818
+ const unwrappedItems = unwrapJsonSchema(itemsSchema);
819
+ if (!unwrappedItems || typeof unwrappedItems !== "object" || Array.isArray(unwrappedItems)) {
820
+ return null;
821
+ }
822
+ const itemSchema = unwrappedItems;
823
+ if (getSchemaType(itemSchema) !== "object") {
824
+ return null;
825
+ }
826
+ if (itemSchema.additionalProperties !== false) {
827
+ return null;
828
+ }
829
+ const properties = itemSchema.properties;
830
+ if (!properties || typeof properties !== "object" || Array.isArray(properties)) {
831
+ return null;
832
+ }
833
+ const propertyMap = properties;
834
+ const entries = Object.entries(maybe);
835
+ if (entries.length < 2) {
836
+ return null;
837
+ }
838
+ if (!entries.every(([, value]) => Array.isArray(value))) {
839
+ return null;
840
+ }
841
+ if (!entries.every(([key]) => Object.hasOwn(propertyMap, key))) {
842
+ return null;
843
+ }
844
+ if (!entries.every(([key]) => {
845
+ const schemaType = getSchemaType(propertyMap[key]);
846
+ return schemaType !== "array" && schemaType !== "object";
847
+ })) {
848
+ return null;
849
+ }
850
+ const lengths = [
851
+ ...new Set(entries.map(([, value]) => value.length))
852
+ ];
853
+ if (lengths.length !== 1) {
854
+ return null;
855
+ }
856
+ const length = lengths[0];
857
+ if (length < 2) {
858
+ return null;
859
+ }
860
+ const zipped = [];
861
+ for (let index = 0; index < length; index += 1) {
862
+ const item = {};
863
+ for (const [key, value] of entries) {
864
+ item[key] = value[index];
865
+ }
866
+ zipped.push(item);
867
+ }
868
+ return coerceArrayToArray(zipped, prefixItems, itemsSchema);
869
+ }
870
+ function coerceSingleKeyObjectToArray(singleValue, itemsSchema) {
871
+ if (Array.isArray(singleValue)) {
872
+ return singleValue.map((v) => coerceBySchema(v, itemsSchema));
873
+ }
874
+ if (singleValue && typeof singleValue === "object") {
875
+ const primitiveWrapped = coercePrimitiveWrappedObject(
876
+ singleValue,
877
+ itemsSchema
878
+ );
879
+ if (primitiveWrapped !== null) {
880
+ return [primitiveWrapped];
881
+ }
882
+ return [coerceBySchema(singleValue, itemsSchema)];
883
+ }
884
+ return null;
885
+ }
627
886
  function coerceObjectToArray(maybe, prefixItems, itemsSchema) {
628
887
  if (Object.hasOwn(maybe, "item")) {
629
888
  const items = maybe.item;
@@ -635,15 +894,23 @@ function coerceObjectToArray(maybe, prefixItems, itemsSchema) {
635
894
  const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
636
895
  return coerceArrayToArray(arr, prefixItems, itemsSchema);
637
896
  }
897
+ const parallelArrays = coerceParallelArraysObjectToArray(
898
+ maybe,
899
+ prefixItems,
900
+ itemsSchema
901
+ );
902
+ if (parallelArrays !== null) {
903
+ return parallelArrays;
904
+ }
638
905
  if (keys.length === 1) {
639
906
  const singleKey = keys[0];
640
907
  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)];
908
+ const result = coerceSingleKeyObjectToArray(
909
+ maybe[singleKey],
910
+ itemsSchema
911
+ );
912
+ if (result !== null) {
913
+ return result;
647
914
  }
648
915
  }
649
916
  }
@@ -673,6 +940,86 @@ function coerceStringToPrimitive(s, schemaType) {
673
940
  }
674
941
  return null;
675
942
  }
943
+ function coercePrimitiveToString(value, schemaType) {
944
+ if (schemaType !== "string") {
945
+ return null;
946
+ }
947
+ if (typeof value === "boolean") {
948
+ return value ? "true" : "false";
949
+ }
950
+ if (typeof value === "number" && Number.isFinite(value)) {
951
+ return String(value);
952
+ }
953
+ return null;
954
+ }
955
+ function coerceStringByEnumWhitespace(rawValue, unwrapped) {
956
+ const enumValues = unwrapped.enum;
957
+ if (!Array.isArray(enumValues) || enumValues.length === 0) {
958
+ return null;
959
+ }
960
+ if (!enumValues.every((item) => typeof item === "string")) {
961
+ return null;
962
+ }
963
+ const normalizedEnumValues = enumValues;
964
+ if (normalizedEnumValues.includes(rawValue)) {
965
+ return null;
966
+ }
967
+ const unquoted = unwrapMatchingQuotes(rawValue);
968
+ if (unquoted !== null) {
969
+ const exactMatches = normalizedEnumValues.filter(
970
+ (item) => item === unquoted
971
+ );
972
+ if (exactMatches.length === 1) {
973
+ return exactMatches[0];
974
+ }
975
+ }
976
+ const candidates = [rawValue, unquoted].filter(
977
+ (item) => item !== null
978
+ );
979
+ for (const candidate of candidates) {
980
+ if (!HAS_WHITESPACE_REGEX.test(candidate)) {
981
+ continue;
982
+ }
983
+ const normalizedInput = candidate.replace(WHITESPACE_REGEX, "");
984
+ const matches = normalizedEnumValues.filter(
985
+ (item) => item.replace(WHITESPACE_REGEX, "") === normalizedInput
986
+ );
987
+ if (matches.length === 1) {
988
+ return matches[0];
989
+ }
990
+ }
991
+ return null;
992
+ }
993
+ function unwrapMatchingQuotes(value) {
994
+ if (value.length < 2) {
995
+ return null;
996
+ }
997
+ const first = value[0];
998
+ const last = value.at(-1);
999
+ const isQuote = (first === SINGLE_QUOTE || first === DOUBLE_QUOTE) && first === last;
1000
+ if (!isQuote) {
1001
+ return null;
1002
+ }
1003
+ return value.slice(1, -1);
1004
+ }
1005
+ function coerceObjectToPrimitive(value, schemaType, fullSchema) {
1006
+ if (!isPrimitiveSchemaType(schemaType)) {
1007
+ return null;
1008
+ }
1009
+ const keys = Object.keys(value);
1010
+ if (keys.length !== 1) {
1011
+ return null;
1012
+ }
1013
+ const singleValue = value[keys[0]];
1014
+ if (singleValue && typeof singleValue === "object") {
1015
+ return null;
1016
+ }
1017
+ const coerced = coerceBySchema(
1018
+ singleValue,
1019
+ fullSchema != null ? fullSchema : { type: schemaType }
1020
+ );
1021
+ return isPrimitiveMatchForSchemaType(coerced, schemaType) ? coerced : null;
1022
+ }
676
1023
  function coerceStringValue(value, schemaType, u) {
677
1024
  const s = value.trim();
678
1025
  if (schemaType === "object") {
@@ -691,6 +1038,10 @@ function coerceStringValue(value, schemaType, u) {
691
1038
  if (primitiveResult !== null) {
692
1039
  return primitiveResult;
693
1040
  }
1041
+ const enumWhitespaceCanonical = coerceStringByEnumWhitespace(s, u);
1042
+ if (enumWhitespaceCanonical !== null) {
1043
+ return enumWhitespaceCanonical;
1044
+ }
694
1045
  return value;
695
1046
  }
696
1047
  function coerceArrayValue(value, prefixItems, itemsSchema) {
@@ -729,9 +1080,23 @@ function coerceBySchema(value, schema) {
729
1080
  if (typeof value === "string") {
730
1081
  return coerceStringValue(value, schemaType, u);
731
1082
  }
1083
+ const primitiveString = coercePrimitiveToString(value, schemaType);
1084
+ if (primitiveString !== null) {
1085
+ return primitiveString;
1086
+ }
732
1087
  if (schemaType === "object" && value && typeof value === "object" && !Array.isArray(value)) {
733
1088
  return coerceObjectToObject(value, u);
734
1089
  }
1090
+ if (value && typeof value === "object" && !Array.isArray(value) && isPrimitiveSchemaType(schemaType)) {
1091
+ const primitiveResult = coerceObjectToPrimitive(
1092
+ value,
1093
+ schemaType,
1094
+ u
1095
+ );
1096
+ if (primitiveResult !== null) {
1097
+ return primitiveResult;
1098
+ }
1099
+ }
735
1100
  if (schemaType === "array") {
736
1101
  const prefixItems = Array.isArray(u.prefixItems) ? u.prefixItems : void 0;
737
1102
  const itemsSchema = u.items;
@@ -1746,7 +2111,7 @@ var XMLTokenizer = class {
1746
2111
  };
1747
2112
 
1748
2113
  // src/rxml/core/parser.ts
1749
- var WHITESPACE_REGEX = /\s/;
2114
+ var WHITESPACE_REGEX2 = /\s/;
1750
2115
  var NUMERIC_STRING_REGEX = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
1751
2116
  var DIGIT_KEY_REGEX2 = /^\d+$/;
1752
2117
  function getTopLevelStringProps(s) {
@@ -1916,7 +2281,7 @@ function parse(xmlInner, schema, options = {}) {
1916
2281
  const closeHead = s.indexOf(`</${rootName}`, range.end);
1917
2282
  if (closeHead === range.end) {
1918
2283
  let p = closeHead + 2 + rootName.length;
1919
- while (p < s.length && WHITESPACE_REGEX.test(s[p])) {
2284
+ while (p < s.length && WHITESPACE_REGEX2.test(s[p])) {
1920
2285
  p += 1;
1921
2286
  }
1922
2287
  if (s[p] === ">") {
@@ -2250,11 +2615,16 @@ function createIntermediateCall(toolName, rawSegment, schema) {
2250
2615
  };
2251
2616
  }
2252
2617
 
2618
+ // src/core/utils/regex.ts
2619
+ function escapeRegExp(literal) {
2620
+ return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2621
+ }
2622
+
2253
2623
  // src/rxml/heuristics/xml-defaults.ts
2254
2624
  var MALFORMED_CLOSE_RE_G = /<\/\s+([A-Za-z0-9_:-]+)\s*>/g;
2255
2625
  var MALFORMED_CLOSE_RE = /<\/\s+([A-Za-z0-9_:-]+)\s*>/;
2256
2626
  var STATUS_TO_STEP_BOUNDARY_RE = /<\/status>\s*<step>/g;
2257
- var WHITESPACE_REGEX2 = /\s/;
2627
+ var WHITESPACE_REGEX3 = /\s/;
2258
2628
  var NAME_CHAR_RE = /[A-Za-z0-9_:-]/;
2259
2629
  var NAME_START_CHAR_RE = /[A-Za-z_:]/;
2260
2630
  var STEP_TAG_RE = /<step>([\s\S]*?)<\/step>/i;
@@ -2395,7 +2765,7 @@ function balanceTags(xml) {
2395
2765
  }
2396
2766
  function skipWs(s, p, len) {
2397
2767
  let idx = p;
2398
- while (idx < len && WHITESPACE_REGEX2.test(s[idx])) {
2768
+ while (idx < len && WHITESPACE_REGEX3.test(s[idx])) {
2399
2769
  idx += 1;
2400
2770
  }
2401
2771
  return idx;
@@ -2448,7 +2818,7 @@ function handleOpeningTagSegment(src, lt, out, stack) {
2448
2818
  return len;
2449
2819
  }
2450
2820
  let r = q - 1;
2451
- while (r >= nameStart && WHITESPACE_REGEX2.test(src[r])) {
2821
+ while (r >= nameStart && WHITESPACE_REGEX3.test(src[r])) {
2452
2822
  r -= 1;
2453
2823
  }
2454
2824
  const selfClosing = src[r] === "/";
@@ -2491,9 +2861,6 @@ function getStringPropertyNames(schema) {
2491
2861
  }
2492
2862
  return names;
2493
2863
  }
2494
- function escapeRegExp(s) {
2495
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2496
- }
2497
2864
  function dedupeSingleTag(xml, key) {
2498
2865
  var _a, _b;
2499
2866
  const escaped = escapeRegExp(key);