跳转到内容

OpenResponses API

OpenClaw 的 Gateway(网关)可以提供一个 OpenResponses 兼容的 POST /v1/responses 端点。

此端点默认处于禁用状态。请先在配置中启用它。

  • POST /v1/responses
  • 与 Gateway(网关) 相同的端口(WS + HTTP 多路复用):http://<gateway-host>:<port>/v1/responses

在底层,请求作为正常的 Gateway(网关) 代理运行执行(与 openclaw agent 代码路径相同),因此路由/权限/配置与您的 Gateway(网关) 匹配。

操作行为与 OpenAI Chat Completions 一致:

  • 使用 Authorization: Bearer <token> 配合正常的 Gateway(网关) 身份验证配置
  • 将该端点视为对 gateway 实例的完整操作员访问权限
  • 对于共享密钥身份验证模式(tokenpassword),忽略较窄的持有者声明的 x-openclaw-scopes 值,并恢复正常的完整操作员默认值
  • 对于承载身份的可信 HTTP 模式(例如可信代理身份验证或 gateway.auth.mode="none"),仍然遵守请求上声明的操作员范围
  • 使用 model: "openclaw"model: "openclaw/default"model: "openclaw/<agentId>"x-openclaw-agent-id 选择代理
  • 当您想要覆盖所选代理的后端模型时,请使用 x-openclaw-model
  • 使用 x-openclaw-session-key 进行显式会话路由
  • 当您需要非默认的合成入站渠道上下文时,请使用 x-openclaw-message-channel

认证矩阵:

  • gateway.auth.mode="token""password" + Authorization: Bearer ...
    • 证明拥有共享的网关操作员密钥
    • 忽略较窄的 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

有关代理目标模型、openclaw/default、嵌入直通和后端模型覆盖如何协同工作的权威说明,请参阅 OpenAI Chat CompletionsModel list and agent routing

默认情况下,该端点是每个请求无状态的(每次调用都会生成一个新的会话密钥)。

如果请求包含 OpenResponses user 字符串,Gateway(网关) 会从中派生一个稳定的会话密钥,以便重复调用可以共享代理会话。

请求遵循 OpenResponses API,采用基于项的输入。目前支持:

  • input:字符串或项对象数组。
  • instructions:合并到系统提示中。
  • tools:客户端工具定义(函数工具)。
  • tool_choice:过滤或要求客户端工具。
  • stream:启用 SSE 流式传输。
  • max_output_tokens:尽力而为的输出限制(取决于提供商)。
  • user:稳定的会话路由。

接受但 当前忽略

  • max_tool_calls
  • reasoning
  • metadata
  • store
  • truncation

支持:

  • previous_response_id:当请求保持在同一个 agent/user/requested-会话 范围内时,OpenClaw 会重用之前的响应会话。

角色:systemdeveloperuserassistant

  • systemdeveloper 被附加到系统提示词。
  • 最近的 userfunction_call_output 项将成为“当前消息”。
  • 较早的用户/助手消息作为上下文历史包含在内。

function_call_output(基于轮次的工具)

Section titled “function_call_output(基于轮次的工具)”

将工具结果发送回模型:

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

出于架构兼容性原因被接受,但在构建提示词时被忽略。

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

如果代理决定调用工具,响应将返回一个 function_call 输出项。 然后,您需要发送一个带有 function_call_output 的后续请求以继续该回合。

支持 base64 或 URL 来源:

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

允许的 MIME 类型(当前):image/jpegimage/pngimage/gifimage/webpimage/heicimage/heif。 最大大小(当前):10MB。

支持 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。

当前行为:

  • 文件内容会被解码并添加到系统提示词中,而不是用户消息, 因此它是临时的(不会保存在会话历史中)。
  • PDF 会解析文本。如果找到的文本很少,首页将被栅格化 为图像并传递给模型。

PDF 解析使用兼容 Node 的 pdfjs-dist 旧版构建(无 worker)。现代 PDF.js 构建期望浏览器 worker/DOM 全局变量,因此未在 Gateway(网关) 中使用。

URL 获取默认值:

  • files.allowUrl: true
  • images.allowUrl: true
  • maxUrlParts: 8(每个请求中基于 URL 的 input_file + input_image 部分总计)
  • 请求受保护(DNS 解析、私有 IP 阻断、重定向限制、超时)。
  • 支持按输入类型(files.urlAllowlistimages.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 阻止。
  • 对于暴露于互联网的网关,除了应用程序级别的保护之外,还应应用网络出口控制。 请参阅安全性

设置 stream: true 以接收服务器发送事件 (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 方法错误

非流式传输:

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