import { Show, Suspense, createSignal, onCleanup } from "solid-js";
import { createResource } from "@/lib/create-resource";
import { getAiCapabilities } from "@/api/ai";
import { formatApiError } from "@/api/client";
import type { RagOutput } from "@/api/client";
import type { NoteFileSource } from "@/lib/note-source";
import { Alert } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { ConfirmDialog } from "@/components/ui/confirm-dialog";
import { PageSpinner } from "@/components/ui/page-spinner";
import { TableRowActions } from "@/components/ui/table-row-actions";
import { createFlash } from "@/lib/flash";
import { formatDateTime } from "@/lib/format";
import { usePreferences, useT } from "@/stores/preferences-context";
import { IconCopy, IconDownload, IconFileText, IconSparkles, IconTrash } from "@/components/ui/icons";
import { RagOutputContent } from "@/components/notes/rag-output-content";
import {
buildRagOutputMarkdown,
ragOutputFileName,
readRagOutputDocument,
type RagOutputPassage,
} from "@/components/notes/rag-output-document";
import { ragOutputMessage, ragOutputPassagePages } from "@/components/notes/rag-output-messages";
const RAG_PAGE_SIZE = 1;
/**
* The capability a worker must have declared for a reindex call to have any
* chance of being answered. `/ai/capabilities` lists what connected services
* actually serve, so the button stays disabled until some worker serves this.
*/
const RAG_INDEX_CAPABILITY = "rag.index";
export function RagOutputsPanel(props: {
noteId: string;
active: boolean;
source: NoteFileSource;
/** The note's title, shown by name in the output drawer. */
noteTitle?: string;
/** Delete control is teacher-only; readers see the list. Defaults to true. */
canManage?: boolean;
}) {
const t = useT();
const { locale } = usePreferences();
const [error, setError] = createSignal("");
const [flash, setFlash] = createFlash();
const [deleteTarget, setDeleteTarget] = createSignal(null);
const [generating, setGenerating] = createSignal(false);
const [copied, setCopied] = createSignal(false);
const [copyError, setCopyError] = createSignal("");
/** Set when a reindex attempt ended without a new output — the truthful state. */
const [missed, setMissed] = createSignal("");
let pollTimer: ReturnType | undefined;
let copiedTimer: ReturnType | undefined;
onCleanup(() => {
if (pollTimer) clearTimeout(pollTimer);
if (copiedTimer) clearTimeout(copiedTimer);
});
const [list, { refetch }] = createResource(
() => (props.active && props.source.listRagOutputs ? props.noteId : null),
async (noteId) => props.source.listRagOutputs!(noteId, { limit: RAG_PAGE_SIZE }),
);
const [capabilities] = createResource(
() => (props.active && props.source.reindexRag ? true : null),
() => getAiCapabilities(),
);
const outputs = () => list()?.items ?? [];
const output = () => outputs()[0] ?? null;
const noteTitle = () => props.noteTitle?.trim() ?? "";
const passageHeader = (passage: RagOutputPassage) => {
const source = passage.sourceName
?? (passage.fromNote ? noteTitle() || ragOutputMessage(locale(), "noteText") : ragOutputMessage(locale(), "unnamedSource"));
const pages = ragOutputPassagePages(locale(), passage.pageStart, passage.pageEnd);
return pages ? `${source} · ${pages}` : source;
};
const passagesNote = (read: ReturnType) => {
if (!read.passagesTruncated) return "";
return read.chunksTotal != null
? ragOutputMessage(locale(), "passagesTruncated", { shown: read.passages.length, total: read.chunksTotal })
: ragOutputMessage(locale(), "passagesTruncatedNoTotal", { shown: read.passages.length });
};
const markdown = () => {
const current = output();
const read = current ? readRagOutputDocument(current) : null;
if (!current || !read) return "";
return buildRagOutputMarkdown({
title: noteTitle() || ragOutputMessage(locale(), "drawerTitle"),
date: formatDateTime(current.generated_at, locale()),
noteTitle: noteTitle(),
document: read,
labels: {
date: ragOutputMessage(locale(), "date"),
note: ragOutputMessage(locale(), "note"),
sources: ragOutputMessage(locale(), "sources"),
failed: ragOutputMessage(locale(), "failedTitle"),
otherFields: ragOutputMessage(locale(), "otherFields"),
passages: ragOutputMessage(locale(), "passages"),
passageHeader,
passagesTruncated: passagesNote(read),
},
});
};
const copy = async () => {
setCopyError("");
if (!navigator.clipboard?.writeText) {
setCopyError(ragOutputMessage(locale(), "copyFailed"));
return;
}
try {
await navigator.clipboard.writeText(markdown());
setCopied(true);
copiedTimer = setTimeout(() => setCopied(false), 2500);
} catch {
setCopied(false);
setCopyError(ragOutputMessage(locale(), "copyFailed"));
}
};
const download = () => {
const current = output();
if (!current) return;
const blob = new Blob([markdown()], { type: "text/markdown;charset=utf-8" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = ragOutputFileName(current.generated_at);
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
};
// The button is live only when a worker actually serves `rag.index`. A
// discovery failure is not evidence of absence, so leave it live and let the
// call itself report; while the list is still loading we do not know either.
const available = () => {
if (capabilities.error || capabilities.loading) return true;
const caps = capabilities();
if (!caps || caps.enabled === false) return false;
return caps.capabilities.some((c) => c.capability === RAG_INDEX_CAPABILITY);
};
const generate = async () => {
if (!props.source.reindexRag || generating()) return;
setError("");
setMissed("");
setGenerating(true);
const previousId = outputs()[0]?.id;
try {
await props.source.reindexRag(props.noteId);
let attempts = 0;
const poll = async () => {
if (!props.active) {
setGenerating(false);
return;
}
attempts += 1;
const next = await refetch();
const firstId = next?.items?.[0]?.id;
if (firstId && firstId !== previousId) {
setGenerating(false);
setFlash(t("courseNotes.ragReady"));
return;
}
if (attempts >= 24) {
// Nothing was produced. Say that plainly: no worker indexed this
// note, and the button runs the whole call again.
setGenerating(false);
setMissed(t("courseNotes.ragNotIndexed"));
return;
}
pollTimer = setTimeout(() => void poll().catch(fail), 2500);
};
const fail = (err: unknown) => {
setGenerating(false);
setError(formatApiError(err));
};
pollTimer = setTimeout(() => void poll().catch(fail), 1500);
} catch (err) {
setGenerating(false);
setError(formatApiError(err));
}
};
const remove = async () => {
const target = deleteTarget();
if (!target || !props.source.deleteRagOutput) return;
setError("");
try {
await props.source.deleteRagOutput(props.noteId, target.id);
await refetch();
setFlash(t("common.deleted"));
} catch (err) {
setError(formatApiError(err));
} finally {
setDeleteTarget(null);
}
};
return (
{t("courseNotes.ragTitle")}
{t("courseNotes.ragHint")}
,
destructive: true,
onSelect: () => setDeleteTarget(output()),
}]}
/>
, onSelect: () => void copy() },
{ label: ragOutputMessage(locale(), "download"), icon: , onSelect: download },
{ label: ragOutputMessage(locale(), "print"), icon: , onSelect: () => window.print() },
]}
/>
{flash()}
{error()}
{missed()}
{ragOutputMessage(locale(), "copied")}
{copyError()}
{t("courseNotes.ragUnavailable")}
}>
{formatApiError(list.error)}
{missed() ? t("courseNotes.ragEmptyFailed") : t("courseNotes.ragEmpty")}
}>
{(current) => (
{t("courseNotes.markdownPreview")}
{formatDateTime(current().generated_at, locale())}
)}
!open && setDeleteTarget(null)}
title={t("confirm.deleteTitle")}
variant="destructive"
summary={deleteTarget() ? formatDateTime(deleteTarget()?.generated_at ?? 0, locale()) : ""}
onConfirm={() => void remove()}
/>
);
}