Skip to content

Commit 13b796f

Browse files
committed
feat(devins-lang): add support for processing flag comments #100
Add support for processing flag comments in DevInsFlowProcessor. This allows the script to handle different scenarios based on the flag comments present in the script output. Also, added a method to run a new script and handle the compiled result in the chat window.
1 parent dd3ad5c commit 13b796f

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/run/flow/DevInsFlowProcessor.kt

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package cc.unitmesh.devti.language.run.flow
22

3+
import cc.unitmesh.devti.gui.chat.ChatActionType
4+
import cc.unitmesh.devti.gui.sendToChatWindow
5+
import cc.unitmesh.devti.language.compiler.DevInsCompiler
36
import cc.unitmesh.devti.language.psi.DevInFile
47
import cc.unitmesh.devti.language.psi.DevInVisitor
8+
import cc.unitmesh.devti.provider.ContextPrompter
59
import com.intellij.execution.process.ProcessEvent
610
import com.intellij.openapi.application.runReadAction
711
import com.intellij.openapi.components.Service
812
import com.intellij.openapi.components.service
913
import com.intellij.openapi.project.Project
1014
import com.intellij.psi.PsiComment
1115
import com.intellij.psi.PsiElement
16+
import org.jetbrains.kotlin.psi.psiUtil.startOffset
1217

1318
@Service(Service.Level.PROJECT)
1419
class DevInsFlowProcessor(val project: Project) {
1520
/**
16-
* The flag comment is the comment that starts with `[devins]`
21+
* This function takes a DevInFile as input and returns a list of PsiElements that are comments.
22+
* It iterates through the DevInFile and adds any comments it finds to the list.
23+
*
24+
* @param devInFile the DevInFile to search for comments
25+
* @return a list of PsiElements that are comments
1726
*/
18-
fun lookupFlagComment(devInFile: DevInFile): List<PsiElement> {
27+
private fun lookupFlagComment(devInFile: DevInFile): List<PsiElement> {
1928
val comments = mutableListOf<PsiElement>()
2029
devInFile.accept(object : DevInVisitor() {
2130
override fun visitComment(comment: PsiComment) {
@@ -27,19 +36,49 @@ class DevInsFlowProcessor(val project: Project) {
2736
}
2837

2938
/**
30-
* continue get last compile result
39+
* Process the output of a script based on the exit code and flag comment.
40+
* If the exit code is not 0, attempts to fix the script with LLM.
41+
* If the exit code is 0 and there is a flag comment, process it.
42+
*
43+
* Flag comment format:
44+
* - [flow]:flowable.devin, means next step is flowable.devin
45+
* - [flow](result), means a handle with result
46+
*
47+
* @param output The output of the script
48+
* @param event The process event containing the exit code
49+
* @param scriptPath The path of the script file
3150
*/
3251
fun process(output: String, event: ProcessEvent, scriptPath: String) {
3352
val devInFile: DevInFile? = runReadAction { DevInFile.lookup(project, scriptPath) }
3453
project.service<DevInsConversationService>().updateIdeOutput(scriptPath, output)
35-
if (event.exitCode == 0) {
36-
val lookUpFlagComment = lookupFlagComment(devInFile!!)
37-
if (lookUpFlagComment.isNotEmpty()) {
38-
// TODO
54+
55+
when {
56+
event.exitCode == 0 -> {
57+
val comment = lookupFlagComment(devInFile!!).firstOrNull() ?: return
58+
if (comment.startOffset == 0) {
59+
val text = comment.text
60+
if (text.startsWith("[flow]")) {
61+
val nextScript = text.substring(6)
62+
val newScript = DevInFile.lookup(project, nextScript) ?: return
63+
this.runTask(newScript)
64+
}
65+
}
66+
}
67+
event.exitCode != 0 -> {
68+
project.service<DevInsConversationService>().tryFixWithLlm(scriptPath)
3969
}
4070
}
41-
if (event.exitCode != 0) {
42-
project.service<DevInsConversationService>().tryFixWithLlm(scriptPath)
71+
}
72+
73+
private fun runTask(newScript: DevInFile) {
74+
val compiledResult = DevInsCompiler(project, newScript).compile()
75+
val prompt = compiledResult.output
76+
77+
sendToChatWindow(project, ChatActionType.CHAT) { panel, service ->
78+
service.handlePromptAndResponse(panel, object : ContextPrompter() {
79+
override fun displayPrompt(): String = prompt
80+
override fun requestPrompt(): String = prompt
81+
}, null, true)
4382
}
4483
}
4584

0 commit comments

Comments
 (0)