Skip to content

Commit 20e8aac

Browse files
Track and cancel active Mistral job between requests
1 parent 6229b1f commit 20e8aac

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,15 +1026,16 @@ class PhotoReasoningViewModel(
10261026
)
10271027
}
10281028

1029-
private fun reasonWithMistral(
1030-
userInput: String,
1031-
selectedImages: List<Bitmap>,
1032-
screenInfoForPrompt: String? = null,
1033-
imageUrisForChat: List<String>? = null
1034-
) {
1035-
_uiState.value = PhotoReasoningUiState.Loading
1036-
val context = appContext
1037-
val apiKeyManager = ApiKeyManager.getInstance(context)
1029+
private fun reasonWithMistral(
1030+
userInput: String,
1031+
selectedImages: List<Bitmap>,
1032+
screenInfoForPrompt: String? = null,
1033+
imageUrisForChat: List<String>? = null
1034+
) {
1035+
_uiState.value = PhotoReasoningUiState.Loading
1036+
_showStopNotificationFlow.value = true
1037+
val context = appContext
1038+
val apiKeyManager = ApiKeyManager.getInstance(context)
10381039

10391040
val initialApiKey = apiKeyManager.getCurrentApiKey(ApiProvider.MISTRAL)
10401041
if (initialApiKey.isNullOrEmpty()) {
@@ -1056,7 +1057,8 @@ private fun reasonWithMistral(
10561057

10571058
resetStreamingCommandState()
10581059

1059-
viewModelScope.launch(Dispatchers.IO) {
1060+
currentReasoningJob?.cancel()
1061+
currentReasoningJob = viewModelScope.launch(Dispatchers.IO) {
10601062
try {
10611063
val currentModel = com.google.ai.sample.GenerativeAiViewModelFactory.getCurrentModel()
10621064
val genSettings = com.google.ai.sample.util.GenerationSettingsPreferences.loadSettings(context, currentModel.modelName)
@@ -1301,6 +1303,10 @@ private fun reasonWithMistral(
13011303
appendErrorMessage("Error: ${e.message}")
13021304
saveChatHistory(context)
13031305
}
1306+
} finally {
1307+
withContext(Dispatchers.Main) {
1308+
refreshStopButtonState()
1309+
}
13041310
}
13051311
}
13061312
}

0 commit comments

Comments
 (0)