51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
"""投顾 Agent 审计日志写入。"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from sqlalchemy import text
|
|
|
|
from common.common_const import AUDIT_AGENT_CHAT_CALL, AUDIT_DRAFT_DISCARD, AUDIT_DRAFT_SAVE
|
|
|
|
|
|
def audit_action_for_path(path: str) -> str:
|
|
if path.endswith("/save"):
|
|
return AUDIT_DRAFT_SAVE
|
|
if path.endswith("/operate"):
|
|
return AUDIT_DRAFT_DISCARD
|
|
return AUDIT_AGENT_CHAT_CALL
|
|
|
|
|
|
async def write_advisor_audit(
|
|
db,
|
|
*,
|
|
user,
|
|
action: str,
|
|
target: str | None,
|
|
trace_id: str,
|
|
detail: dict | None = None,
|
|
status: str = "成功",
|
|
) -> None:
|
|
statement = text(
|
|
"""
|
|
INSERT INTO audit_log
|
|
(user_id, username, module, action, target, detail, trace_id, status)
|
|
VALUES
|
|
(:user_id, :username, :module, :action, :target, :detail, :trace_id, :status)
|
|
"""
|
|
)
|
|
await db.execute(
|
|
statement,
|
|
{
|
|
"user_id": getattr(user, "id", None),
|
|
"username": getattr(user, "username", None),
|
|
"module": "advisor_agent",
|
|
"action": action,
|
|
"target": target,
|
|
"detail": json.dumps(detail or {}, ensure_ascii=False),
|
|
"trace_id": trace_id,
|
|
"status": status,
|
|
},
|
|
)
|
|
await db.commit()
|