refactor: skill.rs → types.rs 重命名
类型定义重命名,消除过时的 'skill' 概念
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
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 {
|
||||
pub fn is_high(&self) -> bool {
|
||||
matches!(self, RiskLevel::High)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 工具元数据 ───
|
||||
|
||||
#[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")]
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user