Skip to main content

Qwen-Audio-Live 服务端事件

读取会话状态、语音回复和任务执行结果。

通过控制 WebSocket 接收 JSON 事件。使用 WebRTC 或 AOQ 时,先从控制 WebSocket 等待 session.updated。媒体连接就绪后,也可通过 WebRTC DataChannel 或 AOQ 数据轨接收服务端事件。 按 type 处理事件。回执的 client_event_id 对应请求的 event_id。服务端事件自己的 event_id 标识当前事件。

session.created

连接建立后,服务返回会话的默认信息。发送 session.update 后,等待 session.updated 确认配置生效。
typestring必填
事件类型,固定为 session.created。
event_idstring必填
服务端事件 ID。
sessionobject必填
默认会话信息。
idstring必填
业务会话 ID。AOQ Relay 使用独立的 sid。
objectstring必填
兼容对象名,例如 realtime.session。
modelstring必填
部署的默认前台语音模型。
modalitiesarray按模型返回
输出模态。
voicestring按模型返回
默认音色。
turn_detectionobject按模型返回
默认轮次检测配置。 以下检测参数在 type 为 server_vad 时返回。
typestring
轮次检测类型。
thresholdnumber
语音活动检测阈值。
prefix_padding_msinteger
检测到语音开始时保留的前置音频时长,单位为毫秒。
silence_duration_msinteger
判定语音结束所需的静音时长,单位为毫秒。
create_responseboolean
是否在检测到语音结束后自动生成回复。
interrupt_responseboolean
检测到用户开始讲话时,是否打断正在生成的回复。
input_audio_transcriptionobject按模型返回
输入音频转写能力及默认配置。
modelstring
用于输入音频转写的模型。
model_inferenceboolean必填
服务具有接入模型的能力。会话配置生效后再开始推理交互。
{
  "type": "session.created",
  "event_id": "evt_created",
  "session": {
    "id": "sess_demo",
    "object": "realtime.session",
    "model": "<默认前台模型 ID>",
    "modalities": [
      "text",
      "audio"
    ],
    "turn_detection": {
      "type": "server_vad"
    },
    "input_audio_transcription": {
      "model": "<转写模型 ID>"
    },
    "model_inference": true
  }
}

session.updated

会话配置已生效。使用返回的配置。
typestring必填
事件类型,固定为 session.updated。
event_idstring必填
服务端事件 ID。
sessionobject必填
生效的会话配置。含会话元数据和任务模式。
idstring必填
业务会话 ID。
modelstring必填
生效的前台语音模型。
objectstring按配置返回
会话对象类型。
modalitiesarray按配置返回
生效的输出模态。
delegationobject必填
任务模式。
typestring必填
client 或 responses。
transportobject按配置返回
返回所选传输方式的连接信息。
typestring按配置返回
使用 WebRTC 时,值为 rtc。
sdpstring按配置返回
使用 WebRTC 时返回 Answer SDP。客户端将它设为远端描述。详见 WebRTC 接入。
aoqobject按配置返回
使用 AOQ 时返回临时连接信息。请在收到 session.updated 后的 5 分钟内使用返回的 sid 和临时凭证发起连接。接入步骤见 AOQ 接入。
sidstring
AOQ 会话 ID。
aoqTokenForClientstring
客户端连接 Relay 时使用的临时令牌。
clientRelayCertFingerprintstring
Relay 的 TLS 证书指纹。
clientRelayEndpointsarray
可连接的 Relay 地址列表。
endpointstring
Relay 地址。
portinteger
Relay 端口。
route_indexinteger
Relay 路由索引。
extraInfoobject
附加连接信息。
workspaceIdHashstring
业务空间 ID 的哈希值。
  • WebSocket
  • WebRTC(节选)
  • AOQ(节选)
{
  "type": "session.updated",
  "event_id": "evt_updated",
  "session": {
    "id": "sess_demo",
    "object": "realtime.session",
    "model": "qwen-audio-4.0-realtime-preview",
    "modalities": [
      "text",
      "audio"
    ],
    "delegation": {
      "type": "client"
    }
  }
}

