Skip to content

Commit

Permalink
feat(devins-lang): add support for processing flag comments #100
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Mar 23, 2024
1 parent dd3ad5c commit 13b796f
Showing 1 changed file with 48 additions and 9 deletions.
@@ -1,21 +1,30 @@
package cc.unitmesh.devti.language.run.flow

import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.language.compiler.DevInsCompiler
import cc.unitmesh.devti.language.psi.DevInFile
import cc.unitmesh.devti.language.psi.DevInVisitor
import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.execution.process.ProcessEvent
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.psiUtil.startOffset

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

/**
* continue get last compile result
* Process the output of a script based on the exit code and flag comment.
* If the exit code is not 0, attempts to fix the script with LLM.
* If the exit code is 0 and there is a flag comment, process it.
*
* Flag comment format:
* - [flow]:flowable.devin, means next step is flowable.devin
* - [flow](result), means a handle with result
*
* @param output The output of the script
* @param event The process event containing the exit code
* @param scriptPath The path of the script file
*/
fun process(output: String, event: ProcessEvent, scriptPath: String) {
val devInFile: DevInFile? = runReadAction { DevInFile.lookup(project, scriptPath) }
project.service<DevInsConversationService>().updateIdeOutput(scriptPath, output)
if (event.exitCode == 0) {
val lookUpFlagComment = lookupFlagComment(devInFile!!)
if (lookUpFlagComment.isNotEmpty()) {
// TODO

when {
event.exitCode == 0 -> {
val comment = lookupFlagComment(devInFile!!).firstOrNull() ?: return
if (comment.startOffset == 0) {
val text = comment.text
if (text.startsWith("[flow]")) {
val nextScript = text.substring(6)
val newScript = DevInFile.lookup(project, nextScript) ?: return
this.runTask(newScript)
}
}
}
event.exitCode != 0 -> {
project.service<DevInsConversationService>().tryFixWithLlm(scriptPath)
}
}
if (event.exitCode != 0) {
project.service<DevInsConversationService>().tryFixWithLlm(scriptPath)
}

private fun runTask(newScript: DevInFile) {
val compiledResult = DevInsCompiler(project, newScript).compile()
val prompt = compiledResult.output

sendToChatWindow(project, ChatActionType.CHAT) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = prompt
override fun requestPrompt(): String = prompt
}, null, true)
}
}

Expand Down

0 comments on commit 13b796f

Please sign in to comment.