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

77 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 { sanitizeClaimText } from "@/lib/api/claimInput";
import { askPaperQa } from "@/lib/paperqa/ask";
// Public agentic Paper QA endpoint (PaperQA2-style). Given a scientific
// question, Claude retrieves the relevant cached papers, READS their full text,
// and answers WITH CITATIONS — every rendered claim grounded to an exact source
// span by lib/grounding.ts. Mirrors app/api/verify/route.ts: nodejs runtime,
// rate-limited, envelope responses, and NEVER logs the question 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("paper_qa.rate_limited", { ip });
return fail("Rate limit reached. Please try again shortly.", 429);
}
let body: { question?: string; limit?: number };
try {
body = await req.json();
} catch {
return fail("Request body must be valid JSON.", 400);
}
// Reuse the shared question/claim input hardening (control chars, invisible /
// bidi smuggling, degenerate repetition, max length) before any LLM sees it.
const sanitized = sanitizeClaimText(body.question, {
maxLength: 2000,
tooLongError:
"Question is too long (max 2000 characters). Ask a single focused question.",
});
if (!sanitized.ok) {
return fail(sanitized.error, 400);
}
const question = sanitized.value;
if (question.length < 10) {
return fail("Please ask a question of at least 10 characters.", 400);
}
const rawLimit = body.limit;
const limit =
typeof rawLimit === "number" && Number.isFinite(rawLimit)
? Math.min(8, Math.max(1, Math.floor(rawLimit)))
: undefined;
try {
const outcome = await askPaperQa(question, limit ? { limit } : undefined);
if (outcome.status === "no_support_found") {
logEvent("paper_qa.no_support", { latencyMs: Date.now() - start });
return ok(outcome);
}
logEvent("paper_qa.success", {
latencyMs: Date.now() - start,
sources: outcome.sources.length,
claims: outcome.claims.length,
droppedClaims: outcome.dropped_claims,
});
return ok(outcome);
} catch (err) {
logEvent("paper_qa.error", { latencyMs: Date.now() - start, error: String(err) });
console.error("[/api/paper-qa] failed:", err);
return fail(
"Something went wrong while answering this question. This has been logged — please try again, or rephrase.",
500
);
}
}