专业的编程技术博客社区

网站首页 > 博客文章 正文

Repomix:让代码库完美适配 AI 的打包工具

baijin 2025-06-10 13:03:07 博客文章 8 ℃ 0 评论

现如今,AI 大模型能处理数据格式越来越丰富了,文本、代码、图片等,都能直接发送给大模型作为上下文。然而,对于代码库来说,处理起来就没有那么方便了。

一个代码库中可能包含数十乃至上百上千个不同的文件,文件类型包括各式的代码文件、配置文件、静态资源和文档等,并可能拥有十分复杂的文件夹结构,这都使得大模型在处理整个代码库时,变得较为困难。

Repomix 项目就针对这个问题提出了解决方案,通过把这个代码仓库打包为单一的、AI 友好的文件,来提升大模型处理代码仓库的能力。


简介

Repomix 是一个把代码仓库打包为单一文件的工具,其代码仓库位于
https://github.com/yamadashy/repomix 。其旨在帮助开发者更好地把代码仓库作为大模型 LLM 的输入数据使用,加速 AI 在代码分析等领域的应用。

Repomix 工具有以下特性和功能:

  • AI 优化:以一种易于人工智能理解和处理的方式格式化代码库
  • Token 计数:提供每个文件以及整个代码仓库的 Token 计数,这对于大语言模型的上下文限制很有帮助
  • 使用简单:只需一条命令,即可打包整个代码仓库
  • 可定制:能轻松配置要包含或排除的内容
  • 支持 Git:自动遵循仓库中的 .gitignore 文件,排除不需要的文件
  • 注重安全:集成 Secretlint 进行严格的安全检查,以检测并防止敏感信息被包含在内

Repomix 打包后的文件,能够直接送给包括 Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama 和 Grok 等 AI 大模型进行处理。


使用

Repomix 是一个 TypeScript 编写的工具,可以直接在项目中通过 npx 安装使用:

npx repomix

也可以在全局环境安装,便于重复使用:

# 使用 npm 安装
npm install -g repomix

# 或使用 yarn 安装
yarn global add repomix

# 或使用 Homebrew 安装 (macOS/Linux)
brew install repomix

# 在项目目录中运行
repomix

运行后,Repomix 会在当前目录生成一个 repomix-output.txt 文件,其以 AI 友好的方式包含了整个代码仓库的数据。打包完成后,就可以把这个文件发送给 LLM,并提供类似的 Prompt 来要求 AI 进行代码重构:

This file contains all the files in the repository combined into one.
I want to refactor the code, so please review it first.

Repomix 是一个命令行工具,在项目根目录下,通过以下命令来打包整个仓库:

repomix

如果想要打包一个特定的文件夹,则也可以提供文件夹路径运行:

repomix path/to/directory

也可以通过 glob 匹配表达式来打包特定的文件或文件夹:

repomix --include "src/**/*.ts,**/*.md"

也可以通过相同的表达式来排除特定文件:

repomix --ignore "**/*.log,tmp/"

Repomix 支持远程仓库的打包,通过 --remote 选项进行:

repomix --remote https://github.com/yamadashy/repomix

# You can also use GitHub shorthand:
repomix --remote yamadashy/repomix

# You can specify the branch name, tag, or commit hash:
repomix --remote https://github.com/yamadashy/repomix --remote-branch main

# Or use a specific commit hash:
repomix --remote https://github.com/yamadashy/repomix --remote-branch 935b695

# Another convenient way is specifying the branch's URL
repomix --remote https://github.com/yamadashy/repomix/tree/main

# Commit's URL is also supported
repomix --remote https://github.com/yamadashy/repomix/commit/836abcd7335137228ad77feb28655d85712680f1

Repomix 支持对于输出文件的压缩来节省空间:

repomix --compress

# You can also use it with remote repositories:
repomix --remote yamaadshy/repomix --compress

可以通过以下命令来初始化一个配置文件 repomix.config.json:

repomix --init


Repomix 输出的文件,支持多种格式,包括默认使用的普通文本格式,以及 XML、Markdown 格式,它们都提供了 AI 易于处理的格式化数据。


总结

Repomix 作为一个专注于把代码仓库打包为 LLM 适用的格式,使开发者更容易使用 LLM 大模型作为代码分析和处理的工具。

Repomix 可以广泛应用于软件开发的各个环节。通过把打包后的代码仓库数据提供给大模型,开发者能够更好地使用 AI 进行包括:代码 Review 和重构、文档生成、测试用例生成、代码质量评估、代码库总结等,为开发进一步提效。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表