Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Show Likert scale feedback form on AWAITING_USER_INPUT and ERROR agent states (#9292)

Xingyao Wang2df453642032
diff --git a/frontend/__tests__/components/event-message.test.tsx b/frontend/__tests__/components/event-message.test.tsx
new file mode 100644
index 0000000000..f197d2d8ab
--- /dev/null
+++ b/frontend/__tests__/components/event-message.test.tsx
@@ -0,0 +1,194 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { screen } from "@testing-library/react";
+import { renderWithProviders } from "test-utils";
+import { EventMessage } from "#/components/features/chat/event-message";
+
+vi.mock("#/hooks/query/use-config", () => ({
+ useConfig: () => ({
+ data: { APP_MODE: "saas" },
+ }),
+}));
+
+vi.mock("#/hooks/query/use-feedback-exists", () => ({
+ useFeedbackExists: (eventId: number | undefined) => ({
+ data: { exists: false },
+ isLoading: false,
+ }),
+}));
+
+describe("EventMessage", () => {
+ afterEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("should render LikertScale for finish action when it's the last message", () => {
+ const finishEvent = {
+ id: 123,
+ source: "agent" as const,
+ action: "finish" as const,
+ args: {
+ final_thought: "Task completed successfully",
+ task_completed: "success" as const,
+ outputs: {},
+ thought: "Task completed successfully",
+ },
+ message: "Task completed successfully",
+ timestamp: new Date().toISOString(),
+ };
+
+ renderWithProviders(
+ <EventMessage
+ event={finishEvent}
+ hasObservationPair={false}
+ isAwaitingUserConfirmation={false}
+ isLastMessage={true}
+ isInLast10Actions={true}
+ />
+ );
+
+ expect(screen.getByLabelText("Rate 1 stars")).toBeInTheDocument();
+ expect(screen.getByLabelText("Rate 5 stars")).toBeInTheDocument();
+ });
+
+ it("should render LikertScale for assistant message when it's the last message", () => {
+ const assistantMessageEvent = {
+ id: 456,
+ source: "agent" as const,
+ action: "message" as const,
+ args: {
+ thought: "I need more information to proceed.",
+ image_urls: null,
+ file_urls: [],
+ wait_for_response: true,
+ },
+ message: "I need more information to proceed.",
+ timestamp: new Date().toISOString(),
+ };
+
+ renderWithProviders(
+ <EventMessage
+ event={assistantMessageEvent}
+ hasObservationPair={false}
+ isAwaitingUserConfirmation={false}
+ isLastMessage={true}
+ isInLast10Actions={true}
+ />
+ );
+
+ expect(screen.getByLabelText("Rate 1 stars")).toBeInTheDocument();
+ expect(screen.getByLabelText("Rate 5 stars")).toBeInTheDocument();
+ });
+
+ it("should render LikertScale for error observation when it's the last message", () => {
+ const errorEvent = {
+ id: 789,
+ source: "user" as const,
+ observation: "error" as const,
+ content: "An error occurred",
+ extras: {
+ error_id: "test-error-123",
+ },
+ message: "An error occurred",
+ timestamp: new Date().toISOString(),
+ cause: 123,
+ };
+
+ renderWithProviders(
+ <EventMessage
+ event={errorEvent}
+ hasObservationPair={false}
+ isAwaitingUserConfirmation={false}
+ isLastMessage={true}
+ isInLast10Actions={true}
+ />
+ );
+
+ expect(screen.getByLabelText("Rate 1 stars")).toBeInTheDocument();
+ expect(screen.getByLabelText("Rate 5 stars")).toBeInTheDocument();
+ });
+
+ it("should NOT render LikertScale when not the last message", () => {
+ const finishEvent = {
+ id: 101,
+ source: "agent" as const,
+ action: "finish" as const,
+ args: {
+ final_thought: "Task completed successfully",
+ task_completed: "success" as const,
+ outputs: {},
+ thought: "Task completed successfully",
+ },
+ message: "Task completed successfully",
+ timestamp: new Date().toISOString(),
+ };
+
+ renderWithProviders(
+ <EventMessage
+ event={finishEvent}
+ hasObservationPair={false}
+ isAwaitingUserConfirmation={false}
+ isLastMessage={false}
+ isInLast10Actions={false}
+ />
+ );
+
+ expect(screen.queryByLabelText("Rate 1 stars")).not.toBeInTheDocument();
+ expect(screen.queryByLabelText("Rate 5 stars")).not.toBeInTheDocument();
+ });
+
+ it("should render LikertScale for error observation when in last 10 actions but not last message", () => {
+ const errorEvent = {
+ id: 999,
+ source: "user" as const,
+ observation: "error" as const,
+ content: "An error occurred",
+ extras: {
+ error_id: "test-error-456",
+ },
+ message: "An error occurred",
+ timestamp: new Date().toISOString(),
+ cause: 123,
+ };
+
+ renderWithProviders(
+ <EventMessage
+ event={errorEvent}
+ hasObservationPair={false}
+ isAwaitingUserConfirmation={false}
+ isLastMessage={false}
+ isInLast10Actions={true}
+ />
+ );
+
+ expect(screen.getByLabelText("Rate 1 stars")).toBeInTheDocument();
+ expect(screen.getByLabelText("Rate 5 stars")).toBeInTheDocument();
+ });
+
+ it("should NOT render LikertScale for error observation when not in last 10 actions", () => {
+ const errorEvent = {
+ id: 888,
+ source: "user" as const,
+ observation: "error" as const,
+ content: "An error occurred",
+ extras: {
+ error_id: "test-error-789",
+ },
+ message: "An error occurred",
+ timestamp: new Date().toISOString(),
+ cause: 123,
+ };
+
+ renderWithProviders(
+ <EventMessage
+ event={errorEvent}
+ hasObservationPair={false}
+ isAwaitingUserConfirmation={false}
+ isLastMessage={false}
+ isInLast10Actions={false}
+ />
+ );
+
+ expect(screen.queryByLabelText("Rate 1 stars")).not.toBeInTheDocument();
+ expect(screen.queryByLabelText("Rate 5 stars")).not.toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/components/features/chat/event-message.tsx b/frontend/src/components/features/chat/event-message.tsx
index fa8bd57ed0..537042e69a 100644
--- a/frontend/src/components/features/chat/event-message.tsx
+++ b/frontend/src/components/features/chat/event-message.tsx
@@ -35,6 +35,7 @@ interface EventMessageProps {
hasObservationPair: boolean;
isAwaitingUserConfirmation: boolean;
isLastMessage: boolean;
+ isInLast10Actions: boolean;
}
export function EventMessage({
@@ -42,25 +43,53 @@ export function EventMessage({
hasObservationPair,
isAwaitingUserConfirmation,
isLastMessage,
+ isInLast10Actions,
}: EventMessageProps) {
const shouldShowConfirmationButtons =
isLastMessage && event.source === "agent" && isAwaitingUserConfirmation;
const { data: config } = useConfig();
- // Use our query hook to check if feedback exists and get rating/reason
const {
data: feedbackData = { exists: false },
isLoading: isCheckingFeedback,
- } = useFeedbackExists(isFinishAction(event) ? event.id : undefined);
+ } = useFeedbackExists(event.id);
+
+ const renderLikertScale = () => {
+ if (config?.APP_MODE !== "saas" || isCheckingFeedback) {
+ return null;
+ }
+
+ // For error observations, show if in last 10 actions
+ // For other events, show only if it's the last message
+ const shouldShow = isErrorObservation(event)
+ ? isInLast10Actions
+ : isLastMessage;
+
+ if (!shouldShow) {
+ return null;
+ }
- if (isErrorObservation(event)) {
return (
- <ErrorMessage
- errorId={event.extras.error_id}
- defaultMessage={event.message}
+ <LikertScale
+ eventId={event.id}
+ initiallySubmitted={feedbackData.exists}
+ initialRating={feedbackData.rating}
+ initialReason={feedbackData.reason}
/>
);
+ };
+
+ if (isErrorObservation(event)) {
+ return (
+ <>
+ <ErrorMessage
+ errorId={event.extras.error_id}
+ defaultMessage={event.message}
+ />
+ {renderLikertScale()}
+ </>
+ );
}
if (hasObservationPair && isOpenHandsAction(event)) {
@@ -70,24 +99,11 @@ export function EventMessage({
return null;
}
- const showLikertScale =
- config?.APP_MODE === "saas" &&
- isFinishAction(event) &&
- isLastMessage &&
- !isCheckingFeedback;
-
if (isFinishAction(event)) {
return (
<>
<ChatMessage type="agent" message={getEventContent(event).details} />
- {showLikertScale && (
- <LikertScale
- eventId={event.id}
- initiallySubmitted={feedbackData.exists}
- initialRating={feedbackData.rating}
- initialReason={feedbackData.reason}
- />
- )}
+ {renderLikertScale()}
</>
);
}
@@ -96,15 +112,20 @@ export function EventMessage({
const message = parseMessageFromEvent(event);
return (
- <ChatMessage type={event.source} message={message}>
- {event.args.image_urls && event.args.image_urls.length > 0 && (
- <ImageCarousel size="small" images={event.args.image_urls} />
- )}
- {event.args.file_urls && event.args.file_urls.length > 0 && (
- <FileList files={event.args.file_urls} />
- )}
- {shouldShowConfirmationButtons && <ConfirmationButtons />}
- </ChatMessage>
+ <>
+ <ChatMessage type={event.source} message={message}>
+ {event.args.image_urls && event.args.image_urls.length > 0 && (
+ <ImageCarousel size="small" images={event.args.image_urls} />
+ )}
+ {event.args.file_urls && event.args.file_urls.length > 0 && (
+ <FileList files={event.args.file_urls} />
+ )}
+ {shouldShowConfirmationButtons && <ConfirmationButtons />}
+ </ChatMessage>
+ {isAssistantMessage(event) &&
+ event.action === "message" &&
+ renderLikertScale()}
+ </>
);
}
diff --git a/frontend/src/components/features/chat/messages.tsx b/frontend/src/components/features/chat/messages.tsx
index baaf436715..04b3ad9824 100644
--- a/frontend/src/components/features/chat/messages.tsx
+++ b/frontend/src/components/features/chat/messages.tsx
@@ -39,6 +39,7 @@ export const Messages: React.FC<MessagesProps> = React.memo(
hasObservationPair={actionHasObservationPair(message)}
isAwaitingUserConfirmation={isAwaitingUserConfirmation}
isLastMessage={messages.length - 1 === index}
+ isInLast10Actions={messages.length - 1 - index < 10}
/>
))}