forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

rag-message-row.tsx142 lines, 5.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 { LogoMark } from "@/components/brand/logo-mark";
8import { formatTime } from "@/lib/format";
9import { currentLocale } from "@/api/client";
10
11export function RagMessageRow(props: {
12 message: RagMessage;
13 /** Rendered under the citations of an answer (e.g. study actions). */
14 footer?: JSX.Element;
15 /**
16 * Re-asks the question this answer replies to. Passed only for the newest
17 * answer — re-asking an older one would append it at the end of the
18 * transcript, far from the turn the reader was looking at.
19 */
20 onRetry?: () => void;
21 labels: {
22 thinking: string;
23 thinking2: string;
24 thinking3: string;
25 copy: string;
26 copied: string;
27 retry: string;
28 sources: string;
29 source: string;
30 subject: string;
31 pages: string;
32 document: string;
33 failed: string;
34 failedWithCode: (code: string) => string;
35 abstained: string;
36 abstainedReason: (reason: string) => string;
37 };
38}) {
39 const [copied, setCopied] = createSignal(false);
40 const assistant = () => props.message.role === "assistant";
41 const failure = () => props.message.error_code
42 ? props.labels.failedWithCode(props.message.error_code)
43 : props.labels.failed;
44 const settled = () => props.message.status === "complete" || props.message.status === "failed";
45 const copyable = () => assistant() && props.message.status === "complete" && !!props.message.content;
46
47 const copy = async () => {
48 try {
49 await navigator.clipboard.writeText(props.message.content);
50 } catch {
51 // No clipboard permission (or an insecure origin): nothing to confirm.
52 return;
53 }
54 setCopied(true);
55 window.setTimeout(() => setCopied(false), 1_500);
56 };
57
58 const iconButton =
59 "inline-flex h-10 w-10 items-center justify-center rounded-md text-muted-foreground sm:h-7 sm:w-7 touch:h-10 touch:w-10 outline-hidden transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring";
60
61 return (
62 // Vibe's turns: the question is a pill on the right; the answer runs the
63 // reading column beside a small brand mark, with no frame, and its
64 // actions sit on one quiet icon row under it with the time at the end.
65 <Show
66 when={assistant()}
67 fallback={
68 <article class="flex justify-end">
69 <div class="max-w-[min(36rem,85%)] whitespace-pre-wrap rounded-3xl bg-muted px-4 py-2.5 text-sm text-foreground">
70 {props.message.content}
71 </div>
72 </article>
73 }
74 >
75 <article class="flex gap-3">
76 <span class="mt-0.5 flex h-7 w-7 shrink-0 items-center justify-center rounded-md bg-primary/10 text-primary-text" aria-hidden="true">
77 <LogoMark size={16} />
78 </span>
79 <div class="min-w-0 flex-1 text-sm text-foreground">
80 <Switch>
81 <Match when={props.message.status === "pending" && !props.message.content}>
82 <p class="animate-pulse pt-1 text-muted-foreground" role="status">
83 <RagThinkingLabel phases={[props.labels.thinking, props.labels.thinking2, props.labels.thinking3]} />
84 </p>
85 </Match>
86 <Match when={props.message.status === "failed"}>
87 <div class="flex items-start gap-2 pt-1 text-destructive-text">
88 <IconAlert class="mt-0.5 h-4 w-4 shrink-0" />
89 <p>{failure()}</p>
90 </div>
91 </Match>
92 <Match when={props.message.abstained}>
93 <div class="space-y-1 pt-1 text-muted-foreground">
94 <p>{props.message.content || props.labels.abstained}</p>
95 <Show when={props.message.reason}>
96 <p class="text-xs">{props.labels.abstainedReason(props.message.reason)}</p>
97 </Show>
98 </div>
99 </Match>
100 <Match when={true}>
101 <CelebiMarkdown text={props.message.content} />
102 </Match>
103 </Switch>
104 <RagCitations citations={props.message.citations} labels={props.labels} />
105 {props.footer}
106 <Show when={settled()}>
107 <div class="mt-2 flex items-center gap-1">
108 <Show when={copyable()}>
109 <button
110 type="button"
111 class={iconButton}
112 aria-label={copied() ? props.labels.copied : props.labels.copy}
113 title={copied() ? props.labels.copied : props.labels.copy}
114 onClick={() => void copy()}
115 >
116 <IconCopy class="h-3.5 w-3.5" />
117 </button>
118 </Show>
119 <Show when={props.onRetry}>
120 {(retry) => (
121 <button
122 type="button"
123 class={iconButton}
124 aria-label={props.labels.retry}
125 title={props.labels.retry}
126 onClick={() => retry()()}
127 >
128 <IconRefresh class="h-3.5 w-3.5" />
129 </button>
130 )}
131 </Show>
132 <Show when={copied()}>
133 <span class="text-xs text-muted-foreground" role="status">{props.labels.copied}</span>
134 </Show>
135 <span class="ml-auto text-xs tabular-nums text-muted-foreground">{formatTime(props.message.created_at, currentLocale())}</span>
136 </div>
137 </Show>
138 </div>
139 </article>
140 </Show>
141 );
142}