codegraph-py 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codegraph_py-1.0.0/PKG-INFO +238 -0
- codegraph_py-1.0.0/README.md +200 -0
- codegraph_py-1.0.0/codegraph/__init__.py +12 -0
- codegraph_py-1.0.0/codegraph/__main__.py +6 -0
- codegraph_py-1.0.0/codegraph/cli.py +911 -0
- codegraph_py-1.0.0/codegraph/codegraph.py +618 -0
- codegraph_py-1.0.0/codegraph/context/__init__.py +216 -0
- codegraph_py-1.0.0/codegraph/db/__init__.py +11 -0
- codegraph_py-1.0.0/codegraph/db/connection.py +163 -0
- codegraph_py-1.0.0/codegraph/db/queries.py +752 -0
- codegraph_py-1.0.0/codegraph/db/schema.sql +151 -0
- codegraph_py-1.0.0/codegraph/directory.py +160 -0
- codegraph_py-1.0.0/codegraph/errors.py +101 -0
- codegraph_py-1.0.0/codegraph/extraction/__init__.py +956 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/__init__.py +64 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/base.py +132 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/c_cfg.py +53 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/cpp_cfg.py +60 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/dart_cfg.py +53 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/go_cfg.py +70 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/java_cfg.py +62 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/javascript_cfg.py +84 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/kotlin_cfg.py +52 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/lua_cfg.py +65 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/python_cfg.py +75 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/ruby_cfg.py +48 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/rust_cfg.py +68 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/scala_cfg.py +59 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/swift_cfg.py +64 -0
- codegraph_py-1.0.0/codegraph/extraction/languages/typescript_cfg.py +89 -0
- codegraph_py-1.0.0/codegraph/extraction/tree_sitter_extractor.py +689 -0
- codegraph_py-1.0.0/codegraph/graph/__init__.py +685 -0
- codegraph_py-1.0.0/codegraph/installer/__init__.py +6 -0
- codegraph_py-1.0.0/codegraph/mcp/__init__.py +668 -0
- codegraph_py-1.0.0/codegraph/project_config.py +191 -0
- codegraph_py-1.0.0/codegraph/resolution/__init__.py +337 -0
- codegraph_py-1.0.0/codegraph/search/__init__.py +653 -0
- codegraph_py-1.0.0/codegraph/sync/__init__.py +204 -0
- codegraph_py-1.0.0/codegraph/types.py +334 -0
- codegraph_py-1.0.0/codegraph/ui/__init__.py +11 -0
- codegraph_py-1.0.0/codegraph/utils.py +218 -0
- codegraph_py-1.0.0/codegraph_py.egg-info/PKG-INFO +238 -0
- codegraph_py-1.0.0/codegraph_py.egg-info/SOURCES.txt +49 -0
- codegraph_py-1.0.0/codegraph_py.egg-info/dependency_links.txt +1 -0
- codegraph_py-1.0.0/codegraph_py.egg-info/entry_points.txt +2 -0
- codegraph_py-1.0.0/codegraph_py.egg-info/requires.txt +16 -0
- codegraph_py-1.0.0/codegraph_py.egg-info/top_level.txt +1 -0
- codegraph_py-1.0.0/pyproject.toml +66 -0
- codegraph_py-1.0.0/setup.cfg +4 -0
- codegraph_py-1.0.0/tests/test_codegraph.py +347 -0
- codegraph_py-1.0.0/tests/test_stress.py +549 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: codegraph-py
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Supercharge AI coding agents with semantic code intelligence — surgical context, fewer tool calls, faster answers. 100% local.
|
|
5
|
+
Author-email: mading <martin98@bu.edu>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/martin98-afk/codegraph-py
|
|
8
|
+
Project-URL: Source, https://github.com/martin98-afk/codegraph-py
|
|
9
|
+
Project-URL: Issues, https://github.com/martin98-afk/codegraph-py/issues
|
|
10
|
+
Keywords: code-intelligence,knowledge-graph,static-analysis,mcp,ai-agent,tree-sitter
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
Requires-Dist: click>=8.0
|
|
26
|
+
Provides-Extra: ts
|
|
27
|
+
Requires-Dist: tree-sitter>=0.21; extra == "ts"
|
|
28
|
+
Requires-Dist: tree-sitter-python>=0.21; extra == "ts"
|
|
29
|
+
Requires-Dist: tree-sitter-javascript>=0.21; extra == "ts"
|
|
30
|
+
Requires-Dist: tree-sitter-typescript>=0.21; extra == "ts"
|
|
31
|
+
Requires-Dist: tree-sitter-java>=0.21; extra == "ts"
|
|
32
|
+
Requires-Dist: tree-sitter-go>=0.21; extra == "ts"
|
|
33
|
+
Requires-Dist: tree-sitter-rust>=0.21; extra == "ts"
|
|
34
|
+
Provides-Extra: watch
|
|
35
|
+
Requires-Dist: watchfiles>=0.20.0; extra == "watch"
|
|
36
|
+
Provides-Extra: all
|
|
37
|
+
Requires-Dist: codegraph-py[ts,watch]; extra == "all"
|
|
38
|
+
|
|
39
|
+
# codegraph-py
|
|
40
|
+
|
|
41
|
+
**Python 版 CodeGraph** — 基于知识图谱的语义代码智能引擎,为 AI 编程助手提供精准的代码上下文。
|
|
42
|
+
|
|
43
|
+
> 对代码库建立预索引的知识图谱。AI Agent 直接查询图结构而非逐文件扫描 —— 精准上下文、更少工具调用、更快回答。
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 特性
|
|
48
|
+
|
|
49
|
+
- 🔍 **多语言 AST 解析** — 基于 tree-sitter 的符号级解析(Python/JS/TS/Go/Java/Rust,更多开发中)
|
|
50
|
+
- 🧠 **知识图谱** — 函数、类、方法、接口之间的包含、调用、继承关系全量索引
|
|
51
|
+
- ⚡ **FTS5 全文搜索** — 毫秒级符号查找,支持 camelCase / snake_case 分词
|
|
52
|
+
- 🔗 **调用链分析** — `callers` / `callees` / `impact` 精准定位影响范围
|
|
53
|
+
- 🏗 **增量同步** — 仅索引变更文件,监听文件系统变更
|
|
54
|
+
- 🛠 **MCP 服务器** — 8 个工具,直接对接 Claude/Cursor 等 AI Agent
|
|
55
|
+
- 🚀 **SQLite WAL 模式** — 256MB 内存映射 I/O,批量 checkpoint,生产级性能
|
|
56
|
+
|
|
57
|
+
## 快速开始
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# 安装
|
|
61
|
+
pip install codegraph-py
|
|
62
|
+
|
|
63
|
+
# 在项目目录初始化索引
|
|
64
|
+
cd your-project
|
|
65
|
+
codegraph init
|
|
66
|
+
|
|
67
|
+
# 搜索符号
|
|
68
|
+
codegraph query "calculateTotal"
|
|
69
|
+
|
|
70
|
+
# 探索代码上下文
|
|
71
|
+
codegraph explore "How does authentication work?"
|
|
72
|
+
|
|
73
|
+
# 查看索引状态
|
|
74
|
+
codegraph status
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## CLI 命令
|
|
78
|
+
|
|
79
|
+
| 命令 | 描述 |
|
|
80
|
+
|------|------|
|
|
81
|
+
| `init [path]` | 初始化并建立索引 |
|
|
82
|
+
| `uninit [path]` | 移除 CodeGraph 索引 |
|
|
83
|
+
| `index [path]` | 重建完整索引 |
|
|
84
|
+
| `sync [path]` | 增量同步变更 |
|
|
85
|
+
| `status [path]` | 显示索引统计 |
|
|
86
|
+
| `query <search>` | 全文搜索符号 |
|
|
87
|
+
| `explore <query>` | 探索代码(符号源码 + 调用路径) |
|
|
88
|
+
| `node <name>` | 查看符号详细信息 |
|
|
89
|
+
| `files` | 列出索引文件及符号统计 |
|
|
90
|
+
| `callers <symbol>` | 查找调用者 |
|
|
91
|
+
| `callees <symbol>` | 查找被调用者 |
|
|
92
|
+
| `impact <symbol>` | 影响范围分析 |
|
|
93
|
+
| `affected [files]` | 查找受影响的测试文件 |
|
|
94
|
+
| `serve [path]` | 启动 MCP 服务器 |
|
|
95
|
+
| `unlock [path]` | 移除陈旧锁文件 |
|
|
96
|
+
| `install` | CLI 集成指南 |
|
|
97
|
+
|
|
98
|
+
## Python API
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from codegraph import CodeGraph
|
|
102
|
+
|
|
103
|
+
# 初始化或打开项目
|
|
104
|
+
cg = CodeGraph.init_sync('/path/to/project')
|
|
105
|
+
|
|
106
|
+
# 全量索引
|
|
107
|
+
result = cg.index_all()
|
|
108
|
+
print(f"Indexed {result.files_indexed} files, {result.nodes_created} nodes")
|
|
109
|
+
|
|
110
|
+
# 搜索符号
|
|
111
|
+
results = cg.search_nodes("calculateTotal")
|
|
112
|
+
for r in results:
|
|
113
|
+
print(f"{r.node.kind}: {r.node.name} ({r.node.file_path}:{r.node.start_line})")
|
|
114
|
+
|
|
115
|
+
# 调用链分析
|
|
116
|
+
callers = cg.get_callers(node.id)
|
|
117
|
+
callees = cg.get_callees(node.id)
|
|
118
|
+
|
|
119
|
+
# 影响分析
|
|
120
|
+
impact = cg.get_impact_radius(node.id)
|
|
121
|
+
print(f"{len(impact.nodes)} symbols affected by change")
|
|
122
|
+
|
|
123
|
+
# 统计信息
|
|
124
|
+
stats = cg.get_stats()
|
|
125
|
+
print(f"Files: {stats.file_count}, Nodes: {stats.node_count}")
|
|
126
|
+
|
|
127
|
+
# 关闭连接
|
|
128
|
+
cg.close()
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## MCP 服务器
|
|
132
|
+
|
|
133
|
+
启动 MCP 服务器供 AI Agent 集成:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
codegraph serve
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
暴露的 MCP 工具:
|
|
140
|
+
|
|
141
|
+
| 工具 | 功能 |
|
|
142
|
+
|------|------|
|
|
143
|
+
| `codegraph_explore` | 主探索工具 — 自然语言查询符号和上下文 |
|
|
144
|
+
| `codegraph_node` | 获取符号或文件的源码 |
|
|
145
|
+
| `codegraph_search` | 快速符号搜索 |
|
|
146
|
+
| `codegraph_callers` | 查找函数调用者 |
|
|
147
|
+
| `codegraph_callees` | 查找函数被调用者 |
|
|
148
|
+
| `codegraph_impact` | 影响范围分析 |
|
|
149
|
+
| `codegraph_status` | 索引健康检查 |
|
|
150
|
+
| `codegraph_files` | 列出索引文件 |
|
|
151
|
+
|
|
152
|
+
## 工作原理
|
|
153
|
+
|
|
154
|
+
1. **`codegraph init`** 在项目根创建 `.codegraph/` 目录(含 SQLite 数据库 + `.gitignore`)
|
|
155
|
+
2. **索引阶段** 扫描源文件,用 tree-sitter 解析 AST,提取符号(函数、类、方法、接口等),存入节点和边
|
|
156
|
+
3. **FTS5 全文索引** 支持毫秒级符号搜索,智能分词 camelCase / snake_case
|
|
157
|
+
4. **图遍历引擎** 沿边(调用、包含、继承等)查找调用链和影响范围
|
|
158
|
+
5. **增量同步** 通过文件 mtime 对比实现,仅处理变更文件
|
|
159
|
+
6. **MCP 服务器** 基于 JSON-RPC 2.0 stdio 协议,8 个工具直连 AI Agent
|
|
160
|
+
|
|
161
|
+
## 技术栈
|
|
162
|
+
|
|
163
|
+
| 组件 | 技术 |
|
|
164
|
+
|------|------|
|
|
165
|
+
| 数据库 | SQLite (WAL + mmap + FTS5) |
|
|
166
|
+
| AST 解析 | tree-sitter (Python/JS/TS/Go/Java/Rust) |
|
|
167
|
+
| CLI | Click |
|
|
168
|
+
| MCP | JSON-RPC 2.0 over stdio |
|
|
169
|
+
| 文件监控 | watchfiles(可选) |
|
|
170
|
+
|
|
171
|
+
## 性能
|
|
172
|
+
|
|
173
|
+
在 **原版 CodeGraph TypeScript 项目**(329 个源文件)上的压测结果:
|
|
174
|
+
|
|
175
|
+
| 指标 | 数据 |
|
|
176
|
+
|------|------|
|
|
177
|
+
| 索引文件数 | 329 |
|
|
178
|
+
| 提取节点数 | 2,349(1,069 函数 + 699 方法 + 153 接口 + 60 类 + 39 类型别名) |
|
|
179
|
+
| 索引耗时 | **3.7 秒** |
|
|
180
|
+
| 数据库 PRAGMA | WAL + 256MB mmap + 64MB cache + NORMAL sync |
|
|
181
|
+
|
|
182
|
+
## 支持的语言
|
|
183
|
+
|
|
184
|
+
### tree-sitter 完整 AST 解析
|
|
185
|
+
- Python ✅
|
|
186
|
+
- JavaScript / JSX ✅
|
|
187
|
+
- TypeScript / TSX ✅
|
|
188
|
+
- Go ✅
|
|
189
|
+
- Java ✅
|
|
190
|
+
- Rust ✅
|
|
191
|
+
|
|
192
|
+
### 更多语言(文件级别跟踪,AST 解析即将支持)
|
|
193
|
+
- C/C++, C#, PHP, Ruby, Swift, Kotlin, Dart, Scala, Lua, Objective-C, R, Solidity, Terraform, 等 50+ 种
|
|
194
|
+
|
|
195
|
+
## 安装
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
# 基础安装
|
|
199
|
+
pip install codegraph-py
|
|
200
|
+
|
|
201
|
+
# 带 tree-sitter 多语言支持
|
|
202
|
+
pip install "codegraph-py[ts]"
|
|
203
|
+
|
|
204
|
+
# 带文件监控
|
|
205
|
+
pip install "codegraph-py[watch]"
|
|
206
|
+
|
|
207
|
+
# 全功能
|
|
208
|
+
pip install "codegraph-py[all]"
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## 项目结构
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
codegraph/
|
|
215
|
+
├── codegraph/
|
|
216
|
+
│ ├── cli.py # 16 个 CLI 命令
|
|
217
|
+
│ ├── codegraph.py # 核心 CodeGraph 类
|
|
218
|
+
│ ├── types.py # 类型定义
|
|
219
|
+
│ ├── db/ # SQLite 数据库层
|
|
220
|
+
│ │ ├── connection.py # 连接管理 + PRAGMA 优化
|
|
221
|
+
│ │ ├── queries.py # 查询构建器
|
|
222
|
+
│ │ └── schema.sql # 数据库 schema
|
|
223
|
+
│ ├── extraction/ # 代码解析引擎
|
|
224
|
+
│ │ ├── tree_sitter_extractor.py # tree-sitter 核心提取器
|
|
225
|
+
│ │ └── languages/ # 语言配置(6 种)
|
|
226
|
+
│ ├── graph/ # 图遍历引擎
|
|
227
|
+
│ ├── search/ # FTS5 全文搜索
|
|
228
|
+
│ ├── mcp/ # MCP 服务器
|
|
229
|
+
│ ├── sync/ # 文件同步/监控
|
|
230
|
+
│ └── resolution/ # 引用解析
|
|
231
|
+
├── tests/
|
|
232
|
+
│ └── test_codegraph.py # 11 个单元测试
|
|
233
|
+
└── pyproject.toml
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## 许可证
|
|
237
|
+
|
|
238
|
+
MIT — 基于原版 [CodeGraph](https://github.com/colbymchenry/codegraph) TypeScript 项目。
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# codegraph-py
|
|
2
|
+
|
|
3
|
+
**Python 版 CodeGraph** — 基于知识图谱的语义代码智能引擎,为 AI 编程助手提供精准的代码上下文。
|
|
4
|
+
|
|
5
|
+
> 对代码库建立预索引的知识图谱。AI Agent 直接查询图结构而非逐文件扫描 —— 精准上下文、更少工具调用、更快回答。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 特性
|
|
10
|
+
|
|
11
|
+
- 🔍 **多语言 AST 解析** — 基于 tree-sitter 的符号级解析(Python/JS/TS/Go/Java/Rust,更多开发中)
|
|
12
|
+
- 🧠 **知识图谱** — 函数、类、方法、接口之间的包含、调用、继承关系全量索引
|
|
13
|
+
- ⚡ **FTS5 全文搜索** — 毫秒级符号查找,支持 camelCase / snake_case 分词
|
|
14
|
+
- 🔗 **调用链分析** — `callers` / `callees` / `impact` 精准定位影响范围
|
|
15
|
+
- 🏗 **增量同步** — 仅索引变更文件,监听文件系统变更
|
|
16
|
+
- 🛠 **MCP 服务器** — 8 个工具,直接对接 Claude/Cursor 等 AI Agent
|
|
17
|
+
- 🚀 **SQLite WAL 模式** — 256MB 内存映射 I/O,批量 checkpoint,生产级性能
|
|
18
|
+
|
|
19
|
+
## 快速开始
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# 安装
|
|
23
|
+
pip install codegraph-py
|
|
24
|
+
|
|
25
|
+
# 在项目目录初始化索引
|
|
26
|
+
cd your-project
|
|
27
|
+
codegraph init
|
|
28
|
+
|
|
29
|
+
# 搜索符号
|
|
30
|
+
codegraph query "calculateTotal"
|
|
31
|
+
|
|
32
|
+
# 探索代码上下文
|
|
33
|
+
codegraph explore "How does authentication work?"
|
|
34
|
+
|
|
35
|
+
# 查看索引状态
|
|
36
|
+
codegraph status
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## CLI 命令
|
|
40
|
+
|
|
41
|
+
| 命令 | 描述 |
|
|
42
|
+
|------|------|
|
|
43
|
+
| `init [path]` | 初始化并建立索引 |
|
|
44
|
+
| `uninit [path]` | 移除 CodeGraph 索引 |
|
|
45
|
+
| `index [path]` | 重建完整索引 |
|
|
46
|
+
| `sync [path]` | 增量同步变更 |
|
|
47
|
+
| `status [path]` | 显示索引统计 |
|
|
48
|
+
| `query <search>` | 全文搜索符号 |
|
|
49
|
+
| `explore <query>` | 探索代码(符号源码 + 调用路径) |
|
|
50
|
+
| `node <name>` | 查看符号详细信息 |
|
|
51
|
+
| `files` | 列出索引文件及符号统计 |
|
|
52
|
+
| `callers <symbol>` | 查找调用者 |
|
|
53
|
+
| `callees <symbol>` | 查找被调用者 |
|
|
54
|
+
| `impact <symbol>` | 影响范围分析 |
|
|
55
|
+
| `affected [files]` | 查找受影响的测试文件 |
|
|
56
|
+
| `serve [path]` | 启动 MCP 服务器 |
|
|
57
|
+
| `unlock [path]` | 移除陈旧锁文件 |
|
|
58
|
+
| `install` | CLI 集成指南 |
|
|
59
|
+
|
|
60
|
+
## Python API
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from codegraph import CodeGraph
|
|
64
|
+
|
|
65
|
+
# 初始化或打开项目
|
|
66
|
+
cg = CodeGraph.init_sync('/path/to/project')
|
|
67
|
+
|
|
68
|
+
# 全量索引
|
|
69
|
+
result = cg.index_all()
|
|
70
|
+
print(f"Indexed {result.files_indexed} files, {result.nodes_created} nodes")
|
|
71
|
+
|
|
72
|
+
# 搜索符号
|
|
73
|
+
results = cg.search_nodes("calculateTotal")
|
|
74
|
+
for r in results:
|
|
75
|
+
print(f"{r.node.kind}: {r.node.name} ({r.node.file_path}:{r.node.start_line})")
|
|
76
|
+
|
|
77
|
+
# 调用链分析
|
|
78
|
+
callers = cg.get_callers(node.id)
|
|
79
|
+
callees = cg.get_callees(node.id)
|
|
80
|
+
|
|
81
|
+
# 影响分析
|
|
82
|
+
impact = cg.get_impact_radius(node.id)
|
|
83
|
+
print(f"{len(impact.nodes)} symbols affected by change")
|
|
84
|
+
|
|
85
|
+
# 统计信息
|
|
86
|
+
stats = cg.get_stats()
|
|
87
|
+
print(f"Files: {stats.file_count}, Nodes: {stats.node_count}")
|
|
88
|
+
|
|
89
|
+
# 关闭连接
|
|
90
|
+
cg.close()
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## MCP 服务器
|
|
94
|
+
|
|
95
|
+
启动 MCP 服务器供 AI Agent 集成:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
codegraph serve
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
暴露的 MCP 工具:
|
|
102
|
+
|
|
103
|
+
| 工具 | 功能 |
|
|
104
|
+
|------|------|
|
|
105
|
+
| `codegraph_explore` | 主探索工具 — 自然语言查询符号和上下文 |
|
|
106
|
+
| `codegraph_node` | 获取符号或文件的源码 |
|
|
107
|
+
| `codegraph_search` | 快速符号搜索 |
|
|
108
|
+
| `codegraph_callers` | 查找函数调用者 |
|
|
109
|
+
| `codegraph_callees` | 查找函数被调用者 |
|
|
110
|
+
| `codegraph_impact` | 影响范围分析 |
|
|
111
|
+
| `codegraph_status` | 索引健康检查 |
|
|
112
|
+
| `codegraph_files` | 列出索引文件 |
|
|
113
|
+
|
|
114
|
+
## 工作原理
|
|
115
|
+
|
|
116
|
+
1. **`codegraph init`** 在项目根创建 `.codegraph/` 目录(含 SQLite 数据库 + `.gitignore`)
|
|
117
|
+
2. **索引阶段** 扫描源文件,用 tree-sitter 解析 AST,提取符号(函数、类、方法、接口等),存入节点和边
|
|
118
|
+
3. **FTS5 全文索引** 支持毫秒级符号搜索,智能分词 camelCase / snake_case
|
|
119
|
+
4. **图遍历引擎** 沿边(调用、包含、继承等)查找调用链和影响范围
|
|
120
|
+
5. **增量同步** 通过文件 mtime 对比实现,仅处理变更文件
|
|
121
|
+
6. **MCP 服务器** 基于 JSON-RPC 2.0 stdio 协议,8 个工具直连 AI Agent
|
|
122
|
+
|
|
123
|
+
## 技术栈
|
|
124
|
+
|
|
125
|
+
| 组件 | 技术 |
|
|
126
|
+
|------|------|
|
|
127
|
+
| 数据库 | SQLite (WAL + mmap + FTS5) |
|
|
128
|
+
| AST 解析 | tree-sitter (Python/JS/TS/Go/Java/Rust) |
|
|
129
|
+
| CLI | Click |
|
|
130
|
+
| MCP | JSON-RPC 2.0 over stdio |
|
|
131
|
+
| 文件监控 | watchfiles(可选) |
|
|
132
|
+
|
|
133
|
+
## 性能
|
|
134
|
+
|
|
135
|
+
在 **原版 CodeGraph TypeScript 项目**(329 个源文件)上的压测结果:
|
|
136
|
+
|
|
137
|
+
| 指标 | 数据 |
|
|
138
|
+
|------|------|
|
|
139
|
+
| 索引文件数 | 329 |
|
|
140
|
+
| 提取节点数 | 2,349(1,069 函数 + 699 方法 + 153 接口 + 60 类 + 39 类型别名) |
|
|
141
|
+
| 索引耗时 | **3.7 秒** |
|
|
142
|
+
| 数据库 PRAGMA | WAL + 256MB mmap + 64MB cache + NORMAL sync |
|
|
143
|
+
|
|
144
|
+
## 支持的语言
|
|
145
|
+
|
|
146
|
+
### tree-sitter 完整 AST 解析
|
|
147
|
+
- Python ✅
|
|
148
|
+
- JavaScript / JSX ✅
|
|
149
|
+
- TypeScript / TSX ✅
|
|
150
|
+
- Go ✅
|
|
151
|
+
- Java ✅
|
|
152
|
+
- Rust ✅
|
|
153
|
+
|
|
154
|
+
### 更多语言(文件级别跟踪,AST 解析即将支持)
|
|
155
|
+
- C/C++, C#, PHP, Ruby, Swift, Kotlin, Dart, Scala, Lua, Objective-C, R, Solidity, Terraform, 等 50+ 种
|
|
156
|
+
|
|
157
|
+
## 安装
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
# 基础安装
|
|
161
|
+
pip install codegraph-py
|
|
162
|
+
|
|
163
|
+
# 带 tree-sitter 多语言支持
|
|
164
|
+
pip install "codegraph-py[ts]"
|
|
165
|
+
|
|
166
|
+
# 带文件监控
|
|
167
|
+
pip install "codegraph-py[watch]"
|
|
168
|
+
|
|
169
|
+
# 全功能
|
|
170
|
+
pip install "codegraph-py[all]"
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## 项目结构
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
codegraph/
|
|
177
|
+
├── codegraph/
|
|
178
|
+
│ ├── cli.py # 16 个 CLI 命令
|
|
179
|
+
│ ├── codegraph.py # 核心 CodeGraph 类
|
|
180
|
+
│ ├── types.py # 类型定义
|
|
181
|
+
│ ├── db/ # SQLite 数据库层
|
|
182
|
+
│ │ ├── connection.py # 连接管理 + PRAGMA 优化
|
|
183
|
+
│ │ ├── queries.py # 查询构建器
|
|
184
|
+
│ │ └── schema.sql # 数据库 schema
|
|
185
|
+
│ ├── extraction/ # 代码解析引擎
|
|
186
|
+
│ │ ├── tree_sitter_extractor.py # tree-sitter 核心提取器
|
|
187
|
+
│ │ └── languages/ # 语言配置(6 种)
|
|
188
|
+
│ ├── graph/ # 图遍历引擎
|
|
189
|
+
│ ├── search/ # FTS5 全文搜索
|
|
190
|
+
│ ├── mcp/ # MCP 服务器
|
|
191
|
+
│ ├── sync/ # 文件同步/监控
|
|
192
|
+
│ └── resolution/ # 引用解析
|
|
193
|
+
├── tests/
|
|
194
|
+
│ └── test_codegraph.py # 11 个单元测试
|
|
195
|
+
└── pyproject.toml
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## 许可证
|
|
199
|
+
|
|
200
|
+
MIT — 基于原版 [CodeGraph](https://github.com/colbymchenry/codegraph) TypeScript 项目。
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CodeGraph - Semantic Code Intelligence for AI coding agents.
|
|
3
|
+
|
|
4
|
+
A local-first code intelligence system that builds a semantic
|
|
5
|
+
knowledge graph from any codebase.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "1.0.0"
|
|
9
|
+
|
|
10
|
+
from codegraph.codegraph import CodeGraph
|
|
11
|
+
|
|
12
|
+
__all__ = ['CodeGraph', '__version__']
|