OpenResponses API(HTTP)

OpenClaw 的 Gateway 可以提供一個 OpenResponses 相容的 POST /v1/responses 端點。

這個端點預設是停用的,需要先在設定中啟用。

  • POST /v1/responses
  • 與 Gateway 共用相同連接埠(WS + HTTP 多工):http://<gateway-host>:<port>/v1/responses

底層實作上,請求會以標準的 Gateway 代理執行流程處理(和 openclaw agent 走相同的程式碼路徑),路由、權限、設定全部共用。

認證、安全性與路由

操作行為和 OpenAI Chat Completions 一致:

  • 使用 Authorization: Bearer <token> 搭配一般的 Gateway 認證設定
  • 將此端點視為 gateway 實例的完整操作者存取介面
  • model: "openclaw:<agentId>"model: "agent:<agentId>"x-openclaw-agent-id 選擇代理
  • x-openclaw-session-key 明確指定 session 路由

透過 gateway.http.endpoints.responses.enabled 啟用或停用此端點。

Session 行為

預設情況下端點是每次請求無狀態(每次呼叫產生新的 session key)。

如果請求中包含 OpenResponses 的 user 字串,Gateway 會從中衍生出穩定的 session key,讓重複呼叫可以共用同一個代理 session。

請求格式(已支援)

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

  • input:字串或 item 物件陣列。
  • instructions:合併至系統提示詞。
  • tools:用戶端工具定義(function 工具)。
  • tool_choice:篩選或要求用戶端工具。
  • stream:啟用 SSE 串流。
  • max_output_tokens:盡力遵守的輸出上限(依 provider 而定)。
  • user:穩定的 session 路由。

可接受但目前忽略的欄位:

  • max_tool_calls
  • reasoning
  • metadata
  • store
  • previous_response_id
  • truncation

Items(input)

message

角色:systemdeveloperuserassistant

  • systemdeveloper 會附加到系統提示詞中。
  • 最近一筆 userfunction_call_output item 會成為「當前訊息」。
  • 先前的 user/assistant 訊息會作為上下文歷史記錄包含進去。

function_call_output(回合制工具)

將工具結果回傳給模型:

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

reasoningitem_reference

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

工具(用戶端 function 工具)

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

如果代理決定呼叫某個工具,回應中會包含一個 function_call 輸出項目。 接著你需要發送一個帶有 function_call_output 的後續請求來繼續該回合。

圖片(input_image

支援 base64 或 URL 來源:

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

允許的 MIME 類型(目前):image/jpegimage/pngimage/gifimage/webpimage/heicimage/heif。 大小上限(目前):10MB。

檔案(input_file

支援 base64 或 URL 來源:

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

允許的 MIME 類型(目前):text/plaintext/markdowntext/htmltext/csvapplication/jsonapplication/pdf

大小上限(目前):5MB。

目前的行為:

  • 檔案內容解碼後會加入系統提示詞,而非使用者訊息,因此是暫時性的(不會保存在 session 歷史中)。
  • PDF 會被解析取出文字。如果文字過少,會把前幾頁轉成圖片傳給模型。

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

URL 擷取預設值:

  • files.allowUrltrue
  • images.allowUrltrue
  • maxUrlParts8(每次請求中 URL 型 input_file + input_image 的總數上限)
  • 請求會受到保護(DNS 解析、私有 IP 阻擋、重新導向次數限制、逾時)。
  • 可選的 hostname 白名單,依輸入類型分別設定(files.urlAllowlistimages.urlAllowlist)。
    • 精確主機名:"cdn.example.com"
    • 萬用子網域:"*.assets.example.com"(不符合 apex 網域)

檔案與圖片限制(設定)

預設值可在 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:10 秒
  • files.pdf.maxPages:4
  • files.pdf.maxPixels:4,000,000
  • files.pdf.minTextChars:200
  • images.maxBytes:10MB
  • images.maxRedirects:3
  • images.timeoutMs:10 秒
  • HEIC/HEIF input_image 來源會在送交 provider 前正規化為 JPEG。

安全注意事項:

  • URL 白名單在擷取前和每次重新導向時都會驗證。
  • 將 hostname 加入白名單不會繞過私有/內部 IP 封鎖。
  • 對於暴露在網路上的 gateway,除了應用層防護外,還應設定網路出口控管。 詳見 安全性

串流(SSE)

設定 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(發生錯誤時)

Usage

當底層 provider 回報 token 計數時,usage 欄位會被填入。

錯誤

錯誤以 JSON 物件回傳:

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

常見情況:

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

範例

非串流:

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"
  }'

串流:

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"
  }'