@@ -45,7 +45,7 @@ import { EditorContextCommand } from '../../commands/registerCommands'
45
45
import { PromptsGenerator } from './prompts/promptsGenerator'
46
46
import { TriggerEventsStorage } from '../../storages/triggerEvents'
47
47
import { SendMessageRequest } from '@amzn/amazon-q-developer-streaming-client'
48
- import { CodeWhispererStreamingServiceException } from '@amzn/codewhisperer-streaming'
48
+ import { CodeWhispererStreamingServiceException , Origin , ToolResult } from '@amzn/codewhisperer-streaming'
49
49
import { UserIntentRecognizer } from './userIntent/userIntentRecognizer'
50
50
import { CWCTelemetryHelper , recordTelemetryChatRunCommand } from './telemetryHelper'
51
51
import { CodeWhispererTracker } from '../../../codewhisperer/tracker/codewhispererTracker'
@@ -81,6 +81,7 @@ import {
81
81
} from '../../constants'
82
82
import { ChatSession } from '../../clients/chat/v0/chat'
83
83
import { ChatHistoryManager } from '../../storages/chatHistory'
84
+ import { FsRead , FsReadParams } from '../../tools/fsRead'
84
85
85
86
export interface ChatControllerMessagePublishers {
86
87
readonly processPromptChatMessage : MessagePublisher < PromptMessage >
@@ -577,6 +578,8 @@ export class ChatController {
577
578
const newFileDoc = await vscode . workspace . openTextDocument ( newFilePath )
578
579
await vscode . window . showTextDocument ( newFileDoc )
579
580
telemetry . ui_click . emit ( { elementId : 'amazonq_createSavedPrompt' } )
581
+ } else if ( message . action . id === 'confirm-tool-use' ) {
582
+ await this . processToolUseMessage ( message )
580
583
}
581
584
}
582
585
@@ -834,10 +837,108 @@ export class ChatController {
834
837
}
835
838
}
836
839
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
+
837
938
private async processPromptMessageAsNewThread ( message : PromptMessage ) {
838
939
this . editorContextExtractor
839
940
. extractContextForTrigger ( 'ChatMessage' )
840
- . then ( ( context ) => {
941
+ . then ( async ( context ) => {
841
942
const triggerID = randomUUID ( )
842
943
this . triggerEventsStorage . addTriggerEvent ( {
843
944
id : triggerID ,
@@ -850,9 +951,10 @@ export class ChatController {
850
951
userInputMessage : {
851
952
content : message . message ,
852
953
userIntent : message . userIntent ,
954
+ origin : Origin . IDE ,
853
955
} ,
854
956
} )
855
- return this . generateResponse (
957
+ await this . generateResponse (
856
958
{
857
959
message : message . message ,
858
960
trigger : ChatTriggerType . ChatMessage ,
@@ -867,6 +969,7 @@ export class ChatController {
867
969
customization : getSelectedCustomization ( ) ,
868
970
context : message . context ,
869
971
chatHistory : this . chatHistoryManager . getHistory ( ) ,
972
+ origin : Origin . IDE ,
870
973
} ,
871
974
triggerID
872
975
)
0 commit comments