session.input_audio.muted

输入已静音。
typestring必填
事件类型,固定为 session.input_audio.muted。
event_idstring必填
服务端事件 ID。
client_event_idstring可选
对应客户端请求的 event_id。
{
  "type": "session.input_audio.muted",
  "event_id": "evt_muted",
  "client_event_id": "evt_mute"
}

session.input_audio.unmuted

输入已恢复。
typestring必填
事件类型,固定为 session.input_audio.unmuted。
event_idstring必填
服务端事件 ID。
client_event_idstring可选
对应客户端请求的 event_id。
{
  "type": "session.input_audio.unmuted",
  "event_id": "evt_unmuted",
  "client_event_id": "evt_unmute"
}

session.input_transcript.delta

收到用户语音的转写。
typestring必填
事件类型,固定为 session.input_transcript.delta。
event_idstring必填
服务端事件 ID。
deltastring必填
本次返回的转写文本。
start_msnumber必填
会话时间线上的开始时间,单位为毫秒。
end_msnumber必填
会话时间线上的结束时间,单位为毫秒。
{
  "type": "session.input_transcript.delta",
  "event_id": "evt_input_transcript",
  "delta": "帮我查库存。",
  "start_ms": 1600,
  "end_ms": 1600
}

session.output_transcript.delta

收到前台语音回复的字幕。字幕时间戳用于定位会话时间线。
typestring必填
事件类型,固定为 session.output_transcript.delta。
event_idstring必填
服务端事件 ID。
deltastring必填
本次返回的回复文本。
start_msnumber必填
会话时间线上的开始时间,单位为毫秒。
end_msnumber必填
会话时间线上的结束时间,单位为毫秒。
{
  "type": "session.output_transcript.delta",
  "event_id": "evt_output_transcript",
  "delta": "库存还有七把雨伞。",
  "start_ms": 5400,
  "end_ms": 5400
}

session.output_audio.delta

收到前台语音的音频分片。
typestring必填
事件类型,固定为 session.output_audio.delta。
event_idstring可选
服务端事件 ID。
deltastring必填
Base64 编码的 24 kHz 单声道 PCM16LE 原始音频。解码后字节数为偶数。音频没有 WAV 文件头。
start_msnumber必填
会话时间线上的开始时间,单位为毫秒。
end_msnumber必填
会话时间线上的结束时间,单位为毫秒。
{
  "type": "session.output_audio.delta",
  "delta": "AAAAAA==",
  "start_ms": 5400,
  "end_ms": 5420
}

session.delegation.created

前台模型派发任务。Client 和 Responses 模式都可收到该事件。回传任务结果时,使用对应任务事件中的 delegation_id。
typestring必填
事件类型,固定为 session.delegation.created。
event_idstring必填
服务端事件 ID。
offset_msnumber必填
本次派发在会话时间线上的偏移,单位为毫秒。
delegation_idstring必填
本次派发对应的任务 ID。
task_idstring可选
任务 ID。回传时使用 delegation_id。
summarystring任务派发时返回
本次派发的任务摘要。
contextobject任务派发时返回
任务受理时的上下文快照。
summarystring必填
上下文摘要。
messagesarray[object]任务派发时返回
上下文消息。
seqinteger
消息在上下文中的序号。
item_idstring
消息项 ID。
rolestring
消息发送方的角色。
sourcestring
消息来源。
audio_start_msinteger
音频开始时间在会话时间线上的偏移,单位为毫秒。
audio_end_msinteger
音频结束时间在会话时间线上的偏移,单位为毫秒。
playbackstring
音频播放状态。
contentstring
消息文本。
incompleteboolean
上下文快照是否不完整。
truncatedboolean
上下文快照是否已截断。
supplementsarray按任务返回
本批受理的补充内容及各自的上下文。
summarystring必填
补充内容摘要。
contextobject任务派发时返回
任务受理时的上下文快照。
summarystring必填
上下文摘要。
messagesarray任务派发时返回
上下文消息。
argumentsstring可选
兼容字段。JSON 字符串,可包含 summary。
{
  "type": "session.delegation.created",
  "event_id": "evt_delegation",
  "offset_ms": 3600,
  "delegation_id": "task_stock",
  "task_id": "task_stock",
  "summary": "查询雨伞库存。",
  "context": {
    "summary": "",
    "messages": [
      {
        "seq": 1,
        "item_id": "item_stock_001",
        "role": "user",
        "audio_start_ms": 900,
        "audio_end_ms": 3600,
        "content": "查询雨伞库存。"
      }
    ],
    "incomplete": false,
    "truncated": false
  },
  "supplements": [
    {
      "summary": "查询雨伞库存。",
      "context": {
        "summary": "",
        "messages": []
      }
    }
  ],
  "arguments": "{\"summary\":\"查询雨伞库存。\"}"
}

