mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
Merge branch 'fix/bound-sync-lab-reads' into fix/review-meds-docs
This commit is contained in:
commit
84064ea045
13 files changed
+170
-65
No files matched your search
@@ -63,6 +63,7 @@ import { resolveIndexProvider } from "@/lib/documents/index-document";
|
||||
import { loadDocumentChatText } from "@/lib/documents/content-index";
|
||||
import { runInboundExtraction } from "@/lib/documents/extract";
|
||||
import { reserveBudget } from "@/lib/ai/coach/budget";
|
||||
import { SYNC_EXTRACT_MAX_READS } from "@/lib/documents/ocr-plan";
|
||||
|
||||
const mockModule = vi.mocked(isModuleEnabled);
|
||||
const mockProvider = vi.mocked(resolveIndexProvider);
|
||||
@@ -195,8 +196,8 @@ describe("maybeAutoStageLabFacts", () => {
|
||||
mockText.mockResolvedValue({ text: longReport, source: "verbatim" });
|
||||
|
||||
await maybeAutoStageLabFacts("u1", "d1");
|
||||
const unbounded = mockExtract.mock.calls[0]![0].chunks.length;
|
||||
expect(unbounded).toBeGreaterThan(1);
|
||||
const inRequest = mockExtract.mock.calls[0]![0].chunks.length;
|
||||
expect(inRequest).toBeGreaterThan(1);
|
||||
|
||||
const arm = vi.fn(async () => {});
|
||||
await maybeAutoStageLabFacts("u1", "d1", {
|
||||
@@ -207,6 +208,24 @@ describe("maybeAutoStageLabFacts", () => {
|
||||
expect(arm).toHaveBeenCalledWith(2);
|
||||
});
|
||||
|
||||
it("stays inside one request without a run deadline, and says what it left out", async () => {
|
||||
// The index route without Prefer: respond-async continues into this
|
||||
// staging with no schedule. Twelve hundred lines is many reads of text.
|
||||
const longReport = Array.from(
|
||||
{ length: 1200 },
|
||||
(_, i) => `Hämoglobin ${i} g/dl Referenzbereich 13-17 LDL 120 mg/dl`,
|
||||
).join("\n");
|
||||
mockText.mockResolvedValue({ text: longReport, source: "verbatim" });
|
||||
|
||||
await maybeAutoStageLabFacts("u1", "d1");
|
||||
const reads = mockExtract.mock.calls[0]![0].chunks.length;
|
||||
expect(reads).toBeGreaterThan(0);
|
||||
expect(reads).toBeLessThanOrEqual(SYNC_EXTRACT_MAX_READS);
|
||||
expect(updateMany.mock.calls[0]![0]).toMatchObject({
|
||||
data: { extractionIncomplete: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("skips (manual path only) when a module is off", async () => {
|
||||
mockModule.mockImplementation(async (_u, key) => key !== "labs");
|
||||
const res = await maybeAutoStageLabFacts("u1", "d1");
|
||||
|
||||
@@ -201,16 +201,21 @@ describe("planInboundTextReads", () => {
|
||||
const line = (i: number) => `Analyt ${i} ${i},0 mg/dl 1 - 9`;
|
||||
const text = (lines: number) =>
|
||||
Array.from({ length: lines }, (_, i) => line(i)).join("\n");
|
||||
const schedule = (calls: number) => ({
|
||||
maxModelCalls: () => calls,
|
||||
arm: async () => undefined,
|
||||
});
|
||||
const noDeadline = schedule(Number.POSITIVE_INFINITY);
|
||||
|
||||
it("reads a short text as one read", () => {
|
||||
const plan = planInboundTextReads(text(10), undefined);
|
||||
const plan = planInboundTextReads(text(10), noDeadline);
|
||||
expect(plan.args.chunks).toHaveLength(1);
|
||||
expect(plan.partial).toBe(false);
|
||||
});
|
||||
|
||||
it("splits a long text at line boundaries into reads no longer than one read carries", () => {
|
||||
const whole = text(400);
|
||||
const plan = planInboundTextReads(whole, undefined);
|
||||
const plan = planInboundTextReads(whole, noDeadline);
|
||||
expect(plan.args.chunks.length).toBeGreaterThan(1);
|
||||
for (const chunk of plan.args.chunks) {
|
||||
expect(chunk.text!.length).toBeLessThanOrEqual(INBOUND_TEXT_READ_CHARS);
|
||||
@@ -221,10 +226,6 @@ describe("planInboundTextReads", () => {
|
||||
});
|
||||
|
||||
it("grows the parts when the deadline allows few reads, and reads from the start past twice the size", () => {
|
||||
const schedule = (calls: number) => ({
|
||||
maxModelCalls: () => calls,
|
||||
arm: async () => undefined,
|
||||
});
|
||||
// Two reads' worth of calls: the parts grow to hold the text.
|
||||
const grown = planInboundTextReads(text(200), schedule(4));
|
||||
expect(grown.args.chunks).toHaveLength(2);
|
||||
@@ -239,7 +240,7 @@ describe("planInboundTextReads", () => {
|
||||
});
|
||||
|
||||
it("caps the reads of a run with no deadline", () => {
|
||||
const plan = planInboundTextReads(text(4000), undefined);
|
||||
const plan = planInboundTextReads(text(4000), noDeadline);
|
||||
expect(plan.args.chunks.length).toBe(OCR_MAX_PAGES);
|
||||
expect(plan.partial).toBe(true);
|
||||
});
|
||||
|
||||
@@ -36,6 +36,7 @@ import {
|
||||
} from "@/lib/documents/extract";
|
||||
import {
|
||||
OCR_EXTRACT_MODEL_CALLS,
|
||||
SYNCHRONOUS_READ_SCHEDULE,
|
||||
type OcrSchedule,
|
||||
} from "@/lib/documents/ocr-plan";
|
||||
import { OcrExtractError } from "@/lib/documents/ocr-read";
|
||||
@@ -267,24 +268,6 @@ function extractionFailure(err: OcrExtractError): AiRunOutcome<never> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The most reads an extraction makes inside one HTTP request (the extract
|
||||
* route without `Prefer: respond-async`, which the iPhone app sends). Each
|
||||
* read is up to `OCR_EXTRACT_MODEL_CALLS` model calls, so this bounds the
|
||||
* request at six calls where an unplanned read of a twenty-page report made
|
||||
* forty. A longer document is read in as many reads by sharing its pages or
|
||||
* text out between them, and what does not fit is reported as
|
||||
* `extractionIncomplete` on the document; the background run, which has a
|
||||
* deadline of its own, reads it a page at a time.
|
||||
*/
|
||||
export const SYNC_EXTRACT_MAX_READS = 3;
|
||||
|
||||
/** The read plan of a request: a fixed number of calls, nothing to arm. */
|
||||
export const SYNCHRONOUS_READ_SCHEDULE: OcrSchedule = {
|
||||
maxModelCalls: () => SYNC_EXTRACT_MAX_READS * OCR_EXTRACT_MODEL_CALLS,
|
||||
arm: async () => undefined,
|
||||
};
|
||||
|
||||
/** Extract and stage facts from one owned document. The caller authorised and charged it. */
|
||||
export async function executeDocumentExtract(args: {
|
||||
userId: string;
|
||||
|
||||
@@ -59,7 +59,8 @@ export interface IndexOrigin {
|
||||
worker: boolean;
|
||||
/**
|
||||
* A background run's deadline, which the lab staging after the index plans
|
||||
* its reads against. A request has none: it reads as before.
|
||||
* its reads against. A request has none: the staging then makes at most
|
||||
* `SYNC_EXTRACT_MAX_READS` reads and reports what it left out.
|
||||
*/
|
||||
schedule?: OcrSchedule;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ import {
|
||||
} from "@/lib/documents/extract";
|
||||
import {
|
||||
OCR_EXTRACT_MODEL_CALLS,
|
||||
SYNCHRONOUS_READ_SCHEDULE,
|
||||
type OcrSchedule,
|
||||
} from "@/lib/documents/ocr-plan";
|
||||
import { OcrExtractError } from "@/lib/documents/ocr-read";
|
||||
@@ -261,7 +262,10 @@ async function stageFactsIfStored(
|
||||
* `schedule` is the run's deadline when a job reads (the auto-index job, the
|
||||
* document run): the report's text is then split into as many reads as fit
|
||||
* before it, instead of up to `OCR_MAX_PAGES` reads of two calls each that a
|
||||
* slow model could stretch past the job's expiry (#1274).
|
||||
* slow model could stretch past the job's expiry (#1274). Absent, the staging
|
||||
* runs inside a request (the index route without `Prefer: respond-async`)
|
||||
* and is planned as one ({@link SYNCHRONOUS_READ_SCHEDULE}); what does not
|
||||
* fit is reported as `extractionIncomplete` on the document.
|
||||
*/
|
||||
export async function maybeAutoStageLabFacts(
|
||||
userId: string,
|
||||
@@ -333,14 +337,13 @@ export async function maybeAutoStageLabFacts(
|
||||
{ cap, surface: "job" },
|
||||
);
|
||||
|
||||
const plan = planInboundTextReads(chat.text, options.schedule);
|
||||
const schedule = options.schedule ?? SYNCHRONOUS_READ_SCHEDULE;
|
||||
const plan = planInboundTextReads(chat.text, schedule);
|
||||
let result: InboundExtractionResult;
|
||||
try {
|
||||
// A run that cannot fit even one read before its deadline refuses here;
|
||||
// the reservation is settled below like any other failed read.
|
||||
await options.schedule?.arm(
|
||||
plan.args.chunks.length * OCR_EXTRACT_MODEL_CALLS,
|
||||
);
|
||||
await schedule.arm(plan.args.chunks.length * OCR_EXTRACT_MODEL_CALLS);
|
||||
result = await runInboundExtraction({
|
||||
userId,
|
||||
provider: provider.pick.entry.instance,
|
||||
|
||||
@@ -462,7 +462,7 @@ export interface InboundReads {
|
||||
/** A document's text in parts at line boundaries, as many as the run's deadline allows. */
|
||||
export function planInboundTextReads(
|
||||
text: string,
|
||||
schedule: OcrSchedule | undefined,
|
||||
schedule: OcrSchedule,
|
||||
): InboundReads {
|
||||
const { chunks, unreadParts } = planTextReads(
|
||||
text,
|
||||
@@ -483,7 +483,7 @@ export function planInboundTextReads(
|
||||
*/
|
||||
export async function planInboundReads(
|
||||
source: InboundReadSource,
|
||||
schedule: OcrSchedule | undefined,
|
||||
schedule: OcrSchedule,
|
||||
): Promise<({ ok: true } & InboundReads) | { ok: false }> {
|
||||
if (source.mode === "text") {
|
||||
return { ok: true, ...planInboundTextReads(source.text, schedule) };
|
||||
|
||||
@@ -46,9 +46,27 @@ export interface OcrSchedule {
|
||||
arm(modelCalls: number): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* The most reads one HTTP request makes: the extract route, and the lab
|
||||
* staging the index route continues into, both without `Prefer:
|
||||
* respond-async` (the iPhone app sends neither). Each read is up to
|
||||
* `OCR_EXTRACT_MODEL_CALLS` model calls, so this bounds the request at six
|
||||
* calls where an unplanned read of a twenty-page report made forty. A longer
|
||||
* document is read in as many reads by sharing its pages or text out between
|
||||
* them, and what does not fit is reported as `extractionIncomplete` on the
|
||||
* document; a background run, which has a deadline of its own, reads it a
|
||||
* page at a time.
|
||||
*/
|
||||
export const SYNC_EXTRACT_MAX_READS = 3;
|
||||
|
||||
/** The read plan of a request: a fixed number of calls, nothing to arm. */
|
||||
export const SYNCHRONOUS_READ_SCHEDULE: OcrSchedule = {
|
||||
maxModelCalls: () => SYNC_EXTRACT_MAX_READS * OCR_EXTRACT_MODEL_CALLS,
|
||||
arm: async () => undefined,
|
||||
};
|
||||
|
||||
/** How many reads the deadline leaves room for. */
|
||||
export function maxReadsFor(schedule: OcrSchedule | undefined): number {
|
||||
if (!schedule) return Number.POSITIVE_INFINITY;
|
||||
export function maxReadsFor(schedule: OcrSchedule): number {
|
||||
return Math.max(
|
||||
1,
|
||||
Math.floor(schedule.maxModelCalls() / OCR_EXTRACT_MODEL_CALLS),
|
||||
@@ -111,7 +129,7 @@ export function splitOcrText(
|
||||
*/
|
||||
export function planTextReads(
|
||||
text: string,
|
||||
schedule: OcrSchedule | undefined,
|
||||
schedule: OcrSchedule,
|
||||
target: number,
|
||||
): { chunks: OcrChunk[]; unreadParts: number } {
|
||||
const maxReads = Math.min(maxReadsFor(schedule), OCR_MAX_PAGES);
|
||||
@@ -167,7 +185,7 @@ export async function planVisionReads(
|
||||
bytes: Buffer,
|
||||
mime: OcrVisionMime,
|
||||
pdfSupported: boolean,
|
||||
schedule: OcrSchedule | undefined,
|
||||
schedule: OcrSchedule,
|
||||
): Promise<OcrVisionPlan> {
|
||||
if (mime !== "application/pdf") {
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user