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.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { checkRateLimit } from "@/lib/rateLimit";
import { splitIntoClaims } from "@/lib/claimSplitter";
import { logEvent } from "@/lib/logger";
import { runBatch } from "@/lib/agents/batchAgent";
import { BatchResultItem } from "@/components/BatchResults";
import { sanitizeClaimText } from "@/lib/api/claimInput";
export const runtime = "nodejs";
// HARD CAP: never process more than this many claims in a single batch, regardless
// of how many the splitter detects. Batch runs SEQUENTIALLY (see runBatch), so this
// directly bounds the worst-case token spend of one request to 5 full agent chains.
const MAX_BATCH = 5;
export interface BatchResponse {
batch_id: string;
results: BatchResultItem[];
truncated: boolean;
total_detected: number;
}
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("batch.rate_limited", { ip });
return NextResponse.json(
{ error: "Rate limit reached. Please try again shortly." },
{ status: 429 }
);
}
let body: { text?: string; claims?: string[] };
try {
body = await req.json();
} catch {
return NextResponse.json({ error: "Request body must be valid JSON." }, { status: 400 });
}
// Build the candidate claim list: explicit claims[] wins; otherwise split the passage.
const candidateClaims = Array.isArray(body.claims)
? body.claims.map((c) => (typeof c === "string" ? c.trim() : "")).filter((c) => c.length > 0)
: splitIntoClaims(body.text ?? "");
// Character-quality hardening: drop any candidate with control chars / invisible
// smuggling / degenerate repetition before it reaches the agent chain. Returns the
// cleaned string for the survivors. Invalid candidates are silently skipped (same
// spirit as filtering empties above) rather than failing the whole batch.
const rawClaims = candidateClaims
.map((c) => sanitizeClaimText(c))
.filter((r): r is { ok: true; value: string } => r.ok)
.map((r) => r.value);
if (rawClaims.length === 0) {
return NextResponse.json(
{ error: "No claims detected. Paste a passage or provide a non-empty claims list." },
{ status: 400 }
);
}
const totalDetected = rawClaims.length;
const truncated = totalDetected > MAX_BATCH;
// Enforce the hard cap: only the first MAX_BATCH claims are ever processed.
const claims = rawClaims.slice(0, MAX_BATCH);
try {
// Delegate the sequential agent-chain loop + persistence to the batch agent.
const { batch_id, results } = await runBatch(claims);
logEvent("batch.success", {
latencyMs: Date.now() - start,
batch_id,
totalDetected,
processed: results.length,
truncated,
verified: results.filter((r) => r.status === "verified").length,
noSupport: results.filter((r) => r.status === "no_support_found").length,
errors: results.filter((r) => r.status === "error").length,
});
const response: BatchResponse = {
batch_id,
results,
truncated,
total_detected: totalDetected,
};
return NextResponse.json(response);
} catch (err) {
logEvent("batch.error", { error: String(err), latencyMs: Date.now() - start });
console.error("[/api/verify/batch] batch run failed:", err);
return NextResponse.json(
{ error: "Something went wrong running the batch. Please try again." },
{ status: 500 }
);
}
}