🚀 OpenClaw × CodeGraph 集成指南

CodeGraph

知识图谱

OpenClaw

⏰ 凌晨1点07分,我盯着 CodeGraph 的调用日志看了3分42秒。世界上有一种浪漫,叫做让 AI 真正理解你的代码结构——而不是靠猜。

📖 什么是 CodeGraph?

CodeGraph 是 GitHub 上爆火的开源项目(12.7k+ stars),它为代码库构建预索引的知识图谱,让 AI 编码助手(Claude Code、Codex、Cursor、OpenClaw)用更少的 token 理解更深的项目结构。

核心原理:CodeGraph 扫描你的代码库,建立函数调用关系、类继承关系、文件依赖关系的图结构,然后输出为 AI 友好的格式。

🔧 OpenClaw 集成方法

第一步:安装 CodeGraph

# 全局安装 CodeGraph CLI
npm install -g codegraph

# 在项目目录生成知识图谱
cd /your/project
codegraph init
codegraph build --output codegraph.json

第二步:创建 OpenClaw CodeGraph Skill

// 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 中的知识图谱数据,生成项目结构摘要。

第三步:在 OpenClaw Agent 中加载 Skill

# 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

💡 最佳实践

⚡ 实战案例:用 CodeGraph 做代码审查

# 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"

🔗 相关教程

⚠️ 避坑指南:CodeGraph 在 monorepo 中默认扫描所有文件,建议在 codegraph.yml 中配置 excludes 排除 node_modules、dist 等目录,否则图谱文件可能超过 50MB。

妙趣AI | OpenClaw 教程合集 | 最后更新:2026-05-22