File size: 11,607 Bytes
0c52aa7 f55ff46 0c52aa7 f55ff46 0c52aa7 f55ff46 0c52aa7 d56270d f55ff46 0c52aa7 f55ff46 0c52aa7 f55ff46 0c52aa7 d56270d f55ff46 0c52aa7 f55ff46 0c52aa7 f55ff46 0c52aa7 f55ff46 d56270d f55ff46 0c52aa7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | "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 (
<Pill tone="good">
<Check size={13} /> Matches the analysts' answer
</Pill>
);
if (g.outcome === "wrong")
return (
<Pill tone="warn">
<CircleAlert size={13} /> Differs from the analysts' answer
</Pill>
);
if (g.outcome === "refused")
return (
<Pill>
<SearchX size={13} /> Not found in the pages
</Pill>
);
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<number | null>(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 (
<>
<Banner
crumbs={[{ label: "SumProof", href: "/" }, { label: "Filing search" }]}
title="Filing search"
pills={
<>
<Pill tone="dark">12,013 pages · 84 SEC filings</Pill>
<Pill>150 analyst questions</Pill>
</>
}
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."
>
<QuestionPicker questions={questions} value={qi} onChange={choose} />
</Banner>
{error && <Card>Could not load this question. Check that the exported data is present in public/data/q.</Card>}
{!detail && !error && <Card>Loading the question…</Card>}
{detail && (
<>
<Card
className="mb-5"
title="SumProof's answer"
icon={<Sparkles size={18} />}
action={
gen ? (
<>
{reasoned && <Pill tone="dark">Reasoned answer</Pill>}
<OutcomePill g={gen} />
</>
) : null
}
>
{gen ? (
<>
<p className="text-[15px] leading-relaxed whitespace-pre-line text-ink">
{gen.outcome === "refused" ? "The retrieved pages do not contain the answer." : answerBody(gen.text)}
</p>
{gen.cited.length > 0 && (
<div className="mt-3 flex flex-wrap items-center gap-2 text-[13px] text-muted">
Source:
{gen.cited.map((row) => {
const p = detail.pages[String(row)];
return p ? (
<button
key={row}
type="button"
onClick={() => setSelected(row)}
className="flex items-center gap-1.5 rounded-full border border-line bg-surface-solid px-3 py-1 text-[12.5px] font-medium text-accent hover:border-accent"
>
<FileText size={13} /> {prettyDoc(p.doc)}, page {p.page + 1}
</button>
) : null;
})}
</div>
)}
</>
) : (
<p className="text-[13.5px] text-muted">No generated answer for this question.</p>
)}
<div className="mt-4 flex flex-wrap items-center gap-2 border-t border-line pt-3 text-[12.5px] text-muted">
<Files size={14} />
{detail.detected ? <>Searched {detail.detected.filings.map(prettyDoc).join(", ")}</> : <>No company named, so all 84 filings were searched</>}
<span className="ml-auto">
<Pill>{TYPE_LABEL[detail.type] ?? detail.type}</Pill>
</span>
</div>
<div className="mt-3 rounded-xl border border-line bg-sunken p-3.5">
<button
type="button"
onClick={() => setShowAnswer((s) => !s)}
aria-expanded={showAnswer}
className="flex items-center gap-2 text-[13.5px] font-medium text-accent"
>
<MessageSquareQuote size={16} /> {showAnswer ? "Hide" : "Show"} the analysts' reference answer
</button>
{showAnswer && (
<div className="mt-2 text-[13.5px] leading-relaxed text-ink">
<p className="font-semibold">{detail.answer}</p>
{detail.justification && <p className="mt-1 text-muted">{detail.justification}</p>}
</div>
)}
</div>
<p className="mt-3 text-[12px] text-muted">
{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.
</p>
</Card>
<div className="grid grid-cols-1 gap-5 xl:grid-cols-[minmax(0,5fr)_minmax(0,7fr)]">
<Card
title="Top evidence pages"
icon={<FileText size={18} />}
action={
goldRank > 0 && goldRank <= 5 ? (
<Pill tone="good">
<Check size={13} /> analysts' page at rank {goldRank}
</Pill>
) : goldRank > 5 ? (
<Pill tone="warn">analysts' page at rank {goldRank}</Pill>
) : (
<Pill tone="warn">analysts' page not in top 10</Pill>
)
}
>
<ol className="flex flex-col gap-2.5">
{hits.map((h, k) => {
const p = detail.pages[String(h.row)];
const active = selected === h.row;
return (
<li key={h.row}>
<button
type="button"
onClick={() => setSelected(h.row)}
aria-pressed={active}
className={`w-full rounded-xl border p-3.5 text-left transition ${
active ? "border-accent bg-sunken shadow-card" : "border-line bg-surface-solid hover:border-accent/60"
}`}
>
<div className="flex flex-wrap items-center gap-2">
<span className="accent-fill grid h-6 w-6 place-items-center rounded-full text-[12px] font-bold text-white">{k + 1}</span>
<span className="text-[13.5px] font-bold text-ink">{prettyDoc(p.doc)}</span>
<span className="text-[12.5px] text-muted">page {p.page + 1}</span>
{h.gold && (
<Pill tone="good" className="ml-auto">
<Check size={12} /> evidence
</Pill>
)}
</div>
<p className="mt-2 line-clamp-3 text-[12.5px] leading-relaxed text-muted">
<Highlight text={snippet(p.text, terms)} terms={terms} />
</p>
{h.score !== undefined && <p className="tabular mt-1.5 text-[11.5px] text-muted">reranker score {h.score.toFixed(2)}</p>}
</button>
</li>
);
})}
</ol>
</Card>
<Card
title={page ? prettyDoc(page.doc) : "Page viewer"}
icon={<FileText size={18} />}
action={
page ? (
<>
{isGold && <Pill tone="good">analysts' evidence page</Pill>}
<Pill tone="dark">page {page.page + 1}</Pill>
</>
) : null
}
>
{page ? (
<>
<div className="max-h-[620px] overflow-y-auto rounded-xl border border-line bg-surface-solid p-5 font-mono text-[12.5px] leading-relaxed whitespace-pre-wrap text-ink">
<Highlight text={page.text} terms={terms} />
</div>
<div className="mt-3 flex flex-wrap items-center gap-3 text-[12.5px] text-muted">
<span>Text extracted with PyMuPDF; highlights are words from the question.</span>
{summary && detail.doc_link && page.doc === detail.gold[0]?.doc && (
<a href={`${detail.doc_link}#page=${page.page + 1}`} target="_blank" rel="noreferrer" className="ml-auto flex items-center gap-1 font-semibold text-accent-deep hover:underline">
Open original filing <ExternalLink size={13} />
</a>
)}
</div>
</>
) : (
<p className="text-[13.5px] text-muted">Select a page to read it.</p>
)}
</Card>
</div>
</>
)}
</>
);
}
|