Skip to content

Commit

Permalink
fix(clients): NPE when verifying configuration
Browse files Browse the repository at this point in the history
Use ModalityState to enable switching to EDT when updating text and icon.

Closes #239
  • Loading branch information
Blarc committed Sep 9, 2024
1 parent fc1fa8c commit baed926
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

- Rethrow generic exceptions when generating commit messages.

### Fixed

- NPE when verifying LLM client configuration.

## [2.2.0] - 2024-08-01

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings2
import com.github.blarc.ai.commits.intellij.plugin.wrap
import com.intellij.icons.AllIcons
import com.intellij.openapi.application.EDT
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.asContextElement
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.ui.CommitMessage
import com.intellij.platform.ide.progress.withBackgroundProgress
Expand All @@ -21,7 +23,7 @@ abstract class LLMClientService<T : LLMClientConfiguration>(private val cs: Coro
abstract suspend fun buildChatModel(client: T): ChatLanguageModel

fun generateCommitMessage(client: T, prompt: String, project: Project, commitMessage: CommitMessage) {
cs.launch(Dispatchers.Default) {
cs.launch(Dispatchers.IO + ModalityState.current().asContextElement()) {
withBackgroundProgress(project, message("action.background")) {
sendRequest(client, prompt, onSuccess = {
withContext(Dispatchers.EDT) {
Expand All @@ -38,17 +40,18 @@ abstract class LLMClientService<T : LLMClientConfiguration>(private val cs: Coro
}

fun verifyConfiguration(client: T, label: JBLabel) {
// TODO @Blarc: Can you make this better? with notifications?
label.text = message("settings.verify.running")
cs.launch(Dispatchers.Default) {
cs.launch(Dispatchers.IO + ModalityState.current().asContextElement()) {
sendRequest(client, "test", onSuccess = {
// This can't be called from EDT thread, because dialog blocks the EDT thread
label.text = message("settings.verify.valid")
label.icon = AllIcons.General.InspectionsOK
withContext(Dispatchers.EDT) {
label.text = message("settings.verify.valid")
label.icon = AllIcons.General.InspectionsOK
}
}, onError = {
// This can't be called from EDT thread, because dialog blocks the EDT thread
label.text = it.wrap(60)
label.icon = AllIcons.General.InspectionsError
withContext(Dispatchers.EDT) {
label.text = it.wrap(60)
label.icon = AllIcons.General.InspectionsError
}
})
}
}
Expand Down

0 comments on commit baed926

Please sign in to comment.