This commit is contained in:
taylorxie
2026-03-09 23:29:50 +08:00
parent 11e37a157d
commit 89160ce482

View File

@@ -164,14 +164,25 @@ class AiBotPlugin(AbsExtraConfigPlugin):
accumulated = "" accumulated = ""
last_edit_len = 0 last_edit_len = 0
EDIT_THRESHOLD = 100 # 每积累100个字符更新一次消息 EDIT_THRESHOLD = 100 # 每积累100个字符更新一次消息
pending_edit: asyncio.Task = None # 追踪正在发送的编辑任务
async def send_edit(content: TextMessageEventContent) -> None:
"""顺序发送编辑消息用shield确保send_message不被cancel保护mautrix内部锁"""
send_task = asyncio.ensure_future(self.client.send_message(evt.room_id, content))
try:
# shield防止wait_for超时cancel send_task本身避免mautrix锁残留
await asyncio.wait_for(asyncio.shield(send_task), timeout=8.0)
except asyncio.TimeoutError:
self.log.debug("Streaming: edit wait_for timed out, awaiting task completion")
await send_task # send_task仍在运行等待自然完成
except Exception as e:
self.log.warning(f"Streaming: edit error: {e}")
if not send_task.done():
await send_task
try: try:
async for chunk in platform.create_chat_completion_stream(self, evt): async for chunk in platform.create_chat_completion_stream(self, evt):
accumulated += chunk accumulated += chunk
# 只有在没有正在进行的编辑任务时才发送中间更新
if len(accumulated) - last_edit_len >= EDIT_THRESHOLD: if len(accumulated) - last_edit_len >= EDIT_THRESHOLD:
if pending_edit is None or pending_edit.done():
display = accumulated + "" display = accumulated + ""
new_content = TextMessageEventContent( new_content = TextMessageEventContent(
msgtype=MessageType.TEXT, msgtype=MessageType.TEXT,
@@ -180,14 +191,9 @@ class AiBotPlugin(AbsExtraConfigPlugin):
formatted_body=markdown.render(display) formatted_body=markdown.render(display)
) )
new_content.set_edit(response_event_id) new_content.set_edit(response_event_id)
# 使用fire-and-forget不等待不取消避免mautrix内部锁死锁 self.log.debug(f"Streaming: mid-edit, accumulated={len(accumulated)}")
pending_edit = asyncio.ensure_future( await send_edit(new_content)
self.client.send_message(evt.room_id, new_content)
)
last_edit_len = len(accumulated) last_edit_len = len(accumulated)
self.log.debug(f"Streaming: mid-edit fired, accumulated={len(accumulated)}")
else:
self.log.debug(f"Streaming: skipping mid-edit, previous still in flight, accumulated={len(accumulated)}")
except Exception as e: except Exception as e:
self.log.exception(f"Streaming error: {e}") self.log.exception(f"Streaming error: {e}")
if not accumulated: if not accumulated:
@@ -195,13 +201,6 @@ class AiBotPlugin(AbsExtraConfigPlugin):
self.log.debug(f"Streaming: loop done, total={len(accumulated)}") self.log.debug(f"Streaming: loop done, total={len(accumulated)}")
# 等待最后一个中间编辑完成(不取消,避免锁问题)
if pending_edit and not pending_edit.done():
try:
await asyncio.wait_for(asyncio.shield(pending_edit), timeout=10.0)
except Exception:
pass
# 输出最终完整内容 # 输出最终完整内容
if not accumulated: if not accumulated:
accumulated = "(无响应)" accumulated = "(无响应)"
@@ -213,7 +212,7 @@ class AiBotPlugin(AbsExtraConfigPlugin):
) )
final_content.set_edit(response_event_id) final_content.set_edit(response_event_id)
self.log.debug("Streaming: sending final edit") self.log.debug("Streaming: sending final edit")
await self.client.send_message(evt.room_id, final_content) await send_edit(final_content)
self.log.debug("Streaming: final edit done") self.log.debug("Streaming: final edit done")
def get_ai_platform(self) -> Platform: def get_ai_platform(self) -> Platform: