feat(ai): Copilot 评分引擎 + Handler + 路由 + 权限码

- scoring.rs: 混合评分 (calculate_risk) + RiskScore/MatchedRule 结构
- engine.rs: CopilotEngine 协调规则评估和评分
- risk_service.rs: 风险计算 + UPSERT 快照 + 规则加载
- insight_service.rs: 洞察 CRUD + 过期清理
- 3 个 Handler: insight/risk/rule,7 个 API 端点
- 5 个权限码: copilot.insights.list/manage, copilot.risk.view, copilot.rules.list/manage
- AiState 扩展 risk_service + insight_service
This commit is contained in:
iven
2026-05-12 12:14:16 +08:00
parent fe983ba4ae
commit 57f33dd726
15 changed files with 698 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
use crate::entity::copilot_insights;
use erp_core::error::AppResult;
use sea_orm::{
ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, Set,
};
use uuid::Uuid;
pub struct InsightService;
impl InsightService {
/// 创建洞察记录
#[allow(clippy::too_many_arguments)]
pub async fn create_insight(
db: &sea_orm::DatabaseConnection,
tenant_id: Uuid,
patient_id: Uuid,
insight_type: String,
source: String,
severity: Option<String>,
title: String,
content: serde_json::Value,
rule_matches: Option<serde_json::Value>,
expires_hours: i64,
created_by: Option<Uuid>,
) -> AppResult<Uuid> {
let id = Uuid::now_v7();
let now = chrono::Utc::now();
let model = copilot_insights::ActiveModel {
id: Set(id),
tenant_id: Set(tenant_id),
patient_id: Set(patient_id),
insight_type: Set(insight_type),
source: Set(source),
severity: Set(severity),
title: Set(title),
content: Set(content),
rule_matches: Set(rule_matches),
llm_supplement: Set(None),
expires_at: Set(now + chrono::Duration::hours(expires_hours)),
is_read: Set(false),
is_dismissed: Set(false),
created_at: Set(now),
updated_at: Set(now),
created_by: Set(created_by),
updated_by: Set(created_by),
deleted_at: Set(None),
version_lock: Set(1),
};
model.insert(db).await?;
Ok(id)
}
/// 分页查询洞察列表
pub async fn list_insights(
db: &sea_orm::DatabaseConnection,
tenant_id: Uuid,
patient_id: Option<Uuid>,
insight_type: Option<String>,
severity: Option<String>,
page: u64,
page_size: u64,
) -> AppResult<(Vec<copilot_insights::Model>, u64)> {
let mut query = copilot_insights::Entity::find()
.filter(copilot_insights::Column::TenantId.eq(tenant_id))
.filter(copilot_insights::Column::DeletedAt.is_null())
.filter(copilot_insights::Column::IsDismissed.eq(false));
if let Some(pid) = patient_id {
query = query.filter(copilot_insights::Column::PatientId.eq(pid));
}
if let Some(ref t) = insight_type {
query = query.filter(copilot_insights::Column::InsightType.eq(t.as_str()));
}
if let Some(ref s) = severity {
query = query.filter(copilot_insights::Column::Severity.eq(s.as_str()));
}
let total = query.clone().count(db).await?;
let items = query
.order_by_desc(copilot_insights::Column::CreatedAt)
.paginate(db, page_size)
.fetch_page(page.saturating_sub(1))
.await?;
Ok((items, total))
}
/// 标记洞察已处理
pub async fn dismiss_insight(
db: &sea_orm::DatabaseConnection,
tenant_id: Uuid,
insight_id: Uuid,
updated_by: Option<Uuid>,
) -> AppResult<()> {
let model = copilot_insights::Entity::find()
.filter(copilot_insights::Column::Id.eq(insight_id))
.filter(copilot_insights::Column::TenantId.eq(tenant_id))
.filter(copilot_insights::Column::DeletedAt.is_null())
.one(db)
.await?
.ok_or_else(|| erp_core::error::AppError::NotFound("洞察记录".into()))?;
let mut active: copilot_insights::ActiveModel = model.into();
active.is_dismissed = Set(true);
active.updated_at = Set(chrono::Utc::now());
active.updated_by = Set(updated_by);
active.version_lock = Set(active.version_lock.unwrap() + 1);
active.update(db).await?;
Ok(())
}
/// 清理过期洞察
pub async fn cleanup_expired(db: &sea_orm::DatabaseConnection) -> AppResult<u64> {
let now = chrono::Utc::now();
let expired = copilot_insights::Entity::find()
.filter(copilot_insights::Column::ExpiresAt.lte(now))
.filter(copilot_insights::Column::DeletedAt.is_null())
.all(db)
.await?;
let count = expired.len() as u64;
for model in expired {
let mut active: copilot_insights::ActiveModel = model.into();
active.deleted_at = Set(Some(now));
active.updated_at = Set(now);
active.update(db).await?;
}
Ok(count)
}
}

View File

