@ai-sdk-tool/parser 2.1.0 → 2.1.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.
- package/dist/index.cjs +729 -280
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +90 -1
- package/dist/index.d.ts +90 -1
- package/dist/index.js +725 -280
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -20,17 +20,21 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
coerceBySchema: () => coerceBySchema,
|
|
23
24
|
createToolMiddleware: () => createToolMiddleware,
|
|
25
|
+
fixToolCallWithSchema: () => fixToolCallWithSchema,
|
|
24
26
|
gemmaToolMiddleware: () => gemmaToolMiddleware,
|
|
27
|
+
getSchemaType: () => getSchemaType,
|
|
25
28
|
hermesToolMiddleware: () => hermesToolMiddleware,
|
|
26
29
|
jsonMixProtocol: () => jsonMixProtocol,
|
|
30
|
+
unwrapJsonSchema: () => unwrapJsonSchema,
|
|
27
31
|
xmlProtocol: () => xmlProtocol,
|
|
28
32
|
xmlToolMiddleware: () => xmlToolMiddleware
|
|
29
33
|
});
|
|
30
34
|
module.exports = __toCommonJS(index_exports);
|
|
31
35
|
|
|
32
|
-
// src/
|
|
33
|
-
var
|
|
36
|
+
// src/stream-handler.ts
|
|
37
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
34
38
|
|
|
35
39
|
// src/utils/dynamic-tool-schema.ts
|
|
36
40
|
function createDynamicIfThenElseSchema(tools) {
|
|
@@ -440,7 +444,6 @@ function appendPair(state, obj, key, value) {
|
|
|
440
444
|
}
|
|
441
445
|
function parsePair(tokens, state, obj) {
|
|
442
446
|
let token = skipPunctuation(tokens, state, [":", "string", "number", "atom"]);
|
|
443
|
-
let key;
|
|
444
447
|
let value;
|
|
445
448
|
if (token.type !== "string") {
|
|
446
449
|
raiseUnexpected(state, token, "string key");
|
|
@@ -490,7 +493,7 @@ function parsePair(tokens, state, obj) {
|
|
|
490
493
|
}
|
|
491
494
|
}
|
|
492
495
|
checkDuplicates(state, obj, token);
|
|
493
|
-
key = String(token.value);
|
|
496
|
+
const key = String(token.value);
|
|
494
497
|
skipColon(tokens, state);
|
|
495
498
|
value = parseAny(tokens, state);
|
|
496
499
|
appendPair(state, obj, key, value);
|
|
@@ -661,7 +664,10 @@ function parse(text, optsOrReviver) {
|
|
|
661
664
|
if (!options.relaxed && !options.warnings && !options.tolerant) {
|
|
662
665
|
if (!options.duplicate) {
|
|
663
666
|
} else {
|
|
664
|
-
return JSON.parse(
|
|
667
|
+
return JSON.parse(
|
|
668
|
+
text,
|
|
669
|
+
options.reviver
|
|
670
|
+
);
|
|
665
671
|
}
|
|
666
672
|
}
|
|
667
673
|
const lexerToUse = options.relaxed ? lexer : strictLexer;
|
|
@@ -681,7 +687,7 @@ function parse(text, optsOrReviver) {
|
|
|
681
687
|
};
|
|
682
688
|
return parseAny(tokens, state, true);
|
|
683
689
|
} else {
|
|
684
|
-
|
|
690
|
+
tokens.reduce((str, token) => {
|
|
685
691
|
return str + token.match;
|
|
686
692
|
}, "");
|
|
687
693
|
if (!options.relaxed && !options.warnings && !options.tolerant && options.duplicate) {
|
|
@@ -705,8 +711,11 @@ function parse(text, optsOrReviver) {
|
|
|
705
711
|
} else {
|
|
706
712
|
tokens = lexer(text);
|
|
707
713
|
tokens = stripTrailingComma(tokens);
|
|
708
|
-
const
|
|
709
|
-
return JSON.parse(
|
|
714
|
+
const newtext = tokens.reduce((str, token) => str + token.match, "");
|
|
715
|
+
return JSON.parse(
|
|
716
|
+
newtext,
|
|
717
|
+
options.reviver
|
|
718
|
+
);
|
|
710
719
|
}
|
|
711
720
|
}
|
|
712
721
|
}
|
|
@@ -760,7 +769,11 @@ function isToolChoiceActive(params) {
|
|
|
760
769
|
}
|
|
761
770
|
function getFunctionTools(params) {
|
|
762
771
|
var _a, _b;
|
|
763
|
-
const
|
|
772
|
+
const functionTools = ((_a = params.tools) != null ? _a : []).filter(
|
|
773
|
+
(t) => t.type === "function"
|
|
774
|
+
);
|
|
775
|
+
if (functionTools.length > 0) return functionTools;
|
|
776
|
+
const rawToolNames = params.providerOptions && typeof params.providerOptions === "object" && ((_b = params.providerOptions.toolCallMiddleware) == null ? void 0 : _b.toolNames) || [];
|
|
764
777
|
const toStringArray = (val) => Array.isArray(val) ? val.filter(
|
|
765
778
|
(item) => typeof item === "string"
|
|
766
779
|
) : [];
|
|
@@ -773,9 +786,7 @@ function getFunctionTools(params) {
|
|
|
773
786
|
inputSchema: { type: "object" }
|
|
774
787
|
}));
|
|
775
788
|
}
|
|
776
|
-
return
|
|
777
|
-
(t) => t.type === "function"
|
|
778
|
-
);
|
|
789
|
+
return [];
|
|
779
790
|
}
|
|
780
791
|
|
|
781
792
|
// src/utils/on-error.ts
|
|
@@ -788,8 +799,243 @@ function extractOnErrorOption(providerOptions) {
|
|
|
788
799
|
return void 0;
|
|
789
800
|
}
|
|
790
801
|
|
|
802
|
+
// src/utils/coercion.ts
|
|
803
|
+
function unwrapJsonSchema(schema) {
|
|
804
|
+
if (!schema || typeof schema !== "object") return schema;
|
|
805
|
+
const s = schema;
|
|
806
|
+
if (s.jsonSchema && typeof s.jsonSchema === "object") {
|
|
807
|
+
return unwrapJsonSchema(s.jsonSchema);
|
|
808
|
+
}
|
|
809
|
+
return schema;
|
|
810
|
+
}
|
|
811
|
+
function getSchemaType(schema) {
|
|
812
|
+
const unwrapped = unwrapJsonSchema(schema);
|
|
813
|
+
if (!unwrapped || typeof unwrapped !== "object") return void 0;
|
|
814
|
+
const t = unwrapped.type;
|
|
815
|
+
if (typeof t === "string") return t;
|
|
816
|
+
if (Array.isArray(t)) {
|
|
817
|
+
const preferred = [
|
|
818
|
+
"object",
|
|
819
|
+
"array",
|
|
820
|
+
"boolean",
|
|
821
|
+
"number",
|
|
822
|
+
"integer",
|
|
823
|
+
"string"
|
|
824
|
+
];
|
|
825
|
+
for (const p of preferred) if (t.includes(p)) return p;
|
|
826
|
+
}
|
|
827
|
+
const s = unwrapped;
|
|
828
|
+
if (s && typeof s === "object" && (s.properties || s.additionalProperties)) {
|
|
829
|
+
return "object";
|
|
830
|
+
}
|
|
831
|
+
if (s && typeof s === "object" && (s.items || s.prefixItems)) {
|
|
832
|
+
return "array";
|
|
833
|
+
}
|
|
834
|
+
return void 0;
|
|
835
|
+
}
|
|
836
|
+
function coerceBySchema(value, schema) {
|
|
837
|
+
const unwrapped = unwrapJsonSchema(schema);
|
|
838
|
+
if (!unwrapped || typeof unwrapped !== "object") {
|
|
839
|
+
if (typeof value === "string") {
|
|
840
|
+
const s = value.trim();
|
|
841
|
+
const lower = s.toLowerCase();
|
|
842
|
+
if (lower === "true") return true;
|
|
843
|
+
if (lower === "false") return false;
|
|
844
|
+
if (/^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) {
|
|
845
|
+
const num = Number(s);
|
|
846
|
+
if (Number.isFinite(num)) return num;
|
|
847
|
+
}
|
|
848
|
+
if (s.startsWith("{") && s.endsWith("}") || s.startsWith("[") && s.endsWith("]")) {
|
|
849
|
+
try {
|
|
850
|
+
const parsed = JSON.parse(s);
|
|
851
|
+
return coerceBySchema(parsed, void 0);
|
|
852
|
+
} catch (e) {
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return value;
|
|
857
|
+
}
|
|
858
|
+
const schemaType = getSchemaType(unwrapped);
|
|
859
|
+
if (typeof value === "string") {
|
|
860
|
+
const s = value.trim();
|
|
861
|
+
if (schemaType === "object") {
|
|
862
|
+
try {
|
|
863
|
+
let normalized = s.replace(/'/g, '"');
|
|
864
|
+
normalized = normalized.replace(/^\{\s*\}$/s, "{}");
|
|
865
|
+
const obj = JSON.parse(normalized);
|
|
866
|
+
if (obj && typeof obj === "object" && !Array.isArray(obj)) {
|
|
867
|
+
const props = unwrapped.properties;
|
|
868
|
+
const out = {};
|
|
869
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
870
|
+
const propSchema = props ? props[k] : void 0;
|
|
871
|
+
out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
|
|
872
|
+
}
|
|
873
|
+
return out;
|
|
874
|
+
}
|
|
875
|
+
} catch (e) {
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
if (schemaType === "array") {
|
|
879
|
+
try {
|
|
880
|
+
const normalized = s.replace(/'/g, '"');
|
|
881
|
+
const arr = JSON.parse(normalized);
|
|
882
|
+
if (Array.isArray(arr)) {
|
|
883
|
+
const u = unwrapped;
|
|
884
|
+
const prefixItems = Array.isArray(
|
|
885
|
+
u.prefixItems
|
|
886
|
+
) ? u.prefixItems : void 0;
|
|
887
|
+
const itemsSchema = u.items;
|
|
888
|
+
if (prefixItems && arr.length === prefixItems.length) {
|
|
889
|
+
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
890
|
+
}
|
|
891
|
+
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
892
|
+
}
|
|
893
|
+
} catch (e) {
|
|
894
|
+
const csv = s.includes("\n") ? s.split(/\n+/) : s.split(/,\s*/);
|
|
895
|
+
const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);
|
|
896
|
+
const u = unwrapped;
|
|
897
|
+
const prefixItems = Array.isArray(
|
|
898
|
+
u.prefixItems
|
|
899
|
+
) ? u.prefixItems : void 0;
|
|
900
|
+
const itemsSchema = u.items;
|
|
901
|
+
if (prefixItems && trimmed.length === prefixItems.length) {
|
|
902
|
+
return trimmed.map((x, i) => coerceBySchema(x, prefixItems[i]));
|
|
903
|
+
}
|
|
904
|
+
return trimmed.map((x) => coerceBySchema(x, itemsSchema));
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
if (schemaType === "object" && value && typeof value === "object" && !Array.isArray(value)) {
|
|
909
|
+
const out = {};
|
|
910
|
+
const props = unwrapped.properties;
|
|
911
|
+
for (const [k, v] of Object.entries(value)) {
|
|
912
|
+
const propSchema = props ? props[k] : void 0;
|
|
913
|
+
out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
|
|
914
|
+
}
|
|
915
|
+
return out;
|
|
916
|
+
}
|
|
917
|
+
if (schemaType === "array") {
|
|
918
|
+
const u = unwrapped;
|
|
919
|
+
const itemsSchema = u.items;
|
|
920
|
+
const prefixItems = Array.isArray(
|
|
921
|
+
u.prefixItems
|
|
922
|
+
) ? u.prefixItems : void 0;
|
|
923
|
+
if (Array.isArray(value)) {
|
|
924
|
+
if (prefixItems && value.length === prefixItems.length) {
|
|
925
|
+
return value.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
926
|
+
}
|
|
927
|
+
return value.map((v) => coerceBySchema(v, itemsSchema));
|
|
928
|
+
}
|
|
929
|
+
if (value && typeof value === "object") {
|
|
930
|
+
const maybe = value;
|
|
931
|
+
if (Object.prototype.hasOwnProperty.call(maybe, "item")) {
|
|
932
|
+
const items = maybe.item;
|
|
933
|
+
const arr = Array.isArray(items) ? items : [items];
|
|
934
|
+
if (prefixItems && arr.length === prefixItems.length) {
|
|
935
|
+
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
936
|
+
}
|
|
937
|
+
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
938
|
+
}
|
|
939
|
+
const keys = Object.keys(maybe);
|
|
940
|
+
if (keys.length === 1) {
|
|
941
|
+
const singleKey = keys[0];
|
|
942
|
+
const singleValue = maybe[singleKey];
|
|
943
|
+
if (Array.isArray(singleValue)) {
|
|
944
|
+
const coercedArray = singleValue.map(
|
|
945
|
+
(v) => coerceBySchema(v, itemsSchema)
|
|
946
|
+
);
|
|
947
|
+
return coercedArray;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
if (keys.length > 0 && keys.every((k) => /^\d+$/.test(k))) {
|
|
951
|
+
const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
|
|
952
|
+
if (prefixItems && arr.length === prefixItems.length) {
|
|
953
|
+
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
954
|
+
}
|
|
955
|
+
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
959
|
+
if (prefixItems && prefixItems.length > 0) {
|
|
960
|
+
return [coerceBySchema(value, prefixItems[0])];
|
|
961
|
+
}
|
|
962
|
+
return [coerceBySchema(value, itemsSchema)];
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
if (typeof value === "string") {
|
|
966
|
+
const s = value.trim();
|
|
967
|
+
if (schemaType === "boolean") {
|
|
968
|
+
const lower = s.toLowerCase();
|
|
969
|
+
if (lower === "true") return true;
|
|
970
|
+
if (lower === "false") return false;
|
|
971
|
+
}
|
|
972
|
+
if (schemaType === "number" || schemaType === "integer") {
|
|
973
|
+
if (/^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) {
|
|
974
|
+
const num = Number(s);
|
|
975
|
+
if (Number.isFinite(num)) return num;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
return value;
|
|
980
|
+
}
|
|
981
|
+
function fixToolCallWithSchema(part, tools) {
|
|
982
|
+
var _a;
|
|
983
|
+
if (part.type !== "tool-call") return part;
|
|
984
|
+
const tc = part;
|
|
985
|
+
let args = {};
|
|
986
|
+
if (typeof tc.input === "string") {
|
|
987
|
+
try {
|
|
988
|
+
args = JSON.parse(tc.input);
|
|
989
|
+
} catch (e) {
|
|
990
|
+
return part;
|
|
991
|
+
}
|
|
992
|
+
} else if (tc.input && typeof tc.input === "object") {
|
|
993
|
+
args = tc.input;
|
|
994
|
+
}
|
|
995
|
+
const schema = (_a = tools.find((t) => t.name === tc.toolName)) == null ? void 0 : _a.inputSchema;
|
|
996
|
+
const coerced = coerceBySchema(args, schema);
|
|
997
|
+
return {
|
|
998
|
+
...part,
|
|
999
|
+
input: JSON.stringify(coerced != null ? coerced : {})
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
function coerceToolCallInput(part, tools) {
|
|
1003
|
+
return fixToolCallWithSchema(part, tools);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// src/utils/protocol.ts
|
|
1007
|
+
function isProtocolFactory(protocol) {
|
|
1008
|
+
return typeof protocol === "function";
|
|
1009
|
+
}
|
|
1010
|
+
|
|
791
1011
|
// src/stream-handler.ts
|
|
792
|
-
|
|
1012
|
+
async function wrapStream({
|
|
1013
|
+
protocol,
|
|
1014
|
+
doStream,
|
|
1015
|
+
doGenerate,
|
|
1016
|
+
params
|
|
1017
|
+
}) {
|
|
1018
|
+
var _a;
|
|
1019
|
+
if (isToolChoiceActive(params)) {
|
|
1020
|
+
return toolChoiceStream({
|
|
1021
|
+
doGenerate,
|
|
1022
|
+
options: extractOnErrorOption(params.providerOptions)
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
const { stream, ...rest } = await doStream();
|
|
1026
|
+
return {
|
|
1027
|
+
stream: stream.pipeThrough(
|
|
1028
|
+
protocol.createStreamParser({
|
|
1029
|
+
tools: getFunctionTools(params),
|
|
1030
|
+
options: {
|
|
1031
|
+
...extractOnErrorOption(params.providerOptions),
|
|
1032
|
+
...(_a = params.providerOptions) == null ? void 0 : _a.toolCallMiddleware
|
|
1033
|
+
}
|
|
1034
|
+
})
|
|
1035
|
+
),
|
|
1036
|
+
...rest
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
793
1039
|
async function toolChoiceStream({
|
|
794
1040
|
doGenerate,
|
|
795
1041
|
options
|
|
@@ -842,283 +1088,337 @@ async function toolChoiceStream({
|
|
|
842
1088
|
};
|
|
843
1089
|
}
|
|
844
1090
|
|
|
845
|
-
// src/
|
|
846
|
-
|
|
847
|
-
|
|
1091
|
+
// src/generate-handler.ts
|
|
1092
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
1093
|
+
async function wrapGenerate({
|
|
1094
|
+
protocol,
|
|
1095
|
+
doGenerate,
|
|
1096
|
+
params
|
|
1097
|
+
}) {
|
|
1098
|
+
var _a, _b;
|
|
1099
|
+
if (isToolChoiceActive(params)) {
|
|
1100
|
+
const result2 = await doGenerate();
|
|
1101
|
+
let parsed2 = {};
|
|
1102
|
+
const first = (_a = result2.content) == null ? void 0 : _a[0];
|
|
1103
|
+
if (first && first.type === "text") {
|
|
1104
|
+
try {
|
|
1105
|
+
parsed2 = JSON.parse(first.text);
|
|
1106
|
+
} catch (error) {
|
|
1107
|
+
const options = extractOnErrorOption(params.providerOptions);
|
|
1108
|
+
(_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
|
|
1109
|
+
options,
|
|
1110
|
+
"Failed to parse toolChoice JSON from generated model output",
|
|
1111
|
+
{
|
|
1112
|
+
text: first.text,
|
|
1113
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1114
|
+
}
|
|
1115
|
+
);
|
|
1116
|
+
parsed2 = {};
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
const toolCall = {
|
|
1120
|
+
type: "tool-call",
|
|
1121
|
+
toolCallId: (0, import_provider_utils2.generateId)(),
|
|
1122
|
+
toolName: parsed2.name || "unknown",
|
|
1123
|
+
input: JSON.stringify(parsed2.arguments || {})
|
|
1124
|
+
};
|
|
1125
|
+
return {
|
|
1126
|
+
...result2,
|
|
1127
|
+
content: [toolCall]
|
|
1128
|
+
};
|
|
1129
|
+
}
|
|
1130
|
+
const result = await doGenerate();
|
|
1131
|
+
if (result.content.length === 0) {
|
|
1132
|
+
return result;
|
|
1133
|
+
}
|
|
1134
|
+
const parsed = result.content.flatMap((contentItem) => {
|
|
1135
|
+
var _a2;
|
|
1136
|
+
if (contentItem.type !== "text") {
|
|
1137
|
+
return [contentItem];
|
|
1138
|
+
}
|
|
1139
|
+
return protocol.parseGeneratedText({
|
|
1140
|
+
text: contentItem.text,
|
|
1141
|
+
tools: getFunctionTools(params),
|
|
1142
|
+
options: {
|
|
1143
|
+
...extractOnErrorOption(params.providerOptions),
|
|
1144
|
+
...(_a2 = params.providerOptions) == null ? void 0 : _a2.toolCallMiddleware
|
|
1145
|
+
}
|
|
1146
|
+
});
|
|
1147
|
+
});
|
|
1148
|
+
const tools = getFunctionTools(params);
|
|
1149
|
+
const newContent = parsed.map(
|
|
1150
|
+
(part) => coerceToolCallInput(part, tools)
|
|
1151
|
+
);
|
|
1152
|
+
return {
|
|
1153
|
+
...result,
|
|
1154
|
+
content: newContent
|
|
1155
|
+
};
|
|
848
1156
|
}
|
|
849
|
-
|
|
1157
|
+
|
|
1158
|
+
// src/transform-handler.ts
|
|
1159
|
+
async function transformParams({
|
|
1160
|
+
params,
|
|
850
1161
|
protocol,
|
|
851
1162
|
toolSystemPromptTemplate
|
|
852
1163
|
}) {
|
|
1164
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
853
1165
|
const resolvedProtocol = isProtocolFactory(protocol) ? protocol() : protocol;
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
),
|
|
871
|
-
...rest
|
|
872
|
-
};
|
|
1166
|
+
const functionTools = ((_a = params.tools) != null ? _a : []).filter(
|
|
1167
|
+
(t) => t.type === "function"
|
|
1168
|
+
);
|
|
1169
|
+
const systemPrompt = resolvedProtocol.formatTools({
|
|
1170
|
+
tools: functionTools,
|
|
1171
|
+
toolSystemPromptTemplate
|
|
1172
|
+
});
|
|
1173
|
+
const processedPrompt = convertToolPrompt(
|
|
1174
|
+
(_b = params.prompt) != null ? _b : [],
|
|
1175
|
+
resolvedProtocol,
|
|
1176
|
+
extractOnErrorOption(params.providerOptions)
|
|
1177
|
+
);
|
|
1178
|
+
const finalPrompt = ((_c = processedPrompt[0]) == null ? void 0 : _c.role) === "system" ? [
|
|
1179
|
+
{
|
|
1180
|
+
role: "system",
|
|
1181
|
+
content: systemPrompt + "\n\n" + processedPrompt[0].content
|
|
873
1182
|
},
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
1183
|
+
...processedPrompt.slice(1)
|
|
1184
|
+
] : [
|
|
1185
|
+
{
|
|
1186
|
+
role: "system",
|
|
1187
|
+
content: systemPrompt
|
|
1188
|
+
},
|
|
1189
|
+
...processedPrompt
|
|
1190
|
+
];
|
|
1191
|
+
const baseReturnParams = {
|
|
1192
|
+
...params,
|
|
1193
|
+
prompt: finalPrompt,
|
|
1194
|
+
tools: [],
|
|
1195
|
+
toolChoice: void 0,
|
|
1196
|
+
providerOptions: {
|
|
1197
|
+
...params.providerOptions || {},
|
|
1198
|
+
toolCallMiddleware: {
|
|
1199
|
+
...params.providerOptions && typeof params.providerOptions === "object" && params.providerOptions.toolCallMiddleware || {},
|
|
1200
|
+
toolNames: functionTools.map((t) => t.name)
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
if (((_d = params.toolChoice) == null ? void 0 : _d.type) === "none") {
|
|
1205
|
+
throw new Error(
|
|
1206
|
+
"The 'none' toolChoice type is not supported by this middleware. Please use 'auto', 'required', or specify a tool name."
|
|
1207
|
+
);
|
|
1208
|
+
}
|
|
1209
|
+
if (((_e = params.toolChoice) == null ? void 0 : _e.type) === "tool") {
|
|
1210
|
+
const selectedToolName = params.toolChoice.toolName;
|
|
1211
|
+
const providerDefinedMatch = ((_f = params.tools) != null ? _f : []).find((t) => {
|
|
1212
|
+
if (t.type === "function") return false;
|
|
1213
|
+
const anyTool = t;
|
|
1214
|
+
return anyTool.id === selectedToolName || anyTool.name === selectedToolName;
|
|
1215
|
+
});
|
|
1216
|
+
if (providerDefinedMatch) {
|
|
1217
|
+
throw new Error(
|
|
1218
|
+
"Provider-defined tools are not supported by this middleware. Please use custom tools."
|
|
1219
|
+
);
|
|
1220
|
+
}
|
|
1221
|
+
const selectedTool = ((_g = params.tools) != null ? _g : []).find(
|
|
1222
|
+
(t) => t.type === "function" && t.name === selectedToolName
|
|
1223
|
+
);
|
|
1224
|
+
if (!selectedTool) {
|
|
1225
|
+
throw new Error(
|
|
1226
|
+
`Tool with name '${selectedToolName}' not found in params.tools.`
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
return {
|
|
1230
|
+
...baseReturnParams,
|
|
1231
|
+
responseFormat: {
|
|
1232
|
+
type: "json",
|
|
1233
|
+
schema: {
|
|
1234
|
+
type: "object",
|
|
1235
|
+
properties: {
|
|
1236
|
+
name: {
|
|
1237
|
+
const: selectedTool.name
|
|
1238
|
+
},
|
|
1239
|
+
arguments: selectedTool.inputSchema
|
|
1240
|
+
},
|
|
1241
|
+
required: ["name", "arguments"]
|
|
1242
|
+
},
|
|
1243
|
+
name: selectedTool.name,
|
|
1244
|
+
description: typeof selectedTool.description === "string" ? selectedTool.description : void 0
|
|
1245
|
+
},
|
|
1246
|
+
providerOptions: {
|
|
1247
|
+
...baseReturnParams.providerOptions || {},
|
|
1248
|
+
toolCallMiddleware: {
|
|
1249
|
+
...baseReturnParams.providerOptions && typeof baseReturnParams.providerOptions === "object" && baseReturnParams.providerOptions.toolCallMiddleware || {},
|
|
1250
|
+
toolChoice: params.toolChoice
|
|
895
1251
|
}
|
|
896
|
-
return {
|
|
897
|
-
...result2,
|
|
898
|
-
content: [
|
|
899
|
-
{
|
|
900
|
-
type: "tool-call",
|
|
901
|
-
toolCallId: (0, import_provider_utils2.generateId)(),
|
|
902
|
-
toolName: parsed.name || "unknown",
|
|
903
|
-
input: JSON.stringify(parsed.arguments || {})
|
|
904
|
-
}
|
|
905
|
-
]
|
|
906
|
-
};
|
|
907
1252
|
}
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
if (((_h = params.toolChoice) == null ? void 0 : _h.type) === "required") {
|
|
1256
|
+
if (!params.tools || params.tools.length === 0) {
|
|
1257
|
+
throw new Error(
|
|
1258
|
+
"Tool choice type 'required' is set, but no tools are provided in params.tools."
|
|
1259
|
+
);
|
|
1260
|
+
}
|
|
1261
|
+
return {
|
|
1262
|
+
...baseReturnParams,
|
|
1263
|
+
responseFormat: {
|
|
1264
|
+
type: "json",
|
|
1265
|
+
schema: createDynamicIfThenElseSchema(functionTools)
|
|
1266
|
+
},
|
|
1267
|
+
providerOptions: {
|
|
1268
|
+
...baseReturnParams.providerOptions || {},
|
|
1269
|
+
toolCallMiddleware: {
|
|
1270
|
+
...baseReturnParams.providerOptions && typeof baseReturnParams.providerOptions === "object" && baseReturnParams.providerOptions.toolCallMiddleware || {},
|
|
1271
|
+
toolChoice: { type: "required" }
|
|
1272
|
+
}
|
|
911
1273
|
}
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
1276
|
+
return baseReturnParams;
|
|
1277
|
+
}
|
|
1278
|
+
function convertToolPrompt(prompt, resolvedProtocol, providerOptions) {
|
|
1279
|
+
const processedPrompt = prompt.map((message) => {
|
|
1280
|
+
var _a;
|
|
1281
|
+
if (message.role === "assistant") {
|
|
1282
|
+
const newContent = [];
|
|
1283
|
+
for (const content of message.content) {
|
|
1284
|
+
if (isToolCallContent(content)) {
|
|
1285
|
+
newContent.push({
|
|
1286
|
+
type: "text",
|
|
1287
|
+
text: resolvedProtocol.formatToolCall(content)
|
|
1288
|
+
});
|
|
1289
|
+
} else if (content.type === "text") {
|
|
1290
|
+
newContent.push(content);
|
|
1291
|
+
} else if (content.type === "reasoning") {
|
|
1292
|
+
newContent.push(content);
|
|
1293
|
+
} else {
|
|
1294
|
+
const options = extractOnErrorOption(providerOptions);
|
|
1295
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
1296
|
+
options,
|
|
1297
|
+
"tool-call-middleware: unknown assistant content; stringifying for provider compatibility",
|
|
1298
|
+
{ content }
|
|
1299
|
+
);
|
|
1300
|
+
newContent.push({
|
|
1301
|
+
type: "text",
|
|
1302
|
+
text: JSON.stringify(content)
|
|
1303
|
+
});
|
|
915
1304
|
}
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
1305
|
+
}
|
|
1306
|
+
const onlyText = newContent.every((c) => c.type === "text");
|
|
1307
|
+
const condensedAssistant = onlyText ? [
|
|
1308
|
+
{
|
|
1309
|
+
type: "text",
|
|
1310
|
+
text: newContent.map((c) => c.text).join("\n")
|
|
1311
|
+
}
|
|
1312
|
+
] : newContent;
|
|
1313
|
+
return { role: "assistant", content: condensedAssistant };
|
|
1314
|
+
}
|
|
1315
|
+
if (message.role === "tool") {
|
|
922
1316
|
return {
|
|
923
|
-
|
|
924
|
-
|
|
1317
|
+
role: "user",
|
|
1318
|
+
// Map tool results to text response blocks, then condense into a single text block
|
|
1319
|
+
content: [
|
|
1320
|
+
{
|
|
1321
|
+
type: "text",
|
|
1322
|
+
text: message.content.map(
|
|
1323
|
+
(toolResult) => isToolResultPart(toolResult) ? resolvedProtocol.formatToolResponse(toolResult) : resolvedProtocol.formatToolResponse(
|
|
1324
|
+
toolResult
|
|
1325
|
+
)
|
|
1326
|
+
).join("\n")
|
|
1327
|
+
}
|
|
1328
|
+
]
|
|
925
1329
|
};
|
|
926
|
-
}
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
{ content }
|
|
950
|
-
);
|
|
951
|
-
newContent.push({
|
|
952
|
-
type: "text",
|
|
953
|
-
text: JSON.stringify(content)
|
|
954
|
-
});
|
|
1330
|
+
}
|
|
1331
|
+
return message;
|
|
1332
|
+
});
|
|
1333
|
+
for (let i = 0; i < processedPrompt.length; i++) {
|
|
1334
|
+
const msg = processedPrompt[i];
|
|
1335
|
+
if (Array.isArray(msg.content)) {
|
|
1336
|
+
const allText = msg.content.every(
|
|
1337
|
+
(c) => (c == null ? void 0 : c.type) === "text"
|
|
1338
|
+
);
|
|
1339
|
+
if (allText && msg.content.length > 1) {
|
|
1340
|
+
const joinedText = msg.content.map((c) => c.text).join("\n");
|
|
1341
|
+
if (msg.role === "system") {
|
|
1342
|
+
processedPrompt[i] = {
|
|
1343
|
+
role: "system",
|
|
1344
|
+
content: joinedText
|
|
1345
|
+
};
|
|
1346
|
+
} else if (msg.role === "assistant") {
|
|
1347
|
+
processedPrompt[i] = {
|
|
1348
|
+
role: "assistant",
|
|
1349
|
+
content: [
|
|
1350
|
+
{
|
|
1351
|
+
type: "text",
|
|
1352
|
+
text: joinedText
|
|
955
1353
|
}
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
1354
|
+
]
|
|
1355
|
+
};
|
|
1356
|
+
} else {
|
|
1357
|
+
processedPrompt[i] = {
|
|
1358
|
+
role: "user",
|
|
1359
|
+
content: [
|
|
959
1360
|
{
|
|
960
1361
|
type: "text",
|
|
961
|
-
text:
|
|
1362
|
+
text: joinedText
|
|
962
1363
|
}
|
|
963
|
-
]
|
|
964
|
-
|
|
965
|
-
}
|
|
966
|
-
if (message.role === "tool") {
|
|
967
|
-
return {
|
|
968
|
-
role: "user",
|
|
969
|
-
// Map tool results to text response blocks, then condense into a single text block
|
|
970
|
-
content: [
|
|
971
|
-
{
|
|
972
|
-
type: "text",
|
|
973
|
-
text: message.content.map(
|
|
974
|
-
(toolResult) => isToolResultPart(toolResult) ? resolvedProtocol.formatToolResponse(toolResult) : resolvedProtocol.formatToolResponse(
|
|
975
|
-
toolResult
|
|
976
|
-
)
|
|
977
|
-
).join("\n")
|
|
978
|
-
}
|
|
979
|
-
]
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
return message;
|
|
983
|
-
});
|
|
984
|
-
for (let i = 0; i < processedPrompt2.length; i++) {
|
|
985
|
-
const msg = processedPrompt2[i];
|
|
986
|
-
if (Array.isArray(msg.content)) {
|
|
987
|
-
const allText = msg.content.every((c) => (c == null ? void 0 : c.type) === "text");
|
|
988
|
-
if (allText && msg.content.length > 1) {
|
|
989
|
-
processedPrompt2[i] = {
|
|
990
|
-
role: msg.role,
|
|
991
|
-
content: [
|
|
992
|
-
{
|
|
993
|
-
type: "text",
|
|
994
|
-
text: msg.content.map((c) => c.text).join("\n")
|
|
995
|
-
}
|
|
996
|
-
]
|
|
997
|
-
};
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
for (let i = processedPrompt2.length - 1; i > 0; i--) {
|
|
1002
|
-
const current = processedPrompt2[i];
|
|
1003
|
-
const prev = processedPrompt2[i - 1];
|
|
1004
|
-
if (current.role === "user" && prev.role === "user") {
|
|
1005
|
-
const prevContent = prev.content.map((c) => c.type === "text" ? c.text : "").join("\n");
|
|
1006
|
-
const currentContent = current.content.map((c) => c.type === "text" ? c.text : "").join("\n");
|
|
1007
|
-
processedPrompt2[i - 1] = {
|
|
1008
|
-
role: "user",
|
|
1009
|
-
content: [
|
|
1010
|
-
{ type: "text", text: prevContent + "\n" + currentContent }
|
|
1011
|
-
]
|
|
1012
|
-
};
|
|
1013
|
-
processedPrompt2.splice(i, 1);
|
|
1014
|
-
}
|
|
1015
|
-
}
|
|
1016
|
-
return processedPrompt2;
|
|
1017
|
-
};
|
|
1018
|
-
const functionTools = ((_a = params.tools) != null ? _a : []).filter(
|
|
1019
|
-
(t) => t.type === "function"
|
|
1020
|
-
);
|
|
1021
|
-
const systemPrompt = resolvedProtocol.formatTools({
|
|
1022
|
-
tools: functionTools,
|
|
1023
|
-
toolSystemPromptTemplate
|
|
1024
|
-
});
|
|
1025
|
-
const processedPrompt = convertToolPrompt(params.prompt);
|
|
1026
|
-
const finalPrompt = ((_b = processedPrompt[0]) == null ? void 0 : _b.role) === "system" ? [
|
|
1027
|
-
{
|
|
1028
|
-
role: "system",
|
|
1029
|
-
content: systemPrompt + "\n\n" + processedPrompt[0].content
|
|
1030
|
-
},
|
|
1031
|
-
...processedPrompt.slice(1)
|
|
1032
|
-
] : [
|
|
1033
|
-
{
|
|
1034
|
-
role: "system",
|
|
1035
|
-
content: systemPrompt
|
|
1036
|
-
},
|
|
1037
|
-
...processedPrompt
|
|
1038
|
-
];
|
|
1039
|
-
const baseReturnParams = {
|
|
1040
|
-
...params,
|
|
1041
|
-
prompt: finalPrompt,
|
|
1042
|
-
tools: [],
|
|
1043
|
-
toolChoice: void 0,
|
|
1044
|
-
providerOptions: {
|
|
1045
|
-
...params.providerOptions || {},
|
|
1046
|
-
toolCallMiddleware: {
|
|
1047
|
-
...params.providerOptions && typeof params.providerOptions === "object" && params.providerOptions.toolCallMiddleware || {},
|
|
1048
|
-
toolNames: functionTools.map((t) => t.name)
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
};
|
|
1052
|
-
if (((_c = params.toolChoice) == null ? void 0 : _c.type) === "none") {
|
|
1053
|
-
throw new Error(
|
|
1054
|
-
"The 'none' toolChoice type is not supported by this middleware. Please use 'auto', 'required', or specify a tool name."
|
|
1055
|
-
);
|
|
1056
|
-
}
|
|
1057
|
-
if (((_d = params.toolChoice) == null ? void 0 : _d.type) === "tool") {
|
|
1058
|
-
const selectedToolName = params.toolChoice.toolName;
|
|
1059
|
-
const selectedTool = (_e = params.tools) == null ? void 0 : _e.find(
|
|
1060
|
-
(tool) => tool.type === "function" ? tool.name === selectedToolName : tool.id === selectedToolName
|
|
1061
|
-
);
|
|
1062
|
-
if (!selectedTool) {
|
|
1063
|
-
throw new Error(
|
|
1064
|
-
`Tool with name '${selectedToolName}' not found in params.tools.`
|
|
1065
|
-
);
|
|
1066
|
-
}
|
|
1067
|
-
if (selectedTool.type === "provider-defined") {
|
|
1068
|
-
throw new Error(
|
|
1069
|
-
"Provider-defined tools are not supported by this middleware. Please use custom tools."
|
|
1070
|
-
);
|
|
1364
|
+
]
|
|
1365
|
+
};
|
|
1071
1366
|
}
|
|
1072
|
-
return {
|
|
1073
|
-
...baseReturnParams,
|
|
1074
|
-
responseFormat: {
|
|
1075
|
-
type: "json",
|
|
1076
|
-
schema: {
|
|
1077
|
-
type: "object",
|
|
1078
|
-
properties: {
|
|
1079
|
-
name: {
|
|
1080
|
-
const: selectedTool.name
|
|
1081
|
-
},
|
|
1082
|
-
arguments: selectedTool.inputSchema
|
|
1083
|
-
},
|
|
1084
|
-
required: ["name", "arguments"]
|
|
1085
|
-
},
|
|
1086
|
-
name: selectedTool.name,
|
|
1087
|
-
description: selectedTool.type === "function" && typeof selectedTool.description === "string" ? selectedTool.description : void 0
|
|
1088
|
-
},
|
|
1089
|
-
providerOptions: {
|
|
1090
|
-
...baseReturnParams.providerOptions || {},
|
|
1091
|
-
toolCallMiddleware: {
|
|
1092
|
-
...baseReturnParams.providerOptions && typeof baseReturnParams.providerOptions === "object" && baseReturnParams.providerOptions.toolCallMiddleware || {},
|
|
1093
|
-
toolChoice: params.toolChoice
|
|
1094
|
-
}
|
|
1095
|
-
}
|
|
1096
|
-
};
|
|
1097
1367
|
}
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
for (let i = processedPrompt.length - 1; i > 0; i--) {
|
|
1371
|
+
const current = processedPrompt[i];
|
|
1372
|
+
const prev = processedPrompt[i - 1];
|
|
1373
|
+
if (current.role === "user" && prev.role === "user") {
|
|
1374
|
+
const prevContent = prev.content.map((c) => c.type === "text" ? c.text : "").join("\n");
|
|
1375
|
+
const currentContent = current.content.map((c) => c.type === "text" ? c.text : "").join("\n");
|
|
1376
|
+
processedPrompt[i - 1] = {
|
|
1377
|
+
role: "user",
|
|
1378
|
+
content: [{ type: "text", text: prevContent + "\n" + currentContent }]
|
|
1379
|
+
};
|
|
1380
|
+
processedPrompt.splice(i, 1);
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
return processedPrompt;
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
// src/tool-call-middleware.ts
|
|
1387
|
+
function createToolMiddleware({
|
|
1388
|
+
protocol,
|
|
1389
|
+
toolSystemPromptTemplate
|
|
1390
|
+
}) {
|
|
1391
|
+
const resolvedProtocol = isProtocolFactory(protocol) ? protocol() : protocol;
|
|
1392
|
+
return {
|
|
1393
|
+
middlewareVersion: "v2",
|
|
1394
|
+
wrapStream: async ({ doStream, doGenerate, params }) => {
|
|
1395
|
+
if (isToolChoiceActive(params)) {
|
|
1396
|
+
return toolChoiceStream({
|
|
1397
|
+
doGenerate,
|
|
1398
|
+
options: extractOnErrorOption(params.providerOptions)
|
|
1399
|
+
});
|
|
1400
|
+
} else {
|
|
1401
|
+
return wrapStream({
|
|
1402
|
+
protocol: resolvedProtocol,
|
|
1403
|
+
doStream,
|
|
1404
|
+
doGenerate,
|
|
1405
|
+
params
|
|
1406
|
+
});
|
|
1120
1407
|
}
|
|
1121
|
-
|
|
1408
|
+
},
|
|
1409
|
+
wrapGenerate: async ({ doGenerate, params }) => wrapGenerate({
|
|
1410
|
+
protocol: resolvedProtocol,
|
|
1411
|
+
doGenerate,
|
|
1412
|
+
params
|
|
1413
|
+
}),
|
|
1414
|
+
transformParams: async ({
|
|
1415
|
+
params
|
|
1416
|
+
}) => {
|
|
1417
|
+
return transformParams({
|
|
1418
|
+
protocol: resolvedProtocol,
|
|
1419
|
+
toolSystemPromptTemplate,
|
|
1420
|
+
params
|
|
1421
|
+
});
|
|
1122
1422
|
}
|
|
1123
1423
|
};
|
|
1124
1424
|
}
|
|
@@ -1359,7 +1659,7 @@ var xmlProtocol = () => ({
|
|
|
1359
1659
|
const toolsForPrompt = (tools || []).map((tool) => ({
|
|
1360
1660
|
name: tool.name,
|
|
1361
1661
|
description: tool.description,
|
|
1362
|
-
parameters: tool.inputSchema
|
|
1662
|
+
parameters: unwrapJsonSchema(tool.inputSchema)
|
|
1363
1663
|
}));
|
|
1364
1664
|
return toolSystemPromptTemplate(JSON.stringify(toolsForPrompt));
|
|
1365
1665
|
},
|
|
@@ -1392,7 +1692,8 @@ var xmlProtocol = () => ({
|
|
|
1392
1692
|
return xmlContent;
|
|
1393
1693
|
},
|
|
1394
1694
|
parseGeneratedText({ text, tools, options }) {
|
|
1395
|
-
var _a, _b;
|
|
1695
|
+
var _a, _b, _c;
|
|
1696
|
+
const originalSchemas = (options == null ? void 0 : options.originalToolSchemas) || {};
|
|
1396
1697
|
const toolNames = tools.map((t) => t.name).filter((name) => name != null);
|
|
1397
1698
|
if (toolNames.length === 0) {
|
|
1398
1699
|
return [{ type: "text", text }];
|
|
@@ -1430,17 +1731,87 @@ var xmlProtocol = () => ({
|
|
|
1430
1731
|
if (v && typeof v === "object" && Object.prototype.hasOwnProperty.call(v, "#text")) {
|
|
1431
1732
|
val = v == null ? void 0 : v["#text"];
|
|
1432
1733
|
}
|
|
1734
|
+
if (Array.isArray(v)) {
|
|
1735
|
+
val = v.map((item) => {
|
|
1736
|
+
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1737
|
+
const textVal = item == null ? void 0 : item["#text"];
|
|
1738
|
+
return typeof textVal === "string" ? textVal.trim() : textVal;
|
|
1739
|
+
}
|
|
1740
|
+
return typeof item === "string" ? item.trim() : item;
|
|
1741
|
+
});
|
|
1742
|
+
} else if (v && typeof v === "object" && !Object.prototype.hasOwnProperty.call(v, "#text")) {
|
|
1743
|
+
const obj = v;
|
|
1744
|
+
const keys = Object.keys(obj);
|
|
1745
|
+
if (keys.length === 1 && keys[0] === "item") {
|
|
1746
|
+
const itemValue = obj.item;
|
|
1747
|
+
if (Array.isArray(itemValue)) {
|
|
1748
|
+
val = itemValue.map((item) => {
|
|
1749
|
+
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1750
|
+
const textVal = item == null ? void 0 : item["#text"];
|
|
1751
|
+
const trimmed2 = typeof textVal === "string" ? textVal.trim() : textVal;
|
|
1752
|
+
if (typeof trimmed2 === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed2)) {
|
|
1753
|
+
const num = Number(trimmed2);
|
|
1754
|
+
if (Number.isFinite(num)) return num;
|
|
1755
|
+
}
|
|
1756
|
+
return trimmed2;
|
|
1757
|
+
}
|
|
1758
|
+
const trimmed = typeof item === "string" ? item.trim() : item;
|
|
1759
|
+
if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
|
|
1760
|
+
const num = Number(trimmed);
|
|
1761
|
+
if (Number.isFinite(num)) return num;
|
|
1762
|
+
}
|
|
1763
|
+
return trimmed;
|
|
1764
|
+
});
|
|
1765
|
+
} else {
|
|
1766
|
+
const trimmed = typeof itemValue === "string" ? itemValue.trim() : itemValue;
|
|
1767
|
+
if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
|
|
1768
|
+
const num = Number(trimmed);
|
|
1769
|
+
if (Number.isFinite(num)) {
|
|
1770
|
+
val = num;
|
|
1771
|
+
} else {
|
|
1772
|
+
val = trimmed;
|
|
1773
|
+
}
|
|
1774
|
+
} else {
|
|
1775
|
+
val = trimmed;
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
} else {
|
|
1779
|
+
const isIndexedTuple = keys.length > 0 && keys.every((key) => /^\d+$/.test(key)) && (() => {
|
|
1780
|
+
const indices = keys.map((k2) => parseInt(k2)).sort((a, b) => a - b);
|
|
1781
|
+
return indices[0] === 0 && indices.every((val2, idx) => val2 === idx);
|
|
1782
|
+
})();
|
|
1783
|
+
if (isIndexedTuple) {
|
|
1784
|
+
const sortedKeys = keys.sort(
|
|
1785
|
+
(a, b) => parseInt(a) - parseInt(b)
|
|
1786
|
+
);
|
|
1787
|
+
val = sortedKeys.map((key) => {
|
|
1788
|
+
const item = obj[key];
|
|
1789
|
+
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1790
|
+
const textVal = item == null ? void 0 : item["#text"];
|
|
1791
|
+
return typeof textVal === "string" ? textVal.trim() : textVal;
|
|
1792
|
+
}
|
|
1793
|
+
return typeof item === "string" ? item.trim() : item;
|
|
1794
|
+
});
|
|
1795
|
+
} else {
|
|
1796
|
+
val = v;
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1433
1800
|
args[k] = typeof val === "string" ? val.trim() : val;
|
|
1434
1801
|
}
|
|
1802
|
+
const originalSchema = originalSchemas[toolName];
|
|
1803
|
+
const fallbackSchema = (_b = tools.find((t) => t.name === toolName)) == null ? void 0 : _b.inputSchema;
|
|
1804
|
+
const schema = originalSchema || fallbackSchema;
|
|
1805
|
+
const coercedArgs = coerceBySchema(args, schema);
|
|
1435
1806
|
processedElements.push({
|
|
1436
1807
|
type: "tool-call",
|
|
1437
1808
|
toolCallId: (0, import_provider_utils4.generateId)(),
|
|
1438
1809
|
toolName,
|
|
1439
|
-
input: JSON.stringify(
|
|
1810
|
+
input: JSON.stringify(coercedArgs)
|
|
1440
1811
|
});
|
|
1441
1812
|
} catch (error) {
|
|
1442
1813
|
const message = `Could not process XML tool call, keeping original text: ${match[0]}`;
|
|
1443
|
-
(
|
|
1814
|
+
(_c = options == null ? void 0 : options.onError) == null ? void 0 : _c.call(options, message, { toolCall: match[0], toolName, error });
|
|
1444
1815
|
processedElements.push({ type: "text", text: match[0] });
|
|
1445
1816
|
}
|
|
1446
1817
|
currentIndex = startIndex + match[0].length;
|
|
@@ -1454,6 +1825,7 @@ var xmlProtocol = () => ({
|
|
|
1454
1825
|
return processedElements;
|
|
1455
1826
|
},
|
|
1456
1827
|
createStreamParser({ tools, options }) {
|
|
1828
|
+
const originalSchemas = (options == null ? void 0 : options.originalToolSchemas) || {};
|
|
1457
1829
|
const toolNames = tools.map((t) => t.name).filter((name) => name != null);
|
|
1458
1830
|
let buffer = "";
|
|
1459
1831
|
let currentToolCall = null;
|
|
@@ -1481,7 +1853,7 @@ var xmlProtocol = () => ({
|
|
|
1481
1853
|
};
|
|
1482
1854
|
return new TransformStream({
|
|
1483
1855
|
transform(chunk, controller) {
|
|
1484
|
-
var _a;
|
|
1856
|
+
var _a, _b;
|
|
1485
1857
|
if (chunk.type !== "text-delta") {
|
|
1486
1858
|
if (buffer) flushText(controller);
|
|
1487
1859
|
controller.enqueue(chunk);
|
|
@@ -1510,14 +1882,86 @@ var xmlProtocol = () => ({
|
|
|
1510
1882
|
if (v && typeof v === "object" && Object.prototype.hasOwnProperty.call(v, "#text")) {
|
|
1511
1883
|
val = v == null ? void 0 : v["#text"];
|
|
1512
1884
|
}
|
|
1885
|
+
if (Array.isArray(v)) {
|
|
1886
|
+
val = v.map((item) => {
|
|
1887
|
+
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1888
|
+
const textVal = item == null ? void 0 : item["#text"];
|
|
1889
|
+
return typeof textVal === "string" ? textVal.trim() : textVal;
|
|
1890
|
+
}
|
|
1891
|
+
return typeof item === "string" ? item.trim() : item;
|
|
1892
|
+
});
|
|
1893
|
+
} else if (v && typeof v === "object" && !Object.prototype.hasOwnProperty.call(v, "#text")) {
|
|
1894
|
+
const obj = v;
|
|
1895
|
+
const keys = Object.keys(obj);
|
|
1896
|
+
if (keys.length === 1 && keys[0] === "item") {
|
|
1897
|
+
const itemValue = obj.item;
|
|
1898
|
+
if (Array.isArray(itemValue)) {
|
|
1899
|
+
val = itemValue.map((item) => {
|
|
1900
|
+
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1901
|
+
const textVal = item == null ? void 0 : item["#text"];
|
|
1902
|
+
const trimmed2 = typeof textVal === "string" ? textVal.trim() : textVal;
|
|
1903
|
+
if (typeof trimmed2 === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed2)) {
|
|
1904
|
+
const num = Number(trimmed2);
|
|
1905
|
+
if (Number.isFinite(num)) return num;
|
|
1906
|
+
}
|
|
1907
|
+
return trimmed2;
|
|
1908
|
+
}
|
|
1909
|
+
const trimmed = typeof item === "string" ? item.trim() : item;
|
|
1910
|
+
if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
|
|
1911
|
+
const num = Number(trimmed);
|
|
1912
|
+
if (Number.isFinite(num)) return num;
|
|
1913
|
+
}
|
|
1914
|
+
return trimmed;
|
|
1915
|
+
});
|
|
1916
|
+
} else {
|
|
1917
|
+
const trimmed = typeof itemValue === "string" ? itemValue.trim() : itemValue;
|
|
1918
|
+
if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
|
|
1919
|
+
const num = Number(trimmed);
|
|
1920
|
+
if (Number.isFinite(num)) {
|
|
1921
|
+
val = num;
|
|
1922
|
+
} else {
|
|
1923
|
+
val = trimmed;
|
|
1924
|
+
}
|
|
1925
|
+
} else {
|
|
1926
|
+
val = trimmed;
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
} else {
|
|
1930
|
+
const isIndexedTuple = keys.length > 0 && keys.every((key) => /^\d+$/.test(key)) && (() => {
|
|
1931
|
+
const indices = keys.map((k2) => parseInt(k2)).sort((a, b) => a - b);
|
|
1932
|
+
return indices[0] === 0 && indices.every((val2, idx) => val2 === idx);
|
|
1933
|
+
})();
|
|
1934
|
+
if (isIndexedTuple) {
|
|
1935
|
+
const sortedKeys = keys.sort(
|
|
1936
|
+
(a, b) => parseInt(a) - parseInt(b)
|
|
1937
|
+
);
|
|
1938
|
+
val = sortedKeys.map((key) => {
|
|
1939
|
+
const item = obj[key];
|
|
1940
|
+
if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
|
|
1941
|
+
const textVal = item == null ? void 0 : item["#text"];
|
|
1942
|
+
return typeof textVal === "string" ? textVal.trim() : textVal;
|
|
1943
|
+
}
|
|
1944
|
+
return typeof item === "string" ? item.trim() : item;
|
|
1945
|
+
});
|
|
1946
|
+
} else {
|
|
1947
|
+
val = v;
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1513
1951
|
args[k] = typeof val === "string" ? val.trim() : val;
|
|
1514
1952
|
}
|
|
1953
|
+
const originalSchema = originalSchemas[currentToolCall.name];
|
|
1954
|
+
const fallbackSchema = (_b = tools.find(
|
|
1955
|
+
(t) => t.name === currentToolCall.name
|
|
1956
|
+
)) == null ? void 0 : _b.inputSchema;
|
|
1957
|
+
const toolSchema = originalSchema || fallbackSchema;
|
|
1958
|
+
const coercedArgs = coerceBySchema(args, toolSchema);
|
|
1515
1959
|
flushText(controller);
|
|
1516
1960
|
controller.enqueue({
|
|
1517
1961
|
type: "tool-call",
|
|
1518
1962
|
toolCallId: (0, import_provider_utils4.generateId)(),
|
|
1519
1963
|
toolName: currentToolCall.name,
|
|
1520
|
-
input: JSON.stringify(
|
|
1964
|
+
input: JSON.stringify(coercedArgs)
|
|
1521
1965
|
});
|
|
1522
1966
|
} catch (e) {
|
|
1523
1967
|
const originalCallText = `<${currentToolCall.name}>${toolContent}${endTag}`;
|
|
@@ -1621,13 +2065,14 @@ For each function call return a json object with function name and arguments wit
|
|
|
1621
2065
|
var xmlToolMiddleware = createToolMiddleware({
|
|
1622
2066
|
protocol: xmlProtocol,
|
|
1623
2067
|
toolSystemPromptTemplate(tools) {
|
|
1624
|
-
return `You are
|
|
2068
|
+
return `You are a function calling AI model.
|
|
1625
2069
|
You are provided with function signatures within <tools></tools> XML tags.
|
|
1626
2070
|
You may call one or more functions to assist with the user query.
|
|
1627
2071
|
Don't make assumptions about what values to plug into functions.
|
|
1628
2072
|
Here are the available tools: <tools>${tools}</tools>
|
|
1629
|
-
For
|
|
1630
|
-
|
|
2073
|
+
For a function call, return exactly one XML element whose tag name matches the tool's name, and nothing else.
|
|
2074
|
+
When an argument is an array, write each item inside a single element on one line separated by commas (or provide a JSON-like list). When an argument is an object, provide a JSON-like value.
|
|
2075
|
+
Examples:
|
|
1631
2076
|
<get_weather>
|
|
1632
2077
|
<location>
|
|
1633
2078
|
San Fransisco
|
|
@@ -1637,10 +2082,14 @@ San Fransisco
|
|
|
1637
2082
|
});
|
|
1638
2083
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1639
2084
|
0 && (module.exports = {
|
|
2085
|
+
coerceBySchema,
|
|
1640
2086
|
createToolMiddleware,
|
|
2087
|
+
fixToolCallWithSchema,
|
|
1641
2088
|
gemmaToolMiddleware,
|
|
2089
|
+
getSchemaType,
|
|
1642
2090
|
hermesToolMiddleware,
|
|
1643
2091
|
jsonMixProtocol,
|
|
2092
|
+
unwrapJsonSchema,
|
|
1644
2093
|
xmlProtocol,
|
|
1645
2094
|
xmlToolMiddleware
|
|
1646
2095
|
});
|