@@ -16,6 +16,7 @@ import {
1616 ChatTriggerType ,
1717 EditorContextExtractor ,
1818 PromptMessage ,
19+ TriggerEvent ,
1920 TriggerEventsStorage ,
2021 TriggerPayload ,
2122 triggerPayloadToChatRequest ,
@@ -30,6 +31,7 @@ import { extractAuthFollowUp } from 'aws-core-vscode/amazonq'
3031import { InlineChatParams , InlineChatResult } from '@aws/language-server-runtimes-types'
3132import { decryptResponse , encryptRequest } from '../../lsp/encryption'
3233import { getCursorState } from '../../lsp/utils'
34+ import { CwsprChatTriggerInteraction , telemetry } from 'aws-core-vscode/telemetry'
3335
3436export class InlineChatProvider {
3537 private readonly editorContextExtractor : EditorContextExtractor
@@ -68,7 +70,34 @@ export class InlineChatProvider {
6870 }
6971 }
7072
73+ private getTriggerInteractionFromTriggerEvent ( triggerEvent : TriggerEvent | undefined ) : CwsprChatTriggerInteraction {
74+ switch ( triggerEvent ?. type ) {
75+ case 'editor_context_command' :
76+ return triggerEvent . command ?. triggerType === 'keybinding' ? 'hotkeys' : 'contextMenu'
77+ case 'follow_up' :
78+ case 'chat_message' :
79+ default :
80+ return 'click'
81+ }
82+ }
83+
7184 public async processPromptMessageLSP ( message : PromptMessage ) : Promise < InlineChatResult > {
85+ const triggerInteraction = this . getTriggerInteractionFromTriggerEvent (
86+ this . triggerEventsStorage . getLastTriggerEventByTabID ( message . tabID )
87+ )
88+ if ( ! AuthUtil . instance . isSsoSession ( ) ) {
89+ telemetry . amazonq_messageResponseError . emit ( {
90+ result : 'Failed' ,
91+ cwsprChatConversationType : 'Chat' ,
92+ cwsprChatRequestLength : message . message ?. length ?? 0 ,
93+ cwsprChatResponseCode : 401 ,
94+ cwsprChatTriggerInteraction : triggerInteraction ,
95+ reason : 'AuthenticationError' ,
96+ reasonDesc : 'Inline chat requires SSO authentication, but current session is not' ,
97+ } )
98+ throw new ToolkitError ( 'Inline chat is only available with SSO authentication' )
99+ }
100+
72101 // TODO: handle partial responses.
73102 getLogger ( ) . info ( 'Making inline chat request with message %O' , message )
74103 const params = this . getCurrentEditorParams ( message . message ?? '' )
@@ -83,6 +112,23 @@ export class InlineChatProvider {
83112
84113 // TODO: remove in favor of LSP implementation.
85114 public async processPromptMessage ( message : PromptMessage ) {
115+ const triggerInteraction = this . getTriggerInteractionFromTriggerEvent (
116+ this . triggerEventsStorage . getLastTriggerEventByTabID ( message . tabID )
117+ )
118+ if ( ! AuthUtil . instance . isSsoSession ( ) ) {
119+ telemetry . amazonq_messageResponseError . emit ( {
120+ result : 'Failed' ,
121+ cwsprChatConversationType : 'Chat' ,
122+ cwsprChatRequestLength : message . message ?. length ?? 0 ,
123+ cwsprChatResponseCode : 401 ,
124+ cwsprChatTriggerInteraction : triggerInteraction ,
125+ reason : 'AuthenticationError' ,
126+ reasonDesc : 'Inline chat requires SSO authentication, but current session is not' ,
127+ credentialStartUrl : AuthUtil . instance . connection ?. startUrl ,
128+ } )
129+ throw new ToolkitError ( 'Inline chat is only available with SSO authentication' )
130+ }
131+
86132 return this . editorContextExtractor
87133 . extractContextForTrigger ( 'ChatMessage' )
88134 . then ( ( context ) => {
@@ -143,7 +189,7 @@ export class InlineChatProvider {
143189 private async generateResponse (
144190 triggerPayload : TriggerPayload & { projectContextQueryLatencyMs ?: number } ,
145191 triggerID : string
146- ) {
192+ ) : Promise < GenerateAssistantResponseCommandOutput | undefined > {
147193 const triggerEvent = this . triggerEventsStorage . getTriggerEvent ( triggerID )
148194 if ( triggerEvent === undefined ) {
149195 return
@@ -182,7 +228,12 @@ export class InlineChatProvider {
182228 let response : GenerateAssistantResponseCommandOutput | undefined = undefined
183229 session . createNewTokenSource ( )
184230 try {
185- response = await session . chatSso ( request )
231+ if ( AuthUtil . instance . isSsoSession ( ) ) {
232+ response = await session . chatSso ( request )
233+ } else {
234+ // Call sendMessage because Q Developer Streaming Client does not have generateAssistantResponse
235+ throw new ToolkitError ( 'Inline chat is only available with SSO authentication' )
236+ }
186237 getLogger ( ) . info (
187238 `response to tab: ${ tabID } conversationID: ${ session . sessionIdentifier } requestID: ${ response . $metadata . requestId } metadata: %O` ,
188239 response . $metadata
0 commit comments