Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/core/src/codewhispererChat/tools/fsRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,29 @@ export class FsRead {
}

private createOutput(content: string): InvokeOutput {
let truncated = false
if (content.length > fsReadToolResponseSize) {
truncated = true
this.logger.info(
`The file is too large, truncating output to the first ${fsReadToolResponseSize} characters.`
)
content = this.truncateContent(content)
}
const outputJson = {
content: content,
truncated: truncated,
}
return {
output: {
kind: OutputKind.Text,
content: content,
kind: OutputKind.Json,
content: outputJson,
},
}
}

private truncateContent(content: string): string {
if (content.length > fsReadToolResponseSize) {
return content.substring(0, fsReadToolResponseSize)
return content.substring(0, fsReadToolResponseSize - 3) + '...'
}
return content
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/codewhispererChat/tools/tool_index.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"fsRead": {
"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.\n * If the file exceeds 200K characters, this tool will only read the first 200K characters of the file.",
"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.\n * If the file exceeds 200K characters, this tool will only read the first 200K characters of the file with `truncated = true` in the output.",
"inputSchema": {
"type": "object",
"properties": {
Expand All @@ -10,7 +10,7 @@
"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.\n * Setting `[startLine, -1]` shows all lines from `startLine` to the end of the file.",
"description": "Optional parameter when reading files.\n * If none is given, the full file or the first 200K characters 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.\n * Setting `[startLine, -1]` shows all lines from `startLine` to the end of the file.",
"items": {
"type": "integer"
},
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/test/codewhispererChat/tools/fsRead.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe('FsRead Tool', () => {
await fsRead.validate()
const result = await fsRead.invoke(process.stdout)

assert.strictEqual(result.output.kind, 'text', 'Output kind should be "text"')
assert.strictEqual(result.output.content, fileContent, 'File content should match exactly')
assert.strictEqual(result.output.kind, 'json', 'Output kind should be "json"')
assert.strictEqual(result.output.content.content, fileContent, 'File content should match exactly')
})

it('truncate output if too large', async () => {
Expand All @@ -44,12 +44,13 @@ describe('FsRead Tool', () => {
const fsRead = new FsRead({ path: filePath })
await fsRead.validate()
const result = await fsRead.invoke(process.stdout)
assert.strictEqual(result.output.kind, 'text', 'Output kind should be "text"')
assert.strictEqual(result.output.kind, 'json', 'Output kind should be "json"')
assert.strictEqual(
result.output.content.length,
result.output.content.content.length,
fsReadToolResponseSize,
'Output should be truncated to the max size'
)
assert.ok(result.output.content.truncated, 'Output should be truncated to the max size')
})

it('reads partial lines of a file', async () => {
Expand All @@ -60,8 +61,8 @@ describe('FsRead Tool', () => {
await fsRead.validate()
const result = await fsRead.invoke(process.stdout)

assert.strictEqual(result.output.kind, 'text')
assert.strictEqual(result.output.content, 'B\nC\nD')
assert.strictEqual(result.output.kind, 'json')
assert.strictEqual(result.output.content.content, 'B\nC\nD')
})

it('throws error if path does not exist', async () => {
Expand All @@ -81,8 +82,8 @@ describe('FsRead Tool', () => {

await fsRead.validate()
const result = await fsRead.invoke(process.stdout)
assert.strictEqual(result.output.kind, 'text')
assert.strictEqual(result.output.content, '')
assert.strictEqual(result.output.kind, 'json')
assert.strictEqual(result.output.content.content, '')
})

it('should require acceptance if fsPath is outside the workspace', () => {
Expand Down