@@ -5,11 +5,13 @@ pub mod cache;
pub mod comparison;
pub mod cost;
pub mod dialysis_risk_scorer;
pub mod insight_service;
pub mod local_rules;
pub mod output_parser;
pub mod post_process;
pub mod prompt;
pub mod quota;
pub mod reanalysis;
pub mod risk_service;
pub mod suggestion;
pub mod usage;

View File

@@ -0,0 +1,132 @@
use crate::copilot::engine::CopilotEngine;
use crate::copilot::rules::RuleData;
use crate::copilot::scoring::RiskScore;
use crate::entity::copilot_risk_snapshots;
use crate::entity::copilot_rules;
use erp_core::error::AppResult;
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set};
use uuid::Uuid;
pub struct RiskService;
impl RiskService {
/// 计算患者风险评分并 UPSERT 快照
pub async fn compute_risk(
db: &sea_orm::DatabaseConnection,
tenant_id: Uuid,
patient_id: Uuid,
) -> AppResult<RiskScore> {
let rules = Self::load_rules(db, tenant_id).await?;
let patient_data = Self::load_patient_data(db, tenant_id, patient_id).await?;
let risk = CopilotEngine::assess_patient(&rules, &patient_data);
let now = chrono::Utc::now();
let existing = copilot_risk_snapshots::Entity::find()
.filter(copilot_risk_snapshots::Column::TenantId.eq(tenant_id))
.filter(copilot_risk_snapshots::Column::PatientId.eq(patient_id))
.filter(copilot_risk_snapshots::Column::DeletedAt.is_null())
.one(db)
.await?;
let rule_details = serde_json::json!({
"matched_rules": risk.matched_rules,
});
if let Some(model) = existing {
let mut active: copilot_risk_snapshots::ActiveModel = model.into();
active.risk_score = Set(risk.score);
active.risk_level = Set(risk.level.clone());
active.rule_details = Set(rule_details);
active.computed_at = Set(now);
active.updated_at = Set(now);
active.version_lock = Set(active.version_lock.unwrap() + 1);
active.update(db).await?;
} else {
let id = Uuid::now_v7();
let model = copilot_risk_snapshots::ActiveModel {
id: Set(id),
tenant_id: Set(tenant_id),
patient_id: Set(patient_id),
risk_score: Set(risk.score),
risk_level: Set(risk.level.clone()),
rule_details: Set(rule_details),
llm_summary: Set(None),
computed_at: Set(now),
data_freshness: Set(None),
created_at: Set(now),
updated_at: Set(now),
created_by: Set(None),
updated_by: Set(None),
deleted_at: Set(None),
version_lock: Set(1),
};
model.insert(db).await?;
}
Ok(risk)
}
/// 查询患者最新风险快照
pub async fn get_latest_risk(
db: &sea_orm::DatabaseConnection,
tenant_id: Uuid,
patient_id: Uuid,
) -> AppResult<Option<copilot_risk_snapshots::Model>> {
let snapshot = copilot_risk_snapshots::Entity::find()
.filter(copilot_risk_snapshots::Column::TenantId.eq(tenant_id))
.filter(copilot_risk_snapshots::Column::PatientId.eq(patient_id))
.filter(copilot_risk_snapshots::Column::DeletedAt.is_null())
.one(db)
.await?;
Ok(snapshot)
}
/// 加载租户的启用规则(含系统级规则)
async fn load_rules(
db: &sea_orm::DatabaseConnection,
tenant_id: Uuid,
) -> AppResult<Vec<RuleData>> {
let rules = copilot_rules::Entity::find()
.filter(copilot_rules::Column::TenantId.eq(tenant_id))
.filter(copilot_rules::Column::Enabled.eq(true))
.filter(copilot_rules::Column::DeletedAt.is_null())
.all(db)
.await?;
let system_rules = copilot_rules::Entity::find()
.filter(copilot_rules::Column::TenantId.eq(Uuid::nil()))
.filter(copilot_rules::Column::Enabled.eq(true))
.filter(copilot_rules::Column::DeletedAt.is_null())
.all(db)
.await?;
let all_rules: Vec<copilot_rules::Model> = rules.into_iter().chain(system_rules).collect();
Ok(all_rules
.into_iter()
.map(|r| {
(
r.id,
r.name,
r.condition_expr,
r.score,
r.severity,
r.suggestion,
)
})
.collect())
}
/// 组装患者数据用于规则评估
/// Phase 0: 基础实现,从 vital_signs_daily 和 lab_report_item 加载最新值
/// Phase 1: 补充聚合字段连续N次偏高等
async fn load_patient_data(
db: &sea_orm::DatabaseConnection,
_tenant_id: Uuid,
_patient_id: Uuid,
) -> AppResult<serde_json::Value> {
// Phase 0: 返回空数据结构,确保规则引擎不会因缺失数据崩溃
// 真实数据加载将在 Phase 1 的 "每日批量刷新" 中实现
let _ = db;
Ok(serde_json::json!({}))
}
}