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
4 changes: 4 additions & 0 deletions COACH.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ owner consent. Playwright uses mocked provider responses and is not a real-provi
## 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.

## Task 4.4

Chat now labels AI-assisted versus evidence-only answers, displays the imported-day/coaching-signal distinction and keeps technical failure categories and raw engine enums collapsed with evidence. Croatian backend/provider failure paths explicitly say when the AI answer could not be completed. Five-turn browser regression uses fabricated deterministic backend output, not owner data or a real model. Consent/auth controls remain unchanged.
16 changes: 16 additions & 0 deletions QUALITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ and loopback host. Production builds/servers reject the flag and production auth
Local Next4318 and fixture backend4319 use only fabricated users/data. Actual WorkOS login/callback is not verified.
Build and E2E run sequentially because both use .next. Historical lint is enforced by a separate mandatory gate,
not by Next build; no new lint fingerprints are accepted. No production login was weakened.


## Coach response continuity (Task 4.5)

Chat sends bounded `conversationContext` (period/topics, reported categories,
validated interpretations, offered action IDs and asked/answered question IDs),
not sliced assistant transcripts. State is ephemeral and cleared with chat or consent
revocation. Server identity/consent remains authoritative.

The main answer shows every validated model paragraph. Labels distinguish complete
AI, partial AI and evidence-only answers, based on actually displayed prose.
`tests/fixtures/coach-conversation.json` contains only fabricated 2020 data: real
synthetic Haiku output and its rendered backend replies, plus explicit contract fixtures.
Playwright checks all five turns, all paragraphs, three prioritized ACTIONs, contextual
summary/follow-up, partial output and misleading AI metadata. It never calls Anthropic.
The raw model is not the only acceptance layer: visible UI text must match the backend.
1 change: 1 addition & 0 deletions app/app/coach-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function sendCoachMessage(input: CoachMessageInput) {
message: input.message,
previousQuestions: input.previousQuestions,
previousAnswers: input.previousAnswers,
conversationContext: input.conversationContext,
from: input.from,
to: input.to,
}),
Expand Down
129 changes: 102 additions & 27 deletions components/product/wellbeing-coach.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
saveCoachNote,
sendCoachMessage,
} from "@/app/app/coach-actions";
import type { CoachNote, CoachReply, CoachStatus } from "@/lib/coach-types";
import type {
CoachNote,
CoachReply,
CoachStatus,
CoachConversationContext,
} from "@/lib/coach-types";

