修复错误代码
This commit is contained in:
@@ -127,7 +127,6 @@ class AiBotPlugin(AbsExtraConfigPlugin):
|
|||||||
platform = self.get_ai_platform()
|
platform = self.get_ai_platform()
|
||||||
|
|
||||||
if platform.is_streaming_enabled():
|
if platform.is_streaming_enabled():
|
||||||
await self.client.set_typing(event.room_id, timeout=0)
|
|
||||||
await self._handle_streaming(event, platform)
|
await self._handle_streaming(event, platform)
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -154,7 +153,7 @@ class AiBotPlugin(AbsExtraConfigPlugin):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
async def _handle_streaming(self, evt: MessageEvent, platform) -> None:
|
async def _handle_streaming(self, evt: MessageEvent, platform) -> None:
|
||||||
# 发送初始占位消息
|
# 发送初始占位消息(typing 保持 on,让用户知道正在处理)
|
||||||
placeholder = TextMessageEventContent(
|
placeholder = TextMessageEventContent(
|
||||||
msgtype=MessageType.TEXT, body="▌", format=Format.HTML, formatted_body="▌"
|
msgtype=MessageType.TEXT, body="▌", format=Format.HTML, formatted_body="▌"
|
||||||
)
|
)
|
||||||
@@ -163,6 +162,7 @@ class AiBotPlugin(AbsExtraConfigPlugin):
|
|||||||
|
|
||||||
accumulated = ""
|
accumulated = ""
|
||||||
last_edit_len = 0
|
last_edit_len = 0
|
||||||
|
first_chunk = True
|
||||||
EDIT_THRESHOLD = 100 # 每积累100个字符更新一次消息
|
EDIT_THRESHOLD = 100 # 每积累100个字符更新一次消息
|
||||||
|
|
||||||
async def send_edit(content: TextMessageEventContent) -> None:
|
async def send_edit(content: TextMessageEventContent) -> None:
|
||||||
@@ -181,6 +181,10 @@ class AiBotPlugin(AbsExtraConfigPlugin):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
async for chunk in platform.create_chat_completion_stream(self, evt):
|
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
|
accumulated += chunk
|
||||||
if len(accumulated) - last_edit_len >= EDIT_THRESHOLD:
|
if len(accumulated) - last_edit_len >= EDIT_THRESHOLD:
|
||||||
display = accumulated + " ▌"
|
display = accumulated + " ▌"
|
||||||
@@ -198,6 +202,10 @@ class AiBotPlugin(AbsExtraConfigPlugin):
|
|||||||
self.log.exception(f"Streaming error: {e}")
|
self.log.exception(f"Streaming error: {e}")
|
||||||
if not accumulated:
|
if not accumulated:
|
||||||
accumulated = f"Streaming error: {e}"
|
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)}")
|
self.log.debug(f"Streaming: loop done, total={len(accumulated)}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user