CodeGraph
知识图谱
OpenClaw
CodeGraph 是 GitHub 上爆火的开源项目(12.7k+ stars),它为代码库构建预索引的知识图谱,让 AI 编码助手(Claude Code、Codex、Cursor、OpenClaw)用更少的 token 理解更深的项目结构。
核心原理:CodeGraph 扫描你的代码库,建立函数调用关系、类继承关系、文件依赖关系的图结构,然后输出为 AI 友好的格式。
# 全局安装 CodeGraph CLI
npm install -g codegraph
# 在项目目录生成知识图谱
cd /your/project
codegraph init
codegraph build --output codegraph.json
// codegraph-skill.yaml
name: codegraph-analyzer
version: 1.0.0
description: 使用 CodeGraph 知识图谱进行代码分析
tools:
- builtin: read
- builtin: exec
instructions: |
你是一个基于 CodeGraph 知识图谱的代码分析专家。
当用户询问代码结构时:
1. 先读取 codegraph.json 获取项目结构概览
2. 根据图谱中的依赖关系分析影响范围
3. 提供精准的代码修改建议
4. 引用具体的函数和类关系
hooks:
onStart: |
读取 codegraph.json 中的知识图谱数据,生成项目结构摘要。
# sessions_spawn 时加载 skill
openclaw session start --agent my-agent \
--skill ./codegraph-skill.yaml \
--context "项目知识图谱已加载至 codegraph.json"
# 或者在 Agent 配置中永久集成
openclaw agent configure my-agent \
--add-skill codegraph-analyzer
# OpenClaw 工作流:基于图谱的代码审查
workflow:
steps:
- name: "加载图谱"
action: read
path: "codegraph.json"
- name: "分析变更影响"
action: agent_skill
skill: codegraph-analyzer
input: "分析 git diff 中变更的函数对下游模块的影响"
- name: "生成审查报告"
action: write
path: "review-report.md"
妙趣AI | OpenClaw 教程合集 | 最后更新:2026-05-22