Skip to content

Commit 7a525ae

Browse files
comment: Move Profile push up a level
- Moves the profile/customization code up a level out of the LSP chat activation, since it doesn't make sense there - Remove the previous onDidChangeProfile handler from the auth setup since it is redundant due to us having it already in onLanguageServerReady() - Renamed function to onLanguageServerReady() to be more aligned with flare naming Signed-off-by: nkomonen-amazon <[email protected]>
1 parent ebceeff commit 7a525ae

File tree

2 files changed

+20
-45
lines changed

2 files changed

+20
-45
lines changed

packages/amazonq/src/lsp/chat/activation.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@ import { pushConfigUpdate } from '../config'
1717
export async function activate(languageClient: LanguageClient, encryptionKey: Buffer, mynahUIPath: string) {
1818
const disposables = globals.context.subscriptions
1919

20-
// Make sure we've sent an auth profile to the language server before even initializing the UI
21-
//
22-
// Ideally the handler for onDidChangeRegionProfile would trigger this, but because the handler is set up too late (due to the current structure), the initial event is missed.
23-
// So we have to explicitly do it here on startup.
24-
if (AuthUtil.instance.isConnectionValid()) {
25-
await pushConfigUpdate(languageClient, {
26-
type: 'profile',
27-
profileArn: AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn,
28-
})
29-
30-
await pushConfigUpdate(languageClient, {
31-
type: 'customization',
32-
customization: getSelectedCustomization(),
33-
})
34-
}
35-
3620
const provider = new AmazonQChatViewProvider(mynahUIPath)
3721

3822
disposables.push(

packages/amazonq/src/lsp/client.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
GetConfigurationFromServerParams,
1717
RenameFilesParams,
1818
ResponseMessage,
19-
updateConfigurationRequestType,
2019
WorkspaceFolder,
2120
} from '@aws/language-server-runtimes/protocol'
2221
import { AuthUtil, CodeWhispererSettings, getSelectedCustomization } from 'aws-core-vscode/codewhisperer'
@@ -164,25 +163,18 @@ export async function startLanguageServer(
164163

165164
const auth = await initializeAuth(client)
166165

167-
await postStartLanguageServer(auth, client, resourcePaths, toDispose)
166+
await onLanguageServerReady(auth, client, resourcePaths, toDispose)
168167

169168
return client
170169
}
171170

172171
async function initializeAuth(client: LanguageClient): Promise<AmazonQLspAuth> {
173-
AuthUtil.instance.regionProfileManager.onDidChangeRegionProfile(async () => {
174-
void pushConfigUpdate(client, {
175-
type: 'profile',
176-
profileArn: AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn,
177-
})
178-
})
179-
180172
const auth = new AmazonQLspAuth(client)
181173
await auth.refreshConnection(true)
182174
return auth
183175
}
184176

185-
async function postStartLanguageServer(
177+
async function onLanguageServerReady(
186178
auth: AmazonQLspAuth,
187179
client: LanguageClient,
188180
resourcePaths: AmazonQResourcePaths,
@@ -208,25 +200,17 @@ async function postStartLanguageServer(
208200

209201
const refreshInterval = auth.startTokenRefreshInterval(10 * oneSecond)
210202

211-
const sendProfileToLsp = async () => {
212-
try {
213-
const result = await client.sendRequest(updateConfigurationRequestType.method, {
214-
section: 'aws.q',
215-
settings: {
216-
profileArn: AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn,
217-
},
218-
})
219-
client.info(
220-
`Client: Updated Amazon Q Profile ${AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn} to Amazon Q LSP`,
221-
result
222-
)
223-
} catch (err) {
224-
client.error('Error when setting Q Developer Profile to Amazon Q LSP', err)
225-
}
226-
}
203+
// We manually push the cached values the first time since event handlers, which should push, may not have been setup yet.
204+
// Execution order is weird and should be fixed in the flare implementation.
205+
// TODO: Revisit if we need this if we setup the event handlers properly
206+
if (AuthUtil.instance.isConnectionValid()) {
207+
await sendProfileToLsp(client)
227208

228-
// send profile to lsp once.
229-
void sendProfileToLsp()
209+
await pushConfigUpdate(client, {
210+
type: 'customization',
211+
customization: getSelectedCustomization(),
212+
})
213+
}
230214

231215
toDispose.push(
232216
AuthUtil.instance.auth.onDidChangeActiveConnection(async () => {
@@ -235,7 +219,7 @@ async function postStartLanguageServer(
235219
AuthUtil.instance.auth.onDidDeleteConnection(async () => {
236220
client.sendNotification(notificationTypes.deleteBearerToken.method)
237221
}),
238-
AuthUtil.instance.regionProfileManager.onDidChangeRegionProfile(sendProfileToLsp),
222+
AuthUtil.instance.regionProfileManager.onDidChangeRegionProfile(() => sendProfileToLsp(client)),
239223
vscode.commands.registerCommand('aws.amazonq.getWorkspaceId', async () => {
240224
const requestType = new RequestType<GetConfigurationFromServerParams, ResponseMessage, Error>(
241225
'aws/getConfigurationFromServer'
@@ -295,6 +279,13 @@ async function postStartLanguageServer(
295279
// Set this inside onReady so that it only triggers on subsequent language server starts (not the first)
296280
onServerRestartHandler(client, auth)
297281
)
282+
283+
async function sendProfileToLsp(client: LanguageClient) {
284+
await pushConfigUpdate(client, {
285+
type: 'profile',
286+
profileArn: AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn,
287+
})
288+
}
298289
}
299290

300291
/**

0 commit comments

Comments
 (0)