Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions packages/core/src/codewhisperer/util/customizationUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,11 @@ export const baseCustomization = {
* @returns customization selected by users, `baseCustomization` if none is selected
*/
export const getSelectedCustomization = (): Customization => {
if (
!AuthUtil.instance.isCustomizationFeatureEnabled ||
!AuthUtil.instance.isValidEnterpriseSsoInUse() ||
!AuthUtil.instance.conn
) {
if (!AuthUtil.instance.isCustomizationFeatureEnabled || !AuthUtil.instance.isIdcConnection()) {
return baseCustomization
}

const selectedCustomizationArr = globals.globalState.tryGet<{ [label: string]: Customization }>(
'CODEWHISPERER_SELECTED_CUSTOMIZATION',
Object,
{}
)
const selectedCustomization = selectedCustomizationArr[AuthUtil.instance.conn.label]
const selectedCustomization = globals.globalState.getCodewhispererCustomization(AuthUtil.instance.profileName)

if (selectedCustomization && selectedCustomization.name !== '') {
return selectedCustomization
Expand All @@ -124,7 +115,7 @@ export const getSelectedCustomization = (): Customization => {
* 2. the override customization arn is different from the previous override customization if any. The purpose is to only do override once on users' behalf.
*/
export const setSelectedCustomization = async (customization: Customization, isOverride: boolean = false) => {
if (!AuthUtil.instance.isValidEnterpriseSsoInUse() || !AuthUtil.instance.conn) {
if (!AuthUtil.instance.isIdcConnection()) {
return
}
if (isOverride) {
Expand All @@ -133,15 +124,10 @@ export const setSelectedCustomization = async (customization: Customization, isO
return
}
}
const selectedCustomizationObj = globals.globalState.tryGet<{ [label: string]: Customization }>(
'CODEWHISPERER_SELECTED_CUSTOMIZATION',
Object,
{}
)
selectedCustomizationObj[AuthUtil.instance.conn.label] = customization
getLogger().debug(`Selected customization ${customization.name} for ${AuthUtil.instance.conn.label}`)

await globals.globalState.update('CODEWHISPERER_SELECTED_CUSTOMIZATION', selectedCustomizationObj)
await globals.globalState.update('CODEWHISPERER_SELECTED_CUSTOMIZATION', customization)
getLogger().debug(`Selected customization ${customization.name} for ${AuthUtil.instance.profileName}`)

if (isOverride) {
await globals.globalState.update('aws.amazonq.customization.overrideV2', customization.arn)
}
Expand All @@ -150,27 +136,27 @@ export const setSelectedCustomization = async (customization: Customization, isO
}

export const getPersistedCustomizations = (): Customization[] => {
if (!AuthUtil.instance.isValidEnterpriseSsoInUse() || !AuthUtil.instance.conn) {
if (!AuthUtil.instance.isIdcConnection()) {
return []
}
const persistedCustomizationsObj = globals.globalState.tryGet<{ [label: string]: Customization[] }>(
'CODEWHISPERER_PERSISTED_CUSTOMIZATIONS',
Object,
{}
)
return persistedCustomizationsObj[AuthUtil.instance.conn.label] || []
return persistedCustomizationsObj[AuthUtil.instance.profileName] || []
}

export const setPersistedCustomizations = async (customizations: Customization[]) => {
if (!AuthUtil.instance.isValidEnterpriseSsoInUse() || !AuthUtil.instance.conn) {
if (!AuthUtil.instance.isIdcConnection()) {
return
}
const persistedCustomizationsObj = globals.globalState.tryGet<{ [label: string]: Customization[] }>(
'CODEWHISPERER_PERSISTED_CUSTOMIZATIONS',
Object,
{}
)
persistedCustomizationsObj[AuthUtil.instance.conn.label] = customizations
persistedCustomizationsObj[AuthUtil.instance.profileName] = customizations
await globals.globalState.update('CODEWHISPERER_PERSISTED_CUSTOMIZATIONS', persistedCustomizationsObj)
}

Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/shared/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as vscode from 'vscode'
import { getLogger } from './logger/logger'
import * as redshift from '../awsService/redshift/models/models'
import { TypeConstructor, cast } from './utilities/typeConstructors'
import { Customization } from '../codewhisperer/client/codewhispereruserclient'

type ToolId = 'codecatalyst' | 'codewhisperer' | 'testId'
export type ToolIdStateKey = `${ToolId}.savedConnectionId`
Expand Down Expand Up @@ -227,6 +228,27 @@ export class GlobalState implements vscode.Memento {
return all?.[warehouseArn]
}

/**
* Get the codewhisperer customerization. If legacy (map of customizations) store the
* customization with label of profile name
*
* @param profileName name of profile, only used in case legacy customization is found
* @returns codewhisperer customization, or undefined if not found.
* If legacy, return the codewhisperer customization for the auth profile name
*/
getCodewhispererCustomization(profileName: string): Customization | undefined {
const result = this.tryGet('CODEWHISPERER_SELECTED_CUSTOMIZATION', Object, undefined)

// Legacy migration for old customization map of type { [label: string]: Customization[] }
if (typeof result === 'object' && Object.values(result).every(Array.isArray)) {
const selectedCustomization = result[profileName]
this.tryUpdate('CODEWHISPERER_SELECTED_CUSTOMIZATION', selectedCustomization)
return selectedCustomization
} else {
return result
}
}

/**
* Sets SSO session creation timestamp for the given session `id`.
*
Expand Down
Loading