papertrail/lib/metering/engineUsage.schemas.ts
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

49 lines
1.8 KiB
TypeScript

import { z } from "zod";
// Zod schemas for per-engine usage metering. Every input crossing into SQL is
// validated first (fail fast, never trust external data), and types are inferred
// from the schemas so callers and the repository share one source of truth.
// Guardrails on a single metered call. units/tokens are non-negative and capped
// so a mis-wired caller cannot record an absurd or negative charge.
export const ENGINE_MAX_LEN = 128;
export const UNITS_MAX = 1_000_000;
export const CLAUDE_TOKENS_MAX = 100_000_000;
// A single metering event: which engine ran, for how many units, at what token
// cost. units defaults to 1 (one call) and claude_tokens to 0 (non-LLM engines).
export const recordEngineUsageSchema = z.object({
orgId: z.string().uuid(),
engine: z.string().trim().min(1).max(ENGINE_MAX_LEN),
units: z.number().int().min(0).max(UNITS_MAX).default(1),
claudeTokens: z.number().int().min(0).max(CLAUDE_TOKENS_MAX).default(0),
});
export type RecordEngineUsageInput = z.infer<typeof recordEngineUsageSchema>;
// Filters for a usage roll-up. `since` narrows to events at/after an instant;
// omitted means all-time for the org.
export const summarizeUsageSchema = z.object({
orgId: z.string().uuid(),
since: z.coerce.date().optional(),
});
export type SummarizeUsageInput = z.infer<typeof summarizeUsageSchema>;
// Per-engine roll-up row: how many events, total units, and total tokens the
// org spent on one engine.
export interface EngineUsageRow {
engine: string;
calls: number;
units: number;
claudeTokens: number;
}
// The full summary: per-engine breakdown plus org-wide totals across all engines.
export interface UsageSummary {
engines: EngineUsageRow[];
totals: {
calls: number;
units: number;
claudeTokens: number;
};
}