fix: MCP插件TimeoutError修复 + 多项Bug修复和性能优化
- fix: MCP插件管理接口改为后台任务,修复TimeoutError - fix: MCP连接失败后上下文清理的cancel scope错误 - feat: MCP插件后台注册添加重试机制 - fix: 限制每章自动创建伏笔数量上限 - fix: 修复JSON非法转义字符清洗 - fix: SSE流式生成添加心跳保活 - fix: 职业生成改用POST请求避免URL长度限制 - perf: 使用torch CPU版本加速Docker构建 - fix: 自动修复JSON字符串值中的裸换行符 - feat: 集成json5容错解析器
This commit is contained in:
@@ -388,6 +388,42 @@ async def create_sse_generator(
|
||||
yield await SSEResponse.send_error(str(e))
|
||||
|
||||
|
||||
class _HeartbeatSentinel:
|
||||
"""心跳哨兵对象,用于标识心跳事件(非AI内容)"""
|
||||
pass
|
||||
|
||||
HEARTBEAT = _HeartbeatSentinel()
|
||||
|
||||
|
||||
async def wrap_stream_with_heartbeat(
|
||||
async_gen: AsyncGenerator,
|
||||
heartbeat_interval: float = 15.0
|
||||
) -> AsyncGenerator:
|
||||
"""
|
||||
包装异步生成器,在等待数据时产生心跳哨兵,防止连接超时断开。
|
||||
|
||||
用法:
|
||||
async for chunk in wrap_stream_with_heartbeat(
|
||||
ai_service.generate_text_stream(prompt),
|
||||
heartbeat_interval=15
|
||||
):
|
||||
if chunk is HEARTBEAT:
|
||||
yield await tracker.heartbeat()
|
||||
continue
|
||||
# chunk 是原始AI数据
|
||||
"""
|
||||
ait = async_gen.__aiter__()
|
||||
while True:
|
||||
try:
|
||||
item = await asyncio.wait_for(ait.__anext__(), timeout=heartbeat_interval)
|
||||
yield item
|
||||
except asyncio.TimeoutError:
|
||||
# 等待超时,产生心跳哨兵
|
||||
yield HEARTBEAT
|
||||
except StopAsyncIteration:
|
||||
return
|
||||
|
||||
|
||||
def create_sse_response(generator: AsyncGenerator[str, None]) -> StreamingResponse:
|
||||
"""
|
||||
创建SSE StreamingResponse - 兼容HTTP/2协议
|
||||
|
||||
Reference in New Issue
Block a user