@agentforge/core 0.16.35 → 0.16.37

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.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { z, ZodTypeAny, output, ZodType, ZodTypeDef } from 'zod';
2
+ import * as _langchain_core_tools from '@langchain/core/tools';
2
3
  import { DynamicStructuredTool } from '@langchain/core/tools';
3
4
  import * as _langchain_langgraph from '@langchain/langgraph';
4
5
  import { AnnotationRoot, BaseChannel, StateDefinition, UpdateType, StateGraph, END, MemorySaver, BaseCheckpointSaver, CheckpointTuple } from '@langchain/langgraph';
@@ -919,233 +920,44 @@ declare function validateTool(tool: Tool): {
919
920
  errors: string[];
920
921
  };
921
922
 
922
- /**
923
- * Tool Builder API
924
- *
925
- * Fluent interface for creating tools with automatic validation.
926
- *
927
- * @example
928
- * ```ts
929
- * const tool = createToolBuilder()
930
- * .name('read-file')
931
- * .description('Read a file from the file system')
932
- * .category(ToolCategory.FILE_SYSTEM)
933
- * .tags(['file', 'read', 'io'])
934
- * .schema(z.object({
935
- * path: z.string().describe('Path to the file')
936
- * }))
937
- * .implement(async ({ path }) => {
938
- * // Implementation
939
- * })
940
- * .build();
941
- * ```
942
- */
943
-
944
923
  type ToolInvoke<TOutput> = (this: unknown, input: unknown) => Promise<TOutput>;
