@@ -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
27
26
import software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionProfileSelectedListener
28
27
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,24 +108,25 @@ class DefaultCodeWhispererModelConfigurator : CodeWhispererModelConfigurator, Pe
108
108
@RequiresBackgroundThread
109
109
override fun listCustomizations (project : Project , passive : Boolean ): List <CustomizationUiItem >? =
110
110
calculateIfIamIdentityCenterConnection(project) {
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())
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
124
125
}
125
126
126
127
// 2. get diff
127
128
val previousCustomizationsShapshot = connectionToCustomizationsShownLastTime.getOrElse(it.id) { emptyList() }
128
- val diff = aggregatedCustomizations .filterNot { customization -> previousCustomizationsShapshot.contains(customization.arn) }.toSet()
129
+ val diff = listAvailableCustomizationsResult? .filterNot { customization -> previousCustomizationsShapshot.contains(customization.arn) }? .toSet()
129
130
130
131
// 3 if passive,
131
132
// (1) update allowlisting
@@ -134,36 +135,42 @@ class DefaultCodeWhispererModelConfigurator : CodeWhispererModelConfigurator, Pe
134
135
// if not passive,
135
136
// (1) update the customization list snapshot (seen by users last time) if it will be displayed
136
137
if (passive) {
137
- connectionIdToIsAllowlisted[it.id] = aggregatedCustomizations.isNotEmpty()
138
- if (diff.isNotEmpty() && ! hasShownNewCustomizationNotification.getAndSet(true )) {
138
+ connectionIdToIsAllowlisted[it.id] = listAvailableCustomizationsResult != null
139
+ if (diff? .isNotEmpty() == true && ! hasShownNewCustomizationNotification.getAndSet(true )) {
139
140
notifyNewCustomization(project)
140
141
}
141
142
} else {
142
- connectionToCustomizationsShownLastTime[it.id] = aggregatedCustomizations.map { customization -> customization.arn }.toMutableList()
143
+ listAvailableCustomizationsResult?.let { customizations ->
144
+ connectionToCustomizationsShownLastTime[it.id] = customizations.map { customization -> customization.arn }.toMutableList()
145
+ }
143
146
}
144
147
145
148
// 4. invalidate selected customization if
146
149
// (1) the API call failed
147
150
// (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
149
151
activeCustomization(project)?.let { activeCustom ->
150
- if (aggregatedCustomizations.isEmpty() ) {
152
+ if (listAvailableCustomizationsResult == null ) {
151
153
invalidateSelectedAndNotify(project)
152
- } else if (! aggregatedCustomizations.any { latestCustom -> latestCustom.arn == activeCustom.arn } ||
153
- (activeCustom.profile != null && activeCustom.profile != QRegionProfileManager .getInstance().activeProfile(project))
154
- ) {
154
+ } else if (! listAvailableCustomizationsResult.any { latestCustom -> latestCustom.arn == activeCustom.arn }) {
155
155
invalidateSelectedAndNotify(project)
156
156
}
157
157
}
158
158
159
159
// 5. transform result to UI items and return
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
- )
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
167
174
}
168
175
connectionToCustomizationUiItems[it.id] = customizationUiItems
169
176
0 commit comments