mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
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:
1 parent
3b74b108a1
commit
b91649d6de
30 files changed
+2281
-569
No files matched your search
@@ -31,9 +31,11 @@ import { queryKeys } from "@/lib/query-keys";
|
||||
import { AdminOpenAIProviderForm } from "./admin-openai-provider-form";
|
||||
import { AnthropicProviderForm } from "./anthropic-provider-form";
|
||||
import { CodexProviderForm } from "./codex-provider-form";
|
||||
import { DocumentScanCard } from "./document-scan-card";
|
||||
import { FallbackChainCard } from "./fallback-chain-card";
|
||||
import { LocalProviderForm } from "./local-provider-form";
|
||||
import { OpenAIProviderForm } from "./openai-provider-form";
|
||||
import { ResponseTimeoutCard } from "./response-timeout-card";
|
||||
import { RuntimeActionsRow } from "./runtime-actions-row";
|
||||
import {
|
||||
PROVIDER_TYPES,
|
||||
@@ -171,6 +173,13 @@ export function AiInsightsCard({
|
||||
onSelect={pickProvider}
|
||||
/>
|
||||
|
||||
{/* v1.22 (#89) — per-user response timeout (mainly for slow
|
||||
local/self-hosted backends). */}
|
||||
<ResponseTimeoutCard userProvider={userProvider} />
|
||||
|
||||
{/* v1.22 (#90) — dedicated document-scan (Lab-OCR) provider. */}
|
||||
<DocumentScanCard userProvider={userProvider} />
|
||||
|
||||
<RuntimeActionsRow
|
||||
provider={selectedProvider}
|
||||
userProvider={userProvider}
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
"use client";
|
||||
|
||||
/* ────────────────────────────────────────────────────────────────
|
||||
* v1.22 (#90) — Document scanning (Lab reports).
|
||||
*
|
||||
* Opt-in dedicated provider for the Lab-OCR ingestion path. OFF by default:
|
||||
* scanning uses the main provider chain. When enabled, document/lab-report
|
||||
* processing routes to THIS provider/model/key instead — so a user can keep a
|
||||
* cheap text Coach on one provider and send expensive vision OCR to a
|
||||
* vision-capable one. Surfaces a hint when the chosen model can't read images.
|
||||
* ──────────────────────────────────────────────────────────────── */
|
||||
|
||||
import { useState } from "react";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { Loader2, Save } from "lucide-react";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { NativeSelect } from "@/components/ui/native-select";
|
||||
import { PasswordInput } from "@/components/ui/password-input";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { apiPatch } from "@/lib/api/api-fetch";
|
||||
import {
|
||||
supportsVisionForConfig,
|
||||
type VisionProviderType,
|
||||
} from "@/lib/ai/vision-capability";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
import { OCR_PROVIDER_TYPES, type UserAIProvider } from "./shared";
|
||||
|
||||
/** Map the OCR provider enum to the vision-capability tag. */
|
||||
function toVisionType(provider: string): VisionProviderType {
|
||||
switch (provider) {
|
||||
case "ANTHROPIC":
|
||||
return "anthropic";
|
||||
case "OPENAI":
|
||||
return "openai";
|
||||
case "LOCAL":
|
||||
return "local";
|
||||
default:
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
|
||||
export function DocumentScanCard({
|
||||
userProvider,
|
||||
}: {
|
||||
userProvider: UserAIProvider | null | undefined;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [enabled, setEnabled] = useState(false);
|
||||
const [provider, setProvider] = useState<string>("OPENAI");
|
||||
const [model, setModel] = useState("");
|
||||
const [baseUrl, setBaseUrl] = useState("");
|
||||
const [apiKey, setApiKey] = useState("");
|
||||
const [msg, setMsg] = useState<string | null>(null);
|
||||
const [ok, setOk] = useState(false);
|
||||
|
||||
const seededKey =
|
||||
userProvider != null
|
||||
? `${userProvider.ocrEnabled}|${userProvider.ocrProvider ?? ""}|${userProvider.ocrModel ?? ""}|${userProvider.ocrBaseUrl ?? ""}`
|
||||
: null;
|
||||
const [previousSeed, setPreviousSeed] = useState<string | null>(null);
|
||||
if (seededKey != null && seededKey !== previousSeed) {
|
||||
setPreviousSeed(seededKey);
|
||||
setEnabled(userProvider?.ocrEnabled ?? false);
|
||||
setProvider(userProvider?.ocrProvider ?? "OPENAI");
|
||||
setModel(userProvider?.ocrModel ?? "");
|
||||
setBaseUrl(userProvider?.ocrBaseUrl ?? "");
|
||||
}
|
||||
|
||||
// Vision-capability hint: warn when a model is chosen that can't read images
|
||||
// (local is trust-by-default; the warning surfaces only for cloud providers).
|
||||
const visionOk =
|
||||
!enabled ||
|
||||
model.trim() === "" ||
|
||||
supportsVisionForConfig(toVisionType(provider), model.trim());
|
||||
|
||||
const saveMutation = useMutation({
|
||||
mutationFn: async () => {
|
||||
const body: Record<string, unknown> = {
|
||||
ocrEnabled: enabled,
|
||||
ocrProvider: provider,
|
||||
ocrModel: model.trim() || null,
|
||||
ocrBaseUrl: provider === "LOCAL" ? baseUrl.trim() || null : null,
|
||||
};
|
||||
if (apiKey.trim()) body.ocrKey = apiKey.trim();
|
||||
await apiPatch("/api/user/ai-provider", body);
|
||||
},
|
||||
onSuccess: () => {
|
||||
setOk(true);
|
||||
setMsg(t("settings.ai.saved"));
|
||||
setApiKey("");
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.userAiProvider() });
|
||||
},
|
||||
onError: (e) => {
|
||||
setOk(false);
|
||||
setMsg(e instanceof Error ? e.message : t("settings.ai.errorGeneric"));
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="bg-muted/50 space-y-4 rounded-lg p-4">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div>
|
||||
<p className="text-sm font-medium">{t("settings.ai.ocrHeading")}</p>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
{t("settings.ai.ocrBody")}
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
checked={enabled}
|
||||
onCheckedChange={setEnabled}
|
||||
aria-label={t("settings.ai.ocrEnableLabel")}
|
||||
data-testid="ai-ocr-enable"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{enabled && (
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<Label htmlFor="ai-ocr-provider">
|
||||
{t("settings.ai.ocrProviderLabel")}
|
||||
</Label>
|
||||
<NativeSelect
|
||||
id="ai-ocr-provider"
|
||||
value={provider}
|
||||
onChange={(e) => setProvider(e.target.value)}
|
||||
className="mt-1 sm:max-w-md"
|
||||
>
|
||||
{OCR_PROVIDER_TYPES.map((p) => (
|
||||
<option key={p} value={p}>
|
||||
{t(`settings.ai.ocrProviderOption.${p}` as const)}
|
||||
</option>
|
||||
))}
|
||||
</NativeSelect>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label htmlFor="ai-ocr-model">{t("settings.ai.modelLabel")}</Label>
|
||||
<Input
|
||||
id="ai-ocr-model"
|
||||
value={model}
|
||||
onChange={(e) => setModel(e.target.value)}
|
||||
placeholder={t("settings.ai.ocrModelPlaceholder")}
|
||||
className="mt-1"
|
||||
/>
|
||||
{!visionOk && (
|
||||
<p className="text-warning mt-1 text-xs">
|
||||
{t("settings.ai.ocrVisionHint")}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{provider === "LOCAL" && (
|
||||
<div>
|
||||
<Label htmlFor="ai-ocr-base-url">
|
||||
{t("settings.ai.baseUrlLabel")}
|
||||
</Label>
|
||||
<Input
|
||||
id="ai-ocr-base-url"
|
||||
value={baseUrl}
|
||||
onChange={(e) => setBaseUrl(e.target.value)}
|
||||
placeholder="http://localhost:11434/v1"
|
||||
className="mt-1"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div>
|
||||
<Label htmlFor="ai-ocr-key">{t("settings.ai.ocrKeyLabel")}</Label>
|
||||
<PasswordInput
|
||||
id="ai-ocr-key"
|
||||
value={apiKey}
|
||||
onChange={(e) => setApiKey(e.target.value)}
|
||||
placeholder={
|
||||
userProvider?.hasOcrKey ? t("settings.ai.savedShort") : ""
|
||||
}
|
||||
className="mt-1"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div>
|
||||
<Button
|
||||
size="sm"
|
||||
className="min-h-11 sm:min-h-9"
|
||||
onClick={() => saveMutation.mutate()}
|
||||
disabled={saveMutation.isPending}
|
||||
>
|
||||
{saveMutation.isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin motion-reduce:animate-none" />
|
||||
) : (
|
||||
<Save className="h-4 w-4" />
|
||||
)}
|
||||
{t("settings.ai.saveCta")}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{msg && (
|
||||
<p className={`text-xs ${ok ? "text-success" : "text-destructive"}`}>
|
||||
{msg}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
"use client";
|
||||
|
||||
/* ────────────────────────────────────────────────────────────────
|
||||
* v1.22 (#89) — Response timeout (seconds).
|
||||
*
|
||||
* A per-user upstream timeout for AI generation, threaded onto the provider
|
||||
* call (`CompletionParams.timeoutMs`). Surfaced here mainly for local /
|
||||
* self-hosted backends: an MLX/exo server can take >60 s on the first request
|
||||
* while it loads the model, which the legacy 60 s default timed out before the
|
||||
* first token landed. Empty = the generous built-in default (~180 s).
|
||||
* ──────────────────────────────────────────────────────────────── */
|
||||
|
||||
import { useState } from "react";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { Loader2, Save } from "lucide-react";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { apiPatch } from "@/lib/api/api-fetch";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
import type { UserAIProvider } from "./shared";
|
||||
|
||||
export function ResponseTimeoutCard({
|
||||
userProvider,
|
||||
}: {
|
||||
userProvider: UserAIProvider | null | undefined;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [value, setValue] = useState("");
|
||||
const [msg, setMsg] = useState<string | null>(null);
|
||||
const [ok, setOk] = useState(false);
|
||||
|
||||
// Seed from the persisted value once it arrives (seed-on-data pattern).
|
||||
const seededKey =
|
||||
userProvider != null
|
||||
? `${userProvider.responseTimeoutSeconds ?? ""}`
|
||||
: null;
|
||||
const [previousSeed, setPreviousSeed] = useState<string | null>(null);
|
||||
if (seededKey != null && seededKey !== previousSeed) {
|
||||
setPreviousSeed(seededKey);
|
||||
setValue(
|
||||
userProvider?.responseTimeoutSeconds != null
|
||||
? String(userProvider.responseTimeoutSeconds)
|
||||
: "",
|
||||
);
|
||||
}
|
||||
|
||||
const saveMutation = useMutation({
|
||||
mutationFn: async () => {
|
||||
const trimmed = value.trim();
|
||||
const responseTimeoutSeconds = trimmed === "" ? null : Number(trimmed);
|
||||
if (
|
||||
responseTimeoutSeconds !== null &&
|
||||
(!Number.isInteger(responseTimeoutSeconds) ||
|
||||
responseTimeoutSeconds < 10 ||
|
||||
responseTimeoutSeconds > 600)
|
||||
) {
|
||||
throw new Error(t("settings.ai.responseTimeoutInvalid"));
|
||||
}
|
||||
await apiPatch("/api/user/ai-provider", { responseTimeoutSeconds });
|
||||
},
|
||||
onSuccess: () => {
|
||||
setOk(true);
|
||||
setMsg(t("settings.ai.saved"));
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.userAiProvider() });
|
||||
},
|
||||
onError: (e) => {
|
||||
setOk(false);
|
||||
setMsg(e instanceof Error ? e.message : t("settings.ai.errorGeneric"));
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="bg-muted/50 space-y-3 rounded-lg p-4">
|
||||
<div>
|
||||
<p className="text-sm font-medium">
|
||||
{t("settings.ai.responseTimeoutHeading")}
|
||||
</p>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
{t("settings.ai.responseTimeoutBody")}
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<Label htmlFor="ai-response-timeout">
|
||||
{t("settings.ai.responseTimeoutLabel")}
|
||||
</Label>
|
||||
<Input
|
||||
id="ai-response-timeout"
|
||||
type="number"
|
||||
inputMode="numeric"
|
||||
min={10}
|
||||
max={600}
|
||||
value={value}
|
||||
onChange={(e) => setValue(e.target.value)}
|
||||
placeholder={t("settings.ai.responseTimeoutPlaceholder")}
|
||||
className="mt-1 sm:max-w-xs"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Button
|
||||
size="sm"
|
||||
className="min-h-11 sm:min-h-9"
|
||||
onClick={() => saveMutation.mutate()}
|
||||
disabled={saveMutation.isPending}
|
||||
>
|
||||
{saveMutation.isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin motion-reduce:animate-none" />
|
||||
) : (
|
||||
<Save className="h-4 w-4" />
|
||||
)}
|
||||
{t("settings.ai.saveCta")}
|
||||
</Button>
|
||||
</div>
|
||||
{msg && (
|
||||
<p className={`text-xs ${ok ? "text-success" : "text-destructive"}`}>
|
||||
{msg}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -27,8 +27,21 @@ export interface UserAIProvider {
|
||||
hasLocalKey: boolean;
|
||||
hasOpenaiKey: boolean;
|
||||
openaiKeyPreview: string | null;
|
||||
// v1.22 (#89) — per-user response timeout, in seconds (null = default).
|
||||
responseTimeoutSeconds: number | null;
|
||||
// v1.22 (#90) — dedicated document-scan (Lab-OCR) provider config.
|
||||
ocrEnabled: boolean;
|
||||
ocrProvider: string | null;
|
||||
ocrModel: string | null;
|
||||
ocrBaseUrl: string | null;
|
||||
hasOcrKey: boolean;
|
||||
ocrKeyPreview: string | null;
|
||||
}
|
||||
|
||||
/** v1.22 (#90) — providers the dedicated document-scan path accepts. */
|
||||
export const OCR_PROVIDER_TYPES = ["OPENAI", "ANTHROPIC", "LOCAL"] as const;
|
||||
export type OcrProviderType = (typeof OCR_PROVIDER_TYPES)[number];
|
||||
|
||||
/**
|
||||
* v1.4.16 phase B2 — provider tags exposed to the UI. Mirrors
|
||||
* `PROVIDER_CHAIN_TYPES` server-side; kept as a const-array here to
|
||||
|
||||
Reference in new issue
Block a user