Skip to content

Commit 072ae53

Browse files
committed
feat(settings): connect validators and binders for LLMClientPanels
1 parent d1bb26c commit 072ae53

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ class AppSettingsConfigurable : BoundConfigurable(message("settings.general.grou
4040
llmClientTable.addLlmClient()
4141
}
4242
.setEditAction {
43-
llmClientTable.editLlmClient()
43+
llmClientTable.editLlmClient()?.let {
44+
val editingActive = llmClientComboBox.selectedItem == it.first
45+
llmClientComboBox.removeItem(it.first)
46+
llmClientComboBox.addItem(it.second)
47+
48+
if (editingActive) {
49+
llmClientComboBox.selectedItem = it.second
50+
}
51+
}
4452
}
4553
.setRemoveAction {
4654
llmClientTable.removeLlmClient()?.let {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ abstract class LLMClient(
1515
@Attribute var timeout: Int,
1616
@Attribute var modelId: String,
1717
@Attribute var temperature: String
18-
) {
19-
18+
) : Cloneable {
2019
@get:Transient
2120
var token: String
2221
get() = retrieveToken(displayName) ?: ""
@@ -30,6 +29,8 @@ abstract class LLMClient(
3029

3130
abstract suspend fun refreshModels()
3231

32+
public abstract override fun clone(): LLMClient
33+
3334
@Throws(Exception::class)
3435
abstract suspend fun verifyConfiguration(
3536
newHost: String,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.blarc.ai.commits.intellij.plugin.settings.clients
22

3-
import javax.swing.JComponent
3+
import com.intellij.openapi.ui.DialogPanel
44

55
interface LLMClientPanel {
66

7-
fun create(): JComponent
7+
fun create(): DialogPanel
88

99
}

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import com.github.blarc.ai.commits.intellij.plugin.AICommitsBundle.message
44
import com.github.blarc.ai.commits.intellij.plugin.createColumn
55
import com.github.blarc.ai.commits.intellij.plugin.settings.AICommitsListCellRenderer
66
import com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings2
7+
import com.intellij.openapi.ui.DialogPanel
78
import com.intellij.openapi.ui.DialogWrapper
9+
import com.intellij.openapi.ui.ValidationInfo
810
import com.intellij.ui.JBCardLayout
911
import com.intellij.ui.components.Panel
1012
import com.intellij.ui.dsl.builder.Align
1113
import com.intellij.ui.dsl.builder.bindItem
1214
import com.intellij.ui.dsl.builder.panel
1315
import com.intellij.ui.dsl.builder.toNullableProperty
1416
import com.intellij.ui.table.TableView
17+
import com.intellij.util.containers.ContainerUtil
1518
import com.intellij.util.ui.ListTableModel
1619
import java.awt.event.MouseAdapter
1720
import java.awt.event.MouseEvent
@@ -65,13 +68,15 @@ class LLMClientTable {
6568

6669
}
6770

68-
fun editLlmClient(): LLMClient? {
71+
fun editLlmClient(): Pair<LLMClient, LLMClient>? {
6972
val selectedLlmClient = table.selectedObject ?: return null
70-
val dialog = LLMClientDialog(selectedLlmClient)
73+
val dialog = LLMClientDialog(selectedLlmClient.clone())
7174

7275
if (dialog.showAndGet()) {
76+
llmClients = llmClients.minus(selectedLlmClient)
77+
llmClients = llmClients.plus(dialog.llmClient)
7378
refreshTableModel()
74-
return selectedLlmClient
79+
return selectedLlmClient to dialog.llmClient
7580
}
7681
return null
7782
}
@@ -95,6 +100,7 @@ class LLMClientTable {
95100
private val cardLayout = JBCardLayout()
96101
private val cardPanel = Panel(null, cardLayout)
97102
private val llmClients: List<LLMClient> = getLlmClients(newLlmClient)
103+
private var currentLlmClientDisplayName: String = llmClients[0].displayName
98104
var llmClient = newLlmClient ?: llmClients[0]
99105

100106
init {
@@ -104,16 +110,28 @@ class LLMClientTable {
104110
llmClients.forEach {
105111
cardPanel.add(it.displayName, it.panel().create())
106112
}
107-
cardLayout.show(cardPanel, llmClients[0].displayName)
113+
cardLayout.show(cardPanel, currentLlmClientDisplayName)
114+
(cardLayout.findComponentById(currentLlmClientDisplayName) as DialogPanel).registerValidators(myDisposable) {
115+
isOKActionEnabled = ContainerUtil.and(it.values) { info: ValidationInfo -> info.okEnabled }
116+
}
117+
108118
init()
109119
}
110120

121+
override fun doOKAction() {
122+
(cardLayout.findComponentById(currentLlmClientDisplayName) as DialogPanel).apply()
123+
super.doOKAction()
124+
}
125+
111126
override fun createCenterPanel() = panel {
112127
row("Client") {
113128
comboBox(llmClients, AICommitsListCellRenderer())
114129
.align(Align.FILL)
115130
.applyToComponent {
116-
addItemListener { cardLayout.show(cardPanel, (it.item as LLMClient).displayName) }
131+
addItemListener {
132+
currentLlmClientDisplayName = (it.item as LLMClient).displayName
133+
cardLayout.show(cardPanel, currentLlmClientDisplayName)
134+
}
117135
}
118136
.applyToComponent {
119137
isEnabled = newLlmClient == null

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ class OpenAIClient(displayName: String = "OpenAI") : LLMClient(
7070
.forEach { modelIds.add(it) }
7171
}
7272

73+
override fun clone(): LLMClient {
74+
val copy = OpenAIClient(displayName)
75+
copy.host = host
76+
copy.proxyUrl = proxyUrl
77+
copy.timeout = timeout
78+
copy.modelId = modelId
79+
copy.temperature = temperature
80+
return copy
81+
}
82+
7383
@Throws(Exception::class)
7484
override suspend fun verifyConfiguration(
7585
newHost: String,

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

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

3-
class TestAIClient(displayName: String = "TestAI") : LLMClient(
4-
displayName,
3+
data class TestAIClient(var name: String = "TestAI") : LLMClient(
4+
name,
55
"testAiBaseUrl",
66
null,
77
30,
@@ -23,6 +23,10 @@ class TestAIClient(displayName: String = "TestAI") : LLMClient(
2323
override suspend fun refreshModels() {
2424
}
2525

26+
override fun clone(): TestAIClient {
27+
return copy()
28+
}
29+
2630
override suspend fun verifyConfiguration(newHost: String, newProxy: String?, newTimeout: String, newToken: String) {
2731
}
2832

0 commit comments

Comments
 (0)