-
Notifications
You must be signed in to change notification settings - Fork 98
Add distribution-owned response feedback UI #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/features/chat/response-feedback/ResponseFeedbackControls.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { feedbackSurveySink } from "./feedbackSurveySink"; | ||
| import { ResponseFeedbackControls } from "./ResponseFeedbackControls"; | ||
|
|
||
| vi.mock("./feedbackSurveySink", () => ({ feedbackSurveySink: vi.fn() })); | ||
|
|
||
| const sink = vi.mocked(feedbackSurveySink); | ||
|
|
||
| describe("ResponseFeedbackControls", () => { | ||
| beforeEach(() => { | ||
| localStorage.clear(); | ||
| sink.mockClear(); | ||
| }); | ||
|
|
||
| it("records only user selections", () => { | ||
| render( | ||
| <ResponseFeedbackControls sessionId="session" messageId="message" />, | ||
| ); | ||
|
|
||
| expect(sink).not.toHaveBeenCalled(); | ||
|
|
||
| fireEvent.click(screen.getByRole("button", { name: /good/i })); | ||
|
|
||
| expect(sink).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| eventType: "responded", | ||
| response: "good", | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("synchronizes the selected response across renderers", () => { | ||
| render( | ||
| <> | ||
| <ResponseFeedbackControls sessionId="session" messageId="message" /> | ||
| <ResponseFeedbackControls sessionId="session" messageId="message" /> | ||
| </>, | ||
| ); | ||
|
|
||
| const goodButtons = screen.getAllByRole("button", { name: /good/i }); | ||
| fireEvent.click(goodButtons[0]); | ||
| expect(goodButtons.every((button) => button.ariaPressed === "true")).toBe( | ||
| true, | ||
| ); | ||
|
|
||
| fireEvent.click(goodButtons[1]); | ||
| expect(goodButtons.every((button) => button.ariaPressed === "false")).toBe( | ||
| true, | ||
| ); | ||
| expect(sink.mock.calls.map(([event]) => event.response)).toEqual([ | ||
| "good", | ||
| "cleared", | ||
| ]); | ||
| }); | ||
| }); |
75 changes: 75 additions & 0 deletions
75
src/features/chat/response-feedback/ResponseFeedbackControls.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { useCallback, useSyncExternalStore } from "react"; | ||
| import { ThumbsDown, ThumbsUp } from "lucide-react"; | ||
| import { useTranslation } from "react-i18next"; | ||
| import { cn } from "@/shared/lib/cn"; | ||
| import { MessageAction } from "@/shared/ui/ai-elements/message"; | ||
| import { | ||
| getResponseFeedbackSelection, | ||
| setResponseFeedbackSelection, | ||
| subscribeResponseFeedbackSelection, | ||
| } from "./responseFeedbackState"; | ||
|
|
||
| interface ResponseFeedbackControlsProps { | ||
| sessionId: string; | ||
| messageId: string; | ||
| } | ||
|
|
||
| export function ResponseFeedbackControls({ | ||
| sessionId, | ||
| messageId, | ||
| }: ResponseFeedbackControlsProps) { | ||
| const { t } = useTranslation("chat"); | ||
| const subscribe = useCallback( | ||
| (onStoreChange: () => void) => | ||
| subscribeResponseFeedbackSelection(sessionId, messageId, onStoreChange), | ||
| [messageId, sessionId], | ||
| ); | ||
| const getSnapshot = useCallback( | ||
| () => getResponseFeedbackSelection(sessionId, messageId), | ||
| [messageId, sessionId], | ||
| ); | ||
| const selection = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); | ||
|
|
||
| const select = (requested: "good" | "bad") => { | ||
| const current = getResponseFeedbackSelection(sessionId, messageId); | ||
| const next = current === requested ? null : requested; | ||
| setResponseFeedbackSelection(sessionId, messageId, next); | ||
| }; | ||
| const goodSelected = selection === "good"; | ||
| const badSelected = selection === "bad"; | ||
| const selectedClassName = | ||
| "bg-accent text-foreground hover:bg-accent active:bg-accent"; | ||
|
|
||
| return ( | ||
| <span className="inline-flex"> | ||
| <MessageAction | ||
| size="icon-xs" | ||
| variant="ghost" | ||
| className={cn( | ||
| "text-muted-foreground/80", | ||
| goodSelected && selectedClassName, | ||
| )} | ||
| label={t("message.responseFeedbackGood")} | ||
| tooltip={t("message.responseFeedbackGood")} | ||
| aria-pressed={goodSelected} | ||
| onClick={() => select("good")} | ||
| > | ||
| <ThumbsUp className="size-3.5" /> | ||
| </MessageAction> | ||
| <MessageAction | ||
| size="icon-xs" | ||
| variant="ghost" | ||
| className={cn( | ||
| "text-muted-foreground/80", | ||
| badSelected && selectedClassName, | ||
| )} | ||
| label={t("message.responseFeedbackBad")} | ||
| tooltip={t("message.responseFeedbackBad")} | ||
| aria-pressed={badSelected} | ||
| onClick={() => select("bad")} | ||
| > | ||
| <ThumbsDown className="size-3.5" /> | ||
| </MessageAction> | ||
| </span> | ||
| ); | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/features/chat/response-feedback/feedbackSurveyEvents.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { feedbackSurveySink } from "./feedbackSurveySink"; | ||
| import { sendFeedbackSurveyEvent } from "./feedbackSurveyEvents"; | ||
|
|
||
| vi.mock("./feedbackSurveySink", () => ({ feedbackSurveySink: vi.fn() })); | ||
|
|
||
| const sink = vi.mocked(feedbackSurveySink); | ||
|
|
||
| describe("feedbackSurveyEvents", () => { | ||
| beforeEach(() => { | ||
| sink.mockClear(); | ||
| }); | ||
|
|
||
| it("forwards survey identity and state to the distribution-owned sink", () => { | ||
| const event = { | ||
| sessionId: "session", | ||
| messageId: "message", | ||
| appearanceId: "appearance", | ||
| surveyType: "response" as const, | ||
| eventType: "responded" as const, | ||
| response: "good" as const, | ||
| }; | ||
|
|
||
| sendFeedbackSurveyEvent(event); | ||
|
|
||
| expect(sink).toHaveBeenCalledOnce(); | ||
| expect(sink).toHaveBeenCalledWith(event); | ||
| }); | ||
| }); |
10 changes: 10 additions & 0 deletions
10
src/features/chat/response-feedback/feedbackSurveyEvents.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { | ||
| type FeedbackSurveySinkEvent, | ||
| feedbackSurveySink, | ||
| } from "./feedbackSurveySink"; | ||
|
|
||
| export type FeedbackSurveyEventInput = FeedbackSurveySinkEvent; | ||
|
|
||
| export function sendFeedbackSurveyEvent(input: FeedbackSurveyEventInput): void { | ||
| feedbackSurveySink(input); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export type FeedbackSurveyResponse = "good" | "bad" | "cleared"; | ||
|
|
||
| export interface FeedbackSurveySinkEvent { | ||
| sessionId: string; | ||
| messageId: string; | ||
| appearanceId: string; | ||
| surveyType: "response"; | ||
| eventType: "responded"; | ||
| response: FeedbackSurveyResponse; | ||
| } | ||
|
|
||
| /** Distribution-owned transport and ordering seam; stock Berd sends nothing. */ | ||
| export function feedbackSurveySink(_event: FeedbackSurveySinkEvent): void {} |
46 changes: 46 additions & 0 deletions
46
src/features/chat/response-feedback/responseFeedbackRows.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { selectResponseFeedbackRowIds } from "./responseFeedbackRows"; | ||
|
|
||
| describe("selectResponseFeedbackRowIds", () => { | ||
| it("prefers the answer over companion rows", () => { | ||
| expect([ | ||
| ...selectResponseFeedbackRowIds([ | ||
| { | ||
| kind: "message", | ||
| rowId: "message:assistant:companion-image", | ||
| messageId: "assistant", | ||
| responseStartMessageId: "assistant", | ||
| }, | ||
| { | ||
| kind: "message", | ||
| rowId: "message:assistant:answer", | ||
| messageId: "assistant", | ||
| responseStartMessageId: "assistant", | ||
| }, | ||
| { | ||
| kind: "message", | ||
| rowId: "message:assistant:companion-mcp-app", | ||
| messageId: "assistant", | ||
| responseStartMessageId: "assistant", | ||
| }, | ||
| ]), | ||
| ]).toEqual(["message:assistant:answer"]); | ||
| }); | ||
|
|
||
| it("uses one final host row when there is no answer row", () => { | ||
| expect([ | ||
| ...selectResponseFeedbackRowIds([ | ||
| { | ||
| kind: "assistant-content-fragment", | ||
| rowId: "message:assistant:fragment-0", | ||
| messageId: "assistant", | ||
| }, | ||
| { | ||
| kind: "assistant-content-fragment", | ||
| rowId: "message:assistant:fragment-1", | ||
| messageId: "assistant", | ||
| }, | ||
| ]), | ||
| ]).toEqual(["message:assistant:fragment-1"]); | ||
| }); | ||
| }); |
30 changes: 30 additions & 0 deletions
30
src/features/chat/response-feedback/responseFeedbackRows.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { TranscriptRowDescriptor } from "@/features/chat/transcript/projection"; | ||
|
|
||
| type FeedbackRow = Pick< | ||
| TranscriptRowDescriptor, | ||
| "kind" | "messageId" | "responseStartMessageId" | "rowId" | ||
| >; | ||
|
|
||
| export function selectResponseFeedbackRowIds( | ||
| rows: readonly FeedbackRow[], | ||
| ): ReadonlySet<string> { | ||
| const selectedByResponse = new Map< | ||
| string, | ||
| { rowId: string; isAnswer: boolean } | ||
| >(); | ||
| for (const row of rows) { | ||
| if ( | ||
| (row.kind !== "message" && row.kind !== "assistant-content-fragment") || | ||
| !row.messageId | ||
| ) { | ||
| continue; | ||
| } | ||
| const responseId = row.responseStartMessageId ?? row.messageId; | ||
| const current = selectedByResponse.get(responseId); | ||
| const isAnswer = row.rowId.endsWith(":answer"); | ||
| if (!current || isAnswer || !current.isAnswer) { | ||
| selectedByResponse.set(responseId, { rowId: row.rowId, isAnswer }); | ||
| } | ||
| } | ||
| return new Set([...selectedByResponse.values()].map(({ rowId }) => rowId)); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.