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