const button =
"rounded-xl bg-[#0F8F63] px-4 py-2 text-sm font-semibold text-white disabled:opacity-50";
Expand All @@ -31,6 +36,8 @@ export function WellbeingCoach({
const [messages, setMessages] = useState<
{ question: string; reply: CoachReply }[]
>([]);
const [conversationContext, setConversationContext] =
useState<CoachConversationContext>();
const [message, setMessage] = useState("");
const [from, setFrom] = useState("");
const [to, setTo] = useState("");
Expand All @@ -56,26 +63,36 @@ export function WellbeingCoach({
if (busy || !message.trim()) return;
setBusy(true);
setError("");
const hr = /[čćžšđ]|\b(moje|sam|sna|stres|zašto|kako|što|predloži)\b/i.test(
message,
);
try {
const result = await sendCoachMessage({
message,
previousQuestions: messages.slice(-4).map((m) => m.question),
previousAnswers: messages
.slice(-4)
.map((m) => m.reply.answer.slice(0, 2000)),
conversationContext,
...(from && to ? { from, to } : {}),
});
if (!result.ok) {
setError(result.error);
setError(
hr
? "Odgovor Coacha nije moguće dovršiti. Pokušajte ponovno."
: result.error,
);
return;
}
setMessages((previous) => [
...previous.slice(-5),
{ question: message, reply: result.data },
]);
setConversationContext(result.data.conversationContext);
setMessage("");
} catch {
setError("Coach unavailable. Please retry.");
setError(
hr
? "Odgovor Coacha nije moguće dovršiti. Pokušajte ponovno."
: "Coach unavailable. Please retry.",
);
} finally {
setBusy(false);
}
Expand All @@ -89,7 +106,10 @@ export function WellbeingCoach({
else {
setStatus(result.data);
setConfirmAi(false);
if (!enabled) setMessages([]);
if (!enabled) {
setMessages([]);
setConversationContext(undefined);
}
}
} catch {
setError("Consent could not be updated. Please retry.");
Expand Down Expand Up @@ -242,7 +262,16 @@ export function WellbeingCoach({
{messages.map((entry, i) => (
<article key={i} className="rounded-xl border border-slate-200 p-4">
<h3 className="font-semibold">You: {entry.question}</h3>
<Reply reply={entry.reply} />
<Reply
reply={entry.reply}
language={
/[čćžšđ]|\b(moje|sam|sna|stres|zašto|kako|što|predloži)\b/i.test(
entry.question,
)
? "hr"
: "en"
}
/>
</article>
))}
{busy && <p role="status">Working with your evidence…</p>}
Expand Down Expand Up @@ -306,7 +335,10 @@ export function WellbeingCoach({
<button
type="button"
className="ml-3 text-sm underline"
onClick={() => setMessages([])}
onClick={() => {
setMessages([]);
setConversationContext(undefined);
}}
>
Clear conversation
</button>
Expand Down Expand Up @@ -374,17 +406,48 @@ export function WellbeingCoach({
function Reply({
reply,
compact = false,
language = "en",
}: {
reply: CoachReply;
compact?: boolean;
language?: "hr" | "en";
}) {
const hr = language === "hr";
const hasAIText =
reply.blocks.some(
(b) => b.id.startsWith("conversation:") && reply.answer.includes(b.text),
) &&
(!reply.rendering || reply.rendering.displayedParagraphs > 0);
const partial =
hasAIText &&
(reply.answerStatus === "partial" || reply.completion === "ai_partial");
return (
<div className="mt-4 text-sm text-slate-700">
<h3 className="font-semibold text-slate-900">{reply.headline}</h3>
<p className="mt-1 text-xs">
{reply.period.from}–{reply.period.to} · {reply.timezone} · Latest
wearable: {reply.latestWearableDate ?? "unknown"} · Data coverage:{" "}
{reply.confidence} · {reply.mode.replaceAll("_", " ")}
{reply.period.from}–{reply.period.to} · {reply.timezone} ·
{hr ? "Zadnji signal za Coach" : "Latest coaching signal"}:{" "}
{reply.latestWearableDate ?? "unknown"} ·{" "}
{hr ? "Zadnji uvezeni dan" : "Latest imported day"}:{" "}
{reply.latestImportedDate ?? reply.latestWearableDate ?? "unknown"} ·{" "}
{hr ? "Pokrivenost" : "Data coverage"}:{" "}
{hr
? reply.confidence === "limited"
? "ograničena"
: "umjerena"
: reply.confidence}{" "}
·{" "}
{hasAIText
? partial
? hr
? "AI odgovor — djelomično dovršen"
: "Partial AI-assisted answer"
: hr
? "AI potpomognuti odgovor"
: "AI-assisted answer"
: hr
? "Odgovor iz podataka — bez AI objašnjenja"
: "Evidence-only answer — not AI-generated"}
</p>
{!compact && (
<p
Expand All @@ -398,7 +461,9 @@ function Reply({
<summary className="cursor-pointer font-semibold">
{compact
? "Key observations"
: "Measurements, evidence & limitations"}
: hr
? "Mjerenja, dokazi i ograničenja"
: "Measurements, evidence & limitations"}
</summary>
<ul className="mt-3 space-y-3">
{reply.blocks.map((block, i) => (
Expand All @@ -423,21 +488,31 @@ function Reply({
</li>
))}
</ul>
{!compact && reply.context.states.length > 0 && (
<p className="mt-3">
Existing detected states:{" "}
{reply.context.states
.map((s) => `${s.type} (${s.date})`)
.join(", ")}
</p>
)}
{!compact && reply.context.recommendations.length > 0 && (
<p className="mt-2">
Existing engine recommendations:{" "}
{reply.context.recommendations
.map((r) => `${r.type} (${r.date})`)
.join(", ")}
</p>
)}
{!compact && reply.failure && (
<p className="mt-2 text-xs">
{hr
? "Tehnička kategorija neuspjeha"
: "Technical failure category"}
: {reply.failure.stage} / {reply.failure.category}
</p>
)}
</details>
{!compact && reply.context.states.length > 0 && (
<p className="mt-3">
Existing detected states:{" "}
{reply.context.states.map((s) => `${s.type} (${s.date})`).join(", ")}
</p>
)}
{!compact && reply.context.recommendations.length > 0 && (
<p className="mt-2">
Existing engine recommendations:{" "}
{reply.context.recommendations
.map((r) => `${r.type} (${r.date})`)
.join(", ")}
</p>
)}
<p className="mt-3 text-xs text-slate-500">{reply.attribution}</p>
</div>
);
Expand Down
28 changes: 28 additions & 0 deletions lib/coach-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ export interface CoachReply {
timezone: string;
confidence: "limited" | "moderate";
latestWearableDate: string | null;
latestImportedDate?: string | null;
completion?: "ai_assisted" | "ai_partial" | "evidence_only";
answerStatus?: "complete" | "partial" | "fallback";
partialNotice?: string;
rendering?: {
modelParagraphs: number;
validatedParagraphs: number;
displayedParagraphs: number;
omittedParagraphs: number;
actionItems: number;
requiredActionItems: number;
};
conversationContext?: CoachConversationContext;
fallbackNotice?: string;
failure?: { category: string; stage: string; retryable: boolean };
blocks: CoachBlock[];
followUp: string | null;
mode: string;
Expand All @@ -40,6 +55,19 @@ export interface CoachMessageInput {
message: string;
previousQuestions?: string[];
previousAnswers?: string[];
conversationContext?: CoachConversationContext;
from?: string;
to?: string;
}
export interface CoachConversationContext {
period?: { from: string; to: string };
topics?: string[];
reportedCircumstances?: string[];
conclusions?: {
kind: "HYPOTHESIS" | "UNKNOWN";
text: string;
evidenceIds: string[];
}[];
offeredActionIds?: string[];
followUps?: { id: string; status: "asked" | "answered" }[];
}
Loading
Loading