import { For, Match, Show, Switch, createSignal } from "solid-js"; import { ApiError, formatApiError } from "@/api/client"; import type { RagQuestionSet, RagStudyScope, RagSummary } from "@/api/client"; import { postRagQuestions, postRagSummarize } from "@/api/rag"; import { CelebiMarkdown } from "@/components/layout/celebi-markdown"; import { Button } from "@/components/ui/button"; import { Select } from "@/components/ui/select"; import type { ragCopy } from "@/components/rag/rag-copy"; type Copy = ReturnType; type StudyResult = | { kind: "idle" } | { kind: "loading" } | { kind: "error"; message: string } | { kind: "summary"; data: RagSummary } | { kind: "questions"; data: RagQuestionSet }; /** The service's own difficulty vocabulary; the backend defaults to `orta`. */ const DIFFICULTIES = ["kolay", "orta", "zor"] as const; const QUESTION_COUNT = 5; /** * "Study these passages" under one RAG answer: summarize or draw practice * questions from exactly the range that answer cited. A 503 means no AI * worker serves these requests right now — the board is told once so every * answer's buttons stand down, instead of each failing in turn. */ export function RagStudyActions(props: { scope: RagStudyScope; copy: Copy; unavailable: boolean; onUnavailable: () => void; }) { const [result, setResult] = createSignal({ kind: "idle" }); const [difficulty, setDifficulty] = createSignal<(typeof DIFFICULTIES)[number]>("orta"); const busy = () => result().kind === "loading"; const run = async (request: () => Promise) => { if (busy() || props.unavailable) return; setResult({ kind: "loading" }); try { setResult(await request()); } catch (err) { if (err instanceof ApiError && err.status === 503) { props.onUnavailable(); setResult({ kind: "idle" }); return; } setResult({ kind: "error", message: props.copy.studyFailed(formatApiError(err)) }); } }; const summarize = () => run(async () => ({ kind: "summary", data: await postRagSummarize(props.scope) })); const practice = () => run(async () => ({ kind: "questions", data: await postRagQuestions({ ...props.scope, n: QUESTION_COUNT, difficulty: difficulty() }), })); const difficultyLabel = (value: string) => value === "kolay" ? props.copy.difficultyEasy : value === "zor" ? props.copy.difficultyHard : props.copy.difficultyMedium; return (

{props.copy.studyFrom(props.scope.ders)}

{props.copy.studyUnavailable}

} >

{props.copy.working}

{(message) => } {(summary) => (

{props.copy.summaryTitle}

{summary().text || props.copy.abstained}

}>
0}>

{props.copy.coveredPages(summary().scope_pages.join(", "))}

)}
{(set) => (

{props.copy.questionsTitle}

0} fallback={

{set().reason ? props.copy.abstainedReason(set().reason) : props.copy.noQuestions}

} >
    {(item) => (
  1. {item.question}

    {props.copy.showAnswer}

    {item.answer}

  2. )}
)}
); }