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

88 lines
3.1 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 { sanitizeClaimText } from "@/lib/api/claimInput";
import { classifyCitation } from "@/lib/citations/classify";
// Public smart-citation classifier (Scite-style). Given a CITING passage and the
// CITED work's claim, Claude classifies the citation STANCE (supporting /
// contrasting / mentioning) and extracts the exact citation-context sentence; the
// trust layer grounds that sentence to the citing text before we return it.
//
// Mirrors app/api/verify/route.ts: nodejs runtime, rate-limited, envelope
// responses, input hardened before any LLM sees it, and NEVER logs the passage or
// claim text.
export const runtime = "nodejs";
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("citations.classify.rate_limited", { ip });
return fail("Rate limit reached. Please try again shortly.", 429);
}
let body: { citing_text?: string; cited_claim?: string };
try {
body = await req.json();
} catch {
return fail("Request body must be valid JSON.", 400);
}
// Reuse the shared free-text hardening (control chars, invisible/bidi smuggling,
// degenerate repetition, max length) for BOTH inputs before any LLM sees them.
const citing = sanitizeClaimText(body.citing_text, {
maxLength: 6000,
tooLongError:
"Citing passage is too long (max 6000 characters). Paste the paragraph that contains the citation.",
});
if (!citing.ok) {
return fail(`Citing passage: ${citing.error}`, 400);
}
if (citing.value.length < 20) {
return fail("Please paste a citing passage of at least 20 characters.", 400);
}
const cited = sanitizeClaimText(body.cited_claim, {
maxLength: 2000,
tooLongError:
"Cited claim is too long (max 2000 characters). Summarize the cited work's finding in a sentence.",
});
if (!cited.ok) {
return fail(`Cited claim: ${cited.error}`, 400);
}
if (cited.value.length < 10) {
return fail("Please provide a cited claim of at least 10 characters.", 400);
}
try {
const outcome = await classifyCitation({
citing_text: citing.value,
cited_claim: cited.value,
});
if (outcome.status === "ungroundable") {
logEvent("citations.classify.ungroundable", { latencyMs: Date.now() - start });
return ok(outcome);
}
logEvent("citations.classify.success", {
latencyMs: Date.now() - start,
stance: outcome.classification.stance,
groundingStatus: outcome.classification.grounding.status,
confidence: outcome.classification.confidence,
});
return ok(outcome);
} catch (err) {
logEvent("citations.classify.error", { latencyMs: Date.now() - start, error: String(err) });
console.error("[/api/citations/classify] failed:", err);
return fail(
"Something went wrong while classifying this citation. This has been logged — please try again, or rephrase.",
500
);
}
}