Refactor tool execution pipeline
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
# 实现细节
|
||||
|
||||
> 2024-06-18 当前状态
|
||||
|
||||
---
|
||||
|
||||
## 关键文件清单
|
||||
|
||||
```
|
||||
src/
|
||||
├── daemon.rs → 主控 (LLM/Tool/Send Consumer, Registry, execute_builtin)
|
||||
├── main.rs → CLI (cmd_tool_dynamic, cmd_tools)
|
||||
├── cli.rs → clap 命令定义
|
||||
├── logger.rs → 三路日志初始化
|
||||
├── tools/
|
||||
│ ├── mod.rs → 模块声明 (6 个)
|
||||
│ ├── spec.rs → ToolSpec + ParamSpec
|
||||
│ ├── parser.rs → parse_dir() YAML 解析 (4 tests)
|
||||
│ ├── registry.rs → Registry (load/list/dispatch)
|
||||
│ ├── executor.rs → execute_loop (spawn→stdout→http/db/result)
|
||||
│ └── approval.rs → ApprovalManager (6位确认码)
|
||||
├── llm/
|
||||
│ ├── types.rs → Message, ToolCall, StreamChunk, ConversationConfig
|
||||
│ ├── conversation.rs → Conversation (run_tool_loop, summarize)
|
||||
│ ├── deepseek.rs → DeepSeek provider (tool_call 流式解析)
|
||||
│ └── provider.rs → LlmProvider trait
|
||||
└── context/
|
||||
├── types.rs → ChatSession, add_tool_result
|
||||
└── builder.rs → build_context (system prompt + 摘要 + 消息)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 工具调用全链路
|
||||
|
||||
```
|
||||
用户消息
|
||||
→ LLM Consumer: 创建 Conversation, 注册 2 元工具
|
||||
→ LLM: 调 query_capabilities (tool_call)
|
||||
→ ToolExecutor → PipelineMessage::ToolCall → 消息队列
|
||||
→ ToolConsumer → execute_builtin → query_capabilities → 返回工具列表
|
||||
→ ToolResult → LLM Consumer → resume
|
||||
|
||||
→ LLM: 调 call_capability(name="weather_now", prompt='{"location":"北京"}')
|
||||
→ ToolConsumer → execute_builtin → call_capability 解包
|
||||
→ registry.dispatch("weather_now", {"location":"北京"})
|
||||
→ executor::execute_loop
|
||||
→ spawn weather --command now
|
||||
→ stdout: {"type":"http","url":"...","desc":"..."}
|
||||
→ 审批(如需要) → reqwest::get → response
|
||||
→ spawn weather --command now (带 response)
|
||||
→ stdout: {"type":"result","content":"北京晴,25°C"}
|
||||
→ ToolResult
|
||||
→ LLM Consumer → resume → 生成回复
|
||||
→ Send Consumer → 微信
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## executor::execute_loop 状态机
|
||||
|
||||
```
|
||||
spawn tool
|
||||
→ stdout JSON parse
|
||||
├─ {"type":"result","content":"..."} → 返回 content
|
||||
├─ {"type":"http","method":"GET","url":"..."}
|
||||
│ → 审批(high risk) → HTTP → 存入 response
|
||||
│ → re-spawn (带 response 字段)
|
||||
└─ {"type":"db","operation":"read_memories",...}
|
||||
→ 审批 → MemoryStore 操作 → 存入 db_response
|
||||
→ re-spawn (带 db_response 字段)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 消息队列路由
|
||||
|
||||
```rust
|
||||
MessageKind::UserMessage | ToolResult | ScheduledTask → LLM Consumer
|
||||
MessageKind::ToolCall | ApprovalRequest → Tool Consumer
|
||||
MessageKind::LLMReply → Send Consumer
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## System Prompt
|
||||
|
||||
```
|
||||
你是 iAs 智能助手,通过微信与用户交流。回复简洁自然。
|
||||
|
||||
你可以通过 function calling 调用两个元工具:
|
||||
1. query_capabilities — 查询所有可用能力工具
|
||||
2. call_capability — 调用能力工具,参数 {"name":"工具名","prompt":"{\"参数\":\"值\"}"}
|
||||
|
||||
流程:收到消息 → query_capabilities → call_capability → 回复
|
||||
```
|
||||
Reference in New Issue
Block a user