fix: 实现 Agent 修复验证闭环
This commit is contained in:
+36
-7
@@ -22,7 +22,7 @@ from backend.diagnosis import (
|
||||
from backend.ai_agent import (
|
||||
analyze_with_ai, chat_with_ai,
|
||||
analyze_with_ai_stream, chat_with_ai_stream,
|
||||
run_diagnostic_agent, take_pending_approval,
|
||||
run_diagnostic_agent, resume_diagnostic_agent, take_pending_approval,
|
||||
)
|
||||
from backend.agent_tools import execute_kubectl
|
||||
from backend.history_store import HistoryStore
|
||||
@@ -371,12 +371,41 @@ async def execute_approved_agent_command(req: AgentApprovalRequest):
|
||||
history_store.finish_run(pending["run_id"], "rejected", result["message"])
|
||||
return result
|
||||
result = await execute_kubectl(pending["command"])
|
||||
response = {"approved": True, "reason": pending["reason"], **result}
|
||||
if pending.get("run_id"):
|
||||
history_store.append_event(pending["run_id"], "approved_result", response)
|
||||
status = "completed" if result.get("returncode") == 0 else "failed"
|
||||
history_store.finish_run(pending["run_id"], status, result.get("stdout") or result.get("stderr", ""))
|
||||
return response
|
||||
approved_event = {"approved": True, "reason": pending["reason"], **result}
|
||||
|
||||
async def event_generator():
|
||||
yield f"data: {json.dumps({'type': 'approved_result', **approved_event}, ensure_ascii=False)}\n\n"
|
||||
run_id = pending.get("run_id", "")
|
||||
if run_id:
|
||||
history_store.append_event(run_id, "approved_result", approved_event)
|
||||
|
||||
final_status = "completed"
|
||||
summary = result.get("stdout") or result.get("stderr", "")
|
||||
async for event in resume_diagnostic_agent(pending, result):
|
||||
if run_id:
|
||||
history_store.append_event(run_id, event.get("type", "event"), {k: v for k, v in event.items() if k != "type"})
|
||||
if event.get("type") == "final":
|
||||
summary = event.get("content", "")
|
||||
elif event.get("type") == "error":
|
||||
final_status = "failed"
|
||||
summary = event.get("message", "")
|
||||
elif event.get("type") == "approval_required":
|
||||
final_status = "waiting"
|
||||
from backend.ai_agent import _PENDING_APPROVALS
|
||||
next_pending = _PENDING_APPROVALS.get(event.get("approval_id"))
|
||||
if next_pending is not None:
|
||||
next_pending["run_id"] = run_id
|
||||
yield f"data: {json.dumps(event, ensure_ascii=False)}\n\n"
|
||||
|
||||
if run_id:
|
||||
history_store.finish_run(run_id, final_status, summary)
|
||||
yield f"data: {json.dumps({'type': 'done', 'run_id': run_id}, ensure_ascii=False)}\n\n"
|
||||
|
||||
return StreamingResponse(
|
||||
event_generator(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/chat")
|
||||
|
||||
Reference in New Issue
Block a user