fix: improve cross-device evidence, request diagnostics and prompt efficiency
This commit is contained in:
1 parent
621f30e437
commit
a66803651a
60 files changed
+3097
-160
No files matched your search
@@ -17,6 +17,7 @@ import json
|
||||
import time
|
||||
from uuid import UUID
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import Response
|
||||
|
||||
from artemis.config import DB_PATH, TRACES_PATH
|
||||
from artemis.runtime import trace_store
|
||||
@@ -35,6 +36,20 @@ from apps.admin_console.services.exploration_listing import exploration_metadata
|
||||
router = APIRouter(tags=["sessions"])
|
||||
|
||||
|
||||
@router.get("/api/sessions/{session_id}/request-diagnostics")
|
||||
def get_request_diagnostics(session_id: str, download: bool = False):
|
||||
from apps.admin_console.services.request_diagnostics import request_diagnostics
|
||||
|
||||
result = request_diagnostics(session_id, limit=None if download else 100)
|
||||
if not download:
|
||||
return result
|
||||
return Response(
|
||||
json.dumps(result, ensure_ascii=False, indent=2),
|
||||
media_type="application/json",
|
||||
headers={"Content-Disposition": 'attachment; filename="model-request-diagnostics.json"'},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/sessions")
|
||||
async def list_sessions():
|
||||
# The body does blocking work (sqlite queries, filesystem scans, ffmpeg
|
||||
|
||||
@@ -7,6 +7,7 @@ from pathlib import Path
|
||||
import re
|
||||
import threading
|
||||
import zipfile
|
||||
from apps.admin_console.services.request_diagnostics import diagnostics_json
|
||||
|
||||
from docx import Document
|
||||
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
|
||||
@@ -900,6 +901,7 @@ def export_session_media(session_id: str, kind: str) -> Path:
|
||||
else zipfile.ZIP_DEFLATED,
|
||||
)
|
||||
if kind == "all":
|
||||
archive.writestr(prefix + "模型请求诊断.json", diagnostics_json(session_id))
|
||||
archive.writestr(
|
||||
prefix + "证据索引.json",
|
||||
json.dumps(
|
||||
@@ -980,6 +982,7 @@ def export_batch(batch_id: str, progress: Callable[[int, int, str], None] | None
|
||||
if source.suffix.lower() in {".mp4", ".mkv", ".webm"}
|
||||
else zipfile.ZIP_DEFLATED,
|
||||
)
|
||||
archive.writestr(f"{prefix}/模型请求诊断.json", diagnostics_json(report["id"]))
|
||||
archive.writestr(
|
||||
f"{prefix}/证据索引.json",
|
||||
json.dumps(
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
"""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) -> 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,)
|
||||
if limit is not None:
|
||||
sql += " LIMIT ?"
|
||||
params += (limit,)
|
||||
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 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,
|
||||
"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)
|
||||
Reference in new issue
Block a user