@@ -285,6 +285,7 @@ export function useChat(
285285 const streamGenRef = useRef ( 0 )
286286 const streamingContentRef = useRef ( '' )
287287 const streamingBlocksRef = useRef < ContentBlock [ ] > ( [ ] )
288+ const subagentStartTimeRef = useRef < number | undefined > ( undefined )
288289 const executionStream = useExecutionStream ( )
289290 const isHomePage = pathname . endsWith ( '/home' )
290291
@@ -503,8 +504,9 @@ export function useChat(
503504 }
504505
505506 const flush = ( ) => {
506- streamingBlocksRef . current = [ ...blocks ]
507- const snapshot = { content : runningText , contentBlocks : [ ...blocks ] }
507+ const blocksCopy = [ ...blocks ]
508+ streamingBlocksRef . current = blocksCopy
509+ const snapshot = { content : runningText , contentBlocks : blocksCopy }
508510 setMessages ( ( prev ) => {
509511 const idx = prev . findIndex ( ( m ) => m . id === assistantId )
510512 if ( idx >= 0 ) {
@@ -771,6 +773,7 @@ export function useChat(
771773 if ( name ) {
772774 activeSubagent = name
773775 subagentStartTime = Date . now ( )
776+ subagentStartTimeRef . current = subagentStartTime
774777 blocks . push ( { type : 'subagent' , content : name } )
775778 flush ( )
776779 }
@@ -789,6 +792,7 @@ export function useChat(
789792 }
790793 activeSubagent = undefined
791794 subagentStartTime = undefined
795+ subagentStartTimeRef . current = undefined
792796 flush ( )
793797 break
794798 }
@@ -819,6 +823,10 @@ export function useChat(
819823
820824 const content = streamingContentRef . current
821825
826+ // If a subagent was in progress when stopped, compute its elapsed duration
827+ const inProgressDuration =
828+ subagentStartTimeRef . current != null ? Date . now ( ) - subagentStartTimeRef . current : undefined
829+
822830 const storedBlocks : TaskStoredContentBlock [ ] = streamingBlocksRef . current . map ( ( block ) => {
823831 if ( block . type === 'tool_call' && block . toolCall ) {
824832 const isCancelled =
@@ -841,7 +849,10 @@ export function useChat(
841849 return {
842850 type : block . type ,
843851 content : block . content ,
844- ...( block . type === 'subagent' && block . duration != null && { duration : block . duration } ) ,
852+ ...( block . type === 'subagent' &&
853+ ( block . duration ?? inProgressDuration ) != null && {
854+ duration : block . duration ?? inProgressDuration ,
855+ } ) ,
845856 }
846857 } )
847858
0 commit comments