Skip to content

Commit 261780f

Browse files
committed
feat(devins-lang): improve conversation service and compiler #100
- Refactored createConversation method to accept result object instead of input string and script path, and updated related methods to reflect the change. Added tryReRun method to handle re-running conversations. This commit improves the conversation service and compiler by refactoring the createConversation method to accept a result object instead of an input string and script path. It also adds a tryReRun method to handle re-running conversations.
1 parent c33d859 commit 261780f

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import cc.unitmesh.devti.agent.CustomAgentExecutor
44
import cc.unitmesh.devti.agent.model.CustomAgentConfig
55
import cc.unitmesh.devti.language.compiler.DevInsCompiler
66
import cc.unitmesh.devti.language.psi.DevInFile
7+
import cc.unitmesh.devti.language.run.flow.DevInsConversationService
78
import cc.unitmesh.devti.language.status.DevInsRunListener
89
import cc.unitmesh.devti.llms.LLMProvider
910
import cc.unitmesh.devti.llms.LlmFactory
@@ -25,6 +26,7 @@ import com.intellij.execution.ui.ConsoleViewContentType
2526
import com.intellij.openapi.actionSystem.ActionManager
2627
import com.intellij.openapi.actionSystem.DefaultActionGroup
2728
import com.intellij.openapi.application.ApplicationManager
29+
import com.intellij.openapi.components.service
2830
import com.intellij.openapi.project.Project
2931
import com.intellij.ui.components.panels.NonOpaquePanel
3032
import kotlinx.coroutines.flow.*
@@ -89,6 +91,8 @@ open class DevInsRunConfigurationProfileState(
8991
val compiler = DevInsCompiler(myProject, file)
9092
val compileResult = compiler.compile()
9193

94+
myProject.service<DevInsConversationService>().createConversation(configuration.getScriptPath(), compileResult)
95+
9296
val output = compileResult.output
9397
val agent = compileResult.workingAgent
9498

@@ -122,13 +126,17 @@ open class DevInsRunConfigurationProfileState(
122126
val stringFlow: Flow<String>? = CustomAgentExecutor(project = myProject).execute(output, agent)
123127
if (stringFlow != null) {
124128
LLMCoroutineScope.scope(myProject).launch {
129+
val llmResult = StringBuilder()
125130
runBlocking {
126131
stringFlow.collect {
132+
llmResult.append(it)
127133
console.print(it, ConsoleViewContentType.NORMAL_OUTPUT)
128134
}
129135
}
130136

131137
console.print("\nDone!", ConsoleViewContentType.SYSTEM_OUTPUT)
138+
myProject.service<DevInsConversationService>()
139+
.updateLlmResponse(configuration.getScriptPath(), llmResult.toString())
132140
processHandler.detachProcess()
133141
}
134142
}
@@ -161,13 +169,17 @@ open class DevInsRunConfigurationProfileState(
161169
}
162170

163171
LLMCoroutineScope.scope(myProject).launch {
172+
val llmResult = StringBuilder()
164173
runBlocking {
165174
llm.stream(output, "").collect {
175+
llmResult.append(it)
166176
console.print(it, ConsoleViewContentType.NORMAL_OUTPUT)
167177
}
168178
}
169179

170180
console.print("\nDone!", ConsoleViewContentType.SYSTEM_OUTPUT)
181+
myProject.service<DevInsConversationService>()
182+
.updateLlmResponse(configuration.getScriptPath(), llmResult.toString())
171183
processHandler.detachProcess()
172184
}
173185
}

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class DevInsConversationService(val project: Project) {
1111
*/
1212
private val cachedConversations: MutableMap<String, DevInsConversation> = mutableMapOf()
1313

14-
fun createConversation(input: String, scriptPath: String): DevInsConversation {
15-
val conversation = DevInsConversation(input, scriptPath, DevInsCompiledResult(), "", "")
14+
fun createConversation(scriptPath: String, result: DevInsCompiledResult): DevInsConversation {
15+
val conversation = DevInsConversation(scriptPath, result, "", "")
1616
cachedConversations[scriptPath] = conversation
1717
return conversation
1818
}
@@ -21,15 +21,9 @@ class DevInsConversationService(val project: Project) {
2121
return cachedConversations[scriptPath]
2222
}
2323

24-
fun updateCompiledResult(scriptPath: String, result: DevInsCompiledResult) {
24+
fun updateLlmResponse(scriptPath: String, llmResponse: String) {
2525
cachedConversations[scriptPath]?.let {
26-
cachedConversations[scriptPath] = it.copy(result = result)
27-
}
28-
}
29-
30-
fun updateApiResponse(scriptPath: String, apiResponse: String) {
31-
cachedConversations[scriptPath]?.let {
32-
cachedConversations[scriptPath] = it.copy(apiResponse = apiResponse)
26+
cachedConversations[scriptPath] = it.copy(llmResponse = llmResponse)
3327
}
3428
}
3529

@@ -38,13 +32,31 @@ class DevInsConversationService(val project: Project) {
3832
cachedConversations[path] = it.copy(ideOutput = ideOutput)
3933
}
4034
}
35+
36+
fun tryReRun(scriptPath: String) {
37+
if (cachedConversations.isEmpty()) {
38+
return
39+
}
40+
41+
val conversation = cachedConversations[scriptPath] ?: return
42+
if (conversation.hadReRun) {
43+
return
44+
}
45+
46+
conversation.hadReRun = true
47+
48+
// call llm again to re-run
49+
}
4150
}
4251

4352

4453
data class DevInsConversation(
45-
val input: String,
4654
val scriptPath: String,
4755
val result: DevInsCompiledResult,
48-
val apiResponse: String,
49-
val ideOutput: String
50-
)
56+
val llmResponse: String,
57+
val ideOutput: String,
58+
val messages: MutableList<cc.unitmesh.devti.llms.custom.Message> = mutableListOf(),
59+
var hadReRun: Boolean = false
60+
) {
61+
// update messages when has Error or Warning
62+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cc.unitmesh.devti.language.psi.DevInVisitor
55
import com.intellij.execution.process.ProcessEvent
66
import com.intellij.openapi.application.runReadAction
77
import com.intellij.openapi.components.Service
8+
import com.intellij.openapi.components.service
89
import com.intellij.openapi.project.Project
910
import com.intellij.psi.PsiComment
1011
import com.intellij.psi.PsiElement
@@ -30,12 +31,12 @@ class DevInsFlowProcessor(val project: Project) {
3031
*/
3132
fun process(output: String, event: ProcessEvent, scriptPath: String) {
3233
val devInFile: DevInFile? = runReadAction { DevInFile.lookup(project, scriptPath) }
34+
project.service<DevInsConversationService>().updateIdeOutput(scriptPath, output)
3335
if (event.exitCode == 0) {
3436
// continue
3537
}
3638
if (event.exitCode != 0) {
37-
// stop
38-
// call compiler to fix issue
39+
project.service<DevInsConversationService>().tryReRun(scriptPath)
3940
}
4041
}
4142

src/main/kotlin/cc/unitmesh/devti/gui/chat/message/AutoDevRateMessageAction.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ enum class ChatMessageRating {
1818

1919
abstract class AutoDevRateMessageAction : DumbAwareToggleAction() {
2020
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.EDT
21-
2221
abstract fun getReaction(): ChatMessageRating
2322
abstract fun getReactionIcon(): Icon
2423
abstract fun getReactionIconSelected(): Icon

0 commit comments

Comments
 (0)