AnyBot/docs/feature-map/branch.md

38 lines
6.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 支線(/btw)
同一個 Bot 用 /btw 開一個分身,帶著同樣的設定、到這裡為止的對話和記憶同時去做另一件事,做完回報到主聊天;主線正在做事時也能開。
## 子功能
- `branch-btw` 支線(/btw):「/btw 順便查一下…」開一個分身,帶著同樣的設定、到這裡為止的對話和記憶同時去做,做完回報到主聊天。主線正在做事時也能開。
- `branch-card` 支線卡:主聊天裡一張卡,寫狀態(正在做、✓ 做完了、沒有做完、已停止),有「停止支線」「看過程」,支線要你核准的步驟直接在卡上按。
- `branch-report` 回報:做完的結果、沒做完的原因只在它結束時貼到主聊天的那則訊息裡說一次(卡上不再重複);沒做完的訊息是淡底加紅色驚嘆號,下面有「重開這條支線」。
## 使用者怎麼到
- 輸入框打「/」選 /btw(Bot 工作中打「/」只剩 /btw,[chat](chat.md))。
- 技能的 `context: fork`:打「/名稱 …」時改在支線做([skills](skills.md))。
- 手機:一樣打「/btw …」;支線卡在聊天裡(「停止支線」、「看過程(N)」),支線要核准的卡在輸入框上方,標題開頭「支線:」。
## 怎麼操作(verify-anybot)
- Bot:支線裡照常做事;支線不能再開支線,也不能改設定。
前提:
- 基本前提和「手機已登入」(README),開著 Bot 1 的聊天:`$CTL phone click '.row[data-bot="1"]'`。
- **主線忙的時候開**(covers `branch-btw`、`branch-card`、`branch-report`):Bot 跑 20 秒的指令時打「/」,送「/btw 查一下匯率」。指令:`$CTL phone send '忙 [[call shell {"command":"sleep 20"}]]' && $CTL wait-api '/api/state?since=0' '.bots[0].busy == true' && $CTL phone type '#input' '/' && $CTL phone wait-text '/btw' --in '#slash' && $CTL phone wait-gone '#slash [data-sl]' --text '/goal' && $CTL phone send '/btw 查一下匯率' && $CTL phone wait-text '做完了' --in '.talk.branch' && $CTL phone shot branch-done && $CTL phone wait-text '支線的結果已交給正在做事的主線' --in '#msgs' 40 && $CTL model 'select(.user | test("匯率")) | {kind, user: .user[0:40]}'`。看到:工作中「/」只剩 /btw;卡「支線 查一下匯率 ✓ 做完了」和結果;先問一次會不會衝突(沒帶工具的請求「主線正在做…支線要做…」),主線這一輪讀到「〔支線回報〕支線「查一下匯率」做完了…」。
- **主線閒著**(covers `branch-report`):等主線做完再開。指令:`$CTL wait-api '/api/state?since=0' '.bots[0].busy == false' && $CTL phone send '/btw 查天氣' && $CTL phone wait-text '支線「查天氣」做完了' --in '#msgs' && $CTL api GET /api/bots/1 --jq '[.messages[] | select(.text | startswith("支線「查天氣」")) | .text]'`。看到:結果貼在主聊天的一則訊息裡(只說一次)。
- **停止支線**(covers `branch-card`、`branch-report`):指令:`$CTL phone send '/btw 跑 [[call shell {"command":"sleep 30"}]]' && $CTL phone wait '[data-brstop]' && $CTL phone click '[data-brstop]' && $CTL phone wait-gone '[data-brstop]' && $CTL phone wait-text '已停止' --in '#msgs' && $CTL phone wait-text '看過程' --in '#msgs' && $CTL api GET /api/bots/1 --jq '[.messages[] | select(.branch) | [.branch.state, .text[0:12]]]'`。看到:「停止支線」不見、卡寫「已停止」和「看過程(1)」;被停止的不回報(沒有多一則「支線「跑…」」)。
- **卡上核准、改不了設定**(covers `branch-card`、`branch-btw`):支線要登出 ChatGPT(按拒絕);支線改自己的描述。指令:`$CTL phone send '/btw 登出 [[call chatgpt_login {"action":"logout"}]]' && $CTL phone wait-text '支線:' --in '#ask' && $CTL api GET '/api/state?since=0' --jq '.approvals[0] | {bot, target, title}' && $CTL phone click '#ask button[data-a="reject"]' && $CTL phone wait-gone '#ask .ask' && $CTL phone send '/btw 改 [[call update_my_profile {"description":"支線改的"}]]' && $CTL phone wait-text '支線改不了設定' --in '#msgs' && $CTL api GET /api/bots/1/detail --jq .description`。看到:卡標題「支線:驗證小幫手 想要…」(target 是那條支線);「支線改不了設定:回到主線再請它改。」,描述還是空的。
- **重開 AnyBot**(covers `branch-card`):開一條跑 30 秒的支線,重開。指令:`$CTL phone send '/btw 再跑 [[call shell {"command":"sleep 30"}]]' && $CTL phone wait '[data-brstop]' && $CTL restart && $CTL phone open --listed && $CTL phone click '.row[data-bot="1"]' && $CTL wait-api /api/bots/1 '[.messages[] | select(.branch.task | tostring | startswith("再跑")) | .branch.state] == ["stopped"]' && $CTL api GET /api/bots/1 --jq '[.messages[] | select(.branch.task | tostring | startswith("再跑")) | .branch.note]'`。看到:卡寫「已停止」,原因「被停止了」(`ctl` 是正常關;當掉後重開寫「AnyBot 重新啟動,這條支線停了」),沒有回報。
- **沒做完**(covers `branch-report`):主 Bot 換成沒金鑰的 xAI 再開支線。指令:`$CTL phone send '換 [[call update_my_profile {"provider":"xai"}]]' && $CTL phone click '#ask button[data-a="once"]' && $CTL wait-api '/api/state?since=0' '.bots[0].provider == "Xai"' && $CTL phone send '/btw 查匯率' && $CTL phone wait-text '支線「查匯率」沒有做完' --in '#msgs' && $CTL phone shot branch-failed && $CTL api GET /api/bots/1 --jq '[.messages[] | select(.error) | .text]'`。看到:主聊天一則「支線「查匯率」沒有做完:還沒有設定 xAI Grok 的 API 金鑰…」(error);手機的卡也寫原因。
- **桌面**(covers `branch-card`、`branch-report`、`branch-btw`):指令:要開視窗——`$CTL stop && ABV_ALLOW_WINDOW=1 $CTL desktop-shot branch ANYBOT_SHOT_VIEW=btw`。看到:電腦版的支線卡;沒做完那則的紅色驚嘆號和「重開這條支線」;同時最多 3 條、會衝突就等主線、`context: fork` 靠自動測試。
- 自動測試:`cargo test app::branch::`、`cargo test a_fork_skill`、`cargo test a_branch`(做完回報、記憶算主 Bot、會衝突就等、手機的支線卡)。
## 陷阱
- 支線:一個 Bot 同時最多 3 條;支線不能再開支線,也不能改設定;支線記的、做的頁面和例行任務都算主 Bot 的。主線在忙時,先用執行步驟用的模型判斷會不會衝突,會就等主線這一輪做完。在 Bot 的電腦上支線有自己的暫時螢幕和瀏覽器(登入從主線複製),看不到主線開著的網頁;在使用者的電腦上還是一次一個。AnyBot 重新啟動時,沒做完的支線會停掉(「AnyBot 重新啟動,這條支線停了」;正常關掉的寫「被停止了」);被停止的支線不回報。
- 支線的結果在主線還在做事時交給正在做事的主線(聊天裡一行「支線的結果已交給正在做事的主線」),主線閒著才另外貼一則。