Skip to content

Commit 35cfb7b

Browse files
committed
feat(devins-language): add support for custom agent execution #101
Add support for executing custom agents for language compilation, improving interaction with the console.
1 parent 59d5b23 commit 35cfb7b

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cc.unitmesh.devti.language.run
22

33
// DONT REMOVE THIS IMPORT
4+
import cc.unitmesh.devti.agent.CustomAgentExecutor
5+
import cc.unitmesh.devti.agent.model.CustomAgentConfig
46
import cc.unitmesh.devti.language.compiler.DevInsCompiler
57
import cc.unitmesh.devti.language.psi.DevInFile
68
import cc.unitmesh.devti.llms.LLMProvider
@@ -25,8 +27,8 @@ import com.intellij.openapi.project.Project
2527
import com.intellij.openapi.vfs.VirtualFileManager
2628
import com.intellij.psi.PsiManager
2729
import com.intellij.ui.components.panels.NonOpaquePanel
30+
import kotlinx.coroutines.flow.Flow
2831
import kotlinx.coroutines.launch
29-
import kotlinx.coroutines.flow.collect
3032
import kotlinx.coroutines.runBlocking
3133
import java.awt.BorderLayout
3234
import java.io.OutputStream
@@ -72,6 +74,57 @@ open class DevInsRunConfigurationProfileState(
7274

7375
val output = compileResult.output
7476

77+
val agent = compileResult.workingAgent
78+
79+
if (agent != null) {
80+
agentRun(output, console, processHandler, agent)
81+
} else {
82+
defaultRun(output, console, processHandler, compileResult.isLocalCommand)
83+
}
84+
85+
return DefaultExecutionResult(console, processHandler)
86+
}
87+
88+
private fun agentRun(
89+
output: String,
90+
console: ConsoleViewWrapperBase,
91+
processHandler: ProcessHandler,
92+
agent: CustomAgentConfig
93+
) {
94+
output.split("\n").forEach {
95+
if (it.contains("<DevInsError>")) {
96+
console.print(it, ConsoleViewContentType.LOG_ERROR_OUTPUT)
97+
} else {
98+
console.print(it, ConsoleViewContentType.USER_INPUT)
99+
}
100+
console.print("\n", ConsoleViewContentType.NORMAL_OUTPUT)
101+
}
102+
103+
console.print("\n--------------------\n", ConsoleViewContentType.NORMAL_OUTPUT)
104+
105+
ApplicationManager.getApplication().invokeLater {
106+
val stringFlow: Flow<String>? = CustomAgentExecutor(project = myProject).execute(output, agent)
107+
if (stringFlow != null) {
108+
LLMCoroutineScope.scope(myProject).launch {
109+
runBlocking {
110+
stringFlow.collect {
111+
console.print(it, ConsoleViewContentType.NORMAL_OUTPUT)
112+
}
113+
}
114+
115+
console.print("\nDone!", ConsoleViewContentType.SYSTEM_OUTPUT)
116+
processHandler.detachProcess()
117+
}
118+
}
119+
}
120+
}
121+
122+
private fun defaultRun(
123+
output: String,
124+
console: ConsoleViewWrapperBase,
125+
processHandler: ProcessHandler,
126+
isLocalMode: Boolean
127+
) {
75128
// contains <DevInsError> means error
76129
output.split("\n").forEach {
77130
if (it.contains("<DevInsError>")) {
@@ -85,7 +138,7 @@ open class DevInsRunConfigurationProfileState(
85138
console.print("\n--------------------\n", ConsoleViewContentType.NORMAL_OUTPUT)
86139

87140
ApplicationManager.getApplication().invokeLater {
88-
if (compileResult.isLocalCommand) {
141+
if (isLocalMode) {
89142
console.print("Local command detected, running in local mode", ConsoleViewContentType.SYSTEM_OUTPUT)
90143
processHandler.detachProcess()
91144
return@invokeLater
@@ -102,8 +155,6 @@ open class DevInsRunConfigurationProfileState(
102155
processHandler.detachProcess()
103156
}
104157
}
105-
106-
return DefaultExecutionResult(console, processHandler)
107158
}
108159

109160
@Throws(ExecutionException::class)

0 commit comments

Comments
 (0)