@@ -2,81 +2,59 @@ package com.github.blarc.ai.commits.intellij.plugin.settings.clients
22
33import com.github.blarc.ai.commits.intellij.plugin.AICommitsBundle.message
44import com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings2
5+ import com.github.blarc.ai.commits.intellij.plugin.wrap
56import com.intellij.icons.AllIcons
6- import com.intellij.openapi.application.EDT
77import com.intellij.openapi.vcs.ui.CommitMessage
88import com.intellij.ui.components.JBLabel
9- import dev.langchain4j.data.message.AiMessage
109import dev.langchain4j.data.message.UserMessage
1110import dev.langchain4j.model.chat.ChatLanguageModel
12- import dev.langchain4j.model.output.Response
1311import kotlinx.coroutines.CoroutineScope
1412import kotlinx.coroutines.Dispatchers
1513import kotlinx.coroutines.launch
1614import 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}
0 commit comments