mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
feat(settings): explain operator switches and keep the Coach memory reachable
Settings, AI says which AI features the server's operator switched off and shows the one setup hint while no provider is configured. The consent withdrawal says what it deletes and what stays, and a document-reading consent on file gets its own withdrawal. Regenerate follows the briefing capability and the connection test names an operator refusal. The Coach memory (facts and stored conversations) is never gated on the Coach: Settings, Coach always shows it, and Settings, AI shows it while the Coach page is out of reach, whenever rows exist. Onboarding offers AI only where it can be set up here: the done-screen panel and the getting-started row go away while the operator has AI off or inside somebody else's record.
This commit is contained in:
1 parent
4449b34ac7
commit
3d85445e50
24 files changed
+913
-232
No files matched your search
@@ -15,9 +15,18 @@
|
||||
* without an active receipt, so every AI surface falls back to its
|
||||
* no-consent state on the next call. Granting posts an affirmative intent,
|
||||
* which is the consent act itself and therefore may supersede an earlier
|
||||
* revocation; the silent mount heal never does. Nothing already stored is
|
||||
* deleted by a withdrawal; that is the export/erase path in Data & Privacy,
|
||||
* and the copy says so rather than implying more than it does.
|
||||
* revocation; the silent mount heal never does.
|
||||
*
|
||||
* v1.39 — a withdrawal also deletes what the app can write again: the notes
|
||||
* a model wrote from the person's data (status notes, the daily briefing,
|
||||
* model-written period summaries, arrival lines, workout notes), in the same
|
||||
* step as the revoke. The person's own records stay: Coach conversations,
|
||||
* what the Coach remembered, plans and document summaries, readable and
|
||||
* deletable by them. The confirm copy says exactly that.
|
||||
*
|
||||
* The narrower consent for reading documents (`ai_extraction`) gets its own
|
||||
* row once one is on file, with its own withdrawal. It is granted where a
|
||||
* document is read, so this is where it is taken back.
|
||||
* ──────────────────────────────────────────────────────────────── */
|
||||
|
||||
import { useState } from "react";
|
||||
@@ -69,10 +78,12 @@ export function AiConsentCard({
|
||||
onSuccess: async () => {
|
||||
setConfirming(false);
|
||||
// The receipt gates every AI surface, so anything that reads consent
|
||||
// state has to re-resolve — not just this card.
|
||||
// state has to re-resolve — not just this card. `/me` carries the
|
||||
// resolved capabilities every AI surface renders from.
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.aiConsentReceipt("ai_full"),
|
||||
});
|
||||
await queryClient.invalidateQueries({ queryKey: queryKeys.authMe() });
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.insightsProviderChain(),
|
||||
});
|
||||
@@ -96,6 +107,7 @@ export function AiConsentCard({
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.aiConsentReceipt("ai_full"),
|
||||
});
|
||||
await queryClient.invalidateQueries({ queryKey: queryKeys.authMe() });
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.insightsProviderChain(),
|
||||
});
|
||||
@@ -217,6 +229,119 @@ export function AiConsentCard({
|
||||
{t("settings.ai.consent.grantError")}
|
||||
</p>
|
||||
) : null}
|
||||
|
||||
<DocumentReadingConsentRow isAuthenticated={isAuthenticated} />
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The consent for reading documents (`ai_extraction`), shown only while one
|
||||
* is on file. Withdrawing it stops documents, lab report scans and medication
|
||||
* text from being sent for reading at once; it deletes nothing, because what
|
||||
* a read produced (a document summary, extracted values) is the person's own
|
||||
* record.
|
||||
*/
|
||||
function DocumentReadingConsentRow({
|
||||
isAuthenticated,
|
||||
}: {
|
||||
isAuthenticated: boolean;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const queryClient = useQueryClient();
|
||||
const [confirming, setConfirming] = useState(false);
|
||||
|
||||
const { data } = useQuery({
|
||||
queryKey: queryKeys.aiConsentReceipt("ai_extraction"),
|
||||
queryFn: async () => {
|
||||
const res = await apiFetchRaw(
|
||||
"/api/consent/ai/latest?kind=ai_extraction",
|
||||
);
|
||||
if (!res.ok) return null;
|
||||
const json = await res.json();
|
||||
return (json.data?.receipt ?? null) as ConsentReceiptWire;
|
||||
},
|
||||
enabled: isAuthenticated,
|
||||
});
|
||||
|
||||
const revoke = useMutation({
|
||||
mutationKey: queryKeys.aiConsentReceipt("ai_extraction"),
|
||||
mutationFn: async () => {
|
||||
const res = await apiFetchRaw(
|
||||
"/api/consent/ai/latest?kind=ai_extraction",
|
||||
{ method: "DELETE" },
|
||||
);
|
||||
if (!res.ok) throw new Error("revoke failed");
|
||||
},
|
||||
onSuccess: async () => {
|
||||
setConfirming(false);
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.aiConsentReceipt("ai_extraction"),
|
||||
});
|
||||
await queryClient.invalidateQueries({ queryKey: queryKeys.authMe() });
|
||||
},
|
||||
});
|
||||
|
||||
const active = data && data.revokedAt === null ? data : null;
|
||||
if (!active) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
data-slot="ai-consent-document-reading"
|
||||
className="border-border space-y-2 border-t pt-3"
|
||||
>
|
||||
<p className="text-sm font-medium">
|
||||
{t("settings.ai.consent.documentReading.title")}
|
||||
</p>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
{t("settings.ai.consent.documentReading.activeSince", {
|
||||
date: formatDateTime(active.signedAt),
|
||||
})}
|
||||
</p>
|
||||
{confirming ? (
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<p className="text-muted-foreground w-full text-xs">
|
||||
{t("settings.ai.consent.documentReading.confirmBody")}
|
||||
</p>
|
||||
<Button
|
||||
type="button"
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
data-slot="ai-consent-document-reading-withdraw-confirm"
|
||||
onClick={() => revoke.mutate()}
|
||||
disabled={revoke.isPending}
|
||||
>
|
||||
{revoke.isPending ? (
|
||||
<Loader2 className="size-4 animate-spin motion-reduce:animate-none" />
|
||||
) : null}
|
||||
{t("settings.ai.consent.confirmWithdraw")}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setConfirming(false)}
|
||||
disabled={revoke.isPending}
|
||||
>
|
||||
{t("common.cancel")}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
data-slot="ai-consent-document-reading-withdraw"
|
||||
onClick={() => setConfirming(true)}
|
||||
>
|
||||
{t("settings.ai.consent.withdraw")}
|
||||
</Button>
|
||||
)}
|
||||
{revoke.isError ? (
|
||||
<p className="text-destructive text-xs" role="alert">
|
||||
{t("settings.ai.consent.error")}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import { ShieldAlert } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { apiGet, apiPatch } from "@/lib/api/api-fetch";
|
||||
import { useAiCapability } from "@/hooks/use-ai-capability";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
@@ -31,6 +32,12 @@ interface AutoReadPref {
|
||||
export function AutoReadCard() {
|
||||
const { t } = useTranslations();
|
||||
const queryClient = useQueryClient();
|
||||
// With the operator's "Reading documents" switch (or all AI) off, nothing
|
||||
// would be read whatever this says, so the switch is shown disabled with
|
||||
// the reason instead of a control that silently does nothing. Turning an
|
||||
// already-on preference off stays possible.
|
||||
const documentAi = useAiCapability("documentAi");
|
||||
const operatorOff = documentAi.reason === "operator_disabled";
|
||||
|
||||
// Whether the honesty confirm is currently shown (user flipped the switch ON
|
||||
// but has not yet acknowledged the trade). Off→on reveals it; the write only
|
||||
@@ -54,10 +61,12 @@ export function AutoReadCard() {
|
||||
queryClient.setQueryData(queryKeys.documentsAutoAiRead(), result);
|
||||
// Flipping the toggle changes whether an ambient/per-document read
|
||||
// egresses without a per-document consent step — refresh the capability
|
||||
// probe the vault UI reads.
|
||||
// probe the vault UI reads, and `/me`, whose `documentAi` capability
|
||||
// reflects the consent the toggle records.
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.inboundDocumentAiCapability(),
|
||||
});
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.authMe() });
|
||||
setPendingEnable(false);
|
||||
},
|
||||
});
|
||||
@@ -92,13 +101,22 @@ export function AutoReadCard() {
|
||||
</div>
|
||||
<Switch
|
||||
checked={checked}
|
||||
disabled={busy}
|
||||
disabled={busy || (operatorOff && !enabled)}
|
||||
onCheckedChange={onSwitch}
|
||||
aria-label={t("settings.ai.autoRead.title")}
|
||||
data-testid="documents-auto-read-enable"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{operatorOff ? (
|
||||
<p
|
||||
data-slot="documents-auto-read-operator-off"
|
||||
className="text-muted-foreground text-xs"
|
||||
>
|
||||
{t("settings.ai.operatorOff.autoRead")}
|
||||
</p>
|
||||
) : null}
|
||||
|
||||
{pendingEnable ? (
|
||||
<div
|
||||
data-slot="documents-auto-read-confirm"
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
"use client";
|
||||
|
||||
import { PowerOff } from "lucide-react";
|
||||
|
||||
import { SettingsCard } from "@/components/settings/settings-card";
|
||||
import { SettingsCardHeader } from "@/components/settings/_card-header";
|
||||
import { useAiCapabilityMap } from "@/hooks/use-ai-capability";
|
||||
import {
|
||||
AI_CAPABILITIES,
|
||||
AI_CAPABILITY_KEYS,
|
||||
AI_OPERATOR_SWITCHES,
|
||||
type AiOperatorSwitch,
|
||||
} from "@/lib/ai/capabilities/types";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
/**
|
||||
* Says, in Settings → AI, what the person running this server has switched
|
||||
* off, so the settings below never read as broken.
|
||||
*
|
||||
* Read from the resolved capabilities on `/api/auth/me`: a capability whose
|
||||
* reason is `operator_disabled` names the operator switch that covers it, and
|
||||
* the notice lists those switches by the names the admin panel uses. With
|
||||
* every switch off (the master switch) it says AI is off on this server. With
|
||||
* none off it renders nothing. It never offers a control: nothing on this
|
||||
* screen can change an operator's switch.
|
||||
*/
|
||||
export function OperatorOffNotice() {
|
||||
const { t } = useTranslations();
|
||||
const capabilities = useAiCapabilityMap();
|
||||
if (!capabilities) return null;
|
||||
|
||||
const off = new Set<AiOperatorSwitch>();
|
||||
for (const key of AI_CAPABILITY_KEYS) {
|
||||
if (capabilities[key]?.reason === "operator_disabled") {
|
||||
off.add(AI_CAPABILITIES[key].operatorSwitch);
|
||||
}
|
||||
}
|
||||
if (off.size === 0) return null;
|
||||
|
||||
const allOff = AI_OPERATOR_SWITCHES.every((s) => off.has(s));
|
||||
const names = AI_OPERATOR_SWITCHES.filter((s) => off.has(s)).map((s) =>
|
||||
t(`admin.assistant.${s}.title`),
|
||||
);
|
||||
|
||||
return (
|
||||
<SettingsCard data-slot="ai-operator-off-notice">
|
||||
<SettingsCardHeader
|
||||
icon={PowerOff}
|
||||
title={
|
||||
allOff
|
||||
? t("settings.ai.operatorOff.allTitle")
|
||||
: t("settings.ai.operatorOff.someTitle")
|
||||
}
|
||||
/>
|
||||
<p className="text-foreground text-sm">
|
||||
{allOff
|
||||
? t("settings.ai.operatorOff.allBody")
|
||||
: t("settings.ai.operatorOff.someBody", {
|
||||
switches: names.join(", "),
|
||||
})}
|
||||
</p>
|
||||
</SettingsCard>
|
||||
);
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import { Switch } from "@/components/ui/switch";
|
||||
import { SettingsCardActions } from "@/components/settings/_card-actions";
|
||||
import { apiFetchRaw, apiPut } from "@/lib/api/api-fetch";
|
||||
import { formatDateTime } from "@/lib/format";
|
||||
import { useAiCapability } from "@/hooks/use-ai-capability";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
import {
|
||||
@@ -39,6 +40,12 @@ export function RuntimeActionsRow({
|
||||
onPrivacyChanged: () => void;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
// "Regenerate" writes the daily briefing, so it is offered only while the
|
||||
// `briefing` capability is available (operator switch, AI analysis opt-out,
|
||||
// provider, consent). The connection test stays: it is how a provider gets
|
||||
// checked before anything else can be on.
|
||||
const briefing = useAiCapability("briefing");
|
||||
const showRegenerate = canRegenerate && briefing.available;
|
||||
|
||||
const [testing, setTesting] = useState(false);
|
||||
const [testMsg, setTestMsg] = useState<string | null>(null);
|
||||
@@ -80,13 +87,18 @@ export function RuntimeActionsRow({
|
||||
setTestMsg(t("settings.ai.testUnexpectedResponse"));
|
||||
return;
|
||||
}
|
||||
// 4xx config errors still arrive via the error envelope.
|
||||
// 4xx config errors still arrive via the error envelope. A refusal
|
||||
// because the operator switched AI off says so in words.
|
||||
if (!res.ok) {
|
||||
setTestOk(false);
|
||||
const code = (json as { meta?: { errorCode?: unknown } | null }).meta
|
||||
?.errorCode;
|
||||
setTestMsg(
|
||||
t("settings.ai.testFailedShort", {
|
||||
message: json.error ?? `HTTP ${res.status}`,
|
||||
}),
|
||||
typeof code === "string" && code.startsWith("assistant.disabled.")
|
||||
? t("settings.ai.operatorOff.testRefused")
|
||||
: t("settings.ai.testFailedShort", {
|
||||
message: json.error ?? `HTTP ${res.status}`,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -204,7 +216,7 @@ export function RuntimeActionsRow({
|
||||
// setting and every status line (last-generated, test / regenerate
|
||||
// result) sit ABOVE it, never under it.
|
||||
<div className="space-y-3">
|
||||
{canRegenerate && (
|
||||
{showRegenerate && (
|
||||
<div className="bg-muted/50 rounded-lg p-3">
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<div className="pr-2">
|
||||
@@ -264,7 +276,7 @@ export function RuntimeActionsRow({
|
||||
)}
|
||||
{t("settings.ai.testProvider")}
|
||||
</Button>
|
||||
{canRegenerate && (
|
||||
{showRegenerate && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
|
||||
Reference in new issue
Block a user