@@ -483,7 +483,7 @@ class RealtimeDemo {
483483
484484 const contentDiv = document . createElement ( 'div' ) ;
485485 contentDiv . className = 'event-content collapsed' ;
486- contentDiv . textContent = JSON . stringify ( event , null , 2 ) ;
486+ contentDiv . textContent = this . formatEventForDisplay ( event ) ;
487487
488488 headerDiv . addEventListener ( 'click' , ( ) => {
489489 const isCollapsed = contentDiv . classList . contains ( 'collapsed' ) ;
@@ -495,10 +495,36 @@ class RealtimeDemo {
495495 eventDiv . appendChild ( contentDiv ) ;
496496 this . eventsContent . appendChild ( eventDiv ) ;
497497
498+ // Discard the oldest raw event entries to prevent unbounded memory growth.
499+ const maxRawEvents = 200 ;
500+ while ( this . eventsContent . children . length > maxRawEvents ) {
501+ this . eventsContent . removeChild ( this . eventsContent . firstChild ) ;
502+ }
503+
498504 // Auto-scroll events pane
499505 this . eventsContent . scrollTop = this . eventsContent . scrollHeight ;
500506 }
501507
508+ formatEventForDisplay ( event ) {
509+ // Replace large payloads before rendering so the debug panel stays responsive.
510+ return JSON . stringify (
511+ event ,
512+ ( key , value ) => {
513+ if ( key === 'audio' && typeof value === 'string' ) {
514+ return `[${ value . length } chars of audio data omitted]` ;
515+ }
516+ if ( typeof value === 'string' && value . length > 2048 ) {
517+ const head = value . slice ( 0 , 512 ) ;
518+ const tail = value . slice ( - 128 ) ;
519+ const skipped = value . length - ( head . length + tail . length ) ;
520+ return `${ head } …[${ skipped } chars omitted]…${ tail } ` ;
521+ }
522+ return value ;
523+ } ,
524+ 2
525+ ) ;
526+ }
527+
502528 addToolEvent ( event ) {
503529 const eventDiv = document . createElement ( 'div' ) ;
504530 eventDiv . className = 'event' ;
0 commit comments