69ba8887ff
- 新建 README.md,整合项目架构、命令、配置、数据流 - 删除 6 个冗余/过时 md 文档 - save_summary_to_db 委托 db::models::save_summary,避免 SQL 重复 - #[allow(dead_code)] 标注 serde 反序列化字段和预留 API - 移除未使用的 re-export (Summarizer, Message, Role, StreamChunk) - 修复 README 中 manage_memos 风险等级和缺失的 AMAP_KEY 配置
123 lines
2.8 KiB
Rust
123 lines
2.8 KiB
Rust
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<serde_json::Value>,
|
|
}
|
|
|
|
impl SkillResult {
|
|
pub fn ok(output: impl Into<String>) -> Self {
|
|
Self {
|
|
success: true,
|
|
output: output.into(),
|
|
data: None,
|
|
}
|
|
}
|
|
|
|
pub fn ok_with_data(output: impl Into<String>, 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<String>) -> Self {
|
|
Self {
|
|
success: false,
|
|
output: msg.into(),
|
|
data: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
// ─── 执行上下文 ───
|
|
|
|
pub type WechatSender = Arc<
|
|
dyn Fn(&str, &str) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>> + Send + Sync,
|
|
>;
|
|
|
|
pub struct ExecutionContext {
|
|
pub user_id: String,
|
|
pub approval_manager: Option<Arc<crate::tools::approval::ApprovalManager>>,
|
|
pub send_wechat: Option<WechatSender>,
|
|
}
|
|
|
|
impl ExecutionContext {
|
|
pub fn new(user_id: impl Into<String>) -> Self {
|
|
Self {
|
|
user_id: user_id.into(),
|
|
approval_manager: None,
|
|
send_wechat: None,
|
|
}
|
|
}
|
|
|
|
pub fn with_approval(mut self, mgr: Arc<crate::tools::approval::ApprovalManager>) -> Self {
|
|
self.approval_manager = Some(mgr);
|
|
self
|
|
}
|
|
|
|
pub fn with_wechat(mut self, send: WechatSender) -> Self {
|
|
self.send_wechat = Some(send);
|
|
self
|
|
}
|
|
}
|