Skip to content

Commit c17a7fd

Browse files
committed
Trying another approach for read and list tool execution
1 parent a6cbf02 commit c17a7fd

File tree

6 files changed

+19
-21
lines changed

6 files changed

+19
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ export class ChatController {
787787
chatStream,
788788
ConversationTracker.getInstance().getTokenForTrigger(triggerID)
789789
)
790-
ToolUtils.validateOutput(output)
790+
ToolUtils.validateOutput(output, tool.type)
791791

792792
let status: ToolResultStatus = ToolResultStatus.SUCCESS
793793
if (output.output.success === false) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,11 @@ export class Messenger {
355355
isReadOrList,
356356
changeList
357357
)
358-
await ToolUtils.queueDescription(tool, chatStream)
358+
await ToolUtils.queueDescription(
359+
tool,
360+
chatStream,
361+
chatStream.validation.requiresAcceptance
362+
)
359363
if (session.messageIdToUpdate === undefined && tool.type === ToolType.FsRead) {
360364
// Store the first messageId in a chain of tool uses
361365
session.setMessageIdToUpdate(toolUse.toolUseId)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { Writable } from 'stream'
99
import { InvokeOutput, OutputKind, sanitizePath, CommandValidation, fsReadToolResponseSize } from './toolShared'
1010
import { isInDirectory } from '../../shared/filesystemUtilities'
1111
import path from 'path'
12-
import { ChatStream } from './chatStream'
1312

1413
export interface FsReadParams {
1514
path: string
@@ -49,8 +48,8 @@ export class FsRead {
4948
this.logger.debug(`Validation succeeded for path: ${this.fsPath}`)
5049
}
5150

52-
public queueDescription(updates: ChatStream): void {
53-
if (updates.validation.requiresAcceptance) {
51+
public queueDescription(updates: Writable, requiresAcceptance: boolean): void {
52+
if (requiresAcceptance) {
5453
const fileName = path.basename(this.fsPath)
5554
const fileUri = vscode.Uri.file(this.fsPath)
5655
updates.write(`Reading file: [${fileName}](${fileUri}), `)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Writable } from 'stream'
1010
import path from 'path'
1111
import { InvokeOutput, OutputKind, sanitizePath, CommandValidation } from './toolShared'
1212
import { isInDirectory } from '../../shared/filesystemUtilities'
13-
import { ChatStream } from './chatStream'
1413

1514
export interface ListDirectoryParams {
1615
path: string
@@ -50,8 +49,8 @@ export class ListDirectory {
5049
}
5150
}
5251

53-
public queueDescription(updates: ChatStream): void {
54-
if (updates.validation.requiresAcceptance) {
52+
public queueDescription(updates: Writable, requiresAcceptance: boolean): void {
53+
if (requiresAcceptance) {
5554
const fileName = path.basename(this.fsPath)
5655
if (this.maxDepth === undefined) {
5756
updates.write(`Analyzing directories recursively: ${fileName}`)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
fsReadToolResponseSize,
1616
} from './toolShared'
1717
import { ListDirectory, ListDirectoryParams } from './listDirectory'
18-
import { ChatStream } from './chatStream'
1918

2019
export enum ToolType {
2120
FsRead = 'fsRead',
@@ -96,10 +95,10 @@ export class ToolUtils {
9695
}
9796
}
9897

99-
static async queueDescription(tool: Tool, updates: ChatStream): Promise<void> {
98+
static async queueDescription(tool: Tool, updates: Writable, requiresAcceptance: boolean): Promise<void> {
10099
switch (tool.type) {
101100
case ToolType.FsRead:
102-
tool.tool.queueDescription(updates)
101+
tool.tool.queueDescription(updates, requiresAcceptance)
103102
break
104103
case ToolType.FsWrite:
105104
await tool.tool.queueDescription(updates)
@@ -108,7 +107,7 @@ export class ToolUtils {
108107
tool.tool.queueDescription(updates)
109108
break
110109
case ToolType.ListDirectory:
111-
tool.tool.queueDescription(updates)
110+
tool.tool.queueDescription(updates, requiresAcceptance)
112111
break
113112
}
114113
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { ToolUse } from '@amzn/codewhisperer-streaming'
2222
import path from 'path'
2323
import fs from '../../../shared/fs/fs'
2424
import { ListDirectory } from '../../../codewhispererChat/tools/listDirectory'
25-
import { ChatStream } from '../../../codewhispererChat/tools/chatStream'
2625

2726
describe('ToolUtils', function () {
2827
let sandbox: sinon.SinonSandbox
@@ -31,7 +30,6 @@ describe('ToolUtils', function () {
3130
let mockExecuteBash: sinon.SinonStubbedInstance<ExecuteBash>
3231
let mockListDirectory: sinon.SinonStubbedInstance<ListDirectory>
3332
let mockWritable: sinon.SinonStubbedInstance<Writable>
34-
let mockChatStream: sinon.SinonStubbedInstance<ChatStream>
3533

3634
beforeEach(function () {
3735
sandbox = sinon.createSandbox()
@@ -42,7 +40,6 @@ describe('ToolUtils', function () {
4240
mockWritable = {
4341
write: sandbox.stub(),
4442
} as unknown as sinon.SinonStubbedInstance<Writable>
45-
mockChatStream = sandbox.createStubInstance(ChatStream)
4643
;(mockFsRead.requiresAcceptance as sinon.SinonStub).returns({ requiresAcceptance: false })
4744
;(mockListDirectory.requiresAcceptance as sinon.SinonStub).returns({ requiresAcceptance: false })
4845
})
@@ -237,30 +234,30 @@ describe('ToolUtils', function () {
237234
// TODO: Adding "void" to the following tests for the current implementation but in the next followup PR I will fix this issue.
238235
it('delegates to FsRead tool queueDescription method', function () {
239236
const tool: Tool = { type: ToolType.FsRead, tool: mockFsRead as unknown as FsRead }
240-
void ToolUtils.queueDescription(tool, mockWritable as unknown as ChatStream)
237+
void ToolUtils.queueDescription(tool, mockWritable as unknown as Writable, false)
241238

242-
assert(mockFsRead.queueDescription.calledOnceWith(mockChatStream))
239+
assert(mockFsRead.queueDescription.calledOnceWith(mockWritable, false))
243240
})
244241

245242
it('delegates to FsWrite tool queueDescription method', function () {
246243
const tool: Tool = { type: ToolType.FsWrite, tool: mockFsWrite as unknown as FsWrite }
247-
void ToolUtils.queueDescription(tool, mockWritable as unknown as ChatStream)
244+
void ToolUtils.queueDescription(tool, mockWritable as unknown as Writable, false)
248245

249246
assert(mockFsWrite.queueDescription.calledOnceWith(mockWritable))
250247
})
251248

252249
it('delegates to ExecuteBash tool queueDescription method', function () {
253250
const tool: Tool = { type: ToolType.ExecuteBash, tool: mockExecuteBash as unknown as ExecuteBash }
254-
void ToolUtils.queueDescription(tool, mockWritable as unknown as ChatStream)
251+
void ToolUtils.queueDescription(tool, mockWritable as unknown as Writable, false)
255252

256253
assert(mockExecuteBash.queueDescription.calledOnceWith(mockWritable))
257254
})
258255

259256
it('delegates to ListDirectory tool queueDescription method', function () {
260257
const tool: Tool = { type: ToolType.ListDirectory, tool: mockListDirectory as unknown as ListDirectory }
261-
void ToolUtils.queueDescription(tool, mockWritable as unknown as ChatStream)
258+
void ToolUtils.queueDescription(tool, mockWritable as unknown as Writable, false)
262259

263-
assert(mockListDirectory.queueDescription.calledOnceWith(mockChatStream))
260+
assert(mockListDirectory.queueDescription.calledOnceWith(mockWritable, false))
264261
})
265262
})
266263

0 commit comments

Comments
 (0)