跳到內容

使用 OpenResponses API 快速整合 Gateway

每次串接不同的 AI 服務都要重新寫一套 API 邏輯真的很煩,尤其是當你需要處理複雜的對話狀態或多模態輸入時。如果你正在尋找一種更統一、更直覺的方式來處理模型回應,OpenClaw Gateway 提供的 OpenResponses API 就是為了簡化這一切而設計的。

OpenClaw 的 Gateway 可以提供一個相容於 OpenResponses 的 POST /v1/responses 端點。這個端點預設是停用的,請記得先在 config 中開啟它。

  • POST /v1/responses
  • 埠號與 Gateway 相同(WS + HTTP 多路複用):http://<gateway-host>:<port>/v1/responses

在底層運作上,這些請求會像一般的 Gateway agent 一樣執行(與 openclaw agent 的程式碼路徑相同),因此路由、權限與設定都會與你的 Gateway 保持一致。

運作行為與 OpenAI Chat Completions 一致:

  • 使用 Authorization: Bearer <token> 搭配正常的 Gateway 認證設定
  • 將此端點視為該 Gateway 實例的完整操作員(operator)權限
  • 對於共享密鑰認證模式(token 和 password),會忽略較窄的 x-openclaw-scopes 值,並恢復正常的完整操作員預設權限
  • 對於信任身份的 HTTP 模式(例如信任代理認證或 gateway.auth.mode="none"),仍會尊重請求上宣告的操作員範圍(scopes)
  • 使用 model: "openclaw", model: "openclaw/default", model: "openclaw/<agentId>" 或 x-openclaw-agent-id 來選擇 agent
  • 當你想覆蓋所選 agent 的後端模型時,請使用 x-openclaw-model
  • 使用 x-openclaw-session-key 進行明確的會話路由
  • 當你需要非預設的合成接入頻道上下文時,請使用 x-openclaw-message-channel

認證矩陣:

  • gateway.auth.mode="token" 或 "password" + Authorization: Bearer ...
    • 證明持有共享的 Gateway 操作員密鑰
    • 忽略較窄的 x-openclaw-scopes
    • 恢復完整的預設操作員範圍集
    • 將此端點上的對話輪次視為擁有者發送者的輪次
  • 信任身份的 HTTP 模式(例如信任代理認證,或在私有接入點使用 gateway.auth.mode="none")
    • 尊重宣告的 x-openclaw-scopes 標頭
    • 僅在宣告的範圍中確實存在 operator.admin 時才獲得擁有者語義

使用 gateway.http.endpoints.responses.enabled 來啟用或停用此端點。

同樣的相容性範圍還包括:

  • GET /v1/models
  • GET /v1/models/{id}
  • POST /v1/embeddings
  • POST /v1/chat/completions

關於 agent 目標模型、openclaw/default、embeddings 傳遞以及後端模型覆蓋如何運作的權威解釋,請參考 OpenAI Chat Completions 和 模型列表與 agent 路由。

預設情況下,此端點對每個請求都是無狀態的(每次調用都會生成新的 session key)。

如果請求包含 OpenResponses 的 user 字串,Gateway 會從中衍生出一個穩定的 session key,這樣重複的調用就可以共享同一個 agent 會話。

請求遵循 OpenResponses API 的 item-based 輸入。目前支援:

  • input: 字串或項目物件陣列。
  • instructions: 合併到 system prompt 中。
  • tools: 用戶端工具定義(function tools)。
  • tool_choice: 過濾或要求用戶端工具。
  • stream: 啟用 SSE 串流。
  • max_output_tokens: 盡力而為的輸出限制(取決於供應商)。
  • user: 穩定的會話路由。

已接收但目前忽略的項:

  • max_tool_calls
  • reasoning
  • metadata
  • store
  • truncation

支援項:

  • previous_response_id: 當請求保持在相同的 agent/user/請求會話範圍內時,OpenClaw 會重用先前的回應會話。

角色:system, developer, user, assistant。

  • system 和 developer 會被附加到 system prompt。
  • 最近的 user 或 function_call_output 項目會成為「當前訊息」。
  • 較早的 user/assistant 訊息會作為上下文歷史記錄包含在內。

將工具結果傳回給模型:

{
"type": "function_call_output",
"call_id": "call_123",
"output": "{\"temperature\": \"72F\"}"
}

為了 schema 相容性而接受,但在構建 prompt 時會被忽略。

透過 tools: [{ type: "function", function: { name, description?, parameters? } }] 提供工具。

如果 agent 決定調用工具,回應會回傳一個 function_call 輸出項目。接著你需要發送一個帶有 function_call_output 的後續請求來繼續該輪次。

支援 base64 或 URL 來源:

{
"type": "input_image",
"source": { "type": "url", "url": "https://example.com/image.png" }
}

允許的 MIME 類型(目前):image/jpeg, image/png, image/gif, image/webp, image/heic, image/heif。 最大容量(目前):10MB。

支援 base64 或 URL 來源:

