跳至內容

建立工作階段

POST/v1/agents/sessions

為已儲存的 Agent 版本,或在請求中定義的內嵌 Agent 建立工作階段。Agent 設定、技能版本與 MCP 連線會在工作階段的整個生命週期內凍結。工作階段一律在 SandBase 的託管雲端執行,因此無須選擇執行環境。若設定 stream=true 並提供初始輸入,回應會是第一個 turn 的 201 Server-Sent Events 串流,而非 JSON 工作階段。執行准入(餘額與組織狀態)會在此檢查。OpenAI SDK 需要 environment 引數;請參閱 OpenAI 相容性(/agents/openai-compatibility)。

標頭

選用<string>Idempotency-Key

選用。省略時服務會自動產生一組,因此重試的請求可能會建立重複項目。重試時請傳送您自己的固定金鑰。

請求主體

嚴格的 JSON 物件。

選用<string>agent_id

已儲存的 Agent ID(agt_ 前綴)。該 Agent 必須為 active 且 projection_status 為 ready。可傳送 agent_id、agent 或兩者。

選用<object>agent

Agent 設定。可接受的欄位有 model、instructions、runtime_profile(codex、claude 或 mcode;預設 codex)、service_tier、tools、text、reasoning 與 multi_agent,驗證方式與建立 Agent 相同。未提供 agent_id 時,它會定義內嵌 Agent,且 model 為必填;內嵌 Agent 不帶任何技能或 MCP 連線。提供 agent_id 時,您傳送的每個欄位只會在此工作階段中取代已儲存版本的對應欄位。未知欄位會回傳 400。

選用<integer>version

Agent 版本。預設為目前版本。內嵌 Agent 不可使用。

選用<string>title

工作階段標題。

選用<string | array>input

選用的第一個輸入:字串,或恰好包含一則訊息的陣列,例如 [{"role":"user","content":[{"type":"input_text","text":"…"}]}]。單純的物件會回傳 400。

選用<string>model

模型覆寫。agent.model 是 OpenAI SDK 的形式;兩者同時傳送時必須一致。

選用<object>metadata

最多 16 組字串鍵值。

選用<boolean>stream

設為 true 時,必須提供非空的輸入,並回傳 201 與 text/event-stream 主體。事件採用 Stream 事件格式(event: <type>, data: <json>);第一個事件 agent.session.created 帶有工作階段快照,之後是該 turn 的 item 與輸出事件。在串流開始前發生的驗證、准入及其他錯誤會回傳一般的 JSON 錯誤。預設為 false。

選用<object>environment

僅供 OpenAI SDK 相容使用。直接呼叫 HTTP 時請省略;接受 {"type":"openai_hosted"},其他任何類型都會回傳 400。

選用<object>model_provider

模型憑證。省略時使用您呼叫時的 API 金鑰。{"type":"custom","base_url":"https://…","api_key":"…"} 會呼叫您自己的 OpenAI 相容端點。僅可寫入。

選用<object>github_workspace

要為此工作階段複製到 /workspace 的 GitHub 儲存庫:{"repository_id":"1195090324","ref":"main","connection_id":"plc_…"}。只有 repository_id 為必填;ref 預設為預設分支。省略或為 null 時,會使用 Agent 的工作區儲存庫(若有),否則不複製任何內容。該儲存庫必須在 Agent 的 GitHub 連線範圍內。請參閱 GitHub 儲存庫工作區(/agents/github-workspace)。

工作階段物件

由 create、get、update、rename、archive 與 unarchive 回傳。

stringid必填

帶有 ses_ 前綴的工作階段 ID。

stringstatus必填

執行狀態:idle、in_progress 或 failed。尚不支援 requires_action,會以 502 executor_contract_error 呈現。

stringlifecycle_status必填

active 或 archived。已封存的工作階段會拒絕輸入。

object · nullfailure必填

公開的失敗資訊,包含 code、message、action 與選用的 retryable。

string · integer · stringagent_id · agent_version · model必填

凍結的 Agent 版本與實際使用的模型。

object · object | nullagent · usage必填

實際 Agent 設定的 OpenAI 格式快照,以及 token 用量(在 turn 回報用量前為 null)。

選用<string · null>source · source_id

工作階段的來源。以 POST /v1/agents/sessions 建立的工作階段會回報 direct,且 source_id 為 null。

integerrow_version必填

用於 rename、archive 與 unarchive 的比較並設定(compare-and-set)權杖。