Merge branch 'feat/ai-web-surfaces' into release/v1.39.0

This commit is contained in:
Marc-André Bombeck committed 2026-09-24 11:37:10 +02:00
commit e0ece41e21
120 files changed
+3851 -2027

No files matched your search

@@ -44,6 +44,23 @@ vi.mock("@/hooks/use-auth", () => ({
}),
}));
// The AI capability map on `/api/auth/me`: everything available, a provider
// configured, nothing switched off by the operator. The notices and the
// setup hint above the provider card have their own suites.
vi.mock("@/hooks/use-ai-capability", () => ({
useAiCapability: () => ({
available: true,
reason: null,
onDeviceAllowed: true,
}),
useAiCapabilityMap: () => null,
useAiProviderState: () => ({
configured: true,
managedBy: "user",
canConfigure: true,
}),
}));
// Stable mocks for every TanStack Query the section reads. The chain
// fixture mirrors the default order (codex first) so the rows render in
// a known order; insights settings + ai-provider GET return enough to
@@ -0,0 +1,144 @@
/**
* What the Coach stored stays readable and deletable while the Coach is off.
*
* Coach conversations and the facts the Coach remembered are the person's
* own records, not caches. Before v1.39 Settings → Coach hid the memory card
* together with the Coach's tuning as soon as the Coach was hidden, and with
* the Coach module off the whole page was out of reach, so a person could
* neither see nor erase what had been stored. Now:
*
* - Settings → Coach always shows the memory (facts and conversations),
* whatever `disableCoach` says;
* - Settings → AI shows the same cards through `<StoredCoachMemory>` while
* Settings → Coach is not reachable, and only when rows exist.
*/
import { renderToStaticMarkup } from "react-dom/server";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const authState = vi.hoisted(() => ({ disableCoach: true }));
vi.mock("@/hooks/use-auth", () => ({
useAuth: () => ({
user: {
id: "u1",
role: "USER",
timezone: "Europe/Berlin",
disableCoach: authState.disableCoach,
},
isAuthenticated: true,
isLoading: false,
refetch: vi.fn(),
}),
}));
// Past hydration: the section's own gates read the account only once
// mounted, and a static render is otherwise the pre-hydration pass, where
// every card shows and the test could not tell a gate from its absence.
vi.mock("@/hooks/use-mounted", () => ({ useMounted: () => true }));
vi.mock("next/navigation", () => ({
useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
usePathname: () => "/settings/coach",
}));
import { I18nProvider } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { CoachSection } from "../coach-section";
import { StoredCoachMemory } from "../coach-memory-section";
const FACT = {
id: "fact-1",
category: "goal",
text: "Wants to walk to work twice a week",
confidence: 0.9,
createdAt: "2026-09-01T08:00:00.000Z",
};
const CONVERSATION = {
id: "conv-1",
title: "Morning blood pressure",
createdAt: "2026-09-01T08:00:00.000Z",
updatedAt: "2026-09-01T08:05:00.000Z",
messageCount: 4,
fenced: false,
attachments: [],
};
function client(stored: { facts: unknown[]; conversations: unknown[] }) {
const c = new QueryClient({
defaultOptions: { queries: { retry: 0, staleTime: Infinity } },
});
c.setQueryData(queryKeys.coachFacts(), stored.facts);
c.setQueryData(queryKeys.coachConversationHistory(""), {
pages: [{ conversations: stored.conversations, nextCursor: null }],
pageParams: [null],
});
return c;
}
function render(
node: React.ReactNode,
stored: { facts: unknown[]; conversations: unknown[] },
) {
return renderToStaticMarkup(
<QueryClientProvider client={client(stored)}>
<I18nProvider initialLocale="en">{node}</I18nProvider>
</QueryClientProvider>,
);
}
beforeEach(() => {
// Everything is seeded; a background refetch must not reach the network.
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response("{}", { status: 500 })),
);
});
afterEach(() => vi.unstubAllGlobals());
describe("Settings → Coach keeps the Coach memory while the Coach is hidden", () => {
it("shows the facts and the conversations with Hide Coach on", () => {
authState.disableCoach = true;
const html = render(<CoachSection />, {
facts: [FACT],
conversations: [CONVERSATION],
});
expect(html).toContain('data-testid="settings-coach-memory-card"');
expect(html).toContain("Wants to walk to work twice a week");
expect(html).toContain('data-testid="settings-coach-conversations-card"');
expect(html).toContain("Morning blood pressure");
// Deletable: the per-row controls are there.
expect(html).toContain('data-slot="settings-coach-memory-forget"');
expect(html).toContain('data-slot="settings-coach-conversation-delete"');
// The Coach's tuning stays hidden with the Coach.
expect(html).not.toContain('id="coach-nudge"');
});
});
describe("<StoredCoachMemory> (Settings → AI while the Coach page is out of reach)", () => {
it("shows what is stored, readable and deletable", () => {
const html = render(<StoredCoachMemory isAuthenticated />, {
facts: [FACT],
conversations: [CONVERSATION],
});
expect(html).toContain('data-testid="settings-coach-memory-card"');
expect(html).toContain('data-testid="settings-coach-conversations-card"');
expect(html).toContain('data-slot="settings-coach-conversation-open"');
expect(html).toContain('data-slot="settings-coach-conversation-delete"');
});
it("shows only the card that has rows", () => {
const html = render(<StoredCoachMemory isAuthenticated />, {
facts: [],
conversations: [CONVERSATION],
});
expect(html).not.toContain('data-testid="settings-coach-memory-card"');
expect(html).toContain('data-testid="settings-coach-conversations-card"');
});
it("renders nothing for somebody the Coach never stored anything for", () => {
const html = render(<StoredCoachMemory isAuthenticated />, {
facts: [],
conversations: [],
});
expect(html).toBe("");
});
});
@@ -16,6 +16,7 @@ vi.mock("next/navigation", () => ({
vi.mock("@tanstack/react-query", () => ({
useQuery: () => ({ data: null, isLoading: false, refetch: vi.fn() }),
useInfiniteQuery: () => ({ data: undefined, isLoading: false }),
useMutation: () => ({ mutate: vi.fn(), isPending: false }),
useQueryClient: () => ({ invalidateQueries: vi.fn() }),
}));
@@ -13,8 +13,13 @@ vi.mock("@/hooks/use-auth", () => ({
user: { modules: modulesRef.value },
}),
}));
vi.mock("@/hooks/use-feature-flags", () => ({
useFeatureFlags: () => ({ briefing: true }),
const briefingRef: { available: boolean } = { available: true };
vi.mock("@/hooks/use-ai-capability", () => ({
useAiCapability: () => ({
available: briefingRef.available,
reason: briefingRef.available ? null : "user_disabled",
onDeviceAllowed: false,
}),
}));
vi.mock("@/hooks/use-insights-layout", () => ({
useInsightsLayoutQuery: () => ({
@@ -63,4 +68,12 @@ describe("<InsightsOverviewArrangeSection> module gate", () => {
it("marks nothing for AI analysis off", () => {
expect(gatedFor({ insights: false })).toEqual([]);
});
it("marks only the daily briefing when the briefing is unavailable", () => {
// The period review renders its composed narrative whatever the AI
// state, so it stays a live row.
briefingRef.available = false;
expect(gatedFor({})).toEqual(["daily-briefing"]);
briefingRef.available = true;
});
});
@@ -63,21 +63,18 @@ vi.mock("@/hooks/use-auth", async () => {
return { ...actual, useAuth: () => authSpy() };
});
// The hub now renders a live coach switch, so it reads the operator assistant
// flag matrix (`flags.coach`) to decide whether the operator killed Coach
// server-wide. Mock the hook directly so the SSR pass doesn't need a
// `<QueryClientProvider>`; default all-on.
import type { AssistantFlagSet } from "@/hooks/use-feature-flags";
const ALL_ON_FLAGS: AssistantFlagSet = {
enabled: true,
coach: true,
briefing: true,
insightStatus: true,
documentAi: true,
// The hub renders a live coach switch and reads the `coach` AI capability to
// decide whether the operator switched the Coach off server-wide. Mock the
// hook directly so the SSR pass doesn't need a `<QueryClientProvider>`.
import type { AiCapabilityState } from "@/lib/ai/capabilities/types";
const COACH_AVAILABLE: AiCapabilityState = {
available: true,
reason: null,
onDeviceAllowed: true,
};
const flagsSpy = vi.fn<() => AssistantFlagSet>(() => ALL_ON_FLAGS);
vi.mock("@/hooks/use-feature-flags", () => ({
useFeatureFlags: () => flagsSpy(),
const coachSpy = vi.fn<() => AiCapabilityState>(() => COACH_AVAILABLE);
vi.mock("@/hooks/use-ai-capability", () => ({
useAiCapability: () => coachSpy(),
}));
function buildUser(
@@ -133,7 +130,7 @@ beforeEach(() => {
user: buildUser({}),
isAuthenticated: true,
}));
flagsSpy.mockImplementation(() => ALL_ON_FLAGS);
coachSpy.mockImplementation(() => COACH_AVAILABLE);
});
afterEach(() => {
@@ -295,7 +292,11 @@ describe("<ModulesSection>", () => {
it("disables the switch + shows a hint when the operator killed Coach", () => {
// Operator assistant master flag off → coach is unavailable server-wide.
flagsSpy.mockImplementation(() => ({ ...ALL_ON_FLAGS, coach: false }));
coachSpy.mockImplementation(() => ({
available: false,
reason: "operator_disabled",
onDeviceAllowed: false,
}));
const html = render();
const coachTag = html.match(
/<button[^>]*id="module-toggle-coach"[^>]*>/,
@@ -45,6 +45,22 @@ vi.mock("@tanstack/react-query", () => ({
useMutation: () => ({ mutate: vi.fn(), isPending: false }),
}));
// The AI capability map on `/api/auth/me`: everything available, a provider
// configured, nothing switched off by the operator.
vi.mock("@/hooks/use-ai-capability", () => ({
useAiCapability: () => ({
available: true,
reason: null,
onDeviceAllowed: true,
}),
useAiCapabilityMap: () => null,
useAiProviderState: () => ({
configured: true,
managedBy: "user",
canConfigure: true,
}),
}));
// Stub the auth hook — sections check `isAuthenticated` to enable queries.
// v1.16.4 — AccountSection gates its content on `useMounted()` so the
// SSR pass and the hydration render agree (React #418 family). The
+24 -2
View File
@@ -19,11 +19,15 @@
*/
import { AiInsightsCard } from "@/components/settings/ai/ai-insights-card";
import { OperatorOffNotice } from "@/components/settings/ai/operator-off-notice";
import { StoredCoachMemory } from "@/components/settings/coach-memory-section";
import { AiSetupHint } from "@/components/insights/ai-setup-hint";
import { useAuth } from "@/hooks/use-auth";
import { useMounted } from "@/hooks/use-mounted";
import { isSurfaceVisible } from "@/lib/modules/surface";
export function AiSection() {
const { isAuthenticated } = useAuth();
const { isAuthenticated, user } = useAuth();
// v1.16.6 — the auth query can resolve before this boundary
// hydrates; every `disabled={!isAuthenticated}` binding in the child
// cards would then disagree with the SSR HTML (React #418). Gate the
@@ -36,5 +40,23 @@ export function AiSection() {
// v1.18.6 (W9) — the visible heading + subtitle now come from the shared
// `<SettingsSectionFrame>` in the route; this body is the single provider
// card. (The "About me" context moved to Settings → Account in v1.18.1 D8.)
return <AiInsightsCard isAuthenticated={authed} />;
//
// v1.39 — above the provider card: what the operator has switched off (so
// nothing below reads as broken), and the one calm setup hint while no
// provider is configured. Below it: what the Coach stored, while Settings →
// Coach is not reachable, so it can always be read and deleted.
const coachSettingsReachable = isSurfaceVisible(
"settings:coach",
user?.modules,
);
return (
<div className="space-y-6">
<OperatorOffNotice />
<AiSetupHint surface="settings" />
<AiInsightsCard isAuthenticated={authed} />
{authed && !coachSettingsReachable ? (
<StoredCoachMemory isAuthenticated={authed} />
) : null}
</div>
);
}
@@ -75,6 +75,35 @@ describe("AiConsentCard", () => {
expect(html).toContain('data-slot="ai-consent-grant"');
});
it("never reports a failed read as a withdrawal", () => {
// A refused or failed read (the consent bucket's rate limit, a network
// drop) says nothing about the decision. Reporting it as "Withdrawn" and
// offering a grant would misstate the person's own choice.
const client = new QueryClient({
defaultOptions: { queries: { retry: false, retryOnMount: false } },
});
client
.getQueryCache()
.build(client, { queryKey: receiptKey })
.setState({
status: "error",
error: new Error("read failed"),
data: undefined,
fetchStatus: "idle",
});
const html = renderToStaticMarkup(
<QueryClientProvider client={client}>
<I18nProvider initialLocale="en">
<AiConsentCard isAuthenticated />
</I18nProvider>
</QueryClientProvider>,
);
expect(html).not.toContain("Withdrawn.");
expect(html).not.toContain('data-slot="ai-consent-grant"');
expect(html).not.toContain('data-slot="ai-consent-withdraw"');
expect(html).toContain('data-slot="ai-consent-load-error"');
});
it("never shows the grant control while consent stands", () => {
const html = renderWith({
id: "rcpt-1",
+145 -6
View File
@@ -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";
@@ -26,6 +35,7 @@ import { Loader2, ShieldCheck, ShieldOff } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Skeleton } from "@/components/ui/skeleton";
import { QueryErrorRow } from "@/components/ui/query-error-row";
import { apiFetchRaw } from "@/lib/api/api-fetch";
import { formatDateTime } from "@/lib/format";
import { useTranslations } from "@/lib/i18n/context";
@@ -47,11 +57,13 @@ export function AiConsentCard({
const queryClient = useQueryClient();
const [confirming, setConfirming] = useState(false);
const { data, isLoading } = useQuery({
const { data, isLoading, isError, refetch } = useQuery({
queryKey: queryKeys.aiConsentReceipt("ai_full"),
queryFn: async () => {
const res = await apiFetchRaw("/api/consent/ai/latest?kind=ai_full");
if (!res.ok) return null;
// A failed read is not a withdrawal: throw so the card says it could
// not load instead of reporting the person's decision wrongly.
if (!res.ok) throw new Error("consent read failed");
const json = await res.json();
return (json.data?.receipt ?? null) as ConsentReceiptWire;
},
@@ -69,10 +81,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 +110,7 @@ export function AiConsentCard({
await queryClient.invalidateQueries({
queryKey: queryKeys.aiConsentReceipt("ai_full"),
});
await queryClient.invalidateQueries({ queryKey: queryKeys.authMe() });
await queryClient.invalidateQueries({
queryKey: queryKeys.insightsProviderChain(),
});
@@ -121,6 +136,17 @@ export function AiConsentCard({
);
}
if (isError) {
return (
<section className="space-y-3" data-slot="ai-consent">
<QueryErrorRow
slot="ai-consent-load-error"
onRetry={() => void refetch()}
/>
</section>
);
}
// Narrow to the receipt itself rather than a boolean, so the branches
// below can read its fields without an assertion.
const activeReceipt = data && data.revokedAt === null ? data : null;
@@ -217,6 +243,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>
);
}
+20 -2
View File
@@ -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"
@@ -44,6 +44,11 @@ import { HealthProfileFactsManager } from "@/components/records/health-profile-f
export function AnamnesisSection() {
const { t } = useTranslations();
// The inclusion card and the about-me note only feed AI surfaces, so they
// follow the person's two AI opt-outs: the Coach module and "AI analysis"
// (the `insights` module, which since v1.39 governs model-written text and
// nothing else). They are opt-outs, not capabilities, on purpose: somebody
// without a provider yet can still write the note the AI will read later.
const coachEnabled = useModuleEnabled("coach");
const insightsEnabled = useModuleEnabled("insights");
return (
@@ -0,0 +1,138 @@
"use client";
import { useMemo, useState } from "react";
import { MessagesSquare, Trash2 } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { QueryErrorRow } from "@/components/ui/query-error-row";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { ConversationTranscript } from "@/components/insights/coach-panel/conversation-transcript";
import {
useCoachConversationHistory,
useDeleteCoachConversationWithUndo,
} from "@/components/insights/coach-panel/use-coach";
import { useTranslations } from "@/lib/i18n/context";
import { formatRelativeTime } from "@/lib/i18n/relative-time";
/**
* The Coach's stored conversations, readable and deletable from Settings.
*
* A conversation is the person's own record, not a cache, so it stays
* reachable whatever the Coach's state: switched off by the operator, hidden
* by the person, without a provider or without consent. Each row opens its
* transcript in place and can be deleted (with the same undo window the
* conversation list uses). Reading and deleting never ask the Coach; the
* routes behind them are data routes.
*
* `hideWhenEmpty`: the card renders nothing when there is nothing stored.
* Settings → AI mounts it that way while the Coach is off, so it appears only
* when there is something to see.
*/
export function CoachConversationsMemoryCard({
isAuthenticated,
hideWhenEmpty = false,
}: {
isAuthenticated: boolean;
hideWhenEmpty?: boolean;
}) {
const { t, locale } = useTranslations();
const [openId, setOpenId] = useState<string | null>(null);
const history = useCoachConversationHistory({ enabled: isAuthenticated });
const { pendingDeleteIds, requestDelete, undoDelete } =
useDeleteCoachConversationWithUndo();
const visible = useMemo(
() => history.conversations.filter((c) => !pendingDeleteIds.has(c.id)),
[history.conversations, pendingDeleteIds],
);
if (hideWhenEmpty && (history.isLoading || history.isError)) return null;
if (hideWhenEmpty && visible.length === 0) return null;
function remove(id: string) {
requestDelete(id);
if (openId === id) setOpenId(null);
toast.success(t("insights.coach.historyDeleted"), {
action: { label: t("common.undo"), onClick: () => undoDelete(id) },
});
}
return (
<SettingsCard
as="section"
aria-labelledby="settings-coach-conversations-title"
data-testid="settings-coach-conversations-card"
>
<SettingsCardHeader
icon={MessagesSquare}
titleId="settings-coach-conversations-title"
title={t("settings.ai.coachConversations.title")}
description={t("settings.ai.coachConversations.description")}
/>
{history.isError ? (
<QueryErrorRow onRetry={() => void history.refetch()} />
) : visible.length === 0 ? (
<p className="text-muted-foreground text-sm">
{history.isLoading
? t("common.loading")
: t("insights.coach.historyEmpty")}
</p>
) : (
<ul className="space-y-2">
{visible.map((c) => (
<li
key={c.id}
data-testid="settings-coach-conversation"
className="border-border bg-background rounded-lg border"
>
<div className="flex items-center gap-2 p-1.5 pl-3">
<button
type="button"
onClick={() =>
setOpenId((current) => (current === c.id ? null : c.id))
}
aria-expanded={openId === c.id}
data-slot="settings-coach-conversation-open"
className="flex min-h-11 min-w-0 flex-1 flex-col justify-center text-left"
>
<span className="truncate text-sm font-medium">
{c.title}
</span>
<span className="text-muted-foreground text-xs">
{formatRelativeTime(c.updatedAt, t, locale)}
</span>
</button>
<Button
type="button"
variant="ghost"
size="icon-lg"
onClick={() => remove(c.id)}
aria-label={t("insights.coach.historyDeleteAria")}
data-slot="settings-coach-conversation-delete"
className="shrink-0"
>
<Trash2 className="size-4" aria-hidden="true" />
</Button>
</div>
{openId === c.id ? <ConversationTranscript id={c.id} /> : null}
</li>
))}
</ul>
)}
{history.hasNextPage ? (
<Button
type="button"
variant="outline"
size="sm"
className="min-h-11 self-start sm:min-h-9"
disabled={history.isFetchingNextPage}
onClick={() => void history.fetchNextPage()}
>
{t("settings.ai.coachConversations.more")}
</Button>
) : null}
</SettingsCard>
);
}
@@ -14,9 +14,14 @@
*
* Reads unwrap `(await res.json()).data` per the envelope convention;
* every read/write routes its key through `queryKeys.coachFacts()` so
* a forget invalidates the list. Gated on `!user.disableCoach` so the
* panel mirrors the rest of the Coach surface — hiding the Coach hides
* its memory controls too.
* a forget invalidates the list.
*
* v1.39 — never gated on the Coach. What the Coach stored is the person's
* own record: it stays readable and deletable while the Coach is off for any
* reason (the operator's switch, Hide Coach, no provider, no consent). The
* facts routes are data routes and ask no AI gate. Settings → Coach always
* shows this card; while that page is not reachable, Settings → AI shows it
* through `<StoredCoachMemory>` below, whenever rows exist.
*/
import { useMemo } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
@@ -45,6 +50,7 @@ import { useTranslations } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { toast } from "sonner";
import { apiDelete, apiGet } from "@/lib/api/api-fetch";
import { CoachConversationsMemoryCard } from "@/components/settings/coach-conversations-memory-card";
/** Closed enum mirrored from the server `CoachFact.category` column. */
const FACT_CATEGORIES = [
@@ -81,8 +87,11 @@ async function fetchFacts(): Promise<CoachFact[]> {
export function CoachMemorySection({
isAuthenticated,
hideWhenEmpty = false,
}: {
isAuthenticated: boolean;
/** Render nothing while there are no stored facts (the Coach-off mount). */
hideWhenEmpty?: boolean;
}) {
const { t } = useTranslations();
const queryClient = useQueryClient();
@@ -138,6 +147,9 @@ export function CoachMemorySection({
})).filter((g) => g.items.length > 0);
}, [facts]);
if (hideWhenEmpty && (query.isPending || query.isError)) return null;
if (hideWhenEmpty && facts.length === 0) return null;
return (
<SettingsCard
as="section"
@@ -279,3 +291,25 @@ export function CoachMemorySection({
</SettingsCard>
);
}
/**
* Everything the Coach stored, for Settings → AI while Settings → Coach is not
* reachable (the Coach switched off by the operator or hidden by the person).
* Each card renders only when it has rows, so a person who never used the
* Coach sees nothing here.
*/
export function StoredCoachMemory({
isAuthenticated,
}: {
isAuthenticated: boolean;
}) {
return (
<>
<CoachMemorySection isAuthenticated={isAuthenticated} hideWhenEmpty />
<CoachConversationsMemoryCard
isAuthenticated={isAuthenticated}
hideWhenEmpty
/>
</>
);
}
+9 -4
View File
@@ -16,9 +16,11 @@
* notification).
* 4. Coach memory — durable-fact review + forget controls.
*
* The nav entry is module-gated on `coach`. The preference + nudge + memory
* cards keep their own content gate on `!user.disableCoach` (mirrored from the
* old AI section): hiding the Coach hides its tuning + memory controls too.
* The nav entry is module-gated on `coach`. The preference + nudge +
* reminder cards keep their own content gate on `!user.disableCoach`: hiding
* the Coach hides its tuning. The memory (facts and stored conversations) is
* never gated: it is the person's own record, readable and deletable whatever
* the Coach's state.
*
* The "about me" context lives in the AI section, not here — the daily
* briefing reads it too, so it is not a Coach-only setting.
@@ -29,6 +31,7 @@
import { DisableCoachCard } from "@/components/settings/ai/disable-coach-card";
import { CoachMemorySection } from "@/components/settings/coach-memory-section";
import { CoachConversationsMemoryCard } from "@/components/settings/coach-conversations-memory-card";
import { CoachRemindersSection } from "@/components/settings/coach-reminders-section";
import { CoachNudgeCard } from "@/components/settings/coach-nudge-card";
import { CoachPrefsSection } from "@/components/settings/coach-prefs-section";
@@ -62,7 +65,9 @@ export function CoachSection() {
</div>
)}
{coachEnabled && <CoachMemorySection isAuthenticated={authed} />}
<CoachMemorySection isAuthenticated={authed} />
<CoachConversationsMemoryCard isAuthenticated={authed} />
{coachEnabled && <CoachRemindersSection isAuthenticated={authed} />}
</div>
@@ -6,7 +6,7 @@ import { LayoutGrid, Loader2 } from "lucide-react";
import { useTranslations } from "@/lib/i18n/context";
import { useAuth } from "@/hooks/use-auth";
import { useFeatureFlags } from "@/hooks/use-feature-flags";
import { useAiCapability } from "@/hooks/use-ai-capability";
import { useInsightsLayoutQuery } from "@/hooks/use-insights-layout";
import { InsightsEditMode } from "@/components/insights/insights-edit-mode";
import { SectionHeading } from "@/components/ui/section-heading";
@@ -35,15 +35,15 @@ import { isSurfaceVisible } from "@/lib/modules/surface";
export function InsightsOverviewArrangeSection({ id }: { id?: string }) {
const { t } = useTranslations();
const { isAuthenticated, user } = useAuth();
const flags = useFeatureFlags();
// The daily briefing is model-written: its row is live only while the
// `briefing` capability is available. The period review always renders
// (its narrative is composed from the numbers), so it stays a live row.
const briefing = useAiCapability("briefing");
const { layout, isLoading } = useInsightsLayoutQuery(isAuthenticated);
const gatedOffSectionIds = useMemo(() => {
const gated = new Set<InsightsSectionId>();
if (!flags.briefing) {
gated.add("daily-briefing");
gated.add("period-review");
}
if (!briefing.available) gated.add("daily-briefing");
// A block whose module is off cannot appear on the overview, so its row
// is not offered as a live toggle. Read from the one surface map the
// overview renders from (`overview:<id>`); cycle follows the resolved
@@ -52,7 +52,7 @@ export function InsightsOverviewArrangeSection({ id }: { id?: string }) {
if (!isSurfaceVisible(`overview:${id}`, user?.modules)) gated.add(id);
}
return gated;
}, [flags.briefing, user?.modules]);
}, [briefing.available, user?.modules]);
return (
<section
+9 -9
View File
@@ -18,8 +18,8 @@
* off `useAuth().user.modules`.
*
* Operator precedence stays honest: a module the operator turned off
* server-wide (the module-availability blob, plus the assistant master flag
* `flags.coach` for the coach row) renders a disabled switch + a
* server-wide (the module-availability blob, or for the coach row the
* operator's Coach switch as the `coach` capability reports it) renders a disabled switch + a
* "disabled server-wide" hint — a per-user toggle could not re-enable it.
*
* The three CORE domains (weight, blood pressure, pulse) render as a
@@ -63,7 +63,7 @@ import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { ModuleToggleRow } from "@/components/settings/module-toggle-row";
import { useAuth } from "@/hooks/use-auth";
import { useFeatureFlags } from "@/hooks/use-feature-flags";
import { useAiCapability } from "@/hooks/use-ai-capability";
import { useTranslations } from "@/lib/i18n/context";
import { apiPatch } from "@/lib/api/api-fetch";
import {
@@ -118,7 +118,7 @@ const MODULE_ICONS: Record<ModuleKey, LucideIcon> = {
export function ModulesSection() {
const { t } = useTranslations();
const { user } = useAuth();
const flags = useFeatureFlags();
const coach = useAiCapability("coach");
const queryClient = useQueryClient();
const modules = user?.modules ?? {};
@@ -234,13 +234,13 @@ export function ModulesSection() {
// Operator precedence. A per-user toggle can never re-enable a
// module the operator turned off server-wide, so the switch goes
// disabled + hint. For coach the operator layer is BOTH the
// module-availability blob AND the assistant master flag
// (`flags.coach`, already master-composed); for cycle and the
// owned modules it is the module-availability blob alone.
// disabled + hint. For coach the operator layer is the
// operator's Coach switch, read from the resolved `coach`
// capability (`operator_disabled`, master applied); for cycle
// and the owned modules it is the module-availability blob.
const operatorAvailable =
delegate === "coach"
? moduleAvailability[key] !== false && flags.coach
? coach.reason !== "operator_disabled"
: moduleAvailability[key] !== false;
const disabledReason = operatorAvailable
? undefined