feat: 新增风险图谱算法与系统仪表盘及操作反馈体系
后端新增风险图谱算法模块、风险观察与反馈服务、规则 DSL 校验器和可解释性引擎,完善系统仪表盘和财务仪表盘统计, 优化 agent 运行和编排执行链路,清理旧开发文档,前端新增 系统趋势、负载热力图等多种仪表盘图表组件,完善操作反馈 对话框和工作台日期选择器,优化报销创建和审批详情交互, 补充单元测试覆盖。
This commit is contained in:
119
server/tests/test_risk_rule_explainability.py
Normal file
119
server/tests/test_risk_rule_explainability.py
Normal file
@@ -0,0 +1,119 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.core.agent_enums import AgentAssetDomain
|
||||
from app.db.base import Base
|
||||
from app.models.agent_asset import AgentAsset
|
||||
from app.schemas.agent_asset import (
|
||||
AgentAssetRiskRuleGenerateRequest,
|
||||
AgentAssetRiskRuleSimulationRequest,
|
||||
)
|
||||
from app.services.agent_asset_rule_library import AgentAssetRuleLibraryManager
|
||||
from app.services.agent_asset_spreadsheet import RISK_RULES_LIBRARY
|
||||
from app.services.agent_assets import AgentAssetService
|
||||
from app.services.risk_rule_generation import RiskRuleGenerationService
|
||||
|
||||
|
||||
class NullRuntimeChatService:
|
||||
def complete(self, *args, **kwargs) -> None:
|
||||
return None
|
||||
|
||||
|
||||
def build_session() -> Session:
|
||||
engine = create_engine(
|
||||
"sqlite+pysqlite:///:memory:",
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
Base.metadata.create_all(bind=engine)
|
||||
session_factory = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
return session_factory()
|
||||
|
||||
|
||||
def test_generated_risk_rule_contains_semantic_plan_and_flow_model(tmp_path) -> None:
|
||||
with build_session() as db:
|
||||
manager = AgentAssetRuleLibraryManager(rule_root=tmp_path / "rules")
|
||||
asset_id = RiskRuleGenerationService(
|
||||
db,
|
||||
rule_library_manager=manager,
|
||||
runtime_chat_service=NullRuntimeChatService(),
|
||||
).generate_rule_asset(
|
||||
AgentAssetRiskRuleGenerateRequest(
|
||||
business_domain=AgentAssetDomain.EXPENSE,
|
||||
expense_category="travel",
|
||||
rule_title="差旅票据城市一致性校验",
|
||||
natural_language=(
|
||||
"差旅报销时,交通票或住宿票据中的城市必须与申报目的地一致;"
|
||||
"未说明绕行、跨城或改签原因时标记高风险。"
|
||||
),
|
||||
requires_attachment=True,
|
||||
),
|
||||
actor="pytest",
|
||||
)
|
||||
asset = db.get(AgentAsset, asset_id)
|
||||
assert asset is not None
|
||||
payload = manager.read_rule_library_json(
|
||||
library=RISK_RULES_LIBRARY,
|
||||
file_name=asset.config_json["rule_document"]["file_name"],
|
||||
)
|
||||
|
||||
assert payload["semantic_plan"]["required_fields"]
|
||||
assert payload["semantic_plan"]["risk_action"]["risk_level"] == payload["outcomes"]["fail"]["severity"]
|
||||
assert payload["flow_model"]["source"] == "json_dsl"
|
||||
assert payload["flow_model"]["nodes"][0]["id"] == "start"
|
||||
assert any(node["type"] == "risk" for node in payload["flow_model"]["nodes"])
|
||||
assert payload["metadata"]["flow_model"]["nodes"] == payload["flow_model"]["nodes"]
|
||||
assert payload["flow_diagram_svg"].startswith("<svg")
|
||||
assert "风险关键词" not in payload["semantic_plan"]["judgment_steps"][0]["description"]
|
||||
|
||||
|
||||
def test_simulation_returns_execution_trace_for_ticket_city_mismatch(tmp_path) -> None:
|
||||
with build_session() as db:
|
||||
manager = AgentAssetRuleLibraryManager(rule_root=tmp_path / "rules")
|
||||
asset_id = RiskRuleGenerationService(
|
||||
db,
|
||||
rule_library_manager=manager,
|
||||
runtime_chat_service=NullRuntimeChatService(),
|
||||
).generate_rule_asset(
|
||||
AgentAssetRiskRuleGenerateRequest(
|
||||
business_domain=AgentAssetDomain.EXPENSE,
|
||||
expense_category="travel",
|
||||
rule_title="当前差旅票据城市一致性规则",
|
||||
natural_language=(
|
||||
"差旅报销时,交通票或住宿票据中的城市必须与申报目的地一致;"
|
||||
"未说明绕行、跨城或改签原因时标记高风险。"
|
||||
),
|
||||
requires_attachment=True,
|
||||
),
|
||||
actor="pytest",
|
||||
)
|
||||
service = AgentAssetService(db)
|
||||
service.rule_library_manager = manager
|
||||
|
||||
simulation = service.simulate_risk_rule_message(
|
||||
asset_id,
|
||||
AgentAssetRiskRuleSimulationRequest(
|
||||
message="去北京出差3天",
|
||||
attachments=[
|
||||
{
|
||||
"name": "train-ticket.pdf",
|
||||
"content_type": "application/pdf",
|
||||
"ocr_text": "武汉 到 上海",
|
||||
"summary": "高铁票 武汉-上海",
|
||||
"document_fields": [
|
||||
{"key": "route", "label": "行程路线", "value": "武汉-上海"}
|
||||
],
|
||||
}
|
||||
],
|
||||
),
|
||||
)
|
||||
|
||||
assert simulation.ready is True
|
||||
assert simulation.hit is True
|
||||
assert simulation.normalized_fields["claim.location"] == "北京"
|
||||
assert simulation.trace["matched"] is True
|
||||
assert "hit" in simulation.trace["path_node_ids"]
|
||||
assert simulation.trace["steps"]
|
||||
Reference in New Issue
Block a user