@@ -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
2726import software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionProfileSelectedListener
2827import 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,24 +108,25 @@ class DefaultCodeWhispererModelConfigurator : CodeWhispererModelConfigurator, Pe
108108 @RequiresBackgroundThread
109109 override fun listCustomizations (project : Project , passive : Boolean ): List <CustomizationUiItem >? =
110110 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
124125 }
125126
126127 // 2. get diff
127128 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()
129130
130131 // 3 if passive,
131132 // (1) update allowlisting
@@ -134,36 +135,42 @@ class DefaultCodeWhispererModelConfigurator : CodeWhispererModelConfigurator, Pe
134135 // if not passive,
135136 // (1) update the customization list snapshot (seen by users last time) if it will be displayed
136137 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 )) {
139140 notifyNewCustomization(project)
140141 }
141142 } 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+ }
143146 }
144147
145148 // 4. invalidate selected customization if
146149 // (1) the API call failed
147150 // (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
149151 activeCustomization(project)?.let { activeCustom ->
150- if (aggregatedCustomizations.isEmpty() ) {
152+ if (listAvailableCustomizationsResult == null ) {
151153 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 }) {
155155 invalidateSelectedAndNotify(project)
156156 }
157157 }
158158
159159 // 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
167174 }
168175 connectionToCustomizationUiItems[it.id] = customizationUiItems
169176
0 commit comments