feat(ai): AgentTool trait + ToolRegistry + AgentOrchestrator — ReAct 循环(最多 5 轮 Tool Call)

This commit is contained in:
iven
2026-05-18 02:56:26 +08:00
parent 877e9831f6
commit 2d62605812
5 changed files with 228 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
use async_trait::async_trait;
use erp_core::health_provider::HealthDataProvider;
use sea_orm::DatabaseConnection;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use uuid::Uuid;
/// Agent Tool trait — 所有 Agent 可调用的工具都实现此 trait
#[async_trait]
pub trait AgentTool: Send + Sync {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn parameters_schema(&self) -> serde_json::Value;
async fn execute(&self, ctx: &ToolContext, params: serde_json::Value) -> ToolResult;
}
/// Tool 执行上下文 — 包含安全过滤后的租户/用户信息
pub struct ToolContext {
pub tenant_id: Uuid,
pub user_id: Uuid,
pub patient_id: Option<Uuid>,
pub db: DatabaseConnection,
pub health_provider: Arc<dyn HealthDataProvider>,
}
/// Tool 执行结果
pub struct ToolResult {
pub output: String,
pub display_hint: Option<DisplayHint>,
}
/// 前端渲染提示 — 告诉前端如何富化展示 Tool 返回的数据
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DisplayHint {
VitalCard {
indicator_type: String,
values: Vec<(String, f64)>,
unit: String,
},
LabReportCard {
report_date: String,
abnormal_count: usize,
},
ActionConfirm {
action_type: String,
summary: String,
confirm_payload: serde_json::Value,
},
RiskAlert {
level: String,
message: String,
},
Text,
}