AnyBot/docs/multi-device.md

144 lines
11 KiB
Markdown
Raw Permalink Normal View History

# 多台電腦:大腦、手腳、螢幕
帳號裡選一台 AnyBot 當**大腦**,其他電腦和手機都連到它。
| 角色 | 是什麼 | 怎麼設 |
|---|---|---|
| 大腦 | 保存所有 Bot、聊天、記憶和例行任務,由它思考和排程。要一直開著 | 登入帳號就是(預設) |
| 螢幕 | 有視窗的電腦:看到的、改的都是大腦的 Bot;同時也是大腦的手腳 | 設定 › 一般 › 手機與帳號 › 大腦 › 「改用它當大腦」 |
| 手腳 | 讓大腦的 Bot 操作這台:畫面、點擊打字、檔案和指令、這台的瀏覽器 | 「加一台電腦」的那一行,或 `anybot setup` |
| 手機 | 網頁版,打開帳號伺服器的網址 | 登入同一個帳號 |
所有電腦都透過**帳號伺服器**互相連絡。每台 AnyBot 主動連過去、保持一條一直開著的連線,所以電腦不用開放連接埠,也不用固定 IP。
最常見的一組配法:**聊天在你的主力機,Bot 操作另一台電腦**——
1. 要控制的那台跑「做法 A」(當大腦+帳號伺服器,Tailscale 網址最省事)。
2. 主力機打開 AnyBot › 設定 › 手機與帳號 › 登入同一帳號 › 大腦選那台。
3. Bot 建在大腦上(它的 `computer`、指令、瀏覽器都在那台執行);你在主力機和手機聊天、審批。
聊天、記憶、模型金鑰都只在大腦上;主力機和手機只是螢幕和手腳。要控制的電腦不在身邊也能操作(照規則先問你)。
## 一、架帳號伺服器和大腦
### 做法 A:放在伺服器上(推薦,Mac 關機時大腦也還在)
需要:一台 Linux 主機(2 GB 以上記憶體;更小的請先加 swap,見 [install.md](install.md) 的常見問題),以及下面其中一種網址。Docker 沒裝會自動裝。
- **Tailscale**(不用網域):那台裝好 Tailscale,並在 Tailscale 管理頁打開 HTTPS(DNS › HTTPS Certificates)。設定時會出現「用 Tailscale:https://那台.xxx.ts.net」,選它就好;只有你 Tailscale 裡的手機和電腦連得到,不用開任何埠。
- **自己的網域**:A 紀錄指到這台,打開 80 和 443 埠,HTTPS 憑證自動申請。
- **區網 IP**(不用網域,例如 `https://10.0.0.25`):憑證由這台伺服器自己簽、自己更新,只有同一個區網連得到。可以單獨用,也可以和 Tailscale 一起用(在家用 IP、出門用 Tailscale)。這台的 IP 要固定(在路由器設成固定 IP)。
每台手機和電腦要先信任這張憑證一次,之後瀏覽器和 AnyBot 都不會再警告:
- 用瀏覽器打開 `http://那台的IP/anybot-ca.crt` 下載。
- iPhone:安裝描述檔,再到「設定 › 一般 › 關於本機 › 憑證信任設定」打開它。
- Android:設定 › 安全性 › 加密與憑證 › 安裝憑證 › CA 憑證。
- Mac:打開下載的檔案加進「鑰匙圈存取」,點兩下憑證,「信任」設成「永遠信任」。
- Windows:點兩下 › 安裝憑證 › 放進「受信任的根憑證授權單位」。
- Linux(手腳電腦):`sudo cp anybot-ca.crt /usr/local/share/ca-certificates/ && sudo update-ca-certificates`。
```sh
curl -fsSL https://code.30cm.net/daniel.w/AnyBot/raw/branch/main/install.sh | sh -s -- server
```
只問幾件事:網域、帳號、密碼,以及要不要在這台架語音服務。會自動申請 HTTPS 憑證、建立帳號、啟動大腦。完成畫面會告訴你手機和電腦怎麼連。之後在伺服器上執行 `cd ~/AnyBot && make server`,可以選「用現有設定,更新並重新啟動」或「加上/拿掉語音服務」;平常更新用 `make update-server`。
**語音服務**(選配):在這台的 CPU 上用 Whisper([speaches](https://speaches.ai),large-v3-turbo)把說的話轉成文字,每台電腦和手機的麥克風都用它,不用金鑰、不花錢。第一次下載約 1.6 GB 的模型;11 秒的中文約 3.5 秒轉完(12 核測得)。想快一點:設定前 `ANYBOT_VOICE_LISTEN_MODEL=Systran/faster-whisper-small`(約快三倍,偶爾錯一個字)。它只開在伺服器內部,不對外。朗讀(把回答唸出來)它目前不會中文,請在 App 的 設定 › 語音 › 說 選 xAI 之類的服務。不用問題的設定方式加 `ANYBOT_SETUP_VOICE=1`。
記憶: a page to see it, a few lines picked while typing, a local model The memory had grown into a list carried whole, a top-14 recalled by a trigram index and pasted into the system prompt with the clock, and a page that broke its own layout. Nobody could see what went to the model, the prompt cache missed every message, and 「記住」 kept saying the same thing twice. Rebuilt per the spec (docs/memory.md): - Three kinds in memory.db: 記得的事 (fact), 做過的事 (event, a line per finished turn), 整理過的 (rollup). Each keeps pinned, uses, last_used, deleted, superseded_by, span. state.json keeps the facts as the copy that syncs; the page reads SQLite. - Search: jieba words ∪ CJK bigrams in an FTS5 unicode61 table (Traditional text goes through Simplified first), weighted share of query terms hit; plus local vectors, vec = clamp((cos−0.35)/0.35). score = max + 0.15×min, events ×0.9. ≥0.15 suggested, ≥0.5 ticked, ≥0.55 carried when nobody chose (3 at most). - What the model gets: the system prompt has the time zone and the pinned memories only, the same every turn. Each person message gets 〔送出時間〕 and 〔相關記憶〕 from Message.memories, built in group_history into a transient Message.context, so the beginning of a chat reads the same next turn (cache hits) and the event log, skills and audit see only what was typed. A memory already carried in view isn't carried again; each carry counts a use. - Picks travel with the message: Cmd::Send / POST send / sync act "memories" (empty = none; absent = the brain chooses, once, saved). - remember finds the like one (cos ≥ 0.85 or the same words) and asks for `replaces`; the old one is superseded, never carried. A fact forgotten and remembered again comes back. New search_memory (query, kind), with dates; read-only. - Nightly on the brain, no model: events past 30 days fold into a line per week (「11/3–11/9 做過的事(14 件):整理照片 ×3…」, ≤400 chars, like with like by words or vectors), weeks past 180 days into months; the folded go to 最近刪除 for 30 days. Facts are never removed. The page offers duplicates, facts unused for half a year, and what can fold now. - Local model: minishlab/potion-multilingual-128M through m2v.rs (same token ids as model2vec-rs on 13/13 sentences, cos ≥ 0.9999, without tokenizers' 1.1 GB). Converted while downloading: vocab.bin, table.i8 (int8 + row scales), dims.json, 137 MB kept of 530 MB; ANYBOT_HF_BASE picks a mirror (now passed into the server's brain container too). Loaded when first needed (34 ms), let go after 10 idle minutes. The brain loads it; a screen only with a document folder set; hands and phones never. 設定 › 一般 › 語意搜尋: 本機模型/Ollama/關閉; a new engine clears every vector. - Three files, three connections: memory.db (backed up nightly, 7 kept), docs.db (a cache), oplog/audit.db (30 days). An old audit.db hands its memories and documents over once and shrinks (a real one: 172 MB → 16 KB). docs_search scans every chunk now, vectors cached in RAM (it stopped at 8000); FTS optimize + VACUUM daily (260 MB of tombstones over 31 MB of text). - API: POST /api/memory (owner) and /api/sync/memory (mirror). Reads answer on the HTTP thread; writes go through phone_act("memory"). - Screens: a memory dialog (Bot menu 記憶…, Bot settings 打開記憶) with counts, live search, filters, suggestions, per-item kind/date/uses and pin/edit/forget/restore; chips above the composer 300 ms after typing stops, with a token estimate; 「帶了 N 條記憶」 under person messages. The phone has the same as a 記憶 tab, chips and tag. Checked: make test passes (app 297, relay 16), with new tests for the scores and thresholds, Traditional Chinese segmentation, remember's dedupe and replaces, two turns sending the same beginning, no double carry, weekly/monthly folding and the trash, and the audit.db move. Brain idle 37 MB as before, ~230 MB with the model loaded; search on the brain 0.2–0.5 ms, 7–44 ms from the phone page. A deadlock found on the way (stats re-taking the lock) is fixed and tested. Docs: docs/memory.md has the whole design; FEATURES.md, protocol, architecture (zh/en), multi-device and install say what changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha3SYw9JJ9dXwaNvYh4
2026-11-10 23:46:50 +00:00
**記憶的本機語意模型**:大腦第一次啟動時會從 Hugging Face 下載約 530 MB,邊下載邊轉成約 130 MB 存在資料卷裡,之後不用再下載。要用時才載入,載入後大腦多用約 190 MB 記憶體,閒置 10 分鐘就放掉。這台連不到 Hugging Face 時,在 `deploy/server/.env` 加 `ANYBOT_HF_BASE=https://hf-mirror.com` 這類鏡像站再重新啟動。下載完成前,記憶搜尋只用文字比對,一樣能用。詳見 [memory.md](memory.md)。
有發行檔時(推 `v*` tag 後由 Gitea Actions 產生,見 [release.md](release.md)),也可以不在伺服器上編譯:下載 `AnyBot-<版本>-server-<架構>.tar.gz`,解開後 `cd deploy/server && ./setup.sh`。
### 做法 B:先用一台 Mac 當大腦(家裡用 Tailscale)
1. 跑帳號伺服器,並設成開機自動啟動:
```sh
make relay
mkdir -p "$HOME/Library/Application Support/AnyBot/relay"
cp relay/target/release/anybot-relay "$HOME/Library/Application Support/AnyBot/relay/"
cp deploy/mac/com.anybot.relay.plist ~/Library/LaunchAgents/ # 先把裡面的網址改成你的
launchctl load ~/Library/LaunchAgents/com.anybot.relay.plist
```
2. 用 Tailscale 給它 HTTPS 網址:`tailscale serve --bg 8780`(網址像 `https://mac-mini.xxxx.ts.net`)。
3. 打開 AnyBot › 設定 › 一般 › 手機與帳號,填這個網址,建立帳號並登入。這台就是大腦。
只有同一個 Tailscale 網路裡的手機和電腦連得到。
## 二、接上其他電腦
### 有視窗的電腦(螢幕)
打開 AnyBot › 設定 › 一般 › 手機與帳號,登入同一個帳號,在「大腦」那一欄按某一台旁邊的「改用它當大腦」。
- 這台原本的 Bot 會留著不刪,可以按「把這台的 Bot 搬過去」複製到大腦(連同聊天、金鑰、技能和連接器;已經在大腦上的不會重複搬)。
- 隨時可以按「改回用這台自己的 Bot」。
- 選大腦時也會打開這台的「裝置連結」:大腦的 Bot 可以用這台當手腳,每個動作仍照大腦的規則先問你。
### 沒有螢幕的 Linux(手腳)
1. 在手機 AnyBot 右上「⋯ › 加一台電腦」,或電腦 AnyBot 的 設定 › 一般 › 手機與帳號 › 大腦 › 加一台電腦 › 產生配對碼。
2. 會出現一行(10 分鐘內有效,只能用一次):
```sh
curl -fsSL https://code.30cm.net/daniel.w/AnyBot/raw/branch/main/install.sh | sh -s -- hands https://anybot.example.com 469615
```
3. 在那台電腦的終端機貼上這一行執行。
它會:還沒裝 AnyBot 就先裝好(系統套件、Rust、編譯,第一次約 5–15 分鐘;已經裝好就直接下一步)、加入帳號(不用密碼)、只讓產生配對碼的那個大腦使用、設成開機自動執行(systemd 使用者服務 `anybot`)、沒有螢幕時開一個 1440×900 的虛擬螢幕,最後確認大腦看得到它。大腦約一分鐘內就會把它列進 Bot 可以用的電腦。
虛擬螢幕(Xvfb)、視窗管理員和輔助使用服務會一起裝好,Bot 看得到這台的畫面。
這台登入了桌面(例如 GNOME)也一樣:Bot 用的是 AnyBot 自己開的虛擬螢幕,不會動到你的桌面,也不用有人在那台前面按「允許分享螢幕」。想讓 Bot 操作那台登入中的桌面,加 `--real-screen`。
更新:在那台再執行一次安裝的那一行(或 `cd ~/AnyBot && ./install.sh hands`),會換成新版並重新啟動,不用再配對。
只打 `anybot setup` 會出現一個畫面:填網址和配對碼(或改用帳號密碼)。給程式用:
```sh
ANYBOT_ACCOUNT_PASSWORD=… anybot setup --server https://… --user 帳號 [--brain-name 雲端大腦] [--no-service]
```
登出後也要繼續跑:`sudo loginctl enable-linger $USER`(完成畫面會提醒)。
### 手機
打開帳號伺服器的網址,登入。可以「加入主畫面」當 App 用、開通知。帳號裡有螢幕電腦時,手機會直接打開它們用的大腦。
## 三、在裝置之間傳檔案和文字
接好之後,帳號裡任何兩台都能互傳,不用經過聊天:
- 電腦:側邊欄底部名字右邊的「↑」›「傳送到其他裝置」,選裝置,把檔案拖進去(或貼一段文字)。
- 手機:左上頭像 ›「傳送到其他裝置」。
檔案會出現在對方的「下載/AnyBot」資料夾並跳通知;文字會直接在對方的剪貼簿裡;傳給手機的,在手機同一個畫面按「下載」。對方沒開機也可以先傳,大腦會留 7 天,它一上線就送到。每一台都要更新到有這個功能的版本(`make update`;伺服器 `make update-server`)。細節見 [FEATURES.md](../FEATURES.md) 第 36 節。
## 四、連不上時
畫面上方會說是哪一段連不到:
| 看到 | 意思 | 怎麼辦 |
|---|---|---|
| 這台電腦沒有連上網路 | 這台斷網 | 檢查 Wi-Fi 或網路線 |
| 連不到帳號伺服器:伺服器沒有開 | 帳號伺服器沒在跑 | 伺服器上 `docker compose ps`;Mac 上看 `launchctl list | grep anybot` |
| 帳號伺服器暫時無法使用 | 正在重開或 HTTPS 憑證還沒好 | 等一下,會自動重連 |
| 大腦目前離線 | 大腦關機、睡眠或 AnyBot 沒開 | 打開大腦;這段時間還能看之前的對話 |
| 大腦收到了,但太久沒有回應 | 大腦太忙或網路很慢 | 稍後再試 |
| 這台電腦的帳號登入失效了 | 帳號被登出或金鑰換了 | 重新登入 |
| 這台電腦只讓大腦「…」使用 | 別台想用這台當手腳 | 正常:配對時就限定了 |
大腦連不到時:之前的對話還能看,不能傳訊息;連回來會自動恢復,不用重開。
**忘記帳號密碼**:在帳號伺服器那台,先停掉它,再執行(輸入兩次新密碼,畫面不顯示):
```sh
anybot-relay --data <資料夾> --reset-password <帳號>
```
舊的加密備份是用舊密碼鎖的,改完之後在電腦上按一次「立刻備份」。
## 五、安全
- 模型金鑰、訊息管道的 Token、連接器的密碼只在大腦上。螢幕電腦看到的是「已設定,在大腦上」,在螢幕上改設定也不會洗掉真的金鑰。
- 手腳只接受帳號裡的 AnyBot,用配對碼加入的只接受那一個大腦。
- 每個動作照大腦的規則先問你(或照你設的「一律允許」)。
- 配對碼 10 分鐘有效、只能用一次,連續猜錯會被暫停 5 分鐘。
- 技術細節見 [architecture.md](architecture.md) 和 [protocol.md](protocol.md)。