範例
儲存庫中的每個範例都在自己的通訊埠上提供 MCP 服務,因此可以同時執行多個。把 MCP 客戶端指向對應的端點,讀取 app://windows,然後呼叫工具即可。
| 範例 | 通訊埠 | 涵蓋內容 |
|---|---|---|
gpui/counter | 8931 | 最小的端對端整合 |
gpui/todo | 8932 | 表格、帶參數的工具、驗證、動態可用性 |
gpui/form | 8933 | 模態鏈收縮可操作集合、遮蔽 |
gpui/dashboard | 8934 | 自我更新的狀態、截斷、async 處理器 |
gpui/multi-window | 8935 | 執行期建立的視窗,各自帶有自己的工具 |
sh
cargo run -p gpui-counter
cargo run -p gpui-todo # {winId}__add {"text": "buy milk"}
cargo run -p gpui-form # {winId}__submit → confirm/cancel on the modal
cargo run -p gpui-dashboard # {winId}__page {"offset": 10}
cargo run -p gpui-multi-window # {winId}__open {"title": "Notes"}範例位於它們自己的 Cargo workspace(examples/)中,與根 workspace 分開,所以建置或測試 Purview 本身時,永遠不會編譯任何 GUI 工具組。
各範例提供的工具
每個工具都命名為 {winId}__{action};目前的 winId 請讀取 app://windows 取得。
| 範例 | 視窗 | 工具 | 參數 |
|---|---|---|---|
| counter | Counter | increment / decrement | — |
| todo | Todos | add | { text: string } |
toggle / remove | { index: number }(從 1 起算;remove 帶 destructive) | ||
clear_done | —(destructive;在沒有已完成項目之前處於隱藏狀態) | ||
| form | Transfer | fill | { recipient?, amount?, account? } |
submit | —(destructive;會開啟模態) | ||
Confirm transfer | confirm / cancel | — | |
| dashboard | Dashboard | page | { offset: number }(read_only) |
pause | — | ||
report | —(read_only、async) | ||
| multi-window | Library | open | { title: string } |
| 每個文件視窗 | append | { line: string } | |
clear | —(destructive) |
留意清單的變化
form 與 multi-window 最值得觀察。在 form 中,提交會開啟模態,Transfer 的工具隨即離開 tools/list,直到模態被關閉。在 multi-window 中,你每開啟一份文件,就會新增它自己的那一對工具。參見免費獲得的協定行為。
你自己整合時的檢查清單
- 在存活環境存在的地方安裝 bridge——對 gpui 而言,就是在
Application::run內部。 - 描述每個視窗,並保留之後會用到的內容區塊控制柄。
- 每個視圖寫一個 sync 方法,並在每次變更之後呼叫它。絕不要在其他任何地方寫入投影。
- 註冊動作,讓它們呼叫與你的按鈕相同的程式碼。
- 用
set_available隱藏不可呼叫的東西,並確保模態能夠被關閉。
關於傳輸格式與規範性要求,請參見協定規範。