Appearance
SDK 与 IDE 集成 — 概念
SDK 架构
Codex 提供两个 SDK——TypeScript 和 Python——采用了截然不同的集成架构:
两种 SDK 的架构对比:
| 维度 | TypeScript SDK | Python SDK |
|---|---|---|
| 通信方式 | 子进程(codex exec) | JSON-RPC 直连 |
| 进程模型 | 启动独立进程 | 启动/连接 App Server |
| 异步支持 | Promise/async-await | 同步 + 异步双 API |
| 输出格式 | JSON Lines 解析 | 结构化事件流 |
| 配置方式 | CodexOptions | CodexConfig |
IDE 集成方式
SDK 为 IDE 插件(如 VS Code、JetBrains)提供了集成接口:
IDE 集成的关键设计决策:
- TypeScript SDK 适用于基于 Node.js 的 IDE(VS Code),通过
codex exec --experimental-json子进程模式运行,每个请求启动新的执行周期 - Python SDK 适用于 Python 环境,直接使用 App Server Protocol 的 JSON-RPC 协议通信,支持持久连接
- 两者都通过 App Server Protocol 与核心通信,确保功能一致性
API 设计
TypeScript SDK API
typescript
class Codex {
constructor(options?: CodexOptions)
// 启动新线程
startThread(options?: ThreadOptions): Thread
// 恢复已有线程
resumeThread(id: string, options?: ThreadOptions): Thread
}Python SDK API
Python SDK 提供同步和异步两套完整 API:
python
# 同步 API
class Codex:
def start_thread(self, **kwargs) -> Thread: ...
def resume_thread(self, id: str) -> Thread: ...
class Thread:
def turn(self, input: Input) -> TurnHandle: ...
def close(self): ...
class TurnHandle:
def result(self) -> TurnResult: ...
def events(self) -> Iterator[ThreadEvent]: ...
# 异步 API
class AsyncCodex:
async def start_thread(self, **kwargs) -> AsyncThread: ...
class AsyncThread:
async def turn(self, input: Input) -> AsyncTurnHandle: ...
class AsyncTurnHandle:
async def result(self) -> TurnResult: ...
async def events(self) -> AsyncIterator[ThreadEvent]: ...Python SDK 的输入类型系统:
| 类型 | 说明 |
|---|---|
Input | 统一输入类型 |
InputItem | 单个输入条目 |
RunInput | 运行输入 |
TextInput | 文本消息 |
ImageInput | 远程图片 URL |
LocalImageInput | 本地图片文件 |
SkillInput | 技能调用 |
MentionInput | 提及(@引用) |
类型系统
Python SDK 的配置和错误类型:
python
# 配置
class CodexConfig:
model: str | None
approval_mode: ApprovalMode # suggest / auto-edit / full-auto
sandbox: Sandbox # off / platform / docker
# 错误层级
CodexError # 基础错误
├── TransportClosedError # 传输关闭
├── JsonRpcError # JSON-RPC 协议错误
│ ├── ParseError # 解析错误
│ ├── InvalidRequestError # 无效请求
│ ├── MethodNotFoundError # 方法未找到
│ ├── InvalidParamsError # 参数无效
│ └── InternalRpcError # 服务器内部错误
├── CodexRpcError # Codex 特定 RPC 错误
│ ├── ServerBusyError # 服务器繁忙
│ └── RetryLimitExceededError # 重试超限错误处理工具:
python
def retry_on_overload(fn, *, max_retries=3):
"""自动重试可恢复的错误(ServerBusyError 等)"""
def is_retryable_error(error: CodexError) -> bool:
"""判断错误是否可重试"""相关概念
- App Server Protocol — SDK 使用的通信协议
- Backend Client — SDK 间接触发的后端调用
- MCP — SDK 可调用的 MCP 工具
- Ephemeral — SDK 的临时执行模式