@falai/agent 0.9.0-alpha-2 → 0.9.2

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.
Files changed (179) hide show
  1. package/README.md +42 -34
  2. package/dist/cjs/src/core/Agent.d.ts +48 -44
  3. package/dist/cjs/src/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/src/core/Agent.js +151 -1110
  5. package/dist/cjs/src/core/Agent.js.map +1 -1
  6. package/dist/cjs/src/core/ResponseModal.d.ts +211 -0
  7. package/dist/cjs/src/core/ResponseModal.d.ts.map +1 -0
  8. package/dist/cjs/src/core/ResponseModal.js +1394 -0
  9. package/dist/cjs/src/core/ResponseModal.js.map +1 -0
  10. package/dist/cjs/src/core/ResponsePipeline.d.ts +8 -4
  11. package/dist/cjs/src/core/ResponsePipeline.d.ts.map +1 -1
  12. package/dist/cjs/src/core/ResponsePipeline.js +48 -20
  13. package/dist/cjs/src/core/ResponsePipeline.js.map +1 -1
  14. package/dist/cjs/src/core/Route.d.ts +12 -5
  15. package/dist/cjs/src/core/Route.d.ts.map +1 -1
  16. package/dist/cjs/src/core/Route.js +26 -5
  17. package/dist/cjs/src/core/Route.js.map +1 -1
  18. package/dist/cjs/src/core/RoutingEngine.d.ts +5 -0
  19. package/dist/cjs/src/core/RoutingEngine.d.ts.map +1 -1
  20. package/dist/cjs/src/core/RoutingEngine.js +37 -25
  21. package/dist/cjs/src/core/RoutingEngine.js.map +1 -1
  22. package/dist/cjs/src/core/SessionManager.d.ts +9 -1
  23. package/dist/cjs/src/core/SessionManager.d.ts.map +1 -1
  24. package/dist/cjs/src/core/SessionManager.js +27 -5
  25. package/dist/cjs/src/core/SessionManager.js.map +1 -1
  26. package/dist/cjs/src/core/Step.d.ts +60 -7
  27. package/dist/cjs/src/core/Step.d.ts.map +1 -1
  28. package/dist/cjs/src/core/Step.js +151 -4
  29. package/dist/cjs/src/core/Step.js.map +1 -1
  30. package/dist/cjs/src/core/ToolManager.d.ts +234 -0
  31. package/dist/cjs/src/core/ToolManager.d.ts.map +1 -0
  32. package/dist/cjs/src/core/ToolManager.js +1117 -0
  33. package/dist/cjs/src/core/ToolManager.js.map +1 -0
  34. package/dist/cjs/src/index.d.ts +5 -4
  35. package/dist/cjs/src/index.d.ts.map +1 -1
  36. package/dist/cjs/src/index.js +11 -3
  37. package/dist/cjs/src/index.js.map +1 -1
  38. package/dist/cjs/src/types/agent.d.ts +2 -1
  39. package/dist/cjs/src/types/agent.d.ts.map +1 -1
  40. package/dist/cjs/src/types/ai.d.ts +1 -1
  41. package/dist/cjs/src/types/ai.d.ts.map +1 -1
  42. package/dist/cjs/src/types/index.d.ts +3 -2
  43. package/dist/cjs/src/types/index.d.ts.map +1 -1
  44. package/dist/cjs/src/types/index.js +3 -1
  45. package/dist/cjs/src/types/index.js.map +1 -1
  46. package/dist/cjs/src/types/route.d.ts +6 -4
  47. package/dist/cjs/src/types/route.d.ts.map +1 -1
  48. package/dist/cjs/src/types/tool.d.ts +84 -14
  49. package/dist/cjs/src/types/tool.d.ts.map +1 -1
  50. package/dist/cjs/src/types/tool.js +13 -0
  51. package/dist/cjs/src/types/tool.js.map +1 -1
  52. package/dist/cjs/src/utils/clone.d.ts.map +1 -1
  53. package/dist/cjs/src/utils/clone.js +0 -4
  54. package/dist/cjs/src/utils/clone.js.map +1 -1
  55. package/dist/cjs/src/utils/history.d.ts +30 -1
  56. package/dist/cjs/src/utils/history.d.ts.map +1 -1
  57. package/dist/cjs/src/utils/history.js +169 -23
  58. package/dist/cjs/src/utils/history.js.map +1 -1
  59. package/dist/cjs/src/utils/index.d.ts +1 -1
  60. package/dist/cjs/src/utils/index.d.ts.map +1 -1
  61. package/dist/cjs/src/utils/index.js +5 -1
  62. package/dist/cjs/src/utils/index.js.map +1 -1
  63. package/dist/src/core/Agent.d.ts +48 -44
  64. package/dist/src/core/Agent.d.ts.map +1 -1
  65. package/dist/src/core/Agent.js +152 -1111
  66. package/dist/src/core/Agent.js.map +1 -1
  67. package/dist/src/core/ResponseModal.d.ts +211 -0
  68. package/dist/src/core/ResponseModal.d.ts.map +1 -0
  69. package/dist/src/core/ResponseModal.js +1389 -0
  70. package/dist/src/core/ResponseModal.js.map +1 -0
  71. package/dist/src/core/ResponsePipeline.d.ts +8 -4
  72. package/dist/src/core/ResponsePipeline.d.ts.map +1 -1
  73. package/dist/src/core/ResponsePipeline.js +48 -20
  74. package/dist/src/core/ResponsePipeline.js.map +1 -1
  75. package/dist/src/core/Route.d.ts +12 -5
  76. package/dist/src/core/Route.d.ts.map +1 -1
  77. package/dist/src/core/Route.js +26 -5
  78. package/dist/src/core/Route.js.map +1 -1
  79. package/dist/src/core/RoutingEngine.d.ts +5 -0
  80. package/dist/src/core/RoutingEngine.d.ts.map +1 -1
  81. package/dist/src/core/RoutingEngine.js +37 -25
  82. package/dist/src/core/RoutingEngine.js.map +1 -1
  83. package/dist/src/core/SessionManager.d.ts +9 -1
  84. package/dist/src/core/SessionManager.d.ts.map +1 -1
  85. package/dist/src/core/SessionManager.js +27 -5
  86. package/dist/src/core/SessionManager.js.map +1 -1
  87. package/dist/src/core/Step.d.ts +60 -7
  88. package/dist/src/core/Step.d.ts.map +1 -1
  89. package/dist/src/core/Step.js +151 -4
  90. package/dist/src/core/Step.js.map +1 -1
  91. package/dist/src/core/ToolManager.d.ts +234 -0
  92. package/dist/src/core/ToolManager.d.ts.map +1 -0
  93. package/dist/src/core/ToolManager.js +1111 -0
  94. package/dist/src/core/ToolManager.js.map +1 -0
  95. package/dist/src/index.d.ts +5 -4
  96. package/dist/src/index.d.ts.map +1 -1
  97. package/dist/src/index.js +3 -2
  98. package/dist/src/index.js.map +1 -1
  99. package/dist/src/types/agent.d.ts +2 -1
  100. package/dist/src/types/agent.d.ts.map +1 -1
  101. package/dist/src/types/ai.d.ts +1 -1
  102. package/dist/src/types/ai.d.ts.map +1 -1
  103. package/dist/src/types/index.d.ts +3 -2
  104. package/dist/src/types/index.d.ts.map +1 -1
  105. package/dist/src/types/index.js +1 -0
  106. package/dist/src/types/index.js.map +1 -1
  107. package/dist/src/types/route.d.ts +6 -4
  108. package/dist/src/types/route.d.ts.map +1 -1
  109. package/dist/src/types/tool.d.ts +84 -14
  110. package/dist/src/types/tool.d.ts.map +1 -1
  111. package/dist/src/types/tool.js +12 -1
  112. package/dist/src/types/tool.js.map +1 -1
  113. package/dist/src/utils/clone.d.ts.map +1 -1
  114. package/dist/src/utils/clone.js +0 -4
  115. package/dist/src/utils/clone.js.map +1 -1
  116. package/dist/src/utils/history.d.ts +30 -1
  117. package/dist/src/utils/history.d.ts.map +1 -1
  118. package/dist/src/utils/history.js +165 -23
  119. package/dist/src/utils/history.js.map +1 -1
  120. package/dist/src/utils/index.d.ts +1 -1
  121. package/dist/src/utils/index.d.ts.map +1 -1
  122. package/dist/src/utils/index.js +1 -1
  123. package/dist/src/utils/index.js.map +1 -1
  124. package/docs/CONTRIBUTING.md +40 -0
  125. package/docs/README.md +14 -6
  126. package/docs/api/README.md +235 -45
  127. package/docs/api/overview.md +140 -33
  128. package/docs/core/agent/session-management.md +152 -5
  129. package/docs/core/ai-integration/response-processing.md +115 -4
  130. package/docs/core/conversation-flows/routes.md +130 -0
  131. package/docs/core/error-handling.md +638 -0
  132. package/docs/core/tools/tool-definition.md +684 -60
  133. package/docs/core/tools/tool-scoping.md +244 -53
  134. package/docs/guides/error-handling-patterns.md +578 -0
  135. package/docs/guides/getting-started/README.md +139 -28
  136. package/docs/guides/migration/README.md +72 -0
  137. package/docs/guides/migration/response-modal-refactor.md +518 -0
  138. package/examples/advanced-patterns/knowledge-based-agent.ts +6 -6
  139. package/examples/advanced-patterns/persistent-onboarding.ts +30 -43
  140. package/examples/advanced-patterns/streaming-responses.ts +169 -96
  141. package/examples/ai-providers/anthropic-integration.ts +9 -5
  142. package/examples/ai-providers/openai-integration.ts +11 -7
  143. package/examples/core-concepts/basic-agent.ts +106 -67
  144. package/examples/core-concepts/modern-streaming-api.ts +309 -0
  145. package/examples/core-concepts/schema-driven-extraction.ts +10 -7
  146. package/examples/core-concepts/session-management.ts +71 -18
  147. package/examples/integrations/healthcare-integration.ts +15 -29
  148. package/examples/integrations/server-session-management.ts +3 -3
  149. package/examples/persistence/memory-sessions.ts +3 -3
  150. package/examples/tools/basic-tools.ts +293 -89
  151. package/examples/tools/data-enrichment-tools.ts +185 -75
  152. package/package.json +1 -1
  153. package/src/core/Agent.ts +190 -1529
  154. package/src/core/ResponseModal.ts +1798 -0
  155. package/src/core/ResponsePipeline.ts +83 -57
  156. package/src/core/Route.ts +39 -12
  157. package/src/core/RoutingEngine.ts +46 -42
  158. package/src/core/SessionManager.ts +39 -7
  159. package/src/core/Step.ts +198 -20
  160. package/src/core/ToolManager.ts +1394 -0
  161. package/src/index.ts +19 -3
  162. package/src/types/agent.ts +2 -1
  163. package/src/types/ai.ts +1 -1
  164. package/src/types/index.ts +13 -2
  165. package/src/types/route.ts +6 -4
  166. package/src/types/tool.ts +116 -25
  167. package/src/utils/clone.ts +6 -8
  168. package/src/utils/history.ts +190 -27
  169. package/src/utils/index.ts +4 -0
  170. package/dist/cjs/src/core/ToolExecutor.d.ts +0 -45
  171. package/dist/cjs/src/core/ToolExecutor.d.ts.map +0 -1
  172. package/dist/cjs/src/core/ToolExecutor.js +0 -84
  173. package/dist/cjs/src/core/ToolExecutor.js.map +0 -1
  174. package/dist/src/core/ToolExecutor.d.ts +0 -45
  175. package/dist/src/core/ToolExecutor.d.ts.map +0 -1
  176. package/dist/src/core/ToolExecutor.js +0 -80
  177. package/dist/src/core/ToolExecutor.js.map +0 -1
  178. package/docs/core/tools/tool-execution.md +0 -815
  179. package/src/core/ToolExecutor.ts +0 -126
