Sadum1's picture
Upload folder using huggingface_hub
930e4a4 verified
Raw History Blame Contribute Delete
3.77 kB
import { useState } from "react";
import { generateSynthesis } from "../api/protocols";
import type { SynthesisResult } from "../types";
interface SynthesisPanelProps {
protocolId: string;
initialSynthesis: SynthesisResult | null;
}
// Only matches real evidence-ID citations (e.g. [pubmed-12345],
// [europepmc-98765]) - deliberately NOT a bare [a-zA-Z0-9_-]+ pattern, since
// the model sometimes brackets abbreviations (e.g. "[ALT]") when it isn't
// citing anything; matching only known source prefixes avoids styling
// those as if they were real citations.
const BOLD_PATTERN = /(\*\*(.+?)\*\*)|(\[((?:pubmed|europepmc)-[a-zA-Z0-9._-]+)\])/g;
function renderInline(text: string) {
const nodes: React.ReactNode[] = [];
let lastIndex = 0;
let key = 0;
for (const match of text.matchAll(BOLD_PATTERN)) {
const index = match.index ?? 0;
if (index > lastIndex) nodes.push(text.slice(lastIndex, index));
const [full, boldFull, boldText, , citationId] = match;
if (boldFull) {
nodes.push(<strong key={key++}>{boldText}</strong>);
} else if (citationId) {
nodes.push(
<span key={key++} className="synthesis-citation">
{citationId}
</span>
);
}
lastIndex = index + full.length;
}
if (lastIndex < text.length) nodes.push(text.slice(lastIndex));
return nodes;
}
function renderSynthesisBody(text: string) {
const lines = text.split("\n");
const blocks: React.ReactNode[] = [];
let listBuffer: string[] = [];
let blockKey = 0;
const flushList = () => {
if (listBuffer.length === 0) return;
blocks.push(
<ul key={`ul-${blockKey++}`}>
{listBuffer.map((item, i) => (
<li key={i}>{renderInline(item)}</li>
))}
</ul>
);
listBuffer = [];
};
for (const rawLine of lines) {
const line = rawLine.trim();
if (line.startsWith("- ") || line.startsWith("* ")) {
listBuffer.push(line.slice(2));
continue;
}
flushList();
if (line.length > 0) {
blocks.push(<p key={`p-${blockKey++}`}>{renderInline(line)}</p>);
}
}
flushList();
return blocks;
}
export function SynthesisPanel({ protocolId, initialSynthesis }: SynthesisPanelProps) {
const [synthesis, setSynthesis] = useState<SynthesisResult | null>(initialSynthesis);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
async function handleGenerate() {
setLoading(true);
setError(null);
try {
const result = await generateSynthesis(protocolId);
setSynthesis(result);
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to generate synthesis.");
} finally {
setLoading(false);
}
}
return (
<div className="synthesis-panel">
<div className="synthesis-header">
<h3>Narrative synthesis</h3>
<button className="secondary-btn" onClick={handleGenerate} disabled={loading}>
{loading
? "Generating…"
: synthesis
? "Regenerate"
: "Generate synthesis"}
</button>
</div>
{error && <div className="form-error">{error}</div>}
{synthesis ? (
<>
<div className="synthesis-text">{renderSynthesisBody(synthesis.text)}</div>
<p className="synthesis-timestamp">
Generated {new Date(synthesis.generated_at).toLocaleString()}
</p>
</>
) : (
!error && (
<p className="empty-state">
No synthesis generated yet. This step uses an LLM to draft prose
from the evidence table above — requires GROQ_API_KEY set in the
backend's .env.
</p>
)
)}
</div>
);
}