opensrc
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseopensrc
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
undefinedbash
undefinedInstall 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
undefinednpx opensrc zod@3.22.0
npx opensrc owner/repo@v1.0.0
undefinedCommands
命令列表
| Command | Description |
|---|---|
| Fetch source for packages/repos |
| List all fetched sources |
| Remove specific source |
| Remove all sources |
| 命令 | 描述 |
|---|---|
| 获取包/仓库的源代码 |
| 列出所有已获取的源代码 |
| 删除指定的源代码 |
| 删除所有已获取的源代码 |
Output Structure
输出结构
After fetching, sources stored in directory:
opensrc/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:
- - adds
.gitignoreto ignore listopensrc/ - - excludes
tsconfig.jsonfrom compilationopensrc/ - - adds section pointing agents to source code
AGENTS.md
Use or to skip prompt.
--modify--modify=false首次运行时,opensrc会提示修改以下文件:
- - 将
.gitignore添加至忽略列表opensrc/ - - 排除
tsconfig.json目录的编译opensrc/ - - 添加指向源代码的章节供Agent参考
AGENTS.md
使用或可跳过提示。
--modify--modify=falseKey Behaviors
核心特性
- Version Detection - For npm, auto-detects installed version from ,
node_modules,package-lock.json, orpnpm-lock.yamlyarn.lock - Repository Resolution - Resolves package to its git repo via registry API, clones at matching tag
- Monorepo Support - Handles packages in monorepos via field
repository.directory - Shallow Clone - Uses for efficient cloning, removes
--depth 1after clone.git - Tag Fallback - Tries ,
v{version}, then default branch if tag not found{version}
- 版本检测 - 针对npm包,自动从、
node_modules、package-lock.json或pnpm-lock.yaml检测已安装版本yarn.lock - 仓库解析 - 通过注册表API将包映射到对应的Git仓库,克隆匹配标签的版本
- Monorepo支持 - 通过字段处理单仓库多包结构中的包
repository.directory - 浅克隆 - 使用实现高效克隆,克隆完成后移除
--depth 1目录.git - 标签回退 - 若未找到匹配标签,依次尝试、
v{version},最后使用默认分支{version}
Common Workflows
常用工作流
Fetching a Package
获取包源代码
bash
undefinedbash
undefinedAgent 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/
undefinedundefinedUpdating Sources
更新源代码
bash
undefinedbash
undefinedRe-run same command to update to currently installed version
重新运行相同命令可更新至当前已安装版本
npx opensrc zod
npx opensrc zod
→ Checks if version changed
→ 检查版本是否变更
→ Re-clones if needed
→ 如有需要则重新克隆
undefinedundefinedMultiple Sources
批量获取源代码
bash
undefinedbash
undefinedFetch multiple at once
一次性获取多个资源
npx opensrc react react-dom next
npx opensrc zod pypi:pydantic vercel/ai
undefinednpx opensrc react react-dom next
npx opensrc zod pypi:pydantic vercel/ai
undefinedReferences
参考文档
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的详细支持说明