@@ -2,30 +2,29 @@ package cc.unitmesh.devti.agent
2
2
3
3
import cc.unitmesh.devti.AutoDevBundle
4
4
import cc.unitmesh.devti.agent.model.CustomAgentConfig
5
- import cc.unitmesh.devti.agent.model.CustomAgentState
6
5
import cc.unitmesh.devti.agent.model.CustomAgentResponseAction
6
+ import cc.unitmesh.devti.agent.model.CustomAgentState
7
7
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
8
8
import cc.unitmesh.devti.gui.chat.ChatRole
9
9
import cc.unitmesh.devti.llms.LLMProvider
10
10
import cc.unitmesh.devti.provider.ContextPrompter
11
- import cc.unitmesh.devti.provider.devins.LanguagePromptProcessor
12
11
import cc.unitmesh.devti.provider.devins.CustomAgentContext
13
- import cc.unitmesh.devti.util.LLMCoroutineScope
12
+ import cc.unitmesh.devti.provider.devins.LanguagePromptProcessor
14
13
import cc.unitmesh.devti.util.parser.CodeFence
15
14
import com.intellij.openapi.components.Service
16
15
import com.intellij.openapi.components.service
17
16
import com.intellij.openapi.diagnostic.logger
18
17
import com.intellij.openapi.project.Project
19
- import kotlinx.coroutines.flow.*
20
- import kotlinx.coroutines.launch
18
+ import kotlinx.coroutines.flow.Flow
21
19
import kotlinx.coroutines.runBlocking
20
+ import java.util.concurrent.CompletableFuture
22
21
23
22
@Service(Service .Level .PROJECT )
24
23
class CustomAgentChatProcessor (val project : Project ) {
25
24
private val customAgentExecutor = project.service<CustomAgentExecutor >()
26
25
private val logger = logger<CustomAgentChatProcessor >()
27
26
28
- fun handleChat (prompter : ContextPrompter , ui : ChatCodingPanel , llmProvider : LLMProvider ) {
27
+ fun handleChat (prompter : ContextPrompter , ui : ChatCodingPanel , llmProvider : LLMProvider ): String? {
29
28
val originPrompt = prompter.requestPrompt()
30
29
ui.addMessage(originPrompt, true , originPrompt)
31
30
@@ -37,9 +36,10 @@ class CustomAgentChatProcessor(val project: Project) {
37
36
val response: Flow <String >? = customAgentExecutor.execute(request, selectedAgent)
38
37
if (response == null ) {
39
38
logger.error(" error for custom agent: $selectedAgent with request: $request " )
40
- return
39
+ return null
41
40
}
42
41
42
+ var llmResponse = " "
43
43
selectedAgent.state = CustomAgentState .FINISHED
44
44
45
45
var devInCode: String? = " "
@@ -63,24 +63,32 @@ class CustomAgentChatProcessor(val project: Project) {
63
63
64
64
ui.hiddenProgressBar()
65
65
ui.updateUI()
66
+
67
+ llmResponse = content
66
68
}
67
69
68
70
CustomAgentResponseAction .Stream -> {
69
71
ui.addMessage(AutoDevBundle .message(" autodev.loading" ))
70
- var msg = " "
71
- LLMCoroutineScope .scope(project).launch {
72
- msg = ui.updateMessage(response)
72
+ val future: CompletableFuture <String > = CompletableFuture ()
73
+ val sb = StringBuilder ()
74
+ runBlocking {
75
+ val result = ui.updateMessage(response)
76
+ sb.append(result)
73
77
}
74
78
75
- llmProvider.appendLocalMessage(msg, ChatRole .Assistant )
79
+ val content = sb.toString()
80
+ llmProvider.appendLocalMessage(content, ChatRole .Assistant )
76
81
ui.hiddenProgressBar()
77
82
ui.updateUI()
78
83
ui.moveCursorToStart()
79
84
80
- val code = CodeFence .parse(msg )
85
+ val code = CodeFence .parse(content )
81
86
if (code.language.displayName == " DevIn" ) {
82
87
devInCode = code.text
83
88
}
89
+
90
+ future.complete(content)
91
+ llmResponse = future.get()
84
92
}
85
93
86
94
CustomAgentResponseAction .TextChunk -> {
@@ -97,6 +105,8 @@ class CustomAgentChatProcessor(val project: Project) {
97
105
ui.moveCursorToStart()
98
106
ui.setInput(content)
99
107
ui.hiddenProgressBar()
108
+
109
+ llmResponse = content
100
110
}
101
111
102
112
CustomAgentResponseAction .Flow -> {
@@ -116,6 +126,8 @@ class CustomAgentChatProcessor(val project: Project) {
116
126
117
127
ui.appendWebView(content, project)
118
128
ui.hiddenProgressBar()
129
+
130
+ llmResponse = content
119
131
}
120
132
121
133
CustomAgentResponseAction .DevIns -> {
@@ -129,13 +141,17 @@ class CustomAgentChatProcessor(val project: Project) {
129
141
ui.updateUI()
130
142
131
143
devInCode = msg
144
+
145
+ llmResponse = msg
132
146
}
133
147
}
134
148
135
149
if (! devInCode.isNullOrEmpty()) {
136
150
LanguagePromptProcessor .instance(" DevIn" ).forEach {
137
- it.execute(project, CustomAgentContext (selectedAgent, devInCode))
151
+ it.execute(project, CustomAgentContext (selectedAgent, devInCode!! ))
138
152
}
139
153
}
154
+
155
+ return llmResponse
140
156
}
141
157
}
0 commit comments