Files
HealthLog/src/app/api/insights/bmi-status/route.ts
T
Marc-André Bombeck 2143445da2 feat(ai): show stored model text inside a shared record as the owner would
Serving stored text and starting model work are separate questions. A
delegate may not start model work on somebody else's record, but reads
the owner's stored briefing, period narrative, status notes, workout
notes and Coach unread signal exactly when the owner would: those
readers now resolve the record's own capability state, not the
viewer's. Generation, warms and enqueues still ask as the viewer, so a
delegate's read of a stale narrative queues nothing. A hidden
model-written narrative is no longer overwritten by a deterministic
warm while AI is unavailable.

The sharing front door reads the digest's briefing lead again, and an
integration case pins both halves.

Refs #975
2026-09-24 10:17:00 +02:00

60 lines
2.3 KiB
TypeScript

import { NextRequest } from "next/server";
import { apiSuccess } from "@/lib/api-response";
import {
generateBmiStatusForUser,
resolveBmiStatusLocale,
} from "@/lib/insights/bmi-status";
import { apiHandler, requireRecordAuth } from "@/lib/api-handler";
import { annotate } from "@/lib/logging/context";
import { resolveServerLocale } from "@/lib/i18n/server-locale";
import { aiCapabilityToServe } from "@/lib/ai/capabilities/gate";
import { unavailableStatusBody } from "@/lib/insights/status-unavailable";
export const dynamic = "force-dynamic";
/**
* A mixed read: the card's frame is data, the note inside it is model text.
* The note is served, and warmed on a miss, only while the `statusText` AI
* capability is available. Otherwise the route answers 200 with no note
* (`text: null`, `preparing: false`, `hasProvider` as provider presence) and
* an `ai` state saying why, without reading the cache or queueing anything.
* The `insights` module is the AI analysis opt-out and is folded into the
* capability, so it no longer refuses the route.
*/
export const GET = apiHandler(async (request: NextRequest) => {
// v1.37.0 — MANAGE-level read: a generated assessment over the whole
// record, which is not a section a scoped grant can name. The miss behind it
// enqueues nothing while a delegate is holding the request.
const { user } = await requireRecordAuth("manage", "record");
const ai = await aiCapabilityToServe(user.id, "statusText");
if (!ai.available) {
annotate({
action: { name: "insights.bmi-status.unavailable" },
meta: { reason: ai.reason },
});
return apiSuccess(await unavailableStatusBody(user.id, ai));
}
const localeParam = request.nextUrl.searchParams.get("locale");
const resolved = await resolveServerLocale({
request,
userLocale: user.locale ?? null,
override: localeParam,
});
const locale = resolveBmiStatusLocale(resolved);
// v1.8.3 — read-only: serve the cache, enqueue generation out of band on
// a miss. The GET never awaits the provider, so opening /insights/<metric>
// can no longer pin the main thread behind a cold LLM round-trip.
const result = await generateBmiStatusForUser(user.id, {
locale,
force: false,
readOnly: true,
});
annotate({ action: { name: "insights.bmi-status" } });
return apiSuccess({ ...result, ai });
});