- 引入 TodoReadTool 工具支持读取当前对话的待办事项列表 - 实现从内存或SQLite数据库读取待办事项的功能 - 添加内存回填机制确保数据一致性 - 在ToolRegistryFactory中注册新的待办事项读取工具 - 更新会话初始化逻辑以传递待办事项存储依赖 - 添加完整的单元测试验证各种读取场景
85 lines
2.9 KiB
Rust
85 lines
2.9 KiB
Rust
use crate::agent::{SystemPrompt, SystemPromptContext, SystemPromptProvider};
|
||
|
||
pub struct TodoPromptProvider;
|
||
|
||
impl TodoPromptProvider {
|
||
pub fn new() -> Self {
|
||
Self
|
||
}
|
||
}
|
||
|
||
impl SystemPromptProvider for TodoPromptProvider {
|
||
fn build(&self, _context: &SystemPromptContext) -> Option<SystemPrompt> {
|
||
Some(SystemPrompt {
|
||
content: TODO_WRITE_INSTRUCTIONS.to_string(),
|
||
context: Some("todo_write".to_string()),
|
||
})
|
||
}
|
||
}
|
||
|
||
const TODO_WRITE_INSTRUCTIONS: &str = r#"
|
||
## TodoWrite 工具
|
||
|
||
你可以使用 `todo_write` 工具在对话中维护结构化的任务列表。
|
||
|
||
### 何时使用
|
||
- 当任务有 3 个或以上明确步骤时,应该使用 todo_write 追踪进度
|
||
- 不需要为简单的单步操作(如回答一个问题、读取一个文件)创建 todo
|
||
|
||
### merge 参数
|
||
- `merge: false`(默认):全量替换 — 只传入需要追踪的 todo,不在列表中的项将被移除
|
||
- `merge: true`(推荐):增量更新 — 只传入需要添加或更新的项,未提及的项保持不变。**绝大多数情况应该使用 merge=true**
|
||
|
||
### 状态语义
|
||
- `pending` — 尚未开始
|
||
- `in_progress` — 当前正在执行(同一时间只能有一个)
|
||
- `completed` — 已完成
|
||
- `cancelled` — 不再需要
|
||
|
||
### 核心规则
|
||
1. 同一时间只能有一个任务处于 `in_progress` 状态
|
||
2. 必须先完成当前 `in_progress` 的任务,再开始下一个
|
||
3. `completed` 和 `cancelled` 的项可以重新激活(改回 `in_progress` 或 `pending`),用于任务返工或恢复
|
||
4. `in_progress` 不能退回 `pending`,应直接标记为 `completed` 或 `cancelled`
|
||
5. 不要先标记 completed 再去实际执行 — 先完成工作,再标记
|
||
6. `content` 字段保持简洁、可执行
|
||
7. **每个任务都必须传 `id`**。新任务由你生成一个短随机字符串作为 id(如 `"r9Tg8Kq2"`),更新任务时使用相同的 id。id 可以从之前 todo_write 返回的 `current_todos` 中获取
|
||
|
||
### 使用范例
|
||
|
||
创建新任务(生成随机 id):
|
||
```json
|
||
{"merge": true, "todos": [{"id": "aB3kLm9x", "content": "修复登录 bug", "status": "in_progress"}]}
|
||
```
|
||
|
||
追加新任务:
|
||
```json
|
||
{"merge": true, "todos": [{"id": "pQ7nWy2z", "content": "补充测试", "status": "pending"}]}
|
||
```
|
||
|
||
更新已有任务(使用创建时的 id):
|
||
```json
|
||
{"merge": true, "todos": [{"id": "aB3kLm9x", "content": "修复登录 bug", "status": "completed"}]}
|
||
```
|
||
|
||
同时更新多项:
|
||
```json
|
||
{"merge": true, "todos": [
|
||
{"id": "aB3kLm9x", "content": "修复登录 bug", "status": "completed"},
|
||
{"id": "pQ7nWy2z", "content": "补充测试", "status": "in_progress"}
|
||
]}
|
||
```
|
||
|
||
### 查询当前列表
|
||
|
||
使用 `todo_read` 工具查看当前任务列表,无需任何参数:
|
||
```json
|
||
{}
|
||
```
|
||
|
||
在以下场景应主动调用 `todo_read`:
|
||
- 对话开始时,检查是否有未完成的任务
|
||
- 不确定当前任务状态时,先查询再操作
|
||
- 完成一个任务后,查看剩余任务
|
||
"#;
|