@@ -264,6 +264,59 @@ async function initializeAuth(client: LanguageClient): Promise<AmazonQLspAuth> {
264264 return auth
265265}
266266
267+ // jscpd:ignore-start
268+ async function initializeLanguageServerConfiguration ( client : LanguageClient , context : string = 'startup' ) {
269+ const logger = getLogger ( 'amazonqLsp' )
270+
271+ if ( AuthUtil . instance . isConnectionValid ( ) ) {
272+ logger . info ( `[${ context } ] Initializing language server configuration` )
273+ // jscpd:ignore-end
274+
275+ try {
276+ // Send profile configuration
277+ logger . debug ( `[${ context } ] Sending profile configuration to language server` )
278+ await sendProfileToLsp ( client )
279+ logger . debug ( `[${ context } ] Profile configuration sent successfully` )
280+
281+ // Send customization configuration
282+ logger . debug ( `[${ context } ] Sending customization configuration to language server` )
283+ await pushConfigUpdate ( client , {
284+ type : 'customization' ,
285+ customization : getSelectedCustomization ( ) ,
286+ } )
287+ logger . debug ( `[${ context } ] Customization configuration sent successfully` )
288+
289+ logger . info ( `[${ context } ] Language server configuration completed successfully` )
290+ } catch ( error ) {
291+ logger . error ( `[${ context } ] Failed to initialize language server configuration: ${ error } ` )
292+ throw error
293+ }
294+ } else {
295+ logger . warn (
296+ `[${ context } ] Connection invalid, skipping language server configuration - this will cause authentication failures`
297+ )
298+ const activeConnection = AuthUtil . instance . auth . activeConnection
299+ const connectionState = activeConnection
300+ ? AuthUtil . instance . auth . getConnectionState ( activeConnection )
301+ : 'no-connection'
302+ logger . warn ( `[${ context } ] Connection state: ${ connectionState } ` )
303+ }
304+ }
305+
306+ async function sendProfileToLsp ( client : LanguageClient ) {
307+ const logger = getLogger ( 'amazonqLsp' )
308+ const profileArn = AuthUtil . instance . regionProfileManager . activeRegionProfile ?. arn
309+
310+ logger . debug ( `Sending profile to LSP: ${ profileArn || 'undefined' } ` )
311+
312+ await pushConfigUpdate ( client , {
313+ type : 'profile' ,
314+ profileArn : profileArn ,
315+ } )
316+
317+ logger . debug ( `Profile sent to LSP successfully` )
318+ }
319+
267320async function onLanguageServerReady (
268321 extensionContext : vscode . ExtensionContext ,
269322 auth : AmazonQLspAuth ,
@@ -295,14 +348,7 @@ async function onLanguageServerReady(
295348 // We manually push the cached values the first time since event handlers, which should push, may not have been setup yet.
296349 // Execution order is weird and should be fixed in the flare implementation.
297350 // TODO: Revisit if we need this if we setup the event handlers properly
298- if ( AuthUtil . instance . isConnectionValid ( ) ) {
299- await sendProfileToLsp ( client )
300-
301- await pushConfigUpdate ( client , {
302- type : 'customization' ,
303- customization : getSelectedCustomization ( ) ,
304- } )
305- }
351+ await initializeLanguageServerConfiguration ( client , 'startup' )
306352
307353 toDispose . push (
308354 inlineManager ,
@@ -404,13 +450,6 @@ async function onLanguageServerReady(
404450 // Set this inside onReady so that it only triggers on subsequent language server starts (not the first)
405451 onServerRestartHandler ( client , auth )
406452 )
407-
408- async function sendProfileToLsp ( client : LanguageClient ) {
409- await pushConfigUpdate ( client , {
410- type : 'profile' ,
411- profileArn : AuthUtil . instance . regionProfileManager . activeRegionProfile ?. arn ,
412- } )
413- }
414453}
415454
416455/**
@@ -430,8 +469,21 @@ function onServerRestartHandler(client: LanguageClient, auth: AmazonQLspAuth) {
430469 // TODO: Port this metric override to common definitions
431470 telemetry . languageServer_crash . emit ( { id : 'AmazonQ' } )
432471
433- // Need to set the auth token in the again
434- await auth . refreshConnection ( true )
472+ const logger = getLogger ( 'amazonqLsp' )
473+ logger . info ( '[crash-recovery] Language server crash detected, reinitializing authentication' )
474+
475+ try {
476+ // Send bearer token
477+ logger . debug ( '[crash-recovery] Refreshing connection and sending bearer token' )
478+ await auth . refreshConnection ( true )
479+ logger . debug ( '[crash-recovery] Bearer token sent successfully' )
480+
481+ // Send profile and customization configuration
482+ await initializeLanguageServerConfiguration ( client , 'crash-recovery' )
483+ logger . info ( '[crash-recovery] Authentication reinitialized successfully' )
484+ } catch ( error ) {
485+ logger . error ( `[crash-recovery] Failed to reinitialize after crash: ${ error } ` )
486+ }
435487 } )
436488}
437489
0 commit comments