当您将 Codex 视作一位拥有明确上下文和清晰“完成”定义的队友时,它的表现最佳。本页提供了 Codex IDE 扩展、Codex CLI 和 Codex 云端的端到端工作流示例。
如果您是 Codex 新手,请先阅读 提示 ,然后再回到这里查看具体实操指南。
如何阅读这些示例
每个工作流包括:
- 何时使用 以及哪种 Codex 交互界面最合适(IDE、CLI 或云端)。
- 步骤 with example user prompts.
- 上下文说明:Codex 自动可见的内容与您应附加的内容的对比。
- 验证:如何检查输出。
Note: IDE 扩展会自动将您打开的文件作为上下文包含在内。在 CLI 中,您通常需要显式指明路径(或使用
/mentionand@路径自动补全来附加文件)。
解释代码库
当您正在入职、接手某项服务,或试图理解某个协议、数据模型或请求流程时,请使用此方法。
IDE 扩展工作流(本地探索最快)
-
打开最相关的文件。
-
选中您关注的代码(可选,但推荐)。
-
向 Codex 提示:
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
Verification:
- 要求提供您可以快速验证的图表或清单:
Summarize the request flow as a numbered list of steps. Then list the files involved.
CLI 工作流(适合需要记录和 shell 命令的场景)
-
启动交互式会话:
codex -
附加文件(可选)并提示:
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 工作流(重现与验证的紧密循环)
-
在仓库根目录启动 Codex:
codex -
向 Codex 提供重现步骤,以及您怀疑的文件:
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 提供:命令输出、发现的调用点,以及它触发的任何堆栈跟踪。
Verification:
- Codex 在修复后应重新运行重现步骤。
- 如果您有标准的检查流水线,请让它运行:
After the fix, run lint + the smallest relevant test suite. Report the commands and results.
IDE 扩展工作流
-
打开您认为存在 Bug 的文件及其最近的调用方。
-
向 Codex 提示:
Find the bug causing "Saved" to show without persisting changes. After proposing the fix, tell me how to verify it in the UI.
编写测试
当您希望明确界定要测试的范围时,请使用此方法。
IDE 扩展工作流(基于选区)
-
打开包含该函数的文件。
-
选中定义该函数的代码行。从命令面板中选择“Add to Codex Thread”以将这些行添加到上下文中。
-
向 Codex 提示:
Write a unit test for this function. Follow conventions used in other tests.
上下文说明:
- 由“Add to Codex Thread”命令提供:选中的行(即“行号”范围)以及打开的文件。
CLI 工作流(提示词中描述路径和行范围)
-
启动 Codex:
codex -
附带函数名提示:
Add a test for the invert_list function in @transform.ts. Cover the happy path plus edge cases.
根据截图制作原型
当您有设计模型、截图或 UI 参考图并希望快速获得可用的原型时,请使用此方法。
CLI 工作流(图像 + 提示词)
-
将截图保存在本地(例如
./specs/ui.png). -
运行 Codex:
codex -
将图像文件拖入终端以将其附加到提示词中。
-
补充约束条件和结构:
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
上下文说明:
- 图像提供了视觉需求,但您仍需指明实现约束(框架、路由、组件样式)。
- 为了获得最佳效果,请以文本形式包含任何不明显的视觉交互行为(悬停状态、验证规则、键盘交互)。
Verification:
- 让 Codex 运行开发服务器(如果允许)并确切告诉您在哪里查看:
Start the dev server and tell me the local URL/route to view the prototype.
IDE 扩展工作流(图像 + 现有文件)
-
在 Codex 聊天中附加图像(拖放或粘贴)。
-
向 Codex 提示:
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 工作流(运行 Vite,然后通过小提示词进行迭代)
-
启动 Codex:
codex -
在单独的终端窗口中启动开发服务器:
npm run dev -
提示 Codex 进行修改:
Propose 2-3 styling improvements for the landing page. -
选择一个方向,并通过简短、具体的提示进行迭代:
Go with option 2. Change only the header: - make the typography more editorial - increase whitespace - ensure it still looks good on mobile -
使用专注的请求重复此过程:
Next iteration: reduce visual noise. Keep the layout, but simplify colors and remove any redundant borders.
Verification:
- 在代码更新时于浏览器中“实时”查看更改。
- 提交您满意的更改,并还原不满意的更改。
- 如果您还原或修改了某项更改,请告知 Codex,以免它在处理下一个提示时覆盖该更改。
将重构任务委托给云端
当您希望先进行仔细的设计(本地上下文、快速检查),然后将冗长的实现工作外包给可以并行运行的云端任务时,请使用此方法。
本地规划 (IDE)
-
确保您当前的工作已提交或至少已暂存,以便您能干净地比较更改。
-
让 Codex 生成重构计划。如果您拥有
$plan技能,请显式调用它:$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 -
审查该计划并协商修改:
Revise the plan to: - specify exactly which files move in each milestone - include a rollback strategy
上下文说明:
- 当 Codex 能够扫描本地当前代码(入口点、模块边界、依赖关系图提示)时,规划的效果最佳。
云端委托(IDE → 云端)
-
如果您还没有这样做,请设置一个 Codex 云端环境.
-
点击提示词输入框下方的云图标,并选择您的云端环境。
-
当您输入下一个提示时,Codex 会在云端创建一个新线程,该线程会继承现有线程的上下文(包括计划和任何本地源代码更改)。
Implement Milestone 1 from the plan. -
审查云端差异,如需迭代则继续迭代。
-
直接从云端创建 PR,或将更改拉取到本地以进行测试和收尾工作。
-
针对计划中的其他里程碑继续迭代。
进行本地代码审查
当你想在提交或创建 PR 之前让人帮忙审查代码时,请使用此功能。
CLI 工作流(审查你的工作树)
-
启动 Codex:
codex -
运行 review 命令:
/review -
可选:提供自定义的关注指令:
/review Focus on edge cases and security issues
Verification:
- 根据审查反馈应用修复,然后重新运行
/review以确认问题已解决。
审查 GitHub 拉取请求
当你想要获取审查反馈而又不想在本地拉取分支时,请使用此功能。
在使用此功能之前,你需要先在你的仓库中启用 Codex 代码审查 。请参见 代码审查.
GitHub 工作流(基于评论驱动)
-
在 GitHub 上打开拉取请求。
-
发表评论并 @ Codex,明确指出重点关注领域:
@codex review -
可选:提供更明确的指示。
@codex review for security vulnerabilities and security concerns
更新文档
当你需要准确且清晰的文档变更时,请使用此工作流。
IDE 或 CLI 工作流(本地编辑 + 本地验证)
-
确定需要修改的文档文件并打开它们(IDE),或
@提及它们(IDE 或 CLI)。 -
向 Codex 提示范围和验证要求:
Update the "advanced features" documentation to provide authentication troubleshooting guidance. Verify that all links are valid. -
Codex 起草变更后,审查文档并根据需要迭代。
Verification:
- 阅读渲染后的页面。