Skip to content

Commit 7d76111

Browse files
committed
feat(openAi): properly save hosts and model ids, remove refresh models, LLMClient implements comparable
1 parent d029c8e commit 7d76111

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class AICommitAction : AnAction(), DumbAware {
3131
val includedChanges = commitWorkflowHandler.ui.getIncludedChanges()
3232
val commitMessage = VcsDataKeys.COMMIT_MESSAGE_CONTROL.getData(e.dataContext) as CommitMessage?
3333

34+
// FIXME @Blarc: Use coroutine with Dispatchers.IO!!!!!!!!!!!
3435
runBackgroundableTask(message("action.background"), project) {
3536
val diff = computeDiff(includedChanges, false, project)
3637
if (diff.isBlank()) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class LLMClient(
1616
@Attribute var timeout: Int,
1717
@Attribute var modelId: String,
1818
@Attribute var temperature: String,
19-
) : Cloneable {
19+
) : Cloneable, Comparable<LLMClient> {
2020
@get:Transient
2121
var token: String
2222
get() = retrieveToken(displayName) ?: ""
@@ -52,4 +52,8 @@ abstract class LLMClient(
5252
sendNotification(Notification.unableToSaveToken(e.message))
5353
}
5454
}
55+
56+
override fun compareTo(other: LLMClient): Int {
57+
return displayName.compareTo(other.displayName)
58+
}
5559
}

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

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

33
import com.github.blarc.ai.commits.intellij.plugin.Icons
4-
import com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings
54
import com.github.blarc.ai.commits.intellij.plugin.settings.clients.LLMClient
65
import com.intellij.openapi.components.service
76
import dev.langchain4j.data.message.UserMessage
@@ -40,7 +39,7 @@ class OpenAiClient(displayName: String = "OpenAI") : LLMClient(
4039
.modelName(modelId)
4140
.temperature(temperature.toDouble())
4241
.timeout(Duration.ofSeconds(timeout.toLong()))
43-
.baseUrl(AppSettings.instance.openAIHost)
42+
.baseUrl(host)
4443
.build()
4544

4645
val response = openAI.generate(
@@ -54,10 +53,8 @@ class OpenAiClient(displayName: String = "OpenAI") : LLMClient(
5453
return response.content().text()
5554
}
5655

57-
override fun getRefreshModelFunction(): (suspend () -> Unit)? {
58-
// Model names are retrieved from Enum and do not need to be refreshed.
59-
return null
60-
}
56+
// Model names are retrieved from Enum and do not need to be refreshed.
57+
override fun getRefreshModelFunction() = null
6158

6259
override fun clone(): LLMClient {
6360
val copy = OpenAiClient(displayName)

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import com.intellij.icons.AllIcons
99
import com.intellij.openapi.components.service
1010
import com.intellij.openapi.progress.runBackgroundableTask
1111
import com.intellij.openapi.ui.ComboBox
12-
import com.intellij.openapi.ui.naturalSorted
1312
import com.intellij.ui.components.JBLabel
1413
import com.intellij.ui.components.JBPasswordField
1514
import com.intellij.ui.components.JBTextField
1615
import com.intellij.ui.dsl.builder.*
1716
import com.intellij.ui.util.minimumWidth
1817
import dev.ai4j.openai4j.OpenAiHttpException
19-
import kotlinx.coroutines.*
18+
import kotlinx.coroutines.DelicateCoroutinesApi
19+
import kotlinx.coroutines.Dispatchers
20+
import kotlinx.coroutines.GlobalScope
21+
import kotlinx.coroutines.launch
2022
import kotlinx.serialization.Serializable
2123
import kotlinx.serialization.json.Json
22-
import javax.swing.DefaultComboBoxModel
2324

2425
class OpenAiClientPanel(private val client: OpenAiClient) : LLMClientPanel {
2526

@@ -95,20 +96,6 @@ class OpenAiClientPanel(private val client: OpenAiClient) : LLMClientPanel {
9596
.widthGroup("input")
9697
.resizableColumn()
9798
.onApply { service<OpenAiClientService>().modelIds.add(modelComboBox.item) }
98-
99-
client.getRefreshModelFunction()?.let { f ->
100-
button(message("settings.refreshModels")) {
101-
runBackgroundableTask(message("settings.loadingModels")) {
102-
runBlocking(Dispatchers.IO) {
103-
f.invoke()
104-
modelComboBox.model = DefaultComboBoxModel(client.getModelIds().naturalSorted().toTypedArray())
105-
modelComboBox.item = client.modelId
106-
}
107-
}
108-
}
109-
.align(AlignX.RIGHT)
110-
.widthGroup("button")
111-
}
11299
}
113100

114101
row {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import com.intellij.openapi.components.Service
55
import com.intellij.openapi.components.State
66
import com.intellij.openapi.components.Storage
77
import com.intellij.util.xmlb.XmlSerializerUtil
8+
import com.intellij.util.xmlb.annotations.XCollection
89
import dev.langchain4j.model.openai.OpenAiChatModelName
910

10-
@Service
11+
@Service(Service.Level.APP)
1112
@State(name = "OpenAiClientService", storages = [Storage("AICommitsOpenAi.xml")])
1213
class OpenAiClientService : PersistentStateComponent<OpenAiClientService> {
13-
14+
@XCollection(style = XCollection.Style.v2)
1415
val hosts = mutableSetOf("https://api.openai.com/v1")
16+
17+
@XCollection(style = XCollection.Style.v2)
1518
val modelIds = OpenAiChatModelName.entries.stream()
1619
.map { it.toString() }
1720
.toList()

0 commit comments

Comments
 (0)