forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

rag-message-row.tsx121 lines, 4.7 KiBHistoryRawDownload
1import { Match, Show, Switch, createSignal, type JSX } from "solid-js";
2import type { RagMessage } from "@/api/client";
3import { CelebiMarkdown } from "@/components/layout/celebi-markdown";
4import { IconAlert, IconCopy, IconRefresh } from "@/components/ui/icons";
5import { RagCitations } from "@/components/rag/rag-citations";
6import { RagThinkingLabel } from "@/components/rag/rag-thinking-label";
7import { cn } from "@/lib/cn";
8
9export function RagMessageRow(props: {
10 message: RagMessage;
11 /** Rendered under the citations of an answer (e.g. study actions). */
12 footer?: JSX.Element;
13 /**
14 * Re-asks the question this answer replies to. Passed only for the newest
15 * answer — re-asking an older one would append it at the end of the
16 * transcript, far from the turn the reader was looking at.
17 */
18 onRetry?: () => void;
19 labels: {
20 thinking: string;
21 thinking2: string;
22 thinking3: string;
23 copy: string;
24 copied: string;
25 retry: string;
26 sources: string;
27 source: string;
28 subject: string;
29 pages: string;
30 document: string;
31 failed: string;
32 failedWithCode: (code: string) => string;
33 abstained: string;
34 abstainedReason: (reason: string) => string;
35 };
36}) {
37 const [copied, setCopied] = createSignal(false);
38 const assistant = () => props.message.role === "assistant";
39 const failure = () => props.message.error_code
40 ? props.labels.failedWithCode(props.message.error_code)
41 : props.labels.failed;
42 const settled = () => props.message.status === "complete" || props.message.status === "failed";
43 const copyable = () => assistant() && props.message.status === "complete" && !!props.message.content;
44
45 const copy = async () => {
46 await navigator.clipboard.writeText(props.message.content);
47 setCopied(true);
48 window.setTimeout(() => setCopied(false), 1_500);
49 };
50
51 return (
52 // The user's turn is a bubble; the answer is not. An answer is the page's
53 // content, not a message in a thread, so it runs the full reading column
54 // with no frame and no avatar competing with the first line of text.
55 <article class={cn("flex", assistant() ? "justify-start" : "justify-end")}>
56 <div
57 class={cn(
58 "text-sm",
59 assistant()
60 ? "w-full min-w-0 text-foreground"
61 : "max-w-[min(46rem,88%)] rounded-xl bg-primary px-4 py-3 text-primary-foreground",
62 )}
63 >
64 <Switch>
65 <Match when={props.message.status === "pending" && !props.message.content}>
66 <p class="animate-pulse text-muted-foreground" role="status">
67 <RagThinkingLabel phases={[props.labels.thinking, props.labels.thinking2, props.labels.thinking3]} />
68 </p>
69 </Match>
70 <Match when={props.message.status === "failed"}>
71 <div class="flex items-start gap-2 text-destructive-text">
72 <IconAlert class="mt-0.5 h-4 w-4 shrink-0" />
73 <p>{failure()}</p>
74 </div>
75 </Match>
76 <Match when={props.message.abstained}>
77 <div class="space-y-1 text-muted-foreground">
78 <p>{props.message.content || props.labels.abstained}</p>
79 <Show when={props.message.reason}>
80 <p class="text-xs">{props.labels.abstainedReason(props.message.reason)}</p>
81 </Show>
82 </div>
83 </Match>
84 <Match when={true}>
85 <CelebiMarkdown text={props.message.content} />
86 </Match>
87 </Switch>
88 <Show when={assistant()}>
89 <RagCitations citations={props.message.citations} labels={props.labels} />
90 {props.footer}
91 <Show when={settled() && (copyable() || props.onRetry)}>
92 <div class="mt-2 flex flex-wrap items-center gap-3">
93 <Show when={copyable()}>
94 <button
95 type="button"
96 class="flex items-center gap-1 text-xs text-muted-foreground transition-colors hover:text-foreground"
97 onClick={() => void copy()}
98 >
99 <IconCopy class="h-3 w-3" />
100 {copied() ? props.labels.copied : props.labels.copy}
101 </button>
102 </Show>
103 <Show when={props.onRetry}>
104 {(retry) => (
105 <button
106 type="button"
107 class="flex items-center gap-1 text-xs text-muted-foreground transition-colors hover:text-foreground"
108 onClick={() => retry()()}
109 >
110 <IconRefresh class="h-3 w-3" />
111 {props.labels.retry}
112 </button>
113 )}
114 </Show>
115 </div>
116 </Show>
117 </Show>
118 </div>
119 </article>
120 );
121}