session.event

接收前台函数调用。使用 call_id 关联结果。
typestring必填
事件类型,固定为 session.event。
event_idstring必填
服务端事件 ID。
client_event_idstring可选
对应客户端请求的 event_id。
eventobject必填
前台模型的函数调用事件。
typestring必填
内层事件名。
itemobject按事件返回
函数调用项。
typestring必填
函数调用项为 function_call。
namestring必填
函数名。
call_idstring必填
调用 ID。
argumentsstring必填
JSON 字符串形式的函数参数。
idstring按事件返回
调用项 ID。
statusstring按事件返回
调用项状态。
output_indexnumber按事件返回
调用项的输出位置。
response_idstring按事件返回
前台响应 ID。
{
  "type": "session.event",
  "event_id": "evt_frontend_tool",
  "event": {
    "type": "response.output_item.added",
    "item": {
      "type": "function_call",
      "name": "lookup_stock",
      "call_id": "call_stock",
      "arguments": "{\"item\":\"雨伞\"}",
      "id": "item_stock",
      "status": "completed"
    },
    "output_index": 0,
    "response_id": "resp_frontend"
  }
}

response.event

接收后台生成或任务事件。内层 response.completed 表示后台生成结束。前台语音回复有自己的字幕和音频事件。
typestring必填
事件类型,固定为 response.event。
event_idstring必填
服务端事件 ID。
delegation_idstring按事件返回
关联的任务 ID。
client_event_idstring可选
对应客户端请求的 event_id。
eventobject必填
后台原生事件或任务事件。
typestring必填
内层事件名。
responseobject按事件返回
后台响应。字段随事件变化。
idstring按事件返回
后台响应 ID。
usageobject可选
后台模型的 Token 用量。
input_tokensinteger按事件返回
后台模型消耗的输入 Token 数。
output_tokensinteger按事件返回
后台模型生成的输出 Token 数。
total_tokensinteger按事件返回
输入和输出的 Token 总数。
{
  "type": "response.event",
  "event_id": "evt_backend_completed",
  "delegation_id": "task_calc",
  "event": {
    "type": "response.completed",
    "response": {
      "id": "resp_calc",
      "usage": {
        "input_tokens": 28,
        "output_tokens": 12,
        "total_tokens": 40
      }
    }
  }
}

session.thinking.appended

服务已受理任务进度。对于请求 session.thinking.append,可用关联字段匹配回执。
typestring必填
事件类型,固定为 session.thinking.appended。
event_idstring必填
服务端事件 ID。
start_msnumber必填
会话时间线上的开始时间,单位为毫秒。
end_msnumber必填
会话时间线上的结束时间,单位为毫秒。
client_event_idstring可选
对应客户端请求的 event_id。
{
  "type": "session.thinking.appended",
  "event_id": "evt_ack",
  "start_ms": 2000,
  "end_ms": 2000,
  "client_event_id": "evt_client_request"
}

