caelum0x 4bb067d5d5 feat: Phase 2 multi-source ingest — evidence integrator (FAERS/ClinVar/ChEMBL/OpenTargets/PubTator)
- 0063 migrations: sources provenance columns (variant_id/compound_id/adverse_event_cui/
  source_version/source_snapshot_id/snapshot_date) + document_entities + source_access_log;
  dropped the sources.source_type CHECK so new providers can cache
- lib/ingest/multiSourcePipeline.ts + drivers/{faers,clinvar,chembl,openTargets,pubtator}.ts
  (cache-once, wraps existing bio engines) + provenance.ts (recordAccess, deterministic snapshot id)
- lib/ingest/entityCanonicalization.ts: ingest-time NER -> deterministic canonicalize -> document_entities
- backend/engines/{faers,clinvar,chembl}/run.py: PaperTrail-native ingest bridges (in-place)
- /api/ingest/multi-source, /api/sources/by-entity, /api/entities/[curie]/sources,
  /api/sources/quality-report + /console/sources/ingest page

tsc + build green; 0063 migrations applied to Neon.
2026-07-10 23:53:39 +03:00

90 lines
3.5 KiB
TypeScript

"use client";
import { useCallback, useState } from "react";
import type { ApiResponse } from "@/lib/api/response";
import { ModuleHeader } from "../../claims/_components/ModuleHeader";
import { ErrorBanner, LoadingBanner } from "@/components/console/StateBanners";
import { IngestControl, type IngestFormValue } from "./_components/IngestControl";
import { CoverageResult } from "./_components/CoverageResult";
import { QualityReportPanel } from "./_components/QualityReportPanel";
import type { MultiSourceIngestResult } from "./_components/types";
// Multi-source ingest console (Phase 2 — evidence integration). Run a multi-database ingest
// for a query and/or a canonical entity, then see the coverage result + per-document
// linked-entity counts, alongside a read-only quality report over the shared cache. Both
// routes are PUBLIC compute routes — the fetches carry no auth/org header.
// Build the request body the /api/ingest/multi-source route expects, omitting empty
// optional fields so the route's Zod refinements accept it.
function toRequestBody(value: IngestFormValue): Record<string, unknown> {
const body: Record<string, unknown> = { limit: value.limit };
if (value.query.length > 0) {
body.query = value.query;
}
if (value.entitySurface.length > 0) {
const entity: Record<string, unknown> = { surface: value.entitySurface };
if (value.entityType.length > 0) {
entity.type = value.entityType;
}
body.entity = entity;
}
if (value.sources.length > 0) {
body.sources = value.sources;
}
return body;
}
export default function SourcesIngestPage() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [result, setResult] = useState<MultiSourceIngestResult | null>(null);
// Bumped after each successful ingest so the quality panel re-pulls the cache totals.
const [reportKey, setReportKey] = useState(0);
const run = useCallback(async (value: IngestFormValue) => {
setLoading(true);
setError(null);
setResult(null);
try {
const res = await fetch("/api/ingest/multi-source", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(toRequestBody(value)),
});
const body = (await res.json().catch(() => null)) as
| ApiResponse<MultiSourceIngestResult>
| null;
if (!body) throw new Error("Unexpected server response.");
if (!res.ok || !body.success || !body.data) {
throw new Error(body.error ?? "The multi-source ingest failed.");
}
setResult(body.data);
setReportKey((k) => k + 1);
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to run the multi-source ingest.");
setResult(null);
} finally {
setLoading(false);
}
}, []);
return (
<div className="space-y-6">
<ModuleHeader
title="Multi-source ingest"
subtitle="Pull PubMed, ClinicalTrials.gov, FAERS, ClinVar, ChEMBL, Open Targets, and PubTator into the shared cache with ingest-time entity canonicalization. Cache-first — already-cached documents are never re-fetched."
/>
<IngestControl loading={loading} onRun={(v) => void run(v)} />
{loading ? (
<LoadingBanner message="Fanning out across the enabled databases and linking entities at ingest…" />
) : null}
{error ? <ErrorBanner message={error} /> : null}
{result ? <CoverageResult result={result} /> : null}
<QualityReportPanel refreshKey={reportKey} />
</div>
);
}