forked from unit-mesh/auto-dev
/
LLMSettingComponent.kt
267 lines (234 loc) ยท 12.2 KB
/
LLMSettingComponent.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package cc.unitmesh.devti.settings
import cc.unitmesh.devti.AutoDevBundle
import com.intellij.json.JsonLanguage
import com.intellij.openapi.editor.colors.EditorColorsUtil
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.project.ProjectManager
import com.intellij.ui.LanguageTextField
import com.intellij.ui.components.JBLabel
import com.intellij.util.ui.FormBuilder
import java.awt.Dimension
import java.awt.FontMetrics
import javax.swing.JPanel
class LLMSettingComponent(private val settings: AutoDevSettingsState) {
// ไปฅไธ LLMParam ๅ้ไธ่ฆๆนๅ๏ผๅ ไธบ่ฟไบๅ้ๅไผ่ขซ็จไฝ้
็ฝฎๆไปถ็ key
private val languageParam by LLMParam.creating { ComboBox(settings.language, HUMAN_LANGUAGES.toList()) }
private val aiEngineParam by LLMParam.creating(onChange = { onSelectedEngineChanged() }) {
ComboBox(settings.aiEngine, AIEngines.values().toList().map { it.name })
}
private val delaySecondsParam by LLMParam.creating { Editable(settings.delaySeconds) }
private val maxTokenLengthParam by LLMParam.creating { Editable(settings.maxTokenLength) }
private val openAIModelsParam by LLMParam.creating { ComboBox(settings.openAiModel, OPENAI_MODEL.toList()) }
private val openAIKeyParam by LLMParam.creating { Password(settings.openAiKey) }
private val customModelParam: LLMParam by LLMParam.creating { Editable(settings.customModel) }
private val customOpenAIHostParam: LLMParam by LLMParam.creating { Editable(settings.customOpenAiHost) }
private val gitTypeParam: LLMParam by LLMParam.creating { ComboBox(settings.gitType, GIT_TYPE.toList()) }
private val gitLabUrlParam: LLMParam by LLMParam.creating { Editable(settings.gitlabUrl) }
private val gitLabTokenParam: LLMParam by LLMParam.creating { Password(settings.gitlabToken) }
private val gitHubTokenParam by LLMParam.creating { Password(settings.githubToken) }
private val customEngineServerParam by LLMParam.creating { Editable(settings.customEngineServer) }
private val customEngineTokenParam by LLMParam.creating { Password(settings.customEngineToken) }
private val xingHuoApiVersionParam by LLMParam.creating {
ComboBox("${settings.xingHuoApiVersion}", XingHuoApiVersion.values().toList().map { it.toString() })
}
private val xingHuoAppIDParam by LLMParam.creating { Editable(settings.xingHuoAppId) }
private val xingHuoApiKeyParam by LLMParam.creating { Password(settings.xingHuoApiKey) }
private val xingHuoApiSecretParam by LLMParam.creating { Password(settings.xingHuoApiSecrect) }
private val customEngineResponseTypeParam by LLMParam.creating { ComboBox(settings.customEngineResponseType, ResponseType.values().map { it.name }.toList()) }
private val customEngineResponseFormatParam by LLMParam.creating { Editable(settings.customEngineResponseFormat) }
private val customEngineRequestBodyFormatParam by LLMParam.creating { Editable(settings.customEngineRequestFormat) }
val project = ProjectManager.getInstance().openProjects.firstOrNull()
private val customEnginePrompt by lazy {
object : LanguageTextField(JsonLanguage.INSTANCE, project, settings.customPrompts) {
override fun createEditor(): EditorEx {
return super.createEditor().apply {
setShowPlaceholderWhenFocused(true)
setHorizontalScrollbarVisible(false)
setVerticalScrollbarVisible(true)
setPlaceholder(AutoDevBundle.message("autodev.custom.prompt.placeholder"))
val scheme = EditorColorsUtil.getColorSchemeForBackground(this.colorsScheme.defaultBackground)
this.colorsScheme = this.createBoundColorSchemeDelegate(scheme)
}
}
}.apply {
val metrics: FontMetrics = getFontMetrics(font)
val columnWidth = metrics.charWidth('m')
setOneLineMode(false)
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height)
}
}
private val llmGroups = mapOf<AIEngines, List<LLMParam>>(
AIEngines.Azure to listOf(
openAIModelsParam,
openAIKeyParam,
customOpenAIHostParam,
),
AIEngines.OpenAI to listOf(
openAIModelsParam,
openAIKeyParam,
customModelParam,
customOpenAIHostParam,
),
AIEngines.Custom to listOf(
customEngineResponseTypeParam,
customEngineServerParam,
customEngineTokenParam,
customEngineResponseFormatParam,
customEngineRequestBodyFormatParam,
),
AIEngines.XingHuo to listOf(
xingHuoApiVersionParam,
xingHuoAppIDParam,
xingHuoApiKeyParam,
xingHuoApiSecretParam,
),
)
private val onSelectedEngineChanged: () -> Unit = {
applySettings(settings, updateParams = false)
}
private val _currentSelectedEngine: AIEngines
get() = AIEngines.values().first { it.name.lowercase() == aiEngineParam.value.lowercase() }
private val currentLLMParams: List<LLMParam>
get() {
return llmGroups[_currentSelectedEngine]
?: throw IllegalStateException("Unknown engine: ${settings.aiEngine}")
}
private fun FormBuilder.addLLMParams(llmParams: List<LLMParam>): FormBuilder = apply {
llmParams.forEach { addLLMParam(it) }
}
private fun FormBuilder.addLLMParam(llmParam: LLMParam): FormBuilder = apply {
llmParam.addToFormBuilder(this)
}
private fun LLMParam.addToFormBuilder(formBuilder: FormBuilder) {
when (this.type) {
LLMParam.ParamType.Password -> {
formBuilder.addLabeledComponent(JBLabel(this.label), ReactivePasswordField(this) {
this.text = it.value
this.isEnabled = it.isEditable
}, 1, false)
}
LLMParam.ParamType.Text -> {
formBuilder.addLabeledComponent(JBLabel(this.label), ReactiveTextField(this) {
this.isEnabled = it.isEditable
}, 1, false)
}
LLMParam.ParamType.ComboBox -> {
formBuilder.addLabeledComponent(JBLabel(this.label), ReactiveComboBox(this), 1, false)
}
else -> {
formBuilder.addSeparator()
}
}
}
private val formBuilder: FormBuilder = FormBuilder.createFormBuilder()
val panel: JPanel get() = formBuilder.panel
fun applySettings(settings: AutoDevSettingsState, updateParams: Boolean = false) {
panel.removeAll()
if (updateParams) {
updateParams(settings)
}
formBuilder
.addLLMParam(languageParam)
.addSeparator()
.addTooltip("For Custom LLM, config Custom Engine Server & Custom Engine Token & Custom Response Format")
.addLLMParam(aiEngineParam)
.addLLMParam(maxTokenLengthParam)
.addLLMParam(delaySecondsParam)
.addSeparator()
.addTooltip("Select Git Type")
.addLLMParam(gitTypeParam)
.addTooltip("GitHub Token is for AutoCRUD Model")
.addLLMParam(gitHubTokenParam)
.addTooltip("GitLab options is for AutoCRUD Model")
.addLLMParam(gitLabUrlParam)
.addLLMParam(gitLabTokenParam)
.addSeparator()
.addLLMParams(currentLLMParams)
.addVerticalGap(2)
.addSeparator()
.addLabeledComponent(JBLabel("Custom Engine Prompt (Json): "), customEnginePrompt, 1, true)
.addComponentFillVertically(JPanel(), 0)
.panel
panel.invalidate()
panel.repaint()
}
private fun updateParams(settings: AutoDevSettingsState) {
settings.apply {
maxTokenLengthParam.value = maxTokenLength
gitTypeParam.value = gitType
gitHubTokenParam.value = githubToken
gitLabTokenParam.value = gitlabToken
gitLabUrlParam.value = gitlabUrl
openAIKeyParam.value = openAiKey
customModelParam.value = customModel
customOpenAIHostParam.value = customOpenAiHost
customEngineServerParam.value = customEngineServer
customEngineResponseTypeParam.value = customEngineResponseType
customEngineTokenParam.value = customEngineToken
openAIModelsParam.value = openAiModel
xingHuoApiVersionParam.value = xingHuoApiVersion.toString()
xingHuoAppIDParam.value = xingHuoAppId
xingHuoApiKeyParam.value = xingHuoApiKey
xingHuoApiSecretParam.value = xingHuoApiSecrect
languageParam.value = language
aiEngineParam.value = aiEngine
customEnginePrompt.text = customPrompts
customEngineResponseFormatParam.value = customEngineResponseFormat
customEngineRequestBodyFormatParam.value = customEngineRequestFormat
delaySecondsParam.value = delaySeconds
}
}
fun exportSettings(destination: AutoDevSettingsState) {
destination.apply {
maxTokenLength = maxTokenLengthParam.value
gitType = gitTypeParam.value
githubToken = gitHubTokenParam.value
gitlabUrl = gitLabUrlParam.value
gitlabToken = gitLabTokenParam.value
openAiKey = openAIKeyParam.value
customModel = customModelParam.value
customOpenAiHost = customOpenAIHostParam.value
xingHuoApiSecrect = xingHuoApiSecretParam.value
xingHuoApiVersion = XingHuoApiVersion.of(xingHuoApiVersionParam.value)
xingHuoAppId = xingHuoAppIDParam.value
xingHuoApiKey = xingHuoApiKeyParam.value
aiEngine = aiEngineParam.value
language = languageParam.value
customEngineServer = customEngineServerParam.value
customEngineResponseType = customEngineResponseTypeParam.value
customEngineToken = customEngineTokenParam.value
customPrompts = customEnginePrompt.text
openAiModel = openAIModelsParam.value
customEngineResponseFormat = customEngineResponseFormatParam.value
customEngineRequestFormat = customEngineRequestBodyFormatParam.value
delaySeconds = delaySecondsParam.value
}
}
fun isModified(settings: AutoDevSettingsState): Boolean {
return settings.maxTokenLength != maxTokenLengthParam.value ||
settings.gitType != gitTypeParam.value ||
settings.githubToken != gitHubTokenParam.value ||
settings.gitlabUrl != gitLabUrlParam.value ||
settings.gitlabToken != gitLabTokenParam.value ||
settings.openAiKey != openAIKeyParam.value ||
settings.customModel != customModelParam.value ||
settings.xingHuoApiSecrect != xingHuoApiSecretParam.value ||
settings.xingHuoApiVersion != XingHuoApiVersion.of(xingHuoApiVersionParam.value) ||
settings.xingHuoAppId != xingHuoAppIDParam.value ||
settings.xingHuoApiKey != xingHuoApiKeyParam.value ||
settings.aiEngine != aiEngineParam.value ||
settings.language != languageParam.value ||
settings.customEngineServer != customEngineServerParam.value ||
settings.customEngineResponseType != customEngineResponseTypeParam.value ||
settings.customEngineToken != customEngineTokenParam.value ||
settings.customPrompts != customEnginePrompt.text ||
settings.openAiModel != openAIModelsParam.value ||
settings.customOpenAiHost != customOpenAIHostParam.value ||
settings.customEngineResponseFormat != customEngineResponseFormatParam.value ||
settings.customEngineRequestFormat != customEngineRequestBodyFormatParam.value ||
settings.delaySeconds != delaySecondsParam.value
}
init {
applySettings(settings)
}
}