@@ -210,6 +210,12 @@ class RealtimeDemo {
210210 case 'history_updated' :
211211 this . updateMessagesFromHistory ( event . history ) ;
212212 break ;
213+ case 'history_added' :
214+ // Append just the new item without clearing the thread.
215+ if ( event . item ) {
216+ this . addMessageFromItem ( event . item ) ;
217+ }
218+ break ;
213219 }
214220 }
215221
@@ -235,13 +241,7 @@ class RealtimeDemo {
235241 // Extract text from content array
236242 item . content . forEach ( contentPart => {
237243 console . log ( 'Content part:' , contentPart ) ;
238- if ( contentPart . type === 'text' && contentPart . text ) {
239- content += contentPart . text ;
240- } else if ( contentPart . type === 'input_text' && contentPart . text ) {
241- content += contentPart . text ;
242- } else if ( contentPart . type === 'input_audio' && contentPart . transcript ) {
243- content += contentPart . transcript ;
244- } else if ( contentPart . type === 'audio' && contentPart . transcript ) {
244+ if ( contentPart && contentPart . transcript ) {
245245 content += contentPart . transcript ;
246246 }
247247 } ) ;
@@ -263,6 +263,35 @@ class RealtimeDemo {
263263
264264 this . scrollToBottom ( ) ;
265265 }
266+
267+ addMessageFromItem ( item ) {
268+ try {
269+ if ( ! item || item . type !== 'message' ) return ;
270+ const role = item . role ;
271+ let content = '' ;
272+
273+ if ( Array . isArray ( item . content ) ) {
274+ for ( const contentPart of item . content ) {
275+ if ( ! contentPart || typeof contentPart !== 'object' ) continue ;
276+ if ( contentPart . type === 'text' && contentPart . text ) {
277+ content += contentPart . text ;
278+ } else if ( contentPart . type === 'input_text' && contentPart . text ) {
279+ content += contentPart . text ;
280+ } else if ( contentPart . type === 'input_audio' && contentPart . transcript ) {
281+ content += contentPart . transcript ;
282+ } else if ( contentPart . type === 'audio' && contentPart . transcript ) {
283+ content += contentPart . transcript ;
284+ }
285+ }
286+ }
287+
288+ if ( content && content . trim ( ) ) {
289+ this . addMessage ( role , content . trim ( ) ) ;
290+ }
291+ } catch ( e ) {
292+ console . error ( 'Failed to add message from item:' , e , item ) ;
293+ }
294+ }
266295
267296 addMessage ( type , content ) {
268297 const messageDiv = document . createElement ( 'div' ) ;
@@ -464,4 +493,4 @@ class RealtimeDemo {
464493// Initialize the demo when the page loads
465494document . addEventListener ( 'DOMContentLoaded' , ( ) => {
466495 new RealtimeDemo ( ) ;
467- } ) ;
496+ } ) ;
0 commit comments