Files
aiagent/backend/scripts/e2e_router_invoke_demo.py
renjianbo 0608161c82 feat: 完善企业场景多线路由与执行稳定性
补齐平台模板与场景 DSL、预算控制、执行看板和企业场景脚本,增强 Windows 启动/迁移与前端代理和聊天会话记忆,修复执行创建阶段 500 与异步链路排障体验。

Made-with: Cursor
2026-04-09 21:58:53 +08:00

112 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
E2E: 演示 Agent「演示-主链路委派invoke」执行成功并校验父子执行链。
前置:
- 已运行 create_router_invoke_demo_agent.py
- 平台 API 可访问
用法:
cd backend && .\\venv\\Scripts\\python.exe scripts/e2e_router_invoke_demo.py
"""
from __future__ import annotations
import os
import sys
import time
from typing import Any, Dict, Optional
import requests
BASE = os.getenv("PLATFORM_BASE_URL", "http://127.0.0.1:8037").rstrip("/")
USER = os.getenv("PLATFORM_USERNAME", "admin")
PWD = os.getenv("PLATFORM_PASSWORD", "123456")
DEMO_AGENT_NAME = os.getenv("DEMO_AGENT_NAME", "演示-主链路委派invoke")
def _login_headers() -> Dict[str, str]:
r = requests.post(
f"{BASE}/api/v1/auth/login",
data={"username": USER, "password": PWD},
headers={"Content-Type": "application/x-www-form-urlencoded"},
timeout=15,
)
r.raise_for_status()
token = r.json().get("access_token")
if not token:
raise RuntimeError("无 access_token")
return {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
def _find_demo_id(h: Dict[str, str]) -> Optional[str]:
r = requests.get(
f"{BASE}/api/v1/agents", params={"search": DEMO_AGENT_NAME, "limit": 50}, headers=h, timeout=30
)
r.raise_for_status()
for a in r.json() or []:
if a.get("name") == DEMO_AGENT_NAME:
return a.get("id")
return None
def _wait_done(h: Dict[str, str], eid: str, timeout_s: float = 180.0) -> Dict[str, Any]:
t0 = time.time()
while time.time() - t0 < timeout_s:
r = requests.get(f"{BASE}/api/v1/executions/{eid}", headers=h, timeout=30)
r.raise_for_status()
d = r.json()
st = d.get("status")
if st in ("completed", "failed"):
return d
time.sleep(1.2)
raise TimeoutError(eid)
def main() -> int:
h = _login_headers()
aid = _find_demo_id(h)
if not aid:
print(f"未找到 Agent: {DEMO_AGENT_NAME},请先运行 create_router_invoke_demo_agent.py", file=sys.stderr)
return 1
body = {
"agent_id": aid,
"input_data": {"query": "E2E 委派演示:请简短回复收到。", "user_id": "e2e_router_user"},
}
cr = requests.post(f"{BASE}/api/v1/executions", headers=h, json=body, timeout=30)
if cr.status_code >= 400:
print(cr.status_code, cr.text[:1200], file=sys.stderr)
return 2
eid = cr.json()["id"]
print(f"execution={eid}")
done = _wait_done(h, eid)
if done.get("status") != "completed":
print("failed:", done.get("error_message"), file=sys.stderr)
return 3
sm = requests.get(
f"{BASE}/api/v1/execution-logs/executions/{eid}/chain/summary", headers=h, timeout=30
)
if sm.status_code == 200:
s = sm.json()
print(
"chain_summary:",
s.get("total_executions"),
s.get("status_count"),
s.get("total_execution_time_ms"),
)
if (s.get("total_executions") or 0) < 2:
print("警告: 期望至少 2 条执行记录(父+子),请确认 API 已重启到最新代码", file=sys.stderr)
elif sm.status_code == 404:
print("chain/summary 404当前 API 进程可能未加载最新路由,跳过汇总校验")
else:
print("chain/summary", sm.status_code, sm.text[:300])
print("E2E 通过")
return 0
if __name__ == "__main__":
raise SystemExit(main())