@@ -47,12 +47,17 @@ const endpoints = createConstantMap({
4747 * 'update' -> plugin auto select the profile on users' behalf as there is only 1 profile
4848 * 'reload' -> on plugin restart, plugin will try to reload previous selected profile
4949 */
50- export type ProfileSwitchIntent = 'user' | 'auth' | 'update' | 'reload'
50+ export type ProfileSwitchIntent = 'user' | 'auth' | 'update' | 'reload' | 'customization'
51+
52+ export type ProfileChangedEvent = {
53+ profile : RegionProfile | undefined
54+ intent : ProfileSwitchIntent
55+ }
5156
5257export class RegionProfileManager {
5358 private static logger = getLogger ( )
5459 private _activeRegionProfile : RegionProfile | undefined
55- private _onDidChangeRegionProfile = new vscode . EventEmitter < RegionProfile | undefined > ( )
60+ private _onDidChangeRegionProfile = new vscode . EventEmitter < ProfileChangedEvent > ( )
5661 public readonly onDidChangeRegionProfile = this . _onDidChangeRegionProfile . event
5762
5863 // Store the last API results (for UI propuse) so we don't need to call service again if doesn't require "latest" result
@@ -112,7 +117,7 @@ export class RegionProfileManager {
112117 }
113118 const availableProfiles : RegionProfile [ ] = [ ]
114119 for ( const [ region , endpoint ] of endpoints . entries ( ) ) {
115- const client = await this . createQClient ( region , endpoint , conn as SsoConnection )
120+ const client = await this . _createQClient ( region , endpoint , conn as SsoConnection )
116121 const requester = async ( request : CodeWhispererUserClient . ListAvailableProfilesRequest ) =>
117122 client . listAvailableProfiles ( request ) . promise ( )
118123 const request : CodeWhispererUserClient . ListAvailableProfilesRequest = { }
@@ -162,7 +167,7 @@ export class RegionProfileManager {
162167 const ssoConn = this . connectionProvider ( ) as SsoConnection
163168
164169 // only prompt to users when users switch from A profile to B profile
165- if ( this . activeRegionProfile !== undefined && regionProfile !== undefined ) {
170+ if ( source !== 'customization' && this . activeRegionProfile !== undefined && regionProfile !== undefined ) {
166171 const response = await showConfirmationMessage ( {
167172 prompt : localize (
168173 'AWS.amazonq.profile.confirmation' ,
@@ -204,13 +209,16 @@ export class RegionProfileManager {
204209 } )
205210 }
206211
207- await this . _switchRegionProfile ( regionProfile )
212+ await this . _switchRegionProfile ( regionProfile , source )
208213 }
209214
210- private async _switchRegionProfile ( regionProfile : RegionProfile | undefined ) {
215+ private async _switchRegionProfile ( regionProfile : RegionProfile | undefined , source : ProfileSwitchIntent ) {
211216 this . _activeRegionProfile = regionProfile
212217
213- this . _onDidChangeRegionProfile . fire ( regionProfile )
218+ this . _onDidChangeRegionProfile . fire ( {
219+ profile : regionProfile ,
220+ intent : source ,
221+ } )
214222 // dont show if it's a default (fallback)
215223 if ( regionProfile && this . profiles . length > 1 ) {
216224 void vscode . window . showInformationMessage ( `You are using the ${ regionProfile . name } profile for Q.` ) . then ( )
@@ -343,7 +351,21 @@ export class RegionProfileManager {
343351 }
344352 }
345353
346- async createQClient ( region : string , endpoint : string , conn : SsoConnection ) : Promise < CodeWhispererUserClient > {
354+ // TODO: Should maintain sdk client in a better way
355+ async createQClient ( profile : RegionProfile ) : Promise < CodeWhispererUserClient > {
356+ const conn = this . connectionProvider ( )
357+ if ( conn === undefined || ! isSsoConnection ( conn ) ) {
358+ throw new Error ( 'No valid SSO connection' )
359+ }
360+ const endpoint = endpoints . get ( profile . region )
361+ if ( ! endpoint ) {
362+ throw new Error ( `trying to initiatize Q client with unrecognizable region ${ profile . region } ` )
363+ }
364+ return this . _createQClient ( profile . region , endpoint , conn )
365+ }
366+
367+ // Visible for testing only, do not use this directly, please use createQClient(profile)
368+ async _createQClient ( region : string , endpoint : string , conn : SsoConnection ) : Promise < CodeWhispererUserClient > {
347369 const token = ( await conn . getToken ( ) ) . accessToken
348370 const serviceOption : ServiceOptions = {
349371 apiConfig : userApiConfig ,
0 commit comments