用途:Codex CLI 是 OpenAI 提供的终端 AI 编程智能体,用于处理复杂编码任务,可委托多文件项目、迭代调试等,能在沙箱环境中本地读取、编辑和运行代码。
主要能力:以 codex exec 进行非交互式程序化调用,支持从标准输入管道传入指令;可通过 --full-auto 实现工作区写入加最小审批,或指定审批与沙箱策略。常用标志包括 -m/--model 覆盖模型(如 gpt-5-codex)、-a/--ask-for-approval(untrusted / on-request / never)、-s/--sandbox(read-only / workspace-write / danger-full-access)、--add-dir 授予额外目录写权限、-C/--cd 设定工作目录、-i/--image 附加图片、-o/--output-last-message 输出最终响应到文件、--json 输出换行分隔 JSON 事件、--ephemeral 不持久化会话、--skip-git-repo-check 允许在 git 仓库外运行、--oss 通过 Ollama 使用本地 OSS 模型。
检测安装:which codex 应返回路径;codex --version 查看版本。安装:npm i -g @openai/codex,需 OpenAI API 密钥或 ChatGPT 账户(首次运行提示)。
使用方式示例:codex exec "Refactor the auth module to use JWT tokens";echo "Fix the failing test in src/utils.test.ts" | codex exec -;codex exec --full-auto "Add error handling to all API routes";codex exec -a never -s workspace-write "Update dependencies and fix breaking changes";codex exec -o result.md "Explain the architecture of this project"。
适用场景:多文件项目重构、批量修复测试或类型错误、代码质量分析(可结合 -s read-only 只读分析)、依赖更新等。
限制:安装与版本需以实际环境为准;使用 -a never 跳过全部审批具有危险性,仅建议在沙箱环境中使用;模型选择、账户与 API 密钥要求以官方为准。更多 CLI 参考见 https://developers.openai.com/codex/cli/reference,SDK 见 https://developers.openai.com/codex/sdk。