Skip to content

Commit cd708b4

Browse files
wlovoWill Lovo
andauthored
feat(codewhisperer): select customization by prefix (#3947)
Problem: The `aws.codeWhisperer.connect` command was introduced to allow enterprises to establish a default customization for onboarding developers after establishing an IdC connection. However, it only selected with a given ARN. Given the current behavior for adding updated customization for CodeWhisperer, an enterprise would need to keep the ARN up-to-date as well when calling this command. Solution: Provide the ability to define the customization to select with a prefix for the customization name. This way the logic to select a customization as updated customizations are introduced can be simplified on the administrator side by maintaining a prefix. e.g. MyModel-v1, MyModel-v2, etc. Co-authored-by: Will Lovo <[email protected]>
1 parent dd19a84 commit cd708b4

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

docs/api.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ Details about any publicly accessible functionalities exposed through [extension
88

99
#### `aws.codeWhisperer.connect`
1010

11-
**Signature**: _async (startUrl?: string, region?: string, customizationArn?: string, customizationName?: string, customizationDescription?: string) => Promise<void>_
11+
**Signature**: _async (startUrl?: string, region?: string, customizationArn?: string, customizationNamePrefix?: string) => Promise<void>_
1212

1313
Shortcut command to directly connect to Identity Center or prompt start URL entry, as well as set a customization for CodeWhisperer requests.
1414

15-
This command supports two sets of arguments:
15+
This command supports the following arguments:
1616

1717
- startUrl and region. If both arguments are provided they will be used, otherwise the command prompts for them interactively.
18-
- customization{Arn, Name, Description}. If at least customizationArn is provided, the command selects this customization.
18+
- customizationArn: select customization by ARN. If provided, `customizationNamePrefix` is ignored.
19+
- customizationNamePrefix: select customization by prefix, if `customizationArn` is `undefined`.

src/codewhisperer/commands/basicCommands.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ReferenceLogViewProvider } from '../service/referenceLogViewProvider'
1818
import { AuthUtil } from '../util/authUtil'
1919
import { isCloud9 } from '../../shared/extensionUtilities'
2020
import { InlineCompletionService } from '../service/inlineCompletionService'
21+
import { getLogger } from '../../shared/logger'
2122
import { openUrl } from '../../shared/utilities/vsCodeUtils'
2223
import {
2324
getPersistedCustomizations,
@@ -120,37 +121,50 @@ export const showSsoSignIn = Commands.declare('aws.codeWhisperer.sso', () => asy
120121
})
121122

122123
// Shortcut command to directly connect to Identity Center or prompt start URL entry
123-
// It can optionally set a customization too.
124+
// It can optionally set a customization too based on given values to match on
124125
export const connectWithCustomization = Commands.declare(
125126
'aws.codeWhisperer.connect',
126-
() =>
127-
async (
128-
startUrl?: string,
129-
region?: string,
130-
customizationArn?: string,
131-
customizationName?: string,
132-
customizationDescription?: string
133-
) => {
134-
// This command supports two sets of arguments:
135-
// * startUrl and region. If both arguments are provided they will be used, otherwise
136-
// the command prompts for them interactively.
137-
// * customization{Arn, Name, Description}. If at least customizationArn is provided,
138-
// the command selects this customization.
139-
if (startUrl && region) {
140-
await connectToEnterpriseSso(startUrl, region)
141-
} else {
142-
await getStartUrl()
143-
}
144-
if (customizationArn) {
145-
const match = getPersistedCustomizations().find(c => c.arn == customizationArn)
146-
const customization = {
147-
arn: customizationArn,
148-
name: customizationName ?? match?.name ?? 'unknown',
149-
description: customizationDescription ?? match?.description ?? 'unknown',
150-
}
151-
await selectCustomization(customization)
152-
}
127+
() => async (startUrl?: string, region?: string, customizationArn?: string, customizationNamePrefix?: string) => {
128+
// This command supports the following arguments:
129+
// * startUrl and region. If both arguments are provided they will be used, otherwise
130+
// the command prompts for them interactively.
131+
// * customizationArn: select customization by ARN. If provided, `customizationNamePrefix` is ignored.
132+
// * customizationNamePrefix: select customization by prefix, if `customizationArn` is `undefined`.
133+
if (startUrl && region) {
134+
await connectToEnterpriseSso(startUrl, region)
135+
} else {
136+
await getStartUrl()
137+
}
138+
139+
// No customization match information given, exit early.
140+
if (!customizationArn && !customizationNamePrefix) {
141+
return
153142
}
143+
144+
let persistedCustomizations = getPersistedCustomizations()
145+
146+
// Check if any customizations have already been persisted.
147+
// If not, call `notifyNewCustomizations` to handle it then recheck.
148+
if (persistedCustomizations.length === 0) {
149+
await notifyNewCustomizations()
150+
persistedCustomizations = getPersistedCustomizations()
151+
}
152+
153+
// If given an ARN, assume a specific customization is desired and find an entry that matches it. Ignores the prefix logic.
154+
// Otherwise if only a prefix is given, find an entry that matches it.
155+
// Backwards compatible with previous implementation.
156+
const match = customizationArn
157+
? persistedCustomizations.find(c => c.arn === customizationArn)
158+
: persistedCustomizations.find(c => c.name?.startsWith(customizationNamePrefix as string))
159+
160+
// If no match is found, nothing to do :)
161+
if (!match) {
162+
getLogger().error(`No customization match found: arn=${customizationArn} prefix=${customizationNamePrefix}`)
163+
return
164+
}
165+
// Since we selected based on a match, we'll reuse the persisted values.
166+
await selectCustomization(match)
167+
}
154168
)
155169

156170
export const showLearnMore = Commands.declare('aws.codeWhisperer.learnMore', () => async () => {

src/codewhisperer/util/customizationUtil.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export async function notifyNewCustomizations() {
5353
}
5454

5555
const newCustomizations = getNewCustomizations(availableCustomizations)
56+
await setPersistedCustomizations(availableCustomizations)
5657
if (newCustomizations.length === 0) {
5758
return
5859
}

0 commit comments

Comments
 (0)