Skip to content

Commit 22a6a40

Browse files
authored
Merge pull request #1 from jtpio/list-directories
Add `list-directory`
1 parent 6b3c6ad commit 22a6a40

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ This extension provides the following commands for AI-assisted interactions with
5959
- Arguments:
6060
- `directoryPath` (string): Path to the directory to navigate to
6161

62+
- **`jupyterlab-ai-commands:list-directory`** - List files and directories in a specific directory
63+
- Arguments:
64+
- `directoryPath` (string, optional): Path to the directory to list. If not provided, lists the root directory
65+
- `includeHidden` (boolean, optional): Whether to include hidden files (default: false)
66+
6267
- **`jupyterlab-ai-commands:get-file-info`** - Get information about a file including its path, name, extension, and content
6368
- Arguments:
6469
- `filePath` (string, optional): Path to the file to read. If not provided, uses the currently active file in the editor

src/file-commands.ts

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,94 @@ function registerNavigateToDirectoryCommand(commands: CommandRegistry): void {
251251
caption: 'Navigate to a specific directory in the file browser',
252252
describedBy: {
253253
args: {
254-
directoryPath: {
254+
path: {
255255
description: 'Path to the directory to navigate to'
256256
}
257257
}
258258
},
259259
execute: async (args: any) => {
260-
const { directoryPath } = args;
260+
const { path } = args;
261261

262262
await commands.execute('filebrowser:go-to-path', {
263-
path: directoryPath
263+
path
264264
});
265265

266266
return {
267267
success: true,
268-
message: `Navigated to directory '${directoryPath}' successfully`,
269-
directoryPath
268+
message: `Navigated to directory '${path}' successfully`,
269+
path
270+
};
271+
}
272+
};
273+
274+
commands.addCommand(command.id, command);
275+
}
276+
277+
/**
278+
* List files and directories in a specific directory
279+
*/
280+
function registerListDirectoryCommand(
281+
commands: CommandRegistry,
282+
docManager: IDocumentManager
283+
): void {
284+
const command = {
285+
id: 'jupyterlab-ai-commands:list-directory',
286+
label: 'List Directory',
287+
caption: 'List files and directories in a specific directory',
288+
describedBy: {
289+
args: {
290+
path: {
291+
description:
292+
'Path to the directory to list. If not provided, lists the root directory'
293+
},
294+
includeHidden: {
295+
description: 'Whether to include hidden files (default: false)'
296+
}
297+
}
298+
},
299+
execute: async (args: any) => {
300+
const { path = '', includeHidden = false } = args;
301+
302+
const contents = await docManager.services.contents.get(path, {
303+
content: true
304+
});
305+
306+
if (contents.type !== 'directory') {
307+
throw new Error(`Path '${path}' is not a directory`);
308+
}
309+
310+
const items = contents.content || [];
311+
const filteredItems = includeHidden
312+
? items
313+
: items.filter((item: any) => !item.name.startsWith('.'));
314+
315+
const formattedItems = filteredItems.map((item: any) => ({
316+
name: item.name,
317+
path: item.path,
318+
type: item.type,
319+
size: item.size || 0,
320+
created: item.created,
321+
lastModified: item.last_modified,
322+
mimetype: item.mimetype || null,
323+
format: item.format || null
324+
}));
325+
326+
const directories = formattedItems.filter(
327+
(item: any) => item.type === 'directory'
328+
);
329+
const files = formattedItems.filter(
330+
(item: any) => item.type !== 'directory'
331+
);
332+
333+
return {
334+
success: true,
335+
message: `Listed ${formattedItems.length} items in directory '${path || '/'}'`,
336+
path: path || '/',
337+
totalItems: formattedItems.length,
338+
directories: directories.length,
339+
files: files.length,
340+
items: formattedItems,
341+
includeHidden
270342
};
271343
}
272344
};
@@ -462,6 +534,7 @@ export function registerFileCommands(
462534
registerRenameFileCommand(commands, docManager);
463535
registerCopyFileCommand(commands, docManager);
464536
registerNavigateToDirectoryCommand(commands);
537+
registerListDirectoryCommand(commands, docManager);
465538
registerGetFileInfoCommand(commands, docManager, editorTracker);
466539
registerSetFileContentCommand(commands, docManager);
467540
}

0 commit comments

Comments
 (0)