Files
HealthLog/src/hooks/use-ai-run.ts
T
Marc-André Bombeck 0acf3da575 feat(documents): read documents and lab scans in the background
"Read with AI" and the lab scan used to run their model calls inside the
request. At the default response time a read of three calls outlasted the
100 s a proxy in front of the server commonly allows, and the person saw a
cut connection for a read the server was still doing.

Both now run as a background run (`DocumentAiRun`, migration 0366):

- `POST /api/documents/inbound/{id}/index` with `Prefer: respond-async`
  answers 202 with a run id once every quick refusal has passed. Without
  the header it answers exactly as before, which the iPhone app relies on.
  A second request while a read of the same document is queued or running
  attaches to that run.
- `POST /api/labs/ocr/extract` always answers 202. Only the web client
  calls it.
- `GET /api/ai-runs/{id}` serves the run: the same body the synchronous
  route answers with, or the failure code and status it would have given.
  Scoped to the caller; another account's run is a 404.

The worker (`document-ai-run`, a long queue with a one-hour expiry, one
job per run under `lockedPass`) claims the run, asks the capability again
for the record and re-checks the wire for the provider it picks, sets the
run's deadline from the person's AI response time and refuses a read that
cannot fit the job's expiry. Sync route and worker run one body per kind,
so the two paths cannot drift. Budget is reserved at enqueue and settled
once by whoever ends the run.

Input and result are sealed with labelled AES-256-GCM, registered for key
rotation; the input is dropped when the run ends. A five-minute reaper
fails a run no worker took within fifteen minutes (and hands its
reservation back), fails a run past its deadline, and deletes a finished
run after an hour. Runs are outside backups and inside the data wipe.

The web client polls through the query-key factory and says, calmly, that
the read continues in the background, and when no worker has picked it up.
2026-10-03 18:18:56 +02:00

137 lines
4.6 KiB
TypeScript

"use client";
/**
* v1.40 — follow a background document AI run to its end.
*
* A route that queues a read answers 202 with `{ runId, status, pollAfterMs }`.
* `resolveAiRun` takes whatever the route answered: a synchronous body passes
* straight through, a queued run is polled through the query cache
* (`queryKeys.aiRun`) until it ends. The result resolves exactly like the
* synchronous body did; a failed run throws the same `ApiError` (message,
* status, `meta.errorCode`) the synchronous route would have, so every
* existing error mapping keeps working.
*
* `useAiRunPhase` is the calm state a screen shows while a read runs in the
* background, and the hint when no worker has picked it up yet.
*/
import { useCallback, useState } from "react";
import type { QueryClient } from "@tanstack/react-query";
import { ApiError, apiGet } from "@/lib/api/api-fetch";
import type { AiRunAccepted, AiRunDto } from "@/lib/documents/ai-runs/types";
import { queryKeys } from "@/lib/query-keys";
/** A queued run has waited this long: say the background worker may be down. */
export const AI_RUN_WORKER_HINT_MS = 30_000;
const DEFAULT_POLL_MS = 1500;
/** Whether a route's 2xx body is a queued run rather than its result. */
export function isAiRunAccepted(data: unknown): data is AiRunAccepted {
return (
typeof data === "object" &&
data !== null &&
typeof (data as { runId?: unknown }).runId === "string" &&
(data as { status?: unknown }).status === "QUEUED"
);
}
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise((resolve, reject) => {
if (signal?.aborted) {
reject(signal.reason);
return;
}
const timer = setTimeout(() => {
signal?.removeEventListener("abort", onAbort);
resolve();
}, ms);
const onAbort = () => {
clearTimeout(timer);
reject(signal?.reason);
};
signal?.addEventListener("abort", onAbort, { once: true });
});
}
export interface WaitForAiRunOptions {
/** Called with every poll answer while the run has not ended. */
onProgress?: (run: AiRunDto) => void;
signal?: AbortSignal;
/** Injectable for tests. */
wait?: (ms: number, signal?: AbortSignal) => Promise<void>;
}
/** Poll one run until it ends; resolve its result or throw its failure. */
export async function waitForAiRun<T>(
queryClient: QueryClient,
accepted: AiRunAccepted,
options: WaitForAiRunOptions = {},
): Promise<T> {
const wait = options.wait ?? sleep;
const key = queryKeys.aiRun(accepted.runId);
let delay = accepted.pollAfterMs > 0 ? accepted.pollAfterMs : DEFAULT_POLL_MS;
try {
for (;;) {
await wait(delay, options.signal);
const run = await queryClient.fetchQuery({
queryKey: key,
queryFn: () =>
apiGet<AiRunDto>(
`/api/ai-runs/${encodeURIComponent(accepted.runId)}`,
),
staleTime: 0,
retry: 2,
});
if (run.status === "SUCCEEDED") return run.result as T;
if (run.status === "FAILED") {
const error = run.error ?? {
code: "aiRuns.failed",
message: "The background read failed.",
status: 500,
};
throw new ApiError(error.message, error.status, {
errorCode: error.code,
});
}
options.onProgress?.(run);
delay = run.retryAfterMs ?? DEFAULT_POLL_MS;
}
} finally {
// The answer has been handed to the caller; it does not stay in memory.
queryClient.removeQueries({ queryKey: key });
}
}
/** A route's 2xx body, followed to its result when it is a queued run. */
export async function resolveAiRun<T>(
queryClient: QueryClient,
data: T | AiRunAccepted,
options: WaitForAiRunOptions & { onQueued?: () => void } = {},
): Promise<T> {
if (!isAiRunAccepted(data)) return data;
options.onQueued?.();
return waitForAiRun<T>(queryClient, data, options);
}
export type AiRunPhase = "idle" | "background" | "waitingForWorker";
/**
* The phase a screen renders while a read runs in the background: `background`
* once the route queued it, `waitingForWorker` once it has sat in the queue
* past {@link AI_RUN_WORKER_HINT_MS}.
*/
export function useAiRunPhase() {
const [phase, setPhase] = useState<AiRunPhase>("idle");
const onQueued = useCallback(() => setPhase("background"), []);
const onProgress = useCallback((run: AiRunDto) => {
setPhase(
run.status === "QUEUED" && (run.queuedForMs ?? 0) >= AI_RUN_WORKER_HINT_MS
? "waitingForWorker"
: "background",
);
}, []);
const reset = useCallback(() => setPhase("idle"), []);
return { phase, onQueued, onProgress, reset };
}