Files
X-Financial/server/src/app/models/agent_run.py
caoxiaozhu 92444e7eae feat: 扩展风险规则体系、审批动态路由与预算中心列表化改造
- 新增 25+ 条风险规则(预算/报销/申请/通用类),完善风险规则模拟与反馈发布机制
- 引入费用审批动态路由、平台风险分级、预审与风险阶段管理
- 预算中心列表化改造,优化票据夹仪表盘与数字员工工作看板
- 新增 Hermes 风险线索收集器、Agent 链路追踪中心
- 扩展数字员工能力库(18 个领域 Skill)与交通费用自动预估
- 完善报销申请快速预览、权限控制与前端测试覆盖
2026-06-01 17:07:14 +08:00

112 lines
5.5 KiB
Python

from __future__ import annotations
import uuid
from datetime import datetime
from typing import Any
from sqlalchemy import DateTime, Float, ForeignKey, Index, Integer, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.types import JSON
from app.db.base_class import Base
class AgentRun(Base):
__tablename__ = "agent_runs"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
run_id: Mapped[str] = mapped_column(String(50), unique=True, index=True)
agent: Mapped[str] = mapped_column(String(30), index=True)
source: Mapped[str] = mapped_column(String(30))
user_id: Mapped[str | None] = mapped_column(String(100), nullable=True)
task_id: Mapped[str | None] = mapped_column(
ForeignKey("agent_assets.id"), nullable=True, index=True
)
ontology_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
route_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
permission_level: Mapped[str] = mapped_column(String(30), default="read")
status: Mapped[str] = mapped_column(String(20), index=True)
result_summary: Mapped[str | None] = mapped_column(Text(), nullable=True)
error_message: Mapped[str | None] = mapped_column(Text(), nullable=True)
started_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), index=True
)
finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
task_asset = relationship("AgentAsset", back_populates="scheduled_runs")
tool_calls = relationship(
"AgentToolCall",
back_populates="run",
cascade="all, delete-orphan",
order_by="asc(AgentToolCall.created_at)",
)
semantic_parse_logs = relationship(
"SemanticParseLog",
back_populates="run",
cascade="all, delete-orphan",
order_by="asc(SemanticParseLog.created_at)",
)
class AgentToolCall(Base):
__tablename__ = "agent_tool_calls"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
run_id: Mapped[str] = mapped_column(ForeignKey("agent_runs.run_id"), index=True)
tool_type: Mapped[str] = mapped_column(String(30))
tool_name: Mapped[str] = mapped_column(String(100))
request_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
response_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
status: Mapped[str] = mapped_column(String(20))
duration_ms: Mapped[int] = mapped_column(Integer, default=0)
error_message: Mapped[str | None] = mapped_column(Text(), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
run = relationship("AgentRun", back_populates="tool_calls")
class SemanticParseLog(Base):
__tablename__ = "semantic_parse_logs"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
run_id: Mapped[str] = mapped_column(ForeignKey("agent_runs.run_id"), index=True)
user_id: Mapped[str | None] = mapped_column(String(100), nullable=True)
raw_query: Mapped[str] = mapped_column(Text())
scenario: Mapped[str] = mapped_column(String(50), index=True)
intent: Mapped[str] = mapped_column(String(50), index=True)
entities_json: Mapped[list[Any]] = mapped_column(JSON, default=list)
time_range_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
metrics_json: Mapped[list[Any]] = mapped_column(JSON, default=list)
constraints_json: Mapped[list[Any]] = mapped_column(JSON, default=list)
risk_flags_json: Mapped[list[Any]] = mapped_column(JSON, default=list)
permission_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
confidence: Mapped[float] = mapped_column(Float, default=0.0)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
run = relationship("AgentRun", back_populates="semantic_parse_logs")
class AgentTraceEvent(Base):
__tablename__ = "agent_trace_events"
__table_args__ = (
Index("ix_agent_trace_events_run_sequence", "run_id", "sequence"),
Index("ix_agent_trace_events_conversation_sequence", "conversation_id", "sequence"),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
run_id: Mapped[str] = mapped_column(ForeignKey("agent_runs.run_id"), index=True)
conversation_id: Mapped[str | None] = mapped_column(String(50), nullable=True, index=True)
sequence: Mapped[int] = mapped_column(Integer, default=0, index=True)
stage: Mapped[str] = mapped_column(String(50), index=True)
event_name: Mapped[str] = mapped_column(String(100), index=True)
title: Mapped[str] = mapped_column(String(160))
summary: Mapped[str | None] = mapped_column(Text(), nullable=True)
status: Mapped[str] = mapped_column(String(20), index=True)
input_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
output_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
error_message: Mapped[str | None] = mapped_column(Text(), nullable=True)
started_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), index=True)
finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
duration_ms: Mapped[int] = mapped_column(Integer, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())