Skip to content

Commit 3003c5a

Browse files
committed
Fix issue with new async message sse being closed
1 parent 6ce3086 commit 3003c5a

2 files changed

Lines changed: 7 additions & 40 deletions

File tree

agentex-ui/hooks/use-create-task.ts

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
'use client';
22

3-
import {
4-
InfiniteData,
5-
useMutation,
6-
useQueryClient,
7-
} from '@tanstack/react-query';
3+
import { useMutation, useQueryClient } from '@tanstack/react-query';
84
import { agentRPCNonStreaming } from 'agentex/lib';
95

106
import { toast } from '@/components/agentex/toast';
117

128
import { tasksKeys } from './use-tasks';
139

1410
import type AgentexSDK from 'agentex';
15-
import type { Task, TaskListResponse } from 'agentex/resources';
11+
import type { Task } from 'agentex/resources';
1612

1713
type CreateTaskParams = {
1814
agentName: string;
@@ -49,40 +45,11 @@ export function useCreateTask({
4945

5046
return response.result;
5147
},
52-
onSuccess: (newTask, variables) => {
53-
// Helper function to update infinite query cache
54-
const updateInfiniteCache = (queryKey: readonly unknown[]) => {
55-
queryClient.setQueryData<InfiniteData<TaskListResponse>>(
56-
queryKey,
57-
old => {
58-
if (!old || !old.pages || old.pages.length === 0) {
59-
// If no cache exists, create initial structure
60-
return {
61-
pages: [[newTask]],
62-
pageParams: [1],
63-
};
64-
}
65-
66-
// Add new task to the first page (prepend to show at top)
67-
const firstPage = old.pages[0] ?? [];
68-
if (firstPage.some(t => t.id === newTask.id)) {
69-
return old; // Avoid duplicates
70-
}
71-
72-
return {
73-
...old,
74-
pages: [[newTask, ...firstPage], ...old.pages.slice(1)],
75-
};
76-
}
77-
);
78-
};
79-
80-
// Update both the agent-specific cache and the generic "all tasks" cache
81-
updateInfiniteCache(tasksKeys.byAgentName(variables.agentName));
82-
updateInfiniteCache(tasksKeys.all);
83-
84-
// Invalidate all task queries to ensure consistency
48+
onSuccess: (_, variables) => {
8549
queryClient.invalidateQueries({ queryKey: tasksKeys.all });
50+
queryClient.invalidateQueries({
51+
queryKey: tasksKeys.byAgentName(variables.agentName),
52+
});
8653
},
8754
onError: error => {
8855
toast.error({

agentex-ui/hooks/use-tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function useInfiniteTasks(
6262
return agentexClient.tasks.list(params);
6363
},
6464
getNextPageParam: (lastPage, allPages) => {
65-
if (lastPage.length < limit) {
65+
if (!lastPage || lastPage.length < limit) {
6666
return undefined;
6767
}
6868
return allPages.length + 1;

0 commit comments

Comments
 (0)