@@ -31,42 +31,11 @@ import {welcomeScreenTabData} from "./walkthrough/welcome";
3131import  {  agentWalkthroughDataModel  }  from  './walkthrough/agent' 
3232import  { createClickTelemetry ,  createOpenAgentTelemetry }  from  "./telemetry/actions" ; 
3333import  { disclaimerAcknowledgeButtonId ,  disclaimerCard }  from  "./texts/disclaimer" ; 
34+ import  { FeatureContext ,  tryNewMap }  from  "./types" ; 
35+ 
3436
3537
3638// Ref: https://github.com/aws/aws-toolkit-vscode/blob/e9ea8082ffe0b9968a873437407d0b6b31b9e1a5/packages/core/src/amazonq/webview/ui/main.ts 
37- export  const  createMynahUI  =  ( 
38-     ideApi : any , 
39-     showWelcomePage : boolean , 
40-     disclaimerAcknowledged : boolean , 
41-     isFeatureDevEnabled : boolean , 
42-     isCodeTransformEnabled : boolean , 
43-     isDocEnabled : boolean , 
44-     isCodeScanEnabled : boolean , 
45-     isCodeTestEnabled : boolean , 
46-     highlightCommand ?: QuickActionCommand , 
47-     profileName ?: string , 
48- 
49- )  =>  { 
50-     const  handler  =  new  WebviewUIHandler ( { 
51-         postMessage : ideApi . postMessage , 
52-         mynahUIRef : {  mynahUI : undefined  } , 
53-         showWelcomePage, 
54-         disclaimerAcknowledged, 
55-         isFeatureDevEnabled, 
56-         isCodeTransformEnabled, 
57-         isDocEnabled, 
58-         isCodeScanEnabled, 
59-         isCodeTestEnabled, 
60-         highlightCommand, 
61-         profileName, 
62-         hybridChat : false , 
63-     } ) 
64- 
65-     return  { 
66-         mynahUI : handler . mynahUI , 
67-         messageReceiver : handler . connector ?. handleMessageReceive , 
68-     } 
69- } 
7039
7140export  class  WebviewUIHandler  { 
7241    postMessage : any 
@@ -81,6 +50,7 @@ export class WebviewUIHandler {
8150    profileName ?: string 
8251    responseMetadata : Map < string ,  string [ ] > 
8352    tabsStorage : TabsStorage 
53+     featureConfigs ?: Map < string ,  FeatureContext > 
8454
8555    mynahUIProps : MynahUIProps 
8656    connector ?: Connector 
@@ -90,14 +60,14 @@ export class WebviewUIHandler {
9060    textMessageHandler ?: TextMessageHandler 
9161    messageController ?: MessageController 
9262
93-     savedContextCommands : MynahUIDataModel [ 'contextCommands' ] 
9463    disclaimerCardActive  : boolean 
9564
9665
9766    mynahUIRef : {  mynahUI : MynahUI  |  undefined  } 
9867    constructor ( { 
9968                    postMessage, 
10069                    mynahUIRef, 
70+                     featureConfigsSerialized, 
10171                    showWelcomePage, 
10272                    disclaimerAcknowledged, 
10373                    isFeatureDevEnabled, 
@@ -112,6 +82,7 @@ export class WebviewUIHandler {
11282                }  : { 
11383        postMessage : any 
11484        mynahUIRef : {  mynahUI : MynahUI  |  undefined  } 
85+         featureConfigsSerialized : [ string ,  FeatureContext ] [ ] 
11586        showWelcomePage : boolean , 
11687        disclaimerAcknowledged : boolean , 
11788        isFeatureDevEnabled : boolean 
@@ -127,6 +98,7 @@ export class WebviewUIHandler {
12798    } )  { 
12899        this . postMessage  =  postMessage 
129100        this . mynahUIRef  =  mynahUIRef 
101+         this . featureConfigs  =  tryNewMap ( featureConfigsSerialized ) 
130102        this . showWelcomePage  =  showWelcomePage ; 
131103        this . disclaimerAcknowledged  =  disclaimerAcknowledged 
132104        this . isFeatureDevEnabled  =  isFeatureDevEnabled 
@@ -206,6 +178,8 @@ export class WebviewUIHandler {
206178                    profileName
207179                } ) 
208180
181+                 this . featureConfigs  =  tryNewMap ( featureConfigsSerialized ) 
182+ 
209183                // Set the new defaults for the quick action commands in all tabs now that isFeatureDevEnabled and isCodeTransformEnabled were enabled/disabled 
210184                for  ( const  tab  of  this . tabsStorage . getTabs ( ) )  { 
211185                    this . mynahUI ?. updateStore ( tab . id ,  { 
@@ -238,7 +212,17 @@ export class WebviewUIHandler {
238212            onCWCOnboardingPageInteractionMessage : ( message : ChatItem ) : string  |  undefined  =>  { 
239213                return  this . messageController ?. sendMessageToTab ( message ,  'cwc' ) 
240214            } , 
215+             onQuickHandlerCommand : ( tabID : string ,  command ?: string ,  eventId ?: string )  =>  { 
216+                 this . tabsStorage . updateTabLastCommand ( tabID ,  command ) 
217+                 if  ( command  ===  'aws.awsq.transform' )  { 
218+                     this . quickActionHandler ?. handleCommand ( {  command : '/transform'  } ,  tabID ,  eventId ) 
219+                 }  else  if  ( command  ===  'aws.awsq.clearchat' )  { 
220+                     this . quickActionHandler ?. handleCommand ( {  command : '/clear'  } ,  tabID ) 
221+                 } 
222+             } , 
241223            onCWCContextCommandMessage : ( message : ChatItem ,  command ?: string ) : string  |  undefined  =>  { 
224+                 const  selectedTab  =  this . tabsStorage . getSelectedTab ( ) 
225+                 this . tabsStorage . updateTabLastCommand ( selectedTab ?. id  ||  '' ,  command  ||  '' ) 
242226                if  ( command  ===  'aws.amazonq.sendToPrompt' )  { 
243227                    return  this . messageController ?. sendSelectedCodeToTab ( message ) 
244228                }  else  { 
@@ -398,7 +382,7 @@ export class WebviewUIHandler {
398382                    }  as  ChatItem ) 
399383                } 
400384            } , 
401-             onChatAnswerReceived : ( tabID : string ,  item : CWCChatItem )  =>  { 
385+             onChatAnswerReceived : ( tabID : string ,  item : CWCChatItem ,   messageData :  any )  =>  { 
402386                if  ( item . type  ===  ChatItemType . ANSWER_PART  ||  item . type  ===  ChatItemType . CODE_RESULT )  { 
403387                    this . mynahUI ?. updateLastChatAnswer ( tabID ,  { 
404388                        ...( item . messageId  !==  undefined  ? {  messageId : item . messageId  }  : { } ) , 
@@ -417,8 +401,12 @@ export class WebviewUIHandler {
417401                    return 
418402                } 
419403
420-                 if  ( item . body  !==  undefined  ||  item . relatedContent  !==  undefined  ||  item . followUp  !==  undefined )  { 
421-                     this . mynahUI ?. addChatItem ( tabID ,  item ) 
404+                 if  ( item . body  !==  undefined  ||  item . relatedContent  !==  undefined  ||  item . followUp  !==  undefined  ||  item . formItems  !==  undefined  ||  item . buttons  !==  undefined )  { 
405+                     this . mynahUI ?. addChatItem ( tabID ,  { 
406+                         ...item , 
407+                         messageId : item . messageId , 
408+                         codeBlockActions : this . getCodeBlockActions ( messageData ) , 
409+                     } ) 
422410                } 
423411
424412                if  ( 
@@ -543,7 +531,7 @@ export class WebviewUIHandler {
543531                this . tabsStorage . updateTabTypeFromUnknown ( newTabID ,  tabType ) 
544532                this . connector ?. onKnownTabOpen ( newTabID ) 
545533                this . connector ?. onUpdateTabType ( newTabID ) 
546- 
534+                  this . featureConfigs   =   tryNewMap ( featureConfigsSerialized ) 
547535                this . mynahUI ?. updateStore ( newTabID ,  this . tabDataGenerator ! . getTabData ( tabType ,  true ) ) 
548536            } , 
549537            onStartNewTransform : ( tabID : string )  =>  { 
@@ -903,6 +891,43 @@ export class WebviewUIHandler {
903891        } ) 
904892
905893    } 
894+ 
895+     private  getCodeBlockActions ( messageData : any )  { 
896+         // Show ViewDiff and AcceptDiff for allowedCommands in CWC 
897+         const  isEnabled  =  this . featureConfigs ?. get ( 'ViewDiffInChat' ) ?. variation  ===  'TREATMENT' 
898+         const  tab  =  this . tabsStorage . getTab ( messageData ?. tabID  ||  '' ) 
899+         const  allowedCommands  =  [ 
900+             'aws.amazonq.refactorCode' , 
901+             'aws.amazonq.fixCode' , 
902+             'aws.amazonq.optimizeCode' , 
903+             'aws.amazonq.sendToPrompt' , 
904+         ] 
905+         if  ( isEnabled  &&  tab ?. type  ===  'cwc'  &&  allowedCommands . includes ( tab . lastCommand  ||  '' ) )  { 
906+             return  { 
907+                 'insert-to-cursor' : undefined , 
908+                 accept_diff : { 
909+                     id : 'accept_diff' , 
910+                     label : 'Apply Diff' , 
911+                     icon : MynahIcons . OK_CIRCLED , 
912+                     data : messageData , 
913+                 } , 
914+                 view_diff : { 
915+                     id : 'view_diff' , 
916+                     label : 'View Diff' , 
917+                     icon : MynahIcons . EYE , 
918+                     data : messageData , 
919+                 } , 
920+             } 
921+         } 
922+         // Show only "Copy" option for codeblocks in Q Test Tab 
923+         if  ( tab ?. type  ===  'testgen' )  { 
924+             return  { 
925+                 'insert-to-cursor' : undefined , 
926+             } 
927+         } 
928+         // Default will show "Copy" and "Insert at cursor" for codeblocks 
929+         return  { } 
930+     } 
906931    get  mynahUI ( ) : MynahUI  |  undefined  { 
907932        return  this . mynahUIRef . mynahUI 
908933    } 
0 commit comments