Skip to content

Commit

Permalink
feat(devins-lang): add support for flow control in DevInsPromptProces…
Browse files Browse the repository at this point in the history
…sor.kt and DevInsCompiler.kt #100
  • Loading branch information
phodal committed Apr 16, 2024
1 parent 84c0700 commit 2908e41
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
@@ -1,6 +1,7 @@
package cc.unitmesh.devti.language.compiler

import cc.unitmesh.devti.agent.model.CustomAgentConfig
import cc.unitmesh.devti.language.psi.DevInFile

data class DevInsCompiledResult(
/**
Expand All @@ -13,6 +14,10 @@ data class DevInsCompiledResult(
var output: String = "",
var isLocalCommand: Boolean = false,
var hasError: Boolean = false,
var executeAgent: CustomAgentConfig? = null
var executeAgent: CustomAgentConfig? = null,
/**
* Next job to be executed
*/
var nextJob: DevInFile? = null
) {
}
Expand Up @@ -14,6 +14,7 @@ import cc.unitmesh.devti.language.psi.DevInUsed
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.psi.PsiElement
import com.intellij.psi.util.elementType
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -48,10 +49,23 @@ class DevInsCompiler(

output.append(it.text)
}

DevInTypes.USED -> processUsed(it as DevInUsed)
DevInTypes.COMMENTS -> {
// ignore comment
if (it.text.startsWith("[flow]:")) {
val fileName = it.text.substringAfter("[flow]:").trim()
val content =
myProject.guessProjectDir()?.findFileByRelativePath(fileName)?.let { virtualFile ->
virtualFile.inputStream.bufferedReader().use { reader -> reader.readText() }
}

if (content != null) {
val devInFile = DevInFile.fromString(myProject, content)
result.nextJob = devInFile
}
}
}

else -> {
output.append(it.text)
logger.warn("Unknown element type: ${it.elementType}")
Expand Down
Expand Up @@ -29,6 +29,14 @@ class DevInsPromptProcessor : LanguagePromptProcessor {
val devInsCompiler = createCompiler(project, text)
val result = devInsCompiler.compile()
AutoDevNotifications.notify(project, result.output)

if (result.nextJob != null) {
val nextJob = result.nextJob!!
val nextResult = createCompiler(project, nextJob).compile()
AutoDevNotifications.notify(project, nextResult.output)
return nextResult.output
}

return result.output
}

Expand All @@ -50,6 +58,13 @@ class DevInsPromptProcessor : LanguagePromptProcessor {
text: String
): DevInsCompiler {
val devInFile = DevInFile.fromString(project, text)
return createCompiler(project, devInFile)
}

private fun createCompiler(
project: Project,
devInFile: DevInFile
): DevInsCompiler {
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val element: PsiElement? = editor?.caretModel?.currentCaret?.offset?.let {
val psiFile = PsiUtilBase.getPsiFileInEditor(editor, project) ?: return@let null
Expand Down

0 comments on commit 2908e41

Please sign in to comment.