Class RealtimeSession
java.lang.Object
com.alibaba.dashscope.resources.audio.RealtimeSession
- All Implemented Interfaces:
AutoCloseable
Realtime session (audio design §1.3): OpenAI-Realtime-style protocol — pure JSON text frames
distinguished by
type, audio base64-encoded (a protocol limitation; ~33% size overhead,
prefer the task protocol for heavy traffic), no task_id, so every session owns a
dedicated socket and cannot share a WsConnection.
Pull model — no SDK threads: OkHttp's reader enqueues events, events() streams them
to the caller's blocking thread, consuming each frame after delivery. User discipline is one send
thread + one receive thread, finally close().
close() is idempotent and the session may re-connect() afterwards; a
generation counter drops late frames from a previous socket so stale events never leak into the
new generation (2.x omni parity). endSession() is unsupported on the qwen_audio dialect.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationDefault wait bound forsession.finishedaftersession.finish(2.x parity). -
Method Summary
Modifier and TypeMethodDescriptionvoidappendAudio(byte[] audio) Appends PCM audio; base64-encoded on the wire (protocol limitation).voidappendAudioBase64(String audioBase64) Appends audio already base64-encoded.voidappendImage(byte[] image) Appends one image frame of the video input; base64-encoded on the wire.voidCancels the in-flight response.voidClears the audio buffered so far.voidclose()Idempotent; the session may be re-connect()ed afterwards.voidcommit()Commits the audio buffered so far (not needed in server-VAD mode).voidconnect()(Re)connects the session.voidcreateItem(com.fasterxml.jackson.databind.JsonNode item) Creates a conversation item.voidresponse.create with no overrides.voidcreateResponse(String instructions, List<String> modalities) response.create with overrides;instructionsis always carried (JSON null when not given, 2.x parity),modalitiesonly when non-null.voidsession.finish, then waits up toDEFAULT_END_SESSION_TIMEOUTfor session.finished.voidendSession(Duration timeout) session.finish, then blocks until session.finished ortimeout; events pumped while waiting stay buffered forevents().voidSends session.finish without waiting; seeendSession(Duration).Iterable<com.fasterxml.jackson.databind.JsonNode>events()Raw event frames, delivered once and released after delivery (a consuming, single-use stream: a seconditerator()throwsSdkException; the claim resets onconnect()); ends when the socket closes (after a completion was seen or a local close) — an early disconnect raisesApiConnectionExceptionmid-iteration, a transport failure raises its mapped exception.response.created to first response.audio.delta; null until both happened.response.created to first response.audio_transcript.delta; null until both happened.voidsendEvent(com.fasterxml.jackson.databind.node.ObjectNode frame) Low-level escape hatch: sends the frame verbatim, auto-addingevent_idif absent.voidupdateSession(UpdateSessionParams update) session.update with the given configuration (only set fields are serialized).
-
Field Details
-
DEFAULT_END_SESSION_TIMEOUT
Default wait bound forsession.finishedaftersession.finish(2.x parity).
-
-
Method Details
-
connect
public void connect()(Re)connects the session. Idempotent while a socket is open (2.x omni parity); afterclose()it opens a fresh generation and drops everything buffered from the old one. The handshake wait runs outside the session monitor soclose()can always take the monitor promptly and cancel a still-connecting socket. A concurrentconnect()returns as soon as a connect attempt owns the socket, i.e. while its handshake may still be in flight. -
updateSession
session.update with the given configuration (only set fields are serialized). -
appendAudio
public void appendAudio(byte[] audio) Appends PCM audio; base64-encoded on the wire (protocol limitation). -
appendAudioBase64
Appends audio already base64-encoded. -
appendImage
public void appendImage(byte[] image) Appends one image frame of the video input; base64-encoded on the wire. -
commit
public void commit()Commits the audio buffered so far (not needed in server-VAD mode). -
clearAppendedAudio
public void clearAppendedAudio()Clears the audio buffered so far. -
createItem
public void createItem(com.fasterxml.jackson.databind.JsonNode item) Creates a conversation item. -
createResponse
public void createResponse()response.create with no overrides. -
createResponse
response.create with overrides;instructionsis always carried (JSON null when not given, 2.x parity),modalitiesonly when non-null. -
cancelResponse
public void cancelResponse()Cancels the in-flight response. -
endSessionAsync
public void endSessionAsync()Sends session.finish without waiting; seeendSession(Duration). -
endSession
public void endSession()session.finish, then waits up toDEFAULT_END_SESSION_TIMEOUTfor session.finished. -
endSession
-
events
Raw event frames, delivered once and released after delivery (a consuming, single-use stream: a seconditerator()throwsSdkException; the claim resets onconnect()); ends when the socket closes (after a completion was seen or a local close) — an early disconnect raisesApiConnectionExceptionmid-iteration, a transport failure raises its mapped exception. Servererror-type frames are delivered as ordinary events. -
sendEvent
public void sendEvent(com.fasterxml.jackson.databind.node.ObjectNode frame) Low-level escape hatch: sends the frame verbatim, auto-addingevent_idif absent. -
getSessionId
-
getLastResponseId
-
getFirstAudioDelay
response.created to first response.audio.delta; null until both happened. -
getFirstTextDelay
response.created to first response.audio_transcript.delta; null until both happened. -
close
public void close()Idempotent; the session may be re-connect()ed afterwards. The handshake wait inconnect()runs outside this monitor, so this call always takes it promptly; a socket whose handshake is still in flight is cancelled, failing that connect fast instead of blocking here for the whole connect timeout (2.x c891540 parity: okhttp's close alone does not abort an in-flight handshake).- Specified by:
closein interfaceAutoCloseable
-