- 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.
66 lines
2.6 KiB
TypeScript
66 lines
2.6 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { ok, fail } from "@/lib/api/response";
|
|
import { checkRateLimit } from "@/lib/rateLimit";
|
|
import { logEvent } from "@/lib/logger";
|
|
import { getPool } from "@/lib/db";
|
|
import { getSourcesForEntity } from "@/lib/ingest/entitySourceQueries";
|
|
import { SourcesByEntityQuerySchema } from "@/lib/ingest/sourcesByEntity.schemas";
|
|
|
|
// Public GET endpoint: the cached sources tagged (at ingest time) with a canonical
|
|
// ontology entity. GET /api/sources/by-entity?curie=HGNC:6024&limit=&offset= joins the
|
|
// INGEST-TIME entity index (document_entities) to `sources` and returns the documents that
|
|
// mention that CURIE, ordered by mention salience. Serves ENTIRELY from the shared cache —
|
|
// no NER, no network, no LLM (CLAUDE.md cache-everything rule; the demo never depends on
|
|
// live latency). Public + IP rate-limited; never logs the source text — only ids/counts.
|
|
export const runtime = "nodejs";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const start = Date.now();
|
|
const ip = req.headers.get("x-forwarded-for") ?? "unknown";
|
|
|
|
const rate = checkRateLimit(ip);
|
|
if (!rate.allowed) {
|
|
logEvent("ingest.sources_by_entity.rate_limited", { ip });
|
|
return fail("Rate limit reached. Please try again shortly.", 429);
|
|
}
|
|
|
|
const { searchParams } = new URL(req.url);
|
|
const parsed = SourcesByEntityQuerySchema.safeParse({
|
|
curie: searchParams.get("curie") ?? undefined,
|
|
limit: searchParams.get("limit") ?? undefined,
|
|
offset: searchParams.get("offset") ?? undefined,
|
|
});
|
|
if (!parsed.success) {
|
|
const issue = parsed.error.issues[0];
|
|
const where = issue?.path.length ? `${issue.path.join(".")}: ` : "";
|
|
return fail(
|
|
`Invalid request — ${where}${issue?.message ?? "provide a curie query param."}`,
|
|
400
|
|
);
|
|
}
|
|
|
|
const { curie, limit, offset } = parsed.data;
|
|
const effectiveLimit = limit ?? 20;
|
|
const effectiveOffset = offset ?? 0;
|
|
|
|
try {
|
|
const page = await getSourcesForEntity(getPool(), curie, effectiveLimit, effectiveOffset);
|
|
logEvent("ingest.sources_by_entity.success", {
|
|
latencyMs: Date.now() - start,
|
|
returned: page.sources.length,
|
|
total: page.total,
|
|
});
|
|
return ok(page, { total: page.total, limit: effectiveLimit });
|
|
} catch (err) {
|
|
logEvent("ingest.sources_by_entity.error", {
|
|
latencyMs: Date.now() - start,
|
|
error: String(err),
|
|
});
|
|
console.error("[/api/sources/by-entity] failed:", err);
|
|
return fail(
|
|
"Something went wrong while loading sources for this entity. This has been logged — please try again shortly.",
|
|
500
|
|
);
|
|
}
|
|
}
|