AnyBot/docs/restructure/08-tech-stack.md

82 lines
6.3 KiB
Markdown
Raw Permalink 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.

# 08 · 技術選型(全部,現況 + 重構決定)
> 每一層:**現況 → 選它的原因 → 重構時的決定(留/換/鎖)**。重構期間不换技術(行為封箱,05);本文件是之後功能協定的選型依據。
## 1. 工具鏈
| 項 | 現況 | 決定 |
|---|---|---|
| Rust 版本 | `rust-version = "1.95"`(最低;本機實測 rustc 1.98) | 鎖 1.95 最低,CI 用 stable;不追 nightly |
| Edition | 2021(bin + relay) | P1 workspace 化時:new crate 可上 2024(更嚴 imports/`unsafe`),舊 crate 不動,混用 OK |
| Resolver | 現單 crate | workspace 必設 `resolver = "2"` |
| Targets | macOS / Linux / Windows(三 OS 都要過 CI) | 平台 crate 按 OS 拆後,CI 矩陣 3 OS × {build,check,nightly check} 照現況 |
| Release profile | `opt-level = 2` | 不動(動 = 行為變更,走功能協定) |
| Workspace lints | 無 | P1 加 `[workspace.lints]`,`clippy -D warnings` 全 crate 沿用 |
## 2. UI(選型鎖死)
- **eframe 0.37 + egui**(immediate-mode,純 Rust,單一執行檔、無 WebView)——這是 AnyBot 形態的根基(桌面 App + `--serve` 同碼),**不換**。
- `egui_plot 0.38`(正式圖表)、`pulldown-cmark 0.13`(Markdown)、`mermaid-rs-renderer 0.3`(純 Rust 畫 mermaid)、`resvg 0.49`(SVG)、`hayro 0.9` + `pdf-extract 0.12`(PDF)、`calamine 0.38`(xlsx)。
- 字型:思源黑體等 `fonts/` 編進執行檔(build.rs INPUTS)——搬 crate 時資源路徑同 commit(05 風險 1)。
## 3. 網路
| 用途 | 選 | 原因/決定 |
|---|---|---|
| HTTP client | `ureq 3`(blocking + json) | 無 async runtime 依賴;鎖 |
| HTTP server | **自寫 `httpd.rs`**(AnyBot 與 relay 共用) | 刻意不用 axum/hyper:很小、零額外依賴、行為可控。決定:**留**,重構進 `net` crate |
| TLS | `rustls 0.23`(ring、tls12)+ `webpki-roots` + `rustls-native-certs` | 支援自簽憑證(區網 https);鎖 |
| WebSocket | `tungstenite 0.30`(handshake,走 rustls) | 長連線到帳號伺服器;鎖 |
| 區網自簽 | `rcgen 0.14` | lantls;鎖 |
| URL 規則 | `url 2.5.8`(WHATWG) | 瀏覽器安全規則的 host 判定;鎖 |
| 非 UTF-8 網頁 | `encoding_rs 0.8` | Big5/GBK/Shift_JIS;鎖 |
## 4. 資料庫與存檔(沒有 SQL 遷移檔的原因)
| 存什麼 | 選 | 決定 |
|---|---|---|
| 全量狀態 | `state.json`(serde) | 檔案式、人工可讀;格式變動走程式內遷移 + T0 round-trip golden |
| 記憶/文件索引/稽核 | **SQLite via `rusqlite 0.41` bundled**(`memory.db`、`docs.db`、`audit.db`) | bundled = 不需系統 SQLite(含 Firefox Profile Groups 讀)。決定:**留 SQLite bundled,不上 sqlx/diesel**(sqlx 要 async runtime,與全域 blocking 架構相衝;schema 遷移在程式內,已有遷移測試) |
| 工作紀錄 | `oplog/bot-<id>.jsonl`(每行先落盤再做) | 崩潰重建的契約,T0 golden 鎖行序 |
| 密鑰封存 | `ring` + `argon2` + 系統鑰匙圈(vault key) | 鎖 |
| 快取型 | `docs.db` 刪了重建 | 不動 |
## 5. 平台手腳(锁档区,實機測試 + 人類簽名)
- `agent-desktop-core/-macos/-linux/-windows` = **vercel-labs/agent-desktop,tag v1.1.0 鎖死**(AX/AT-SPI/UIA 讀控件,不靠截圖)。決定:pin tag 升級 = 功能協定 + 實機。
- macOS:`objc2`/`objc2-foundation`(自有圖示通知)、`arboard`(貼上打字);Linux:`x11rb 0.13`(xtest/record)、`wayland-client 0.31 + protocols(-wlr/-misc)`、`ashpd 0.13`(portal 遠端桌面/截屏)、`zbus 5`、`libc`(SO_RCVTIMEO 防凍死)、`memmap2`;Windows:`windows 0.61`(Gdi/鍵盤滑鼠/訊息/執行緒)、`arboard`(ConPTY 自 portable-pty)。
- 终端:`portable-pty 0.9` + `vt100 0.16`(真的終端機 + 讀屏)、`crossterm 0.29`(setup)、`ctrlc 3`(停腦先收尾)。
- 音:`cpal 0.18`(麥)、`rodio 0.23`(播放,全格式)。
- 文字/其他:`jieba-rs 0.11` + `zhconv`(中文斷詞)、`unicode-*`、`chrono + chrono-tz + iana-time-zone`、`image 0.25`(選格式)、`zip 9`、`flate2`、`base64`、`des`(VNC 挑戰)、`regex`、`log`、`dirs 7`。
## 6. relay(刻意極薄)
serde/serde_json、ring、argon2(密碼)、base64、flate2、chrono、ureq、tungstenite。**不存密碼**(登入憑證),帳號/裝置表/加密備份/轉發。決定:留薄;收進 workspace members(P1)統一 lock,依賴不動。
## 7. 基礎設施與部署
- **Bot 的電腦(box)**:Docker 容器,Debian + XFCE + Chromium(`box/Dockerfile`),rootless docker;VM 用 Lima(`make` 的 docker host 可 Lima/ssh/伺服器)。
- **自架伺服器**:`install.sh … server`(apt/dnf/yum/pacman/zypper/apk/xbps 都認)、Docker 開機自動(rootless linger)、Caddy(`ANYBOT_PROXY_SECRET` 擋來源)、Tailscale(憑證/tailnet)/區網自簽;邀請碼註冊,擁有者制,雲端大腦上限/資源上限/無 NET_RAW。
- **手動页**:`mobile/index.html` 單檔 + Service Worker(離線快取),PWA。
- 決定:部署形态全部**不動**(重構只搬 Rust 码);`deploy/`、`install.sh` 進 `scripts/`(P8)。
## 8. 建置與 CI
- `build.rs`:版本戳 `v<Cargo版本>-<commit>[-dirty]`,INPUTS = `src box fonts icons mobile Cargo.toml Cargo.lock build.rs`(只有這些動才 dirty)——**workspace 化時 INPUTS 要改成 `crates/` + 資源目錄,P1 驗收項**。
- `Makefile`:all/build(app+relay)/check-rust/test/lint/deps/doctor/install/update(-server/-box)/dist/hands/migrate…——收編進 `scripts/`(P8),目標名不動。
- CI(.gitea):`cargo build`、`cargo test --bin anybot -- --skip live`、`clippy -D`、relay test、windows-check、nightly check;release.yml 發版。決定:拆 crate 後 test 命令逐 crate 化 + `--skip live` 語義不變(鎖檔測試留根)。
- 驗收工具:`verify-anybot` skill + `ctl`(假模型、scratch HOME、無真金鑰)——環境協定不動。
## 9. 新增建議(重構配套,不碰產品行為)
| 工具 | 用途 | 何時 |
|---|---|---|
| `cargo-deny` | license/audit/bans 守 workspace 依賴 | P1 |
| `criterion`(`benches/` 收編 `bench/`) | 效能基線(回合組裝、記憶搜尋) | P8 |
| `cargo geiger`/邊界鎖測試 | crate 反向依賴偵測(05 T4 用 cargo metadata) | P3 起 |
| `rustfmt` per-crate | 現況全專案未强制 fmt(CI 註記一次性 reformat) | P1 一次性 commit |
## 10. 选型红线(永不動的清單,重構不得順手換)
eframe/egui(形态)、ureq+自寫 httpd(無 async 全域)、rusqlite bundled、rustls(ring)、agent-desktop pin tag、tungstenite、sandbox-exec/Landlock、build.rs INPUTS 語義、版本戳格式、`rust-version=1.95`。要動 = 功能協定(commit 附 `已驗證:`)+ 實機 + 人類簽名。