@ai-sdk-tool/parser 3.1.2 → 3.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-TQT6XSP7.js → chunk-3KQVEBKO.js} +26 -688
- package/dist/chunk-3KQVEBKO.js.map +1 -0
- package/dist/community.cjs +101 -649
- package/dist/community.cjs.map +1 -1
- package/dist/community.js +1 -1
- package/dist/index.cjs +19 -684
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -108
- package/dist/index.d.ts +2 -108
- package/dist/index.js +1 -7
- package/package.json +10 -9
- package/dist/chunk-TQT6XSP7.js.map +0 -1
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
export * from "@ai-sdk-tool/rjson";
|
|
3
|
+
|
|
1
4
|
// src/core/heuristics/engine.ts
|
|
2
5
|
function applyRawSegmentUpdate(current, result) {
|
|
3
6
|
if (result.rawSegment !== void 0) {
|
|
@@ -23,12 +26,12 @@ function applyWarningsUpdate(current, result) {
|
|
|
23
26
|
}
|
|
24
27
|
return current;
|
|
25
28
|
}
|
|
26
|
-
function attemptReparse(current, result, reparseCount, maxReparses,
|
|
29
|
+
function attemptReparse(current, result, reparseCount, maxReparses, parse3) {
|
|
27
30
|
if (!result.reparse || result.rawSegment === void 0 || reparseCount >= maxReparses) {
|
|
28
31
|
return { state: current, newCount: reparseCount };
|
|
29
32
|
}
|
|
30
33
|
try {
|
|
31
|
-
const reparsed =
|
|
34
|
+
const reparsed = parse3(result.rawSegment, current.schema);
|
|
32
35
|
return {
|
|
33
36
|
state: { ...current, parsed: reparsed, errors: [] },
|
|
34
37
|
newCount: reparseCount + 1
|
|
@@ -465,6 +468,9 @@ function extractStepStatusFromString(normXml) {
|
|
|
465
468
|
return null;
|
|
466
469
|
}
|
|
467
470
|
|
|
471
|
+
// src/core/protocols/json-protocol.ts
|
|
472
|
+
import { parse as parseRJSON } from "@ai-sdk-tool/rjson";
|
|
473
|
+
|
|
468
474
|
// src/core/utils/debug.ts
|
|
469
475
|
var LINE_SPLIT_REGEX = /\r?\n/;
|
|
470
476
|
function normalizeBooleanString(value) {
|
|
@@ -648,676 +654,11 @@ function escapeRegExp2(literal) {
|
|
|
648
654
|
return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
649
655
|
}
|
|
650
656
|
|
|
651
|
-
// src/core/utils/robust-json.ts
|
|
652
|
-
var WHITESPACE_TEST_REGEX = /\s/;
|
|
653
|
-
var WHITESPACE_REGEX2 = /^\s+/;
|
|
654
|
-
var OBJECT_START_REGEX = /^\{/;
|
|
655
|
-
var OBJECT_END_REGEX = /^\}/;
|
|
656
|
-
var ARRAY_START_REGEX = /^\[/;
|
|
657
|
-
var ARRAY_END_REGEX = /^\]/;
|
|
658
|
-
var COMMA_REGEX = /^,/;
|
|
659
|
-
var COLON_REGEX = /^:/;
|
|
660
|
-
var KEYWORD_REGEX = /^(?:true|false|null)/;
|
|
661
|
-
var NUMBER_REGEX = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/;
|
|
662
|
-
var STRING_DOUBLE_REGEX = /^"(?:[^"\\]|\\["bnrtf\\/]|\\u[0-9a-fA-F]{4})*"/;
|
|
663
|
-
var STRING_SINGLE_REGEX = /^'((?:[^'\\]|\\['bnrtf\\/]|\\u[0-9a-fA-F]{4})*)'/;
|
|
664
|
-
var COMMENT_SINGLE_REGEX = /^\/\/.*?(?:\r\n|\r|\n)/;
|
|
665
|
-
var COMMENT_MULTI_REGEX = /^\/\*[\s\S]*?\*\//;
|
|
666
|
-
var IDENTIFIER_REGEX = /^[$a-zA-Z0-9_\-+.*?!|&%^/#\\]+/;
|
|
667
|
-
function some(array, f) {
|
|
668
|
-
let acc = false;
|
|
669
|
-
for (let i = 0; i < array.length; i += 1) {
|
|
670
|
-
const result = f(array[i], i, array);
|
|
671
|
-
acc = result === void 0 ? false : result;
|
|
672
|
-
if (acc) {
|
|
673
|
-
return acc;
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
return acc;
|
|
677
|
-
}
|
|
678
|
-
function makeLexer(tokenSpecs) {
|
|
679
|
-
return (contents) => {
|
|
680
|
-
const tokens = [];
|
|
681
|
-
let line = 1;
|
|
682
|
-
let remainingContents = contents;
|
|
683
|
-
function findToken() {
|
|
684
|
-
const result = some(tokenSpecs, (tokenSpec) => {
|
|
685
|
-
const m = tokenSpec.re.exec(remainingContents);
|
|
686
|
-
if (m) {
|
|
687
|
-
const raw = m[0];
|
|
688
|
-
remainingContents = remainingContents.slice(raw.length);
|
|
689
|
-
return {
|
|
690
|
-
raw,
|
|
691
|
-
matched: tokenSpec.f(m)
|
|
692
|
-
// Process the match using the spec's function
|
|
693
|
-
};
|
|
694
|
-
}
|
|
695
|
-
return;
|
|
696
|
-
});
|
|
697
|
-
return result === false ? void 0 : result;
|
|
698
|
-
}
|
|
699
|
-
while (remainingContents !== "") {
|
|
700
|
-
const matched = findToken();
|
|
701
|
-
if (!matched) {
|
|
702
|
-
const err = new SyntaxError(
|
|
703
|
-
`Unexpected character: ${remainingContents[0]}; input: ${remainingContents.substr(
|
|
704
|
-
0,
|
|
705
|
-
100
|
|
706
|
-
)}`
|
|
707
|
-
);
|
|
708
|
-
err.line = line;
|
|
709
|
-
throw err;
|
|
710
|
-
}
|
|
711
|
-
const tokenWithLine = matched.matched;
|
|
712
|
-
tokenWithLine.line = line;
|
|
713
|
-
line += matched.raw.replace(/[^\n]/g, "").length;
|
|
714
|
-
tokens.push(tokenWithLine);
|
|
715
|
-
}
|
|
716
|
-
return tokens;
|
|
717
|
-
};
|
|
718
|
-
}
|
|
719
|
-
function fStringSingle(m) {
|
|
720
|
-
const content = m[1].replace(
|
|
721
|
-
/([^'\\]|\\['bnrtf\\]|\\u[0-9a-fA-F]{4})/g,
|
|
722
|
-
(mm) => {
|
|
723
|
-
if (mm === '"') {
|
|
724
|
-
return '\\"';
|
|
725
|
-
}
|
|
726
|
-
if (mm === "\\'") {
|
|
727
|
-
return "'";
|
|
728
|
-
}
|
|
729
|
-
return mm;
|
|
730
|
-
}
|
|
731
|
-
);
|
|
732
|
-
const match = `"${content}"`;
|
|
733
|
-
return {
|
|
734
|
-
type: "string",
|
|
735
|
-
match,
|
|
736
|
-
// The transformed, double-quoted string representation
|
|
737
|
-
// Use JSON.parse on the transformed string to handle escape sequences correctly
|
|
738
|
-
value: JSON.parse(match)
|
|
739
|
-
};
|
|
740
|
-
}
|
|
741
|
-
function fStringDouble(m) {
|
|
742
|
-
return {
|
|
743
|
-
type: "string",
|
|
744
|
-
match: m[0],
|
|
745
|
-
// The raw matched string (including quotes)
|
|
746
|
-
value: JSON.parse(m[0])
|
|
747
|
-
// Use JSON.parse to handle escapes and get the value
|
|
748
|
-
};
|
|
749
|
-
}
|
|
750
|
-
function fIdentifier(m) {
|
|
751
|
-
const value = m[0];
|
|
752
|
-
const match = '"' + value.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + // Escape backslashes and quotes
|
|
753
|
-
'"';
|
|
754
|
-
return {
|
|
755
|
-
type: "string",
|
|
756
|
-
// Treat identifiers as strings
|
|
757
|
-
value,
|
|
758
|
-
// The original identifier name
|
|
759
|
-
match
|
|
760
|
-
// The double-quoted string representation
|
|
761
|
-
};
|
|
762
|
-
}
|
|
763
|
-
function fComment(m) {
|
|
764
|
-
const match = m[0].replace(
|
|
765
|
-
/./g,
|
|
766
|
-
(c) => WHITESPACE_TEST_REGEX.test(c) ? c : " "
|
|
767
|
-
);
|
|
768
|
-
return {
|
|
769
|
-
type: " ",
|
|
770
|
-
// Represent comments as whitespace tokens
|
|
771
|
-
match,
|
|
772
|
-
// String containing original newlines and spaces for other chars
|
|
773
|
-
value: void 0
|
|
774
|
-
// Comments don't have a semantic value
|
|
775
|
-
};
|
|
776
|
-
}
|
|
777
|
-
function fNumber(m) {
|
|
778
|
-
return {
|
|
779
|
-
type: "number",
|
|
780
|
-
match: m[0],
|
|
781
|
-
// The raw matched number string
|
|
782
|
-
value: Number.parseFloat(m[0])
|
|
783
|
-
// Convert string to number
|
|
784
|
-
};
|
|
785
|
-
}
|
|
786
|
-
function fKeyword(m) {
|
|
787
|
-
let value;
|
|
788
|
-
switch (m[0]) {
|
|
789
|
-
case "null":
|
|
790
|
-
value = null;
|
|
791
|
-
break;
|
|
792
|
-
case "true":
|
|
793
|
-
value = true;
|
|
794
|
-
break;
|
|
795
|
-
case "false":
|
|
796
|
-
value = false;
|
|
797
|
-
break;
|
|
798
|
-
default:
|
|
799
|
-
throw new Error(`Unexpected keyword: ${m[0]}`);
|
|
800
|
-
}
|
|
801
|
-
return {
|
|
802
|
-
type: "atom",
|
|
803
|
-
// Use 'atom' type for these literals
|
|
804
|
-
match: m[0],
|
|
805
|
-
// The raw matched keyword
|
|
806
|
-
value
|
|
807
|
-
// The corresponding JavaScript value
|
|
808
|
-
};
|
|
809
|
-
}
|
|
810
|
-
function makeTokenSpecs(relaxed) {
|
|
811
|
-
function f(type) {
|
|
812
|
-
return (m) => {
|
|
813
|
-
return { type, match: m[0], value: void 0 };
|
|
814
|
-
};
|
|
815
|
-
}
|
|
816
|
-
let tokenSpecs = [
|
|
817
|
-
{ re: WHITESPACE_REGEX2, f: f(" ") },
|
|
818
|
-
// Whitespace
|
|
819
|
-
{ re: OBJECT_START_REGEX, f: f("{") },
|
|
820
|
-
// Object start
|
|
821
|
-
{ re: OBJECT_END_REGEX, f: f("}") },
|
|
822
|
-
// Object end
|
|
823
|
-
{ re: ARRAY_START_REGEX, f: f("[") },
|
|
824
|
-
// Array start
|
|
825
|
-
{ re: ARRAY_END_REGEX, f: f("]") },
|
|
826
|
-
// Array end
|
|
827
|
-
{ re: COMMA_REGEX, f: f(",") },
|
|
828
|
-
// Comma separator
|
|
829
|
-
{ re: COLON_REGEX, f: f(":") },
|
|
830
|
-
// Key-value separator
|
|
831
|
-
{ re: KEYWORD_REGEX, f: fKeyword },
|
|
832
|
-
// Keywords
|
|
833
|
-
// Number: optional sign, digits, optional decimal part, optional exponent
|
|
834
|
-
{ re: NUMBER_REGEX, f: fNumber },
|
|
835
|
-
// String: double-quoted, handles escapes
|
|
836
|
-
{ re: STRING_DOUBLE_REGEX, f: fStringDouble }
|
|
837
|
-
];
|
|
838
|
-
if (relaxed) {
|
|
839
|
-
tokenSpecs = tokenSpecs.concat([
|
|
840
|
-
// Single-quoted strings
|
|
841
|
-
{
|
|
842
|
-
re: STRING_SINGLE_REGEX,
|
|
843
|
-
f: fStringSingle
|
|
844
|
-
},
|
|
845
|
-
// Single-line comments (// ...)
|
|
846
|
-
{ re: COMMENT_SINGLE_REGEX, f: fComment },
|
|
847
|
-
// Multi-line comments (/* ... */)
|
|
848
|
-
{ re: COMMENT_MULTI_REGEX, f: fComment },
|
|
849
|
-
// Unquoted identifiers (treated as strings)
|
|
850
|
-
// Allows letters, numbers, _, -, +, ., *, ?, !, |, &, %, ^, /, #, \
|
|
851
|
-
{ re: IDENTIFIER_REGEX, f: fIdentifier }
|
|
852
|
-
// Note: The order matters here. Identifiers are checked after keywords/numbers.
|
|
853
|
-
]);
|
|
854
|
-
}
|
|
855
|
-
return tokenSpecs;
|
|
856
|
-
}
|
|
857
|
-
var lexer = makeLexer(makeTokenSpecs(true));
|
|
858
|
-
var strictLexer = makeLexer(makeTokenSpecs(false));
|
|
859
|
-
function previousNWSToken(tokens, index) {
|
|
860
|
-
let currentIndex = index;
|
|
861
|
-
for (; currentIndex >= 0; currentIndex -= 1) {
|
|
862
|
-
if (tokens[currentIndex].type !== " ") {
|
|
863
|
-
return currentIndex;
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
return;
|
|
867
|
-
}
|
|
868
|
-
function stripTrailingComma(tokens) {
|
|
869
|
-
const res = [];
|
|
870
|
-
tokens.forEach((token, index) => {
|
|
871
|
-
if (index > 0 && (token.type === "]" || token.type === "}")) {
|
|
872
|
-
const prevNWSTokenIndex = previousNWSToken(res, res.length - 1);
|
|
873
|
-
if (prevNWSTokenIndex !== void 0 && res[prevNWSTokenIndex].type === ",") {
|
|
874
|
-
const preCommaIndex = previousNWSToken(res, prevNWSTokenIndex - 1);
|
|
875
|
-
if (preCommaIndex !== void 0 && res[preCommaIndex].type !== "[" && res[preCommaIndex].type !== "{") {
|
|
876
|
-
res[prevNWSTokenIndex] = {
|
|
877
|
-
type: " ",
|
|
878
|
-
match: " ",
|
|
879
|
-
// Represent as a single space
|
|
880
|
-
value: void 0,
|
|
881
|
-
// Whitespace has no value
|
|
882
|
-
line: res[prevNWSTokenIndex].line
|
|
883
|
-
// Preserve original line number
|
|
884
|
-
};
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
res.push(token);
|
|
889
|
-
});
|
|
890
|
-
return res;
|
|
891
|
-
}
|
|
892
|
-
function transform(text) {
|
|
893
|
-
let tokens = lexer(text);
|
|
894
|
-
tokens = stripTrailingComma(tokens);
|
|
895
|
-
return tokens.reduce((str, token) => str + token.match, "");
|
|
896
|
-
}
|
|
897
|
-
function popToken(tokens, state) {
|
|
898
|
-
var _a, _b;
|
|
899
|
-
const token = tokens[state.pos];
|
|
900
|
-
state.pos += 1;
|
|
901
|
-
if (!token) {
|
|
902
|
-
const lastLine = tokens.length !== 0 ? (_b = (_a = tokens.at(-1)) == null ? void 0 : _a.line) != null ? _b : 1 : 1;
|
|
903
|
-
return { type: "eof", match: "", value: void 0, line: lastLine };
|
|
904
|
-
}
|
|
905
|
-
return token;
|
|
906
|
-
}
|
|
907
|
-
function strToken(token) {
|
|
908
|
-
switch (token.type) {
|
|
909
|
-
case "atom":
|
|
910
|
-
case "string":
|
|
911
|
-
case "number":
|
|
912
|
-
return `${token.type} ${token.match}`;
|
|
913
|
-
case "eof":
|
|
914
|
-
return "end-of-file";
|
|
915
|
-
default:
|
|
916
|
-
return `'${token.type}'`;
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
function skipColon(tokens, state) {
|
|
920
|
-
const colon = popToken(tokens, state);
|
|
921
|
-
if (colon.type !== ":") {
|
|
922
|
-
const message = `Unexpected token: ${strToken(colon)}, expected ':'`;
|
|
923
|
-
if (state.tolerant) {
|
|
924
|
-
state.warnings.push({
|
|
925
|
-
message,
|
|
926
|
-
line: colon.line
|
|
927
|
-
});
|
|
928
|
-
state.pos -= 1;
|
|
929
|
-
} else {
|
|
930
|
-
const err = new SyntaxError(message);
|
|
931
|
-
err.line = colon.line;
|
|
932
|
-
throw err;
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
function skipPunctuation(tokens, state, valid) {
|
|
937
|
-
const punctuation = [",", ":", "]", "}"];
|
|
938
|
-
let token = popToken(tokens, state);
|
|
939
|
-
while (true) {
|
|
940
|
-
if (valid == null ? void 0 : valid.includes(token.type)) {
|
|
941
|
-
return token;
|
|
942
|
-
}
|
|
943
|
-
if (token.type === "eof") {
|
|
944
|
-
return token;
|
|
945
|
-
}
|
|
946
|
-
if (punctuation.includes(token.type)) {
|
|
947
|
-
const message = `Unexpected token: ${strToken(
|
|
948
|
-
token
|
|
949
|
-
)}, expected '[', '{', number, string or atom`;
|
|
950
|
-
if (state.tolerant) {
|
|
951
|
-
state.warnings.push({
|
|
952
|
-
message,
|
|
953
|
-
line: token.line
|
|
954
|
-
});
|
|
955
|
-
token = popToken(tokens, state);
|
|
956
|
-
} else {
|
|
957
|
-
const err = new SyntaxError(message);
|
|
958
|
-
err.line = token.line;
|
|
959
|
-
throw err;
|
|
960
|
-
}
|
|
961
|
-
} else {
|
|
962
|
-
return token;
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
function raiseError(state, token, message) {
|
|
967
|
-
if (state.tolerant) {
|
|
968
|
-
state.warnings.push({
|
|
969
|
-
message,
|
|
970
|
-
line: token.line
|
|
971
|
-
});
|
|
972
|
-
} else {
|
|
973
|
-
const err = new SyntaxError(message);
|
|
974
|
-
err.line = token.line;
|
|
975
|
-
throw err;
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
function raiseUnexpected(state, token, expected) {
|
|
979
|
-
raiseError(
|
|
980
|
-
state,
|
|
981
|
-
token,
|
|
982
|
-
`Unexpected token: ${strToken(token)}, expected ${expected}`
|
|
983
|
-
);
|
|
984
|
-
}
|
|
985
|
-
function checkDuplicates(state, obj, token) {
|
|
986
|
-
const key = String(token.value);
|
|
987
|
-
if (!state.duplicate && Object.hasOwn(obj, key)) {
|
|
988
|
-
raiseError(state, token, `Duplicate key: ${key}`);
|
|
989
|
-
}
|
|
990
|
-
}
|
|
991
|
-
function appendPair(state, obj, key, value) {
|
|
992
|
-
const finalValue = state.reviver ? state.reviver(key, value) : value;
|
|
993
|
-
if (finalValue !== void 0) {
|
|
994
|
-
obj[key] = finalValue;
|
|
995
|
-
}
|
|
996
|
-
}
|
|
997
|
-
function parsePair(tokens, state, obj) {
|
|
998
|
-
let token = skipPunctuation(tokens, state, [":", "string", "number", "atom"]);
|
|
999
|
-
let value;
|
|
1000
|
-
if (token.type !== "string") {
|
|
1001
|
-
raiseUnexpected(state, token, "string key");
|
|
1002
|
-
if (state.tolerant) {
|
|
1003
|
-
switch (token.type) {
|
|
1004
|
-
case ":":
|
|
1005
|
-
token = {
|
|
1006
|
-
type: "string",
|
|
1007
|
-
value: "null",
|
|
1008
|
-
match: '"null"',
|
|
1009
|
-
line: token.line
|
|
1010
|
-
};
|
|
1011
|
-
state.pos -= 1;
|
|
1012
|
-
break;
|
|
1013
|
-
case "number":
|
|
1014
|
-
// Use number as string key
|
|
1015
|
-
case "atom":
|
|
1016
|
-
token = {
|
|
1017
|
-
type: "string",
|
|
1018
|
-
value: String(token.value),
|
|
1019
|
-
match: `"${token.value}"`,
|
|
1020
|
-
line: token.line
|
|
1021
|
-
};
|
|
1022
|
-
break;
|
|
1023
|
-
case "[":
|
|
1024
|
-
// Assume missing key before an array
|
|
1025
|
-
case "{":
|
|
1026
|
-
state.pos -= 1;
|
|
1027
|
-
value = parseAny(tokens, state);
|
|
1028
|
-
checkDuplicates(state, obj, {
|
|
1029
|
-
type: "string",
|
|
1030
|
-
value: "null",
|
|
1031
|
-
match: '"null"',
|
|
1032
|
-
line: token.line
|
|
1033
|
-
});
|
|
1034
|
-
appendPair(state, obj, "null", value);
|
|
1035
|
-
return;
|
|
1036
|
-
// Finished parsing this "pair"
|
|
1037
|
-
case "eof":
|
|
1038
|
-
return;
|
|
1039
|
-
// Cannot recover
|
|
1040
|
-
default:
|
|
1041
|
-
return;
|
|
1042
|
-
}
|
|
1043
|
-
} else {
|
|
1044
|
-
return;
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
checkDuplicates(state, obj, token);
|
|
1048
|
-
const key = String(token.value);
|
|
1049
|
-
skipColon(tokens, state);
|
|
1050
|
-
value = parseAny(tokens, state);
|
|
1051
|
-
appendPair(state, obj, key, value);
|
|
1052
|
-
}
|
|
1053
|
-
function parseElement(tokens, state, arr) {
|
|
1054
|
-
const key = arr.length;
|
|
1055
|
-
const value = parseAny(tokens, state);
|
|
1056
|
-
arr[key] = state.reviver ? state.reviver(String(key), value) : value;
|
|
1057
|
-
}
|
|
1058
|
-
function parseObject(tokens, state) {
|
|
1059
|
-
const obj = {};
|
|
1060
|
-
return parseMany(tokens, state, obj, {
|
|
1061
|
-
skip: [":", "}"],
|
|
1062
|
-
// Initially skip over colon or closing brace (for empty/tolerant cases)
|
|
1063
|
-
elementParser: parsePair,
|
|
1064
|
-
// Use parsePair to parse each key-value element
|
|
1065
|
-
elementName: "string key",
|
|
1066
|
-
// Expected element type for errors
|
|
1067
|
-
endSymbol: "}"
|
|
1068
|
-
// The closing token for an object
|
|
1069
|
-
});
|
|
1070
|
-
}
|
|
1071
|
-
function parseArray(tokens, state) {
|
|
1072
|
-
const arr = [];
|
|
1073
|
-
return parseMany(tokens, state, arr, {
|
|
1074
|
-
skip: ["]"],
|
|
1075
|
-
// Initially skip over closing bracket (for empty/tolerant cases)
|
|
1076
|
-
elementParser: parseElement,
|
|
1077
|
-
// Use parseElement to parse each array item
|
|
1078
|
-
elementName: "json value",
|
|
1079
|
-
// Expected element type for errors
|
|
1080
|
-
endSymbol: "]"
|
|
1081
|
-
// The closing token for an array
|
|
1082
|
-
});
|
|
1083
|
-
}
|
|
1084
|
-
function handleInvalidToken(token, state, opts, result) {
|
|
1085
|
-
raiseUnexpected(state, token, `',' or '${opts.endSymbol}'`);
|
|
1086
|
-
if (state.tolerant) {
|
|
1087
|
-
if (token.type === "eof") {
|
|
1088
|
-
return result;
|
|
1089
|
-
}
|
|
1090
|
-
state.pos -= 1;
|
|
1091
|
-
return null;
|
|
1092
|
-
}
|
|
1093
|
-
return result;
|
|
1094
|
-
}
|
|
1095
|
-
function handleCommaToken(params) {
|
|
1096
|
-
const { token, tokens, state, opts, result } = params;
|
|
1097
|
-
const nextToken = tokens[state.pos];
|
|
1098
|
-
if (state.tolerant && nextToken && nextToken.type === opts.endSymbol) {
|
|
1099
|
-
raiseError(state, token, `Trailing comma before '${opts.endSymbol}'`);
|
|
1100
|
-
popToken(tokens, state);
|
|
1101
|
-
return result;
|
|
1102
|
-
}
|
|
1103
|
-
opts.elementParser(tokens, state, result);
|
|
1104
|
-
return null;
|
|
1105
|
-
}
|
|
1106
|
-
function parseManyInitialElement(tokens, state, result, opts) {
|
|
1107
|
-
const token = skipPunctuation(tokens, state, opts.skip);
|
|
1108
|
-
if (token.type === "eof") {
|
|
1109
|
-
raiseUnexpected(state, token, `'${opts.endSymbol}' or ${opts.elementName}`);
|
|
1110
|
-
return result;
|
|
1111
|
-
}
|
|
1112
|
-
if (token.type === opts.endSymbol) {
|
|
1113
|
-
return result;
|
|
1114
|
-
}
|
|
1115
|
-
state.pos -= 1;
|
|
1116
|
-
opts.elementParser(tokens, state, result);
|
|
1117
|
-
return;
|
|
1118
|
-
}
|
|
1119
|
-
function parseManyProcessToken(params) {
|
|
1120
|
-
const { token, tokens, state, opts, result } = params;
|
|
1121
|
-
if (token.type !== opts.endSymbol && token.type !== ",") {
|
|
1122
|
-
const handledResult = handleInvalidToken(token, state, opts, result);
|
|
1123
|
-
if (handledResult !== null) {
|
|
1124
|
-
return handledResult;
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
if (token.type === opts.endSymbol) {
|
|
1128
|
-
return result;
|
|
1129
|
-
}
|
|
1130
|
-
if (token.type === ",") {
|
|
1131
|
-
const handledResult = handleCommaToken({
|
|
1132
|
-
token,
|
|
1133
|
-
tokens,
|
|
1134
|
-
state,
|
|
1135
|
-
opts,
|
|
1136
|
-
result
|
|
1137
|
-
});
|
|
1138
|
-
if (handledResult !== null) {
|
|
1139
|
-
return handledResult;
|
|
1140
|
-
}
|
|
1141
|
-
return;
|
|
1142
|
-
}
|
|
1143
|
-
opts.elementParser(tokens, state, result);
|
|
1144
|
-
return;
|
|
1145
|
-
}
|
|
1146
|
-
function parseMany(tokens, state, result, opts) {
|
|
1147
|
-
const initialResult = parseManyInitialElement(tokens, state, result, opts);
|
|
1148
|
-
if (initialResult !== void 0) {
|
|
1149
|
-
return initialResult;
|
|
1150
|
-
}
|
|
1151
|
-
while (true) {
|
|
1152
|
-
const token = popToken(tokens, state);
|
|
1153
|
-
const processedResult = parseManyProcessToken({
|
|
1154
|
-
token,
|
|
1155
|
-
tokens,
|
|
1156
|
-
state,
|
|
1157
|
-
opts,
|
|
1158
|
-
result
|
|
1159
|
-
});
|
|
1160
|
-
if (processedResult !== void 0) {
|
|
1161
|
-
return processedResult;
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
function endChecks(tokens, state, ret) {
|
|
1166
|
-
if (state.pos < tokens.length) {
|
|
1167
|
-
if (state.tolerant) {
|
|
1168
|
-
skipPunctuation(tokens, state);
|
|
1169
|
-
}
|
|
1170
|
-
if (state.pos < tokens.length) {
|
|
1171
|
-
raiseError(
|
|
1172
|
-
state,
|
|
1173
|
-
tokens[state.pos],
|
|
1174
|
-
`Unexpected token: ${strToken(tokens[state.pos])}, expected end-of-input`
|
|
1175
|
-
);
|
|
1176
|
-
}
|
|
1177
|
-
}
|
|
1178
|
-
if (state.tolerant && state.warnings.length > 0) {
|
|
1179
|
-
const message = state.warnings.length === 1 ? state.warnings[0].message : `${state.warnings.length} parse warnings`;
|
|
1180
|
-
const err = new SyntaxError(message);
|
|
1181
|
-
err.line = state.warnings[0].line;
|
|
1182
|
-
err.warnings = state.warnings;
|
|
1183
|
-
err.obj = ret;
|
|
1184
|
-
throw err;
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
|
-
function parseAny(tokens, state, end = false) {
|
|
1188
|
-
const token = skipPunctuation(tokens, state);
|
|
1189
|
-
let ret;
|
|
1190
|
-
if (token.type === "eof") {
|
|
1191
|
-
if (end) {
|
|
1192
|
-
raiseUnexpected(state, token, "json value");
|
|
1193
|
-
}
|
|
1194
|
-
raiseUnexpected(state, token, "json value");
|
|
1195
|
-
return;
|
|
1196
|
-
}
|
|
1197
|
-
switch (token.type) {
|
|
1198
|
-
case "{":
|
|
1199
|
-
ret = parseObject(tokens, state);
|
|
1200
|
-
break;
|
|
1201
|
-
case "[":
|
|
1202
|
-
ret = parseArray(tokens, state);
|
|
1203
|
-
break;
|
|
1204
|
-
case "string":
|
|
1205
|
-
// String literal
|
|
1206
|
-
case "number":
|
|
1207
|
-
// Number literal
|
|
1208
|
-
case "atom":
|
|
1209
|
-
ret = token.value;
|
|
1210
|
-
break;
|
|
1211
|
-
default:
|
|
1212
|
-
raiseUnexpected(state, token, "json value");
|
|
1213
|
-
if (state.tolerant) {
|
|
1214
|
-
ret = null;
|
|
1215
|
-
} else {
|
|
1216
|
-
return;
|
|
1217
|
-
}
|
|
1218
|
-
}
|
|
1219
|
-
if (end) {
|
|
1220
|
-
ret = state.reviver ? state.reviver("", ret) : ret;
|
|
1221
|
-
endChecks(tokens, state, ret);
|
|
1222
|
-
}
|
|
1223
|
-
return ret;
|
|
1224
|
-
}
|
|
1225
|
-
function normalizeParseOptions(optsOrReviver) {
|
|
1226
|
-
var _a;
|
|
1227
|
-
let options = {};
|
|
1228
|
-
if (typeof optsOrReviver === "function") {
|
|
1229
|
-
options.reviver = optsOrReviver;
|
|
1230
|
-
} else if (optsOrReviver !== null && typeof optsOrReviver === "object") {
|
|
1231
|
-
options = { ...optsOrReviver };
|
|
1232
|
-
} else if (optsOrReviver !== void 0) {
|
|
1233
|
-
throw new TypeError(
|
|
1234
|
-
"Second argument must be a reviver function or an options object."
|
|
1235
|
-
);
|
|
1236
|
-
}
|
|
1237
|
-
if (options.relaxed === void 0) {
|
|
1238
|
-
if (options.warnings === true || options.tolerant === true) {
|
|
1239
|
-
options.relaxed = true;
|
|
1240
|
-
} else if (options.warnings === false && options.tolerant === false) {
|
|
1241
|
-
options.relaxed = false;
|
|
1242
|
-
} else {
|
|
1243
|
-
options.relaxed = true;
|
|
1244
|
-
}
|
|
1245
|
-
}
|
|
1246
|
-
options.tolerant = options.tolerant || options.warnings;
|
|
1247
|
-
options.duplicate = (_a = options.duplicate) != null ? _a : false;
|
|
1248
|
-
return options;
|
|
1249
|
-
}
|
|
1250
|
-
function createParseState(options) {
|
|
1251
|
-
var _a, _b;
|
|
1252
|
-
return {
|
|
1253
|
-
pos: 0,
|
|
1254
|
-
reviver: options.reviver,
|
|
1255
|
-
tolerant: (_a = options.tolerant) != null ? _a : false,
|
|
1256
|
-
duplicate: (_b = options.duplicate) != null ? _b : false,
|
|
1257
|
-
warnings: []
|
|
1258
|
-
};
|
|
1259
|
-
}
|
|
1260
|
-
function parseWithCustomParser(text, options) {
|
|
1261
|
-
const lexerToUse = options.relaxed ? lexer : strictLexer;
|
|
1262
|
-
let tokens = lexerToUse(text);
|
|
1263
|
-
if (options.relaxed) {
|
|
1264
|
-
tokens = stripTrailingComma(tokens);
|
|
1265
|
-
}
|
|
1266
|
-
tokens = tokens.filter((token) => token.type !== " ");
|
|
1267
|
-
const state = createParseState(options);
|
|
1268
|
-
return parseAny(tokens, state, true);
|
|
1269
|
-
}
|
|
1270
|
-
function parseWithTransform(text, options) {
|
|
1271
|
-
let tokens = lexer(text);
|
|
1272
|
-
tokens = stripTrailingComma(tokens);
|
|
1273
|
-
const newtext = tokens.reduce((str, token) => str + token.match, "");
|
|
1274
|
-
return JSON.parse(
|
|
1275
|
-
newtext,
|
|
1276
|
-
options.reviver
|
|
1277
|
-
);
|
|
1278
|
-
}
|
|
1279
|
-
function parse2(text, optsOrReviver) {
|
|
1280
|
-
const options = normalizeParseOptions(optsOrReviver);
|
|
1281
|
-
if (!(options.relaxed || options.warnings || options.tolerant) && options.duplicate) {
|
|
1282
|
-
return JSON.parse(
|
|
1283
|
-
text,
|
|
1284
|
-
options.reviver
|
|
1285
|
-
);
|
|
1286
|
-
}
|
|
1287
|
-
if (options.warnings || options.tolerant || !options.duplicate) {
|
|
1288
|
-
return parseWithCustomParser(text, options);
|
|
1289
|
-
}
|
|
1290
|
-
return parseWithTransform(text, options);
|
|
1291
|
-
}
|
|
1292
|
-
function stringifyPair(obj, key) {
|
|
1293
|
-
return `${JSON.stringify(key)}:${stringify(obj[key])}`;
|
|
1294
|
-
}
|
|
1295
|
-
function stringify(obj) {
|
|
1296
|
-
const type = typeof obj;
|
|
1297
|
-
if (type === "string" || type === "number" || type === "boolean" || obj === null) {
|
|
1298
|
-
return JSON.stringify(obj);
|
|
1299
|
-
}
|
|
1300
|
-
if (type === "undefined") {
|
|
1301
|
-
return "null";
|
|
1302
|
-
}
|
|
1303
|
-
if (Array.isArray(obj)) {
|
|
1304
|
-
const elements = obj.map(stringify).join(",");
|
|
1305
|
-
return `[${elements}]`;
|
|
1306
|
-
}
|
|
1307
|
-
if (type === "object") {
|
|
1308
|
-
const keys = Object.keys(obj);
|
|
1309
|
-
keys.sort();
|
|
1310
|
-
const pairs = keys.map((key) => stringifyPair(obj, key)).join(",");
|
|
1311
|
-
return `{${pairs}}`;
|
|
1312
|
-
}
|
|
1313
|
-
return "null";
|
|
1314
|
-
}
|
|
1315
|
-
|
|
1316
657
|
// src/core/protocols/json-protocol.ts
|
|
1317
658
|
function processToolCallJson(toolCallJson, fullMatch, processedElements, options) {
|
|
1318
659
|
var _a, _b;
|
|
1319
660
|
try {
|
|
1320
|
-
const parsedToolCall =
|
|
661
|
+
const parsedToolCall = parseRJSON(toolCallJson);
|
|
1321
662
|
processedElements.push({
|
|
1322
663
|
type: "tool-call",
|
|
1323
664
|
toolCallId: generateId(),
|
|
@@ -1445,7 +786,7 @@ function emitToolCall(context) {
|
|
|
1445
786
|
var _a, _b;
|
|
1446
787
|
const { state, controller, toolCallStart, toolCallEnd, options } = context;
|
|
1447
788
|
try {
|
|
1448
|
-
const parsedToolCall =
|
|
789
|
+
const parsedToolCall = parseRJSON(state.currentToolCallJson);
|
|
1449
790
|
closeTextBlock(state, controller);
|
|
1450
791
|
controller.enqueue({
|
|
1451
792
|
type: "tool-call",
|
|
@@ -1638,10 +979,10 @@ function isTCMProtocolFactory(protocol) {
|
|
|
1638
979
|
}
|
|
1639
980
|
|
|
1640
981
|
// src/core/protocols/xml-protocol.ts
|
|
1641
|
-
import { extractRawInner, parse as
|
|
982
|
+
import { extractRawInner, parse as parse2, stringify } from "@ai-sdk-tool/rxml";
|
|
1642
983
|
var defaultPipelineConfig2 = defaultPipelineConfig;
|
|
1643
984
|
var NAME_CHAR_RE2 = /[A-Za-z0-9_:-]/;
|
|
1644
|
-
var
|
|
985
|
+
var WHITESPACE_REGEX2 = /\s/;
|
|
1645
986
|
function getToolSchema(tools, toolName) {
|
|
1646
987
|
var _a;
|
|
1647
988
|
return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
@@ -1652,7 +993,7 @@ function normalizeCloseTags(xml) {
|
|
|
1652
993
|
function tryParseSecondaryXml(content, toolSchema, options) {
|
|
1653
994
|
const balanced = balanceTags(content);
|
|
1654
995
|
try {
|
|
1655
|
-
let parsed =
|
|
996
|
+
let parsed = parse2(balanced, toolSchema, {
|
|
1656
997
|
onError: options == null ? void 0 : options.onError,
|
|
1657
998
|
noChildNodes: []
|
|
1658
999
|
});
|
|
@@ -1667,7 +1008,7 @@ function tryParseSecondaryXml(content, toolSchema, options) {
|
|
|
1667
1008
|
}
|
|
1668
1009
|
if (deduped !== balanced) {
|
|
1669
1010
|
try {
|
|
1670
|
-
let reparsed =
|
|
1011
|
+
let reparsed = parse2(deduped, toolSchema, {
|
|
1671
1012
|
onError: options == null ? void 0 : options.onError,
|
|
1672
1013
|
noChildNodes: []
|
|
1673
1014
|
});
|
|
@@ -1699,7 +1040,7 @@ function processToolCallWithPipeline(params) {
|
|
|
1699
1040
|
toolSchema
|
|
1700
1041
|
);
|
|
1701
1042
|
const result = applyHeuristicPipeline(ctx, pipelineConfig, {
|
|
1702
|
-
parse: (xml, schema) =>
|
|
1043
|
+
parse: (xml, schema) => parse2(xml, schema, { onError: options == null ? void 0 : options.onError, noChildNodes: [] }),
|
|
1703
1044
|
onError: options == null ? void 0 : options.onError,
|
|
1704
1045
|
maxReparses
|
|
1705
1046
|
});
|
|
@@ -1744,7 +1085,7 @@ function handleStreamingToolCallEnd(params) {
|
|
|
1744
1085
|
toolSchema
|
|
1745
1086
|
);
|
|
1746
1087
|
const result = applyHeuristicPipeline(ctx, pipelineConfig, {
|
|
1747
|
-
parse: (xml, schema) =>
|
|
1088
|
+
parse: (xml, schema) => parse2(xml, schema, { onError: options == null ? void 0 : options.onError, noChildNodes: [] }),
|
|
1748
1089
|
onError: options == null ? void 0 : options.onError,
|
|
1749
1090
|
maxReparses
|
|
1750
1091
|
});
|
|
@@ -1752,7 +1093,7 @@ function handleStreamingToolCallEnd(params) {
|
|
|
1752
1093
|
} else {
|
|
1753
1094
|
try {
|
|
1754
1095
|
const primary = escapeInvalidLt(normalizeCloseTags(toolContent));
|
|
1755
|
-
const parsed =
|
|
1096
|
+
const parsed = parse2(primary, toolSchema, {
|
|
1756
1097
|
onError: options == null ? void 0 : options.onError,
|
|
1757
1098
|
noChildNodes: []
|
|
1758
1099
|
});
|
|
@@ -1815,7 +1156,7 @@ function consumeClosingTag(text, lt) {
|
|
|
1815
1156
|
}
|
|
1816
1157
|
function consumeOpenTag(text, lt) {
|
|
1817
1158
|
let p = lt + 1;
|
|
1818
|
-
while (p < text.length &&
|
|
1159
|
+
while (p < text.length && WHITESPACE_REGEX2.test(text[p])) {
|
|
1819
1160
|
p += 1;
|
|
1820
1161
|
}
|
|
1821
1162
|
const nameStart = p;
|
|
@@ -1828,7 +1169,7 @@ function consumeOpenTag(text, lt) {
|
|
|
1828
1169
|
return null;
|
|
1829
1170
|
}
|
|
1830
1171
|
let r = q - 1;
|
|
1831
|
-
while (r >= nameStart &&
|
|
1172
|
+
while (r >= nameStart && WHITESPACE_REGEX2.test(text[r])) {
|
|
1832
1173
|
r -= 1;
|
|
1833
1174
|
}
|
|
1834
1175
|
const selfClosing = text[r] === "/";
|
|
@@ -1857,7 +1198,7 @@ function nextTagToken(text, fromPos) {
|
|
|
1857
1198
|
if (next === "/") {
|
|
1858
1199
|
const closing = consumeClosingTag(text, lt);
|
|
1859
1200
|
let p = lt + 2;
|
|
1860
|
-
while (p < text.length &&
|
|
1201
|
+
while (p < text.length && WHITESPACE_REGEX2.test(text[p])) {
|
|
1861
1202
|
p += 1;
|
|
1862
1203
|
}
|
|
1863
1204
|
const nameStart = p;
|
|
@@ -2184,7 +1525,7 @@ var xmlProtocol = (protocolOptions) => {
|
|
|
2184
1525
|
args = toolCall.input;
|
|
2185
1526
|
}
|
|
2186
1527
|
}
|
|
2187
|
-
return
|
|
1528
|
+
return stringify(toolCall.toolName, args, {
|
|
2188
1529
|
suppressEmptyNode: false,
|
|
2189
1530
|
format: true,
|
|
2190
1531
|
minimalEscaping: true
|
|
@@ -2305,7 +1646,7 @@ var xmlProtocol = (protocolOptions) => {
|
|
|
2305
1646
|
// src/core/protocols/yaml-protocol.ts
|
|
2306
1647
|
import YAML from "yaml";
|
|
2307
1648
|
var NAME_CHAR_RE3 = /[A-Za-z0-9_:-]/;
|
|
2308
|
-
var
|
|
1649
|
+
var WHITESPACE_REGEX3 = /\s/;
|
|
2309
1650
|
var LEADING_WHITESPACE_RE = /^(\s*)/;
|
|
2310
1651
|
function findClosingTagEnd(text, contentStart, toolName) {
|
|
2311
1652
|
let pos = contentStart;
|
|
@@ -2322,7 +1663,7 @@ function findClosingTagEnd(text, contentStart, toolName) {
|
|
|
2322
1663
|
break;
|
|
2323
1664
|
}
|
|
2324
1665
|
let p = ltIdx + 2;
|
|
2325
|
-
while (p < gtIdx &&
|
|
1666
|
+
while (p < gtIdx && WHITESPACE_REGEX3.test(text[p])) {
|
|
2326
1667
|
p++;
|
|
2327
1668
|
}
|
|
2328
1669
|
const nameStart = p;
|
|
@@ -2342,7 +1683,7 @@ function findClosingTagEnd(text, contentStart, toolName) {
|
|
|
2342
1683
|
pos = gtIdx === -1 ? text.length : gtIdx + 1;
|
|
2343
1684
|
} else {
|
|
2344
1685
|
let p = ltIdx + 1;
|
|
2345
|
-
while (p < text.length &&
|
|
1686
|
+
while (p < text.length && WHITESPACE_REGEX3.test(text[p])) {
|
|
2346
1687
|
p++;
|
|
2347
1688
|
}
|
|
2348
1689
|
const nameStart = p;
|
|
@@ -2355,7 +1696,7 @@ function findClosingTagEnd(text, contentStart, toolName) {
|
|
|
2355
1696
|
break;
|
|
2356
1697
|
}
|
|
2357
1698
|
let r = gtIdx - 1;
|
|
2358
|
-
while (r >= nameStart &&
|
|
1699
|
+
while (r >= nameStart && WHITESPACE_REGEX3.test(text[r])) {
|
|
2359
1700
|
r--;
|
|
2360
1701
|
}
|
|
2361
1702
|
const selfClosing = text[r] === "/";
|
|
@@ -3749,9 +3090,6 @@ export {
|
|
|
3749
3090
|
logParsedSummary,
|
|
3750
3091
|
getPotentialStartIndex,
|
|
3751
3092
|
escapeRegExp2 as escapeRegExp,
|
|
3752
|
-
transform,
|
|
3753
|
-
parse2 as parse,
|
|
3754
|
-
stringify,
|
|
3755
3093
|
jsonProtocol,
|
|
3756
3094
|
isProtocolFactory,
|
|
3757
3095
|
isTCMProtocolFactory,
|
|
@@ -3775,4 +3113,4 @@ export {
|
|
|
3775
3113
|
xmlToolMiddleware,
|
|
3776
3114
|
yamlToolMiddleware
|
|
3777
3115
|
};
|
|
3778
|
-
//# sourceMappingURL=chunk-
|
|
3116
|
+
//# sourceMappingURL=chunk-3KQVEBKO.js.map
|