@codebolt/agent 1.1.0 → 2.2.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 +157 -66
- package/dist/builderpattern/agent.d.ts +86 -0
- package/dist/builderpattern/agent.js +324 -0
- package/dist/builderpattern/followupquestionbuilder.d.ts +75 -0
- package/dist/builderpattern/followupquestionbuilder.js +197 -0
- package/dist/builderpattern/index.d.ts +14 -0
- package/dist/builderpattern/index.js +23 -0
- package/dist/builderpattern/llmoutputhandler.d.ts +102 -0
- package/dist/builderpattern/llmoutputhandler.js +452 -0
- package/dist/builderpattern/promptbuilder.d.ts +382 -0
- package/dist/builderpattern/promptbuilder.js +805 -0
- package/dist/builderpattern/systemprompt.d.ts +20 -0
- package/dist/builderpattern/systemprompt.js +48 -0
- package/dist/builderpattern/taskInstruction.d.ts +37 -0
- package/dist/builderpattern/taskInstruction.js +57 -0
- package/dist/builderpattern/usermessage.d.ts +70 -0
- package/dist/builderpattern/usermessage.js +123 -0
- package/dist/composablepattern/agent.d.ts +169 -0
- package/dist/composablepattern/agent.js +598 -0
- package/dist/composablepattern/codebolt-storage.d.ts +67 -0
- package/dist/composablepattern/codebolt-storage.js +320 -0
- package/dist/composablepattern/document.d.ts +149 -0
- package/dist/composablepattern/document.js +405 -0
- package/dist/composablepattern/examples/codebolt-integration-example.d.ts +12 -0
- package/dist/composablepattern/examples/codebolt-integration-example.js +218 -0
- package/dist/composablepattern/examples/codebolt-storage-example.d.ts +40 -0
- package/dist/composablepattern/examples/codebolt-storage-example.js +223 -0
- package/dist/composablepattern/examples/custom-steps-example.d.ts +20 -0
- package/dist/composablepattern/examples/custom-steps-example.js +455 -0
- package/dist/composablepattern/examples/document-agent.d.ts +17 -0
- package/dist/composablepattern/examples/document-agent.js +107 -0
- package/dist/composablepattern/examples/simple-codebolt-integration.d.ts +8 -0
- package/dist/composablepattern/examples/simple-codebolt-integration.js +164 -0
- package/dist/composablepattern/examples/weather-agent.d.ts +18 -0
- package/dist/composablepattern/examples/weather-agent.js +86 -0
- package/dist/composablepattern/examples/workflow-example.d.ts +12 -0
- package/dist/composablepattern/examples/workflow-example.js +463 -0
- package/dist/composablepattern/index.d.ts +63 -0
- package/dist/composablepattern/index.js +115 -0
- package/dist/composablepattern/memory.d.ts +89 -0
- package/dist/composablepattern/memory.js +141 -0
- package/dist/composablepattern/tool.d.ts +84 -0
- package/dist/composablepattern/tool.js +260 -0
- package/dist/composablepattern/types.d.ts +194 -0
- package/dist/composablepattern/types.js +6 -0
- package/dist/composablepattern/user-context.d.ts +214 -0
- package/dist/composablepattern/user-context.js +275 -0
- package/dist/composablepattern/workflow.d.ts +388 -0
- package/dist/composablepattern/workflow.js +562 -0
- package/dist/processor/agent/agentStep.d.ts +49 -0
- package/dist/processor/agent/agentStep.js +241 -0
- package/dist/processor/agent/toolExecutor.d.ts +19 -0
- package/dist/processor/agent/toolExecutor.js +90 -0
- package/dist/processor/index.d.ts +7 -0
- package/dist/processor/index.js +36 -0
- package/dist/processor/messageModifiers/baseMessageModifier.d.ts +20 -0
- package/dist/processor/messageModifiers/baseMessageModifier.js +55 -0
- package/dist/processor/processors/baseProcessor.d.ts +14 -0
- package/dist/processor/processors/baseProcessor.js +29 -0
- package/dist/processor/tools/baseTool.d.ts +10 -0
- package/dist/processor/tools/baseTool.js +20 -0
- package/dist/processor/tools/toolList.d.ts +9 -0
- package/dist/processor/tools/toolList.js +22 -0
- package/dist/processor/types/interfaces.d.ts +94 -0
- package/dist/processor/types/interfaces.js +2 -0
- package/dist/processor-pieces/base/baseMessageModifier.d.ts +12 -0
- package/dist/processor-pieces/base/baseMessageModifier.js +15 -0
- package/dist/processor-pieces/base/basePostInferenceProcessor.d.ts +13 -0
- package/dist/processor-pieces/base/basePostInferenceProcessor.js +18 -0
- package/dist/processor-pieces/base/basePostToolCallProcessor.d.ts +15 -0
- package/dist/processor-pieces/base/basePostToolCallProcessor.js +25 -0
- package/dist/processor-pieces/base/basePreInferenceProcessor.d.ts +12 -0
- package/dist/processor-pieces/base/basePreInferenceProcessor.js +17 -0
- package/dist/processor-pieces/base/basePreToolCallProcessor.d.ts +18 -0
- package/dist/processor-pieces/base/basePreToolCallProcessor.js +66 -0
- package/dist/processor-pieces/base/index.d.ts +9 -0
- package/dist/processor-pieces/base/index.js +18 -0
- package/dist/processor-pieces/index.d.ts +1 -0
- package/dist/processor-pieces/index.js +59 -0
- package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.d.ts +22 -0
- package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.js +144 -0
- package/dist/processor-pieces/messageModifiers/argumentProcessorModifier.d.ts +13 -0
- package/dist/processor-pieces/messageModifiers/argumentProcessorModifier.js +68 -0
- package/dist/processor-pieces/messageModifiers/atFileProcessorModifier.d.ts +25 -0
- package/dist/processor-pieces/messageModifiers/atFileProcessorModifier.js +453 -0
- package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.d.ts +15 -0
- package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.js +75 -0
- package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.d.ts +11 -0
- package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.js +46 -0
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.d.ts +18 -0
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.js +104 -0
- package/dist/processor-pieces/messageModifiers/chatRecordingModifier.d.ts +23 -0
- package/dist/processor-pieces/messageModifiers/chatRecordingModifier.js +173 -0
- package/dist/processor-pieces/messageModifiers/coreSystemPromptModifier.d.ts +14 -0
- package/dist/processor-pieces/messageModifiers/coreSystemPromptModifier.js +130 -0
- package/dist/processor-pieces/messageModifiers/directoryContextModifier.d.ts +36 -0
- package/dist/processor-pieces/messageModifiers/directoryContextModifier.js +418 -0
- package/dist/processor-pieces/messageModifiers/environmentContextModifier.d.ts +26 -0
- package/dist/processor-pieces/messageModifiers/environmentContextModifier.js +260 -0
- package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.d.ts +12 -0
- package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.js +60 -0
- package/dist/processor-pieces/messageModifiers/ideContextModifier.d.ts +34 -0
- package/dist/processor-pieces/messageModifiers/ideContextModifier.js +157 -0
- package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.d.ts +18 -0
- package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.js +222 -0
- package/dist/processor-pieces/messageModifiers/index.d.ts +11 -0
- package/dist/processor-pieces/messageModifiers/index.js +26 -0
- package/dist/processor-pieces/messageModifiers/memoryImportModifier.d.ts +15 -0
- package/dist/processor-pieces/messageModifiers/memoryImportModifier.js +129 -0
- package/dist/processor-pieces/messageModifiers/toolInjectionModifier.d.ts +20 -0
- package/dist/processor-pieces/messageModifiers/toolInjectionModifier.js +152 -0
- package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.d.ts +22 -0
- package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.js +194 -0
- package/dist/processor-pieces/postInferenceProcessors/loopDetectionModifier.d.ts +29 -0
- package/dist/processor-pieces/postInferenceProcessors/loopDetectionModifier.js +174 -0
- package/dist/processor-pieces/postToolCallProcessors/index.d.ts +1 -0
- package/dist/processor-pieces/postToolCallProcessors/index.js +2 -0
- package/dist/processor-pieces/postToolCallProcessors/shellProcessorModifier.d.ts +25 -0
- package/dist/processor-pieces/postToolCallProcessors/shellProcessorModifier.js +225 -0
- package/dist/processor-pieces/preInferenceProcessors/chatCompressionModifier.d.ts +35 -0
- package/dist/processor-pieces/preInferenceProcessors/chatCompressionModifier.js +255 -0
- package/dist/processor-pieces/preInferenceProcessors/index.d.ts +1 -0
- package/dist/processor-pieces/preInferenceProcessors/index.js +2 -0
- package/dist/processor-pieces/pretoolCallProcessors/index.d.ts +1 -0
- package/dist/processor-pieces/pretoolCallProcessors/index.js +2 -0
- package/dist/processor-pieces/processors/advancedLoopDetectionProcessor.d.ts +41 -0
- package/dist/processor-pieces/processors/advancedLoopDetectionProcessor.js +197 -0
- package/dist/processor-pieces/processors/chatCompressionProcessor.d.ts +26 -0
- package/dist/processor-pieces/processors/chatCompressionProcessor.js +90 -0
- package/dist/processor-pieces/processors/chatRecordingProcessor.d.ts +81 -0
- package/dist/processor-pieces/processors/chatRecordingProcessor.js +329 -0
- package/dist/processor-pieces/processors/contextManagementProcessor.d.ts +40 -0
- package/dist/processor-pieces/processors/contextManagementProcessor.js +305 -0
- package/dist/processor-pieces/processors/loopDetectionProcessor.d.ts +30 -0
- package/dist/processor-pieces/processors/loopDetectionProcessor.js +137 -0
- package/dist/processor-pieces/processors/responseValidationProcessor.d.ts +30 -0
- package/dist/processor-pieces/processors/responseValidationProcessor.js +228 -0
- package/dist/processor-pieces/processors/telemetryProcessor.d.ts +99 -0
- package/dist/processor-pieces/processors/telemetryProcessor.js +311 -0
- package/dist/processor-pieces/processors/tokenManagementProcessor.d.ts +37 -0
- package/dist/processor-pieces/processors/tokenManagementProcessor.js +178 -0
- package/dist/processor-pieces/processors/toolExecutionProcessor.d.ts +43 -0
- package/dist/processor-pieces/processors/toolExecutionProcessor.js +207 -0
- package/dist/processor-pieces/tools/fileTools.d.ts +26 -0
- package/dist/processor-pieces/tools/fileTools.js +162 -0
- package/dist/processor-pieces/utils/messageModifierHelper.d.ts +4 -0
- package/dist/processor-pieces/utils/messageModifierHelper.js +58 -0
- package/dist/types/commonTypes.d.ts +4 -0
- package/dist/types/processorTypes.d.ts +67 -0
- package/dist/types/processorTypes.js +58 -0
- package/dist/unified/agent/agent.d.ts +17 -0
- package/dist/unified/agent/agent.js +79 -0
- package/dist/unified/agent/team.d.ts +2 -0
- package/dist/unified/agent/team.js +6 -0
- package/dist/unified/agent/tools.d.ts +44 -0
- package/dist/unified/agent/tools.js +487 -0
- package/dist/unified/agent/workflow.d.ts +24 -0
- package/dist/unified/agent/workflow.js +275 -0
- package/dist/unified/agent/workflowControls.d.ts +11 -0
- package/dist/unified/agent/workflowControls.js +20 -0
- package/dist/unified/agent/workflowSteps.d.ts +63 -0
- package/dist/unified/agent/workflowSteps.js +284 -0
- package/dist/unified/base/agentStep.d.ts +32 -0
- package/dist/unified/base/agentStep.js +96 -0
- package/dist/unified/base/create/createInitialPromptGenerators.d.ts +5 -0
- package/dist/unified/base/create/createInitialPromptGenerators.js +17 -0
- package/dist/unified/base/index.d.ts +5 -0
- package/dist/unified/base/index.js +13 -0
- package/dist/unified/base/initialPromptGenerator.d.ts +48 -0
- package/dist/unified/base/initialPromptGenerator.js +118 -0
- package/dist/unified/base/responseExecutor.d.ts +36 -0
- package/dist/unified/base/responseExecutor.js +283 -0
- package/dist/unified/index.d.ts +18 -0
- package/dist/unified/index.js +42 -0
- package/dist/unified/team/team.d.ts +1 -0
- package/dist/unified/team/team.js +2 -0
- package/dist/unified/types/libTypes.d.ts +378 -0
- package/dist/unified/types/libTypes.js +6 -0
- package/dist/unified/types/types.d.ts +212 -0
- package/dist/unified/types/types.js +43 -0
- package/dist/unified/utils/utils.d.ts +40 -0
- package/dist/unified/utils/utils.js +219 -0
- package/package.json +29 -7
|
@@ -0,0 +1,418 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.DirectoryContextModifier = void 0;
|
|
40
|
+
const base_1 = require("../base");
|
|
41
|
+
const fs = __importStar(require("node:fs/promises"));
|
|
42
|
+
const path = __importStar(require("node:path"));
|
|
43
|
+
const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
|
|
44
|
+
const MAX_ITEMS = 200;
|
|
45
|
+
const TRUNCATION_INDICATOR = '...';
|
|
46
|
+
const DEFAULT_IGNORED_FOLDERS = new Set(['node_modules', '.git', 'dist', 'build', '.next', '.cache', '.tmp', 'tmp']);
|
|
47
|
+
// Error handling utility (simplified version of gemini-cli's getErrorMessage)
|
|
48
|
+
function getErrorMessage(error) {
|
|
49
|
+
if (error instanceof Error) {
|
|
50
|
+
return error.message;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
return String(error);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return 'Failed to get error details';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Node error type guard
|
|
60
|
+
function isNodeError(error) {
|
|
61
|
+
return error instanceof Error && 'code' in error;
|
|
62
|
+
}
|
|
63
|
+
class DirectoryContextModifier extends base_1.BaseMessageModifier {
|
|
64
|
+
constructor(options = {}) {
|
|
65
|
+
super();
|
|
66
|
+
this.gitignorePatterns = new Set();
|
|
67
|
+
this.gitignoreRegexes = [];
|
|
68
|
+
this.options = {
|
|
69
|
+
workspaceDirectories: options.workspaceDirectories || []
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
async modify(originalRequest, createdMessage) {
|
|
73
|
+
try {
|
|
74
|
+
// Get workspace directories from codebolt or use provided options
|
|
75
|
+
let workspaceDirectories = this.options.workspaceDirectories;
|
|
76
|
+
if (!workspaceDirectories || workspaceDirectories.length === 0) {
|
|
77
|
+
const { projectPath } = await codeboltjs_1.default.project.getProjectPath();
|
|
78
|
+
workspaceDirectories = projectPath ? [projectPath] : [];
|
|
79
|
+
}
|
|
80
|
+
if (workspaceDirectories.length === 0) {
|
|
81
|
+
return createdMessage;
|
|
82
|
+
}
|
|
83
|
+
// Load gitignore patterns from all workspace directories
|
|
84
|
+
await this.loadGitignorePatterns(workspaceDirectories);
|
|
85
|
+
const folderStructures = await Promise.all(workspaceDirectories.map((dir) => this.getFolderStructure(dir)));
|
|
86
|
+
const folderStructure = folderStructures.join('\n');
|
|
87
|
+
let workingDirPreamble;
|
|
88
|
+
if (workspaceDirectories.length === 1) {
|
|
89
|
+
workingDirPreamble = `I'm currently working in the directory: ${workspaceDirectories[0]}`;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
const dirList = workspaceDirectories.map((dir) => ` - ${dir}`).join('\n');
|
|
93
|
+
workingDirPreamble = `I'm currently working in the following directories:\n${dirList}`;
|
|
94
|
+
}
|
|
95
|
+
const directoryContext = `${workingDirPreamble}
|
|
96
|
+
Here is the folder structure of the current working directories:
|
|
97
|
+
|
|
98
|
+
${folderStructure}`;
|
|
99
|
+
const contextMessage = {
|
|
100
|
+
role: 'user', // Note: gemini-cli adds this as user message, not system
|
|
101
|
+
content: directoryContext
|
|
102
|
+
};
|
|
103
|
+
// Add as user message (like gemini-cli does)
|
|
104
|
+
const messages = [...createdMessage.message.messages, contextMessage];
|
|
105
|
+
return Promise.resolve({
|
|
106
|
+
message: {
|
|
107
|
+
...createdMessage.message,
|
|
108
|
+
messages
|
|
109
|
+
},
|
|
110
|
+
metadata: {
|
|
111
|
+
...createdMessage.metadata,
|
|
112
|
+
directoryContextAdded: true,
|
|
113
|
+
workspaceDirectories
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
console.error('Error in DirectoryContextModifier:', error);
|
|
119
|
+
return createdMessage;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async loadGitignorePatterns(workspaceDirectories) {
|
|
123
|
+
this.gitignorePatterns.clear();
|
|
124
|
+
this.gitignoreRegexes = [];
|
|
125
|
+
for (const dir of workspaceDirectories) {
|
|
126
|
+
try {
|
|
127
|
+
const gitignorePath = path.join(dir, '.gitignore');
|
|
128
|
+
const content = await fs.readFile(gitignorePath, 'utf-8');
|
|
129
|
+
this.parseGitignoreContent(content);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
// .gitignore file doesn't exist or can't be read, continue
|
|
133
|
+
console.debug(`No .gitignore found in ${dir}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
parseGitignoreContent(content) {
|
|
138
|
+
const lines = content.split('\n')
|
|
139
|
+
.map(line => line.trim())
|
|
140
|
+
.filter(line => line && !line.startsWith('#')); // Remove empty lines and comments
|
|
141
|
+
for (const line of lines) {
|
|
142
|
+
this.gitignorePatterns.add(line);
|
|
143
|
+
// Convert gitignore pattern to regex
|
|
144
|
+
let regexPattern = line
|
|
145
|
+
.replace(/\./g, '\\.') // Escape dots
|
|
146
|
+
.replace(/\*/g, '.*') // Convert * to .*
|
|
147
|
+
.replace(/\?/g, '.') // Convert ? to .
|
|
148
|
+
.replace(/\//g, '\\/'); // Escape slashes
|
|
149
|
+
// Handle directory patterns (ending with /)
|
|
150
|
+
if (line.endsWith('/')) {
|
|
151
|
+
regexPattern = regexPattern.slice(0, -2) + '$'; // Remove \/ and add end anchor
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
this.gitignoreRegexes.push(new RegExp(regexPattern));
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
console.warn(`Invalid gitignore pattern: ${line}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
isGitIgnored(filePath, fileName) {
|
|
162
|
+
// Check against gitignore patterns
|
|
163
|
+
for (const regex of this.gitignoreRegexes) {
|
|
164
|
+
if (regex.test(fileName) || regex.test(filePath)) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Check exact matches
|
|
169
|
+
return this.gitignorePatterns.has(fileName) ||
|
|
170
|
+
this.gitignorePatterns.has(filePath) ||
|
|
171
|
+
this.gitignorePatterns.has(fileName + '/'); // Directory pattern
|
|
172
|
+
}
|
|
173
|
+
shouldIgnoreFile(fileName, filePath) {
|
|
174
|
+
// Check gitignore first
|
|
175
|
+
if (filePath && this.isGitIgnored(filePath, fileName)) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
// Ignore hidden files (except .gitignore itself)
|
|
179
|
+
if (fileName.startsWith('.') && fileName !== '.gitignore') {
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
// Ignore temporary files and caches
|
|
183
|
+
const ignoredPatterns = [
|
|
184
|
+
/^───.*\.tar\.zst$/, // Weird temporary files like ───0069a971db0f139f.tar.zst
|
|
185
|
+
/\.tmp$/,
|
|
186
|
+
/\.temp$/,
|
|
187
|
+
/\.cache$/,
|
|
188
|
+
/\.log$/,
|
|
189
|
+
/\.swp$/,
|
|
190
|
+
/\.bak$/,
|
|
191
|
+
/~$/,
|
|
192
|
+
/^#.*#$/,
|
|
193
|
+
/\.DS_Store$/,
|
|
194
|
+
/Thumbs\.db$/,
|
|
195
|
+
/desktop\.ini$/,
|
|
196
|
+
];
|
|
197
|
+
return ignoredPatterns.some(pattern => pattern.test(fileName));
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Generates a string representation of a directory's structure,
|
|
201
|
+
* limiting the number of items displayed. Ignored folders are shown
|
|
202
|
+
* followed by '...' instead of their contents.
|
|
203
|
+
*
|
|
204
|
+
* @param directory The absolute or relative path to the directory.
|
|
205
|
+
* @param options Optional configuration settings.
|
|
206
|
+
* @returns A promise resolving to the formatted folder structure string.
|
|
207
|
+
*/
|
|
208
|
+
async getFolderStructure(directory, options) {
|
|
209
|
+
var _a, _b;
|
|
210
|
+
const resolvedPath = path.resolve(directory);
|
|
211
|
+
const mergedOptions = {
|
|
212
|
+
maxItems: (_a = options === null || options === void 0 ? void 0 : options.maxItems) !== null && _a !== void 0 ? _a : MAX_ITEMS,
|
|
213
|
+
ignoredFolders: (_b = options === null || options === void 0 ? void 0 : options.ignoredFolders) !== null && _b !== void 0 ? _b : DEFAULT_IGNORED_FOLDERS,
|
|
214
|
+
fileIncludePattern: options === null || options === void 0 ? void 0 : options.fileIncludePattern,
|
|
215
|
+
};
|
|
216
|
+
try {
|
|
217
|
+
// 1. Read the structure using BFS, respecting maxItems
|
|
218
|
+
const structureRoot = await this.readFullStructure(resolvedPath, mergedOptions);
|
|
219
|
+
if (!structureRoot) {
|
|
220
|
+
return `Error: Could not read directory "${resolvedPath}". Check path and permissions.`;
|
|
221
|
+
}
|
|
222
|
+
// 2. Format the structure into a string
|
|
223
|
+
const structureLines = [];
|
|
224
|
+
// Pass true for isRoot for the initial call
|
|
225
|
+
this.formatStructure(structureRoot, '', true, true, structureLines);
|
|
226
|
+
// 3. Build the final output string
|
|
227
|
+
function isTruncated(node) {
|
|
228
|
+
if (node.hasMoreFiles || node.hasMoreSubfolders || node.isIgnored) {
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
for (const sub of node.subFolders) {
|
|
232
|
+
if (isTruncated(sub)) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
let summary = `Showing up to ${mergedOptions.maxItems} items (files + folders).`;
|
|
239
|
+
if (isTruncated(structureRoot)) {
|
|
240
|
+
summary += ` Folders or files indicated with ${TRUNCATION_INDICATOR} contain more items not shown, were ignored, or the display limit (${mergedOptions.maxItems} items) was reached.`;
|
|
241
|
+
}
|
|
242
|
+
return `${summary}\n\n${resolvedPath}${path.sep}\n${structureLines.join('\n')}`;
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
console.error(`Error getting folder structure for ${resolvedPath}:`, error);
|
|
246
|
+
return `Error processing directory "${resolvedPath}": ${getErrorMessage(error)}`;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async readFullStructure(rootPath, options) {
|
|
250
|
+
const rootName = path.basename(rootPath);
|
|
251
|
+
const rootNode = {
|
|
252
|
+
name: rootName,
|
|
253
|
+
path: rootPath,
|
|
254
|
+
files: [],
|
|
255
|
+
subFolders: [],
|
|
256
|
+
totalChildren: 0,
|
|
257
|
+
totalFiles: 0,
|
|
258
|
+
};
|
|
259
|
+
const queue = [
|
|
260
|
+
{ folderInfo: rootNode, currentPath: rootPath },
|
|
261
|
+
];
|
|
262
|
+
let currentItemCount = 0;
|
|
263
|
+
// Count the root node itself as one item if we are not just listing its content
|
|
264
|
+
const processedPaths = new Set(); // To avoid processing same path if symlinks create loops
|
|
265
|
+
while (queue.length > 0) {
|
|
266
|
+
const { folderInfo, currentPath } = queue.shift();
|
|
267
|
+
if (processedPaths.has(currentPath)) {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
processedPaths.add(currentPath);
|
|
271
|
+
if (currentItemCount >= options.maxItems) {
|
|
272
|
+
// If the root itself caused us to exceed, we can't really show anything.
|
|
273
|
+
// Otherwise, this folder won't be processed further.
|
|
274
|
+
// The parent that queued this would have set its own hasMoreSubfolders flag.
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
let entries;
|
|
278
|
+
try {
|
|
279
|
+
const rawEntries = await fs.readdir(currentPath, { withFileTypes: true });
|
|
280
|
+
// Sort entries alphabetically by name for consistent processing order
|
|
281
|
+
entries = rawEntries.sort((a, b) => a.name.localeCompare(b.name));
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
if (isNodeError(error) &&
|
|
285
|
+
(error.code === 'EACCES' || error.code === 'ENOENT')) {
|
|
286
|
+
console.warn(`Warning: Could not read directory ${currentPath}: ${error.message}`);
|
|
287
|
+
if (currentPath === rootPath && error.code === 'ENOENT') {
|
|
288
|
+
return null; // Root directory itself not found
|
|
289
|
+
}
|
|
290
|
+
// For other EACCES/ENOENT on subdirectories, just skip them.
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
throw error;
|
|
294
|
+
}
|
|
295
|
+
const filesInCurrentDir = [];
|
|
296
|
+
const subFoldersInCurrentDir = [];
|
|
297
|
+
// Process files first in the current directory
|
|
298
|
+
for (const entry of entries) {
|
|
299
|
+
if (entry.isFile()) {
|
|
300
|
+
if (currentItemCount >= options.maxItems) {
|
|
301
|
+
folderInfo.hasMoreFiles = true;
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
const fileName = entry.name;
|
|
305
|
+
// Skip hidden files, temporary files, and other unwanted files
|
|
306
|
+
const filePath = path.join(currentPath, fileName);
|
|
307
|
+
if (this.shouldIgnoreFile(fileName, filePath)) {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (!options.fileIncludePattern ||
|
|
311
|
+
options.fileIncludePattern.test(fileName)) {
|
|
312
|
+
filesInCurrentDir.push(fileName);
|
|
313
|
+
currentItemCount++;
|
|
314
|
+
folderInfo.totalFiles++;
|
|
315
|
+
folderInfo.totalChildren++;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
folderInfo.files = filesInCurrentDir;
|
|
320
|
+
// Then process directories and queue them
|
|
321
|
+
for (const entry of entries) {
|
|
322
|
+
if (entry.isDirectory()) {
|
|
323
|
+
// Check if adding this directory ITSELF would meet or exceed maxItems
|
|
324
|
+
// (currentItemCount refers to items *already* added before this one)
|
|
325
|
+
if (currentItemCount >= options.maxItems) {
|
|
326
|
+
folderInfo.hasMoreSubfolders = true;
|
|
327
|
+
break; // Already at limit, cannot add this folder or any more
|
|
328
|
+
}
|
|
329
|
+
// If adding THIS folder makes us hit the limit exactly, and it might have children,
|
|
330
|
+
// it's better to show '...' for the parent, unless this is the very last item slot.
|
|
331
|
+
// This logic is tricky. Let's try a simpler: if we can't add this item, mark and break.
|
|
332
|
+
const subFolderName = entry.name;
|
|
333
|
+
const subFolderPath = path.join(currentPath, subFolderName);
|
|
334
|
+
// Check if folder should be ignored (built-in ignore list or gitignore)
|
|
335
|
+
if (options.ignoredFolders.has(subFolderName) ||
|
|
336
|
+
this.shouldIgnoreFile(subFolderName, subFolderPath)) {
|
|
337
|
+
const ignoredSubFolder = {
|
|
338
|
+
name: subFolderName,
|
|
339
|
+
path: subFolderPath,
|
|
340
|
+
files: [],
|
|
341
|
+
subFolders: [],
|
|
342
|
+
totalChildren: 0,
|
|
343
|
+
totalFiles: 0,
|
|
344
|
+
isIgnored: true,
|
|
345
|
+
};
|
|
346
|
+
subFoldersInCurrentDir.push(ignoredSubFolder);
|
|
347
|
+
currentItemCount++; // Count the ignored folder itself
|
|
348
|
+
folderInfo.totalChildren++; // Also counts towards parent's children
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const subFolderNode = {
|
|
352
|
+
name: subFolderName,
|
|
353
|
+
path: subFolderPath,
|
|
354
|
+
files: [],
|
|
355
|
+
subFolders: [],
|
|
356
|
+
totalChildren: 0,
|
|
357
|
+
totalFiles: 0,
|
|
358
|
+
};
|
|
359
|
+
subFoldersInCurrentDir.push(subFolderNode);
|
|
360
|
+
currentItemCount++;
|
|
361
|
+
folderInfo.totalChildren++; // Counts towards parent's children
|
|
362
|
+
// Add to queue for processing its children later
|
|
363
|
+
queue.push({ folderInfo: subFolderNode, currentPath: subFolderPath });
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
folderInfo.subFolders = subFoldersInCurrentDir;
|
|
367
|
+
}
|
|
368
|
+
return rootNode;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Reads the directory structure using BFS, respecting maxItems.
|
|
372
|
+
* @param node The current node in the reduced structure.
|
|
373
|
+
* @param indent The current indentation string.
|
|
374
|
+
* @param isLast Sibling indicator.
|
|
375
|
+
* @param builder Array to build the string lines.
|
|
376
|
+
*/
|
|
377
|
+
formatStructure(node, currentIndent, isLastChildOfParent, isProcessingRootNode, builder) {
|
|
378
|
+
const connector = isLastChildOfParent ? '└───' : '├───';
|
|
379
|
+
// The root node of the structure (the one passed initially to getFolderStructure)
|
|
380
|
+
// is not printed with a connector line itself, only its name as a header.
|
|
381
|
+
// Its children are printed relative to that conceptual root.
|
|
382
|
+
// Ignored root nodes ARE printed with a connector.
|
|
383
|
+
if (!isProcessingRootNode || node.isIgnored) {
|
|
384
|
+
builder.push(`${currentIndent}${connector}${node.name}${path.sep}${node.isIgnored ? TRUNCATION_INDICATOR : ''}`);
|
|
385
|
+
}
|
|
386
|
+
// Determine the indent for the children of *this* node.
|
|
387
|
+
// If *this* node was the root of the whole structure, its children start with no indent before their connectors.
|
|
388
|
+
// Otherwise, children's indent extends from the current node's indent.
|
|
389
|
+
const indentForChildren = isProcessingRootNode
|
|
390
|
+
? ''
|
|
391
|
+
: currentIndent + (isLastChildOfParent ? ' ' : '│ ');
|
|
392
|
+
// Render files of the current node
|
|
393
|
+
const fileCount = node.files.length;
|
|
394
|
+
for (let i = 0; i < fileCount; i++) {
|
|
395
|
+
const isLastFileAmongSiblings = i === fileCount - 1 &&
|
|
396
|
+
node.subFolders.length === 0 &&
|
|
397
|
+
!node.hasMoreSubfolders;
|
|
398
|
+
const fileConnector = isLastFileAmongSiblings ? '└───' : '├───';
|
|
399
|
+
builder.push(`${indentForChildren}${fileConnector}${node.files[i]}`);
|
|
400
|
+
}
|
|
401
|
+
if (node.hasMoreFiles) {
|
|
402
|
+
const isLastIndicatorAmongSiblings = node.subFolders.length === 0 && !node.hasMoreSubfolders;
|
|
403
|
+
const fileConnector = isLastIndicatorAmongSiblings ? '└───' : '├───';
|
|
404
|
+
builder.push(`${indentForChildren}${fileConnector}${TRUNCATION_INDICATOR}`);
|
|
405
|
+
}
|
|
406
|
+
// Render subfolders of the current node
|
|
407
|
+
const subFolderCount = node.subFolders.length;
|
|
408
|
+
for (let i = 0; i < subFolderCount; i++) {
|
|
409
|
+
const isLastSubfolderAmongSiblings = i === subFolderCount - 1 && !node.hasMoreSubfolders;
|
|
410
|
+
// Children are never the root node being processed initially.
|
|
411
|
+
this.formatStructure(node.subFolders[i], indentForChildren, isLastSubfolderAmongSiblings, false, builder);
|
|
412
|
+
}
|
|
413
|
+
if (node.hasMoreSubfolders) {
|
|
414
|
+
builder.push(`${indentForChildren}└───${TRUNCATION_INDICATOR}`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
exports.DirectoryContextModifier = DirectoryContextModifier;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ProcessedMessage } from "@codebolt/types/agent";
|
|
2
|
+
import { BaseMessageModifier } from "../base";
|
|
3
|
+
import { FlatUserMessage } from "@codebolt/types/sdk";
|
|
4
|
+
export interface EnvironmentContextOptions {
|
|
5
|
+
enableFullContext?: boolean;
|
|
6
|
+
maxFiles?: number;
|
|
7
|
+
maxFileSize?: number;
|
|
8
|
+
includePatterns?: string[];
|
|
9
|
+
excludePatterns?: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare class EnvironmentContextModifier extends BaseMessageModifier {
|
|
12
|
+
private readonly options;
|
|
13
|
+
private readonly defaultExcludePatterns;
|
|
14
|
+
constructor(options?: EnvironmentContextOptions);
|
|
15
|
+
modify(originalRequest: FlatUserMessage, createdMessage: ProcessedMessage): Promise<ProcessedMessage>;
|
|
16
|
+
private generateFullContext;
|
|
17
|
+
private findRelevantFiles;
|
|
18
|
+
private walkDirectory;
|
|
19
|
+
private isExcluded;
|
|
20
|
+
private isIncluded;
|
|
21
|
+
private matchesPattern;
|
|
22
|
+
private countContextFiles;
|
|
23
|
+
enableFullContext(): void;
|
|
24
|
+
disableFullContext(): void;
|
|
25
|
+
isFullContextEnabled(): boolean;
|
|
26
|
+
}
|