@agentforge/core 0.16.12 → 0.16.13
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 +308 -301
- package/dist/index.d.cts +15 -20
- package/dist/index.d.ts +15 -20
- package/dist/index.js +308 -301
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -841,96 +841,29 @@ function toolBuilder() {
|
|
|
841
841
|
return new ToolBuilder();
|
|
842
842
|
}
|
|
843
843
|
|
|
844
|
-
// src/
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
function serializeToolResult(result) {
|
|
848
|
-
if (typeof result === "string") {
|
|
849
|
-
return result;
|
|
850
|
-
}
|
|
851
|
-
if (typeof result === "object" && result !== null) {
|
|
852
|
-
return JSON.stringify(result, null, 2);
|
|
853
|
-
}
|
|
854
|
-
return String(result);
|
|
855
|
-
}
|
|
856
|
-
function isJsonSchemaObject(value) {
|
|
857
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
858
|
-
}
|
|
859
|
-
function isJsonSchemaDefinitionMap(value) {
|
|
860
|
-
if (!isJsonSchemaObject(value)) {
|
|
861
|
-
return false;
|
|
862
|
-
}
|
|
863
|
-
return Object.values(value).every(isJsonSchemaObject);
|
|
844
|
+
// src/tools/registry-collection.ts
|
|
845
|
+
function getAllRegistryTools(tools) {
|
|
846
|
+
return Array.from(tools.values());
|
|
864
847
|
}
|
|
865
|
-
function
|
|
866
|
-
|
|
867
|
-
return {};
|
|
868
|
-
}
|
|
869
|
-
const ref = jsonSchema.$ref;
|
|
870
|
-
const definitions = jsonSchema.definitions;
|
|
871
|
-
if (typeof ref !== "string" || !isJsonSchemaDefinitionMap(definitions)) {
|
|
872
|
-
return jsonSchema;
|
|
873
|
-
}
|
|
874
|
-
const refName = ref.replace("#/definitions/", "");
|
|
875
|
-
return definitions[refName] ?? jsonSchema;
|
|
848
|
+
function getRegistryToolNames(tools) {
|
|
849
|
+
return Array.from(tools.keys());
|
|
876
850
|
}
|
|
877
|
-
function
|
|
878
|
-
return
|
|
879
|
-
name: tool.metadata.name,
|
|
880
|
-
description: tool.metadata.description,
|
|
881
|
-
schema: tool.schema,
|
|
882
|
-
func: async (input) => serializeToolResult(await tool.invoke(input))
|
|
883
|
-
});
|
|
851
|
+
function getRegistryToolsByCategory(tools, category) {
|
|
852
|
+
return getAllRegistryTools(tools).filter((tool) => tool.metadata.category === category);
|
|
884
853
|
}
|
|
885
|
-
function
|
|
886
|
-
return tools.
|
|
854
|
+
function getRegistryToolsByTag(tools, tag) {
|
|
855
|
+
return getAllRegistryTools(tools).filter((tool) => tool.metadata.tags?.includes(tag));
|
|
887
856
|
}
|
|
888
|
-
function
|
|
889
|
-
const
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
857
|
+
function searchRegistryTools(tools, query) {
|
|
858
|
+
const lowerQuery = query.toLowerCase();
|
|
859
|
+
return getAllRegistryTools(tools).filter((tool) => {
|
|
860
|
+
const name = tool.metadata.name.toLowerCase();
|
|
861
|
+
const displayName = tool.metadata.displayName?.toLowerCase() ?? "";
|
|
862
|
+
const description = tool.metadata.description.toLowerCase();
|
|
863
|
+
return name.includes(lowerQuery) || displayName.includes(lowerQuery) || description.includes(lowerQuery);
|
|
893
864
|
});
|
|
894
|
-
return extractToolSchema(jsonSchema);
|
|
895
|
-
}
|
|
896
|
-
function getToolDescription(tool) {
|
|
897
|
-
const { metadata } = tool;
|
|
898
|
-
const parts = [];
|
|
899
|
-
parts.push(`${metadata.name}: ${metadata.description}`);
|
|
900
|
-
if (metadata.displayName) {
|
|
901
|
-
parts.push(`Display Name: ${metadata.displayName}`);
|
|
902
|
-
}
|
|
903
|
-
parts.push(`Category: ${metadata.category}`);
|
|
904
|
-
if (metadata.tags && metadata.tags.length > 0) {
|
|
905
|
-
parts.push(`Tags: ${metadata.tags.join(", ")}`);
|
|
906
|
-
}
|
|
907
|
-
if (metadata.usageNotes) {
|
|
908
|
-
parts.push(`
|
|
909
|
-
Usage Notes: ${metadata.usageNotes}`);
|
|
910
|
-
}
|
|
911
|
-
if (metadata.limitations && metadata.limitations.length > 0) {
|
|
912
|
-
parts.push(`
|
|
913
|
-
Limitations:`);
|
|
914
|
-
metadata.limitations.forEach((limit) => {
|
|
915
|
-
parts.push(` - ${limit}`);
|
|
916
|
-
});
|
|
917
|
-
}
|
|
918
|
-
if (metadata.examples && metadata.examples.length > 0) {
|
|
919
|
-
parts.push(`
|
|
920
|
-
Examples:`);
|
|
921
|
-
metadata.examples.forEach((example, i) => {
|
|
922
|
-
parts.push(` ${i + 1}. ${example.description}`);
|
|
923
|
-
if (example.explanation) {
|
|
924
|
-
parts.push(` ${example.explanation}`);
|
|
925
|
-
}
|
|
926
|
-
});
|
|
927
|
-
}
|
|
928
|
-
return parts.join("\n");
|
|
929
865
|
}
|
|
930
866
|
|
|
931
|
-
// src/tools/registry.ts
|
|
932
|
-
var import_zod3 = require("zod");
|
|
933
|
-
|
|
934
867
|
// src/langgraph/observability/logger.ts
|
|
935
868
|
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
936
869
|
LogLevel2["DEBUG"] = "debug";
|
|
@@ -1026,31 +959,301 @@ function createLogger(name, options) {
|
|
|
1026
959
|
return new LoggerImpl(name, options);
|
|
1027
960
|
}
|
|
1028
961
|
|
|
1029
|
-
// src/tools/registry-
|
|
1030
|
-
|
|
1031
|
-
|
|
962
|
+
// src/tools/registry-events.ts
|
|
963
|
+
var logger = createLogger("agentforge:core:tools:registry", { level: "info" /* INFO */ });
|
|
964
|
+
function addRegistryEventHandler(eventHandlers, event, handler) {
|
|
965
|
+
if (!eventHandlers.has(event)) {
|
|
966
|
+
eventHandlers.set(event, /* @__PURE__ */ new Set());
|
|
967
|
+
}
|
|
968
|
+
eventHandlers.get(event).add(handler);
|
|
1032
969
|
}
|
|
1033
|
-
function
|
|
1034
|
-
|
|
970
|
+
function removeRegistryEventHandler(eventHandlers, event, handler) {
|
|
971
|
+
const handlers = eventHandlers.get(event);
|
|
972
|
+
if (handlers) {
|
|
973
|
+
handlers.delete(handler);
|
|
974
|
+
}
|
|
1035
975
|
}
|
|
1036
|
-
function
|
|
1037
|
-
|
|
976
|
+
function emitRegistryEvent(eventHandlers, event, data) {
|
|
977
|
+
const handlers = eventHandlers.get(event);
|
|
978
|
+
if (!handlers) {
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
981
|
+
handlers.forEach((handler) => {
|
|
982
|
+
try {
|
|
983
|
+
handler(data);
|
|
984
|
+
} catch (error) {
|
|
985
|
+
logger.error("Event handler error", {
|
|
986
|
+
event: String(event),
|
|
987
|
+
error: error instanceof Error ? error.message : String(error),
|
|
988
|
+
...error instanceof Error && error.stack ? { stack: error.stack } : {}
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
});
|
|
1038
992
|
}
|
|
1039
|
-
|
|
1040
|
-
|
|
993
|
+
|
|
994
|
+
// src/tools/registry-prompt.ts
|
|
995
|
+
var import_zod3 = require("zod");
|
|
996
|
+
|
|
997
|
+
// src/langchain/converter.ts
|
|
998
|
+
var import_tools = require("@langchain/core/tools");
|
|
999
|
+
var import_zod_to_json_schema = require("zod-to-json-schema");
|
|
1000
|
+
function serializeToolResult(result) {
|
|
1001
|
+
if (typeof result === "string") {
|
|
1002
|
+
return result;
|
|
1003
|
+
}
|
|
1004
|
+
if (typeof result === "object" && result !== null) {
|
|
1005
|
+
return JSON.stringify(result, null, 2);
|
|
1006
|
+
}
|
|
1007
|
+
return String(result);
|
|
1041
1008
|
}
|
|
1042
|
-
function
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1009
|
+
function isJsonSchemaObject(value) {
|
|
1010
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1011
|
+
}
|
|
1012
|
+
function isJsonSchemaDefinitionMap(value) {
|
|
1013
|
+
if (!isJsonSchemaObject(value)) {
|
|
1014
|
+
return false;
|
|
1015
|
+
}
|
|
1016
|
+
return Object.values(value).every(isJsonSchemaObject);
|
|
1017
|
+
}
|
|
1018
|
+
function extractToolSchema(jsonSchema) {
|
|
1019
|
+
if (!isJsonSchemaObject(jsonSchema)) {
|
|
1020
|
+
return {};
|
|
1021
|
+
}
|
|
1022
|
+
const ref = jsonSchema.$ref;
|
|
1023
|
+
const definitions = jsonSchema.definitions;
|
|
1024
|
+
if (typeof ref !== "string" || !isJsonSchemaDefinitionMap(definitions)) {
|
|
1025
|
+
return jsonSchema;
|
|
1026
|
+
}
|
|
1027
|
+
const refName = ref.replace("#/definitions/", "");
|
|
1028
|
+
return definitions[refName] ?? jsonSchema;
|
|
1029
|
+
}
|
|
1030
|
+
function toLangChainTool(tool) {
|
|
1031
|
+
return new import_tools.DynamicStructuredTool({
|
|
1032
|
+
name: tool.metadata.name,
|
|
1033
|
+
description: tool.metadata.description,
|
|
1034
|
+
schema: tool.schema,
|
|
1035
|
+
func: async (input) => serializeToolResult(await tool.invoke(input))
|
|
1049
1036
|
});
|
|
1050
1037
|
}
|
|
1038
|
+
function toLangChainTools(tools) {
|
|
1039
|
+
return tools.map(toLangChainTool);
|
|
1040
|
+
}
|
|
1041
|
+
function getToolJsonSchema(tool) {
|
|
1042
|
+
const jsonSchema = (0, import_zod_to_json_schema.zodToJsonSchema)(tool.schema, {
|
|
1043
|
+
name: tool.metadata.name,
|
|
1044
|
+
$refStrategy: "none"
|
|
1045
|
+
// Don't use $ref for nested schemas
|
|
1046
|
+
});
|
|
1047
|
+
return extractToolSchema(jsonSchema);
|
|
1048
|
+
}
|
|
1049
|
+
function getToolDescription(tool) {
|
|
1050
|
+
const { metadata } = tool;
|
|
1051
|
+
const parts = [];
|
|
1052
|
+
parts.push(`${metadata.name}: ${metadata.description}`);
|
|
1053
|
+
if (metadata.displayName) {
|
|
1054
|
+
parts.push(`Display Name: ${metadata.displayName}`);
|
|
1055
|
+
}
|
|
1056
|
+
parts.push(`Category: ${metadata.category}`);
|
|
1057
|
+
if (metadata.tags && metadata.tags.length > 0) {
|
|
1058
|
+
parts.push(`Tags: ${metadata.tags.join(", ")}`);
|
|
1059
|
+
}
|
|
1060
|
+
if (metadata.usageNotes) {
|
|
1061
|
+
parts.push(`
|
|
1062
|
+
Usage Notes: ${metadata.usageNotes}`);
|
|
1063
|
+
}
|
|
1064
|
+
if (metadata.limitations && metadata.limitations.length > 0) {
|
|
1065
|
+
parts.push(`
|
|
1066
|
+
Limitations:`);
|
|
1067
|
+
metadata.limitations.forEach((limit) => {
|
|
1068
|
+
parts.push(` - ${limit}`);
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
if (metadata.examples && metadata.examples.length > 0) {
|
|
1072
|
+
parts.push(`
|
|
1073
|
+
Examples:`);
|
|
1074
|
+
metadata.examples.forEach((example, i) => {
|
|
1075
|
+
parts.push(` ${i + 1}. ${example.description}`);
|
|
1076
|
+
if (example.explanation) {
|
|
1077
|
+
parts.push(` ${example.explanation}`);
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
return parts.join("\n");
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
// src/tools/registry-prompt.ts
|
|
1085
|
+
function convertRegistryToolsToLangChain(tools) {
|
|
1086
|
+
return toLangChainTools(tools);
|
|
1087
|
+
}
|
|
1088
|
+
function generateRegistryPrompt(tools, options = {}) {
|
|
1089
|
+
const {
|
|
1090
|
+
includeExamples = false,
|
|
1091
|
+
includeNotes = false,
|
|
1092
|
+
includeLimitations = false,
|
|
1093
|
+
includeRelations = false,
|
|
1094
|
+
groupByCategory = false,
|
|
1095
|
+
categories,
|
|
1096
|
+
maxExamplesPerTool,
|
|
1097
|
+
minimal = false
|
|
1098
|
+
} = options;
|
|
1099
|
+
let toolsToInclude = tools;
|
|
1100
|
+
if (categories && categories.length > 0) {
|
|
1101
|
+
toolsToInclude = toolsToInclude.filter(
|
|
1102
|
+
(tool) => categories.includes(tool.metadata.category)
|
|
1103
|
+
);
|
|
1104
|
+
}
|
|
1105
|
+
if (toolsToInclude.length === 0) {
|
|
1106
|
+
return "No tools available.";
|
|
1107
|
+
}
|
|
1108
|
+
const lines = ["Available Tools:", ""];
|
|
1109
|
+
if (groupByCategory) {
|
|
1110
|
+
const toolsByCategory = /* @__PURE__ */ new Map();
|
|
1111
|
+
for (const tool of toolsToInclude) {
|
|
1112
|
+
const category = tool.metadata.category;
|
|
1113
|
+
if (!toolsByCategory.has(category)) {
|
|
1114
|
+
toolsByCategory.set(category, []);
|
|
1115
|
+
}
|
|
1116
|
+
toolsByCategory.get(category).push(tool);
|
|
1117
|
+
}
|
|
1118
|
+
for (const [category, categoryTools] of toolsByCategory) {
|
|
1119
|
+
lines.push(`${category.toUpperCase().replace(/-/g, " ")} TOOLS:`);
|
|
1120
|
+
for (const tool of categoryTools) {
|
|
1121
|
+
lines.push(...formatToolForPrompt(tool, {
|
|
1122
|
+
includeExamples,
|
|
1123
|
+
includeNotes,
|
|
1124
|
+
includeLimitations,
|
|
1125
|
+
includeRelations,
|
|
1126
|
+
maxExamplesPerTool,
|
|
1127
|
+
minimal
|
|
1128
|
+
}));
|
|
1129
|
+
}
|
|
1130
|
+
lines.push("");
|
|
1131
|
+
}
|
|
1132
|
+
} else {
|
|
1133
|
+
for (const tool of toolsToInclude) {
|
|
1134
|
+
lines.push(...formatToolForPrompt(tool, {
|
|
1135
|
+
includeExamples,
|
|
1136
|
+
includeNotes,
|
|
1137
|
+
includeLimitations,
|
|
1138
|
+
includeRelations,
|
|
1139
|
+
maxExamplesPerTool,
|
|
1140
|
+
minimal
|
|
1141
|
+
}));
|
|
1142
|
+
lines.push("");
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
return lines.join("\n").trim();
|
|
1146
|
+
}
|
|
1147
|
+
function formatToolForPrompt(tool, options) {
|
|
1148
|
+
const { metadata } = tool;
|
|
1149
|
+
const lines = [];
|
|
1150
|
+
if (options.minimal) {
|
|
1151
|
+
lines.push(`## ${metadata.name}`);
|
|
1152
|
+
let hasContent = false;
|
|
1153
|
+
if (options.includeRelations && metadata.relations) {
|
|
1154
|
+
const relationLines = formatRelations(metadata.relations);
|
|
1155
|
+
if (relationLines.length > 0) {
|
|
1156
|
+
lines.push(...relationLines);
|
|
1157
|
+
hasContent = true;
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1161
|
+
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1162
|
+
const examples = metadata.examples.slice(0, maxExamples);
|
|
1163
|
+
for (const example of examples) {
|
|
1164
|
+
lines.push(` Example: ${example.description}`);
|
|
1165
|
+
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1166
|
+
if (example.explanation) {
|
|
1167
|
+
lines.push(` ${example.explanation}`);
|
|
1168
|
+
}
|
|
1169
|
+
hasContent = true;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
if (options.includeNotes && metadata.usageNotes) {
|
|
1173
|
+
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1174
|
+
hasContent = true;
|
|
1175
|
+
}
|
|
1176
|
+
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1177
|
+
lines.push(" Limitations:");
|
|
1178
|
+
for (const limitation of metadata.limitations) {
|
|
1179
|
+
lines.push(` - ${limitation}`);
|
|
1180
|
+
}
|
|
1181
|
+
hasContent = true;
|
|
1182
|
+
}
|
|
1183
|
+
if (!hasContent) {
|
|
1184
|
+
return [];
|
|
1185
|
+
}
|
|
1186
|
+
return lines;
|
|
1187
|
+
}
|
|
1188
|
+
lines.push(`- ${metadata.name}: ${metadata.description}`);
|
|
1189
|
+
const schemaShape = getSchemaShape(tool.schema);
|
|
1190
|
+
if (schemaShape) {
|
|
1191
|
+
const params = Object.keys(schemaShape);
|
|
1192
|
+
if (params.length > 0) {
|
|
1193
|
+
const paramDescriptions = params.map((param) => {
|
|
1194
|
+
const field = schemaShape[param];
|
|
1195
|
+
const typeName = field._def.typeName;
|
|
1196
|
+
const type = typeName.replace("Zod", "").toLowerCase();
|
|
1197
|
+
return `${param} (${type})`;
|
|
1198
|
+
});
|
|
1199
|
+
lines.push(` Parameters: ${paramDescriptions.join(", ")}`);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
if (options.includeRelations && metadata.relations) {
|
|
1203
|
+
const relationLines = formatRelations(metadata.relations);
|
|
1204
|
+
if (relationLines.length > 0) {
|
|
1205
|
+
lines.push(...relationLines);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (options.includeNotes && metadata.usageNotes) {
|
|
1209
|
+
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1210
|
+
}
|
|
1211
|
+
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1212
|
+
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1213
|
+
const examples = metadata.examples.slice(0, maxExamples);
|
|
1214
|
+
for (const example of examples) {
|
|
1215
|
+
lines.push(` Example: ${example.description}`);
|
|
1216
|
+
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1217
|
+
if (example.explanation) {
|
|
1218
|
+
lines.push(` ${example.explanation}`);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1223
|
+
lines.push(" Limitations:");
|
|
1224
|
+
for (const limitation of metadata.limitations) {
|
|
1225
|
+
lines.push(` - ${limitation}`);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
return lines;
|
|
1229
|
+
}
|
|
1230
|
+
function formatRelations(relations) {
|
|
1231
|
+
const lines = [];
|
|
1232
|
+
if (relations.requires && relations.requires.length > 0) {
|
|
1233
|
+
lines.push(` Requires: ${relations.requires.join(", ")}`);
|
|
1234
|
+
}
|
|
1235
|
+
if (relations.suggests && relations.suggests.length > 0) {
|
|
1236
|
+
lines.push(` Suggests: ${relations.suggests.join(", ")}`);
|
|
1237
|
+
}
|
|
1238
|
+
if (relations.conflicts && relations.conflicts.length > 0) {
|
|
1239
|
+
lines.push(` Conflicts: ${relations.conflicts.join(", ")}`);
|
|
1240
|
+
}
|
|
1241
|
+
if (relations.follows && relations.follows.length > 0) {
|
|
1242
|
+
lines.push(` Follows: ${relations.follows.join(", ")}`);
|
|
1243
|
+
}
|
|
1244
|
+
if (relations.precedes && relations.precedes.length > 0) {
|
|
1245
|
+
lines.push(` Precedes: ${relations.precedes.join(", ")}`);
|
|
1246
|
+
}
|
|
1247
|
+
return lines;
|
|
1248
|
+
}
|
|
1249
|
+
function getSchemaShape(schema) {
|
|
1250
|
+
if (schema instanceof import_zod3.z.ZodObject) {
|
|
1251
|
+
return schema.shape;
|
|
1252
|
+
}
|
|
1253
|
+
return void 0;
|
|
1254
|
+
}
|
|
1051
1255
|
|
|
1052
1256
|
// src/tools/registry.ts
|
|
1053
|
-
var logger = createLogger("agentforge:core:tools:registry", { level: "info" /* INFO */ });
|
|
1054
1257
|
var RegistryEvent = /* @__PURE__ */ ((RegistryEvent2) => {
|
|
1055
1258
|
RegistryEvent2["TOOL_REGISTERED"] = "tool:registered";
|
|
1056
1259
|
RegistryEvent2["TOOL_REMOVED"] = "tool:removed";
|
|
@@ -1323,10 +1526,7 @@ var ToolRegistry = class {
|
|
|
1323
1526
|
* ```
|
|
1324
1527
|
*/
|
|
1325
1528
|
on(event, handler) {
|
|
1326
|
-
|
|
1327
|
-
this.eventHandlers.set(event, /* @__PURE__ */ new Set());
|
|
1328
|
-
}
|
|
1329
|
-
this.eventHandlers.get(event).add(handler);
|
|
1529
|
+
addRegistryEventHandler(this.eventHandlers, event, handler);
|
|
1330
1530
|
}
|
|
1331
1531
|
/**
|
|
1332
1532
|
* Unregister an event handler
|
|
@@ -1342,10 +1542,7 @@ var ToolRegistry = class {
|
|
|
1342
1542
|
* ```
|
|
1343
1543
|
*/
|
|
1344
1544
|
off(event, handler) {
|
|
1345
|
-
|
|
1346
|
-
if (handlers) {
|
|
1347
|
-
handlers.delete(handler);
|
|
1348
|
-
}
|
|
1545
|
+
removeRegistryEventHandler(this.eventHandlers, event, handler);
|
|
1349
1546
|
}
|
|
1350
1547
|
/**
|
|
1351
1548
|
* Emit an event to all registered handlers
|
|
@@ -1355,20 +1552,7 @@ var ToolRegistry = class {
|
|
|
1355
1552
|
* @private
|
|
1356
1553
|
*/
|
|
1357
1554
|
emit(event, data) {
|
|
1358
|
-
|
|
1359
|
-
if (handlers) {
|
|
1360
|
-
handlers.forEach((handler) => {
|
|
1361
|
-
try {
|
|
1362
|
-
handler(data);
|
|
1363
|
-
} catch (error) {
|
|
1364
|
-
logger.error("Event handler error", {
|
|
1365
|
-
event,
|
|
1366
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1367
|
-
...error instanceof Error && error.stack ? { stack: error.stack } : {}
|
|
1368
|
-
});
|
|
1369
|
-
}
|
|
1370
|
-
});
|
|
1371
|
-
}
|
|
1555
|
+
emitRegistryEvent(this.eventHandlers, event, data);
|
|
1372
1556
|
}
|
|
1373
1557
|
/**
|
|
1374
1558
|
* Convert all registered tools to LangChain format
|
|
@@ -1391,7 +1575,7 @@ var ToolRegistry = class {
|
|
|
1391
1575
|
* ```
|
|
1392
1576
|
*/
|
|
1393
1577
|
toLangChainTools() {
|
|
1394
|
-
return
|
|
1578
|
+
return convertRegistryToolsToLangChain(this.getAll());
|
|
1395
1579
|
}
|
|
1396
1580
|
/**
|
|
1397
1581
|
* Generate a formatted prompt describing all tools
|
|
@@ -1422,184 +1606,7 @@ var ToolRegistry = class {
|
|
|
1422
1606
|
* ```
|
|
1423
1607
|
*/
|
|
1424
1608
|
generatePrompt(options = {}) {
|
|
1425
|
-
|
|
1426
|
-
includeExamples = false,
|
|
1427
|
-
includeNotes = false,
|
|
1428
|
-
includeLimitations = false,
|
|
1429
|
-
includeRelations = false,
|
|
1430
|
-
groupByCategory = false,
|
|
1431
|
-
categories,
|
|
1432
|
-
maxExamplesPerTool,
|
|
1433
|
-
minimal = false
|
|
1434
|
-
} = options;
|
|
1435
|
-
let tools = this.getAll();
|
|
1436
|
-
if (categories && categories.length > 0) {
|
|
1437
|
-
tools = tools.filter((tool) => categories.includes(tool.metadata.category));
|
|
1438
|
-
}
|
|
1439
|
-
if (tools.length === 0) {
|
|
1440
|
-
return "No tools available.";
|
|
1441
|
-
}
|
|
1442
|
-
const lines = ["Available Tools:", ""];
|
|
1443
|
-
if (groupByCategory) {
|
|
1444
|
-
const toolsByCategory = /* @__PURE__ */ new Map();
|
|
1445
|
-
for (const tool of tools) {
|
|
1446
|
-
const category = tool.metadata.category;
|
|
1447
|
-
if (!toolsByCategory.has(category)) {
|
|
1448
|
-
toolsByCategory.set(category, []);
|
|
1449
|
-
}
|
|
1450
|
-
toolsByCategory.get(category).push(tool);
|
|
1451
|
-
}
|
|
1452
|
-
for (const [category, categoryTools] of toolsByCategory) {
|
|
1453
|
-
lines.push(`${category.toUpperCase().replace(/-/g, " ")} TOOLS:`);
|
|
1454
|
-
for (const tool of categoryTools) {
|
|
1455
|
-
lines.push(...this.formatToolForPrompt(tool, {
|
|
1456
|
-
includeExamples,
|
|
1457
|
-
includeNotes,
|
|
1458
|
-
includeLimitations,
|
|
1459
|
-
includeRelations,
|
|
1460
|
-
maxExamplesPerTool,
|
|
1461
|
-
minimal
|
|
1462
|
-
}));
|
|
1463
|
-
}
|
|
1464
|
-
lines.push("");
|
|
1465
|
-
}
|
|
1466
|
-
} else {
|
|
1467
|
-
for (const tool of tools) {
|
|
1468
|
-
lines.push(...this.formatToolForPrompt(tool, {
|
|
1469
|
-
includeExamples,
|
|
1470
|
-
includeNotes,
|
|
1471
|
-
includeLimitations,
|
|
1472
|
-
includeRelations,
|
|
1473
|
-
maxExamplesPerTool,
|
|
1474
|
-
minimal
|
|
1475
|
-
}));
|
|
1476
|
-
lines.push("");
|
|
1477
|
-
}
|
|
1478
|
-
}
|
|
1479
|
-
return lines.join("\n").trim();
|
|
1480
|
-
}
|
|
1481
|
-
/**
|
|
1482
|
-
* Format a single tool for inclusion in a prompt
|
|
1483
|
-
*
|
|
1484
|
-
* @param tool - The tool to format
|
|
1485
|
-
* @param options - Formatting options
|
|
1486
|
-
* @returns Array of formatted lines
|
|
1487
|
-
* @private
|
|
1488
|
-
*/
|
|
1489
|
-
formatToolForPrompt(tool, options) {
|
|
1490
|
-
const { metadata } = tool;
|
|
1491
|
-
const lines = [];
|
|
1492
|
-
if (options.minimal) {
|
|
1493
|
-
lines.push(`## ${metadata.name}`);
|
|
1494
|
-
let hasContent = false;
|
|
1495
|
-
if (options.includeRelations && metadata.relations) {
|
|
1496
|
-
const relationLines = this.formatRelations(metadata.relations);
|
|
1497
|
-
if (relationLines.length > 0) {
|
|
1498
|
-
lines.push(...relationLines);
|
|
1499
|
-
hasContent = true;
|
|
1500
|
-
}
|
|
1501
|
-
}
|
|
1502
|
-
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1503
|
-
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1504
|
-
const examples = metadata.examples.slice(0, maxExamples);
|
|
1505
|
-
for (const example of examples) {
|
|
1506
|
-
lines.push(` Example: ${example.description}`);
|
|
1507
|
-
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1508
|
-
if (example.explanation) {
|
|
1509
|
-
lines.push(` ${example.explanation}`);
|
|
1510
|
-
}
|
|
1511
|
-
hasContent = true;
|
|
1512
|
-
}
|
|
1513
|
-
}
|
|
1514
|
-
if (options.includeNotes && metadata.usageNotes) {
|
|
1515
|
-
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1516
|
-
hasContent = true;
|
|
1517
|
-
}
|
|
1518
|
-
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1519
|
-
lines.push(` Limitations:`);
|
|
1520
|
-
for (const limitation of metadata.limitations) {
|
|
1521
|
-
lines.push(` - ${limitation}`);
|
|
1522
|
-
}
|
|
1523
|
-
hasContent = true;
|
|
1524
|
-
}
|
|
1525
|
-
if (!hasContent) {
|
|
1526
|
-
return [];
|
|
1527
|
-
}
|
|
1528
|
-
return lines;
|
|
1529
|
-
}
|
|
1530
|
-
lines.push(`- ${metadata.name}: ${metadata.description}`);
|
|
1531
|
-
const schemaShape = this.getSchemaShape(tool.schema);
|
|
1532
|
-
if (schemaShape) {
|
|
1533
|
-
const params = Object.keys(schemaShape);
|
|
1534
|
-
if (params.length > 0) {
|
|
1535
|
-
const paramDescriptions = params.map((param) => {
|
|
1536
|
-
const field = schemaShape[param];
|
|
1537
|
-
const typeName = field._def.typeName;
|
|
1538
|
-
const type = typeName.replace("Zod", "").toLowerCase();
|
|
1539
|
-
return `${param} (${type})`;
|
|
1540
|
-
});
|
|
1541
|
-
lines.push(` Parameters: ${paramDescriptions.join(", ")}`);
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
if (options.includeRelations && metadata.relations) {
|
|
1545
|
-
const relationLines = this.formatRelations(metadata.relations);
|
|
1546
|
-
if (relationLines.length > 0) {
|
|
1547
|
-
lines.push(...relationLines);
|
|
1548
|
-
}
|
|
1549
|
-
}
|
|
1550
|
-
if (options.includeNotes && metadata.usageNotes) {
|
|
1551
|
-
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1552
|
-
}
|
|
1553
|
-
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1554
|
-
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1555
|
-
const examples = metadata.examples.slice(0, maxExamples);
|
|
1556
|
-
for (const example of examples) {
|
|
1557
|
-
lines.push(` Example: ${example.description}`);
|
|
1558
|
-
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1559
|
-
if (example.explanation) {
|
|
1560
|
-
lines.push(` ${example.explanation}`);
|
|
1561
|
-
}
|
|
1562
|
-
}
|
|
1563
|
-
}
|
|
1564
|
-
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1565
|
-
lines.push(` Limitations:`);
|
|
1566
|
-
for (const limitation of metadata.limitations) {
|
|
1567
|
-
lines.push(` - ${limitation}`);
|
|
1568
|
-
}
|
|
1569
|
-
}
|
|
1570
|
-
return lines;
|
|
1571
|
-
}
|
|
1572
|
-
/**
|
|
1573
|
-
* Format tool relations for inclusion in a prompt
|
|
1574
|
-
*
|
|
1575
|
-
* @param relations - The relations to format
|
|
1576
|
-
* @returns Array of formatted lines
|
|
1577
|
-
* @private
|
|
1578
|
-
*/
|
|
1579
|
-
formatRelations(relations) {
|
|
1580
|
-
const lines = [];
|
|
1581
|
-
if (relations.requires && relations.requires.length > 0) {
|
|
1582
|
-
lines.push(` Requires: ${relations.requires.join(", ")}`);
|
|
1583
|
-
}
|
|
1584
|
-
if (relations.suggests && relations.suggests.length > 0) {
|
|
1585
|
-
lines.push(` Suggests: ${relations.suggests.join(", ")}`);
|
|
1586
|
-
}
|
|
1587
|
-
if (relations.conflicts && relations.conflicts.length > 0) {
|
|
1588
|
-
lines.push(` Conflicts: ${relations.conflicts.join(", ")}`);
|
|
1589
|
-
}
|
|
1590
|
-
if (relations.follows && relations.follows.length > 0) {
|
|
1591
|
-
lines.push(` Follows: ${relations.follows.join(", ")}`);
|
|
1592
|
-
}
|
|
1593
|
-
if (relations.precedes && relations.precedes.length > 0) {
|
|
1594
|
-
lines.push(` Precedes: ${relations.precedes.join(", ")}`);
|
|
1595
|
-
}
|
|
1596
|
-
return lines;
|
|
1597
|
-
}
|
|
1598
|
-
getSchemaShape(schema) {
|
|
1599
|
-
if (schema instanceof import_zod3.z.ZodObject) {
|
|
1600
|
-
return schema.shape;
|
|
1601
|
-
}
|
|
1602
|
-
return void 0;
|
|
1609
|
+
return generateRegistryPrompt(this.getAll(), options);
|
|
1603
1610
|
}
|
|
1604
1611
|
};
|
|
1605
1612
|
|