AnyBot/docs/restructure/04-sequence-diagrams.md

116 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

# 04 · 時序圖(主要流程)
> 重構验收基準:搬移後每張圖的參與者只做 crate 改名,訊息序列必須逐條不變。
## 1. 聊天回合(含工具與核准)
```mermaid
sequenceDiagram
participant U as UI (chat_ui)
participant A as agent.rs 回合
participant O as oplog
participant P as providers
participant G as policy/gate
participant T as tools
participant M as memory
U->>A: 送出訊息 (Bot, Message)
A->>M: 組系統指示 (釘選記憶+facts)
A->>O: 回合開始 落盤
loop 每一步 (StepPlan)
A->>P: 送模型(串流)
P-->>A: tool_call / 文字
A->>G: 該動作:直接做/先問/拒絕?
alt 先問
G-->>U: 核准卡片
U->>G: 批准/拒絕
end
A->>O: 步驟開始 先寫磁碟再做
A->>T: 執行工具
T-->>A: 結果
end
A->>M: 記「做過的事」
A-->>U: 摘要/長對話壓縮
```
**重構注意**:`tools.rs 的 specs`、核准關卡的文字,是鎖檔契約(`tool_specs_are_locked`、`approval_decisions_are_characterized`)。core 搬走 tools 時,鎖檔 JSON 同 commit 跟著走,輸出 byte 級不變。
## 2. 崩潰復原(開機)
```mermaid
sequenceDiagram
participant B as app-bin 啟動
participant L as lock.rs
participant S as store (state.json)
participant O as oplog
participant U as UI
B->>L: 取得 anybot.lock(單一實例)
B->>S: 讀 state.json
B->>O: 掃描未完成回合
O-->>B: 聊天重建 + 「繼續」卡片
B->>U: 渲染(接上鏡像同步)
```
## 3. 手機 → 大腦(經 relay)
```mermaid
sequenceDiagram
participant M as mobile/index.html
participant R as anybot-relay
participant T as tunnel/wire
participant B as 大腦 AnyBot (remote.rs)
M->>R: 登入/請求 (HTTP)
R->>T: WebSocket 推給裝置
T->>B: /api/device/run 請求
B-->>R: 結果(經 wire)
R-->>M: 回應
```
## 4. Drops(裝置間傳檔,大腦中繼)
```mermaid
sequenceDiagram
participant S as 來源電腦
participant B as 大腦 (drops/)
participant D as 目的電腦
participant M as 手機
S->>B: drop 分段 (2MB/段)
B->>D: /api/device/run drop_put 逐段推
D-->>B: 進度(斷線從對方已有位置續傳)
Note over B: 離線 → 等上線,保留 7 天
B->>M: 給手機的檔留在大腦等下載
```
## 5. 瀏覽器操作(Bot 用电脑)
```mermaid
sequenceDiagram
participant A as agent 回合
participant T as tools (computer/browser)
participant G as gate 核准
participant BR as botbrowser (CDP)
participant CB as cloudbox (Docker)
A->>G: 開瀏覽器/點按 → 先問
G-->>A: 批准
A->>CB: 起 Bot 的電腦
A->>BR: DevTools 協定 導航/讀取/輸入
BR-->>A: 畫面/DOM 結果
A->>T: 截圖存 attachments
```
## 6. 例行任務/觸發(routines)
```mermaid
sequenceDiagram
participant C as schedule/clock
participant R as routines/triggers
participant A as agent 回合
participant N as notify/channels
C->>R: 時間到/事件觸發
R->>A: 起回合(無使用者訊息)
A-->>N: 完成 → 推通道/通知
```
## 驗收方式
每張圖对应一組 `tests/` 腳本化 scenario(見 05 的 T3):啟動 `app-bin` 的 lib(不開視窗,`--serve` 模式),照圖走一遍,比對 oplog 落盤順序與聊天重建結果。