代码库智能分析工具

Beret 是一款代码库智能分析工具,通过构建内存中的RDF知识图谱,支持CLI命令、MCP工具和SPARQL查询,用于代码结构分析、工程实践检测和架构图生成。

  • by chapeaux
  • Rust
  • 本地部署
  • 7 星标
  • 更新于
在 GitHub 查看 chapeaux/beret

使用说明

贝雷帽

一种代码库智能工具,用于构建内存中的RDF知识图,并通过CLI命令、MCP工具和SPARQL查询将其公开。它使用结构化AST分析解析17种编程语言,从配置文件和文档中提取元数据,为人类活动分析的git历史建立索引,检测工程实践,并生成架构图。

安装

方法 命令 要求
npx npx @chapeaux/beret Node.js
npx(JSR) npx jsr:@chapeaux/beret Node.js
货物 cargo install chapeaux-beret 锈蚀1.85+
来源 cargo build --release 锈蚀1.85+

发布配置文件支持LTO和单个编码器单元,以实现最大的二进制性能。

用法

贝雷帽有三种操作模式: 命令行界面 为了直接分析, MCP 工作站 用于AI工具集成,以及 HTTP MCP 用于远程访问。

CLI模式

直接从终端运行分析命令。每个命令都对目标目录进行索引,并将JSON打印到stdout。

beret describe                     # consolidated project analysis
beret activity                     # git contributors, active files, recent commits
beret testing                      # test frameworks, test ratio
beret architecture                 # layers, structure counts
beret dependencies                 # package managers, dep count
beret practices                    # all detected engineering practices
beret stats                        # counts by structure type
beret find main                    # find symbols named "main"
beret diagram                      # generate LikeC4 architecture diagram
beret query 'SELECT ?f WHERE { ?f <http://repo.example.org/a> <http://repo.example.org/Function> }'

所有命令都接受可选 [PATH] 参数(默认为当前目录):

beret describe /path/to/project
beret activity ~/repos/my-app

使用 --exclude 在使用gitignore样式模式进行索引时跳过路径(可重复):

beret describe --exclude node_modules --exclude target
beret stats --exclude '*.generated.*' --exclude vendor/
beret describe /path/to/project --exclude dist --exclude .cache

MCP服务器模式

服务器从一个空的知识图和按需索引开始。呼叫 refresh_index 使用目录路径填充图形,然后使用其他工具进行探索。这使您可以启动一个服务器,并在工作时将其重新定位到不同的项目或子目录。

模式 命令 传输 index_repo 工具
本地 beret 不
远程 beret --serve 8080 HTTP/SSE 是
远程(绑定所有) beret --serve 0.0.0.0:9090 HTTP/SSE 是

可选 [PATH] 论点(例如。, beret /path/to/project)为设置默认目录 refresh_index 当呼叫时没有路径。如果省略,则默认为当前工作目录。

在stdio模式下, --exclude 在命令行上传递的模式作为默认值应用于每个 refresh_index 电话。其他模式可以通过 exclude 刀具参数。

MCP客户端配置

安装 需要安装 配置
停止 cargo install chapeaux-beret {"command": "beret"}
通过npx进行标准化 无 {"command": "npx", "args": ["-y", "@chapeaux/beret"]}
通过JSR实现标准化 无 {"command": "npx", "args": ["-y", "jsr:@chapeaux/beret"]}
远程HTTP 无(服务器在其他地方运行) {"url": "http://your-server:8080/sse"}

所有条目都放置在

{
  "mcpServers": {
    "beret": ...
  }
}

在您的客户端配置中(例如。, mcp.json, claude_desktop_config.json).

