feat(labs): PDF upload, text extraction, LLM draft pipeline

This commit is contained in:
marcuspaico
2026-08-17 15:41:02 -07:00
parent 89394732fd
commit 8e9f3d5894
12 changed files with 237 additions and 5 deletions

15
server/src/lib/extract.ts Normal file
View File

@@ -0,0 +1,15 @@
import { ExtractedDraft } from "@helios/shared";
import { chatJSON, type LlmDeps } from "./llm";
const SYSTEM = `You extract structured blood-test results from the raw text of a lab report.
Return ONLY a JSON object: {"collectedDate": "YYYY-MM-DD" or null, "labName": string or null, "markers": [{"panel": string or null, "name": string, "value": string, "unit": string or null, "referenceRange": string or null, "flagged": boolean}]}.
Rules: copy names, values, units, and reference ranges EXACTLY as printed — do not convert units or round values. "value" is always a string (keep comparators like "<0.3"). Set "flagged" true only when the report marks the result abnormal (H, L, *, bold, out-of-range annotation). Use the specimen collection date, not the report date. Skip commentary, footers, and reference-only rows with no result.`;
const MAX_CHARS = 40_000;
export async function extractFromText(deps: LlmDeps, text: string): Promise<ExtractedDraft> {
const out = await chatJSON(deps, { system: SYSTEM, user: text.slice(0, MAX_CHARS) });
const parsed = ExtractedDraft.safeParse(out);
if (!parsed.success) throw new Error("llm_error: draft failed schema validation");
return parsed.data;
}

7
server/src/lib/pdf.ts Normal file
View File

@@ -0,0 +1,7 @@
import { extractText, getDocumentProxy } from "unpdf";
export async function pdfToText(data: Uint8Array): Promise<string> {
const doc = await getDocumentProxy(data);
const { text } = await extractText(doc, { mergePages: true });
return text;
}