945
- /**
946
- * Builder for creating tools with a fluent API
947
- *
948
- * This provides a more ergonomic way to create tools compared to
949
- * manually constructing the metadata object.
950
- */
924
+ type SafeToolResult<T> = {
925
+ success: boolean;
926
+ data?: T;
927
+ error?: string;
928
+ };
929
+
951
930
  declare class ToolBuilder<TInput = unknown, TOutput = unknown> {
952
931
  private metadata;
953
932
  private _schema?;
954
933
  private _invoke?;
955
934
  constructor(metadata?: Partial<ToolMetadata>, _schema?: z.ZodSchema<TInput> | undefined, _invoke?: ToolInvoke<TOutput> | undefined);
956
- /**
957
- * Set the tool name (required)
958
- *
959
- * @param name - Tool name in kebab-case (e.g., 'read-file')
960
- */
961
935
  name(name: string): this;
962
- /**
963
- * Set the tool description (required)
964
- *
965
- * @param description - Clear description of what the tool does
966
- */
967
936
  description(description: string): this;
968
- /**
969
- * Set the tool category (required)
970
- *
971
- * @param category - Tool category for organization
972
- */
973
937
  category(category: ToolCategory): this;
974
- /**
975
- * Set the display name (optional)
976
- *
977
- * @param displayName - Human-friendly name for UI display
978
- */
979
938
  displayName(displayName: string): this;
980
- /**
981
- * Set tags for searchability (optional)
982
- *
983
- * @param tags - Array of tags for categorization and search
984
- */
985
939
  tags(tags: string[]): this;
986
- /**
987
- * Add a single tag (optional)
988
- *
989
- * @param tag - Tag to add
990
- */
991
940
  tag(tag: string): this;
992
- /**
993
- * Add an example (optional)
994
- *
995
- * @param example - Usage example for the tool
996
- */
997
941
  example(example: ToolExample): this;
998
- /**
999
- * Set usage notes (optional)
1000
- *
1001
- * @param notes - Important usage information
1002
- */
1003
942
  usageNotes(notes: string): this;
1004
- /**
1005
- * Set limitations (optional)
1006
- *
1007
- * @param limitations - Array of known limitations
1008
- */
1009
943
  limitations(limitations: string[]): this;
1010
- /**
1011
- * Add a single limitation (optional)
1012
- *
1013
- * @param limitation - Limitation to add
1014
- */
1015
944
  limitation(limitation: string): this;
1016
- /**
1017
- * Set version (optional)
1018
- *
1019
- * @param version - Semantic version string
1020
- */
1021
945
  version(version: string): this;
1022
- /**
1023
- * Set author (optional)
1024
- *
1025
- * @param author - Tool author name
1026
- */
1027
946
  author(author: string): this;
1028
- /**
1029
- * Set tools that must be called before this tool (optional)
1030
- *
1031
- * @param tools - Array of tool names that are required
1032
- * @example
1033
- * ```ts
1034
- * .requires(['view-file', 'search-codebase'])
1035
- * ```
1036
- */
1037
947
  requires(tools: string[]): this;
1038
- /**
1039
- * Set tools that work well with this tool (optional)
1040
- *
1041
- * @param tools - Array of tool names that are suggested
1042
- * @example
1043
- * ```ts
1044
- * .suggests(['run-tests', 'format-code'])
1045
- * ```
1046
- */
1047
948
  suggests(tools: string[]): this;
1048
- /**
1049
- * Set tools that conflict with this tool (optional)
1050
- *
1051
- * @param tools - Array of tool names that conflict
1052
- * @example
1053
- * ```ts
1054
- * .conflicts(['delete-file'])
1055
- * ```
1056
- */
1057
949
  conflicts(tools: string[]): this;
1058
- /**
1059
- * Set tools this typically follows in a workflow (optional)
1060
- *
1061
- * @param tools - Array of tool names this follows
1062
- * @example
1063
- * ```ts
1064
- * .follows(['search-codebase', 'view-file'])
1065
- * ```
1066
- */
1067
950
  follows(tools: string[]): this;
1068
- /**
1069
- * Set tools this typically precedes in a workflow (optional)
1070
- *
1071
- * @param tools - Array of tool names this precedes
1072
- * @example
1073
- * ```ts
1074
- * .precedes(['run-tests'])
1075
- * ```
1076
- */
1077
951
  precedes(tools: string[]): this;
1078
- /**
1079
- * Set the input schema (required)
1080
- *
1081
- * All fields MUST have .describe() for LLM understanding!
1082
- *
1083
- * @param schema - Zod schema for input validation
1084
- */
1085
952
  schema<T>(schema: z.ZodSchema<T>): ToolBuilder<T, TOutput>;
1086
- /**
1087
- * Set the implementation function (required)
1088
- *
1089
- * @param invoke - Async function that implements the tool
1090
- */
1091
953
  implement<T>(invoke: (input: TInput) => Promise<T>): ToolBuilder<TInput, T>;
1092
- /**
1093
- * Set the implementation function with automatic error handling
1094
- *
1095
- * Wraps the implementation in a try-catch block and returns a standardized
1096
- * result object with success/error information.
1097
- *
1098
- * @param invoke - Async function that implements the tool
1099
- * @returns ToolBuilder with safe result type { success: boolean; data?: T; error?: string }
1100
- *
1101
- * @example
1102
- * ```ts
1103
- * const tool = toolBuilder()
1104
- * .name('read-file')
1105
- * .schema(z.object({ path: z.string() }))
1106
- * .implementSafe(async ({ path }) => {
1107
- * return await fs.readFile(path, 'utf-8');
1108
- * })
1109
- * .build();
1110
- *
1111
- * // Result will be: { success: true, data: "file content" }
1112
- * // Or on error: { success: false, error: "ENOENT: no such file..." }
1113
- * ```
1114
- */
1115
- implementSafe<T>(invoke: (input: TInput) => Promise<T>): ToolBuilder<TInput, {
1116
- success: boolean;
1117
- data?: T;
1118
- error?: string;
1119
- }>;
1120
- /**
1121
- * Build the tool with validation
1122
- *
1123
- * Validates:
1124
- * - All required fields are present
1125
- * - Metadata is valid
1126
- * - Schema has descriptions on all fields
1127
- *
1128
- * @returns The validated tool
1129
- * @throws {Error} If validation fails
1130
- */
954
+ implementSafe<T>(invoke: (input: TInput) => Promise<T>): ToolBuilder<TInput, SafeToolResult<T>>;
1131
955
  build(): Tool<TInput, TOutput>;
1132
956
  }
1133
- /**
1134
- * Create a new tool builder
1135
- *
1136
- * @example
1137
- * ```ts
1138
- * const tool = toolBuilder()
1139
- * .name('my-tool')
1140
- * .description('Does something useful')
1141
- * .category(ToolCategory.UTILITY)
1142
- * .schema(z.object({ input: z.string().describe('Input') }))
1143
- * .implement(async ({ input }) => input)
1144
- * .build();
1145
- * ```
1146
- */
1147
957
  declare function toolBuilder(): ToolBuilder;
