Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
关键数字修正: - Rust 74.5K行(原66K), Tauri命令 183(原182), SaaS路由 121 - 前端组件 104, lib/ 85文件, Store 17+4子store - TODO/FIXME 仅 8 个(前端4+Rust4) 内容增强: - 中间件完整14层注册清单含注册条件和优先级分类 - Store完整目录结构, Pipeline完整目录树 - Hands测试分布, Memory 16个Tauri命令列表 - 管家模式: 关键词路由→语义路由(TF-IDF)修正 - 代码健康度指标新增
119 lines
4.0 KiB
Markdown
119 lines
4.0 KiB
Markdown
---
|
||
title: 管家模式
|
||
updated: 2026-04-11
|
||
status: active
|
||
tags: [module, butler, interaction]
|
||
---
|
||
|
||
# 管家模式 (Butler Mode)
|
||
|
||
> 从 [[index]] 导航。关联模块: [[chat]] [[middleware]] [[memory]]
|
||
|
||
## 设计思想
|
||
|
||
**核心问题: 非技术用户(如医院行政)不会写 prompt,需要 AI 主动引导。**
|
||
|
||
设计决策:
|
||
1. **默认激活** — 所有聊天都经过 ButlerRouter,不需要用户手动开启
|
||
2. **语义路由** — SemanticSkillRouter 用 TF-IDF 匹配 75 个技能,替代简单关键词
|
||
3. **痛点积累** — 从对话中提取用户痛点,积累后生成方案建议
|
||
4. **双模式 UI** — simple(纯聊天,默认) / professional(完整功能),渐进式解锁
|
||
|
||
## 代码逻辑
|
||
|
||
### 数据流
|
||
|
||
```
|
||
用户消息
|
||
→ ButlerRouter 中间件 (middleware/butler_router.rs)
|
||
→ ButlerRouterBackend trait → SemanticRouterAdapter
|
||
→ SemanticSkillRouter (zclaw-skills/src/semantic_router.rs)
|
||
→ TF-IDF 计算与 75 个技能的相似度
|
||
→ 返回 RoutingHint { category, confidence, skill_id }
|
||
→ 增强 system prompt (匹配的技能上下文)
|
||
→ LLM 响应
|
||
→ PainAggregator 提取痛点
|
||
→ PainStorage (内存 Vec 热缓存 + SQLite 持久层)
|
||
→ 全局 PAIN_STORAGE 单例
|
||
→ SolutionGenerator
|
||
→ 基于痛点生成解决方案提案
|
||
```
|
||
|
||
### 语义路由桥接(kernel 层)
|
||
|
||
```rust
|
||
// crates/zclaw-kernel/src/kernel/mod.rs:196-231
|
||
struct SemanticRouterAdapter { router: Arc<SemanticSkillRouter> }
|
||
impl ButlerRouterBackend for SemanticRouterAdapter {
|
||
async fn classify(&self, query: &str) -> Option<RoutingHint> { ... }
|
||
}
|
||
```
|
||
|
||
这是 kernel 依赖 zclaw-runtime + zclaw-skills 的桥接点。
|
||
|
||
### 冷启动 (新用户引导)
|
||
|
||
入口: `desktop/src/hooks/use-cold-start.ts`(lib/ 下有同名文件)
|
||
|
||
```
|
||
idle → (检测新用户) → greeting_sent → waiting_response → completed
|
||
```
|
||
|
||
4 个阶段,自动检测用户是否需要引导,发送欢迎消息,等待响应后完成。
|
||
|
||
### UI 双模式
|
||
|
||
| 模式 | Store | 特点 |
|
||
|------|-------|------|
|
||
| simple (默认) | `uiModeStore.ts` | 纯聊天界面,隐藏高级功能 |
|
||
| professional | `uiModeStore.ts` | 完整功能面板 |
|
||
|
||
切换文件: `desktop/src/store/uiModeStore.ts`
|
||
简洁侧边栏: `desktop/src/components/SimpleSidebar.tsx`
|
||
管家面板: `desktop/src/components/ButlerPanel.tsx` (3 区: 洞察/方案/记忆)
|
||
|
||
### Tauri 命令
|
||
|
||
5 个 butler 命令 (已标注 @reserved):
|
||
|
||
```rust
|
||
// desktop/src-tauri/src/intelligence/pain_aggregator.rs
|
||
butler_list_pain_points
|
||
butler_record_pain_point
|
||
butler_generate_solution
|
||
butler_list_proposals
|
||
butler_update_proposal_status
|
||
```
|
||
|
||
### Intelligence 层文件结构
|
||
|
||
```
|
||
desktop/src-tauri/src/intelligence/
|
||
├── compactor.rs (5 commands: token estimation + compaction)
|
||
├── heartbeat.rs (10 commands: heartbeat engine CRUD)
|
||
├── identity.rs (16 commands: agent identity manager)
|
||
├── pain_aggregator.rs (5 commands: butler pain points)
|
||
└── reflection.rs (7 commands: reflection engine)
|
||
```
|
||
|
||
## 关联模块
|
||
|
||
- [[middleware]] — ButlerRouter 是中间件链中的第一层
|
||
- [[chat]] — 消息流经过管家路由增强
|
||
- [[memory]] — 痛点存储在 memory 子系统
|
||
- [[hands-skills]] — 语义路由使用 75 个技能的 TF-IDF
|
||
|
||
## 关键文件
|
||
|
||
| 文件 | 职责 |
|
||
|------|------|
|
||
| `crates/zclaw-runtime/src/middleware/butler_router.rs` | 管家路由器 + ButlerRouterBackend trait |
|
||
| `crates/zclaw-skills/src/semantic_router.rs` | SemanticSkillRouter TF-IDF 实现 |
|
||
| `crates/zclaw-kernel/src/kernel/mod.rs:196-231` | SemanticRouterAdapter 桥接 |
|
||
| `crates/zclaw-kernel/src/intelligence/pain_storage.rs` | 痛点双写 (内存+SQLite) |
|
||
| `crates/zclaw-kernel/src/intelligence/solution_generator.rs` | 方案生成 |
|
||
| `desktop/src/hooks/use-cold-start.ts` | 冷启动 4 阶段 |
|
||
| `desktop/src/store/uiModeStore.ts` | 双模式切换 |
|
||
| `desktop/src/components/SimpleSidebar.tsx` | 简洁模式侧边栏 |
|
||
| `desktop/src/components/ButlerPanel.tsx` | 管家面板 (洞察/方案/记忆) |
|