代码索引库

codemogger是一个为AI编码代理设计的代码索引库,通过tree-sitter解析源代码并将其分块为语义单元,支持本地嵌入和SQLite存储,提供向量和全文搜索功能。

  • by glommer
  • TypeScript
  • 本地部署
  • 349 星标
  • 更新于
在 GitHub 查看 glommer/codemogger

使用说明

密码记录器

AI编码代理的代码索引库。使用树状图解析源代码,将其分块为语义单元(函数、结构、类、impl块),在本地嵌入它们,并使用矢量+全文搜索将所有内容存储在一个SQLite文件中。

没有Docker,没有服务器,没有API密钥。一 .db 每个代码库的文件。

为什么

编码代理需要理解代码库。他们需要找到事物的定义位置,发现概念是如何在文件中实现的,并快速浏览不熟悉的代码。这需要关键字搜索(精确的标识符查找)和语义搜索(不知道确切名称时的自然语言查询)。

随着人工智能编码工具变得更加可组合——代理调用代理,MCP服务器插入不同的主机——这种能力需要以本地运行的库的形式存在。没有外部服务器,没有API密钥,没有Docker容器。只是一个返回结果的函数调用。

codemogger就是那个图书馆。嵌入式SQLite(通过 图尔索)使用FTS+矢量搜索 .db 文件。

安装

npm install -g codemogger

或使用 npx 无需安装即可运行。

快速开始

# Index a project
codemogger index ./my-project

# Search
codemogger search "authentication middleware"

添加到编码代理的MCP配置(Claude Code、OpenCode等):

{
  "mcpServers": {
    "codemogger": {
      "command": "npx",
      "args": ["-y", "codemogger", "mcp"]
    }
  }
}

MCP服务器公开了三个工具:

  • codemogger_search -索引代码的语义和关键字搜索
  • codemogger_index -首次索引代码库
  • codemogger_reindex -修改文件后更新索引

将本地数据库添加到 .gitignore:

# codemogger db
.codemogger/

软件开发工具包

codemogger也可用作库。SDK没有模型依赖关系,您可以提供自己的嵌入函数:

import { CodeIndex } from "codemogger"
import { pipeline } from "@huggingface/transformers"

// Load embedding model (runs locally, no API keys)
const extractor = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2", { dtype: "q8" })

const embedder = async (texts: string[]): Promise<number[][]> => {
  const output = await extractor(texts, { pooling: "mean", normalize: true })
  return output.tolist() as number[][]
}

const db = new CodeIndex({
  dbPath: "./my-project.db",
  embedder,
  embeddingModel: "all-MiniLM-L6-v2",
})

await db.index("/path/to/project")

// Semantic: "what does this codebase do?"
const results = await db.search("authentication middleware", { mode: "semantic" })

// Keyword: precise identifier lookup
const results = await db.search("BTreeCursor", { mode: "keyword" })

await db.close()

MCP服务器和CLI随附 all-MiniLM-L6-v2 默认情况下。

命令行界面

# Install globally
npm install -g codemogger

# Index a directory
codemogger index ./my-project

# Search
codemogger search "authentication middleware"

# List indexed codebases
codemogger list

运作原理

  1. 扫描 -步行目录,尊重 .gitignore,从扩展中检测语言
  2. 块 -使用树保姆(WASM)解析每个文件,提取顶级定义(函数、结构、类、impl块)。大于150行的项目被拆分为子项目。
  3. 嵌入 -使用提供的嵌入模型对每个块进行编码(本地运行,无API)
  4. 商店 -使用FTS索引将块+嵌入写入SQLite
  5. 搜索 -向量余弦相似度(语义)或带加权字段的FTS(关键字)

增量:在后续运行中,只有更改的文件(通过SHA-256哈希)会被重新处理。

语言

Rust、C、C++、Go、Python、Zig、Java、Scala、JavaScript、TypeScript、TSX、PHP、Ruby。

基准测试

在苹果M2(8GB)上对4个真实世界的代码库进行了基准测试。每个项目都使用自己的独立数据库。嵌入使用 vector8 (int8量化,395字节/块,而float32为1536)。嵌入模型: all-MiniLM-L6-v2 (q8量化,本地CPU)。搜索时间在3次运行中为p50。

