feat(ai): UI-configurable response timeout, local streaming, dedicated OCR provider

Add three per-user AI settings, all configured in the app (no new env vars),
behind one migration (0198):

- Response timeout (seconds). A per-user upstream timeout threaded onto the
  provider call. A local/self-hosted backend can take more than the legacy
  60 s default to load a model on the first request, which timed the Coach
  out before the first token. Defaults to a generous ~180 s when unset;
  validated to 10–600 s.

- True token streaming for the local OpenAI-compatible client. The client now
  POSTs with stream:true and parses the SSE delta chunks, emitting tokens as
  they arrive, with a per-idle-gap timeout instead of a whole-call ceiling and
  a graceful fallback to the buffered path when a server cannot stream. The
  Coach route runs the provider call inside the response stream behind a
  keepalive heartbeat so a reverse proxy no longer idle-drops the connection
  during model load or generation. Grounding, refusal, and sentinel-stripping
  still run on the complete reply before any token reaches the client.

- Dedicated document-scan (Lab-OCR) provider. Off by default — the OCR path
  uses the main provider chain. When enabled, lab-report processing resolves a
  separate provider/model/key, with a hint when the chosen model is not
  vision-capable. The new key column is AES-256-GCM at rest and registered in
  the encrypted-columns registry and the rotation script.

i18n: EN + DE translations for the new Settings → AI strings (other locales
carry interim copies to keep locale parity green).
This commit is contained in:
Marc-André Bombeck committed 2026-06-27 15:21:40 +02:00
1 parent 3b74b108a1
commit b91649d6de
30 files changed
+2281 -569

No files matched your search

+28
View File
@@ -334,6 +334,34 @@ const aiProviderResponse = z
.string()
.nullable()
.describe("`...` + last 4 chars of the stored OpenAI key, or null."),
responseTimeoutSeconds: z
.number()
.int()
.nullable()
.describe(
"v1.22 (#89) — per-user response timeout for AI generation, in seconds (10–600). Null = the generous built-in default (~180 s). Mainly for slow local/self-hosted backends.",
),
ocrEnabled: z
.boolean()
.describe(
"v1.22 (#90) — whether a dedicated document-scan (Lab-OCR) provider is active. When false the OCR path uses the main provider chain.",
),
ocrProvider: z
.enum(["OPENAI", "ANTHROPIC", "LOCAL"])
.nullable()
.describe("The dedicated document-scan provider, or null when unset."),
ocrModel: z.string().nullable(),
ocrBaseUrl: z
.string()
.nullable()
.describe("Custom base URL (LOCAL OCR provider only); null otherwise."),
hasOcrKey: z.boolean(),
ocrKeyPreview: z
.string()
.nullable()
.describe(
"`...` + last 4 chars of the stored document-scan provider key, or null.",
),
})
.meta({
id: "AiProviderResponse",