import { CheckIcon } from "@chopin/icons"; import { ExcerptCorrection } from "./excerpt-correction"; import { answerText, questionName, SIGNALS } from "./analysis-labels"; import type { ConversationPlan } from "@chopin/protocol"; import type { CardLink } from "./links"; import type { ExcerptCorrectionAction } from "./analysis-action"; export type { ExcerptCorrectionAction } from "./analysis-action"; export function AnalysisOverview({ analysis, canEdit = false, links, messageId = "", messageText, onAddExcerpt, onCard, state, }: { analysis?: ConversationPlan.AnalysisRecord; canEdit?: boolean; links: CardLink[]; messageId?: string; messageText: string; onAddExcerpt?: (action: ExcerptCorrectionAction) => Promise; onCard?: (link: CardLink) => void; state?: ConversationPlan.State; }) { let outcomes = analysis?.outcomes ?? []; let rows = outcomes.map((outcome, index) => { let completeLink = links.find(item => item.source.start === outcome.start && item.source.end === outcome.end ); let linkedSubspans = links.filter(item => item.source.start >= outcome.start && item.source.end <= outcome.end && (item.source.start !== outcome.start || item.source.end !== outcome.end) ).map(item => item.source); let link = completeLink ?? links.find(item => item.source.start >= outcome.start && item.source.end <= outcome.end); let role = analysis?.passes.find(pass => pass.stage === "targeting")?.answers[ `c${index}_role` ]; let label = link?.label ?? (role?.type === "choice" ? role.choice.replaceAll("_", " ") : "Excerpt"); let quote = messageText.slice(outcome.start, outcome.end); return { outcome, label, link, completeLink: !!completeLink, linkedSubspans, quote: quote || link?.source.quote || `Excerpt ${index + 1}`, }; }); if (rows.length === 0 && links.length > 0) { rows = links.map(link => ({ outcome: { start: link.source.start, end: link.source.end, status: "accepted" as const, gate: "accepted", eventIds: [], }, label: link.label, link, completeLink: true, linkedSubspans: [], quote: link.source.quote, })); } let review = rows.filter(row => row.outcome.status === "review" && !row.completeLink).length; let ignored = rows.filter(row => row.outcome.status === "ignored" && !row.completeLink).length; return ( <> {rows.length > 0 && (
{rows.map(({ outcome, label, link, completeLink, linkedSubspans, quote }, index) => (
{link && onCard ? ( ) : {label}} {!link && outcome.status !== "accepted" && ( {outcome.status === "review" ? "Held for review" : "Not applied"} )}

“{plainQuote(quote)}”

{outcome.status !== "accepted" && !link && (

{outcome.gate}

)} {["review", "ignored"].includes(outcome.status) && ( )}
))}
)} {outcomes.length > 0 && (review > 0 || ignored > 0) && (

{review} held for review · {ignored} not applied

)} ); } /** Excerpts are raw Markdown source; show them as read, without emphasis markers. */ export function plainQuote(text: string): string { return text.replace(/\*\*|__|~~|`/g, "").replace( /(^|[^\w])[*_](?=\S)|(?<=\S)[*_](?=[^\w]|$)/g, "$1", ); } /** Model signals and run details, for the closed Diagnostics disclosure. */ export function AnalysisDiagnostics({ analysis }: { analysis?: ConversationPlan.AnalysisRecord }) { if (!analysis) return null; let triage = analysis.passes.find(pass => pass.stage === "triage"); let signals = Object.entries(triage?.answers ?? {}) .filter(([key, answer]) => SIGNALS[key] && answer.type === "noul" && answer.noul >= 0.55) .sort((a, b) => (b[1] as { noul: number }).noul - (a[1] as { noul: number }).noul) .map(([key]) => SIGNALS[key]); return (
{signals.length > 0 &&

Detected: {signals.join(", ")}

} {analysis.error &&

Error: {analysis.error}

}

Model {analysis.modelVersion || "not recorded"} · Questions{" "} {analysis.questionSetVersion || "not recorded"} {analysis.latencyMs === undefined ? "" : ` · ${analysis.latencyMs} ms`}

{analysis.policyGate &&

Policy: {analysis.policyGate}

} {analysis.quoteValidation && (

Source quotes: {analysis.quoteValidation.filter(range => range.valid).length}/{analysis .quoteValidation.length} valid

)} {analysis.passes.map((pass, index) => (

{pass.stage}

{Object.entries(pass.answers).map(([key, answer]) => (
{questionName(key)}
{answerText(answer)}
))}
))}
); }