mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(labs-ocr): read PDFs on the configured provider, retire the separate scan provider
Lab-report OCR now rasterizes a PDF to page images for any non-Anthropic vision provider, exactly as the document vault already does — Anthropic keeps its native PDF block, every other vision provider (codex/OAuth, OpenAI, local) reads the rendered pages. The capability probe reports `pdfSupported` whenever the picked provider reads PDFs natively or the server-side rasterizer is available, so the "scan a PDF" affordance shows for a codex provider too. Drop the dedicated document-scan (Lab-OCR) provider: labs OCR always uses the main configured provider chain. Removes the settings card, the `aiOcr*` resolution branch, the GET/PATCH handler fields, the OpenAPI schema fields, the i18n keys across all locales, and the five backing `User` columns (migration 0234). The on-device tesseract fallback is untouched. Dashboard hero: stack the overdue-dose action directly beneath the verdict text (tight gap, left-aligned) on every viewport instead of pushing it to the far container edge on desktop.
This commit is contained in:
1 parent
c8e67042a8
commit
46f8c71ee5
25 files changed
+191
-775
No files matched your search
@@ -139,7 +139,7 @@ describe("GET /api/user/ai-provider availability", () => {
|
||||
expect(body.data.managedBy).toBeNull();
|
||||
});
|
||||
|
||||
it("returns the v1.22 timeout + OCR fields", async () => {
|
||||
it("returns the v1.22 response timeout field", async () => {
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue({
|
||||
aiProvider: "OPENAI",
|
||||
aiModel: "gpt-4o",
|
||||
@@ -148,11 +148,6 @@ describe("GET /api/user/ai-provider availability", () => {
|
||||
aiLocalKeyEncrypted: null,
|
||||
aiOpenaiKeyEncrypted: null,
|
||||
aiResponseTimeoutSeconds: 240,
|
||||
aiOcrEnabled: true,
|
||||
aiOcrProvider: "ANTHROPIC",
|
||||
aiOcrModel: "claude-sonnet-4-6",
|
||||
aiOcrBaseUrl: null,
|
||||
aiOcrKeyEncrypted: "enc-ocr-1234",
|
||||
} as never);
|
||||
vi.mocked(resolveProviderAvailability).mockResolvedValue({
|
||||
aiAvailable: true,
|
||||
@@ -161,21 +156,10 @@ describe("GET /api/user/ai-provider availability", () => {
|
||||
|
||||
const res = await (GET as () => Promise<Response>)();
|
||||
const body = (await res.json()) as {
|
||||
data: {
|
||||
responseTimeoutSeconds: number | null;
|
||||
ocrEnabled: boolean;
|
||||
ocrProvider: string | null;
|
||||
hasOcrKey: boolean;
|
||||
ocrKeyPreview: string | null;
|
||||
};
|
||||
data: { responseTimeoutSeconds: number | null };
|
||||
};
|
||||
|
||||
expect(body.data.responseTimeoutSeconds).toBe(240);
|
||||
expect(body.data.ocrEnabled).toBe(true);
|
||||
expect(body.data.ocrProvider).toBe("ANTHROPIC");
|
||||
expect(body.data.hasOcrKey).toBe(true);
|
||||
// Preview is `...` + last 4 of the decrypted value (decrypt → `dec:...`).
|
||||
expect(body.data.ocrKeyPreview).toMatch(/^\.\.\./);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -207,32 +191,4 @@ describe("PATCH /api/user/ai-provider — v1.22 fields", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("persists OCR provider config and encrypts the key", async () => {
|
||||
const res = await patch(
|
||||
patchRequest({
|
||||
ocrEnabled: true,
|
||||
ocrProvider: "OPENAI",
|
||||
ocrModel: "gpt-4o",
|
||||
ocrKey: "sk-ocr-secret",
|
||||
}),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(vi.mocked(prisma.user.update)).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
aiOcrEnabled: true,
|
||||
aiOcrProvider: "OPENAI",
|
||||
aiOcrModel: "gpt-4o",
|
||||
aiOcrKeyEncrypted: "enc:sk-ocr-secret",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects an invalid OCR provider", async () => {
|
||||
await expect(
|
||||
patch(patchRequest({ ocrProvider: "CHATGPT_OAUTH" })),
|
||||
).rejects.toThrow(/Invalid OCR provider/);
|
||||
});
|
||||
});
|
||||
@@ -11,9 +11,6 @@ import { annotate } from "@/lib/logging/context";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const ALLOWED = new Set(["OPENAI", "ANTHROPIC", "LOCAL", "CHATGPT_OAUTH"]);
|
||||
// v1.22 (#90) — the dedicated document-scan provider has no OAuth path, so the
|
||||
// allowlist is the three keyed/self-hosted providers only (no CHATGPT_OAUTH).
|
||||
const OCR_PROVIDERS = new Set(["OPENAI", "ANTHROPIC", "LOCAL"]);
|
||||
|
||||
export const GET = apiHandler(async () => {
|
||||
const { user } = await requireAuth();
|
||||
@@ -28,13 +25,8 @@ export const GET = apiHandler(async () => {
|
||||
aiAnthropicKeyEncrypted: true,
|
||||
aiLocalKeyEncrypted: true,
|
||||
aiOpenaiKeyEncrypted: true,
|
||||
// v1.22 (#89 + #90)
|
||||
// v1.22 (#89)
|
||||
aiResponseTimeoutSeconds: true,
|
||||
aiOcrEnabled: true,
|
||||
aiOcrProvider: true,
|
||||
aiOcrModel: true,
|
||||
aiOcrBaseUrl: true,
|
||||
aiOcrKeyEncrypted: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -62,16 +54,6 @@ export const GET = apiHandler(async () => {
|
||||
: null,
|
||||
// v1.22 (#89) — per-user response timeout, in seconds (null = default).
|
||||
responseTimeoutSeconds: u?.aiResponseTimeoutSeconds ?? null,
|
||||
// v1.22 (#90) — dedicated document-scan (Lab-OCR) provider config. Key
|
||||
// material is presence + 4-char preview only, like every other provider.
|
||||
ocrEnabled: Boolean(u?.aiOcrEnabled),
|
||||
ocrProvider: u?.aiOcrProvider ?? null,
|
||||
ocrModel: u?.aiOcrModel ?? null,
|
||||
ocrBaseUrl: u?.aiOcrBaseUrl ?? null,
|
||||
hasOcrKey: Boolean(u?.aiOcrKeyEncrypted),
|
||||
ocrKeyPreview: u?.aiOcrKeyEncrypted
|
||||
? `...${decrypt(u.aiOcrKeyEncrypted).slice(-4)}`
|
||||
: null,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -179,62 +161,6 @@ export const PATCH = apiHandler(async (request: NextRequest) => {
|
||||
}
|
||||
}
|
||||
|
||||
// ── v1.22 (#90) — dedicated document-scan (Lab-OCR) provider ──
|
||||
if (body.ocrEnabled !== undefined) {
|
||||
if (typeof body.ocrEnabled === "boolean") {
|
||||
updates.aiOcrEnabled = body.ocrEnabled;
|
||||
} else {
|
||||
throw new HttpError(422, "Invalid ocrEnabled");
|
||||
}
|
||||
}
|
||||
|
||||
if (body.ocrProvider !== undefined) {
|
||||
if (body.ocrProvider === null || body.ocrProvider === "") {
|
||||
updates.aiOcrProvider = null;
|
||||
} else if (
|
||||
typeof body.ocrProvider === "string" &&
|
||||
OCR_PROVIDERS.has(body.ocrProvider)
|
||||
) {
|
||||
updates.aiOcrProvider = body.ocrProvider;
|
||||
} else {
|
||||
throw new HttpError(422, "Invalid OCR provider");
|
||||
}
|
||||
}
|
||||
|
||||
if (body.ocrModel !== undefined) {
|
||||
if (body.ocrModel === null || body.ocrModel === "") {
|
||||
updates.aiOcrModel = null;
|
||||
} else if (typeof body.ocrModel === "string") {
|
||||
updates.aiOcrModel = body.ocrModel.trim() || null;
|
||||
}
|
||||
}
|
||||
|
||||
if (body.ocrBaseUrl !== undefined) {
|
||||
if (body.ocrBaseUrl === null || body.ocrBaseUrl === "") {
|
||||
updates.aiOcrBaseUrl = null;
|
||||
} else if (typeof body.ocrBaseUrl === "string") {
|
||||
const trimmed = body.ocrBaseUrl.trim();
|
||||
// Same SSRF guard as the main LOCAL base URL: reject private/internal
|
||||
// hosts unless the operator opted in via ALLOW_LOCAL_AI_PRIVATE_HOSTS.
|
||||
const allowPrivate = isLocalAiHostAllowed(trimmed);
|
||||
if (!allowPrivate && !isPublicUrl(trimmed)) {
|
||||
return apiError(
|
||||
"OCR base URL points to an internal/private host. Ops must allow it on this instance via ALLOW_LOCAL_AI_PRIVATE_HOSTS.",
|
||||
422,
|
||||
);
|
||||
}
|
||||
updates.aiOcrBaseUrl = trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
if (body.ocrKey !== undefined) {
|
||||
if (body.ocrKey === null || body.ocrKey === "") {
|
||||
updates.aiOcrKeyEncrypted = null;
|
||||
} else if (typeof body.ocrKey === "string") {
|
||||
updates.aiOcrKeyEncrypted = encrypt(body.ocrKey.trim());
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(updates).length === 0) {
|
||||
throw new HttpError(422, "No valid fields");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user