1148
958
 
959
+ type RegistryTool = Tool<unknown, unknown>;
960
+
1149
961
  type RegistryEventHandler<TData = unknown> = (data: TData) => void;
1150
962
 
1151
963
  interface RegistryPromptOptions {
@@ -1159,355 +971,41 @@ interface RegistryPromptOptions {
1159
971
  minimal?: boolean;
1160
972
  }
1161
973
 
1162
- /**
1163
- * Tool Registry
1164
- *
1165
- * Central registry for managing and querying tools.
1166
- * Provides CRUD operations, querying, and event notifications.
1167
- *
1168
- * @example
1169
- * ```ts
1170
- * const registry = new ToolRegistry();
1171
- *
1172
- * registry.register(readFileTool);
1173
- * registry.register(writeFileTool);
1174
- *
1175
- * const fileTool = registry.get('read-file');
1176
- * const fileTools = registry.getByCategory(ToolCategory.FILE_SYSTEM);
1177
- * ```
1178
- */
1179
-
1180
- /**
1181
- * Registry events
1182
- */
1183
974
  declare enum RegistryEvent {
1184
975
  TOOL_REGISTERED = "tool:registered",
1185
976
  TOOL_REMOVED = "tool:removed",
1186
977
  TOOL_UPDATED = "tool:updated",
1187
978
  REGISTRY_CLEARED = "registry:cleared"
1188
979
  }
1189
- /**
1190
- * Event handler type
1191
- */
1192
980
  type EventHandler = RegistryEventHandler;
1193
- type RegisterManyTool = Tool<never, unknown>;
1194
- type RegistryTool = Tool<unknown, unknown>;
1195
- /**
1196
- * Options for generating tool prompts
1197
- */
1198
981
  interface PromptOptions extends RegistryPromptOptions {
1199
- /** Include usage examples in the prompt */
1200
- includeExamples?: boolean;
1201
- /** Include usage notes in the prompt */
1202
- includeNotes?: boolean;
1203
- /** Include limitations in the prompt */
1204
- includeLimitations?: boolean;
1205
- /** Include tool relations in the prompt */
1206
- includeRelations?: boolean;
1207
- /** Group tools by category */
1208
- groupByCategory?: boolean;
1209
- /** Filter by specific categories */
1210
- categories?: ToolCategory[];
1211
- /** Maximum number of examples to include per tool */
1212
- maxExamplesPerTool?: number;
1213
- /**
1214
- * Minimal mode - only supplementary context, not full definitions
1215
- *
1216
- * Use this when tool definitions are sent via API (OpenAI, Anthropic, etc.)
1217
- *
1218
- * When true:
1219
- * - Excludes basic tool name/description/parameters
1220
- * - Includes only relations, examples, notes, limitations
1221
- * - Tool names are used as headers for matching
1222
- *
1223
- * When false (default):
1224
- * - Includes full tool definitions (current behavior)
1225
- * - Backward compatible
1226
- */
1227
- minimal?: boolean;
1228
982
  }
1229
- /**
1230
- * Tool Registry - Central registry for managing tools
1231
- *
1232
- * Features:
1233
- * - CRUD operations (register, get, remove, update)
1234
- * - Query operations (by category, tag, search)
1235
- * - Bulk operations (registerMany, clear)
1236
- * - Event system for observability
1237
- *
1238
- * @example
1239
- * ```ts
1240
- * const registry = new ToolRegistry();
1241
- *
1242
- * // Register tools
1243
- * registry.register(myTool);
1244
- * registry.registerMany([tool1, tool2, tool3]);
1245
- *
1246
- * // Query tools
1247
- * const tool = registry.get('my-tool');
1248
- * const fileTools = registry.getByCategory(ToolCategory.FILE_SYSTEM);
1249
- * const searchResults = registry.search('file');
1250
- *
1251
- * // Listen to events
1252
- * registry.on(RegistryEvent.TOOL_REGISTERED, (tool) => {
1253
- * console.log('Tool registered:', tool.metadata.name);
1254
- * });
1255
- * ```
1256
- */
983
+
1257
984
  declare class ToolRegistry {
1258
985
  private tools;
1259
986
  private eventHandlers;
1260
987
  private readonly mutationEvents;
1261
988
  private readonly emitMutation;
1262
- /**
1263
- * Register a tool in the registry
1264
- *
1265
- * @param tool - The tool to register
1266
- * @throws Error if a tool with the same name already exists
1267
- *
1268
- * @example
1269
- * ```ts
1270
- * registry.register(readFileTool);
1271
- * ```
1272
- */
989
+ private readonly mutations;
990
+ private readonly queries;
991
+ constructor();
1273
992
  register<TInput, TOutput>(tool: Tool<TInput, TOutput>): void;
1274
- /**
1275
- * Get a tool by name
1276
- *
1277
- * @param name - The tool name
1278
- * @returns The tool, or undefined if not found
1279
- *
1280
- * @example
1281
- * ```ts
1282
- * const tool = registry.get('read-file');
1283
- * if (tool) {
1284
- * const result = await tool.execute({ path: './file.txt' });
1285
- * }
1286
- * ```
1287
- */
1288
993
  get(name: string): RegistryTool | undefined;
1289
- /**
1290
- * Check if a tool exists in the registry
1291
- *
1292
- * @param name - The tool name
1293
- * @returns True if the tool exists
1294
- *
1295
- * @example
1296
- * ```ts
1297
- * if (registry.has('read-file')) {
1298
- * console.log('Tool exists!');
1299
- * }
1300
- * ```
1301
- */
1302
994
  has(name: string): boolean;
1303
- /**
1304
- * Remove a tool from the registry
1305
- *
1306
- * @param name - The tool name
1307
- * @returns True if the tool was removed, false if it didn't exist
1308
- *
1309
- * @example
1310
- * ```ts
1311
- * const removed = registry.remove('read-file');
1312
- * console.log(removed ? 'Removed' : 'Not found');
1313
- * ```
1314
- */
1315
995
  remove(name: string): boolean;
1316
- /**
1317
- * Update an existing tool
1318
- *
1319
- * @param name - The tool name
1320
- * @param tool - The new tool definition
1321
- * @returns True if updated, false if the tool didn't exist
1322
- * @throws Error if the tool's metadata.name doesn't match the name parameter
1323
- *
1324
- * @example
1325
- * ```ts
1326
- * const updated = registry.update('read-file', newReadFileTool);
1327
- * ```
1328
- */
1329
996
  update<TInput, TOutput>(name: string, tool: Tool<TInput, TOutput>): boolean;
1330
- /**
1331
- * Get all registered tools
1332
- *
1333
- * @returns Array of all tools
1334
- *
1335
- * @example
1336
- * ```ts
1337
- * const allTools = registry.getAll();
1338
- * console.log(`Total tools: ${allTools.length}`);
1339
- * ```
1340
- */
1341
997
  getAll(): RegistryTool[];
1342
- /**
1343
- * Get tools by category
1344
- *
1345
- * @param category - The tool category
1346
- * @returns Array of tools in the category
1347
- *
1348
- * @example
1349
- * ```ts
1350
- * const fileTools = registry.getByCategory(ToolCategory.FILE_SYSTEM);
1351
- * ```
1352
- */
1353
998
  getByCategory(category: ToolCategory): RegistryTool[];
1354
- /**
1355
- * Get tools by tag
1356
- *
1357
- * @param tag - The tag to search for
1358
- * @returns Array of tools with the tag
1359
- *
1360
- * @example
1361
- * ```ts
1362
- * const fileTools = registry.getByTag('file');
1363
- * ```
1364
- */
1365
999
  getByTag(tag: string): RegistryTool[];
1366
- /**
1367
- * Search tools by name or description
1368
- *
1369
- * Case-insensitive search across tool names, display names, and descriptions.
1370
- *
1371
- * @param query - The search query
1372
- * @returns Array of matching tools
1373
- *
1374
- * @example
1375
- * ```ts
1376
- * const results = registry.search('file');
1377
- * // Returns tools with 'file' in name or description
1378
- * ```
1379
- */
1380
1000
  search(query: string): RegistryTool[];
1381
- /**
1382
- * Register multiple tools at once
1383
- *
1384
- * @param tools - Iterable of tools to register
1385
- * @throws Error if any tool name conflicts with existing tools
1386
- *
1387
- * @example
1388
- * ```ts
1389
- * registry.registerMany([tool1, tool2, tool3]);
1390
- * ```
1391
- */
1392
- registerMany(tools: Iterable<RegisterManyTool>): void;
1393
- /**
1394
- * Clear all tools from the registry
1395
- *
1396
- * @example
1397
- * ```ts
1398
- * registry.clear();
1399
- * console.log(registry.size()); // 0
1400
- * ```
1401
- */
1001
+ registerMany(tools: Iterable<Tool<never, unknown>>): void;
1402
1002
  clear(): void;
1403
- /**
1404
- * Get the number of registered tools
1405
- *
1406
- * @returns Number of tools in the registry
1407
- *
1408
- * @example
1409
- * ```ts
1410
- * console.log(`Registry has ${registry.size()} tools`);
1411
- * ```
1412
- */
1413
1003
  size(): number;
1414
- /**
1415
- * Get all tool names
1416
- *
1417
- * @returns Array of tool names
1418
- *
1419
- * @example
1420
- * ```ts
1421
- * const names = registry.getNames();
1422
- * console.log('Available tools:', names.join(', '));
1423
- * ```
1424
- */
1425
1004
  getNames(): string[];
1426
- /**
1427
- * Register an event handler
1428
- *
1429
- * @param event - The event to listen for
1430
- * @param handler - The handler function
1431
- *
1432
- * @example
1433
- * ```ts
1434
- * registry.on(RegistryEvent.TOOL_REGISTERED, (tool) => {
1435
- * console.log('New tool:', tool.metadata.name);
1436
- * });
1437
- * ```
1438
- */
1439
1005
  on(event: RegistryEvent, handler: EventHandler): void;
1440
- /**
1441
- * Unregister an event handler
1442
- *
1443
- * @param event - The event to stop listening for
1444
- * @param handler - The handler function to remove
1445
- *
1446
- * @example
1447
- * ```ts
1448
- * const handler = (tool) => console.log(tool);
1449
- * registry.on(RegistryEvent.TOOL_REGISTERED, handler);
1450
- * registry.off(RegistryEvent.TOOL_REGISTERED, handler);
1451
- * ```
1452
- */
1453
1006
  off(event: RegistryEvent, handler: EventHandler): void;
1454
- /**
1455
- * Emit an event to all registered handlers
1456
- *
1457
- * @param event - The event to emit
1458
- * @param data - The event data
1459
- * @private
1460
- */
1461
1007
  private emit;
1462
- /**
1463
- * Convert all registered tools to LangChain format
1464
- *
1465
- * This allows the entire registry to be used with LangChain agents.
1466
- *
1467
- * @returns Array of LangChain DynamicStructuredTools
1468
- *
1469
- * @example
1470
- * ```ts
1471
- * const registry = new ToolRegistry();
1472
- * registry.registerMany([tool1, tool2, tool3]);
1473
- *
1474
- * const langchainTools = registry.toLangChainTools();
1475
- *
1476
- * const agent = createAgent({
1477
- * model: new ChatOpenAI(),
1478
- * tools: langchainTools,
1479
- * });
1480
- * ```
1481
- */
1482
- toLangChainTools(): DynamicStructuredTool[];
1483
- /**
1484
- * Generate a formatted prompt describing all tools
1485
- *
1486
- * Creates a human-readable description of all tools in the registry,
1487
- * suitable for inclusion in LLM prompts.
1488
- *
1489
- * @param options - Options for customizing the prompt
1490
- * @returns Formatted prompt string
1491
- *
1492
- * @example
1493
- * ```ts
1494
- * const prompt = registry.generatePrompt({
1495
- * includeExamples: true,
1496
- * groupByCategory: true,
1497
- * maxExamplesPerTool: 2,
1498
- * });
1499
- *
1500
- * console.log(prompt);
1501
- * // Available Tools:
1502
- * //
1503
- * // FILE SYSTEM TOOLS:
1504
- * // - read-file: Read a file from the file system
1505
- * // Parameters: path (string)
1506
- * // Example: Read a text file
1507
- * // Input: { "path": "./README.md" }
1508
- * // ...
1509
- * ```
1510
- */
1008
+ toLangChainTools(): _langchain_core_tools.DynamicStructuredTool<_langchain_core_tools.ToolSchemaBase, any, any, any, string>[];
1511
1009
  generatePrompt(options?: PromptOptions): string;
1512
1010
  }
1513
1011