@agentforge/core 0.16.11 → 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 +320 -298
- package/dist/index.d.cts +16 -21
- package/dist/index.d.ts +16 -21
- package/dist/index.js +320 -298
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -841,6 +841,159 @@ function toolBuilder() {
|
|
|
841
841
|
return new ToolBuilder();
|
|
842
842
|
}
|
|
843
843
|
|
|
844
|
+
// src/tools/registry-collection.ts
|
|
845
|
+
function getAllRegistryTools(tools) {
|
|
846
|
+
return Array.from(tools.values());
|
|
847
|
+
}
|
|
848
|
+
function getRegistryToolNames(tools) {
|
|
849
|
+
return Array.from(tools.keys());
|
|
850
|
+
}
|
|
851
|
+
function getRegistryToolsByCategory(tools, category) {
|
|
852
|
+
return getAllRegistryTools(tools).filter((tool) => tool.metadata.category === category);
|
|
853
|
+
}
|
|
854
|
+
function getRegistryToolsByTag(tools, tag) {
|
|
855
|
+
return getAllRegistryTools(tools).filter((tool) => tool.metadata.tags?.includes(tag));
|
|
856
|
+
}
|
|
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);
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// src/langgraph/observability/logger.ts
|
|
868
|
+
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
869
|
+
LogLevel2["DEBUG"] = "debug";
|
|
870
|
+
LogLevel2["INFO"] = "info";
|
|
871
|
+
LogLevel2["WARN"] = "warn";
|
|
872
|
+
LogLevel2["ERROR"] = "error";
|
|
873
|
+
return LogLevel2;
|
|
874
|
+
})(LogLevel || {});
|
|
875
|
+
var LOG_LEVEL_PRIORITY = {
|
|
876
|
+
["debug" /* DEBUG */]: 0,
|
|
877
|
+
["info" /* INFO */]: 1,
|
|
878
|
+
["warn" /* WARN */]: 2,
|
|
879
|
+
["error" /* ERROR */]: 3
|
|
880
|
+
};
|
|
881
|
+
var LoggerImpl = class _LoggerImpl {
|
|
882
|
+
name;
|
|
883
|
+
options;
|
|
884
|
+
context;
|
|
885
|
+
constructor(name, options = {}, context = {}) {
|
|
886
|
+
this.name = name;
|
|
887
|
+
this.context = context;
|
|
888
|
+
this.options = {
|
|
889
|
+
level: options.level ?? "info" /* INFO */,
|
|
890
|
+
format: options.format ?? "pretty",
|
|
891
|
+
destination: options.destination ?? process.stdout,
|
|
892
|
+
includeTimestamp: options.includeTimestamp ?? true,
|
|
893
|
+
includeContext: options.includeContext ?? true
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
debug(message, data) {
|
|
897
|
+
this.log("debug" /* DEBUG */, message, data);
|
|
898
|
+
}
|
|
899
|
+
info(message, data) {
|
|
900
|
+
this.log("info" /* INFO */, message, data);
|
|
901
|
+
}
|
|
902
|
+
warn(message, data) {
|
|
903
|
+
this.log("warn" /* WARN */, message, data);
|
|
904
|
+
}
|
|
905
|
+
error(message, data) {
|
|
906
|
+
this.log("error" /* ERROR */, message, data);
|
|
907
|
+
}
|
|
908
|
+
isDebugEnabled() {
|
|
909
|
+
return this.isLevelEnabled("debug" /* DEBUG */);
|
|
910
|
+
}
|
|
911
|
+
isLevelEnabled(level) {
|
|
912
|
+
return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[this.options.level];
|
|
913
|
+
}
|
|
914
|
+
withContext(context) {
|
|
915
|
+
return new _LoggerImpl(this.name, this.options, { ...this.context, ...context });
|
|
916
|
+
}
|
|
917
|
+
log(level, message, data) {
|
|
918
|
+
if (LOG_LEVEL_PRIORITY[level] < LOG_LEVEL_PRIORITY[this.options.level]) {
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
const entry = {
|
|
922
|
+
level,
|
|
923
|
+
name: this.name,
|
|
924
|
+
message
|
|
925
|
+
};
|
|
926
|
+
if (this.options.includeTimestamp) {
|
|
927
|
+
entry.timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
928
|
+
}
|
|
929
|
+
if (this.options.includeContext && Object.keys(this.context).length > 0) {
|
|
930
|
+
entry.context = this.context;
|
|
931
|
+
}
|
|
932
|
+
if (data !== void 0) {
|
|
933
|
+
entry.data = data;
|
|
934
|
+
}
|
|
935
|
+
const output = this.format(entry);
|
|
936
|
+
this.options.destination.write(output + "\n");
|
|
937
|
+
}
|
|
938
|
+
format(entry) {
|
|
939
|
+
if (this.options.format === "json") {
|
|
940
|
+
return JSON.stringify(entry);
|
|
941
|
+
}
|
|
942
|
+
const parts = [];
|
|
943
|
+
if (entry.timestamp) {
|
|
944
|
+
parts.push(`[${entry.timestamp}]`);
|
|
945
|
+
}
|
|
946
|
+
parts.push(`[${entry.level.toUpperCase()}]`);
|
|
947
|
+
parts.push(`[${entry.name}]`);
|
|
948
|
+
parts.push(entry.message);
|
|
949
|
+
if (entry.context) {
|
|
950
|
+
parts.push(`context=${JSON.stringify(entry.context)}`);
|
|
951
|
+
}
|
|
952
|
+
if (entry.data !== void 0) {
|
|
953
|
+
parts.push(`data=${JSON.stringify(entry.data)}`);
|
|
954
|
+
}
|
|
955
|
+
return parts.join(" ");
|
|
956
|
+
}
|
|
957
|
+
};
|
|
958
|
+
function createLogger(name, options) {
|
|
959
|
+
return new LoggerImpl(name, options);
|
|
960
|
+
}
|
|
961
|
+
|
|
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);
|
|
969
|
+
}
|
|
970
|
+
function removeRegistryEventHandler(eventHandlers, event, handler) {
|
|
971
|
+
const handlers = eventHandlers.get(event);
|
|
972
|
+
if (handlers) {
|
|
973
|
+
handlers.delete(handler);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
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
|
+
});
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// src/tools/registry-prompt.ts
|
|
995
|
+
var import_zod3 = require("zod");
|
|
996
|
+
|
|
844
997
|
// src/langchain/converter.ts
|
|
845
998
|
var import_tools = require("@langchain/core/tools");
|
|
846
999
|
var import_zod_to_json_schema = require("zod-to-json-schema");
|
|
@@ -928,106 +1081,179 @@ Examples:`);
|
|
|
928
1081
|
return parts.join("\n");
|
|
929
1082
|
}
|
|
930
1083
|
|
|
931
|
-
// src/tools/registry.ts
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
context;
|
|
952
|
-
constructor(name, options = {}, context = {}) {
|
|
953
|
-
this.name = name;
|
|
954
|
-
this.context = context;
|
|
955
|
-
this.options = {
|
|
956
|
-
level: options.level ?? "info" /* INFO */,
|
|
957
|
-
format: options.format ?? "pretty",
|
|
958
|
-
destination: options.destination ?? process.stdout,
|
|
959
|
-
includeTimestamp: options.includeTimestamp ?? true,
|
|
960
|
-
includeContext: options.includeContext ?? true
|
|
961
|
-
};
|
|
962
|
-
}
|
|
963
|
-
debug(message, data) {
|
|
964
|
-
this.log("debug" /* DEBUG */, message, data);
|
|
965
|
-
}
|
|
966
|
-
info(message, data) {
|
|
967
|
-
this.log("info" /* INFO */, message, data);
|
|
968
|
-
}
|
|
969
|
-
warn(message, data) {
|
|
970
|
-
this.log("warn" /* WARN */, message, data);
|
|
971
|
-
}
|
|
972
|
-
error(message, data) {
|
|
973
|
-
this.log("error" /* ERROR */, message, data);
|
|
974
|
-
}
|
|
975
|
-
isDebugEnabled() {
|
|
976
|
-
return this.isLevelEnabled("debug" /* DEBUG */);
|
|
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
|
+
);
|
|
977
1104
|
}
|
|
978
|
-
|
|
979
|
-
return
|
|
1105
|
+
if (toolsToInclude.length === 0) {
|
|
1106
|
+
return "No tools available.";
|
|
980
1107
|
}
|
|
981
|
-
|
|
982
|
-
|
|
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
|
+
}
|
|
983
1144
|
}
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
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
|
+
}
|
|
987
1159
|
}
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
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
|
+
}
|
|
995
1171
|
}
|
|
996
|
-
if (
|
|
997
|
-
|
|
1172
|
+
if (options.includeNotes && metadata.usageNotes) {
|
|
1173
|
+
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1174
|
+
hasContent = true;
|
|
998
1175
|
}
|
|
999
|
-
if (
|
|
1000
|
-
|
|
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;
|
|
1001
1182
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1183
|
+
if (!hasContent) {
|
|
1184
|
+
return [];
|
|
1185
|
+
}
|
|
1186
|
+
return lines;
|
|
1004
1187
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
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(", ")}`);
|
|
1008
1200
|
}
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1201
|
+
}
|
|
1202
|
+
if (options.includeRelations && metadata.relations) {
|
|
1203
|
+
const relationLines = formatRelations(metadata.relations);
|
|
1204
|
+
if (relationLines.length > 0) {
|
|
1205
|
+
lines.push(...relationLines);
|
|
1012
1206
|
}
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
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
|
+
}
|
|
1018
1220
|
}
|
|
1019
|
-
|
|
1020
|
-
|
|
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}`);
|
|
1021
1226
|
}
|
|
1022
|
-
return parts.join(" ");
|
|
1023
1227
|
}
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
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;
|
|
1027
1254
|
}
|
|
1028
1255
|
|
|
1029
1256
|
// src/tools/registry.ts
|
|
1030
|
-
var logger = createLogger("agentforge:core:tools:registry", { level: "info" /* INFO */ });
|
|
1031
1257
|
var RegistryEvent = /* @__PURE__ */ ((RegistryEvent2) => {
|
|
1032
1258
|
RegistryEvent2["TOOL_REGISTERED"] = "tool:registered";
|
|
1033
1259
|
RegistryEvent2["TOOL_REMOVED"] = "tool:removed";
|
|
@@ -1156,7 +1382,7 @@ var ToolRegistry = class {
|
|
|
1156
1382
|
* ```
|
|
1157
1383
|
*/
|
|
1158
1384
|
getAll() {
|
|
1159
|
-
return
|
|
1385
|
+
return getAllRegistryTools(this.tools);
|
|
1160
1386
|
}
|
|
1161
1387
|
/**
|
|
1162
1388
|
* Get tools by category
|
|
@@ -1170,7 +1396,7 @@ var ToolRegistry = class {
|
|
|
1170
1396
|
* ```
|
|
1171
1397
|
*/
|
|
1172
1398
|
getByCategory(category) {
|
|
1173
|
-
return this.
|
|
1399
|
+
return getRegistryToolsByCategory(this.tools, category);
|
|
1174
1400
|
}
|
|
1175
1401
|
/**
|
|
1176
1402
|
* Get tools by tag
|
|
@@ -1184,9 +1410,7 @@ var ToolRegistry = class {
|
|
|
1184
1410
|
* ```
|
|
1185
1411
|
*/
|
|
1186
1412
|
getByTag(tag) {
|
|
1187
|
-
return this.
|
|
1188
|
-
(tool) => tool.metadata.tags?.includes(tag)
|
|
1189
|
-
);
|
|
1413
|
+
return getRegistryToolsByTag(this.tools, tag);
|
|
1190
1414
|
}
|
|
1191
1415
|
/**
|
|
1192
1416
|
* Search tools by name or description
|
|
@@ -1203,13 +1427,7 @@ var ToolRegistry = class {
|
|
|
1203
1427
|
* ```
|
|
1204
1428
|
*/
|
|
1205
1429
|
search(query) {
|
|
1206
|
-
|
|
1207
|
-
return this.getAll().filter((tool) => {
|
|
1208
|
-
const name = tool.metadata.name.toLowerCase();
|
|
1209
|
-
const displayName = tool.metadata.displayName?.toLowerCase() || "";
|
|
1210
|
-
const description = tool.metadata.description.toLowerCase();
|
|
1211
|
-
return name.includes(lowerQuery) || displayName.includes(lowerQuery) || description.includes(lowerQuery);
|
|
1212
|
-
});
|
|
1430
|
+
return searchRegistryTools(this.tools, query);
|
|
1213
1431
|
}
|
|
1214
1432
|
/**
|
|
1215
1433
|
* Register multiple tools at once
|
|
@@ -1292,7 +1510,7 @@ var ToolRegistry = class {
|
|
|
1292
1510
|
* ```
|
|
1293
1511
|
*/
|
|
1294
1512
|
getNames() {
|
|
1295
|
-
return
|
|
1513
|
+
return getRegistryToolNames(this.tools);
|
|
1296
1514
|
}
|
|
1297
1515
|
/**
|
|
1298
1516
|
* Register an event handler
|
|
@@ -1308,10 +1526,7 @@ var ToolRegistry = class {
|
|
|
1308
1526
|
* ```
|
|
1309
1527
|
*/
|
|
1310
1528
|
on(event, handler) {
|
|
1311
|
-
|
|
1312
|
-
this.eventHandlers.set(event, /* @__PURE__ */ new Set());
|
|
1313
|
-
}
|
|
1314
|
-
this.eventHandlers.get(event).add(handler);
|
|
1529
|
+
addRegistryEventHandler(this.eventHandlers, event, handler);
|
|
1315
1530
|
}
|
|
1316
1531
|
/**
|
|
1317
1532
|
* Unregister an event handler
|
|
@@ -1327,10 +1542,7 @@ var ToolRegistry = class {
|
|
|
1327
1542
|
* ```
|
|
1328
1543
|
*/
|
|
1329
1544
|
off(event, handler) {
|
|
1330
|
-
|
|
1331
|
-
if (handlers) {
|
|
1332
|
-
handlers.delete(handler);
|
|
1333
|
-
}
|
|
1545
|
+
removeRegistryEventHandler(this.eventHandlers, event, handler);
|
|
1334
1546
|
}
|
|
1335
1547
|
/**
|
|
1336
1548
|
* Emit an event to all registered handlers
|
|
@@ -1340,20 +1552,7 @@ var ToolRegistry = class {
|
|
|
1340
1552
|
* @private
|
|
1341
1553
|
*/
|
|
1342
1554
|
emit(event, data) {
|
|
1343
|
-
|
|
1344
|
-
if (handlers) {
|
|
1345
|
-
handlers.forEach((handler) => {
|
|
1346
|
-
try {
|
|
1347
|
-
handler(data);
|
|
1348
|
-
} catch (error) {
|
|
1349
|
-
logger.error("Event handler error", {
|
|
1350
|
-
event,
|
|
1351
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1352
|
-
...error instanceof Error && error.stack ? { stack: error.stack } : {}
|
|
1353
|
-
});
|
|
1354
|
-
}
|
|
1355
|
-
});
|
|
1356
|
-
}
|
|
1555
|
+
emitRegistryEvent(this.eventHandlers, event, data);
|
|
1357
1556
|
}
|
|
1358
1557
|
/**
|
|
1359
1558
|
* Convert all registered tools to LangChain format
|
|
@@ -1376,7 +1575,7 @@ var ToolRegistry = class {
|
|
|
1376
1575
|
* ```
|
|
1377
1576
|
*/
|
|
1378
1577
|
toLangChainTools() {
|
|
1379
|
-
return
|
|
1578
|
+
return convertRegistryToolsToLangChain(this.getAll());
|
|
1380
1579
|
}
|
|
1381
1580
|
/**
|
|
1382
1581
|
* Generate a formatted prompt describing all tools
|
|
@@ -1407,184 +1606,7 @@ var ToolRegistry = class {
|
|
|
1407
1606
|
* ```
|
|
1408
1607
|
*/
|
|
1409
1608
|
generatePrompt(options = {}) {
|
|
1410
|
-
|
|
1411
|
-
includeExamples = false,
|
|
1412
|
-
includeNotes = false,
|
|
1413
|
-
includeLimitations = false,
|
|
1414
|
-
includeRelations = false,
|
|
1415
|
-
groupByCategory = false,
|
|
1416
|
-
categories,
|
|
1417
|
-
maxExamplesPerTool,
|
|
1418
|
-
minimal = false
|
|
1419
|
-
} = options;
|
|
1420
|
-
let tools = this.getAll();
|
|
1421
|
-
if (categories && categories.length > 0) {
|
|
1422
|
-
tools = tools.filter((tool) => categories.includes(tool.metadata.category));
|
|
1423
|
-
}
|
|
1424
|
-
if (tools.length === 0) {
|
|
1425
|
-
return "No tools available.";
|
|
1426
|
-
}
|
|
1427
|
-
const lines = ["Available Tools:", ""];
|
|
1428
|
-
if (groupByCategory) {
|
|
1429
|
-
const toolsByCategory = /* @__PURE__ */ new Map();
|
|
1430
|
-
for (const tool of tools) {
|
|
1431
|
-
const category = tool.metadata.category;
|
|
1432
|
-
if (!toolsByCategory.has(category)) {
|
|
1433
|
-
toolsByCategory.set(category, []);
|
|
1434
|
-
}
|
|
1435
|
-
toolsByCategory.get(category).push(tool);
|
|
1436
|
-
}
|
|
1437
|
-
for (const [category, categoryTools] of toolsByCategory) {
|
|
1438
|
-
lines.push(`${category.toUpperCase().replace(/-/g, " ")} TOOLS:`);
|
|
1439
|
-
for (const tool of categoryTools) {
|
|
1440
|
-
lines.push(...this.formatToolForPrompt(tool, {
|
|
1441
|
-
includeExamples,
|
|
1442
|
-
includeNotes,
|
|
1443
|
-
includeLimitations,
|
|
1444
|
-
includeRelations,
|
|
1445
|
-
maxExamplesPerTool,
|
|
1446
|
-
minimal
|
|
1447
|
-
}));
|
|
1448
|
-
}
|
|
1449
|
-
lines.push("");
|
|
1450
|
-
}
|
|
1451
|
-
} else {
|
|
1452
|
-
for (const tool of tools) {
|
|
1453
|
-
lines.push(...this.formatToolForPrompt(tool, {
|
|
1454
|
-
includeExamples,
|
|
1455
|
-
includeNotes,
|
|
1456
|
-
includeLimitations,
|
|
1457
|
-
includeRelations,
|
|
1458
|
-
maxExamplesPerTool,
|
|
1459
|
-
minimal
|
|
1460
|
-
}));
|
|
1461
|
-
lines.push("");
|
|
1462
|
-
}
|
|
1463
|
-
}
|
|
1464
|
-
return lines.join("\n").trim();
|
|
1465
|
-
}
|
|
1466
|
-
/**
|
|
1467
|
-
* Format a single tool for inclusion in a prompt
|
|
1468
|
-
*
|
|
1469
|
-
* @param tool - The tool to format
|
|
1470
|
-
* @param options - Formatting options
|
|
1471
|
-
* @returns Array of formatted lines
|
|
1472
|
-
* @private
|
|
1473
|
-
*/
|
|
1474
|
-
formatToolForPrompt(tool, options) {
|
|
1475
|
-
const { metadata } = tool;
|
|
1476
|
-
const lines = [];
|
|
1477
|
-
if (options.minimal) {
|
|
1478
|
-
lines.push(`## ${metadata.name}`);
|
|
1479
|
-
let hasContent = false;
|
|
1480
|
-
if (options.includeRelations && metadata.relations) {
|
|
1481
|
-
const relationLines = this.formatRelations(metadata.relations);
|
|
1482
|
-
if (relationLines.length > 0) {
|
|
1483
|
-
lines.push(...relationLines);
|
|
1484
|
-
hasContent = true;
|
|
1485
|
-
}
|
|
1486
|
-
}
|
|
1487
|
-
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1488
|
-
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1489
|
-
const examples = metadata.examples.slice(0, maxExamples);
|
|
1490
|
-
for (const example of examples) {
|
|
1491
|
-
lines.push(` Example: ${example.description}`);
|
|
1492
|
-
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1493
|
-
if (example.explanation) {
|
|
1494
|
-
lines.push(` ${example.explanation}`);
|
|
1495
|
-
}
|
|
1496
|
-
hasContent = true;
|
|
1497
|
-
}
|
|
1498
|
-
}
|
|
1499
|
-
if (options.includeNotes && metadata.usageNotes) {
|
|
1500
|
-
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1501
|
-
hasContent = true;
|
|
1502
|
-
}
|
|
1503
|
-
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1504
|
-
lines.push(` Limitations:`);
|
|
1505
|
-
for (const limitation of metadata.limitations) {
|
|
1506
|
-
lines.push(` - ${limitation}`);
|
|
1507
|
-
}
|
|
1508
|
-
hasContent = true;
|
|
1509
|
-
}
|
|
1510
|
-
if (!hasContent) {
|
|
1511
|
-
return [];
|
|
1512
|
-
}
|
|
1513
|
-
return lines;
|
|
1514
|
-
}
|
|
1515
|
-
lines.push(`- ${metadata.name}: ${metadata.description}`);
|
|
1516
|
-
const schemaShape = this.getSchemaShape(tool.schema);
|
|
1517
|
-
if (schemaShape) {
|
|
1518
|
-
const params = Object.keys(schemaShape);
|
|
1519
|
-
if (params.length > 0) {
|
|
1520
|
-
const paramDescriptions = params.map((param) => {
|
|
1521
|
-
const field = schemaShape[param];
|
|
1522
|
-
const typeName = field._def.typeName;
|
|
1523
|
-
const type = typeName.replace("Zod", "").toLowerCase();
|
|
1524
|
-
return `${param} (${type})`;
|
|
1525
|
-
});
|
|
1526
|
-
lines.push(` Parameters: ${paramDescriptions.join(", ")}`);
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
if (options.includeRelations && metadata.relations) {
|
|
1530
|
-
const relationLines = this.formatRelations(metadata.relations);
|
|
1531
|
-
if (relationLines.length > 0) {
|
|
1532
|
-
lines.push(...relationLines);
|
|
1533
|
-
}
|
|
1534
|
-
}
|
|
1535
|
-
if (options.includeNotes && metadata.usageNotes) {
|
|
1536
|
-
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1537
|
-
}
|
|
1538
|
-
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1539
|
-
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1540
|
-
const examples = metadata.examples.slice(0, maxExamples);
|
|
1541
|
-
for (const example of examples) {
|
|
1542
|
-
lines.push(` Example: ${example.description}`);
|
|
1543
|
-
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1544
|
-
if (example.explanation) {
|
|
1545
|
-
lines.push(` ${example.explanation}`);
|
|
1546
|
-
}
|
|
1547
|
-
}
|
|
1548
|
-
}
|
|
1549
|
-
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1550
|
-
lines.push(` Limitations:`);
|
|
1551
|
-
for (const limitation of metadata.limitations) {
|
|
1552
|
-
lines.push(` - ${limitation}`);
|
|
1553
|
-
}
|
|
1554
|
-
}
|
|
1555
|
-
return lines;
|
|
1556
|
-
}
|
|
1557
|
-
/**
|
|
1558
|
-
* Format tool relations for inclusion in a prompt
|
|
1559
|
-
*
|
|
1560
|
-
* @param relations - The relations to format
|
|
1561
|
-
* @returns Array of formatted lines
|
|
1562
|
-
* @private
|
|
1563
|
-
*/
|
|
1564
|
-
formatRelations(relations) {
|
|
1565
|
-
const lines = [];
|
|
1566
|
-
if (relations.requires && relations.requires.length > 0) {
|
|
1567
|
-
lines.push(` Requires: ${relations.requires.join(", ")}`);
|
|
1568
|
-
}
|
|
1569
|
-
if (relations.suggests && relations.suggests.length > 0) {
|
|
1570
|
-
lines.push(` Suggests: ${relations.suggests.join(", ")}`);
|
|
1571
|
-
}
|
|
1572
|
-
if (relations.conflicts && relations.conflicts.length > 0) {
|
|
1573
|
-
lines.push(` Conflicts: ${relations.conflicts.join(", ")}`);
|
|
1574
|
-
}
|
|
1575
|
-
if (relations.follows && relations.follows.length > 0) {
|
|
1576
|
-
lines.push(` Follows: ${relations.follows.join(", ")}`);
|
|
1577
|
-
}
|
|
1578
|
-
if (relations.precedes && relations.precedes.length > 0) {
|
|
1579
|
-
lines.push(` Precedes: ${relations.precedes.join(", ")}`);
|
|
1580
|
-
}
|
|
1581
|
-
return lines;
|
|
1582
|
-
}
|
|
1583
|
-
getSchemaShape(schema) {
|
|
1584
|
-
if (schema instanceof import_zod3.z.ZodObject) {
|
|
1585
|
-
return schema.shape;
|
|
1586
|
-
}
|
|
1587
|
-
return void 0;
|
|
1609
|
+
return generateRegistryPrompt(this.getAll(), options);
|
|
1588
1610
|
}
|
|
1589
1611
|
};
|
|
1590
1612
|
|