@@ -7,17 +7,20 @@ import * as vscode from 'vscode'
77import { activateAmazonQCommon , amazonQContextPrefix , deactivateCommon } from './extension'
88import { DefaultAmazonQAppInitContext } from 'aws-core-vscode/amazonq'
99import { activate as activateQGumby } from 'aws-core-vscode/amazonqGumby'
10- import { ExtContext , globals , CrashMonitoring } from 'aws-core-vscode/shared'
10+ import { ExtContext , globals , CrashMonitoring , getLogger , isNetworkError , isSageMaker } from 'aws-core-vscode/shared'
1111import { filetypes , SchemaService } from 'aws-core-vscode/sharedNode'
1212import { updateDevMode } from 'aws-core-vscode/dev'
1313import { CommonAuthViewProvider } from 'aws-core-vscode/login'
1414import { isExtensionActive , VSCODE_EXTENSION_ID } from 'aws-core-vscode/utils'
1515import { registerSubmitFeedback } from 'aws-core-vscode/feedback'
1616import { DevOptions } from 'aws-core-vscode/dev'
17- import { Auth } from 'aws-core-vscode/auth'
17+ import { Auth , AuthUtils , getTelemetryMetadataForConn , isAnySsoConnection } from 'aws-core-vscode/auth'
1818import api from './api'
1919import { activate as activateCWChat } from './app/chat/activation'
2020import { beta } from 'aws-core-vscode/dev'
21+ import { activate as activateNotifications } from 'aws-core-vscode/notifications'
22+ import { AuthState , AuthUtil } from 'aws-core-vscode/codewhisperer'
23+ import { telemetry , AuthUserState } from 'aws-core-vscode/telemetry'
2124
2225export async function activate ( context : vscode . ExtensionContext ) {
2326 // IMPORTANT: No other code should be added to this function. Place it in one of the following 2 functions where appropriate.
@@ -61,6 +64,50 @@ async function activateAmazonQNode(context: vscode.ExtensionContext) {
6164
6265 await setupDevMode ( context )
6366 await beta . activate ( context )
67+
68+ // TODO: Should probably emit for web as well.
69+ // Will the web metric look the same?
70+ const authState = await getAuthState ( )
71+ telemetry . auth_userState . emit ( {
72+ passive : true ,
73+ result : 'Succeeded' ,
74+ source : AuthUtils . ExtensionUse . instance . sourceForTelemetry ( ) ,
75+ ...authState ,
76+ } )
77+
78+ await activateNotifications ( context , authState , getAuthState )
79+ }
80+
81+ async function getAuthState ( ) : Promise < Omit < AuthUserState , 'source' > > {
82+ let authState : AuthState = 'disconnected'
83+ try {
84+ // May call connection validate functions that try to refresh the token.
85+ // This could result in network errors.
86+ authState = ( await AuthUtil . instance . getChatAuthState ( false ) ) . codewhispererChat
87+ } catch ( err ) {
88+ if (
89+ isNetworkError ( err ) &&
90+ AuthUtil . instance . conn &&
91+ AuthUtil . instance . auth . getConnectionState ( AuthUtil . instance . conn ) === 'valid'
92+ ) {
93+ authState = 'connectedWithNetworkError'
94+ } else {
95+ throw err
96+ }
97+ }
98+ const currConn = AuthUtil . instance . conn
99+ if ( currConn !== undefined && ! ( isAnySsoConnection ( currConn ) || isSageMaker ( ) ) ) {
100+ getLogger ( ) . error ( `Current Amazon Q connection is not SSO, type is: %s` , currConn ?. type )
101+ }
102+
103+ return {
104+ authStatus :
105+ authState === 'connected' || authState === 'expired' || authState === 'connectedWithNetworkError'
106+ ? authState
107+ : 'notConnected' ,
108+ authEnabledConnections : AuthUtils . getAuthFormIdsFromConnection ( currConn ) . join ( ',' ) ,
109+ ...( await getTelemetryMetadataForConn ( currConn ) ) ,
110+ }
64111}
65112
66113/**
0 commit comments