coldteadotai/pr-lens
关于
理解代码的最快方式
开发工具可以做什么
- Not Mermaid, better We built a custom renderer from scratch. It renders rich visuals, reduces cognitive overload, represents code additions, removals and changes and still works for representing very large systems
实测星标增长
- 24小时
- +6
- 7天
- +19
- 15天
- +46
- 1个月
- +135
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.zh-CN.md
原文
PR Lens
English · 简体中文
本文为简体中文译文;最新说明以英文 README为准。命令和配置示例保留英文原文。
先通过图示了解拉取请求、代码库或软件,再阅读具体代码
PR Lens 直接在拉取请求中呈现每个改动的架构和数据流地图,
并指出可能出问题的地方。
在实时画布中打开即可跟随导览查看,也可以让编码智能体带你了解任何代码库。
安装 GitHub App · 安装 Skill
开源项目免费 · 也可以让编码智能体生成地图: npx skills add coldteadotai/pr-lens
功能
经典 PR 图解
我们用 PR Lens 为引入 React Hooks、Node fetch 和 Kubernetes Ingress 的拉取请求生成了地图,使用的渲染器和格式与上面的地图相同。
react/react#13968 · 36 个文件 · +5,868/−130 · 5 条泳道。通过特性开关引入 Hooks。
nodejs/node#41749 · 16 个文件 · +8,076/−3 · 5 条泳道。为 Node 本身加入 fetch、Request、Response 和 Headers。
kubernetes/kubernetes#14175 · 8 个文件 · +766/−0 · 4 条泳道。首次加入用于七层负载均衡的 Ingress 类型。
另七个案例 · Vue, Rust, Tokio, Neovim, Django, webpack, vLLM
vuejs/core#2532 · 11 个文件 · +1,081/−670 · 3 条泳道。引入 <script setup> 和最初版本的 ref 语法糖。
rust-lang/rust#31954 · 26 个文件 · +369/−16 · 4 条泳道。引入 ? 运算符,用更简洁、可链式使用的写法替代 try!。
tokio-rs/tokio#1657 · 100 个文件 · +7,408/−6,795 · 3 条泳道。重写工作窃取线程池,减少调度器的额外开销。
neovim/neovim#11336 · 15 个文件 · +5,556/−1 · 3 条泳道。将 LSP 客户端纳入 Neovim 本身。
django/django#11209 · 38 个文件 · +931/−42 · 5 条泳道。加入 ASGI 处理器和可在协程中安全使用的请求上下文。
webpack/webpack#10440 · 13 个文件 · +567/−5 · 5 条泳道。通过 ContainerPlugin 引入模块联邦。
vllm-project/vllm#1348 · 6 个文件 · +764/−139 · 3 条泳道。引入 PagedAttention V2 及其序列级并行能力。
查看全部经典案例。 所有案例都可以打开交互查看。
配置
通过 .github/pr-lens.yml 调整 PR Lens 的行为。GitLab 和 Bitbucket 没有 .github/ 目录时,也可以使用 .gitlab/pr-lens.yml 或仓库根目录的 pr-lens.yml,作用相同。配置参考列出了各项设置、默认值和示例。
修正名称和错误
使用 CLI 生成地图时,把修正写入 .github/pr-lens.yml,不要直接编辑生成的 SVG 文件:
schemaVersion: 0.1.0
map:
rename:
- match: services/legacy-mailer.ts
to: Postmark sender
exclude:
- "**/*.test.ts"
这些修正会覆盖在模型分析结果之上,代码变化或模型在不同运行中采用不同名称时仍然有效。你可以重命名组件、排除文件、将卡片固定到指定泳道,或把相关内容分组。完整说明见 packages/cli。
其他运行方式
多数用户使用 App 即可。需要在自己的 CI 中运行、使用自己的模型,或在创建拉取请求前生成地图时,可以选择下面的方式。
通过编码智能体
在创建拉取请求之前,智能体就可以使用同一套渲染器,帮你理解代码库或检查它自己的改动。系统逐渐变大时,也可以用它持续梳理系统结构。
npx skills add coldteadotai/pr-lens
然后向编码智能体提出类似这样的请求:
用 PR Lens 为你刚完成的改动生成地图,并附到拉取请求中。
智能体读取 diff 并编写地图文档,然后运行 npx @coldtea/pr-lens-cli validate,直到文档通过所有检查。接着渲染地图,并用 gh pr create --attach 将其加入拉取请求描述,创建 PR 时即可看到。如果地图中的名称有误,Skill 会指导智能体修改 .github/pr-lens.yml,而不是编辑生成的文件。详情见 packages/agent-skill。
也可以把下面的提示词交给智能体,让它协助完成配置:
Set up PR Lens for me. It maps how code changes touch the system and how data flows through it.
1. Install the agent skill: `npx skills add coldteadotai/pr-lens`.
2. Help me install the GitHub App at https://github.com/apps/coldtea-pr-lens on every repository where I review pull requests. It posts one comment per pull request and updates that comment on every push. It does not need a model key from me.
3. If I'd rather run it from CI with my own model key, offer the Action instead: `.github/workflows/pr-lens.yml` using `coldteadotai/pr-lens/packages/action@v0`, with the key saved as a repository secret. It works with any service that speaks `/chat/completions`, such as OpenAI or Gemini.
4. Then test it: map the latest change in this repository and show me the SVGs or the canvas.
作为工作流:GitHub Action · 自己的 CI · 自己的密钥 · 静态评论
这种方式在你自己的 CI 中使用自己的模型密钥生成地图并发布评论。先把密钥保存为仓库 Secret。下面的示例使用 GEMINI_API_KEY,因为 provider 默认是 Gemini。将以下内容保存为 .github/workflows/pr-lens.yml 并提交:
name: PR Lens
on:
pull_request:
permissions:
contents: write # to publish the rendered SVGs
pull-requests: write # to post the comment
concurrency: # one run per pull request; a push supersedes the last
group: pr-lens-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
lens:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # the diff is between two commits, so both must be here
- uses: coldteadotai/pr-lens/packages/action@v0
with:
api-key: ${{ secrets.GEMINI_API_KEY }}
你可以选择不同模型。provider 支持 gemini(默认)、openai,也支持配合 base-url 和 model 使用 openai-compatible,因此同一套工作流可以接入 OpenRouter、DeepSeek 或自己的服务。密钥通过环境变量传给 CLI,不出现在命令行参数中;diff 只发送给你选择的提供商。Action 无法在运行之间保留交互状态,因此发布的是静态评论,复选框交互仅适用于 App。提供商、视图(lens)、品牌设置和其他输入见 packages/action。
在 GitLab 上:CI/CD 组件 · 自己的流水线 · 自己的密钥 · 可更新的评论
GitLab CI 可以在合并请求中发布同样的地图。添加两个 Masked 变量:模型密钥,以及具有 api 权限范围的项目访问令牌;后者用于发布评论。然后添加该组件:
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
include:
- component: gitlab.com/coldteadotai/pr-lens/[email protected]
workflow: rules 必须写在你自己的 .gitlab-ci.yml 中。GitLab 只有在该文件明确启用时才运行合并请求流水线,仅在被引入组件中定义规则不起作用。由于 CI_JOB_TOKEN 无法发布评论,需要另行提供访问令牌。在 GitLab.com 上,项目访问令牌需要 Premium 或 Ultimate 套餐;Free 套餐也可以使用具有 api 权限范围的个人访问令牌,但评论会显示你的用户名。
SVG 作为附件上传到项目中,因此能够阅读合并请求的人即使没有仓库文件访问权限,也可以看到地图。私有项目需要注意:任何持有附件链接的人都能查看图片,包括项目外的人;链接难以猜中,但图片访问仅依赖该链接保护。如果项目启用了 Require authentication to view media files,图片将无法加载,读者仍可通过评论中的画布链接查看地图。详情见 packages/gitlab-component。
在 Bitbucket 上:pipe · 自己的流水线 · 自己的密钥 · 可更新的评论
在 Bitbucket Pipelines 的 pull-requests: 下添加 pipe:
clone:
depth: full # the diff needs history back to the merge base
pipelines:
pull-requests:
"**":
- step:
name: PR Lens
script:
- pipe: docker://ghcr.io/coldteadotai/pr-lens-pipe:0.1.0
variables:
GEMINI_API_KEY: $GEMINI_API_KEY
PR_LENS_TOKEN: $PR_LENS_TOKEN
pipe 只能访问所在步骤显式传入的变量,因此示例按名称传入两个 Secured 变量。第一个是模型密钥;第二个是具有 pullrequest:write 和 repository:write 权限范围的仓库访问令牌,其中 repository:write 用于将地图保存到仓库的 Downloads。所有 Bitbucket 套餐都提供仓库访问令牌,工作区级令牌则需要 Premium 套餐。
Bitbucket 以普通 Markdown 显示评论,不支持折叠区域或明暗主题配对。评论依次展示标题、统计数字、每个视图的一张地图,以及局部放大图。Bitbucket 不会为来自 fork 的拉取请求运行流水线,因此 pipe 无法为这类 PR 生成地图。如果仓库经常收到来自 fork 的贡献,可以使用托管 App。详情见 packages/bitbucket-pipe。
通过 CLI · 在本地逐步运行
CLI 支持在本地逐步执行其他运行方式中的操作。只有 analyze 会调用模型,它通过环境变量读取密钥,不接受密钥命令行参数:
export GEMINI_API_KEY=… # the default provider; OPENAI_API_KEY with --provider openai
# Diff in, graph document out — measured against the merge base, not the branch tip.
npx @coldtea/pr-lens-cli analyze --base origin/main
# The document as light and dark SVGs, plus the manifest a comment is built from.
# Each drawing lands in its own directory under .pr-lens/, named after its title.
npx @coldtea/pr-lens-cli render .pr-lens/graph.json
# The pull request comment as markdown, on stdout. Posting is your business.
npx @coldtea/pr-lens-cli comment --graph .pr-lens/<drawing>/drawn.graph.json --manifest .pr-lens/<drawing>/manifest.json \
--asset-base-url https://raw.githubusercontent.com/owner/repo/pr-lens/42
# Any PR Lens document, checked against the contract — every problem, not just the first.
npx @coldtea/pr-lens-cli validate .pr-lens/graph.json .github/pr-lens.yml
# After the merge: the pull-request document as a stored map of the system, worth committing.
npx @coldtea/pr-lens-cli export .pr-lens/graph.json -o .github/pr-lens.map.json
所有产物都写入 .pr-lens/,CLI 首次写入时会将该目录加入 .gitignore。这些文件可以随时根据 diff 重新生成,适合作为临时产物保留;需要提交的是 export 生成的映射文件。可以用 --out 指定其他输出位置。
使用 comment --target gitlab 或 --target bitbucket 可以生成适合相应平台的评论。analyze 会根据 Git remote 判断平台。接入 Ollama、DeepSeek、OpenRouter 或其他兼容 /chat/completions 的服务时,传入 --provider openai-compatible --base-url <url>。全部命令、修正规则文件,以及可供脚本判断的错误码见 packages/cli。
在终端中使用 · 在创建拉取请求前生成地图
创建拉取请求之前,也可以在本地生成地图,先检查自己的改动:
npx @coldtea/pr-lens-cli analyze --base origin/main
npx @coldtea/pr-lens-cli render .pr-lens/graph.json
open .pr-lens/*-dark-*.svg # macOS; the SVGs are self-contained, any browser reads them
审查智能体生成的代码时,也可以一边阅读 diff,一边让编写代码的智能体生成地图。安装 Skill 后,告诉它“用 PR Lens 渲染这次改动并打开 SVG”,即可在创建 PR 前结合地图查看 diff。
包
| 包 | 用途 |
|---|---|
packages/schema |
@coldtea/pr-lens-schema: 其他所有模块共用的格式 |
packages/renderer |
@coldtea/pr-lens-renderer: 以确定性的方式,将 JSON 图文档转换成本页展示的明暗主题动态 SVG |
packages/cli |
@coldtea/pr-lens-cli: 使用你自己的模型密钥分析 diff、生成地图并撰写评论 |
packages/action |
GitHub Action:分析改动、发布地图并发表评论 |
packages/gitlab-component |
GitLab CI/CD 组件:在合并请求中执行相同操作 |
packages/bitbucket-pipe |
Bitbucket pipe:在拉取请求中执行相同操作 |
packages/agent-skill |
@coldtea/pr-lens-agent-skill: 指导编码智能体为它刚完成的改动生成地图 |
本地开发
pnpm install
pnpm verify # build, typecheck, test
需要 Node 20.11 或更新版本,以及 pnpm 10。
自托管画布
从 docs/canvas-api.md 开始。
社区
可以在 Discord 提问或分享地图。新版本和功能会发布在 X 账号 @ColdteaAI。
参与贡献
请先创建 Issue,等待维护者同意后,再由你或智能体编写代码。未关联已获同意 Issue 的 PR 会被关闭。Issue 获得同意后,请在 PR 中链接到它。
提交请只使用你自己的姓名。不要为模型添加 Co-Authored-By 行、“Generated with”页脚或会话链接。可以使用智能体(我们也在用),但提交归你所有,你需要对内容负全责。多数工具默认会添加这些信息,请主动关闭。
开发初衷
许可证
MIT © Coldtea
探索相关项目
加载中…

