From f702d2300b93183a1f390c4fe3b8151eab1048bf Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 30 Sep 2025 12:06:52 -0500 Subject: [PATCH] Add special handling for viewing /projects --- src/lib/tools/TextEditorViewTool.test.ts | 61 ++++++++++++++++++++---- src/lib/tools/TextEditorViewTool.ts | 7 +++ 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/lib/tools/TextEditorViewTool.test.ts b/src/lib/tools/TextEditorViewTool.test.ts index 144fab54..68441548 100644 --- a/src/lib/tools/TextEditorViewTool.test.ts +++ b/src/lib/tools/TextEditorViewTool.test.ts @@ -9,12 +9,22 @@ class MockFS implements JSRuntimeFS { constructor() { // Set up a basic project structure + this.dirs.add('/projects'); + this.dirs.add('/projects/project1'); + this.dirs.add('/projects/project1/src'); + this.dirs.add('/projects/project1/.git'); + this.dirs.add('/projects/project2'); + this.dirs.add('/projects/project2/src'); this.dirs.add('/project'); this.dirs.add('/project/.git'); this.dirs.add('/project/src'); this.dirs.add('/project/node_modules'); this.dirs.add('/tmp'); + this.files.set('/projects/project1/package.json', '{"name": "project1"}'); + this.files.set('/projects/project1/src/index.ts', 'console.log("project1");'); + this.files.set('/projects/project2/package.json', '{"name": "project2"}'); + this.files.set('/projects/project2/src/main.ts', 'console.log("project2");'); this.files.set('/project/package.json', '{"name": "test"}'); this.files.set('/project/src/index.ts', 'console.log("hello");'); this.files.set('/project/.gitignore', 'node_modules\n.git\n'); @@ -65,12 +75,14 @@ class MockFS implements JSRuntimeFS { async readdir(path: string, options: { withFileTypes: true }): Promise; async readdir(path: string, options?: { withFileTypes?: boolean }): Promise; async readdir(path: string, options?: { withFileTypes?: boolean }): Promise { + // Normalize path by removing trailing slash + const normalizedPath = path.replace(/\/$/, '') || '/'; const entries: string[] = []; // Find all files and directories that are direct children of this path for (const filePath of this.files.keys()) { - if (filePath.startsWith(path + '/')) { - const relativePath = filePath.slice(path.length + 1); + if (filePath.startsWith(normalizedPath + '/')) { + const relativePath = filePath.slice(normalizedPath.length + 1); const parts = relativePath.split('/'); if (parts.length === 1) { entries.push(parts[0]); @@ -79,8 +91,8 @@ class MockFS implements JSRuntimeFS { } for (const dirPath of this.dirs) { - if (dirPath.startsWith(path + '/')) { - const relativePath = dirPath.slice(path.length + 1); + if (dirPath.startsWith(normalizedPath + '/')) { + const relativePath = dirPath.slice(normalizedPath.length + 1); const parts = relativePath.split('/'); if (parts.length === 1) { entries.push(parts[0]); @@ -91,8 +103,8 @@ class MockFS implements JSRuntimeFS { if (options?.withFileTypes) { return entries.map(name => ({ name, - isDirectory: () => this.dirs.has(`${path}/${name}`), - isFile: () => this.files.has(`${path}/${name}`) + isDirectory: () => this.dirs.has(`${normalizedPath}/${name}`), + isFile: () => this.files.has(`${normalizedPath}/${name}`) })); } @@ -109,7 +121,10 @@ class MockFS implements JSRuntimeFS { size?: number; mtimeMs?: number; }> { - if (this.dirs.has(path)) { + // Normalize path by removing trailing slash for directories + const normalizedPath = path.replace(/\/$/, '') || '/'; + + if (this.dirs.has(normalizedPath)) { return { isDirectory: () => true, isFile: () => false, @@ -118,11 +133,11 @@ class MockFS implements JSRuntimeFS { }; } - if (this.files.has(path)) { + if (this.files.has(normalizedPath)) { return { isDirectory: () => false, isFile: () => true, - size: this.files.get(path)!.length, + size: this.files.get(normalizedPath)!.length, mtimeMs: Date.now() }; } @@ -241,4 +256,32 @@ describe('TextEditorViewTool', () => { expect(result).toContain('[Binary file: JPG file'); expect(result).toContain('This appears to be a binary file and cannot be displayed as text.'); }); + + it('should show only project directories when viewing /projects', async () => { + const result = await tool.execute({ path: '/projects' }); + + // Should show the project directories + expect(result).toContain('project1/'); + expect(result).toContain('project2/'); + + // Should NOT show the contents of the project directories (depth 1 only) + expect(result).not.toContain('package.json'); + expect(result).not.toContain('src/'); + expect(result).not.toContain('index.ts'); + expect(result).not.toContain('main.ts'); + }); + + it('should show only project directories when viewing /projects/', async () => { + const result = await tool.execute({ path: '/projects/' }); + + // Should show the project directories + expect(result).toContain('project1/'); + expect(result).toContain('project2/'); + + // Should NOT show the contents of the project directories (depth 1 only) + expect(result).not.toContain('package.json'); + expect(result).not.toContain('src/'); + expect(result).not.toContain('index.ts'); + expect(result).not.toContain('main.ts'); + }); }); \ No newline at end of file diff --git a/src/lib/tools/TextEditorViewTool.ts b/src/lib/tools/TextEditorViewTool.ts index 596799f8..aadcacca 100644 --- a/src/lib/tools/TextEditorViewTool.ts +++ b/src/lib/tools/TextEditorViewTool.ts @@ -52,6 +52,13 @@ export class TextEditorViewTool implements Tool { const stats = await this.fs.stat(absolutePath); if (stats.isDirectory()) { + // Special handling for /projects directory - only show project directories, not their contents + const normalizedPath = absolutePath.replace(/\/$/, ''); // Remove trailing slash + if (normalizedPath === '/projects') { + const tree = await this.generateDirectoryTree(absolutePath, "", 1); // Only show immediate children + return tree; + } + // If it's a directory, generate a tree-like listing // Note: start_line and end_line are ignored for directories const tree = await this.generateDirectoryTree(absolutePath);