Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(feedback): Batch event feedback status request (#9884)

sp.wackb28e0533e025
diff --git a/frontend/__tests__/hooks/query/use-feedback-exists.test.tsx b/frontend/__tests__/hooks/query/use-feedback-exists.test.tsx
deleted file mode 100644
index 0935e0658c..0000000000
--- a/frontend/__tests__/hooks/query/use-feedback-exists.test.tsx
+++ /dev/null
@@ -1,140 +0,0 @@
-import { renderHook, waitFor } from "@testing-library/react";
-import { describe, expect, it, vi, beforeEach } from "vitest";
-import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
-import OpenHands from "#/api/open-hands";
-import { useFeedbackExists } from "#/hooks/query/use-feedback-exists";
-
-// Mock the useConfig hook
-vi.mock("#/hooks/query/use-config", () => ({
- useConfig: vi.fn(),
-}));
-
-// Mock the useConversationId hook
-vi.mock("#/hooks/use-conversation-id", () => ({
- useConversationId: () => ({ conversationId: "test-conversation-id" }),
-}));
-
-describe("useFeedbackExists", () => {
- let queryClient: QueryClient;
- const mockCheckFeedbackExists = vi.spyOn(OpenHands, "checkFeedbackExists");
-
- beforeEach(() => {
- queryClient = new QueryClient({
- defaultOptions: { queries: { retry: false } },
- });
- mockCheckFeedbackExists.mockClear();
- });
-
- const wrapper = ({ children }: { children: React.ReactNode }) => (
- <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
- );
-
- it("should not call API when APP_MODE is not saas", async () => {
- const { useConfig } = await import("#/hooks/query/use-config");
- vi.mocked(useConfig).mockReturnValue({
- data: { APP_MODE: "oss" },
- isLoading: false,
- error: null,
- } as ReturnType<typeof useConfig>);
-
- const { result } = renderHook(() => useFeedbackExists(123), {
- wrapper,
- });
-
- // Wait for any potential async operations
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- // Verify that the API was not called
- expect(mockCheckFeedbackExists).not.toHaveBeenCalled();
-
- // Verify that the query is disabled
- expect(result.current.data).toBeUndefined();
- });
-
- it("should call API when APP_MODE is saas", async () => {
- const { useConfig } = await import("#/hooks/query/use-config");
- vi.mocked(useConfig).mockReturnValue({
- data: { APP_MODE: "saas" },
- isLoading: false,
- error: null,
- } as ReturnType<typeof useConfig>);
-
- mockCheckFeedbackExists.mockResolvedValue({
- exists: true,
- rating: 5,
- reason: "Great job!",
- });
-
- const { result } = renderHook(() => useFeedbackExists(123), {
- wrapper,
- });
-
- // Wait for the query to complete
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- // Verify that the API was called
- expect(mockCheckFeedbackExists).toHaveBeenCalledWith(
- "test-conversation-id",
- 123,
- );
-
- // Verify that the data is returned
- expect(result.current.data).toEqual({
- exists: true,
- rating: 5,
- reason: "Great job!",
- });
- });
-
- it("should not call API when eventId is not provided", async () => {
- const { useConfig } = await import("#/hooks/query/use-config");
- vi.mocked(useConfig).mockReturnValue({
- data: { APP_MODE: "saas" },
- isLoading: false,
- error: null,
- } as ReturnType<typeof useConfig>);
-
- const { result } = renderHook(() => useFeedbackExists(undefined), {
- wrapper,
- });
-
- // Wait for any potential async operations
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- // Verify that the API was not called
- expect(mockCheckFeedbackExists).not.toHaveBeenCalled();
-
- // Verify that the query is disabled
- expect(result.current.data).toBeUndefined();
- });
-
- it("should not call API when config is not loaded yet", async () => {
- const { useConfig } = await import("#/hooks/query/use-config");
- vi.mocked(useConfig).mockReturnValue({
- data: undefined,
- isLoading: true,
- error: null,
- } as ReturnType<typeof useConfig>);
-
- const { result } = renderHook(() => useFeedbackExists(123), {
- wrapper,
- });
-
- // Wait for any potential async operations
- await waitFor(() => {
- expect(result.current.isLoading).toBe(false);
- });
-
- // Verify that the API was not called
- expect(mockCheckFeedbackExists).not.toHaveBeenCalled();
-
- // Verify that the query is disabled
- expect(result.current.data).toBeUndefined();
- });
-});
diff --git a/frontend/src/api/open-hands.ts b/frontend/src/api/open-hands.ts
index e291e1839c..cf0127968a 100644
--- a/frontend/src/api/open-hands.ts
+++ b/frontend/src/api/open-hands.ts
@@ -20,6 +20,7 @@ import { ApiSettings, PostApiSettings, Provider } from "#/types/settings";
import { GitUser, GitRepository, Branch } from "#/types/git";
import { SuggestedTask } from "#/components/features/home/tasks/task.types";
import { RepositoryMicroagent } from "#/types/microagent-management";
+import { BatchFeedbackData } from "#/hooks/query/use-batch-feedback";
class OpenHands {
private static currentConversation: Conversation | null = null;
@@ -166,6 +167,38 @@ class OpenHands {
}
}
+ /**
+ * Get feedback for multiple events in a conversation
+ * @param conversationId The conversation ID
+ * @returns Map of event IDs to feedback data including existence, rating, reason and metadata
+ */
+ static async getBatchFeedback(conversationId: string): Promise<
+ Record<
+ string,
+ {
+ exists: boolean;
+ rating?: number;
+ reason?: string;
+ metadata?: Record<string, BatchFeedbackData>;
+ }
+ >
+ > {
+ const url = `/feedback/conversation/${conversationId}/batch`;
+ const { data } = await openHands.get<
+ Record<
+ string,
+ {
+ exists: boolean;
+ rating?: number;
+ reason?: string;
+ metadata?: Record<string, BatchFeedbackData>;
+ }
+ >
+ >(url);
+
+ return data;
+ }
+
/**
* Authenticate with GitHub token
* @returns Response with authentication status and user info if successful
diff --git a/frontend/src/hooks/mutation/use-submit-conversation-feedback.ts b/frontend/src/hooks/mutation/use-submit-conversation-feedback.ts
index ff5ae004d1..e01a4a155e 100644
--- a/frontend/src/hooks/mutation/use-submit-conversation-feedback.ts
+++ b/frontend/src/hooks/mutation/use-submit-conversation-feedback.ts
@@ -1,7 +1,10 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
-import { useTranslation } from "react-i18next";
import OpenHands from "#/api/open-hands";
import { useConversationId } from "#/hooks/use-conversation-id";
+import {
+ BatchFeedbackData,
+ getFeedbackQueryKey,
+} from "../query/use-batch-feedback";
type SubmitConversationFeedbackArgs = {
rating: number;
@@ -12,7 +15,6 @@ type SubmitConversationFeedbackArgs = {
export const useSubmitConversationFeedback = () => {
const { conversationId } = useConversationId();
const queryClient = useQueryClient();
- const { t } = useTranslation();
return useMutation({
mutationFn: ({ rating, eventId, reason }: SubmitConversationFeedbackArgs) =>
@@ -22,18 +24,56 @@ export const useSubmitConversationFeedback = () => {
eventId,
reason,
),
- onSuccess: (_, { eventId }) => {
- // Invalidate the feedback existence query to trigger a refetch
+ onMutate: async ({ rating, eventId, reason }) => {
+ if (!eventId) return { previousFeedback: null };
+
+ // Get the query key for the feedback data
+ const queryKey = getFeedbackQueryKey(conversationId);
+
+ // Cancel any outgoing refetches
+ await queryClient.cancelQueries({ queryKey });
+
+ // Snapshot the previous value
+ const previousFeedback =
+ queryClient.getQueryData<Record<string, BatchFeedbackData>>(queryKey);
+
+ // Optimistically update the cache
+ queryClient.setQueryData<Record<string, BatchFeedbackData>>(
+ queryKey,
+ (old = {}) => {
+ const newData = { ...old };
+ newData[eventId.toString()] = {
+ exists: true,
+ rating,
+ reason,
+ metadata: { source: "likert-scale" },
+ };
+ return newData;
+ },
+ );
+
+ // Return a context object with the snapshotted value
+ return { previousFeedback };
+ },
+ onError: (error, { eventId }, context) => {
+ // Roll back to the previous value on error
+ if (context?.previousFeedback && eventId) {
+ queryClient.setQueryData(
+ getFeedbackQueryKey(conversationId),
+ context.previousFeedback,
+ );
+ }
+ // Log error but don't show toast - user will just see the UI stay in unsubmitted state
+ // eslint-disable-next-line no-console
+ console.error(error);
+ },
+ onSettled: (_, __, { eventId }) => {
if (eventId) {
+ // Invalidate both the old and new query keys to ensure consistency
queryClient.invalidateQueries({
- queryKey: ["feedback", "exists", conversationId, eventId],
+ queryKey: getFeedbackQueryKey(conversationId),
});
}
},
- onError: (error) => {
- // Log error but don't show toast - user will just see the UI stay in unsubmitted state
- // eslint-disable-next-line no-console
- console.error(t("FEEDBACK$FAILED_TO_SUBMIT"), error);
- },
});
};
diff --git a/frontend/src/hooks/query/use-batch-feedback.ts b/frontend/src/hooks/query/use-batch-feedback.ts
new file mode 100644
index 0000000000..01bf72953b
--- /dev/null
+++ b/frontend/src/hooks/query/use-batch-feedback.ts
@@ -0,0 +1,52 @@
+import React from "react";
+import { useQuery, useQueryClient } from "@tanstack/react-query";
+import OpenHands from "#/api/open-hands";
+import { useConversationId } from "#/hooks/use-conversation-id";
+import { useConfig } from "#/hooks/query/use-config";
+import { useRuntimeIsReady } from "#/hooks/use-runtime-is-ready";
+
+export interface BatchFeedbackData {
+ exists: boolean;
+ rating?: number;
+ reason?: string;
+ metadata?: Record<string, unknown>;
+}
+
+// Query key factory to ensure consistency across hooks
+export const getFeedbackQueryKey = (conversationId?: string) =>
+ ["feedback", "data", conversationId] as const;
+
+// Query key factory for individual feedback existence
+export const getFeedbackExistsQueryKey = (
+ conversationId: string,
+ eventId: number,
+) => ["feedback", "exists", conversationId, eventId] as const;
+
+export const useBatchFeedback = () => {
+ const { conversationId } = useConversationId();
+ const { data: config } = useConfig();
+ const queryClient = useQueryClient();
+ const runtimeIsReady = useRuntimeIsReady();
+
+ const query = useQuery({
+ queryKey: getFeedbackQueryKey(conversationId),
+ queryFn: () => OpenHands.getBatchFeedback(conversationId!),
+ enabled: runtimeIsReady && !!conversationId && config?.APP_MODE === "saas",
+ staleTime: 1000 * 60 * 5, // 5 minutes
+ gcTime: 1000 * 60 * 15, // 15 minutes
+ });
+
+ // Update individual feedback cache entries when batch data changes
+ React.useEffect(() => {
+ if (query.data && conversationId) {
+ Object.entries(query.data).forEach(([eventId, feedback]) => {
+ queryClient.setQueryData(
+ getFeedbackExistsQueryKey(conversationId, parseInt(eventId, 10)),
+ feedback,
+ );
+ });
+ }
+ }, [query.data, conversationId, queryClient]);
+
+ return query;
+};
diff --git a/frontend/src/hooks/query/use-feedback-exists.ts b/frontend/src/hooks/query/use-feedback-exists.ts
index 281dbb9d07..5320023f62 100644
--- a/frontend/src/hooks/query/use-feedback-exists.ts
+++ b/frontend/src/hooks/query/use-feedback-exists.ts
@@ -1,25 +1,28 @@
-import { useQuery } from "@tanstack/react-query";
-import OpenHands from "#/api/open-hands";
+import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useConversationId } from "#/hooks/use-conversation-id";
import { useConfig } from "#/hooks/query/use-config";
+import { BatchFeedbackData, getFeedbackQueryKey } from "./use-batch-feedback";
-export interface FeedbackData {
- exists: boolean;
- rating?: number;
- reason?: string;
-}
+export type FeedbackData = BatchFeedbackData;
export const useFeedbackExists = (eventId?: number) => {
+ const queryClient = useQueryClient();
const { conversationId } = useConversationId();
const { data: config } = useConfig();
return useQuery<FeedbackData>({
- queryKey: ["feedback", "exists", conversationId, eventId],
+ queryKey: [...getFeedbackQueryKey(conversationId), eventId],
queryFn: () => {
if (!eventId) return { exists: false };
- return OpenHands.checkFeedbackExists(conversationId, eventId);
+
+ // Try to get the data from the batch cache
+ const batchData = queryClient.getQueryData<
+ Record<string, BatchFeedbackData>
+ >(getFeedbackQueryKey(conversationId));
+
+ return batchData?.[eventId.toString()] ?? { exists: false };
},
- enabled: !!eventId && config?.APP_MODE === "saas",
+ enabled: !!eventId && !!conversationId && config?.APP_MODE === "saas",
staleTime: 1000 * 60 * 5, // 5 minutes
gcTime: 1000 * 60 * 15, // 15 minutes
});
diff --git a/frontend/src/routes/conversation.tsx b/frontend/src/routes/conversation.tsx
index 1b77025dd0..f85c031b13 100644
--- a/frontend/src/routes/conversation.tsx
+++ b/frontend/src/routes/conversation.tsx
@@ -8,7 +8,7 @@ import { Controls } from "#/components/features/controls/controls";
import { clearTerminal } from "#/state/command-slice";
import { useEffectOnce } from "#/hooks/use-effect-once";
import { clearJupyter } from "#/state/jupyter-slice";
-
+import { useBatchFeedback } from "#/hooks/query/use-batch-feedback";
import { ChatInterface } from "../components/features/chat/chat-interface";
import { WsClientProvider } from "#/context/ws-client-provider";
import { EventHandler } from "../wrapper/event-handler";
@@ -37,6 +37,9 @@ function AppContent() {
const { data: isAuthed } = useIsAuthed();
const { providers } = useUserProviders();
+ // Fetch batch feedback data when conversation is loaded
+ useBatchFeedback();
+
const dispatch = useDispatch();
const navigate = useNavigate();