Sign in

    moon_repomix

    Pack local repositories into AI-ready XML and Markdown with secret filtering and o200k token counts.

    repomix
    cli
    context
    native
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    8 hours ago
    Downloads
    2

    #moon-repomix

    把本地代码目录整理成一份 XML 或 Markdown,方便交给 AI 阅读或留作代码上下文。使用 MoonBit native 实现,提供 CLI 和库 API,运行时不需要 Node.js。代码处理在本地完成,分词资源随安装包提供。

    #最快上手

    源码构建需要 MoonBit 0.10.15+943464bfa(见 moon-toolchain)和平台 C 工具链。在仓库根目录先更新依赖,再运行:

    moon update moon run src/cmd/moon-repomix --target native --release -- examples/demo --style markdown --stdout --tokenizer-path assets/tokenizer-o200k_base.tiktoken

    终端会显示包含 MoonBit、TypeScript 源码和目录结构的 Markdown;debug.log 和 src/scratch.txt 被忽略。文档写到标准输出,统计和跳过原因写到标准错误。

    想一起看看密钥排除和预算超限的行为,运行这个三步演示:

    moon run --target native scripts/demo.mbtx

    它会自动构建 CLI,将输入副本、XML、Markdown 和诊断日志保留在新建的 dist/demo/;目录已存在就报错,不覆盖。也可传入另一个尚不存在的输出目录。步骤和观察点见 演示指南。

    #安装

    #使用平台归档

    解压对应平台的构建归档,保留 bin、share 和 LICENSES 目录。在解压目录中直接运行:

    ./bin/moon-repomix /path/to/project -o context.xml

    Windows PowerShell:

    .\bin\moon-repomix.exe "C:\path\to\project" -o context.xml

    运行归档中的程序不需要安装 MoonBit;也可将 bin 加入 PATH。

    #从源码安装

    在完成 moon update 后,把程序与分词资源一起安装到指定前缀:

    moon run --target native scripts/install.mbtx "$HOME/.local"

    Windows PowerShell:

    moon run --target native scripts/install.mbtx "$env:LOCALAPPDATA\moon-repomix"

    将前缀下的 bin 加入 PATH。安装版从程序所在位置寻找 ../share/moon-repomix/,不依赖当前工作目录。移动安装目录时,请一起保留 bin、share 及其相对位置;仅复制可执行文件不构成完整安装。

    通过 moon run 执行 CLI 时须显式指定词表。三平台 CI 已验证 macOS arm64、Linux x64 和 Windows x64 的检查、构建、功能测试、安装及归档。各平台构建须使用相应的 native C 工具链。

    #CLI 用法

    moon-repomix ./project moon-repomix ./project --style markdown -o context.md moon-repomix ./project --include 'src/**,README.md' --ignore '**/*_test.mbt' moon-repomix ./project --stdout > context.xml moon-repomix ./project --token-budget 100000 moon-repomix ./project --init

    默认输出 repomix-output.xml;Markdown 默认输出 repomix-output.md。-o 会替换已有同名输出,使用时请选好路径。相对输出路径和显式配置路径以当前工作目录为准;自动发现的 repomix.config.json 位于所选目录内。--init 创建配置,不覆盖已有配置。全部选项见 --help 和 配置文档。

    本仓库的 repomix.config.json 默认忽略 assets/**,让自打包文档聚焦代码、脚本和说明,避免词表与黄金数据占用大量 token。需要打包这些资源时,使用 --config 指向内容为 {} 的配置文件,再用 --include 选择目标文件。

    #打包行为

    • 读取 .gitignore、.ignore、.repomixignore(含嵌套规则),支持 include/ignore glob;按相对路径稳定排序。include 不会绕过忽略规则。
    • 打包有效 UTF-8 普通文件;去掉 UTF-8 BOM,保留原始换行。默认单文件上限为 50 MiB;链接、二进制、不可读或超限文件跳过。
    • 默认扫描密钥,排除命中的整个文件。诊断给出规则、路径、行号和磁盘字节区间,不打印匹配值。
    • 用完整 o200k_base 词表计算最终文档的 token 数,同时报告逐文件统计。超预算不截断:仍输出完整文档,CLI 返回 1;正常完成且未超预算返回 0。配置、资源、扫描或写入错误也返回 1,需结合诊断区分。
    • 文件输出先写同目录临时文件,再原子替换;打包失败时保留已有输出。--stdout 将文档写到标准输出,统计和诊断写到标准错误。

    更多细节:文件选择 · 输出格式 · 扫描规则 · 分词。

    #安全与使用边界

    密钥扫描是启发式检测,不能保证发现所有凭证;分享前仍应检查最终文档。--no-security-check 会关闭扫描并显示警告。忽略规则控制文件选择,不是安全隔离;不要在打包时允许不可信进程并发修改输入目录。

    打包和计数在内存中完成,内存占用随输入和输出大小增长。token 预算用于告警,不是内存限制,也不自动挑选或裁剪文件。

    #库 API

    本地引用源码时,用 moon.work 将调用项目与本仓库列为 members,在调用项目的 moon.mod 声明 Delthin/moon_repomix@0.1.0 依赖,并在 moon.pkg 导入:

    import {
    "Delthin/moon_repomix" @repomix,
    }

    在 async fn 中先加载资源,再打包:

    let options = @repomix.options("/absolute/project")
    let runtime = @repomix.Runtime::load("/absolute/share/moon-repomix/o200k_base.tiktoken")
    let result = @repomix.pack(options, runtime)
    // result.text 是完整文档;result.tokens 是完整文档的 token 数。
    // result.files / skipped / findings / security / budget 提供统计及状态。

    pack 返回结果,不写文件;由调用者决定如何处理输出和预算超限。词表同目录还须有 tokenizer-unicode-16.0.0.txt,两份资源均校验 SHA-256。一个 Runtime 可复用于多次顺序打包。接口见 src/pkg.generated.mbti。

    #开发与打包

    CI 安装 latest 后核对 moon-toolchain;版本不一致时停止,须核对并更新工具链要求。

    moon update moon check --target native --deny-warn moon test --target native moon build --target native --release moon run --target native scripts/package.mbtx dist macos-arm64

    打包标签为 macos-arm64、macos-x64、linux-x64、windows-x64,在对应平台和架构上构建;macOS x64 尚无 CI 运行证据。归档包含程序、词表、Unicode 数据及许可证。

    构建后可运行常见场景及真实 CLI 功能测试:

    moon run --target native scripts/scenarios.mbtx _build/native/release/build/cmd/moon-repomix/moon-repomix.exe assets/tokenizer-o200k_base.tiktoken moon run --target native scripts/functional.mbtx _build/native/release/build/cmd/moon-repomix/moon-repomix.exe assets/tokenizer-o200k_base.tiktoken moon run --target native scripts/self-pack-test.mbtx ../moon-repomix-self-check

    自打包检查会新建输出目录,保留 XML、Markdown 和日志;已有目录会报错。安装了 xmllint 时可追加 --xml-oracle,独立核对黄金数据从 XML 还原后的正文。

    测试覆盖编码和大小边界、配置合并、密钥排除、输出格式、预算、错误保护及路径处理;Linux CI 还用独立 XML 解析器核对输出,并验证 moon package 生成的源码包解压后能运行 demo。源码包位于 _build/publish/Delthin-moon_repomix-0.1.0.zip。

    #来源与许可

    项目参考 Repomix 的本地仓库打包行为,采用 MIT 许可。第三方代码及资源的许可保存在 LICENSES/;分词实现、词表与 Unicode 数据的来源、版本及兼容范围见 分词文档,扫描依据见 扫描规则。

    Finding

    Locations refer to the original disk UTF-8 bytes, including a stripped BOM.

    Skipped

    Path is root-relative. Reasons are documented in docs/file-selection.md.

    PackError

    pub(all) suberror PackError {
    ConfigurationError
    SelectionError
    ScanError
    ResourceError
    }

    BudgetStatus

    pub(all) enum BudgetStatus {
    NotSet
    Within(Int)
    Exceeded(Int)
    }

    FileStats

    pub(all) struct FileStats {
    path : String
    disk_bytes : Int
    tokens : Int
    }

    PackResult

    pub struct PackResult {
    text : String
    bytes : Int
    tokens : Int
    files : Array[FileStats]
    skipped : Array[
    Skipped
    ]
    findings : Array[
    Finding
    ]
    security : SecurityStatus
    budget : BudgetStatus
    }

    Excluded files retain only metadata; their contents are absent.

    Runtime

    pub struct Runtime {
    encoder :
    Encoder

    }

    Validated resources shared by sequential pack operations.

    Runtime::load

    async fn Runtime::load(tokenizer_path : String) -> Runtime

    SecurityStatus

    pub(all) enum SecurityStatus {
    Passed
    Excluded
    Disabled
    }

    options

    fn options(directory : String) ->
    Options
    raise

    Default validated options for one directory, with XML and scanning enabled.

    pack

    async fn pack(options :
    Options
    , runtime : Runtime) -> PackResult

    Build and count a complete document. The caller decides where to write it.

    version

    fn version() -> String

    Version of the native CLI and library.