Library 用法

CodeGraph 可以直接嵌進你的應用(例如 Electron main process)
npm 套件 re-export 程式化 API · import 與 require 都可用 · Node 22.5+

嵌入範例

npm 套件 re-export 它的程式化 API,importrequire 都會解析到你自己 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):DatabaseConnectionQueryBuildergetDatabasePathinitGrammars / loadGrammarsForLanguagesFileLock

嵌入需求

類別方法速查

方法用途
CodeGraph.init(root)建新專案 + (可選)初始索引
CodeGraph.open(root)開既有專案(可選 sync)
indexAll()全量索引(帶進度回呼)
sync()增量同步
searchNodes(name)依名稱搜尋節點
getCallers / getCallees誰呼叫它 / 它呼叫誰
getImpactRadius(id, depth)影響半徑
buildContext(task, opts)為某任務組出 AI 要的上下文(markdown/JSON)
watch / unwatch啟動 / 停止檔案監看自動同步
看完這頁你應該能說出:嵌入的兩種語法、Node 22.5+ 的原因(node:sqlite)、以及 watch/unwatch 與 CLI 的 daemon 同步的關係。