Skip to content

Commit

Permalink
fix(devins-lang): add basic handle for exitCode=-1 to recall function
Browse files Browse the repository at this point in the history
- Improve file writing performance by optimizing the process termination listener and adding a new input field for DevInsCompiledResult. This will enhance the overall efficiency of the DevIns language compiler.
  • Loading branch information
phodal committed Mar 23, 2024
1 parent ff38ea9 commit 6bcdf15
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
Expand Up @@ -3,8 +3,10 @@ package cc.unitmesh.devti.language.compiler
import cc.unitmesh.devti.agent.model.CustomAgentConfig

data class DevInsCompiledResult(
var input: String = "",
var output: String = "",
var isLocalCommand: Boolean = false,
var hasError: Boolean = false,
var workingAgent: CustomAgentConfig? = null
)
) {
}
Expand Up @@ -34,6 +34,7 @@ class DevInsCompiler(
* Todo: build AST tree, then compile
*/
fun compile(): DevInsCompiledResult {
result.input = file.text
file.children.forEach {
when (it.elementType) {
DevInTypes.TEXT_SEGMENT -> output.append(it.text)
Expand Down
Expand Up @@ -29,7 +29,7 @@ import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.ui.components.panels.NonOpaquePanel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.awt.BorderLayout
Expand All @@ -49,12 +49,18 @@ open class DevInsRunConfigurationProfileState(
val sb = StringBuilder()

processHandler.addProcessListener(object : ProcessAdapter() {
var result = ""
override fun processTerminated(event: ProcessEvent) {
super.processTerminated(event)

ApplicationManager.getApplication().messageBus
.syncPublisher(DevInsRunListener.TOPIC)
.runFinish(sb.toString(), event, configuration.getScriptPath())
.runFinish(result, event, configuration.getScriptPath())
}

override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
super.onTextAvailable(event, outputType)
result = sb.toString()
}
})

Expand Down Expand Up @@ -108,6 +114,7 @@ open class DevInsRunConfigurationProfileState(
}

console.print("\n--------------------\n", ConsoleViewContentType.NORMAL_OUTPUT)

// throw error if contains any <DevInsError>
if (output.contains("<DevInsError>")) {
processHandler.exitWithError()
Expand Down Expand Up @@ -166,7 +173,7 @@ open class DevInsRunConfigurationProfileState(
LLMCoroutineScope.scope(myProject).launch {
val llmResult = StringBuilder()
runBlocking {
llm.stream(output, "").collect {
llm.stream(output, "", false).collect {
llmResult.append(it)
console.print(it, ConsoleViewContentType.NORMAL_OUTPUT)
}
Expand Down
@@ -1,11 +1,18 @@
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.DevInsCompiledResult
import cc.unitmesh.devti.llms.LLMProvider
import cc.unitmesh.devti.llms.LlmFactory
import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project

@Service(Service.Level.PROJECT)
class DevInsConversationService(val project: Project) {
private val llm: LLMProvider = LlmFactory.instance.create(project)

/**
* The cached conversations
*/
Expand Down Expand Up @@ -63,13 +70,47 @@ class DevInsConversationService(val project: Project) {

conversation.hadReRun = true
// call llm again to re-run

val prompt = StringBuilder()

// todo: refactor to DevIn template file
if (conversation.compiledResult.isLocalCommand) {
prompt.append("You are a top software developer in the world, which can help me to fix the issue.\n")
prompt.append("When I use DevIn language and compile the script, I got an error, can you help me to fix it?\n")
prompt.append("Origin DevIn script:\n")
prompt.append("```devin\n")
prompt.append(conversation.compiledResult.input)
prompt.append("```\n")

prompt.append("The Compile Result:\n")
prompt.append("####\n")
prompt.append(conversation.compiledResult.output)
prompt.append("####\n")
}

prompt.append("""
Here is the run result, can you help me to fix it?
Run result:
####
${conversation.ideOutput}
####
""".trimIndent()
)

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


data class DevInsConversation(
val scriptPath: String,
val result: DevInsCompiledResult,
val compiledResult: DevInsCompiledResult,
val llmResponse: String,
val ideOutput: String,
val messages: MutableList<cc.unitmesh.devti.llms.custom.Message> = mutableListOf(),
Expand Down

0 comments on commit 6bcdf15

Please sign in to comment.