@codebolt/agent 2.2.2 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/builderpattern/agent.js +2 -2
- package/dist/builderpattern/followupquestionbuilder.js +1 -1
- package/dist/builderpattern/llmoutputhandler.js +7 -7
- package/dist/builderpattern/usermessage.d.ts +6 -1
- package/dist/composablepattern/document.js +2 -35
- package/dist/local-tools/fileTools.d.ts +26 -0
- package/dist/local-tools/fileTools.js +162 -0
- package/dist/processor/types/interfaces.d.ts +44 -21
- package/dist/processor-pieces/additionalModifiers/argumentProcessorModifier.d.ts +13 -0
- package/dist/processor-pieces/additionalModifiers/argumentProcessorModifier.js +68 -0
- package/dist/processor-pieces/additionalModifiers/atFileProcessorModifier.d.ts +15 -0
- package/dist/processor-pieces/additionalModifiers/atFileProcessorModifier.js +212 -0
- package/dist/processor-pieces/additionalModifiers/chatCompressionModifier.d.ts +19 -0
- package/dist/processor-pieces/additionalModifiers/chatCompressionModifier.js +110 -0
- package/dist/processor-pieces/additionalModifiers/chatRecordingModifier.d.ts +23 -0
- package/dist/processor-pieces/additionalModifiers/chatRecordingModifier.js +173 -0
- package/dist/processor-pieces/additionalModifiers/coreSystemPromptModifier.d.ts +14 -0
- package/dist/processor-pieces/additionalModifiers/coreSystemPromptModifier.js +93 -0
- package/dist/processor-pieces/additionalModifiers/directoryContextModifier.d.ts +30 -0
- package/dist/processor-pieces/additionalModifiers/directoryContextModifier.js +330 -0
- package/dist/processor-pieces/additionalModifiers/environmentContextModifier.d.ts +26 -0
- package/dist/processor-pieces/additionalModifiers/environmentContextModifier.js +269 -0
- package/dist/processor-pieces/additionalModifiers/fallbackHandlerModifier.d.ts +38 -0
- package/dist/processor-pieces/additionalModifiers/fallbackHandlerModifier.js +217 -0
- package/dist/processor-pieces/additionalModifiers/ideContextModifier.d.ts +34 -0
- package/dist/processor-pieces/additionalModifiers/ideContextModifier.js +147 -0
- package/dist/processor-pieces/additionalModifiers/index.d.ts +12 -0
- package/dist/processor-pieces/additionalModifiers/index.js +28 -0
- package/dist/processor-pieces/additionalModifiers/loopDetectionModifier.d.ts +29 -0
- package/dist/processor-pieces/additionalModifiers/loopDetectionModifier.js +155 -0
- package/dist/processor-pieces/additionalModifiers/memoryImportModifier.d.ts +15 -0
- package/dist/processor-pieces/additionalModifiers/memoryImportModifier.js +129 -0
- package/dist/processor-pieces/additionalModifiers/shellProcessorModifier.d.ts +25 -0
- package/dist/processor-pieces/additionalModifiers/shellProcessorModifier.js +169 -0
- package/dist/processor-pieces/additionalModifiers/toolInjectionModifier.d.ts +20 -0
- package/dist/processor-pieces/additionalModifiers/toolInjectionModifier.js +152 -0
- package/dist/processor-pieces/messageModifiers/addCurrentDirectoryRootFilesModifier.d.ts +7 -0
- package/dist/processor-pieces/messageModifiers/addCurrentDirectoryRootFilesModifier.js +93 -0
- package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.d.ts +14 -5
- package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.js +41 -40
- package/dist/processor-pieces/messageModifiers/advancedSystemInstructionMessageModifier.d.ts +9 -0
- package/dist/processor-pieces/messageModifiers/advancedSystemInstructionMessageModifier.js +77 -0
- package/dist/processor-pieces/messageModifiers/atFileProcessorModifier.js +5 -5
- package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.d.ts +5 -13
- package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.js +50 -58
- package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.d.ts +6 -8
- package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.js +66 -22
- package/dist/processor-pieces/messageModifiers/chatCompressionModifier.d.ts +19 -0
- package/dist/processor-pieces/messageModifiers/chatCompressionModifier.js +110 -0
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.example.d.ts +20 -0
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.example.js +124 -0
- package/dist/processor-pieces/messageModifiers/environmentContext.d.ts +41 -0
- package/dist/processor-pieces/messageModifiers/environmentContext.js +355 -0
- package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.d.ts +3 -2
- package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.js +1 -2
- package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.d.ts +3 -2
- package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.js +1 -2
- package/dist/processor-pieces/messageModifiers/mentionedFilesModifier.d.ts +23 -0
- package/dist/processor-pieces/messageModifiers/mentionedFilesModifier.js +207 -0
- package/dist/processor-pieces/messageModifiers/simpleMessageModifier.d.ts +7 -0
- package/dist/processor-pieces/messageModifiers/simpleMessageModifier.js +26 -0
- package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.d.ts +18 -2
- package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.js +291 -93
- package/dist/processor-pieces/postInference/llmOutputValidityProcessor.d.ts +1 -0
- package/dist/processor-pieces/postInference/llmOutputValidityProcessor.js +2 -0
- package/dist/processor-pieces/postInferenceProcessors/checkForNoToolCall.d.ts +7 -0
- package/dist/processor-pieces/postInferenceProcessors/checkForNoToolCall.js +24 -0
- package/dist/processor-pieces/postToolCall/advancedLoopDetectionProcessor.d.ts +41 -0
- package/dist/processor-pieces/postToolCall/advancedLoopDetectionProcessor.js +197 -0
- package/dist/processor-pieces/postToolCall/chatCompressionProcessor.d.ts +26 -0
- package/dist/processor-pieces/postToolCall/chatCompressionProcessor.js +90 -0
- package/dist/processor-pieces/postToolCall/chatRecordingProcessor.d.ts +81 -0
- package/dist/processor-pieces/postToolCall/chatRecordingProcessor.js +329 -0
- package/dist/processor-pieces/postToolCall/contextManagementProcessor.d.ts +40 -0
- package/dist/processor-pieces/postToolCall/contextManagementProcessor.js +272 -0
- package/dist/processor-pieces/postToolCall/conversationCompactorProcessor.d.ts +49 -0
- package/dist/processor-pieces/postToolCall/conversationCompactorProcessor.js +291 -0
- package/dist/processor-pieces/postToolCall/conversationContinuityProcessor.d.ts +49 -0
- package/dist/processor-pieces/postToolCall/conversationContinuityProcessor.js +293 -0
- package/dist/processor-pieces/postToolCall/followUpConversationProcessor.d.ts +47 -0
- package/dist/processor-pieces/postToolCall/followUpConversationProcessor.js +249 -0
- package/dist/processor-pieces/postToolCall/loopDetectionProcessor.d.ts +30 -0
- package/dist/processor-pieces/postToolCall/loopDetectionProcessor.js +137 -0
- package/dist/processor-pieces/postToolCall/responseValidationProcessor.d.ts +30 -0
- package/dist/processor-pieces/postToolCall/responseValidationProcessor.js +228 -0
- package/dist/processor-pieces/postToolCall/telemetryProcessor.d.ts +99 -0
- package/dist/processor-pieces/postToolCall/telemetryProcessor.js +278 -0
- package/dist/processor-pieces/postToolCall/tokenManagementProcessor.d.ts +37 -0
- package/dist/processor-pieces/postToolCall/tokenManagementProcessor.js +178 -0
- package/dist/processor-pieces/postToolCall/toolExecutionProcessor.d.ts +43 -0
- package/dist/processor-pieces/postToolCall/toolExecutionProcessor.js +207 -0
- package/dist/processor-pieces/preInference/conversationCompactorProcessor.d.ts +52 -0
- package/dist/processor-pieces/preInference/conversationCompactorProcessor.js +264 -0
- package/dist/processor-pieces/preInference/conversationContinuityProcessor.d.ts +49 -0
- package/dist/processor-pieces/preInference/conversationContinuityProcessor.js +293 -0
- package/dist/processor-pieces/preInferenceProcessors/conversationCompaction.d.ts +7 -0
- package/dist/processor-pieces/preInferenceProcessors/conversationCompaction.js +31 -0
- package/dist/processor-pieces/preToolCall/index.d.ts +9 -0
- package/dist/processor-pieces/preToolCall/index.js +18 -0
- package/dist/processor-pieces/preToolCall/localToolInterceptorProcessor.d.ts +58 -0
- package/dist/processor-pieces/preToolCall/localToolInterceptorProcessor.js +444 -0
- package/dist/processor-pieces/preToolCall/toolParameterModifierProcessor.d.ts +44 -0
- package/dist/processor-pieces/preToolCall/toolParameterModifierProcessor.js +367 -0
- package/dist/processor-pieces/preToolCall/toolValidationProcessor.d.ts +58 -0
- package/dist/processor-pieces/preToolCall/toolValidationProcessor.js +391 -0
- package/dist/unified/agent/agent.js +2 -2
- package/dist/unified/agent/tool.d.ts +99 -0
- package/dist/unified/agent/tool.js +440 -0
- package/dist/unified/base/initialPromptGenerator.js +5 -5
- package/dist/unified/base/responseExecutor.js +6 -6
- package/dist/unified/examples/agentExample.d.ts +46 -0
- package/dist/unified/examples/agentExample.js +464 -0
- package/dist/unified/examples/documentationPatternExample.d.ts +10 -0
- package/dist/unified/examples/documentationPatternExample.js +385 -0
- package/dist/unified/examples/followUpProcessorsExample.d.ts +45 -0
- package/dist/unified/examples/followUpProcessorsExample.js +311 -0
- package/dist/unified/examples/orchestratorExample.d.ts +16 -0
- package/dist/unified/examples/orchestratorExample.js +415 -0
- package/dist/unified/examples/preToolCallProcessorsExample.d.ts +36 -0
- package/dist/unified/examples/preToolCallProcessorsExample.js +458 -0
- package/dist/unified/examples/workflowExample.d.ts +12 -0
- package/dist/unified/examples/workflowExample.js +497 -0
- package/dist/unified/orchestrator/orchestrator.d.ts +260 -0
- package/dist/unified/orchestrator/orchestrator.js +519 -0
- package/package.json +15 -15
- package/dist/agent.d.ts +0 -86
- package/dist/agent.js +0 -324
- package/dist/followupquestionbuilder.d.ts +0 -75
- package/dist/followupquestionbuilder.js +0 -197
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -23
- package/dist/llmoutputhandler.d.ts +0 -102
- package/dist/llmoutputhandler.js +0 -452
- package/dist/promptbuilder.d.ts +0 -382
- package/dist/promptbuilder.js +0 -805
- package/dist/systemprompt.d.ts +0 -20
- package/dist/systemprompt.js +0 -48
- package/dist/taskInstruction.d.ts +0 -37
- package/dist/taskInstruction.js +0 -57
- package/dist/usermessage.d.ts +0 -70
- package/dist/usermessage.js +0 -123
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalToolInterceptorProcessor = void 0;
|
|
4
|
+
const processor_1 = require("../../processor");
|
|
5
|
+
/**
|
|
6
|
+
* Processor that intercepts specific tool calls and handles them locally
|
|
7
|
+
* instead of sending them to external tool execution services.
|
|
8
|
+
*/
|
|
9
|
+
class LocalToolInterceptorProcessor extends processor_1.BaseProcessor {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
super(options);
|
|
12
|
+
this.localTools = options.localTools || new Map();
|
|
13
|
+
this.enableCustomToolHandling = options.enableCustomToolHandling !== false;
|
|
14
|
+
this.enableToolDelegation = options.enableToolDelegation !== false;
|
|
15
|
+
this.defaultExecutionTimeout = options.defaultExecutionTimeout || 30000; // 30 seconds
|
|
16
|
+
this.enableLogging = options.enableLogging !== false;
|
|
17
|
+
}
|
|
18
|
+
async processInput(input) {
|
|
19
|
+
var _a;
|
|
20
|
+
try {
|
|
21
|
+
const { message, context } = input;
|
|
22
|
+
// Check if this is a pre-tool call processing
|
|
23
|
+
if (!(context === null || context === void 0 ? void 0 : context.preToolCallProcessing)) {
|
|
24
|
+
return [this.createEvent('LocalToolInterceptionSkipped', {
|
|
25
|
+
reason: 'Not a pre-tool call processing context'
|
|
26
|
+
})];
|
|
27
|
+
}
|
|
28
|
+
const toolName = context.toolName;
|
|
29
|
+
const toolInput = context.toolInput;
|
|
30
|
+
const toolUseId = context.toolUseId;
|
|
31
|
+
// Check if we should intercept this tool
|
|
32
|
+
const shouldIntercept = this.shouldInterceptTool(toolName, toolInput, context);
|
|
33
|
+
if (!shouldIntercept) {
|
|
34
|
+
return [this.createEvent('LocalToolInterceptionSkipped', {
|
|
35
|
+
toolName,
|
|
36
|
+
reason: 'Tool not configured for local interception'
|
|
37
|
+
})];
|
|
38
|
+
}
|
|
39
|
+
// Execute the tool locally
|
|
40
|
+
const startTime = Date.now();
|
|
41
|
+
const result = await this.executeLocalTool(toolName, toolInput, context);
|
|
42
|
+
const executionTime = Date.now() - startTime;
|
|
43
|
+
const interceptorInfo = {
|
|
44
|
+
toolName,
|
|
45
|
+
intercepted: true,
|
|
46
|
+
executionMethod: this.getExecutionMethod(toolName),
|
|
47
|
+
executionTime,
|
|
48
|
+
result
|
|
49
|
+
};
|
|
50
|
+
return [
|
|
51
|
+
this.createEvent('LocalToolIntercepted', interceptorInfo),
|
|
52
|
+
this.createEvent('ToolIntercepted', {
|
|
53
|
+
result,
|
|
54
|
+
toolName,
|
|
55
|
+
executionTime,
|
|
56
|
+
userRejected: false
|
|
57
|
+
})
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
console.error('Error in LocalToolInterceptorProcessor:', error);
|
|
62
|
+
return [this.createEvent('LocalToolInterceptionError', {
|
|
63
|
+
error: error instanceof Error ? error.message : String(error),
|
|
64
|
+
toolName: (_a = input.context) === null || _a === void 0 ? void 0 : _a.toolName
|
|
65
|
+
})];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
shouldInterceptTool(toolName, toolInput, context) {
|
|
69
|
+
// Check if tool is in local tools registry
|
|
70
|
+
if (this.localTools.has(toolName)) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
// Check for custom tool patterns
|
|
74
|
+
if (this.enableCustomToolHandling) {
|
|
75
|
+
// Intercept tools with specific prefixes
|
|
76
|
+
const customPrefixes = ['local--', 'custom--', 'internal--'];
|
|
77
|
+
if (customPrefixes.some(prefix => toolName.startsWith(prefix))) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
// Intercept specific tool types
|
|
81
|
+
const localToolTypes = [
|
|
82
|
+
'file_read_local',
|
|
83
|
+
'file_write_local',
|
|
84
|
+
'execute_script_local',
|
|
85
|
+
'validate_data',
|
|
86
|
+
'transform_data',
|
|
87
|
+
'calculate',
|
|
88
|
+
'format_output'
|
|
89
|
+
];
|
|
90
|
+
if (localToolTypes.includes(toolName)) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
async executeLocalTool(toolName, toolInput, context) {
|
|
97
|
+
// Check if tool is in local registry
|
|
98
|
+
if (this.localTools.has(toolName)) {
|
|
99
|
+
const handler = this.localTools.get(toolName);
|
|
100
|
+
return await this.executeToolHandler(handler, toolInput, context);
|
|
101
|
+
}
|
|
102
|
+
// Handle custom tool patterns
|
|
103
|
+
if (toolName.startsWith('local--')) {
|
|
104
|
+
return await this.handleLocalPrefixTool(toolName, toolInput, context);
|
|
105
|
+
}
|
|
106
|
+
if (toolName.startsWith('custom--')) {
|
|
107
|
+
return await this.handleCustomPrefixTool(toolName, toolInput, context);
|
|
108
|
+
}
|
|
109
|
+
if (toolName.startsWith('internal--')) {
|
|
110
|
+
return await this.handleInternalPrefixTool(toolName, toolInput, context);
|
|
111
|
+
}
|
|
112
|
+
// Handle specific local tool types
|
|
113
|
+
switch (toolName) {
|
|
114
|
+
case 'file_read_local':
|
|
115
|
+
return await this.handleLocalFileRead(toolInput, context);
|
|
116
|
+
case 'file_write_local':
|
|
117
|
+
return await this.handleLocalFileWrite(toolInput, context);
|
|
118
|
+
case 'execute_script_local':
|
|
119
|
+
return await this.handleLocalScriptExecution(toolInput, context);
|
|
120
|
+
case 'validate_data':
|
|
121
|
+
return await this.handleDataValidation(toolInput, context);
|
|
122
|
+
case 'transform_data':
|
|
123
|
+
return await this.handleDataTransformation(toolInput, context);
|
|
124
|
+
case 'calculate':
|
|
125
|
+
return await this.handleCalculation(toolInput, context);
|
|
126
|
+
case 'format_output':
|
|
127
|
+
return await this.handleOutputFormatting(toolInput, context);
|
|
128
|
+
default:
|
|
129
|
+
throw new Error(`Unknown local tool: ${toolName}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async executeToolHandler(handler, toolInput, context) {
|
|
133
|
+
// Validate input if validator is provided
|
|
134
|
+
if (handler.validate && !handler.validate(toolInput)) {
|
|
135
|
+
throw new Error(`Invalid input for tool ${handler.name}`);
|
|
136
|
+
}
|
|
137
|
+
// Execute with timeout
|
|
138
|
+
const timeout = handler.timeout || this.defaultExecutionTimeout;
|
|
139
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
140
|
+
setTimeout(() => reject(new Error(`Tool execution timeout: ${handler.name}`)), timeout);
|
|
141
|
+
});
|
|
142
|
+
const executionPromise = Promise.resolve(handler.execute(toolInput, context));
|
|
143
|
+
try {
|
|
144
|
+
const result = await Promise.race([executionPromise, timeoutPromise]);
|
|
145
|
+
return typeof result === 'string' ? result : JSON.stringify(result);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
throw new Error(`Tool execution failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async handleLocalPrefixTool(toolName, toolInput, context) {
|
|
152
|
+
const actualToolName = toolName.replace('local--', '');
|
|
153
|
+
if (this.enableLogging) {
|
|
154
|
+
console.log(`[LocalToolInterceptor] Handling local tool: ${actualToolName}`);
|
|
155
|
+
}
|
|
156
|
+
// Simple local tool implementations
|
|
157
|
+
switch (actualToolName) {
|
|
158
|
+
case 'echo':
|
|
159
|
+
return `Local echo: ${JSON.stringify(toolInput)}`;
|
|
160
|
+
case 'timestamp':
|
|
161
|
+
return `Current timestamp: ${new Date().toISOString()}`;
|
|
162
|
+
case 'uuid':
|
|
163
|
+
return `Generated UUID: ${this.generateUUID()}`;
|
|
164
|
+
case 'hash':
|
|
165
|
+
return `Hash of input: ${this.simpleHash(JSON.stringify(toolInput))}`;
|
|
166
|
+
default:
|
|
167
|
+
return `Local tool executed: ${actualToolName} with input: ${JSON.stringify(toolInput)}`;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
async handleCustomPrefixTool(toolName, toolInput, context) {
|
|
171
|
+
const actualToolName = toolName.replace('custom--', '');
|
|
172
|
+
if (this.enableLogging) {
|
|
173
|
+
console.log(`[LocalToolInterceptor] Handling custom tool: ${actualToolName}`);
|
|
174
|
+
}
|
|
175
|
+
// Custom tool implementations can be added here
|
|
176
|
+
return `Custom tool ${actualToolName} executed with result: ${JSON.stringify(toolInput)}`;
|
|
177
|
+
}
|
|
178
|
+
async handleInternalPrefixTool(toolName, toolInput, context) {
|
|
179
|
+
const actualToolName = toolName.replace('internal--', '');
|
|
180
|
+
if (this.enableLogging) {
|
|
181
|
+
console.log(`[LocalToolInterceptor] Handling internal tool: ${actualToolName}`);
|
|
182
|
+
}
|
|
183
|
+
// Internal tool implementations
|
|
184
|
+
switch (actualToolName) {
|
|
185
|
+
case 'context_info':
|
|
186
|
+
return `Context information: ${JSON.stringify(context, null, 2)}`;
|
|
187
|
+
case 'memory_usage':
|
|
188
|
+
return `Memory usage: ${JSON.stringify(process.memoryUsage(), null, 2)}`;
|
|
189
|
+
case 'environment':
|
|
190
|
+
return `Environment: ${process.env.NODE_ENV || 'development'}`;
|
|
191
|
+
default:
|
|
192
|
+
return `Internal tool ${actualToolName} executed`;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async handleLocalFileRead(toolInput, context) {
|
|
196
|
+
const filePath = toolInput.filePath || toolInput.path;
|
|
197
|
+
if (!filePath) {
|
|
198
|
+
throw new Error('File path is required for local file read');
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
const fs = await import('fs/promises');
|
|
202
|
+
const content = await fs.readFile(filePath, 'utf-8');
|
|
203
|
+
return `File read successfully: ${filePath}\nContent length: ${content.length} characters`;
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
throw new Error(`Failed to read file ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async handleLocalFileWrite(toolInput, context) {
|
|
210
|
+
const filePath = toolInput.filePath || toolInput.path;
|
|
211
|
+
const content = toolInput.content;
|
|
212
|
+
if (!filePath || content === undefined) {
|
|
213
|
+
throw new Error('File path and content are required for local file write');
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const fs = await import('fs/promises');
|
|
217
|
+
await fs.writeFile(filePath, content, 'utf-8');
|
|
218
|
+
return `File written successfully: ${filePath}\nContent length: ${content.length} characters`;
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
throw new Error(`Failed to write file ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async handleLocalScriptExecution(toolInput, context) {
|
|
225
|
+
const script = toolInput.script;
|
|
226
|
+
const args = toolInput.args || [];
|
|
227
|
+
if (!script) {
|
|
228
|
+
throw new Error('Script is required for local script execution');
|
|
229
|
+
}
|
|
230
|
+
// For security, only allow specific safe scripts
|
|
231
|
+
const allowedScripts = ['echo', 'date', 'pwd', 'ls'];
|
|
232
|
+
if (!allowedScripts.includes(script)) {
|
|
233
|
+
throw new Error(`Script not allowed for local execution: ${script}`);
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
const { exec } = await import('child_process');
|
|
237
|
+
const { promisify } = await import('util');
|
|
238
|
+
const execAsync = promisify(exec);
|
|
239
|
+
const command = `${script} ${args.join(' ')}`;
|
|
240
|
+
const { stdout, stderr } = await execAsync(command);
|
|
241
|
+
return `Script executed: ${command}\nOutput: ${stdout}${stderr ? `\nError: ${stderr}` : ''}`;
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
throw new Error(`Script execution failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async handleDataValidation(toolInput, context) {
|
|
248
|
+
const data = toolInput.data;
|
|
249
|
+
const schema = toolInput.schema;
|
|
250
|
+
if (!data) {
|
|
251
|
+
throw new Error('Data is required for validation');
|
|
252
|
+
}
|
|
253
|
+
// Simple validation logic
|
|
254
|
+
const validationResults = {
|
|
255
|
+
isValid: true,
|
|
256
|
+
errors: [],
|
|
257
|
+
warnings: []
|
|
258
|
+
};
|
|
259
|
+
if (schema) {
|
|
260
|
+
// Basic schema validation
|
|
261
|
+
if (schema.required && Array.isArray(schema.required)) {
|
|
262
|
+
schema.required.forEach((field) => {
|
|
263
|
+
if (!(field in data)) {
|
|
264
|
+
validationResults.isValid = false;
|
|
265
|
+
validationResults.errors.push(`Required field missing: ${field}`);
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return `Data validation completed: ${validationResults.isValid ? 'VALID' : 'INVALID'}\n${JSON.stringify(validationResults, null, 2)}`;
|
|
271
|
+
}
|
|
272
|
+
async handleDataTransformation(toolInput, context) {
|
|
273
|
+
const data = toolInput.data;
|
|
274
|
+
const transformation = toolInput.transformation;
|
|
275
|
+
if (!data || !transformation) {
|
|
276
|
+
throw new Error('Data and transformation are required');
|
|
277
|
+
}
|
|
278
|
+
let result = data;
|
|
279
|
+
// Apply transformations
|
|
280
|
+
switch (transformation.type) {
|
|
281
|
+
case 'uppercase':
|
|
282
|
+
result = typeof data === 'string' ? data.toUpperCase() : JSON.stringify(data).toUpperCase();
|
|
283
|
+
break;
|
|
284
|
+
case 'lowercase':
|
|
285
|
+
result = typeof data === 'string' ? data.toLowerCase() : JSON.stringify(data).toLowerCase();
|
|
286
|
+
break;
|
|
287
|
+
case 'reverse':
|
|
288
|
+
result = typeof data === 'string' ? data.split('').reverse().join('') : JSON.stringify(data);
|
|
289
|
+
break;
|
|
290
|
+
case 'sort':
|
|
291
|
+
if (Array.isArray(data)) {
|
|
292
|
+
result = [...data].sort();
|
|
293
|
+
}
|
|
294
|
+
break;
|
|
295
|
+
default:
|
|
296
|
+
result = data;
|
|
297
|
+
}
|
|
298
|
+
return `Data transformation completed: ${transformation.type}\nResult: ${JSON.stringify(result)}`;
|
|
299
|
+
}
|
|
300
|
+
async handleCalculation(toolInput, context) {
|
|
301
|
+
const expression = toolInput.expression;
|
|
302
|
+
const values = toolInput.values || {};
|
|
303
|
+
if (!expression) {
|
|
304
|
+
throw new Error('Expression is required for calculation');
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
// Simple calculator for basic operations
|
|
308
|
+
let result;
|
|
309
|
+
if (expression.includes('+')) {
|
|
310
|
+
const [a, b] = expression.split('+').map(x => parseFloat(x.trim()));
|
|
311
|
+
result = a + b;
|
|
312
|
+
}
|
|
313
|
+
else if (expression.includes('-')) {
|
|
314
|
+
const [a, b] = expression.split('-').map(x => parseFloat(x.trim()));
|
|
315
|
+
result = a - b;
|
|
316
|
+
}
|
|
317
|
+
else if (expression.includes('*')) {
|
|
318
|
+
const [a, b] = expression.split('*').map(x => parseFloat(x.trim()));
|
|
319
|
+
result = a * b;
|
|
320
|
+
}
|
|
321
|
+
else if (expression.includes('/')) {
|
|
322
|
+
const [a, b] = expression.split('/').map(x => parseFloat(x.trim()));
|
|
323
|
+
result = a / b;
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
result = parseFloat(expression);
|
|
327
|
+
}
|
|
328
|
+
return `Calculation result: ${expression} = ${result}`;
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
throw new Error(`Calculation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
async handleOutputFormatting(toolInput, context) {
|
|
335
|
+
const data = toolInput.data;
|
|
336
|
+
const format = toolInput.format || 'json';
|
|
337
|
+
if (!data) {
|
|
338
|
+
throw new Error('Data is required for output formatting');
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
let formattedOutput;
|
|
342
|
+
switch (format.toLowerCase()) {
|
|
343
|
+
case 'json':
|
|
344
|
+
formattedOutput = JSON.stringify(data, null, 2);
|
|
345
|
+
break;
|
|
346
|
+
case 'yaml':
|
|
347
|
+
// Simple YAML-like formatting
|
|
348
|
+
formattedOutput = this.toYamlLike(data);
|
|
349
|
+
break;
|
|
350
|
+
case 'csv':
|
|
351
|
+
formattedOutput = this.toCsv(data);
|
|
352
|
+
break;
|
|
353
|
+
case 'table':
|
|
354
|
+
formattedOutput = this.toTable(data);
|
|
355
|
+
break;
|
|
356
|
+
default:
|
|
357
|
+
formattedOutput = String(data);
|
|
358
|
+
}
|
|
359
|
+
return `Output formatted as ${format}:\n${formattedOutput}`;
|
|
360
|
+
}
|
|
361
|
+
catch (error) {
|
|
362
|
+
throw new Error(`Output formatting failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
getExecutionMethod(toolName) {
|
|
366
|
+
if (this.localTools.has(toolName)) {
|
|
367
|
+
return 'local';
|
|
368
|
+
}
|
|
369
|
+
if (toolName.startsWith('custom--')) {
|
|
370
|
+
return 'custom';
|
|
371
|
+
}
|
|
372
|
+
return 'delegated';
|
|
373
|
+
}
|
|
374
|
+
generateUUID() {
|
|
375
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
376
|
+
const r = Math.random() * 16 | 0;
|
|
377
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
378
|
+
return v.toString(16);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
simpleHash(str) {
|
|
382
|
+
let hash = 0;
|
|
383
|
+
for (let i = 0; i < str.length; i++) {
|
|
384
|
+
const char = str.charCodeAt(i);
|
|
385
|
+
hash = ((hash << 5) - hash) + char;
|
|
386
|
+
hash = hash & hash; // Convert to 32-bit integer
|
|
387
|
+
}
|
|
388
|
+
return hash.toString(16);
|
|
389
|
+
}
|
|
390
|
+
toYamlLike(data, indent = 0) {
|
|
391
|
+
const spaces = ' '.repeat(indent);
|
|
392
|
+
if (typeof data === 'object' && data !== null) {
|
|
393
|
+
if (Array.isArray(data)) {
|
|
394
|
+
return data.map(item => `${spaces}- ${this.toYamlLike(item, indent + 1)}`).join('\n');
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
return Object.entries(data)
|
|
398
|
+
.map(([key, value]) => `${spaces}${key}: ${this.toYamlLike(value, indent + 1)}`)
|
|
399
|
+
.join('\n');
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return String(data);
|
|
403
|
+
}
|
|
404
|
+
toCsv(data) {
|
|
405
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
406
|
+
const headers = Object.keys(data[0]);
|
|
407
|
+
const csvHeaders = headers.join(',');
|
|
408
|
+
const csvRows = data.map(row => headers.map(header => JSON.stringify(row[header] || '')).join(','));
|
|
409
|
+
return [csvHeaders, ...csvRows].join('\n');
|
|
410
|
+
}
|
|
411
|
+
return JSON.stringify(data);
|
|
412
|
+
}
|
|
413
|
+
toTable(data) {
|
|
414
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
415
|
+
const headers = Object.keys(data[0]);
|
|
416
|
+
const headerRow = '| ' + headers.join(' | ') + ' |';
|
|
417
|
+
const separatorRow = '|' + headers.map(() => '---').join('|') + '|';
|
|
418
|
+
const dataRows = data.map(row => '| ' + headers.map(header => String(row[header] || '')).join(' | ') + ' |');
|
|
419
|
+
return [headerRow, separatorRow, ...dataRows].join('\n');
|
|
420
|
+
}
|
|
421
|
+
return JSON.stringify(data, null, 2);
|
|
422
|
+
}
|
|
423
|
+
// Public methods for tool management
|
|
424
|
+
addLocalTool(handler) {
|
|
425
|
+
this.localTools.set(handler.name, handler);
|
|
426
|
+
if (this.enableLogging) {
|
|
427
|
+
console.log(`[LocalToolInterceptor] Added local tool: ${handler.name}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
removeLocalTool(toolName) {
|
|
431
|
+
const removed = this.localTools.delete(toolName);
|
|
432
|
+
if (removed && this.enableLogging) {
|
|
433
|
+
console.log(`[LocalToolInterceptor] Removed local tool: ${toolName}`);
|
|
434
|
+
}
|
|
435
|
+
return removed;
|
|
436
|
+
}
|
|
437
|
+
getLocalTools() {
|
|
438
|
+
return Array.from(this.localTools.keys());
|
|
439
|
+
}
|
|
440
|
+
hasLocalTool(toolName) {
|
|
441
|
+
return this.localTools.has(toolName);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
exports.LocalToolInterceptorProcessor = LocalToolInterceptorProcessor;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { BaseProcessor, ProcessorInput, ProcessorOutput } from '../../processor';
|
|
2
|
+
export interface ToolParameterModificationInfo {
|
|
3
|
+
toolName: string;
|
|
4
|
+
originalParameters: any;
|
|
5
|
+
modifiedParameters: any;
|
|
6
|
+
modificationsApplied: string[];
|
|
7
|
+
transformationsApplied: string[];
|
|
8
|
+
}
|
|
9
|
+
export interface ParameterTransformation {
|
|
10
|
+
name: string;
|
|
11
|
+
condition?: (toolName: string, params: any) => boolean;
|
|
12
|
+
transform: (params: any, context?: Record<string, any>) => any;
|
|
13
|
+
description?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface ToolParameterModifierProcessorOptions {
|
|
16
|
+
enableParameterTransformation?: boolean;
|
|
17
|
+
enableParameterEnrichment?: boolean;
|
|
18
|
+
enableParameterSanitization?: boolean;
|
|
19
|
+
transformations?: ParameterTransformation[];
|
|
20
|
+
defaultTransformations?: boolean;
|
|
21
|
+
enableLogging?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Processor that modifies tool parameters before execution, applying transformations,
|
|
25
|
+
* enrichments, and sanitization to improve tool execution.
|
|
26
|
+
*/
|
|
27
|
+
export declare class ToolParameterModifierProcessor extends BaseProcessor {
|
|
28
|
+
private readonly enableParameterTransformation;
|
|
29
|
+
private readonly enableParameterEnrichment;
|
|
30
|
+
private readonly enableParameterSanitization;
|
|
31
|
+
private readonly transformations;
|
|
32
|
+
private readonly enableLogging;
|
|
33
|
+
constructor(options?: ToolParameterModifierProcessorOptions);
|
|
34
|
+
processInput(input: ProcessorInput): Promise<ProcessorOutput[]>;
|
|
35
|
+
private modifyParameters;
|
|
36
|
+
private sanitizeParameters;
|
|
37
|
+
private enrichParameters;
|
|
38
|
+
private getDefaultTransformations;
|
|
39
|
+
private deepClone;
|
|
40
|
+
addTransformation(transformation: ParameterTransformation): void;
|
|
41
|
+
removeTransformation(name: string): boolean;
|
|
42
|
+
getTransformations(): string[];
|
|
43
|
+
hasTransformation(name: string): boolean;
|
|
44
|
+
}
|