POST /v1/sessions/{session_id}/events
向 Session 发送事件(通常是用户消息),触发 Agent 开始处理。这是一个异步操作:消息被接受后,Agent 会在后台开始生成回复,可通过 SSE 流或列表事件 API 获取结果。
请求头
| 头部 | 必选 | 说明 |
|---|---|---|
Authorization | 是 | Bearer <PAT> |
Content-Type | 是 | application/json |
路径参数
| 参数 | 类型 | 必选 | 说明 |
|---|---|---|---|
session_id | string | 是 | Session ID(sess_ 前缀) |
请求体
| 字段 | 类型 | 必选 | 说明 |
|---|---|---|---|
events | array | 是 | 事件对象数组 |
events[].type | string | 是 | 事件类型 |
events[].content | string | array | 视类型 | 消息内容(user.message 类型时必填) |
events[].content[].type | string | 是 | 内容块类型,如 text |
events[].content[].text | string | 是 | 文本内容 |
events[].file_attachments | array | 否 | user.message 的可选文件附件元数据。服务端会保留在事件中,并转发到 turn 输入。 |
支持的事件类型
| type | 说明 | 必填字段 |
|---|---|---|
user.message | 用户发送消息 | content |
user.interrupt | 用户中断 Agent 执行 | - |
user.tool_confirmation | 对需要确认的 Agent 工具调用授权或拒绝 | tool_use_id、result(allow 或 deny) |
user.custom_tool_result | 返回 client-side 自定义工具的执行结果 | custom_tool_use_id;content 可选,默认保存为空 text block |
user.define_outcome | 用户定义预期结果 | 服务端不强制额外必填字段 |
session.status_idle | Worker/session 状态事件 | 服务端不强制额外必填字段 |
turn_completed | Worker 终态事件 | 服务端不强制额外必填字段 |
Human-in-the-loop 响应事件
当某一轮需要人工输入时,事件流会发送session.status_idle,其中 stop_reason.type 为 "requires_action",并带有 event_ids 数组。每个 ID 都指向需要响应的事件:
- 对
agent.tool_use,发送user.tool_confirmation,并将tool_use_id设为该agent.tool_use事件的id - 对
agent.custom_tool_use,客户端先执行自定义工具,再发送user.custom_tool_result,并将custom_tool_use_id设为该agent.custom_tool_use事件的id
user.custom_tool_result 的 content 可以是字符串、单个 text content block 或 text content block 数组。返回事件中会统一保存为 content block 数组。
user.tool_confirmation 的最新字段是 result,取值 "allow" 或 "deny"。服务端仍兼容旧字段 decision("approve" 或 "deny"),但只有在未提供 result 时才读取,返回事件会统一保存为 result。
示例请求
示例响应
HTTP 202 Accepted 返回已创建的事件列表。HTTP 202 表示消息已被接受并进入处理队列。响应字段
顶层响应:| 字段 | 类型 | 说明 |
|---|---|---|
data | array | 已创建事件对象数组,顺序与请求中的 events 一致 |
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 事件唯一标识(evt_ 前缀) |
type | string | 事件类型 |
content | string 或 array | 事件特定内容。user.message 保留请求中的形态;user.custom_tool_result 保存为 text content block 数组。 |
session_id | string | 所属 Session ID |
turn_id | string | 事件属于某个 turn 时存在。user.message 会创建新的 turn_id。 |
schema_version | string | Schema 版本号 |
created_at | string | 创建时间(ISO 8601) |
processed_at | string | 处理时间(ISO 8601) |
tool_use_id | string | user.tool_confirmation 存在此字段 |
custom_tool_use_id | string | user.custom_tool_result 存在此字段 |
result | string | user.tool_confirmation 存在此字段,取值 "allow" 或 "deny" |
deny_message | string | user.tool_confirmation 拒绝时的可选原因 |
GET /v1/sessions/{session_id}/events/stream 实时监听 Agent 的响应事件。
错误码
| HTTP | type | 触发条件 |
|---|---|---|
| 400 | invalid_request_error | events 为空、事件类型不支持、user.message 缺少 content、缺少 tool_use_id、缺少 custom_tool_use_id、result 非法,或请求格式错误 |
| 401 | authentication_error | PAT 无效或过期 |
| 404 | not_found_error | Session 不存在 |
| 409 | conflict_error | 向 processing 状态的 Session 发消息(需先 cancel 当前轮或等待 idle) |
相关
Session 事件流
通过 SSE 实时获取 Agent 的思考、消息、工具调用与状态。