Files
iAs/src/tools/types.rs
T
wunianxiao a04447c7bc docs: 为全部 38 个源文件补充详细模块级注释
为 iAs 项目的所有 Rust 源文件添加了系统化的模块级文档注释(//!),
覆盖全部 src/ 下的文件。每个文件包含模块职责、架构设计、数据流
和关键设计决策的说明。

主要变更:
- 入口/CLI: main.rs, cli.rs — 系统架构概览、子命令说明
- Daemon/Worker: daemon.rs, worker.rs — 三消费者架构、旧架构说明
- 微信通道: mod.rs, client.rs, types.rs — API 端点、登录流程
- LLM 系统: mod.rs, types.rs, provider.rs, deepseek.rs, conversation.rs
  — 架构分层、流式处理、工具循环、摘要机制
- 上下文管理: mod.rs, types.rs, builder.rs, tools.rs
  — Checkpoint 机制、Token Budget、双重摘要
- 工具系统: mod.rs, types.rs, builtin.rs, approval.rs
  — 两层元工具架构、审批流程
- 内置工具: mod.rs + 6 个工具 — API 端点、认证方式、参数说明
- 消息队列: mod.rs, message_queue.rs, runner.rs
  — 公平轮转算法、三消费者路由
- 数据库/状态/调度/日志: db/mod.rs, state.rs, scheduler.rs, logger.rs
  — 表结构、回退策略、定时任务、日志初始化
2026-06-10 09:46:42 +08:00

152 lines
4.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! ## 工具系统类型定义
//!
//! 定义了工具系统的核心类型:
//!
//! - `RiskLevel` — 工具的安全级别(Low/High)
//! - `SkillSpec` — 工具的元数据描述(名称、参数、风险等级)
//! - `SkillResult` — 工具执行结果
//! - `ExecutionContext` — 工具执行时的上下文环境
//! - `WechatSender` — 微信发送回调类型别名
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
/// ## 风险等级 —— 工具的安全级别
///
/// * `Low` — 低风险,直接执行(如天气查询、日期时间)
/// * `High` — 高风险,需要用户输入确认码审批(如写备忘录、发消息)
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RiskLevel {
Low,
High,
}
impl RiskLevel {}
/// ## 工具元数据(SkillSpec)—— 工具的自描述信息
///
/// 用于给 LLM 展示"有什么工具可用、怎么用"。
/// * `name` — 工具名称
/// * `description` — 工具描述
/// * `risk_level` — 风险等级(Low/High
/// * `parameters` — JSON Schema 格式的参数定义
/// * `timeout_secs` — 执行超时时间
#[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
}
/// ## 工具执行结果
///
/// * `success` — 执行是否成功
/// * `output` — 可读的输出文本(将返回给 LLM)
/// * `data` — 结构化数据(可选),用于后续处理
#[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,
}
}
}
/// ## 执行上下文 —— 工具执行时需要的环境信息
///
/// 当工具在 daemon 上下文中执行时,携带以下信息:
/// * `user_id` — 触发工具的用户
/// * `approval_manager` — 审批管理器(高风险工具需要)
/// * `send_wechat` — 微信发送回调(用于向用户发送审批提示)
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
}
}