@@ -8,32 +8,28 @@ import com.intellij.ide.actions.RevealFileAction
88import com.intellij.idea.LoggerFactory
99import com.intellij.openapi.project.ProjectManager
1010import com.intellij.ui.EditorTextField
11+ import com.intellij.ui.JBColor
1112import com.intellij.ui.dsl.builder.panel
1213import com.intellij.util.ui.FormBuilder
1314import javax.swing.JPanel
1415
1516class LLMSettingComponent (private val settings : AutoDevSettingsState ) {
1617 // 以下 LLMParam 变量不要改名,因为这些变量名会被用作配置文件的 key
17- private val languageParam by LLMParam .creating({ LanguageChangedCallback .language = it}) {
18- ComboBox (settings.language, HUMAN_LANGUAGES .values().map { it.display }) }
19- private val aiEngineParam by LLMParam .creating(onChange = { onSelectedEngineChanged() }) {
20- ComboBox (settings.aiEngine, AIEngines .values().toList().map { it.name })
18+ private val languageParam by LLMParam .creating({ LanguageChangedCallback .language = it }) {
19+ ComboBox (settings.language, HUMAN_LANGUAGES .values().map { it.display })
2120 }
21+
2222 private val delaySecondsParam by LLMParam .creating { Editable (settings.delaySeconds) }
2323 private val maxTokenLengthParam by LLMParam .creating { Editable (settings.maxTokenLength) }
24- private val openAIModelsParam by LLMParam .creating { ComboBox (settings.openAiModel, OPENAI_MODEL .toList()) }
25- private val openAIKeyParam by LLMParam .creating { Password (settings.openAiKey) }
2624 private val customModelParam: LLMParam by LLMParam .creating { Editable (settings.customModel) }
2725 private val customOpenAIHostParam: LLMParam by LLMParam .creating { Editable (settings.customOpenAiHost) }
2826
2927 private val customEngineServerParam by LLMParam .creating { Editable (settings.customEngineServer) }
3028 private val customEngineTokenParam by LLMParam .creating { Password (settings.customEngineToken) }
3129
32- private val customEngineResponseTypeParam by LLMParam .creating { ComboBox (settings.customEngineResponseType, ResponseType .values().map { it.name }.toList()) }
3330 private val customEngineResponseFormatParam by LLMParam .creating { Editable (settings.customEngineResponseFormat) }
3431 private val customEngineRequestBodyFormatParam by LLMParam .creating { Editable (settings.customEngineRequestFormat) }
3532
36-
3733 val project = ProjectManager .getInstance().openProjects.firstOrNull()
3834 private val customEnginePrompt: EditorTextField by lazy {
3935 JsonLanguageField (
@@ -44,33 +40,14 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
4440 ).apply { LanguageChangedCallback .placeholder(" autodev.custom.prompt.placeholder" , this , 1 ) }
4541 }
4642
47- private val llmGroups = mapOf<AIEngines , List <LLMParam >>(
48- AIEngines .OpenAI to listOf (
49- openAIModelsParam,
50- openAIKeyParam,
51- customModelParam,
52- customOpenAIHostParam,
53- ),
54- AIEngines .Custom to listOf (
55- customEngineResponseTypeParam,
56- customEngineServerParam,
57- customEngineTokenParam,
58- customEngineResponseFormatParam,
59- customEngineRequestBodyFormatParam,
60- ),
61- )
62-
63-
64- private val onSelectedEngineChanged: () -> Unit = {
65- applySettings(settings, updateParams = false )
66- }
67- private val _currentSelectedEngine : AIEngines
68- get() = AIEngines .values().firstOrNull { it.name.lowercase() == aiEngineParam.value.lowercase() } ? : AIEngines .OpenAI
69-
7043 private val currentLLMParams: List <LLMParam >
7144 get() {
72- return llmGroups[_currentSelectedEngine ]
73- ? : throw IllegalStateException (" Unknown engine: ${aiEngineParam.value} " )
45+ return listOf (
46+ customEngineServerParam,
47+ customEngineTokenParam,
48+ customEngineResponseFormatParam,
49+ customEngineRequestBodyFormatParam,
50+ )
7451 }
7552
7653 private fun FormBuilder.addLLMParams (llmParams : List <LLMParam >): FormBuilder = apply {
@@ -111,38 +88,31 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
11188
11289
11390 fun applySettings (settings : AutoDevSettingsState , updateParams : Boolean = false) {
114-
115- if (updateParams && engineChanged(settings).also { updateParams(settings) }) {
116- return
117- }
11891 panel.removeAll()
11992
12093 formBuilder
121- .addLLMParam(languageParam)
122- .addSeparator()
123- .addTooltip(" For Custom LLM, config Custom Engine Server & Custom Engine Token & Custom Response Format" )
124- .addLLMParam(aiEngineParam)
125- .addLLMParam(maxTokenLengthParam)
126- .addLLMParam(delaySecondsParam)
94+ .addLLMParam(languageParam)
12795 .addSeparator()
128- .addComponent(panel {
129- row {
130- comment(" For OpenAI LLM, config OpenAI Key & OpenAI Model & Custom OpenAI Host <a>Open Log for Debug</a>" ) {
131- RevealFileAction .openFile(LoggerFactory .getLogFilePath())
132- }
133- }
134- })
135- .addLLMParams(currentLLMParams)
136- .addComponent(panel {
137- if (project != null ) {
138- testLLMConnection(project)
96+ .addLLMParams(currentLLMParams)
97+ .addLLMParam(maxTokenLengthParam)
98+ .addLLMParam(delaySecondsParam)
99+ .addSeparator()
100+ .addComponent(panel {
101+ if (project != null ) {
102+ testLLMConnection(project)
103+ }
104+
105+ row {
106+ text(AutoDevBundle .message(" settings.autodev.coder.testConnectionButton.tips" )).apply {
107+ this .component.foreground = JBColor .RED
139108 }
140- })
141- .addVerticalGap(2 )
142- .addSeparator()
143- .addLabeledComponent(jBLabel(" settings.autodev.coder.customEnginePrompt" , 1 ), customEnginePrompt, 1 , true )
144- .addComponentFillVertically(JPanel (), 0 )
145- .panel
109+ }
110+ })
111+ .addVerticalGap(2 )
112+ .addSeparator()
113+ .addLabeledComponent(jBLabel(" settings.autodev.coder.customEnginePrompt" , 1 ), customEnginePrompt, 1 , true )
114+ .addComponentFillVertically(JPanel (), 0 )
115+ .panel
146116
147117 panel.invalidate()
148118 panel.repaint()
@@ -151,15 +121,11 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
151121 private fun updateParams (settings : AutoDevSettingsState ) {
152122 settings.apply {
153123 maxTokenLengthParam.value = maxTokenLength
154- openAIKeyParam.value = openAiKey
155124 customModelParam.value = customModel
156125 customOpenAIHostParam.value = customOpenAiHost
157126 customEngineServerParam.value = customEngineServer
158- customEngineResponseTypeParam.value = customEngineResponseType
159127 customEngineTokenParam.value = customEngineToken
160- openAIModelsParam.value = openAiModel
161128 languageParam.value = language
162- aiEngineParam.value = aiEngine
163129 customEnginePrompt.text = customPrompts
164130 customEngineResponseFormatParam.value = customEngineResponseFormat
165131 customEngineRequestBodyFormatParam.value = customEngineRequestFormat
@@ -170,16 +136,12 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
170136 fun exportSettings (destination : AutoDevSettingsState ) {
171137 destination.apply {
172138 maxTokenLength = maxTokenLengthParam.value
173- openAiKey = openAIKeyParam.value
174139 customModel = customModelParam.value
175140 customOpenAiHost = customOpenAIHostParam.value
176- aiEngine = aiEngineParam.value
177141 language = languageParam.value
178142 customEngineServer = customEngineServerParam.value
179- customEngineResponseType = customEngineResponseTypeParam.value
180143 customEngineToken = customEngineTokenParam.value
181144 customPrompts = customEnginePrompt.text
182- openAiModel = openAIModelsParam.value
183145 customEngineResponseFormat = customEngineResponseFormatParam.value
184146 customEngineRequestFormat = customEngineRequestBodyFormatParam.value
185147 delaySeconds = delaySecondsParam.value
@@ -188,25 +150,17 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
188150
189151 fun isModified (settings : AutoDevSettingsState ): Boolean {
190152 return settings.maxTokenLength != maxTokenLengthParam.value ||
191- settings.openAiKey != openAIKeyParam.value ||
192153 settings.customModel != customModelParam.value ||
193- settings.aiEngine != aiEngineParam.value ||
194154 settings.language != languageParam.value ||
195155 settings.customEngineServer != customEngineServerParam.value ||
196- settings.customEngineResponseType != customEngineResponseTypeParam.value ||
197156 settings.customEngineToken != customEngineTokenParam.value ||
198157 settings.customPrompts != customEnginePrompt.text ||
199- settings.openAiModel != openAIModelsParam.value ||
200158 settings.customOpenAiHost != customOpenAIHostParam.value ||
201159 settings.customEngineResponseFormat != customEngineResponseFormatParam.value ||
202160 settings.customEngineRequestFormat != customEngineRequestBodyFormatParam.value ||
203161 settings.delaySeconds != delaySecondsParam.value
204162 }
205163
206- private fun engineChanged (settings : AutoDevSettingsState ): Boolean {
207- return settings.aiEngine != aiEngineParam.value
208- }
209-
210164 init {
211165 applySettings(settings)
212166 LanguageChangedCallback .language = AutoDevSettingsState .getInstance().language
0 commit comments