從寫程式碼,到創作下一幕

探索 字節跳動 - 火山方舟 的 AI 程式設計與影片創作活動。

Agent Plan & Coding Plan

一站體驗多款熱門模型,為 AI 程式設計與智能體開發提供更多選擇。新使用者可聯絡(微信: goo_lvyouyou)免費體驗 9.9 agent plan。

Seedance 2.5

讓創意,躍然成片。探索 30 秒影片、多模態參考與局部編輯,把腦海中的畫面變成下一支作品。

繁體中文

Codex SDK

Codex SDK

以程式設計方式控制本機 Codex 智能體

如果你通過 Codex CLI、IDE 擴充套件或 Codex 雲端使用 Codex,也可以通過程式設計方式控制它。

當你需要執行以下操作時,可使用 SDK:

  • 在 CI/CD 流水線中控制 Codex
  • 建立自己的智能體,與 Codex 互動以執行復雜的工程任務
  • 將 Codex 整合到自己的內部工具和工作流程中
  • 將 Codex 整合到自己的應用中

使用 Codex SDK 自動執行編碼任務,包括 CI 中的作業。使用 Codex 應用伺服器建置自定義客戶端,以處理身份驗證、對話歷史記錄、審批和流式智能體事件。

codex mcp-server 命令和獨立的 codex-mcp-server 二進位制檔案已被移除。現有整合請改用 Codex 應用伺服器

如果你擁有測試版存取權限,並且需要掃描程式碼儲存庫或變更,以取得結構化的 安全發現和覆蓋情況,請使用 Codex Security TypeScript SDK

TypeScript 庫

TypeScript 庫可讓你的應用啟動、繼續和恢復本機 Codex 執行緒。

請在服務端使用該庫;它需要 Node.js 18 或更高版本。

安裝

首先,使用 npm 安裝 Codex SDK:

npm install @openai/codex-sdk

用法

建立一個 Codex 執行緒,並使用你的提示詞執行它。



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);
const result2 = await thread2.run("Pick up where you left off");

console.log(result2.finalResponse);

更多詳情,請參閱 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,建立執行緒,然後執行提示詞:

from openai_codex import Codex, Sandbox

with Codex() as codex:
    thread = codex.thread_start(
        model="gpt-5.6-terra",
        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-5.6-terra")
        result = await thread.run("Implement the plan")
        print(result.final_response)


asyncio.run(main())

沙箱預設

建立執行緒或為後續輪次更改其檔案系統存取權限時,使用同一組 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 儲存庫