import { useI18n } from "./UiLanguage.jsx"; import React, { useState, useRef } from "react"; import Markdown from "./Markdown"; import DownloadMenu from "./DownloadMenu.jsx"; import { Upload, FileText, Download, LoaderCircle, ArrowRight, Trash2, Files, MessageSquare, Settings2, Check, } from "lucide-react"; import { json, request, post, stream, saveResponse, setToken } from "./client"; export function Assistant({ language, onLanguage, enabled }) { const { t: tr } = useI18n(); const [file, setFile] = useState(null), [busy, setBusy] = useState(false), [error, setError] = useState(""), [question, setQuestion] = useState(""), [mode, setMode] = useState("answer"), [messages, setMessages] = useState([]), [running, setRunning] = useState(false), [term, setTerm] = useState(""), [column, setColumn] = useState(""), [rows, setRows] = useState(null); const abort = useRef(null), sequence = useRef(0); async function upload(selected) { if (!selected) return; if ( selected.size > (selected.name.toLowerCase().endsWith(".csv") ? 25 : 50) * 1024 * 1024 ) { setError("PDFs may be up to 50 MB; CSVs up to 25 MB."); return; } const seq = ++sequence.current; abort.current?.abort(); setRunning(false); setBusy(true); setError(""); try { if (file) await request("/api/uploads/" + file.id, { method: "DELETE" }).catch( () => {}, ); const form = new FormData(); form.append("file", selected); const result = await json("/api/uploads", { method: "POST", body: form }); if (seq !== sequence.current) return; setFile(result); setMessages([]); setRows(null); setTerm(""); setColumn(""); } catch (e) { setFile(null); setError(e.message); } finally { if (seq === sequence.current) setBusy(false); } } async function remove() { sequence.current++; abort.current?.abort(); setRunning(false); try { if (file) await request("/api/uploads/" + file.id, { method: "DELETE" }); setFile(null); setMessages([]); setRows(null); } catch (e) { setError(e.message); } } async function ask(e) { e.preventDefault(); if (!file || running) return; const controller = new AbortController(); abort.current = controller; const seq = sequence.current; setRunning(true); setError(""); setMessages((v) => [ ...v, { role: "user", content: question }, { role: "assistant", content: "" }, ]); const text = question; setQuestion(""); try { await stream( "/api/uploads/" + file.id + "/chat", { question: text, language, mode }, (part) => { if (seq === sequence.current) setMessages((v) => v.map((m, i) => i === v.length - 1 ? { ...m, content: m.content + part } : m, ), ); }, controller.signal, ); } catch (e) { if (e.name !== "AbortError" && seq === sequence.current) setError(e.message); } finally { if (seq === sequence.current) setRunning(false); } } async function exportFile(format) { try { await saveResponse( await request(`/api/uploads/${file.id}/download?format=${format}`), "document", ); } catch (e) { setError(e.message); } } async function filter(e) { e?.preventDefault(); try { setRows( await json( `/api/uploads/${file.id}/rows?term=${encodeURIComponent(term)}&column=${encodeURIComponent(column)}`, ), ); } catch (e) { setError(e.message); } } return (

{tr("File assistant")}

{tr( "Read a PDF or examine a CSV, then ask a question or draft an office note.", )}

{error && (
{error}
)} {file && ( <>
{file.name} {file.kind === "pdf" ? tr("{value0} pages · {value1} pages checked for OCR", { value0: file.page_count, value1: file.scanned_page_count, }) : tr("{value0} rows · {value1} columns", { value0: file.profile.row_count.toLocaleString(), value1: file.profile.column_count, })}
exportFile("docx"), }, { id: "pdf", label: "PDF document (.pdf)", description: "Ready to print or share", onSelect: () => exportFile("pdf"), }, { id: "original", label: "Original file", onSelect: () => exportFile("original"), }, { id: "text", label: "Plain text (.txt)", onSelect: () => exportFile("text"), }, ]} />
{(file.warnings || []).map((warning, i) => (
{warning}
))} {file.kind === "pdf" ? (
{tr("Read extracted text and page numbers")}
{file.text}
) : ( <>
setTerm(e.target.value)} placeholder={tr("Find an exact term across columns…")} maxLength={300} />
{rows && (

{rows.total_matches.toLocaleString()} {tr(" matching rows. Showing up to the first 100.")}

)}
{file.profile.columns.map((c) => ( ))} {(rows?.rows || file.preview).map((r, i) => ( {file.profile.columns.map((c) => ( ))} ))}
{c}
{r[c] === null ? "—" : String(r[c])}
{tr("Schema, missing values & summary statistics")}
{file.profile.columns.map((c) => ( ))}
{tr("Column")} {tr("Type")} {tr("Missing values")}
{c} {file.profile.dtypes[c]} {file.profile.null_counts[c]}
{JSON.stringify(file.profile.summary_stats, null, 2)}
{rows?.distribution && Object.keys(rows.distribution).length > 0 && (

{column} {tr(" · largest categories")}

{Object.entries(rows.distribution).map(([key, value]) => (
{key} {value}
))}
)} )}

{tr("Ask about this file")}

message.role === "assistant" && message.content, ) } items={[ { id: "docx", label: "Word document (.docx)", description: "Latest response · editable", onSelect: () => exportFile("response_docx"), }, { id: "pdf", label: "PDF document (.pdf)", description: "Latest response · ready to share", onSelect: () => exportFile("response_pdf"), }, { id: "conversation-docx", label: "Word conversation (.docx)", description: "Recent questions and responses", onSelect: () => exportFile("transcript"), }, { id: "conversation-pdf", label: "PDF conversation (.pdf)", description: "Recent questions and responses", onSelect: () => exportFile("transcript_pdf"), }, ]} />

{tr( "Uploads expire after 30 minutes and are isolated to this browser session. Expired files are removed from memory automatically. Download anything you want to keep.", )}

{messages.map((m, i) => (
{m.role === "user" ? tr("Your question") : tr("From your file")}
{m.content || (running ? "Preparing the response…" : "No response received.")}
))}