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