@ai-sdk-tool/parser 3.2.0 → 3.3.0
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/README.md +24 -116
- package/dist/{chunk-PIUBQRFC.js → chunk-3QJVNEHE.js} +369 -59
- package/dist/chunk-3QJVNEHE.js.map +1 -0
- package/dist/chunk-DZB6Y354.js +240 -0
- package/dist/chunk-DZB6Y354.js.map +1 -0
- package/dist/chunk-IX4FJELL.js +671 -0
- package/dist/chunk-IX4FJELL.js.map +1 -0
- package/dist/chunk-TR2ARLIF.js +2216 -0
- package/dist/chunk-TR2ARLIF.js.map +1 -0
- package/dist/community.cjs +3547 -223
- package/dist/community.cjs.map +1 -1
- package/dist/community.js +4 -1
- package/dist/community.js.map +1 -1
- package/dist/index.cjs +3584 -175
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +11 -1
- package/dist/rjson.cjs +697 -0
- package/dist/rjson.cjs.map +1 -0
- package/dist/rjson.d.cts +108 -0
- package/dist/rjson.d.ts +108 -0
- package/dist/rjson.js +11 -0
- package/dist/rjson.js.map +1 -0
- package/dist/rxml.cjs +2471 -0
- package/dist/rxml.cjs.map +1 -0
- package/dist/rxml.d.cts +90 -0
- package/dist/rxml.d.ts +90 -0
- package/dist/rxml.js +10 -0
- package/dist/rxml.js.map +1 -0
- package/dist/schema-coerce.cjs +266 -0
- package/dist/schema-coerce.cjs.map +1 -0
- package/dist/schema-coerce.d.cts +5 -0
- package/dist/schema-coerce.d.ts +5 -0
- package/dist/schema-coerce.js +11 -0
- package/dist/schema-coerce.js.map +1 -0
- package/package.json +46 -14
- package/dist/chunk-PIUBQRFC.js.map +0 -1
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
parse as parse2,
|
|
3
|
+
stringify
|
|
4
|
+
} from "./chunk-TR2ARLIF.js";
|
|
5
|
+
import {
|
|
6
|
+
parse
|
|
7
|
+
} from "./chunk-IX4FJELL.js";
|
|
8
|
+
import {
|
|
9
|
+
coerceBySchema
|
|
10
|
+
} from "./chunk-DZB6Y354.js";
|
|
6
11
|
|
|
7
12
|
// src/core/utils/debug.ts
|
|
8
13
|
var LINE_SPLIT_REGEX = /\r?\n/;
|
|
@@ -191,7 +196,7 @@ function escapeRegExp(literal) {
|
|
|
191
196
|
function processToolCallJson(toolCallJson, fullMatch, processedElements, options) {
|
|
192
197
|
var _a, _b;
|
|
193
198
|
try {
|
|
194
|
-
const parsedToolCall =
|
|
199
|
+
const parsedToolCall = parse(toolCallJson);
|
|
195
200
|
processedElements.push({
|
|
196
201
|
type: "tool-call",
|
|
197
202
|
toolCallId: generateId(),
|
|
@@ -319,7 +324,7 @@ function emitToolCall(context) {
|
|
|
319
324
|
var _a, _b;
|
|
320
325
|
const { state, controller, toolCallStart, toolCallEnd, options } = context;
|
|
321
326
|
try {
|
|
322
|
-
const parsedToolCall =
|
|
327
|
+
const parsedToolCall = parse(state.currentToolCallJson);
|
|
323
328
|
closeTextBlock(state, controller);
|
|
324
329
|
controller.enqueue({
|
|
325
330
|
type: "tool-call",
|
|
@@ -512,13 +517,8 @@ function isTCMProtocolFactory(protocol) {
|
|
|
512
517
|
}
|
|
513
518
|
|
|
514
519
|
// src/core/protocols/xml-protocol.ts
|
|
515
|
-
import { parse, stringify } from "@ai-sdk-tool/rxml";
|
|
516
520
|
var NAME_CHAR_RE = /[A-Za-z0-9_:-]/;
|
|
517
521
|
var WHITESPACE_REGEX = /\s/;
|
|
518
|
-
var REGEX_ESCAPE_RE = /[.*+?^${}()|[\]\\]/g;
|
|
519
|
-
function escapeRegExp2(value) {
|
|
520
|
-
return value.replace(REGEX_ESCAPE_RE, "\\$&");
|
|
521
|
-
}
|
|
522
522
|
function getToolSchema(tools, toolName) {
|
|
523
523
|
var _a;
|
|
524
524
|
return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
|
|
@@ -532,7 +532,7 @@ function processToolCall(params) {
|
|
|
532
532
|
onError: (_a = options == null ? void 0 : options.onError) != null ? _a : parseOptions == null ? void 0 : parseOptions.onError
|
|
533
533
|
};
|
|
534
534
|
try {
|
|
535
|
-
const parsed =
|
|
535
|
+
const parsed = parse2(toolCall.content, toolSchema, parseConfig);
|
|
536
536
|
processedElements.push({
|
|
537
537
|
type: "tool-call",
|
|
538
538
|
toolCallId: generateId(),
|
|
@@ -570,7 +570,7 @@ function handleStreamingToolCallEnd(params) {
|
|
|
570
570
|
};
|
|
571
571
|
flushText(ctrl);
|
|
572
572
|
try {
|
|
573
|
-
const parsedResult =
|
|
573
|
+
const parsedResult = parse2(toolContent, toolSchema, parseConfig);
|
|
574
574
|
ctrl.enqueue({
|
|
575
575
|
type: "tool-call",
|
|
576
576
|
toolCallId: generateId(),
|
|
@@ -683,21 +683,25 @@ function nextTagToken(text, fromPos) {
|
|
|
683
683
|
nextPos: open.nextPos
|
|
684
684
|
};
|
|
685
685
|
}
|
|
686
|
-
function findNextToolTag(text, searchIndex,
|
|
686
|
+
function findNextToolTag(text, searchIndex, toolName) {
|
|
687
|
+
var _a, _b;
|
|
688
|
+
const startTag = `<${toolName}>`;
|
|
687
689
|
const openIdx = text.indexOf(startTag, searchIndex);
|
|
688
|
-
const
|
|
690
|
+
const selfMatch = findSelfClosingTag(text, toolName, searchIndex);
|
|
691
|
+
const selfIdx = (_a = selfMatch == null ? void 0 : selfMatch.index) != null ? _a : -1;
|
|
689
692
|
if (openIdx === -1 && selfIdx === -1) {
|
|
690
693
|
return null;
|
|
691
694
|
}
|
|
692
695
|
const isSelfClosing = selfIdx !== -1 && (openIdx === -1 || selfIdx < openIdx);
|
|
693
696
|
return {
|
|
694
697
|
tagStart: isSelfClosing ? selfIdx : openIdx,
|
|
695
|
-
isSelfClosing
|
|
698
|
+
isSelfClosing,
|
|
699
|
+
tagLength: isSelfClosing ? (_b = selfMatch == null ? void 0 : selfMatch.length) != null ? _b : 0 : startTag.length
|
|
696
700
|
};
|
|
697
701
|
}
|
|
698
702
|
function findLastCloseTagStart(segment, toolName) {
|
|
699
703
|
const closeTagPattern = new RegExp(
|
|
700
|
-
`</\\s*${
|
|
704
|
+
`</\\s*${escapeRegExp(toolName)}\\s*>`,
|
|
701
705
|
"g"
|
|
702
706
|
);
|
|
703
707
|
let closeTagStart = -1;
|
|
@@ -711,8 +715,8 @@ function findLastCloseTagStart(segment, toolName) {
|
|
|
711
715
|
}
|
|
712
716
|
return closeTagStart;
|
|
713
717
|
}
|
|
714
|
-
function pushSelfClosingToolCall(toolCalls, toolName, text, tagStart,
|
|
715
|
-
const endIndex = tagStart +
|
|
718
|
+
function pushSelfClosingToolCall(toolCalls, toolName, text, tagStart, tagLength) {
|
|
719
|
+
const endIndex = tagStart + tagLength;
|
|
716
720
|
toolCalls.push({
|
|
717
721
|
toolName,
|
|
718
722
|
startIndex: tagStart,
|
|
@@ -722,6 +726,24 @@ function pushSelfClosingToolCall(toolCalls, toolName, text, tagStart, selfTag) {
|
|
|
722
726
|
});
|
|
723
727
|
return endIndex;
|
|
724
728
|
}
|
|
729
|
+
var selfClosingTagCache = /* @__PURE__ */ new Map();
|
|
730
|
+
function getSelfClosingTagPattern(toolName) {
|
|
731
|
+
let pattern = selfClosingTagCache.get(toolName);
|
|
732
|
+
if (!pattern) {
|
|
733
|
+
pattern = new RegExp(`<\\s*${escapeRegExp(toolName)}\\s*/>`, "g");
|
|
734
|
+
selfClosingTagCache.set(toolName, pattern);
|
|
735
|
+
}
|
|
736
|
+
return pattern;
|
|
737
|
+
}
|
|
738
|
+
function findSelfClosingTag(text, toolName, fromIndex) {
|
|
739
|
+
const pattern = getSelfClosingTagPattern(toolName);
|
|
740
|
+
pattern.lastIndex = fromIndex;
|
|
741
|
+
const match = pattern.exec(text);
|
|
742
|
+
if (!match || match.index === void 0) {
|
|
743
|
+
return null;
|
|
744
|
+
}
|
|
745
|
+
return { index: match.index, length: match[0].length };
|
|
746
|
+
}
|
|
725
747
|
function appendOpenToolCallIfComplete(toolCalls, text, toolName, tagStart, startTag) {
|
|
726
748
|
const contentStart = tagStart + startTag.length;
|
|
727
749
|
const fullTagEnd = findClosingTagEndFlexible(text, contentStart, toolName);
|
|
@@ -743,10 +765,9 @@ function appendOpenToolCallIfComplete(toolCalls, text, toolName, tagStart, start
|
|
|
743
765
|
function findToolCallsForName(text, toolName) {
|
|
744
766
|
const toolCalls = [];
|
|
745
767
|
const startTag = `<${toolName}>`;
|
|
746
|
-
const selfTag = `<${toolName}/>`;
|
|
747
768
|
let searchIndex = 0;
|
|
748
769
|
while (searchIndex < text.length) {
|
|
749
|
-
const match = findNextToolTag(text, searchIndex,
|
|
770
|
+
const match = findNextToolTag(text, searchIndex, toolName);
|
|
750
771
|
if (match === null) {
|
|
751
772
|
break;
|
|
752
773
|
}
|
|
@@ -756,7 +777,7 @@ function findToolCallsForName(text, toolName) {
|
|
|
756
777
|
toolName,
|
|
757
778
|
text,
|
|
758
779
|
match.tagStart,
|
|
759
|
-
|
|
780
|
+
match.tagLength
|
|
760
781
|
);
|
|
761
782
|
continue;
|
|
762
783
|
}
|
|
@@ -779,28 +800,114 @@ function findToolCalls(text, toolNames) {
|
|
|
779
800
|
return toolCalls.sort((a, b) => a.startIndex - b.startIndex);
|
|
780
801
|
}
|
|
781
802
|
function findEarliestToolTag(buffer, toolNames) {
|
|
803
|
+
var _a, _b;
|
|
782
804
|
let bestIndex = -1;
|
|
783
805
|
let bestName = "";
|
|
784
806
|
let bestSelfClosing = false;
|
|
807
|
+
let bestTagLength = 0;
|
|
785
808
|
if (toolNames.length > 0) {
|
|
786
809
|
for (const name of toolNames) {
|
|
787
810
|
const openTag = `<${name}>`;
|
|
788
|
-
const selfTag = `<${name}/>`;
|
|
789
811
|
const idxOpen = buffer.indexOf(openTag);
|
|
790
|
-
const
|
|
812
|
+
const selfMatch = findSelfClosingTag(buffer, name, 0);
|
|
813
|
+
const idxSelf = (_a = selfMatch == null ? void 0 : selfMatch.index) != null ? _a : -1;
|
|
791
814
|
if (idxOpen !== -1 && (bestIndex === -1 || idxOpen < bestIndex)) {
|
|
792
815
|
bestIndex = idxOpen;
|
|
793
816
|
bestName = name;
|
|
794
817
|
bestSelfClosing = false;
|
|
818
|
+
bestTagLength = openTag.length;
|
|
795
819
|
}
|
|
796
820
|
if (idxSelf !== -1 && (bestIndex === -1 || idxSelf < bestIndex)) {
|
|
797
821
|
bestIndex = idxSelf;
|
|
798
822
|
bestName = name;
|
|
799
823
|
bestSelfClosing = true;
|
|
824
|
+
bestTagLength = (_b = selfMatch == null ? void 0 : selfMatch.length) != null ? _b : 0;
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return {
|
|
829
|
+
index: bestIndex,
|
|
830
|
+
name: bestName,
|
|
831
|
+
selfClosing: bestSelfClosing,
|
|
832
|
+
tagLength: bestTagLength
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
function isOpenTagPrefix(suffix, toolName) {
|
|
836
|
+
return `${toolName}>`.startsWith(suffix);
|
|
837
|
+
}
|
|
838
|
+
function consumeWhitespace(text, index) {
|
|
839
|
+
let i = index;
|
|
840
|
+
while (i < text.length && WHITESPACE_REGEX.test(text.charAt(i))) {
|
|
841
|
+
i += 1;
|
|
842
|
+
}
|
|
843
|
+
return i;
|
|
844
|
+
}
|
|
845
|
+
function consumeToolNamePrefix(text, index, toolName) {
|
|
846
|
+
let i = index;
|
|
847
|
+
let nameIndex = 0;
|
|
848
|
+
while (i < text.length && nameIndex < toolName.length) {
|
|
849
|
+
if (text.charAt(i) !== toolName.charAt(nameIndex)) {
|
|
850
|
+
return { index: i, done: false, valid: false };
|
|
851
|
+
}
|
|
852
|
+
i += 1;
|
|
853
|
+
nameIndex += 1;
|
|
854
|
+
}
|
|
855
|
+
return { index: i, done: nameIndex === toolName.length, valid: true };
|
|
856
|
+
}
|
|
857
|
+
function isSelfClosingSuffixRemainder(text, index) {
|
|
858
|
+
if (text.charAt(index) !== "/") {
|
|
859
|
+
return false;
|
|
860
|
+
}
|
|
861
|
+
if (index + 1 >= text.length) {
|
|
862
|
+
return true;
|
|
863
|
+
}
|
|
864
|
+
return index + 1 === text.length - 1 && text.charAt(index + 1) === ">";
|
|
865
|
+
}
|
|
866
|
+
function isSelfClosingTagPrefix(suffix, toolName) {
|
|
867
|
+
let i = consumeWhitespace(suffix, 0);
|
|
868
|
+
if (i >= suffix.length) {
|
|
869
|
+
return true;
|
|
870
|
+
}
|
|
871
|
+
const nameRemainder = suffix.slice(i);
|
|
872
|
+
if (toolName.startsWith(nameRemainder)) {
|
|
873
|
+
return true;
|
|
874
|
+
}
|
|
875
|
+
const nameResult = consumeToolNamePrefix(suffix, i, toolName);
|
|
876
|
+
if (!nameResult.valid) {
|
|
877
|
+
return false;
|
|
878
|
+
}
|
|
879
|
+
i = nameResult.index;
|
|
880
|
+
if (i >= suffix.length) {
|
|
881
|
+
return true;
|
|
882
|
+
}
|
|
883
|
+
if (!nameResult.done) {
|
|
884
|
+
return false;
|
|
885
|
+
}
|
|
886
|
+
i = consumeWhitespace(suffix, i);
|
|
887
|
+
if (i >= suffix.length) {
|
|
888
|
+
return true;
|
|
889
|
+
}
|
|
890
|
+
return isSelfClosingSuffixRemainder(suffix, i);
|
|
891
|
+
}
|
|
892
|
+
function findPotentialToolTagStart(buffer, toolNames) {
|
|
893
|
+
if (toolNames.length === 0 || buffer.length === 0) {
|
|
894
|
+
return -1;
|
|
895
|
+
}
|
|
896
|
+
const lastGt = buffer.lastIndexOf(">");
|
|
897
|
+
const offset = lastGt === -1 ? 0 : lastGt + 1;
|
|
898
|
+
const trailing = buffer.slice(offset);
|
|
899
|
+
for (let i = trailing.length - 1; i >= 0; i -= 1) {
|
|
900
|
+
if (trailing.charAt(i) !== "<") {
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
const suffix = trailing.slice(i + 1);
|
|
904
|
+
for (const name of toolNames) {
|
|
905
|
+
if (isOpenTagPrefix(suffix, name) || isSelfClosingTagPrefix(suffix, name)) {
|
|
906
|
+
return offset + i;
|
|
800
907
|
}
|
|
801
908
|
}
|
|
802
909
|
}
|
|
803
|
-
return
|
|
910
|
+
return -1;
|
|
804
911
|
}
|
|
805
912
|
function createFlushTextHandler(getCurrentTextId, setCurrentTextId, getHasEmittedTextStart, setHasEmittedTextStart) {
|
|
806
913
|
return (controller, text) => {
|
|
@@ -846,7 +953,7 @@ function processToolCallInBuffer(params) {
|
|
|
846
953
|
parseOptions
|
|
847
954
|
} = params;
|
|
848
955
|
const endTagPattern = new RegExp(
|
|
849
|
-
`</\\s*${
|
|
956
|
+
`</\\s*${escapeRegExp(currentToolCall.name)}\\s*>`
|
|
850
957
|
);
|
|
851
958
|
const endMatch = endTagPattern.exec(buffer);
|
|
852
959
|
if (!endMatch || endMatch.index === void 0) {
|
|
@@ -886,18 +993,22 @@ function processNoToolCallInBuffer(params) {
|
|
|
886
993
|
const {
|
|
887
994
|
index: earliestStartTagIndex,
|
|
888
995
|
name: earliestToolName,
|
|
889
|
-
selfClosing
|
|
996
|
+
selfClosing,
|
|
997
|
+
tagLength
|
|
890
998
|
} = findEarliestToolTag(buffer, toolNames);
|
|
891
999
|
if (earliestStartTagIndex === -1) {
|
|
892
|
-
const
|
|
893
|
-
const
|
|
894
|
-
|
|
1000
|
+
const potentialStart = findPotentialToolTagStart(buffer, toolNames);
|
|
1001
|
+
const safeLen = Math.max(
|
|
1002
|
+
0,
|
|
1003
|
+
potentialStart === -1 ? buffer.length : potentialStart
|
|
1004
|
+
);
|
|
1005
|
+
const remaining = buffer.slice(safeLen);
|
|
895
1006
|
if (safeLen > 0) {
|
|
896
1007
|
flushText(controller, buffer.slice(0, safeLen));
|
|
897
|
-
setBuffer(
|
|
1008
|
+
setBuffer(remaining);
|
|
898
1009
|
}
|
|
899
1010
|
return {
|
|
900
|
-
buffer:
|
|
1011
|
+
buffer: remaining,
|
|
901
1012
|
currentToolCall: null,
|
|
902
1013
|
shouldBreak: true,
|
|
903
1014
|
shouldContinue: false
|
|
@@ -905,8 +1016,7 @@ function processNoToolCallInBuffer(params) {
|
|
|
905
1016
|
}
|
|
906
1017
|
flushText(controller, buffer.substring(0, earliestStartTagIndex));
|
|
907
1018
|
if (selfClosing) {
|
|
908
|
-
const
|
|
909
|
-
const newBuffer2 = buffer.substring(earliestStartTagIndex + selfTag.length);
|
|
1019
|
+
const newBuffer2 = buffer.substring(earliestStartTagIndex + tagLength);
|
|
910
1020
|
setBuffer(newBuffer2);
|
|
911
1021
|
handleStreamingToolCallEnd({
|
|
912
1022
|
toolContent: "",
|
|
@@ -1672,7 +1782,6 @@ function hasInputProperty(obj) {
|
|
|
1672
1782
|
|
|
1673
1783
|
// src/generate-handler.ts
|
|
1674
1784
|
import { generateId as generateId2 } from "@ai-sdk/provider-utils";
|
|
1675
|
-
import { coerceBySchema } from "@ai-sdk-tool/schema-coerce";
|
|
1676
1785
|
function parseToolChoiceJson(text, providerOptions) {
|
|
1677
1786
|
var _a;
|
|
1678
1787
|
try {
|
|
@@ -1967,29 +2076,230 @@ function formatXmlNode(tagName, value, depth) {
|
|
|
1967
2076
|
}
|
|
1968
2077
|
|
|
1969
2078
|
// src/core/prompts/xml-system-prompt.ts
|
|
2079
|
+
import dedent from "dedent";
|
|
1970
2080
|
function xmlSystemPromptTemplate(tools) {
|
|
1971
|
-
const
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
You may call one or more functions to assist with the user query.
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
-
|
|
1985
|
-
-
|
|
1986
|
-
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
2081
|
+
const toolsText = renderToolsForXmlPrompt(tools);
|
|
2082
|
+
const header = dedent`
|
|
2083
|
+
# Tools
|
|
2084
|
+
You may call one or more functions to assist with the user query.
|
|
2085
|
+
`;
|
|
2086
|
+
const definitions = [
|
|
2087
|
+
"You have access to the following functions:",
|
|
2088
|
+
"<tools>",
|
|
2089
|
+
toolsText,
|
|
2090
|
+
"</tools>"
|
|
2091
|
+
].join("\n");
|
|
2092
|
+
const rules = dedent`
|
|
2093
|
+
<rules>
|
|
2094
|
+
- Use exactly one XML element whose tag name is the function name.
|
|
2095
|
+
- Put each parameter as a child element.
|
|
2096
|
+
- Values must follow the schema exactly (numbers, arrays, objects, enums → copy as-is).
|
|
2097
|
+
- Do not add or remove functions or parameters.
|
|
2098
|
+
- Each required parameter must appear once.
|
|
2099
|
+
- Output nothing before or after the function call.
|
|
2100
|
+
- It is also possible to call multiple types of functions in one turn or to call a single function multiple times.
|
|
2101
|
+
</rules>
|
|
2102
|
+
`;
|
|
2103
|
+
const examples = dedent`
|
|
2104
|
+
For each function call, output the function name and parameter in the following format:
|
|
2105
|
+
<example_function_name>
|
|
2106
|
+
<example_parameter_1>value_1</example_parameter_1>
|
|
2107
|
+
<example_parameter_2>This is the value for the second parameter
|
|
2108
|
+
that can span
|
|
2109
|
+
multiple lines</example_parameter_2>
|
|
2110
|
+
</example_function_name>
|
|
2111
|
+
`;
|
|
2112
|
+
return [header, definitions, rules, examples].join("\n\n");
|
|
2113
|
+
}
|
|
2114
|
+
var INDENT = " ";
|
|
2115
|
+
function renderToolsForXmlPrompt(tools) {
|
|
2116
|
+
if (!tools.length) {
|
|
2117
|
+
return "none";
|
|
2118
|
+
}
|
|
2119
|
+
return tools.map(renderToolForXmlPrompt).join("\n\n");
|
|
2120
|
+
}
|
|
2121
|
+
function renderToolForXmlPrompt(tool) {
|
|
2122
|
+
const lines = [`name: ${tool.name}`];
|
|
2123
|
+
if (tool.description) {
|
|
2124
|
+
lines.push(`description: ${tool.description}`);
|
|
2125
|
+
}
|
|
2126
|
+
lines.push("parameters:");
|
|
2127
|
+
const normalizedSchema = normalizeSchema(tool.inputSchema);
|
|
2128
|
+
lines.push(...renderParametersSummary(normalizedSchema, 1));
|
|
2129
|
+
lines.push(`schema: ${stringifySchema(normalizedSchema)}`);
|
|
2130
|
+
return lines.join("\n");
|
|
2131
|
+
}
|
|
2132
|
+
function normalizeSchema(schema) {
|
|
2133
|
+
if (typeof schema === "string") {
|
|
2134
|
+
try {
|
|
2135
|
+
return JSON.parse(schema);
|
|
2136
|
+
} catch (e) {
|
|
2137
|
+
return { type: "string", const: schema };
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
return schema;
|
|
2141
|
+
}
|
|
2142
|
+
function renderParametersSummary(schema, indentLevel) {
|
|
2143
|
+
var _a, _b;
|
|
2144
|
+
const indent = INDENT.repeat(indentLevel);
|
|
2145
|
+
if (schema === void 0 || schema === null) {
|
|
2146
|
+
return [`${indent}(none)`];
|
|
2147
|
+
}
|
|
2148
|
+
if (schema === true) {
|
|
2149
|
+
return [`${indent}(any)`];
|
|
2150
|
+
}
|
|
2151
|
+
if (schema === false) {
|
|
2152
|
+
return [`${indent}(no valid parameters)`];
|
|
2153
|
+
}
|
|
2154
|
+
if (typeof schema !== "object") {
|
|
2155
|
+
return [`${indent}- value (${String(schema)})`];
|
|
2156
|
+
}
|
|
2157
|
+
const schemaType = [];
|
|
2158
|
+
if (Array.isArray(schema.type)) {
|
|
2159
|
+
schemaType.push(...schema.type);
|
|
2160
|
+
} else if (schema.type) {
|
|
2161
|
+
schemaType.push(schema.type);
|
|
2162
|
+
}
|
|
2163
|
+
const isObjectLike = schemaType.includes("object") || !!schema.properties;
|
|
2164
|
+
if (isObjectLike) {
|
|
2165
|
+
const properties = (_a = schema.properties) != null ? _a : {};
|
|
2166
|
+
const requiredSet = new Set((_b = schema.required) != null ? _b : []);
|
|
2167
|
+
const propertyNames = Object.keys(properties).sort();
|
|
2168
|
+
if (propertyNames.length === 0) {
|
|
2169
|
+
return [`${indent}(no named parameters)`];
|
|
2170
|
+
}
|
|
2171
|
+
const lines = [];
|
|
2172
|
+
for (const propName of propertyNames) {
|
|
2173
|
+
const propSchema = properties[propName];
|
|
2174
|
+
lines.push(
|
|
2175
|
+
renderPropertySummaryLine({
|
|
2176
|
+
indent,
|
|
2177
|
+
propName,
|
|
2178
|
+
propSchema,
|
|
2179
|
+
required: requiredSet.has(propName)
|
|
2180
|
+
})
|
|
2181
|
+
);
|
|
2182
|
+
}
|
|
2183
|
+
return lines.length ? lines : [`${indent}(no parameters)`];
|
|
2184
|
+
}
|
|
2185
|
+
return [`${indent}- value (${summarizeType(schema)})`];
|
|
2186
|
+
}
|
|
2187
|
+
function renderPropertySummaryLine({
|
|
2188
|
+
indent,
|
|
2189
|
+
propName,
|
|
2190
|
+
propSchema,
|
|
2191
|
+
required
|
|
2192
|
+
}) {
|
|
2193
|
+
const typeLabel = summarizeType(propSchema);
|
|
2194
|
+
const requiredLabel = required ? "required" : "optional";
|
|
2195
|
+
const extras = collectPropertyExtras(propSchema);
|
|
2196
|
+
const extraText = extras.length ? ` - ${extras.join("; ")}` : "";
|
|
2197
|
+
return `${indent}- ${propName} (${typeLabel}, ${requiredLabel})${extraText}`;
|
|
2198
|
+
}
|
|
2199
|
+
function collectPropertyExtras(propSchema) {
|
|
2200
|
+
if (!propSchema || typeof propSchema !== "object") {
|
|
2201
|
+
return [];
|
|
2202
|
+
}
|
|
2203
|
+
const extras = [];
|
|
2204
|
+
if (propSchema.enum) {
|
|
2205
|
+
extras.push(`enum: ${formatEnumForSummary(propSchema.enum)}`);
|
|
2206
|
+
}
|
|
2207
|
+
if (propSchema.default !== void 0) {
|
|
2208
|
+
extras.push(`default: ${formatValue(propSchema.default)}`);
|
|
2209
|
+
}
|
|
2210
|
+
if (propSchema.description) {
|
|
2211
|
+
extras.push(propSchema.description);
|
|
2212
|
+
}
|
|
2213
|
+
return extras;
|
|
2214
|
+
}
|
|
2215
|
+
function summarizeType(schema) {
|
|
2216
|
+
var _a;
|
|
2217
|
+
if (schema === void 0 || schema === null) {
|
|
2218
|
+
return "unknown";
|
|
2219
|
+
}
|
|
2220
|
+
if (schema === true) {
|
|
2221
|
+
return "any";
|
|
2222
|
+
}
|
|
2223
|
+
if (schema === false) {
|
|
2224
|
+
return "never";
|
|
2225
|
+
}
|
|
2226
|
+
if (typeof schema !== "object") {
|
|
2227
|
+
return String(schema);
|
|
2228
|
+
}
|
|
2229
|
+
const schemaType = schema.type;
|
|
2230
|
+
let baseType = "";
|
|
2231
|
+
if (Array.isArray(schemaType) && schemaType.length) {
|
|
2232
|
+
baseType = schemaType.join(" | ");
|
|
2233
|
+
} else if (typeof schemaType === "string") {
|
|
2234
|
+
baseType = schemaType;
|
|
2235
|
+
} else if (schema.enum) {
|
|
2236
|
+
const inferred = Array.from(
|
|
2237
|
+
new Set(schema.enum.map((value) => typeof value))
|
|
2238
|
+
);
|
|
2239
|
+
if (inferred.length === 1) {
|
|
2240
|
+
baseType = (_a = inferred[0]) != null ? _a : "";
|
|
2241
|
+
}
|
|
2242
|
+
} else if (schema.const !== void 0) {
|
|
2243
|
+
baseType = typeof schema.const;
|
|
2244
|
+
}
|
|
2245
|
+
if (!baseType) {
|
|
2246
|
+
baseType = "any";
|
|
2247
|
+
}
|
|
2248
|
+
if (baseType === "array" && schema.items) {
|
|
2249
|
+
const itemType = Array.isArray(schema.items) ? schema.items.map((item) => summarizeType(item)).join(" | ") : summarizeType(schema.items);
|
|
2250
|
+
return `array<${itemType}>`;
|
|
2251
|
+
}
|
|
2252
|
+
if (baseType === "string" && schema.format) {
|
|
2253
|
+
return `string (${schema.format})`;
|
|
2254
|
+
}
|
|
2255
|
+
return baseType;
|
|
2256
|
+
}
|
|
2257
|
+
var ENUM_MAX_INLINE = 6;
|
|
2258
|
+
var ENUM_PREVIEW_LIMIT = 5;
|
|
2259
|
+
function formatEnumForSummary(values) {
|
|
2260
|
+
if (values.length <= ENUM_MAX_INLINE) {
|
|
2261
|
+
return formatValue(values);
|
|
2262
|
+
}
|
|
2263
|
+
const preview = values.slice(0, ENUM_PREVIEW_LIMIT).map((value) => formatValue(value));
|
|
2264
|
+
return `[${preview.join(", ")}, ... (${values.length} total)]`;
|
|
2265
|
+
}
|
|
2266
|
+
function formatValue(value) {
|
|
2267
|
+
if (typeof value === "string") {
|
|
2268
|
+
return JSON.stringify(value);
|
|
2269
|
+
}
|
|
2270
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
2271
|
+
return String(value);
|
|
2272
|
+
}
|
|
2273
|
+
if (value === null) {
|
|
2274
|
+
return "null";
|
|
2275
|
+
}
|
|
2276
|
+
if (Array.isArray(value)) {
|
|
2277
|
+
return `[${value.map(formatValue).join(", ")}]`;
|
|
2278
|
+
}
|
|
2279
|
+
return JSON.stringify(value);
|
|
2280
|
+
}
|
|
2281
|
+
function stringifySchema(schema) {
|
|
2282
|
+
if (schema === void 0) {
|
|
2283
|
+
return "null";
|
|
2284
|
+
}
|
|
2285
|
+
return JSON.stringify(stripSchemaKeys(schema));
|
|
2286
|
+
}
|
|
2287
|
+
function stripSchemaKeys(value) {
|
|
2288
|
+
if (Array.isArray(value)) {
|
|
2289
|
+
return value.map((entry) => stripSchemaKeys(entry));
|
|
2290
|
+
}
|
|
2291
|
+
if (value && typeof value === "object") {
|
|
2292
|
+
const record = value;
|
|
2293
|
+
const cleaned = {};
|
|
2294
|
+
for (const [key, entry] of Object.entries(record)) {
|
|
2295
|
+
if (key === "$schema") {
|
|
2296
|
+
continue;
|
|
2297
|
+
}
|
|
2298
|
+
cleaned[key] = stripSchemaKeys(entry);
|
|
2299
|
+
}
|
|
2300
|
+
return cleaned;
|
|
2301
|
+
}
|
|
2302
|
+
return value;
|
|
1993
2303
|
}
|
|
1994
2304
|
|
|
1995
2305
|
// src/core/prompts/yaml-system-prompt.ts
|
|
@@ -2567,4 +2877,4 @@ export {
|
|
|
2567
2877
|
xmlToolMiddleware,
|
|
2568
2878
|
yamlToolMiddleware
|
|
2569
2879
|
};
|
|
2570
|
-
//# sourceMappingURL=chunk-
|
|
2880
|
+
//# sourceMappingURL=chunk-3QJVNEHE.js.map
|