@ai-sdk-tool/parser 2.1.3 → 2.1.5
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/LICENSE +13 -0
- package/README.md +5 -1
- package/dist/index.cjs +227 -972
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +198 -2
- package/dist/index.d.ts +198 -2
- package/dist/index.js +200 -972
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,16 +17,41 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
21
31
|
var index_exports = {};
|
|
22
32
|
__export(index_exports, {
|
|
33
|
+
RJSON: () => robust_json_exports,
|
|
34
|
+
createDynamicIfThenElseSchema: () => createDynamicIfThenElseSchema,
|
|
23
35
|
createToolMiddleware: () => createToolMiddleware,
|
|
36
|
+
decodeOriginalTools: () => decodeOriginalTools,
|
|
37
|
+
encodeOriginalTools: () => encodeOriginalTools,
|
|
38
|
+
escapeRegExp: () => escapeRegExp,
|
|
39
|
+
extractOnErrorOption: () => extractOnErrorOption,
|
|
40
|
+
extractToolNamesFromOriginalTools: () => extractToolNamesFromOriginalTools,
|
|
24
41
|
gemmaToolMiddleware: () => gemmaToolMiddleware,
|
|
42
|
+
getDebugLevel: () => getDebugLevel,
|
|
43
|
+
getPotentialStartIndex: () => getPotentialStartIndex,
|
|
44
|
+
hasInputProperty: () => hasInputProperty,
|
|
25
45
|
hermesToolMiddleware: () => hermesToolMiddleware,
|
|
46
|
+
isToolCallContent: () => isToolCallContent,
|
|
47
|
+
isToolChoiceActive: () => isToolChoiceActive,
|
|
48
|
+
isToolResultPart: () => isToolResultPart,
|
|
26
49
|
jsonMixProtocol: () => jsonMixProtocol,
|
|
50
|
+
logParsedChunk: () => logParsedChunk,
|
|
51
|
+
logParsedSummary: () => logParsedSummary,
|
|
52
|
+
logRawChunk: () => logRawChunk,
|
|
27
53
|
morphXmlProtocol: () => morphXmlProtocol,
|
|
54
|
+
originalToolsSchema: () => originalToolsSchema,
|
|
28
55
|
xmlToolMiddleware: () => xmlToolMiddleware
|
|
29
56
|
});
|
|
30
57
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -114,9 +141,9 @@ function escapeRegExp(literal) {
|
|
|
114
141
|
return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
115
142
|
}
|
|
116
143
|
|
|
117
|
-
// src/utils/
|
|
118
|
-
var
|
|
119
|
-
__export(
|
|
144
|
+
// src/utils/robust-json.ts
|
|
145
|
+
var robust_json_exports = {};
|
|
146
|
+
__export(robust_json_exports, {
|
|
120
147
|
parse: () => parse,
|
|
121
148
|
stringify: () => stringify,
|
|
122
149
|
transform: () => transform
|
|
@@ -747,210 +774,6 @@ function stringify(obj) {
|
|
|
747
774
|
return "null";
|
|
748
775
|
}
|
|
749
776
|
|
|
750
|
-
// src/utils/coercion.ts
|
|
751
|
-
function unwrapJsonSchema(schema) {
|
|
752
|
-
if (!schema || typeof schema !== "object") return schema;
|
|
753
|
-
const s = schema;
|
|
754
|
-
if (s.jsonSchema && typeof s.jsonSchema === "object") {
|
|
755
|
-
return unwrapJsonSchema(s.jsonSchema);
|
|
756
|
-
}
|
|
757
|
-
return schema;
|
|
758
|
-
}
|
|
759
|
-
function getSchemaType(schema) {
|
|
760
|
-
const unwrapped = unwrapJsonSchema(schema);
|
|
761
|
-
if (!unwrapped || typeof unwrapped !== "object") return void 0;
|
|
762
|
-
const t = unwrapped.type;
|
|
763
|
-
if (typeof t === "string") return t;
|
|
764
|
-
if (Array.isArray(t)) {
|
|
765
|
-
const preferred = [
|
|
766
|
-
"object",
|
|
767
|
-
"array",
|
|
768
|
-
"boolean",
|
|
769
|
-
"number",
|
|
770
|
-
"integer",
|
|
771
|
-
"string"
|
|
772
|
-
];
|
|
773
|
-
for (const p of preferred) if (t.includes(p)) return p;
|
|
774
|
-
}
|
|
775
|
-
const s = unwrapped;
|
|
776
|
-
if (s && typeof s === "object" && (s.properties || s.additionalProperties)) {
|
|
777
|
-
return "object";
|
|
778
|
-
}
|
|
779
|
-
if (s && typeof s === "object" && (s.items || s.prefixItems)) {
|
|
780
|
-
return "array";
|
|
781
|
-
}
|
|
782
|
-
return void 0;
|
|
783
|
-
}
|
|
784
|
-
function coerceBySchema(value, schema) {
|
|
785
|
-
const unwrapped = unwrapJsonSchema(schema);
|
|
786
|
-
if (!unwrapped || typeof unwrapped !== "object") {
|
|
787
|
-
if (typeof value === "string") {
|
|
788
|
-
const s = value.trim();
|
|
789
|
-
const lower = s.toLowerCase();
|
|
790
|
-
if (lower === "true") return true;
|
|
791
|
-
if (lower === "false") return false;
|
|
792
|
-
if (/^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) {
|
|
793
|
-
const num = Number(s);
|
|
794
|
-
if (Number.isFinite(num)) return num;
|
|
795
|
-
}
|
|
796
|
-
if (s.startsWith("{") && s.endsWith("}") || s.startsWith("[") && s.endsWith("]")) {
|
|
797
|
-
try {
|
|
798
|
-
const parsed = JSON.parse(s);
|
|
799
|
-
return coerceBySchema(parsed, void 0);
|
|
800
|
-
} catch (e) {
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
return value;
|
|
805
|
-
}
|
|
806
|
-
const schemaType = getSchemaType(unwrapped);
|
|
807
|
-
if (typeof value === "string") {
|
|
808
|
-
const s = value.trim();
|
|
809
|
-
if (schemaType === "object") {
|
|
810
|
-
try {
|
|
811
|
-
let normalized = s.replace(/'/g, '"');
|
|
812
|
-
normalized = normalized.replace(/^\{\s*\}$/s, "{}");
|
|
813
|
-
const obj = JSON.parse(normalized);
|
|
814
|
-
if (obj && typeof obj === "object" && !Array.isArray(obj)) {
|
|
815
|
-
const props = unwrapped.properties;
|
|
816
|
-
const out = {};
|
|
817
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
818
|
-
const propSchema = props ? props[k] : void 0;
|
|
819
|
-
out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
|
|
820
|
-
}
|
|
821
|
-
return out;
|
|
822
|
-
}
|
|
823
|
-
} catch (e) {
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
if (schemaType === "array") {
|
|
827
|
-
try {
|
|
828
|
-
const normalized = s.replace(/'/g, '"');
|
|
829
|
-
const arr = JSON.parse(normalized);
|
|
830
|
-
if (Array.isArray(arr)) {
|
|
831
|
-
const u = unwrapped;
|
|
832
|
-
const prefixItems = Array.isArray(
|
|
833
|
-
u.prefixItems
|
|
834
|
-
) ? u.prefixItems : void 0;
|
|
835
|
-
const itemsSchema = u.items;
|
|
836
|
-
if (prefixItems && arr.length === prefixItems.length) {
|
|
837
|
-
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
838
|
-
}
|
|
839
|
-
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
840
|
-
}
|
|
841
|
-
} catch (e) {
|
|
842
|
-
const csv = s.includes("\n") ? s.split(/\n+/) : s.split(/,\s*/);
|
|
843
|
-
const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);
|
|
844
|
-
const u = unwrapped;
|
|
845
|
-
const prefixItems = Array.isArray(
|
|
846
|
-
u.prefixItems
|
|
847
|
-
) ? u.prefixItems : void 0;
|
|
848
|
-
const itemsSchema = u.items;
|
|
849
|
-
if (prefixItems && trimmed.length === prefixItems.length) {
|
|
850
|
-
return trimmed.map((x, i) => coerceBySchema(x, prefixItems[i]));
|
|
851
|
-
}
|
|
852
|
-
return trimmed.map((x) => coerceBySchema(x, itemsSchema));
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
if (schemaType === "object" && value && typeof value === "object" && !Array.isArray(value)) {
|
|
857
|
-
const out = {};
|
|
858
|
-
const props = unwrapped.properties;
|
|
859
|
-
for (const [k, v] of Object.entries(value)) {
|
|
860
|
-
const propSchema = props ? props[k] : void 0;
|
|
861
|
-
out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
|
|
862
|
-
}
|
|
863
|
-
return out;
|
|
864
|
-
}
|
|
865
|
-
if (schemaType === "array") {
|
|
866
|
-
const u = unwrapped;
|
|
867
|
-
const itemsSchema = u.items;
|
|
868
|
-
const prefixItems = Array.isArray(
|
|
869
|
-
u.prefixItems
|
|
870
|
-
) ? u.prefixItems : void 0;
|
|
871
|
-
if (Array.isArray(value)) {
|
|
872
|
-
if (prefixItems && value.length === prefixItems.length) {
|
|
873
|
-
return value.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
874
|
-
}
|
|
875
|
-
return value.map((v) => coerceBySchema(v, itemsSchema));
|
|
876
|
-
}
|
|
877
|
-
if (value && typeof value === "object") {
|
|
878
|
-
const maybe = value;
|
|
879
|
-
if (Object.prototype.hasOwnProperty.call(maybe, "item")) {
|
|
880
|
-
const items = maybe.item;
|
|
881
|
-
const arr = Array.isArray(items) ? items : [items];
|
|
882
|
-
if (prefixItems && arr.length === prefixItems.length) {
|
|
883
|
-
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
884
|
-
}
|
|
885
|
-
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
886
|
-
}
|
|
887
|
-
const keys = Object.keys(maybe);
|
|
888
|
-
if (keys.length === 1) {
|
|
889
|
-
const singleKey = keys[0];
|
|
890
|
-
const singleValue = maybe[singleKey];
|
|
891
|
-
if (Array.isArray(singleValue)) {
|
|
892
|
-
const coercedArray = singleValue.map(
|
|
893
|
-
(v) => coerceBySchema(v, itemsSchema)
|
|
894
|
-
);
|
|
895
|
-
return coercedArray;
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
if (keys.length > 0 && keys.every((k) => /^\d+$/.test(k))) {
|
|
899
|
-
const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
|
|
900
|
-
if (prefixItems && arr.length === prefixItems.length) {
|
|
901
|
-
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
902
|
-
}
|
|
903
|
-
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
907
|
-
if (prefixItems && prefixItems.length > 0) {
|
|
908
|
-
return [coerceBySchema(value, prefixItems[0])];
|
|
909
|
-
}
|
|
910
|
-
return [coerceBySchema(value, itemsSchema)];
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
if (typeof value === "string") {
|
|
914
|
-
const s = value.trim();
|
|
915
|
-
if (schemaType === "boolean") {
|
|
916
|
-
const lower = s.toLowerCase();
|
|
917
|
-
if (lower === "true") return true;
|
|
918
|
-
if (lower === "false") return false;
|
|
919
|
-
}
|
|
920
|
-
if (schemaType === "number" || schemaType === "integer") {
|
|
921
|
-
if (/^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) {
|
|
922
|
-
const num = Number(s);
|
|
923
|
-
if (Number.isFinite(num)) return num;
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
return value;
|
|
928
|
-
}
|
|
929
|
-
function fixToolCallWithSchema(part, tools) {
|
|
930
|
-
var _a;
|
|
931
|
-
if (part.type !== "tool-call") return part;
|
|
932
|
-
const tc = part;
|
|
933
|
-
let args = {};
|
|
934
|
-
if (typeof tc.input === "string") {
|
|
935
|
-
try {
|
|
936
|
-
args = JSON.parse(tc.input);
|
|
937
|
-
} catch (e) {
|
|
938
|
-
return part;
|
|
939
|
-
}
|
|
940
|
-
} else if (tc.input && typeof tc.input === "object") {
|
|
941
|
-
args = tc.input;
|
|
942
|
-
}
|
|
943
|
-
const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
|
|
944
|
-
const coerced = coerceBySchema(args, schema);
|
|
945
|
-
return {
|
|
946
|
-
...part,
|
|
947
|
-
input: JSON.stringify(coerced != null ? coerced : {})
|
|
948
|
-
};
|
|
949
|
-
}
|
|
950
|
-
function coerceToolCallInput(part, tools) {
|
|
951
|
-
return fixToolCallWithSchema(part, tools);
|
|
952
|
-
}
|
|
953
|
-
|
|
954
777
|
// src/utils/debug.ts
|
|
955
778
|
function normalizeBooleanString(value) {
|
|
956
779
|
const normalized = value.trim().toLowerCase();
|
|
@@ -1038,38 +861,34 @@ function extractOnErrorOption(providerOptions) {
|
|
|
1038
861
|
return void 0;
|
|
1039
862
|
}
|
|
1040
863
|
|
|
1041
|
-
// src/utils/
|
|
1042
|
-
|
|
1043
|
-
|
|
864
|
+
// src/utils/provider-options.ts
|
|
865
|
+
var originalToolsSchema = {
|
|
866
|
+
encode: encodeOriginalTools,
|
|
867
|
+
decode: decodeOriginalTools
|
|
868
|
+
};
|
|
869
|
+
function encodeOriginalTools(tools) {
|
|
870
|
+
return (tools == null ? void 0 : tools.map((t) => ({
|
|
871
|
+
name: t.name,
|
|
872
|
+
inputSchema: JSON.stringify(t.inputSchema)
|
|
873
|
+
}))) || [];
|
|
874
|
+
}
|
|
875
|
+
function decodeOriginalTools(originalTools) {
|
|
876
|
+
const tools = (originalTools == null ? void 0 : originalTools.map(
|
|
877
|
+
(t) => ({
|
|
878
|
+
name: t.name,
|
|
879
|
+
inputSchema: JSON.parse(t.inputSchema)
|
|
880
|
+
})
|
|
881
|
+
)) || [];
|
|
882
|
+
return tools;
|
|
883
|
+
}
|
|
884
|
+
function extractToolNamesFromOriginalTools(originalTools) {
|
|
885
|
+
return (originalTools == null ? void 0 : originalTools.map((t) => t.name)) || [];
|
|
1044
886
|
}
|
|
1045
|
-
|
|
1046
|
-
// src/utils/tools.ts
|
|
1047
887
|
function isToolChoiceActive(params) {
|
|
1048
888
|
var _a, _b, _c;
|
|
1049
889
|
const toolChoice = (_b = (_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware) == null ? void 0 : _b.toolChoice;
|
|
1050
890
|
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"));
|
|
1051
891
|
}
|
|
1052
|
-
function getFunctionTools(params) {
|
|
1053
|
-
var _a, _b;
|
|
1054
|
-
const functionTools = ((_a = params.tools) != null ? _a : []).filter(
|
|
1055
|
-
(t) => t.type === "function"
|
|
1056
|
-
);
|
|
1057
|
-
if (functionTools.length > 0) return functionTools;
|
|
1058
|
-
const rawToolNames = params.providerOptions && typeof params.providerOptions === "object" && ((_b = params.providerOptions.toolCallMiddleware) == null ? void 0 : _b.toolNames) || [];
|
|
1059
|
-
const toStringArray = (val) => Array.isArray(val) ? val.filter(
|
|
1060
|
-
(item) => typeof item === "string"
|
|
1061
|
-
) : [];
|
|
1062
|
-
const toolNames = toStringArray(rawToolNames);
|
|
1063
|
-
if (toolNames.length > 0) {
|
|
1064
|
-
return toolNames.map((name) => ({
|
|
1065
|
-
type: "function",
|
|
1066
|
-
name,
|
|
1067
|
-
description: "",
|
|
1068
|
-
inputSchema: { type: "object" }
|
|
1069
|
-
}));
|
|
1070
|
-
}
|
|
1071
|
-
return [];
|
|
1072
|
-
}
|
|
1073
892
|
|
|
1074
893
|
// src/utils/type-guards.ts
|
|
1075
894
|
function isToolCallContent(content) {
|
|
@@ -1139,7 +958,7 @@ var jsonMixProtocol = ({
|
|
|
1139
958
|
}
|
|
1140
959
|
if (toolCallJson) {
|
|
1141
960
|
try {
|
|
1142
|
-
const parsedToolCall =
|
|
961
|
+
const parsedToolCall = robust_json_exports.parse(toolCallJson);
|
|
1143
962
|
processedElements.push({
|
|
1144
963
|
type: "tool-call",
|
|
1145
964
|
toolCallId: (0, import_provider_utils2.generateId)(),
|
|
@@ -1262,7 +1081,7 @@ var jsonMixProtocol = ({
|
|
|
1262
1081
|
isInsideToolCall = true;
|
|
1263
1082
|
} else {
|
|
1264
1083
|
try {
|
|
1265
|
-
const parsedToolCall =
|
|
1084
|
+
const parsedToolCall = robust_json_exports.parse(currentToolCallJson);
|
|
1266
1085
|
if (currentTextId && hasEmittedTextStart) {
|
|
1267
1086
|
controller.enqueue({ type: "text-end", id: currentTextId });
|
|
1268
1087
|
currentTextId = null;
|
|
@@ -1324,619 +1143,17 @@ var jsonMixProtocol = ({
|
|
|
1324
1143
|
|
|
1325
1144
|
// src/protocols/morph-xml-protocol.ts
|
|
1326
1145
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
1327
|
-
var
|
|
1328
|
-
var WARN_ON_DUPLICATE_STRING_TAGS = true;
|
|
1329
|
-
function getToolSchema(tools, originalSchemas, toolName) {
|
|
1330
|
-
var _a;
|
|
1331
|
-
const original = originalSchemas[toolName];
|
|
1332
|
-
if (original) return original;
|
|
1333
|
-
const fallback = (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
1334
|
-
return fallback;
|
|
1335
|
-
}
|
|
1336
|
-
function getPropertySchema(toolSchema, key) {
|
|
1337
|
-
const unwrapped = unwrapJsonSchema(toolSchema);
|
|
1338
|
-
if (!unwrapped || typeof unwrapped !== "object") return void 0;
|
|
1339
|
-
const u = unwrapped;
|
|
1340
|
-
const props = u.properties;
|
|
1341
|
-
if (props && Object.prototype.hasOwnProperty.call(props, key)) {
|
|
1342
|
-
return props[key];
|
|
1343
|
-
}
|
|
1344
|
-
return void 0;
|
|
1345
|
-
}
|
|
1346
|
-
function extractRawInner(xmlContent, tagName) {
|
|
1347
|
-
const isNameStartChar = (ch) => /[A-Za-z_:]/.test(ch);
|
|
1348
|
-
const isNameChar = (ch) => /[A-Za-z0-9_.:-]/.test(ch);
|
|
1349
|
-
const len = xmlContent.length;
|
|
1350
|
-
const target = tagName;
|
|
1351
|
-
let bestStart = -1;
|
|
1352
|
-
let bestEnd = -1;
|
|
1353
|
-
let bestDepth = Number.POSITIVE_INFINITY;
|
|
1354
|
-
const skipQuoted = (s, i2) => {
|
|
1355
|
-
const quote = s[i2];
|
|
1356
|
-
i2++;
|
|
1357
|
-
while (i2 < s.length) {
|
|
1358
|
-
const ch = s[i2];
|
|
1359
|
-
if (ch === "\\") {
|
|
1360
|
-
i2 += 2;
|
|
1361
|
-
continue;
|
|
1362
|
-
}
|
|
1363
|
-
if (ch === quote) {
|
|
1364
|
-
return i2 + 1;
|
|
1365
|
-
}
|
|
1366
|
-
i2++;
|
|
1367
|
-
}
|
|
1368
|
-
return i2;
|
|
1369
|
-
};
|
|
1370
|
-
let i = 0;
|
|
1371
|
-
let depth = 0;
|
|
1372
|
-
while (i < len) {
|
|
1373
|
-
const lt = xmlContent.indexOf("<", i);
|
|
1374
|
-
if (lt === -1) return void 0;
|
|
1375
|
-
i = lt + 1;
|
|
1376
|
-
if (i >= len) return void 0;
|
|
1377
|
-
const ch = xmlContent[i];
|
|
1378
|
-
if (ch === "!") {
|
|
1379
|
-
if (xmlContent.startsWith("!--", i + 1)) {
|
|
1380
|
-
const close = xmlContent.indexOf("-->", i + 4);
|
|
1381
|
-
i = close === -1 ? len : close + 3;
|
|
1382
|
-
continue;
|
|
1383
|
-
}
|
|
1384
|
-
if (xmlContent.startsWith("![CDATA[", i + 1)) {
|
|
1385
|
-
const close = xmlContent.indexOf("]]>", i + 9);
|
|
1386
|
-
i = close === -1 ? len : close + 3;
|
|
1387
|
-
continue;
|
|
1388
|
-
}
|
|
1389
|
-
const gt = xmlContent.indexOf(">", i + 1);
|
|
1390
|
-
i = gt === -1 ? len : gt + 1;
|
|
1391
|
-
continue;
|
|
1392
|
-
} else if (ch === "?") {
|
|
1393
|
-
const close = xmlContent.indexOf("?>", i + 1);
|
|
1394
|
-
i = close === -1 ? len : close + 2;
|
|
1395
|
-
continue;
|
|
1396
|
-
} else if (ch === "/") {
|
|
1397
|
-
let j = i + 1;
|
|
1398
|
-
if (j < len && isNameStartChar(xmlContent[j])) {
|
|
1399
|
-
j++;
|
|
1400
|
-
while (j < len && isNameChar(xmlContent[j])) j++;
|
|
1401
|
-
}
|
|
1402
|
-
const gt = xmlContent.indexOf(">", j);
|
|
1403
|
-
i = gt === -1 ? len : gt + 1;
|
|
1404
|
-
depth = Math.max(0, depth - 1);
|
|
1405
|
-
continue;
|
|
1406
|
-
} else {
|
|
1407
|
-
let j = i;
|
|
1408
|
-
if (j < len && isNameStartChar(xmlContent[j])) {
|
|
1409
|
-
j++;
|
|
1410
|
-
while (j < len && isNameChar(xmlContent[j])) j++;
|
|
1411
|
-
}
|
|
1412
|
-
const name = xmlContent.slice(i, j);
|
|
1413
|
-
let k = j;
|
|
1414
|
-
let isSelfClosing = false;
|
|
1415
|
-
while (k < len) {
|
|
1416
|
-
const c = xmlContent[k];
|
|
1417
|
-
if (c === '"' || c === "'") {
|
|
1418
|
-
k = skipQuoted(xmlContent, k);
|
|
1419
|
-
continue;
|
|
1420
|
-
}
|
|
1421
|
-
if (c === ">") {
|
|
1422
|
-
break;
|
|
1423
|
-
}
|
|
1424
|
-
if (c === "/" && xmlContent[k + 1] === ">") {
|
|
1425
|
-
isSelfClosing = true;
|
|
1426
|
-
k++;
|
|
1427
|
-
break;
|
|
1428
|
-
}
|
|
1429
|
-
k++;
|
|
1430
|
-
}
|
|
1431
|
-
const tagEnd = k;
|
|
1432
|
-
if (name === target) {
|
|
1433
|
-
const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
|
|
1434
|
-
if (isSelfClosing) {
|
|
1435
|
-
if (depth < bestDepth) {
|
|
1436
|
-
bestStart = contentStart;
|
|
1437
|
-
bestEnd = contentStart;
|
|
1438
|
-
bestDepth = depth;
|
|
1439
|
-
if (bestDepth === 0) {
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
} else {
|
|
1443
|
-
let pos = contentStart;
|
|
1444
|
-
let sameDepth = 1;
|
|
1445
|
-
while (pos < len) {
|
|
1446
|
-
const nextLt = xmlContent.indexOf("<", pos);
|
|
1447
|
-
if (nextLt === -1) break;
|
|
1448
|
-
const nx = nextLt + 1;
|
|
1449
|
-
if (nx >= len) break;
|
|
1450
|
-
const h = xmlContent[nx];
|
|
1451
|
-
if (h === "!") {
|
|
1452
|
-
if (xmlContent.startsWith("!--", nx + 1)) {
|
|
1453
|
-
const close = xmlContent.indexOf("-->", nx + 4);
|
|
1454
|
-
pos = close === -1 ? len : close + 3;
|
|
1455
|
-
continue;
|
|
1456
|
-
}
|
|
1457
|
-
if (xmlContent.startsWith("![CDATA[", nx + 1)) {
|
|
1458
|
-
const close = xmlContent.indexOf("]]>", nx + 9);
|
|
1459
|
-
pos = close === -1 ? len : close + 3;
|
|
1460
|
-
continue;
|
|
1461
|
-
}
|
|
1462
|
-
const gt2 = xmlContent.indexOf(">", nx + 1);
|
|
1463
|
-
pos = gt2 === -1 ? len : gt2 + 1;
|
|
1464
|
-
continue;
|
|
1465
|
-
} else if (h === "?") {
|
|
1466
|
-
const close = xmlContent.indexOf("?>", nx + 1);
|
|
1467
|
-
pos = close === -1 ? len : close + 2;
|
|
1468
|
-
continue;
|
|
1469
|
-
} else if (h === "/") {
|
|
1470
|
-
let t = nx + 1;
|
|
1471
|
-
if (t < len && isNameStartChar(xmlContent[t])) {
|
|
1472
|
-
t++;
|
|
1473
|
-
while (t < len && isNameChar(xmlContent[t])) t++;
|
|
1474
|
-
}
|
|
1475
|
-
const endName = xmlContent.slice(nx + 1, t);
|
|
1476
|
-
const gt2 = xmlContent.indexOf(">", t);
|
|
1477
|
-
if (endName === target) {
|
|
1478
|
-
sameDepth--;
|
|
1479
|
-
if (sameDepth === 0) {
|
|
1480
|
-
if (depth < bestDepth) {
|
|
1481
|
-
bestStart = contentStart;
|
|
1482
|
-
bestEnd = nextLt;
|
|
1483
|
-
bestDepth = depth;
|
|
1484
|
-
if (bestDepth === 0) {
|
|
1485
|
-
}
|
|
1486
|
-
}
|
|
1487
|
-
break;
|
|
1488
|
-
}
|
|
1489
|
-
}
|
|
1490
|
-
pos = gt2 === -1 ? len : gt2 + 1;
|
|
1491
|
-
continue;
|
|
1492
|
-
} else {
|
|
1493
|
-
let t = nx;
|
|
1494
|
-
if (t < len && isNameStartChar(xmlContent[t])) {
|
|
1495
|
-
t++;
|
|
1496
|
-
while (t < len && isNameChar(xmlContent[t])) t++;
|
|
1497
|
-
}
|
|
1498
|
-
const startName = xmlContent.slice(nx, t);
|
|
1499
|
-
let u = t;
|
|
1500
|
-
let selfClose = false;
|
|
1501
|
-
while (u < len) {
|
|
1502
|
-
const cu = xmlContent[u];
|
|
1503
|
-
if (cu === '"' || cu === "'") {
|
|
1504
|
-
u = skipQuoted(xmlContent, u);
|
|
1505
|
-
continue;
|
|
1506
|
-
}
|
|
1507
|
-
if (cu === ">") break;
|
|
1508
|
-
if (cu === "/" && xmlContent[u + 1] === ">") {
|
|
1509
|
-
selfClose = true;
|
|
1510
|
-
u++;
|
|
1511
|
-
break;
|
|
1512
|
-
}
|
|
1513
|
-
u++;
|
|
1514
|
-
}
|
|
1515
|
-
if (startName === target && !selfClose) {
|
|
1516
|
-
sameDepth++;
|
|
1517
|
-
}
|
|
1518
|
-
pos = xmlContent[u] === ">" ? u + 1 : u + 1;
|
|
1519
|
-
continue;
|
|
1520
|
-
}
|
|
1521
|
-
}
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
i = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
|
|
1525
|
-
depth += isSelfClosing ? 0 : 1;
|
|
1526
|
-
continue;
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
if (bestStart !== -1) {
|
|
1530
|
-
return xmlContent.slice(bestStart, bestEnd);
|
|
1531
|
-
}
|
|
1532
|
-
return void 0;
|
|
1533
|
-
}
|
|
1534
|
-
function findFirstTopLevelRange(xmlContent, tagName) {
|
|
1535
|
-
const isNameStartChar = (ch) => /[A-Za-z_:]/.test(ch);
|
|
1536
|
-
const isNameChar = (ch) => /[A-Za-z0-9_.:-]/.test(ch);
|
|
1537
|
-
const len = xmlContent.length;
|
|
1538
|
-
const target = tagName;
|
|
1539
|
-
const skipQuoted = (s, i2) => {
|
|
1540
|
-
const quote = s[i2];
|
|
1541
|
-
i2++;
|
|
1542
|
-
while (i2 < s.length) {
|
|
1543
|
-
const ch = s[i2];
|
|
1544
|
-
if (ch === "\\") {
|
|
1545
|
-
i2 += 2;
|
|
1546
|
-
continue;
|
|
1547
|
-
}
|
|
1548
|
-
if (ch === quote) return i2 + 1;
|
|
1549
|
-
i2++;
|
|
1550
|
-
}
|
|
1551
|
-
return i2;
|
|
1552
|
-
};
|
|
1553
|
-
let i = 0;
|
|
1554
|
-
let depth = 0;
|
|
1555
|
-
while (i < len) {
|
|
1556
|
-
const lt = xmlContent.indexOf("<", i);
|
|
1557
|
-
if (lt === -1) return void 0;
|
|
1558
|
-
i = lt + 1;
|
|
1559
|
-
if (i >= len) return void 0;
|
|
1560
|
-
const ch = xmlContent[i];
|
|
1561
|
-
if (ch === "!") {
|
|
1562
|
-
if (xmlContent.startsWith("!--", i + 1)) {
|
|
1563
|
-
const close = xmlContent.indexOf("-->", i + 4);
|
|
1564
|
-
i = close === -1 ? len : close + 3;
|
|
1565
|
-
continue;
|
|
1566
|
-
}
|
|
1567
|
-
if (xmlContent.startsWith("![CDATA[", i + 1)) {
|
|
1568
|
-
const close = xmlContent.indexOf("]]>", i + 9);
|
|
1569
|
-
i = close === -1 ? len : close + 3;
|
|
1570
|
-
continue;
|
|
1571
|
-
}
|
|
1572
|
-
const gt = xmlContent.indexOf(">", i + 1);
|
|
1573
|
-
i = gt === -1 ? len : gt + 1;
|
|
1574
|
-
continue;
|
|
1575
|
-
} else if (ch === "?") {
|
|
1576
|
-
const close = xmlContent.indexOf("?>", i + 1);
|
|
1577
|
-
i = close === -1 ? len : close + 2;
|
|
1578
|
-
continue;
|
|
1579
|
-
} else if (ch === "/") {
|
|
1580
|
-
const gt = xmlContent.indexOf(">", i + 1);
|
|
1581
|
-
i = gt === -1 ? len : gt + 1;
|
|
1582
|
-
depth = Math.max(0, depth - 1);
|
|
1583
|
-
continue;
|
|
1584
|
-
} else {
|
|
1585
|
-
let j = i;
|
|
1586
|
-
if (j < len && isNameStartChar(xmlContent[j])) {
|
|
1587
|
-
j++;
|
|
1588
|
-
while (j < len && isNameChar(xmlContent[j])) j++;
|
|
1589
|
-
}
|
|
1590
|
-
const name = xmlContent.slice(i, j);
|
|
1591
|
-
let k = j;
|
|
1592
|
-
let isSelfClosing = false;
|
|
1593
|
-
while (k < len) {
|
|
1594
|
-
const c = xmlContent[k];
|
|
1595
|
-
if (c === '"' || c === "'") {
|
|
1596
|
-
k = skipQuoted(xmlContent, k);
|
|
1597
|
-
continue;
|
|
1598
|
-
}
|
|
1599
|
-
if (c === ">") break;
|
|
1600
|
-
if (c === "/" && xmlContent[k + 1] === ">") {
|
|
1601
|
-
isSelfClosing = true;
|
|
1602
|
-
k++;
|
|
1603
|
-
break;
|
|
1604
|
-
}
|
|
1605
|
-
k++;
|
|
1606
|
-
}
|
|
1607
|
-
const tagEnd = k;
|
|
1608
|
-
if (depth === 0 && name === target) {
|
|
1609
|
-
const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
|
|
1610
|
-
if (isSelfClosing) return { start: contentStart, end: contentStart };
|
|
1611
|
-
let pos = contentStart;
|
|
1612
|
-
let sameDepth = 1;
|
|
1613
|
-
while (pos < len) {
|
|
1614
|
-
const nextLt = xmlContent.indexOf("<", pos);
|
|
1615
|
-
if (nextLt === -1) break;
|
|
1616
|
-
const nx = nextLt + 1;
|
|
1617
|
-
if (nx >= len) break;
|
|
1618
|
-
const h = xmlContent[nx];
|
|
1619
|
-
if (h === "!") {
|
|
1620
|
-
if (xmlContent.startsWith("!--", nx + 1)) {
|
|
1621
|
-
const close = xmlContent.indexOf("-->", nx + 4);
|
|
1622
|
-
pos = close === -1 ? len : close + 3;
|
|
1623
|
-
continue;
|
|
1624
|
-
}
|
|
1625
|
-
if (xmlContent.startsWith("![CDATA[", nx + 1)) {
|
|
1626
|
-
const close = xmlContent.indexOf("]]>", nx + 9);
|
|
1627
|
-
pos = close === -1 ? len : close + 3;
|
|
1628
|
-
continue;
|
|
1629
|
-
}
|
|
1630
|
-
const gt2 = xmlContent.indexOf(">", nx + 1);
|
|
1631
|
-
pos = gt2 === -1 ? len : gt2 + 1;
|
|
1632
|
-
continue;
|
|
1633
|
-
} else if (h === "?") {
|
|
1634
|
-
const close = xmlContent.indexOf("?>", nx + 1);
|
|
1635
|
-
pos = close === -1 ? len : close + 2;
|
|
1636
|
-
continue;
|
|
1637
|
-
} else if (h === "/") {
|
|
1638
|
-
let t = nx + 1;
|
|
1639
|
-
if (t < len && isNameStartChar(xmlContent[t])) {
|
|
1640
|
-
t++;
|
|
1641
|
-
while (t < len && isNameChar(xmlContent[t])) t++;
|
|
1642
|
-
}
|
|
1643
|
-
const endName = xmlContent.slice(nx + 1, t);
|
|
1644
|
-
const gt2 = xmlContent.indexOf(">", t);
|
|
1645
|
-
if (endName === target) {
|
|
1646
|
-
sameDepth--;
|
|
1647
|
-
if (sameDepth === 0) {
|
|
1648
|
-
return { start: contentStart, end: nextLt };
|
|
1649
|
-
}
|
|
1650
|
-
}
|
|
1651
|
-
pos = gt2 === -1 ? len : gt2 + 1;
|
|
1652
|
-
continue;
|
|
1653
|
-
} else {
|
|
1654
|
-
let t = nx;
|
|
1655
|
-
if (t < len && isNameStartChar(xmlContent[t])) {
|
|
1656
|
-
t++;
|
|
1657
|
-
while (t < len && isNameChar(xmlContent[t])) t++;
|
|
1658
|
-
}
|
|
1659
|
-
let u = t;
|
|
1660
|
-
let selfClose = false;
|
|
1661
|
-
while (u < len) {
|
|
1662
|
-
const cu = xmlContent[u];
|
|
1663
|
-
if (cu === '"' || cu === "'") {
|
|
1664
|
-
u = skipQuoted(xmlContent, u);
|
|
1665
|
-
continue;
|
|
1666
|
-
}
|
|
1667
|
-
if (cu === ">") break;
|
|
1668
|
-
if (cu === "/" && xmlContent[u + 1] === ">") {
|
|
1669
|
-
selfClose = true;
|
|
1670
|
-
u++;
|
|
1671
|
-
break;
|
|
1672
|
-
}
|
|
1673
|
-
u++;
|
|
1674
|
-
}
|
|
1675
|
-
if (!selfClose) {
|
|
1676
|
-
}
|
|
1677
|
-
pos = xmlContent[u] === ">" ? u + 1 : u + 1;
|
|
1678
|
-
continue;
|
|
1679
|
-
}
|
|
1680
|
-
}
|
|
1681
|
-
return void 0;
|
|
1682
|
-
}
|
|
1683
|
-
i = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
|
|
1684
|
-
depth += isSelfClosing ? 0 : 1;
|
|
1685
|
-
continue;
|
|
1686
|
-
}
|
|
1687
|
-
}
|
|
1688
|
-
return void 0;
|
|
1689
|
-
}
|
|
1690
|
-
function countTagOccurrences(xmlContent, tagName, excludeRanges, skipFirst = true) {
|
|
1691
|
-
const isNameStartChar = (ch) => /[A-Za-z_:]/.test(ch);
|
|
1692
|
-
const isNameChar = (ch) => /[A-Za-z0-9_.:-]/.test(ch);
|
|
1693
|
-
const len = xmlContent.length;
|
|
1694
|
-
const target = tagName;
|
|
1695
|
-
const skipQuoted = (s, i2) => {
|
|
1696
|
-
const quote = s[i2];
|
|
1697
|
-
i2++;
|
|
1698
|
-
while (i2 < s.length) {
|
|
1699
|
-
const ch = s[i2];
|
|
1700
|
-
if (ch === "\\") {
|
|
1701
|
-
i2 += 2;
|
|
1702
|
-
continue;
|
|
1703
|
-
}
|
|
1704
|
-
if (ch === quote) return i2 + 1;
|
|
1705
|
-
i2++;
|
|
1706
|
-
}
|
|
1707
|
-
return i2;
|
|
1708
|
-
};
|
|
1709
|
-
let i = 0;
|
|
1710
|
-
let count = 0;
|
|
1711
|
-
const isExcluded = (pos) => {
|
|
1712
|
-
if (!excludeRanges || excludeRanges.length === 0) return false;
|
|
1713
|
-
for (const r of excludeRanges) {
|
|
1714
|
-
if (pos >= r.start && pos < r.end) return true;
|
|
1715
|
-
}
|
|
1716
|
-
return false;
|
|
1717
|
-
};
|
|
1718
|
-
while (i < len) {
|
|
1719
|
-
const lt = xmlContent.indexOf("<", i);
|
|
1720
|
-
if (lt === -1) break;
|
|
1721
|
-
i = lt + 1;
|
|
1722
|
-
if (i >= len) break;
|
|
1723
|
-
const ch = xmlContent[i];
|
|
1724
|
-
if (ch === "!") {
|
|
1725
|
-
if (xmlContent.startsWith("!--", i + 1)) {
|
|
1726
|
-
const close = xmlContent.indexOf("-->", i + 4);
|
|
1727
|
-
i = close === -1 ? len : close + 3;
|
|
1728
|
-
continue;
|
|
1729
|
-
}
|
|
1730
|
-
if (xmlContent.startsWith("![CDATA[", i + 1)) {
|
|
1731
|
-
const close = xmlContent.indexOf("]]>", i + 9);
|
|
1732
|
-
i = close === -1 ? len : close + 3;
|
|
1733
|
-
continue;
|
|
1734
|
-
}
|
|
1735
|
-
const gt = xmlContent.indexOf(">", i + 1);
|
|
1736
|
-
i = gt === -1 ? len : gt + 1;
|
|
1737
|
-
continue;
|
|
1738
|
-
} else if (ch === "?") {
|
|
1739
|
-
const close = xmlContent.indexOf("?>", i + 1);
|
|
1740
|
-
i = close === -1 ? len : close + 2;
|
|
1741
|
-
continue;
|
|
1742
|
-
} else if (ch === "/") {
|
|
1743
|
-
const gt = xmlContent.indexOf(">", i + 1);
|
|
1744
|
-
i = gt === -1 ? len : gt + 1;
|
|
1745
|
-
continue;
|
|
1746
|
-
} else {
|
|
1747
|
-
let j = i;
|
|
1748
|
-
if (j < len && isNameStartChar(xmlContent[j])) {
|
|
1749
|
-
j++;
|
|
1750
|
-
while (j < len && isNameChar(xmlContent[j])) j++;
|
|
1751
|
-
}
|
|
1752
|
-
const name = xmlContent.slice(i, j);
|
|
1753
|
-
let k = j;
|
|
1754
|
-
while (k < len) {
|
|
1755
|
-
const c = xmlContent[k];
|
|
1756
|
-
if (c === '"' || c === "'") {
|
|
1757
|
-
k = skipQuoted(xmlContent, k);
|
|
1758
|
-
continue;
|
|
1759
|
-
}
|
|
1760
|
-
if (c === ">") break;
|
|
1761
|
-
if (c === "/" && xmlContent[k + 1] === ">") {
|
|
1762
|
-
k++;
|
|
1763
|
-
break;
|
|
1764
|
-
}
|
|
1765
|
-
k++;
|
|
1766
|
-
}
|
|
1767
|
-
if (name === target && !isExcluded(lt)) {
|
|
1768
|
-
if (skipFirst) {
|
|
1769
|
-
skipFirst = false;
|
|
1770
|
-
} else {
|
|
1771
|
-
count++;
|
|
1772
|
-
}
|
|
1773
|
-
}
|
|
1774
|
-
i = k + 1;
|
|
1775
|
-
continue;
|
|
1776
|
-
}
|
|
1777
|
-
}
|
|
1778
|
-
return count;
|
|
1779
|
-
}
|
|
1780
|
-
function processParsedArgs(parsedArgs, toolSchema, toolContent, toolName, options) {
|
|
1781
|
-
var _a, _b, _c;
|
|
1782
|
-
const args = {};
|
|
1783
|
-
let cancelToolCall = false;
|
|
1784
|
-
const stringTypedProps = (() => {
|
|
1785
|
-
const set = /* @__PURE__ */ new Set();
|
|
1786
|
-
const unwrapped = unwrapJsonSchema(toolSchema);
|
|
1787
|
-
if (unwrapped && typeof unwrapped === "object") {
|
|
1788
|
-
const u = unwrapped;
|
|
1789
|
-
const props = u.properties;
|
|
1790
|
-
if (props && typeof props === "object") {
|
|
1791
|
-
for (const key of Object.keys(props)) {
|
|
1792
|
-
const t = getSchemaType(props[key]);
|
|
1793
|
-
if (t === "string") set.add(key);
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
1796
|
-
}
|
|
1797
|
-
return set;
|
|
1798
|
-
})();
|
|
1799
|
-
for (const k of Object.keys(parsedArgs || {})) {
|
|
1800
|
-
const v = parsedArgs[k];
|
|
1801
|
-
let val = v;
|
|
1802
|
-
const propSchema = getPropertySchema(toolSchema, k);
|
|
1803
|
-
const propType = getSchemaType(propSchema);
|
|
1804
|
-
if (propType === "string" && !Array.isArray(v)) {
|
|
1805
|
-
const excludeRanges = [];
|
|
1806
|
-
for (const other of stringTypedProps) {
|
|
1807
|
-
if (other === k) continue;
|
|
1808
|
-
const range = findFirstTopLevelRange(toolContent, other);
|
|
1809
|
-
if (range) excludeRanges.push(range);
|
|
1810
|
-
}
|
|
1811
|
-
const occurrences = countTagOccurrences(
|
|
1812
|
-
toolContent,
|
|
1813
|
-
k,
|
|
1814
|
-
excludeRanges,
|
|
1815
|
-
true
|
|
1816
|
-
);
|
|
1817
|
-
if (occurrences > 0) {
|
|
1818
|
-
if (WARN_ON_DUPLICATE_STRING_TAGS) {
|
|
1819
|
-
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
1820
|
-
options,
|
|
1821
|
-
`Duplicate string tags for <${k}> detected; cancelling tool call`,
|
|
1822
|
-
{
|
|
1823
|
-
toolName,
|
|
1824
|
-
toolCall: `<${toolName}>${toolContent}</${toolName}>`
|
|
1825
|
-
}
|
|
1826
|
-
);
|
|
1827
|
-
}
|
|
1828
|
-
cancelToolCall = true;
|
|
1829
|
-
break;
|
|
1830
|
-
}
|
|
1831
|
-
const raw = extractRawInner(toolContent, k);
|
|
1832
|
-
if (typeof raw === "string") {
|
|
1833
|
-
args[k] = raw;
|
|
1834
|
-
continue;
|
|
1835
|
-
}
|
|
1836
|
-
}
|
|
1837
|
-
if (v && typeof v === "object" && Object.prototype.hasOwnProperty.call(v, "#text")) {
|
|
1838
|
-
val = v == null ? void 0 : v["#text"];
|
|
1839
|
-
}
|
|
1840
|
-
if (Array.isArray(v)) {
|
|
1841
|
-
if (propType === "string") {
|
|
1842
|
-
const mapped = v.map((item) => {
|
|
1843
|
-
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1844
|
-
const textVal = item == null ? void 0 : item["#text"];
|
|
1845
|
-
return typeof textVal === "string" ? textVal : String(textVal);
|
|
1846
|
-
}
|
|
1847
|
-
return typeof item === "string" ? item : String(item);
|
|
1848
|
-
});
|
|
1849
|
-
if (mapped.length > 1 && WARN_ON_DUPLICATE_STRING_TAGS) {
|
|
1850
|
-
(_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
|
|
1851
|
-
options,
|
|
1852
|
-
`Duplicate string tags for <${k}> detected; cancelling tool call`,
|
|
1853
|
-
{
|
|
1854
|
-
toolName,
|
|
1855
|
-
toolCall: `<${toolName}>${toolContent}</${toolName}>`
|
|
1856
|
-
}
|
|
1857
|
-
);
|
|
1858
|
-
}
|
|
1859
|
-
if (mapped.length > 1) {
|
|
1860
|
-
cancelToolCall = true;
|
|
1861
|
-
break;
|
|
1862
|
-
} else {
|
|
1863
|
-
args[k] = (_c = mapped[0]) != null ? _c : "";
|
|
1864
|
-
continue;
|
|
1865
|
-
}
|
|
1866
|
-
} else {
|
|
1867
|
-
val = v.map((item) => {
|
|
1868
|
-
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1869
|
-
const textVal = item == null ? void 0 : item["#text"];
|
|
1870
|
-
return typeof textVal === "string" ? textVal.trim() : textVal;
|
|
1871
|
-
}
|
|
1872
|
-
return typeof item === "string" ? item.trim() : item;
|
|
1873
|
-
});
|
|
1874
|
-
}
|
|
1875
|
-
} else if (v && typeof v === "object" && !Object.prototype.hasOwnProperty.call(v, "#text")) {
|
|
1876
|
-
const obj = v;
|
|
1877
|
-
const keys = Object.keys(obj);
|
|
1878
|
-
if (keys.length === 1 && keys[0] === "item") {
|
|
1879
|
-
const itemValue = obj.item;
|
|
1880
|
-
if (Array.isArray(itemValue)) {
|
|
1881
|
-
val = itemValue.map((item) => {
|
|
1882
|
-
let currentVal = item;
|
|
1883
|
-
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1884
|
-
currentVal = item == null ? void 0 : item["#text"];
|
|
1885
|
-
}
|
|
1886
|
-
const trimmed = typeof currentVal === "string" ? currentVal.trim() : currentVal;
|
|
1887
|
-
if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
|
|
1888
|
-
const num = Number(trimmed);
|
|
1889
|
-
if (Number.isFinite(num)) return num;
|
|
1890
|
-
}
|
|
1891
|
-
return trimmed;
|
|
1892
|
-
});
|
|
1893
|
-
} else {
|
|
1894
|
-
const trimmed = typeof itemValue === "string" ? itemValue.trim() : itemValue;
|
|
1895
|
-
if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
|
|
1896
|
-
const num = Number(trimmed);
|
|
1897
|
-
val = Number.isFinite(num) ? num : trimmed;
|
|
1898
|
-
} else {
|
|
1899
|
-
val = trimmed;
|
|
1900
|
-
}
|
|
1901
|
-
}
|
|
1902
|
-
} else {
|
|
1903
|
-
let isIndexedTuple = false;
|
|
1904
|
-
if (keys.length > 0 && keys.every((key) => /^\d+$/.test(key))) {
|
|
1905
|
-
const indices = keys.map((k2) => parseInt(k2, 10)).sort((a, b) => a - b);
|
|
1906
|
-
isIndexedTuple = indices[0] === 0 && indices.every((val2, idx) => val2 === idx);
|
|
1907
|
-
}
|
|
1908
|
-
if (isIndexedTuple) {
|
|
1909
|
-
const sortedKeys = keys.sort(
|
|
1910
|
-
(a, b) => parseInt(a, 10) - parseInt(b, 10)
|
|
1911
|
-
);
|
|
1912
|
-
val = sortedKeys.map((key) => {
|
|
1913
|
-
const item = obj[key];
|
|
1914
|
-
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1915
|
-
const textVal = item == null ? void 0 : item["#text"];
|
|
1916
|
-
return typeof textVal === "string" ? textVal.trim() : textVal;
|
|
1917
|
-
}
|
|
1918
|
-
return typeof item === "string" ? item.trim() : item;
|
|
1919
|
-
});
|
|
1920
|
-
} else {
|
|
1921
|
-
val = v;
|
|
1922
|
-
}
|
|
1923
|
-
}
|
|
1924
|
-
}
|
|
1925
|
-
args[k] = typeof val === "string" ? val.trim() : val;
|
|
1926
|
-
}
|
|
1927
|
-
return { args, cancelToolCall };
|
|
1928
|
-
}
|
|
1146
|
+
var RXML = __toESM(require("@ai-sdk-tool/rxml"), 1);
|
|
1929
1147
|
var morphXmlProtocol = () => ({
|
|
1930
1148
|
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
1931
1149
|
const toolsForPrompt = (tools || []).map((tool) => ({
|
|
1932
1150
|
name: tool.name,
|
|
1933
1151
|
description: tool.description,
|
|
1934
|
-
parameters: unwrapJsonSchema(tool.inputSchema)
|
|
1152
|
+
parameters: RXML.unwrapJsonSchema(tool.inputSchema)
|
|
1935
1153
|
}));
|
|
1936
1154
|
return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
|
|
1937
1155
|
},
|
|
1938
1156
|
formatToolCall(toolCall) {
|
|
1939
|
-
const builder = new import_fast_xml_parser.XMLBuilder({ format: true, suppressEmptyNode: true });
|
|
1940
1157
|
let args = {};
|
|
1941
1158
|
const inputValue = hasInputProperty(toolCall) ? toolCall.input : void 0;
|
|
1942
1159
|
if (typeof inputValue === "string") {
|
|
@@ -1948,85 +1165,60 @@ var morphXmlProtocol = () => ({
|
|
|
1948
1165
|
} else {
|
|
1949
1166
|
args = inputValue;
|
|
1950
1167
|
}
|
|
1951
|
-
|
|
1952
|
-
|
|
1168
|
+
return RXML.stringify(toolCall.toolName, args, {
|
|
1169
|
+
suppressEmptyNode: false,
|
|
1170
|
+
format: false
|
|
1953
1171
|
});
|
|
1954
|
-
return xmlContent;
|
|
1955
1172
|
},
|
|
1956
1173
|
formatToolResponse(toolResult) {
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
tool_name: toolResult.toolName,
|
|
1961
|
-
result: toolResult.output
|
|
1962
|
-
}
|
|
1174
|
+
return RXML.stringify("tool_response", {
|
|
1175
|
+
tool_name: toolResult.toolName,
|
|
1176
|
+
result: toolResult.output
|
|
1963
1177
|
});
|
|
1964
|
-
return xmlContent;
|
|
1965
1178
|
},
|
|
1966
1179
|
parseGeneratedText({ text, tools, options }) {
|
|
1967
|
-
var _a
|
|
1968
|
-
const originalSchemas = (options == null ? void 0 : options.originalToolSchemas) || {};
|
|
1180
|
+
var _a;
|
|
1969
1181
|
const toolNames = tools.map((t) => t.name).filter((name) => name != null);
|
|
1970
1182
|
if (toolNames.length === 0) {
|
|
1971
1183
|
return [{ type: "text", text }];
|
|
1972
1184
|
}
|
|
1973
|
-
const toolNamesPattern = toolNames.map((n) => escapeRegExp(n)).join("|");
|
|
1974
|
-
const toolCallRegex = new RegExp(
|
|
1975
|
-
String.raw`<(${toolNamesPattern})>([\s\S]*?)<\/\1>`,
|
|
1976
|
-
"g"
|
|
1977
|
-
);
|
|
1978
1185
|
const processedElements = [];
|
|
1979
1186
|
let currentIndex = 0;
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
const toolContent = match[2].trim();
|
|
1985
|
-
if (startIndex > currentIndex) {
|
|
1986
|
-
const textSegment = text.substring(currentIndex, startIndex);
|
|
1187
|
+
const toolCalls = findToolCalls(text, toolNames);
|
|
1188
|
+
for (const toolCall of toolCalls) {
|
|
1189
|
+
if (toolCall.startIndex > currentIndex) {
|
|
1190
|
+
const textSegment = text.substring(currentIndex, toolCall.startIndex);
|
|
1987
1191
|
if (textSegment.trim()) {
|
|
1988
1192
|
processedElements.push({ type: "text", text: textSegment });
|
|
1989
1193
|
}
|
|
1990
1194
|
}
|
|
1991
1195
|
try {
|
|
1992
|
-
const
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1196
|
+
const toolSchema = getToolSchema(tools, toolCall.toolName);
|
|
1197
|
+
const parsed = RXML.parse(toolCall.content, toolSchema, {
|
|
1198
|
+
onError: options == null ? void 0 : options.onError,
|
|
1199
|
+
// Disable HTML self-closing tag behavior to allow base, meta, link etc. as regular tags
|
|
1200
|
+
noChildNodes: []
|
|
1201
|
+
});
|
|
1202
|
+
processedElements.push({
|
|
1203
|
+
type: "tool-call",
|
|
1204
|
+
toolCallId: (0, import_provider_utils3.generateId)(),
|
|
1205
|
+
toolName: toolCall.toolName,
|
|
1206
|
+
input: JSON.stringify(parsed)
|
|
1997
1207
|
});
|
|
1998
|
-
const parsedArgs = ((_a = parser.parse(`<root>${toolContent}</root>`)) == null ? void 0 : _a.root) || {};
|
|
1999
|
-
const toolSchema = getToolSchema(tools, originalSchemas, toolName);
|
|
2000
|
-
const { args, cancelToolCall } = processParsedArgs(
|
|
2001
|
-
parsedArgs,
|
|
2002
|
-
toolSchema,
|
|
2003
|
-
toolContent,
|
|
2004
|
-
toolName,
|
|
2005
|
-
options
|
|
2006
|
-
);
|
|
2007
|
-
if (cancelToolCall) {
|
|
2008
|
-
const originalCallText = match[0];
|
|
2009
|
-
(_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
|
|
2010
|
-
options,
|
|
2011
|
-
`Duplicate string tags detected; cancelling tool call`,
|
|
2012
|
-
{ toolCall: originalCallText, toolName }
|
|
2013
|
-
);
|
|
2014
|
-
processedElements.push({ type: "text", text: originalCallText });
|
|
2015
|
-
} else {
|
|
2016
|
-
const coercedArgs = coerceBySchema(args, toolSchema);
|
|
2017
|
-
processedElements.push({
|
|
2018
|
-
type: "tool-call",
|
|
2019
|
-
toolCallId: (0, import_provider_utils3.generateId)(),
|
|
2020
|
-
toolName,
|
|
2021
|
-
input: JSON.stringify(coercedArgs)
|
|
2022
|
-
});
|
|
2023
|
-
}
|
|
2024
1208
|
} catch (error) {
|
|
2025
|
-
const
|
|
2026
|
-
|
|
2027
|
-
|
|
1209
|
+
const originalCallText = text.substring(
|
|
1210
|
+
toolCall.startIndex,
|
|
1211
|
+
toolCall.endIndex
|
|
1212
|
+
);
|
|
1213
|
+
const message = `Could not process XML tool call, keeping original text: ${originalCallText}`;
|
|
1214
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, message, {
|
|
1215
|
+
toolCall: originalCallText,
|
|
1216
|
+
toolName: toolCall.toolName,
|
|
1217
|
+
error
|
|
1218
|
+
});
|
|
1219
|
+
processedElements.push({ type: "text", text: originalCallText });
|
|
2028
1220
|
}
|
|
2029
|
-
currentIndex =
|
|
1221
|
+
currentIndex = toolCall.endIndex;
|
|
2030
1222
|
}
|
|
2031
1223
|
if (currentIndex < text.length) {
|
|
2032
1224
|
const remainingText = text.substring(currentIndex);
|
|
@@ -2037,8 +1229,8 @@ var morphXmlProtocol = () => ({
|
|
|
2037
1229
|
return processedElements;
|
|
2038
1230
|
},
|
|
2039
1231
|
createStreamParser({ tools, options }) {
|
|
2040
|
-
const originalSchemas = (options == null ? void 0 : options.originalToolSchemas) || {};
|
|
2041
1232
|
const toolNames = tools.map((t) => t.name).filter((name) => name != null);
|
|
1233
|
+
const maxStartTagLen = toolNames.length ? Math.max(...toolNames.map((n) => `<${n}>`.length)) : 0;
|
|
2042
1234
|
let buffer = "";
|
|
2043
1235
|
let currentToolCall = null;
|
|
2044
1236
|
let currentTextId = null;
|
|
@@ -2080,61 +1272,34 @@ var morphXmlProtocol = () => ({
|
|
|
2080
1272
|
const toolContent = buffer.substring(0, endTagIndex);
|
|
2081
1273
|
buffer = buffer.substring(endTagIndex + endTag.length);
|
|
2082
1274
|
try {
|
|
2083
|
-
const
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
1275
|
+
const toolSchema = getToolSchema(tools, currentToolCall.name);
|
|
1276
|
+
const parsed = RXML.parse(toolContent, toolSchema, {
|
|
1277
|
+
onError: options == null ? void 0 : options.onError,
|
|
1278
|
+
// Disable HTML self-closing tag behavior to allow base, meta, link etc. as regular tags
|
|
1279
|
+
noChildNodes: []
|
|
2088
1280
|
});
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
currentToolCall.name
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
toolSchema,
|
|
2098
|
-
toolContent,
|
|
2099
|
-
currentToolCall.name,
|
|
2100
|
-
options
|
|
2101
|
-
);
|
|
2102
|
-
if (cancelToolCall) {
|
|
2103
|
-
const originalCallText = `<${currentToolCall.name}>${toolContent}</${currentToolCall.name}>`;
|
|
2104
|
-
if (options == null ? void 0 : options.onError) {
|
|
2105
|
-
options.onError(
|
|
2106
|
-
"Duplicate string tags detected; cancelling tool call",
|
|
2107
|
-
{
|
|
2108
|
-
toolCall: originalCallText,
|
|
2109
|
-
toolName: currentToolCall.name
|
|
2110
|
-
}
|
|
2111
|
-
);
|
|
2112
|
-
}
|
|
2113
|
-
flushText(controller, originalCallText);
|
|
2114
|
-
} else {
|
|
2115
|
-
const coercedArgs = coerceBySchema(
|
|
2116
|
-
args,
|
|
2117
|
-
toolSchema
|
|
2118
|
-
);
|
|
2119
|
-
flushText(controller);
|
|
2120
|
-
controller.enqueue({
|
|
2121
|
-
type: "tool-call",
|
|
2122
|
-
toolCallId: (0, import_provider_utils3.generateId)(),
|
|
2123
|
-
toolName: currentToolCall.name,
|
|
2124
|
-
input: JSON.stringify(coercedArgs)
|
|
2125
|
-
});
|
|
2126
|
-
}
|
|
2127
|
-
} catch (e) {
|
|
1281
|
+
flushText(controller);
|
|
1282
|
+
controller.enqueue({
|
|
1283
|
+
type: "tool-call",
|
|
1284
|
+
toolCallId: (0, import_provider_utils3.generateId)(),
|
|
1285
|
+
toolName: currentToolCall.name,
|
|
1286
|
+
input: JSON.stringify(parsed)
|
|
1287
|
+
});
|
|
1288
|
+
} catch (error) {
|
|
2128
1289
|
const originalCallText = `<${currentToolCall.name}>${toolContent}${endTag}`;
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
);
|
|
1290
|
+
let message = "Could not process streaming XML tool call; emitting original text.";
|
|
1291
|
+
if (error instanceof RXML.RXMLDuplicateStringTagError) {
|
|
1292
|
+
message = `Duplicate string tags detected in streaming tool call '${currentToolCall.name}'; emitting original text.`;
|
|
1293
|
+
} else if (error instanceof RXML.RXMLCoercionError) {
|
|
1294
|
+
message = `Failed to coerce arguments for streaming tool call '${currentToolCall.name}'; emitting original text.`;
|
|
1295
|
+
} else if (error instanceof RXML.RXMLParseError) {
|
|
1296
|
+
message = `Failed to parse XML for streaming tool call '${currentToolCall.name}'; emitting original text.`;
|
|
2137
1297
|
}
|
|
1298
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, message, {
|
|
1299
|
+
toolCall: originalCallText,
|
|
1300
|
+
toolName: currentToolCall.name,
|
|
1301
|
+
error
|
|
1302
|
+
});
|
|
2138
1303
|
flushText(controller, originalCallText);
|
|
2139
1304
|
}
|
|
2140
1305
|
currentToolCall = null;
|
|
@@ -2163,6 +1328,14 @@ var morphXmlProtocol = () => ({
|
|
|
2163
1328
|
);
|
|
2164
1329
|
currentToolCall = { name: earliestToolName, content: "" };
|
|
2165
1330
|
} else {
|
|
1331
|
+
const tail = Math.max(0, maxStartTagLen - 1);
|
|
1332
|
+
const safeLen = Math.max(0, buffer.length - tail);
|
|
1333
|
+
if (safeLen > 0) {
|
|
1334
|
+
const textToFlush = buffer.slice(0, safeLen);
|
|
1335
|
+
flushText(controller, textToFlush);
|
|
1336
|
+
buffer = buffer.slice(safeLen);
|
|
1337
|
+
continue;
|
|
1338
|
+
}
|
|
2166
1339
|
break;
|
|
2167
1340
|
}
|
|
2168
1341
|
}
|
|
@@ -2184,26 +1357,66 @@ var morphXmlProtocol = () => ({
|
|
|
2184
1357
|
extractToolCallSegments({ text, tools }) {
|
|
2185
1358
|
const toolNames = tools.map((t) => t.name).filter(Boolean);
|
|
2186
1359
|
if (toolNames.length === 0) return [];
|
|
2187
|
-
|
|
2188
|
-
if (!names) return [];
|
|
2189
|
-
const regex = new RegExp(`<(${names})>[\\s\\S]*?<\\/\\1>`, "g");
|
|
2190
|
-
const segments = [];
|
|
2191
|
-
let m;
|
|
2192
|
-
while ((m = regex.exec(text)) != null) {
|
|
2193
|
-
segments.push(m[0]);
|
|
2194
|
-
}
|
|
2195
|
-
return segments;
|
|
1360
|
+
return findToolCalls(text, toolNames).map((tc) => tc.segment);
|
|
2196
1361
|
}
|
|
2197
1362
|
});
|
|
1363
|
+
function getToolSchema(tools, toolName) {
|
|
1364
|
+
var _a;
|
|
1365
|
+
return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
1366
|
+
}
|
|
1367
|
+
function findToolCalls(text, toolNames) {
|
|
1368
|
+
var _a;
|
|
1369
|
+
const toolCalls = [];
|
|
1370
|
+
for (const toolName of toolNames) {
|
|
1371
|
+
let searchIndex = 0;
|
|
1372
|
+
while (searchIndex < text.length) {
|
|
1373
|
+
const startTag = `<${toolName}>`;
|
|
1374
|
+
const tagStart = text.indexOf(startTag, searchIndex);
|
|
1375
|
+
if (tagStart === -1) break;
|
|
1376
|
+
const remainingText = text.substring(tagStart);
|
|
1377
|
+
const range = RXML.findFirstTopLevelRange(remainingText, toolName);
|
|
1378
|
+
if (range) {
|
|
1379
|
+
const contentStart = tagStart + startTag.length;
|
|
1380
|
+
const contentEnd = contentStart + (range.end - range.start);
|
|
1381
|
+
let fullTagEnd = contentEnd + `</${toolName}>`.length;
|
|
1382
|
+
const closeHead = text.indexOf(`</${toolName}`, contentEnd);
|
|
1383
|
+
if (closeHead === contentEnd) {
|
|
1384
|
+
let p = closeHead + 2 + toolName.length;
|
|
1385
|
+
while (p < text.length && /\s/.test(text[p])) p++;
|
|
1386
|
+
if (text[p] === ">") fullTagEnd = p + 1;
|
|
1387
|
+
}
|
|
1388
|
+
const segment = text.substring(tagStart, fullTagEnd);
|
|
1389
|
+
const content = (_a = RXML.extractRawInner(segment, toolName)) != null ? _a : text.substring(contentStart, contentEnd);
|
|
1390
|
+
toolCalls.push({
|
|
1391
|
+
toolName,
|
|
1392
|
+
startIndex: tagStart,
|
|
1393
|
+
endIndex: fullTagEnd,
|
|
1394
|
+
content,
|
|
1395
|
+
segment
|
|
1396
|
+
});
|
|
1397
|
+
searchIndex = fullTagEnd;
|
|
1398
|
+
} else {
|
|
1399
|
+
searchIndex = tagStart + startTag.length;
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
return toolCalls.sort((a, b) => a.startIndex - b.startIndex);
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
// src/protocols/tool-call-protocol.ts
|
|
1407
|
+
function isProtocolFactory(protocol) {
|
|
1408
|
+
return typeof protocol === "function";
|
|
1409
|
+
}
|
|
2198
1410
|
|
|
2199
1411
|
// src/generate-handler.ts
|
|
2200
1412
|
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
1413
|
+
var RXML2 = __toESM(require("@ai-sdk-tool/rxml"), 1);
|
|
2201
1414
|
async function wrapGenerate({
|
|
2202
1415
|
protocol,
|
|
2203
1416
|
doGenerate,
|
|
2204
1417
|
params
|
|
2205
1418
|
}) {
|
|
2206
|
-
var _a, _b;
|
|
1419
|
+
var _a, _b, _c, _d;
|
|
2207
1420
|
if (isToolChoiceActive(params)) {
|
|
2208
1421
|
const result2 = await doGenerate();
|
|
2209
1422
|
let parsed2 = {};
|
|
@@ -2244,6 +1457,9 @@ async function wrapGenerate({
|
|
|
2244
1457
|
content: [toolCall]
|
|
2245
1458
|
};
|
|
2246
1459
|
}
|
|
1460
|
+
const tools = originalToolsSchema.decode(
|
|
1461
|
+
(_d = (_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware) == null ? void 0 : _d.originalTools
|
|
1462
|
+
);
|
|
2247
1463
|
const result = await doGenerate();
|
|
2248
1464
|
if (result.content.length === 0) {
|
|
2249
1465
|
return result;
|
|
@@ -2259,16 +1475,15 @@ async function wrapGenerate({
|
|
|
2259
1475
|
}
|
|
2260
1476
|
return protocol.parseGeneratedText({
|
|
2261
1477
|
text: contentItem.text,
|
|
2262
|
-
tools
|
|
1478
|
+
tools,
|
|
2263
1479
|
options: {
|
|
2264
1480
|
...extractOnErrorOption(params.providerOptions),
|
|
2265
1481
|
...(_a2 = params.providerOptions) == null ? void 0 : _a2.toolCallMiddleware
|
|
2266
1482
|
}
|
|
2267
1483
|
});
|
|
2268
1484
|
});
|
|
2269
|
-
const tools = getFunctionTools(params);
|
|
2270
1485
|
const newContent = parsed.map(
|
|
2271
|
-
(part) =>
|
|
1486
|
+
(part) => fixToolCallWithSchema(part, tools)
|
|
2272
1487
|
);
|
|
2273
1488
|
const debugLevel = getDebugLevel();
|
|
2274
1489
|
if (debugLevel === "stream") {
|
|
@@ -2290,6 +1505,27 @@ async function wrapGenerate({
|
|
|
2290
1505
|
content: newContent
|
|
2291
1506
|
};
|
|
2292
1507
|
}
|
|
1508
|
+
function fixToolCallWithSchema(part, tools) {
|
|
1509
|
+
var _a;
|
|
1510
|
+
if (part.type !== "tool-call") return part;
|
|
1511
|
+
const tc = part;
|
|
1512
|
+
let args = {};
|
|
1513
|
+
if (typeof tc.input === "string") {
|
|
1514
|
+
try {
|
|
1515
|
+
args = JSON.parse(tc.input);
|
|
1516
|
+
} catch (e) {
|
|
1517
|
+
return part;
|
|
1518
|
+
}
|
|
1519
|
+
} else if (tc.input && typeof tc.input === "object") {
|
|
1520
|
+
args = tc.input;
|
|
1521
|
+
}
|
|
1522
|
+
const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
|
|
1523
|
+
const coerced = RXML2.coerceBySchema(args, schema);
|
|
1524
|
+
return {
|
|
1525
|
+
...part,
|
|
1526
|
+
input: JSON.stringify(coerced != null ? coerced : {})
|
|
1527
|
+
};
|
|
1528
|
+
}
|
|
2293
1529
|
|
|
2294
1530
|
// src/stream-handler.ts
|
|
2295
1531
|
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
@@ -2299,7 +1535,7 @@ async function wrapStream({
|
|
|
2299
1535
|
doGenerate,
|
|
2300
1536
|
params
|
|
2301
1537
|
}) {
|
|
2302
|
-
var _a;
|
|
1538
|
+
var _a, _b, _c;
|
|
2303
1539
|
if (isToolChoiceActive(params)) {
|
|
2304
1540
|
return toolChoiceStream({
|
|
2305
1541
|
doGenerate,
|
|
@@ -2308,10 +1544,12 @@ async function wrapStream({
|
|
|
2308
1544
|
}
|
|
2309
1545
|
const { stream, ...rest } = await doStream();
|
|
2310
1546
|
const debugLevel = getDebugLevel();
|
|
2311
|
-
const tools =
|
|
1547
|
+
const tools = originalToolsSchema.decode(
|
|
1548
|
+
(_b = (_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware) == null ? void 0 : _b.originalTools
|
|
1549
|
+
);
|
|
2312
1550
|
const options = {
|
|
2313
1551
|
...extractOnErrorOption(params.providerOptions),
|
|
2314
|
-
...(
|
|
1552
|
+
...(_c = params.providerOptions) == null ? void 0 : _c.toolCallMiddleware
|
|
2315
1553
|
};
|
|
2316
1554
|
if (debugLevel === "off") {
|
|
2317
1555
|
return {
|
|
@@ -2520,10 +1758,10 @@ async function transformParams({
|
|
|
2520
1758
|
...params.providerOptions || {},
|
|
2521
1759
|
toolCallMiddleware: {
|
|
2522
1760
|
...params.providerOptions && typeof params.providerOptions === "object" && params.providerOptions.toolCallMiddleware || {},
|
|
2523
|
-
// INTERNAL: used by the middleware
|
|
2524
|
-
//
|
|
2525
|
-
//
|
|
2526
|
-
|
|
1761
|
+
// INTERNAL: used by the middleware so downstream parsers can access
|
|
1762
|
+
// the original tool schemas even if providers strip `params.tools`.
|
|
1763
|
+
// Not a stable public API.
|
|
1764
|
+
originalTools: originalToolsSchema.encode(functionTools)
|
|
2527
1765
|
}
|
|
2528
1766
|
}
|
|
2529
1767
|
};
|
|
@@ -2804,18 +2042,35 @@ When an argument is an array, write each item inside a single element on one lin
|
|
|
2804
2042
|
Examples:
|
|
2805
2043
|
<get_weather>
|
|
2806
2044
|
<location>
|
|
2807
|
-
San
|
|
2045
|
+
San Francisco
|
|
2808
2046
|
</location>
|
|
2809
2047
|
</get_weather>`;
|
|
2810
2048
|
}
|
|
2811
2049
|
});
|
|
2812
2050
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2813
2051
|
0 && (module.exports = {
|
|
2052
|
+
RJSON,
|
|
2053
|
+
createDynamicIfThenElseSchema,
|
|
2814
2054
|
createToolMiddleware,
|
|
2055
|
+
decodeOriginalTools,
|
|
2056
|
+
encodeOriginalTools,
|
|
2057
|
+
escapeRegExp,
|
|
2058
|
+
extractOnErrorOption,
|
|
2059
|
+
extractToolNamesFromOriginalTools,
|
|
2815
2060
|
gemmaToolMiddleware,
|
|
2061
|
+
getDebugLevel,
|
|
2062
|
+
getPotentialStartIndex,
|
|
2063
|
+
hasInputProperty,
|
|
2816
2064
|
hermesToolMiddleware,
|
|
2065
|
+
isToolCallContent,
|
|
2066
|
+
isToolChoiceActive,
|
|
2067
|
+
isToolResultPart,
|
|
2817
2068
|
jsonMixProtocol,
|
|
2069
|
+
logParsedChunk,
|
|
2070
|
+
logParsedSummary,
|
|
2071
|
+
logRawChunk,
|
|
2818
2072
|
morphXmlProtocol,
|
|
2073
|
+
originalToolsSchema,
|
|
2819
2074
|
xmlToolMiddleware
|
|
2820
2075
|
});
|
|
2821
2076
|
//# sourceMappingURL=index.cjs.map
|