Skip to content

Commit 3def040

Browse files
authored
fix(chat): Remove enforceMaxSize from fsRead (aws#6957)
## Problem - fix(chat): Remove enforceMaxSize from fsRead - It had the wrong limit size of 30720 ## Solution - Remove it since we have toolResult size validation after executing the tool already --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent c6d63bc commit 3def040

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ import {
9393
} from '../../constants'
9494
import { ChatSession } from '../../clients/chat/v0/chat'
9595
import { amazonQTabSuffix } from '../../../shared/constants'
96-
import { maxToolOutputCharacterLength, OutputKind } from '../../tools/toolShared'
96+
import { OutputKind } from '../../tools/toolShared'
9797
import { ToolUtils, Tool, ToolType } from '../../tools/toolUtils'
9898
import { ChatStream } from '../../tools/chatStream'
9999
import { ChatHistoryStorage } from '../../storages/chatHistoryStorage'
@@ -723,11 +723,7 @@ export class ChatController {
723723
requiresAcceptance: false,
724724
})
725725
const output = await ToolUtils.invoke(tool, chatStream)
726-
if (output.output.content.length > maxToolOutputCharacterLength) {
727-
throw Error(
728-
`Tool output exceeds maximum character limit of ${maxToolOutputCharacterLength}`
729-
)
730-
}
726+
ToolUtils.validateOutput(output)
731727

732728
toolResults.push({
733729
content: [

packages/core/src/codewhispererChat/tools/fsRead.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import * as vscode from 'vscode'
66
import { getLogger } from '../../shared/logger/logger'
77
import fs from '../../shared/fs/fs'
8-
import { InvokeOutput, maxToolResponseSize, OutputKind, sanitizePath } from './toolShared'
8+
import { InvokeOutput, OutputKind, sanitizePath } from './toolShared'
99
import { Writable } from 'stream'
1010
import path from 'path'
1111

@@ -89,7 +89,7 @@ export class FsRead {
8989
private handleFileRange(fullText: string): InvokeOutput {
9090
if (!this.readRange || this.readRange.length === 0) {
9191
this.logger.info('No range provided. returning entire file.')
92-
return this.createOutput(this.enforceMaxSize(fullText))
92+
return this.createOutput(fullText)
9393
}
9494

9595
const lines = fullText.split('\n')
@@ -101,7 +101,7 @@ export class FsRead {
101101

102102
this.logger.info(`Reading file: ${this.fsPath}, lines ${start + 1}-${end + 1}`)
103103
const slice = lines.slice(start, end + 1).join('\n')
104-
return this.createOutput(this.enforceMaxSize(slice))
104+
return this.createOutput(slice)
105105
}
106106

107107
private parseLineRange(lineCount: number, range: number[]): [number, number] {
@@ -121,17 +121,6 @@ export class FsRead {
121121
return [finalStart, finalEnd]
122122
}
123123

124-
private enforceMaxSize(content: string): string {
125-
const byteCount = Buffer.byteLength(content, 'utf8')
126-
if (byteCount > maxToolResponseSize) {
127-
throw new Error(
128-
`This tool only supports reading ${maxToolResponseSize} bytes at a time.
129-
You tried to read ${byteCount} bytes. Try executing with fewer lines specified.`
130-
)
131-
}
132-
return content
133-
}
134-
135124
private createOutput(content: string): InvokeOutput {
136125
return {
137126
output: {

packages/core/src/codewhispererChat/tools/toolShared.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import path from 'path'
77
import fs from '../../shared/fs/fs'
88

9-
export const maxToolResponseSize = 30720 // 30KB
10-
export const maxToolOutputCharacterLength = 800_000
9+
export const maxToolResponseSize = 800_000
1110

1211
export enum OutputKind {
1312
Text = 'text',

packages/core/src/codewhispererChat/tools/toolUtils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { FsRead, FsReadParams } from './fsRead'
77
import { FsWrite, FsWriteParams } from './fsWrite'
88
import { CommandValidation, ExecuteBash, ExecuteBashParams } from './executeBash'
99
import { ToolResult, ToolResultContentBlock, ToolResultStatus, ToolUse } from '@amzn/codewhisperer-streaming'
10-
import { InvokeOutput } from './toolShared'
10+
import { InvokeOutput, maxToolResponseSize } from './toolShared'
1111
import { ListDirectory, ListDirectoryParams } from './listDirectory'
1212

1313
export enum ToolType {
@@ -63,6 +63,12 @@ export class ToolUtils {
6363
}
6464
}
6565

66+
static validateOutput(output: InvokeOutput): void {
67+
if (output.output.content.length > maxToolResponseSize) {
68+
throw Error(`Tool output exceeds maximum character limit of ${maxToolResponseSize}`)
69+
}
70+
}
71+
6672
static async queueDescription(tool: Tool, updates: Writable): Promise<void> {
6773
switch (tool.type) {
6874
case ToolType.FsRead:

packages/core/src/test/codewhispererChat/tools/fsRead.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,6 @@ describe('FsRead Tool', () => {
5454
)
5555
})
5656

57-
it('throws error if content exceeds 30KB', async function () {
58-
const bigContent = 'x'.repeat(35_000)
59-
const bigFilePath = await testFolder.write('bigFile.txt', bigContent)
60-
61-
const fsRead = new FsRead({ path: bigFilePath })
62-
await fsRead.validate()
63-
64-
await assert.rejects(
65-
fsRead.invoke(process.stdout),
66-
/This tool only supports reading \d+ bytes at a time/i,
67-
'Expected a size-limit error'
68-
)
69-
})
70-
7157
it('invalid line range', async () => {
7258
const filePath = await testFolder.write('rangeTest.txt', '1\n2\n3')
7359
const fsRead = new FsRead({ path: filePath, readRange: [3, 2] })

packages/core/src/test/codewhispererChat/tools/toolShared.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,30 @@ describe('ToolUtils', function () {
155155
})
156156
})
157157

158+
describe('validateOutput', function () {
159+
it('does not throw error if output is within size limit', function () {
160+
const output: InvokeOutput = {
161+
output: {
162+
kind: OutputKind.Text,
163+
content: 'a'.repeat(700_000),
164+
},
165+
}
166+
assert.doesNotThrow(() => ToolUtils.validateOutput(output))
167+
})
168+
it('throws error if output exceeds max size', function () {
169+
const output: InvokeOutput = {
170+
output: {
171+
kind: OutputKind.Text,
172+
content: 'a'.repeat(900_000), // 900,000 characters
173+
},
174+
}
175+
assert.throws(() => ToolUtils.validateOutput(output), {
176+
name: 'Error',
177+
message: 'Tool output exceeds maximum character limit of 800000',
178+
})
179+
})
180+
})
181+
158182
describe('queueDescription', function () {
159183
// TODO: Adding "void" to the following tests for the current implementation but in the next followup PR I will fix this issue.
160184
it('delegates to FsRead tool queueDescription method', function () {

0 commit comments

Comments
 (0)