11package com.github.blarc.ai.commits.intellij.plugin.settings
22
3- import com.aallam.openai.api.exception.OpenAIAPIException
4- import com.github.blarc.ai.commits.intellij.plugin.*
3+ import com.github.blarc.ai.commits.intellij.plugin.AICommitsBundle
54import com.github.blarc.ai.commits.intellij.plugin.AICommitsBundle.message
65import com.github.blarc.ai.commits.intellij.plugin.settings.clients.LLMClientTable
76import com.github.blarc.ai.commits.intellij.plugin.settings.prompts.Prompt
87import com.github.blarc.ai.commits.intellij.plugin.settings.prompts.PromptTable
9- import com.intellij.icons.AllIcons
108import com.intellij.openapi.options.BoundConfigurable
11- import com.intellij.openapi.progress.runBackgroundableTask
129import com.intellij.openapi.ui.ComboBox
13- import com.intellij.openapi.ui.naturalSorted
1410import com.intellij.ui.CommonActionsPanel
1511import com.intellij.ui.ToolbarDecorator
1612import com.intellij.ui.components.JBLabel
17- import com.intellij.ui.components.JBPasswordField
18- import com.intellij.ui.components.JBTextField
1913import com.intellij.ui.dsl.builder.*
20- import com.intellij.ui.util.minimumWidth
21- import kotlinx.coroutines.*
2214import java.util.*
23- import javax.swing.DefaultComboBoxModel
2415
2516class AppSettingsConfigurable : BoundConfigurable (message("settings.general.group.title")) {
2617
27- private val hostComboBox = ComboBox <String >()
28- private val tokenPasswordField = JBPasswordField ()
29- private val verifyLabel = JBLabel ()
30- private val proxyTextField = JBTextField ()
31- private val socketTimeoutTextField = JBTextField ()
32- private var modelComboBox = ComboBox <String >()
3318 private val llmClientTable = LLMClientTable ()
3419 private val promptTable = PromptTable ()
3520 private lateinit var llmClientToolbarDecorator: ToolbarDecorator
3621 private lateinit var toolbarDecorator: ToolbarDecorator
3722 private lateinit var promptComboBox: Cell <ComboBox <Prompt >>
3823
39- init {
40- hostComboBox.isEditable = true
41- hostComboBox.model = DefaultComboBoxModel (Vector (AppSettings2 .instance.getActiveLLMClient().hosts.naturalSorted()))
42- modelComboBox.model = DefaultComboBoxModel (Vector (AppSettings2 .instance.getActiveLLMClient().modelIds.naturalSorted()))
43- modelComboBox.renderer = AppSettingsListCellRenderer ()
44- }
45-
4624 override fun createPanel () = panel {
4725
4826 group(JBLabel (" OpenAI" )) {
49- row {
50- label(message(" settings.openAIHost" ))
51- .widthGroup(" label" )
52-
53- cell(hostComboBox)
54- .bindItem(AppSettings2 .instance.getActiveLLMClient()::host.toNullableProperty())
55- .widthGroup(" input" )
56- }
57- row {
58- label(message(" settings.openAIProxy" )).widthGroup(" label" )
59- cell(proxyTextField)
60- .bindText(AppSettings2 .instance.getActiveLLMClient()::proxyUrl.toNonNullableProperty(" " ))
61- .applyToComponent { minimumWidth = 400 }
62- .resizableColumn()
63- .widthGroup(" input" )
64- }
65- row {
66- comment(message(" settings.openAIProxyComment" ))
67- }
68- row {
69- label(message(" settings.openAISocketTimeout" )).widthGroup(" label" )
70- cell(socketTimeoutTextField)
71- .bindIntText(AppSettings2 .instance.getActiveLLMClient()::timeout)
72- .applyToComponent { minimumWidth = 400 }
73- .resizableColumn()
74- .widthGroup(" input" )
75- .validationOnInput { isInt(it.text) }
76- }
77- row {
78- label(message(" settings.openAIToken" ))
79- .widthGroup(" label" )
80- cell(tokenPasswordField)
81- .bindText(
82- { AppSettings2 .instance.getActiveLLMClient().token.orEmpty() },
83- { AppSettings2 .instance.getActiveLLMClient().token = it }
84- )
85- .emptyText(message(" settings.openAITokenExample" ))
86- .align(Align .FILL )
87- .resizableColumn()
88- .focused()
89- button(message(" settings.verifyToken" )) { verifyToken() }
90- .align(AlignX .RIGHT )
91- .widthGroup(" button" )
92- }
93- row {
94- comment(message(" settings.openAITokenComment" ))
95- .align(AlignX .LEFT )
96- cell(verifyLabel)
97- .align(AlignX .RIGHT )
98- }
99- row {
100- label(message(" settings.openAIModel" )).widthGroup(" label" )
101-
102- cell(modelComboBox)
103- .bindItem({ AppSettings2 .instance.getActiveLLMClient().modelId }, {
104- if (it != null ) {
105- AppSettings2 .instance.getActiveLLMClient().modelId = it
106- }
107- })
108- .resizableColumn()
109- .align(Align .FILL )
110- button(message(" settings.refreshModels" )) {
111- runBackgroundableTask(message(" settings.loadingModels" )) {
112- runBlocking(Dispatchers .IO ) {
113- OpenAIService .instance.refreshOpenAIModelIds()
114- modelComboBox.model = DefaultComboBoxModel (Vector (AppSettings2 .instance.getActiveLLMClient().modelIds.naturalSorted()))
115- modelComboBox.item = AppSettings2 .instance.getActiveLLMClient().modelId
116- }
117- }
118- }
119- .align(AlignX .RIGHT )
120- .widthGroup(" button" )
121- }
122-
123- row {
124- label(message(" settings.openAITemperature" ))
125- .widthGroup(" label" )
126-
127- textField()
128- .bindText(AppSettings2 .instance.getActiveLLMClient()::temperature)
129- .applyToComponent { minimumWidth = 400 }
130- .resizableColumn()
131- .widthGroup(" input" )
132- .validationOnInput { temperatureValid(it.text) }
133-
134- contextHelp(message(" settings.openAITemperatureComment" ))
135- }
136-
137- row {
138- cell(verifyLabel)
139- .align(AlignX .RIGHT )
140- }
141-
14227 row {
14328 llmClientToolbarDecorator = ToolbarDecorator .createDecorator(llmClientTable.table)
14429 .setAddAction {
@@ -151,8 +36,6 @@ class AppSettingsConfigurable : BoundConfigurable(message("settings.general.grou
15136 }
15237 }
15338
154-
155-
15639 group(JBLabel (" Prompt" )) {
15740 row {
15841 label(message(" settings.locale" )).widthGroup(" labelPrompt" )
@@ -224,7 +107,8 @@ class AppSettingsConfigurable : BoundConfigurable(message("settings.general.grou
224107 }
225108
226109 override fun apply () {
227- AppSettings2 .instance.getActiveLLMClient().hosts.add(hostComboBox.item)
110+ // TODO @Blarc
111+ // AppSettings2.instance.getActiveLLMClient().hosts.add(hostComboBox.item)
228112 promptTable.apply ()
229113 super .apply ()
230114 }
@@ -234,34 +118,4 @@ class AppSettingsConfigurable : BoundConfigurable(message("settings.general.grou
234118 super .reset()
235119 }
236120
237- @OptIn(DelicateCoroutinesApi ::class )
238- private fun verifyToken () {
239- runBackgroundableTask(message(" settings.verify.running" )) {
240- if (tokenPasswordField.password.isEmpty()) {
241- verifyLabel.icon = AllIcons .General .InspectionsError
242- verifyLabel.text = message(" settings.verify.token-is-empty" )
243- } else {
244- verifyLabel.icon = AllIcons .General .InlineRefreshHover
245- verifyLabel.text = message(" settings.verify.running" )
246-
247- GlobalScope .launch(Dispatchers .IO ) {
248- try {
249- OpenAIService .instance.verifyOpenAIConfiguration(hostComboBox.item, String (tokenPasswordField.password), proxyTextField.text, socketTimeoutTextField.text)
250- verifyLabel.text = message(" settings.verify.valid" )
251- verifyLabel.icon = AllIcons .General .InspectionsOK
252- } catch (e: OpenAIAPIException ) {
253- verifyLabel.text = message(" settings.verify.invalid" , e.statusCode)
254- verifyLabel.icon = AllIcons .General .InspectionsError
255- } catch (e: NumberFormatException ) {
256- verifyLabel.text = message(" settings.verify.invalid" , e.localizedMessage)
257- verifyLabel.icon = AllIcons .General .InspectionsError
258- } catch (e: Exception ) {
259- verifyLabel.text = message(" settings.verify.invalid" , " Unknown" )
260- verifyLabel.icon = AllIcons .General .InspectionsError
261- }
262- }
263- }
264- }
265-
266- }
267121}
0 commit comments