Skip to content

Commit 4c76a29

Browse files
committed
refactor(gui): improve chat coding panel UI and add message rendering logic #51
The chat coding panel has been refactored to improve the user interface and add a new message rendering logic. The changes include:
1 parent 56a88fc commit 4c76a29

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ class CustomAgentChatProcessor(val project: Project) {
3232
selectedAgent.state = CustomAgentState.FINISHED
3333
when (selectedAgent.responseAction) {
3434
ResponseAction.Direct -> {
35-
ui.addMessage(response, false, response)
35+
ui.addMessage(response, false, response).also {
36+
it.updateContent(response)
37+
it.reRenderAssistantOutput()
38+
}
3639
ui.hiddenProgressBar()
40+
ui.updateUI()
3741
}
3842

3943
ResponseAction.TextChunk -> {
@@ -42,8 +46,12 @@ class CustomAgentChatProcessor(val project: Project) {
4246
}
4347

4448
ResponseAction.Flow -> {
45-
ui.addMessage(response, false, response)
49+
ui.addMessage(response, false, response).also {
50+
it.updateContent(response)
51+
it.reRenderAssistantOutput()
52+
}
4653
ui.hiddenProgressBar()
54+
ui.updateUI()
4755
}
4856

4957
ResponseAction.WebView -> {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
118118
}
119119
}
120120

121+
/**
122+
* Add a message to the chat panel and update ui
123+
*/
121124
fun addMessage(message: String, isMe: Boolean = false, displayPrompt: String = ""): MessageView {
122125
val role = if (isMe) ChatRole.User else ChatRole.Assistant
123126
val displayText = displayPrompt.ifEmpty { message }
@@ -129,7 +132,6 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
129132
scrollToBottom()
130133
progressBar.isIndeterminate = true
131134
updateUI()
132-
133135
return messageView
134136
}
135137

@@ -200,10 +202,7 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
200202
}.collect {
201203
text += it
202204

203-
// 以下两个 API 设计不合理,如果必须要同时调用,那就只提供一个就好了
204-
messageView.updateSourceContent(text)
205205
messageView.updateContent(text)
206-
207206
messageView.scrollToBottom()
208207
}
209208

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,13 @@ class MessageView(private val message: String, val role: ChatRole, private val d
103103
}
104104
}
105105

106+
private var answer: String = ""
107+
106108
fun updateContent(content: String) {
109+
this.answer = content
107110
MessageWorker(content).execute()
108111
}
109112

110-
111-
private var answer: String = ""
112-
fun updateSourceContent(source: String) {
113-
answer = source
114-
}
115-
116113
fun scrollToBottom() {
117114
SwingUtilities.invokeLater {
118115
val bounds: Rectangle = bounds
@@ -122,7 +119,6 @@ class MessageView(private val message: String, val role: ChatRole, private val d
122119

123120
fun reRenderAssistantOutput() {
124121
ApplicationManager.getApplication().invokeLater {
125-
126122
centerPanel.remove(component)
127123
centerPanel.updateUI()
128124

src/main/kotlin/com/intellij/temporary/gui/block/CodeBlockView.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.intellij.openapi.Disposable
88
import com.intellij.openapi.actionSystem.ActionPlaces
99
import com.intellij.openapi.actionSystem.ex.ActionUtil
1010
import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl
11-
import com.intellij.openapi.application.ApplicationManager
1211
import com.intellij.openapi.application.ReadAction
1312
import com.intellij.openapi.editor.Document
1413
import com.intellij.openapi.editor.Editor
@@ -124,7 +123,7 @@ class CodeBlockView(
124123
val markupModel: MarkupModelEx = editor.markupModel
125124
(markupModel as EditorMarkupModel).isErrorStripeVisible = false
126125

127-
val settings = editor.getSettings().also {
126+
val settings = editor.settings.also {
128127
it.isDndEnabled = false
129128
it.isLineNumbersShown = false
130129
it.additionalLinesCount = 0
@@ -160,14 +159,12 @@ class CodeBlockView(
160159
message: CompletableMessage
161160
): CodePartEditorInfo {
162161
val forceFoldEditorByDefault = message.getRole() === ChatRole.User
163-
val content = graphProperty.get()
164-
165-
val createCodeViewerFile: VirtualFile = createCodeViewerFile(language, content)
162+
val createCodeViewerFile = createCodeViewerFile(language, graphProperty.get())
166163
val document: Document =
167164
createCodeViewerFile.findDocument() ?: throw IllegalStateException("Document not found")
168165

169166
val editor: EditorEx =
170-
createCodeViewerEditor(project, createCodeViewerFile as LightVirtualFile, document, disposable)
167+
createCodeViewerEditor(project, createCodeViewerFile, document, disposable)
171168

172169
val toolbarActionGroup = ActionUtil.getActionGroup("AutoDev.ToolWindow.Snippet.Toolbar")!!
173170
toolbarActionGroup.let {

0 commit comments

Comments
 (0)