Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix/refresh convo list on create (#251)

Robert Brennan3f55620bafb6
diff --git a/__tests__/hooks/mutation/use-create-conversation.test.tsx b/__tests__/hooks/mutation/use-create-conversation.test.tsx
index f155429e6d..c65641901f 100644
--- a/__tests__/hooks/mutation/use-create-conversation.test.tsx
+++ b/__tests__/hooks/mutation/use-create-conversation.test.tsx
@@ -86,4 +86,55 @@ describe("useCreateConversation", () => {
);
});
});
+
+ it("invalidates the conversation list and start-tasks queries on success", async () => {
+ vi.spyOn(AgentServerConversationService, "createConversation").mockResolvedValue(
+ {
+ id: "task-id",
+ created_by_user_id: null,
+ status: "READY",
+ detail: null,
+ app_conversation_id: "conv-1",
+ agent_server_url: "http://agent-server.local",
+ request: {
+ initial_message: null,
+ processors: [],
+ llm_model: null,
+ selected_repository: null,
+ selected_branch: null,
+ git_provider: "github",
+ suggested_task: null,
+ title: null,
+ trigger: null,
+ pr_number: [],
+ parent_conversation_id: null,
+ agent_type: "default",
+ },
+ created_at: new Date().toISOString(),
+ updated_at: new Date().toISOString(),
+ },
+ );
+
+ const queryClient = new QueryClient();
+ const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
+
+ const { result } = renderHook(() => useCreateConversation(), {
+ wrapper: ({ children }) => (
+ <QueryClientProvider client={queryClient}>
+ {children}
+ </QueryClientProvider>
+ ),
+ });
+
+ await result.current.mutateAsync({});
+
+ await waitFor(() => {
+ expect(invalidateSpy).toHaveBeenCalledWith({
+ queryKey: ["user", "conversations"],
+ });
+ expect(invalidateSpy).toHaveBeenCalledWith({
+ queryKey: ["start-tasks"],
+ });
+ });
+ });
});
diff --git a/__tests__/hooks/mutation/use-delete-conversation.test.tsx b/__tests__/hooks/mutation/use-delete-conversation.test.tsx
new file mode 100644
index 0000000000..8360f28cca
--- /dev/null
+++ b/__tests__/hooks/mutation/use-delete-conversation.test.tsx
@@ -0,0 +1,69 @@
+import { renderHook, waitFor } from "@testing-library/react";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { describe, expect, it, vi } from "vitest";
+import AgentServerConversationService from "#/api/conversation-service/agent-server-conversation-service.api";
+import { useDeleteConversation } from "#/hooks/mutation/use-delete-conversation";
+
+describe("useDeleteConversation", () => {
+ it("invalidates the conversation list and start-tasks queries on settle", async () => {
+ vi.spyOn(
+ AgentServerConversationService,
+ "deleteConversation",
+ ).mockResolvedValue(undefined);
+
+ const queryClient = new QueryClient();
+ const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
+
+ const { result } = renderHook(() => useDeleteConversation(), {
+ wrapper: ({ children }) => (
+ <QueryClientProvider client={queryClient}>
+ {children}
+ </QueryClientProvider>
+ ),
+ });
+
+ await result.current.mutateAsync({ conversationId: "conv-1" });
+
+ await waitFor(() => {
+ expect(invalidateSpy).toHaveBeenCalledWith({
+ queryKey: ["user", "conversations"],
+ });
+ expect(invalidateSpy).toHaveBeenCalledWith({
+ queryKey: ["start-tasks"],
+ });
+ });
+ });
+
+ it("still invalidates both queries when the delete request fails", async () => {
+ vi.spyOn(
+ AgentServerConversationService,
+ "deleteConversation",
+ ).mockRejectedValue(new Error("boom"));
+
+ const queryClient = new QueryClient({
+ defaultOptions: { mutations: { retry: false } },
+ });
+ const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
+
+ const { result } = renderHook(() => useDeleteConversation(), {
+ wrapper: ({ children }) => (
+ <QueryClientProvider client={queryClient}>
+ {children}
+ </QueryClientProvider>
+ ),
+ });
+
+ await expect(
+ result.current.mutateAsync({ conversationId: "conv-1" }),
+ ).rejects.toThrow("boom");
+
+ await waitFor(() => {
+ expect(invalidateSpy).toHaveBeenCalledWith({
+ queryKey: ["user", "conversations"],
+ });
+ expect(invalidateSpy).toHaveBeenCalledWith({
+ queryKey: ["start-tasks"],
+ });
+ });
+ });
+});
diff --git a/src/hooks/mutation/use-create-conversation.ts b/src/hooks/mutation/use-create-conversation.ts
index 67feec4584..4c8db91288 100644
--- a/src/hooks/mutation/use-create-conversation.ts
+++ b/src/hooks/mutation/use-create-conversation.ts
@@ -91,6 +91,13 @@ export const useCreateConversation = () => {
queryClient.invalidateQueries({
queryKey: ["user", "conversations"],
});
+ // The cloud SaaS path returns a start task (no app_conversation_id
+ // yet); the sidebar surfaces those via `useStartTasks` which doesn't
+ // poll, so invalidate it explicitly so the in-flight task shows up
+ // in the conversation list immediately.
+ queryClient.invalidateQueries({
+ queryKey: ["start-tasks"],
+ });
},
});
};
diff --git a/src/hooks/mutation/use-delete-conversation.ts b/src/hooks/mutation/use-delete-conversation.ts
index 4cec046d55..c20dc49ddc 100644
--- a/src/hooks/mutation/use-delete-conversation.ts
+++ b/src/hooks/mutation/use-delete-conversation.ts
@@ -42,6 +42,11 @@ export const useDeleteConversation = () => {
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["user", "conversations"] });
+ // Mirror useCreateConversation: cloud SaaS surfaces in-flight
+ // conversations via useStartTasks, so invalidate that key too so the
+ // panel refreshes regardless of whether the deleted item was a ready
+ // conversation or a still-provisioning start task.
+ queryClient.invalidateQueries({ queryKey: ["start-tasks"] });
},
});
};