Skip to content

Commit 8dbd22e

Browse files
committed
feat(agent): add display message handling for custom agents #379
Enhance custom agent execution by adding display message accumulation via StringBuilder. This allows for better message tracking and display in the UI. Also includes minor refactoring of prompt handling and adds a showLoading method to NormalChatCodingPanel.
1 parent ab7dea3 commit 8dbd22e

File tree

6 files changed

+22
-14
lines changed

6 files changed

+22
-14
lines changed

core/src/main/kotlin/cc/unitmesh/devti/agent/custom/CustomAgentChatProcessor.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@ class CustomAgentChatProcessor(val project: Project) {
2929

3030
fun handleChat(prompter: ContextPrompter, ui: NormalChatCodingPanel, llmProvider: LLMProvider): String? {
3131
val originPrompt = prompter.requestPrompt()
32-
val displayMessage = originPrompt
32+
val displayMessage = StringBuilder()
3333

3434
val request = originPrompt.trim()
3535
val selectedAgent: CustomAgentConfig = ui.getSelectedCustomAgent()
3636

3737
selectedAgent.state = CustomAgentState.HANDLING
3838

39-
val response: Flow<String>? = customAgentExecutor.execute(request, selectedAgent)
39+
ui.showLoading()
40+
41+
val response: Flow<String>? = customAgentExecutor.execute(request, selectedAgent, displayMessage)
4042
if (response == null) {
4143
logger.error("error for custom agent: $selectedAgent with request: $request")
4244
return null
4345
}
4446

45-
ui.addMessage(originPrompt, true, displayMessage)
47+
val message = displayMessage.toString()
48+
ui.addMessage(message, true, message)
4649

4750
var llmResponse = ""
4851
selectedAgent.state = CustomAgentState.FINISHED

core/src/main/kotlin/cc/unitmesh/devti/agent/custom/CustomAgentExecutor.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import cc.unitmesh.devti.llms.custom.Message
1010
import cc.unitmesh.devti.llms.custom.updateCustomFormat
1111
import cc.unitmesh.devti.provider.devins.CustomAgentContext
1212
import cc.unitmesh.devti.provider.devins.LanguageProcessor
13-
import cc.unitmesh.devti.util.readText
1413
import com.intellij.openapi.components.Service
1514
import com.intellij.openapi.diagnostic.logger
1615
import com.intellij.openapi.project.Project
@@ -32,37 +31,37 @@ class CustomAgentExecutor(val project: Project) : CustomSSEProcessor(project) {
3231
override var requestFormat: String = ""
3332
override var responseFormat: String = ""
3433

35-
fun execute(promptText: String, agent: CustomAgentConfig): Flow<String>? {
36-
var prompt = promptText
37-
34+
fun execute(promptText: String, agent: CustomAgentConfig, displayMessage: StringBuilder): Flow<String>? {
3835
if (agent.isFromDevIns) {
3936
val devin = LanguageProcessor.devin()!!
4037
val file = project.baseDir.findFileByRelativePath(agent.devinScriptPath)!!
41-
prompt = runBlocking {
38+
val prompt = runBlocking {
4239
val context = CustomAgentContext(
4340
agent, "", filePath = file,
4441
initVariables = mapOf("input" to promptText)
4542
)
4643
devin.execute(project, context)
4744
}
4845

46+
displayMessage.append(prompt)
4947
messages.add(Message("user", prompt))
5048
return LlmFactory.create(project).stream(prompt, "")
5149
}
5250

51+
displayMessage.append(promptText)
5352
messages.add(Message("user", promptText))
5453

5554
this.requestFormat = agent.connector?.requestFormat ?: this.requestFormat
5655
this.responseFormat = agent.connector?.responseFormat ?: this.responseFormat
5756

58-
val customRequest = CustomRequest(listOf(Message("user", prompt)))
57+
val customRequest = CustomRequest(listOf(Message("user", promptText)))
5958
var request = if (requestFormat.isNotEmpty()) {
6059
customRequest.updateCustomFormat(requestFormat)
6160
} else {
6261
Json.encodeToString<CustomRequest>(customRequest)
6362
}
6463

65-
request = replacePlaceholders(request, prompt)
64+
request = replacePlaceholders(request, promptText)
6665

6766
val body = request.toRequestBody("application/json".toMediaTypeOrNull())
6867
val builder = Request.Builder()

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/NormalChatCodingPanel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ class NormalChatCodingPanel(private val chatCodingService: ChatCodingService, va
190190
return messageView
191191
}
192192

193+
fun showLoading() {
194+
195+
}
196+
193197
fun getHistoryMessages(): List<LlmMsg.ChatMessage> {
194198
val messages = mutableListOf<LlmMsg.ChatMessage>()
195199
for (i in 0 until myList.componentCount) {

core/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/TestCodeGenTask.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
220220
val agent = loadTestRagConfig() ?: return ""
221221

222222
val query = testPromptContext.sourceCode
223-
val stringFlow: Flow<String> = CustomAgentExecutor(project).execute(query, agent) ?: return ""
223+
val stringFlow: Flow<String> = CustomAgentExecutor(project)
224+
.execute(query, agent, StringBuilder()) ?: return ""
224225

225226
val responseBuilder = StringBuilder()
226227
runBlocking {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import com.intellij.openapi.vfs.VirtualFileManager
4040
import com.intellij.ui.components.panels.NonOpaquePanel
4141
import kotlinx.coroutines.flow.*
4242
import kotlinx.coroutines.launch
43-
import kotlinx.coroutines.runBlocking
4443
import java.awt.BorderLayout
4544
import javax.swing.JComponent
4645

@@ -221,7 +220,8 @@ open class DevInsRunConfigurationProfileState(
221220
agent: CustomAgentConfig
222221
) {
223222
ApplicationManager.getApplication().invokeLater {
224-
val stringFlow: Flow<String>? = CustomAgentExecutor(project = myProject).execute(output, agent)
223+
val stringFlow: Flow<String>? = CustomAgentExecutor(project = myProject)
224+
.execute(output, agent, StringBuilder())
225225
if (stringFlow != null) {
226226
AutoDevCoroutineScope.scope(myProject).launch {
227227
val llmResult = StringBuilder()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class CustomRemoteAgentLlmExecutor(
1717
) : ShireLlmExecutor(context) {
1818
override fun execute(postFunction: PostFunction) {
1919
ApplicationManager.getApplication().invokeLater {
20-
val stringFlow: Flow<String>? = CustomAgentExecutor(project = context.myProject).execute(context.prompt, agent)
20+
val stringFlow: Flow<String>? = CustomAgentExecutor(project = context.myProject)
21+
.execute(context.prompt, agent, StringBuilder())
2122

2223
val console = context.console
2324
if (stringFlow == null) {

0 commit comments

Comments
 (0)