Skip to content

Commit

Permalink
feat(custom-agent): refactor and add support for custom agent respons…
Browse files Browse the repository at this point in the history
…e actions #51

The commit refactors the `CustomAgentChatProcessor` and `ChatCodingPanel` classes to support custom agent response actions. It introduces a new `CustomAgentConfig` class to handle the configuration of custom agents and updates the `executeQuery` method in `CustomAgentHandler` to return a response based on the selected agent's response action. The `findIntention` method is replaced with `executeQuery` to better align with the new functionality. The `ChatCodingPanel` is updated to retrieve the selected custom agent using the new `getSelectedCustomAgent` method, which returns a `CustomAgentConfig` object.
  • Loading branch information
phodal committed Mar 5, 2024
1 parent c67b7b1 commit 0bfac9e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
@@ -1,11 +1,13 @@
package cc.unitmesh.devti.counit

import cc.unitmesh.devti.util.LLMCoroutineScope
import cc.unitmesh.devti.counit.model.CustomAgentConfig
import cc.unitmesh.devti.counit.model.ResponseAction
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
import cc.unitmesh.devti.gui.chat.ChatContext
import cc.unitmesh.devti.gui.chat.ChatRole
import cc.unitmesh.devti.llms.LlmFactory
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.util.LLMCoroutineScope
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.logger
Expand All @@ -24,42 +26,54 @@ class CustomAgentChatProcessor(val project: Project) {
ui.addMessage(originPrompt, true, originPrompt)

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

val response = customAgentHandler.findIntention(request)
val response = customAgentHandler.executeQuery(request, selectedAgent)
if (response == null) {
logger.error("can not find intention for request: $request")
return
}

ui.addMessage(response, true, response)
when (selectedAgent.responseAction) {
ResponseAction.Direct -> {
ui.addMessage(response, true, response)
}

// loading
ui.addMessage("start to identify intention", false, "start to identify intention")
LLMCoroutineScope.scope(project).launch {
llmProvider.appendLocalMessage(response, ChatRole.User)
ResponseAction.TextChunk -> {
ui.setInput(response)
}

val intentionFlow = llmProvider.stream(response, "")
val result = ui.updateMessage(intentionFlow)
ResponseAction.Flow -> {
ui.addMessage(response, true, response)

llmProvider.appendLocalMessage(result, ChatRole.Assistant)
// loading
ui.addMessage("start to identify intention", false, "start to identify intention")
LLMCoroutineScope.scope(project).launch {
llmProvider.appendLocalMessage(response, ChatRole.User)

val searchTip = "search API by query and hypothetical document"
llmProvider.appendLocalMessage(searchTip, ChatRole.User)
ui.addMessage(searchTip, true, searchTip)
val intentionFlow = llmProvider.stream(response, "")
val result = ui.updateMessage(intentionFlow)

val related = customAgentHandler.semanticQuery("") ?: ""
if (related.isEmpty()) {
val noResultTip = "no related API found"
llmProvider.appendLocalMessage(noResultTip, ChatRole.Assistant)
ui.addMessage(noResultTip, false, noResultTip)
return@launch
}
llmProvider.appendLocalMessage(result, ChatRole.Assistant)

val searchTip = "search API by query and hypothetical document"
llmProvider.appendLocalMessage(searchTip, ChatRole.User)
ui.addMessage(searchTip, true, searchTip)

val related = customAgentHandler.semanticQuery("") ?: ""
if (related.isEmpty()) {
val noResultTip = "no related API found"
llmProvider.appendLocalMessage(noResultTip, ChatRole.Assistant)
ui.addMessage(noResultTip, false, noResultTip)
return@launch
}

llmProvider.appendLocalMessage(related, ChatRole.User)
llmProvider.appendLocalMessage(related, ChatRole.User)

ApplicationManager.getApplication().invokeLater {
ui.addMessage(related, true, related)
ApplicationManager.getApplication().invokeLater {
ui.addMessage(related, true, related)
}
}
}
}
}
Expand Down
Expand Up @@ -5,7 +5,7 @@ import com.intellij.openapi.project.Project

@Service(Service.Level.PROJECT)
class CustomAgentHandler(val project: Project) {
fun findIntention(input: String): String? {
fun executeQuery(input: String, selectedAgent: Any): String? {
return null
}

Expand Down
Expand Up @@ -2,6 +2,7 @@ package cc.unitmesh.devti.gui.chat

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.alignRight
import cc.unitmesh.devti.counit.model.CustomAgentConfig
import cc.unitmesh.devti.fullHeight
import cc.unitmesh.devti.fullWidth
import cc.unitmesh.devti.provider.ContextPrompter
Expand Down Expand Up @@ -240,7 +241,7 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
return inputSection.hasSelectedAgent()
}

fun getSelectedCustomAgent(): Any {
fun getSelectedCustomAgent(): CustomAgentConfig {
return inputSection.getSelectedAgent()
}
}

0 comments on commit 0bfac9e

Please sign in to comment.