caelum0x 174222bcaa feat: Builder-Track named-user tools, bio + evidence-intelligence platform, Apache-2.0
Builds PaperTrail from the base skeleton into a full evidence-verification platform
for the "Built with Claude: Life Sciences" hackathon (Builder Track).

New named-user features (Build Beyond the Bench):
- Lab Notebook Companion — rough bench notes -> structured, searchable, grounded
  experiment records (lib/labNotebook, /console/lab-notebook, migration 0059)
- Clinical Trial Matcher — de-identified patient notes -> ranked eligible
  ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning
  (lib/trialMatcher, /console/trial-matcher, migrations 0060)

Platform:
- Deterministic verification moat (biostats recompute, exact-span grounding)
- 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar,
  PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier)
- Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise
  governance (RBAC, /api/v1 gateway, metering, audit chain, retention)
- 17 OSS projects assimilated into native TypeScript on our stack (backend/engines
  retained + credited in NOTICE)
- Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site
- Next.js 16 upgrade, benchmark harness (honest smoke-test caveat)

Licensing: Apache-2.0 (LICENSE + NOTICE).
2026-07-10 16:23:13 +03:00

100 lines
3.6 KiB
TypeScript

import { NextRequest } from "next/server";
import { getPool } from "@/lib/db";
import { checkRateLimit } from "@/lib/rateLimit";
import { sanitizeClaimText } from "@/lib/api/claimInput";
import { ok, fail } from "@/lib/api/response";
import { logEvent } from "@/lib/logger";
import { generateSynthesisReport } from "@/lib/synthesisReport/generate";
export const runtime = "nodejs";
// PUBLIC compute route: topic/claim IN → a long-form, fully-cited evidence review OUT
// (STORM-style). Mirrors /api/verify + /api/evidence-pipeline hardening — nodejs runtime,
// IP rate limiting, claim sanitisation BEFORE anything downstream, and NEVER logging the
// topic text — and returns the standard { success, data, error } envelope via ok/fail.
//
// The engine supplies every number (via runEvidencePipeline); Claude drafts the prose;
// every factual sentence is grounded to a source span before it reaches the client.
const MAX_TOPIC_LENGTH = 2000;
const MAX_QUERY_LENGTH = 2000;
export async function POST(req: NextRequest) {
const start = Date.now();
const ip = req.headers.get("x-forwarded-for") ?? "unknown";
const rate = checkRateLimit(ip);
if (!rate.allowed) {
logEvent("synthesis_report.rate_limited", { ip });
return fail("Rate limit reached. Please try again shortly.", 429);
}
let body: { topic?: unknown; query?: unknown; limit?: unknown };
try {
body = await req.json();
} catch {
return fail("Request body must be valid JSON.", 400);
}
// Topic sanitisation identical in spirit to /api/verify: strips control/invisible
// chars, caps length, normalises whitespace — all BEFORE it reaches retrieval,
// embeddings, the DB, or Claude.
const sanitized = sanitizeClaimText(body.topic, {
maxLength: MAX_TOPIC_LENGTH,
tooLongError:
"Topic is too long (max 2000 characters). Enter a claim or a focused research question.",
});
if (!sanitized.ok) {
return fail(sanitized.error, 400);
}
const topic = sanitized.value;
if (topic.length < 10) {
return fail("Please provide a topic or claim of at least 10 characters.", 400);
}
// Optional search-steering query, sanitised the same way.
let query: string | undefined;
if (body.query !== undefined && body.query !== null && body.query !== "") {
const sanitizedQuery = sanitizeClaimText(body.query, {
maxLength: MAX_QUERY_LENGTH,
tooLongError: "Query is too long (max 2000 characters).",
});
if (!sanitizedQuery.ok) {
return fail(sanitizedQuery.error, 400);
}
query = sanitizedQuery.value;
}
// Optional cap on retrieved candidate sources — validated at the boundary.
let limit: number | undefined;
if (body.limit !== undefined) {
const n = Number(body.limit);
if (!Number.isInteger(n) || n < 1 || n > 20) {
return fail("`limit` must be an integer between 1 and 20.", 400);
}
limit = n;
}
try {
const pool = getPool();
const report = await generateSynthesisReport(pool, { topic, query, limit });
logEvent("synthesis_report.success", {
latencyMs: Date.now() - start,
usedSources: report.usedSources.length,
droppedSentences: report.droppedSentenceCount,
grounded: report.grounded,
certainty: report.facts.certainty ?? "n/a",
});
return ok(report);
} catch (err) {
logEvent("synthesis_report.error", { latencyMs: Date.now() - start, error: String(err) });
console.error("[/api/synthesis-report] failed:", err);
return fail(
"Something went wrong while generating the evidence review. This has been logged — please try again, or try a different topic.",
500
);
}
}