@codebolt/agent 2.2.2 → 2.2.5
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 +5 -5
- 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,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.MemoryImportModifier = void 0;
|
|
37
|
+
const base_1 = require("../base");
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
class MemoryImportModifier extends base_1.BaseMessageModifier {
|
|
41
|
+
constructor(options = {}) {
|
|
42
|
+
super();
|
|
43
|
+
this.options = {
|
|
44
|
+
enableMemoryImport: options.enableMemoryImport !== false,
|
|
45
|
+
maxFileSize: options.maxFileSize || 1024 * 1024, // 1MB
|
|
46
|
+
allowedExtensions: options.allowedExtensions || ['.md', '.txt', '.json', '.yml', '.yaml', '.xml'],
|
|
47
|
+
basePath: options.basePath || process.cwd(),
|
|
48
|
+
maxImports: options.maxImports || 10
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async modify(originalRequest, createdMessage) {
|
|
52
|
+
try {
|
|
53
|
+
if (!this.options.enableMemoryImport) {
|
|
54
|
+
return createdMessage;
|
|
55
|
+
}
|
|
56
|
+
// Get the user message content to process
|
|
57
|
+
const userMessage = createdMessage.message.messages.find(msg => msg.role === 'user');
|
|
58
|
+
if (!userMessage || typeof userMessage.content !== 'string') {
|
|
59
|
+
return createdMessage;
|
|
60
|
+
}
|
|
61
|
+
// Look for @path/to/file patterns (memory import syntax)
|
|
62
|
+
const memoryImportPattern = /@([^\s@]+(?:\.[a-zA-Z0-9]+)?)/g;
|
|
63
|
+
let content = userMessage.content;
|
|
64
|
+
const processedImports = [];
|
|
65
|
+
let match;
|
|
66
|
+
let importCount = 0;
|
|
67
|
+
while ((match = memoryImportPattern.exec(userMessage.content)) !== null && importCount < this.options.maxImports) {
|
|
68
|
+
const filePath = match[1];
|
|
69
|
+
const fullPath = path.isAbsolute(filePath) ? filePath : path.join(this.options.basePath, filePath);
|
|
70
|
+
try {
|
|
71
|
+
// Check if file exists and is allowed
|
|
72
|
+
const stats = await fs.promises.stat(fullPath);
|
|
73
|
+
if (!stats.isFile()) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (stats.size > this.options.maxFileSize) {
|
|
77
|
+
content = content.replace(match[0], `[File too large: ${filePath}]`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const ext = path.extname(filePath);
|
|
81
|
+
if (!this.options.allowedExtensions.includes(ext)) {
|
|
82
|
+
content = content.replace(match[0], `[Unsupported file type: ${filePath}]`);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
// Read and import the file content
|
|
86
|
+
const fileContent = await fs.promises.readFile(fullPath, 'utf-8');
|
|
87
|
+
const importedContent = `--- Content from ${filePath} ---\n${fileContent}\n--- End of ${filePath} ---`;
|
|
88
|
+
content = content.replace(match[0], importedContent);
|
|
89
|
+
processedImports.push(filePath);
|
|
90
|
+
importCount++;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
// Replace with error message if file can't be read
|
|
94
|
+
content = content.replace(match[0], `[Error reading ${filePath}: ${error instanceof Error ? error.message : 'unknown error'}]`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (processedImports.length === 0) {
|
|
98
|
+
return createdMessage;
|
|
99
|
+
}
|
|
100
|
+
// Update the user message with processed content
|
|
101
|
+
const messages = createdMessage.message.messages.map(msg => {
|
|
102
|
+
if (msg.role === 'user' && msg === userMessage) {
|
|
103
|
+
return {
|
|
104
|
+
...msg,
|
|
105
|
+
content
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return msg;
|
|
109
|
+
});
|
|
110
|
+
return Promise.resolve({
|
|
111
|
+
message: {
|
|
112
|
+
...createdMessage.message,
|
|
113
|
+
messages
|
|
114
|
+
},
|
|
115
|
+
metadata: {
|
|
116
|
+
...createdMessage.metadata,
|
|
117
|
+
memoryImportsProcessed: true,
|
|
118
|
+
importedFiles: processedImports,
|
|
119
|
+
totalImports: processedImports.length
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
console.error('Error in MemoryImportModifier:', error);
|
|
125
|
+
return createdMessage;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.MemoryImportModifier = MemoryImportModifier;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ProcessedMessage } from "@codebolt/types/agent";
|
|
2
|
+
import { BaseMessageModifier } from "../base";
|
|
3
|
+
import { FlatUserMessage } from "@codebolt/types/sdk";
|
|
4
|
+
export interface ShellProcessorOptions {
|
|
5
|
+
enableShellExecution?: boolean;
|
|
6
|
+
maxExecutionTime?: number;
|
|
7
|
+
allowedCommands?: string[];
|
|
8
|
+
blockedCommands?: string[];
|
|
9
|
+
workingDirectory?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare class ShellProcessorModifier extends BaseMessageModifier {
|
|
12
|
+
private readonly options;
|
|
13
|
+
private readonly execAsync;
|
|
14
|
+
private readonly SHELL_TRIGGER;
|
|
15
|
+
private readonly SHELL_CLOSE;
|
|
16
|
+
private readonly ARGS_PLACEHOLDER;
|
|
17
|
+
constructor(options?: ShellProcessorOptions);
|
|
18
|
+
modify(originalRequest: FlatUserMessage, createdMessage: ProcessedMessage): Promise<ProcessedMessage>;
|
|
19
|
+
private processShellInjections;
|
|
20
|
+
private extractShellInjections;
|
|
21
|
+
private validateCommand;
|
|
22
|
+
private executeCommand;
|
|
23
|
+
private escapeShellArg;
|
|
24
|
+
private escapeRegex;
|
|
25
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ShellProcessorModifier = void 0;
|
|
4
|
+
const base_1 = require("../base");
|
|
5
|
+
const child_process_1 = require("child_process");
|
|
6
|
+
const util_1 = require("util");
|
|
7
|
+
class ShellProcessorModifier extends base_1.BaseMessageModifier {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
super();
|
|
10
|
+
this.execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
11
|
+
this.SHELL_TRIGGER = '!{';
|
|
12
|
+
this.SHELL_CLOSE = '}';
|
|
13
|
+
this.ARGS_PLACEHOLDER = '{{args}}';
|
|
14
|
+
this.options = {
|
|
15
|
+
enableShellExecution: options.enableShellExecution || false, // Disabled by default for security
|
|
16
|
+
maxExecutionTime: options.maxExecutionTime || 10000, // 10 seconds
|
|
17
|
+
allowedCommands: options.allowedCommands || ['ls', 'pwd', 'echo', 'cat', 'head', 'tail', 'grep', 'find', 'wc'],
|
|
18
|
+
blockedCommands: options.blockedCommands || ['rm', 'del', 'format', 'sudo', 'su', 'chmod', 'chown', 'kill', 'killall'],
|
|
19
|
+
workingDirectory: options.workingDirectory || process.cwd()
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
async modify(originalRequest, createdMessage) {
|
|
23
|
+
var _a;
|
|
24
|
+
try {
|
|
25
|
+
// Get the user message content to process
|
|
26
|
+
const userMessage = createdMessage.message.messages.find(msg => msg.role === 'user');
|
|
27
|
+
if (!userMessage || typeof userMessage.content !== 'string') {
|
|
28
|
+
return createdMessage;
|
|
29
|
+
}
|
|
30
|
+
const content = userMessage.content;
|
|
31
|
+
const args = ((_a = createdMessage.metadata) === null || _a === void 0 ? void 0 : _a.args) || '';
|
|
32
|
+
// First, replace {{args}} placeholders
|
|
33
|
+
let processedContent = content.replace(new RegExp(this.escapeRegex(this.ARGS_PLACEHOLDER), 'g'), args);
|
|
34
|
+
// Then process shell injections if enabled
|
|
35
|
+
if (this.options.enableShellExecution && processedContent.includes(this.SHELL_TRIGGER)) {
|
|
36
|
+
processedContent = await this.processShellInjections(processedContent, args);
|
|
37
|
+
}
|
|
38
|
+
// If content was modified, update the message
|
|
39
|
+
if (processedContent !== content) {
|
|
40
|
+
const updatedMessages = createdMessage.message.messages.map(msg => {
|
|
41
|
+
if (msg.role === 'user' && typeof msg.content === 'string' && msg.content === content) {
|
|
42
|
+
return {
|
|
43
|
+
...msg,
|
|
44
|
+
content: processedContent
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return msg;
|
|
48
|
+
});
|
|
49
|
+
return Promise.resolve({
|
|
50
|
+
message: {
|
|
51
|
+
...createdMessage.message,
|
|
52
|
+
messages: updatedMessages
|
|
53
|
+
},
|
|
54
|
+
metadata: {
|
|
55
|
+
...createdMessage.metadata,
|
|
56
|
+
shellProcessed: true,
|
|
57
|
+
argsReplaced: content.includes(this.ARGS_PLACEHOLDER)
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return createdMessage;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
console.error('Error in ShellProcessorModifier:', error);
|
|
65
|
+
return createdMessage;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async processShellInjections(content, args) {
|
|
69
|
+
let processedContent = content;
|
|
70
|
+
const injections = this.extractShellInjections(content);
|
|
71
|
+
// Process injections in reverse order to maintain correct indices
|
|
72
|
+
for (let i = injections.length - 1; i >= 0; i--) {
|
|
73
|
+
const injection = injections[i];
|
|
74
|
+
try {
|
|
75
|
+
// Replace {{args}} in the command with shell-escaped args
|
|
76
|
+
const command = injection.command.replace(new RegExp(this.escapeRegex(this.ARGS_PLACEHOLDER), 'g'), this.escapeShellArg(args));
|
|
77
|
+
// Validate and execute command
|
|
78
|
+
this.validateCommand(command);
|
|
79
|
+
const result = await this.executeCommand(command);
|
|
80
|
+
const replacement = result;
|
|
81
|
+
processedContent = processedContent.substring(0, injection.startIndex) +
|
|
82
|
+
replacement +
|
|
83
|
+
processedContent.substring(injection.endIndex);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
87
|
+
const replacement = `[Shell command error: ${errorMessage}]`;
|
|
88
|
+
processedContent = processedContent.substring(0, injection.startIndex) +
|
|
89
|
+
replacement +
|
|
90
|
+
processedContent.substring(injection.endIndex);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return processedContent;
|
|
94
|
+
}
|
|
95
|
+
extractShellInjections(content) {
|
|
96
|
+
const injections = [];
|
|
97
|
+
let searchIndex = 0;
|
|
98
|
+
while (true) {
|
|
99
|
+
const startIndex = content.indexOf(this.SHELL_TRIGGER, searchIndex);
|
|
100
|
+
if (startIndex === -1)
|
|
101
|
+
break;
|
|
102
|
+
const contentStart = startIndex + this.SHELL_TRIGGER.length;
|
|
103
|
+
const endIndex = content.indexOf(this.SHELL_CLOSE, contentStart);
|
|
104
|
+
if (endIndex === -1) {
|
|
105
|
+
// No closing brace found, skip this occurrence
|
|
106
|
+
searchIndex = contentStart;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const command = content.substring(contentStart, endIndex).trim();
|
|
110
|
+
if (command.length > 0) {
|
|
111
|
+
injections.push({
|
|
112
|
+
startIndex,
|
|
113
|
+
endIndex: endIndex + this.SHELL_CLOSE.length,
|
|
114
|
+
command
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
searchIndex = endIndex + this.SHELL_CLOSE.length;
|
|
118
|
+
}
|
|
119
|
+
return injections;
|
|
120
|
+
}
|
|
121
|
+
validateCommand(command) {
|
|
122
|
+
if (!this.options.enableShellExecution) {
|
|
123
|
+
throw new Error('Shell execution is disabled');
|
|
124
|
+
}
|
|
125
|
+
// Extract the base command (first word)
|
|
126
|
+
const baseCommand = command.trim().split(/\s+/)[0];
|
|
127
|
+
// Check blocked commands
|
|
128
|
+
if (this.options.blockedCommands.some(blocked => baseCommand.includes(blocked))) {
|
|
129
|
+
throw new Error(`Command '${baseCommand}' is blocked for security reasons`);
|
|
130
|
+
}
|
|
131
|
+
// Check allowed commands (if allowlist is defined)
|
|
132
|
+
if (this.options.allowedCommands.length > 0 &&
|
|
133
|
+
!this.options.allowedCommands.some(allowed => baseCommand.includes(allowed))) {
|
|
134
|
+
throw new Error(`Command '${baseCommand}' is not in the allowed commands list`);
|
|
135
|
+
}
|
|
136
|
+
// Additional security checks
|
|
137
|
+
if (command.includes('&&') || command.includes('||') || command.includes(';') || command.includes('|')) {
|
|
138
|
+
throw new Error('Command chaining and piping are not allowed');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async executeCommand(command) {
|
|
142
|
+
try {
|
|
143
|
+
const { stdout, stderr } = await this.execAsync(command, {
|
|
144
|
+
cwd: this.options.workingDirectory,
|
|
145
|
+
timeout: this.options.maxExecutionTime,
|
|
146
|
+
maxBuffer: 1024 * 1024 // 1MB buffer
|
|
147
|
+
});
|
|
148
|
+
let result = stdout;
|
|
149
|
+
if (stderr) {
|
|
150
|
+
result += `\n[stderr]: ${stderr}`;
|
|
151
|
+
}
|
|
152
|
+
return result || '[Command executed successfully with no output]';
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
if (error.killed && error.signal === 'SIGTERM') {
|
|
156
|
+
throw new Error(`Command timed out after ${this.options.maxExecutionTime}ms`);
|
|
157
|
+
}
|
|
158
|
+
throw new Error(`Command failed: ${error.message}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
escapeShellArg(arg) {
|
|
162
|
+
// Basic shell escaping - in production, use a proper shell escaping library
|
|
163
|
+
return `"${arg.replace(/"/g, '\\"')}"`;
|
|
164
|
+
}
|
|
165
|
+
escapeRegex(str) {
|
|
166
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
exports.ShellProcessorModifier = ShellProcessorModifier;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ProcessedMessage } from "@codebolt/types/agent";
|
|
2
|
+
import { BaseMessageModifier } from "../base";
|
|
3
|
+
import { FlatUserMessage } from "@codebolt/types/sdk";
|
|
4
|
+
export interface ToolInjectionOptions {
|
|
5
|
+
toolsLocation?: 'Tool' | 'SystemMessage' | 'InsidePrompt';
|
|
6
|
+
includeToolDescriptions?: boolean;
|
|
7
|
+
maxToolsInMessage?: number;
|
|
8
|
+
giveToolExamples?: boolean;
|
|
9
|
+
maxToolExamples?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class ToolInjectionModifier extends BaseMessageModifier {
|
|
12
|
+
private readonly options;
|
|
13
|
+
constructor(options?: ToolInjectionOptions);
|
|
14
|
+
modify(originalRequest: FlatUserMessage, createdMessage: ProcessedMessage): Promise<ProcessedMessage>;
|
|
15
|
+
private addToolsInsidePrompt;
|
|
16
|
+
private addToolsAsSystemMessage;
|
|
17
|
+
private addToolsAsToolCalls;
|
|
18
|
+
private generateToolsInfo;
|
|
19
|
+
private generateToolExample;
|
|
20
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ToolInjectionModifier = void 0;
|
|
7
|
+
const base_1 = require("../base");
|
|
8
|
+
const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
|
|
9
|
+
class ToolInjectionModifier extends base_1.BaseMessageModifier {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
super();
|
|
12
|
+
this.options = {
|
|
13
|
+
toolsLocation: options.toolsLocation || 'Tool',
|
|
14
|
+
includeToolDescriptions: options.includeToolDescriptions !== false,
|
|
15
|
+
maxToolsInMessage: options.maxToolsInMessage || 20,
|
|
16
|
+
giveToolExamples: options.giveToolExamples || false,
|
|
17
|
+
maxToolExamples: options.maxToolExamples || 2
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
async modify(originalRequest, createdMessage) {
|
|
21
|
+
try {
|
|
22
|
+
const toolsResponse = await codeboltjs_1.default.mcp.listMcpFromServers(['codebolt']);
|
|
23
|
+
const tools = (toolsResponse === null || toolsResponse === void 0 ? void 0 : toolsResponse.data) || [];
|
|
24
|
+
if (tools.length === 0) {
|
|
25
|
+
return createdMessage;
|
|
26
|
+
}
|
|
27
|
+
switch (this.options.toolsLocation) {
|
|
28
|
+
case 'InsidePrompt':
|
|
29
|
+
//@ts-ignore
|
|
30
|
+
return this.addToolsInsidePrompt(createdMessage, tools);
|
|
31
|
+
//@ts-ignore
|
|
32
|
+
case 'SystemMessage':
|
|
33
|
+
//@ts-ignore
|
|
34
|
+
return this.addToolsAsSystemMessage(createdMessage, tools);
|
|
35
|
+
case 'Tool':
|
|
36
|
+
//@ts-ignore
|
|
37
|
+
return this.addToolsAsToolCalls(createdMessage, tools);
|
|
38
|
+
default:
|
|
39
|
+
//@ts-ignore
|
|
40
|
+
return this.addToolsAsToolCalls(createdMessage, tools);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error('Error in ToolInjectionModifier:', error);
|
|
45
|
+
return createdMessage;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
addToolsInsidePrompt(createdMessage, tools) {
|
|
49
|
+
const toolsInfo = this.generateToolsInfo(tools);
|
|
50
|
+
// Modify the last user message to include tools information
|
|
51
|
+
const modifiedMessages = createdMessage.message.messages.map((message, index) => {
|
|
52
|
+
if (message.role === 'user' && index === createdMessage.message.messages.length - 1) {
|
|
53
|
+
return {
|
|
54
|
+
...message,
|
|
55
|
+
content: `${message.content}\n\n--- Available Tools ---\n${toolsInfo}`
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return message;
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
message: {
|
|
62
|
+
...createdMessage.message,
|
|
63
|
+
messages: modifiedMessages
|
|
64
|
+
},
|
|
65
|
+
metadata: {
|
|
66
|
+
...createdMessage.metadata,
|
|
67
|
+
toolsInjected: true,
|
|
68
|
+
toolsLocation: 'InsidePrompt',
|
|
69
|
+
toolsCount: tools.length
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
addToolsAsSystemMessage(createdMessage, tools) {
|
|
74
|
+
const toolsInfo = this.generateToolsInfo(tools);
|
|
75
|
+
const toolsMessage = {
|
|
76
|
+
role: 'system',
|
|
77
|
+
content: `Available Tools:\n${toolsInfo}`
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
message: {
|
|
81
|
+
...createdMessage.message,
|
|
82
|
+
messages: [toolsMessage, ...createdMessage.message.messages]
|
|
83
|
+
},
|
|
84
|
+
metadata: {
|
|
85
|
+
...createdMessage.metadata,
|
|
86
|
+
toolsInjected: true,
|
|
87
|
+
toolsLocation: 'SystemMessage',
|
|
88
|
+
toolsCount: tools.length
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
addToolsAsToolCalls(createdMessage, tools) {
|
|
93
|
+
return {
|
|
94
|
+
message: {
|
|
95
|
+
...createdMessage.message,
|
|
96
|
+
tools,
|
|
97
|
+
tool_choice: 'auto'
|
|
98
|
+
},
|
|
99
|
+
metadata: {
|
|
100
|
+
...createdMessage.metadata,
|
|
101
|
+
toolsInjected: true,
|
|
102
|
+
toolsLocation: 'Tool',
|
|
103
|
+
toolsCount: tools.length
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
generateToolsInfo(tools) {
|
|
108
|
+
const toolsInfo = [];
|
|
109
|
+
tools.slice(0, this.options.maxToolsInMessage).forEach((tool, index) => {
|
|
110
|
+
let toolInfo = `${index + 1}. **${tool.function.name}**`;
|
|
111
|
+
if (this.options.includeToolDescriptions && tool.function.description) {
|
|
112
|
+
toolInfo += `\n Description: ${tool.function.description}`;
|
|
113
|
+
}
|
|
114
|
+
if (tool.function.parameters) {
|
|
115
|
+
const params = typeof tool.function.parameters === 'object'
|
|
116
|
+
? Object.keys(tool.function.parameters).join(', ')
|
|
117
|
+
: String(tool.function.parameters);
|
|
118
|
+
toolInfo += `\n Parameters: ${params}`;
|
|
119
|
+
}
|
|
120
|
+
if (this.options.giveToolExamples && index < this.options.maxToolExamples) {
|
|
121
|
+
const example = this.generateToolExample(tool);
|
|
122
|
+
if (example) {
|
|
123
|
+
toolInfo += `\n Example: ${example}`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
toolsInfo.push(toolInfo);
|
|
127
|
+
});
|
|
128
|
+
return toolsInfo.join('\n\n');
|
|
129
|
+
}
|
|
130
|
+
generateToolExample(tool) {
|
|
131
|
+
// Generate simple examples based on tool name
|
|
132
|
+
const toolName = tool.function.name.toLowerCase();
|
|
133
|
+
if (toolName.includes('read') && toolName.includes('file')) {
|
|
134
|
+
return `${tool.function.name}({ filePath: "example.txt" })`;
|
|
135
|
+
}
|
|
136
|
+
if (toolName.includes('write') && toolName.includes('file')) {
|
|
137
|
+
return `${tool.function.name}({ filePath: "output.txt", content: "Hello World" })`;
|
|
138
|
+
}
|
|
139
|
+
if (toolName.includes('delete') && toolName.includes('file')) {
|
|
140
|
+
return `${tool.function.name}({ filePath: "temp.txt" })`;
|
|
141
|
+
}
|
|
142
|
+
if (toolName.includes('move') && toolName.includes('file')) {
|
|
143
|
+
return `${tool.function.name}({ sourcePath: "old.txt", destinationPath: "new.txt" })`;
|
|
144
|
+
}
|
|
145
|
+
if (toolName.includes('copy') && toolName.includes('file')) {
|
|
146
|
+
return `${tool.function.name}({ sourcePath: "source.txt", destinationPath: "copy.txt" })`;
|
|
147
|
+
}
|
|
148
|
+
// Generic example
|
|
149
|
+
return `${tool.function.name}({ /* parameters */ })`;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.ToolInjectionModifier = ToolInjectionModifier;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ProcessedMessage } from "@codebolt/types/agent";
|
|
2
|
+
import { BaseMessageModifier } from "../base";
|
|
3
|
+
import { FlatUserMessage } from "@codebolt/types/sdk";
|
|
4
|
+
export declare class AddCurrentDirectoryRootFilesModifier extends BaseMessageModifier {
|
|
5
|
+
constructor();
|
|
6
|
+
modify(originalRequest: FlatUserMessage, createdMessage: ProcessedMessage, context?: Record<string, unknown>): Promise<ProcessedMessage>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.AddCurrentDirectoryRootFilesModifier = void 0;
|
|
37
|
+
const base_1 = require("../base");
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
class AddCurrentDirectoryRootFilesModifier extends base_1.BaseMessageModifier {
|
|
40
|
+
constructor() {
|
|
41
|
+
super();
|
|
42
|
+
}
|
|
43
|
+
async modify(originalRequest, createdMessage, context) {
|
|
44
|
+
try {
|
|
45
|
+
// Use current working directory
|
|
46
|
+
const currentPath = process.cwd();
|
|
47
|
+
// Read directory contents
|
|
48
|
+
const files = await fs.promises.readdir(currentPath, { withFileTypes: true });
|
|
49
|
+
// Format files list with type indicators
|
|
50
|
+
const filesList = files
|
|
51
|
+
.map(file => {
|
|
52
|
+
return ` ${file.name}`;
|
|
53
|
+
})
|
|
54
|
+
.join('\n');
|
|
55
|
+
const filesContent = filesList || 'No files found';
|
|
56
|
+
// Find system message and append files list to it
|
|
57
|
+
const messages = [...createdMessage.message.messages];
|
|
58
|
+
const systemMessageIndex = messages.findIndex(message => message.role === 'system');
|
|
59
|
+
if (systemMessageIndex !== -1) {
|
|
60
|
+
// Update existing system message
|
|
61
|
+
messages[systemMessageIndex] = {
|
|
62
|
+
...messages[systemMessageIndex],
|
|
63
|
+
content: `${messages[systemMessageIndex].content}\n\nCurrent directory root files:\n\n${filesContent}`
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
// Add new system message if none exists
|
|
68
|
+
messages.unshift({
|
|
69
|
+
role: 'system',
|
|
70
|
+
content: `Current directory root files:\n\n${filesContent}`
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
// Return updated message
|
|
74
|
+
return {
|
|
75
|
+
message: {
|
|
76
|
+
...createdMessage.message,
|
|
77
|
+
messages
|
|
78
|
+
},
|
|
79
|
+
metadata: {
|
|
80
|
+
...createdMessage.metadata,
|
|
81
|
+
currentDirectoryFilesAdded: true,
|
|
82
|
+
currentDirectoryPath: currentPath
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
console.error('Error in AddCurrentDirectoryRootFilesModifier:', error);
|
|
88
|
+
// Return original message if there's an error
|
|
89
|
+
return createdMessage;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.AddCurrentDirectoryRootFilesModifier = AddCurrentDirectoryRootFilesModifier;
|
|
@@ -1,19 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProcessedMessage } from "@codebolt/types/agent";
|
|
2
|
+
import { BaseMessageModifier } from "../base";
|
|
3
|
+
import { FlatUserMessage, Tool } from "@codebolt/types/sdk";
|
|
4
|
+
export interface ToolList {
|
|
5
|
+
getAllTools(): Tool[];
|
|
6
|
+
addTool?(tool: Tool): void;
|
|
7
|
+
removeTool?(toolName: string): void;
|
|
8
|
+
getTool?(toolName: string): Tool | undefined;
|
|
9
|
+
getToolNames?(): string[];
|
|
10
|
+
}
|
|
2
11
|
export interface AddToolsListMessageModifierOptions {
|
|
3
|
-
toolsList
|
|
12
|
+
toolsList?: ToolList;
|
|
4
13
|
addToolsLocation?: 'InsidePrompt' | 'Tool' | 'SystemMessage';
|
|
5
14
|
giveToolExamples?: boolean;
|
|
6
15
|
maxToolExamples?: number;
|
|
7
16
|
includeToolDescriptions?: boolean;
|
|
8
17
|
}
|
|
9
18
|
export declare class AddToolsListMessageModifier extends BaseMessageModifier {
|
|
10
|
-
private readonly toolsList
|
|
19
|
+
private readonly toolsList?;
|
|
11
20
|
private readonly addToolsLocation;
|
|
12
21
|
private readonly giveToolExamples;
|
|
13
22
|
private readonly maxToolExamples;
|
|
14
23
|
private readonly includeToolDescriptions;
|
|
15
|
-
constructor(options
|
|
16
|
-
modify(
|
|
24
|
+
constructor(options?: AddToolsListMessageModifierOptions);
|
|
25
|
+
modify(originalRequest: FlatUserMessage, createdMessage: ProcessedMessage): Promise<ProcessedMessage>;
|
|
17
26
|
private addToolsInsidePrompt;
|
|
18
27
|
private addToolsAsSystemMessage;
|
|
19
28
|
private addToolsAsToolCalls;
|