Skip to content

Commit e8ac525

Browse files
committed
feat: add for prompt by text
1 parent 4212af0 commit e8ac525

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/main/kotlin/cc/unitmesh/devti/connector/custom/CustomConnector.kt

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,36 @@ package cc.unitmesh.devti.connector.custom
22

33
import cc.unitmesh.devti.connector.CodeCopilot
44
import cc.unitmesh.devti.settings.DevtiSettingsState
5+
import kotlinx.serialization.decodeFromString
6+
import kotlinx.serialization.json.Json
7+
58

69
class CustomConnector : CodeCopilot {
7-
val url = DevtiSettingsState.getInstance()?.customEngineServer ?: ""
8-
val key = DevtiSettingsState.getInstance()?.customEngineToken ?: ""
10+
val devtiSettingsState = DevtiSettingsState.getInstance()
11+
12+
val url = devtiSettingsState?.customEngineServer ?: ""
13+
val key = devtiSettingsState?.customEngineToken ?: ""
14+
var promptConfig: PromptConfig? = null
15+
16+
init {
17+
val prompts = devtiSettingsState?.customEnginePrompts
18+
try {
19+
if (prompts != null) {
20+
promptConfig = Json.decodeFromString(prompts)
21+
}
22+
} catch (e: Exception) {
23+
println("Error parsing prompts: $e")
24+
}
25+
26+
if (promptConfig == null) {
27+
promptConfig = PromptConfig(
28+
PromptItem("Auto complete", "{code}"),
29+
PromptItem("Auto comment", "{code}"),
30+
PromptItem("Code review", "{code}"),
31+
PromptItem("Find bug", "{code}")
32+
)
33+
}
34+
}
935

1036
private fun prompt(instruction: String, input: String): String {
1137
// val retrofit = Retrofit.Builder()
@@ -17,19 +43,23 @@ class CustomConnector : CodeCopilot {
1743
}
1844

1945
override fun codeCompleteFor(text: String, className: String): String {
20-
return ""
46+
val complete = promptConfig!!.autoComplete
47+
return prompt(complete.instruction, complete.input.replace("{code}", text))
2148
}
2249

2350
override fun autoComment(text: String): String {
24-
return ""
51+
val comment = promptConfig!!.autoComment
52+
return prompt(comment.instruction, comment.input.replace("{code}", text))
2553
}
2654

2755
override fun codeReviewFor(text: String): String {
28-
return ""
56+
val review = promptConfig!!.codeReview
57+
return prompt(review.instruction, review.input.replace("{code}", text))
2958
}
3059

3160
override fun findBug(text: String): String {
32-
return ""
61+
val bug = promptConfig!!.findBug
62+
return prompt(bug.instruction, bug.input.replace("{code}", text))
3363
}
3464

3565
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DevtiSettingsConfigurable : Configurable {
3232
modified = modified or (!component!!.aiEngine.equals(settings.aiEngine))
3333
modified = modified or (!component!!.customEngineServer.equals(settings.customEngineServer))
3434
modified = modified or (!component!!.customEngineToken.equals(settings.customEngineToken))
35-
modified = modified or (!component!!.customEnginePrompt.equals(settings.customEnginePrompt))
35+
modified = modified or (!component!!.customEnginePrompt.equals(settings.customEnginePrompts))
3636
return modified
3737
}
3838

@@ -45,7 +45,7 @@ class DevtiSettingsConfigurable : Configurable {
4545
settings.aiEngine = component!!.getAiEngine()
4646
settings.customEngineServer = component!!.getCustomEngineServer()
4747
settings.customEngineToken = component!!.getCustomEngineToken()
48-
settings.customEnginePrompt = component!!.getCustomEnginePrompt()
48+
settings.customEnginePrompts = component!!.getCustomEnginePrompt()
4949
}
5050

5151
override fun reset() {
@@ -57,7 +57,7 @@ class DevtiSettingsConfigurable : Configurable {
5757
component!!.setAiEngine(settings.aiEngine)
5858
component!!.setCustomEngineServer(settings.customEngineServer)
5959
component!!.setCustomEngineToken(settings.customEngineToken)
60-
component!!.setCustomEnginePrompt(settings.customEnginePrompt)
60+
component!!.setCustomEnginePrompt(settings.customEnginePrompts)
6161
}
6262

6363
override fun disposeUIResources() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class DevtiSettingsState : PersistentStateComponent<DevtiSettingsState> {
1717
var customOpenAiHost = ""
1818
var customEngineServer = ""
1919
var customEngineToken = ""
20-
var customEnginePrompt = ""
20+
var customEnginePrompts = ""
2121

2222
override fun getState(): DevtiSettingsState {
2323
return this
2424
}
2525

2626
override fun loadState(state: DevtiSettingsState) {
27-
if (customEnginePrompt == "") {
28-
customEnginePrompt = DEFAULT_PROMPTS.trimIndent()
27+
if (customEnginePrompts == "") {
28+
customEnginePrompts = DEFAULT_PROMPTS.trimIndent()
2929
}
3030

3131
XmlSerializerUtil.copyBean(state, this)

0 commit comments

Comments
 (0)