如果您通过 Codex CLI、IDE 扩展程序或 Codex 云端使用 Codex,也可以通过代码控制它。
如果您有以下需求,可以使用 SDK:
- 在 CI/CD 流水线中控制 Codex
- 创建自己的智能体,让它与 Codex 协作完成复杂的工程任务
- 将 Codex 集成到内部工具和工作流中
- 将 Codex 集成到自己的应用中
使用 Codex SDK 可以自动完成编码任务,包括 CI 中的作业。使用 Codex App Server 可以构建自定义客户端,用于处理身份验证、对话历史记录和审批,以及接收并处理智能体的事件流。
codex mcp-server 命令和独立的 codex-mcp-server 二进制文件已移除。对于现有集成,请改用 Codex App Server。
如果您已获得测试版访问权限,需要扫描代码仓库或代码变更,并获取结构化的安全发现和扫描覆盖情况,请使用 Codex Security TypeScript SDK。
TypeScript 库
使用 TypeScript 库,您的应用可以创建本地 Codex 对话线程,在同一线程中继续对话,也可以恢复之前的线程。
请在服务端使用此库。它需要 Node.js 18 或更高版本。
安装
首先,使用 npm 安装 Codex SDK:
npm install @openai/codex-sdk
用法
创建一个 Codex 对话线程,然后传入提示词来运行任务。
import { Codex } from "@openai/codex-sdk";
const codex = new Codex();
const thread = codex.startThread();
const result = await thread.run(
"Make a plan to diagnose and fix the CI failures"
);
console.log(result.finalResponse);再次调用 run() 即可在同一线程中继续对话。您也可以提供线程 ID 来恢复之前的线程。
// running the same thread
const result = await thread.run("Implement the plan");
console.log(result.finalResponse);
// resuming past thread
const threadId = thread.id!;
const thread2 = codex.resumeThread(threadId, {
sandboxMode: "workspace-write",
});
const result2 = await thread2.run("Pick up where you left off");
console.log(result2.finalResponse);要识别由您的集成创建的线程,请在创建线程时设置 threadSource。例如,codex.startThread({ threadSource: "ci" }) 会将线程的来源标记为 ci。恢复线程时会保留其原始来源。
创建线程时,按以下方式设置模型和沙盒:
import { Codex } from "@openai/codex-sdk";
const codex = new Codex();
const thread = codex.startThread({
model: "gpt-6.1-sol",
modelReasoningEffort: "low",
sandboxMode: "workspace-write",
});
const result = await thread.run(
"Make a plan to diagnose and fix the CI failures"
);
console.log(result.finalResponse);TypeScript SDK 采用异步方式运行。请在应用的异步函数中使用 await 等待结果:
import { Codex } from "@openai/codex-sdk";
async function main(): Promise<void> {
const codex = new Codex();
const thread = codex.startThread();
const result = await thread.run("Implement the plan");
console.log(result.finalResponse);
}
await main();覆盖配置
传入 config 可以为 Codex 设置结构化配置。对于无法用点分路径表示的键,请使用 configOverrides 将每个原始 TOML 字符串作为单独的 --config 参数传入:
const codex = new Codex({
config: { default_permissions: "audit" },
configOverrides: [
'permissions.audit.filesystem={":root"="read","/path/to/project/.env"="deny"}',
],
});原始 TOML 覆盖项的优先级高于结构化的 config。SDK 随后会应用 baseUrl 等托管设置以及线程专属选项,因此这些设置和选项的优先级高于前两者。上述覆盖项不会修改您的 Codex 配置文件。
更多详情,请参阅 TypeScript 代码仓库。
Python 库
Python SDK 通过 JSON-RPC 控制本地 Codex App Server。它需要 Python 3.10 或更高版本。已发布的 SDK 构建版本依赖固定版本的 Codex CLI 运行时。
安装
运行以下命令安装 SDK:
pip install openai-codex
已发布的 SDK 会自动使用其指定版本的运行时。仅当您明确需要使用某个特定的本地 Codex 可执行文件时,才传入 CodexConfig(codex_bin=...)。
Python SDK 已发布稳定版。运行 pip install openai-codex 可安装最新稳定版。如果您希望安装更新的预发布版本,请使用 pip install --pre openai-codex。
用法
启动 Codex,创建对话线程,然后传入提示词来运行任务。以下示例使用 GPT-6.1 Sol,您登录的账号必须有权使用该模型。如果您的账号无权使用,请省略 model,以使用您配置的默认模型。请参阅各套餐的模型可用性。
from openai_codex import Codex, Sandbox
with Codex() as codex:
thread = codex.thread_start(
model="gpt-6.1-sol",
sandbox=Sandbox.workspace_write,
)
result = thread.run("Make a plan to diagnose and fix the CI failures")
print(result.final_response)如果您的应用已采用异步方式运行,请使用 AsyncCodex:
import asyncio
from openai_codex import AsyncCodex
async def main() -> None:
async with AsyncCodex() as codex:
thread = await codex.thread_start(model="gpt-6.1-sol")
result = await thread.run("Implement the plan")
print(result.final_response)
asyncio.run(main())传入来自其他系统的内容
如果内容来自工具、应用或其他智能体,请将 ExternalMessage 作为 thread.run() 或 thread.turn() 的完整输入。例如,ExternalMessage(tool_name="issue_tracker", content=issue_body) 指明了提供议题描述的工具。Codex 将该描述视为工具输出,其优先级低于用户指令和开发者指令。这条消息既可以开启新一轮对话,也可以加入正在进行的常规轮次。
恢复线程时跳过历史记录加载
将 include_turns=False 传给 codex.thread_resume() 或 codex.thread_fork(),返回的响应中就不会包含以往的轮次。模型仍保有该线程的上下文。如果之后需要获取历史记录,请调用 thread.read(include_turns=True)。
使用 Codex CLI 0.166.0 或更高版本时,请使用线程中保存的模型提供商来恢复线程。如果切换模型提供商,请调用 codex.thread_fork(thread_id),以便通过新的提供商基于同一份历史记录继续对话。
为单个轮次设置服务层级
如果只想为一个新轮次选择服务层级,请将 turn_service_tier 传给 run() 或 turn()。例如,设置 turn_service_tier="default" 后,该轮次会使用标准速度。如果要更改本轮及后续轮次的服务层级,请传入 service_tier。加入正在进行的轮次时,该轮次会沿用原有的服务层级。
如果您自行提供 Codex 可执行文件,则使用 ExternalMessage、在恢复或派生线程时显式指定 include_turns,以及使用 turn_service_tier 均需要 Codex CLI 0.151.0 或更高版本。
沙盒预设
创建线程时,或为后续轮次更改线程的文件系统访问权限时,都可以使用同一组 Sandbox 预设:
from openai_codex import Codex, Sandbox
with Codex() as codex:
thread = codex.thread_start(sandbox=Sandbox.workspace_write)
thread.run("Make the requested change.")
review = thread.run("Review the diff only.", sandbox=Sandbox.read_only)可用预设如下:
Sandbox.read_only:仅允许读取文件。Sandbox.workspace_write:允许读取文件,并允许在工作空间和已配置的可写根目录内写入文件。Sandbox.full_access:运行时不限制文件系统访问。
如果您省略 sandbox=,App Server 会使用其配置的默认值。传给 run(...) 或 turn(...) 的沙盒设置会在本轮以及同一线程的后续轮次中生效。
更多详情,请参阅 Python 代码仓库。