session.instructions.appended

服务已受理追问内容。对于请求 session.instruction.append,可用关联字段匹配回执。
typestring必填
事件类型,固定为 session.instructions.appended。
event_idstring必填
服务端事件 ID。
start_msnumber必填
会话时间线上的开始时间,单位为毫秒。
end_msnumber必填
会话时间线上的结束时间,单位为毫秒。
client_event_idstring可选
对应客户端请求的 event_id。
{
  "type": "session.instructions.appended",
  "event_id": "evt_ack",
  "start_ms": 2000,
  "end_ms": 2000,
  "client_event_id": "evt_client_request"
}

session.commentary.appended

服务已受理结果或文本。对于请求 session.commentary.append,可用关联字段匹配回执。
typestring必填
事件类型,固定为 session.commentary.appended。
event_idstring必填
服务端事件 ID。
start_msnumber必填
会话时间线上的开始时间,单位为毫秒。
end_msnumber必填
会话时间线上的结束时间,单位为毫秒。
client_event_idstring可选
对应客户端请求的 event_id。
{
  "type": "session.commentary.appended",
  "event_id": "evt_ack",
  "start_ms": 2000,
  "end_ms": 2000,
  "client_event_id": "evt_client_request"
}

session.usage.updated

会话激活后,服务每 5 秒上报一次累计计费时长,空闲时也会上报。当前值取最新的 usage.seconds,最终值从 session.closed 中读取。后台模型的 Token 用量见 response.event。
typestring必填
事件类型,固定为 session.usage.updated。
event_idstring必填
服务端事件 ID。
usageobject必填
会话用量。
secondsnumber必填
会话累计计费时长(秒)。取 3 秒与实际会话时长中的较大值。
context_windowobject可选
上下文使用情况。
usage_rationumber可选
Token 用量除以上下文上限。结果可能滞后。
{
  "type": "session.usage.updated",
  "event_id": "evt_usage",
  "usage": {
    "seconds": 32.5
  },
  "context_window": {
    "usage_ratio": 0.12
  }
}

session.closed

会话已结束。停止发送事件并释放资源。需要继续对话时,建立新连接并初始化会话。
typestring必填
事件类型,固定为 session.closed。
event_idstring必填
服务端事件 ID。
reasonstring必填
关闭原因。可能为 close_requested、expired、content、remote_hangup 或 connection_lost。
sessionobject必填
会话关闭时返回的配置快照。
idstring随快照返回
业务会话 ID。
objectstring随快照返回
会话对象类型。
statusstring随快照返回
快照中的状态可能仍为 active。以 session.closed 事件判断会话已关闭。
modelstring随快照返回
会话使用的前台语音模型。
usageobject必填
会话结束时的最终用量。
secondsnumber必填
会话累计计费时长(秒)。取 3 秒与实际会话时长中的较大值。
{
  "type": "session.closed",
  "event_id": "evt_closed",
  "reason": "close_requested",
  "session": {
    "id": "sess_demo",
    "object": "realtime.session",
    "status": "active",
    "model": "qwen-audio-4.0-realtime-preview"
  },
  "usage": {
    "seconds": 45.8
  }
}

error

根据错误码检查请求和会话状态。收到关闭事件后,重新建连。
typestring必填
事件类型,固定为 error。
event_idstring必填
服务端事件 ID。
errorobject必填
错误详情。
typestring必填
invalid_request_error 或 server_error。
codestring必填
错误码。例如 invalid_session_configuration 或 invalid_event_state。
messagestring必填
错误说明。
event_idstring可选
对应客户端请求的 event_id。
{
  "type": "error",
  "event_id": "evt_server_error",
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_session_configuration",
    "message": "Invalid session configuration",
    "event_id": "evt_update_client"
  }
}