From 70ea0a6916176c56edf5d728b2e1f93af4f8a1a9 Mon Sep 17 00:00:00 2001 From: taylorxie Date: Tue, 10 Mar 2026 11:01:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maubot_llmplus/aibot.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/maubot_llmplus/aibot.py b/maubot_llmplus/aibot.py index e9cdb7e..851aacb 100644 --- a/maubot_llmplus/aibot.py +++ b/maubot_llmplus/aibot.py @@ -127,7 +127,6 @@ class AiBotPlugin(AbsExtraConfigPlugin): platform = self.get_ai_platform() if platform.is_streaming_enabled(): - await self.client.set_typing(event.room_id, timeout=0) await self._handle_streaming(event, platform) return @@ -154,7 +153,7 @@ class AiBotPlugin(AbsExtraConfigPlugin): return None async def _handle_streaming(self, evt: MessageEvent, platform) -> None: - # 发送初始占位消息 + # 发送初始占位消息(typing 保持 on,让用户知道正在处理) placeholder = TextMessageEventContent( msgtype=MessageType.TEXT, body="▌", format=Format.HTML, formatted_body="▌" ) @@ -163,6 +162,7 @@ class AiBotPlugin(AbsExtraConfigPlugin): accumulated = "" last_edit_len = 0 + first_chunk = True EDIT_THRESHOLD = 100 # 每积累100个字符更新一次消息 async def send_edit(content: TextMessageEventContent) -> None: @@ -181,6 +181,10 @@ class AiBotPlugin(AbsExtraConfigPlugin): try: async for chunk in platform.create_chat_completion_stream(self, evt): + if first_chunk: + # 收到第一个 chunk 才关掉 typing,此前 typing 持续显示(解决高 TTFT 卡顿感) + await self.client.set_typing(evt.room_id, timeout=0) + first_chunk = False accumulated += chunk if len(accumulated) - last_edit_len >= EDIT_THRESHOLD: display = accumulated + " ▌" @@ -198,6 +202,10 @@ class AiBotPlugin(AbsExtraConfigPlugin): self.log.exception(f"Streaming error: {e}") if not accumulated: accumulated = f"Streaming error: {e}" + finally: + # 确保无论如何 typing 都会关掉(含未收到任何 chunk 的情况) + if first_chunk: + await self.client.set_typing(evt.room_id, timeout=0) self.log.debug(f"Streaming: loop done, total={len(accumulated)}")