@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.js
CHANGED
|
@@ -666,6 +666,159 @@ function toolBuilder() {
|
|
|
666
666
|
return new ToolBuilder();
|
|
667
667
|
}
|
|
668
668
|
|
|
669
|
+
// src/tools/registry-collection.ts
|
|
670
|
+
function getAllRegistryTools(tools) {
|
|
671
|
+
return Array.from(tools.values());
|
|
672
|
+
}
|
|
673
|
+
function getRegistryToolNames(tools) {
|
|
674
|
+
return Array.from(tools.keys());
|
|
675
|
+
}
|
|
676
|
+
function getRegistryToolsByCategory(tools, category) {
|
|
677
|
+
return getAllRegistryTools(tools).filter((tool) => tool.metadata.category === category);
|
|
678
|
+
}
|
|
679
|
+
function getRegistryToolsByTag(tools, tag) {
|
|
680
|
+
return getAllRegistryTools(tools).filter((tool) => tool.metadata.tags?.includes(tag));
|
|
681
|
+
}
|
|
682
|
+
function searchRegistryTools(tools, query) {
|
|
683
|
+
const lowerQuery = query.toLowerCase();
|
|
684
|
+
return getAllRegistryTools(tools).filter((tool) => {
|
|
685
|
+
const name = tool.metadata.name.toLowerCase();
|
|
686
|
+
const displayName = tool.metadata.displayName?.toLowerCase() ?? "";
|
|
687
|
+
const description = tool.metadata.description.toLowerCase();
|
|
688
|
+
return name.includes(lowerQuery) || displayName.includes(lowerQuery) || description.includes(lowerQuery);
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
// src/langgraph/observability/logger.ts
|
|
693
|
+
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
694
|
+
LogLevel2["DEBUG"] = "debug";
|
|
695
|
+
LogLevel2["INFO"] = "info";
|
|
696
|
+
LogLevel2["WARN"] = "warn";
|
|
697
|
+
LogLevel2["ERROR"] = "error";
|
|
698
|
+
return LogLevel2;
|
|
699
|
+
})(LogLevel || {});
|
|
700
|
+
var LOG_LEVEL_PRIORITY = {
|
|
701
|
+
["debug" /* DEBUG */]: 0,
|
|
702
|
+
["info" /* INFO */]: 1,
|
|
703
|
+
["warn" /* WARN */]: 2,
|
|
704
|
+
["error" /* ERROR */]: 3
|
|
705
|
+
};
|
|
706
|
+
var LoggerImpl = class _LoggerImpl {
|
|
707
|
+
name;
|
|
708
|
+
options;
|
|
709
|
+
context;
|
|
710
|
+
constructor(name, options = {}, context = {}) {
|
|
711
|
+
this.name = name;
|
|
712
|
+
this.context = context;
|
|
713
|
+
this.options = {
|
|
714
|
+
level: options.level ?? "info" /* INFO */,
|
|
715
|
+
format: options.format ?? "pretty",
|
|
716
|
+
destination: options.destination ?? process.stdout,
|
|
717
|
+
includeTimestamp: options.includeTimestamp ?? true,
|
|
718
|
+
includeContext: options.includeContext ?? true
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
debug(message, data) {
|
|
722
|
+
this.log("debug" /* DEBUG */, message, data);
|
|
723
|
+
}
|
|
724
|
+
info(message, data) {
|
|
725
|
+
this.log("info" /* INFO */, message, data);
|
|
726
|
+
}
|
|
727
|
+
warn(message, data) {
|
|
728
|
+
this.log("warn" /* WARN */, message, data);
|
|
729
|
+
}
|
|
730
|
+
error(message, data) {
|
|
731
|
+
this.log("error" /* ERROR */, message, data);
|
|
732
|
+
}
|
|
733
|
+
isDebugEnabled() {
|
|
734
|
+
return this.isLevelEnabled("debug" /* DEBUG */);
|
|
735
|
+
}
|
|
736
|
+
isLevelEnabled(level) {
|
|
737
|
+
return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[this.options.level];
|
|
738
|
+
}
|
|
739
|
+
withContext(context) {
|
|
740
|
+
return new _LoggerImpl(this.name, this.options, { ...this.context, ...context });
|
|
741
|
+
}
|
|
742
|
+
log(level, message, data) {
|
|
743
|
+
if (LOG_LEVEL_PRIORITY[level] < LOG_LEVEL_PRIORITY[this.options.level]) {
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const entry = {
|
|
747
|
+
level,
|
|
748
|
+
name: this.name,
|
|
749
|
+
message
|
|
750
|
+
};
|
|
751
|
+
if (this.options.includeTimestamp) {
|
|
752
|
+
entry.timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
753
|
+
}
|
|
754
|
+
if (this.options.includeContext && Object.keys(this.context).length > 0) {
|
|
755
|
+
entry.context = this.context;
|
|
756
|
+
}
|
|
757
|
+
if (data !== void 0) {
|
|
758
|
+
entry.data = data;
|
|
759
|
+
}
|
|
760
|
+
const output = this.format(entry);
|
|
761
|
+
this.options.destination.write(output + "\n");
|
|
762
|
+
}
|
|
763
|
+
format(entry) {
|
|
764
|
+
if (this.options.format === "json") {
|
|
765
|
+
return JSON.stringify(entry);
|
|
766
|
+
}
|
|
767
|
+
const parts = [];
|
|
768
|
+
if (entry.timestamp) {
|
|
769
|
+
parts.push(`[${entry.timestamp}]`);
|
|
770
|
+
}
|
|
771
|
+
parts.push(`[${entry.level.toUpperCase()}]`);
|
|
772
|
+
parts.push(`[${entry.name}]`);
|
|
773
|
+
parts.push(entry.message);
|
|
774
|
+
if (entry.context) {
|
|
775
|
+
parts.push(`context=${JSON.stringify(entry.context)}`);
|
|
776
|
+
}
|
|
777
|
+
if (entry.data !== void 0) {
|
|
778
|
+
parts.push(`data=${JSON.stringify(entry.data)}`);
|
|
779
|
+
}
|
|
780
|
+
return parts.join(" ");
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
function createLogger(name, options) {
|
|
784
|
+
return new LoggerImpl(name, options);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
// src/tools/registry-events.ts
|
|
788
|
+
var logger = createLogger("agentforge:core:tools:registry", { level: "info" /* INFO */ });
|
|
789
|
+
function addRegistryEventHandler(eventHandlers, event, handler) {
|
|
790
|
+
if (!eventHandlers.has(event)) {
|
|
791
|
+
eventHandlers.set(event, /* @__PURE__ */ new Set());
|
|
792
|
+
}
|
|
793
|
+
eventHandlers.get(event).add(handler);
|
|
794
|
+
}
|
|
795
|
+
function removeRegistryEventHandler(eventHandlers, event, handler) {
|
|
796
|
+
const handlers = eventHandlers.get(event);
|
|
797
|
+
if (handlers) {
|
|
798
|
+
handlers.delete(handler);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
function emitRegistryEvent(eventHandlers, event, data) {
|
|
802
|
+
const handlers = eventHandlers.get(event);
|
|
803
|
+
if (!handlers) {
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
handlers.forEach((handler) => {
|
|
807
|
+
try {
|
|
808
|
+
handler(data);
|
|
809
|
+
} catch (error) {
|
|
810
|
+
logger.error("Event handler error", {
|
|
811
|
+
event: String(event),
|
|
812
|
+
error: error instanceof Error ? error.message : String(error),
|
|
813
|
+
...error instanceof Error && error.stack ? { stack: error.stack } : {}
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
// src/tools/registry-prompt.ts
|
|
820
|
+
import { z as z3 } from "zod";
|
|
821
|
+
|
|
669
822
|
// src/langchain/converter.ts
|
|
670
823
|
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
671
824
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
@@ -753,106 +906,179 @@ Examples:`);
|
|
|
753
906
|
return parts.join("\n");
|
|
754
907
|
}
|
|
755
908
|
|
|
756
|
-
// src/tools/registry.ts
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
context;
|
|
777
|
-
constructor(name, options = {}, context = {}) {
|
|
778
|
-
this.name = name;
|
|
779
|
-
this.context = context;
|
|
780
|
-
this.options = {
|
|
781
|
-
level: options.level ?? "info" /* INFO */,
|
|
782
|
-
format: options.format ?? "pretty",
|
|
783
|
-
destination: options.destination ?? process.stdout,
|
|
784
|
-
includeTimestamp: options.includeTimestamp ?? true,
|
|
785
|
-
includeContext: options.includeContext ?? true
|
|
786
|
-
};
|
|
787
|
-
}
|
|
788
|
-
debug(message, data) {
|
|
789
|
-
this.log("debug" /* DEBUG */, message, data);
|
|
790
|
-
}
|
|
791
|
-
info(message, data) {
|
|
792
|
-
this.log("info" /* INFO */, message, data);
|
|
793
|
-
}
|
|
794
|
-
warn(message, data) {
|
|
795
|
-
this.log("warn" /* WARN */, message, data);
|
|
796
|
-
}
|
|
797
|
-
error(message, data) {
|
|
798
|
-
this.log("error" /* ERROR */, message, data);
|
|
799
|
-
}
|
|
800
|
-
isDebugEnabled() {
|
|
801
|
-
return this.isLevelEnabled("debug" /* DEBUG */);
|
|
909
|
+
// src/tools/registry-prompt.ts
|
|
910
|
+
function convertRegistryToolsToLangChain(tools) {
|
|
911
|
+
return toLangChainTools(tools);
|
|
912
|
+
}
|
|
913
|
+
function generateRegistryPrompt(tools, options = {}) {
|
|
914
|
+
const {
|
|
915
|
+
includeExamples = false,
|
|
916
|
+
includeNotes = false,
|
|
917
|
+
includeLimitations = false,
|
|
918
|
+
includeRelations = false,
|
|
919
|
+
groupByCategory = false,
|
|
920
|
+
categories,
|
|
921
|
+
maxExamplesPerTool,
|
|
922
|
+
minimal = false
|
|
923
|
+
} = options;
|
|
924
|
+
let toolsToInclude = tools;
|
|
925
|
+
if (categories && categories.length > 0) {
|
|
926
|
+
toolsToInclude = toolsToInclude.filter(
|
|
927
|
+
(tool) => categories.includes(tool.metadata.category)
|
|
928
|
+
);
|
|
802
929
|
}
|
|
803
|
-
|
|
804
|
-
return
|
|
930
|
+
if (toolsToInclude.length === 0) {
|
|
931
|
+
return "No tools available.";
|
|
805
932
|
}
|
|
806
|
-
|
|
807
|
-
|
|
933
|
+
const lines = ["Available Tools:", ""];
|
|
934
|
+
if (groupByCategory) {
|
|
935
|
+
const toolsByCategory = /* @__PURE__ */ new Map();
|
|
936
|
+
for (const tool of toolsToInclude) {
|
|
937
|
+
const category = tool.metadata.category;
|
|
938
|
+
if (!toolsByCategory.has(category)) {
|
|
939
|
+
toolsByCategory.set(category, []);
|
|
940
|
+
}
|
|
941
|
+
toolsByCategory.get(category).push(tool);
|
|
942
|
+
}
|
|
943
|
+
for (const [category, categoryTools] of toolsByCategory) {
|
|
944
|
+
lines.push(`${category.toUpperCase().replace(/-/g, " ")} TOOLS:`);
|
|
945
|
+
for (const tool of categoryTools) {
|
|
946
|
+
lines.push(...formatToolForPrompt(tool, {
|
|
947
|
+
includeExamples,
|
|
948
|
+
includeNotes,
|
|
949
|
+
includeLimitations,
|
|
950
|
+
includeRelations,
|
|
951
|
+
maxExamplesPerTool,
|
|
952
|
+
minimal
|
|
953
|
+
}));
|
|
954
|
+
}
|
|
955
|
+
lines.push("");
|
|
956
|
+
}
|
|
957
|
+
} else {
|
|
958
|
+
for (const tool of toolsToInclude) {
|
|
959
|
+
lines.push(...formatToolForPrompt(tool, {
|
|
960
|
+
includeExamples,
|
|
961
|
+
includeNotes,
|
|
962
|
+
includeLimitations,
|
|
963
|
+
includeRelations,
|
|
964
|
+
maxExamplesPerTool,
|
|
965
|
+
minimal
|
|
966
|
+
}));
|
|
967
|
+
lines.push("");
|
|
968
|
+
}
|
|
808
969
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
970
|
+
return lines.join("\n").trim();
|
|
971
|
+
}
|
|
972
|
+
function formatToolForPrompt(tool, options) {
|
|
973
|
+
const { metadata } = tool;
|
|
974
|
+
const lines = [];
|
|
975
|
+
if (options.minimal) {
|
|
976
|
+
lines.push(`## ${metadata.name}`);
|
|
977
|
+
let hasContent = false;
|
|
978
|
+
if (options.includeRelations && metadata.relations) {
|
|
979
|
+
const relationLines = formatRelations(metadata.relations);
|
|
980
|
+
if (relationLines.length > 0) {
|
|
981
|
+
lines.push(...relationLines);
|
|
982
|
+
hasContent = true;
|
|
983
|
+
}
|
|
812
984
|
}
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
985
|
+
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
986
|
+
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
987
|
+
const examples = metadata.examples.slice(0, maxExamples);
|
|
988
|
+
for (const example of examples) {
|
|
989
|
+
lines.push(` Example: ${example.description}`);
|
|
990
|
+
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
991
|
+
if (example.explanation) {
|
|
992
|
+
lines.push(` ${example.explanation}`);
|
|
993
|
+
}
|
|
994
|
+
hasContent = true;
|
|
995
|
+
}
|
|
820
996
|
}
|
|
821
|
-
if (
|
|
822
|
-
|
|
997
|
+
if (options.includeNotes && metadata.usageNotes) {
|
|
998
|
+
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
999
|
+
hasContent = true;
|
|
823
1000
|
}
|
|
824
|
-
if (
|
|
825
|
-
|
|
1001
|
+
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1002
|
+
lines.push(" Limitations:");
|
|
1003
|
+
for (const limitation of metadata.limitations) {
|
|
1004
|
+
lines.push(` - ${limitation}`);
|
|
1005
|
+
}
|
|
1006
|
+
hasContent = true;
|
|
826
1007
|
}
|
|
827
|
-
|
|
828
|
-
|
|
1008
|
+
if (!hasContent) {
|
|
1009
|
+
return [];
|
|
1010
|
+
}
|
|
1011
|
+
return lines;
|
|
829
1012
|
}
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
1013
|
+
lines.push(`- ${metadata.name}: ${metadata.description}`);
|
|
1014
|
+
const schemaShape = getSchemaShape(tool.schema);
|
|
1015
|
+
if (schemaShape) {
|
|
1016
|
+
const params = Object.keys(schemaShape);
|
|
1017
|
+
if (params.length > 0) {
|
|
1018
|
+
const paramDescriptions = params.map((param) => {
|
|
1019
|
+
const field = schemaShape[param];
|
|
1020
|
+
const typeName = field._def.typeName;
|
|
1021
|
+
const type = typeName.replace("Zod", "").toLowerCase();
|
|
1022
|
+
return `${param} (${type})`;
|
|
1023
|
+
});
|
|
1024
|
+
lines.push(` Parameters: ${paramDescriptions.join(", ")}`);
|
|
833
1025
|
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
1026
|
+
}
|
|
1027
|
+
if (options.includeRelations && metadata.relations) {
|
|
1028
|
+
const relationLines = formatRelations(metadata.relations);
|
|
1029
|
+
if (relationLines.length > 0) {
|
|
1030
|
+
lines.push(...relationLines);
|
|
837
1031
|
}
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
1032
|
+
}
|
|
1033
|
+
if (options.includeNotes && metadata.usageNotes) {
|
|
1034
|
+
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1035
|
+
}
|
|
1036
|
+
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1037
|
+
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1038
|
+
const examples = metadata.examples.slice(0, maxExamples);
|
|
1039
|
+
for (const example of examples) {
|
|
1040
|
+
lines.push(` Example: ${example.description}`);
|
|
1041
|
+
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1042
|
+
if (example.explanation) {
|
|
1043
|
+
lines.push(` ${example.explanation}`);
|
|
1044
|
+
}
|
|
843
1045
|
}
|
|
844
|
-
|
|
845
|
-
|
|
1046
|
+
}
|
|
1047
|
+
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1048
|
+
lines.push(" Limitations:");
|
|
1049
|
+
for (const limitation of metadata.limitations) {
|
|
1050
|
+
lines.push(` - ${limitation}`);
|
|
846
1051
|
}
|
|
847
|
-
return parts.join(" ");
|
|
848
1052
|
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
1053
|
+
return lines;
|
|
1054
|
+
}
|
|
1055
|
+
function formatRelations(relations) {
|
|
1056
|
+
const lines = [];
|
|
1057
|
+
if (relations.requires && relations.requires.length > 0) {
|
|
1058
|
+
lines.push(` Requires: ${relations.requires.join(", ")}`);
|
|
1059
|
+
}
|
|
1060
|
+
if (relations.suggests && relations.suggests.length > 0) {
|
|
1061
|
+
lines.push(` Suggests: ${relations.suggests.join(", ")}`);
|
|
1062
|
+
}
|
|
1063
|
+
if (relations.conflicts && relations.conflicts.length > 0) {
|
|
1064
|
+
lines.push(` Conflicts: ${relations.conflicts.join(", ")}`);
|
|
1065
|
+
}
|
|
1066
|
+
if (relations.follows && relations.follows.length > 0) {
|
|
1067
|
+
lines.push(` Follows: ${relations.follows.join(", ")}`);
|
|
1068
|
+
}
|
|
1069
|
+
if (relations.precedes && relations.precedes.length > 0) {
|
|
1070
|
+
lines.push(` Precedes: ${relations.precedes.join(", ")}`);
|
|
1071
|
+
}
|
|
1072
|
+
return lines;
|
|
1073
|
+
}
|
|
1074
|
+
function getSchemaShape(schema) {
|
|
1075
|
+
if (schema instanceof z3.ZodObject) {
|
|
1076
|
+
return schema.shape;
|
|
1077
|
+
}
|
|
1078
|
+
return void 0;
|
|
852
1079
|
}
|
|
853
1080
|
|
|
854
1081
|
// src/tools/registry.ts
|
|
855
|
-
var logger = createLogger("agentforge:core:tools:registry", { level: "info" /* INFO */ });
|
|
856
1082
|
var RegistryEvent = /* @__PURE__ */ ((RegistryEvent2) => {
|
|
857
1083
|
RegistryEvent2["TOOL_REGISTERED"] = "tool:registered";
|
|
858
1084
|
RegistryEvent2["TOOL_REMOVED"] = "tool:removed";
|
|
@@ -981,7 +1207,7 @@ var ToolRegistry = class {
|
|
|
981
1207
|
* ```
|
|
982
1208
|
*/
|
|
983
1209
|
getAll() {
|
|
984
|
-
return
|
|
1210
|
+
return getAllRegistryTools(this.tools);
|
|
985
1211
|
}
|
|
986
1212
|
/**
|
|
987
1213
|
* Get tools by category
|
|
@@ -995,7 +1221,7 @@ var ToolRegistry = class {
|
|
|
995
1221
|
* ```
|
|
996
1222
|
*/
|
|
997
1223
|
getByCategory(category) {
|
|
998
|
-
return this.
|
|
1224
|
+
return getRegistryToolsByCategory(this.tools, category);
|
|
999
1225
|
}
|
|
1000
1226
|
/**
|
|
1001
1227
|
* Get tools by tag
|
|
@@ -1009,9 +1235,7 @@ var ToolRegistry = class {
|
|
|
1009
1235
|
* ```
|
|
1010
1236
|
*/
|
|
1011
1237
|
getByTag(tag) {
|
|
1012
|
-
return this.
|
|
1013
|
-
(tool) => tool.metadata.tags?.includes(tag)
|
|
1014
|
-
);
|
|
1238
|
+
return getRegistryToolsByTag(this.tools, tag);
|
|
1015
1239
|
}
|
|
1016
1240
|
/**
|
|
1017
1241
|
* Search tools by name or description
|
|
@@ -1028,13 +1252,7 @@ var ToolRegistry = class {
|
|
|
1028
1252
|
* ```
|
|
1029
1253
|
*/
|
|
1030
1254
|
search(query) {
|
|
1031
|
-
|
|
1032
|
-
return this.getAll().filter((tool) => {
|
|
1033
|
-
const name = tool.metadata.name.toLowerCase();
|
|
1034
|
-
const displayName = tool.metadata.displayName?.toLowerCase() || "";
|
|
1035
|
-
const description = tool.metadata.description.toLowerCase();
|
|
1036
|
-
return name.includes(lowerQuery) || displayName.includes(lowerQuery) || description.includes(lowerQuery);
|
|
1037
|
-
});
|
|
1255
|
+
return searchRegistryTools(this.tools, query);
|
|
1038
1256
|
}
|
|
1039
1257
|
/**
|
|
1040
1258
|
* Register multiple tools at once
|
|
@@ -1117,7 +1335,7 @@ var ToolRegistry = class {
|
|
|
1117
1335
|
* ```
|
|
1118
1336
|
*/
|
|
1119
1337
|
getNames() {
|
|
1120
|
-
return
|
|
1338
|
+
return getRegistryToolNames(this.tools);
|
|
1121
1339
|
}
|
|
1122
1340
|
/**
|
|
1123
1341
|
* Register an event handler
|
|
@@ -1133,10 +1351,7 @@ var ToolRegistry = class {
|
|
|
1133
1351
|
* ```
|
|
1134
1352
|
*/
|
|
1135
1353
|
on(event, handler) {
|
|
1136
|
-
|
|
1137
|
-
this.eventHandlers.set(event, /* @__PURE__ */ new Set());
|
|
1138
|
-
}
|
|
1139
|
-
this.eventHandlers.get(event).add(handler);
|
|
1354
|
+
addRegistryEventHandler(this.eventHandlers, event, handler);
|
|
1140
1355
|
}
|
|
1141
1356
|
/**
|
|
1142
1357
|
* Unregister an event handler
|
|
@@ -1152,10 +1367,7 @@ var ToolRegistry = class {
|
|
|
1152
1367
|
* ```
|
|
1153
1368
|
*/
|
|
1154
1369
|
off(event, handler) {
|
|
1155
|
-
|
|
1156
|
-
if (handlers) {
|
|
1157
|
-
handlers.delete(handler);
|
|
1158
|
-
}
|
|
1370
|
+
removeRegistryEventHandler(this.eventHandlers, event, handler);
|
|
1159
1371
|
}
|
|
1160
1372
|
/**
|
|
1161
1373
|
* Emit an event to all registered handlers
|
|
@@ -1165,20 +1377,7 @@ var ToolRegistry = class {
|
|
|
1165
1377
|
* @private
|
|
1166
1378
|
*/
|
|
1167
1379
|
emit(event, data) {
|
|
1168
|
-
|
|
1169
|
-
if (handlers) {
|
|
1170
|
-
handlers.forEach((handler) => {
|
|
1171
|
-
try {
|
|
1172
|
-
handler(data);
|
|
1173
|
-
} catch (error) {
|
|
1174
|
-
logger.error("Event handler error", {
|
|
1175
|
-
event,
|
|
1176
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1177
|
-
...error instanceof Error && error.stack ? { stack: error.stack } : {}
|
|
1178
|
-
});
|
|
1179
|
-
}
|
|
1180
|
-
});
|
|
1181
|
-
}
|
|
1380
|
+
emitRegistryEvent(this.eventHandlers, event, data);
|
|
1182
1381
|
}
|
|
1183
1382
|
/**
|
|
1184
1383
|
* Convert all registered tools to LangChain format
|
|
@@ -1201,7 +1400,7 @@ var ToolRegistry = class {
|
|
|
1201
1400
|
* ```
|
|
1202
1401
|
*/
|
|
1203
1402
|
toLangChainTools() {
|
|
1204
|
-
return
|
|
1403
|
+
return convertRegistryToolsToLangChain(this.getAll());
|
|
1205
1404
|
}
|
|
1206
1405
|
/**
|
|
1207
1406
|
* Generate a formatted prompt describing all tools
|
|
@@ -1232,184 +1431,7 @@ var ToolRegistry = class {
|
|
|
1232
1431
|
* ```
|
|
1233
1432
|
*/
|
|
1234
1433
|
generatePrompt(options = {}) {
|
|
1235
|
-
|
|
1236
|
-
includeExamples = false,
|
|
1237
|
-
includeNotes = false,
|
|
1238
|
-
includeLimitations = false,
|
|
1239
|
-
includeRelations = false,
|
|
1240
|
-
groupByCategory = false,
|
|
1241
|
-
categories,
|
|
1242
|
-
maxExamplesPerTool,
|
|
1243
|
-
minimal = false
|
|
1244
|
-
} = options;
|
|
1245
|
-
let tools = this.getAll();
|
|
1246
|
-
if (categories && categories.length > 0) {
|
|
1247
|
-
tools = tools.filter((tool) => categories.includes(tool.metadata.category));
|
|
1248
|
-
}
|
|
1249
|
-
if (tools.length === 0) {
|
|
1250
|
-
return "No tools available.";
|
|
1251
|
-
}
|
|
1252
|
-
const lines = ["Available Tools:", ""];
|
|
1253
|
-
if (groupByCategory) {
|
|
1254
|
-
const toolsByCategory = /* @__PURE__ */ new Map();
|
|
1255
|
-
for (const tool of tools) {
|
|
1256
|
-
const category = tool.metadata.category;
|
|
1257
|
-
if (!toolsByCategory.has(category)) {
|
|
1258
|
-
toolsByCategory.set(category, []);
|
|
1259
|
-
}
|
|
1260
|
-
toolsByCategory.get(category).push(tool);
|
|
1261
|
-
}
|
|
1262
|
-
for (const [category, categoryTools] of toolsByCategory) {
|
|
1263
|
-
lines.push(`${category.toUpperCase().replace(/-/g, " ")} TOOLS:`);
|
|
1264
|
-
for (const tool of categoryTools) {
|
|
1265
|
-
lines.push(...this.formatToolForPrompt(tool, {
|
|
1266
|
-
includeExamples,
|
|
1267
|
-
includeNotes,
|
|
1268
|
-
includeLimitations,
|
|
1269
|
-
includeRelations,
|
|
1270
|
-
maxExamplesPerTool,
|
|
1271
|
-
minimal
|
|
1272
|
-
}));
|
|
1273
|
-
}
|
|
1274
|
-
lines.push("");
|
|
1275
|
-
}
|
|
1276
|
-
} else {
|
|
1277
|
-
for (const tool of tools) {
|
|
1278
|
-
lines.push(...this.formatToolForPrompt(tool, {
|
|
1279
|
-
includeExamples,
|
|
1280
|
-
includeNotes,
|
|
1281
|
-
includeLimitations,
|
|
1282
|
-
includeRelations,
|
|
1283
|
-
maxExamplesPerTool,
|
|
1284
|
-
minimal
|
|
1285
|
-
}));
|
|
1286
|
-
lines.push("");
|
|
1287
|
-
}
|
|
1288
|
-
}
|
|
1289
|
-
return lines.join("\n").trim();
|
|
1290
|
-
}
|
|
1291
|
-
/**
|
|
1292
|
-
* Format a single tool for inclusion in a prompt
|
|
1293
|
-
*
|
|
1294
|
-
* @param tool - The tool to format
|
|
1295
|
-
* @param options - Formatting options
|
|
1296
|
-
* @returns Array of formatted lines
|
|
1297
|
-
* @private
|
|
1298
|
-
*/
|
|
1299
|
-
formatToolForPrompt(tool, options) {
|
|
1300
|
-
const { metadata } = tool;
|
|
1301
|
-
const lines = [];
|
|
1302
|
-
if (options.minimal) {
|
|
1303
|
-
lines.push(`## ${metadata.name}`);
|
|
1304
|
-
let hasContent = false;
|
|
1305
|
-
if (options.includeRelations && metadata.relations) {
|
|
1306
|
-
const relationLines = this.formatRelations(metadata.relations);
|
|
1307
|
-
if (relationLines.length > 0) {
|
|
1308
|
-
lines.push(...relationLines);
|
|
1309
|
-
hasContent = true;
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1313
|
-
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1314
|
-
const examples = metadata.examples.slice(0, maxExamples);
|
|
1315
|
-
for (const example of examples) {
|
|
1316
|
-
lines.push(` Example: ${example.description}`);
|
|
1317
|
-
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1318
|
-
if (example.explanation) {
|
|
1319
|
-
lines.push(` ${example.explanation}`);
|
|
1320
|
-
}
|
|
1321
|
-
hasContent = true;
|
|
1322
|
-
}
|
|
1323
|
-
}
|
|
1324
|
-
if (options.includeNotes && metadata.usageNotes) {
|
|
1325
|
-
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1326
|
-
hasContent = true;
|
|
1327
|
-
}
|
|
1328
|
-
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1329
|
-
lines.push(` Limitations:`);
|
|
1330
|
-
for (const limitation of metadata.limitations) {
|
|
1331
|
-
lines.push(` - ${limitation}`);
|
|
1332
|
-
}
|
|
1333
|
-
hasContent = true;
|
|
1334
|
-
}
|
|
1335
|
-
if (!hasContent) {
|
|
1336
|
-
return [];
|
|
1337
|
-
}
|
|
1338
|
-
return lines;
|
|
1339
|
-
}
|
|
1340
|
-
lines.push(`- ${metadata.name}: ${metadata.description}`);
|
|
1341
|
-
const schemaShape = this.getSchemaShape(tool.schema);
|
|
1342
|
-
if (schemaShape) {
|
|
1343
|
-
const params = Object.keys(schemaShape);
|
|
1344
|
-
if (params.length > 0) {
|
|
1345
|
-
const paramDescriptions = params.map((param) => {
|
|
1346
|
-
const field = schemaShape[param];
|
|
1347
|
-
const typeName = field._def.typeName;
|
|
1348
|
-
const type = typeName.replace("Zod", "").toLowerCase();
|
|
1349
|
-
return `${param} (${type})`;
|
|
1350
|
-
});
|
|
1351
|
-
lines.push(` Parameters: ${paramDescriptions.join(", ")}`);
|
|
1352
|
-
}
|
|
1353
|
-
}
|
|
1354
|
-
if (options.includeRelations && metadata.relations) {
|
|
1355
|
-
const relationLines = this.formatRelations(metadata.relations);
|
|
1356
|
-
if (relationLines.length > 0) {
|
|
1357
|
-
lines.push(...relationLines);
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
if (options.includeNotes && metadata.usageNotes) {
|
|
1361
|
-
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1362
|
-
}
|
|
1363
|
-
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1364
|
-
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1365
|
-
const examples = metadata.examples.slice(0, maxExamples);
|
|
1366
|
-
for (const example of examples) {
|
|
1367
|
-
lines.push(` Example: ${example.description}`);
|
|
1368
|
-
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1369
|
-
if (example.explanation) {
|
|
1370
|
-
lines.push(` ${example.explanation}`);
|
|
1371
|
-
}
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1375
|
-
lines.push(` Limitations:`);
|
|
1376
|
-
for (const limitation of metadata.limitations) {
|
|
1377
|
-
lines.push(` - ${limitation}`);
|
|
1378
|
-
}
|
|
1379
|
-
}
|
|
1380
|
-
return lines;
|
|
1381
|
-
}
|
|
1382
|
-
/**
|
|
1383
|
-
* Format tool relations for inclusion in a prompt
|
|
1384
|
-
*
|
|
1385
|
-
* @param relations - The relations to format
|
|
1386
|
-
* @returns Array of formatted lines
|
|
1387
|
-
* @private
|
|
1388
|
-
*/
|
|
1389
|
-
formatRelations(relations) {
|
|
1390
|
-
const lines = [];
|
|
1391
|
-
if (relations.requires && relations.requires.length > 0) {
|
|
1392
|
-
lines.push(` Requires: ${relations.requires.join(", ")}`);
|
|
1393
|
-
}
|
|
1394
|
-
if (relations.suggests && relations.suggests.length > 0) {
|
|
1395
|
-
lines.push(` Suggests: ${relations.suggests.join(", ")}`);
|
|
1396
|
-
}
|
|
1397
|
-
if (relations.conflicts && relations.conflicts.length > 0) {
|
|
1398
|
-
lines.push(` Conflicts: ${relations.conflicts.join(", ")}`);
|
|
1399
|
-
}
|
|
1400
|
-
if (relations.follows && relations.follows.length > 0) {
|
|
1401
|
-
lines.push(` Follows: ${relations.follows.join(", ")}`);
|
|
1402
|
-
}
|
|
1403
|
-
if (relations.precedes && relations.precedes.length > 0) {
|
|
1404
|
-
lines.push(` Precedes: ${relations.precedes.join(", ")}`);
|
|
1405
|
-
}
|
|
1406
|
-
return lines;
|
|
1407
|
-
}
|
|
1408
|
-
getSchemaShape(schema) {
|
|
1409
|
-
if (schema instanceof z3.ZodObject) {
|
|
1410
|
-
return schema.shape;
|
|
1411
|
-
}
|
|
1412
|
-
return void 0;
|
|
1434
|
+
return generateRegistryPrompt(this.getAll(), options);
|
|
1413
1435
|
}
|
|
1414
1436
|
};
|
|
1415
1437
|
|