AnyBot/docs/restructure/01-target-structure.md

70 lines
4.9 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.

# 01 · 目標專案結構
## 對那份提案(`my-large-project`)的評估
提案的**骨架完全正確**,可直接採用:workspace 根 + `crates/` 分層 + 全域 `Cargo.lock` + `.cargo/config.toml` + 根層 `tests/`/`benches/`。要調整的是**層的名字與內容**,因為那份模板是為 web 後端(Axum + SQLx + migrations)寫的,AnyBot 是桌面 App + relay + mobile:
| 提案項目 | 採用? | AnyBot 化 |
|---|---|---|
| workspace 根 Cargo.toml / Cargo.lock | ✅ | 現況 relay 已是獨立 crate,應收進 workspace members,統一 lock |
| `.cargo/config.toml` | ✅ | 放 linker(sold/lld)與 target 快取設定 |
| `crates/domain` | ✅ 概念 | 改名 `core`:Bot 邏輯、tools 契約、policy、workflow——零 UI、零平台依赖 |
| `crates/infrastructure` | ✅ 概念 | AnyBot 的「基礎設施」是 OS 能力與網路,拆成 `platform-*` + `net` + `browser` 數個 crate |
| `crates/shared` | ✅ | i18n、lock、logfile、store、testkit |
| `crates/api-server` | ✅ 概念 | AnyBot 入口是 eframe GUI,改名 `app-bin`;提案的「main.rs 極簡 + lib.rs 暴露 app 建構式」照做,整合測試就能直接呼叫 |
| `routes.rs 取代 mod.rs` | ➖ | 不適用(沒有 HTTP 路由;httpd 的路由在 net crate 內照用) |
| `tests/` `benches/` | ✅ | `benches/` 收編現有 `bench/` |
| `migrations/` | ➖ | 不需要(無 SQL 資料庫;vault/memstore 是檔案格式,版本遷移寫進 `core` 的測試) |
| `scripts/` | ✅ | 收編 `install.sh`、`Makefile` 周邊、deploy 腳本 |
**提案缺的、AnyBot 需要的兩件事:**
1. **平台分層**:mac/linux/windows 能力(axread/x11/wl/portal/mic/win…)必須按 OS 分 crate,`cfg` 才不用散在 103 個檔裡。
2. **UI 與核心分離**:`app/*_ui.rs` 全進 `ui` crate,經 trait 呼叫 core,這是「agent 改到極限」的主因——現在 UI 和邏輯互相穿透。
## 目標結構
```
anybot/
├── Cargo.toml # [workspace] members,統一 version/lints profile
├── Cargo.lock
├── .cargo/config.toml
├── crates/
│ ├── shared/ # i18n i18n_table lock logfile store stats usage version
│ ├── core/ # agent brains tools policy gate claims triggers intents
│ │ ├── src/ # workflow routines schedule goals plan memory memstore
│ │ │ ├── lib.rs # facts evidence checkpoint teach skills mcp mcp_oauth
│ │ │ └── ... # 無 UI、無 eframe、無 OS API
│ ├── platform-mac/ # axread axwrite macapps mic computer(desk) 的 mac 實作
│ ├── platform-linux/ # x11 wl portal desk landlock
│ ├── platform-win/ # win
│ ├── browser/ # browser botbrowser preview web wire
│ ├── net/ # httpd net egress tunnel remote relay-client lantls bidi
│ ├── channels/ # channels phone notify mention embed share publish attach
│ ├── relay/ # 現 relay/ 收進 workspace(anybot-relay)
│ ├── ui/ # app/*_ui.rs + theme + layout(含 layout_audit)
│ └── app-bin/ # main.rs(極簡)+ lib.rs(暴露 App::build)
├── tests/ # 跨 crate E2E(啟動 app-bin lib、跑腳本化 scenario)
├── benches/ # 收編 bench/
├── docs/feature-map/ # 不動,為重構的驗收來源
├── scripts/
└── mobile/ site/ deploy/ box/ # 非 Rust 資產,位置不動(根目錄杂物清進 assets/)
```
## 現有模組 → crate 對照(103 個頂層檔)
| crate | 收走的現有模組 |
|---|---|
| shared | shared i18n i18n_table lock logfile store stats usage version icons theme |
| core | agent agent/* brains tools policy gate claims triggers intents workflow routines schedule goal plan checkpoints facts evidence teach skills memory memstore docindex find marks outputs mcp mcp_oauth providers model m2v smart speech voice chatgpt doctor garden always hooks secrets vault_store |
| platform-mac | axread axwrite macapps mic computer(desk 共用部分進 core trait) |
| platform-linux | x11 wl portal desk landlock |
| platform-win | win |
| browser | browser botbrowser preview web wire embed(pageview 渲染) |
| net | httpd net egress tunnel remote relay-client lantls bidi phonefit p2p 面 |
| channels | channels phone notify mention attach share publish drops avatar desktop(螢幕串流端) |
| ui | app/*(41 個 *_ui.rs)+ app.rs 拆分 + layout_audit featuremap(驗證面) |
| app-bin | appctl app.rs 啟動段 setup profile pages pages_ui main 生命週期 |
> 搬移順序:shared → core(leaf 先行)→ platform → net → browser → channels → ui → app-bin。
> 每個 crate 的 `Cargo.toml` 依賴只允許指向**更下层**;違規由測試 `crate_boundaries_are_locked` 擋(見 05)。