@@ -223,6 +223,55 @@ async function initializeAuth(client: LanguageClient): Promise<AmazonQLspAuth> {
223223 return auth
224224}
225225
226+ async function initializeLanguageServerConfiguration ( client : LanguageClient , context : string = 'startup' ) {
227+ const logger = getLogger ( 'amazonqLsp' )
228+
229+ if ( AuthUtil . instance . isConnectionValid ( ) ) {
230+ logger . info ( `[${ context } ] Connection valid, initializing language server configuration` )
231+
232+ try {
233+ // Send profile configuration
234+ logger . info ( `[${ context } ] Sending profile configuration to language server` )
235+ await sendProfileToLsp ( client )
236+ logger . info ( `[${ context } ] Profile configuration sent successfully` )
237+
238+ // Send customization configuration
239+ logger . info ( `[${ context } ] Sending customization configuration to language server` )
240+ await pushConfigUpdate ( client , {
241+ type : 'customization' ,
242+ customization : getSelectedCustomization ( ) ,
243+ } )
244+ logger . info ( `[${ context } ] Customization configuration sent successfully` )
245+ } catch ( error ) {
246+ logger . error ( `[${ context } ] Failed to initialize language server configuration: ${ error } ` )
247+ throw error
248+ }
249+ } else {
250+ logger . warn (
251+ `[${ context } ] Connection invalid, skipping language server configuration - this will cause authentication failures`
252+ )
253+ const activeConnection = AuthUtil . instance . auth . activeConnection
254+ const connectionState = activeConnection
255+ ? AuthUtil . instance . auth . getConnectionState ( activeConnection )
256+ : 'no-connection'
257+ logger . warn ( `[${ context } ] Connection state: ${ connectionState } ` )
258+ }
259+ }
260+
261+ async function sendProfileToLsp ( client : LanguageClient ) {
262+ const logger = getLogger ( 'amazonqLsp' )
263+ const profileArn = AuthUtil . instance . regionProfileManager . activeRegionProfile ?. arn
264+
265+ logger . debug ( `Sending profile to LSP: ${ profileArn || 'undefined' } ` )
266+
267+ await pushConfigUpdate ( client , {
268+ type : 'profile' ,
269+ profileArn : profileArn ,
270+ } )
271+
272+ logger . debug ( `Profile sent to LSP successfully` )
273+ }
274+
226275async function onLanguageServerReady (
227276 extensionContext : vscode . ExtensionContext ,
228277 auth : AmazonQLspAuth ,
@@ -254,14 +303,7 @@ async function onLanguageServerReady(
254303 // We manually push the cached values the first time since event handlers, which should push, may not have been setup yet.
255304 // Execution order is weird and should be fixed in the flare implementation.
256305 // TODO: Revisit if we need this if we setup the event handlers properly
257- if ( AuthUtil . instance . isConnectionValid ( ) ) {
258- await sendProfileToLsp ( client )
259-
260- await pushConfigUpdate ( client , {
261- type : 'customization' ,
262- customization : getSelectedCustomization ( ) ,
263- } )
264- }
306+ await initializeLanguageServerConfiguration ( client , 'startup' )
265307
266308 toDispose . push (
267309 inlineManager ,
@@ -355,13 +397,6 @@ async function onLanguageServerReady(
355397 // Set this inside onReady so that it only triggers on subsequent language server starts (not the first)
356398 onServerRestartHandler ( client , auth )
357399 )
358-
359- async function sendProfileToLsp ( client : LanguageClient ) {
360- await pushConfigUpdate ( client , {
361- type : 'profile' ,
362- profileArn : AuthUtil . instance . regionProfileManager . activeRegionProfile ?. arn ,
363- } )
364- }
365400}
366401
367402/**
@@ -381,8 +416,21 @@ function onServerRestartHandler(client: LanguageClient, auth: AmazonQLspAuth) {
381416 // TODO: Port this metric override to common definitions
382417 telemetry . languageServer_crash . emit ( { id : 'AmazonQ' } )
383418
384- // Need to set the auth token in the again
385- await auth . refreshConnection ( true )
419+ const logger = getLogger ( 'amazonqLsp' )
420+ logger . info ( '[crash-recovery] Language server crash detected, reinitializing authentication' )
421+
422+ try {
423+ // Send bearer token
424+ logger . info ( '[crash-recovery] Refreshing connection and sending bearer token' )
425+ await auth . refreshConnection ( true )
426+ logger . info ( '[crash-recovery] Bearer token sent successfully' )
427+
428+ // Send profile and customization configuration
429+ await initializeLanguageServerConfiguration ( client , 'crash-recovery' )
430+ logger . info ( '[crash-recovery] Language server configuration reinitialized successfully' )
431+ } catch ( error ) {
432+ logger . error ( `[crash-recovery] Failed to reinitialize after crash: ${ error } ` )
433+ }
386434 } )
387435}
388436
0 commit comments