feat: Phase 3 - parallel execution, progress reporting, result caching + AgentChat bug fixes

Phase 3 能力:
- DAG 并行执行 (workflow_engine): asyncio.gather 并行执行就绪节点
- Debate 并行 (orchestrator): for 循环改为 asyncio.gather
- 粒度进度上报 (workflow_engine + tasks + websocket): Redis 推送 + DB 降级
- 工具结果缓存 (tool_manager): 确定性工具默认开启缓存
- LLM 响应缓存 (core): messages[-4:] + model 哈希,5min TTL

AgentChat bug 修复 (Gitea #1-#5):
- #1 SSE 降级重复空消息: fallback POST 前移除占位消息
- #2 streamTimeout 泄漏: while 正常退出后 clearTimeout
- #3 loading 闪烁: final/error 事件中提前设 loading=false
- #4 SSE 事件类型对齐: 确认匹配,未知类型加 console.warn
- #5 retryMessage 流式残留: 重试时清理占位消息

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
renjianbo
2026-05-05 00:00:51 +08:00
parent f3cb35c460
commit 7e00b027d4
8 changed files with 605 additions and 345 deletions

View File

@@ -13,6 +13,20 @@ import asyncio
router = APIRouter()
def _get_progress_from_redis(execution_id: str) -> Optional[dict]:
"""从 Redis 读取进度数据。"""
try:
from app.core.redis_client import get_redis_client
redis_client = get_redis_client()
if redis_client:
raw = redis_client.get(f"workflow:progress:{execution_id}")
if raw:
return json.loads(raw)
except Exception:
pass
return None
@router.websocket("/api/v1/ws/executions/{execution_id}")
async def websocket_execution_status(
websocket: WebSocket,
@@ -21,31 +35,36 @@ async def websocket_execution_status(
):
"""
WebSocket实时推送执行状态
Args:
websocket: WebSocket连接
execution_id: 执行记录ID
token: JWT Token可选通过query参数传递
"""
# 验证token可选如果需要认证
# user = await get_current_user_optional(token)
# 建立连接
await websocket_manager.connect(websocket, execution_id)
db = SessionLocal()
try:
# 发送初始状态
execution = db.query(Execution).filter(Execution.id == execution_id).first()
if execution:
await websocket_manager.send_personal_message({
"type": "status",
"execution_id": execution_id,
"status": execution.status,
"progress": 0,
"message": "连接已建立"
}, websocket)
# 尝试从 Redis 读取进度
redis_progress = _get_progress_from_redis(execution_id)
if redis_progress:
await websocket_manager.send_personal_message({
"type": "progress",
"execution_id": execution_id,
**redis_progress,
}, websocket)
else:
await websocket_manager.send_personal_message({
"type": "status",
"execution_id": execution_id,
"status": execution.status,
"progress": 0,
"message": "连接已建立"
}, websocket)
else:
await websocket_manager.send_personal_message({
"type": "error",
@@ -53,30 +72,56 @@ async def websocket_execution_status(
}, websocket)
await websocket.close()
return
last_progress = -1
# 持续监听并推送状态更新
while True:
try:
# 接收客户端消息(心跳等)
data = await websocket.receive_text()
# 处理客户端消息
# 接收客户端消息(心跳等),超时 1 秒以便轮询进度
try:
message = json.loads(data)
if message.get("type") == "ping":
await websocket_manager.send_personal_message({
"type": "pong"
}, websocket)
except:
pass
data = await asyncio.wait_for(websocket.receive_text(), timeout=1.0)
try:
message = json.loads(data)
if message.get("type") == "ping":
await websocket_manager.send_personal_message({
"type": "pong"
}, websocket)
except Exception:
pass
except asyncio.TimeoutError:
pass # 超时后轮询进度
except WebSocketDisconnect:
break
# 检查执行状态
db.refresh(execution)
# 优先从 Redis 读取进度(推送模式)
redis_progress = _get_progress_from_redis(execution_id)
if redis_progress:
pct = redis_progress.get("progress", -1)
if pct != last_progress:
last_progress = pct
await websocket_manager.send_personal_message({
"type": "progress",
"execution_id": execution_id,
**redis_progress,
}, websocket)
else:
# Redis 不可用时回退到 DB 轮询
db.refresh(execution)
pct = 100 if execution.status in ["completed", "failed"] else (50 if execution.status == "running" else 0)
if pct != last_progress:
last_progress = pct
await websocket_manager.send_personal_message({
"type": "status",
"execution_id": execution_id,
"status": execution.status,
"progress": pct,
"message": f"执行中..." if execution.status == "running" else "等待执行"
}, websocket)
# 如果执行完成或失败,发送最终状态并断开
db.refresh(execution)
if execution.status in ["completed", "failed"]:
await websocket_manager.send_personal_message({
"type": "status",
@@ -87,24 +132,10 @@ async def websocket_execution_status(
"error": execution.error_message if execution.status == "failed" else None,
"execution_time": execution.execution_time
}, websocket)
# 等待一下再断开,确保客户端收到消息
await asyncio.sleep(1)
break
# 定期发送状态更新每2秒
await asyncio.sleep(2)
# 重新查询执行状态
db.refresh(execution)
await websocket_manager.send_personal_message({
"type": "status",
"execution_id": execution_id,
"status": execution.status,
"progress": 50 if execution.status == "running" else 0,
"message": f"执行中..." if execution.status == "running" else "等待执行"
}, websocket)
except WebSocketDisconnect:
pass
except Exception as e:
@@ -114,7 +145,7 @@ async def websocket_execution_status(
"type": "error",
"message": f"发生错误: {str(e)}"
}, websocket)
except:
except Exception:
pass
finally:
websocket_manager.disconnect(websocket, execution_id)