@@ -5,7 +5,10 @@ import type {
55 RealtimeVoiceSessionConnection ,
66 RealtimeVoiceToolResultOptions ,
77} from "openclaw/plugin-sdk/realtime-voice" ;
8- import { REALTIME_VOICE_AUDIO_FORMAT_G711_ULAW_8KHZ } from "openclaw/plugin-sdk/realtime-voice" ;
8+ import {
9+ REALTIME_VOICE_AUDIO_FORMAT_G711_ULAW_8KHZ ,
10+ realtimeVoiceAudioDurationMs ,
11+ } from "openclaw/plugin-sdk/realtime-voice" ;
912import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime" ;
1013import {
1114 XAI_REALTIME_DEFAULT_PREFIX_PADDING_MS ,
@@ -22,18 +25,23 @@ import {
2225
2326export class XaiRealtimePlaybackMarkOverflowError extends Error { }
2427
28+ type XaiAssistantAudioItem = {
29+ itemId : string ;
30+ bytes : number ;
31+ startTimestamp : number ;
32+ } ;
33+
2534export abstract class XaiRealtimeVoiceProtocol {
2635 protected readonly audioFormat : RealtimeVoiceAudioFormat ;
2736 protected markQueue : string [ ] = [ ] ;
28- protected responseStartTimestamp : number | null = null ;
2937 protected responseActive = false ;
3038 protected responseCreateInFlight = false ;
3139 protected responseCancelInFlight = false ;
3240 protected responseCreatePending = false ;
3341 protected continuingToolCallIds = new Set < string > ( ) ;
3442 protected pendingToolCallIds = new Set < string > ( ) ;
3543 protected latestMediaTimestamp = 0 ;
36- protected lastAssistantItemId : string | null = null ;
44+ protected assistantAudioItem : XaiAssistantAudioItem | null = null ;
3745 protected toolCallBuffers = new Map < string , { name : string ; callId : string ; args : string } > ( ) ;
3846 protected deliveredToolCallKeys = new Set < string > ( ) ;
3947 protected pendingToolResultAcks = new Set < string > ( ) ;
@@ -101,20 +109,11 @@ export abstract class XaiRealtimeVoiceProtocol {
101109 }
102110
103111 handleBargeIn ( options ?: RealtimeVoiceBargeInOptions ) : void {
104- const assistantItemId = this . lastAssistantItemId ;
105- const responseStartTimestamp = this . responseStartTimestamp ;
106- const outputInterruptible =
107- responseStartTimestamp !== null &&
112+ const assistantAudioItem = this . assistantAudioItem ;
113+ const shouldInterruptProvider =
114+ assistantAudioItem !== null &&
108115 ( this . responseActive || this . markQueue . length > 0 || options ?. audioPlaybackActive === true ) ;
109- const shouldInterruptProvider = assistantItemId !== null && outputInterruptible ;
110- const audioEndMs = shouldInterruptProvider
111- ? Math . max (
112- 0 ,
113- responseStartTimestamp === null
114- ? this . latestMediaTimestamp
115- : this . latestMediaTimestamp - responseStartTimestamp ,
116- )
117- : null ;
116+ const audioEndMs = shouldInterruptProvider ? this . audioEndMs ( assistantAudioItem ) : null ;
118117 if ( this . responseActive && ! this . responseCancelInFlight ) {
119118 this . sendEvent ( { type : "response.cancel" } , "reason=barge-in" ) ;
120119 this . responseCancelInFlight = true ;
@@ -123,16 +122,15 @@ export abstract class XaiRealtimeVoiceProtocol {
123122 this . sendEvent (
124123 {
125124 type : "conversation.item.truncate" ,
126- item_id : assistantItemId ,
125+ item_id : assistantAudioItem . itemId ,
127126 content_index : 0 ,
128127 audio_end_ms : audioEndMs ,
129128 } ,
130129 `reason=barge-in audioEndMs=${ audioEndMs } ` ,
131130 ) ;
132131 this . config . onClearAudio ( "barge-in" ) ;
133132 this . markQueue = [ ] ;
134- this . lastAssistantItemId = null ;
135- this . responseStartTimestamp = null ;
133+ this . assistantAudioItem = null ;
136134 return ;
137135 }
138136 this . config . onClearAudio ( "barge-in" ) ;
@@ -142,16 +140,13 @@ export abstract class XaiRealtimeVoiceProtocol {
142140 protected handleServerVadBargeIn ( ) : void {
143141 // xAI owns server-VAD cancellation, but only the relay knows how much
144142 // queued audio actually played. Trim provider history to that boundary.
145- if (
146- this . lastAssistantItemId !== null &&
147- this . responseStartTimestamp !== null &&
148- this . markQueue . length > 0
149- ) {
150- const audioEndMs = Math . max ( 0 , this . latestMediaTimestamp - this . responseStartTimestamp ) ;
143+ const assistantAudioItem = this . assistantAudioItem ;
144+ if ( assistantAudioItem !== null && this . markQueue . length > 0 ) {
145+ const audioEndMs = this . audioEndMs ( assistantAudioItem ) ;
151146 this . sendEvent (
152147 {
153148 type : "conversation.item.truncate" ,
154- item_id : this . lastAssistantItemId ,
149+ item_id : assistantAudioItem . itemId ,
155150 content_index : 0 ,
156151 audio_end_ms : audioEndMs ,
157152 } ,
@@ -160,8 +155,13 @@ export abstract class XaiRealtimeVoiceProtocol {
160155 }
161156 this . config . onClearAudio ( "barge-in" ) ;
162157 this . markQueue = [ ] ;
163- this . lastAssistantItemId = null ;
164- this . responseStartTimestamp = null ;
158+ this . assistantAudioItem = null ;
159+ }
160+
161+ private audioEndMs ( item : XaiAssistantAudioItem ) : number {
162+ const producedAudioMs = Math . floor ( realtimeVoiceAudioDurationMs ( this . audioFormat , item . bytes ) ) ;
163+ const playbackAudioMs = Math . max ( 0 , this . latestMediaTimestamp - item . startTimestamp ) ;
164+ return Math . min ( producedAudioMs , playbackAudioMs ) ;
165165 }
166166
167167 protected buildSessionUpdate ( ) : XaiRealtimeSessionUpdate {
@@ -300,12 +300,11 @@ export abstract class XaiRealtimeVoiceProtocol {
300300
301301 protected resetRealtimeSessionState ( options : { preserveToolCallState ?: boolean } = { } ) : void {
302302 this . markQueue = [ ] ;
303- this . responseStartTimestamp = null ;
304303 this . responseActive = false ;
305304 this . responseCreateInFlight = false ;
306305 this . responseCancelInFlight = false ;
307306 this . responseCreatePending = false ;
308- this . lastAssistantItemId = null ;
307+ this . assistantAudioItem = null ;
309308 this . resetInputTranscripts ( ) ;
310309 if ( ! options . preserveToolCallState ) {
311310 this . continuingToolCallIds . clear ( ) ;
0 commit comments