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-study-actions.tsx136 lines, 6.1 KiBHistoryRawDownload
1import { For, Match, Show, Switch, createSignal } from "solid-js";
2import { ApiError, formatApiError } from "@/api/client";
3import type { RagQuestionSet, RagStudyScope, RagSummary } from "@/api/client";
4import { postRagQuestions, postRagSummarize } from "@/api/rag";
5import { CelebiMarkdown } from "@/components/layout/celebi-markdown";
6import { Button } from "@/components/ui/button";
7import { Select } from "@/components/ui/select";
8import type { ragCopy } from "@/components/rag/rag-copy";
9
10type Copy = ReturnType<typeof ragCopy>;
11
12type StudyResult =
13 | { kind: "idle" }
14 | { kind: "loading" }
15 | { kind: "error"; message: string }
16 | { kind: "summary"; data: RagSummary }
17 | { kind: "questions"; data: RagQuestionSet };
18
19/** The service's own difficulty vocabulary; the backend defaults to `orta`. */
20const DIFFICULTIES = ["kolay", "orta", "zor"] as const;
21const QUESTION_COUNT = 5;
22
23/**
24 * "Study these passages" under one RAG answer: summarize or draw practice
25 * questions from exactly the range that answer cited. A 503 means no AI
26 * worker serves these requests right now — the board is told once so every
27 * answer's buttons stand down, instead of each failing in turn.
28 */
29export function RagStudyActions(props: {
30 scope: RagStudyScope;
31 copy: Copy;
32 unavailable: boolean;
33 onUnavailable: () => void;
34}) {
35 const [result, setResult] = createSignal<StudyResult>({ kind: "idle" });
36 const [difficulty, setDifficulty] = createSignal<(typeof DIFFICULTIES)[number]>("orta");
37 const busy = () => result().kind === "loading";
38
39 const run = async (request: () => Promise<StudyResult>) => {
40 if (busy() || props.unavailable) return;
41 setResult({ kind: "loading" });
42 try {
43 setResult(await request());
44 } catch (err) {
45 if (err instanceof ApiError && err.status === 503) {
46 props.onUnavailable();
47 setResult({ kind: "idle" });
48 return;
49 }
50 setResult({ kind: "error", message: props.copy.studyFailed(formatApiError(err)) });
51 }
52 };
53 const summarize = () => run(async () => ({ kind: "summary", data: await postRagSummarize(props.scope) }));
54 const practice = () =>
55 run(async () => ({
56 kind: "questions",
57 data: await postRagQuestions({ ...props.scope, n: QUESTION_COUNT, difficulty: difficulty() }),
58 }));
59 const difficultyLabel = (value: string) =>
60 value === "kolay" ? props.copy.difficultyEasy : value === "zor" ? props.copy.difficultyHard : props.copy.difficultyMedium;
61
62 return (
63 <div class="mt-3 space-y-2 border-t border-border/60 pt-3">
64 <p class="text-xs font-medium text-muted-foreground">{props.copy.studyFrom(props.scope.ders)}</p>
65 <Show
66 when={!props.unavailable}
67 fallback={<p class="text-xs text-muted-foreground">{props.copy.studyUnavailable}</p>}
68 >
69 <div class="flex flex-wrap items-center gap-2">
70 <Button type="button" size="sm" variant="outline" class="h-8 rounded-lg" disabled={busy()} onClick={() => void summarize()}>
71 {props.copy.summarize}
72 </Button>
73 <Button type="button" size="sm" variant="outline" class="h-8 rounded-lg" disabled={busy()} onClick={() => void practice()}>
74 {props.copy.practice}
75 </Button>
76 <Select
77 aria-label={props.copy.difficulty}
78 wrapperClass="w-auto"
79 class="h-8 rounded-lg text-[13px]"
80 value={difficulty()}
81 onChange={(event) => setDifficulty(event.currentTarget.value as (typeof DIFFICULTIES)[number])}
82 >
83 <For each={DIFFICULTIES}>{(value) => <option value={value}>{difficultyLabel(value)}</option>}</For>
84 </Select>
85 </div>
86 </Show>
87
88 <Switch>
89 <Match when={result().kind === "loading"}>
90 <p class="animate-pulse text-xs text-muted-foreground" role="status">{props.copy.working}</p>
91 </Match>
92 <Match when={result().kind === "error" && (result() as { message: string }).message}>
93 {(message) => <p class="text-xs text-destructive-text" role="alert">{message()}</p>}
94 </Match>
95 <Match when={result().kind === "summary" && (result() as { data: RagSummary }).data}>
96 {(summary) => (
97 <section class="space-y-2 rounded-lg border border-border-line bg-muted/30 p-3" aria-label={props.copy.summaryTitle}>
98 <p class="text-xs font-semibold text-text-strong">{props.copy.summaryTitle}</p>
99 <Show when={!summary().abstained} fallback={<p class="text-xs text-muted-foreground">{summary().text || props.copy.abstained}</p>}>
100 <CelebiMarkdown text={summary().text} />
101 </Show>
102 <Show when={summary().scope_pages.length > 0}>
103 <p class="text-xs text-muted-foreground">{props.copy.coveredPages(summary().scope_pages.join(", "))}</p>
104 </Show>
105 </section>
106 )}
107 </Match>
108 <Match when={result().kind === "questions" && (result() as { data: RagQuestionSet }).data}>
109 {(set) => (
110 <section class="space-y-2 rounded-lg border border-border-line bg-muted/30 p-3" aria-label={props.copy.questionsTitle}>
111 <p class="text-xs font-semibold text-text-strong">{props.copy.questionsTitle}</p>
112 <Show
113 when={!set().abstained && set().items.length > 0}
114 fallback={<p class="text-xs text-muted-foreground">{set().reason ? props.copy.abstainedReason(set().reason) : props.copy.noQuestions}</p>}
115 >
116 <ol class="list-decimal space-y-2 pl-5 text-sm">
117 <For each={set().items}>
118 {(item) => (
119 <li class="space-y-1">
120 <p>{item.question}</p>
121 <details class="text-xs text-muted-foreground">
122 <summary class="cursor-pointer select-none font-medium text-foreground">{props.copy.showAnswer}</summary>
123 <p class="mt-1 whitespace-pre-wrap">{item.answer}</p>
124 </details>
125 </li>
126 )}
127 </For>
128 </ol>
129 </Show>
130 </section>
131 )}
132 </Match>
133 </Switch>
134 </div>
135 );
136}