npm 套件 re-export 它的程式化 API,import 和 require 都會解析到你自己 process 裡的 CodeGraph class:
import CodeGraph from '@colbymchenry/codegraph';
// CommonJS 也行:
// const { CodeGraph } = require('@colbymchenry/codegraph');
const cg = await CodeGraph.init('/path/to/project');
// 或:const cg = await CodeGraph.open('/path/to/project');
await cg.indexAll({
onProgress: (p) => console.log(`${p.phase}: ${p.current}/${p.total}`)
});
const results = cg.searchNodes('UserService');
const callers = cg.getCallers(results[0].node.id);
const context = await cg.buildContext('fix login bug', { maxNodes: 20, includeCode: true, format: 'markdown' });
const impact = cg.getImpactRadius(results[0].node.id, 2);
cg.watch(); // 檔案變動自動同步
cg.unwatch(); // 停止 watch
cg.close();
更底層的積木也從同一入口 export(給直接驅動圖的 caller):DatabaseConnection、QueryBuilder、getDatabasePath、initGrammars / loadGrammarsForLanguages、FileLock。
npm i @colbymchenry/codegraph),這樣配對的 per-platform 套件(載編譯好的 library 與依賴)會一起被 fetch。node:sqlite;Electron 的 bundled Node ≥22.5 也算)。CLI 與 MCP server 不受影響——它們跑在 self-contained bundle runtime。@types/node 並設 skipLibCheck: true(常見預設)。| 方法 | 用途 |
|---|---|
CodeGraph.init(root) | 建新專案 + (可選)初始索引 |
CodeGraph.open(root) | 開既有專案(可選 sync) |
indexAll() | 全量索引(帶進度回呼) |
sync() | 增量同步 |
searchNodes(name) | 依名稱搜尋節點 |
getCallers / getCallees | 誰呼叫它 / 它呼叫誰 |
getImpactRadius(id, depth) | 影響半徑 |
buildContext(task, opts) | 為某任務組出 AI 要的上下文(markdown/JSON) |
watch / unwatch | 啟動 / 停止檔案監看自動同步 |