演出

项目 语言 文件 语义 关键字 ripgrep
图尔索 生锈 748 35毫秒 1毫秒 25毫秒
包子 Zig 9255 137毫秒 2毫秒 166毫秒
TypeScript TypeScript 39298 242毫秒 4毫秒 1500毫秒
Kubernetes 前进 16668 617毫秒 12毫秒 731毫秒

关键字搜索是 比ripgrep快25x-370x 并返回精确的定义,而不是数千个文件匹配。

索引是以嵌入为主的一次性成本(约97%的时间)。后续运行仅重新嵌入更改的文件。

搜索质量:语义搜索与ripgrep

真正的优势不是速度,而是 当你不知道确切的关键字时,找到正确的代码.

“提前写入日志复制和同步” (旅游)

codemogger(前5名) ripgrep
impl LogicalLog -core/mvcc/persistent-storage/logical_log.rs 匹配3个文件
enum CommitState -core/mvcc/database/mod.rs (关键字:“提前写”)
function new -core/mvcc/数据库/checkpoint_state_machine.rs
struct LogicalLog -core/mvcc/persistent_storage/logical_log.rs
function checkpoint_shutdown -核心/存储/pager.rs

“SQL语句解析和编译” (旅游)

codemogger(前5名) ripgrep
function parse_and_build -core/translate/logical.rs 匹配139个文件
macro compile_sql -core/incremental/compiler.rs (关键字:“语句”)
function parse_from_clause_opt -parser/src/paparser.rs
function parse_from_clause_table -核心/翻译/规划.rs
function parse_table -核心/翻译/规划.rs

“HTTP请求解析和响应写入” (Bun)

codemogger(前5名) ripgrep
function consumeRequestLine -packages/bun-uws/src/HttpParser.h 0个文件匹配
declaration ConsumeRequestLineResult -packages/bun-uws/src/HttpParser.h (关键字:“HTTP”)
function llhttp__after_headers_complete -src/bun.js/bindings/node/http/l1http/http.c
function llhttp_message_needs_eof -src/bun.js/bindings/node/http/l1http/http.c
function shortRead -packages/bun-uws/src/HttpParser.h

“根据资源需求将Pod调度到节点” (库贝内特斯)

codemogger(前5名) ripgrep
type Scheduling -staging/src/k8s.io/api/node/v1beta1/types.go 匹配429个文件
type Scheduling -staging/src/k8s.io/api/node/v1/types.go (关键字:“调度”)
type SchedulingApplyConfiguration -舞台/。../node/v1/scheduling.go
function runPodAndGetNodeName -test/e2e/schedules/predictes.go
type createPodsOp -测试/集成/调度性能/调度性能go

“容器健康检查探测和重启策略” (库贝内特斯)

codemogger(前5名) ripgrep
type ContainerFailures -test/utils/conditions.go 匹配1652个文件
variable _ -test/e2e/common/node/container_probe.go (关键字:“container”)
function checkContainerStateTransition -pkg/kubelet/status/status_manager.go
function TestDoProbe_TerminatedContainerWithRestartPolicyNever -pkg/kubelet/prober/worker_test.go
function proveHealthCheckNodePortDeallocated -pkg/register/core/service/storage/storage_test.go

ripgrep在常见关键字上匹配数千个文件。codemogger返回5个最相关的定义。

依赖关系注释

@tursodatabase/database 当前被固定到预发布版本(0.5.0-pre.14).没有稳定的 0.5.x 尚未释放;一旦发布,就应该更新。预发布版本足够稳定,可以用于开发,但在达到最终版本之前可能会有突破性的变化。

建筑

  • Bun/TypeScript 运行时
  • 树保姆 AST感知组块-13种语言语法
  • 全迷你LM-L6-v2 对于局部嵌入(384维,q8量化)
  • 图尔索 用于存储-带有FTS+矢量搜索扩展的嵌入式SQLite
  • 单个数据库文件 使用每个代码库的FTS表和全局向量搜索存储多个代码库

许可证

麻省理工学院