@ai-sdk-tool/parser 3.2.1 → 3.3.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.
@@ -17,7 +17,6 @@ var __copyProps = (to, from, except, desc) => {
17
17
  }
18
18
  return to;
19
19
  };
20
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
21
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
21
  // If the importer is in node compatibility mode or this is not an ESM
23
22
  // file that has been converted to a CommonJS file using a Babel-
@@ -36,43 +35,642 @@ __export(community_exports, {
36
35
  });
37
36
  module.exports = __toCommonJS(community_exports);
38
37
 
39
- // src/index.ts
40
- var src_exports = {};
41
- __export(src_exports, {
42
- createDynamicIfThenElseSchema: () => createDynamicIfThenElseSchema,
43
- createToolMiddleware: () => createToolMiddleware,
44
- decodeOriginalTools: () => decodeOriginalTools,
45
- encodeOriginalTools: () => encodeOriginalTools,
46
- escapeRegExp: () => escapeRegExp,
47
- extractOnErrorOption: () => extractOnErrorOption,
48
- extractToolNamesFromOriginalTools: () => extractToolNamesFromOriginalTools,
49
- getDebugLevel: () => getDebugLevel,
50
- getPotentialStartIndex: () => getPotentialStartIndex,
51
- hasInputProperty: () => hasInputProperty,
52
- hermesToolMiddleware: () => hermesToolMiddleware,
53
- isProtocolFactory: () => isProtocolFactory,
54
- isTCMProtocolFactory: () => isTCMProtocolFactory,
55
- isToolChoiceActive: () => isToolChoiceActive,
56
- isToolResultPart: () => isToolResultPart,
57
- jsonProtocol: () => jsonProtocol,
58
- logParseFailure: () => logParseFailure,
59
- logParsedChunk: () => logParsedChunk,
60
- logParsedSummary: () => logParsedSummary,
61
- logRawChunk: () => logRawChunk,
62
- originalToolsSchema: () => originalToolsSchema,
63
- toolChoiceStream: () => toolChoiceStream,
64
- transformParams: () => transformParams,
65
- wrapGenerate: () => wrapGenerate,
66
- wrapStream: () => wrapStream,
67
- xmlProtocol: () => xmlProtocol,
68
- xmlToolMiddleware: () => xmlToolMiddleware,
69
- yamlProtocol: () => yamlProtocol,
70
- yamlToolMiddleware: () => yamlToolMiddleware
71
- });
72
- __reExport(src_exports, require("@ai-sdk-tool/rjson"));
73
-
74
- // src/core/protocols/json-protocol.ts
75
- var import_rjson = require("@ai-sdk-tool/rjson");
38
+ // src/rjson/index.ts
39
+ var WHITESPACE_TEST_REGEX = /\s/;
40
+ var WHITESPACE_REGEX = /^\s+/;
41
+ var OBJECT_START_REGEX = /^\{/;
42
+ var OBJECT_END_REGEX = /^\}/;
43
+ var ARRAY_START_REGEX = /^\[/;
44
+ var ARRAY_END_REGEX = /^\]/;
45
+ var COMMA_REGEX = /^,/;
46
+ var COLON_REGEX = /^:/;
47
+ var KEYWORD_REGEX = /^(?:true|false|null)/;
48
+ var NUMBER_REGEX = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/;
49
+ var STRING_DOUBLE_REGEX = /^"(?:[^"\\]|\\["bnrtf\\/]|\\u[0-9a-fA-F]{4})*"/;
50
+ var STRING_SINGLE_REGEX = /^'((?:[^'\\]|\\['bnrtf\\/]|\\u[0-9a-fA-F]{4})*)'/;
51
+ var COMMENT_SINGLE_REGEX = /^\/\/.*?(?:\r\n|\r|\n)/;
52
+ var COMMENT_MULTI_REGEX = /^\/\*[\s\S]*?\*\//;
53
+ var IDENTIFIER_REGEX = /^[$a-zA-Z0-9_\-+.*?!|&%^/#\\]+/;
54
+ function some(array, f) {
55
+ let acc = false;
56
+ for (let i = 0; i < array.length; i += 1) {
57
+ const result = f(array[i], i, array);
58
+ acc = result === void 0 ? false : result;
59
+ if (acc) {
60
+ return acc;
61
+ }
62
+ }
63
+ return acc;
64
+ }
65
+ function makeLexer(tokenSpecs) {
66
+ return (contents) => {
67
+ const tokens = [];
68
+ let line = 1;
69
+ let remainingContents = contents;
70
+ function findToken() {
71
+ const result = some(tokenSpecs, (tokenSpec) => {
72
+ const m = tokenSpec.re.exec(remainingContents);
73
+ if (m) {
74
+ const raw = m[0];
75
+ remainingContents = remainingContents.slice(raw.length);
76
+ return {
77
+ raw,
78
+ matched: tokenSpec.f(m)
79
+ // Process the match using the spec's function
80
+ };
81
+ }
82
+ return;
83
+ });
84
+ return result === false ? void 0 : result;
85
+ }
86
+ while (remainingContents !== "") {
87
+ const matched = findToken();
88
+ if (!matched) {
89
+ const err = new SyntaxError(
90
+ `Unexpected character: ${remainingContents[0]}; input: ${remainingContents.substr(
91
+ 0,
92
+ 100
93
+ )}`
94
+ );
95
+ err.line = line;
96
+ throw err;
97
+ }
98
+ const tokenWithLine = matched.matched;
99
+ tokenWithLine.line = line;
100
+ line += matched.raw.replace(/[^\n]/g, "").length;
101
+ tokens.push(tokenWithLine);
102
+ }
103
+ return tokens;
104
+ };
105
+ }
106
+ function fStringSingle(m) {
107
+ const content = m[1].replace(
108
+ /([^'\\]|\\['bnrtf\\]|\\u[0-9a-fA-F]{4})/g,
109
+ (mm) => {
110
+ if (mm === '"') {
111
+ return '\\"';
112
+ }
113
+ if (mm === "\\'") {
114
+ return "'";
115
+ }
116
+ return mm;
117
+ }
118
+ );
119
+ const match = `"${content}"`;
120
+ return {
121
+ type: "string",
122
+ match,
123
+ // The transformed, double-quoted string representation
124
+ // Use JSON.parse on the transformed string to handle escape sequences correctly
125
+ value: JSON.parse(match)
126
+ };
127
+ }
128
+ function fStringDouble(m) {
129
+ return {
130
+ type: "string",
131
+ match: m[0],
132
+ // The raw matched string (including quotes)
133
+ value: JSON.parse(m[0])
134
+ // Use JSON.parse to handle escapes and get the value
135
+ };
136
+ }
137
+ function fIdentifier(m) {
138
+ const value = m[0];
139
+ const match = '"' + value.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + // Escape backslashes and quotes
140
+ '"';
141
+ return {
142
+ type: "string",
143
+ // Treat identifiers as strings
144
+ value,
145
+ // The original identifier name
146
+ match
147
+ // The double-quoted string representation
148
+ };
149
+ }
150
+ function fComment(m) {
151
+ const match = m[0].replace(
152
+ /./g,
153
+ (c) => WHITESPACE_TEST_REGEX.test(c) ? c : " "
154
+ );
155
+ return {
156
+ type: " ",
157
+ // Represent comments as whitespace tokens
158
+ match,
159
+ // String containing original newlines and spaces for other chars
160
+ value: void 0
161
+ // Comments don't have a semantic value
162
+ };
163
+ }
164
+ function fNumber(m) {
165
+ return {
166
+ type: "number",
167
+ match: m[0],
168
+ // The raw matched number string
169
+ value: Number.parseFloat(m[0])
170
+ // Convert string to number
171
+ };
172
+ }
173
+ function fKeyword(m) {
174
+ let value;
175
+ switch (m[0]) {
176
+ case "null":
177
+ value = null;
178
+ break;
179
+ case "true":
180
+ value = true;
181
+ break;
182
+ case "false":
183
+ value = false;
184
+ break;
185
+ default:
186
+ throw new Error(`Unexpected keyword: ${m[0]}`);
187
+ }
188
+ return {
189
+ type: "atom",
190
+ // Use 'atom' type for these literals
191
+ match: m[0],
192
+ // The raw matched keyword
193
+ value
194
+ // The corresponding JavaScript value
195
+ };
196
+ }
197
+ function makeTokenSpecs(relaxed) {
198
+ function f(type) {
199
+ return (m) => {
200
+ return { type, match: m[0], value: void 0 };
201
+ };
202
+ }
203
+ let tokenSpecs = [
204
+ { re: WHITESPACE_REGEX, f: f(" ") },
205
+ // Whitespace
206
+ { re: OBJECT_START_REGEX, f: f("{") },
207
+ // Object start
208
+ { re: OBJECT_END_REGEX, f: f("}") },
209
+ // Object end
210
+ { re: ARRAY_START_REGEX, f: f("[") },
211
+ // Array start
212
+ { re: ARRAY_END_REGEX, f: f("]") },
213
+ // Array end
214
+ { re: COMMA_REGEX, f: f(",") },
215
+ // Comma separator
216
+ { re: COLON_REGEX, f: f(":") },
217
+ // Key-value separator
218
+ { re: KEYWORD_REGEX, f: fKeyword },
219
+ // Keywords
220
+ // Number: optional sign, digits, optional decimal part, optional exponent
221
+ { re: NUMBER_REGEX, f: fNumber },
222
+ // String: double-quoted, handles escapes
223
+ { re: STRING_DOUBLE_REGEX, f: fStringDouble }
224
+ ];
225
+ if (relaxed) {
226
+ tokenSpecs = tokenSpecs.concat([
227
+ // Single-quoted strings
228
+ {
229
+ re: STRING_SINGLE_REGEX,
230
+ f: fStringSingle
231
+ },
232
+ // Single-line comments (// ...)
233
+ { re: COMMENT_SINGLE_REGEX, f: fComment },
234
+ // Multi-line comments (/* ... */)
235
+ { re: COMMENT_MULTI_REGEX, f: fComment },
236
+ // Unquoted identifiers (treated as strings)
237
+ // Allows letters, numbers, _, -, +, ., *, ?, !, |, &, %, ^, /, #, \
238
+ { re: IDENTIFIER_REGEX, f: fIdentifier }
239
+ // Note: The order matters here. Identifiers are checked after keywords/numbers.
240
+ ]);
241
+ }
242
+ return tokenSpecs;
243
+ }
244
+ var lexer = makeLexer(makeTokenSpecs(true));
245
+ var strictLexer = makeLexer(makeTokenSpecs(false));
246
+ function previousNWSToken(tokens, index) {
247
+ let currentIndex = index;
248
+ for (; currentIndex >= 0; currentIndex -= 1) {
249
+ if (tokens[currentIndex].type !== " ") {
250
+ return currentIndex;
251
+ }
252
+ }
253
+ return;
254
+ }
255
+ function stripTrailingComma(tokens) {
256
+ const res = [];
257
+ tokens.forEach((token, index) => {
258
+ if (index > 0 && (token.type === "]" || token.type === "}")) {
259
+ const prevNWSTokenIndex = previousNWSToken(res, res.length - 1);
260
+ if (prevNWSTokenIndex !== void 0 && res[prevNWSTokenIndex].type === ",") {
261
+ const preCommaIndex = previousNWSToken(res, prevNWSTokenIndex - 1);
262
+ if (preCommaIndex !== void 0 && res[preCommaIndex].type !== "[" && res[preCommaIndex].type !== "{") {
263
+ res[prevNWSTokenIndex] = {
264
+ type: " ",
265
+ match: " ",
266
+ // Represent as a single space
267
+ value: void 0,
268
+ // Whitespace has no value
269
+ line: res[prevNWSTokenIndex].line
270
+ // Preserve original line number
271
+ };
272
+ }
273
+ }
274
+ }
275
+ res.push(token);
276
+ });
277
+ return res;
278
+ }
279
+ function popToken(tokens, state) {
280
+ var _a, _b;
281
+ const token = tokens[state.pos];
282
+ state.pos += 1;
283
+ if (!token) {
284
+ const lastLine = tokens.length !== 0 ? (_b = (_a = tokens.at(-1)) == null ? void 0 : _a.line) != null ? _b : 1 : 1;
285
+ return { type: "eof", match: "", value: void 0, line: lastLine };
286
+ }
287
+ return token;
288
+ }
289
+ function strToken(token) {
290
+ switch (token.type) {
291
+ case "atom":
292
+ case "string":
293
+ case "number":
294
+ return `${token.type} ${token.match}`;
295
+ case "eof":
296
+ return "end-of-file";
297
+ default:
298
+ return `'${token.type}'`;
299
+ }
300
+ }
301
+ function skipColon(tokens, state) {
302
+ const colon = popToken(tokens, state);
303
+ if (colon.type !== ":") {
304
+ const message = `Unexpected token: ${strToken(colon)}, expected ':'`;
305
+ if (state.tolerant) {
306
+ state.warnings.push({
307
+ message,
308
+ line: colon.line
309
+ });
310
+ state.pos -= 1;
311
+ } else {
312
+ const err = new SyntaxError(message);
313
+ err.line = colon.line;
314
+ throw err;
315
+ }
316
+ }
317
+ }
318
+ function skipPunctuation(tokens, state, valid) {
319
+ const punctuation = [",", ":", "]", "}"];
320
+ let token = popToken(tokens, state);
321
+ while (true) {
322
+ if (valid == null ? void 0 : valid.includes(token.type)) {
323
+ return token;
324
+ }
325
+ if (token.type === "eof") {
326
+ return token;
327
+ }
328
+ if (punctuation.includes(token.type)) {
329
+ const message = `Unexpected token: ${strToken(
330
+ token
331
+ )}, expected '[', '{', number, string or atom`;
332
+ if (state.tolerant) {
333
+ state.warnings.push({
334
+ message,
335
+ line: token.line
336
+ });
337
+ token = popToken(tokens, state);
338
+ } else {
339
+ const err = new SyntaxError(message);
340
+ err.line = token.line;
341
+ throw err;
342
+ }
343
+ } else {
344
+ return token;
345
+ }
346
+ }
347
+ }
348
+ function raiseError(state, token, message) {
349
+ if (state.tolerant) {
350
+ state.warnings.push({
351
+ message,
352
+ line: token.line
353
+ });
354
+ } else {
355
+ const err = new SyntaxError(message);
356
+ err.line = token.line;
357
+ throw err;
358
+ }
359
+ }
360
+ function raiseUnexpected(state, token, expected) {
361
+ raiseError(
362
+ state,
363
+ token,
364
+ `Unexpected token: ${strToken(token)}, expected ${expected}`
365
+ );
366
+ }
367
+ function checkDuplicates(state, obj, token) {
368
+ const key = String(token.value);
369
+ if (!state.duplicate && Object.hasOwn(obj, key)) {
370
+ raiseError(state, token, `Duplicate key: ${key}`);
371
+ }
372
+ }
373
+ function appendPair(state, obj, key, value) {
374
+ const finalValue = state.reviver ? state.reviver(key, value) : value;
375
+ if (finalValue !== void 0) {
376
+ obj[key] = finalValue;
377
+ }
378
+ }
379
+ function parsePair(tokens, state, obj) {
380
+ let token = skipPunctuation(tokens, state, [":", "string", "number", "atom"]);
381
+ let value;
382
+ if (token.type !== "string") {
383
+ raiseUnexpected(state, token, "string key");
384
+ if (state.tolerant) {
385
+ switch (token.type) {
386
+ case ":":
387
+ token = {
388
+ type: "string",
389
+ value: "null",
390
+ match: '"null"',
391
+ line: token.line
392
+ };
393
+ state.pos -= 1;
394
+ break;
395
+ case "number":
396
+ // Use number as string key
397
+ case "atom":
398
+ token = {
399
+ type: "string",
400
+ value: String(token.value),
401
+ match: `"${token.value}"`,
402
+ line: token.line
403
+ };
404
+ break;
405
+ case "[":
406
+ // Assume missing key before an array
407
+ case "{":
408
+ state.pos -= 1;
409
+ value = parseAny(tokens, state);
410
+ checkDuplicates(state, obj, {
411
+ type: "string",
412
+ value: "null",
413
+ match: '"null"',
414
+ line: token.line
415
+ });
416
+ appendPair(state, obj, "null", value);
417
+ return;
418
+ // Finished parsing this "pair"
419
+ case "eof":
420
+ return;
421
+ // Cannot recover
422
+ default:
423
+ return;
424
+ }
425
+ } else {
426
+ return;
427
+ }
428
+ }
429
+ checkDuplicates(state, obj, token);
430
+ const key = String(token.value);
431
+ skipColon(tokens, state);
432
+ value = parseAny(tokens, state);
433
+ appendPair(state, obj, key, value);
434
+ }
435
+ function parseElement(tokens, state, arr) {
436
+ const key = arr.length;
437
+ const value = parseAny(tokens, state);
438
+ arr[key] = state.reviver ? state.reviver(String(key), value) : value;
439
+ }
440
+ function parseObject(tokens, state) {
441
+ const obj = {};
442
+ return parseMany(tokens, state, obj, {
443
+ skip: [":", "}"],
444
+ // Initially skip over colon or closing brace (for empty/tolerant cases)
445
+ elementParser: parsePair,
446
+ // Use parsePair to parse each key-value element
447
+ elementName: "string key",
448
+ // Expected element type for errors
449
+ endSymbol: "}"
450
+ // The closing token for an object
451
+ });
452
+ }
453
+ function parseArray(tokens, state) {
454
+ const arr = [];
455
+ return parseMany(tokens, state, arr, {
456
+ skip: ["]"],
457
+ // Initially skip over closing bracket (for empty/tolerant cases)
458
+ elementParser: parseElement,
459
+ // Use parseElement to parse each array item
460
+ elementName: "json value",
461
+ // Expected element type for errors
462
+ endSymbol: "]"
463
+ // The closing token for an array
464
+ });
465
+ }
466
+ function handleInvalidToken(token, state, opts, result) {
467
+ raiseUnexpected(state, token, `',' or '${opts.endSymbol}'`);
468
+ if (state.tolerant) {
469
+ if (token.type === "eof") {
470
+ return result;
471
+ }
472
+ state.pos -= 1;
473
+ return null;
474
+ }
475
+ return result;
476
+ }
477
+ function handleCommaToken(params) {
478
+ const { token, tokens, state, opts, result } = params;
479
+ const nextToken = tokens[state.pos];
480
+ if (state.tolerant && nextToken && nextToken.type === opts.endSymbol) {
481
+ raiseError(state, token, `Trailing comma before '${opts.endSymbol}'`);
482
+ popToken(tokens, state);
483
+ return result;
484
+ }
485
+ opts.elementParser(tokens, state, result);
486
+ return null;
487
+ }
488
+ function parseManyInitialElement(tokens, state, result, opts) {
489
+ const token = skipPunctuation(tokens, state, opts.skip);
490
+ if (token.type === "eof") {
491
+ raiseUnexpected(state, token, `'${opts.endSymbol}' or ${opts.elementName}`);
492
+ return result;
493
+ }
494
+ if (token.type === opts.endSymbol) {
495
+ return result;
496
+ }
497
+ state.pos -= 1;
498
+ opts.elementParser(tokens, state, result);
499
+ return;
500
+ }
501
+ function parseManyProcessToken(params) {
502
+ const { token, tokens, state, opts, result } = params;
503
+ if (token.type !== opts.endSymbol && token.type !== ",") {
504
+ const handledResult = handleInvalidToken(token, state, opts, result);
505
+ if (handledResult !== null) {
506
+ return handledResult;
507
+ }
508
+ }
509
+ if (token.type === opts.endSymbol) {
510
+ return result;
511
+ }
512
+ if (token.type === ",") {
513
+ const handledResult = handleCommaToken({
514
+ token,
515
+ tokens,
516
+ state,
517
+ opts,
518
+ result
519
+ });
520
+ if (handledResult !== null) {
521
+ return handledResult;
522
+ }
523
+ return;
524
+ }
525
+ opts.elementParser(tokens, state, result);
526
+ return;
527
+ }
528
+ function parseMany(tokens, state, result, opts) {
529
+ const initialResult = parseManyInitialElement(tokens, state, result, opts);
530
+ if (initialResult !== void 0) {
531
+ return initialResult;
532
+ }
533
+ while (true) {
534
+ const token = popToken(tokens, state);
535
+ const processedResult = parseManyProcessToken({
536
+ token,
537
+ tokens,
538
+ state,
539
+ opts,
540
+ result
541
+ });
542
+ if (processedResult !== void 0) {
543
+ return processedResult;
544
+ }
545
+ }
546
+ }
547
+ function endChecks(tokens, state, ret) {
548
+ if (state.pos < tokens.length) {
549
+ if (state.tolerant) {
550
+ skipPunctuation(tokens, state);
551
+ }
552
+ if (state.pos < tokens.length) {
553
+ raiseError(
554
+ state,
555
+ tokens[state.pos],
556
+ `Unexpected token: ${strToken(tokens[state.pos])}, expected end-of-input`
557
+ );
558
+ }
559
+ }
560
+ if (state.tolerant && state.warnings.length > 0) {
561
+ const message = state.warnings.length === 1 ? state.warnings[0].message : `${state.warnings.length} parse warnings`;
562
+ const err = new SyntaxError(message);
563
+ err.line = state.warnings[0].line;
564
+ err.warnings = state.warnings;
565
+ err.obj = ret;
566
+ throw err;
567
+ }
568
+ }
569
+ function parseAny(tokens, state, end = false) {
570
+ const token = skipPunctuation(tokens, state);
571
+ let ret;
572
+ if (token.type === "eof") {
573
+ if (end) {
574
+ raiseUnexpected(state, token, "json value");
575
+ }
576
+ raiseUnexpected(state, token, "json value");
577
+ return;
578
+ }
579
+ switch (token.type) {
580
+ case "{":
581
+ ret = parseObject(tokens, state);
582
+ break;
583
+ case "[":
584
+ ret = parseArray(tokens, state);
585
+ break;
586
+ case "string":
587
+ // String literal
588
+ case "number":
589
+ // Number literal
590
+ case "atom":
591
+ ret = token.value;
592
+ break;
593
+ default:
594
+ raiseUnexpected(state, token, "json value");
595
+ if (state.tolerant) {
596
+ ret = null;
597
+ } else {
598
+ return;
599
+ }
600
+ }
601
+ if (end) {
602
+ ret = state.reviver ? state.reviver("", ret) : ret;
603
+ endChecks(tokens, state, ret);
604
+ }
605
+ return ret;
606
+ }
607
+ function normalizeParseOptions(optsOrReviver) {
608
+ var _a;
609
+ let options = {};
610
+ if (typeof optsOrReviver === "function") {
611
+ options.reviver = optsOrReviver;
612
+ } else if (optsOrReviver !== null && typeof optsOrReviver === "object") {
613
+ options = { ...optsOrReviver };
614
+ } else if (optsOrReviver !== void 0) {
615
+ throw new TypeError(
616
+ "Second argument must be a reviver function or an options object."
617
+ );
618
+ }
619
+ if (options.relaxed === void 0) {
620
+ if (options.warnings === true || options.tolerant === true) {
621
+ options.relaxed = true;
622
+ } else if (options.warnings === false && options.tolerant === false) {
623
+ options.relaxed = false;
624
+ } else {
625
+ options.relaxed = true;
626
+ }
627
+ }
628
+ options.tolerant = options.tolerant || options.warnings;
629
+ options.duplicate = (_a = options.duplicate) != null ? _a : false;
630
+ return options;
631
+ }
632
+ function createParseState(options) {
633
+ var _a, _b;
634
+ return {
635
+ pos: 0,
636
+ reviver: options.reviver,
637
+ tolerant: (_a = options.tolerant) != null ? _a : false,
638
+ duplicate: (_b = options.duplicate) != null ? _b : false,
639
+ warnings: []
640
+ };
641
+ }
642
+ function parseWithCustomParser(text, options) {
643
+ const lexerToUse = options.relaxed ? lexer : strictLexer;
644
+ let tokens = lexerToUse(text);
645
+ if (options.relaxed) {
646
+ tokens = stripTrailingComma(tokens);
647
+ }
648
+ tokens = tokens.filter((token) => token.type !== " ");
649
+ const state = createParseState(options);
650
+ return parseAny(tokens, state, true);
651
+ }
652
+ function parseWithTransform(text, options) {
653
+ let tokens = lexer(text);
654
+ tokens = stripTrailingComma(tokens);
655
+ const newtext = tokens.reduce((str, token) => str + token.match, "");
656
+ return JSON.parse(
657
+ newtext,
658
+ options.reviver
659
+ );
660
+ }
661
+ function parse(text, optsOrReviver) {
662
+ const options = normalizeParseOptions(optsOrReviver);
663
+ if (!(options.relaxed || options.warnings || options.tolerant) && options.duplicate) {
664
+ return JSON.parse(
665
+ text,
666
+ options.reviver
667
+ );
668
+ }
669
+ if (options.warnings || options.tolerant || !options.duplicate) {
670
+ return parseWithCustomParser(text, options);
671
+ }
672
+ return parseWithTransform(text, options);
673
+ }
76
674
 
77
675
  // src/core/utils/debug.ts
78
676
  var LINE_SPLIT_REGEX = /\r?\n/;
@@ -261,7 +859,7 @@ function escapeRegExp(literal) {
261
859
  function processToolCallJson(toolCallJson, fullMatch, processedElements, options) {
262
860
  var _a, _b;
263
861
  try {
264
- const parsedToolCall = (0, import_rjson.parse)(toolCallJson);
862
+ const parsedToolCall = parse(toolCallJson);
265
863
  processedElements.push({
266
864
  type: "tool-call",
267
865
  toolCallId: generateId(),
@@ -357,234 +955,2818 @@ function emitIncompleteToolCall(state, controller, toolCallStart) {
357
955
  });
358
956
  state.currentToolCallJson = "";
359
957
  }
360
- function handleFinishChunk(state, controller, toolCallStart, chunk) {
361
- if (state.buffer.length > 0) {
362
- flushBuffer(state, controller, toolCallStart);
958
+ function handleFinishChunk(state, controller, toolCallStart, chunk) {
959
+ if (state.buffer.length > 0) {
960
+ flushBuffer(state, controller, toolCallStart);
961
+ }
962
+ closeTextBlock(state, controller);
963
+ emitIncompleteToolCall(state, controller, toolCallStart);
964
+ controller.enqueue(chunk);
965
+ }
966
+ function publishText(text, state, controller) {
967
+ if (state.isInsideToolCall) {
968
+ closeTextBlock(state, controller);
969
+ state.currentToolCallJson += text;
970
+ } else if (text.length > 0) {
971
+ if (!state.currentTextId) {
972
+ state.currentTextId = generateId();
973
+ controller.enqueue({
974
+ type: "text-start",
975
+ id: state.currentTextId
976
+ });
977
+ state.hasEmittedTextStart = true;
978
+ }
979
+ controller.enqueue({
980
+ type: "text-delta",
981
+ id: state.currentTextId,
982
+ delta: text
983
+ });
984
+ }
985
+ }
986
+ function emitToolCall(context) {
987
+ var _a, _b;
988
+ const { state, controller, toolCallStart, toolCallEnd, options } = context;
989
+ try {
990
+ const parsedToolCall = parse(state.currentToolCallJson);
991
+ closeTextBlock(state, controller);
992
+ controller.enqueue({
993
+ type: "tool-call",
994
+ toolCallId: generateId(),
995
+ toolName: parsedToolCall.name,
996
+ input: JSON.stringify((_a = parsedToolCall.arguments) != null ? _a : {})
997
+ });
998
+ } catch (error) {
999
+ logParseFailure({
1000
+ phase: "stream",
1001
+ reason: "Failed to parse streaming tool call JSON segment",
1002
+ snippet: `${toolCallStart}${state.currentToolCallJson}${toolCallEnd}`,
1003
+ error
1004
+ });
1005
+ const errorId = generateId();
1006
+ const errorContent = `${toolCallStart}${state.currentToolCallJson}${toolCallEnd}`;
1007
+ controller.enqueue({
1008
+ type: "text-start",
1009
+ id: errorId
1010
+ });
1011
+ controller.enqueue({
1012
+ type: "text-delta",
1013
+ id: errorId,
1014
+ delta: errorContent
1015
+ });
1016
+ controller.enqueue({
1017
+ type: "text-end",
1018
+ id: errorId
1019
+ });
1020
+ (_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
1021
+ options,
1022
+ "Could not process streaming JSON tool call; emitting original text.",
1023
+ {
1024
+ toolCall: errorContent
1025
+ }
1026
+ );
1027
+ }
1028
+ }
1029
+ function processTagMatch(context) {
1030
+ const { state } = context;
1031
+ if (state.isInsideToolCall) {
1032
+ emitToolCall(context);
1033
+ state.currentToolCallJson = "";
1034
+ state.isInsideToolCall = false;
1035
+ } else {
1036
+ state.currentToolCallJson = "";
1037
+ state.isInsideToolCall = true;
1038
+ }
1039
+ }
1040
+ function processBufferTags(context) {
1041
+ const { state, controller, toolCallStart, toolCallEnd } = context;
1042
+ let startIndex = getPotentialStartIndex(
1043
+ state.buffer,
1044
+ state.isInsideToolCall ? toolCallEnd : toolCallStart
1045
+ );
1046
+ while (startIndex != null) {
1047
+ const tag = state.isInsideToolCall ? toolCallEnd : toolCallStart;
1048
+ if (startIndex + tag.length > state.buffer.length) {
1049
+ break;
1050
+ }
1051
+ publishText(state.buffer.slice(0, startIndex), state, controller);
1052
+ state.buffer = state.buffer.slice(startIndex + tag.length);
1053
+ processTagMatch(context);
1054
+ startIndex = getPotentialStartIndex(
1055
+ state.buffer,
1056
+ state.isInsideToolCall ? toolCallEnd : toolCallStart
1057
+ );
1058
+ }
1059
+ }
1060
+ function handlePartialTag(state, controller, toolCallStart) {
1061
+ if (state.isInsideToolCall) {
1062
+ return;
1063
+ }
1064
+ const potentialIndex = getPotentialStartIndex(state.buffer, toolCallStart);
1065
+ if (potentialIndex != null && potentialIndex + toolCallStart.length > state.buffer.length) {
1066
+ publishText(state.buffer.slice(0, potentialIndex), state, controller);
1067
+ state.buffer = state.buffer.slice(potentialIndex);
1068
+ } else {
1069
+ publishText(state.buffer, state, controller);
1070
+ state.buffer = "";
1071
+ }
1072
+ }
1073
+ var jsonProtocol = ({
1074
+ toolCallStart = "<tool_call>",
1075
+ toolCallEnd = "</tool_call>"
1076
+ } = {}) => ({
1077
+ formatTools({
1078
+ tools,
1079
+ toolSystemPromptTemplate
1080
+ }) {
1081
+ return toolSystemPromptTemplate(tools || []);
1082
+ },
1083
+ formatToolCall(toolCall) {
1084
+ let args = {};
1085
+ if (toolCall.input != null) {
1086
+ try {
1087
+ args = JSON.parse(toolCall.input);
1088
+ } catch (e) {
1089
+ args = toolCall.input;
1090
+ }
1091
+ }
1092
+ return `${toolCallStart}${JSON.stringify({
1093
+ name: toolCall.toolName,
1094
+ arguments: args
1095
+ })}${toolCallEnd}`;
1096
+ },
1097
+ parseGeneratedText({
1098
+ text,
1099
+ options
1100
+ }) {
1101
+ const startEsc = escapeRegExp(toolCallStart);
1102
+ const endEsc = escapeRegExp(toolCallEnd);
1103
+ const toolCallRegex = new RegExp(
1104
+ `${startEsc}([\0-\uFFFF]*?)${endEsc}`,
1105
+ "gs"
1106
+ );
1107
+ const processedElements = [];
1108
+ let currentIndex = 0;
1109
+ let match = toolCallRegex.exec(text);
1110
+ while (match !== null) {
1111
+ currentIndex = processMatchedToolCall({
1112
+ match,
1113
+ text,
1114
+ currentIndex,
1115
+ processedElements,
1116
+ options
1117
+ });
1118
+ match = toolCallRegex.exec(text);
1119
+ }
1120
+ if (currentIndex < text.length) {
1121
+ const remainingText = text.substring(currentIndex);
1122
+ addTextSegment(remainingText, processedElements);
1123
+ }
1124
+ return processedElements;
1125
+ },
1126
+ createStreamParser({
1127
+ options
1128
+ }) {
1129
+ const state = {
1130
+ isInsideToolCall: false,
1131
+ buffer: "",
1132
+ currentToolCallJson: "",
1133
+ currentTextId: null,
1134
+ hasEmittedTextStart: false
1135
+ };
1136
+ return new TransformStream({
1137
+ transform(chunk, controller) {
1138
+ var _a;
1139
+ if (chunk.type === "finish") {
1140
+ handleFinishChunk(state, controller, toolCallStart, chunk);
1141
+ return;
1142
+ }
1143
+ if (chunk.type !== "text-delta") {
1144
+ controller.enqueue(chunk);
1145
+ return;
1146
+ }
1147
+ const textContent = (_a = chunk.delta) != null ? _a : "";
1148
+ state.buffer += textContent;
1149
+ processBufferTags({
1150
+ state,
1151
+ controller,
1152
+ toolCallStart,
1153
+ toolCallEnd,
1154
+ options
1155
+ });
1156
+ handlePartialTag(state, controller, toolCallStart);
1157
+ }
1158
+ });
1159
+ },
1160
+ extractToolCallSegments({ text }) {
1161
+ const startEsc = escapeRegExp(toolCallStart);
1162
+ const endEsc = escapeRegExp(toolCallEnd);
1163
+ const regex = new RegExp(`${startEsc}([\0-\uFFFF]*?)${endEsc}`, "gs");
1164
+ const segments = [];
1165
+ let m = regex.exec(text);
1166
+ while (m != null) {
1167
+ segments.push(m[0]);
1168
+ m = regex.exec(text);
1169
+ }
1170
+ return segments;
1171
+ }
1172
+ });
1173
+
1174
+ // src/core/protocols/protocol-interface.ts
1175
+ function isTCMProtocolFactory(protocol) {
1176
+ return typeof protocol === "function";
1177
+ }
1178
+
1179
+ // src/rxml/errors/types.ts
1180
+ var RXMLParseError = class extends Error {
1181
+ constructor(message, cause, line, column) {
1182
+ super(message);
1183
+ this.name = "RXMLParseError";
1184
+ this.cause = cause;
1185
+ this.line = line;
1186
+ this.column = column;
1187
+ }
1188
+ };
1189
+ var RXMLDuplicateStringTagError = class extends Error {
1190
+ constructor(message) {
1191
+ super(message);
1192
+ this.name = "RXMLDuplicateStringTagError";
1193
+ }
1194
+ };
1195
+ var RXMLCoercionError = class extends Error {
1196
+ constructor(message, cause) {
1197
+ super(message);
1198
+ this.name = "RXMLCoercionError";
1199
+ this.cause = cause;
1200
+ }
1201
+ };
1202
+ var RXMLStringifyError = class extends Error {
1203
+ constructor(message, cause) {
1204
+ super(message);
1205
+ this.name = "RXMLStringifyError";
1206
+ this.cause = cause;
1207
+ }
1208
+ };
1209
+
1210
+ // src/rxml/core/types.ts
1211
+ var CharCodes = {
1212
+ OPEN_BRACKET: "<".charCodeAt(0),
1213
+ CLOSE_BRACKET: ">".charCodeAt(0),
1214
+ MINUS: "-".charCodeAt(0),
1215
+ SLASH: "/".charCodeAt(0),
1216
+ EXCLAMATION: "!".charCodeAt(0),
1217
+ QUESTION: "?".charCodeAt(0),
1218
+ SINGLE_QUOTE: "'".charCodeAt(0),
1219
+ DOUBLE_QUOTE: '"'.charCodeAt(0),
1220
+ OPEN_CORNER_BRACKET: "[".charCodeAt(0),
1221
+ CLOSE_CORNER_BRACKET: "]".charCodeAt(0),
1222
+ SPACE: " ".charCodeAt(0),
1223
+ TAB: " ".charCodeAt(0),
1224
+ NEWLINE: "\n".charCodeAt(0),
1225
+ CARRIAGE_RETURN: "\r".charCodeAt(0)
1226
+ };
1227
+ var DEFAULT_NO_CHILD_NODES = [
1228
+ "img",
1229
+ "br",
1230
+ "input",
1231
+ "meta",
1232
+ "link",
1233
+ "hr",
1234
+ "area",
1235
+ "base",
1236
+ "col",
1237
+ "embed",
1238
+ "param",
1239
+ "source",
1240
+ "track",
1241
+ "wbr"
1242
+ ];
1243
+ var NAME_SPACER = "\r\n >/= ";
1244
+
1245
+ // src/rxml/utils/helpers.ts
1246
+ var NAME_START_CHAR_REGEX = /[A-Za-z_:]/;
1247
+ var NAME_CHAR_REGEX = /[A-Za-z0-9_.:-]/;
1248
+ function isNameStartChar(ch) {
1249
+ return NAME_START_CHAR_REGEX.test(ch);
1250
+ }
1251
+ function isNameChar(ch) {
1252
+ return NAME_CHAR_REGEX.test(ch);
1253
+ }
1254
+ function skipQuoted(s, i) {
1255
+ const quote = s[i];
1256
+ let pos = i + 1;
1257
+ while (pos < s.length) {
1258
+ const ch = s[pos];
1259
+ if (ch === "\\") {
1260
+ pos += 2;
1261
+ continue;
1262
+ }
1263
+ if (ch === quote) {
1264
+ return pos + 1;
1265
+ }
1266
+ pos += 1;
1267
+ }
1268
+ return pos;
1269
+ }
1270
+ function parseName(s, pos) {
1271
+ const start = pos;
1272
+ let currentPos = pos;
1273
+ while (NAME_SPACER.indexOf(s[currentPos]) === -1 && s[currentPos]) {
1274
+ currentPos += 1;
1275
+ }
1276
+ return { name: s.slice(start, currentPos), newPos: currentPos };
1277
+ }
1278
+ function parseString(s, pos) {
1279
+ const startChar = s[pos];
1280
+ const startPos = pos + 1;
1281
+ const endPos = s.indexOf(startChar, startPos);
1282
+ if (endPos === -1) {
1283
+ const tagEnd = s.indexOf(">", startPos);
1284
+ if (tagEnd !== -1) {
1285
+ return { value: s.slice(startPos, tagEnd), newPos: tagEnd };
1286
+ }
1287
+ return { value: s.slice(startPos), newPos: s.length };
1288
+ }
1289
+ return { value: s.slice(startPos, endPos), newPos: endPos + 1 };
1290
+ }
1291
+ function getLineColumn(s, pos) {
1292
+ let line = 1;
1293
+ let column = 1;
1294
+ for (let i = 0; i < pos && i < s.length; i += 1) {
1295
+ if (s[i] === "\n") {
1296
+ line += 1;
1297
+ column = 1;
1298
+ } else {
1299
+ column += 1;
1300
+ }
1301
+ }
1302
+ return { line, column };
1303
+ }
1304
+ function escapeXml(text) {
1305
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
1306
+ }
1307
+ function escapeXmlMinimalText(text) {
1308
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/]]>/g, "]]&gt;");
1309
+ }
1310
+ function escapeXmlMinimalAttr(value, wrapper = '"') {
1311
+ let escaped = value.replace(/&/g, "&amp;").replace(/</g, "&lt;");
1312
+ if (wrapper === '"') {
1313
+ escaped = escaped.replace(/"/g, "&quot;");
1314
+ } else {
1315
+ escaped = escaped.replace(/'/g, "&apos;");
1316
+ }
1317
+ return escaped;
1318
+ }
1319
+ function unescapeXml(text) {
1320
+ return text.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&amp;/g, "&");
1321
+ }
1322
+
1323
+ // src/rxml/builders/stringify.ts
1324
+ function stringify(rootTag, obj, options = {}) {
1325
+ var _a, _b, _c, _d, _e;
1326
+ try {
1327
+ const format = (_a = options.format) != null ? _a : true;
1328
+ const declaration = (_b = options.declaration) != null ? _b : false;
1329
+ const minimalEscaping = (_c = options.minimalEscaping) != null ? _c : false;
1330
+ const suppressEmptyNode = (_d = options.suppressEmptyNode) != null ? _d : false;
1331
+ const strictBooleanAttributes = (_e = options.strictBooleanAttributes) != null ? _e : false;
1332
+ let result = "";
1333
+ if (declaration) {
1334
+ result += '<?xml version="1.0" encoding="UTF-8"?>\n';
1335
+ }
1336
+ result += stringifyValue(rootTag, obj, {
1337
+ depth: 0,
1338
+ format,
1339
+ suppressEmptyNode,
1340
+ minimalEscaping,
1341
+ strictBooleanAttributes
1342
+ });
1343
+ if (result.endsWith("\n")) {
1344
+ return result.slice(0, -1);
1345
+ }
1346
+ return result;
1347
+ } catch (error) {
1348
+ throw new RXMLStringifyError("Failed to stringify XML", error);
1349
+ }
1350
+ }
1351
+ function escapeContent(content, minimalEscaping) {
1352
+ return minimalEscaping ? escapeXmlMinimalText(content) : escapeXml(content);
1353
+ }
1354
+ function createSelfClosingTag(tagName, indent, newline) {
1355
+ return `${indent}<${tagName}/>${newline}`;
1356
+ }
1357
+ function createTextElement(tagName, content, indent, newline) {
1358
+ return `${indent}<${tagName}>${content}</${tagName}>${newline}`;
1359
+ }
1360
+ function isPrimitive(value) {
1361
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
1362
+ }
1363
+ function stringifyPrimitive(tagName, value, context, format) {
1364
+ const { minimalEscaping, suppressEmptyNode } = context;
1365
+ const content = escapeContent(String(value), minimalEscaping);
1366
+ if (content === "" && suppressEmptyNode) {
1367
+ return "";
1368
+ }
1369
+ return createTextElement(tagName, content, format.indent, format.newline);
1370
+ }
1371
+ function stringifyArray(tagName, value, context) {
1372
+ let result = "";
1373
+ for (const item of value) {
1374
+ result += stringifyValue(tagName, item, context);
1375
+ }
1376
+ return result;
1377
+ }
1378
+ function stringifyValue(tagName, value, context) {
1379
+ const { format, suppressEmptyNode, minimalEscaping } = context;
1380
+ const indent = format ? " ".repeat(context.depth) : "";
1381
+ const newline = format ? "\n" : "";
1382
+ if (value === null || value === void 0) {
1383
+ if (suppressEmptyNode) {
1384
+ return "";
1385
+ }
1386
+ return createSelfClosingTag(tagName, indent, newline);
1387
+ }
1388
+ if (isPrimitive(value)) {
1389
+ return stringifyPrimitive(tagName, value, context, { indent, newline });
1390
+ }
1391
+ if (Array.isArray(value)) {
1392
+ return stringifyArray(tagName, value, context);
1393
+ }
1394
+ if (typeof value === "object") {
1395
+ return stringifyObject(tagName, value, context);
1396
+ }
1397
+ const content = escapeContent(String(value), minimalEscaping);
1398
+ if (content === "" && suppressEmptyNode) {
1399
+ return "";
1400
+ }
1401
+ return createTextElement(tagName, content, indent, newline);
1402
+ }
1403
+ function extractObjectParts(obj) {
1404
+ const attributes = {};
1405
+ const elements = {};
1406
+ let textContent;
1407
+ for (const [key, value] of Object.entries(obj)) {
1408
+ if (key.startsWith("@")) {
1409
+ attributes[key.substring(1)] = value;
1410
+ } else if (key === "#text" || key === "_text") {
1411
+ textContent = String(value);
1412
+ } else if (key === "_attributes") {
1413
+ if (typeof value === "object" && value !== null) {
1414
+ Object.assign(attributes, value);
1415
+ }
1416
+ } else {
1417
+ elements[key] = value;
1418
+ }
1419
+ }
1420
+ return { attributes, elements, textContent };
1421
+ }
1422
+ function formatAttribute(attrName, attrValue, minimalEscaping, strictBooleanAttributes) {
1423
+ if (attrValue === null) {
1424
+ return strictBooleanAttributes ? ` ${attrName}="${attrName}"` : ` ${attrName}`;
1425
+ }
1426
+ const valueStr = String(attrValue);
1427
+ if (valueStr.indexOf('"') === -1) {
1428
+ const escaped2 = minimalEscaping ? escapeXmlMinimalAttr(valueStr, '"') : escapeXml(valueStr);
1429
+ return ` ${attrName}="${escaped2}"`;
1430
+ }
1431
+ const escaped = minimalEscaping ? escapeXmlMinimalAttr(valueStr, "'") : escapeXml(valueStr);
1432
+ return ` ${attrName}='${escaped}'`;
1433
+ }
1434
+ function buildOpeningTag(tagName, attributes, context) {
1435
+ let openTag = `<${tagName}`;
1436
+ const { minimalEscaping, strictBooleanAttributes } = context;
1437
+ for (const [attrName, attrValue] of Object.entries(attributes)) {
1438
+ openTag += formatAttribute(
1439
+ attrName,
1440
+ attrValue,
1441
+ minimalEscaping,
1442
+ strictBooleanAttributes
1443
+ );
1444
+ }
1445
+ return openTag;
1446
+ }
1447
+ function stringifyTextOnlyContent(options) {
1448
+ const { tagName, textContent, openTag, format, minimalEscaping } = options;
1449
+ const content = escapeContent(textContent, minimalEscaping);
1450
+ return `${format.indent}${openTag}${content}</${tagName}>${format.newline}`;
1451
+ }
1452
+ function stringifyComplexContent(tagName, parts, context, options) {
1453
+ const { format, minimalEscaping, depth } = context;
1454
+ const { textContent, elements } = parts;
1455
+ const hasElements = Object.keys(elements).length > 0;
1456
+ let result = `${options.indent}${options.openTag}`;
1457
+ if (textContent) {
1458
+ const content = escapeContent(textContent, minimalEscaping);
1459
+ result += format ? `${options.newline}${options.childIndent}${content}` : content;
1460
+ }
1461
+ if (hasElements) {
1462
+ if (format) {
1463
+ result += options.newline;
1464
+ }
1465
+ for (const [elementName, elementValue] of Object.entries(elements)) {
1466
+ result += stringifyValue(elementName, elementValue, {
1467
+ ...context,
1468
+ depth: depth + 1
1469
+ });
1470
+ }
1471
+ if (format) {
1472
+ result += options.indent;
1473
+ }
1474
+ }
1475
+ result += `</${tagName}>${options.newline}`;
1476
+ return result;
1477
+ }
1478
+ function stringifyObject(tagName, obj, context) {
1479
+ const { depth, format, suppressEmptyNode } = context;
1480
+ const indent = format ? " ".repeat(depth) : "";
1481
+ const newline = format ? "\n" : "";
1482
+ const childIndent = format ? " ".repeat(depth + 1) : "";
1483
+ const parts = extractObjectParts(obj);
1484
+ const openTag = buildOpeningTag(tagName, parts.attributes, context);
1485
+ const hasElements = Object.keys(parts.elements).length > 0;
1486
+ const hasTextContent = parts.textContent !== void 0 && parts.textContent !== "";
1487
+ if (!(hasElements || hasTextContent)) {
1488
+ if (suppressEmptyNode) {
1489
+ return "";
1490
+ }
1491
+ return `${indent}${openTag}/>${newline}`;
1492
+ }
1493
+ const fullOpenTag = `${openTag}>`;
1494
+ if (!hasElements && hasTextContent && parts.textContent) {
1495
+ return stringifyTextOnlyContent({
1496
+ tagName,
1497
+ textContent: parts.textContent,
1498
+ openTag: fullOpenTag,
1499
+ format: { indent, newline },
1500
+ minimalEscaping: context.minimalEscaping
1501
+ });
1502
+ }
1503
+ return stringifyComplexContent(tagName, parts, context, {
1504
+ indent,
1505
+ newline,
1506
+ childIndent,
1507
+ openTag: fullOpenTag
1508
+ });
1509
+ }
1510
+
1511
+ // src/schema-coerce/index.ts
1512
+ var NUMERIC_REGEX = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
1513
+ var EMPTY_OBJECT_REGEX = /^\{\s*\}$/s;
1514
+ var NEWLINE_SPLIT_REGEX = /\n+/;
1515
+ var COMMA_SPLIT_REGEX = /,\s*/;
1516
+ var DIGIT_KEY_REGEX = /^\d+$/;
1517
+ function unwrapJsonSchema(schema) {
1518
+ if (!schema || typeof schema !== "object") {
1519
+ return schema;
1520
+ }
1521
+ const s = schema;
1522
+ if (s.jsonSchema && typeof s.jsonSchema === "object") {
1523
+ return unwrapJsonSchema(s.jsonSchema);
1524
+ }
1525
+ return schema;
1526
+ }
1527
+ function getSchemaType(schema) {
1528
+ const unwrapped = unwrapJsonSchema(schema);
1529
+ if (!unwrapped || typeof unwrapped !== "object") {
1530
+ return;
1531
+ }
1532
+ const t = unwrapped.type;
1533
+ if (typeof t === "string") {
1534
+ return t;
1535
+ }
1536
+ if (Array.isArray(t)) {
1537
+ const preferred = [
1538
+ "object",
1539
+ "array",
1540
+ "boolean",
1541
+ "number",
1542
+ "integer",
1543
+ "string"
1544
+ ];
1545
+ for (const p of preferred) {
1546
+ if (t.includes(p)) {
1547
+ return p;
1548
+ }
1549
+ }
1550
+ }
1551
+ const s = unwrapped;
1552
+ if (s && typeof s === "object" && (s.properties || s.additionalProperties)) {
1553
+ return "object";
1554
+ }
1555
+ if (s && typeof s === "object" && (s.items || s.prefixItems)) {
1556
+ return "array";
1557
+ }
1558
+ return;
1559
+ }
1560
+ function schemaAllowsPropertyViaCombinators(s, key, depth) {
1561
+ const anyOfValues = s.anyOf;
1562
+ const oneOfValues = s.oneOf;
1563
+ const allOfValues = s.allOf;
1564
+ let hasCombinator = false;
1565
+ let anyOfAllows = true;
1566
+ let oneOfAllows = true;
1567
+ let allOfAllows = true;
1568
+ if (Array.isArray(anyOfValues)) {
1569
+ hasCombinator = true;
1570
+ anyOfAllows = anyOfValues.some(
1571
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1572
+ );
1573
+ }
1574
+ if (Array.isArray(oneOfValues)) {
1575
+ hasCombinator = true;
1576
+ oneOfAllows = oneOfValues.some(
1577
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1578
+ );
1579
+ }
1580
+ if (Array.isArray(allOfValues)) {
1581
+ hasCombinator = true;
1582
+ allOfAllows = allOfValues.every(
1583
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1584
+ );
1585
+ }
1586
+ if (!hasCombinator) {
1587
+ return false;
1588
+ }
1589
+ return anyOfAllows && oneOfAllows && allOfAllows;
1590
+ }
1591
+ function schemaHasPropertyDirectly(s, key) {
1592
+ const props = s.properties;
1593
+ if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] !== false) {
1594
+ return true;
1595
+ }
1596
+ const required = s.required;
1597
+ if (Array.isArray(required) && required.includes(key)) {
1598
+ return true;
1599
+ }
1600
+ const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
1601
+ return patternSchemas.some((schema) => schema !== false);
1602
+ }
1603
+ function schemaHasPropertyViaAdditional(s) {
1604
+ const additional = s.additionalProperties;
1605
+ if (additional === true || additional && typeof additional === "object" && !Array.isArray(additional)) {
1606
+ return true;
1607
+ }
1608
+ if (Object.hasOwn(s, "additionalProperties")) {
1609
+ return false;
1610
+ }
1611
+ const type = s.type;
1612
+ const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
1613
+ 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;
1614
+ return !!(isObjectType || hasObjectKeywords);
1615
+ }
1616
+ function schemaDisallowsPropertyDirectly(s, key) {
1617
+ const props = s.properties;
1618
+ if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] === false) {
1619
+ return true;
1620
+ }
1621
+ const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
1622
+ return patternSchemas.some((schema) => schema === false);
1623
+ }
1624
+ function schemaHasProperty(schema, key, depth = 0) {
1625
+ if (depth > 5) {
1626
+ return true;
1627
+ }
1628
+ const unwrapped = unwrapJsonSchema(schema);
1629
+ if (schemaIsUnconstrained(unwrapped)) {
1630
+ return true;
1631
+ }
1632
+ if (!unwrapped || typeof unwrapped !== "object") {
1633
+ return false;
1634
+ }
1635
+ const s = unwrapped;
1636
+ if (schemaDisallowsPropertyDirectly(s, key)) {
1637
+ return false;
1638
+ }
1639
+ if (schemaHasPropertyDirectly(s, key)) {
1640
+ return true;
1641
+ }
1642
+ if (schemaHasPropertyViaAdditional(s)) {
1643
+ return true;
1644
+ }
1645
+ return schemaAllowsPropertyViaCombinators(s, key, depth);
1646
+ }
1647
+ function schemaIsUnconstrained(schema) {
1648
+ const unwrapped = unwrapJsonSchema(schema);
1649
+ if (unwrapped == null || unwrapped === true) {
1650
+ return true;
1651
+ }
1652
+ if (typeof unwrapped !== "object" || Array.isArray(unwrapped)) {
1653
+ return false;
1654
+ }
1655
+ return Object.keys(unwrapped).length === 0;
1656
+ }
1657
+ function getPatternSchemasForKey(patternProperties, key) {
1658
+ if (!patternProperties || typeof patternProperties !== "object" || Array.isArray(patternProperties)) {
1659
+ return [];
1660
+ }
1661
+ const schemas = [];
1662
+ for (const [pattern, schema] of Object.entries(
1663
+ patternProperties
1664
+ )) {
1665
+ try {
1666
+ const regex = new RegExp(pattern);
1667
+ if (regex.test(key)) {
1668
+ schemas.push(schema);
1669
+ }
1670
+ } catch (e) {
1671
+ }
1672
+ }
1673
+ return schemas;
1674
+ }
1675
+ function coerceValueForKey(value, key, unwrapped) {
1676
+ const schemas = [];
1677
+ const props = unwrapped.properties;
1678
+ if (props && Object.hasOwn(props, key)) {
1679
+ schemas.push(props[key]);
1680
+ }
1681
+ const patternSchemas = getPatternSchemasForKey(
1682
+ unwrapped.patternProperties,
1683
+ key
1684
+ );
1685
+ if (patternSchemas.length > 0) {
1686
+ schemas.push(...patternSchemas);
1687
+ }
1688
+ if (schemas.length > 0) {
1689
+ let out = value;
1690
+ for (const schema of schemas) {
1691
+ if (typeof schema === "boolean") {
1692
+ continue;
1693
+ }
1694
+ out = coerceBySchema(out, schema);
1695
+ }
1696
+ return out;
1697
+ }
1698
+ const additional = unwrapped.additionalProperties;
1699
+ if (additional && typeof additional === "object" && !Array.isArray(additional)) {
1700
+ return coerceBySchema(value, additional);
1701
+ }
1702
+ if (additional === true || additional === false) {
1703
+ return value;
1704
+ }
1705
+ return coerceBySchema(value, void 0);
1706
+ }
1707
+ function coerceStringWithoutSchema(value) {
1708
+ const s = value.trim();
1709
+ const lower = s.toLowerCase();
1710
+ if (lower === "true") {
1711
+ return true;
1712
+ }
1713
+ if (lower === "false") {
1714
+ return false;
1715
+ }
1716
+ if (NUMERIC_REGEX.test(s)) {
1717
+ const num = Number(s);
1718
+ if (Number.isFinite(num)) {
1719
+ return num;
1720
+ }
1721
+ }
1722
+ if (s.startsWith("{") && s.endsWith("}") || s.startsWith("[") && s.endsWith("]")) {
1723
+ try {
1724
+ const parsed = JSON.parse(s);
1725
+ return coerceBySchema(parsed, void 0);
1726
+ } catch (e) {
1727
+ }
1728
+ }
1729
+ return value;
1730
+ }
1731
+ function coerceStringToObject(s, unwrapped) {
1732
+ try {
1733
+ let normalized = s.replace(/'/g, '"');
1734
+ normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
1735
+ const obj = JSON.parse(normalized);
1736
+ if (obj && typeof obj === "object" && !Array.isArray(obj)) {
1737
+ return coerceObjectToObject(obj, unwrapped);
1738
+ }
1739
+ } catch (e) {
1740
+ }
1741
+ return null;
1742
+ }
1743
+ function coerceStringToArray(s, unwrapped) {
1744
+ const prefixItems = Array.isArray(unwrapped.prefixItems) ? unwrapped.prefixItems : void 0;
1745
+ const itemsSchema = unwrapped.items;
1746
+ try {
1747
+ const normalized = s.replace(/'/g, '"');
1748
+ const arr = JSON.parse(normalized);
1749
+ if (Array.isArray(arr)) {
1750
+ if (prefixItems && arr.length === prefixItems.length) {
1751
+ return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
1752
+ }
1753
+ return arr.map((v) => coerceBySchema(v, itemsSchema));
1754
+ }
1755
+ } catch (e) {
1756
+ const csv = s.includes("\n") ? s.split(NEWLINE_SPLIT_REGEX) : s.split(COMMA_SPLIT_REGEX);
1757
+ const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);
1758
+ if (prefixItems && trimmed.length === prefixItems.length) {
1759
+ return trimmed.map((x, i) => coerceBySchema(x, prefixItems[i]));
1760
+ }
1761
+ return trimmed.map((x) => coerceBySchema(x, itemsSchema));
1762
+ }
1763
+ return null;
1764
+ }
1765
+ function coerceObjectToObject(value, unwrapped) {
1766
+ const out = {};
1767
+ for (const [k, v] of Object.entries(value)) {
1768
+ out[k] = coerceValueForKey(v, k, unwrapped);
1769
+ }
1770
+ return out;
1771
+ }
1772
+ function coerceArrayToArray(value, prefixItems, itemsSchema) {
1773
+ if (prefixItems && value.length === prefixItems.length) {
1774
+ return value.map((v, i) => coerceBySchema(v, prefixItems[i]));
1775
+ }
1776
+ return value.map((v) => coerceBySchema(v, itemsSchema));
1777
+ }
1778
+ function coerceObjectToArray(maybe, prefixItems, itemsSchema) {
1779
+ if (Object.hasOwn(maybe, "item")) {
1780
+ const items = maybe.item;
1781
+ const arr = Array.isArray(items) ? items : [items];
1782
+ return coerceArrayToArray(arr, prefixItems, itemsSchema);
1783
+ }
1784
+ const keys = Object.keys(maybe);
1785
+ if (keys.length > 0 && keys.every((k) => DIGIT_KEY_REGEX.test(k))) {
1786
+ const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
1787
+ return coerceArrayToArray(arr, prefixItems, itemsSchema);
1788
+ }
1789
+ if (keys.length === 1) {
1790
+ const singleKey = keys[0];
1791
+ if (!(schemaIsUnconstrained(itemsSchema) || schemaHasProperty(itemsSchema, singleKey))) {
1792
+ const singleValue = maybe[singleKey];
1793
+ if (Array.isArray(singleValue)) {
1794
+ return singleValue.map((v) => coerceBySchema(v, itemsSchema));
1795
+ }
1796
+ if (singleValue && typeof singleValue === "object") {
1797
+ return [coerceBySchema(singleValue, itemsSchema)];
1798
+ }
1799
+ }
1800
+ }
1801
+ return null;
1802
+ }
1803
+ function coercePrimitiveToArray(value, prefixItems, itemsSchema) {
1804
+ if (prefixItems && prefixItems.length > 0) {
1805
+ return [coerceBySchema(value, prefixItems[0])];
1806
+ }
1807
+ return [coerceBySchema(value, itemsSchema)];
1808
+ }
1809
+ function coerceStringToPrimitive(s, schemaType) {
1810
+ if (schemaType === "boolean") {
1811
+ const lower = s.toLowerCase();
1812
+ if (lower === "true") {
1813
+ return true;
1814
+ }
1815
+ if (lower === "false") {
1816
+ return false;
1817
+ }
1818
+ }
1819
+ if ((schemaType === "number" || schemaType === "integer") && NUMERIC_REGEX.test(s)) {
1820
+ const num = Number(s);
1821
+ if (Number.isFinite(num)) {
1822
+ return num;
1823
+ }
1824
+ }
1825
+ return null;
1826
+ }
1827
+ function coerceStringValue(value, schemaType, u) {
1828
+ const s = value.trim();
1829
+ if (schemaType === "object") {
1830
+ const result = coerceStringToObject(s, u);
1831
+ if (result !== null) {
1832
+ return result;
1833
+ }
1834
+ }
1835
+ if (schemaType === "array") {
1836
+ const result = coerceStringToArray(s, u);
1837
+ if (result !== null) {
1838
+ return result;
1839
+ }
1840
+ }
1841
+ const primitiveResult = coerceStringToPrimitive(s, schemaType);
1842
+ if (primitiveResult !== null) {
1843
+ return primitiveResult;
1844
+ }
1845
+ return value;
1846
+ }
1847
+ function coerceArrayValue(value, prefixItems, itemsSchema) {
1848
+ if (Array.isArray(value)) {
1849
+ return coerceArrayToArray(value, prefixItems, itemsSchema);
1850
+ }
1851
+ if (value && typeof value === "object") {
1852
+ const result = coerceObjectToArray(
1853
+ value,
1854
+ prefixItems,
1855
+ itemsSchema
1856
+ );
1857
+ if (result !== null) {
1858
+ return result;
1859
+ }
1860
+ if (getSchemaType(itemsSchema) === "array") {
1861
+ return [value];
1862
+ }
1863
+ return [coerceBySchema(value, itemsSchema)];
1864
+ }
1865
+ if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1866
+ return coercePrimitiveToArray(value, prefixItems, itemsSchema);
1867
+ }
1868
+ return [value];
1869
+ }
1870
+ function coerceBySchema(value, schema) {
1871
+ const unwrapped = unwrapJsonSchema(schema);
1872
+ if (!unwrapped || typeof unwrapped !== "object") {
1873
+ if (typeof value === "string") {
1874
+ return coerceStringWithoutSchema(value);
1875
+ }
1876
+ return value;
1877
+ }
1878
+ const schemaType = getSchemaType(unwrapped);
1879
+ const u = unwrapped;
1880
+ if (typeof value === "string") {
1881
+ return coerceStringValue(value, schemaType, u);
1882
+ }
1883
+ if (schemaType === "object" && value && typeof value === "object" && !Array.isArray(value)) {
1884
+ return coerceObjectToObject(value, u);
1885
+ }
1886
+ if (schemaType === "array") {
1887
+ const prefixItems = Array.isArray(u.prefixItems) ? u.prefixItems : void 0;
1888
+ const itemsSchema = u.items;
1889
+ return coerceArrayValue(value, prefixItems, itemsSchema);
1890
+ }
1891
+ return value;
1892
+ }
1893
+
1894
+ // src/rxml/schema/coercion.ts
1895
+ function getPropertySchema(toolSchema, key) {
1896
+ const unwrapped = unwrapJsonSchema(toolSchema);
1897
+ if (!unwrapped || typeof unwrapped !== "object") {
1898
+ return;
1899
+ }
1900
+ const u = unwrapped;
1901
+ const props = u.properties;
1902
+ if (props && Object.hasOwn(props, key)) {
1903
+ return props[key];
1904
+ }
1905
+ return;
1906
+ }
1907
+ function getNodeValue(children, schema, tagName, textNodeName) {
1908
+ if (children.length === 0) {
1909
+ return "";
1910
+ }
1911
+ if (children.length === 1 && typeof children[0] === "string") {
1912
+ return children[0];
1913
+ }
1914
+ return processComplexContent(
1915
+ children,
1916
+ getPropertySchema(schema, tagName),
1917
+ textNodeName
1918
+ );
1919
+ }
1920
+ function addAttributesToValue(value, attributes, textNodeName) {
1921
+ if (Object.keys(attributes).length === 0) {
1922
+ return value;
1923
+ }
1924
+ if (typeof value === "string") {
1925
+ const valueResult = { [textNodeName]: value };
1926
+ for (const [attrName, attrValue] of Object.entries(attributes)) {
1927
+ valueResult[`@_${attrName}`] = attrValue;
1928
+ }
1929
+ return valueResult;
1930
+ }
1931
+ if (value && typeof value === "object" && !Array.isArray(value)) {
1932
+ for (const [attrName, attrValue] of Object.entries(attributes)) {
1933
+ value[`@_${attrName}`] = attrValue;
1934
+ }
1935
+ }
1936
+ return value;
1937
+ }
1938
+ function addToResult(result, tagName, value) {
1939
+ if (result[tagName]) {
1940
+ if (!Array.isArray(result[tagName])) {
1941
+ result[tagName] = [result[tagName]];
1942
+ }
1943
+ result[tagName].push(value);
1944
+ } else {
1945
+ result[tagName] = value;
1946
+ }
1947
+ }
1948
+ function domToObject(nodes, schema, textNodeName = "#text") {
1949
+ const result = {};
1950
+ for (const node of nodes) {
1951
+ if (typeof node === "string") {
1952
+ continue;
1953
+ }
1954
+ const { tagName, children, attributes } = node;
1955
+ let value = getNodeValue(children, schema, tagName, textNodeName);
1956
+ value = addAttributesToValue(value, attributes, textNodeName);
1957
+ addToResult(result, tagName, value);
1958
+ }
1959
+ return result;
1960
+ }
1961
+ function processChildElement(child, schema, textNodeName) {
1962
+ let childValue;
1963
+ if (child.children.length === 0) {
1964
+ childValue = "";
1965
+ } else if (child.children.length === 1 && typeof child.children[0] === "string") {
1966
+ childValue = child.children[0];
1967
+ } else {
1968
+ childValue = processComplexContent(
1969
+ child.children,
1970
+ getPropertySchema(schema, child.tagName),
1971
+ textNodeName
1972
+ );
1973
+ }
1974
+ return addAttributesToValue(childValue, child.attributes, textNodeName);
1975
+ }
1976
+ function combineContent(textContent, elements, textNodeName) {
1977
+ const hasText = textContent.length > 0;
1978
+ const hasElements = Object.keys(elements).length > 0;
1979
+ if (hasText && hasElements) {
1980
+ return {
1981
+ [textNodeName]: textContent.join("").trim(),
1982
+ ...elements
1983
+ };
1984
+ }
1985
+ if (hasText) {
1986
+ return textContent.join("").trim();
1987
+ }
1988
+ if (hasElements) {
1989
+ return elements;
1990
+ }
1991
+ return "";
1992
+ }
1993
+ function processComplexContent(children, schema, textNodeName) {
1994
+ const textContent = [];
1995
+ const elements = {};
1996
+ for (const child of children) {
1997
+ if (typeof child === "string") {
1998
+ textContent.push(child);
1999
+ } else {
2000
+ const childValue = processChildElement(child, schema, textNodeName);
2001
+ addToResult(elements, child.tagName, childValue);
2002
+ }
2003
+ }
2004
+ return combineContent(textContent, elements, textNodeName);
2005
+ }
2006
+ function coerceDomBySchema(domObject, schema) {
2007
+ try {
2008
+ return coerceBySchema(domObject, schema);
2009
+ } catch (error) {
2010
+ throw new RXMLCoercionError("Failed to coerce DOM object by schema", error);
2011
+ }
2012
+ }
2013
+ function visitObjectProperties(props, collected, visit) {
2014
+ for (const [key, propSchema] of Object.entries(props)) {
2015
+ const t = getSchemaType(propSchema);
2016
+ if (t === "string") {
2017
+ collected.add(key);
2018
+ } else if (t === "object" || t === "array") {
2019
+ visit(propSchema);
2020
+ }
2021
+ }
2022
+ }
2023
+ function visitArrayItems(u, visit) {
2024
+ const items = u.items;
2025
+ if (items) {
2026
+ visit(items);
2027
+ }
2028
+ const prefix = u.prefixItems;
2029
+ if (Array.isArray(prefix)) {
2030
+ for (const item of prefix) {
2031
+ visit(item);
2032
+ }
2033
+ }
2034
+ }
2035
+ function getStringTypedProperties(schema) {
2036
+ const collected = /* @__PURE__ */ new Set();
2037
+ const visit = (s) => {
2038
+ const unwrapped = unwrapJsonSchema(s);
2039
+ if (!unwrapped || typeof unwrapped !== "object") {
2040
+ return;
2041
+ }
2042
+ const u = unwrapped;
2043
+ const type = getSchemaType(unwrapped);
2044
+ if (type === "object") {
2045
+ const props = u.properties;
2046
+ if (props && typeof props === "object") {
2047
+ visitObjectProperties(props, collected, visit);
2048
+ }
2049
+ } else if (type === "array") {
2050
+ visitArrayItems(u, visit);
2051
+ }
2052
+ };
2053
+ visit(schema);
2054
+ return collected;
2055
+ }
2056
+ function processArrayContent(value, schema, textNodeName) {
2057
+ if (!Array.isArray(value)) {
2058
+ return value;
2059
+ }
2060
+ const schemaType = getSchemaType(schema);
2061
+ if (schemaType === "string") {
2062
+ return value.map((item) => {
2063
+ if (typeof item === "string") {
2064
+ return item.trim();
2065
+ }
2066
+ if (item && typeof item === "object" && textNodeName in item) {
2067
+ const textVal = item[textNodeName];
2068
+ return typeof textVal === "string" ? textVal.trim() : String(textVal);
2069
+ }
2070
+ return String(item);
2071
+ });
2072
+ }
2073
+ return value.map((item) => {
2074
+ if (typeof item === "string") {
2075
+ return item.trim();
2076
+ }
2077
+ if (item && typeof item === "object" && textNodeName in item) {
2078
+ const textVal = item[textNodeName];
2079
+ return typeof textVal === "string" ? textVal.trim() : textVal;
2080
+ }
2081
+ return item;
2082
+ });
2083
+ }
2084
+ function processIndexedTuple(obj, textNodeName) {
2085
+ const keys = Object.keys(obj);
2086
+ const indices = keys.map((k) => Number.parseInt(k, 10)).sort((a, b) => a - b);
2087
+ const isValidTuple = indices[0] === 0 && indices.every((val, idx) => val === idx);
2088
+ if (!isValidTuple) {
2089
+ return [obj];
2090
+ }
2091
+ const sortedKeys = keys.sort(
2092
+ (a, b) => Number.parseInt(a, 10) - Number.parseInt(b, 10)
2093
+ );
2094
+ return sortedKeys.map((key) => {
2095
+ const item = obj[key];
2096
+ if (item && typeof item === "object" && textNodeName in item) {
2097
+ const textVal = item[textNodeName];
2098
+ return typeof textVal === "string" ? textVal.trim() : textVal;
2099
+ }
2100
+ return typeof item === "string" ? item.trim() : item;
2101
+ });
2102
+ }
2103
+
2104
+ // src/rxml/schema/extraction.ts
2105
+ function skipDoctype(xmlContent, i, len) {
2106
+ const gt = xmlContent.indexOf(">", i + 1);
2107
+ return gt === -1 ? len : gt + 1;
2108
+ }
2109
+ function skipComment(xmlContent, i, len) {
2110
+ const close = xmlContent.indexOf("-->", i + 4);
2111
+ return close === -1 ? len : close + 3;
2112
+ }
2113
+ function skipCdata(xmlContent, i, len) {
2114
+ const close = xmlContent.indexOf("]]>", i + 9);
2115
+ return close === -1 ? len : close + 3;
2116
+ }
2117
+ function skipProcessingInstruction(xmlContent, i, len) {
2118
+ const close = xmlContent.indexOf("?>", i + 1);
2119
+ return close === -1 ? len : close + 2;
2120
+ }
2121
+ function skipSpecialConstruct(xmlContent, i, len) {
2122
+ const ch = xmlContent[i];
2123
+ if (ch === "!") {
2124
+ if (xmlContent.startsWith("!DOCTYPE", i + 1)) {
2125
+ return skipDoctype(xmlContent, i, len);
2126
+ }
2127
+ if (xmlContent.startsWith("!--", i + 1)) {
2128
+ return skipComment(xmlContent, i, len);
2129
+ }
2130
+ if (xmlContent.startsWith("![CDATA[", i + 1)) {
2131
+ return skipCdata(xmlContent, i, len);
2132
+ }
2133
+ const gt = xmlContent.indexOf(">", i + 1);
2134
+ return gt === -1 ? len : gt + 1;
2135
+ }
2136
+ if (ch === "?") {
2137
+ return skipProcessingInstruction(xmlContent, i, len);
2138
+ }
2139
+ return -1;
2140
+ }
2141
+ function parseTagName(xmlContent, i, len) {
2142
+ let j = i;
2143
+ if (j < len && isNameStartChar(xmlContent[j])) {
2144
+ j += 1;
2145
+ while (j < len && isNameChar(xmlContent[j])) {
2146
+ j += 1;
2147
+ }
2148
+ }
2149
+ return { name: xmlContent.slice(i, j), pos: j };
2150
+ }
2151
+ function skipToTagEnd(xmlContent, start, len) {
2152
+ let k = start;
2153
+ let isSelfClosing = false;
2154
+ while (k < len) {
2155
+ const c = xmlContent[k];
2156
+ if (c === '"' || c === "'") {
2157
+ k = skipQuoted(xmlContent, k);
2158
+ continue;
2159
+ }
2160
+ if (c === ">") {
2161
+ break;
2162
+ }
2163
+ if (c === "/" && xmlContent[k + 1] === ">") {
2164
+ isSelfClosing = true;
2165
+ k += 1;
2166
+ break;
2167
+ }
2168
+ k += 1;
2169
+ }
2170
+ return { pos: k, isSelfClosing };
2171
+ }
2172
+ function processClosingTagMatch(options) {
2173
+ const { xmlContent, nx, len, tagName, depth, nextLt } = options;
2174
+ const tagInfo = parseTagName(xmlContent, nx + 1, len);
2175
+ const gt = xmlContent.indexOf(">", tagInfo.pos);
2176
+ if (tagInfo.name === tagName) {
2177
+ const newDepth = depth - 1;
2178
+ if (newDepth === 0) {
2179
+ return { newPos: nextLt, newDepth, found: true };
2180
+ }
2181
+ return { newPos: gt === -1 ? len : gt + 1, newDepth, found: false };
2182
+ }
2183
+ return { newPos: gt === -1 ? len : gt + 1, newDepth: depth, found: false };
2184
+ }
2185
+ function processOpeningTagMatch(options) {
2186
+ const { xmlContent, nx, len, tagName, depth } = options;
2187
+ const tagInfo = parseTagName(xmlContent, nx, len);
2188
+ const tagEndInfo = skipToTagEnd(xmlContent, tagInfo.pos, len);
2189
+ const newDepth = tagInfo.name === tagName && !tagEndInfo.isSelfClosing ? depth + 1 : depth;
2190
+ const newPos = xmlContent[tagEndInfo.pos] === ">" ? tagEndInfo.pos + 1 : tagEndInfo.pos + 1;
2191
+ return { newPos, newDepth };
2192
+ }
2193
+ function findMatchingCloseTag(xmlContent, startPos, tagName, len) {
2194
+ let pos = startPos;
2195
+ let depth = 1;
2196
+ while (pos < len) {
2197
+ const nextLt = xmlContent.indexOf("<", pos);
2198
+ if (nextLt === -1 || nextLt + 1 >= len) {
2199
+ break;
2200
+ }
2201
+ const nx = nextLt + 1;
2202
+ const h = xmlContent[nx];
2203
+ const specialPos = skipSpecialConstruct(xmlContent, nx, len);
2204
+ if (specialPos !== -1) {
2205
+ pos = specialPos;
2206
+ continue;
2207
+ }
2208
+ if (h === "/") {
2209
+ const result = processClosingTagMatch({
2210
+ xmlContent,
2211
+ nx,
2212
+ len,
2213
+ tagName,
2214
+ depth,
2215
+ nextLt
2216
+ });
2217
+ if (result.found) {
2218
+ return result.newPos;
2219
+ }
2220
+ pos = result.newPos;
2221
+ depth = result.newDepth;
2222
+ } else {
2223
+ const result = processOpeningTagMatch({
2224
+ xmlContent,
2225
+ nx,
2226
+ len,
2227
+ tagName,
2228
+ depth
2229
+ });
2230
+ pos = result.newPos;
2231
+ depth = result.newDepth;
2232
+ }
2233
+ }
2234
+ return -1;
2235
+ }
2236
+ function updateBestMatch(depth, bestDepth, contentStart, contentEnd) {
2237
+ if (depth < bestDepth) {
2238
+ return { start: contentStart, end: contentEnd, depth };
2239
+ }
2240
+ return null;
2241
+ }
2242
+ function processTargetTag(options) {
2243
+ const { xmlContent, tagEnd, isSelfClosing, target, len, depth, bestDepth } = options;
2244
+ const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
2245
+ if (isSelfClosing) {
2246
+ return updateBestMatch(depth, bestDepth, contentStart, contentStart);
2247
+ }
2248
+ const closePos = findMatchingCloseTag(xmlContent, contentStart, target, len);
2249
+ if (closePos !== -1) {
2250
+ return updateBestMatch(depth, bestDepth, contentStart, closePos);
2251
+ }
2252
+ return null;
2253
+ }
2254
+ function handleClosingTagInExtract(xmlContent, i, len, depth) {
2255
+ const gt = xmlContent.indexOf(">", i + 1);
2256
+ return {
2257
+ newPos: gt === -1 ? len : gt + 1,
2258
+ newDepth: Math.max(0, depth - 1)
2259
+ };
2260
+ }
2261
+ function processOpeningTagInExtract(options) {
2262
+ const { xmlContent, i, len, target, depth, bestDepth } = options;
2263
+ const tagInfo = parseTagName(xmlContent, i, len);
2264
+ const tagEndInfo = skipToTagEnd(xmlContent, tagInfo.pos, len);
2265
+ const tagEnd = tagEndInfo.pos;
2266
+ const isSelfClosing = tagEndInfo.isSelfClosing;
2267
+ let bestMatch = null;
2268
+ if (tagInfo.name === target) {
2269
+ bestMatch = processTargetTag({
2270
+ xmlContent,
2271
+ tagEnd,
2272
+ isSelfClosing,
2273
+ target,
2274
+ len,
2275
+ depth,
2276
+ bestDepth
2277
+ });
2278
+ }
2279
+ return {
2280
+ newPos: xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1,
2281
+ newDepth: depth + (isSelfClosing ? 0 : 1),
2282
+ bestMatch
2283
+ };
2284
+ }
2285
+ function extractRawInner(xmlContent, tagName) {
2286
+ const len = xmlContent.length;
2287
+ const target = tagName;
2288
+ let bestStart = -1;
2289
+ let bestEnd = -1;
2290
+ let bestDepth = Number.POSITIVE_INFINITY;
2291
+ let i = 0;
2292
+ let depth = 0;
2293
+ while (i < len) {
2294
+ const lt = xmlContent.indexOf("<", i);
2295
+ if (lt === -1 || lt + 1 >= len) {
2296
+ return;
2297
+ }
2298
+ i = lt + 1;
2299
+ const ch = xmlContent[i];
2300
+ const specialPos = skipSpecialConstruct(xmlContent, i, len);
2301
+ if (specialPos !== -1) {
2302
+ i = specialPos;
2303
+ continue;
2304
+ }
2305
+ if (ch === "/") {
2306
+ const result2 = handleClosingTagInExtract(xmlContent, i, len, depth);
2307
+ i = result2.newPos;
2308
+ depth = result2.newDepth;
2309
+ continue;
2310
+ }
2311
+ const result = processOpeningTagInExtract({
2312
+ xmlContent,
2313
+ i,
2314
+ len,
2315
+ target,
2316
+ depth,
2317
+ bestDepth
2318
+ });
2319
+ if (result.bestMatch) {
2320
+ bestStart = result.bestMatch.start;
2321
+ bestEnd = result.bestMatch.end;
2322
+ bestDepth = result.bestMatch.depth;
2323
+ }
2324
+ i = result.newPos;
2325
+ depth = result.newDepth;
2326
+ }
2327
+ if (bestStart !== -1) {
2328
+ return xmlContent.slice(bestStart, bestEnd);
2329
+ }
2330
+ return;
2331
+ }
2332
+ function processOpeningTag(options) {
2333
+ const { xmlContent, tagEnd, isSelfClosing, target, len, ranges } = options;
2334
+ const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
2335
+ if (isSelfClosing) {
2336
+ ranges.push({ start: contentStart, end: contentStart });
2337
+ return contentStart;
2338
+ }
2339
+ const closePos = findMatchingCloseTag(xmlContent, contentStart, target, len);
2340
+ if (closePos !== -1) {
2341
+ ranges.push({ start: contentStart, end: closePos });
2342
+ const gt = xmlContent.indexOf(">", closePos);
2343
+ return gt === -1 ? len : gt + 1;
2344
+ }
2345
+ return -1;
2346
+ }
2347
+ function handleClosingTagInFindAll(xmlContent, i, len) {
2348
+ const gt = xmlContent.indexOf(">", i + 1);
2349
+ return gt === -1 ? len : gt + 1;
2350
+ }
2351
+ function findAllInnerRanges(xmlContent, tagName) {
2352
+ const len = xmlContent.length;
2353
+ const target = tagName;
2354
+ const ranges = [];
2355
+ let i = 0;
2356
+ while (i < len) {
2357
+ const lt = xmlContent.indexOf("<", i);
2358
+ if (lt === -1 || lt + 1 >= len) {
2359
+ break;
2360
+ }
2361
+ i = lt + 1;
2362
+ const ch = xmlContent[i];
2363
+ const specialPos = skipSpecialConstruct(xmlContent, i, len);
2364
+ if (specialPos !== -1) {
2365
+ i = specialPos;
2366
+ continue;
2367
+ }
2368
+ if (ch === "/") {
2369
+ i = handleClosingTagInFindAll(xmlContent, i, len);
2370
+ continue;
2371
+ }
2372
+ const tagInfo = parseTagName(xmlContent, i, len);
2373
+ const tagEndInfo = skipToTagEnd(xmlContent, tagInfo.pos, len);
2374
+ const tagEnd = tagEndInfo.pos;
2375
+ const isSelfClosing = tagEndInfo.isSelfClosing;
2376
+ if (tagInfo.name !== target) {
2377
+ i = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
2378
+ continue;
2379
+ }
2380
+ const nextPos = processOpeningTag({
2381
+ xmlContent,
2382
+ tagEnd,
2383
+ isSelfClosing,
2384
+ target,
2385
+ len,
2386
+ ranges
2387
+ });
2388
+ if (nextPos === -1) {
2389
+ break;
2390
+ }
2391
+ i = nextPos;
2392
+ }
2393
+ return ranges;
2394
+ }
2395
+ function findTopLevelTargetRange(options) {
2396
+ const { xmlContent, tagEnd, isSelfClosing, target, len } = options;
2397
+ const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
2398
+ if (isSelfClosing) {
2399
+ return { start: contentStart, end: contentStart };
2400
+ }
2401
+ const closePos = findMatchingCloseTag(xmlContent, contentStart, target, len);
2402
+ if (closePos !== -1) {
2403
+ return { start: contentStart, end: closePos };
2404
+ }
2405
+ return;
2406
+ }
2407
+ function handleClosingTagInFindFirst(xmlContent, i, len, depth) {
2408
+ const gt = xmlContent.indexOf(">", i + 1);
2409
+ return {
2410
+ newPos: gt === -1 ? len : gt + 1,
2411
+ newDepth: Math.max(0, depth - 1)
2412
+ };
2413
+ }
2414
+ function findFirstTopLevelRange(xmlContent, tagName) {
2415
+ const len = xmlContent.length;
2416
+ const target = tagName;
2417
+ let i = 0;
2418
+ let depth = 0;
2419
+ while (i < len) {
2420
+ const lt = xmlContent.indexOf("<", i);
2421
+ if (lt === -1 || lt + 1 >= len) {
2422
+ return;
2423
+ }
2424
+ i = lt + 1;
2425
+ const ch = xmlContent[i];
2426
+ const specialPos = skipSpecialConstruct(xmlContent, i, len);
2427
+ if (specialPos !== -1) {
2428
+ i = specialPos;
2429
+ continue;
2430
+ }
2431
+ if (ch === "/") {
2432
+ const result = handleClosingTagInFindFirst(xmlContent, i, len, depth);
2433
+ i = result.newPos;
2434
+ depth = result.newDepth;
2435
+ continue;
2436
+ }
2437
+ const tagInfo = parseTagName(xmlContent, i, len);
2438
+ const tagEndInfo = skipToTagEnd(xmlContent, tagInfo.pos, len);
2439
+ const tagEnd = tagEndInfo.pos;
2440
+ const isSelfClosing = tagEndInfo.isSelfClosing;
2441
+ if (depth === 0 && tagInfo.name === target) {
2442
+ return findTopLevelTargetRange({
2443
+ xmlContent,
2444
+ tagEnd,
2445
+ isSelfClosing,
2446
+ target,
2447
+ len
2448
+ });
2449
+ }
2450
+ i = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
2451
+ depth += isSelfClosing ? 0 : 1;
2452
+ }
2453
+ return;
2454
+ }
2455
+ function isPositionExcluded(pos, excludeRanges) {
2456
+ if (!excludeRanges || excludeRanges.length === 0) {
2457
+ return false;
2458
+ }
2459
+ for (const r of excludeRanges) {
2460
+ if (pos >= r.start && pos < r.end) {
2461
+ return true;
2462
+ }
2463
+ }
2464
+ return false;
2465
+ }
2466
+ function skipCommentInCounting(xmlContent, i, len) {
2467
+ const close = xmlContent.indexOf("-->", i + 4);
2468
+ return close === -1 ? len : close + 3;
2469
+ }
2470
+ function skipCdataInCounting(xmlContent, i, len) {
2471
+ const close = xmlContent.indexOf("]]>", i + 9);
2472
+ return close === -1 ? len : close + 3;
2473
+ }
2474
+ function skipSpecialInCounting(xmlContent, ch, i, len) {
2475
+ if (ch === "!") {
2476
+ if (xmlContent.startsWith("!--", i + 1)) {
2477
+ return skipCommentInCounting(xmlContent, i, len);
2478
+ }
2479
+ if (xmlContent.startsWith("![CDATA[", i + 1)) {
2480
+ return skipCdataInCounting(xmlContent, i, len);
2481
+ }
2482
+ const gt = xmlContent.indexOf(">", i + 1);
2483
+ return gt === -1 ? len : gt + 1;
2484
+ }
2485
+ if (ch === "?") {
2486
+ const close = xmlContent.indexOf("?>", i + 1);
2487
+ return close === -1 ? len : close + 2;
2488
+ }
2489
+ if (ch === "/") {
2490
+ const gt = xmlContent.indexOf(">", i + 1);
2491
+ return gt === -1 ? len : gt + 1;
2492
+ }
2493
+ return -1;
2494
+ }
2495
+ function parseAndCountTag(options) {
2496
+ const { xmlContent, i, len, target, lt, excludeRanges } = options;
2497
+ let j = i;
2498
+ if (j < len && isNameStartChar(xmlContent[j])) {
2499
+ j += 1;
2500
+ while (j < len && isNameChar(xmlContent[j])) {
2501
+ j += 1;
2502
+ }
2503
+ }
2504
+ const name = xmlContent.slice(i, j);
2505
+ let k = j;
2506
+ while (k < len) {
2507
+ const c = xmlContent[k];
2508
+ if (c === '"' || c === "'") {
2509
+ k = skipQuoted(xmlContent, k);
2510
+ continue;
2511
+ }
2512
+ if (c === ">") {
2513
+ break;
2514
+ }
2515
+ if (c === "/" && xmlContent[k + 1] === ">") {
2516
+ k += 1;
2517
+ break;
2518
+ }
2519
+ k += 1;
2520
+ }
2521
+ const shouldCount = name === target && !isPositionExcluded(lt, excludeRanges);
2522
+ return { nextPos: k + 1, shouldCount };
2523
+ }
2524
+ function countTagOccurrences(xmlContent, tagName, excludeRanges, shouldSkipFirst = true) {
2525
+ const len = xmlContent.length;
2526
+ const target = tagName;
2527
+ let i = 0;
2528
+ let count = 0;
2529
+ let skipFirstLocal = shouldSkipFirst;
2530
+ while (i < len) {
2531
+ const lt = xmlContent.indexOf("<", i);
2532
+ if (lt === -1) {
2533
+ break;
2534
+ }
2535
+ i = lt + 1;
2536
+ if (i >= len) {
2537
+ break;
2538
+ }
2539
+ const ch = xmlContent[i];
2540
+ const skipPos = skipSpecialInCounting(xmlContent, ch, i, len);
2541
+ if (skipPos !== -1) {
2542
+ i = skipPos;
2543
+ continue;
2544
+ }
2545
+ const result = parseAndCountTag({
2546
+ xmlContent,
2547
+ i,
2548
+ len,
2549
+ target,
2550
+ lt,
2551
+ excludeRanges
2552
+ });
2553
+ if (result.shouldCount) {
2554
+ if (skipFirstLocal) {
2555
+ skipFirstLocal = false;
2556
+ } else {
2557
+ count += 1;
2558
+ }
2559
+ }
2560
+ i = result.nextPos;
2561
+ }
2562
+ return count;
2563
+ }
2564
+
2565
+ // src/rxml/core/tokenizer.ts
2566
+ var XMLTokenizer = class {
2567
+ constructor(xmlString, options = {}) {
2568
+ this.pos = 0;
2569
+ this.xmlString = xmlString;
2570
+ this.options = {
2571
+ keepComments: false,
2572
+ keepWhitespace: false,
2573
+ noChildNodes: DEFAULT_NO_CHILD_NODES.slice(),
2574
+ textNodeName: "#text",
2575
+ throwOnDuplicateStringTags: true,
2576
+ ...options
2577
+ };
2578
+ this.pos = options.pos || 0;
2579
+ }
2580
+ /**
2581
+ * Handle closing tag parsing
2582
+ */
2583
+ handleClosingTag(tagName, children) {
2584
+ const closeStart = this.pos + 2;
2585
+ this.pos = this.xmlString.indexOf(">", this.pos);
2586
+ const closeTag = this.xmlString.substring(closeStart, this.pos);
2587
+ if (tagName && closeTag.trim() !== tagName) {
2588
+ const { line, column } = getLineColumn(this.xmlString, this.pos);
2589
+ throw new RXMLParseError(
2590
+ `Unexpected close tag at line ${line}, column ${column}. Expected </${tagName}>, found </${closeTag}>`,
2591
+ void 0,
2592
+ line,
2593
+ column
2594
+ );
2595
+ }
2596
+ if (this.pos !== -1) {
2597
+ this.pos += 1;
2598
+ }
2599
+ return children;
2600
+ }
2601
+ /**
2602
+ * Check if we're at end of string and should throw unclosed tag error
2603
+ */
2604
+ checkUnclosedTag(tagName, consumedToEnd) {
2605
+ if (tagName && this.pos >= this.xmlString.length && !consumedToEnd) {
2606
+ const { line, column } = getLineColumn(this.xmlString, this.pos - 1);
2607
+ throw new RXMLParseError(
2608
+ `Unclosed tag at line ${line}, column ${column}. Expected closing tag </${tagName}>`,
2609
+ void 0,
2610
+ line,
2611
+ column
2612
+ );
2613
+ }
2614
+ }
2615
+ /**
2616
+ * Process special content (comments, CDATA, DOCTYPE) and track if we consumed to end
2617
+ */
2618
+ processSpecialContent(children) {
2619
+ const prevPos = this.pos;
2620
+ this.handleSpecialContent(children);
2621
+ return this.pos >= this.xmlString.length && prevPos < this.xmlString.length;
2622
+ }
2623
+ /**
2624
+ * Handle text content parsing
2625
+ */
2626
+ handleTextContent(children) {
2627
+ const text = this.parseText();
2628
+ if (this.options.keepWhitespace) {
2629
+ if (text.length > 0) {
2630
+ children.push(text);
2631
+ }
2632
+ } else {
2633
+ const trimmed = text.trim();
2634
+ if (trimmed.length > 0) {
2635
+ children.push(trimmed);
2636
+ }
2637
+ }
2638
+ this.pos += 1;
2639
+ }
2640
+ /**
2641
+ * Handle regular element parsing
2642
+ */
2643
+ handleRegularElement(children) {
2644
+ const node = this.parseNode();
2645
+ children.push(node);
2646
+ if (node.tagName[0] === "?") {
2647
+ children.push(...node.children);
2648
+ node.children = [];
2649
+ }
2650
+ }
2651
+ /**
2652
+ * Process a single child element based on the current character
2653
+ */
2654
+ processSingleChild(children, tagName) {
2655
+ if (this.xmlString.charCodeAt(this.pos) !== CharCodes.OPEN_BRACKET) {
2656
+ this.handleTextContent(children);
2657
+ return { shouldReturn: false, consumedToEnd: false };
2658
+ }
2659
+ const nextChar = this.xmlString.charCodeAt(this.pos + 1);
2660
+ if (nextChar === CharCodes.SLASH) {
2661
+ const result = this.handleClosingTag(tagName, children);
2662
+ if (result !== null) {
2663
+ return { shouldReturn: true, consumedToEnd: false };
2664
+ }
2665
+ return { shouldReturn: false, consumedToEnd: false };
2666
+ }
2667
+ if (nextChar === CharCodes.EXCLAMATION) {
2668
+ const wasConsumedToEnd = this.processSpecialContent(children);
2669
+ return { shouldReturn: false, consumedToEnd: wasConsumedToEnd };
2670
+ }
2671
+ this.handleRegularElement(children);
2672
+ return { shouldReturn: false, consumedToEnd: false };
2673
+ }
2674
+ /**
2675
+ * Parse XML children recursively
2676
+ */
2677
+ parseChildren(tagName) {
2678
+ const children = [];
2679
+ let consumedToEnd = false;
2680
+ while (this.xmlString[this.pos]) {
2681
+ const result = this.processSingleChild(children, tagName);
2682
+ if (result.shouldReturn) {
2683
+ return children;
2684
+ }
2685
+ if (result.consumedToEnd) {
2686
+ consumedToEnd = true;
2687
+ }
2688
+ }
2689
+ this.checkUnclosedTag(tagName, consumedToEnd);
2690
+ return children;
2691
+ }
2692
+ /**
2693
+ * Check if character is whitespace
2694
+ */
2695
+ isWhitespace(code) {
2696
+ return code === CharCodes.SPACE || code === CharCodes.TAB || code === CharCodes.NEWLINE || code === CharCodes.CARRIAGE_RETURN;
2697
+ }
2698
+ /**
2699
+ * Skip whitespace characters
2700
+ */
2701
+ skipWhitespace() {
2702
+ while (this.pos < this.xmlString.length && this.isWhitespace(this.xmlString.charCodeAt(this.pos))) {
2703
+ this.pos += 1;
2704
+ }
2705
+ }
2706
+ /**
2707
+ * Parse attribute value
2708
+ */
2709
+ parseAttributeValue() {
2710
+ if (this.pos >= this.xmlString.length || this.xmlString[this.pos] !== "=") {
2711
+ return null;
2712
+ }
2713
+ this.pos += 1;
2714
+ this.skipWhitespace();
2715
+ const code = this.xmlString.charCodeAt(this.pos);
2716
+ if (code === CharCodes.SINGLE_QUOTE || code === CharCodes.DOUBLE_QUOTE) {
2717
+ const { value: parsedValue, newPos: valueEnd } = parseString(
2718
+ this.xmlString,
2719
+ this.pos
2720
+ );
2721
+ this.pos = valueEnd;
2722
+ return parsedValue;
2723
+ }
2724
+ return null;
2725
+ }
2726
+ /**
2727
+ * Parse single attribute
2728
+ */
2729
+ parseAttribute(attributes) {
2730
+ const { name: attrName, newPos: nameEnd } = parseName(
2731
+ this.xmlString,
2732
+ this.pos
2733
+ );
2734
+ this.pos = nameEnd;
2735
+ this.skipWhitespace();
2736
+ const value = this.parseAttributeValue();
2737
+ attributes[attrName] = value;
2738
+ }
2739
+ /**
2740
+ * Parse all attributes
2741
+ */
2742
+ parseAttributes() {
2743
+ const attributes = {};
2744
+ while (this.xmlString.charCodeAt(this.pos) !== CharCodes.CLOSE_BRACKET && this.xmlString[this.pos]) {
2745
+ const c = this.xmlString.charCodeAt(this.pos);
2746
+ if (this.isWhitespace(c)) {
2747
+ this.pos += 1;
2748
+ continue;
2749
+ }
2750
+ if (c > 64 && c < 91 || c > 96 && c < 123) {
2751
+ this.parseAttribute(attributes);
2752
+ } else {
2753
+ this.pos += 1;
2754
+ }
2755
+ }
2756
+ return attributes;
2757
+ }
2758
+ /**
2759
+ * Parse special tag content (script, style)
2760
+ */
2761
+ parseSpecialTagContent(_tagName, closingTag) {
2762
+ const start = this.pos + 1;
2763
+ this.pos = this.xmlString.indexOf(closingTag, this.pos);
2764
+ if (this.pos === -1) {
2765
+ const children2 = [this.xmlString.slice(start)];
2766
+ this.pos = this.xmlString.length;
2767
+ return children2;
2768
+ }
2769
+ const children = [this.xmlString.slice(start, this.pos)];
2770
+ this.pos += closingTag.length;
2771
+ return children;
2772
+ }
2773
+ /**
2774
+ * Parse node children based on tag type
2775
+ */
2776
+ parseNodeChildren(tagName, isSelfClosing) {
2777
+ var _a;
2778
+ if (isSelfClosing) {
2779
+ this.pos += 1;
2780
+ return [];
2781
+ }
2782
+ if (tagName === "script") {
2783
+ return this.parseSpecialTagContent(tagName, "</script>");
2784
+ }
2785
+ if (tagName === "style") {
2786
+ return this.parseSpecialTagContent(tagName, "</style>");
2787
+ }
2788
+ if (((_a = this.options.noChildNodes) == null ? void 0 : _a.indexOf(tagName)) === -1) {
2789
+ this.pos += 1;
2790
+ return this.parseChildren(tagName);
2791
+ }
2792
+ this.pos += 1;
2793
+ if (DEFAULT_NO_CHILD_NODES.includes(tagName)) {
2794
+ return [];
2795
+ }
2796
+ const closingTag = `</${tagName}>`;
2797
+ const closingPos = this.xmlString.indexOf(closingTag, this.pos);
2798
+ if (closingPos !== -1) {
2799
+ this.pos = closingPos + closingTag.length;
2800
+ }
2801
+ return [];
2802
+ }
2803
+ /**
2804
+ * Parse a single XML node
2805
+ */
2806
+ parseNode() {
2807
+ this.pos += 1;
2808
+ const { name: tagName, newPos } = parseName(this.xmlString, this.pos);
2809
+ this.pos = newPos;
2810
+ const attributes = this.parseAttributes();
2811
+ const isSelfClosing = this.xmlString.charCodeAt(this.pos - 1) === CharCodes.SLASH || tagName[0] === "?" && this.xmlString.charCodeAt(this.pos - 1) === CharCodes.QUESTION;
2812
+ const children = this.parseNodeChildren(tagName, isSelfClosing);
2813
+ return { tagName, attributes, children };
2814
+ }
2815
+ /**
2816
+ * Parse text content until next tag
2817
+ */
2818
+ parseText() {
2819
+ const start = this.pos;
2820
+ this.pos = this.xmlString.indexOf("<", this.pos) - 1;
2821
+ if (this.pos === -2) {
2822
+ this.pos = this.xmlString.length;
2823
+ }
2824
+ return this.xmlString.slice(start, this.pos + 1);
2825
+ }
2826
+ /**
2827
+ * Handle comments, CDATA, and DOCTYPE declarations
2828
+ */
2829
+ handleSpecialContent(children) {
2830
+ if (this.xmlString.charCodeAt(this.pos + 2) === CharCodes.MINUS) {
2831
+ this.handleComment(children);
2832
+ } else if (this.xmlString.charCodeAt(this.pos + 2) === CharCodes.OPEN_CORNER_BRACKET && this.xmlString.charCodeAt(this.pos + 8) === CharCodes.OPEN_CORNER_BRACKET && this.xmlString.substr(this.pos + 3, 5).toLowerCase() === "cdata") {
2833
+ this.handleCData(children);
2834
+ } else {
2835
+ this.handleDoctype(children);
2836
+ }
2837
+ }
2838
+ /**
2839
+ * Handle XML comments
2840
+ */
2841
+ handleComment(children) {
2842
+ const startCommentPos = this.pos;
2843
+ while (this.pos !== -1 && !(this.xmlString.charCodeAt(this.pos) === CharCodes.CLOSE_BRACKET && this.xmlString.charCodeAt(this.pos - 1) === CharCodes.MINUS && this.xmlString.charCodeAt(this.pos - 2) === CharCodes.MINUS)) {
2844
+ this.pos = this.xmlString.indexOf(">", this.pos + 1);
2845
+ }
2846
+ if (this.pos === -1) {
2847
+ this.pos = this.xmlString.length;
2848
+ }
2849
+ if (this.options.keepComments) {
2850
+ children.push(this.xmlString.substring(startCommentPos, this.pos + 1));
2851
+ }
2852
+ this.pos += 1;
2853
+ }
2854
+ /**
2855
+ * Handle CDATA sections
2856
+ */
2857
+ handleCData(children) {
2858
+ const cdataEndIndex = this.xmlString.indexOf("]]>", this.pos);
2859
+ if (cdataEndIndex === -1) {
2860
+ children.push(this.xmlString.substr(this.pos + 9));
2861
+ this.pos = this.xmlString.length;
2862
+ } else {
2863
+ children.push(this.xmlString.substring(this.pos + 9, cdataEndIndex));
2864
+ this.pos = cdataEndIndex + 3;
2865
+ }
2866
+ }
2867
+ /**
2868
+ * Handle DOCTYPE declarations
2869
+ */
2870
+ handleDoctype(children) {
2871
+ const startDoctype = this.pos + 1;
2872
+ this.pos += 2;
2873
+ let encapsulated = false;
2874
+ while ((this.xmlString.charCodeAt(this.pos) !== CharCodes.CLOSE_BRACKET || encapsulated) && this.xmlString[this.pos]) {
2875
+ if (this.xmlString.charCodeAt(this.pos) === CharCodes.OPEN_CORNER_BRACKET) {
2876
+ encapsulated = true;
2877
+ } else if (encapsulated && this.xmlString.charCodeAt(this.pos) === CharCodes.CLOSE_CORNER_BRACKET) {
2878
+ encapsulated = false;
2879
+ }
2880
+ this.pos += 1;
2881
+ }
2882
+ children.push(this.xmlString.substring(startDoctype, this.pos));
2883
+ this.pos += 1;
2884
+ }
2885
+ /**
2886
+ * Get current position
2887
+ */
2888
+ getPosition() {
2889
+ return this.pos;
2890
+ }
2891
+ /**
2892
+ * Set position
2893
+ */
2894
+ setPosition(pos) {
2895
+ this.pos = pos;
2896
+ }
2897
+ };
2898
+
2899
+ // src/rxml/core/parser.ts
2900
+ var WHITESPACE_REGEX2 = /\s/;
2901
+ var NUMERIC_STRING_REGEX = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
2902
+ var DIGIT_KEY_REGEX2 = /^\d+$/;
2903
+ function getTopLevelStringProps(s) {
2904
+ const set = /* @__PURE__ */ new Set();
2905
+ const unwrapped = unwrapJsonSchema(s);
2906
+ if (unwrapped && typeof unwrapped === "object") {
2907
+ const props = unwrapped.properties;
2908
+ if (props && typeof props === "object") {
2909
+ for (const [k, v] of Object.entries(props)) {
2910
+ if (getSchemaType(v) === "string") {
2911
+ set.add(k);
2912
+ }
2913
+ }
2914
+ }
2915
+ }
2916
+ return set;
2917
+ }
2918
+ function restorePlaceholderString(val, placeholderMap) {
2919
+ if (val.startsWith("__RXML_PLACEHOLDER_")) {
2920
+ const orig = placeholderMap.get(val);
2921
+ return orig !== void 0 ? orig : val;
2922
+ }
2923
+ return val;
2924
+ }
2925
+ function restorePlaceholdersInObject(obj, _placeholderMap, textNodeName, restorer) {
2926
+ const out = {};
2927
+ for (const [k, v] of Object.entries(obj)) {
2928
+ const restored = restorer(v);
2929
+ if (k === textNodeName && typeof restored === "string") {
2930
+ out[k] = restored.trim();
2931
+ } else {
2932
+ out[k] = restored;
2933
+ }
2934
+ }
2935
+ return out;
2936
+ }
2937
+ function createPlaceholderRestorer(placeholderMap, textNodeName) {
2938
+ const restorer = (val) => {
2939
+ if (val == null) {
2940
+ return val;
2941
+ }
2942
+ if (typeof val === "string") {
2943
+ return restorePlaceholderString(val, placeholderMap);
2944
+ }
2945
+ if (Array.isArray(val)) {
2946
+ return val.map(restorer);
2947
+ }
2948
+ if (typeof val === "object") {
2949
+ return restorePlaceholdersInObject(
2950
+ val,
2951
+ placeholderMap,
2952
+ textNodeName,
2953
+ restorer
2954
+ );
2955
+ }
2956
+ return val;
2957
+ };
2958
+ return restorer;
2959
+ }
2960
+ function tryConvertToNumber(val) {
2961
+ if (typeof val !== "string") {
2962
+ return val;
2963
+ }
2964
+ const trimmed = val.trim();
2965
+ if (NUMERIC_STRING_REGEX.test(trimmed)) {
2966
+ const num = Number(trimmed);
2967
+ if (Number.isFinite(num)) {
2968
+ return num;
2969
+ }
2970
+ }
2971
+ return trimmed;
2972
+ }
2973
+ function processItemValue(item, textNodeName) {
2974
+ let currentVal = item;
2975
+ if (item && typeof item === "object" && Object.hasOwn(item, textNodeName)) {
2976
+ currentVal = item[textNodeName];
2977
+ }
2978
+ const trimmed = typeof currentVal === "string" ? currentVal.trim() : currentVal;
2979
+ return tryConvertToNumber(trimmed);
2980
+ }
2981
+ function processItemWrapper(itemValue, textNodeName) {
2982
+ if (Array.isArray(itemValue)) {
2983
+ return itemValue.map((item) => processItemValue(item, textNodeName));
2984
+ }
2985
+ const trimmed = typeof itemValue === "string" ? itemValue.trim() : itemValue;
2986
+ return tryConvertToNumber(trimmed);
2987
+ }
2988
+ function deepDecodeStringsBySchema(input, schema) {
2989
+ var _a;
2990
+ if (input == null || schema == null) {
2991
+ return input;
2992
+ }
2993
+ const type = getSchemaType(schema);
2994
+ if (type === "string" && typeof input === "string") {
2995
+ return unescapeXml(input);
2996
+ }
2997
+ if (type === "array" && Array.isArray(input)) {
2998
+ const unwrapped = unwrapJsonSchema(schema);
2999
+ const itemSchema = (_a = unwrapped == null ? void 0 : unwrapped.items) != null ? _a : {};
3000
+ return input.map((item) => deepDecodeStringsBySchema(item, itemSchema));
3001
+ }
3002
+ if (type === "object" && input && typeof input === "object") {
3003
+ const obj = input;
3004
+ const out = {};
3005
+ for (const key of Object.keys(obj)) {
3006
+ const childSchema = getPropertySchema(schema, key);
3007
+ out[key] = deepDecodeStringsBySchema(obj[key], childSchema);
3008
+ }
3009
+ return out;
3010
+ }
3011
+ if (typeof input === "string") {
3012
+ return unescapeXml(input);
3013
+ }
3014
+ return input;
3015
+ }
3016
+ function parse2(xmlInner, schema, options = {}) {
3017
+ var _a, _b, _c;
3018
+ const textNodeName = (_a = options.textNodeName) != null ? _a : "#text";
3019
+ const throwDup = (_b = options.throwOnDuplicateStringTags) != null ? _b : true;
3020
+ let actualXmlInner = xmlInner.trim();
3021
+ if (actualXmlInner.startsWith("<") && actualXmlInner.endsWith(">")) {
3022
+ const s = actualXmlInner;
3023
+ let i = 0;
3024
+ let rootStart = -1;
3025
+ let rootName = "";
3026
+ while (i < s.length) {
3027
+ const lt = s.indexOf("<", i);
3028
+ if (lt === -1) {
3029
+ break;
3030
+ }
3031
+ const next = s[lt + 1];
3032
+ if (next === "?") {
3033
+ const end = s.indexOf("?>", lt + 2);
3034
+ i = end === -1 ? s.length : end + 2;
3035
+ continue;
3036
+ }
3037
+ if (next === "!") {
3038
+ if (s.startsWith("!--", lt + 2)) {
3039
+ const end2 = s.indexOf("-->", lt + 5);
3040
+ i = end2 === -1 ? s.length : end2 + 3;
3041
+ continue;
3042
+ }
3043
+ if (s.startsWith("![CDATA[", lt + 2)) {
3044
+ const end2 = s.indexOf("]]>", lt + 9);
3045
+ i = end2 === -1 ? s.length : end2 + 3;
3046
+ continue;
3047
+ }
3048
+ const end = s.indexOf(">", lt + 2);
3049
+ i = end === -1 ? s.length : end + 1;
3050
+ continue;
3051
+ }
3052
+ if (next === "/") {
3053
+ break;
3054
+ }
3055
+ let j = lt + 1;
3056
+ while (j < s.length && s[j] !== " " && s[j] !== "\n" && s[j] !== "\r" && s[j] !== " " && s[j] !== "/" && s[j] !== ">") {
3057
+ j += 1;
3058
+ }
3059
+ rootStart = lt;
3060
+ rootName = s.slice(lt + 1, j);
3061
+ break;
3062
+ }
3063
+ if (rootStart === 0 && rootName) {
3064
+ const range = findFirstTopLevelRange(s, rootName);
3065
+ if (range) {
3066
+ let fullEnd = range.end + `</${rootName}>`.length;
3067
+ const closeHead = s.indexOf(`</${rootName}`, range.end);
3068
+ if (closeHead === range.end) {
3069
+ let p = closeHead + 2 + rootName.length;
3070
+ while (p < s.length && WHITESPACE_REGEX2.test(s[p])) {
3071
+ p += 1;
3072
+ }
3073
+ if (s[p] === ">") {
3074
+ fullEnd = p + 1;
3075
+ }
3076
+ }
3077
+ if (fullEnd === s.length) {
3078
+ const unwrapped = unwrapJsonSchema(schema);
3079
+ const schemaProps = unwrapped && typeof unwrapped === "object" ? unwrapped.properties : void 0;
3080
+ if (schemaProps && !Object.hasOwn(schemaProps, rootName)) {
3081
+ actualXmlInner = s.slice(range.start, range.end);
3082
+ }
3083
+ }
3084
+ }
3085
+ }
3086
+ }
3087
+ const topLevelStringProps = getTopLevelStringProps(schema);
3088
+ const deepStringTypedProps = getStringTypedProperties(schema);
3089
+ const duplicateKeys = /* @__PURE__ */ new Set();
3090
+ for (const key of topLevelStringProps) {
3091
+ const excludeRanges = [];
3092
+ for (const other of topLevelStringProps) {
3093
+ if (other === key) {
3094
+ continue;
3095
+ }
3096
+ const range = findFirstTopLevelRange(actualXmlInner, other);
3097
+ if (range) {
3098
+ excludeRanges.push(range);
3099
+ }
3100
+ }
3101
+ const occurrences = countTagOccurrences(
3102
+ actualXmlInner,
3103
+ key,
3104
+ excludeRanges,
3105
+ true
3106
+ );
3107
+ if (occurrences > 0 && throwDup) {
3108
+ throw new RXMLDuplicateStringTagError(
3109
+ `Duplicate string tags for <${key}> detected`
3110
+ );
3111
+ }
3112
+ if (occurrences > 0 && !throwDup) {
3113
+ duplicateKeys.add(key);
3114
+ if (options.onError) {
3115
+ options.onError(
3116
+ `RXML: Duplicate string tags for <${key}> detected; using first occurrence.`,
3117
+ { tag: key, occurrences }
3118
+ );
3119
+ }
3120
+ }
3121
+ }
3122
+ let xmlInnerForParsing = actualXmlInner;
3123
+ const originalContentMap = /* @__PURE__ */ new Map();
3124
+ try {
3125
+ const ranges = [];
3126
+ for (const key of deepStringTypedProps) {
3127
+ const innerRanges = findAllInnerRanges(actualXmlInner, key);
3128
+ for (const r of innerRanges) {
3129
+ if (r.end > r.start) {
3130
+ ranges.push({ ...r, key });
3131
+ }
3132
+ }
3133
+ }
3134
+ if (ranges.length > 0) {
3135
+ const sorted = [...ranges].sort((a, b) => a.start - b.start);
3136
+ let rebuilt = "";
3137
+ let cursor = 0;
3138
+ for (const r of sorted) {
3139
+ if (r.start < cursor) {
3140
+ continue;
3141
+ }
3142
+ if (cursor < r.start) {
3143
+ rebuilt += actualXmlInner.slice(cursor, r.start);
3144
+ }
3145
+ const placeholder = `__RXML_PLACEHOLDER_${r.key}_${r.start}_${r.end}__`;
3146
+ const originalContent = actualXmlInner.slice(r.start, r.end);
3147
+ originalContentMap.set(placeholder, originalContent);
3148
+ rebuilt += placeholder;
3149
+ cursor = r.end;
3150
+ }
3151
+ if (cursor < actualXmlInner.length) {
3152
+ rebuilt += actualXmlInner.slice(cursor);
3153
+ }
3154
+ xmlInnerForParsing = rebuilt;
3155
+ }
3156
+ } catch (error) {
3157
+ if (options.onError) {
3158
+ options.onError(
3159
+ "RXML: Failed to replace string placeholders, falling back to original XML.",
3160
+ { error }
3161
+ );
3162
+ }
3163
+ xmlInnerForParsing = actualXmlInner;
3164
+ }
3165
+ let parsedNodes;
3166
+ try {
3167
+ const wrappedXml = `<root>${xmlInnerForParsing}</root>`;
3168
+ const tokenizer = new XMLTokenizer(wrappedXml, {
3169
+ ...options,
3170
+ textNodeName
3171
+ });
3172
+ const rootNode = tokenizer.parseNode();
3173
+ parsedNodes = rootNode.children;
3174
+ } catch (cause) {
3175
+ throw new RXMLParseError("Failed to parse XML", cause);
3176
+ }
3177
+ const parsedArgs = domToObject(parsedNodes, schema, textNodeName);
3178
+ const restorePlaceholdersDeep = createPlaceholderRestorer(
3179
+ originalContentMap,
3180
+ textNodeName
3181
+ );
3182
+ const parsedArgsRestored = restorePlaceholdersDeep(parsedArgs);
3183
+ const args = {};
3184
+ for (const k of Object.keys(parsedArgsRestored || {})) {
3185
+ const v = parsedArgsRestored[k];
3186
+ let val = v;
3187
+ const propSchema = getPropertySchema(schema, k);
3188
+ const propType = getSchemaType(propSchema);
3189
+ if (propType === "string" && duplicateKeys.has(k) && Array.isArray(v)) {
3190
+ const firstValue = v[0];
3191
+ if (typeof firstValue === "string" && firstValue.startsWith("__RXML_PLACEHOLDER_")) {
3192
+ const originalContent = originalContentMap.get(firstValue);
3193
+ if (originalContent !== void 0) {
3194
+ args[k] = originalContent;
3195
+ continue;
3196
+ }
3197
+ } else {
3198
+ args[k] = firstValue;
3199
+ continue;
3200
+ }
3201
+ }
3202
+ if (propType === "string" && !Array.isArray(v)) {
3203
+ const placeholderUsed = typeof v === "string" && v.startsWith("__RXML_PLACEHOLDER_") || v && typeof v === "object" && Object.hasOwn(v, textNodeName) && typeof v[textNodeName] === "string" && v[textNodeName].startsWith(
3204
+ "__RXML_PLACEHOLDER_"
3205
+ );
3206
+ if (placeholderUsed) {
3207
+ let placeholderKey;
3208
+ if (typeof v === "string") {
3209
+ placeholderKey = v;
3210
+ } else {
3211
+ placeholderKey = v[textNodeName];
3212
+ }
3213
+ const originalContent = originalContentMap.get(placeholderKey);
3214
+ if (originalContent !== void 0) {
3215
+ args[k] = originalContent;
3216
+ continue;
3217
+ }
3218
+ }
3219
+ const raw = extractRawInner(actualXmlInner, k);
3220
+ if (typeof raw === "string") {
3221
+ args[k] = raw;
3222
+ continue;
3223
+ }
3224
+ }
3225
+ if (v && typeof v === "object" && Object.hasOwn(v, textNodeName)) {
3226
+ val = v[textNodeName];
3227
+ }
3228
+ if (Array.isArray(v)) {
3229
+ if (propType === "string") {
3230
+ const mapped = v.map((item) => {
3231
+ if (item && typeof item === "object" && Object.hasOwn(item, textNodeName)) {
3232
+ const textVal = item[textNodeName];
3233
+ return typeof textVal === "string" ? textVal : String(textVal);
3234
+ }
3235
+ return typeof item === "string" ? item : String(item);
3236
+ });
3237
+ if (mapped.length > 1 && throwDup) {
3238
+ throw new RXMLDuplicateStringTagError(
3239
+ `Duplicate string tags for <${k}> detected`
3240
+ );
3241
+ }
3242
+ if (mapped.length > 1 && !throwDup && options.onError) {
3243
+ options.onError(
3244
+ `RXML: Duplicate string tags for <${k}> detected; using first occurrence.`,
3245
+ { tag: k, occurrences: mapped.length }
3246
+ );
3247
+ }
3248
+ args[k] = (_c = mapped[0]) != null ? _c : "";
3249
+ continue;
3250
+ }
3251
+ val = processArrayContent(v, propSchema, textNodeName);
3252
+ } else if (v && typeof v === "object" && !Object.hasOwn(v, textNodeName)) {
3253
+ const obj = v;
3254
+ const keys2 = Object.keys(obj);
3255
+ if (keys2.length === 1 && keys2[0] === "item") {
3256
+ val = processItemWrapper(obj.item, textNodeName);
3257
+ } else {
3258
+ let isIndexedTuple = false;
3259
+ if (keys2.length > 0 && keys2.every((key) => DIGIT_KEY_REGEX2.test(key))) {
3260
+ const indices = keys2.map((keyStr) => Number.parseInt(keyStr, 10)).sort((a, b) => a - b);
3261
+ isIndexedTuple = indices[0] === 0 && indices.every((indexVal, idx) => indexVal === idx);
3262
+ }
3263
+ if (isIndexedTuple) {
3264
+ val = processIndexedTuple(obj, textNodeName);
3265
+ } else {
3266
+ val = v;
3267
+ }
3268
+ }
3269
+ }
3270
+ args[k] = typeof val === "string" ? val.trim() : val;
3271
+ }
3272
+ for (const key of topLevelStringProps) {
3273
+ if (!Object.hasOwn(args, key)) {
3274
+ const raw = extractRawInner(actualXmlInner, key);
3275
+ if (typeof raw === "string") {
3276
+ args[key] = raw;
3277
+ }
3278
+ }
3279
+ }
3280
+ let dataToCoerce = args;
3281
+ const keys = Object.keys(args);
3282
+ if (keys.length === 1) {
3283
+ const rootKey = keys[0];
3284
+ const rootValue = args[rootKey];
3285
+ const unwrapped = unwrapJsonSchema(schema);
3286
+ if (unwrapped && typeof unwrapped === "object") {
3287
+ const schemaProps = unwrapped.properties;
3288
+ if (schemaProps && !Object.hasOwn(schemaProps, rootKey)) {
3289
+ dataToCoerce = rootValue;
3290
+ }
3291
+ }
3292
+ }
3293
+ try {
3294
+ const coerced = coerceDomBySchema(dataToCoerce, schema);
3295
+ const decoded = deepDecodeStringsBySchema(coerced, schema);
3296
+ return decoded;
3297
+ } catch (error) {
3298
+ throw new RXMLCoercionError("Failed to coerce by schema", error);
3299
+ }
3300
+ }
3301
+
3302
+ // src/rxml/heuristics/engine.ts
3303
+ function applyRawSegmentUpdate(current, result) {
3304
+ if (result.rawSegment !== void 0) {
3305
+ return { ...current, rawSegment: result.rawSegment };
3306
+ }
3307
+ return current;
3308
+ }
3309
+ function applyParsedUpdate(current, result) {
3310
+ if (result.parsed !== void 0) {
3311
+ return { ...current, parsed: result.parsed };
3312
+ }
3313
+ return current;
3314
+ }
3315
+ function applyWarningsUpdate(current, result) {
3316
+ var _a, _b;
3317
+ if (result.warnings && result.warnings.length > 0) {
3318
+ const meta = (_a = current.meta) != null ? _a : {};
3319
+ const existingWarnings = (_b = meta.warnings) != null ? _b : [];
3320
+ return {
3321
+ ...current,
3322
+ meta: { ...meta, warnings: [...existingWarnings, ...result.warnings] }
3323
+ };
3324
+ }
3325
+ return current;
3326
+ }
3327
+ function attemptReparse(current, result, reparseCount, maxReparses, parse4) {
3328
+ if (!result.reparse || result.rawSegment === void 0 || reparseCount >= maxReparses) {
3329
+ return { state: current, newCount: reparseCount };
3330
+ }
3331
+ try {
3332
+ const reparsed = parse4(result.rawSegment, current.schema);
3333
+ return {
3334
+ state: { ...current, parsed: reparsed, errors: [] },
3335
+ newCount: reparseCount + 1
3336
+ };
3337
+ } catch (error) {
3338
+ return {
3339
+ state: { ...current, errors: [...current.errors, error] },
3340
+ newCount: reparseCount + 1
3341
+ };
3342
+ }
3343
+ }
3344
+ function executePhase(ctx, heuristics, options) {
3345
+ var _a;
3346
+ let current = ctx;
3347
+ let reparseCount = 0;
3348
+ const maxReparses = (_a = options.maxReparses) != null ? _a : 2;
3349
+ for (const heuristic of heuristics) {
3350
+ if (!heuristic.applies(current)) {
3351
+ continue;
3352
+ }
3353
+ const result = heuristic.run(current);
3354
+ current = applyRawSegmentUpdate(current, result);
3355
+ current = applyParsedUpdate(current, result);
3356
+ current = applyWarningsUpdate(current, result);
3357
+ const reparseResult = attemptReparse(
3358
+ current,
3359
+ result,
3360
+ reparseCount,
3361
+ maxReparses,
3362
+ options.parse
3363
+ );
3364
+ current = reparseResult.state;
3365
+ reparseCount = reparseResult.newCount;
3366
+ if (result.stop) {
3367
+ break;
3368
+ }
3369
+ }
3370
+ return current;
3371
+ }
3372
+ function applyHeuristicPipeline(ctx, config, options) {
3373
+ let current = ctx;
3374
+ if (config.preParse && config.preParse.length > 0) {
3375
+ current = executePhase(current, config.preParse, options);
3376
+ }
3377
+ if (current.parsed === null && current.errors.length === 0) {
3378
+ try {
3379
+ const parsed = options.parse(current.rawSegment, current.schema);
3380
+ current = { ...current, parsed, errors: [] };
3381
+ } catch (error) {
3382
+ current = { ...current, errors: [error] };
3383
+ }
3384
+ }
3385
+ if (current.errors.length > 0 && config.fallbackReparse && config.fallbackReparse.length > 0) {
3386
+ current = executePhase(current, config.fallbackReparse, options);
3387
+ }
3388
+ if (current.parsed !== null && config.postParse && config.postParse.length > 0) {
3389
+ current = executePhase(current, config.postParse, options);
3390
+ }
3391
+ return current;
3392
+ }
3393
+ function createIntermediateCall(toolName, rawSegment, schema) {
3394
+ return {
3395
+ toolName,
3396
+ schema,
3397
+ rawSegment,
3398
+ parsed: null,
3399
+ errors: [],
3400
+ meta: { originalContent: rawSegment }
3401
+ };
3402
+ }
3403
+
3404
+ // src/rxml/heuristics/xml-defaults.ts
3405
+ var MALFORMED_CLOSE_RE_G = /<\/\s+([A-Za-z0-9_:-]+)\s*>/g;
3406
+ var MALFORMED_CLOSE_RE = /<\/\s+([A-Za-z0-9_:-]+)\s*>/;
3407
+ var STATUS_TO_STEP_BOUNDARY_RE = /<\/status>\s*<step>/g;
3408
+ var WHITESPACE_REGEX3 = /\s/;
3409
+ var NAME_CHAR_RE = /[A-Za-z0-9_:-]/;
3410
+ var NAME_START_CHAR_RE = /[A-Za-z_:]/;
3411
+ var STEP_TAG_RE = /<step>([\s\S]*?)<\/step>/i;
3412
+ var STATUS_TAG_RE = /<status>([\s\S]*?)<\/status>/i;
3413
+ var normalizeCloseTagsHeuristic = {
3414
+ id: "normalize-close-tags",
3415
+ phase: "pre-parse",
3416
+ applies: () => true,
3417
+ run: (ctx) => {
3418
+ const normalized = ctx.rawSegment.replace(MALFORMED_CLOSE_RE_G, "</$1>");
3419
+ if (normalized !== ctx.rawSegment) {
3420
+ return { rawSegment: normalized };
3421
+ }
3422
+ return {};
3423
+ }
3424
+ };
3425
+ var escapeInvalidLtHeuristic = {
3426
+ id: "escape-invalid-lt",
3427
+ phase: "pre-parse",
3428
+ applies: () => true,
3429
+ run: (ctx) => {
3430
+ const escaped = escapeInvalidLt(ctx.rawSegment);
3431
+ if (escaped !== ctx.rawSegment) {
3432
+ return { rawSegment: escaped };
3433
+ }
3434
+ return {};
3435
+ }
3436
+ };
3437
+ var balanceTagsHeuristic = {
3438
+ id: "balance-tags",
3439
+ phase: "fallback-reparse",
3440
+ applies: (ctx) => {
3441
+ var _a;
3442
+ const original = ((_a = ctx.meta) == null ? void 0 : _a.originalContent) || ctx.rawSegment;
3443
+ const normalized = original.replace(MALFORMED_CLOSE_RE_G, "</$1>");
3444
+ const balanced = balanceTags(original);
3445
+ const hasMalformedClose = MALFORMED_CLOSE_RE.test(original);
3446
+ if (!hasMalformedClose && balanced.length > normalized.length && ctx.errors.length === 0) {
3447
+ return false;
3448
+ }
3449
+ return balanced !== normalized;
3450
+ },
3451
+ run: (ctx) => {
3452
+ var _a;
3453
+ const original = ((_a = ctx.meta) == null ? void 0 : _a.originalContent) || ctx.rawSegment;
3454
+ const balanced = balanceTags(original);
3455
+ const escaped = escapeInvalidLt(balanced);
3456
+ return { rawSegment: escaped, reparse: true };
3457
+ }
3458
+ };
3459
+ var dedupeShellStringTagsHeuristic = {
3460
+ id: "dedupe-shell-string-tags",
3461
+ phase: "fallback-reparse",
3462
+ applies: (ctx) => shouldDeduplicateStringTags(ctx.schema),
3463
+ run: (ctx) => {
3464
+ const names = getStringPropertyNames(ctx.schema);
3465
+ let deduped = ctx.rawSegment;
3466
+ for (const key of names) {
3467
+ deduped = dedupeSingleTag(deduped, key);
3468
+ }
3469
+ if (deduped !== ctx.rawSegment) {
3470
+ return { rawSegment: deduped, reparse: true };
3471
+ }
3472
+ return {};
3473
+ }
3474
+ };
3475
+ var repairAgainstSchemaHeuristic = {
3476
+ id: "repair-against-schema",
3477
+ phase: "post-parse",
3478
+ applies: (ctx) => ctx.parsed !== null && typeof ctx.parsed === "object",
3479
+ run: (ctx) => {
3480
+ const repaired = repairParsedAgainstSchema(ctx.parsed, ctx.schema);
3481
+ if (repaired !== ctx.parsed) {
3482
+ return { parsed: repaired };
3483
+ }
3484
+ return {};
3485
+ }
3486
+ };
3487
+ var defaultPipelineConfig = {
3488
+ preParse: [normalizeCloseTagsHeuristic, escapeInvalidLtHeuristic],
3489
+ fallbackReparse: [balanceTagsHeuristic, dedupeShellStringTagsHeuristic],
3490
+ postParse: [repairAgainstSchemaHeuristic]
3491
+ };
3492
+ var INDEX_TAG_RE = /^<(\d+)(?:>|\/?>)/;
3493
+ function isIndexTagAt(xml, pos) {
3494
+ const remaining = xml.slice(pos);
3495
+ return INDEX_TAG_RE.test(remaining);
3496
+ }
3497
+ function escapeInvalidLt(xml) {
3498
+ const len = xml.length;
3499
+ let out = "";
3500
+ for (let i = 0; i < len; i += 1) {
3501
+ const ch = xml[i];
3502
+ if (ch === "<") {
3503
+ const next = i + 1 < len ? xml[i + 1] : "";
3504
+ const isValidStart = NAME_START_CHAR_RE.test(next) || next === "/" || next === "!" || next === "?";
3505
+ const isIndexTag = !isValidStart && isIndexTagAt(xml, i);
3506
+ if (!(isValidStart || isIndexTag)) {
3507
+ out += "&lt;";
3508
+ continue;
3509
+ }
3510
+ }
3511
+ out += ch;
3512
+ }
3513
+ return out;
3514
+ }
3515
+ function balanceTags(xml) {
3516
+ const src = xml.replace(MALFORMED_CLOSE_RE_G, "</$1>").replace(STATUS_TO_STEP_BOUNDARY_RE, "</status></step><step>");
3517
+ let i = 0;
3518
+ const len = src.length;
3519
+ const out = [];
3520
+ const stack = [];
3521
+ while (i < len) {
3522
+ const lt = src.indexOf("<", i);
3523
+ if (lt === -1) {
3524
+ out.push(src.slice(i));
3525
+ break;
3526
+ }
3527
+ out.push(src.slice(i, lt));
3528
+ if (lt + 1 >= len) {
3529
+ break;
3530
+ }
3531
+ const next = src[lt + 1];
3532
+ if (next === "!" || next === "?") {
3533
+ i = handleSpecialTagSegment(src, lt, out);
3534
+ continue;
3535
+ }
3536
+ if (next === "/") {
3537
+ i = handleClosingTagSegment(src, lt, out, stack);
3538
+ continue;
3539
+ }
3540
+ i = handleOpeningTagSegment(src, lt, out, stack);
3541
+ }
3542
+ for (let k = stack.length - 1; k >= 0; k -= 1) {
3543
+ out.push(`</${stack[k]}>`);
3544
+ }
3545
+ return out.join("");
3546
+ }
3547
+ function skipWs(s, p, len) {
3548
+ let idx = p;
3549
+ while (idx < len && WHITESPACE_REGEX3.test(s[idx])) {
3550
+ idx += 1;
363
3551
  }
364
- closeTextBlock(state, controller);
365
- emitIncompleteToolCall(state, controller, toolCallStart);
366
- controller.enqueue(chunk);
3552
+ return idx;
367
3553
  }
368
- function publishText(text, state, controller) {
369
- if (state.isInsideToolCall) {
370
- closeTextBlock(state, controller);
371
- state.currentToolCallJson += text;
372
- } else if (text.length > 0) {
373
- if (!state.currentTextId) {
374
- state.currentTextId = generateId();
375
- controller.enqueue({
376
- type: "text-start",
377
- id: state.currentTextId
378
- });
379
- state.hasEmittedTextStart = true;
3554
+ function parseTagNameAt(s, p, len) {
3555
+ let idx = p;
3556
+ const start = idx;
3557
+ while (idx < len && NAME_CHAR_RE.test(s[idx])) {
3558
+ idx += 1;
3559
+ }
3560
+ return { name: s.slice(start, idx), pos: idx };
3561
+ }
3562
+ function handleSpecialTagSegment(src, lt, out) {
3563
+ const gt = src.indexOf(">", lt + 1);
3564
+ if (gt === -1) {
3565
+ out.push(src.slice(lt));
3566
+ return src.length;
3567
+ }
3568
+ out.push(src.slice(lt, gt + 1));
3569
+ return gt + 1;
3570
+ }
3571
+ function handleClosingTagSegment(src, lt, out, stack) {
3572
+ const len = src.length;
3573
+ let p = skipWs(src, lt + 2, len);
3574
+ const { name, pos } = parseTagNameAt(src, p, len);
3575
+ p = pos;
3576
+ const gt = src.indexOf(">", p);
3577
+ const closingText = gt === -1 ? src.slice(lt) : src.slice(lt, gt + 1);
3578
+ const idx = stack.lastIndexOf(name);
3579
+ if (idx !== -1) {
3580
+ for (let k = stack.length - 1; k > idx; k -= 1) {
3581
+ out.push(`</${stack[k]}>`);
3582
+ stack.pop();
380
3583
  }
381
- controller.enqueue({
382
- type: "text-delta",
383
- id: state.currentTextId,
384
- delta: text
385
- });
3584
+ out.push(closingText);
3585
+ stack.pop();
386
3586
  }
3587
+ return gt === -1 ? len : gt + 1;
387
3588
  }
388
- function emitToolCall(context) {
389
- var _a, _b;
390
- const { state, controller, toolCallStart, toolCallEnd, options } = context;
391
- try {
392
- const parsedToolCall = (0, import_rjson.parse)(state.currentToolCallJson);
393
- closeTextBlock(state, controller);
394
- controller.enqueue({
395
- type: "tool-call",
396
- toolCallId: generateId(),
397
- toolName: parsedToolCall.name,
398
- input: JSON.stringify((_a = parsedToolCall.arguments) != null ? _a : {})
399
- });
400
- } catch (error) {
401
- logParseFailure({
402
- phase: "stream",
403
- reason: "Failed to parse streaming tool call JSON segment",
404
- snippet: `${toolCallStart}${state.currentToolCallJson}${toolCallEnd}`,
405
- error
406
- });
407
- const errorId = generateId();
408
- const errorContent = `${toolCallStart}${state.currentToolCallJson}${toolCallEnd}`;
409
- controller.enqueue({
410
- type: "text-start",
411
- id: errorId
412
- });
413
- controller.enqueue({
414
- type: "text-delta",
415
- id: errorId,
416
- delta: errorContent
417
- });
418
- controller.enqueue({
419
- type: "text-end",
420
- id: errorId
421
- });
422
- (_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
423
- options,
424
- "Could not process streaming JSON tool call; emitting original text.",
425
- {
426
- toolCall: errorContent
427
- }
428
- );
3589
+ function handleOpeningTagSegment(src, lt, out, stack) {
3590
+ const len = src.length;
3591
+ let p = skipWs(src, lt + 1, len);
3592
+ const nameStart = p;
3593
+ const parsed = parseTagNameAt(src, p, len);
3594
+ p = parsed.pos;
3595
+ const name = src.slice(nameStart, p);
3596
+ const q = src.indexOf(">", p);
3597
+ if (q === -1) {
3598
+ out.push(src.slice(lt));
3599
+ return len;
429
3600
  }
3601
+ let r = q - 1;
3602
+ while (r >= nameStart && WHITESPACE_REGEX3.test(src[r])) {
3603
+ r -= 1;
3604
+ }
3605
+ const selfClosing = src[r] === "/";
3606
+ out.push(src.slice(lt, q + 1));
3607
+ if (!selfClosing && name) {
3608
+ stack.push(name);
3609
+ }
3610
+ return q + 1;
430
3611
  }
431
- function processTagMatch(context) {
432
- const { state } = context;
433
- if (state.isInsideToolCall) {
434
- emitToolCall(context);
435
- state.currentToolCallJson = "";
436
- state.isInsideToolCall = false;
437
- } else {
438
- state.currentToolCallJson = "";
439
- state.isInsideToolCall = true;
3612
+ function extractSchemaProperties(schema) {
3613
+ const unwrapped = unwrapJsonSchema(schema);
3614
+ if (!unwrapped || typeof unwrapped !== "object") {
3615
+ return void 0;
440
3616
  }
3617
+ return unwrapped.properties;
441
3618
  }
442
- function processBufferTags(context) {
443
- const { state, controller, toolCallStart, toolCallEnd } = context;
444
- let startIndex = getPotentialStartIndex(
445
- state.buffer,
446
- state.isInsideToolCall ? toolCallEnd : toolCallStart
447
- );
448
- while (startIndex != null) {
449
- const tag = state.isInsideToolCall ? toolCallEnd : toolCallStart;
450
- if (startIndex + tag.length > state.buffer.length) {
451
- break;
452
- }
453
- publishText(state.buffer.slice(0, startIndex), state, controller);
454
- state.buffer = state.buffer.slice(startIndex + tag.length);
455
- processTagMatch(context);
456
- startIndex = getPotentialStartIndex(
457
- state.buffer,
458
- state.isInsideToolCall ? toolCallEnd : toolCallStart
459
- );
3619
+ function shouldDeduplicateStringTags(schema) {
3620
+ const props = extractSchemaProperties(schema);
3621
+ if (!props) {
3622
+ return false;
460
3623
  }
3624
+ const commandRaw = props.command;
3625
+ if (!commandRaw) {
3626
+ return false;
3627
+ }
3628
+ const command = unwrapJsonSchema(commandRaw);
3629
+ return (command == null ? void 0 : command.type) === "array";
461
3630
  }
462
- function handlePartialTag(state, controller, toolCallStart) {
463
- if (state.isInsideToolCall) {
464
- return;
3631
+ function getStringPropertyNames(schema) {
3632
+ const props = extractSchemaProperties(schema);
3633
+ if (!props) {
3634
+ return [];
465
3635
  }
466
- const potentialIndex = getPotentialStartIndex(state.buffer, toolCallStart);
467
- if (potentialIndex != null && potentialIndex + toolCallStart.length > state.buffer.length) {
468
- publishText(state.buffer.slice(0, potentialIndex), state, controller);
469
- state.buffer = state.buffer.slice(potentialIndex);
470
- } else {
471
- publishText(state.buffer, state, controller);
472
- state.buffer = "";
3636
+ const names = [];
3637
+ for (const key of Object.keys(props)) {
3638
+ const prop = unwrapJsonSchema(props[key]);
3639
+ if ((prop == null ? void 0 : prop.type) === "string") {
3640
+ names.push(key);
3641
+ }
473
3642
  }
3643
+ return names;
474
3644
  }
475
- var jsonProtocol = ({
476
- toolCallStart = "<tool_call>",
477
- toolCallEnd = "</tool_call>"
478
- } = {}) => ({
479
- formatTools({
480
- tools,
481
- toolSystemPromptTemplate
482
- }) {
483
- return toolSystemPromptTemplate(tools || []);
484
- },
485
- formatToolCall(toolCall) {
486
- let args = {};
487
- if (toolCall.input != null) {
488
- try {
489
- args = JSON.parse(toolCall.input);
490
- } catch (e) {
491
- args = toolCall.input;
492
- }
3645
+ function escapeRegExp2(s) {
3646
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3647
+ }
3648
+ function dedupeSingleTag(xml, key) {
3649
+ var _a, _b;
3650
+ const escaped = escapeRegExp2(key);
3651
+ const re = new RegExp(`<${escaped}>([\\s\\S]*?)<\\/${escaped}>`, "g");
3652
+ const matches = Array.from(xml.matchAll(re));
3653
+ if (matches.length <= 1) {
3654
+ return xml;
3655
+ }
3656
+ const last = matches.at(-1);
3657
+ let result = "";
3658
+ let cursor = 0;
3659
+ for (const m of matches) {
3660
+ const idx = (_a = m.index) != null ? _a : 0;
3661
+ result += xml.slice(cursor, idx);
3662
+ if (last && idx === ((_b = last.index) != null ? _b : -1)) {
3663
+ result += m[0];
493
3664
  }
494
- return `${toolCallStart}${JSON.stringify({
495
- name: toolCall.toolName,
496
- arguments: args
497
- })}${toolCallEnd}`;
498
- },
499
- parseGeneratedText({
500
- text,
501
- options
502
- }) {
503
- const startEsc = escapeRegExp(toolCallStart);
504
- const endEsc = escapeRegExp(toolCallEnd);
505
- const toolCallRegex = new RegExp(
506
- `${startEsc}([\0-\uFFFF]*?)${endEsc}`,
507
- "gs"
508
- );
509
- const processedElements = [];
510
- let currentIndex = 0;
511
- let match = toolCallRegex.exec(text);
512
- while (match !== null) {
513
- currentIndex = processMatchedToolCall({
514
- match,
515
- text,
516
- currentIndex,
517
- processedElements,
518
- options
519
- });
520
- match = toolCallRegex.exec(text);
3665
+ cursor = idx + m[0].length;
3666
+ }
3667
+ result += xml.slice(cursor);
3668
+ return result;
3669
+ }
3670
+ function repairParsedAgainstSchema(input, schema) {
3671
+ if (!input || typeof input !== "object") {
3672
+ return input;
3673
+ }
3674
+ const properties = extractSchemaProperties(schema);
3675
+ if (!properties) {
3676
+ return input;
3677
+ }
3678
+ applySchemaProps(input, properties);
3679
+ return input;
3680
+ }
3681
+ function applySchemaProps(obj, properties) {
3682
+ for (const key of Object.keys(obj)) {
3683
+ const propSchema = properties[key];
3684
+ if (!propSchema) {
3685
+ continue;
521
3686
  }
522
- if (currentIndex < text.length) {
523
- const remainingText = text.substring(currentIndex);
524
- addTextSegment(remainingText, processedElements);
3687
+ const prop = unwrapJsonSchema(propSchema);
3688
+ if ((prop == null ? void 0 : prop.type) === "array" && prop.items) {
3689
+ const itemSchema = unwrapJsonSchema(prop.items);
3690
+ obj[key] = coerceArrayItems(obj[key], itemSchema);
3691
+ continue;
525
3692
  }
526
- return processedElements;
527
- },
528
- createStreamParser({
529
- options
530
- }) {
531
- const state = {
532
- isInsideToolCall: false,
533
- buffer: "",
534
- currentToolCallJson: "",
535
- currentTextId: null,
536
- hasEmittedTextStart: false
537
- };
538
- return new TransformStream({
539
- transform(chunk, controller) {
540
- var _a;
541
- if (chunk.type === "finish") {
542
- handleFinishChunk(state, controller, toolCallStart, chunk);
543
- return;
544
- }
545
- if (chunk.type !== "text-delta") {
546
- controller.enqueue(chunk);
547
- return;
548
- }
549
- const textContent = (_a = chunk.delta) != null ? _a : "";
550
- state.buffer += textContent;
551
- processBufferTags({
552
- state,
553
- controller,
554
- toolCallStart,
555
- toolCallEnd,
556
- options
557
- });
558
- handlePartialTag(state, controller, toolCallStart);
3693
+ if ((prop == null ? void 0 : prop.type) === "object") {
3694
+ const val = obj[key];
3695
+ if (val && typeof val === "object") {
3696
+ obj[key] = repairParsedAgainstSchema(val, prop);
559
3697
  }
560
- });
561
- },
562
- extractToolCallSegments({ text }) {
563
- const startEsc = escapeRegExp(toolCallStart);
564
- const endEsc = escapeRegExp(toolCallEnd);
565
- const regex = new RegExp(`${startEsc}([\0-\uFFFF]*?)${endEsc}`, "gs");
566
- const segments = [];
567
- let m = regex.exec(text);
568
- while (m != null) {
569
- segments.push(m[0]);
570
- m = regex.exec(text);
571
3698
  }
572
- return segments;
573
3699
  }
574
- });
575
-
576
- // src/core/protocols/protocol-interface.ts
577
- function isProtocolFactory(protocol) {
578
- return typeof protocol === "function";
579
3700
  }
580
- function isTCMProtocolFactory(protocol) {
581
- return typeof protocol === "function";
3701
+ function coerceArrayItems(val, itemSchema) {
3702
+ if (!Array.isArray(val)) {
3703
+ return val;
3704
+ }
3705
+ return val.map((v) => coerceArrayItem(v, itemSchema));
3706
+ }
3707
+ function coerceArrayItem(v, itemSchema) {
3708
+ const itemType = itemSchema == null ? void 0 : itemSchema.type;
3709
+ if (typeof v === "string" && itemType === "object") {
3710
+ const parsed = tryParseStringToSchemaObject(v, itemSchema);
3711
+ if (parsed !== null) {
3712
+ return parsed;
3713
+ }
3714
+ const fallback = extractStepStatusFromString(
3715
+ v.replace(MALFORMED_CLOSE_RE_G, "</$1>")
3716
+ );
3717
+ if (fallback) {
3718
+ return fallback;
3719
+ }
3720
+ return v;
3721
+ }
3722
+ if (v && typeof v === "object" && itemType === "object") {
3723
+ return repairParsedAgainstSchema(v, itemSchema);
3724
+ }
3725
+ return v;
3726
+ }
3727
+ function tryParseStringToSchemaObject(xml, itemSchema) {
3728
+ try {
3729
+ const normalized = xml.replace(MALFORMED_CLOSE_RE_G, "</$1>");
3730
+ const fixed = parse2(normalized, itemSchema, { noChildNodes: [] });
3731
+ return typeof fixed === "string" ? null : fixed;
3732
+ } catch (e) {
3733
+ return null;
3734
+ }
3735
+ }
3736
+ function extractStepStatusFromString(normXml) {
3737
+ const stepMatch = normXml.match(STEP_TAG_RE);
3738
+ const statusMatch = normXml.match(STATUS_TAG_RE);
3739
+ if (stepMatch && statusMatch) {
3740
+ return { step: stepMatch[1], status: statusMatch[1] };
3741
+ }
3742
+ return null;
3743
+ }
3744
+
3745
+ // src/rxml/parse.ts
3746
+ function parse3(xml, schema, options = {}) {
3747
+ if (!options.repair) {
3748
+ return parse2(xml, schema, options);
3749
+ }
3750
+ const baseOptions = {
3751
+ ...options,
3752
+ repair: false
3753
+ };
3754
+ const ctx = createIntermediateCall("", xml, schema);
3755
+ const result = applyHeuristicPipeline(ctx, defaultPipelineConfig, {
3756
+ parse: (raw, s) => parse2(raw, s, baseOptions),
3757
+ onError: options.onError,
3758
+ maxReparses: options.maxReparses
3759
+ });
3760
+ if (result.parsed !== null) {
3761
+ return result.parsed;
3762
+ }
3763
+ const error = result.errors[0];
3764
+ throw new RXMLParseError("Failed to parse XML with repair heuristics", error);
582
3765
  }
583
3766
 
584
3767
  // src/core/protocols/xml-protocol.ts
585
- var import_rxml = require("@ai-sdk-tool/rxml");
586
- var NAME_CHAR_RE = /[A-Za-z0-9_:-]/;
587
- var WHITESPACE_REGEX = /\s/;
3768
+ var NAME_CHAR_RE2 = /[A-Za-z0-9_:-]/;
3769
+ var WHITESPACE_REGEX4 = /\s/;
588
3770
  function getToolSchema(tools, toolName) {
589
3771
  var _a;
590
3772
  return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
@@ -598,7 +3780,7 @@ function processToolCall(params) {
598
3780
  onError: (_a = options == null ? void 0 : options.onError) != null ? _a : parseOptions == null ? void 0 : parseOptions.onError
599
3781
  };
600
3782
  try {
601
- const parsed = (0, import_rxml.parse)(toolCall.content, toolSchema, parseConfig);
3783
+ const parsed = parse3(toolCall.content, toolSchema, parseConfig);
602
3784
  processedElements.push({
603
3785
  type: "tool-call",
604
3786
  toolCallId: generateId(),
@@ -636,7 +3818,7 @@ function handleStreamingToolCallEnd(params) {
636
3818
  };
637
3819
  flushText(ctrl);
638
3820
  try {
639
- const parsedResult = (0, import_rxml.parse)(toolContent, toolSchema, parseConfig);
3821
+ const parsedResult = parse3(toolContent, toolSchema, parseConfig);
640
3822
  ctrl.enqueue({
641
3823
  type: "tool-call",
642
3824
  toolCallId: generateId(),
@@ -686,11 +3868,11 @@ function consumeClosingTag(text, lt) {
686
3868
  }
687
3869
  function consumeOpenTag(text, lt) {
688
3870
  let p = lt + 1;
689
- while (p < text.length && WHITESPACE_REGEX.test(text[p])) {
3871
+ while (p < text.length && WHITESPACE_REGEX4.test(text[p])) {
690
3872
  p += 1;
691
3873
  }
692
3874
  const nameStart = p;
693
- while (p < text.length && NAME_CHAR_RE.test(text.charAt(p))) {
3875
+ while (p < text.length && NAME_CHAR_RE2.test(text.charAt(p))) {
694
3876
  p += 1;
695
3877
  }
696
3878
  const name = text.slice(nameStart, p);
@@ -699,7 +3881,7 @@ function consumeOpenTag(text, lt) {
699
3881
  return null;
700
3882
  }
701
3883
  let r = q - 1;
702
- while (r >= nameStart && WHITESPACE_REGEX.test(text[r])) {
3884
+ while (r >= nameStart && WHITESPACE_REGEX4.test(text[r])) {
703
3885
  r -= 1;
704
3886
  }
705
3887
  const selfClosing = text[r] === "/";
@@ -728,11 +3910,11 @@ function nextTagToken(text, fromPos) {
728
3910
  if (next === "/") {
729
3911
  const closing = consumeClosingTag(text, lt);
730
3912
  let p = lt + 2;
731
- while (p < text.length && WHITESPACE_REGEX.test(text[p])) {
3913
+ while (p < text.length && WHITESPACE_REGEX4.test(text[p])) {
732
3914
  p += 1;
733
3915
  }
734
3916
  const nameStart = p;
735
- while (p < text.length && NAME_CHAR_RE.test(text.charAt(p))) {
3917
+ while (p < text.length && NAME_CHAR_RE2.test(text.charAt(p))) {
736
3918
  p += 1;
737
3919
  }
738
3920
  const name = text.slice(nameStart, p);
@@ -903,7 +4085,7 @@ function isOpenTagPrefix(suffix, toolName) {
903
4085
  }
904
4086
  function consumeWhitespace(text, index) {
905
4087
  let i = index;
906
- while (i < text.length && WHITESPACE_REGEX.test(text.charAt(i))) {
4088
+ while (i < text.length && WHITESPACE_REGEX4.test(text.charAt(i))) {
907
4089
  i += 1;
908
4090
  }
909
4091
  return i;
@@ -1174,7 +4356,7 @@ var xmlProtocol = (protocolOptions) => {
1174
4356
  args = toolCall.input;
1175
4357
  }
1176
4358
  }
1177
- return (0, import_rxml.stringify)(toolCall.toolName, args, {
4359
+ return stringify(toolCall.toolName, args, {
1178
4360
  suppressEmptyNode: false,
1179
4361
  format: true,
1180
4362
  minimalEscaping: true
@@ -1292,8 +4474,8 @@ var xmlProtocol = (protocolOptions) => {
1292
4474
 
1293
4475
  // src/core/protocols/yaml-protocol.ts
1294
4476
  var import_yaml = __toESM(require("yaml"), 1);
1295
- var NAME_CHAR_RE2 = /[A-Za-z0-9_:-]/;
1296
- var WHITESPACE_REGEX2 = /\s/;
4477
+ var NAME_CHAR_RE3 = /[A-Za-z0-9_:-]/;
4478
+ var WHITESPACE_REGEX5 = /\s/;
1297
4479
  var LEADING_WHITESPACE_RE = /^(\s*)/;
1298
4480
  function findClosingTagEnd(text, contentStart, toolName) {
1299
4481
  let pos = contentStart;
@@ -1310,11 +4492,11 @@ function findClosingTagEnd(text, contentStart, toolName) {
1310
4492
  break;
1311
4493
  }
1312
4494
  let p = ltIdx + 2;
1313
- while (p < gtIdx && WHITESPACE_REGEX2.test(text[p])) {
4495
+ while (p < gtIdx && WHITESPACE_REGEX5.test(text[p])) {
1314
4496
  p++;
1315
4497
  }
1316
4498
  const nameStart = p;
1317
- while (p < gtIdx && NAME_CHAR_RE2.test(text.charAt(p))) {
4499
+ while (p < gtIdx && NAME_CHAR_RE3.test(text.charAt(p))) {
1318
4500
  p++;
1319
4501
  }
1320
4502
  const name = text.slice(nameStart, p);
@@ -1330,11 +4512,11 @@ function findClosingTagEnd(text, contentStart, toolName) {
1330
4512
  pos = gtIdx === -1 ? text.length : gtIdx + 1;
1331
4513
  } else {
1332
4514
  let p = ltIdx + 1;
1333
- while (p < text.length && WHITESPACE_REGEX2.test(text[p])) {
4515
+ while (p < text.length && WHITESPACE_REGEX5.test(text[p])) {
1334
4516
  p++;
1335
4517
  }
1336
4518
  const nameStart = p;
1337
- while (p < text.length && NAME_CHAR_RE2.test(text.charAt(p))) {
4519
+ while (p < text.length && NAME_CHAR_RE3.test(text.charAt(p))) {
1338
4520
  p++;
1339
4521
  }
1340
4522
  const name = text.slice(nameStart, p);
@@ -1343,7 +4525,7 @@ function findClosingTagEnd(text, contentStart, toolName) {
1343
4525
  break;
1344
4526
  }
1345
4527
  let r = gtIdx - 1;
1346
- while (r >= nameStart && WHITESPACE_REGEX2.test(text[r])) {
4528
+ while (r >= nameStart && WHITESPACE_REGEX5.test(text[r])) {
1347
4529
  r--;
1348
4530
  }
1349
4531
  const selfClosing = text[r] === "/";
@@ -1825,30 +5007,14 @@ function decodeOriginalTools(originalTools) {
1825
5007
  })
1826
5008
  );
1827
5009
  }
1828
- function extractToolNamesFromOriginalTools(originalTools) {
1829
- return (originalTools == null ? void 0 : originalTools.map((t) => t.name)) || [];
1830
- }
1831
5010
  function isToolChoiceActive(params) {
1832
5011
  var _a, _b, _c;
1833
5012
  const toolChoice = (_b = (_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware) == null ? void 0 : _b.toolChoice;
1834
5013
  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"));
1835
5014
  }
1836
5015
 
1837
- // src/core/utils/type-guards.ts
1838
- function isToolResultPart(content) {
1839
- if (!content || typeof content !== "object") {
1840
- return false;
1841
- }
1842
- const c = content;
1843
- return c.type === "tool-result" && typeof c.toolName === "string" && typeof c.toolCallId === "string" && "output" in c;
1844
- }
1845
- function hasInputProperty(obj) {
1846
- return typeof obj === "object" && obj !== null && "input" in obj;
1847
- }
1848
-
1849
5016
  // src/generate-handler.ts
1850
5017
  var import_provider_utils = require("@ai-sdk/provider-utils");
1851
- var import_schema_coerce = require("@ai-sdk-tool/schema-coerce");
1852
5018
  function parseToolChoiceJson(text, providerOptions) {
1853
5019
  var _a;
1854
5020
  try {
@@ -2010,7 +5176,7 @@ function fixToolCallWithSchema(part, tools) {
2010
5176
  args = part.input;
2011
5177
  }
2012
5178
  const schema = (_a = tools.find((t) => t.name === part.toolName)) == null ? void 0 : _a.inputSchema;
2013
- const coerced = (0, import_schema_coerce.coerceBySchema)(args, schema);
5179
+ const coerced = coerceBySchema(args, schema);
2014
5180
  return {
2015
5181
  ...part,
2016
5182
  input: JSON.stringify(coerced != null ? coerced : {})