feat: 新增风险图谱算法与系统仪表盘及操作反馈体系
后端新增风险图谱算法模块、风险观察与反馈服务、规则 DSL 校验器和可解释性引擎,完善系统仪表盘和财务仪表盘统计, 优化 agent 运行和编排执行链路,清理旧开发文档,前端新增 系统趋势、负载热力图等多种仪表盘图表组件,完善操作反馈 对话框和工作台日期选择器,优化报销创建和审批详情交互, 补充单元测试覆盖。
This commit is contained in:
112
server/src/app/services/agent_feedback.py
Normal file
112
server/src/app/services/agent_feedback.py
Normal file
@@ -0,0 +1,112 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.base import Base
|
||||
from app.models.agent_feedback import AgentOperationFeedback
|
||||
from app.schemas.agent_feedback import (
|
||||
AgentFeedbackCreate,
|
||||
AgentFeedbackRead,
|
||||
AgentFeedbackSummaryRead,
|
||||
)
|
||||
|
||||
LOW_RATING_MAX = 3
|
||||
|
||||
|
||||
class AgentFeedbackService:
|
||||
def __init__(self, db: Session) -> None:
|
||||
self.db = db
|
||||
|
||||
def ensure_storage_ready(self) -> None:
|
||||
Base.metadata.create_all(bind=self.db.get_bind(), tables=[AgentOperationFeedback.__table__])
|
||||
|
||||
def create_feedback(self, payload: AgentFeedbackCreate) -> AgentFeedbackRead:
|
||||
self.ensure_storage_ready()
|
||||
feedback = AgentOperationFeedback(
|
||||
run_id=payload.run_id,
|
||||
conversation_id=payload.conversation_id,
|
||||
user_id=payload.user_id,
|
||||
agent=payload.agent or "",
|
||||
source=payload.source or "",
|
||||
session_type=payload.session_type or "",
|
||||
operation_type=payload.operation_type or "assistant_round",
|
||||
operation_status=payload.operation_status or "",
|
||||
rating=int(payload.rating),
|
||||
reason=self._normalize_reason(payload.reason),
|
||||
context_json=self._normalize_context(payload.context_json),
|
||||
)
|
||||
self.db.add(feedback)
|
||||
self.db.commit()
|
||||
self.db.refresh(feedback)
|
||||
return AgentFeedbackRead.model_validate(feedback)
|
||||
|
||||
def summarize_feedback(
|
||||
self,
|
||||
*,
|
||||
agent: str | None = None,
|
||||
session_type: str | None = None,
|
||||
limit: int = 200,
|
||||
) -> AgentFeedbackSummaryRead:
|
||||
self.ensure_storage_ready()
|
||||
stmt = select(AgentOperationFeedback).order_by(AgentOperationFeedback.created_at.desc()).limit(limit)
|
||||
if agent:
|
||||
stmt = stmt.where(AgentOperationFeedback.agent == agent)
|
||||
if session_type:
|
||||
stmt = stmt.where(AgentOperationFeedback.session_type == session_type)
|
||||
|
||||
feedback_items = list(self.db.scalars(stmt).all())
|
||||
rating_distribution = {str(score): 0 for score in range(1, 6)}
|
||||
agents: dict[str, int] = {}
|
||||
session_types: dict[str, int] = {}
|
||||
low_feedback: list[dict[str, Any]] = []
|
||||
total_rating = 0
|
||||
|
||||
for item in feedback_items:
|
||||
rating = max(1, min(int(item.rating or 0), 5))
|
||||
total_rating += rating
|
||||
rating_distribution[str(rating)] = rating_distribution.get(str(rating), 0) + 1
|
||||
if item.agent:
|
||||
agents[item.agent] = agents.get(item.agent, 0) + 1
|
||||
if item.session_type:
|
||||
session_types[item.session_type] = session_types.get(item.session_type, 0) + 1
|
||||
if rating <= LOW_RATING_MAX:
|
||||
low_feedback.append(
|
||||
{
|
||||
"feedback_id": item.feedback_id,
|
||||
"run_id": item.run_id,
|
||||
"conversation_id": item.conversation_id,
|
||||
"user_id": item.user_id,
|
||||
"agent": item.agent,
|
||||
"session_type": item.session_type,
|
||||
"rating": rating,
|
||||
"reason": item.reason,
|
||||
"created_at": item.created_at,
|
||||
}
|
||||
)
|
||||
|
||||
total_feedback = len(feedback_items)
|
||||
average_rating = round(total_rating / total_feedback, 2) if total_feedback else 0.0
|
||||
return AgentFeedbackSummaryRead(
|
||||
window_limit=limit,
|
||||
total_feedback=total_feedback,
|
||||
average_rating=average_rating,
|
||||
low_rating_count=len(low_feedback),
|
||||
rating_distribution=rating_distribution,
|
||||
agents=agents,
|
||||
session_types=session_types,
|
||||
recent_low_feedback=low_feedback[:10],
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _normalize_reason(value: str | None) -> str | None:
|
||||
normalized = str(value or "").strip()
|
||||
return normalized[:1000] if normalized else None
|
||||
|
||||
@staticmethod
|
||||
def _normalize_context(value: dict[str, Any] | None) -> dict[str, Any]:
|
||||
if not isinstance(value, dict):
|
||||
return {}
|
||||
return value
|
||||
Reference in New Issue
Block a user