src/installer — 多 agent 安裝器

新增一個 agent target = 一個新檔案 + registry 一列
檔案:src/installer/ · targets/registry.ts · 手刻 TOML serializer

大方向

安裝器是 codegraph install(與裸 codegraph / npx @colbymchenry/codegraph)的入口。架構刻意做成可擴充:targets/registry.ts 列所有 agent;targets/types.ts 定義 AgentTarget interface——加第 5 個 agent = targets/ 一個新檔 + registry 一個 entry。每個 target 自己負責它的 config 檔位置與 MCP-server JSON/TOML/JSONC 寫法。


AgentTarget / registry.ts src/installer/agent 支援清單與介面。

目前的 targets:claude.ts、cursor.ts、codex.ts、opencode.ts,加上 Hermes / Gemini CLI / Antigravity / Kiro / Copilot 三 surface。每個 target 實現偵測(agent 是否安裝)、config 路徑、MCP server 設定寫入與移除。codegraph install 的「問哪些 agent」就是列 registry 自動偵測。

教學重點:安裝器只接 agent、不索引程式——CLI 安裝、agent 設定、專案索引是三個獨立階段(對應 install.sh / codegraph install / codegraph init)。
targets/toml.ts src/installer/手刻 TOML serializer,只作用於 [mcp_servers.codegraph]。

Codex 用 TOML。為了不破壞使用者原本的設定,這個 serializer 只處理 [mcp_servers.codegraph],sibling tables 與 [[array_of_tables]] 原樣保留。不引入新 dependency。

opencode target(jsonc) src/installer/targets/opencode.tsopencode 讀 opencode.jsonc,用 jsonc-parser 做 surgical 編輯。

opencode 預設讀 opencode.jsonc:偏好既有 .jsonc、fallback .json、greenfield 建 .jsonc。編輯用 jsonc-parser 精準改——使用者的註解與排版在 install / re-install / uninstall round-trip 後存活。這是「絕不亂開實驗性 flag、不啟用 plugin、不信任第三方指令」原則的體現。

instructions 段落(marker-fenced) src/installer/在 CLAUDE.md / AGENTS.md / GEMINI.md 寫 marker-fenced 段落。

主 agent 收到 MCP initialize 的指引,但 sub-agent 與非 MCP harness 收不到——所以安裝器在每個 agent 的 instructions 檔寫入短 marker-fenced 段落,指向 codegraph explore CLI 對等指令。`codegraph uninstall` 可乾淨移除。Copilot target 目前不寫 instructions(MCP 指引已足夠)。

本站實測對照

本站是 opencode 使用情境——安裝 codegraph 時,opencode target 就是寫入 opencode.jsonc 的 mcp_servers.codegraph 區塊。你也可以用 codegraph install --print-config opencode 只印設定不寫檔。

看完這頁你應該能說出:新增 agent 的檔案對應、Codex 的 TOML 為何手刻、opencode 的 jsonc 編輯為何要 surgical、以及 instructions 段落為什麼存在(sub-agent 看不到 MCP 指引)。

🔍 進階真實情境 Worked Example:新增一個全新 agent target

架構頁說「新增 agent = 一個新檔案 + registry 一列」。這裡把這句話變成完整實作:你的團隊用一個內部 agent harness,要把它加進 codegraph install 的支援清單。

新增 target 工作流
# ① 實作 AgentTarget interface(src/installer/targets/myagent.ts)
#    - detect(): agent 是否已安裝
#    - configPath / configFormat: 設定檔位置與格式(jsonc/json/toml)
#    - install(): 寫入 MCP server 設定 + instructions 段落
#    - uninstall(): 反向移除(保留使用者原有內容)

# ② 註冊到 targets/registry.ts
#    import { myagentTarget } from './myagent';
#    export const targets = [..., myagentTarget];

# ③ 驗證偵測
codegraph install    # 清單應出現 myagent

# ④ 驗證 round-trip(安裝 → 重裝 → 反安裝)
codegraph install --agent myagent
codegraph install --agent myagent    # 重裝:使用者註解與排版要存活
codegraph uninstall --agent myagent  # 乾淨移除,不留殘留

# ⑤ 驗證 instructions 段落有寫進 AGENTS.md / CLAUDE.md

為什麼走這條路徑:介面(AgentTarget)把「支援一個 agent」所需的全部行為收斂成五個職責——新增者不用碰其它 target 的邏輯。重點驗證在 round-trip:安裝器最容易被罵的就是「動了我的設定檔」,所以 surgical 編輯(jsonc-parser / 手刻 TOML)與 round-trip 存活是驗收關鍵。

🔬 深入原理擴充

內部架構:三個被誤解的邊界。① 安裝器不索引程式——CLI 安裝、agent 設定、專案索引是三個獨立階段(install.sh / codegraph install / codegraph init),別混在一起。② Codex 的 TOML 用手刻 serializer——只處理 [mcp_servers.codegraph],sibling tables 與 [[array_of_tables]] 原樣保留,不引入新 dependency。③ opencode 的 jsonc 用 jsonc-parser 做 surgical 編輯——使用者的註解與排版在 re-install/uninstall round-trip 後存活。另有 marker-fenced instructions 段落:主 agent 收到 MCP initialize 指引,但 sub-agent 與非 MCP harness 收不到,所以要寫進 instructions 檔。

大家以為分析正確但其實有誤:以為安裝器會順便索引專案。實際它只接 agent——裝完 codegraph install 後還要在專案裡跑 codegraph init 才有索引。另一個誤解:以為手刻 TOML serializer 是「過度工程、省事引個套件就好」。實際「不破壞使用者設定」在 TOML 上極難——尤其 [[array_of_tables]] 與註解的位置——手刻是為了「只動我們該動的那一格」的承諾。

🩺 診斷式疑難排解表

症狀可能原因解決方案
install 清單沒列某 agenttarget 的 detect() 回 false檢查偵測邏輯(agent 是否已安裝、版本偵測)
重裝後使用者註解消失用了整檔覆寫而非 surgical 編輯改用 jsonc-parser / 手刻 TOML,只動 codegraph 區塊
uninstall 後殘留段落marker-fenced 格式不一致確認 marker 前後綴與 install 時寫入的完全一致
設定被寫進錯誤檔案configPath 邏輯錯檢查該 target 的 config 路徑解析(環境變數、家目錄)
裝完 agent 不用 codegraphinstructions 段落沒寫或格式不被讀確認有寫入 marker-fenced 段落且被該 agent 讀取

🧩 進階挑戰題

  1. 「新增 agent = 一個新檔案 + registry 一列」的擴充性設計,是否足以處理「同一個 agent 有多種 surface(如 Copilot 的三種 surface)」?若要支援,interface 需要怎麼擴充?
  2. 為什麼 Codex 的 TOML 要手刻 serializer 而不是引一個 TOML 套件?「不破壞使用者設定」在 [[array_of_tables]] 上有多難?
  3. 設計一個新 agent target 的驗收清單:偵測、config 路徑、寫入、instructions、uninstall round-trip——每一項的驗證方法與通過標準。