|
|
@@ -432,63 +432,37 @@ async def _background_execute(
|
|
|
record_id: str,
|
|
|
chain_id: str,
|
|
|
) -> None:
|
|
|
- """后台执行审查调试,通过 event_queue 推送进度"""
|
|
|
- async with debug_semaphore:
|
|
|
- completed_data = None
|
|
|
- error_occurred = None
|
|
|
- steps_collected: List[dict] = []
|
|
|
+ """后台执行审查调试,通过 event_queue 推送进度给 SSE 端点。
|
|
|
|
|
|
+ 不消费 event_queue —— 仅启动 executor 并等待其完成,
|
|
|
+ 将结果持久化到调用记录。SSE 事件由 GET stream 端点独立消费。
|
|
|
+ """
|
|
|
+ async with debug_semaphore:
|
|
|
try:
|
|
|
- # 发送 started 事件
|
|
|
+ from core.debug.executor import DebugExecutor
|
|
|
+
|
|
|
await event_queue.put(("started", {
|
|
|
"task_id": task_id,
|
|
|
"chain_id": chain_id,
|
|
|
"total_steps": CHAIN_STEPS_COUNT.get(chain_id, 3),
|
|
|
}))
|
|
|
|
|
|
- # 启动执行器
|
|
|
- exec_task = asyncio.create_task(
|
|
|
- _run_debug_execution(request, event_queue, task_id, record_id)
|
|
|
- )
|
|
|
-
|
|
|
- # 消费队列事件
|
|
|
- while True:
|
|
|
- try:
|
|
|
- event_type, data = await asyncio.wait_for(
|
|
|
- event_queue.get(),
|
|
|
- timeout=DEBUG_GLOBAL_TIMEOUT,
|
|
|
- )
|
|
|
- except asyncio.TimeoutError:
|
|
|
- error_occurred = "执行超时"
|
|
|
- await event_queue.put(("error", {
|
|
|
- "task_id": task_id,
|
|
|
- "message": f"执行超时 (>{DEBUG_GLOBAL_TIMEOUT}s)",
|
|
|
- }))
|
|
|
- exec_task.cancel()
|
|
|
- break
|
|
|
-
|
|
|
- if event_type == "__end__":
|
|
|
- break
|
|
|
-
|
|
|
- if event_type == "started":
|
|
|
- continue
|
|
|
-
|
|
|
- if event_type == "step_result":
|
|
|
- steps_collected.append({
|
|
|
- "index": data.get("step_index"),
|
|
|
- "name": data.get("step_name"),
|
|
|
- "status": data.get("status"),
|
|
|
- "duration_ms": int((data.get("duration") or 0) * 1000),
|
|
|
- "input": data.get("input", {}),
|
|
|
- "output": data.get("output", {}),
|
|
|
- })
|
|
|
-
|
|
|
- if event_type == "error":
|
|
|
- error_occurred = data.get("message", "未知错误")
|
|
|
+ executor = DebugExecutor()
|
|
|
+ result = await executor.execute(request, event_queue)
|
|
|
|
|
|
- if event_type == "completed":
|
|
|
- completed_data = data
|
|
|
- break
|
|
|
+ # 保存调用记录
|
|
|
+ try:
|
|
|
+ await _save_debug_record(
|
|
|
+ request=request,
|
|
|
+ task_id=task_id,
|
|
|
+ record_id=record_id,
|
|
|
+ chain_id=chain_id,
|
|
|
+ completed_data=result.get("completed_data"),
|
|
|
+ error_message=result.get("error_occurred"),
|
|
|
+ steps=result.get("steps_collected", []),
|
|
|
+ )
|
|
|
+ except Exception as exc:
|
|
|
+ logger.warning("[execute_review] 保存调用记录失败: %s", exc)
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
await event_queue.put(("error", {
|
|
|
@@ -504,20 +478,6 @@ async def _background_execute(
|
|
|
finally:
|
|
|
_running_tasks.pop(task_id, None)
|
|
|
|
|
|
- # 自动保存调用记录
|
|
|
- try:
|
|
|
- await _save_debug_record(
|
|
|
- request=request,
|
|
|
- task_id=task_id,
|
|
|
- record_id=record_id,
|
|
|
- chain_id=chain_id,
|
|
|
- completed_data=completed_data,
|
|
|
- error_message=error_occurred,
|
|
|
- steps=steps_collected,
|
|
|
- )
|
|
|
- except Exception as exc:
|
|
|
- logger.warning("[execute_review] 保存调用记录失败: %s", exc)
|
|
|
-
|
|
|
|
|
|
# ============ SSE 断线重连端点 ============
|
|
|
|