Skip to content

Commit 946f5a0

Browse files
committed
feat(settings): add new LLMParam components
This commit adds new LLMParam components to the LLMSettingsComponent. The new components include a text field, a password field, and a combo box. These components allow users to input and select different settings for the LLM feature. The components are created using the Reactive delegate, which allows for easy updating of the component values.
1 parent dac557b commit 946f5a0

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

src/main/kotlin/cc/unitmesh/devti/llms/LLMProviderFactory.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import cc.unitmesh.devti.llms.azure.AzureOpenAIProvider
44
import cc.unitmesh.devti.llms.custom.CustomLLMProvider
55
import cc.unitmesh.devti.llms.openai.OpenAIProvider
66
import cc.unitmesh.devti.llms.xianghuo.XingHuoProvider
7+
import cc.unitmesh.devti.settings.AIEngines
78
import cc.unitmesh.devti.settings.AutoDevSettingsState
89
import com.intellij.openapi.components.Service
910
import com.intellij.openapi.project.Project
1011

1112
@Service
1213
class LLMProviderFactory {
13-
private val aiEngine: String
14-
get() = AutoDevSettingsState.getInstance().aiEngine
14+
private val aiEngine: AIEngines
15+
get() = AIEngines.values().find { it.name.lowercase() == AutoDevSettingsState.getInstance().aiEngine.lowercase() } ?: AIEngines.OpenAI
1516
fun connector(project: Project): LLMProvider {
1617
return when (aiEngine) {
17-
// TODO use mapping and avoid hard code engine name
18-
"OpenAI" -> project.getService(OpenAIProvider::class.java)
19-
"Custom" -> project.getService(CustomLLMProvider::class.java)
20-
"Azure" -> project.getService(AzureOpenAIProvider::class.java)
21-
"XingHuo" -> project.getService(XingHuoProvider::class.java)
18+
AIEngines.OpenAI -> project.getService(OpenAIProvider::class.java)
19+
AIEngines.Custom -> project.getService(CustomLLMProvider::class.java)
20+
AIEngines.Azure -> project.getService(AzureOpenAIProvider::class.java)
21+
AIEngines.XingHuo -> project.getService(XingHuoProvider::class.java)
2222
else -> project.getService(OpenAIProvider::class.java)
2323
}
2424
}

src/main/kotlin/cc/unitmesh/devti/llms/xianghuo/XingHuoProvider.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ class XingHuoProvider(val project: Project) : LLMProvider {
3737
private val hmacsha256Algorithms = "hmacsha256"
3838
private val uid = UUID.randomUUID().toString().substring(0, 32)
3939

40-
private val hmacsha256 by lazy {
41-
val hmac = Mac.getInstance(hmacsha256Algorithms)
42-
val keySpec = SecretKeySpec(secrectKey.toByteArray(), hmacsha256Algorithms)
43-
hmac.init(keySpec)
44-
hmac
45-
}
40+
private val hmacsha256: Mac
41+
get() {
42+
val hmac = Mac.getInstance(hmacsha256Algorithms)
43+
val keySpec = SecretKeySpec(secrectKey.toByteArray(), hmacsha256Algorithms)
44+
hmac.init(keySpec)
45+
return hmac
46+
}
4647

4748
override fun prompt(promptText: String): String {
4849
// prompt 接口看似是无用的废弃接口,因为所有 LLM 请求都只能异步返回,不可能直接返回同步结果
@@ -54,12 +55,9 @@ class XingHuoProvider(val project: Project) : LLMProvider {
5455
override fun stream(promptText: String, systemPrompt: String): Flow<String> {
5556
return callbackFlow {
5657
val client = OkHttpClient()
57-
client.newWebSocket(request, MyListener(this, onSocketOpend = {
58+
client.newWebSocket(request, MyListener(this, onSocketOpen = {
5859
val msg = getSendBody(promptText)
59-
println("sending $msg")
6060
send(msg)
61-
}, onSocketClosed = {
62-
close()
6361
}))
6462
awaitClose()
6563
}
@@ -68,19 +66,18 @@ class XingHuoProvider(val project: Project) : LLMProvider {
6866

6967
class MyListener(
7068
private val producerScope: ProducerScope<String>,
71-
private val onSocketOpend: WebSocket.() -> Unit,
72-
private val onSocketClosed: WebSocket.() -> Unit
69+
private val onSocketOpen: WebSocket.() -> Unit,
7370
) : WebSocketListener() {
7471

7572
private var sockedOpen = false
7673
override fun onOpen(webSocket: WebSocket, response: Response) {
77-
webSocket.onSocketOpend()
74+
webSocket.onSocketOpen()
7875
sockedOpen = true
7976
}
8077

81-
override fun onMessage(webSocket: WebSocket, body: String) {
82-
return runCatching {
83-
val element = Json.parseToJsonElement(body)
78+
override fun onMessage(webSocket: WebSocket, text: String) {
79+
runCatching {
80+
val element = Json.parseToJsonElement(text)
8481
val choices = element.jsonObject["payload"]!!.jsonObject["choices"]!!
8582
val statusCode: Int = choices.jsonObject["status"]?.jsonPrimitive?.int!!
8683
val message = choices.jsonObject["text"]!!.jsonArray[0]
@@ -96,11 +93,12 @@ class XingHuoProvider(val project: Project) : LLMProvider {
9693
}
9794

9895
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
99-
webSocket.onSocketClosed()
96+
producerScope.close()
10097
}
10198

10299
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
103100
// WebSocket connection failed
101+
println("failure ${t.message} ${response?.body} ${response?.message} ${response?.code}")
104102
producerScope.trySend("onFailure ${response?.body} ${response?.message} ${response?.code}")
105103
producerScope.close()
106104
}
@@ -131,6 +129,7 @@ class XingHuoProvider(val project: Project) : LLMProvider {
131129
urlBuilder.addQueryParameter(it.key, it.value)
132130
}
133131
val url = urlBuilder.build().toString().replace("https://", "wss://")
132+
println(url)
134133
return Request.Builder().url(url).build()
135134
}
136135

src/main/kotlin/cc/unitmesh/devti/settings/LLMParamComponent.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ fun ReactivePasswordField(param: LLMParam, initBlock: JBPasswordField.(LLMParam)
5656

5757
component.initBlock(reactive.value)
5858
component.document.addUndoableEditListener {
59-
if (component.text == param.value) return@addUndoableEditListener
60-
reactive.value.value = component.text
59+
if (component.password.joinToString("") == param.value) return@addUndoableEditListener
60+
reactive.value.value = component.password.joinToString("")
6161
}
6262

6363
return component
@@ -74,7 +74,6 @@ fun ReactiveComboBox(param: LLMParam, initBlock: ComboBox<String>.(LLMParam) ->
7474
component.initBlock(reactive)
7575
component.addItemListener {
7676
if (it.stateChange == ItemEvent.SELECTED) {
77-
println("item changed to ${component.selectedItem}")
7877
reactive.value = component.selectedItem as String
7978
}
8079
}
@@ -111,7 +110,6 @@ class LLMParam(
111110
val isEditable: Boolean = true,
112111
val type: ParamType = ParamType.Text,
113112
var items: List<String> = emptyList(),
114-
var visible: Boolean = true,
115113
) {
116114
enum class ParamType {
117115
Text, Password, ComboBox, Separator
@@ -126,7 +124,6 @@ class LLMParam(
126124
val changed = field != newValue
127125
field = newValue
128126
if (changed) {
129-
println("value changed $newValue $value")
130127
onChange?.invoke(this, newValue)
131128
}
132129
}
@@ -165,8 +162,6 @@ class LLMParam(
165162

166163
fun ComboBox(value: String, items: List<String>) =
167164
LLMParam(value = value, type = ParamType.ComboBox, items = items.toList())
168-
169-
fun Separator() = LLMParam(type = ParamType.Separator)
170165
}
171166
}
172167

src/main/kotlin/cc/unitmesh/devti/settings/LLMSettingComponent.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
8888

8989
private val currentLLMParams: List<LLMParam>
9090
get() {
91-
println("current engine: ${_currentSelectedEngine}")
9291
return llmGroups[_currentSelectedEngine] ?: throw IllegalStateException("Unknown engine: ${settings.aiEngine}")
9392
}
9493

@@ -131,7 +130,6 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
131130

132131

133132
fun applySettings(settings: AutoDevSettingsState, updateParams: Boolean = false) {
134-
println("applySettings ${panel.components.size}")
135133
panel.removeAll()
136134
if (updateParams) {
137135
updateParams(settings)

0 commit comments

Comments
 (0)