Skip to content

Commit 7fc38c6

Browse files
authored
pref: 优化panle输出, 可以在输出时选中, 并且不会一直刷新processbar (#250)
1 parent c7d5f80 commit 7fc38c6

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,29 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
220220
private suspend fun updateMessageInUi(content: Flow<String>): String {
221221
val messageView = MessageView("", ChatRole.Assistant, "")
222222
myList.add(messageView)
223-
val startTime = System.currentTimeMillis() // 记录代码开始执行的时间
224-
223+
val startTime = System.currentTimeMillis()
225224
var text = ""
226-
content.onCompletion {
227-
logger.info("onCompletion ${it?.message}")
228-
hiddenProgressBar()
229-
}.catch {
230-
it.printStackTrace()
231-
}.collect {
232-
text += it
233-
225+
val batchSize = 5
226+
val buffer = mutableListOf<String>()
227+
228+
content
229+
.buffer(capacity = 10)
230+
.collect { newText ->
231+
buffer.add(newText)
232+
if (buffer.size >= batchSize) {
233+
text += buffer.joinToString("")
234+
buffer.clear()
235+
messageView.updateContent(text)
236+
}
237+
}
238+
// 处理剩余的缓冲内容
239+
if (buffer.isNotEmpty()) {
240+
text += buffer.joinToString("")
234241
messageView.updateContent(text)
235-
messageView.scrollToBottom()
236242
}
237243

238244
if (delaySeconds.isNotEmpty()) {
239245
val elapsedTime = System.currentTimeMillis() - startTime
240-
// waiting for the last message to be rendered, like sleep 5 ms?
241-
// 此处的 20s 出自 openAI 免费账户访问 3/min
242246
withContext(Dispatchers.IO) {
243247
val delaySec = delaySeconds.toLong()
244248
val remainingTime = maxOf(delaySec * 1000 - elapsedTime, 0)
@@ -247,7 +251,6 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
247251
}
248252

249253
messageView.reRenderAssistantOutput()
250-
251254
return text
252255
}
253256

core/src/main/kotlin/cc/unitmesh/devti/gui/component/DisplayComponent.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,23 @@ class DisplayComponent(question: String) : JEditorPane() {
2626
}
2727

2828
fun updateMessage(content: String) {
29+
// 记录当前的选中状态
30+
val selectionStart = this.selectionStart
31+
val selectionEnd = this.selectionEnd
2932
this.text = content
33+
if (selectionStart != selectionEnd) {
34+
SwingUtilities.invokeLater {
35+
this.setSelectionStart(selectionStart)
36+
this.setSelectionEnd(selectionEnd)
37+
}
38+
}
3039
}
3140

41+
3242
private fun stripHtmlAndUnescapeXmlEntities(input: String): String {
43+
// 使用 Jsoup 去除 HTML 标签
3344
val text = Jsoup.parse(input).text()
45+
// 使用 Apache Commons Text 解码 XML 实体
3446
return StringEscapeUtils.unescapeXml(text)
3547
}
36-
}
48+
}

0 commit comments

Comments
 (0)