Live dogfooding showed Trial Matcher extracted the patient profile perfectly but returned 0 matches in 'degraded mode'. Three real bugs, each fixed: 1. ClinicalTrials.gov search 400'd: buildQuery joins all profile search terms into one query.term, and Essie ANDs them — ~11+ terms 400s and even fewer over-constrains to zero matches. searchTrialsForMatching now PROGRESSIVELY BROADENS (10→6→4→3→2 leading terms), returning the first attempt that matches; throws only if every attempt errors, else []. 2. Eligibility assessment failed on trials with many criteria: the per-criterion JSON array overran maxTokens (3072) and failed to parse. Bounded to 15 criteria/type + raised to 4096. 3. Degraded banner over-fired: a single per-trial JSON hiccup (already dropped) flipped the whole run to 'eligibility unavailable' even with good matches. Now a plain error only degrades when it leaves ZERO matches; quota always surfaces. Verified locally: 5 real HER2-breast-cancer trials matched, scored, and grounded, degraded=null. tsc + build green; 2 failing tests are pre-existing NER tests unrelated to these files.
355 lines
13 KiB
TypeScript
355 lines
13 KiB
TypeScript
// ClinicalTrials.gov API v2 client. No key required.
|
|
// Docs: https://clinicaltrials.gov/data-api/api
|
|
|
|
const CTGOV_BASE = "https://clinicaltrials.gov/api/v2/studies";
|
|
|
|
// Raw per-arm outcome numbers from the registry (event count + denominator), which
|
|
// let us recompute absolute risk deterministically rather than trust a stated %.
|
|
export interface TrialGroupResult {
|
|
groupTitle: string;
|
|
eventCount: number | null;
|
|
denominator: number | null;
|
|
riskPercent: number | null; // eventCount / denominator * 100
|
|
}
|
|
|
|
// A single registered statistical analysis from a trial's results section — the
|
|
// machine-readable, sponsor-reported effect estimate. This is ground truth we can
|
|
// verify a claim against deterministically, no LLM in the numeric loop.
|
|
export interface TrialResultAnalysis {
|
|
outcomeTitle: string;
|
|
outcomeType: string | null; // "PRIMARY" | "SECONDARY" | "OTHER_PRE_SPECIFIED" ...
|
|
paramType: string | null; // e.g. "Hazard Ratio (HR)", "Odds Ratio (OR)", "Risk Ratio (RR)"
|
|
paramValue: number | null;
|
|
ciPct: number | null; // e.g. 95
|
|
ciLower: number | null;
|
|
ciUpper: number | null;
|
|
pValue: string | null; // kept as string: registries store "<0.001", "0.03", etc.
|
|
method: string | null;
|
|
// Raw-count-derived stats for this analysis's outcome (when arm-level counts exist).
|
|
groupResults?: TrialGroupResult[];
|
|
absoluteRiskReduction?: number | null; // percentage points, from raw counts
|
|
numberNeededToTreat?: number | null;
|
|
computedRRR?: number | null; // relative risk reduction (%) recomputed from raw counts
|
|
}
|
|
|
|
function toNum(v: unknown): number | null {
|
|
if (v === null || v === undefined || v === "") return null;
|
|
const n = Number(v);
|
|
return Number.isFinite(n) ? n : null;
|
|
}
|
|
|
|
// Pull the arm-level event counts + denominators for a binary outcome measure and
|
|
// derive ARR / NNT / recomputed RRR. Handles the common 2-arm binary-count layout;
|
|
// returns empty/nulls when the structure isn't a simple count (e.g. time-to-event only).
|
|
function extractRawStats(outcome: any): {
|
|
groupResults: TrialGroupResult[];
|
|
arr: number | null;
|
|
nnt: number | null;
|
|
computedRRR: number | null;
|
|
} {
|
|
const groups: Array<{ id: string; title: string }> = (outcome?.groups ?? []).map((g: any) => ({
|
|
id: g?.id ?? "",
|
|
title: g?.title ?? "",
|
|
}));
|
|
|
|
const denomCounts: Record<string, number | null> = {};
|
|
for (const d of outcome?.denoms ?? []) {
|
|
for (const c of d?.counts ?? []) denomCounts[c?.groupId] = toNum(c?.value);
|
|
}
|
|
|
|
// First class -> first category -> measurements per group (the binary event counts).
|
|
const firstCategory = outcome?.classes?.[0]?.categories?.[0];
|
|
const eventCounts: Record<string, number | null> = {};
|
|
for (const m of firstCategory?.measurements ?? []) eventCounts[m?.groupId] = toNum(m?.value);
|
|
|
|
const groupResults: TrialGroupResult[] = groups.map((g) => {
|
|
const eventCount = eventCounts[g.id] ?? null;
|
|
const denominator = denomCounts[g.id] ?? null;
|
|
const riskPercent =
|
|
eventCount !== null && denominator && denominator > 0 ? (eventCount / denominator) * 100 : null;
|
|
return { groupTitle: g.title, eventCount, denominator, riskPercent };
|
|
});
|
|
|
|
const risks = groupResults.map((g) => g.riskPercent).filter((r): r is number => r !== null);
|
|
if (risks.length !== 2) return { groupResults, arr: null, nnt: null, computedRRR: null };
|
|
|
|
const high = Math.max(risks[0], risks[1]);
|
|
const low = Math.min(risks[0], risks[1]);
|
|
const arr = high - low; // percentage points
|
|
const computedRRR = high > 0 ? (arr / high) * 100 : null;
|
|
const nnt = arr > 0 ? 100 / arr : null; // risks are in %, so 100/arr participants
|
|
return { groupResults, arr, nnt, computedRRR };
|
|
}
|
|
|
|
/**
|
|
* Fetch a trial's REGISTERED statistical results (primary/secondary outcome analyses)
|
|
* from the structured resultsSection — not the free-text description. Returns [] when
|
|
* the study has no posted results. Ordered primary-first.
|
|
*/
|
|
export async function fetchTrialResults(nctId: string): Promise<TrialResultAnalysis[]> {
|
|
const res = await fetch(`${CTGOV_BASE}/${encodeURIComponent(nctId)}?format=json`);
|
|
if (!res.ok) return [];
|
|
const data = await res.json();
|
|
|
|
const outcomes =
|
|
data?.resultsSection?.outcomeMeasuresModule?.outcomeMeasures ?? [];
|
|
const analyses: TrialResultAnalysis[] = [];
|
|
|
|
for (const outcome of outcomes) {
|
|
const outcomeTitle: string = outcome?.title ?? "";
|
|
const outcomeType: string | null = outcome?.type ?? null;
|
|
const raw = extractRawStats(outcome);
|
|
for (const a of outcome?.analyses ?? []) {
|
|
analyses.push({
|
|
outcomeTitle,
|
|
outcomeType,
|
|
paramType: a?.paramType ?? null,
|
|
paramValue: toNum(a?.paramValue),
|
|
ciPct: toNum(a?.ciPctValue),
|
|
ciLower: toNum(a?.ciLowerLimit),
|
|
ciUpper: toNum(a?.ciUpperLimit),
|
|
pValue: a?.pValue != null ? String(a.pValue) : null,
|
|
method: a?.statisticalMethod ?? null,
|
|
groupResults: raw.groupResults.length > 0 ? raw.groupResults : undefined,
|
|
absoluteRiskReduction: raw.arr,
|
|
numberNeededToTreat: raw.nnt,
|
|
computedRRR: raw.computedRRR,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Primary outcomes first — that's what a claim usually references.
|
|
return analyses.sort((x, y) => {
|
|
const px = x.outcomeType === "PRIMARY" ? 0 : 1;
|
|
const py = y.outcomeType === "PRIMARY" ? 0 : 1;
|
|
return px - py;
|
|
});
|
|
}
|
|
|
|
export interface TrialRecord {
|
|
nctId: string;
|
|
title: string;
|
|
summaryText: string;
|
|
url: string;
|
|
// Structured trial context straight from the API's designModule (not LLM-inferred).
|
|
phase: string | null;
|
|
enrollmentCount: number | null;
|
|
}
|
|
|
|
/** Search ClinicalTrials.gov for studies matching a free-text query. */
|
|
export async function searchTrials(query: string, pageSize = 5): Promise<TrialRecord[]> {
|
|
const params = new URLSearchParams({
|
|
"query.term": query,
|
|
pageSize: String(pageSize),
|
|
format: "json",
|
|
fields: [
|
|
"NCTId",
|
|
"BriefTitle",
|
|
"BriefSummary",
|
|
"DetailedDescription",
|
|
"PrimaryOutcomeMeasure",
|
|
"EligibilityCriteria",
|
|
"ResultsFirstPostDate",
|
|
"Phase",
|
|
"EnrollmentCount",
|
|
].join(","),
|
|
});
|
|
|
|
const res = await fetch(`${CTGOV_BASE}?${params}`);
|
|
if (!res.ok) throw new Error(`ClinicalTrials.gov search failed: ${res.status}`);
|
|
const data = await res.json();
|
|
|
|
const studies = data?.studies ?? [];
|
|
return studies.map((s: any): TrialRecord => {
|
|
const proto = s?.protocolSection ?? {};
|
|
const idModule = proto.identificationModule ?? {};
|
|
const descModule = proto.descriptionModule ?? {};
|
|
const outcomesModule = proto.outcomesModule ?? {};
|
|
const eligModule = proto.eligibilityModule ?? {};
|
|
const designModule = proto.designModule ?? {};
|
|
|
|
const nctId = idModule.nctId ?? "unknown";
|
|
const title = idModule.briefTitle ?? "";
|
|
const summary = descModule.briefSummary ?? "";
|
|
const detail = descModule.detailedDescription ?? "";
|
|
const primaryOutcomes = (outcomesModule.primaryOutcomes ?? [])
|
|
.map((o: any) => o?.measure)
|
|
.filter(Boolean)
|
|
.join("; ");
|
|
const eligibility = eligModule.eligibilityCriteria ?? "";
|
|
|
|
// Structured design fields — phases is an array like ["PHASE3"]; enrollmentInfo.count is a number.
|
|
const phases: string[] = Array.isArray(designModule.phases) ? designModule.phases : [];
|
|
const phase = phases.length > 0 ? phases.join("/") : null;
|
|
const rawCount = designModule.enrollmentInfo?.count;
|
|
const enrollmentCount = typeof rawCount === "number" ? rawCount : null;
|
|
|
|
const summaryText = [
|
|
title,
|
|
summary,
|
|
detail,
|
|
primaryOutcomes ? `Primary outcome: ${primaryOutcomes}` : "",
|
|
eligibility ? `Eligibility: ${eligibility}` : "",
|
|
]
|
|
.filter(Boolean)
|
|
.join("\n\n");
|
|
|
|
return {
|
|
nctId,
|
|
title,
|
|
summaryText,
|
|
url: `https://clinicaltrials.gov/study/${nctId}`,
|
|
phase,
|
|
enrollmentCount,
|
|
};
|
|
});
|
|
}
|
|
|
|
// A trial candidate for the CLINICAL TRIAL MATCHER: carries the raw eligibility
|
|
// criteria text (so each criterion can be grounded verbatim) plus the structured
|
|
// eligibility gates the registry exposes directly (sex, age bounds, healthy-volunteer
|
|
// flag) and enough context (conditions, brief summary, status) to reason about fit.
|
|
export interface TrialCandidate {
|
|
nctId: string;
|
|
title: string;
|
|
url: string;
|
|
phase: string | null;
|
|
enrollmentCount: number | null;
|
|
overallStatus: string | null;
|
|
conditions: string[];
|
|
briefSummary: string;
|
|
/** Raw eligibility criteria text — the source of truth every criterion is grounded to. */
|
|
eligibilityCriteria: string;
|
|
sex: string | null; // "ALL" | "FEMALE" | "MALE"
|
|
minAge: string | null; // e.g. "18 Years"
|
|
maxAge: string | null; // e.g. "75 Years"
|
|
healthyVolunteers: boolean | null;
|
|
}
|
|
|
|
/**
|
|
* Search ClinicalTrials.gov for candidate trials to match a patient against, requesting
|
|
* the eligibility + status + conditions fields the matcher needs. Reuses the same v2 API
|
|
* base and protocolSection parsing style as searchTrials, but returns the eligibility-rich
|
|
* TrialCandidate shape (raw criteria text preserved for verbatim grounding) rather than the
|
|
* pooled-summary TrialRecord shape used by the verification pipeline.
|
|
*/
|
|
// ClinicalTrials.gov's Essie `query.term` ANDs its space-separated terms, so a long, specific
|
|
// term list both (a) 400s past ~10 terms and (b) over-constrains to zero matches well before
|
|
// that. A rich patient profile produces exactly such a term soup, which silently zeroed out the
|
|
// whole trial search. So we PROGRESSIVELY BROADEN: try the fullest safe query, then fewer and
|
|
// fewer leading terms, and return the first attempt that actually matches trials. We only throw
|
|
// if EVERY attempt errored (an honest degrade); if searches succeeded but nothing matched, we
|
|
// return [] (an honest no-match), never fabricating a result.
|
|
// Max terms before Essie 400s is ~10; the ladder below that broadens toward a match.
|
|
const MAX_SAFE_TERMS = 10;
|
|
const QUERY_TERM_LADDER = [6, 4, 3, 2] as const;
|
|
|
|
export async function searchTrialsForMatching(
|
|
query: string,
|
|
pageSize = 6
|
|
): Promise<TrialCandidate[]> {
|
|
const words = query.trim().split(/\s+/).filter(Boolean);
|
|
if (words.length === 0) return [];
|
|
|
|
// Distinct term counts to try, most-specific-but-safe first, each no larger than the query.
|
|
// Start at min(words, 10) — the fullest query that won't 400 — then broaden down the ladder.
|
|
const caps = Array.from(
|
|
new Set(
|
|
[Math.min(words.length, MAX_SAFE_TERMS), ...QUERY_TERM_LADDER].filter(
|
|
(n) => n >= 1 && n <= words.length
|
|
)
|
|
)
|
|
).sort((a, b) => b - a);
|
|
|
|
let lastError: unknown = null;
|
|
let anySucceeded = false;
|
|
for (const n of caps) {
|
|
const term = words.slice(0, n).join(" ");
|
|
try {
|
|
const results = await fetchMatchingTrials(term, pageSize);
|
|
anySucceeded = true;
|
|
if (results.length > 0) return results;
|
|
} catch (err) {
|
|
lastError = err;
|
|
}
|
|
}
|
|
|
|
// Every attempt threw (e.g. CT.gov down): surface it so the run degrades honestly.
|
|
if (!anySucceeded && lastError) throw lastError;
|
|
// Searches ran but nothing matched across all broadenings: honest empty result.
|
|
return [];
|
|
}
|
|
|
|
async function fetchMatchingTrials(
|
|
term: string,
|
|
pageSize: number
|
|
): Promise<TrialCandidate[]> {
|
|
const params = new URLSearchParams({
|
|
"query.term": term,
|
|
pageSize: String(pageSize),
|
|
format: "json",
|
|
fields: [
|
|
"NCTId",
|
|
"BriefTitle",
|
|
"BriefSummary",
|
|
"Condition",
|
|
"EligibilityCriteria",
|
|
"Sex",
|
|
"MinimumAge",
|
|
"MaximumAge",
|
|
"HealthyVolunteers",
|
|
"OverallStatus",
|
|
"Phase",
|
|
"EnrollmentCount",
|
|
].join(","),
|
|
});
|
|
|
|
const res = await fetch(`${CTGOV_BASE}?${params}`);
|
|
if (!res.ok) throw new Error(`ClinicalTrials.gov search failed: ${res.status}`);
|
|
const data = await res.json();
|
|
|
|
const studies = data?.studies ?? [];
|
|
return studies.map((s: any): TrialCandidate => {
|
|
const proto = s?.protocolSection ?? {};
|
|
const idModule = proto.identificationModule ?? {};
|
|
const descModule = proto.descriptionModule ?? {};
|
|
const eligModule = proto.eligibilityModule ?? {};
|
|
const designModule = proto.designModule ?? {};
|
|
const statusModule = proto.statusModule ?? {};
|
|
const condModule = proto.conditionsModule ?? {};
|
|
|
|
const nctId = idModule.nctId ?? "unknown";
|
|
const title = idModule.briefTitle ?? "";
|
|
const briefSummary = descModule.briefSummary ?? "";
|
|
const eligibilityCriteria = eligModule.eligibilityCriteria ?? "";
|
|
|
|
const conditions: string[] = Array.isArray(condModule.conditions)
|
|
? condModule.conditions.filter((c: unknown): c is string => typeof c === "string")
|
|
: [];
|
|
|
|
const phases: string[] = Array.isArray(designModule.phases) ? designModule.phases : [];
|
|
const phase = phases.length > 0 ? phases.join("/") : null;
|
|
const rawCount = designModule.enrollmentInfo?.count;
|
|
const enrollmentCount = typeof rawCount === "number" ? rawCount : null;
|
|
|
|
const hv = eligModule.healthyVolunteers;
|
|
const healthyVolunteers = typeof hv === "boolean" ? hv : null;
|
|
|
|
return {
|
|
nctId,
|
|
title,
|
|
url: `https://clinicaltrials.gov/study/${nctId}`,
|
|
phase,
|
|
enrollmentCount,
|
|
overallStatus: statusModule.overallStatus ?? null,
|
|
conditions,
|
|
briefSummary,
|
|
eligibilityCriteria,
|
|
sex: eligModule.sex ?? null,
|
|
minAge: eligModule.minimumAge ?? null,
|
|
maxAge: eligModule.maximumAge ?? null,
|
|
healthyVolunteers,
|
|
};
|
|
});
|
|
}
|