@@ -146,17 +146,58 @@ function withRun(
146146 return runs . map ( ( run , at ) => ( at === index ? change ( run ) : run ) ) ;
147147}
148148
149+ /** How a run is named when its RUN_STARTED never reached the phone and its events name no run. */
150+ const UNSTARTED = "unstarted:" ;
151+
152+ const fresh = ( id : string ) : LiveRun => ( {
153+ id,
154+ ended : false ,
155+ messages : [ ] ,
156+ tools : [ ] ,
157+ } ) ;
158+
159+ /**
160+ * The runs, with one to take this event where none would. The phone joins the thread a moment after it sends, when
161+ * the Bot's run is often under way already, and the gateway does not always replay that run's start: its words and
162+ * steps then arrive for a run the phone never saw begin. They were dropped, so nothing was drawn live and the reply
163+ * only appeared each time the messages were read again. Such a run is taken up from its first event instead: under
164+ * the id the event names, else as the newest run when that one is over.
165+ */
166+ function adopted ( runs : LiveRuns , event : LiveEvent ) : LiveRuns {
167+ const named = runOf ( event ) ;
168+ if ( named )
169+ return runs . some ( ( run ) => run . id === named )
170+ ? runs
171+ : [ ...runs , fresh ( named ) ] ;
172+ const tool = toolOf ( event ) ;
173+ const messages = [ messageOf ( event ) , text ( event . parentMessageId ) ] ;
174+ const holds = runs . some (
175+ ( run ) =>
176+ ( tool !== undefined && run . tools . some ( ( item ) => item . id === tool ) ) ||
177+ messages . some (
178+ ( id ) => id !== undefined && run . messages . some ( ( item ) => item . id === id ) ,
179+ ) ,
180+ ) ;
181+ if ( holds || ( runs . length > 0 && ! runs [ runs . length - 1 ] ?. ended ) ) return runs ;
182+ return [ ...runs , fresh ( `${ UNSTARTED } ${ messages [ 0 ] ?? tool ?? runs . length } ` ) ] ;
183+ }
184+
149185/** The runs after one more event. Events of a type it doesn't know change nothing. */
150186export function reduce ( runs : LiveRuns , event : LiveEvent ) : LiveRuns {
151187 switch ( event . type ) {
152188 case "RUN_STARTED" : {
153189 const id = runOf ( event ) ;
154190 if ( ! id || runs . some ( ( run ) => run . id === id ) ) return runs ;
155- return [ ...runs , { id , ended : false , messages : [ ] , tools : [ ] } ] ;
191+ return [ ...runs , fresh ( id ) ] ;
156192 }
157193 case "RUN_FINISHED" :
158194 case "RUN_ERROR" : {
159- const at = find ( runs , event ) ;
195+ let at = find ( runs , event ) ;
196+ // A run taken up without its start was named for its first message: its end names the real id.
197+ if ( at < 0 )
198+ at = runs . findLastIndex (
199+ ( run ) => ! run . ended && run . id . startsWith ( UNSTARTED ) ,
200+ ) ;
160201 const error =
161202 event . type === "RUN_ERROR"
162203 ? ( text ( event . message ) ?. trim ( ) ??
@@ -177,10 +218,12 @@ export function reduce(runs: LiveRuns, event: LiveEvent): LiveRuns {
177218 if ( typeof event . role === "string" && event . role !== "assistant" )
178219 return runs ;
179220 const id = messageOf ( event ) ;
180- const at = find ( runs , event ) ;
181- if ( ! id || at < 0 ) return runs ;
221+ if ( ! id ) return runs ;
222+ const taken = adopted ( runs , event ) ;
223+ const at = find ( taken , event ) ;
224+ if ( at < 0 ) return runs ;
182225 const delta = typeof event . delta === "string" ? event . delta : "" ;
183- return withRun ( runs , at , ( run ) => {
226+ return withRun ( taken , at , ( run ) => {
184227 const held = run . messages . some ( ( item ) => item . id === id ) ;
185228 if ( ! held )
186229 return { ...run , messages : [ ...run . messages , { id, text : delta } ] } ;
@@ -194,10 +237,15 @@ export function reduce(runs: LiveRuns, event: LiveEvent): LiveRuns {
194237 } ) ;
195238 }
196239 case "TOOL_CALL_START" :
197- return started ( runs , event ) ;
240+ return toolOf ( event ) && text ( event . toolCallName )
241+ ? started ( adopted ( runs , event ) , event )
242+ : runs ;
198243 case "TOOL_CALL_CHUNK" : {
199244 // A chunk stands for START, ARGS and END in one: the first one of a call names it.
200- const begun = started ( runs , event ) ;
245+ const begun =
246+ toolOf ( event ) && text ( event . toolCallName )
247+ ? started ( adopted ( runs , event ) , event )
248+ : runs ;
201249 const delta = typeof event . delta === "string" ? event . delta : "" ;
202250 return delta
203251 ? withTool ( begun , event , ( tool ) => ( {
@@ -308,9 +356,22 @@ export function stopOffer(
308356 return { runs : left , stopping : left . length === 0 } ;
309357}
310358
311- /** What the thread replayed when the socket joined, down to the runs still in progress: an ended one is in the messages. */
312- export function caughtUp ( runs : LiveRuns ) : LiveRuns {
313- return runs . filter ( ( run ) => ! run . ended ) ;
359+ /**
360+ * What the thread replayed when the socket joined, down to the runs still in progress: an ended one is in the messages.
361+ * `runId` is the run the join token said was under way: it is drawn even when the replay left out its start, so what
362+ * it says next has a run to go to.
363+ */
364+ export function caughtUp ( runs : LiveRuns , runId ?: string | null ) : LiveRuns {
365+ const going = runs . filter ( ( run ) => ! run . ended ) ;
366+ if ( ! runId || runs . some ( ( run ) => run . id === runId ) ) return going ;
367+ return [ ...going , fresh ( runId ) ] ;
368+ }
369+
370+ /** The gateway said no run is in progress in the thread (`stream_idle`): every run drawn as live is over. */
371+ export function idle ( runs : LiveRuns ) : LiveRuns {
372+ return runs . some ( ( run ) => ! run . ended )
373+ ? runs . map ( ( run ) => ( run . ended ? run : { ...run , ended : true } ) )
374+ : runs ;
314375}
315376
316377/**
0 commit comments