caelum0x 1d4f80bb4c feat: expand backend/ — specialize 6 more OSS engines in place (MiniCheck, Loki, paper-qa, ASReview, BioCypher, STORM)
Each engine now carries a PaperTrail-native papertrail_*.py + PAPERTRAIL.md, mirrored by a native TS
module + public API (deterministic scoring; Claude only for grounded language steps):
- MiniCheck: negation-aware entailment + negative_supported verdict for absence claims
  (backend/engines/MiniCheck/papertrail_negation.py -> lib/grounding/negationEntailment.ts, /api/verify/absence-claim)
- Loki/OpenFactVerification: claim-frame on-topic reranker (cuts retrieval noise)
  (papertrail_rerank.py -> lib/agents/contextualRank.ts, /api/retrieval/rerank)
- paper-qa: source-quality tiers A/B/C/D (retracted -> untrusted, weight 0)
  (papertrail_source_quality.py -> lib/paperqa/sourceQuality.ts, /api/sources/quality-tier)
- ASReview: ensemble screening (inclusion + quality + risk-of-bias in one pass)
  (papertrail_ensemble.py -> lib/screening/ensemble.ts, /api/screening/ensemble)
- BioCypher: bring-your-own-KG CSV import with Biolink domain/range validation (0071 kg_import_batches)
  (papertrail_byokg.py -> lib/kg/byoKg.ts, /api/kg/import)
- STORM: structured Claim/Best-Case/Critique/Synthesis debate for mixed verdicts
  (papertrail_debate.py -> lib/synthesis/debate.ts, /api/synthesis/debate)

15 OSS engines now specialized in place. tsc + build green; 0071 applied to Neon.
2026-07-11 03:41:53 +03:00

101 lines
3.4 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 { withOrg, type Ctx } from "@/lib/api/handler";
import { requireRole } from "@/lib/authz/rbac";
import { getPool } from "@/lib/db";
import { type KgPool } from "@/lib/kg/repository";
import { validateAndImportKg, KgImportRequestSchema } from "@/lib/kg/byoKg";
// Public POST endpoint for BRING-YOUR-OWN-KG IMPORT.
//
// An org member (editor+) uploads their own knowledge graph — a list of nodes and a list
// of typed edges — and we import it into the shared evidence graph (kg_nodes / kg_edges,
// migration 0052). Every edge's predicate is validated against the Biolink slot
// domain/range via lib/kg/biolink.ts (the TypeScript port of the vendored BioCypher
// engine — see backend/engines/biocypher/PAPERTRAIL.md): an ill-typed edge (e.g. a
// `treats` whose subject is a disease) is REJECTED with a reason, never silently coerced
// into the graph. The run is recorded as a kg_import_batches audit row.
//
// There is NO LLM anywhere in the validation: the accept/reject decision is deterministic
// Biolink typing. We prefer an honest rejection over a forced, poisoned edge. No node/edge
// text is logged — only the org id and counts.
export const runtime = "nodejs";
function requirePool(): KgPool | null {
try {
return getPool() as unknown as KgPool;
} catch {
return null;
}
}
export const POST = withOrg(async (req: NextRequest, ctx: Ctx) => {
const start = Date.now();
const ip = req.headers.get("x-forwarded-for") ?? "unknown";
const rate = checkRateLimit(ip);
if (!rate.allowed) {
logEvent("kg.import.rate_limited", { orgId: ctx.org.id });
return fail("Rate limit reached. Please try again shortly.", 429);
}
// Importing writes to the shared graph — require an editor (throws a 403-mapped error
// that withOrg maps to a fail() response otherwise).
requireRole(ctx, "editor");
let raw: unknown;
try {
raw = await req.json();
} catch {
return fail("Request body must be valid JSON.", 400);
}
const parsed = KgImportRequestSchema.safeParse(raw);
if (!parsed.success) {
const issue = parsed.error.issues[0];
const where = issue?.path.length ? `${issue.path.join(".")}: ` : "";
return fail(
`Invalid KG import request — ${where}${issue?.message ?? "provide nodes[] and edges[]."}`,
400
);
}
const pool = requirePool();
if (!pool) {
logEvent("kg.import.db_unconfigured", { orgId: ctx.org.id });
return fail("The knowledge graph is temporarily unavailable.", 503);
}
try {
const result = await validateAndImportKg(
pool,
ctx.org.id,
parsed.data,
ctx.user.id
);
logEvent("kg.import.success", {
orgId: ctx.org.id,
latencyMs: Date.now() - start,
importedNodes: result.imported.nodes,
importedEdges: result.imported.edges,
rejectedCount: result.rejected.length,
});
return ok(result);
} catch (err) {
logEvent("kg.import.error", {
orgId: ctx.org.id,
latencyMs: Date.now() - start,
error: String(err),
});
console.error("[/api/kg/import] failed:", err);
return fail(
"Something went wrong while importing the knowledge graph. This has been logged — please check your inputs and try again.",
500
);
}
});