-
Notifications
You must be signed in to change notification settings - Fork 749
feat(chat): Add ListDirectory tool #6888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d5808a9
feat(chat): Add ListDirectory tool
jguoamz e21e50c
Fix fsRead validation requiring file path, update tool_index.json
jguoamz 1c5c2c1
Fix fsRead test, improve tool description
jguoamz 90b18ed
Update unit tests
jguoamz afd3df6
Address comments, change list directory result format
jguoamz 1e747fc
Fix unit test
jguoamz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/core/src/codewhispererChat/tools/listDirectory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import * as vscode from 'vscode' | ||
| import { getLogger } from '../../shared/logger/logger' | ||
| import { readDirectoryRecursively } from '../../shared/utilities/workspaceUtils' | ||
| import fs from '../../shared/fs/fs' | ||
| import { InvokeOutput, OutputKind, sanitizePath } from './toolShared' | ||
| import { Writable } from 'stream' | ||
| import path from 'path' | ||
|
|
||
| export interface ListDirectoryParams { | ||
| path: string | ||
| } | ||
|
|
||
| export class ListDirectory { | ||
| private fsPath: string | ||
| private readonly logger = getLogger('listDirectory') | ||
|
|
||
| constructor(params: ListDirectoryParams) { | ||
| this.fsPath = params.path | ||
| } | ||
|
|
||
| public async validate(): Promise<void> { | ||
| this.logger.debug(`Validating fsPath: ${this.fsPath}`) | ||
| if (!this.fsPath || this.fsPath.trim().length === 0) { | ||
| throw new Error('Path cannot be empty.') | ||
| } | ||
|
|
||
| const sanitized = sanitizePath(this.fsPath) | ||
| this.fsPath = sanitized | ||
|
|
||
| const fileUri = vscode.Uri.file(this.fsPath) | ||
| let exists: boolean | ||
| try { | ||
| exists = await fs.exists(fileUri) | ||
jguoamz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!exists) { | ||
| throw new Error(`Path: "${this.fsPath}" does not exist or cannot be accessed.`) | ||
| } | ||
| } catch (err) { | ||
| throw new Error(`Path: "${this.fsPath}" does not exist or cannot be accessed. (${err})`) | ||
| } | ||
|
|
||
| this.logger.debug(`Validation succeeded for path: ${this.fsPath}`) | ||
| } | ||
|
|
||
| public queueDescription(updates: Writable): void { | ||
| const fileName = path.basename(this.fsPath) | ||
| updates.write(`Listing directory on: [${fileName}]`) | ||
| updates.end() | ||
| } | ||
|
|
||
| public async invoke(updates: Writable): Promise<InvokeOutput> { | ||
| try { | ||
| const fileUri = vscode.Uri.file(this.fsPath) | ||
| const listing = await readDirectoryRecursively(fileUri, 0) | ||
| return this.createOutput(listing.join('\n')) | ||
| } catch (error: any) { | ||
| this.logger.error(`Failed to list directory "${this.fsPath}": ${error.message || error}`) | ||
| throw new Error(`[fs_read] Failed to list directory "${this.fsPath}": ${error.message || error}`) | ||
jguoamz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private createOutput(content: string): InvokeOutput { | ||
| return { | ||
| output: { | ||
| kind: OutputKind.Text, | ||
| content: content, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/core/src/test/codewhispererChat/tools/listDirectory.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import assert from 'assert' | ||
| import { ListDirectory } from '../../../codewhispererChat/tools/listDirectory' | ||
| import { TestFolder } from '../../testUtil' | ||
| import path from 'path' | ||
|
|
||
| describe('ListDirectory Tool', () => { | ||
| let testFolder: TestFolder | ||
|
|
||
| before(async () => { | ||
| testFolder = await TestFolder.create() | ||
| }) | ||
|
|
||
| it('throws if path is empty', async () => { | ||
| const listDirectory = new ListDirectory({ path: '' }) | ||
| await assert.rejects(listDirectory.validate(), /Path cannot be empty/i, 'Expected an error about empty path') | ||
| }) | ||
|
|
||
| it('lists directory contents', async () => { | ||
| await testFolder.mkdir('subfolder') | ||
| await testFolder.write('fileA.txt', 'fileA content') | ||
| await testFolder.write(path.join('subfolder', 'fileB.md'), '# fileB') | ||
|
|
||
| const listDirectory = new ListDirectory({ path: testFolder.path }) | ||
| await listDirectory.validate() | ||
| const result = await listDirectory.invoke(process.stdout) | ||
|
|
||
| const lines = result.output.content.split('\n') | ||
| const hasFileA = lines.some((line: string | string[]) => line.includes('- ') && line.includes('fileA.txt')) | ||
| const hasSubfolder = lines.some((line: string | string[]) => line.includes('d ') && line.includes('subfolder')) | ||
|
|
||
| assert.ok(hasFileA, 'Should list fileA.txt in the directory output') | ||
| assert.ok(hasSubfolder, 'Should list the subfolder in the directory output') | ||
| }) | ||
|
|
||
| it('throws error if path does not exist', async () => { | ||
| const missingPath = path.join(testFolder.path, 'no_such_file.txt') | ||
| const listDirectory = new ListDirectory({ path: missingPath }) | ||
|
|
||
| await assert.rejects( | ||
| listDirectory.validate(), | ||
| /does not exist or cannot be accessed/i, | ||
| 'Expected an error indicating the path does not exist' | ||
| ) | ||
| }) | ||
|
|
||
| it('expands ~ path', async () => { | ||
| const listDirectory = new ListDirectory({ path: '~' }) | ||
| await listDirectory.validate() | ||
| const result = await listDirectory.invoke(process.stdout) | ||
|
|
||
| assert.strictEqual(result.output.kind, 'text') | ||
| assert.ok(result.output.content.length > 0) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.