Skip to content

Commit 1a59765

Browse files
committed
feat(custom_agent): add state management for custom agent flow #51
The custom agent chat flow now includes state management, allowing for a more robust and interactive experience. The `CustomAgentState` enum has been added to track the state of the custom agent, which can be `START`, `HANDLING`, or `FINISHED`.
1 parent 178b89d commit 1a59765

File tree

5 files changed

+37
-25
lines changed

5 files changed

+37
-25
lines changed

example/custom_agent/server.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class Messages(BaseModel):
2222

2323
@app.post("/api/agent/api-market")
2424
def mock_market(messages: Messages):
25-
return """GET /wp/v2/posts
25+
return """
26+
```markdown
27+
GET /wp/v2/posts
2628
GET /wp/v2/posts/{id}
2729
POST /wp/v2/posts
2830
PUT /wp/v2/posts/{id}
@@ -32,12 +34,15 @@ def mock_market(messages: Messages):
3234
GET /wp/v2/pages/{id}
3335
POST /wp/v2/pages
3436
PUT /wp/v2/pages/{id}
35-
DELETE /wp/v2/pages/{id}"""
37+
DELETE /wp/v2/pages/{id}
38+
```
39+
"""
3640

3741

3842
@app.post("/api/agent/frontend-gen")
3943
def mock_frontend(messages: Messages):
40-
return """Button:可快速创建不同样式的按钮。
44+
return """```markdown
45+
Button:可快速创建不同样式的按钮。
4146
Checkbox:提供多选框组件,通常用于某选项的打开或关闭。
4247
CheckboxGroup:多选框群组,用于控制多选框全选或者不全选状态。
4348
CustomDialog:自定义弹窗(CustomDialog)可用于广告、中奖、警告、软件更新等与用户交互响应操作。
@@ -50,6 +55,7 @@ def mock_frontend(messages: Messages):
5055
TextInput:单行文本输入框组件。
5156
Radio:提供相应的用户交互选择项。
5257
Toggle:Toggle为开关组件,常用于在应用中进行开关操作。
58+
```
5359
"""
5460

5561

src/main/kotlin/cc/unitmesh/devti/counit/CustomAgentChatProcessor.kt

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
package cc.unitmesh.devti.counit
22

33
import cc.unitmesh.devti.counit.model.CustomAgentConfig
4+
import cc.unitmesh.devti.counit.model.CustomAgentState
45
import cc.unitmesh.devti.counit.model.ResponseAction
56
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
67
import cc.unitmesh.devti.gui.chat.ChatContext
7-
import cc.unitmesh.devti.gui.chat.ChatRole
8-
import cc.unitmesh.devti.llms.LlmFactory
98
import cc.unitmesh.devti.provider.ContextPrompter
10-
import cc.unitmesh.devti.util.LLMCoroutineScope
119
import com.intellij.openapi.components.Service
1210
import com.intellij.openapi.diagnostic.logger
1311
import com.intellij.openapi.project.Project
14-
import kotlinx.coroutines.launch
1512

1613
@Service(Service.Level.PROJECT)
1714
class CustomAgentChatProcessor(val project: Project) {
18-
private val llmFactory = LlmFactory()
19-
2015
private val customAgentExecutor = CustomAgentExecutor(project)
21-
private val llmProvider = llmFactory.create(project)
2216

2317
fun handleChat(prompter: ContextPrompter, ui: ChatCodingPanel, context: ChatContext?) {
2418
val originPrompt = prompter.requestPrompt()
@@ -27,33 +21,29 @@ class CustomAgentChatProcessor(val project: Project) {
2721
val request = originPrompt.trim()
2822
val selectedAgent: CustomAgentConfig = ui.getSelectedCustomAgent()
2923

24+
selectedAgent.state = CustomAgentState.HANDLING
25+
3026
val response = customAgentExecutor.execute(request, selectedAgent)
3127
if (response == null) {
3228
logger.error("error for custom agent: $selectedAgent with request: $request")
3329
return
3430
}
3531

32+
selectedAgent.state = CustomAgentState.FINISHED
3633
when (selectedAgent.responseAction) {
3734
ResponseAction.Direct -> {
38-
ui.addMessage(response, true, response)
35+
ui.addMessage(response, false, response)
36+
ui.hiddenProgressBar()
3937
}
4038

4139
ResponseAction.TextChunk -> {
4240
ui.setInput(response)
41+
ui.hiddenProgressBar()
4342
}
4443

4544
ResponseAction.Flow -> {
46-
ui.addMessage(response, true, response)
47-
48-
// loading
49-
LLMCoroutineScope.scope(project).launch {
50-
llmProvider.appendLocalMessage(response, ChatRole.User)
51-
52-
val intentionFlow = llmProvider.stream(response, "")
53-
val result = ui.updateMessage(intentionFlow)
54-
55-
llmProvider.appendLocalMessage(result, ChatRole.Assistant)
56-
}
45+
ui.addMessage(response, false, response)
46+
ui.hiddenProgressBar()
5747
}
5848

5949
ResponseAction.WebView -> TODO()

src/main/kotlin/cc/unitmesh/devti/counit/model/CustomAgentConfig.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,16 @@ data class CustomAgentConfig(
7878
val transition: List<CustomFlowTransition> = emptyList(),
7979
val interactive: InteractionType = InteractionType.ChatPanel,
8080
val auth: CustomAgentAuth? = null
81-
)
81+
) {
82+
var state: CustomAgentState = CustomAgentState.START
83+
}
84+
85+
@Serializable
86+
enum class CustomAgentState {
87+
START,
88+
HANDLING,
89+
FINISHED
90+
}
8291

8392
@Serializable
8493
data class CustomAgentAuth(

src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingPanel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,8 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
244244
fun getSelectedCustomAgent(): CustomAgentConfig {
245245
return inputSection.getSelectedAgent()
246246
}
247+
248+
fun hiddenProgressBar() {
249+
progressBar.isVisible = false
250+
}
247251
}

src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingService.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cc.unitmesh.devti.AutoDevBundle
55
import cc.unitmesh.devti.util.LLMCoroutineScope
66
import cc.unitmesh.devti.counit.CustomAgentChatProcessor
77
import cc.unitmesh.devti.counit.configurable.customAgentSetting
8+
import cc.unitmesh.devti.counit.model.CustomAgentState
89
import cc.unitmesh.devti.llms.LlmFactory
910
import cc.unitmesh.devti.util.parser.PostCodeProcessor
1011
import cc.unitmesh.devti.provider.ContextPrompter
@@ -35,8 +36,10 @@ class ChatCodingService(var actionType: ChatActionType, val project: Project) {
3536
val displayPrompt = prompter.displayPrompt()
3637

3738
if (project.customAgentSetting.enableCustomRag && ui.hasSelectedCustomAgent()) {
38-
counitProcessor.handleChat(prompter, ui, context)
39-
return
39+
if (ui.getSelectedCustomAgent().state === CustomAgentState.START) {
40+
counitProcessor.handleChat(prompter, ui, context)
41+
return
42+
}
4043
}
4144

4245
ui.addMessage(requestPrompt, true, displayPrompt)

0 commit comments

Comments
 (0)