Skip to content

Commit af30f81

Browse files
fs relaated function added to apis
1 parent 7f1803d commit af30f81

File tree

2 files changed

+129
-11
lines changed

2 files changed

+129
-11
lines changed

src/modules/fs.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const cbfs = {
8585
newContent
8686
},
8787
},
88-
"commandOutput"
88+
"updateFileResponse"
8989
);
9090
},
9191
/**
@@ -207,6 +207,89 @@ const cbfs = {
207207
"writeToFileResponse"
208208
);
209209
},
210+
211+
/**
212+
* @function listFiles
213+
* @description Lists all files in a directory.
214+
* @param {string} path - The path to list files from.
215+
* @param {boolean} isRecursive - Whether to list files recursively.
216+
* @param {boolean} askForPermission - Whether to ask for permission before listing.
217+
* @returns {Promise<{success: boolean, result: any}>} A promise that resolves with the list of files.
218+
*/
219+
220+
221+
/**
222+
* @function grepSearch
223+
* @description Performs a grep search in files.
224+
* @param {string} path - The path to search within.
225+
* @param {string} query - The query to search for.
226+
* @param {string} includePattern - Pattern of files to include.
227+
* @param {string} excludePattern - Pattern of files to exclude.
228+
* @param {boolean} caseSensitive - Whether the search is case sensitive.
229+
* @returns {Promise<{success: boolean, result: any}>} A promise that resolves with the search results.
230+
*/
231+
grepSearch: (path: string, query: string, includePattern?: string, excludePattern?: string, caseSensitive: boolean = true): Promise<{success: boolean, result: any}> => {
232+
return cbws.messageManager.sendAndWaitForResponse(
233+
{
234+
"type": "fsEvent",
235+
"action": "grep_search",
236+
"message": {
237+
path,
238+
query,
239+
includePattern,
240+
excludePattern,
241+
caseSensitive
242+
}
243+
},
244+
"grepSearchResponse"
245+
);
246+
},
247+
248+
/**
249+
* @function fileSearch
250+
* @description Performs a fuzzy search for files.
251+
* @param {string} query - The query to search for.
252+
* @returns {Promise<{success: boolean, result: any}>} A promise that resolves with the search results.
253+
*/
254+
fileSearch: (query: string): Promise<{success: boolean, result: any}> => {
255+
return cbws.messageManager.sendAndWaitForResponse(
256+
{
257+
"type": "fsEvent",
258+
"action": "file_search",
259+
"message": {
260+
query
261+
}
262+
},
263+
"fileSearchResponse"
264+
);
265+
},
266+
267+
/**
268+
* @function editFileWithDiff
269+
* @description Edits a file by applying a diff.
270+
* @param {string} targetFile - The target file to edit.
271+
* @param {string} codeEdit - The code edit to apply.
272+
* @param {string} diffIdentifier - The diff identifier.
273+
* @param {string} prompt - The prompt for the edit.
274+
* @param {string} applyModel - The model to apply the edit with.
275+
* @returns {Promise<{success: boolean, result: any}>} A promise that resolves with the edit result.
276+
*/
277+
editFileWithDiff: (targetFile: string, codeEdit: string, diffIdentifier: string, prompt: string, applyModel?: string): Promise<{success: boolean, result: any}> => {
278+
return cbws.messageManager.sendAndWaitForResponse(
279+
{
280+
"type": "fsEvent",
281+
"action": "edit_file_with_diff",
282+
"message": {
283+
target_file: targetFile,
284+
code_edit: codeEdit,
285+
diffIdentifier,
286+
prompt,
287+
applyModel
288+
}
289+
},
290+
"editFileAndApplyDiffResponse"
291+
);
292+
},
210293

211294
};
212295

testcases/tests/fs-test.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function testFileSystemOperations() {
55

66
try {
77

8-
await codebolt.activate();
8+
99
await codebolt.waitForConnection();
1010

1111
console.log('\n1. Testing file creation...');
@@ -20,33 +20,68 @@ async function testFileSystemOperations() {
2020
const readResult = await codebolt.fs.readFile('./fs-test-file.txt');
2121
console.log('✅ File read:', readResult);
2222

23-
23+
console.log('\n3. Testing file update...');
24+
const updateResult = await codebolt.fs.updateFile(
25+
'fs-test-file.txt',
26+
'.',
27+
'This is updated content for the test file'
28+
);
29+
console.log('✅ File updated:', updateResult);
2430

25-
console.log('\n3. Testing directory listing...');
31+
console.log('\n4. Testing writeToFile...');
32+
const writeResult = await codebolt.fs.writeToFile(
33+
'./fs-test-file.txt',
34+
'This is content written using writeToFile method'
35+
);
36+
console.log('✅ Write to file:', writeResult);
37+
38+
console.log('\n5. Testing directory listing with listFile...');
2639
const listResult = await codebolt.fs.listFile('.', false);
27-
console.log('✅ Directory listing:', listResult);
40+
console.log('✅ Directory listing (listFile):', listResult);
41+
42+
console.log('\n6. Testing directory listing with listFiles...');
43+
const listFilesResult = await codebolt.fs.listFile('.', false, true);
44+
console.log('✅ Directory listing (listFiles):', listFilesResult);
2845

29-
console.log('\n4. Testing recursive directory listing...');
46+
console.log('\n7. Testing recursive directory listing...');
3047
const recursiveListResult = await codebolt.fs.listFile('.', true);
3148
console.log('✅ Recursive listing:', recursiveListResult);
3249

33-
console.log('\n5. Testing folder creation...');
50+
console.log('\n8. Testing folder creation...');
3451
const folderResult = await codebolt.fs.createFolder('test-folder', '.');
3552
console.log('✅ Folder created:', folderResult);
3653

37-
console.log('\n6. Testing file search...');
54+
console.log('\n9. Testing file search with searchFiles...');
3855
const searchResult = await codebolt.fs.searchFiles('.', '.*\\.txt', '*.txt');
3956
console.log('✅ File search result:', searchResult);
4057

41-
console.log('\n7. Testing code definition names...');
58+
console.log('\n10. Testing fileSearch (fuzzy search)...');
59+
const fuzzySearchResult = await codebolt.fs.fileSearch('test');
60+
console.log('✅ Fuzzy file search result:', fuzzySearchResult);
61+
62+
console.log('\n11. Testing grepSearch...');
63+
const grepResult = await codebolt.fs.grepSearch('.', 'test', '*.txt', null, true);
64+
console.log('✅ Grep search result:', grepResult);
65+
66+
console.log('\n12. Testing code definition names...');
4267
const codeDefResult = await codebolt.fs.listCodeDefinitionNames('.');
4368
console.log('✅ Code definitions:', codeDefResult);
4469

45-
console.log('\n8. Testing file deletion...');
70+
console.log('\n13. Testing editFileWithDiff...');
71+
const editResult = await codebolt.fs.editFileWithDiff(
72+
'./fs-test-file.txt',
73+
'Updated content with diff',
74+
'test-diff',
75+
'Test file edit',
76+
'default'
77+
);
78+
console.log('✅ Edit file with diff:', editResult);
79+
80+
console.log('\n14. Testing file deletion...');
4681
const deleteResult = await codebolt.fs.deleteFile('fs-test-file.txt', '.');
4782
console.log('✅ File deleted:', deleteResult);
4883

49-
console.log('\n9. Testing folder deletion...');
84+
console.log('\n15. Testing folder deletion...');
5085
const deleteFolderResult = await codebolt.fs.deleteFolder('test-folder', '.');
5186
console.log('✅ Folder deleted:', deleteFolderResult);
5287

0 commit comments

Comments
 (0)