From 5c60dbe2b110a7836875129946050efdd56c1d4f Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Thu, 10 Jul 2025 16:52:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20WebChat=20?= =?UTF-8?q?=E4=B8=8B=E5=8F=AF=E8=83=BD=E6=B6=88=E6=81=AF=E9=94=99=E4=BD=8D?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/pipeline/scheduler.py | 2 +- .../platform/sources/webchat/webchat_event.py | 20 +++++++------------ astrbot/dashboard/routes/chat.py | 14 ++++--------- dashboard/src/views/ChatPage.vue | 15 +++++++------- 4 files changed, 19 insertions(+), 32 deletions(-) diff --git a/astrbot/core/pipeline/scheduler.py b/astrbot/core/pipeline/scheduler.py index d29c7ec80..a014aae6f 100644 --- a/astrbot/core/pipeline/scheduler.py +++ b/astrbot/core/pipeline/scheduler.py @@ -73,7 +73,7 @@ class PipelineScheduler: await self._process_stages(event) # 如果没有发送操作, 则发送一个空消息, 以便于后续的处理 - if not event._has_send_oper and event.get_platform_name() == "webchat": + if event.get_platform_name() == "webchat": await event.send(None) logger.debug("pipeline 执行完毕。") diff --git a/astrbot/core/platform/sources/webchat/webchat_event.py b/astrbot/core/platform/sources/webchat/webchat_event.py index c4e5d63c0..3bf1c0a2a 100644 --- a/astrbot/core/platform/sources/webchat/webchat_event.py +++ b/astrbot/core/platform/sources/webchat/webchat_event.py @@ -22,7 +22,11 @@ class WebChatMessageEvent(AstrMessageEvent): web_chat_back_queue = webchat_queue_mgr.get_or_create_back_queue(cid) if not message: await web_chat_back_queue.put( - {"type": "end", "data": "", "streaming": False} + { + "type": "end", + "data": "", + "streaming": False, + } # end means this request is finished ) return "" @@ -99,16 +103,6 @@ class WebChatMessageEvent(AstrMessageEvent): async def send(self, message: MessageChain): await WebChatMessageEvent._send(message, session_id=self.session_id) - cid = self.session_id.split("!")[-1] - web_chat_back_queue = webchat_queue_mgr.get_or_create_back_queue(cid) - await web_chat_back_queue.put( - { - "type": "end", - "data": "", - "streaming": False, - "cid": cid, - } - ) await super().send(message) async def send_streaming(self, generator, use_fallback: bool = False): @@ -120,7 +114,7 @@ class WebChatMessageEvent(AstrMessageEvent): # 分割符 await web_chat_back_queue.put( { - "type": "end", + "type": "break", # break means a segment end "data": final_data, "streaming": True, "cid": cid, @@ -134,7 +128,7 @@ class WebChatMessageEvent(AstrMessageEvent): await web_chat_back_queue.put( { - "type": "end", + "type": "complete", # complete means we return the final result "data": final_data, "streaming": True, "cid": cid, diff --git a/astrbot/dashboard/routes/chat.py b/astrbot/dashboard/routes/chat.py index b704a8888..9e090474c 100644 --- a/astrbot/dashboard/routes/chat.py +++ b/astrbot/dashboard/routes/chat.py @@ -156,6 +156,7 @@ class ChatRoute(Route): while True: try: result = await asyncio.wait_for(back_queue.get(), timeout=10) + print(f"Received result: {result}") except asyncio.TimeoutError: continue @@ -166,15 +167,12 @@ class ChatRoute(Route): type = result.get("type") cid = result.get("cid") streaming = result.get("streaming", False) - chain_type = result.get("chain_type") yield f"data: {json.dumps(result, ensure_ascii=False)}\n\n" await asyncio.sleep(0.05) - if streaming and type != "end": - # If the result is still streaming, we continue to wait for more data - continue - - if result_text: + if type == "end": + break + elif (streaming and type == "complete") or not streaming: # append bot message conversation = self.db.get_conversation_by_user_id( username, cid @@ -188,10 +186,6 @@ class ChatRoute(Route): self.db.update_conversation( username, cid, history=json.dumps(history) ) - if chain_type not in ["tool_call", "tool_call_result"]: - # If the result is not a tool call or tool call result, - # we can break the loop and end the stream - break except BaseException as _: logger.debug(f"用户 {username} 断开聊天长连接。") diff --git a/dashboard/src/views/ChatPage.vue b/dashboard/src/views/ChatPage.vue index 54977572c..3d6aeb444 100644 --- a/dashboard/src/views/ChatPage.vue +++ b/dashboard/src/views/ChatPage.vue @@ -981,20 +981,19 @@ export default { } else { message_obj.message.value += chunk_json.data; } - } else if (chunk_json.type === 'end') { - in_streaming = false; - // 在消息流结束后初始化代码复制按钮和图片点击事件 - this.initCodeCopyButtons(); - this.initImageClickEvents(); - continue; } else if (chunk_json.type === 'update_title') { // 更新对话标题 const conversation = this.conversations.find(c => c.cid === chunk_json.cid); if (conversation) { conversation.title = chunk_json.data; } - } else { - console.warn('未知数据类型:', chunk_json.type); + } + if ((chunk_json.type === 'break' && chunk_json.streaming) || !chunk_json.streaming) { + // break means a segment end + in_streaming = false; + // 在消息流结束后初始化代码复制按钮和图片点击事件 + this.initCodeCopyButtons(); + this.initImageClickEvents(); } this.scrollToBottom(); } From 2534f59398334fb0ce05fe9e3a43be47fb2b5e99 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Thu, 10 Jul 2025 16:59:58 +0800 Subject: [PATCH 2/2] chore: remove debug print statement from chat route --- astrbot/dashboard/routes/chat.py | 1 - 1 file changed, 1 deletion(-) diff --git a/astrbot/dashboard/routes/chat.py b/astrbot/dashboard/routes/chat.py index 9e090474c..651f1b65c 100644 --- a/astrbot/dashboard/routes/chat.py +++ b/astrbot/dashboard/routes/chat.py @@ -156,7 +156,6 @@ class ChatRoute(Route): while True: try: result = await asyncio.wait_for(back_queue.get(), timeout=10) - print(f"Received result: {result}") except asyncio.TimeoutError: continue