"use client";
import { Check, CircleAlert, ExternalLink, FileText, Files, MessageSquareQuote, SearchX, Sparkles } from "lucide-react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useEffect, useMemo, useState } from "react";
import { prettyDoc, TYPE_LABEL, type GeneratedAnswer, type QuestionSummary } from "@/lib/types";
import { Highlight, keyTerms, QuestionPicker, snippet, useQuestion } from "./QuestionPicker";
import { Banner, Card, Pill } from "./ui";
/** The answer without its "Source: [n]" line or inline [n] markers (the sources are shown as chips). */
function answerBody(text: string): string {
return text
.replace(/\n?\s*sources?\s*:[\s\S]*$/i, "")
.replace(/\s*\[\d{1,2}\]/g, "")
.trim();
}
function OutcomePill({ g }: { g: GeneratedAnswer }) {
if (g.outcome === "correct")
return (
Matches the analysts' answer
);
if (g.outcome === "wrong")
return (
Differs from the analysts' answer
);
if (g.outcome === "refused")
return (
Not found in the pages
);
return null;
}
export function FilingSearch({ questions }: { questions: QuestionSummary[] }) {
const params = useSearchParams();
const router = useRouter();
const pathname = usePathname();
const fromUrl = params.has("q") ? Number(params.get("q")) : NaN;
const [qi, setQi] = useState(Number.isInteger(fromUrl) && fromUrl >= 0 && fromUrl < questions.length ? fromUrl : questions[0].i);
const { detail, error } = useQuestion(qi);
const [selected, setSelected] = useState(null);
const [showAnswer, setShowAnswer] = useState(false);
const choose = (i: number) => {
setQi(i);
setSelected(null);
setShowAnswer(false);
router.replace(`${pathname}?q=${i}`, { scroll: false });
};
const hits = useMemo(() => detail?.methods.reranked.slice(0, 5) ?? [], [detail]);
// The most accurate measured setting: reasoning before answering (calculation questions), else top-5 RAG.
const reasoned = detail?.generated.think_4096;
const gen = reasoned ?? detail?.generated.top5;
// Open the page the answer cites; otherwise the top-ranked page.
useEffect(() => {
if (detail && selected === null && hits.length) setSelected(gen?.cited.find((r) => detail.pages[String(r)]) ?? hits[0].row);
}, [detail, selected, hits, gen]);
const terms = detail ? keyTerms(detail.question) : [];
const page = detail && selected !== null ? detail.pages[String(selected)] : null;
const isGold = detail && selected !== null ? detail.gold.some((g) => g.row === selected) : false;
const goldRank = detail ? detail.methods.reranked.findIndex((h) => h.gold) + 1 : 0;
const summary = questions.find((q) => q.i === qi);
return (
<>
12,013 pages · 84 SEC filings150 analyst questions
>
}
meta="Pick an analyst question about a company's SEC filing. SumProof finds the pages that answer it, writes the answer, and shows the page it used."
>
{error && Could not load this question. Check that the exported data is present in public/data/q.}
{!detail && !error && Loading the question…}
{detail && (
<>
}
action={
gen ? (
<>
{reasoned && Reasoned answer}
>
) : null
}
>
{gen ? (
<>
{gen.outcome === "refused" ? "The retrieved pages do not contain the answer." : answerBody(gen.text)}
{gen.cited.length > 0 && (
Source:
{gen.cited.map((row) => {
const p = detail.pages[String(row)];
return p ? (
) : null;
})}
)}
>
) : (
No generated answer for this question.
)}
{detail.detected ? <>Searched {detail.detected.filings.map(prettyDoc).join(", ")}> : <>No company named, so all 84 filings were searched>}
{TYPE_LABEL[detail.type] ?? detail.type}
{showAnswer && (
{detail.answer}
{detail.justification &&
{detail.justification}
}
)}
{reasoned
? `Written by Qwen3.5-4B from the 5 best pages below, after reasoning for ${reasoned.thinking_tokens?.toLocaleString("en-US")} tokens: on calculation questions this lifts accuracy from 38% to 68%.`
: "Written by Qwen3.5-4B from the 5 best pages below."}{" "}
AI can misread tables and calculations, so check the source page before relying on a number.
}
action={
goldRank > 0 && goldRank <= 5 ? (
analysts' page at rank {goldRank}
) : goldRank > 5 ? (
analysts' page at rank {goldRank}
) : (
analysts' page not in top 10
)
}
>
{hits.map((h, k) => {
const p = detail.pages[String(h.row)];
const active = selected === h.row;
return (
Text extracted with PyMuPDF; highlights are words from the question.
{summary && detail.doc_link && page.doc === detail.gold[0]?.doc && (
Open original filing
)}