Skip to content

範例

儲存庫中的每個範例都在自己的通訊埠上提供 MCP 服務,因此可以同時執行多個。把 MCP 客戶端指向對應的端點,讀取 app://windows,然後呼叫工具即可。

範例通訊埠涵蓋內容
gpui/counter8931最小的端對端整合
gpui/todo8932表格、帶參數的工具、驗證、動態可用性
gpui/form8933模態鏈收縮可操作集合、遮蔽
gpui/dashboard8934自我更新的狀態、截斷、async 處理器
gpui/multi-window8935執行期建立的視窗,各自帶有自己的工具
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 取得。

範例視窗工具參數
counterCounterincrement / decrement
todoTodosadd{ text: string }
toggle / remove{ index: number }(從 1 起算;removedestructive
clear_done—(destructive;在沒有已完成項目之前處於隱藏狀態)
formTransferfill{ recipient?, amount?, account? }
submit—(destructive;會開啟模態)
Confirm transferconfirm / cancel
dashboardDashboardpage{ offset: number }read_only
pause
report—(read_onlyasync
multi-windowLibraryopen{ title: string }
每個文件視窗append{ line: string }
clear—(destructive

留意清單的變化

formmulti-window 最值得觀察。在 form 中,提交會開啟模態,Transfer 的工具隨即離開 tools/list,直到模態被關閉。在 multi-window 中,你每開啟一份文件,就會新增它自己的那一對工具。參見免費獲得的協定行為

你自己整合時的檢查清單

  1. 在存活環境存在的地方安裝 bridge——對 gpui 而言,就是在 Application::run 內部。
  2. 描述每個視窗,並保留之後會用到的內容區塊控制柄。
  3. 每個視圖寫一個 sync 方法,並在每次變更之後呼叫它。絕不要在其他任何地方寫入投影。
  4. 註冊動作,讓它們呼叫與你的按鈕相同的程式碼。
  5. set_available 隱藏不可呼叫的東西,並確保模態能夠被關閉。

關於傳輸格式與規範性要求,請參見協定規範