Skip to content

Codex 发挥最大效果的关键:把它当成有明确上下文的队友,告诉它清晰的"完成标准"。本文提供 8 个真实开发场景的完整工作流,包括 IDE 扩展、CLI 和云端三种接入方式的具体操作步骤和可直接复用的 Prompt 模板。

Codex 工作流示例

如果你刚开始用 Codex,建议先读 Prompting 指南,再回来看这些具体示例。

看示例的方式

每个工作流都包含:

  • 适合场景和推荐的接入方式(IDE、CLI 还是 Cloud)
  • 步骤和 Prompt 示例
  • 上下文说明:Codex 自动能看到什么 vs 你需要手动提供什么
  • 验证方式:怎么确认输出是对的

提示:IDE 扩展会自动把当前打开的文件作为上下文。CLI 通常需要你在 Prompt 里明确写出文件路径,或者用 /mention@ 路径自动补全来附加文件。


场景一:解释代码库

适合:刚接手一个服务、理解某个协议/数据模型/请求流程。

IDE 扩展(本地探索最快)

  1. 打开最相关的文件
  2. 选中你关心的代码(可选,但推荐)
  3. 发送 Prompt:
text
Explain how the request flows through the selected code.

Include:
- a short summary of the responsibilities of each module involved
- what data is validated and where
- one or two "gotchas" to watch for when changing this

验证方式:让 Codex 输出一个你能快速确认的结构:

text
Summarize the request flow as a numbered list of steps. Then list the files involved.

CLI(需要文字记录 + 执行命令时)

  1. 启动交互式会话:codex
  2. 附加文件并发送 Prompt:
text
I need to understand the protocol used by this service. Read @foo.ts @schema.ts and explain the schema and request/response flow. Focus on required vs optional fields and backward compatibility rules.

可以用 @ 在输入框插入文件路径,或者 /mention 附加特定文件。


场景二:修复 Bug

适合:有可以本地复现的故障行为。

CLI(复现 + 验证最紧凑)

  1. 在仓库根目录启动 Codex:codex
  2. 提供复现步骤 + 可疑文件:
text
Bug: Clicking "Save" on the settings screen sometimes shows "Saved" but doesn't persist the change.

Repro:
1) Start the app: npm run dev
2) Go to /settings
3) Toggle "Enable alerts"
4) Click Save
5) Refresh the page: the toggle resets

Constraints:
- Do not change the API shape.
- Keep the fix minimal and add a regression test if feasible.

Start by reproducing the bug locally, then propose a patch and run checks.

上下文说明:

  • 你提供的:复现步骤和约束条件(这比高层描述重要得多)
  • Codex 自动收集的:命令输出、发现的调用点、触发的 stack trace

验证:

text
After the fix, run lint + the smallest relevant test suite. Report the commands and results.

IDE 扩展

  1. 打开 Bug 所在的文件和它最近的调用方
  2. 发 Prompt:
text
Find the bug causing "Saved" to show without persisting changes. After proposing the fix, tell me how to verify it in the UI.

场景三:写测试

适合:你想精确控制测试覆盖范围。

IDE 扩展(基于代码选中)

  1. 打开包含该函数的文件
  2. 选中函数定义的行,用命令面板执行"Add to Codex Thread"把这些行加入上下文
  3. 发 Prompt:
text
Write a unit test for this function. Follow conventions used in other tests.

上下文说明:IDE 扩展的"Add to Codex Thread"命令会传入选中的代码行 + 打开的文件。

CLI(在 Prompt 里描述路径和范围)

  1. 启动 Codex:codex
  2. 发 Prompt:
text
Add a test for the invert_list function in @transform.ts. Cover the happy path plus edge cases.

场景四:截图 → 原型

适合:有设计稿、截图或 UI 参考,想快速出一个能跑的原型。

