SDK — ควบคุม Codex แบบ Programmatic
Codex SDK
ใช้ SDK เมื่อต้องการควบคุม Codex แบบ Programmatic ใน CI/CD Pipeline หรือสร้าง Agent ของตัวเอง
ใช้ SDK ในกรณีต่อไปนี้:
- ควบคุม Codex เป็นส่วนหนึ่งของ CI/CD Pipeline
- สร้าง Agent ของตัวเองที่ทำงานร่วมกับ Codex
- ฝัง Codex เข้าใน Internal Tools และ Workflows
- Integrate Codex ใน Application ของตัวเอง
TypeScript Library
TypeScript Library ให้ Control Codex จาก Application แบบ Comprehensive กว่า Non-interactive Mode
ใช้ Library ทางฝั่ง Server-side — ต้องการ Node.js 18 หรือใหม่กว่า
ติดตั้ง
npm install @openai/codex-sdk
การใช้งาน
เริ่ม Thread กับ Codex และรันด้วย Prompt:
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);
เรียก run() อีกครั้งเพื่อต่อบน Thread เดิม หรือ Resume Thread เก่าโดยระบุ Thread ID:
// running the same thread
const result = await thread.run("Implement the plan");
console.log(result);
// resuming past thread
const threadId = "<thread-id>";
const thread2 = codex.resumeThread(threadId);
const result2 = await thread2.run("Pick up where you left off");
console.log(result2);
Python Library
Python SDK ควบคุม Local Codex App-server ผ่าน JSON-RPC ต้องการ Python 3.10 หรือใหม่กว่า
ติดตั้ง Python SDK
pip install openai-codex
การใช้งาน Python SDK
เริ่ม Codex, สร้าง Thread และรัน Prompt:
from openai_codex import Codex, Sandbox
with Codex() as codex:
thread = codex.thread_start(
model="gpt-5.4",
sandbox=Sandbox.workspace_write,
)
result = thread.run("Make a plan to diagnose and fix the CI failures")
print(result.final_response)
ใช้ AsyncCodex เมื่อ Application เป็น Async:
import asyncio
from openai_codex import AsyncCodex
async def main() -> None:
async with AsyncCodex() as codex:
thread = await codex.thread_start(model="gpt-5.4")
result = await thread.run("Implement the plan")
print(result.final_response)
asyncio.run(main())
Sandbox Presets
ใช้ Sandbox Presets เดียวกันเมื่อสร้าง Thread:
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)
Presets ที่มีให้ใช้:
Sandbox.read_only— อ่านไฟล์ได้แต่ไม่อนุญาตให้เขียนSandbox.workspace_write— อ่านไฟล์และเขียนใน Workspace ได้Sandbox.full_access— รันโดยไม่มีข้อจำกัดด้าน Filesystem