@@ -18,6 +18,7 @@ import {
1818 type ThinkingConfig ,
1919 TurnCoverage ,
2020} from "@google/genai" ;
21+ import { canonicalizeBase64 } from "openclaw/plugin-sdk/media-runtime" ;
2122import {
2223 resolveExpiresAtMsFromDurationMs ,
2324 timestampMsToIsoString ,
@@ -468,6 +469,8 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
468469 private reconnectAttempts = 0 ;
469470 private reconnectTimer : ReturnType < typeof setTimeout > | undefined ;
470471 private hasConnectedSession = false ;
472+ private terminalError : Error | undefined ;
473+ private terminalCloseNotified = false ;
471474 private readonly pendingTranscripts : Record < RealtimeVoiceRole , string > = {
472475 user : "" ,
473476 assistant : "" ,
@@ -480,6 +483,9 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
480483 }
481484
482485 async connect ( ) : Promise < void > {
486+ if ( this . terminalError ) {
487+ throw this . terminalError ;
488+ }
483489 const canResumeSession =
484490 this . config . sessionResumption !== false && Boolean ( this . resumptionHandle ) ;
485491 if ( this . hasConnectedSession && ! canResumeSession ) {
@@ -534,6 +540,10 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
534540 this . sessionConfigured = false ;
535541 this . pendingFunctionNames . clear ( ) ;
536542 this . session = null ;
543+ if ( this . terminalError ) {
544+ this . notifyTerminalClose ( ) ;
545+ return ;
546+ }
537547 if ( this . intentionallyClosed ) {
538548 this . config . onClose ?.( "completed" ) ;
539549 return ;
@@ -788,7 +798,12 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
788798
789799 for ( const part of content . modelTurn ?. parts ?? [ ] ) {
790800 if ( part . inlineData ?. data ) {
791- const pcm = Buffer . from ( part . inlineData . data , "base64" ) ;
801+ const canonicalAudio = canonicalizeBase64 ( part . inlineData . data ) ;
802+ if ( ! canonicalAudio ) {
803+ this . failConnection ( new Error ( "Google Live stream returned malformed base64 audio data" ) ) ;
804+ return ;
805+ }
806+ const pcm = Buffer . from ( canonicalAudio , "base64" ) ;
792807 const sampleRate = parsePcmSampleRate ( part . inlineData . mimeType ) ;
793808 const audio = this . toOutputAudio ( pcm , sampleRate ) ;
794809 if ( audio . length > 0 ) {
@@ -845,6 +860,41 @@ class GoogleRealtimeVoiceBridge implements RealtimeVoiceBridge {
845860 this . pendingTranscripts . assistant = "" ;
846861 }
847862
863+ private failConnection ( error : Error ) : void {
864+ if ( this . terminalError ) {
865+ return ;
866+ }
867+ this . terminalError = error ;
868+ this . intentionallyClosed = true ;
869+ this . connected = false ;
870+ this . sessionConfigured = false ;
871+ if ( this . reconnectTimer ) {
872+ clearTimeout ( this . reconnectTimer ) ;
873+ this . reconnectTimer = undefined ;
874+ }
875+ this . pendingFunctionNames . clear ( ) ;
876+ this . flushPendingTranscripts ( ) ;
877+ const session = this . session ;
878+ this . session = null ;
879+ try {
880+ this . config . onError ?.( error ) ;
881+ } finally {
882+ try {
883+ session ?. close ( ) ;
884+ } finally {
885+ this . notifyTerminalClose ( ) ;
886+ }
887+ }
888+ }
889+
890+ private notifyTerminalClose ( ) : void {
891+ if ( this . terminalCloseNotified ) {
892+ return ;
893+ }
894+ this . terminalCloseNotified = true ;
895+ this . config . onClose ?.( "error" ) ;
896+ }
897+
848898 private handleToolCall ( toolCall : LiveServerToolCall ) : void {
849899 for ( const call of toolCall . functionCalls ?? [ ] ) {
850900 const name = call . name ?. trim ( ) ;
0 commit comments