Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(home-chat): deduplicate flush blocks copy, persist subagent durat…
…ion on mid-stream stop
  • Loading branch information
waleedlatif1 committed Mar 15, 2026
commit 5eb93ddb816da62d7255cb5a98ecff2010709d32
17 changes: 14 additions & 3 deletions apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export function useChat(
const streamGenRef = useRef(0)
const streamingContentRef = useRef('')
const streamingBlocksRef = useRef<ContentBlock[]>([])
const subagentStartTimeRef = useRef<number | undefined>(undefined)
const executionStream = useExecutionStream()
const isHomePage = pathname.endsWith('/home')

Expand Down Expand Up @@ -503,8 +504,9 @@ export function useChat(
}

const flush = () => {
streamingBlocksRef.current = [...blocks]
const snapshot = { content: runningText, contentBlocks: [...blocks] }
const blocksCopy = [...blocks]
streamingBlocksRef.current = blocksCopy
const snapshot = { content: runningText, contentBlocks: blocksCopy }
setMessages((prev) => {
const idx = prev.findIndex((m) => m.id === assistantId)
if (idx >= 0) {
Expand Down Expand Up @@ -771,6 +773,7 @@ export function useChat(
if (name) {
activeSubagent = name
subagentStartTime = Date.now()
subagentStartTimeRef.current = subagentStartTime
blocks.push({ type: 'subagent', content: name })
flush()
}
Expand All @@ -789,6 +792,7 @@ export function useChat(
}
activeSubagent = undefined
subagentStartTime = undefined
subagentStartTimeRef.current = undefined
flush()
break
}
Expand Down Expand Up @@ -819,6 +823,10 @@ export function useChat(

const content = streamingContentRef.current

// If a subagent was in progress when stopped, compute its elapsed duration
const inProgressDuration =
subagentStartTimeRef.current != null ? Date.now() - subagentStartTimeRef.current : undefined

const storedBlocks: TaskStoredContentBlock[] = streamingBlocksRef.current.map((block) => {
if (block.type === 'tool_call' && block.toolCall) {
const isCancelled =
Expand All @@ -841,7 +849,10 @@ export function useChat(
return {
type: block.type,
content: block.content,
...(block.type === 'subagent' && block.duration != null && { duration: block.duration }),
...(block.type === 'subagent' &&
(block.duration ?? inProgressDuration) != null && {
duration: block.duration ?? inProgressDuration,
}),
}
})

Expand Down
Loading