← 返回 Skills 市场
sipoon

CodeGraph Index

作者 Sipoon · GitHub ↗ · v0.1.0 · MIT-0
cross-platform ⚠ suspicious
38
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install sipoon-codegraph-index
功能描述
预构建代码符号和调用索引,支持快速查询函数调用关系、项目结构和技术栈,减少大仓库分析开销。
使用说明 (SKILL.md)

codegraph-index

借鉴来源:CodeGraph (colbymchenry/codegraph)

预索引代码知识图谱,用 tree-sitter 建立符号关系、调用图、代码结构,通过 MCP Server 供 Agent 查询。

Benchmark:平均减少 57% token,71% 更少 tool calls,大型仓库效果更明显。


触发条件

满足以下任一场景时激活:

  • 用户要求分析项目结构、架构、技术栈
  • 用户要求查找某个函数/类的调用关系
  • 用户要求了解代码库规模(文件数、语言分布)
  • 工作区代码量超过 200 个源文件
  • 进入大型代码库(>5000 行)需要快速定位

工作流程

Phase A:初始化索引(首次使用)

# 检查 tree-sitter CLI 是否存在
tree-sitter --version

# 如果不存在,用 npm 安装(CodeGraph 兼容 Windows)
npm install -g tree-sitter-cli

# 初始化项目索引
cd \x3C项目目录>
tree-sitter graph

Phase B:索引构建(核心)

对工作区执行 tree-sitter 解析,输出四类信息:

1. 符号表(Symbols)

tree-sitter query --scope source.ts "\x3C项目>" "(function_declaration name: (identifier) @func-name)"

2. 调用图(Call Graph)

tree-sitter query --scope source.ts "\x3C项目>" "(call_expression function: (identifier) @caller)"

3. 导入关系(Imports)

tree-sitter query --scope source.ts "\x3C项目>" "(import_statement)"

4. 文件结构摘要

# 按语言统计文件数
Get-ChildItem -Recurse -Include *.ts,*.tsx,*.js,*.py,*.go,*.rs | Group-Object Extension | Select-Object Name, Count

Phase C:MCP Server 封装(可选,看 OpenClaw MCP 支持情况)

将索引结果封装为 MCP Tool,供 Agent 调用:

Available Tools:
- symbol_search(query) → 返回符号定义位置
- callgraph(function_name) → 返回调用链
- file_structure(lang) → 返回文件树
- import_graph(file) → 返回导入/被导入关系

Phase D:Agent 查询模式

收到代码探索请求时:

  1. 先查索引(symbol_search / callgraph)
  2. 再读文件(只读相关文件,不是全文扫描)
  3. 避免 spawn Explore 子 agent 全文扫描

输出格式

## 代码结构摘要

| 指标 | 值 |
|------|-----|
| 总文件数 | N |
| 总代码行数 | N |
| 主语言 | TypeScript |
| 框架 | Next.js 16 |

## 核心模块

| 模块 | 路径 | 导出符号 |
|------|------|---------|
| auth | src/auth/ | login, logout, refresh |
| api | src/api/ | fetch, post, put |

## 调用关系(简化)

auth/login → api/fetch → /auth/token

与直接 grep/glob 的对比

操作 无索引 有索引
找函数定义 grep 全文 → 扫描所有文件 symbol_search → 直接返回
找调用链 手动追踪 callgraph → 一步返回
大仓库探索 消耗大量 token 减少 57% token

限制与注意事项

  • tree-sitter CLI 不支持 C#(需要 .NET 分析器)
  • Windows 下 tree-sitter graph 输出路径可能用反斜杠
  • 索引只对解析过的语言有效,先确认项目语言是否被支持
  • 索引文件(.tree-sitter/)应该加入 .gitignore

死代码检测(扩展)

利用调用图识别孤立节点(叶子节点无入边 = 潜在死代码):

# 通过 tree-sitter 调用图,找出从未被调用的函数
# 步骤:
# 1. 用 tree-sitter graph 导出调用图(JSON 格式)
# 2. 解析 JSON,找出所有函数定义节点
# 3. 过滤出没有任何调用者的节点

python3 -c "
import json, sys
graph = json.load(open('.tree-sitter/call_graph.json'))
calls = set()
for edge in graph.get('calls', []):
    calls.add(edge['caller'])
    
for node in graph.get('definitions', []):
    if node['name'] not in calls:
        print(f'POTENTIAL DEAD: {node[\"name\]} @ {node[\"file\"]}:{node[\"line\"]}')
"

注意:调用图只能检测静态调用,动态调用(eval、反射)需人工确认。


触发命令

当用户说"分析这个项目"、"找某个函数在哪"、"了解代码结构"时使用。

下一跳(Skill 链式调用)

codegraph-index 是代码探索前置技能,索引完成后按以下路径调用:

codegraph-index → refactoring(发现死代码/结构问题时) → agent-teams(需要多角度代码审查时) → 直接读文件(索引结果足够时,不需要下一步)

下一跳触发条件

  • 索引完成后发现大量死代码 → 调用 \refactoring 做影响分析和重构
  • 索引完成后需要多角度审查 → 调用 gent-teams
  • 索引结果已足够回答用户问题 → 直接输出结果,不需要下一步\r
安全使用建议
Install only if you are comfortable with an agent using tree-sitter to scan broad parts of your codebase and potentially install tree-sitter-cli globally. Prefer using it after an explicit request to build a code index, and review generated .tree-sitter files so derived code structure is not committed or shared accidentally.
能力评估
Purpose & Capability
The stated purpose is to build code symbol, call graph, import, and structure indexes with tree-sitter, and the described commands generally match that code-analysis goal.
Instruction Scope
Activation conditions include broad requests such as analyzing a project, understanding code structure, large workspaces, or repositories over 5000 lines, which could cause indexing to run when the user only expected a lightweight explanation.
Install Mechanism
The workflow tells the agent to run npm install -g tree-sitter-cli if tree-sitter is missing, which modifies the global developer environment and is not gated by an explicit user confirmation in the artifact.
Credentials
Recursive workspace parsing and generated index files are expected for a codegraph tool, but they are broad local-code operations and the artifact does not clearly limit scope, exclude sensitive directories, or require opt-in before indexing.
Persistence & Privilege
The skill suggests creating .tree-sitter index files and adding them to .gitignore; this is disclosed and purpose-aligned, but it is still persistent local derived code data.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install sipoon-codegraph-index
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /sipoon-codegraph-index 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v0.1.0
Initial release of codegraph-index skill: - Introduces fast codebase indexing using tree-sitter for symbol tables, call graphs, import relations, and structure summaries. - Integrates with MCP Server for efficient, tool-based agent queries, reducing token usage and tool calls, especially in large repositories. - Provides clear triggers, workflow phases (indexing, querying), and structured output formats for project analysis. - Supports dead code detection through static call graph analysis. - Lists current limitations, language support considerations, and follow-up skill chain routes.
元数据
Slug sipoon-codegraph-index
版本 0.1.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

CodeGraph Index 是什么?

预构建代码符号和调用索引,支持快速查询函数调用关系、项目结构和技术栈,减少大仓库分析开销。 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 38 次。

如何安装 CodeGraph Index?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install sipoon-codegraph-index」即可一键安装,无需额外配置。

CodeGraph Index 是免费的吗?

是的,CodeGraph Index 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

CodeGraph Index 支持哪些平台?

CodeGraph Index 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 CodeGraph Index?

由 Sipoon(@sipoon)开发并维护,当前版本 v0.1.0。

💬 留言讨论