Skip to content

Commit f382728

Browse files
Fix ViewModel scope break and enable Mistral key rotation in service
1 parent 0f7290d commit f382728

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/ScreenCaptureApiClients.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ data class ServiceMistralResponseMessage(
7171
val content: String
7272
)
7373

74-
internal suspend fun callMistralApi(modelName: String, apiKey: String, chatHistory: List<Content>, inputContent: Content): Pair<String?, String?> {
74+
internal suspend fun callMistralApi(
75+
modelName: String,
76+
apiKey: String,
77+
chatHistory: List<Content>,
78+
inputContent: Content,
79+
availableApiKeys: List<String> = listOf(apiKey)
80+
): Pair<String?, String?> {
7581
var responseText: String? = null
7682
var errorMessage: String? = null
7783

@@ -130,7 +136,8 @@ internal suspend fun callMistralApi(modelName: String, apiKey: String, chatHisto
130136
.addHeader("Authorization", "Bearer $apiKey")
131137
.build()
132138

133-
val coordinated = MistralRequestCoordinator.execute(apiKeys = listOf(apiKey), maxAttempts = 4) { key ->
139+
val keysForCoordinator = availableApiKeys.filter { it.isNotBlank() }.distinct().ifEmpty { listOf(apiKey) }
140+
val coordinated = MistralRequestCoordinator.execute(apiKeys = keysForCoordinator, maxAttempts = maxOf(4, keysForCoordinator.size * 3)) { key ->
134141
client.newCall(
135142
request.newBuilder()
136143
.header("Authorization", "Bearer $key")

app/src/main/kotlin/com/google/ai/sample/ScreenCaptureService.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,16 @@ class ScreenCaptureService : Service() {
297297
if (apiProvider == ApiProvider.VERCEL) {
298298
responseText = callVercelApi(applicationContext, modelName, apiKey, chatHistoryDtos, inputContentDto)
299299
} else if (apiProvider == ApiProvider.MISTRAL) {
300-
val result = callMistralApi(modelName, apiKey, chatHistory, inputContent)
300+
val availableMistralKeys = ApiKeyManager.getInstance(applicationContext)
301+
.getApiKeys(ApiProvider.MISTRAL)
302+
.filter { it.isNotBlank() }
303+
val result = callMistralApi(
304+
modelName = modelName,
305+
apiKey = apiKey,
306+
chatHistory = chatHistory,
307+
inputContent = inputContent,
308+
availableApiKeys = availableMistralKeys
309+
)
301310
responseText = result.first
302311
errorMessage = result.second
303312
} else if (apiProvider == ApiProvider.PUTER) {

app/src/main/kotlin/com/google/ai/sample/feature/multimodal/PhotoReasoningViewModel.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,8 +2076,6 @@ private fun processCommands(text: String) {
20762076
refreshStopButtonState()
20772077
}
20782078
}
2079-
}
2080-
20812079
private fun executeAccessibilityCommand(command: Command, shouldTrackCommand: Boolean) {
20822080
ScreenOperatorAccessibilityService.executeCommand(command)
20832081
if (shouldTrackCommand) {

0 commit comments

Comments
 (0)