@@ -148,6 +148,26 @@ function sameVisibleTurnItemStructure(before: VisibleTurnItem, after: VisibleTur
148148 ) ;
149149}
150150
151+ function isStreamingAssistantMessage ( visibleItem : VisibleTurnItem ) : boolean {
152+ return visibleItem . item . type === "assistant_message" && visibleItem . item . streaming ;
153+ }
154+
155+ /** Whether `next` only streams more text into assistant messages that were already streaming. */
156+ function sameApartFromStreamedAssistantText (
157+ previous : OrchestrationV2ThreadProjection [ "visibleTurnItems" ] ,
158+ next : OrchestrationV2ThreadProjection [ "visibleTurnItems" ] ,
159+ ) : boolean {
160+ return sameProjectionArray (
161+ previous ,
162+ next ,
163+ ( before , after ) =>
164+ before === after ||
165+ ( isStreamingAssistantMessage ( before ) &&
166+ isStreamingAssistantMessage ( after ) &&
167+ sameVisibleTurnItemStructure ( before , after ) ) ,
168+ ) ;
169+ }
170+
151171/**
152172 * Whether `previous` and `next` have the same ThreadStructure: they differ
153173 * only by text streamed into existing assistant and reasoning items or by a
@@ -267,6 +287,32 @@ export function createEnvironmentThreadDetailAtoms<E>(
267287 ) ;
268288 } ) ;
269289
290+ // Holds while only streamed assistant text moves, so the timeline re-renders on structure;
291+ // each streaming message reads its text through assistantMessageTextAtom.
292+ const timelineVisibleTurnItemsAtomFamily = Atom . family ( ( key : string ) => {
293+ let previous = EMPTY_VISIBLE_TURN_ITEMS ;
294+ return Atom . make ( ( get ) => {
295+ const next = get ( visibleTurnItemsAtomFamily ( key ) ) ;
296+ if ( ! sameApartFromStreamedAssistantText ( previous , next ) ) previous = next ;
297+ return previous ;
298+ } ) . pipe (
299+ Atom . setIdleTTL ( 0 ) ,
300+ Atom . withLabel ( `environment-thread-timeline-visible-turn-items:${ key } ` ) ,
301+ ) ;
302+ } ) ;
303+
304+ const assistantMessageTextAtomFamily = Atom . family ( ( key : string ) => {
305+ const separator = key . lastIndexOf ( "\u0000" ) ;
306+ const thread = key . slice ( 0 , separator ) ;
307+ const itemId = key . slice ( separator + 1 ) ;
308+ return Atom . make ( ( get ) => {
309+ const item = get ( visibleTurnItemsAtomFamily ( thread ) ) . find (
310+ ( visibleItem ) => visibleItem . item . id === itemId ,
311+ ) ?. item ;
312+ return item ?. type === "assistant_message" ? item . text : null ;
313+ } ) . pipe ( Atom . setIdleTTL ( 0 ) , Atom . withLabel ( `environment-thread-assistant-message-text:${ key } ` ) ) ;
314+ } ) ;
315+
270316 const queueWorkflowAtomFamily = Atom . family ( ( key : string ) => {
271317 let previous : Pick <
272318 OrchestrationV2ThreadProjection ,
@@ -472,6 +518,10 @@ export function createEnvironmentThreadDetailAtoms<E>(
472518 visibleTurnItemsAtom : ( ref : ScopedThreadRef ) => visibleTurnItemsAtomFamily ( threadKey ( ref ) ) ,
473519 quietVisibleTurnItemsAtom : ( ref : ScopedThreadRef ) =>
474520 quietVisibleTurnItemsAtomFamily ( threadKey ( ref ) ) ,
521+ timelineVisibleTurnItemsAtom : ( ref : ScopedThreadRef ) =>
522+ timelineVisibleTurnItemsAtomFamily ( threadKey ( ref ) ) ,
523+ assistantMessageTextAtom : ( ref : ScopedThreadRef , itemId : string ) =>
524+ assistantMessageTextAtomFamily ( `${ threadKey ( ref ) } \u0000${ itemId } ` ) ,
475525 statusAtom : ( ref : ScopedThreadRef ) => statusAtomFamily ( threadKey ( ref ) ) ,
476526 sequenceAtom : ( ref : ScopedThreadRef ) => sequenceAtomFamily ( threadKey ( ref ) ) ,
477527 errorAtom : ( ref : ScopedThreadRef ) => errorAtomFamily ( threadKey ( ref ) ) ,
0 commit comments