Skip to content

Commit 32453bf

Browse files
committed
feat(clients): show client exceptions
1 parent 6ab4c11 commit 32453bf

File tree

4 files changed

+55
-56
lines changed

4 files changed

+55
-56
lines changed

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/AICommitsExtensions.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,21 @@ fun <T>Cell<T>.emptyText(emptyText: String) : Cell<T> where T : JComponent, T :
5353
this.component.emptyText.text = emptyText
5454
return this
5555
}
56+
57+
fun String.wrap(length: Int): String {
58+
var input = this
59+
val wrapped = StringBuilder()
60+
61+
while (input.length > length) {
62+
var index = input.lastIndexOf(' ', length)
63+
64+
if (index == -1) index = length
65+
wrapped.append(input.substring(0, index)).append("<br>")
66+
67+
input = input.substring(index).trimStart()
68+
}
69+
70+
wrapped.append(input)
71+
72+
return wrapped.toString()
73+
}

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/clients/LLMClientPanel.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ abstract class LLMClientPanel(
119119
open fun Panel.verifyRow() {
120120
row {
121121
cell(verifyLabel)
122-
.applyToComponent { setAllowAutoWrapping(true) }
122+
.applyToComponent {
123+
setAllowAutoWrapping(true)
124+
setCopyable(true)
125+
}
123126
.align(AlignX.LEFT)
124127

125128
button(message("settings.verifyToken")) { verifyConfiguration() }

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/clients/LLMClientService.kt

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,59 @@ package com.github.blarc.ai.commits.intellij.plugin.settings.clients
22

33
import com.github.blarc.ai.commits.intellij.plugin.AICommitsBundle.message
44
import com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings2
5+
import com.github.blarc.ai.commits.intellij.plugin.wrap
56
import com.intellij.icons.AllIcons
6-
import com.intellij.openapi.application.EDT
77
import com.intellij.openapi.vcs.ui.CommitMessage
88
import com.intellij.ui.components.JBLabel
9-
import dev.langchain4j.data.message.AiMessage
109
import dev.langchain4j.data.message.UserMessage
1110
import dev.langchain4j.model.chat.ChatLanguageModel
12-
import dev.langchain4j.model.output.Response
1311
import kotlinx.coroutines.CoroutineScope
1412
import kotlinx.coroutines.Dispatchers
1513
import kotlinx.coroutines.launch
1614
import kotlinx.coroutines.withContext
1715

18-
abstract class LLMClientService <T: LLMClientConfiguration>(private val cs: CoroutineScope) {
16+
abstract class LLMClientService<T : LLMClientConfiguration>(private val cs: CoroutineScope) {
1917

2018
abstract fun buildChatModel(client: T): ChatLanguageModel
2119

2220
fun generateCommitMessage(client: T, prompt: String, commitMessage: CommitMessage) {
23-
// TODO @Blarc: catch exceptions
2421
val model = buildChatModel(client)
25-
sendRequest(model, prompt) {
26-
withContext(Dispatchers.EDT) {
27-
commitMessage.setCommitMessage(it.content().text())
28-
}
22+
sendRequest(model, prompt, onSuccess = {
23+
commitMessage.setCommitMessage(it)
2924
AppSettings2.instance.recordHit()
30-
}
25+
}, onError = {
26+
commitMessage.setCommitMessage(it)
27+
})
3128
}
3229

3330
fun verifyConfiguration(client: T, label: JBLabel) {
34-
// TODO @Blarc: catch exceptions
3531
val model = buildChatModel(client)
36-
sendRequest(model, "test") {
37-
withContext(Dispatchers.EDT) {
38-
label.text = message("settings.verify.valid")
39-
label.icon = AllIcons.General.InspectionsOK
40-
}
41-
}
42-
43-
// GlobalScope.launch(Dispatchers.IO) {
44-
// try {
45-
// client.verifyConfiguration(hostComboBox.item, proxyTextField.text, socketTimeoutTextField.text, modelComboBox.item, String(tokenPasswordField.password))
46-
// verifyLabel.text = message("settings.verify.valid")
47-
// verifyLabel.icon = AllIcons.General.InspectionsOK
48-
// } catch (e: Exception) {
49-
// var errorMessage = e.localizedMessage
50-
// if (e.cause is OpenAiHttpException) {
51-
// val openAiError = Json.decodeFromString<OpenAiErrorWrapper>((e.cause as OpenAiHttpException).message!!).error
52-
// errorMessage = openAiError.code
53-
// }
54-
// verifyLabel.text = message("settings.verify.invalid", errorMessage)
55-
// verifyLabel.icon = AllIcons.General.InspectionsError
56-
// }
57-
// }
32+
sendRequest(model, "test", onSuccess = {
33+
label.text = message("settings.verify.valid")
34+
label.icon = AllIcons.General.InspectionsOK
35+
}, onError = {
36+
label.text = it.wrap(80)
37+
label.icon = AllIcons.General.InspectionsError
38+
})
5839
}
5940

60-
// @Serializable
61-
// data class OpenAiError(val message: String?, val type: String?, val param: String?, val code: String?)
62-
//
63-
// @Serializable
64-
// data class OpenAiErrorWrapper(val error: OpenAiError)
65-
66-
private fun sendRequest(model: ChatLanguageModel, text: String, onResponse: suspend (r: Response<AiMessage>) -> Unit ) {
41+
private fun sendRequest(model: ChatLanguageModel, text: String, onSuccess: suspend (r: String) -> Unit, onError: suspend (r: String) -> Unit) {
6742
cs.launch(Dispatchers.Default) {
68-
val response = withContext(Dispatchers.IO) {
69-
model.generate(
70-
listOf(
71-
UserMessage.from(
72-
"user",
73-
text
43+
try {
44+
val response = withContext(Dispatchers.IO) {
45+
model.generate(
46+
listOf(
47+
UserMessage.from(
48+
"user",
49+
text
50+
)
7451
)
75-
)
76-
)
52+
).content().text()
53+
}
54+
onSuccess(response)
55+
} catch (e: Exception) {
56+
onError(e.message ?: "Unknown error.")
7757
}
78-
onResponse(response)
7958
}
8059
}
81-
8260
}

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/clients/ollama/OllamaClientService.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.blarc.ai.commits.intellij.plugin.settings.clients.ollama
22

33
import com.github.blarc.ai.commits.intellij.plugin.settings.clients.LLMClientService
4-
import com.intellij.openapi.application.EDT
54
import com.intellij.openapi.components.Service
65
import com.intellij.openapi.components.service
76
import com.intellij.openapi.ui.ComboBox
@@ -38,10 +37,11 @@ class OllamaClientService(private val cs: CoroutineScope) : LLMClientService<Oll
3837
OllamaClientSharedState.getInstance().modelIds.addAll(availableModels.content()
3938
.map { it.name }
4039
)
41-
withContext(Dispatchers.EDT) {
42-
comboBox.model = DefaultComboBoxModel(client.getModelIds().naturalSorted().toTypedArray())
43-
comboBox.item = client.modelId
44-
}
40+
41+
// This can't be called from EDT thread, because dialog blocks the EDT thread
42+
val modelItems = client.getModelIds().naturalSorted().toTypedArray()
43+
comboBox.model = DefaultComboBoxModel(modelItems)
44+
comboBox.item = client.modelId
4545
}
4646
}
4747

0 commit comments

Comments
 (0)