@@ -68,6 +68,9 @@ const GOOGLE_REALTIME_BROWSER_NEW_SESSION_TTL_MS = 60 * 1000;
6868const GOOGLE_REALTIME_RECONNECT_MAX_ATTEMPTS = 3 ;
6969const GOOGLE_REALTIME_RECONNECT_BASE_DELAY_MS = 250 ;
7070const GOOGLE_REALTIME_RECONNECT_MAX_DELAY_MS = 2_000 ;
71+ const GOOGLE_REALTIME_MAX_PENDING_TRANSCRIPT_BYTES = 256 * 1024 ;
72+ const GOOGLE_REALTIME_TRANSCRIPT_OVERFLOW_MESSAGE =
73+ "Google Live transcript exceeded the 256 KiB UTF-8 pending buffer limit" ;
7174// Google Live requires a leading letter/underscore and caps function names at 128 characters.
7275const GOOGLE_REALTIME_TOOL_NAME_RE = / ^ [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ . : - ] { 0 , 127 } $ / ;
7376const MULAW_LINEAR_SAMPLES = new Int16Array ( 256 ) ;
@@ -143,6 +146,10 @@ type GoogleRealtimeLiveConfig = {
143146
144147type GoogleRealtimeVoiceBridgeConfig = RealtimeVoiceBridgeCreateRequest & GoogleRealtimeLiveConfig ;
145148type GoogleLiveTranscription = NonNullable < LiveServerContent [ "inputTranscription" ] > ;
149+ type GoogleLiveTranscriptAccumulator = {
150+ text : string ;
151+ byteCount : number ;
152+ } ;
146153
147154function trimToUndefined ( value : unknown ) : string | undefined {
148155 return normalizeOptionalString ( value ) ;
@@ -478,10 +485,13 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
478485 private closeNotified = false ;
479486 private connectionOwner : GoogleLiveConnectionAttempt | undefined ;
480487 private connectAttempt : GoogleLiveConnectionAttempt | undefined ;
481- private readonly pendingTranscripts : Record < RealtimeVoiceRole , string > = {
482- user : "" ,
483- assistant : "" ,
484- } ;
488+ // Google can interleave independent input/output transcripts, so each role
489+ // owns its own in-progress byte budget until `finished` or terminal cleanup.
490+ private readonly pendingTranscripts : Record < RealtimeVoiceRole , GoogleLiveTranscriptAccumulator > =
491+ {
492+ user : { text : "" , byteCount : 0 } ,
493+ assistant : { text : "" , byteCount : 0 } ,
494+ } ;
485495
486496 constructor ( private readonly config : GoogleRealtimeVoiceBridgeConfig ) {
487497 this . audioFormat = config . audioFormat ?? REALTIME_VOICE_AUDIO_FORMAT_G711_ULAW_8KHZ ;
@@ -858,13 +868,17 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
858868 }
859869
860870 if ( content . inputTranscription ) {
861- this . appendTranscript ( "user" , content . inputTranscription ) ;
871+ if ( ! this . appendTranscript ( "user" , content . inputTranscription ) ) {
872+ return ;
873+ }
862874 }
863875
864876 if ( content . outputTranscription ) {
865877 // outputAudioTranscription is requested in the session config. Keep that
866878 // official stream canonical; modelTurn text has no transcript turn identity.
867- this . appendTranscript ( "assistant" , content . outputTranscription ) ;
879+ if ( ! this . appendTranscript ( "assistant" , content . outputTranscription ) ) {
880+ return ;
881+ }
868882 }
869883
870884 for ( const part of content . modelTurn ?. parts ?? [ ] ) {
@@ -886,22 +900,33 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
886900 }
887901 }
888902
889- private appendTranscript ( role : RealtimeVoiceRole , transcript : GoogleLiveTranscription ) : void {
903+ private appendTranscript ( role : RealtimeVoiceRole , transcript : GoogleLiveTranscription ) : boolean {
890904 const text = transcript . text ;
891905 if ( text ) {
892- this . pendingTranscripts [ role ] += text ;
906+ const pending = this . pendingTranscripts [ role ] ;
907+ const textBytes = Buffer . byteLength ( text , "utf8" ) ;
908+ if ( pending . byteCount + textBytes > GOOGLE_REALTIME_MAX_PENDING_TRANSCRIPT_BYTES ) {
909+ this . resetPendingTranscripts ( ) ;
910+ this . failConnection ( new Error ( GOOGLE_REALTIME_TRANSCRIPT_OVERFLOW_MESSAGE ) ) ;
911+ return false ;
912+ }
913+ pending . text += text ;
914+ pending . byteCount += textBytes ;
893915 this . emitTranscript ( role , text , false ) ;
894916 }
895917 // turnComplete belongs to model generation and is unordered with transcription.
896918 // Finalize only on the protocol terminal or when the bridge permanently closes.
897919 if ( transcript . finished ) {
898920 this . flushPendingTranscript ( role ) ;
899921 }
922+ return true ;
900923 }
901924
902925 private flushPendingTranscript ( role : RealtimeVoiceRole ) : void {
903- const completeText = this . pendingTranscripts [ role ] . trim ( ) ;
904- this . pendingTranscripts [ role ] = "" ;
926+ const pending = this . pendingTranscripts [ role ] ;
927+ const completeText = pending . text . trim ( ) ;
928+ pending . text = "" ;
929+ pending . byteCount = 0 ;
905930 if ( completeText ) {
906931 this . emitTranscript ( role , completeText , true ) ;
907932 }
@@ -927,8 +952,8 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
927952 }
928953
929954 private resetPendingTranscripts ( ) : void {
930- this . pendingTranscripts . user = "" ;
931- this . pendingTranscripts . assistant = "" ;
955+ this . pendingTranscripts . user = { text : "" , byteCount : 0 } ;
956+ this . pendingTranscripts . assistant = { text : "" , byteCount : 0 } ;
932957 }
933958
934959 private failConnection ( error : Error ) : void {
0 commit comments