1
1
package cc.unitmesh.devti.language.run.flow
2
2
3
+ import cc.unitmesh.devti.gui.chat.ChatActionType
4
+ import cc.unitmesh.devti.gui.sendToChatWindow
5
+ import cc.unitmesh.devti.language.compiler.DevInsCompiler
3
6
import cc.unitmesh.devti.language.psi.DevInFile
4
7
import cc.unitmesh.devti.language.psi.DevInVisitor
8
+ import cc.unitmesh.devti.provider.ContextPrompter
5
9
import com.intellij.execution.process.ProcessEvent
6
10
import com.intellij.openapi.application.runReadAction
7
11
import com.intellij.openapi.components.Service
8
12
import com.intellij.openapi.components.service
9
13
import com.intellij.openapi.project.Project
10
14
import com.intellij.psi.PsiComment
11
15
import com.intellij.psi.PsiElement
16
+ import org.jetbrains.kotlin.psi.psiUtil.startOffset
12
17
13
18
@Service(Service .Level .PROJECT )
14
19
class DevInsFlowProcessor (val project : Project ) {
15
20
/* *
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
17
26
*/
18
- fun lookupFlagComment (devInFile : DevInFile ): List <PsiElement > {
27
+ private fun lookupFlagComment (devInFile : DevInFile ): List <PsiElement > {
19
28
val comments = mutableListOf<PsiElement >()
20
29
devInFile.accept(object : DevInVisitor () {
21
30
override fun visitComment (comment : PsiComment ) {
@@ -27,19 +36,49 @@ class DevInsFlowProcessor(val project: Project) {
27
36
}
28
37
29
38
/* *
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
31
50
*/
32
51
fun process (output : String , event : ProcessEvent , scriptPath : String ) {
33
52
val devInFile: DevInFile ? = runReadAction { DevInFile .lookup(project, scriptPath) }
34
53
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)
39
69
}
40
70
}
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 )
43
82
}
44
83
}
45
84
0 commit comments