mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
perf(bundle): keep zod out of the documents and Coach client routes
Both routes reached the schema library through a constant: the list of document kinds, the list and bulk limits, the attachment cap, the Coach data clusters and preference defaults, the conversation title length, the scope source list and the report leaf order. Each sat in a schema module, so importing the number shipped the library. The constants now live in zod-free modules (lib/documents/inbound-constants, lib/ai/coach/prefs-constants, lib/ai/coach/scope-sources, lib/report-selection/catalogue) and the schema modules re-export them for server code; the Coach page checks its scope parameter against the closed list instead of parsing it. A guard walks each route's static client graph with the TypeScript parser and fails on a zod import. Two guards follow earlier commits in this branch: the fetch-limit allowlist names the shared document picker sheet, and the card density test states a floor on the cards it finds.
This commit is contained in:
1 parent
3bae93256d
commit
d0677adac9
26 files changed
+421
-190
No files matched your search
@@ -14,11 +14,22 @@
|
||||
*/
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { REASONING_LEVELS } from "@/lib/ai/reasoning/levels";
|
||||
import {
|
||||
DEFAULT_REASONING_LEVEL,
|
||||
REASONING_LEVELS,
|
||||
type ReasoningLevel,
|
||||
} from "@/lib/ai/reasoning/levels";
|
||||
COACH_DATA_CLUSTERS,
|
||||
DEFAULT_COACH_PREFS,
|
||||
} from "@/lib/ai/coach/prefs-constants";
|
||||
|
||||
// The values a client component needs live in a zod-free module, so the
|
||||
// Coach and the settings pages do not pull the schema library in with them.
|
||||
// Re-exported here for the server code that imports them from the schema.
|
||||
export {
|
||||
COACH_DATA_CLUSTERS,
|
||||
DEFAULT_COACH_CLUSTERS,
|
||||
DEFAULT_COACH_PREFS,
|
||||
coachReasoningLevel,
|
||||
type CoachDataCluster,
|
||||
} from "@/lib/ai/coach/prefs-constants";
|
||||
|
||||
/**
|
||||
* Tone presets the Coach system-prompt prefix toggles between. v1.4.22
|
||||
@@ -99,36 +110,7 @@ export type CoachDefaultWindow = z.infer<typeof coachDefaultWindowEnum>;
|
||||
* The cluster set is the opt-in source of truth going forward;
|
||||
* `excludeMetrics` stays valid for back-compat and only narrows.
|
||||
*/
|
||||
export const coachDataClusterEnum = z.enum([
|
||||
"cardio",
|
||||
"body",
|
||||
"activity",
|
||||
"workouts",
|
||||
"sleep",
|
||||
"mood",
|
||||
"glucose",
|
||||
"medication",
|
||||
"mobility",
|
||||
"environment",
|
||||
]);
|
||||
export type CoachDataCluster = z.infer<typeof coachDataClusterEnum>;
|
||||
|
||||
/**
|
||||
* Clusters enabled when the user has never touched the cluster picker
|
||||
* (`dataClusters === undefined`). These four reproduce today's legacy
|
||||
* five domains: cardio carries BP + pulse, body carries weight, mood
|
||||
* and medication map straight through. The additive members riding
|
||||
* inside cardio/body (HRV, resting HR, body-composition) only surface
|
||||
* when the user actually has rows for them — empty blocks are dropped —
|
||||
* so a web-only account stays close to the legacy 5-domain output while
|
||||
* an iOS account quietly gains the extra signals it already stores.
|
||||
*/
|
||||
export const DEFAULT_COACH_CLUSTERS: ReadonlyArray<CoachDataCluster> = [
|
||||
"cardio",
|
||||
"body",
|
||||
"mood",
|
||||
"medication",
|
||||
];
|
||||
export const coachDataClusterEnum = z.enum(COACH_DATA_CLUSTERS);
|
||||
|
||||
/**
|
||||
* v1.18.1 — Coach cadence-suggestion state. Persisted
|
||||
@@ -216,25 +198,6 @@ export const coachPrefsSchema = z.object({
|
||||
|
||||
export type CoachPrefs = z.infer<typeof coachPrefsSchema>;
|
||||
|
||||
/**
|
||||
* Default preferences applied when the user has never opened the
|
||||
* settings cog. Equivalent to `coachPrefsSchema.parse({})` but
|
||||
* returned as a plain object so call sites can compare references
|
||||
* (e.g., short-circuit a snapshot rebuild when prefs match defaults).
|
||||
*/
|
||||
export const DEFAULT_COACH_PREFS: CoachPrefs = {
|
||||
tone: "warm",
|
||||
verbosity: "default",
|
||||
excludeMetrics: [],
|
||||
showEvidenceByDefault: false,
|
||||
defaultWindow: "allTime",
|
||||
};
|
||||
|
||||
/** The person's reasoning level; an absent key is the default, `medium`. */
|
||||
export function coachReasoningLevel(prefs: CoachPrefs): ReasoningLevel {
|
||||
return prefs.reasoning ?? DEFAULT_REASONING_LEVEL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a row's `coachPrefsJson` Json blob into a typed `CoachPrefs`,
|
||||
* falling back to defaults when the row is null. Keeps the call sites at
|
||||
|
||||
@@ -25,9 +25,33 @@
|
||||
* it is always narrowed from the session.
|
||||
*/
|
||||
import { z } from "zod/v4";
|
||||
import {
|
||||
DOCUMENT_BULK_MAX_IDS,
|
||||
DOCUMENT_CONTENT_INDEX_SOURCES,
|
||||
DOCUMENT_LIST_DEFAULT_LIMIT,
|
||||
INBOUND_DOCUMENT_KINDS,
|
||||
MAX_COACH_ATTACHMENTS,
|
||||
type DocumentContentIndexSourceValue,
|
||||
type InboundDocumentKindValue,
|
||||
} from "@/lib/documents/inbound-constants";
|
||||
|
||||
import type { AiCapabilityState } from "@/lib/ai/capabilities/types";
|
||||
|
||||
// The constants and label lists a client needs live in a zod-free module, so
|
||||
// a component that wants the list of document kinds does not pull the schema
|
||||
// library into its route. Re-exported here for the server code that already
|
||||
// imports them from the schema module.
|
||||
export {
|
||||
DOCUMENT_BULK_MAX_IDS,
|
||||
DOCUMENT_CONTENT_INDEX_SOURCES,
|
||||
DOCUMENT_LIST_DEFAULT_LIMIT,
|
||||
INBOUND_DOCUMENT_KINDS,
|
||||
MAX_COACH_ATTACHMENTS,
|
||||
isAiReadSource,
|
||||
type DocumentContentIndexSourceValue,
|
||||
type InboundDocumentKindValue,
|
||||
} from "@/lib/documents/inbound-constants";
|
||||
|
||||
/** Max facts a single document may stage / confirm. A dense letter is ~40. */
|
||||
export const INBOUND_MAX_FACTS = 120;
|
||||
|
||||
@@ -47,44 +71,6 @@ export const INBOUND_FACT_TYPES = [
|
||||
] as const;
|
||||
export type InboundFactType = (typeof INBOUND_FACT_TYPES)[number];
|
||||
|
||||
/** The document-kind labels (no interpretation — a label only). */
|
||||
export const INBOUND_DOCUMENT_KINDS = [
|
||||
"DOCTOR_REPORT",
|
||||
"DISCHARGE_LETTER",
|
||||
"LAB_RESULT",
|
||||
"IMAGING",
|
||||
"PRESCRIPTION",
|
||||
"REFERRAL",
|
||||
"INSURANCE",
|
||||
"VACCINATION",
|
||||
"SICK_NOTE",
|
||||
"OTHER",
|
||||
] as const;
|
||||
export type InboundDocumentKindValue = (typeof INBOUND_DOCUMENT_KINDS)[number];
|
||||
|
||||
/**
|
||||
* How a document's content index was produced (mirrors the server `source`
|
||||
* column). `vision` means an AI provider read the original (the AI-first path);
|
||||
* every other value is a provider-free local extraction. The UI reads it to
|
||||
* tell an AI-read document from a locally-indexed one — the former already had
|
||||
* the richer read, the latter can still be offered "Read with AI".
|
||||
*/
|
||||
export const DOCUMENT_CONTENT_INDEX_SOURCES = [
|
||||
"vision",
|
||||
"text-ocr",
|
||||
"local-pdf",
|
||||
"local-ocr",
|
||||
] as const;
|
||||
export type DocumentContentIndexSourceValue =
|
||||
(typeof DOCUMENT_CONTENT_INDEX_SOURCES)[number];
|
||||
|
||||
/** True when the index was produced by an AI provider reading the original. */
|
||||
export function isAiReadSource(
|
||||
source: DocumentContentIndexSourceValue | string | null,
|
||||
): boolean {
|
||||
return source === "vision";
|
||||
}
|
||||
|
||||
/**
|
||||
* Narrow the free-String DB `source` column to the DTO union, or `null` for an
|
||||
* absent / unrecognised value. The column is a free String server-side (adding
|
||||
@@ -880,7 +866,6 @@ export const DOCUMENT_LIST_SORTS = [
|
||||
export type DocumentListSort = (typeof DOCUMENT_LIST_SORTS)[number];
|
||||
|
||||
export const DOCUMENT_LIST_MAX_LIMIT = 100;
|
||||
export const DOCUMENT_LIST_DEFAULT_LIMIT = 50;
|
||||
|
||||
/**
|
||||
* The library list query: title/filename search, category filter, a
|
||||
@@ -917,9 +902,6 @@ export type DocumentListQuery = z.infer<typeof documentListQuerySchema>;
|
||||
|
||||
// ─── Vault: bulk actions ───────────────────────────────────────────────────
|
||||
|
||||
/** Max document ids one bulk request may touch. */
|
||||
export const DOCUMENT_BULK_MAX_IDS = 100;
|
||||
|
||||
export const DOCUMENT_BULK_ACTIONS = [
|
||||
"setKind",
|
||||
"linkEpisode",
|
||||
@@ -1110,11 +1092,6 @@ export type DocumentChatHistoryQuery = z.infer<
|
||||
|
||||
// ─── Fenced multi-document coach chat (S7) ──────────────────────────────────
|
||||
|
||||
/** Max documents attachable to one coach conversation. Bounds prompt size + the
|
||||
* numeric-grounding union; five labelled documents is already past the point a
|
||||
* chat answer stays useful. */
|
||||
export const MAX_COACH_ATTACHMENTS = 5;
|
||||
|
||||
/**
|
||||
* Request body for `POST /api/insights/chat/fenced`. `.strict()` — the schema
|
||||
* REFUSES the tool-mode fields (`scope` / `guidedQuestion` / `prefill`) and
|
||||
|
||||
Reference in new issue
Block a user