Skip to content

Commit f7181f4

Browse files
authored
Merge branch 'aws:master' into multi-cell-context
2 parents 5f29d4a + 86c2f2a commit f7181f4

File tree

8 files changed

+163
-27
lines changed

8 files changed

+163
-27
lines changed

aws-toolkit-vscode.code-workspace

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"folders": [
3-
{
4-
"path": "."
5-
},
6-
{
7-
"path": "packages/toolkit"
8-
},
9-
{
10-
"path": "packages/core"
11-
},
12-
{
13-
"path": "packages/amazonq"
14-
}
15-
],
16-
"settings": {
17-
"typescript.tsdk": "node_modules/typescript/lib"
18-
}
19-
}
2+
"folders": [
3+
{
4+
"path": ".",
5+
},
6+
{
7+
"path": "packages/toolkit",
8+
},
9+
{
10+
"path": "packages/core",
11+
},
12+
{
13+
"path": "packages/amazonq",
14+
},
15+
],
16+
"settings": {
17+
"typescript.tsdk": "node_modules/typescript/lib",
18+
},
19+
}

packages/amazonq/src/extension.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { AuthUtils, CredentialsStore, LoginManager, initializeAuth } from 'aws-core-vscode/auth'
6+
import { Auth, AuthUtils, CredentialsStore, LoginManager, initializeAuth } from 'aws-core-vscode/auth'
77
import { activate as activateCodeWhisperer, shutdown as shutdownCodeWhisperer } from 'aws-core-vscode/codewhisperer'
88
import { makeEndpointsProvider, registerGenericCommands } from 'aws-core-vscode'
99
import { CommonAuthWebview } from 'aws-core-vscode/login'
@@ -43,6 +43,7 @@ import { registerCommands } from './commands'
4343
import { focusAmazonQPanel } from 'aws-core-vscode/codewhispererChat'
4444
import { activate as activateAmazonqLsp } from './lsp/activation'
4545
import { activate as activateInlineCompletion } from './app/inline/activation'
46+
import { isAmazonInternalOs } from 'aws-core-vscode/shared'
4647

4748
export const amazonQContextPrefix = 'amazonq'
4849

@@ -119,10 +120,13 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is
119120
}
120121
// This contains every lsp agnostic things (auth, security scan, code scan)
121122
await activateCodeWhisperer(extContext as ExtContext)
122-
if (Experiments.instance.get('amazonqLSP', false)) {
123+
if (
124+
(Experiments.instance.get('amazonqLSP', false) || Auth.instance.isInternalAmazonUser()) &&
125+
!isAmazonInternalOs()
126+
) {
127+
// start the Amazon Q LSP for internal users first
123128
await activateAmazonqLsp(context)
124129
}
125-
126130
if (!Experiments.instance.get('amazonqLSPInline', false)) {
127131
await activateInlineCompletion()
128132
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ import {
3232
ShowSaveFileDialogParams,
3333
LSPErrorCodes,
3434
tabBarActionRequestType,
35+
ShowDocumentParams,
36+
ShowDocumentResult,
37+
ShowDocumentRequest,
38+
contextCommandsNotificationType,
39+
ContextCommandParams,
3540
} from '@aws/language-server-runtimes/protocol'
3641
import { v4 as uuidv4 } from 'uuid'
3742
import * as vscode from 'vscode'
@@ -286,6 +291,23 @@ export function registerMessageListeners(
286291
targetUri: targetUri.toString(),
287292
}
288293
})
294+
295+
languageClient.onRequest<ShowDocumentParams, ShowDocumentResult>(
296+
ShowDocumentRequest.method,
297+
async (params: ShowDocumentParams): Promise<ShowDocumentParams | ResponseError<ShowDocumentResult>> => {
298+
const uri = vscode.Uri.parse(params.uri)
299+
const doc = await vscode.workspace.openTextDocument(uri)
300+
await vscode.window.showTextDocument(doc, { preview: false })
301+
return params
302+
}
303+
)
304+
305+
languageClient.onNotification(contextCommandsNotificationType.method, (params: ContextCommandParams) => {
306+
void provider.webview?.postMessage({
307+
command: contextCommandsNotificationType.method,
308+
params: params,
309+
})
310+
})
289311
}
290312

