use serde::{Deserialize, Serialize}; use std::future::Future; use std::pin::Pin; use std::sync::Arc; // ─── 风险等级 ─── #[derive(Debug, Clone, PartialEq, Deserialize)] #[serde(rename_all = "lowercase")] pub enum RiskLevel { Low, High, } impl RiskLevel {} // ─── 工具元数据 ─── #[derive(Debug, Clone, Deserialize)] pub struct SkillSpec { pub name: String, pub description: String, #[serde(default = "default_risk_level")] pub risk_level: RiskLevel, #[serde(default)] pub parameters: serde_json::Value, #[serde(default = "default_timeout")] #[allow(dead_code)] pub timeout_secs: u64, } fn default_risk_level() -> RiskLevel { RiskLevel::Low } fn default_timeout() -> u64 { 30 } // ─── 工具执行结果 ─── #[derive(Debug, Clone, Serialize)] pub struct SkillResult { pub success: bool, pub output: String, #[serde(skip_serializing_if = "Option::is_none")] pub data: Option, } impl SkillResult { pub fn ok(output: impl Into) -> Self { Self { success: true, output: output.into(), data: None, } } pub fn ok_with_data(output: impl Into, data: serde_json::Value) -> Self { Self { success: true, output: output.into(), data: Some(data), } } pub fn rejected(skill_name: &str) -> Self { Self { success: false, output: format!("用户拒绝了你的调用:{}", skill_name), data: None, } } #[allow(dead_code)] pub fn expired(skill_name: &str) -> Self { Self { success: false, output: format!("用户没有确认操作:{}", skill_name), data: None, } } pub fn error(msg: impl Into) -> Self { Self { success: false, output: msg.into(), data: None, } } } // ─── 执行上下文 ─── pub type WechatSender = Arc< dyn Fn(&str, &str) -> Pin> + Send>> + Send + Sync, >; pub struct ExecutionContext { pub user_id: String, pub approval_manager: Option>, pub send_wechat: Option, } impl ExecutionContext { pub fn new(user_id: impl Into) -> Self { Self { user_id: user_id.into(), approval_manager: None, send_wechat: None, } } pub fn with_approval(mut self, mgr: Arc) -> Self { self.approval_manager = Some(mgr); self } pub fn with_wechat(mut self, send: WechatSender) -> Self { self.send_wechat = Some(send); self } }