From 188de69a370304ff033e5151cbe83f31dbcf961c Mon Sep 17 00:00:00 2001 From: Zhenxian Liang Date: Mon, 16 Nov 2026 19:43:50 +0800 Subject: [PATCH 1/2] Fix #26: let users explain a downvote with a short comment --- frontend/src/App.tsx | 70 +++++++++- frontend/src/feedback-comment.ts | 95 ++++++++++++++ frontend/src/styles.css | 12 ++ frontend/tests/feedback-comment.test.mjs | 159 +++++++++++++++++++++++ 4 files changed, 331 insertions(+), 5 deletions(-) create mode 100644 frontend/src/feedback-comment.ts create mode 100644 frontend/tests/feedback-comment.test.mjs diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 1ef03dc..c0e0886 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,8 +1,15 @@ -import { FormEvent, KeyboardEvent as ReactKeyboardEvent, useEffect, useRef, useState } from "react"; +import { FormEvent, KeyboardEvent as ReactKeyboardEvent, useEffect, useReducer, useRef, useState } from "react"; import { PASSWORD_INPUT_MAX_LENGTH, USERNAME_INPUT_MAX_LENGTH, validateAuthCredentials } from "./auth-validation"; import { safeCitationSourceUrl } from "./source-url"; import { QuestionStarters } from "./QuestionStarters"; import { buildWorkflowStages, mergePipelineEvent, responsePipelineEvents, type PipelineEvent } from "./workflow-progress"; +import { + FEEDBACK_COMMENT_MAX_LENGTH, + buildFeedbackRequestBody, + feedbackCommentError, + feedbackCommentReducer, + initialFeedbackCommentState, +} from "./feedback-comment"; import { Activity, AlertTriangle, @@ -256,9 +263,11 @@ function App() { const [selectedCitationMessageId, setSelectedCitationMessageId] = useState(null); const [structuredResult, setStructuredResult] = useState(null); const [error, setError] = useState(null); + const [notice, setNotice] = useState(null); const [mobilePanel, setMobilePanel] = useState<"left" | "right" | null>(null); const [feedbackSent, setFeedbackSent] = useState>({}); const [feedbackPending, setFeedbackPending] = useState>({}); + const [feedbackComment, dispatchFeedbackComment] = useReducer(feedbackCommentReducer, initialFeedbackCommentState); const bottomRef = useRef(null); const composerRef = useRef(null); const generationRef = useRef(0); @@ -414,6 +423,8 @@ function App() { setStructuredResult(null); setFeedbackSent({}); setFeedbackPending({}); + dispatchFeedbackComment({ type: "reset" }); + setNotice(null); setMobilePanel(null); } @@ -784,6 +795,8 @@ function App() { setEvents([]); setFeedbackSent({}); setFeedbackPending({}); + dispatchFeedbackComment({ type: "reset" }); + setNotice(null); committedResponseIdsRef.current.clear(); restoreFocusRef.current = null; closeMobilePanel(); @@ -791,10 +804,26 @@ function App() { startSession(sessionCreateKeyRef.current); } - async function sendFeedback(messageId: string, rating: "up" | "down") { + // A downvote opens the comment box first; the vote itself is only sent + // when the user submits from the box (an empty comment submits a plain + // downvote, exactly like the old one-click behaviour). An upvote stays + // one click and never asks for text. + function openFeedbackComment(messageId: string) { + if (feedbackSent[messageId] || feedbackPending[messageId]) return; + dispatchFeedbackComment({ type: "open", messageId }); + } + + async function sendFeedback(messageId: string, rating: "up" | "down", comment?: string) { const message = messages.find((item) => item.id === messageId); const sessionId = message?.sessionId || activeSessionIdRef.current; if (!sessionId || feedbackSent[messageId] || feedbackPending[messageId]) return; + if (rating === "down") { + const commentProblem = feedbackCommentError(comment ?? ""); + if (commentProblem) { + setError(commentProblem); + return; + } + } const generation = generationRef.current; setFeedbackPending((current) => ({ ...current, [messageId]: true })); try { @@ -805,11 +834,16 @@ function App() { // Retried clicks for one answer/rating must remain one metric event. "Idempotency-Key": `${sessionId}:${messageId}:${rating}`, }), - body: JSON.stringify({ session_id: sessionId, message_id: messageId, rating }), + body: JSON.stringify(buildFeedbackRequestBody({ sessionId, messageId, rating, comment })), }); await ensureResponseOk(response); if (generationRef.current === generation) { setFeedbackSent((current) => ({ ...current, [messageId]: rating })); + if (rating === "down") { + dispatchFeedbackComment({ type: "submitted", messageId }); + setError(null); + setNotice("反馈已提交,谢谢你的补充。"); + } } } catch (requestError) { if (generationRef.current === generation) { @@ -817,6 +851,9 @@ function App() { returnToAuthentication("登录状态已过期,请重新登录。"); return; } + // The comment box stays open with the draft kept, so a failed + // submit does not make the user retype. + setNotice(null); setError(requestError instanceof HttpResponseError ? `反馈提交失败:${formatHttpError(requestError)}` : "反馈提交失败,请稍后重试。"); @@ -985,7 +1022,30 @@ function App() { )} {message.role === "assistant" && message.latency !== undefined &&
响应 {Math.round(message.latency)} ms
} - {message.role === "assistant" && message.id !== "welcome" &&
这条回答有帮助吗?
} + {message.role === "assistant" && message.id !== "welcome" &&
这条回答有帮助吗?
} + {message.role === "assistant" && message.id !== "welcome" && feedbackComment.openMessageId === message.id && ( +
+ +