Skip to content

Commit 3188b9e

Browse files
committed
refactor(SSE handling): improve SSE parsing logic #97
Refactored the Server Sent Event (SSE) parsing logic to handle edge cases better and make code cleaner. The new approach fixes #16 which involved adding checks on whether incoming events were `"ping":` or just starting with `":"` followed by "ping". It also added functionality to clear chat window before showing new messages while creating a new conversation using NewChatAction component. Made this change because it enhances user experience by providing smoother interaction between server push notifications and client display of those notifications. Modified these files accordingly as per suggestions from SonarLint analysis.
1 parent c839c89 commit 3188b9e

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/main/kotlin/cc/unitmesh/devti/gui/toolbar/NewChatAction.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@ class NewChatAction : DumbAwareAction(), CustomComponentAction {
3838
AutoDevToolWindowFactory.Util.id
3939
)
4040

41-
AutoDevToolWindowFactory().createToolWindowContent(project, toolWindowManager!!)
41+
val contentManager = toolWindowManager?.contentManager
42+
val codingPanel =
43+
contentManager?.component?.components?.filterIsInstance<ChatCodingPanel>()?.firstOrNull()
44+
45+
if (codingPanel == null) {
46+
AutoDevToolWindowFactory().createToolWindowContent(project, toolWindowManager!!)
47+
return@addActionListener
48+
}
49+
50+
codingPanel.clearChat()
4251
}
4352
}
4453
}

src/main/kotlin/cc/unitmesh/devti/llms/azure/ResponseBodyCallback.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ class ResponseBodyCallback(private val emitter: FlowableEmitter<SSE>, private va
6464
sse = when {
6565
line!!.startsWith("data:") -> {
6666
val data = line!!.substring(5).trim { it <= ' ' }
67-
// data: ping - 2024-03-05 02:07:20.310586
68-
if (data.startsWith("ping") || data.startsWith(" ping")) {
67+
// https://github.com/sysid/sse-starlette/issues/16
68+
// ```
69+
// event: ping
70+
// data: 2020-12-22 15:33:27.463789
71+
// ```
72+
if (data.startsWith(":ping") || data.startsWith(": ping")) {
6973
null
7074
} else {
7175
SSE(data)
@@ -84,7 +88,14 @@ class ResponseBodyCallback(private val emitter: FlowableEmitter<SSE>, private va
8488
}
8589
// starts with event:
8690
line!!.startsWith("event:") -> {
87-
// do nothing
91+
// https://github.com/sysid/sse-starlette/issues/16
92+
val eventName = line!!.substring(6).trim { it <= ' ' }
93+
if (eventName == "ping") {
94+
// skip ping event and data
95+
emitter.onNext(sse)
96+
emitter.onNext(sse)
97+
}
98+
8899
null
89100
}
90101

0 commit comments

Comments
 (0)