Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ NEXT_PUBLIC_WORKOS_REDIRECT_URI=http://localhost:3000/callback
# Two separate EU Sentry projects: backend and web. Empty disables monitoring.
SENTRY_DSN=
NEXT_PUBLIC_SENTRY_DSN=
# Wellbeing Coach uses the existing authenticated backend connection above.
# ANTHROPIC_API_KEY and COACH_* belong only to loadiq-api; never expose them here.
# E2E flags are set only by scripts/e2e-server.mjs. Never configure in deployment.
53 changes: 53 additions & 0 deletions COACH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Wellbeing Coach v0 dashboard

`WellbeingCoach` adds a daily brief and conversation to `/app` without redesigning the
existing dashboard. Its initial data is read server-side after the existing dashboard/insight
refresh, so no mount-time background server action races with navigation. A coach outage
is caught separately and cannot remove existing metrics; explicit actions can retry.
Coach reads have a five-second fetch deadline; chat has thirty seconds for the provider's
bounded request. Existing non-coach API behavior is unchanged.

`app/app/coach-actions.ts` uses WorkOS `currentUser()` and the existing server-only API
client. No browser user ID, external-auth header or provider key is trusted or forwarded.
Next server actions retain their origin protections. Responses use no-store backend fetches.

The UI displays the exact backend facts/trends, evidence, coverage/freshness, qualified
hypotheses, suggestions and one follow-up. Optional historical dates are bounded by the
backend. Garmin attribution accompanies derived output; manual context is not called a
measured activity. English matches the existing dashboard language.

Chat stays in component memory (maximum six exchanges) and can be cleared. It is not
persisted in localStorage, browser recordings or a transcript table. After Anthropic consent, up to four previous user questions and assistant summaries provide conversation context to Anthropic; saved raw notes are not sent. Explicitly confirmed notes are separately saved, reviewable and removable.

The AI transparency panel shows provider processing/retention and the absence of guaranteed
EU processing. Consent is unchecked by default, requires explicit confirmation and can be
revoked. Revocation also clears visible conversation. When provider configuration is missing,
AI enabling is unavailable and the UI honestly labels evidence-only mode. Failed requests
show a controlled retry state; no backend response payload is logged.

Only the existing backend URL/internal key and WorkOS settings belong here. Anthropic credentials
and operator review flags are backend-only. No real-provider or production configuration is
changed by adding this UI; the backend must have the additive coach migration applied.

Tests use fabricated Alice/Bob accounts and a local fixture server. `pnpm test:e2e` includes
brief/chat consistency, explicit AI consent/revocation, confirmed note lifecycle, stale data,
and identity-spoof rejection, plus existing dashboard/privacy tests. No real Anthropic/Garmin
calls, health recordings, traces, screenshots or video artifacts are required by CI.

Run `pnpm typecheck`, `pnpm quality`, `pnpm test:e2e`, then `pnpm build` (sequentially with
E2E because both use `.next`). Real WorkOS + real LLM verification requires a configured
backend and explicit provider-specific consent; synthetic fixtures are not proof of that flow.

Task 4.1: Anthropic consent explicitly covers questions, recent conversation summaries and summarized health
evidence being processed by Anthropic. Saved notes are not sent verbatim. Do not put identifiers,
locations or credentials into questions. Chat fetch deadline is thirty seconds for bounded retrieval
and synthesis. No automatic consent grant or authentication bypass is introduced.

Real-provider status (2026-11-16): backend Haiku 5.5 passed a three-turn synthetic Croatian
conversation. The displayed coverage label describes measurement coverage, not certainty
about causes. WorkOS-to-real-Garmin-to-LLM remains unverified and disabled pending separate
owner consent. Playwright uses mocked provider responses and is not a real-provider test.

## Task 4.3

Chat displays a concise conversational answer; full measurements and limitations remain expandable. Ephemeral previous assistant answers include follow-up questions for continuity. No transcript is persisted. Real WorkOS staging login is verified locally with development bypass off. UI consent remains owner-controlled and can be recorded while AI is paused. No real health questions are sent until separate explicit consent and usage approval. Backend evaluations include twenty synthetic Haiku/Sonnet pairs and Croatian multi-turn repeats.
67 changes: 67 additions & 0 deletions app/app/coach-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"use server";
import { currentUser } from "@/lib/current-user";
import { coachApiRequest } from "@/lib/loadiq-api";
import type {
CoachMessageInput,
CoachNote,
CoachReply,
CoachStatus,
} from "@/lib/coach-types";

async function safely<T>(operation: () => Promise<T>) {
await currentUser();
try {
return { ok: true as const, data: await operation() };
} catch {
return {
ok: false as const,
error:
"Coach unavailable. Please retry or reload to check the latest state.",
};
}
}
export async function loadCoach() {
return safely(async () => {
const [status, brief, notes] = await Promise.all([
coachApiRequest<CoachStatus>("/status"),
coachApiRequest<CoachReply>("/brief"),
coachApiRequest<CoachNote[]>("/context"),
]);
return { status, brief, notes };
});
}
export async function sendCoachMessage(input: CoachMessageInput) {
return safely(() =>
coachApiRequest<CoachReply>("/chat", "POST", {
message: input.message,
previousQuestions: input.previousQuestions,
previousAnswers: input.previousAnswers,
from: input.from,
to: input.to,
}),
);
}
export async function changeCoachConsent(
enabled: boolean,
policyVersion: string,
) {
return safely(() =>
coachApiRequest<CoachStatus>("/consent", "POST", {
enabled,
policyVersion,
}),
);
}
export async function saveCoachNote(text: string, confirmed: boolean) {
return safely(() =>
coachApiRequest<CoachNote>("/context", "POST", { text, confirmed }),
);
}
export async function removeCoachNote(id: string) {
return safely(() =>
coachApiRequest<{ removed: boolean }>(
`/context/${encodeURIComponent(id)}`,
"DELETE",
),
);
}
4 changes: 4 additions & 0 deletions components/product/dashboard-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
type ImportRecord,
} from "@/lib/loadiq-api";
import { TrendPanel } from "./trend-panel";
import { WellbeingCoach } from "./wellbeing-coach";
import { loadCoach } from "@/app/app/coach-actions";

export async function DashboardPage() {
const [{ user }, summary, timeline, recentActivity, recentImports] =
Expand All @@ -44,6 +46,7 @@ export async function DashboardPage() {
getRecentImports(5),
]);

const coach = await loadCoach();
const greetingName = user.firstName ?? user.email.split("@")[0];
const activeStates = summary.daily.states.filter(
(state) => state.status !== "RESOLVED",
Expand Down Expand Up @@ -80,6 +83,7 @@ export async function DashboardPage() {
</header>

<main className="mx-auto flex max-w-7xl flex-col gap-6 px-5 py-8 lg:px-8">
<WellbeingCoach initial={coach} />
<section className="grid gap-6 xl:grid-cols-[1.4fr_1fr]">
<Card className="overflow-hidden border-white/70 bg-[linear-gradient(135deg,#0F172A_0%,#11304A_55%,#0F8F63_100%)] text-white">
<CardHeader className="pb-4">
Expand Down
Loading
Loading