@@ -8,13 +8,6 @@ import { getIcon } from '../../shared/icons'
88import { DataQuickPickItem } from '../../shared/ui/pickerPrompter'
99import { CodeWhispererConfig , RegionProfile } from '../models/model'
1010import { showConfirmationMessage } from '../../shared/utilities/messages'
11- import {
12- Connection ,
13- isBuilderIdConnection ,
14- isIdcSsoConnection ,
15- isSsoConnection ,
16- SsoConnection ,
17- } from '../../auth/connection'
1811import globals from '../../shared/extensionGlobals'
1912import { once } from '../../shared/utilities/functionUtils'
2013import CodeWhispererUserClient from '../client/codewhispereruserclient'
@@ -28,6 +21,7 @@ import { parse } from '@aws-sdk/util-arn-parser'
2821import { isAwsError , ToolkitError } from '../../shared/errors'
2922import { telemetry } from '../../shared/telemetry/telemetry'
3023import { localize } from '../../shared/utilities/vsCodeUtils'
24+ import { AuthUtil } from '../util/authUtil'
3125
3226// TODO: is there a better way to manage all endpoint strings in one place?
3327export const defaultServiceConfig : CodeWhispererConfig = {
@@ -60,21 +54,19 @@ export class RegionProfileManager {
6054 private _profiles : RegionProfile [ ] = [ ]
6155
6256 get activeRegionProfile ( ) {
63- const conn = this . connectionProvider ( )
64- if ( isBuilderIdConnection ( conn ) ) {
57+ if ( AuthUtil . instance . isBuilderIdConnection ( ) ) {
6558 return undefined
6659 }
6760 return this . _activeRegionProfile
6861 }
6962
7063 get clientConfig ( ) : CodeWhispererConfig {
71- const conn = this . connectionProvider ( )
72- if ( ! conn ) {
64+ if ( ! AuthUtil . instance . isConnected ( ) ) {
7365 throw new ToolkitError ( 'trying to get client configuration without credential' )
7466 }
7567
7668 // builder id should simply use default IAD
77- if ( isBuilderIdConnection ( conn ) ) {
69+ if ( AuthUtil . instance . isBuilderIdConnection ( ) ) {
7870 return defaultServiceConfig
7971 }
8072
@@ -102,18 +94,17 @@ export class RegionProfileManager {
10294 return this . _profiles
10395 }
10496
105- constructor ( private readonly connectionProvider : ( ) => Connection | undefined ) { }
97+ constructor ( ) { }
10698
107- async listRegionProfile ( ) : Promise < RegionProfile [ ] > {
99+ async listRegionProfiles ( ) : Promise < RegionProfile [ ] > {
108100 this . _profiles = [ ]
109101
110- const conn = this . connectionProvider ( )
111- if ( conn === undefined || ! isSsoConnection ( conn ) ) {
102+ if ( ! AuthUtil . instance . isConnected ( ) || ! AuthUtil . instance . isSsoSession ( ) ) {
112103 return [ ]
113104 }
114105 const availableProfiles : RegionProfile [ ] = [ ]
115106 for ( const [ region , endpoint ] of endpoints . entries ( ) ) {
116- const client = await this . createQClient ( region , endpoint , conn as SsoConnection )
107+ const client = await this . createQClient ( region , endpoint )
117108 const requester = async ( request : CodeWhispererUserClient . ListAvailableProfilesRequest ) =>
118109 client . listAvailableProfiles ( request ) . promise ( )
119110 const request : CodeWhispererUserClient . ListAvailableProfilesRequest = { }
@@ -138,7 +129,7 @@ export class RegionProfileManager {
138129 availableProfiles . push ( ...mappedPfs )
139130 } catch ( e ) {
140131 const logMsg = isAwsError ( e ) ? `requestId=${ e . requestId } ; message=${ e . message } ` : ( e as Error ) . message
141- RegionProfileManager . logger . error ( `failed to listRegionProfile : ${ logMsg } ` )
132+ RegionProfileManager . logger . error ( `failed to listRegionProfiles : ${ logMsg } ` )
142133 throw e
143134 }
144135
@@ -150,18 +141,14 @@ export class RegionProfileManager {
150141 }
151142
152143 async switchRegionProfile ( regionProfile : RegionProfile | undefined , source : ProfileSwitchIntent ) {
153- const conn = this . connectionProvider ( )
154- if ( conn === undefined || ! isIdcSsoConnection ( conn ) ) {
144+ if ( ! AuthUtil . instance . isConnected ( ) || ! AuthUtil . instance . isIdcConnection ( ) ) {
155145 return
156146 }
157147
158148 if ( regionProfile && this . activeRegionProfile && regionProfile . arn === this . activeRegionProfile . arn ) {
159149 return
160150 }
161151
162- // TODO: make it typesafe
163- const ssoConn = this . connectionProvider ( ) as SsoConnection
164-
165152 // only prompt to users when users switch from A profile to B profile
166153 if ( this . activeRegionProfile !== undefined && regionProfile !== undefined ) {
167154 const response = await showConfirmationMessage ( {
@@ -179,9 +166,9 @@ export class RegionProfileManager {
179166 telemetry . amazonq_didSelectProfile . emit ( {
180167 source : source ,
181168 amazonQProfileRegion : this . activeRegionProfile ?. region ?? 'not-set' ,
182- ssoRegion : ssoConn . ssoRegion ,
169+ ssoRegion : AuthUtil . instance . connection ?. region ,
183170 result : 'Cancelled' ,
184- credentialStartUrl : ssoConn . startUrl ,
171+ credentialStartUrl : AuthUtil . instance . connection ? .startUrl ,
185172 profileCount : this . profiles . length ,
186173 } )
187174 return
@@ -198,9 +185,9 @@ export class RegionProfileManager {
198185 telemetry . amazonq_didSelectProfile . emit ( {
199186 source : source ,
200187 amazonQProfileRegion : regionProfile ?. region ?? 'not-set' ,
201- ssoRegion : ssoConn . ssoRegion ,
188+ ssoRegion : AuthUtil . instance . connection ?. region ,
202189 result : 'Succeeded' ,
203- credentialStartUrl : ssoConn . startUrl ,
190+ credentialStartUrl : AuthUtil . instance . connection ? .startUrl ,
204191 profileCount : this . profiles . length ,
205192 } )
206193 }
@@ -222,20 +209,19 @@ export class RegionProfileManager {
222209 }
223210
224211 restoreProfileSelection = once ( async ( ) => {
225- const conn = this . connectionProvider ( )
226- if ( conn ) {
227- await this . restoreRegionProfile ( conn )
212+ if ( AuthUtil . instance . isConnected ( ) ) {
213+ await this . restoreRegionProfile ( )
228214 }
229215 } )
230216
231217 // Note: should be called after [AuthUtil.instance.conn] returns non null
232- async restoreRegionProfile ( conn : Connection ) {
233- const previousSelected = this . loadPersistedRegionProfle ( ) [ conn . id ] || undefined
218+ async restoreRegionProfile ( ) {
219+ const previousSelected = this . loadPersistedRegionProfle ( ) [ AuthUtil . instance . profileName ] || undefined
234220 if ( ! previousSelected ) {
235221 return
236222 }
237223 // cross-validation
238- this . listRegionProfile ( )
224+ this . listRegionProfiles ( )
239225 . then ( async ( profiles ) => {
240226 const r = profiles . find ( ( it ) => it . arn === previousSelected . arn )
241227 if ( ! r ) {
@@ -275,10 +261,8 @@ export class RegionProfileManager {
275261 }
276262
277263 async persistSelectRegionProfile ( ) {
278- const conn = this . connectionProvider ( )
279-
280264 // default has empty arn and shouldn't be persisted because it's just a fallback
281- if ( ! conn || this . activeRegionProfile === undefined ) {
265+ if ( ! AuthUtil . instance . isConnected ( ) || this . activeRegionProfile === undefined ) {
282266 return
283267 }
284268
@@ -289,15 +273,15 @@ export class RegionProfileManager {
289273 { }
290274 )
291275
292- previousPersistedState [ conn . id ] = this . activeRegionProfile
276+ previousPersistedState [ AuthUtil . instance . profileName ] = this . activeRegionProfile
293277 await globals . globalState . update ( 'aws.amazonq.regionProfiles' , previousPersistedState )
294278 }
295279
296280 async generateQuickPickItem ( ) : Promise < DataQuickPickItem < string > [ ] > {
297281 const selected = this . activeRegionProfile
298282 let profiles : RegionProfile [ ] = [ ]
299283 try {
300- profiles = await this . listRegionProfile ( )
284+ profiles = await this . listRegionProfiles ( )
301285 } catch ( e ) {
302286 return [
303287 {
@@ -344,8 +328,15 @@ export class RegionProfileManager {
344328 }
345329 }
346330
347- async createQClient ( region : string , endpoint : string , conn : SsoConnection ) : Promise < CodeWhispererUserClient > {
348- const token = ( await conn . getToken ( ) ) . accessToken
331+ requireProfileSelection ( ) {
332+ if ( AuthUtil . instance . isBuilderIdConnection ( ) ) {
333+ return false
334+ }
335+ return AuthUtil . instance . isIdcConnection ( ) && this . activeRegionProfile === undefined
336+ }
337+
338+ async createQClient ( region : string , endpoint : string ) : Promise < CodeWhispererUserClient > {
339+ const token = await AuthUtil . instance . getToken ( )
349340 const serviceOption : ServiceOptions = {
350341 apiConfig : userApiConfig ,
351342 region : region ,
0 commit comments