Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions astrbot/dashboard/routes/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ async def _poll_webchat_stream_result(back_queue, username: str):
try:
result = await asyncio.wait_for(back_queue.get(), timeout=1)
except asyncio.TimeoutError:
# Return a sentinel so the caller can send an SSE heartbeat to
# keep the connection alive during long-running operations (e.g.
# context compression with reasoning models). See #6938.
return None, False
except asyncio.CancelledError:
logger.debug(f"[WebChat] 用户 {username} 断开聊天长连接。")
Expand Down Expand Up @@ -364,6 +367,11 @@ async def stream():
client_disconnected = True
break
if not result:
# Send an SSE comment as keep-alive so the client
# doesn't time out during slow backend ops like
# context compression with reasoning models (#6938).
if not client_disconnected:
yield ": heartbeat\n\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了提高代码的可读性和可维护性,建议将 SSE 心跳消息 ": heartbeat\n\n" 提取为一个模块级别的常量。这样可以避免在代码中出现魔法字符串,并且如果将来需要修改心跳消息,只需在一个地方进行更改。

例如,可以在文件顶部定义:

SSE_HEARTBEAT = ": heartbeat\n\n"

然后在 stream 函数中使用它。

Suggested change
yield ": heartbeat\n\n"
yield SSE_HEARTBEAT

continue

if (
Expand Down
Loading