Skip to content

Commit 93a5e52

Browse files
committed
fix(chat): handle custom agent state and add support for custom variable compilation #51
1 parent 71cf4f2 commit 93a5e52

File tree

6 files changed

+58
-9
lines changed

6 files changed

+58
-9
lines changed

src/main/kotlin/cc/unitmesh/devti/counit/view/WebViewWindow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.awt.Component
1414
* for custom webview can refs: https://github.com/mucharafal/jcef_example
1515
*/
1616
class WebViewWindow {
17-
// offical doc: https://plugins.jetbrains.com/docs/intellij/jcef.html#executing-javascript
17+
// official doc: https://plugins.jetbrains.com/docs/intellij/jcef.html#executing-javascript
1818
private val browser: JBCefBrowser
1919

2020
init {

src/main/kotlin/cc/unitmesh/devti/custom/compile/CustomVariable.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ enum class CustomVariable(val variable: String, val description: String) {
1717
fun all(): List<CustomVariable> = values().toList()
1818

1919
fun hasVariable(content: String): Boolean {
20-
return all().any { content.contains("\${${it.variable}}") }
20+
return all().any { content.contains("\$${it.variable}") }
2121
}
2222

2323
fun compile(content: String, compiler: VariableTemplateCompiler): String {

src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputSection.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import com.intellij.util.ui.JBEmptyBorder
4040
import com.intellij.util.ui.JBUI
4141
import com.intellij.util.ui.UIUtil
4242
import com.intellij.util.ui.components.BorderLayoutPanel
43+
import kotlinx.serialization.decodeFromString
4344
import kotlinx.serialization.json.Json
4445
import java.awt.Color
4546
import java.awt.Component
@@ -199,6 +200,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
199200
text += "${selectedItem.customVariable.variable} "
200201
}
201202
this@AutoDevInputSection.popup?.cancel()
203+
this@AutoDevInputSection.requestFocus()
202204
}
203205

204206
KeyEvent.VK_DOWN -> {
@@ -219,6 +221,11 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
219221
list.setSelectedIndex(list.getItemsCount() - 1)
220222
}
221223
}
224+
225+
// Esc
226+
KeyEvent.VK_ESCAPE -> {
227+
this@AutoDevInputSection.requestFocus()
228+
}
222229
}
223230
}
224231
})

src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingService.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import cc.unitmesh.devti.util.LLMCoroutineScope
66
import cc.unitmesh.devti.counit.CustomAgentChatProcessor
77
import cc.unitmesh.devti.counit.configurable.customAgentSetting
88
import cc.unitmesh.devti.counit.model.CustomAgentState
9+
import cc.unitmesh.devti.custom.compile.CustomVariable
10+
import cc.unitmesh.devti.custom.compile.VariableTemplateCompiler
911
import cc.unitmesh.devti.llms.LlmFactory
1012
import cc.unitmesh.devti.util.parser.PostCodeProcessor
1113
import cc.unitmesh.devti.provider.ContextPrompter
@@ -29,16 +31,30 @@ class ChatCodingService(var actionType: ChatActionType, val project: Project) {
2931
context: ChatContext? = null,
3032
newChatContext: Boolean,
3133
) {
32-
val requestPrompt = prompter.requestPrompt()
33-
val displayPrompt = prompter.displayPrompt()
34+
var requestPrompt = prompter.requestPrompt()
35+
var displayPrompt = prompter.displayPrompt()
3436

3537
if (project.customAgentSetting.enableCustomRag && ui.hasSelectedCustomAgent()) {
36-
if (ui.getSelectedCustomAgent().state === CustomAgentState.START) {
37-
counitProcessor.handleChat(prompter, ui, context, llmProvider)
38-
return
38+
val selectedCustomAgent = ui.getSelectedCustomAgent()
39+
when {
40+
selectedCustomAgent.state === CustomAgentState.START -> {
41+
counitProcessor.handleChat(prompter, ui, context, llmProvider)
42+
return
43+
}
44+
45+
selectedCustomAgent.state === CustomAgentState.FINISHED -> {
46+
if (CustomVariable.hasVariable(requestPrompt)) {
47+
val compiler = prompter.toTemplateCompiler()
48+
compiler?.also {
49+
requestPrompt = CustomVariable.compile(requestPrompt, it)
50+
displayPrompt = CustomVariable.compile(displayPrompt, it)
51+
}
52+
}
53+
}
3954
}
4055
}
4156

57+
4258
ui.addMessage(requestPrompt, true, displayPrompt)
4359
ui.addMessage(AutoDevBundle.message("autodev.loading"))
4460

src/main/kotlin/cc/unitmesh/devti/provider/ContextPrompter.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package cc.unitmesh.devti.provider
22

3+
import cc.unitmesh.devti.custom.compile.VariableTemplateCompiler
34
import cc.unitmesh.devti.gui.chat.ChatActionType
45
import cc.unitmesh.devti.provider.builtin.DefaultContextPrompter
56
import cc.unitmesh.devti.provider.context.ChatContextProvider
67
import cc.unitmesh.devti.provider.context.ChatCreationContext
78
import cc.unitmesh.devti.settings.coder.coderSetting
9+
import com.intellij.lang.html.HTMLLanguage
810
import com.intellij.openapi.diagnostic.logger
911
import com.intellij.openapi.extensions.ExtensionPointName
12+
import com.intellij.openapi.fileEditor.FileEditorManager
1013
import com.intellij.openapi.project.Project
14+
import com.intellij.openapi.project.ProjectManager
15+
import com.intellij.openapi.vfs.VirtualFile
16+
import com.intellij.psi.PsiDocumentManager
1117
import com.intellij.psi.PsiElement
1218
import com.intellij.psi.PsiFile
1319
import com.intellij.serviceContainer.LazyExtensionInstance
@@ -80,6 +86,26 @@ abstract class ContextPrompter : LazyExtensionInstance<ContextPrompter>() {
8086
open fun displayPrompt(): String = ""
8187
open fun requestPrompt(): String = ""
8288

89+
fun toTemplateCompiler(): VariableTemplateCompiler? {
90+
val project = project ?: ProjectManager.getInstance().openProjects.firstOrNull() ?: return null
91+
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return null
92+
93+
val file: PsiFile = file ?: PsiDocumentManager.getInstance(project).getPsiFile(editor.document) ?: return null
94+
95+
val selectedText = selectedText.ifEmpty {
96+
editor.selectionModel.selectedText ?: ""
97+
}
98+
99+
return VariableTemplateCompiler(
100+
language = file.language,
101+
file = file,
102+
element = element,
103+
editor = editor,
104+
selectedText = selectedText
105+
)
106+
107+
}
108+
83109
companion object {
84110
private val EP_NAME: ExtensionPointName<ContextPrompter> =
85111
ExtensionPointName.create("cc.unitmesh.contextPrompter")

src/test/kotlin/cc/unitmesh/devti/custom/variable/CustomVariableTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.junit.Test
88
class CustomVariableTest {
99
@Test
1010
fun should_parse_variable_from_content() {
11-
CustomVariable.hasVariable("解释一下代码:\${selection}") shouldBe true
12-
CustomVariable.hasVariable("解释一下代码:\${selection") shouldBe false
11+
CustomVariable.hasVariable("解释一下代码:\$selection") shouldBe true
12+
CustomVariable.hasVariable("解释一下代码:\$selectio") shouldBe false
1313
}
1414
}

0 commit comments

Comments
 (0)