@@ -0,0 +1,1111 @@
1
+ /**
2
+ * ToolManager - Centralized tool management with simplified creation APIs
3
+ */
4
+ import { ToolScope } from "../types";
5
+ import { logger } from "../utils";
6
+ /**
7
+ * Error thrown when tool creation fails
8
+ */
9
+ export class ToolCreationError extends Error {
10
+ constructor(message, toolId, cause) {
11
+ super(message);
12
+ this.toolId = toolId;
13
+ this.cause = cause;
14
+ this.name = 'ToolCreationError';
15
+ }
16
+ }
17
+ /**
18
+ * Error thrown when tool execution fails
19
+ */
20
+ export class ToolExecutionError extends Error {
21
+ constructor(message, toolId, executionContext, cause) {
22
+ super(message);
23
+ this.toolId = toolId;
24
+ this.executionContext = executionContext;
25
+ this.cause = cause;
26
+ this.name = 'ToolExecutionError';
27
+ }
28
+ }
29
+ /**
30
+ * ToolManager - Centralized tool management with simplified APIs
31
+ */
32
+ export class ToolManager {
33
+ constructor(agent) {
34
+ this.agent = agent;
35
+ this.toolRegistry = new Map();
36
+ }
37
+ /**
38
+ * Validate a tool definition for completeness and correctness
39
+ */
40
+ validateToolDefinition(definition) {
41
+ const errors = [];
42
+ // Required fields validation
43
+ if (!definition.id || typeof definition.id !== 'string') {
44
+ errors.push('Tool ID is required and must be a non-empty string');
45
+ }
46
+ else if (definition.id.trim() === '') {
47
+ errors.push('Tool ID cannot be empty or whitespace only');
48
+ }
49
+ else if (!/^[a-zA-Z0-9_-]+$/.test(definition.id)) {
50
+ errors.push('Tool ID must contain only alphanumeric characters, underscores, and hyphens');
51
+ }
52
+ if (!definition.handler || typeof definition.handler !== 'function') {
53
+ errors.push('Tool handler is required and must be a function');
54
+ }
55
+ // Optional fields validation
56
+ if (definition.name !== undefined && (typeof definition.name !== 'string' || definition.name.trim() === '')) {
57
+ errors.push('Tool name must be a non-empty string if provided');
58
+ }
59
+ if (definition.description !== undefined && (typeof definition.description !== 'string' || definition.description.trim() === '')) {
60
+ errors.push('Tool description must be a non-empty string if provided');
61
+ }
62
+ // Parameters validation (basic JSON schema check)
63
+ if (definition.parameters !== undefined) {
64
+ try {
65
+ if (typeof definition.parameters === 'object' && definition.parameters !== null) {
66
+ // Basic validation for JSON schema structure
67
+ const params = definition.parameters;
68
+ if ('type' in params && params.type && typeof params.type !== 'string') {
69
+ errors.push('Tool parameters type must be a string if specified');
70
+ }
71
+ if ('properties' in params && params.properties && typeof params.properties !== 'object') {
72
+ errors.push('Tool parameters properties must be an object if specified');
73
+ }
74
+ }
75
+ else if (typeof definition.parameters !== 'string') {
76
+ errors.push('Tool parameters must be an object (JSON schema) or string if provided');
77
+ }
78
+ }
79
+ catch {
80
+ errors.push('Tool parameters must be valid JSON schema or string');
81
+ }
82
+ }
83
+ if (errors.length > 0) {
84
+ throw new ToolCreationError(`Tool definition validation failed: ${errors.join('; ')}`, definition.id || 'unknown');
85
+ }
86
+ }
87
+ /**
88
+ * Validate data enrichment configuration
89
+ */
90
+ validateDataEnrichmentConfig(config) {
91
+ const errors = [];
92
+ if (!config.fields || !Array.isArray(config.fields) || config.fields.length === 0) {
93
+ errors.push('Data enrichment fields must be a non-empty array');
94
+ }
95
+ if (!config.enricher || typeof config.enricher !== 'function') {
96
+ errors.push('Data enrichment enricher must be a function');
97
+ }
98
+ if (errors.length > 0) {
99
+ throw new ToolCreationError(`Data enrichment configuration validation failed: ${errors.join('; ')}`, config.id || 'unknown');
100
+ }
101
+ }
102
+ /**
103
+ * Validate validation configuration
104
+ */
105
+ validateValidationConfig(config) {
106
+ const errors = [];
107
+ if (!config.fields || !Array.isArray(config.fields) || config.fields.length === 0) {
108
+ errors.push('Validation fields must be a non-empty array');
109
+ }
110
+ if (!config.validator || typeof config.validator !== 'function') {
111
+ errors.push('Validation validator must be a function');
112
+ }
113
+ if (errors.length > 0) {
114
+ throw new ToolCreationError(`Validation configuration validation failed: ${errors.join('; ')}`, config.id || 'unknown');
115
+ }
116
+ }
117
+ /**
118
+ * Validate API call configuration
119
+ */
120
+ validateApiCallConfig(config) {
121
+ const errors = [];
122
+ if (!config.endpoint) {
123
+ errors.push('API call endpoint is required');
124
+ }
125
+ else if (typeof config.endpoint !== 'string' && typeof config.endpoint !== 'function') {
126
+ errors.push('API call endpoint must be a string or function');
127
+ }
128
+ if (config.method && !['GET', 'POST', 'PUT', 'DELETE'].includes(config.method)) {
129
+ errors.push('API call method must be one of: GET, POST, PUT, DELETE');
130
+ }
131
+ if (config.headers && typeof config.headers !== 'object' && typeof config.headers !== 'function') {
132
+ errors.push('API call headers must be an object or function');
133
+ }
134
+ if (config.body && typeof config.body !== 'function') {
135
+ errors.push('API call body must be a function');
136
+ }
137
+ if (config.transform && typeof config.transform !== 'function') {
138
+ errors.push('API call transform must be a function');
139
+ }
140
+ if (errors.length > 0) {
141
+ throw new ToolCreationError(`API call configuration validation failed: ${errors.join('; ')}`, config.id || 'unknown');
142
+ }
143
+ }
144
+ /**
145
+ * Validate computation configuration
146
+ */
147
+ validateComputationConfig(config) {
148
+ const errors = [];
149
+ if (!config.inputs || !Array.isArray(config.inputs) || config.inputs.length === 0) {
150
+ errors.push('Computation inputs must be a non-empty array');
151
+ }
152
+ if (!config.compute || typeof config.compute !== 'function') {
153
+ errors.push('Computation compute function is required');
154
+ }
155
+ if (errors.length > 0) {
156
+ throw new ToolCreationError(`Computation configuration validation failed: ${errors.join('; ')}`, config.id || 'unknown');
157
+ }
158
+ }
159
+ /**
160
+ * Create a tool instance with type inference from parent Agent
161
+ * Does not register the tool anywhere - just creates it
162
+ */
163
+ create(definition) {
164
+ try {
165
+ // Validate the tool definition first
166
+ this.validateToolDefinition(definition);
167
+ logger.debug(`[ToolManager] Created tool: ${definition.id}`);
168
+ return definition;
169
+ }
170
+ catch (error) {
171
+ throw new ToolCreationError(`Failed to create tool '${definition.id}': ${error instanceof Error ? error.message : String(error)}`, definition.id, error instanceof Error ? error : undefined);
172
+ }
173
+ }
174
+ /**
175
+ * Register a tool in the registry for later reference by ID
176
+ * Can accept a tool instance
177
+ */
178
+ register(tool) {
179
+ try {
180
+ if (!tool) {
181
+ throw new ToolCreationError('Tool is required for registration', 'unknown');
182
+ }
183
+ if (!('handler' in tool) || typeof tool.handler !== 'function') {
184
+ throw new ToolCreationError('Invalid tool provided for registration - must have a handler function', tool?.id || 'unknown');
185
+ }
186
+ // Validate the tool
187
+ if (!tool.id || typeof tool.id !== 'string' || tool.id.trim() === '') {
188
+ throw new ToolCreationError('Tool ID is required and must be a non-empty string', tool.id || 'unknown');
189
+ }
190
+ // Check for ID conflicts and provide better error context
191
+ if (this.toolRegistry.has(tool.id)) {
192
+ const existingTool = this.toolRegistry.get(tool.id);
193
+ logger.warn(`[ToolManager] Overwriting existing registered tool: ${tool.id} (previous: ${existingTool?.name || 'unnamed'})`);
194
+ }
195
+ this.toolRegistry.set(tool.id, tool);
196
+ logger.debug(`[ToolManager] Registered tool: ${tool.id} (${tool.name || 'unnamed'})`);
197
+ return tool;
198
+ }
199
+ catch (error) {
200
+ if (error instanceof ToolCreationError) {
201
+ throw error;
202
+ }
203
+ const toolId = tool?.id || 'unknown';
204
+ throw new ToolCreationError(`Failed to register tool '${toolId}': ${error instanceof Error ? error.message : String(error)}`, toolId, error instanceof Error ? error : undefined);
205
+ }
206
+ }
207
+ /**
208
+ * Register multiple tools at once
209
+ */
210
+ registerMany(tools) {
211
+ return tools.map(tool => this.register(tool));
212
+ }
213
+ /**
214
+ * Get a registered tool by ID
215
+ */
216
+ getRegisteredTool(toolId) {
217
+ return this.toolRegistry.get(toolId);
218
+ }
219
+ /**
220
+ * Get all registered tools
221
+ */
222
+ getAllRegistered() {
223
+ return new Map(this.toolRegistry);
224
+ }
225
+ /**
226
+ * Check if a tool is registered
227
+ */
228
+ isRegistered(toolId) {
229
+ return this.toolRegistry.has(toolId);
230
+ }
231
+ /**
232
+ * Get all registered tool IDs
233
+ */
234
+ getRegisteredIds() {
235
+ return Array.from(this.toolRegistry.keys());
236
+ }
237
+ /**
238
+ * Get tool by ID from a specific scope
239
+ */
240
+ getFromScope(toolId, scope, step, route) {
241
+ return this.find(toolId, scope, step, route);
242
+ }
243
+ /**
244
+ * Check if a tool exists in any scope
245
+ */
246
+ exists(toolId, step, route) {
247
+ return this.find(toolId, ToolScope.ALL, step, route) !== undefined;
248
+ }
249
+ /**
250
+ * Get tool count by scope
251
+ */
252
+ getToolCount(scope, step, route) {
253
+ return this.getAvailable(scope, step, route).length;
254
+ }
255
+ /**
256
+ * Clear all registered tools
257
+ */
258
+ clearRegistry() {
259
+ this.toolRegistry.clear();
260
+ logger.debug('[ToolManager] Cleared tool registry');
261
+ }
262
+ /**
263
+ * Remove a tool from registry
264
+ */
265
+ unregister(toolId) {
266
+ const existed = this.toolRegistry.has(toolId);
267
+ this.toolRegistry.delete(toolId);
268
+ if (existed) {
269
+ logger.debug(`[ToolManager] Unregistered tool: ${toolId}`);
270
+ }
271
+ return existed;
272
+ }
273
+ /**
274
+ * Add a tool to the agent scope (creates and adds in one operation)
275
+ */
276
+ addToAgent(tool) {
277
+ // Validate tool before adding
278
+ if (!tool || !tool.id || !tool.handler) {
279
+ throw new ToolCreationError('Invalid tool: must have id and handler properties', tool?.id || 'unknown');
280
+ }
281
+ // Add to agent's tools array using the unified interface
282
+ if (this.agent) {
283
+ this.agent.addTool(tool);
284
+ }
285
+ else {
286
+ logger.warn(`[ToolManager] No agent available, tool not added to agent scope: ${tool.id}`);
287
+ }
288
+ logger.debug(`[ToolManager] Added tool to agent scope: ${tool.id}`);
289
+ return tool;
290
+ }
291
+ /**
292
+ * Add a tool to a specific route scope (creates and adds in one operation)
293
+ */
294
+ addToRoute(route, tool) {
295
+ // Add to route's tools array using the existing createTool method
296
+ if (route && typeof route.createTool === 'function') {
297
+ route.createTool(tool);
298
+ }
299
+ else {
300
+ logger.warn(`[ToolManager] Route does not support createTool method, tool not added to route scope: ${tool.id}`);
301
+ }
302
+ logger.debug(`[ToolManager] Added tool to route scope: ${tool.id}`);
303
+ return tool;
304
+ }
305
+ /**
306
+ * Find a tool by ID across different scopes with enhanced resolution logic
307
+ * Priority: step → route → agent → registry
308
+ * Supports both ID and name matching for better compatibility
309
+ */
310
+ find(toolId, scope, step, route) {
311
+ logger.debug(`[ToolManager] Finding tool: ${toolId} with scope: ${scope || 'ALL'}`);
312
+ // Check step-level tools first (if step provided and scope allows)
313
+ if (step && (!scope || scope === ToolScope.STEP || scope === ToolScope.ALL)) {
314
+ if (step.tools) {
315
+ for (const toolRef of step.tools) {
316
+ if (typeof toolRef !== 'string') {
317
+ // Inline tool object - check both id and name
318
+ if (toolRef.id === toolId || toolRef.name === toolId) {
319
+ logger.debug(`[ToolManager] Found tool in step scope: ${toolId}`);
320
+ return toolRef;
321
+ }
322
+ }
323
+ else {
324
+ // String reference - check if it matches and resolve from registry
325
+ if (toolRef === toolId) {
326
+ const registeredTool = this.toolRegistry.get(toolId);
327
+ if (registeredTool) {
328
+ logger.debug(`[ToolManager] Found tool reference in step, resolved from registry: ${toolId}`);
329
+ return registeredTool;
330
+ }
331
+ }
332
+ }
333
+ }
334
+ }
335
+ }
336
+ // Check route-level tools (if route provided and scope allows)
337
+ if (route && (!scope || scope === ToolScope.ROUTE || scope === ToolScope.ALL)) {
338
+ if (route.tools) {
339
+ const routeTool = route.tools.find((t) => t.id === toolId || t.name === toolId);
340
+ if (routeTool) {
341
+ logger.debug(`[ToolManager] Found tool in route scope: ${toolId}`);
342
+ return routeTool;
343
+ }
344
+ }
345
+ }
346
+ // Check agent-level tools (if scope allows)
347
+ if (!scope || scope === ToolScope.AGENT || scope === ToolScope.ALL) {
348
+ if (this.agent) {
349
+ const agentTools = this.agent.getTools();
350
+ const agentTool = agentTools.find((t) => t.id === toolId || t.name === toolId);
351
+ if (agentTool) {
352
+ logger.debug(`[ToolManager] Found tool in agent scope: ${toolId}`);
353
+ return agentTool;
354
+ }
355
+ }
356
+ }
357
+ // Check registry (if scope allows)
358
+ if (!scope || scope === ToolScope.REGISTERED || scope === ToolScope.ALL) {
359
+ const registeredTool = this.toolRegistry.get(toolId);
360
+ if (registeredTool) {
361
+ logger.debug(`[ToolManager] Found tool in registry: ${toolId}`);
362
+ return registeredTool;
363
+ }
364
+ // Also check by name in registry
365
+ for (const [id, tool] of Array.from(this.toolRegistry.entries())) {
366
+ if (tool.name === toolId) {
367
+ logger.debug(`[ToolManager] Found tool in registry by name: ${toolId} (id: ${id})`);
368
+ return tool;
369
+ }
370
+ }
371
+ }
372
+ logger.debug(`[ToolManager] Tool not found: ${toolId}`);
373
+ return undefined;
374
+ }
375
+ /**
376
+ * Get available tools for current context with enhanced resolution and deduplication
377
+ * Returns tools in priority order with higher-priority scopes taking precedence
378
+ */
379
+ getAvailable(scope, step, route) {
380
+ const toolMap = new Map();
381
+ const resolvedTools = [];
382
+ logger.debug(`[ToolManager] Getting available tools with scope: ${scope || 'ALL'}`);
383
+ // Add registered tools first (lowest priority)
384
+ if (!scope || scope === ToolScope.REGISTERED || scope === ToolScope.ALL) {
385
+ for (const [id, tool] of Array.from(this.toolRegistry.entries())) {
386
+ toolMap.set(id, tool);
387
+ }
388
+ logger.debug(`[ToolManager] Added ${this.toolRegistry.size} registered tools`);
389
+ }
390
+ // Add agent-level tools (override registered tools with same ID)
391
+ if (!scope || scope === ToolScope.AGENT || scope === ToolScope.ALL) {
392
+ if (this.agent) {
393
+ const agentTools = this.agent.getTools();
394
+ for (const tool of agentTools) {
395
+ toolMap.set(tool.id, tool);
396
+ }
397
+ logger.debug(`[ToolManager] Added ${agentTools.length} agent tools`);
398
+ }
399
+ }
400
+ // Add route-level tools (override agent and registered tools with same ID)
401
+ if (route && (!scope || scope === ToolScope.ROUTE || scope === ToolScope.ALL)) {
402
+ if (route.tools) {
403
+ for (const tool of route.tools) {
404
+ toolMap.set(tool.id, tool);
405
+ }
406
+ logger.debug(`[ToolManager] Added ${route.tools.length} route tools`);
407
+ }
408
+ }
409
+ // Add step-level tools (highest priority - override all others with same ID)
410
+ if (step && (!scope || scope === ToolScope.STEP || scope === ToolScope.ALL)) {
411
+ if (step.tools) {
412
+ for (const toolRef of step.tools) {
413
+ if (typeof toolRef !== 'string') {
414
+ // Inline tool object - add directly
415
+ toolMap.set(toolRef.id, toolRef);
416
+ resolvedTools.push(toolRef);
417
+ }
418
+ else {
419
+ // String reference - resolve from registry and add if found
420
+ const registeredTool = this.toolRegistry.get(toolRef);
421
+ if (registeredTool) {
422
+ toolMap.set(registeredTool.id, registeredTool);
423
+ resolvedTools.push(registeredTool);
424
+ }
425
+ else {
426
+ logger.warn(`[ToolManager] Step references unknown tool: ${toolRef}`);
427
+ }
428
+ }
429
+ }
430
+ logger.debug(`[ToolManager] Added ${step.tools.length} step tools (${resolvedTools.length} resolved)`);
431
+ }
432
+ }
433
+ // Convert map to array, preserving priority order
434
+ const allTools = Array.from(toolMap.values());
435
+ // If we have step-specific tools, prioritize them
436
+ if (resolvedTools.length > 0) {
437
+ // Add resolved step tools first, then other tools not already included
438
+ const stepToolIds = new Set(resolvedTools.map(t => t.id));
439
+ const otherTools = allTools.filter(t => !stepToolIds.has(t.id));
440
+ return [...resolvedTools, ...otherTools];
441
+ }
442
+ logger.debug(`[ToolManager] Returning ${allTools.length} available tools`);
443
+ return allTools;
444
+ }
445
+ /**
446
+ * Execute a tool by ID with proper error handling and fallback strategies
447
+ * Consolidates tool execution logic from ToolExecutor and ResponseModal
448
+ */
449
+ async execute(toolId, args, context) {
450
+ const maxRetries = context?.retryCount || 2;
451
+ const fallbackTools = context?.fallbackTools || [];
452
+ let lastError;
453
+ // Validate input parameters
454
+ if (!toolId || typeof toolId !== 'string' || toolId.trim() === '') {
455
+ return {
456
+ success: false,
457
+ error: 'Tool ID is required and must be a non-empty string',
458
+ metadata: { toolId, args }
459
+ };
460
+ }
461
+ // Try primary tool with retries
462
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
463
+ try {
464
+ const tool = this.find(toolId, undefined, context?.step, context?.route);
465
+ if (!tool) {
466
+ // Tool not found - try fallback tools if available
467
+ if (fallbackTools.length > 0) {
468
+ logger.warn(`[ToolManager] Primary tool '${toolId}' not found, trying fallback tools: ${fallbackTools.join(', ')}`);
469
+ for (const fallbackId of fallbackTools) {
470
+ const fallbackResult = await this.execute(fallbackId, args, {
471
+ ...context,
472
+ fallbackTools: [], // Prevent infinite recursion
473
+ retryCount: 0 // Don't retry fallback tools
474
+ });
475
+ if (fallbackResult.success) {
476
+ logger.info(`[ToolManager] Fallback tool '${fallbackId}' succeeded for primary tool '${toolId}'`);
477
+ return {
478
+ ...fallbackResult,
479
+ metadata: {
480
+ ...fallbackResult.metadata,
481
+ primaryTool: toolId,
482
+ fallbackUsed: fallbackId
483
+ }
484
+ };
485
+ }
486
+ }
487
+ }
488
+ return {
489
+ success: false,
490
+ error: `Tool not found: ${toolId}${fallbackTools.length > 0 ? ` (fallback tools also failed: ${fallbackTools.join(', ')})` : ''}`,
491
+ metadata: { toolId, args, fallbackTools }
492
+ };
493
+ }
494
+ // Execute the tool with proper context
495
+ const result = await this.executeTool({
496
+ tool,
497
+ context: context?.context || (await this.agent.getContext()),
498
+ updateContext: context?.updateContext || this.agent.updateContext.bind(this.agent),
499
+ updateData: context?.updateData || this.agent.updateCollectedData.bind(this.agent),
500
+ history: context?.history || [],
501
+ data: context?.data,
502
+ toolArguments: args,
503
+ });
504
+ // Success - return result with execution metadata
505
+ if (result.success) {
506
+ return {
507
+ ...result,
508
+ metadata: {
509
+ ...result.metadata,
510
+ toolId,
511
+ attempt: attempt + 1,
512
+ maxRetries
513
+ }
514
+ };
515
+ }
516
+ else {
517
+ // Tool execution returned failure - don't retry, return immediately
518
+ return {
519
+ ...result,
520
+ metadata: {
521
+ ...result.metadata,
522
+ toolId,
523
+ attempt: attempt + 1,
524
+ maxRetries
525
+ }
526
+ };
527
+ }
528
+ }
529
+ catch (error) {
530
+ lastError = error instanceof Error ? error : new Error(String(error));
531
+ const errorMessage = lastError.message;
532
+ // Check if this is a transient error that should be retried
533
+ const isTransientError = this.isTransientError(lastError);
534
+ if (attempt < maxRetries && isTransientError) {
535
+ logger.warn(`[ToolManager] Tool execution attempt ${attempt + 1} failed for ${toolId}, retrying: ${errorMessage}`);
536
+ // Exponential backoff for retries
537
+ const delay = Math.min(1000 * Math.pow(2, attempt), 5000);
538
+ await new Promise(resolve => setTimeout(resolve, delay));
539
+ continue;
540
+ }
541
+ else {
542
+ logger.error(`[ToolManager] Tool execution failed for ${toolId} after ${attempt + 1} attempts: ${errorMessage}`);
543
+ break;
544
+ }
545
+ }
546
+ }
547
+ // All retries failed - try fallback tools
548
+ if (fallbackTools.length > 0) {
549
+ logger.warn(`[ToolManager] Primary tool '${toolId}' failed after retries, trying fallback tools: ${fallbackTools.join(', ')}`);
550
+ for (const fallbackId of fallbackTools) {
551
+ try {
552
+ const fallbackResult = await this.execute(fallbackId, args, {
553
+ ...context,
554
+ fallbackTools: [], // Prevent infinite recursion
555
+ retryCount: 0 // Don't retry fallback tools
556
+ });
557
+ if (fallbackResult.success) {
558
+ logger.info(`[ToolManager] Fallback tool '${fallbackId}' succeeded for failed primary tool '${toolId}'`);
559
+ return {
560
+ ...fallbackResult,
561
+ metadata: {
562
+ ...fallbackResult.metadata,
563
+ primaryTool: toolId,
564
+ primaryError: lastError?.message,
565
+ fallbackUsed: fallbackId
566
+ }
567
+ };
568
+ }
569
+ }
570
+ catch (fallbackError) {
571
+ logger.warn(`[ToolManager] Fallback tool '${fallbackId}' also failed: ${fallbackError instanceof Error ? fallbackError.message : String(fallbackError)}`);
572
+ }
573
+ }
574
+ }
575
+ // All attempts and fallbacks failed
576
+ throw new ToolExecutionError(`Tool execution failed after ${maxRetries + 1} attempts: ${lastError?.message || 'Unknown error'}${fallbackTools.length > 0 ? ` (fallback tools also failed: ${fallbackTools.join(', ')})` : ''}`, toolId, { args, context, attempts: maxRetries + 1, fallbackTools }, lastError);
577
+ }
578
+ /**
579
+ * Determine if an error is transient and should be retried
580
+ */
581
+ isTransientError(error) {
582
+ const transientPatterns = [
583
+ /network/i,
584
+ /timeout/i,
585
+ /connection/i,
586
+ /temporary/i,
587
+ /rate limit/i,
588
+ /503/,
589
+ /502/,
590
+ /504/,
591
+ /ECONNRESET/,
592
+ /ETIMEDOUT/,
593
+ /ENOTFOUND/
594
+ ];
595
+ return transientPatterns.some(pattern => pattern.test(error.message));
596
+ }
597
+ /**
598
+ * Execute a single tool with context and collected data
599
+ * Consolidates logic from ToolExecutor class with enhanced error handling
600
+ */
601
+ async executeTool(params) {
602
+ const { tool, context, updateContext, updateData, history, data, toolArguments } = params;
603
+ const startTime = Date.now();
604
+ try {
605
+ // Validate tool before execution
606
+ if (!tool || !tool.handler || typeof tool.handler !== 'function') {
607
+ return {
608
+ success: false,
609
+ error: `Invalid tool: ${tool?.id || 'unknown'} - missing or invalid handler`,
610
+ metadata: { toolId: tool?.id, executionTime: 0 }
611
+ };
612
+ }
613
+ // Build tool context with complete agent data
614
+ const toolContext = {
615
+ context,
616
+ updateContext,
617
+ updateData,
618
+ history,
619
+ data: data || {},
620
+ getField: (key) => {
621
+ return data?.[key];
622
+ },
623
+ setField: async (key, value) => {
624
+ const update = {};
625
+ update[key] = value;
626
+ await updateData(update);
627
+ },
628
+ hasField: (key) => {
629
+ return data != null && key in data;
630
+ }
631
+ };
632
+ logger.debug(`[ToolManager] Executing tool: ${tool.id} with args:`, toolArguments);
633
+ // Execute tool with timeout protection
634
+ const executionTimeout = 30000; // 30 seconds default timeout
635
+ const timeoutPromise = new Promise((_, reject) => {
636
+ setTimeout(() => reject(new Error(`Tool execution timeout after ${executionTimeout}ms`)), executionTimeout);
637
+ });
638
+ const result = await Promise.race([
639
+ tool.handler(toolContext, toolArguments),
640
+ timeoutPromise
641
+ ]);
642
+ const executionTime = Date.now() - startTime;
643
+ logger.debug(`[ToolManager] Tool ${tool.id} completed in ${executionTime}ms`);
644
+ // Handle different result types
645
+ let toolResult;
646
+ if (result && typeof result === 'object' && ('data' in result || 'success' in result || 'error' in result)) {
647
+ // It's already a ToolResult-like object
648
+ toolResult = result;
649
+ }
650
+ else {
651
+ // It's a raw result - wrap it
652
+ toolResult = {
653
+ data: result,
654
+ success: true
655
+ };
656
+ }
657
+ // Apply data updates from tool result with validation
658
+ if (toolResult.dataUpdate) {
659
+ try {
660
+ if (typeof toolResult.dataUpdate === 'object' && toolResult.dataUpdate !== null) {
661
+ await updateData(toolResult.dataUpdate);
662
+ }
663
+ else {
664
+ logger.warn(`[ToolManager] Tool ${tool.id} returned invalid dataUpdate: expected object, got ${typeof toolResult.dataUpdate}`);
665
+ }
666
+ }
667
+ catch (updateError) {
668
+ logger.error(`[ToolManager] Failed to apply data update from tool ${tool.id}:`, updateError);
669
+ return {
670
+ success: false,
671
+ error: `Failed to apply data update: ${updateError instanceof Error ? updateError.message : String(updateError)}`,
672
+ metadata: { toolId: tool.id, executionTime, updateError: updateError instanceof Error ? updateError.message : String(updateError) }
673
+ };
674
+ }
675
+ }
676
+ // Apply context updates from tool result with validation
677
+ if (toolResult.contextUpdate) {
678
+ try {
679
+ if (typeof toolResult.contextUpdate === 'object' && toolResult.contextUpdate !== null) {
680
+ await updateContext(toolResult.contextUpdate);
681
+ }
682
+ else {
683
+ logger.warn(`[ToolManager] Tool ${tool.id} returned invalid contextUpdate: expected object, got ${typeof toolResult.contextUpdate}`);
684
+ }
685
+ }
686
+ catch (updateError) {
687
+ logger.error(`[ToolManager] Failed to apply context update from tool ${tool.id}:`, updateError);
688
+ return {
689
+ success: false,
690
+ error: `Failed to apply context update: ${updateError instanceof Error ? updateError.message : String(updateError)}`,
691
+ metadata: { toolId: tool.id, executionTime, updateError: updateError instanceof Error ? updateError.message : String(updateError) }
692
+ };
693
+ }
694
+ }
695
+ // Return execution result with metadata
696
+ return {
697
+ success: toolResult.success !== false, // Default to true unless explicitly false
698
+ data: toolResult.data,
699
+ contextUpdate: toolResult.contextUpdate,
700
+ dataUpdate: toolResult.dataUpdate,
701
+ error: toolResult.error,
702
+ metadata: {
703
+ toolId: tool.id,
704
+ toolName: tool.name,
705
+ executionTime,
706
+ ...(toolResult.meta || {})
707
+ }
708
+ };
709
+ }
710
+ catch (error) {
711
+ const executionTime = Date.now() - startTime;
712
+ logger.error(`[ToolManager] Tool execution error for ${tool.id} after ${executionTime}ms:`, error);
713
+ // Re-throw the error so the execute method can handle retries
714
+ throw error;
715
+ }
716
+ }
717
+ /**
718
+ * Execute multiple tools in sequence
719
+ * Consolidates logic from ToolExecutor class
720
+ */
721
+ async executeTools(params) {
722
+ const { tools, context, updateContext, updateData, history, data } = params;
723
+ const results = [];
724
+ for (const tool of tools) {
725
+ const result = await this.executeTool({
726
+ tool,
727
+ context,
728
+ updateContext,
729
+ updateData,
730
+ history,
731
+ data,
732
+ });
733
+ results.push(result);
734
+ // If tool failed, stop execution chain
735
+ if (!result.success) {
736
+ logger.error(`[ToolManager] Tool ${tool.id || "unknown"} failed:`, result.error);
737
+ break;
738
+ }
739
+ // Apply context updates from tool result
740
+ if (result.contextUpdate) {
741
+ await updateContext(result.contextUpdate);
742
+ }
743
+ // Apply data updates from tool result
744
+ if (result.dataUpdate) {
745
+ await updateData(result.dataUpdate);
746
+ }
747
+ }
748
+ return results;
749
+ }
750
+ /**
751
+ * Create a data enrichment tool that modifies collected data
752
+ * Returns a tool instance that can be registered or added to scope
753
+ */
754
+ createDataEnrichment(config) {
755
+ // Validate configuration first
756
+ this.validateDataEnrichmentConfig(config);
757
+ const tool = {
758
+ id: config.id,
759
+ name: config.name || `Data Enrichment: ${config.id}`,
760
+ description: config.description || `Enriches data fields: ${config.fields.join(', ')}`,
761
+ parameters: {
762
+ type: 'object',
763
+ properties: {
764
+ fields: {
765
+ type: 'array',
766
+ items: { type: 'string' },
767
+ description: `Fields to enrich: ${config.fields.join(', ')}`
768
+ }
769
+ }
770
+ },
771
+ handler: async (context) => {
772
+ try {
773
+ // Extract the specified fields from current data
774
+ const fieldData = {};
775
+ for (const field of config.fields) {
776
+ if (context.hasField(field)) {
777
+ const value = context.getField(field);
778
+ if (value !== undefined) {
779
+ fieldData[field] = value;
780
+ }
781
+ }
782
+ }
783
+ // Call the enricher function
784
+ const enrichedData = await config.enricher(context.context, fieldData);
785
+ // Update the data with enriched values
786
+ if (enrichedData && typeof enrichedData === 'object') {
787
+ await context.updateData(enrichedData);
788
+ }
789
+ logger.debug(`[ToolManager] Data enrichment completed for tool: ${config.id}`);
790
+ return {
791
+ success: true,
792
+ dataUpdate: enrichedData
793
+ };
794
+ }
795
+ catch (error) {
796
+ const errorMessage = error instanceof Error ? error.message : String(error);
797
+ logger.error(`[ToolManager] Data enrichment failed for ${config.id}: ${errorMessage}`);
798
+ throw new ToolExecutionError(`Data enrichment failed: ${errorMessage}`, config.id, { fields: config.fields }, error instanceof Error ? error : undefined);
799
+ }
800
+ }
801
+ };
802
+ return tool;
803
+ }
804
+ /**
805
+ * Create a validation tool that validates data fields
806
+ * Returns a tool instance that can be registered or added to scope
807
+ */
808
+ createValidation(config) {
809
+ // Validate configuration first
810
+ this.validateValidationConfig(config);
811
+ const tool = {
812
+ id: config.id,
813
+ name: config.name || `Validation: ${config.id}`,
814
+ description: config.description || `Validates data fields: ${config.fields.join(', ')}`,
815
+ parameters: {
816
+ type: 'object',
817
+ properties: {
818
+ fields: {
819
+ type: 'array',
820
+ items: { type: 'string' },
821
+ description: `Fields to validate: ${config.fields.join(', ')}`
822
+ }
823
+ }
824
+ },
825
+ handler: async (context) => {
826
+ try {
827
+ // Extract the specified fields from current data
828
+ const fieldData = {};
829
+ for (const field of config.fields) {
830
+ if (context.hasField(field)) {
831
+ const value = context.getField(field);
832
+ if (value !== undefined) {
833
+ fieldData[field] = value;
834
+ }
835
+ }
836
+ }
837
+ // Call the validator function
838
+ const result = await config.validator(context.context, fieldData);
839
+ logger.debug(`[ToolManager] Validation completed for tool: ${config.id}, valid: ${result.valid}`);
840
+ return result;
841
+ }
842
+ catch (error) {
843
+ const errorMessage = error instanceof Error ? error.message : String(error);
844
+ logger.error(`[ToolManager] Validation failed for ${config.id}: ${errorMessage}`);
845
+ // Return validation failure result instead of throwing
846
+ return {
847
+ valid: false,
848
+ errors: [{
849
+ field: 'validation',
850
+ message: `Validation error: ${errorMessage}`,
851
+ value: undefined,
852
+ schemaPath: config.id
853
+ }],
854
+ warnings: []
855
+ };
856
+ }
857
+ }
858
+ };
859
+ return tool;
860
+ }
861
+ /**
862
+ * Create an API call tool that makes external HTTP requests
863
+ * Returns a tool instance that can be registered or added to scope
864
+ */
865
+ createApiCall(config) {
866
+ // Validate configuration first
867
+ this.validateApiCallConfig(config);
868
+ const tool = {
869
+ id: config.id,
870
+ name: config.name || `API Call: ${config.id}`,
871
+ description: config.description || `Makes API call to external service`,
872
+ parameters: {
873
+ type: 'object',
874
+ properties: {
875
+ endpoint: {
876
+ type: 'string',
877
+ description: 'API endpoint URL'
878
+ },
879
+ method: {
880
+ type: 'string',
881
+ enum: ['GET', 'POST', 'PUT', 'DELETE'],
882
+ description: 'HTTP method'
883
+ }
884
+ }
885
+ },
886
+ handler: async (context, args) => {
887
+ try {
888
+ // Resolve endpoint URL
889
+ const endpoint = typeof config.endpoint === 'function'
890
+ ? config.endpoint(context.context, context.data)
891
+ : config.endpoint;
892
+ // Resolve headers
893
+ const headers = typeof config.headers === 'function'
894
+ ? config.headers(context.context)
895
+ : config.headers || {};
896
+ // Prepare request options
897
+ const requestOptions = {
898
+ method: config.method || 'GET',
899
+ headers: {
900
+ 'Content-Type': 'application/json',
901
+ ...headers
902
+ }
903
+ };
904
+ // Add body for non-GET requests
905
+ if (config.body && (config.method === 'POST' || config.method === 'PUT')) {
906
+ const bodyData = config.body(context.context, context.data, args);
907
+ requestOptions.body = JSON.stringify(bodyData);
908
+ }
909
+ // Make the API call
910
+ const response = await fetch(endpoint, requestOptions);
911
+ if (!response.ok) {
912
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
913
+ }
914
+ // Parse response
915
+ let responseData;
916
+ const contentType = response.headers.get('content-type');
917
+ if (contentType && contentType.includes('application/json')) {
918
+ responseData = await response.json();
919
+ }
920
+ else {
921
+ responseData = await response.text();
922
+ }
923
+ // Transform response if transformer provided
924
+ const result = config.transform ? config.transform(responseData) : responseData;
925
+ logger.debug(`[ToolManager] API call completed for tool: ${config.id}`);
926
+ return result;
927
+ }
928
+ catch (error) {
929
+ const errorMessage = error instanceof Error ? error.message : String(error);
930
+ logger.error(`[ToolManager] API call failed for ${config.id}: ${errorMessage}`);
931
+ throw new ToolExecutionError(`API call failed: ${errorMessage}`, config.id, { endpoint: config.endpoint, method: config.method, args }, error instanceof Error ? error : undefined);
932
+ }
933
+ }
934
+ };
935
+ return tool;
936
+ }
937
+ /**
938
+ * Create a computation tool that performs calculations on input data
939
+ * Returns a tool instance that can be registered or added to scope
940
+ */
941
+ createComputation(config) {
942
+ // Validate configuration first
943
+ this.validateComputationConfig(config);
944
+ const tool = {
945
+ id: config.id,
946
+ name: config.name || `Computation: ${config.id}`,
947
+ description: config.description || `Performs computation on inputs: ${config.inputs.join(', ')}`,
948
+ parameters: {
949
+ type: 'object',
950
+ properties: {
951
+ inputs: {
952
+ type: 'array',
953
+ items: { type: 'string' },
954
+ description: `Input fields: ${config.inputs.join(', ')}`
955
+ }
956
+ }
957
+ },
958
+ handler: async (context, args) => {
959
+ try {
960
+ // Extract the specified input fields from current data
961
+ const inputData = {};
962
+ for (const input of config.inputs) {
963
+ if (context.hasField(input)) {
964
+ const value = context.getField(input);
965
+ if (value !== undefined) {
966
+ inputData[input] = value;
967
+ }
968
+ }
969
+ }
970
+ // Call the compute function
971
+ const result = await config.compute(context.context, inputData, args);
972
+ logger.debug(`[ToolManager] Computation completed for tool: ${config.id}`);
973
+ return result;
974
+ }
975
+ catch (error) {
976
+ const errorMessage = error instanceof Error ? error.message : String(error);
977
+ logger.error(`[ToolManager] Computation failed for ${config.id}: ${errorMessage}`);
978
+ throw new ToolExecutionError(`Computation failed: ${errorMessage}`, config.id, { inputs: config.inputs, args }, error instanceof Error ? error : undefined);
979
+ }
980
+ }
981
+ };
982
+ return tool;
983
+ }
984
+ /**
985
+ * Get detailed information about a tool for debugging
986
+ */
987
+ getToolInfo(toolId, scope, step, route) {
988
+ const tool = this.find(toolId, scope, step, route);
989
+ if (!tool) {
990
+ return { found: false };
991
+ }
992
+ // Determine which scope the tool was found in
993
+ let foundScope = 'unknown';
994
+ // Check step scope
995
+ if (step?.tools) {
996
+ const stepTool = step.tools.find((t) => (typeof t === 'string' && t === toolId) ||
997
+ (typeof t === 'object' && (t.id === toolId || t.name === toolId)));
998
+ if (stepTool)
999
+ foundScope = 'step';
1000
+ }
1001
+ // Check route scope
1002
+ if (foundScope === 'unknown' && route?.tools) {
1003
+ const routeTool = route.tools.find((t) => t.id === toolId || t.name === toolId);
1004
+ if (routeTool)
1005
+ foundScope = 'route';
1006
+ }
1007
+ // Check agent scope
1008
+ if (foundScope === 'unknown' && this.agent) {
1009
+ const agentTools = this.agent.getTools();
1010
+ const agentTool = agentTools.find((t) => t.id === toolId || t.name === toolId);
1011
+ if (agentTool)
1012
+ foundScope = 'agent';
1013
+ }
1014
+ // Check registry
1015
+ if (foundScope === 'unknown' && this.toolRegistry.has(toolId)) {
1016
+ foundScope = 'registry';
1017
+ }
1018
+ return {
1019
+ found: true,
1020
+ tool,
1021
+ scope: foundScope,
1022
+ metadata: {
1023
+ id: tool.id,
1024
+ name: tool.name,
1025
+ hasDescription: !!tool.description,
1026
+ hasParameters: !!tool.parameters,
1027
+ handlerLength: tool.handler.length
1028
+ }
1029
+ };
1030
+ }
1031
+ /**
1032
+ * Validate that all tools in a list exist and are accessible
1033
+ */
1034
+ validateToolReferences(toolIds, step, route) {
1035
+ const missing = [];
1036
+ const found = [];
1037
+ const details = [];
1038
+ for (const toolId of toolIds) {
1039
+ const info = this.getToolInfo(toolId, ToolScope.ALL, step, route);
1040
+ if (info.found) {
1041
+ found.push(toolId);
1042
+ details.push({ id: toolId, found: true, scope: info.scope });
1043
+ }
1044
+ else {
1045
+ missing.push(toolId);
1046
+ details.push({ id: toolId, found: false });
1047
+ }
1048
+ }
1049
+ return {
1050
+ valid: missing.length === 0,
1051
+ missing,
1052
+ found,
1053
+ details
1054
+ };
1055
+ }
1056
+ /**
1057
+ * Get comprehensive statistics about the tool system
1058
+ */
1059
+ getStatistics() {
1060
+ const registeredToolIds = Array.from(this.toolRegistry.keys());
1061
+ const agentTools = this.agent ? this.agent.getTools() : [];
1062
+ const agentToolIds = agentTools.map((t) => t.id);
1063
+ // Find duplicate IDs between registry and agent
1064
+ const duplicateIds = registeredToolIds.filter(id => agentToolIds.includes(id));
1065
+ const allAvailable = this.getAvailable();
1066
+ return {
1067
+ registeredTools: this.toolRegistry.size,
1068
+ agentTools: agentTools.length,
1069
+ totalAvailable: allAvailable.length,
1070
+ registeredToolIds,
1071
+ duplicateIds
1072
+ };
1073
+ }
1074
+ /**
1075
+ * Perform health check on the tool system
1076
+ */
1077
+ healthCheck() {
1078
+ const issues = [];
1079
+ const warnings = [];
1080
+ const stats = this.getStatistics();
1081
+ // Check for duplicate tool IDs
1082
+ if (stats.duplicateIds.length > 0) {
1083
+ warnings.push(`Duplicate tool IDs found between registry and agent: ${stats.duplicateIds.join(', ')}`);
1084
+ }
1085
+ // Check for tools with missing handlers
1086
+ for (const [id, tool] of Array.from(this.toolRegistry.entries())) {
1087
+ if (!tool.handler || typeof tool.handler !== 'function') {
1088
+ issues.push(`Tool '${id}' has invalid or missing handler`);
1089
+ }
1090
+ if (!tool.id || tool.id.trim() === '') {
1091
+ issues.push(`Tool has empty or invalid ID: ${JSON.stringify(tool)}`);
1092
+ }
1093
+ }
1094
+ // Check agent tools if available
1095
+ if (this.agent) {
1096
+ const agentTools = this.agent.getTools();
1097
+ for (const tool of agentTools) {
1098
+ if (!tool.handler || typeof tool.handler !== 'function') {
1099
+ issues.push(`Agent tool '${tool.id}' has invalid or missing handler`);
1100
+ }
1101
+ }
1102
+ }
1103
+ return {
1104
+ healthy: issues.length === 0,
1105
+ issues,
1106
+ warnings,
1107
+ statistics: stats
1108
+ };
1109
+ }
1110
+ }
1111
+ //# sourceMappingURL=ToolManager.js.map