{
"type": "input_file",
"source": {
"type": "base64",
"media_type": "text/plain",
"data": "SGVsbG8gV29ybGQh",
"filename": "hello.txt"
}
}

允許的 MIME 類型(目前):text/plain, text/markdown, text/html, text/csv, application/json, application/pdf。

最大容量(目前):5MB。

目前的行為:

  • 檔案內容會被解碼並添加到 system prompt,而不是 user message,因此它是臨時性的(不會持久化在會話歷史中)。
  • PDF 會進行文字解析。如果找不到足夠的文字,前幾頁會被轉換成圖片並傳遞給模型。

PDF 解析使用 Node.js 友好的 pdfjs-dist legacy 版本(無 worker)。現代的 PDF.js 版本需要瀏覽器 worker/DOM 全域變數,因此在 Gateway 中不使用。

URL 抓取預設值:

  • files.allowUrl: true
  • images.allowUrl: true
  • maxUrlParts: 8(每個請求總共允許的 URL 型 input_file + input_image 數量)
  • 請求受到保護(DNS 解析、私有 IP 封鎖、重新導向限制、逾時)。
  • 支援按輸入類型設定選用的主機名稱白名單(files.urlAllowlist, images.urlAllowlist)。
    • 精確主機:"cdn.example.com"
    • 通配符子網域:"*.assets.example.com"(不匹配頂級域名)
    • 空白或省略白名單表示沒有主機名稱白名單限制。
  • 若要完全停用基於 URL 的抓取,請設定 files.allowUrl: false 和/或 images.allowUrl: false。

預設值可以在 gateway.http.endpoints.responses 下進行調整:

{
gateway: {
http: {
endpoints: {
responses: {
enabled: true,
maxBodyBytes: 20000000,
maxUrlParts: 8,
files: {
allowUrl: true,
urlAllowlist: ["cdn.example.com", "*.assets.example.com"],
allowedMimes: [
"text/plain",
"text/markdown",
"text/html",
"text/csv",
"application/json",
"application/pdf",
],
maxBytes: 5242880,
maxChars: 200000,
maxRedirects: 3,
timeoutMs: 10000,
pdf: {
maxPages: 4,
maxPixels: 4000000,
minTextChars: 200,
},
},
images: {
allowUrl: true,
urlAllowlist: ["images.example.com"],
allowedMimes: [
"image/jpeg",
"image/png",
"image/gif",
"image/webp",
"image/heic",
"image/heif",
],
maxBytes: 10485760,
maxRedirects: 3,
timeoutMs: 10000,
},
},
},
},
},
}

省略時的預設值:

  • maxBodyBytes: 20MB
  • maxUrlParts: 8
  • files.maxBytes: 5MB
  • files.maxChars: 200k
  • files.maxRedirects: 3
  • files.timeoutMs: 10s
  • files.pdf.maxPages: 4
  • files.pdf.maxPixels: 4,000,000
  • files.pdf.minTextChars: 200
  • images.maxBytes: 10MB
  • images.maxRedirects: 3
  • images.timeoutMs: 10s
  • HEIC/HEIF 的 input_image 來源會被接受,並在交付給供應商前正規化為 JPEG。

安全提示:

  • URL 白名單會在抓取前以及重新導向跳轉時強制執行。
  • 將主機名稱加入白名單並不會繞過私有/內部 IP 封鎖。
  • 對於暴露在網際網路上的 Gateway,除了應用程式層級的防護外,請務必實施網路流出控制。請參閱 Security。

設定 stream: true 以接收 Server-Sent Events (SSE):

  • Content-Type: text/event-stream
  • 每個事件行包含 event: <type> 和 data: <json>
  • 串流以 data: [DONE] 結束

目前發出的事件類型:

  • response.created
  • response.in_progress
  • response.output_item.added
  • response.content_part.added
  • response.output_text.delta
  • response.output_text.done
  • response.content_part.done
  • response.output_item.done
  • response.completed
  • response.failed(發生錯誤時)

當底層供應商回報 token 計數時,會填充 usage 欄位。

錯誤使用如下的 JSON 物件:

{ "error": { "message": "...", "type": "invalid_request_error" } }

常見情況:

  • 401 缺少或無效的認證
  • 400 無效的請求主體
  • 405 錯誤的 HTTP 方法

非串流模式:

Terminal window
curl -sS http://127.0.0.1:18789/v1/responses \
-H 'Authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-H 'x-openclaw-agent-id: main' \
-d '{
"model": "openclaw",
"input": "hi"
}'

串流模式:

Terminal window
curl -N http://127.0.0.1:18789/v1/responses \
-H 'Authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-H 'x-openclaw-agent-id: main' \
-d '{
"model": "openclaw",
"stream": true,
"input": "hi"
}'

如果你在設定過程中遇到任何問題,隨時可以使用 AI Setup Assistant 尋求協助。

OpenClaw

OpenClaw Expert

還是卡住了?

如果這篇文件沒有解決你的情境,直接問 OpenClaw Expert,拿到可執行步驟。