opensrc

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

opensrc

opensrc

CLI tool to fetch source code for packages/repos, giving AI coding agents deeper implementation context.
一款用于获取包/仓库源代码的CLI工具,为AI编码Agent提供更深入的实现上下文。

When to Use

适用场景

  • Need to understand how a library/package works internally (not just its interface)
  • Debugging issues where types alone are insufficient
  • Exploring implementation patterns in dependencies
  • Agent needs to reference actual source code of a package
  • 需要理解库/包的内部工作原理(而非仅了解其接口)
  • 调试仅靠类型信息无法解决的问题
  • 探索依赖项中的实现模式
  • Agent需要参考包的实际源代码

Quick Start

快速开始

bash
undefined
bash
undefined

Install globally or use npx

全局安装或使用npx

npm install -g opensrc
npm install -g opensrc

Fetch npm package (auto-detects installed version from lockfile)

获取npm包(自动从锁文件检测已安装版本)

npx opensrc zod
npx opensrc zod

Fetch from other registries

从其他注册表获取

npx opensrc pypi:requests # Python/PyPI npx opensrc crates:serde # Rust/crates.io
npx opensrc pypi:requests # Python/PyPI npx opensrc crates:serde # Rust/crates.io

Fetch GitHub repo directly

直接获取GitHub仓库

npx opensrc vercel/ai # owner/repo shorthand npx opensrc github:owner/repo # explicit prefix npx opensrc https://github.com/colinhacks/zod # full URL
npx opensrc vercel/ai # 所有者/仓库简写格式 npx opensrc github:owner/repo # 显式前缀格式 npx opensrc https://github.com/colinhacks/zod # 完整URL格式

Fetch specific version/ref

获取特定版本/引用

npx opensrc zod@3.22.0 npx opensrc owner/repo@v1.0.0
undefined
npx opensrc zod@3.22.0 npx opensrc owner/repo@v1.0.0
undefined

Commands

命令列表

CommandDescription
opensrc <packages...>
Fetch source for packages/repos
opensrc list
List all fetched sources
opensrc remove <name>
Remove specific source
opensrc clean
Remove all sources
命令描述
opensrc <packages...>
获取包/仓库的源代码
opensrc list
列出所有已获取的源代码
opensrc remove <name>
删除指定的源代码
opensrc clean
删除所有已获取的源代码

Output Structure

输出结构

After fetching, sources stored in
opensrc/
directory:
opensrc/
├── settings.json           # User preferences
├── sources.json            # Index of fetched packages/repos
└── repos/
    └── github.com/
        └── owner/
            └── repo/       # Cloned source code
获取完成后,源代码将存储在
opensrc/
目录下:
opensrc/
├── settings.json           # 用户偏好设置
├── sources.json            # 已获取包/仓库的索引
└── repos/
    └── github.com/
        └── owner/
            └── repo/       # 克隆的源代码

File Modifications

文件修改

On first run, opensrc prompts to modify:
  • .gitignore
    - adds
    opensrc/
    to ignore list
  • tsconfig.json
    - excludes
    opensrc/
    from compilation
  • AGENTS.md
    - adds section pointing agents to source code
Use
--modify
or
--modify=false
to skip prompt.
首次运行时,opensrc会提示修改以下文件:
  • .gitignore
    - 将
    opensrc/
    添加至忽略列表
  • tsconfig.json
    - 排除
    opensrc/
    目录的编译
  • AGENTS.md
    - 添加指向源代码的章节供Agent参考
使用
--modify
--modify=false
可跳过提示。

Key Behaviors

核心特性

  1. Version Detection - For npm, auto-detects installed version from
    node_modules
    ,
    package-lock.json
    ,
    pnpm-lock.yaml
    , or
    yarn.lock
  2. Repository Resolution - Resolves package to its git repo via registry API, clones at matching tag
  3. Monorepo Support - Handles packages in monorepos via
    repository.directory
    field
  4. Shallow Clone - Uses
    --depth 1
    for efficient cloning, removes
    .git
    after clone
  5. Tag Fallback - Tries
    v{version}
    ,
    {version}
    , then default branch if tag not found
  1. 版本检测 - 针对npm包,自动从
    node_modules
    package-lock.json
    pnpm-lock.yaml
    yarn.lock
    检测已安装版本
  2. 仓库解析 - 通过注册表API将包映射到对应的Git仓库,克隆匹配标签的版本
  3. Monorepo支持 - 通过
    repository.directory
    字段处理单仓库多包结构中的包
  4. 浅克隆 - 使用
    --depth 1
    实现高效克隆,克隆完成后移除
    .git
    目录
  5. 标签回退 - 若未找到匹配标签,依次尝试
    v{version}
    {version}
    ,最后使用默认分支

Common Workflows

常用工作流

Fetching a Package

获取包源代码

bash
undefined
bash
undefined

Agent needs to understand zod's implementation

Agent需要理解zod的实现细节

npx opensrc zod
npx opensrc zod

→ Detects version from lockfile

→ 从锁文件检测版本

→ Finds repo URL from npm registry

→ 从npm注册表获取仓库URL

→ Clones at matching git tag

→ 克隆对应Git标签的代码

→ Source available at opensrc/repos/github.com/colinhacks/zod/

→ 源代码存储在opensrc/repos/github.com/colinhacks/zod/

undefined
undefined

Updating Sources

更新源代码

bash
undefined
bash
undefined

Re-run same command to update to currently installed version

重新运行相同命令可更新至当前已安装版本

npx opensrc zod
npx opensrc zod

→ Checks if version changed

→ 检查版本是否变更

→ Re-clones if needed

→ 如有需要则重新克隆

undefined
undefined

Multiple Sources

批量获取源代码

bash
undefined
bash
undefined

Fetch multiple at once

一次性获取多个资源

npx opensrc react react-dom next npx opensrc zod pypi:pydantic vercel/ai
undefined
npx opensrc react react-dom next npx opensrc zod pypi:pydantic vercel/ai
undefined

References

参考文档

For detailed information:
  • CLI Usage & Options - Full command reference
  • Architecture - Code structure and extension
  • Registry Support - npm, PyPI, crates.io details
如需详细信息:
  • CLI使用与选项 - 完整命令参考
  • 架构设计 - 代码结构与扩展方式
  • 注册表支持 - npm、PyPI、crates.io的详细支持说明