@@ -45,7 +45,7 @@ import { EditorContextCommand } from '../../commands/registerCommands'
4545import { PromptsGenerator } from './prompts/promptsGenerator'
4646import { TriggerEventsStorage } from '../../storages/triggerEvents'
4747import { SendMessageRequest } from '@amzn/amazon-q-developer-streaming-client'
48- import { CodeWhispererStreamingServiceException } from '@amzn/codewhisperer-streaming'
48+ import { CodeWhispererStreamingServiceException , Origin , ToolResult } from '@amzn/codewhisperer-streaming'
4949import { UserIntentRecognizer } from './userIntent/userIntentRecognizer'
5050import { CWCTelemetryHelper , recordTelemetryChatRunCommand } from './telemetryHelper'
5151import { CodeWhispererTracker } from '../../../codewhisperer/tracker/codewhispererTracker'
@@ -81,6 +81,7 @@ import {
8181} from '../../constants'
8282import { ChatSession } from '../../clients/chat/v0/chat'
8383import { ChatHistoryManager } from '../../storages/chatHistory'
84+ import { FsRead , FsReadParams } from '../../tools/fsRead'
8485
8586export interface ChatControllerMessagePublishers {
8687 readonly processPromptChatMessage : MessagePublisher < PromptMessage >
@@ -577,6 +578,8 @@ export class ChatController {
577578 const newFileDoc = await vscode . workspace . openTextDocument ( newFilePath )
578579 await vscode . window . showTextDocument ( newFileDoc )
579580 telemetry . ui_click . emit ( { elementId : 'amazonq_createSavedPrompt' } )
581+ } else if ( message . action . id === 'confirm-tool-use' ) {
582+ await this . processToolUseMessage ( message )
580583 }
581584 }
582585
@@ -834,10 +837,108 @@ export class ChatController {
834837 }
835838 }
836839
840+ private async processToolUseMessage ( message : CustomFormActionMessage ) {
841+ const tabID = message . tabID
842+ if ( ! tabID ) {
843+ return
844+ }
845+ this . editorContextExtractor
846+ . extractContextForTrigger ( 'ChatMessage' )
847+ . then ( async ( context ) => {
848+ const triggerID = randomUUID ( )
849+ this . triggerEventsStorage . addTriggerEvent ( {
850+ id : triggerID ,
851+ tabID : message . tabID ,
852+ message : undefined ,
853+ type : 'chat_message' ,
854+ context,
855+ } )
856+ const session = this . sessionStorage . getSession ( tabID )
857+ const toolUse = session . toolUse
858+ if ( ! toolUse || ! toolUse . input ) {
859+ return
860+ }
861+ session . setToolUse ( undefined )
862+
863+ let result : any
864+ const toolResults : ToolResult [ ] = [ ]
865+ try {
866+ switch ( toolUse . name ) {
867+ // case 'execute_bash': {
868+ // const executeBash = new ExecuteBash(toolUse.input as unknown as ExecuteBashParams)
869+ // await executeBash.validate()
870+ // result = await executeBash.invoke(process.stdout)
871+ // break
872+ // }
873+ case 'fs_read' : {
874+ const fsRead = new FsRead ( toolUse . input as unknown as FsReadParams )
875+ await fsRead . validate ( )
876+ result = await fsRead . invoke ( )
877+ break
878+ }
879+ // case 'fs_write': {
880+ // const fsWrite = new FsWrite(toolUse.input as unknown as FsWriteParams)
881+ // const ctx = new DefaultContext()
882+ // result = await fsWrite.invoke(ctx, process.stdout)
883+ // break
884+ // }
885+ // case 'open_file': {
886+ // result = await openFile(toolUse.input as unknown as OpenFileParams)
887+ // break
888+ // }
889+ default :
890+ break
891+ }
892+ toolResults . push ( {
893+ content : [
894+ result . output . kind === 'text'
895+ ? { text : result . output . content }
896+ : { json : result . output . content } ,
897+ ] ,
898+ toolUseId : toolUse . toolUseId ,
899+ status : 'success' ,
900+ } )
901+ } catch ( e : any ) {
902+ toolResults . push ( { content : [ { text : e . message } ] , toolUseId : toolUse . toolUseId , status : 'error' } )
903+ }
904+
905+ this . chatHistoryManager . appendUserMessage ( {
906+ userInputMessage : {
907+ content : 'Tool Results' ,
908+ userIntent : undefined ,
909+ origin : Origin . IDE ,
910+ } ,
911+ } )
912+
913+ await this . generateResponse (
914+ {
915+ message : 'Tool Results' ,
916+ trigger : ChatTriggerType . ChatMessage ,
917+ query : undefined ,
918+ codeSelection : context ?. focusAreaContext ?. selectionInsideExtendedCodeBlock ,
919+ fileText : context ?. focusAreaContext ?. extendedCodeBlock ,
920+ fileLanguage : context ?. activeFileContext ?. fileLanguage ,
921+ filePath : context ?. activeFileContext ?. filePath ,
922+ matchPolicy : context ?. activeFileContext ?. matchPolicy ,
923+ codeQuery : context ?. focusAreaContext ?. names ,
924+ userIntent : undefined ,
925+ customization : getSelectedCustomization ( ) ,
926+ context : undefined ,
927+ toolResults : toolResults ,
928+ origin : Origin . IDE ,
929+ } ,
930+ triggerID
931+ )
932+ } )
933+ . catch ( ( e ) => {
934+ this . processException ( e , tabID )
935+ } )
936+ }
937+
837938 private async processPromptMessageAsNewThread ( message : PromptMessage ) {
838939 this . editorContextExtractor
839940 . extractContextForTrigger ( 'ChatMessage' )
840- . then ( ( context ) => {
941+ . then ( async ( context ) => {
841942 const triggerID = randomUUID ( )
842943 this . triggerEventsStorage . addTriggerEvent ( {
843944 id : triggerID ,
@@ -850,9 +951,10 @@ export class ChatController {
850951 userInputMessage : {
851952 content : message . message ,
852953 userIntent : message . userIntent ,
954+ origin : Origin . IDE ,
853955 } ,
854956 } )
855- return this . generateResponse (
957+ await this . generateResponse (
856958 {
857959 message : message . message ,
858960 trigger : ChatTriggerType . ChatMessage ,
@@ -867,6 +969,7 @@ export class ChatController {
867969 customization : getSelectedCustomization ( ) ,
868970 context : message . context ,
869971 chatHistory : this . chatHistoryManager . getHistory ( ) ,
972+ origin : Origin . IDE ,
870973 } ,
871974 triggerID
872975 )
0 commit comments