连接后,要求代理为目录建立索引(例如,“为当前项目建立索引”),或者为远程HTTP建立存储库(例如https://github.com/user/repo").

工具

所有列表返回工具支持 limit, offset,以及 exclude 参数。 exclude 采用逗号分隔的目录名称列表进行过滤(例如。, "docs,stories,dist").当结果超过限制时,响应包括 has_more: true 和 next_offset 用于分页。

勘探工具

工具 参数 默认限制 它的作用
file_stats exclude? -- 函数、类、配置、文档、二进制文件、提交等的计数。从这里开始查看项目概述。
find_entry_points exclude?, limit?, offset? 100 查找主要功能、索引文件、app/server/cli模块。答案:“这个应用程序从哪里开始?”
list_structures path?, kind?, exclude?, limit?, offset? 200 列出所有索引结构。按路径子字符串和/或类型过滤(Function, Class, Config, Document, Binary, Stylesheet, Section, Style, Element, Commit).
find_symbol name, exclude?, limit?, offset? 100 查找函数、类、结构或模块的定义位置。部分名称匹配。

调用图工具

工具 参数 默认限制 它的作用
find_callers name?, exclude?, limit?, offset? 100 谁调用此函数?省略名称以获得所有调用边。
find_callees name?, exclude?, limit?, offset? 100 此函数调用什么?省略名称以获得所有调用边。
find_dead_code exclude?, limit?, offset? 100 函数已定义,但从未在任何地方调用。查找未使用的代码和重构候选代码。

依赖关系和配置工具

工具 参数 默认限制 它的作用
find_dependencies exclude?, limit?, offset? 200 列出构建文件中的所有外部包依赖项(package.json、Cargo.toml、pom.xml、go.mod、requirements.txt等15个以上)。

代码搜索工具

工具 参数 默认限制 它的作用
search_pattern pattern, language, exclude?, limit?, offset? 200 使用AST-grep语法进行结构化AST搜索。与文本搜索不同,匹配代码结构。使用 $NAME 对于通配符, $$$ARGS 对于variadic。
query_codebase sparql, limit?, offset? 500 针对高级查询的知识图的原始SPARQL SELECT/ASK。

可视化工具

工具 参数 默认限制 它的作用
generate_diagram path?, depth?, code_only?, exclude?, limit? 500个元素 生成一个 喜欢C4 架构图。根据代码库大小自动调整深度。
  • depth: 0 =自动(默认), 1 =目录, 2 =+个文件, 3 =+函数/类
  • code_only: true (默认)--不包括文档、配置、二进制文件、测试目录、故事等。
  • exclude:逗号分隔的目录名要额外排除
  • 贝雷帽主题:自定义颜色(海军蓝 #00005F,橙色 #F5921B,黄金 #FFCC17,青色 #37A3A3,蓝色 #0066CC),每个元素类型的不同形状,样式关系箭头
  • 输出:粘贴到 游乐场.likec.dev,另存为 .c4,或使用渲染 npx likec4

实践和项目分析工具

工具 参数 功能
describe_project — 推荐第一个电话。 综合分析,涵盖以下所有类别以及git活动的跨领域见解。
describe_practices -- 14个类别中所有检测到的工程实践的高级总结。
describe_testing -- 测试框架、测试函数计数(注释+基于路径的检测)、测试代码比。
describe_ci_cd -- CI平台、容器化、构建工具、部署平台、基础设施即代码。
describe_code_quality -- Linter、格式化器、类型检查器、约定(git钩子、lint stage等)、代码分析。
describe_architecture -- 架构层、单仓库检测、结构计数。
describe_documentation -- 文档工件(许可证、变更日志、贡献、代码所有者等)。
describe_dependencies -- 包管理器、依赖关系计数、自动更新工具。
describe_activity -- Git活动:提交次数最多的贡献者、最活跃的文件、最近的提交。

索引管理工具

工具 参数 功能
refresh_index path?, exclude? 为目录建立索引并构建知识图。在使用其他工具之前,请先调用此命令。 exclude 在索引过程中跳过逗号分隔的gitignore样式模式。
index_repo (仅限HTTP) url 克隆一个git仓库(如果已经克隆,则进行拉取)并对其进行索引

分页

当结果被分页时,响应会用元数据包装结果:

{
  "results": [...],
  "total": 1847,
  "returned": 200,
  "offset": 0,
  "has_more": true,
  "next_offset": 200,
  "message": "Showing 1–200 of 1847 results. Use offset: 200 to see the next page."
}

search_pattern 例子

查找所有Rust函数:

pattern: "fn $NAME($$$ARGS) { $$$BODY }"
language: "rust"

查找JavaScript中的所有console.log调用:

pattern: "console.log($MSG)"
language: "javascript"

查找从基类继承的Python类:

pattern: "class $NAME($BASE): $$$BODY"
language: "python"

query_codebase SPARQL示例

所有IRI都使用 repo: 前缀,扩展为 http://repo.example.org/.

列出所有功能:

SELECT ?func WHERE {
  ?func <http://repo.example.org/a> <http://repo.example.org/Function>
}

查找函数的所有调用者:

SELECT ?caller WHERE {
  ?caller <http://repo.example.org/calls> <http://repo.example.org/process>
}

查找所有依赖项:

SELECT ?file ?dep WHERE {
  ?file <http://repo.example.org/dependsOn> ?dep
}

查找测试函数(基于注释或路径):

SELECT ?func WHERE {
  ?func <http://repo.example.org/isTestFunction> <http://repo.example.org/true>
}

查找最近提交的作者:

SELECT ?author ?date ?message WHERE {
  ?commit <http://repo.example.org/commitAuthor> ?author .
  ?commit <http://repo.example.org/commitDate> ?date .
  ?commit <http://repo.example.org/commitMessage> ?message
} ORDER BY DESC(?date) LIMIT 10

查找修改最多的文件:

SELECT ?file ?count WHERE {
  ?file <http://repo.example.org/commitCount> ?count
} ORDER BY DESC(?count) LIMIT 20

列出MIME类型的二进制文件:

SELECT ?file ?mime WHERE {
  ?file <http://repo.example.org/a> <http://repo.example.org/Binary> .
  ?file <http://repo.example.org/hasMimeType> ?mime
}

检查是否存在任何呼叫关系:

ASK { ?a <http://repo.example.org/calls> ?b }

知识图谱

代码结构

三重 含义
<file/name> repo:a repo:Function 函数、方法或构造函数定义
<file/name> repo:a repo:Class 类、结构、接口、特性或模块定义
<caller> repo:calls <callee> 函数调用关系
<file/name> repo:isTestFunction repo:true 测试功能(通过注释或路径检测)

受试者通过文件路径进行资格认证(例如。, src/main.rs/handle_request).呼叫目标是不合格的被叫方名称。对于以下方法调用 obj.method(),仅 method 被记录下来。对于命名空间呼叫,如 mod::func(),仅 func 被记录下来。

试验检测:功能通过以下方式标记为测试 @Test/@ParameterizedTest/@RepeatedTest 注释(Java), [TestMethod]/[Fact]/[Theory]/[Test] 属性(C#),或位于测试目录中时(/test/, /tests/, /__tests__/, /spec/).

配置文件(JSON、YAML)

三重 含义
<file> repo:a repo:Config 配置文件
<file> repo:declares <key> 文件中的顶级密钥
<file> repo:dependsOn <pkg> 依赖项(来自20多种构建文件格式)

文档(Markdown、HTML、AsciiDoc、重新结构化文本、手册页)

三重 含义
<file> repo:a repo:Document 文档文件
<file/heading> repo:a repo:Section 标题或章节
<file/#id> repo:a repo:Element 带有id的HTML元素
<file/.class> repo:a repo:Element 带有类的HTML元素

样式表(CSS)

三重 含义
<file> repo:a repo:Stylesheet CSS文件
<file/selector> repo:a repo:Style CSS选择器规则

二进制文件

三重 含义
<file> repo:a repo:Binary 二进制文件
<file> repo:hasMimeType <mime> 文件的MIME类型
<file> repo:hasSize <bytes> 文件大小(字节)

Git活动

索引来自 git log 输出(最后500次提交)。即使不是git repo,也很优雅地跳过了。

三重 含义
repo:project repo:hasCommit <hash> 存储库中的提交
<hash> repo:a repo:Commit 提交对象
<hash> repo:commitAuthor <name> 提交人
<hash> repo:commitDate <iso-date> 承诺日期
<hash> repo:commitMessage <text> 提交消息(第一行)
<hash> repo:commitTouches <file> 提交更改的文件
<file> repo:lastModifiedBy <name> 最近接触此文件的提交的作者
<file> repo:lastModifiedDate <date> 最近一次提交的日期
<file> repo:commitCount <n> 接触此文件的提交数
<file> repo:contributedBy <name> 已提交此文件的作者

支持的语言

语言 扩展 函数 类/类型 调用
python .py def class 电话
生锈 .rs fn struct, impl 调用表达式
JavaScript .js, .mjs, .cjs function class 调用表达式
TypeScript .ts function class, interface 调用表达式
TSX .tsx function class, interface 调用表达式
去吧 .go func 类型规格 调用表达式
Java .java 方法、构造函数 class, interface, enum 方法调用
C .c 功能 struct 调用表达式
C .cpp, .cc, .cxx, .hpp 功能 struct, class 调用表达式
C .cs 方法 class, interface, struct, enum 调用
红宝石 .rb def class, module 电话
PHP .php function,方法 class, interface, enum 电话
Kotlin .kt, .kts fun class, object 调用表达式
Swift .swift func class, struct 调用表达式
Scala .scala, .sc def class, object, trait 调用表达式
Bash .sh, .bash function -- 命令
Lua .lua function -- 函数调用

非代码文件

类型 扩展名 提取的内容
JSON .json 顶级密钥、package.json依赖项
YAML .yml, .yaml 顶级按键
Markdown .md, .markdown 标题
AsciiDoc .adoc, .asciidoc 标题
重新结构化文本 .rst 标题
手册页 .1–.9 标题
HTML .html, .htm 元素ID和类
CSS .css 选择器

二进制文件(仅元数据)

图片(.png, .jpg, .gif, .webp, .svg, .ico, .bmp),音频(.mp3, .wav, .ogg, .flac, .aac),视频(.mp4, .webm, .avi, .mov, .mkv),字体(.ttf, .otf, .woff, .woff2),文件(.pdf),档案(.zip, .gz, .tar),可执行文件(.exe, .dll, .so, .dylib, .wasm),数据库(.sqlite, .db).

生成文件依赖关系提取

从20多种构建文件格式中提取依赖关系: package.json, Cargo.toml, pom.xml, build.gradle(.kts), go.mod, Gemfile, Podfile, requirements.txt, pyproject.toml, composer.json, Pipfile, pubspec.yaml, Package.swift, build.sbt, mix.exs, .csproj/.fsproj, Dockerfile/Containerfile (来自), docker-compose.yml (图像), .spec (需要/构建需要), debian/control (依赖项/构建依赖项)。

工程实践

从文件存在和目录结构中检测到。全部使用 <project> 作为主题。

谓词 示例值 从中检测到
usesCIPlatform github actions、gitlab ci、jenkins、packit、zuul、tekton .github/workflows/, .gitlab-ci.yml, Jenkinsfile, .packit.yaml, .zuul.yaml
usesTestFramework 杰斯、皮特斯、柏树、剧作家、tmt、贾可可 jest.config.*, pytest.ini, cypress.config.*, .fmf/, jacoco 插件
usesLinter eslint、biome、ruff、rubocop、rpmlint、checkstyle .eslintrc*, biome.json, ruff.toml, .rpmlintrc
usesFormatter 更漂亮,editorconfig .prettierrc*, .editorconfig
usesBuildTool make、gradle、maven、cmake、autotools、kbuild、tito Makefile, build.gradle, pom.xml, configure.ac, Kbuild
usesContainerization docker,docker作曲,podman,helm,kustomize Dockerfile, Containerfile, docker-compose.yml, Chart.yaml
usesPackageManager npm,纱线,pnpm,货物,pip,诗歌,bundler package.json, yarn.lock, Cargo.toml, Pipfile
usesTypeChecking typescript、mypy、javascriptjsdoc tsconfig.json, mypy.ini
usesCodeAnalysis sonarqube,编解码器 sonar-project.properties, .codecov.yml
usesConfigManagement ansible、木偶、厨师 ansible.cfg, Puppetfile, Berksfile
usesDeploymentPlatform 无服务器、vercel、netlify、fly、heroku serverless.yml, vercel.json, fly.toml
usesPackagingFormat rpm、deb、snap、flatpak、appimage、arch .spec, debian/, snapcraft.yaml, *.flatpak.json
hasLayer 源代码、测试、api、ui、域、服务、基础架构 目录名称: src/, tests/, api/, deploy/等等。
hasDocumentation 许可证、变更日志、贡献指南、代码所有者、api规范 LICENSE, CHANGELOG.md, CONTRIBUTING.md, CODEOWNERS
followsConvention 常规提交、git挂钩、lint分级、systemd、selinux、fedora门控 .commitlintrc*, .husky/, *.service, *.te

演出

ingestor使用并行文件遍历(通过 ignore crate)和直接树型节点类型匹配以提高速度。在调试模式下,在1秒内将5000个文件/60000个三元组作为基准。Git历史索引添加了一个 git log 文件遍历后调用。

出版

板条箱.io

cargo publish

发布为 chapeaux-beret。用户安装时使用 cargo install chapeaux-beret.

npm

cd npm && npm publish --access public

发布为 @chapeaux/beret。需要将预构建的二进制文件上传到GitHub版本。

JSR公司

cd npm && npx jsr publish

发布为 @chapeaux/beret 关于JSR。用户使用 npx jsr:@chapeaux/beret.

测试

cargo test

许可证

麻省理工学院