291313
function isServerEvent(command: string) {

packages/amazonq/src/lsp/client.ts

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,30 @@
66
import vscode, { env, version } from 'vscode'
77
import * as nls from 'vscode-nls'
88
import * as crypto from 'crypto'
9-
import { LanguageClient, LanguageClientOptions } from 'vscode-languageclient'
9+
import { LanguageClient, LanguageClientOptions, RequestType } from 'vscode-languageclient'
1010
import { InlineCompletionManager } from '../app/inline/completion'
1111
import { AmazonQLspAuth, encryptionKey, notificationTypes } from './auth'
1212
import { AuthUtil } from 'aws-core-vscode/codewhisperer'
13-
import { ConnectionMetadata } from '@aws/language-server-runtimes/protocol'
13+
import {
14+
ConnectionMetadata,
15+
CreateFilesParams,
16+
DeleteFilesParams,
17+
DidChangeWorkspaceFoldersParams,
18+
DidSaveTextDocumentParams,
19+
GetConfigurationFromServerParams,
20+
RenameFilesParams,
21+
ResponseMessage,
22+
updateConfigurationRequestType,
23+
WorkspaceFolder,
24+
} from '@aws/language-server-runtimes/protocol'
1425
import {
1526
Settings,
1627
oidcClientName,
1728
createServerOptions,
1829
globals,
1930
Experiments,
2031
Commands,
32+
oneSecond,
2133
validateNodeExe,
2234
getLogger,
2335
} from 'aws-core-vscode/shared'
@@ -75,6 +87,9 @@ export async function startLanguageServer(
7587
window: {
7688
notifications: true,
7789
},
90+
q: {
91+
developerProfiles: true,
92+
},
7893
},
7994
},
8095
credentials: {
@@ -134,7 +149,27 @@ export async function startLanguageServer(
134149
activate(client, encryptionKey, resourcePaths.ui)
135150
}
136151

137-
const refreshInterval = auth.startTokenRefreshInterval()
152+
const refreshInterval = auth.startTokenRefreshInterval(10 * oneSecond)
153+
154+
const sendProfileToLsp = async () => {
155+
try {
156+
const result = await client.sendRequest(updateConfigurationRequestType.method, {
157+
section: 'aws.q',
158+
settings: {
159+
profileArn: AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn,
160+
},
161+
})
162+
client.info(
163+
`Client: Updated Amazon Q Profile ${AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn} to Amazon Q LSP`,
164+
result
165+
)
166+
} catch (err) {
167+
client.error('Error when setting Q Developer Profile to Amazon Q LSP', err)
168+
}
169+
}
170+
171+
// send profile to lsp once.
172+
void sendProfileToLsp()
138173

139174
toDispose.push(
140175
AuthUtil.instance.auth.onDidChangeActiveConnection(async () => {
@@ -143,6 +178,62 @@ export async function startLanguageServer(
143178
AuthUtil.instance.auth.onDidDeleteConnection(async () => {
144179
client.sendNotification(notificationTypes.deleteBearerToken.method)
145180
}),
181+
AuthUtil.instance.regionProfileManager.onDidChangeRegionProfile(sendProfileToLsp),
182+
vscode.commands.registerCommand('aws.amazonq.getWorkspaceId', async () => {
183+
const requestType = new RequestType<GetConfigurationFromServerParams, ResponseMessage, Error>(
184+
'aws/getConfigurationFromServer'
185+
)
186+
const workspaceIdResp = await client.sendRequest(requestType.method, {
187+
section: 'aws.q.workspaceContext',
188+
})
189+
return workspaceIdResp
190+
}),
191+
vscode.workspace.onDidCreateFiles((e) => {
192+
client.sendNotification('workspace/didCreateFiles', {
193+
files: e.files.map((it) => {
194+
return { uri: it.fsPath }
195+
}),
196+
} as CreateFilesParams)
197+
}),
198+
vscode.workspace.onDidDeleteFiles((e) => {
199+
client.sendNotification('workspace/didDeleteFiles', {
200+
files: e.files.map((it) => {
201+
return { uri: it.fsPath }
202+
}),
203+
} as DeleteFilesParams)
204+
}),
205+
vscode.workspace.onDidRenameFiles((e) => {
206+
client.sendNotification('workspace/didRenameFiles', {
207+
files: e.files.map((it) => {
208+
return { oldUri: it.oldUri.fsPath, newUri: it.newUri.fsPath }
209+
}),
210+
} as RenameFilesParams)
211+
}),
212+
vscode.workspace.onDidSaveTextDocument((e) => {
213+
client.sendNotification('workspace/didSaveTextDocument', {
214+
textDocument: {
215+
uri: e.uri.fsPath,
216+
},
217+
} as DidSaveTextDocumentParams)
218+
}),
219+
vscode.workspace.onDidChangeWorkspaceFolders((e) => {
220+
client.sendNotification('workspace/didChangeWorkspaceFolder', {
221+
event: {
222+
added: e.added.map((it) => {
223+
return {
224+
name: it.name,
225+
uri: it.uri.fsPath,
226+
} as WorkspaceFolder
227+
}),
228+
removed: e.removed.map((it) => {
229+
return {
230+
name: it.name,
231+
uri: it.uri.fsPath,
232+
} as WorkspaceFolder
233+
}),
234+
},
235+
} as DidChangeWorkspaceFoldersParams)
236+
}),
146237
{ dispose: () => clearInterval(refreshInterval) }
147238
)
148239
})

packages/amazonq/src/lsp/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export interface ExtendedAmazonQLSPConfig extends LspConfig {
1111
}
1212

1313
export const defaultAmazonQLspConfig: ExtendedAmazonQLSPConfig = {
14-
manifestUrl: 'https://aws-toolkit-language-servers.amazonaws.com/codewhisperer/0/manifest.json',
15-
supportedVersions: '^3.1.1',
14+
manifestUrl: 'https://aws-toolkit-language-servers.amazonaws.com/remoteWorkspaceContext/0/manifest.json',
15+
supportedVersions: '^1.0.0',
1616
id: 'AmazonQ', // used across IDEs for identifying global storage/local disk locations. Do not change.
1717
suppressPromptPrefix: 'amazonQ',
1818
path: undefined,

packages/amazonq/test/unit/amazonq/lsp/chat/messages.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('registerMessageListeners', () => {
2727
error: errorStub,
2828
sendNotification: sandbox.stub(),
2929
onRequest: sandbox.stub(),
30+
onNotification: sandbox.stub(),
3031
} as unknown as LanguageClient
3132

3233
provider = {

packages/core/src/codewhisperer/util/editorContext.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { checkLeftContextKeywordsForJson } from './commonUtil'
1818
import { CodeWhispererSupplementalContext } from '../models/model'
1919
import { getOptOutPreference } from '../../shared/telemetry/util'
2020
import { indent } from '../../shared/utilities/textUtilities'
21+
import { isInDirectory } from '../../shared/filesystemUtilities'
2122
import { AuthUtil } from './authUtil'
2223

2324
let tabSize: number = getTabSizeSetting()
@@ -195,6 +196,22 @@ export function getFileRelativePath(editor: vscode.TextEditor): string {
195196
return relativePath.substring(0, CodeWhispererConstants.filenameCharsLimit)
196197
}
197198

199+
async function getWorkspaceId(editor: vscode.TextEditor): Promise<string | undefined> {
200+
try {
201+
const workspaceIds: { workspaces: { workspaceRoot: string; workspaceId: string }[] } =
202+
await vscode.commands.executeCommand('aws.amazonq.getWorkspaceId')
203+
for (const item of workspaceIds.workspaces) {
204+
const path = vscode.Uri.parse(item.workspaceRoot).fsPath
205+
if (isInDirectory(path, editor.document.uri.fsPath)) {
206+
return item.workspaceId
207+
}
208+
}
209+
} catch (err) {
210+
getLogger().warn(`No workspace id found ${err}`)
211+
}
212+
return undefined
213+
}
214+
198215
export async function buildListRecommendationRequest(
199216
editor: vscode.TextEditor,
200217
nextToken: string,
@@ -233,6 +250,7 @@ export async function buildListRecommendationRequest(
233250
supplementalContexts: supplementalContext,
234251
customizationArn: selectedCustomization.arn === '' ? undefined : selectedCustomization.arn,
235252
optOutPreference: getOptOutPreference(),
253+
workspaceId: await getWorkspaceId(editor),
236254
profileArn: profile?.arn,
237255
},
238256
supplementalMetadata: supplementalContexts,

packages/core/src/shared/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export * from './extensionUtilities'
1818
export * from './extensionStartup'
1919
export { RegionProvider } from './regions/regionProvider'
2020
export { Commands } from './vscode/commands2'
21-
export { getMachineId, getServiceEnvVarConfig } from './vscode/env'
21+
export { getMachineId, getServiceEnvVarConfig, isAmazonInternalOs } from './vscode/env'
2222
export { getLogger } from './logger/logger'
2323
export { activateExtension, openUrl } from './utilities/vsCodeUtils'
2424
export { waitUntil, sleep, Timeout } from './utilities/timeoutUtils'

0 commit comments

Comments
 (0)