caelum0x 9c55b78072 feat: Builder Track — 3 named-user tools made demo-ready (self-serve, built to last)
For "Built Beyond the Bench": each tool now lets its named user finish end-to-end
without the builder, with a one-click realistic example, honest loading/empty/error
states (incl. a degraded state if the Anthropic key is usage-capped — no white-screen),
and reasoned, provenance-first results.

- Lab Notebook companion (wet-lab scientist): messy/dictated bench notes -> structured,
  searchable record; grounded vs auto-inferred badges (verbatim quote per grounded
  field, dropped if not found — never invented); Copy-as-JSON / Export-.md; 503 degraded
  path on the structure route.
- Trial Matcher (research coordinator): patient vignette -> ranked ClinicalTrials.gov
  candidates with per-criterion inclusion/exclusion reasoning; honest "unknown — add the
  fact and re-run" for unstated criteria; partial-run degraded mode (per-trial failures
  dropped, never fabricated); one-click example patient.
- Claim Verification (medical-affairs reviewer): example claims (one accurate, one
  subtly-overstated), verdict card + trust score + flagged claim-vs-source spans.

tsc + next build clean. Records the multi-source benchmark run (pre source-count fix)
for the honest trail.
2026-07-11 18:47:39 +03:00

183 lines
6.0 KiB
TypeScript

"use client";
import { useCallback, useEffect, useState } from "react";
import { StructuredRecord } from "./StructuredRecord";
import { recordToJson, recordToMarkdown, slugForFilename } from "./exportRecord";
import type { LabExperimentRecord } from "./types";
// Detail view of a saved experiment: the full grounded structured record plus the
// original raw notes it was derived from (collapsed by default), export/copy actions
// (make the record artifact-worthy for a real lab notebook / ELN), and a delete action.
interface ExperimentDetailProps {
record: LabExperimentRecord | null;
loading: boolean;
error: string | null;
onDelete: (id: string) => void;
deleting: boolean;
}
function formatDate(iso: string): string {
const d = new Date(iso);
return Number.isNaN(d.getTime()) ? iso : d.toLocaleDateString();
}
// Trigger a client-side file download of `content` with the given filename + MIME type.
// Object URL is revoked after the click so we don't leak blob URLs.
function downloadFile(filename: string, content: string, mime: string): void {
const blob = new Blob([content], { type: mime });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Export/copy actions for a saved record. "Copy as JSON" writes the full record to the
// clipboard (with a graceful fallback if the Clipboard API is unavailable); "Export .md"
// downloads a Markdown document a scientist can paste into a lab notebook.
function ExportActions({ record }: { record: LabExperimentRecord }) {
const [copied, setCopied] = useState(false);
const [copyError, setCopyError] = useState(false);
// Clear the "Copied" flash after a moment.
useEffect(() => {
if (!copied) return;
const t = setTimeout(() => setCopied(false), 1800);
return () => clearTimeout(t);
}, [copied]);
const onCopyJson = useCallback(async () => {
setCopyError(false);
const json = recordToJson(record);
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(json);
setCopied(true);
return;
}
throw new Error("clipboard unavailable");
} catch {
// Fallback: hand the scientist the artifact as a download so the action never
// dead-ends, even where the Clipboard API is blocked (insecure origin / permissions).
downloadFile(`${slugForFilename(record.title)}.json`, json, "application/json");
setCopyError(true);
}
}, [record]);
const onExportMd = useCallback(() => {
downloadFile(
`${slugForFilename(record.title)}.md`,
recordToMarkdown(record),
"text/markdown"
);
}, [record]);
return (
<div className="flex flex-wrap items-center gap-2">
<button
type="button"
onClick={() => void onCopyJson()}
className="rounded-md border border-ink/15 px-3 py-1.5 text-xs font-medium text-ink/70 hover:bg-ink/[0.03]"
title="Copy the full structured record (with grounded quotes) to your clipboard"
>
{copied ? "Copied" : copyError ? "Downloaded JSON" : "Copy as JSON"}
</button>
<button
type="button"
onClick={onExportMd}
className="rounded-md border border-ink/15 px-3 py-1.5 text-xs font-medium text-ink/70 hover:bg-ink/[0.03]"
title="Download a Markdown document to paste into your lab notebook"
>
Export .md
</button>
</div>
);
}
export function ExperimentDetail({
record,
loading,
error,
onDelete,
deleting,
}: ExperimentDetailProps) {
if (error) {
return (
<div className="rounded-lg border border-ink/15 bg-white p-6 text-sm text-red-700">
{error}
</div>
);
}
if (loading) {
return (
<div className="rounded-lg border border-ink/15 bg-white p-6 text-center text-sm text-ink/40">
Loading experiment…
</div>
);
}
if (!record) {
return (
<div className="rounded-lg border border-ink/15 bg-white p-6 text-center text-sm text-ink/40">
Select a saved experiment to view its structured record.
</div>
);
}
return (
<div className="rounded-lg border border-ink/15 bg-white p-4">
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<h3 className="text-base font-semibold text-ink/80">{record.title}</h3>
<p className="mt-0.5 text-xs text-ink/40">
{record.experimentDate ? `${record.experimentDate} · ` : ""}
saved {formatDate(record.createdAt)}
</p>
</div>
<div className="flex shrink-0 flex-wrap items-center justify-end gap-2">
<ExportActions record={record} />
<button
type="button"
onClick={() => onDelete(record.id)}
disabled={deleting}
className="rounded-md border border-ink/15 px-3 py-1.5 text-xs font-medium text-red-700 hover:bg-red-50 disabled:opacity-50"
>
{deleting ? "Deleting…" : "Delete"}
</button>
</div>
</div>
{record.tags.length > 0 ? (
<div className="mt-3 flex flex-wrap gap-1">
{record.tags.map((t, i) => (
<span
key={`${t}-${i}`}
className="rounded-full bg-ink/[0.05] px-2 py-0.5 text-xs text-ink/60"
>
{t}
</span>
))}
</div>
) : null}
<div className="mt-5 border-t border-ink/15 pt-4">
<StructuredRecord structured={record.structured} />
</div>
<details className="mt-5 border-t border-ink/15 pt-4">
<summary className="cursor-pointer text-xs font-semibold uppercase tracking-wide text-ink/40">
Original raw notes
</summary>
<pre className="mt-2 whitespace-pre-wrap rounded-md bg-ink/[0.03] p-3 font-mono text-xs text-ink/70">
{record.rawNotes}
</pre>
</details>
</div>
);
}