@@ -23,9 +23,9 @@ import software.aws.toolkits.core.utils.getLogger
23
23
import software.aws.toolkits.jetbrains.services.amazonq.CodeWhispererFeatureConfigService
24
24
import software.aws.toolkits.jetbrains.services.amazonq.calculateIfIamIdentityCenterConnection
25
25
import software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionProfile
26
+ import software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionProfileManager
26
27
import software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionProfileSelectedListener
27
28
import software.aws.toolkits.jetbrains.services.codewhisperer.credentials.CodeWhispererClientAdaptor
28
- import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants
29
29
import software.aws.toolkits.jetbrains.utils.notifyInfo
30
30
import software.aws.toolkits.jetbrains.utils.notifyWarn
31
31
import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread
@@ -108,25 +108,24 @@ class DefaultCodeWhispererModelConfigurator : CodeWhispererModelConfigurator, Pe
108
108
@RequiresBackgroundThread
109
109
override fun listCustomizations (project : Project , passive : Boolean ): List <CustomizationUiItem >? =
110
110
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())
125
124
}
126
125
127
126
// 2. get diff
128
127
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()
130
129
131
130
// 3 if passive,
132
131
// (1) update allowlisting
@@ -135,42 +134,36 @@ class DefaultCodeWhispererModelConfigurator : CodeWhispererModelConfigurator, Pe
135
134
// if not passive,
136
135
// (1) update the customization list snapshot (seen by users last time) if it will be displayed
137
136
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 )) {
140
139
notifyNewCustomization(project)
141
140
}
142
141
} 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()
146
143
}
147
144
148
145
// 4. invalidate selected customization if
149
146
// (1) the API call failed
150
147
// (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
151
149
activeCustomization(project)?.let { activeCustom ->
152
- if (listAvailableCustomizationsResult == null ) {
150
+ if (aggregatedCustomizations.isEmpty() ) {
153
151
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
+ ) {
155
155
invalidateSelectedAndNotify(project)
156
156
}
157
157
}
158
158
159
159
// 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
+ )
174
167
}
175
168
connectionToCustomizationUiItems[it.id] = customizationUiItems
176
169
0 commit comments