Skip to content

Commit 21da67d

Browse files
committed
fix(refetch): task sse terminal event removed and auto-refetch behaviour disabled
1 parent 7529a75 commit 21da67d

3 files changed

Lines changed: 33 additions & 91 deletions

File tree

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,16 +2622,30 @@ export function useChat(
26222622
[]
26232623
)
26242624

2625-
const invalidateChatQueries = useCallback(() => {
2626-
const activeChatId = chatIdRef.current
2627-
if (activeChatId) {
2625+
const invalidateTaskHistory = useCallback(
2626+
(refetchType: 'active' | 'none' = 'active') => {
2627+
const activeChatId = chatIdRef.current
2628+
if (!activeChatId) {
2629+
return
2630+
}
2631+
26282632
queryClient.invalidateQueries({
26292633
queryKey: taskKeys.detail(activeChatId),
2634+
refetchType,
26302635
})
2631-
}
2636+
},
2637+
[queryClient]
2638+
)
2639+
2640+
const invalidateTaskList = useCallback(() => {
26322641
queryClient.invalidateQueries({ queryKey: taskKeys.list(workspaceId) })
26332642
}, [workspaceId, queryClient])
26342643

2644+
const invalidateChatQueries = useCallback(() => {
2645+
invalidateTaskHistory()
2646+
invalidateTaskList()
2647+
}, [invalidateTaskHistory, invalidateTaskList])
2648+
26352649
const messagesRef = useRef(messages)
26362650
messagesRef.current = messages
26372651

@@ -2643,7 +2657,8 @@ export function useChat(
26432657
clearActiveTurn()
26442658
setTransportIdle()
26452659
abortControllerRef.current = null
2646-
invalidateChatQueries()
2660+
invalidateTaskHistory('none')
2661+
invalidateTaskList()
26472662

26482663
if (!options?.error) {
26492664
const cid = chatIdRef.current
@@ -2660,7 +2675,13 @@ export function useChat(
26602675
void enqueueQueueDispatchRef.current({ type: 'send_head' })
26612676
}
26622677
},
2663-
[clearActiveTurn, invalidateChatQueries, reconcileTerminalPreviewSessions, setTransportIdle]
2678+
[
2679+
clearActiveTurn,
2680+
invalidateTaskHistory,
2681+
invalidateTaskList,
2682+
reconcileTerminalPreviewSessions,
2683+
setTransportIdle,
2684+
]
26642685
)
26652686
finalizeRef.current = finalize
26662687

apps/sim/hooks/use-task-events.test.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,8 @@ describe('handleTaskStatusEvent', () => {
1616
vi.clearAllMocks()
1717
})
1818

19-
it('invalidates the task list and completed chat detail', () => {
20-
handleTaskStatusEvent(
21-
queryClient,
22-
JSON.stringify({
23-
chatId: 'chat-1',
24-
type: 'completed',
25-
timestamp: Date.now(),
26-
})
27-
)
28-
29-
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(2)
30-
expect(queryClient.invalidateQueries).toHaveBeenNthCalledWith(1, {
31-
queryKey: taskKeys.lists(),
32-
})
33-
expect(queryClient.invalidateQueries).toHaveBeenNthCalledWith(2, {
34-
queryKey: taskKeys.detail('chat-1'),
35-
})
36-
})
37-
38-
it('keeps list invalidation only for non-completed task events', () => {
39-
handleTaskStatusEvent(
40-
queryClient,
41-
JSON.stringify({
42-
chatId: 'chat-1',
43-
type: 'started',
44-
timestamp: Date.now(),
45-
})
46-
)
47-
48-
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(1)
49-
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({
50-
queryKey: taskKeys.lists(),
51-
})
52-
})
53-
54-
it('preserves list invalidation when task event payload is invalid', () => {
55-
handleTaskStatusEvent(queryClient, '{')
56-
19+
it('invalidates the task list for task status updates', () => {
20+
handleTaskStatusEvent(queryClient)
5721
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(1)
5822
expect(queryClient.invalidateQueries).toHaveBeenCalledWith({
5923
queryKey: taskKeys.lists(),

apps/sim/hooks/use-task-events.ts

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,12 @@ import { taskKeys } from '@/hooks/queries/tasks'
66

77
const logger = createLogger('TaskEvents')
88

9-
interface TaskStatusEventPayload {
10-
chatId?: string
11-
type?: 'started' | 'completed' | 'created' | 'deleted' | 'renamed'
12-
}
13-
14-
function parseTaskStatusEventPayload(data: unknown): TaskStatusEventPayload | null {
15-
let parsed = data
16-
17-
if (typeof parsed === 'string') {
18-
try {
19-
parsed = JSON.parse(parsed)
20-
} catch {
21-
return null
22-
}
23-
}
24-
25-
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
26-
return null
27-
}
28-
29-
const record = parsed as Record<string, unknown>
30-
31-
return {
32-
...(typeof record.chatId === 'string' ? { chatId: record.chatId } : {}),
33-
...(typeof record.type === 'string'
34-
? { type: record.type as TaskStatusEventPayload['type'] }
35-
: {}),
36-
}
37-
}
38-
39-
export function handleTaskStatusEvent(
40-
queryClient: Pick<QueryClient, 'invalidateQueries'>,
41-
data: unknown
42-
): void {
9+
export function handleTaskStatusEvent(queryClient: Pick<QueryClient, 'invalidateQueries'>): void {
4310
queryClient.invalidateQueries({ queryKey: taskKeys.lists() })
44-
45-
const payload = parseTaskStatusEventPayload(data)
46-
if (!payload) {
47-
logger.warn('Received invalid task_status payload')
48-
return
49-
}
50-
51-
if (payload.type === 'completed' && payload.chatId) {
52-
queryClient.invalidateQueries({ queryKey: taskKeys.detail(payload.chatId) })
53-
}
5411
}
5512

5613
/**
57-
* Subscribes to task status SSE events and invalidates task caches on changes.
14+
* Subscribes to task status SSE events and refreshes the task list on changes.
5815
*/
5916
export function useTaskEvents(workspaceId: string | undefined) {
6017
const queryClient = useQueryClient()
@@ -66,8 +23,8 @@ export function useTaskEvents(workspaceId: string | undefined) {
6623
`/api/mothership/events?workspaceId=${encodeURIComponent(workspaceId)}`
6724
)
6825

69-
eventSource.addEventListener('task_status', (event) => {
70-
handleTaskStatusEvent(queryClient, event instanceof MessageEvent ? event.data : undefined)
26+
eventSource.addEventListener('task_status', () => {
27+
handleTaskStatusEvent(queryClient)
7128
})
7229

7330
eventSource.onerror = () => {

0 commit comments

Comments
 (0)