-
Notifications
You must be signed in to change notification settings - Fork 83
feat: port fs related tools from VSC. #894
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
Changes from all commits
1a45400
c7692fa
f70f694
0d8f7b9
706747b
f441d4d
8febafb
108f8d4
9f72fc5
49f47b1
216db07
bfc96a7
3b7bf15
a9b8804
a430ad3
1949a56
4c02f9f
235affc
ceebf24
eb820bc
af5c752
b3aa351
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import * as path from 'path' | ||
| import * as fs from 'fs' | ||
| import * as os from 'os' | ||
| import * as crypto from 'crypto' | ||
|
|
||
| /** | ||
| * Interface for working with temporary files. | ||
| * Simplified port of https://github.com/aws/aws-toolkit-vscode/blob/16477869525fb79f8dc82cb22e301aaea9c5e0c6/packages/core/src/test/testUtil.ts#L77 | ||
| * | ||
| * Proper usage requires adding the proper logic into the hooks. See example below: | ||
| * | ||
| * before(async () => { | ||
| * ... | ||
| * testFolder = await TestFolder.create() | ||
| * ... | ||
| * } | ||
| * // Only necessary if test state should not bleed through. | ||
| * afterEach(async () => { | ||
| * ... | ||
| * await testFolder.clear() | ||
| * ... | ||
| * }) | ||
| * | ||
| * after(async () => { | ||
| * ... | ||
| * await testFolder.delete() | ||
| * ... | ||
| * }) | ||
| */ | ||
| export class TestFolder { | ||
| private constructor(public readonly path: string) {} | ||
|
|
||
| async write(fileName: string, content: string): Promise<string> { | ||
| const filePath = path.join(this.path, fileName) | ||
| fs.writeFileSync(filePath, content) | ||
| return filePath | ||
| } | ||
|
|
||
| static async create() { | ||
| const tempDir = path.join( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: A future optimization you could do is delete the root folder at the start/end of the entire test run (if easily doable). This will handle any mistakes from tests failing to clean up properly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree. I noticed the VS Code implementation does this, and it would be a great feature. We do this by wrapping all the tests with our own test runner in VSC, but I don't see similar logic here yet, so this change would require some ground work. |
||
| os.type() === 'Darwin' ? '/tmp' : os.tmpdir(), | ||
| 'aws-language-servers', | ||
| 'test', | ||
| crypto.randomBytes(4).toString('hex') | ||
| ) | ||
| await fs.promises.mkdir(tempDir, { recursive: true }) | ||
| return new TestFolder(tempDir) | ||
| } | ||
|
|
||
| async delete() { | ||
| fs.rmSync(this.path, { recursive: true, force: true }) | ||
| } | ||
|
|
||
| async clear() { | ||
| const files = await fs.readdirSync(this.path) | ||
| for (const f of files) { | ||
| await fs.rmSync(path.join(this.path, f), { recursive: true, force: true }) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,3 +78,16 @@ export function normalize(p: string): string { | |
| } | ||
| return normalizeSeparator(path.normalize(p)) | ||
| } | ||
|
|
||
| export function sanitize(inputPath: string): string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In aws-toolkit-vscode this santize() was added recently, but I don't get why it was added: aws/aws-toolkit-vscode#6840 (comment) Why is this needed instead of our normalize() ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like they do slightly different things that definitely overlap with |
||
| let sanitized = inputPath.trim() | ||
|
|
||
| if (sanitized.startsWith('~')) { | ||
| sanitized = path.join(os.homedir(), sanitized.slice(1)) | ||
| } | ||
|
|
||
| if (!path.isAbsolute(sanitized)) { | ||
| sanitized = path.resolve(sanitized) | ||
| } | ||
| return sanitized | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import * as assert from 'assert' | ||
| import { FsRead } from './fsRead' | ||
| import * as path from 'path' | ||
| import * as fs from 'fs/promises' | ||
| import { TestFeatures } from '@aws/language-server-runtimes/testing' | ||
| import { Workspace } from '@aws/language-server-runtimes/server-interface' | ||
| import { testFolder } from '@aws/lsp-core' | ||
| import { StubbedInstance } from 'ts-sinon' | ||
|
|
||
| describe('FsRead Tool', () => { | ||
| let features: TestFeatures | ||
| let tempFolder: testFolder.TestFolder | ||
|
|
||
| const stdout = new WritableStream({ | ||
| write(chunk) { | ||
| process.stdout.write(chunk) | ||
| }, | ||
| }) | ||
|
|
||
| before(async () => { | ||
| features = new TestFeatures() | ||
| features.workspace = { | ||
| // @ts-ignore reading a file does not require all of fs to be implemented. | ||
| fs: { | ||
| readFile: (path, options?) => | ||
| fs.readFile(path, { encoding: (options?.encoding || 'utf-8') as BufferEncoding }), | ||
| readdir: path => fs.readdir(path, { withFileTypes: true }), | ||
| exists: path => | ||
| fs | ||
| .access(path) | ||
| .then(() => true) | ||
| .catch(() => false), | ||
| } as Workspace['fs'], | ||
| } as StubbedInstance<Workspace> | ||
| tempFolder = await testFolder.TestFolder.create() | ||
| }) | ||
|
|
||
| after(async () => { | ||
| tempFolder.delete() | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| tempFolder.clear() | ||
| }) | ||
|
|
||
| it('throws if path is empty', async () => { | ||
| const fsRead = new FsRead(features) | ||
| await assert.rejects(() => fsRead.invoke({ path: '' })) | ||
| }) | ||
|
|
||
| it('reads entire file', async () => { | ||
| const fileContent = 'Line 1\nLine 2\nLine 3' | ||
| const filePath = await tempFolder.write('fullFile.txt', fileContent) | ||
|
|
||
| const fsRead = new FsRead(features) | ||
| const result = await fsRead.invoke({ path: filePath }) | ||
|
|
||
| assert.strictEqual(result.output.kind, 'text', 'Output kind should be "text"') | ||
| assert.strictEqual(result.output.content, fileContent, 'File content should match exactly') | ||
| }) | ||
|
|
||
| it('reads partial lines of a file', async () => { | ||
| const fileContent = 'A\nB\nC\nD\nE\nF' | ||
| const filePath = await tempFolder.write('partialFile.txt', fileContent) | ||
|
|
||
| const fsRead = new FsRead(features) | ||
| const result = await fsRead.invoke({ path: filePath, readRange: [2, 4] }) | ||
|
|
||
| assert.strictEqual(result.output.kind, 'text') | ||
| assert.strictEqual(result.output.content, 'B\nC\nD') | ||
| }) | ||
|
|
||
| it('throws error if path does not exist', async () => { | ||
| const filePath = path.join(tempFolder.path, 'no_such_file.txt') | ||
| const fsRead = new FsRead(features) | ||
|
|
||
| await assert.rejects(fsRead.invoke({ path: filePath })) | ||
| }) | ||
|
|
||
| it('throws error if content exceeds 30KB', async function () { | ||
| const bigContent = 'x'.repeat(35_000) | ||
|
|
||
| const filePath = await tempFolder.write('bigFile.txt', bigContent) | ||
|
|
||
| const fsRead = new FsRead(features) | ||
|
|
||
| await assert.rejects( | ||
| fsRead.invoke({ path: filePath }), | ||
| /This tool only supports reading \d+ bytes at a time/i, | ||
| 'Expected a size-limit error' | ||
| ) | ||
| }) | ||
|
|
||
| it('invalid line range', async () => { | ||
| const filePath = await tempFolder.write('rangeTest.txt', '1\n2\n3') | ||
| const fsRead = new FsRead(features) | ||
|
|
||
| await fsRead.invoke({ path: filePath, readRange: [3, 2] }) | ||
| const result = await fsRead.invoke({ path: filePath, readRange: [3, 2] }) | ||
| assert.strictEqual(result.output.kind, 'text') | ||
| assert.strictEqual(result.output.content, '') | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import { sanitize } from '@aws/lsp-core/out/util/path' | ||
| import { InvokeOutput, maxToolResponseSize } from './toolShared' | ||
| import { Features } from '@aws/language-server-runtimes/server-interface/server' | ||
|
|
||
| // Port of https://github.com/aws/aws-toolkit-vscode/blob/10bb1c7dc45f128df14d749d95905c0e9b808096/packages/core/src/codewhispererChat/tools/fsRead.ts#L17 | ||
|
|
||
| export interface FsReadParams { | ||
| path: string | ||
| readRange?: number[] | ||
| } | ||
|
|
||
| export class FsRead { | ||
| private readonly logging: Features['logging'] | ||
| private readonly workspace: Features['workspace'] | ||
|
|
||
| constructor(features: Pick<Features, 'workspace' | 'logging'> & Partial<Features>) { | ||
| this.logging = features.logging | ||
| this.workspace = features.workspace | ||
| } | ||
|
|
||
| public async invoke(params: FsReadParams): Promise<InvokeOutput> { | ||
| const path = sanitize(params.path) | ||
| const fileContents = await this.readFile(path) | ||
| this.logging.info(`Read file: ${path}, size: ${fileContents.length}`) | ||
| return this.handleFileRange(params, fileContents) | ||
| } | ||
|
|
||
| private async readFile(filePath: string): Promise<string> { | ||
| this.logging.info(`Reading file: ${filePath}`) | ||
| return await this.workspace.fs.readFile(filePath) | ||
| } | ||
|
|
||
| private handleFileRange(params: FsReadParams, fullText: string): InvokeOutput { | ||
| if (!params.readRange || params.readRange.length === 0) { | ||
| this.logging.log('No range provided. returning entire file.') | ||
| return this.createOutput(this.enforceMaxSize(fullText)) | ||
| } | ||
|
|
||
| const lines = fullText.split('\n') | ||
| const [start, end] = this.parseLineRange(lines.length, params.readRange) | ||
| if (start > end) { | ||
| this.logging.error(`Invalid range: ${params.readRange.join('-')}`) | ||
| return this.createOutput('') | ||
| } | ||
|
|
||
| this.logging.log(`Reading file: ${params.path}, lines ${start + 1}-${end + 1}`) | ||
| const slice = lines.slice(start, end + 1).join('\n') | ||
| return this.createOutput(this.enforceMaxSize(slice)) | ||
| } | ||
|
|
||
| private parseLineRange(lineCount: number, range: number[]): [number, number] { | ||
| const startIdx = range[0] | ||
| let endIdx = range.length >= 2 ? range[1] : undefined | ||
|
|
||
| if (endIdx === undefined) { | ||
| endIdx = -1 | ||
| } | ||
|
|
||
| const convert = (i: number): number => { | ||
| return i < 0 ? lineCount + i : i - 1 | ||
| } | ||
|
|
||
| const finalStart = Math.max(0, Math.min(lineCount - 1, convert(startIdx))) | ||
| const finalEnd = Math.max(0, Math.min(lineCount - 1, convert(endIdx))) | ||
| return [finalStart, finalEnd] | ||
| } | ||
|
|
||
| private enforceMaxSize(content: string): string { | ||
| const byteCount = Buffer.byteLength(content, 'utf8') | ||
| if (byteCount > maxToolResponseSize) { | ||
| throw new Error( | ||
| `This tool only supports reading ${maxToolResponseSize} bytes at a time. | ||
| You tried to read ${byteCount} bytes. Try executing with fewer lines specified.` | ||
| ) | ||
| } | ||
| return content | ||
| } | ||
|
|
||
| private createOutput(content: string): InvokeOutput { | ||
| return { | ||
| output: { | ||
| kind: 'text', | ||
| content: content, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| public getSpec() { | ||
| return { | ||
| name: 'fsRead', | ||
| description: | ||
| 'A tool for reading a file. \n* This tool returns the contents of a file, and the optional `readRange` determines what range of lines will be read from the specified file.', | ||
| inputSchema: { | ||
| type: 'object', | ||
| parameters: { | ||
| path: { | ||
| description: 'Absolute path to a file, e.g. `/repo/file.py`.', | ||
| type: 'string', | ||
| }, | ||
| readRange: { | ||
| description: | ||
| 'Optional parameter when reading files.\n* If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[startLine, -1]` shows all lines from `startLine` to the end of the file.', | ||
| items: { | ||
| type: 'integer', | ||
| }, | ||
| type: 'array', | ||
| }, | ||
| }, | ||
| required: ['path'], | ||
| }, | ||
| } as const | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for now, this kind of precise cross-reference will help a lot, in each of these util files.