@@ -23,9 +23,9 @@ import software.aws.toolkits.core.utils.getLogger
2323import  software.aws.toolkits.jetbrains.services.amazonq.CodeWhispererFeatureConfigService 
2424import  software.aws.toolkits.jetbrains.services.amazonq.calculateIfIamIdentityCenterConnection 
2525import  software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionProfile 
26+ import  software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionProfileManager 
2627import  software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionProfileSelectedListener 
2728import  software.aws.toolkits.jetbrains.services.codewhisperer.credentials.CodeWhispererClientAdaptor 
28- import  software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants 
2929import  software.aws.toolkits.jetbrains.utils.notifyInfo 
3030import  software.aws.toolkits.jetbrains.utils.notifyWarn 
3131import  software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread 
@@ -108,25 +108,24 @@ class DefaultCodeWhispererModelConfigurator : CodeWhispererModelConfigurator, Pe
108108    @RequiresBackgroundThread
109109    override  fun  listCustomizations (project :  Project , passive :  Boolean ): List <CustomizationUiItem >?  = 
110110        calculateIfIamIdentityCenterConnection(project) {
111-             //  1. invoke API and get result
112-             val  listAvailableCustomizationsResult =  try  {
113-                 CodeWhispererClientAdaptor .getInstance(project).listAvailableCustomizations()
114-             } catch  (e:  Exception ) {
115-                 val  requestId =  (e as ?  CodeWhispererRuntimeException )?.requestId()
116-                 val  logMessage =  if  (CodeWhispererConstants .Customization .noAccessToCustomizationExceptionPredicate(e)) {
117-                     //  TODO: not required for non GP users
118-                     " ListAvailableCustomizations: connection ${it.id}  is not allowlisted, requestId: ${requestId.orEmpty()} " 
119-                 } else  {
120-                     " ListAvailableCustomizations: failed due to unknown error ${e.message} , requestId: ${requestId.orEmpty()} " 
121-                 }
122- 
123-                 LOG .debug { logMessage }
124-                 null 
111+             //  1. fetch all profiles, invoke fetch customizations API and get result for each profile and aggregate all the results
112+             val  listAvailableProfilesResult =  QRegionProfileManager .getInstance().listRegionProfiles(project)
113+                 ? :  error(" Attempted to fetch profiles while there does not exist"  )
114+ 
115+             val  aggregatedCustomizations =  listAvailableProfilesResult.flatMap { profile -> 
116+                 runCatching {
117+                     CodeWhispererClientAdaptor .getInstance(project).listAvailableCustomizations(profile)
118+                 }.onFailure { e -> 
119+                     val  requestId =  (e as ?  CodeWhispererRuntimeException )?.requestId()
120+                     val  logMessage =  " ListAvailableCustomizations: failed due to unknown error ${e.message} , "   + 
121+                         " requestId: ${requestId.orEmpty()} , profileName: ${profile.profileName} " 
122+                     LOG .debug { logMessage }
123+                 }.getOrDefault(emptyList())
125124            }
126125
127126            //  2. get diff
128127            val  previousCustomizationsShapshot =  connectionToCustomizationsShownLastTime.getOrElse(it.id) { emptyList() }
129-             val  diff =  listAvailableCustomizationsResult? .filterNot { customization ->  previousCustomizationsShapshot.contains(customization.arn) }? .toSet()
128+             val  diff =  aggregatedCustomizations .filterNot { customization ->  previousCustomizationsShapshot.contains(customization.arn) }.toSet()
130129
131130            //  3 if passive,
132131            //    (1) update allowlisting
@@ -135,42 +134,36 @@ class DefaultCodeWhispererModelConfigurator : CodeWhispererModelConfigurator, Pe
135134            //    if not passive,
136135            //    (1) update the customization list snapshot (seen by users last time) if it will be displayed
137136            if  (passive) {
138-                 connectionIdToIsAllowlisted[it.id] =  listAvailableCustomizationsResult  !=   null 
139-                 if  (diff? .isNotEmpty()  ==   true  &&  ! hasShownNewCustomizationNotification.getAndSet(true )) {
137+                 connectionIdToIsAllowlisted[it.id] =  aggregatedCustomizations.isNotEmpty() 
138+                 if  (diff.isNotEmpty() &&  ! hasShownNewCustomizationNotification.getAndSet(true )) {
140139                    notifyNewCustomization(project)
141140                }
142141            } else  {
143-                 listAvailableCustomizationsResult?.let  { customizations -> 
144-                     connectionToCustomizationsShownLastTime[it.id] =  customizations.map { customization ->  customization.arn }.toMutableList()
145-                 }
142+                 connectionToCustomizationsShownLastTime[it.id] =  aggregatedCustomizations.map { customization ->  customization.arn }.toMutableList()
146143            }
147144
148145            //  4. invalidate selected customization if
149146            //     (1) the API call failed
150147            //     (2) the selected customization is not in the resultset of API call
148+             //     (3) the existing q region profile associated with the selected customization does not match the currently active profile
151149            activeCustomization(project)?.let  { activeCustom -> 
152-                 if  (listAvailableCustomizationsResult  ==   null ) {
150+                 if  (aggregatedCustomizations.isEmpty() ) {
153151                    invalidateSelectedAndNotify(project)
154-                 } else  if  (! listAvailableCustomizationsResult.any { latestCustom ->  latestCustom.arn ==  activeCustom.arn }) {
152+                 } else  if  (! aggregatedCustomizations.any { latestCustom ->  latestCustom.arn ==  activeCustom.arn } || 
153+                     (activeCustom.profile !=  null  &&  activeCustom.profile !=  QRegionProfileManager .getInstance().activeProfile(project))
154+                 ) {
155155                    invalidateSelectedAndNotify(project)
156156                }
157157            }
158158
159159            //  5. transform result to UI items and return
160-             val  customizationUiItems =  if  (diff !=  null ) {
161-                 listAvailableCustomizationsResult.let  { customizations -> 
162-                     val  nameToCount =  customizations.groupingBy { customization ->  customization.name }.eachCount()
163- 
164-                     customizations.map { customization -> 
165-                         CustomizationUiItem (
166-                             customization,
167-                             isNew =  diff.contains(customization),
168-                             shouldPrefixAccountId =  (nameToCount[customization.name] ? :  0 ) >  1 
169-                         )
170-                     }
171-                 }
172-             } else  {
173-                 null 
160+             val  nameToCount =  aggregatedCustomizations.groupingBy { customization ->  customization.name }.eachCount()
161+             val  customizationUiItems =  aggregatedCustomizations.map { customization -> 
162+                 CustomizationUiItem (
163+                     customization,
164+                     isNew =  diff.contains(customization),
165+                     shouldPrefixAccountId =  (nameToCount[customization.name] ? :  0 ) >  1 
166+                 )
174167            }
175168            connectionToCustomizationUiItems[it.id] =  customizationUiItems
176169
0 commit comments