@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.js
CHANGED
|
@@ -666,96 +666,29 @@ function toolBuilder() {
|
|
|
666
666
|
return new ToolBuilder();
|
|
667
667
|
}
|
|
668
668
|
|
|
669
|
-
// src/
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
function serializeToolResult(result) {
|
|
673
|
-
if (typeof result === "string") {
|
|
674
|
-
return result;
|
|
675
|
-
}
|
|
676
|
-
if (typeof result === "object" && result !== null) {
|
|
677
|
-
return JSON.stringify(result, null, 2);
|
|
678
|
-
}
|
|
679
|
-
return String(result);
|
|
680
|
-
}
|
|
681
|
-
function isJsonSchemaObject(value) {
|
|
682
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
683
|
-
}
|
|
684
|
-
function isJsonSchemaDefinitionMap(value) {
|
|
685
|
-
if (!isJsonSchemaObject(value)) {
|
|
686
|
-
return false;
|
|
687
|
-
}
|
|
688
|
-
return Object.values(value).every(isJsonSchemaObject);
|
|
669
|
+
// src/tools/registry-collection.ts
|
|
670
|
+
function getAllRegistryTools(tools) {
|
|
671
|
+
return Array.from(tools.values());
|
|
689
672
|
}
|
|
690
|
-
function
|
|
691
|
-
|
|
692
|
-
return {};
|
|
693
|
-
}
|
|
694
|
-
const ref = jsonSchema.$ref;
|
|
695
|
-
const definitions = jsonSchema.definitions;
|
|
696
|
-
if (typeof ref !== "string" || !isJsonSchemaDefinitionMap(definitions)) {
|
|
697
|
-
return jsonSchema;
|
|
698
|
-
}
|
|
699
|
-
const refName = ref.replace("#/definitions/", "");
|
|
700
|
-
return definitions[refName] ?? jsonSchema;
|
|
673
|
+
function getRegistryToolNames(tools) {
|
|
674
|
+
return Array.from(tools.keys());
|
|
701
675
|
}
|
|
702
|
-
function
|
|
703
|
-
return
|
|
704
|
-
name: tool.metadata.name,
|
|
705
|
-
description: tool.metadata.description,
|
|
706
|
-
schema: tool.schema,
|
|
707
|
-
func: async (input) => serializeToolResult(await tool.invoke(input))
|
|
708
|
-
});
|
|
676
|
+
function getRegistryToolsByCategory(tools, category) {
|
|
677
|
+
return getAllRegistryTools(tools).filter((tool) => tool.metadata.category === category);
|
|
709
678
|
}
|
|
710
|
-
function
|
|
711
|
-
return tools.
|
|
679
|
+
function getRegistryToolsByTag(tools, tag) {
|
|
680
|
+
return getAllRegistryTools(tools).filter((tool) => tool.metadata.tags?.includes(tag));
|
|
712
681
|
}
|
|
713
|
-
function
|
|
714
|
-
const
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
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);
|
|
718
689
|
});
|
|
719
|
-
return extractToolSchema(jsonSchema);
|
|
720
|
-
}
|
|
721
|
-
function getToolDescription(tool) {
|
|
722
|
-
const { metadata } = tool;
|
|
723
|
-
const parts = [];
|
|
724
|
-
parts.push(`${metadata.name}: ${metadata.description}`);
|
|
725
|
-
if (metadata.displayName) {
|
|
726
|
-
parts.push(`Display Name: ${metadata.displayName}`);
|
|
727
|
-
}
|
|
728
|
-
parts.push(`Category: ${metadata.category}`);
|
|
729
|
-
if (metadata.tags && metadata.tags.length > 0) {
|
|
730
|
-
parts.push(`Tags: ${metadata.tags.join(", ")}`);
|
|
731
|
-
}
|
|
732
|
-
if (metadata.usageNotes) {
|
|
733
|
-
parts.push(`
|
|
734
|
-
Usage Notes: ${metadata.usageNotes}`);
|
|
735
|
-
}
|
|
736
|
-
if (metadata.limitations && metadata.limitations.length > 0) {
|
|
737
|
-
parts.push(`
|
|
738
|
-
Limitations:`);
|
|
739
|
-
metadata.limitations.forEach((limit) => {
|
|
740
|
-
parts.push(` - ${limit}`);
|
|
741
|
-
});
|
|
742
|
-
}
|
|
743
|
-
if (metadata.examples && metadata.examples.length > 0) {
|
|
744
|
-
parts.push(`
|
|
745
|
-
Examples:`);
|
|
746
|
-
metadata.examples.forEach((example, i) => {
|
|
747
|
-
parts.push(` ${i + 1}. ${example.description}`);
|
|
748
|
-
if (example.explanation) {
|
|
749
|
-
parts.push(` ${example.explanation}`);
|
|
750
|
-
}
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
|
-
return parts.join("\n");
|
|
754
690
|
}
|
|
755
691
|
|
|
756
|
-
// src/tools/registry.ts
|
|
757
|
-
import { z as z3 } from "zod";
|
|
758
|
-
|
|
759
692
|
// src/langgraph/observability/logger.ts
|
|
760
693
|
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
761
694
|
LogLevel2["DEBUG"] = "debug";
|
|
@@ -851,31 +784,301 @@ function createLogger(name, options) {
|
|
|
851
784
|
return new LoggerImpl(name, options);
|
|
852
785
|
}
|
|
853
786
|
|
|
854
|
-
// src/tools/registry-
|
|
855
|
-
|
|
856
|
-
|
|
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);
|
|
857
794
|
}
|
|
858
|
-
function
|
|
859
|
-
|
|
795
|
+
function removeRegistryEventHandler(eventHandlers, event, handler) {
|
|
796
|
+
const handlers = eventHandlers.get(event);
|
|
797
|
+
if (handlers) {
|
|
798
|
+
handlers.delete(handler);
|
|
799
|
+
}
|
|
860
800
|
}
|
|
861
|
-
function
|
|
862
|
-
|
|
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
|
+
});
|
|
863
817
|
}
|
|
864
|
-
|
|
865
|
-
|
|
818
|
+
|
|
819
|
+
// src/tools/registry-prompt.ts
|
|
820
|
+
import { z as z3 } from "zod";
|
|
821
|
+
|
|
822
|
+
// src/langchain/converter.ts
|
|
823
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
824
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
825
|
+
function serializeToolResult(result) {
|
|
826
|
+
if (typeof result === "string") {
|
|
827
|
+
return result;
|
|
828
|
+
}
|
|
829
|
+
if (typeof result === "object" && result !== null) {
|
|
830
|
+
return JSON.stringify(result, null, 2);
|
|
831
|
+
}
|
|
832
|
+
return String(result);
|
|
866
833
|
}
|
|
867
|
-
function
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
834
|
+
function isJsonSchemaObject(value) {
|
|
835
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
836
|
+
}
|
|
837
|
+
function isJsonSchemaDefinitionMap(value) {
|
|
838
|
+
if (!isJsonSchemaObject(value)) {
|
|
839
|
+
return false;
|
|
840
|
+
}
|
|
841
|
+
return Object.values(value).every(isJsonSchemaObject);
|
|
842
|
+
}
|
|
843
|
+
function extractToolSchema(jsonSchema) {
|
|
844
|
+
if (!isJsonSchemaObject(jsonSchema)) {
|
|
845
|
+
return {};
|
|
846
|
+
}
|
|
847
|
+
const ref = jsonSchema.$ref;
|
|
848
|
+
const definitions = jsonSchema.definitions;
|
|
849
|
+
if (typeof ref !== "string" || !isJsonSchemaDefinitionMap(definitions)) {
|
|
850
|
+
return jsonSchema;
|
|
851
|
+
}
|
|
852
|
+
const refName = ref.replace("#/definitions/", "");
|
|
853
|
+
return definitions[refName] ?? jsonSchema;
|
|
854
|
+
}
|
|
855
|
+
function toLangChainTool(tool) {
|
|
856
|
+
return new DynamicStructuredTool({
|
|
857
|
+
name: tool.metadata.name,
|
|
858
|
+
description: tool.metadata.description,
|
|
859
|
+
schema: tool.schema,
|
|
860
|
+
func: async (input) => serializeToolResult(await tool.invoke(input))
|
|
874
861
|
});
|
|
875
862
|
}
|
|
863
|
+
function toLangChainTools(tools) {
|
|
864
|
+
return tools.map(toLangChainTool);
|
|
865
|
+
}
|
|
866
|
+
function getToolJsonSchema(tool) {
|
|
867
|
+
const jsonSchema = zodToJsonSchema(tool.schema, {
|
|
868
|
+
name: tool.metadata.name,
|
|
869
|
+
$refStrategy: "none"
|
|
870
|
+
// Don't use $ref for nested schemas
|
|
871
|
+
});
|
|
872
|
+
return extractToolSchema(jsonSchema);
|
|
873
|
+
}
|
|
874
|
+
function getToolDescription(tool) {
|
|
875
|
+
const { metadata } = tool;
|
|
876
|
+
const parts = [];
|
|
877
|
+
parts.push(`${metadata.name}: ${metadata.description}`);
|
|
878
|
+
if (metadata.displayName) {
|
|
879
|
+
parts.push(`Display Name: ${metadata.displayName}`);
|
|
880
|
+
}
|
|
881
|
+
parts.push(`Category: ${metadata.category}`);
|
|
882
|
+
if (metadata.tags && metadata.tags.length > 0) {
|
|
883
|
+
parts.push(`Tags: ${metadata.tags.join(", ")}`);
|
|
884
|
+
}
|
|
885
|
+
if (metadata.usageNotes) {
|
|
886
|
+
parts.push(`
|
|
887
|
+
Usage Notes: ${metadata.usageNotes}`);
|
|
888
|
+
}
|
|
889
|
+
if (metadata.limitations && metadata.limitations.length > 0) {
|
|
890
|
+
parts.push(`
|
|
891
|
+
Limitations:`);
|
|
892
|
+
metadata.limitations.forEach((limit) => {
|
|
893
|
+
parts.push(` - ${limit}`);
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
if (metadata.examples && metadata.examples.length > 0) {
|
|
897
|
+
parts.push(`
|
|
898
|
+
Examples:`);
|
|
899
|
+
metadata.examples.forEach((example, i) => {
|
|
900
|
+
parts.push(` ${i + 1}. ${example.description}`);
|
|
901
|
+
if (example.explanation) {
|
|
902
|
+
parts.push(` ${example.explanation}`);
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
return parts.join("\n");
|
|
907
|
+
}
|
|
908
|
+
|
|
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
|
+
);
|
|
929
|
+
}
|
|
930
|
+
if (toolsToInclude.length === 0) {
|
|
931
|
+
return "No tools available.";
|
|
932
|
+
}
|
|
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
|
+
}
|
|
969
|
+
}
|
|
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
|
+
}
|
|
984
|
+
}
|
|
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
|
+
}
|
|
996
|
+
}
|
|
997
|
+
if (options.includeNotes && metadata.usageNotes) {
|
|
998
|
+
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
999
|
+
hasContent = true;
|
|
1000
|
+
}
|
|
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;
|
|
1007
|
+
}
|
|
1008
|
+
if (!hasContent) {
|
|
1009
|
+
return [];
|
|
1010
|
+
}
|
|
1011
|
+
return lines;
|
|
1012
|
+
}
|
|
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(", ")}`);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
if (options.includeRelations && metadata.relations) {
|
|
1028
|
+
const relationLines = formatRelations(metadata.relations);
|
|
1029
|
+
if (relationLines.length > 0) {
|
|
1030
|
+
lines.push(...relationLines);
|
|
1031
|
+
}
|
|
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
|
+
}
|
|
1045
|
+
}
|
|
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}`);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
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;
|
|
1079
|
+
}
|
|
876
1080
|
|
|
877
1081
|
// src/tools/registry.ts
|
|
878
|
-
var logger = createLogger("agentforge:core:tools:registry", { level: "info" /* INFO */ });
|
|
879
1082
|
var RegistryEvent = /* @__PURE__ */ ((RegistryEvent2) => {
|
|
880
1083
|
RegistryEvent2["TOOL_REGISTERED"] = "tool:registered";
|
|
881
1084
|
RegistryEvent2["TOOL_REMOVED"] = "tool:removed";
|
|
@@ -1148,10 +1351,7 @@ var ToolRegistry = class {
|
|
|
1148
1351
|
* ```
|
|
1149
1352
|
*/
|
|
1150
1353
|
on(event, handler) {
|
|
1151
|
-
|
|
1152
|
-
this.eventHandlers.set(event, /* @__PURE__ */ new Set());
|
|
1153
|
-
}
|
|
1154
|
-
this.eventHandlers.get(event).add(handler);
|
|
1354
|
+
addRegistryEventHandler(this.eventHandlers, event, handler);
|
|
1155
1355
|
}
|
|
1156
1356
|
/**
|
|
1157
1357
|
* Unregister an event handler
|
|
@@ -1167,10 +1367,7 @@ var ToolRegistry = class {
|
|
|
1167
1367
|
* ```
|
|
1168
1368
|
*/
|
|
1169
1369
|
off(event, handler) {
|
|
1170
|
-
|
|
1171
|
-
if (handlers) {
|
|
1172
|
-
handlers.delete(handler);
|
|
1173
|
-
}
|
|
1370
|
+
removeRegistryEventHandler(this.eventHandlers, event, handler);
|
|
1174
1371
|
}
|
|
1175
1372
|
/**
|
|
1176
1373
|
* Emit an event to all registered handlers
|
|
@@ -1180,20 +1377,7 @@ var ToolRegistry = class {
|
|
|
1180
1377
|
* @private
|
|
1181
1378
|
*/
|
|
1182
1379
|
emit(event, data) {
|
|
1183
|
-
|
|
1184
|
-
if (handlers) {
|
|
1185
|
-
handlers.forEach((handler) => {
|
|
1186
|
-
try {
|
|
1187
|
-
handler(data);
|
|
1188
|
-
} catch (error) {
|
|
1189
|
-
logger.error("Event handler error", {
|
|
1190
|
-
event,
|
|
1191
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1192
|
-
...error instanceof Error && error.stack ? { stack: error.stack } : {}
|
|
1193
|
-
});
|
|
1194
|
-
}
|
|
1195
|
-
});
|
|
1196
|
-
}
|
|
1380
|
+
emitRegistryEvent(this.eventHandlers, event, data);
|
|
1197
1381
|
}
|
|
1198
1382
|
/**
|
|
1199
1383
|
* Convert all registered tools to LangChain format
|
|
@@ -1216,7 +1400,7 @@ var ToolRegistry = class {
|
|
|
1216
1400
|
* ```
|
|
1217
1401
|
*/
|
|
1218
1402
|
toLangChainTools() {
|
|
1219
|
-
return
|
|
1403
|
+
return convertRegistryToolsToLangChain(this.getAll());
|
|
1220
1404
|
}
|
|
1221
1405
|
/**
|
|
1222
1406
|
* Generate a formatted prompt describing all tools
|
|
@@ -1247,184 +1431,7 @@ var ToolRegistry = class {
|
|
|
1247
1431
|
* ```
|
|
1248
1432
|
*/
|
|
1249
1433
|
generatePrompt(options = {}) {
|
|
1250
|
-
|
|
1251
|
-
includeExamples = false,
|
|
1252
|
-
includeNotes = false,
|
|
1253
|
-
includeLimitations = false,
|
|
1254
|
-
includeRelations = false,
|
|
1255
|
-
groupByCategory = false,
|
|
1256
|
-
categories,
|
|
1257
|
-
maxExamplesPerTool,
|
|
1258
|
-
minimal = false
|
|
1259
|
-
} = options;
|
|
1260
|
-
let tools = this.getAll();
|
|
1261
|
-
if (categories && categories.length > 0) {
|
|
1262
|
-
tools = tools.filter((tool) => categories.includes(tool.metadata.category));
|
|
1263
|
-
}
|
|
1264
|
-
if (tools.length === 0) {
|
|
1265
|
-
return "No tools available.";
|
|
1266
|
-
}
|
|
1267
|
-
const lines = ["Available Tools:", ""];
|
|
1268
|
-
if (groupByCategory) {
|
|
1269
|
-
const toolsByCategory = /* @__PURE__ */ new Map();
|
|
1270
|
-
for (const tool of tools) {
|
|
1271
|
-
const category = tool.metadata.category;
|
|
1272
|
-
if (!toolsByCategory.has(category)) {
|
|
1273
|
-
toolsByCategory.set(category, []);
|
|
1274
|
-
}
|
|
1275
|
-
toolsByCategory.get(category).push(tool);
|
|
1276
|
-
}
|
|
1277
|
-
for (const [category, categoryTools] of toolsByCategory) {
|
|
1278
|
-
lines.push(`${category.toUpperCase().replace(/-/g, " ")} TOOLS:`);
|
|
1279
|
-
for (const tool of categoryTools) {
|
|
1280
|
-
lines.push(...this.formatToolForPrompt(tool, {
|
|
1281
|
-
includeExamples,
|
|
1282
|
-
includeNotes,
|
|
1283
|
-
includeLimitations,
|
|
1284
|
-
includeRelations,
|
|
1285
|
-
maxExamplesPerTool,
|
|
1286
|
-
minimal
|
|
1287
|
-
}));
|
|
1288
|
-
}
|
|
1289
|
-
lines.push("");
|
|
1290
|
-
}
|
|
1291
|
-
} else {
|
|
1292
|
-
for (const tool of tools) {
|
|
1293
|
-
lines.push(...this.formatToolForPrompt(tool, {
|
|
1294
|
-
includeExamples,
|
|
1295
|
-
includeNotes,
|
|
1296
|
-
includeLimitations,
|
|
1297
|
-
includeRelations,
|
|
1298
|
-
maxExamplesPerTool,
|
|
1299
|
-
minimal
|
|
1300
|
-
}));
|
|
1301
|
-
lines.push("");
|
|
1302
|
-
}
|
|
1303
|
-
}
|
|
1304
|
-
return lines.join("\n").trim();
|
|
1305
|
-
}
|
|
1306
|
-
/**
|
|
1307
|
-
* Format a single tool for inclusion in a prompt
|
|
1308
|
-
*
|
|
1309
|
-
* @param tool - The tool to format
|
|
1310
|
-
* @param options - Formatting options
|
|
1311
|
-
* @returns Array of formatted lines
|
|
1312
|
-
* @private
|
|
1313
|
-
*/
|
|
1314
|
-
formatToolForPrompt(tool, options) {
|
|
1315
|
-
const { metadata } = tool;
|
|
1316
|
-
const lines = [];
|
|
1317
|
-
if (options.minimal) {
|
|
1318
|
-
lines.push(`## ${metadata.name}`);
|
|
1319
|
-
let hasContent = false;
|
|
1320
|
-
if (options.includeRelations && metadata.relations) {
|
|
1321
|
-
const relationLines = this.formatRelations(metadata.relations);
|
|
1322
|
-
if (relationLines.length > 0) {
|
|
1323
|
-
lines.push(...relationLines);
|
|
1324
|
-
hasContent = true;
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1328
|
-
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1329
|
-
const examples = metadata.examples.slice(0, maxExamples);
|
|
1330
|
-
for (const example of examples) {
|
|
1331
|
-
lines.push(` Example: ${example.description}`);
|
|
1332
|
-
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1333
|
-
if (example.explanation) {
|
|
1334
|
-
lines.push(` ${example.explanation}`);
|
|
1335
|
-
}
|
|
1336
|
-
hasContent = true;
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
if (options.includeNotes && metadata.usageNotes) {
|
|
1340
|
-
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1341
|
-
hasContent = true;
|
|
1342
|
-
}
|
|
1343
|
-
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1344
|
-
lines.push(` Limitations:`);
|
|
1345
|
-
for (const limitation of metadata.limitations) {
|
|
1346
|
-
lines.push(` - ${limitation}`);
|
|
1347
|
-
}
|
|
1348
|
-
hasContent = true;
|
|
1349
|
-
}
|
|
1350
|
-
if (!hasContent) {
|
|
1351
|
-
return [];
|
|
1352
|
-
}
|
|
1353
|
-
return lines;
|
|
1354
|
-
}
|
|
1355
|
-
lines.push(`- ${metadata.name}: ${metadata.description}`);
|
|
1356
|
-
const schemaShape = this.getSchemaShape(tool.schema);
|
|
1357
|
-
if (schemaShape) {
|
|
1358
|
-
const params = Object.keys(schemaShape);
|
|
1359
|
-
if (params.length > 0) {
|
|
1360
|
-
const paramDescriptions = params.map((param) => {
|
|
1361
|
-
const field = schemaShape[param];
|
|
1362
|
-
const typeName = field._def.typeName;
|
|
1363
|
-
const type = typeName.replace("Zod", "").toLowerCase();
|
|
1364
|
-
return `${param} (${type})`;
|
|
1365
|
-
});
|
|
1366
|
-
lines.push(` Parameters: ${paramDescriptions.join(", ")}`);
|
|
1367
|
-
}
|
|
1368
|
-
}
|
|
1369
|
-
if (options.includeRelations && metadata.relations) {
|
|
1370
|
-
const relationLines = this.formatRelations(metadata.relations);
|
|
1371
|
-
if (relationLines.length > 0) {
|
|
1372
|
-
lines.push(...relationLines);
|
|
1373
|
-
}
|
|
1374
|
-
}
|
|
1375
|
-
if (options.includeNotes && metadata.usageNotes) {
|
|
1376
|
-
lines.push(` Notes: ${metadata.usageNotes}`);
|
|
1377
|
-
}
|
|
1378
|
-
if (options.includeExamples && metadata.examples && metadata.examples.length > 0) {
|
|
1379
|
-
const maxExamples = options.maxExamplesPerTool || metadata.examples.length;
|
|
1380
|
-
const examples = metadata.examples.slice(0, maxExamples);
|
|
1381
|
-
for (const example of examples) {
|
|
1382
|
-
lines.push(` Example: ${example.description}`);
|
|
1383
|
-
lines.push(` Input: ${JSON.stringify(example.input)}`);
|
|
1384
|
-
if (example.explanation) {
|
|
1385
|
-
lines.push(` ${example.explanation}`);
|
|
1386
|
-
}
|
|
1387
|
-
}
|
|
1388
|
-
}
|
|
1389
|
-
if (options.includeLimitations && metadata.limitations && metadata.limitations.length > 0) {
|
|
1390
|
-
lines.push(` Limitations:`);
|
|
1391
|
-
for (const limitation of metadata.limitations) {
|
|
1392
|
-
lines.push(` - ${limitation}`);
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
return lines;
|
|
1396
|
-
}
|
|
1397
|
-
/**
|
|
1398
|
-
* Format tool relations for inclusion in a prompt
|
|
1399
|
-
*
|
|
1400
|
-
* @param relations - The relations to format
|
|
1401
|
-
* @returns Array of formatted lines
|
|
1402
|
-
* @private
|
|
1403
|
-
*/
|
|
1404
|
-
formatRelations(relations) {
|
|
1405
|
-
const lines = [];
|
|
1406
|
-
if (relations.requires && relations.requires.length > 0) {
|
|
1407
|
-
lines.push(` Requires: ${relations.requires.join(", ")}`);
|
|
1408
|
-
}
|
|
1409
|
-
if (relations.suggests && relations.suggests.length > 0) {
|
|
1410
|
-
lines.push(` Suggests: ${relations.suggests.join(", ")}`);
|
|
1411
|
-
}
|
|
1412
|
-
if (relations.conflicts && relations.conflicts.length > 0) {
|
|
1413
|
-
lines.push(` Conflicts: ${relations.conflicts.join(", ")}`);
|
|
1414
|
-
}
|
|
1415
|
-
if (relations.follows && relations.follows.length > 0) {
|
|
1416
|
-
lines.push(` Follows: ${relations.follows.join(", ")}`);
|
|
1417
|
-
}
|
|
1418
|
-
if (relations.precedes && relations.precedes.length > 0) {
|
|
1419
|
-
lines.push(` Precedes: ${relations.precedes.join(", ")}`);
|
|
1420
|
-
}
|
|
1421
|
-
return lines;
|
|
1422
|
-
}
|
|
1423
|
-
getSchemaShape(schema) {
|
|
1424
|
-
if (schema instanceof z3.ZodObject) {
|
|
1425
|
-
return schema.shape;
|
|
1426
|
-
}
|
|
1427
|
-
return void 0;
|
|
1434
|
+
return generateRegistryPrompt(this.getAll(), options);
|
|
1428
1435
|
}
|
|
1429
1436
|
};
|
|
1430
1437
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentforge/core",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.13",
|
|
4
4
|
"description": "Production-ready TypeScript agent framework built on LangGraph with orchestration, middleware, and typed abstractions.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|