@@ -223,6 +223,55 @@ async function initializeAuth(client: LanguageClient): Promise<AmazonQLspAuth> {
223
223
return auth
224
224
}
225
225
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
+
226
275
async function onLanguageServerReady (
227
276
extensionContext : vscode . ExtensionContext ,
228
277
auth : AmazonQLspAuth ,
@@ -254,14 +303,7 @@ async function onLanguageServerReady(
254
303
// We manually push the cached values the first time since event handlers, which should push, may not have been setup yet.
255
304
// Execution order is weird and should be fixed in the flare implementation.
256
305
// 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' )
265
307
266
308
toDispose . push (
267
309
inlineManager ,
@@ -355,13 +397,6 @@ async function onLanguageServerReady(
355
397
// Set this inside onReady so that it only triggers on subsequent language server starts (not the first)
356
398
onServerRestartHandler ( client , auth )
357
399
)
358
-
359
- async function sendProfileToLsp ( client : LanguageClient ) {
360
- await pushConfigUpdate ( client , {
361
- type : 'profile' ,
362
- profileArn : AuthUtil . instance . regionProfileManager . activeRegionProfile ?. arn ,
363
- } )
364
- }
365
400
}
366
401
367
402
/**
@@ -381,8 +416,21 @@ function onServerRestartHandler(client: LanguageClient, auth: AmazonQLspAuth) {
381
416
// TODO: Port this metric override to common definitions
382
417
telemetry . languageServer_crash . emit ( { id : 'AmazonQ' } )
383
418
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
+ }
386
434
} )
387
435
}
388
436
0 commit comments