Skip to content

Commit 6956864

Browse files
committed
refactor(observer): consolidate error notification logic #259
- Move error notification logic to `AgentObserver` for reusability. - Replace direct chat window calls with `sendErrorNotification` method. - Simplify process output handling in `ExternalTaskAgentObserver`.
1 parent 11a6a49 commit 6956864

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

core/src/main/kotlin/cc/unitmesh/devti/observer/ExternalTaskAgentObserver.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cc.unitmesh.devti.observer
22

33
import cc.unitmesh.devti.provider.observer.AgentObserver
4-
import com.intellij.application.subscribe
54
import com.intellij.execution.ExecutionListener
65
import com.intellij.execution.ExecutionManager
76
import com.intellij.execution.process.CapturingProcessHandler
@@ -20,28 +19,37 @@ class ExternalTaskAgentObserver : AgentObserver, Disposable {
2019
private var connection: MessageBusConnection? = null
2120

2221
override fun onRegister(project: Project) {
23-
ExecutionManager.EXECUTION_TOPIC.subscribe(this, object : ExecutionListener {
22+
connection = project.messageBus.connect()
23+
connection?.subscribe(ExecutionManager.EXECUTION_TOPIC, object : ExecutionListener {
24+
private var globalBuffer = StringBuilder()
25+
2426
override fun processStarted(executorId: String, env: ExecutionEnvironment, handler: ProcessHandler) {
2527
if (handler is ExternalSystemProcessHandler) {
2628
handler.addProcessListener(object : ProcessListener {
2729
private val outputBuffer = StringBuilder()
28-
29-
override fun onTextAvailable(event: ProcessEvent,
30+
override fun onTextAvailable(
31+
event: ProcessEvent,
3032
outputType: Key<*>
3133
) {
3234
outputBuffer.append(event.text)
3335
}
3436

3537
override fun processTerminated(event: ProcessEvent) {
36-
println("Process Output:\n$outputBuffer")
38+
globalBuffer = outputBuffer
3739
}
3840
})
3941
}
4042
}
4143

42-
override fun processTerminated(executorId: String, env: ExecutionEnvironment, handler: ProcessHandler, exitCode: Int) {
43-
if (handler is ExternalSystemProcessHandler) {
44-
println("Process terminated with exit code $exitCode")
44+
override fun processTerminated(
45+
executorId: String,
46+
env: ExecutionEnvironment,
47+
handler: ProcessHandler,
48+
exitCode: Int
49+
) {
50+
if (handler is ExternalSystemProcessHandler && exitCode != 0) {
51+
val prompt = "Help Me fix follow build issue:\n$globalBuffer"
52+
sendErrorNotification(project, prompt)
4553
}
4654
}
4755
})

core/src/main/kotlin/cc/unitmesh/devti/observer/TestAgentObserver.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ class TestAgentObserver : AgentObserver, Disposable {
2929
private fun sendResult(test: SMTestProxy, project: Project, searchScope: GlobalSearchScope) {
3030
val sourceCode = test.getLocation(project, searchScope)
3131
runInEdt {
32-
sendToChatWindow(project, ChatActionType.CHAT) { contentPanel, _ ->
33-
val psiElement = sourceCode?.psiElement
34-
val language = psiElement?.language?.displayName ?: ""
35-
val filepath = psiElement?.containingFile?.virtualFile?.relativePath(project) ?: ""
36-
val code = runReadAction<String> { psiElement?.text ?: "" }
37-
contentPanel.setInput(
38-
"""Help me fix follow test issue:
32+
val psiElement = sourceCode?.psiElement
33+
val language = psiElement?.language?.displayName ?: ""
34+
val filepath = psiElement?.containingFile?.virtualFile?.relativePath(project) ?: ""
35+
val code = runReadAction<String> { psiElement?.text ?: "" }
36+
val prompt = """Help me fix follow test issue:
3937
| ErrorMessage:
4038
|```
4139
|${test.errorMessage}
@@ -49,8 +47,9 @@ class TestAgentObserver : AgentObserver, Disposable {
4947
|$code
5048
|```
5149
|""".trimMargin()
52-
)
53-
}
50+
51+
52+
sendErrorNotification(project, prompt)
5453
}
5554
}
5655

core/src/main/kotlin/cc/unitmesh/devti/provider/observer/AgentObserver.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package cc.unitmesh.devti.provider.observer
22

3+
import cc.unitmesh.devti.gui.chat.message.ChatActionType
4+
import cc.unitmesh.devti.gui.sendToChatWindow
35
import com.intellij.openapi.extensions.ExtensionPointName
46
import com.intellij.openapi.project.Project
57

68
interface AgentObserver {
79
fun onRegister(project: Project)
810

11+
fun sendErrorNotification(project: Project, prompt: String) {
12+
sendToChatWindow(project, ChatActionType.CHAT) { contentPanel, _ ->
13+
contentPanel.setInput(prompt)
14+
}
15+
}
16+
917
companion object {
1018
private val EP_NAME: ExtensionPointName<AgentObserver> =
1119
ExtensionPointName("cc.unitmesh.agentObserver")

0 commit comments

Comments
 (0)