mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
The coverage check hung off generateInsight, whose only caller runWithFallback has no production caller left -- every live surface routes through runRawCompletionWithFallback instead. The admin quality dashboard was reading a metric nothing emitted. Widen the input to the structural shape both payloads share and call it from the comprehensive post-parse step, so the annotation reflects real generations. Writing the locale tests found three gaps the English-only banks hid: Polish verb suffixes fell outside the ASCII word class, a trailing word boundary after an accented character never closed, and German subordinate clauses put the verb last so the natural phrasing of a dose instruction never matched. The payload collector also skipped legacy bare-string recommendations entirely.
78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
/**
|
|
* Outbound safety screen over a whole comprehensive-insight payload.
|
|
*
|
|
* Lives apart from the generator on purpose: it is a pure function of the
|
|
* payload shape, both the background generator and the user-initiated POST
|
|
* route run it before their persist, and neither should have to import the
|
|
* other to get at it.
|
|
*
|
|
* What it closes. The number-grounding gate covers `dailyBriefing.paragraph`,
|
|
* `signalsOfDay[]` and `keyFindings[]`, and it grades NUMBERS only — so a
|
|
* digit-free "your 10-year cardiovascular risk is elevated, consider stepping
|
|
* your dose up" passed it untouched, and `recommendations[]` / `summary` were
|
|
* not graded at all. This screens every prose field the user can read against
|
|
* the insights safety contracts.
|
|
*/
|
|
import type { Locale } from "@/lib/i18n/config";
|
|
import { readBriefingBlock } from "@/lib/ai/briefing-grounding";
|
|
import {
|
|
screenModelOutput,
|
|
INSIGHTS_CONTRACTS,
|
|
type OutboundReason,
|
|
} from "@/lib/ai/safety/outbound-screen";
|
|
|
|
/** Every free-text field of a comprehensive payload the user can read. */
|
|
export function collectInsightProse(parsed: unknown): string[] {
|
|
const out: string[] = [];
|
|
const push = (v: unknown) => {
|
|
if (typeof v === "string" && v.trim().length > 0) out.push(v);
|
|
};
|
|
if (!parsed || typeof parsed !== "object") return out;
|
|
const root = parsed as Record<string, unknown>;
|
|
|
|
push(root.summary);
|
|
for (const rec of Array.isArray(root.recommendations)
|
|
? root.recommendations
|
|
: []) {
|
|
// The live schema admits BOTH a structured object and a legacy bare
|
|
// string. Reading only `.text` silently skipped every string rec, which is
|
|
// exactly the shape older cached payloads use.
|
|
if (typeof rec === "string") {
|
|
push(rec);
|
|
} else if (rec && typeof rec === "object") {
|
|
push((rec as Record<string, unknown>).text);
|
|
}
|
|
}
|
|
|
|
const briefing = readBriefingBlock(parsed);
|
|
if (briefing) {
|
|
push(briefing.paragraph);
|
|
for (const s of briefing.signalsOfDay ?? []) {
|
|
push(s?.headline);
|
|
push(s?.nudge);
|
|
push(s?.delta);
|
|
}
|
|
for (const f of briefing.keyFindings ?? []) {
|
|
push(f?.headline);
|
|
push(f?.detail);
|
|
push(f?.delta);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Screen every prose field of a payload. Returns the first tripped contract,
|
|
* or null when the whole payload is clean.
|
|
*/
|
|
export function screenInsightPayloadProse(
|
|
parsed: unknown,
|
|
locale: Locale,
|
|
): OutboundReason | null {
|
|
for (const text of collectInsightProse(parsed)) {
|
|
const decision = screenModelOutput(text, locale, INSIGHTS_CONTRACTS);
|
|
if (decision.block && decision.reason) return decision.reason;
|
|
}
|
|
return null;
|
|
}
|