a04447c7bc
为 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 — 表结构、回退策略、定时任务、日志初始化
338 lines
8.8 KiB
Rust
338 lines
8.8 KiB
Rust
//! ## CLI 子命令定义 —— clap 参数解析
|
||
//!
|
||
//! 定义了 iAs 的所有命令行子命令和参数:
|
||
//!
|
||
//! | 命令 | 说明 |
|
||
//! |------|------|
|
||
//! | `ias login` | 扫码登录微信 |
|
||
//! | `ias listen --llm` | 启动 AI 自动回复 |
|
||
//! | `ias send --to <id> --text <msg>` | 手动发送消息 |
|
||
//! | `ias whoami` | 查看登录状态 |
|
||
//! | `ias usage` | Token 用量统计 |
|
||
//! | `ias service` | 后台服务模式 |
|
||
//! | `ias daemon` | 守护进程模式 |
|
||
//! | `ias tool <tool>` | 调用内置工具 |
|
||
|
||
use clap::{Parser, Subcommand};
|
||
|
||
/// ## iAs —— 微信 AI 智能助手
|
||
///
|
||
/// 支持扫码登录、长轮询收发消息、AI 自动回复(DeepSeek)、
|
||
/// 内置工具(天气/搜索/备忘录/日期时间等)、Token 用量统计。
|
||
///
|
||
/// ### 快速开始
|
||
/// ```bash
|
||
/// ias login # 扫码登录微信
|
||
/// ias listen --llm # 启动 AI 自动回复
|
||
/// ias tool weather 北京 # 查询天气
|
||
/// ias tool search "最新新闻" # 联网搜索
|
||
/// ```
|
||
#[derive(Parser, Debug)]
|
||
#[command(name = "ias", version, about, long_about = None)]
|
||
pub struct Cli {
|
||
#[command(subcommand)]
|
||
pub command: Commands,
|
||
}
|
||
|
||
#[derive(Subcommand, Debug)]
|
||
pub enum Commands {
|
||
/// 扫码登录微信
|
||
///
|
||
/// 在终端显示登录二维码,扫描后完成登录。
|
||
/// 认证信息存入 PostgreSQL 或本地文件。
|
||
///
|
||
/// 示例:
|
||
/// ias login
|
||
/// ias login --timeout 600
|
||
Login {
|
||
/// 登录超时秒数(默认 480s)
|
||
#[arg(long, default_value = "480")]
|
||
timeout: u64,
|
||
},
|
||
|
||
/// 监听微信消息(长轮询)
|
||
///
|
||
/// 启动微信消息监听,接收并处理消息。
|
||
///
|
||
/// 示例:
|
||
/// ias listen # 仅监听,不做回复
|
||
/// ias listen --llm # AI 自动回复
|
||
/// ias listen --echo # 原样回显
|
||
Listen {
|
||
/// 启用 AI 自动回复
|
||
#[arg(long)]
|
||
llm: bool,
|
||
|
||
/// 原样回显收到的消息
|
||
#[arg(long)]
|
||
echo: bool,
|
||
},
|
||
|
||
/// 发送文本消息
|
||
///
|
||
/// 示例:
|
||
/// ias send --to wxid_xxx --text "你好"
|
||
Send {
|
||
/// 目标用户 ID
|
||
#[arg(long)]
|
||
to: String,
|
||
|
||
/// 消息内容
|
||
#[arg(long)]
|
||
text: String,
|
||
|
||
/// 上下文令牌
|
||
#[arg(long)]
|
||
context_token: Option<String>,
|
||
},
|
||
|
||
/// 查看当前登录状态
|
||
Whoami,
|
||
|
||
/// 查看 LLM Token 使用统计
|
||
///
|
||
/// 默认查询最近 7 天。
|
||
///
|
||
/// 示例:
|
||
/// ias usage
|
||
/// ias usage --since 2026-06-01T00:00:00Z --model deepseek-v4-flash
|
||
Usage {
|
||
/// 起始时间(RFC3339 格式)
|
||
#[arg(long)]
|
||
since: Option<String>,
|
||
|
||
/// 结束时间(RFC3339 格式)
|
||
#[arg(long)]
|
||
until: Option<String>,
|
||
|
||
/// 按模型名称过滤
|
||
#[arg(long)]
|
||
model: Option<String>,
|
||
},
|
||
|
||
/// 后台服务模式(等同 listen --llm + 文件日志)
|
||
///
|
||
/// 配合 systemd 实现开机自启。
|
||
///
|
||
/// 示例:
|
||
/// ias service
|
||
Service,
|
||
|
||
/// 守护进程模式(daemon + worker 分离架构)
|
||
///
|
||
/// Daemon 持有 WeChat 长连接和数据库,每条消息 spawn 独立 worker。
|
||
/// Worker 代码更新后编译即可,下一条消息自动用新版本。
|
||
///
|
||
/// 示例:
|
||
/// ias daemon
|
||
/// ias daemon --sock /tmp/ias.sock
|
||
Daemon {
|
||
/// Unix Domain Socket 路径
|
||
#[arg(long, default_value = "/tmp/ias_daemon.sock")]
|
||
sock: String,
|
||
},
|
||
|
||
/// [已废弃] Worker 进程(新架构使用 daemon 统一 tokio task)
|
||
///
|
||
/// 示例:
|
||
/// ias worker --sock /tmp/ias_daemon.sock
|
||
Worker {
|
||
/// Unix Domain Socket 路径
|
||
#[arg(long, default_value = "/tmp/ias_daemon.sock")]
|
||
sock: String,
|
||
},
|
||
|
||
/// 调用内置工具(无需登录,独立运行)
|
||
///
|
||
/// 可直接在终端调用天气、搜索、备忘录、日期时间、高德地图等工具。
|
||
#[command(
|
||
subcommand,
|
||
after_help = "示例:\n ias tool datetime\n ias tool weather 北京\n ias tool weather 上海 --days 7\n ias tool search \"Rust 2024 edition\"\n ias tool fetch https://example.com/article\n ias tool memos list\n ias tool memos add \"明天下午2点开会\"\n ias tool memos delete 3\n ias tool amap poi-search --keywords 肯德基 --city 北京\n ias tool amap geocode --address 西直门\n ias tool amap route-plan --type walking --origin 116.397,39.909 --destination 116.427,39.903\n ias tool amap travel-plan --city 北京 --interests 景点,美食 --route-type walking"
|
||
)]
|
||
Tool(ToolCommand),
|
||
}
|
||
|
||
// ─── 工具子命令 ───
|
||
|
||
#[derive(Subcommand, Debug, Clone)]
|
||
pub enum ToolCommand {
|
||
/// 获取当前日期时间(北京时间 UTC+8)
|
||
Datetime,
|
||
|
||
/// 管理备忘录
|
||
#[command(subcommand)]
|
||
Memos(MemosAction),
|
||
|
||
/// 查询指定城市的天气
|
||
///
|
||
/// 示例:
|
||
/// ias tool weather 北京
|
||
/// ias tool weather 上海 --days 7
|
||
Weather {
|
||
/// 城市名称
|
||
location: String,
|
||
|
||
/// 查询天数(1-7,默认 3)
|
||
#[arg(long, default_value = "3")]
|
||
days: u32,
|
||
},
|
||
|
||
/// 联网搜索最新信息(需配置 TAVILY_API_KEY)
|
||
///
|
||
/// 示例:
|
||
/// ias tool search "最新 AI 新闻"
|
||
/// ias tool search "Rust async" --max-results 10 --no-answer
|
||
/// ias tool search "比特币" --topic finance --time-range week
|
||
Search {
|
||
/// 搜索关键词
|
||
query: String,
|
||
|
||
/// 最大结果数(默认 5)
|
||
#[arg(long, default_value = "5")]
|
||
max_results: u32,
|
||
|
||
/// 不包含 AI 生成的摘要
|
||
#[arg(long)]
|
||
no_answer: bool,
|
||
|
||
/// 搜索主题: general / news / finance
|
||
#[arg(long)]
|
||
topic: Option<String>,
|
||
|
||
/// 发布时间过滤: day / week / month / year
|
||
#[arg(long)]
|
||
time_range: Option<String>,
|
||
},
|
||
|
||
/// 抓取网页并提取正文内容
|
||
Fetch {
|
||
/// 网页 URL
|
||
url: String,
|
||
},
|
||
|
||
/// 高德地图工具(POI搜索、地理编码、路径规划、旅游规划)
|
||
#[command(subcommand)]
|
||
Amap(AmapAction),
|
||
}
|
||
|
||
#[derive(Subcommand, Debug, Clone)]
|
||
pub enum AmapAction {
|
||
/// POI(地点)搜索
|
||
PoiSearch {
|
||
/// 搜索关键词
|
||
#[arg(long)]
|
||
keywords: String,
|
||
|
||
/// 城市名称
|
||
#[arg(long)]
|
||
city: Option<String>,
|
||
|
||
/// 中心点坐标(经度,纬度),用于周边搜索
|
||
#[arg(long)]
|
||
location: Option<String>,
|
||
|
||
/// 搜索半径(米),默认 1000
|
||
#[arg(long)]
|
||
radius: Option<u32>,
|
||
|
||
/// 页码,默认 1
|
||
#[arg(long, default_value = "1")]
|
||
page: u32,
|
||
|
||
/// 每页数量,默认 10
|
||
#[arg(long, default_value = "10")]
|
||
offset: u32,
|
||
},
|
||
|
||
/// 地理编码:地址 → 坐标
|
||
Geocode {
|
||
/// 地址名称
|
||
#[arg(long)]
|
||
address: String,
|
||
|
||
/// 城市名称(可选)
|
||
#[arg(long)]
|
||
city: Option<String>,
|
||
},
|
||
|
||
/// 逆地理编码:坐标 → 地址
|
||
ReverseGeocode {
|
||
/// 坐标(经度,纬度)
|
||
#[arg(long)]
|
||
location: String,
|
||
},
|
||
|
||
/// 路径规划
|
||
RoutePlan {
|
||
/// 出行方式:walking/driving/riding/transit
|
||
#[arg(long)]
|
||
r#type: String,
|
||
|
||
/// 起点坐标(经度,纬度)
|
||
#[arg(long)]
|
||
origin: String,
|
||
|
||
/// 终点坐标(经度,纬度)
|
||
#[arg(long)]
|
||
destination: String,
|
||
|
||
/// 城市名称(公交方式必填)
|
||
#[arg(long)]
|
||
city: Option<String>,
|
||
|
||
/// 途经点(驾车可选,多个用;分隔)
|
||
#[arg(long)]
|
||
waypoints: Option<String>,
|
||
|
||
/// 策略(驾车默认10躲避拥堵,公交默认0最快捷)
|
||
#[arg(long)]
|
||
strategy: Option<u32>,
|
||
},
|
||
|
||
/// 智能旅游规划
|
||
TravelPlan {
|
||
/// 城市名称
|
||
#[arg(long)]
|
||
city: String,
|
||
|
||
/// 兴趣点关键词(逗号分隔),如 景点,美食,酒店
|
||
#[arg(long, default_value = "景点,美食")]
|
||
interests: String,
|
||
|
||
/// 路线类型:walking/driving/riding/transit
|
||
#[arg(long, default_value = "walking")]
|
||
route_type: String,
|
||
},
|
||
|
||
/// 生成地图可视化链接
|
||
MapLink {
|
||
/// 地图数据 JSON 数组
|
||
#[arg(long)]
|
||
data: String,
|
||
},
|
||
}
|
||
|
||
#[derive(Subcommand, Debug, Clone)]
|
||
pub enum MemosAction {
|
||
/// 列出所有备忘录
|
||
List,
|
||
|
||
/// 添加备忘录
|
||
///
|
||
/// 示例:
|
||
/// ias tool memos add "明天下午2点开会"
|
||
Add {
|
||
/// 备忘录内容
|
||
content: String,
|
||
},
|
||
|
||
/// 删除指定 ID 的备忘录
|
||
///
|
||
/// 示例:
|
||
/// ias tool memos delete 3
|
||
Delete {
|
||
/// 备忘录 ID
|
||
id: i64,
|
||
},
|
||
}
|