Skip to main content
Sessions

订阅 Session Thread Event Stream

通过 Server-Sent Events 流式订阅指定 Forward Session Thread 的公开 Event。

GET /api/v1/forward/sessions/{session_id}/threads/{thread_id}/stream

请求参数

位置参数类型是否必填说明
HeaderAuthorizationstringBearer <PAT 或 SAT>
HeaderAcceptstring建议使用 text/event-stream;省略时仍可建立连接。
HeaderLast-Event-IDstring从该 Thread Event 之后继续订阅。
Pathsession_idstringsess_ 前缀的 Session ID。
Paththread_idstringsthr_ 前缀的 Thread ID。
该接口没有查询参数,不支持 typetypestypes[]include_tool_callsinclude_thinkingevent_deltas[]。即使这些参数的值为空,也会返回 400 invalid_request,且不会建立下游流。

示例请求

curl -N -X GET 'https://api.qoder.com/api/v1/forward/sessions/sess_xxx/threads/sthr_child_xxx/stream' \
  -H "Authorization: Bearer $QODER_ACCESS_TOKEN" \
  -H "Accept: text/event-stream" \
  -H "Last-Event-ID: evt_cursor_xxx"

流格式

id: evt_xxx
event: agent.message
data: {"id":"evt_xxx","type":"agent.message","session_id":"sess_xxx","content":[{"type":"text","text":"收到任务"}],"processed_at":"2026-06-22T11:05:00Z"}
服务端会定期发送 : heartbeat 注释行以保持连接。客户端应消费全部 frame、在本地做展示过滤,并持续保存最后收到的 SSE id 用于重连。

Last-Event-ID

Last-Event-ID 必须属于当前 Thread:
  • 游标属于其他 Session 或 Thread 时,返回 404 thread_not_found
  • 运行时明确判定它是当前 Thread 的非法 Event cursor 时,返回 400 invalid_event_cursor
  • 省略该 Header 时,从该 Thread 的第一条 Event 开始回放;不会遗漏事件,但会重复收到已消费的 Event,请基于 Event ID 做幂等消费。
  • SSE 连接可能因网关超时、服务端重启等原因断开,服务端不保证连接长期存活;客户端必须自行实现重连重试,重连时携带最后收到的 Event ID 作为 Last-Event-ID,从断点继续订阅。

错误

HTTPTypeCode触发条件
400invalid_requestinvalid_request传入该接口不支持的 selector。
400invalid_request_errorinvalid_event_cursorLast-Event-ID 是当前 Thread 的非法 Event cursor。
401authentication_errorauthentication_requiredPAT 或 SAT 无效或已过期。
404not_foundsession_not_foundSession 不存在或当前调用方不可见。
404not_found_errorthread_not_foundThread 不存在、不属于该 Session,或 cursor 指向其他 Session/Thread。
502api_errorruntime_unavailableThread 运行服务暂时不可用。

Thread 发现与恢复

父 Session history/stream 会通过以下 lifecycle Event 暴露 child Thread 的创建和状态变化:session.thread_createdsession.thread_status_runningsession.thread_status_idlesession.thread_status_rescheduledsession.thread_status_terminated 从父 Session stream 收到 session.thread_created 后,可将该 Event 的 ID 作为 child Thread stream 的首个 Last-Event-ID,避免遗漏发现 Thread 到建立连接之间产生的 Event。 如果通过 Thread 列表发现已经活跃的 child Thread,请先读取 Thread Event 历史,再使用响应中的 last_id 建立 stream。Thread 归档后不能新建或恢复 child Thread stream;请改用父 Session history/stream 恢复 lifecycle Event,并通过获取 Thread 接口读取最终状态。
订阅 Session Thread Event Stream - Qoder