CLI(图片 + Prompt)

  1. 把截图保存到本地(比如 ./specs/ui.png
  2. 启动 Codex:codex
  3. 把图片文件拖进终端来附加到 Prompt
  4. 补充约束和结构:
text
Create a new dashboard based on this image.

Constraints:
- Use react, vite, and tailwind. Write the code in typescript.
- Match spacing, typography, and layout as closely as possible.

Deliverables:
- A new route/page that renders the UI
- Any small components needed
- README.md with instructions to run it locally

上下文说明:图片提供视觉需求,但你仍然需要指定实现约束(框架、路由、组件风格)。非直觉的行为(hover 状态、校验规则、键盘交互)用文字写清楚。

验证:

text
Start the dev server and tell me the local URL/route to view the prototype.

IDE 扩展(图片 + 现有文件)

  1. 在 Codex 对话框里附加图片(拖入或粘贴)
  2. 发 Prompt:
text
Create a new settings page. Use the attached screenshot as the target UI.
Follow design and visual patterns from other files in this project.

场景五:UI 实时迭代

适合:"设计 → 调整 → 刷新 → 再调整"的高频循环,Codex 负责改代码,你实时看结果。

CLI + 开发服务器

  1. 启动 Codex:codex
  2. 另开一个终端启动开发服务器:npm run dev
  3. 让 Codex 提几个方向:
text
Propose 2-3 styling improvements for the landing page.
  1. 选一个方向,用精确的小步骤迭代:
text
Go with option 2.

Change only the header:
- make the typography more editorial
- increase whitespace
- ensure it still looks good on mobile
  1. 继续小步骤迭代:
text
Next iteration: reduce visual noise.
Keep the layout, but simplify colors and remove any redundant borders.

验证:在浏览器里实时查看改动。提交你满意的,回滚不满意的。如果你回滚或修改了某个改动,告诉 Codex,否则它下一条 Prompt 可能会覆盖你的修改。


场景六:重构委托给云端

适合:在本地做好设计(有代码上下文,能快速检查),把漫长的实现交给云端任务并行处理。

本地规划(IDE)

  1. 确认当前改动都已提交或 stash,以便之后干净地对比
  2. 让 Codex 生成重构计划:
text
$plan

We need to refactor the auth subsystem to:
- split responsibilities (token parsing vs session loading vs permissions)
- reduce circular imports
- improve testability

Constraints:
- No user-visible behavior changes
- Keep public APIs stable
- Include a step-by-step migration plan
  1. Review 计划,谈判细节:
text
Revise the plan to:
- specify exactly which files move in each milestone
- include a rollback strategy

上下文说明:规划阶段 Codex 在本地扫描代码效果最好(入口点、模块边界、依赖关系图)。

委托给云端(IDE → Cloud)

  1. 如果还没设置,先配置云端环境
  2. 点击 Prompt 输入框下方的云端图标,选择你的云端环境
  3. 发送下一条 Prompt,Codex 会在云端新建一个线程,继承当前线程的上下文(包含计划和本地代码改动):
text
Implement Milestone 1 from the plan.
  1. Review 云端 diff,根据需要迭代
  2. 直接在云端创建 PR,或者把改动拉到本地测试后再提
  3. 继续执行计划的后续 milestone

场景七:本地 Code Review

适合:提交或建 PR 前想多一双眼睛检查。

CLI(Review 工作区)

  1. 启动 Codex:codex
  2. 运行 review 命令:
text
/review
  1. 可以加自定义关注点:
text
/review Focus on edge cases and security issues

验证:根据 review 反馈修复问题,然后重跑 /review 确认问题已解决。


场景八:Review GitHub Pull Request

适合:不想把分支拉到本地也能看 review 反馈。

前置条件:需要先在仓库上开启 Codex Code review,参考 GitHub 集成

GitHub(评论驱动)

  1. 打开 GitHub 上的 Pull Request
  2. 留一条评论触发 Codex:
text
@codex review
  1. 可以附加具体关注点:
text
@codex review for security vulnerabilities and security concerns

常见问题

Q: 给 Codex 写 Prompt 时,复现步骤真的那么重要吗?

A: 是的,这是最关键的部分。高层描述("保存有时不生效")让 Codex 只能猜测,而精确的复现步骤(按哪个按钮、刷新后看什么)让 Codex 能自己触发 Bug 并验证修复效果。

Q: CLI 和 IDE 扩展哪个更适合日常使用?

A: 看习惯。IDE 扩展自动包含打开的文件为上下文,不用手动 @ 引用,适合边写代码边对话;CLI 更灵活,适合需要组合命令、查看完整输出记录、或者没有 GUI 的场景。

Q: 云端任务完成后,怎么把改动拿回本地?

A: 云端任务完成后,可以在 Codex 界面里直接创建 PR,然后在本地 git fetch && git checkout <branch-name> 拉取分支到本地继续工作。