Files
Artemis-Eapil/apps/admin_console/services/request_diagnostics.py
T
chenmingxuan e1ea099592 feat: add official Codex skill and harden task execution
Install and update artemis-use with task diagnostics and plugin guidance. Preserve cross-device file receipts, review canonical reports, guard stage transitions and reduce small history summaries. Improve exploration lifecycle, request recovery and browser live connections.
2026-10-18 14:18:37 +08:00

83 lines
2.8 KiB
Python

"""Read/export model diagnostics from the task's existing trace database."""
from datetime import UTC, datetime
import json
import time
from apps.admin_console.database.connection import db_session
def request_diagnostics(
session_id: str, *, db_path=None, limit: int | None = None, offset: int = 0
) -> dict:
with db_session(db_path) as conn:
session = conn.execute(
"SELECT status FROM sessions WHERE session_id = ?", (session_id,)
).fetchone()
sql = (
"SELECT payload FROM traces WHERE session_id = ? AND type = 'llm_diagnostic' "
"ORDER BY timestamp DESC"
)
params: tuple = (session_id,)
total = conn.execute(
"SELECT count(*) FROM traces WHERE session_id=? AND type='llm_diagnostic'",
params,
).fetchone()[0]
if limit is not None:
sql += " LIMIT ? OFFSET ?"
params += (limit, offset)
rows = conn.execute(sql, params).fetchall()
requests = []
seen = set()
running = bool(session and session["status"] in {"running", "resuming"})
for row in rows:
try:
value = json.loads(row["payload"])
except (ValueError, TypeError):
continue
if not isinstance(value, dict):
continue
request_id = value.get("request_id")
value["active"] = (
running
and offset == 0
and request_id not in seen
and value.get("phase")
in {
"waiting_response",
"waiting_output",
"receiving_output",
"generating_tool_arguments",
"retry_wait",
}
)
seen.add(request_id)
value["timestamps_utc"] = {
field: datetime.fromtimestamp(value[field], UTC).isoformat()
for field in (
"started_at",
"headers_at",
"first_output_at",
"last_output_at",
"ended_at",
"retry_at",
)
if isinstance(value.get(field), (int, float))
}
requests.append(value)
return {
"schema_version": 1,
"session_id": session_id,
"session_status": session["status"] if session else "unknown",
"collected_at": time.time(),
"requests": requests,
"total": total,
"offset": offset,
"next_offset": offset + limit if limit is not None and offset + limit < total else None,
"note": "时间戳为 Unix 秒;timestamps_utc 为 UTC。HTTP 200 不代表完整输出。usage=null 表示服务未报告用量。历史任务没有记录的字段无法补推。",
}
def diagnostics_json(session_id: str) -> str:
return json.dumps(request_diagnostics(session_id), ensure_ascii=False, indent=2)