The owner's wave-7 ruling: embeddings die entirely (git history is the
stash); clustering is birth-session derived (chunk A, 950ddfd), labels
are session names, and the prober's graph tools replace injection.
- settings: GraphSettings collapses to { enabled } — embedding,
embeddingModel, clustering, labeling all die. Migration story: stale
record keys are UNREAD and dropped by the sanitizer's reconstruction;
the next graph-section PUT (replace-whole) persists the collapsed
shape. The settings-panel GraphSection renders one master row.
- deleted: src/server/embeddings.ts (adapters, resolveEmbeddingPortFor,
embedNewNodes, embeddingPort, embeddingModelCatalog), the
GET /api/models/embeddings route, scripts/embed-graph.ts, tests for
all three, and the /api/models/embeddings picker surfaces in the UI.
- prices: EMBEDDING_PRICES overlay + callCost fallback die. cost-summary:
the embedding stage fold dies — back to three stages (extraction/
audit/naming) per the owner ruling; historical stages.embedding spend
in old run records is simply not displayed.
- ports: EmbeddingPort/EmbeddingResult/EmbeddingTask die. The embeddings
LEDGER surface (readEmbeddings/writeEmbeddings + EmbeddingsLedger +
EMBEDDINGS_FILE + graph-deletion's vector pruning) STAYS — inert data,
kept only so graph-context.ts compiles until Teacher's pass removes
it; the seam dies in the follow-up interlude.
- chat-handler (approved cross-lane unwiring, wave-6 precedent): the
extraction pipeline's embedder becomes a documented no-op (nothing
embedded, nothing spent, ledger untouched) until Teacher's pass
removes the phase; the graph-context injection dispatch gate, the
graphContext deps wiring, the setContextRespawn seam, and
turn-service's triggerGraphContext are gone — injection is dead
code pending Teacher's file deletions.
- scripts: backfill-graph/import-notes pass the same no-op embedder;
replay-layout derives clusters from birthSessionPartition (the same
partition the real settle uses) instead of the embeddings ledger.
- settings-body: parseGraphSettingsBody reads the master switch only —
old-shape keys in a body are ignored, not rejected; bad shapes 400.
- next.config: the @huggingface tracing-exclusion guards die with the
last reference.
Cross-lane flags: extraction.ts/graph-context.ts keep stale embed
comments + the embed phase until Teacher's full pass (their parallel
interlude owns those files — zero overlap kept by design);
semantic-clusters.ts + its test stay one more round (extraction.test.ts,
Teacher's file, still imports it — they delete it with the namer cases);
ChatSettings.graphContextTokens + the graph-context-control UI stay
until Teacher+UI.
Gates: tsc clean, eslint clean, 885/885 tests, build compiles.
Live-verified on the port-3004 dev tree: PUT /api/settings/graph with
dead keys collapses on write, bad shapes 400, GET reads {enabled: true},
/api/models/embeddings 404s.
352 lines
15 KiB
TypeScript
352 lines
15 KiB
TypeScript
/**
|
|
* The shared settings-PUT body parsers — one seam per settings surface.
|
|
* Extracted verbatim from the per-chat route (default-settings wave,
|
|
* 2026-09-18): identical parse, identical errors. The section parsers
|
|
* (settings-storage wave, 2026-09-19) follow the same house rules.
|
|
*
|
|
* Replace-whole semantics: the client sends the complete desired SECTION;
|
|
* absent fields are absent overrides ({} clears everything). No merges, so
|
|
* a stale client can never resurrect a cleared override. Unknown body keys
|
|
* are dropped (same tolerance as parseSettingsBody) — unknown DISK keys are
|
|
* a different concern and are preserved by the sanitizer (core/settings.ts).
|
|
*/
|
|
|
|
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
import { GRAPH_CONTEXT_TOKENS_MAX, isHexColor, type ChatSettings } from "../core";
|
|
import type {
|
|
AgentSettings,
|
|
AppearanceSettings,
|
|
BackgroundModelRole,
|
|
GeneralSettings,
|
|
GraphSettings,
|
|
ModelsSettings,
|
|
RoleModelOverride,
|
|
} from "../core/settings.ts";
|
|
import { BACKGROUND_MODEL_ROLES, MODEL_SLOTS } from "../core/settings.ts";
|
|
import { TOGGLEABLE_TOOLS } from "../core/pending.ts";
|
|
|
|
/** Parsed-or-rejected — `ok: false` carries the 400's error message. */
|
|
export type SettingsBodyResult<T> =
|
|
| { ok: true; settings: T }
|
|
| { ok: false; error: string };
|
|
|
|
export function parseSettingsBody(body: unknown): SettingsBodyResult<ChatSettings> {
|
|
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
return { ok: false, error: "Invalid JSON body" };
|
|
}
|
|
const raw = body as {
|
|
model?: { provider?: unknown; id?: unknown };
|
|
thinkingLevel?: unknown;
|
|
graphContextTokens?: unknown;
|
|
};
|
|
const settings: ChatSettings = {};
|
|
if (raw.model !== undefined) {
|
|
if (
|
|
typeof raw.model !== "object" ||
|
|
raw.model === null ||
|
|
typeof raw.model.provider !== "string" ||
|
|
typeof raw.model.id !== "string"
|
|
) {
|
|
return { ok: false, error: "model must be {provider, id}" };
|
|
}
|
|
settings.model = { provider: raw.model.provider, id: raw.model.id };
|
|
}
|
|
if (raw.thinkingLevel !== undefined) {
|
|
if (typeof raw.thinkingLevel !== "string") {
|
|
return { ok: false, error: "thinkingLevel must be a string" };
|
|
}
|
|
settings.thinkingLevel = raw.thinkingLevel as ThinkingLevel;
|
|
}
|
|
// Graph context injection budget (vision-context-injection.md): integer
|
|
// tokens, 0 (off) to GRAPH_CONTEXT_TOKENS_MAX, no silent clamping — the
|
|
// slider is the only writer, and it only ever sends multiples of the step.
|
|
if (raw.graphContextTokens !== undefined) {
|
|
if (
|
|
typeof raw.graphContextTokens !== "number" ||
|
|
!Number.isInteger(raw.graphContextTokens) ||
|
|
raw.graphContextTokens < 0 ||
|
|
raw.graphContextTokens > GRAPH_CONTEXT_TOKENS_MAX
|
|
) {
|
|
return {
|
|
ok: false,
|
|
error: `graphContextTokens must be an integer between 0 and ${GRAPH_CONTEXT_TOKENS_MAX}`,
|
|
};
|
|
}
|
|
settings.graphContextTokens = raw.graphContextTokens;
|
|
}
|
|
return { ok: true, settings };
|
|
}
|
|
|
|
// ─── Section parsers (settings-storage wave, 2026-09-19) ────────────────────
|
|
// One parser per settings.json section; the route maps the URL section to
|
|
// its parser via SETTINGS_SECTIONS below. Same discipline as
|
|
// parseSettingsBody: strict on every known field (a bad type/shape is a
|
|
// 400, never a silent clamp/drop), unknown keys dropped.
|
|
|
|
function asRecord(body: unknown): Record<string, unknown> | null {
|
|
if (!body || typeof body !== "object" || Array.isArray(body)) return null;
|
|
return body as Record<string, unknown>;
|
|
}
|
|
|
|
/** general: { autoUpdate?: boolean } */
|
|
export function parseGeneralSettingsBody(body: unknown): SettingsBodyResult<GeneralSettings> {
|
|
const raw = asRecord(body);
|
|
if (!raw) return { ok: false, error: "Invalid JSON body" };
|
|
const settings: GeneralSettings = {};
|
|
if (raw.autoUpdate !== undefined) {
|
|
if (typeof raw.autoUpdate !== "boolean") {
|
|
return { ok: false, error: "autoUpdate must be a boolean" };
|
|
}
|
|
settings.autoUpdate = raw.autoUpdate;
|
|
}
|
|
return { ok: true, settings };
|
|
}
|
|
|
|
/** agent.models: only the six known background roles; each override's
|
|
* fields mirror ChatSettings (model pair + independent thinkingLevel). */
|
|
function parseRoleOverride(value: unknown, role: string): { ok: true; override: RoleModelOverride } | { ok: false; error: string } {
|
|
const raw = asRecord(value);
|
|
if (!raw) return { ok: false, error: `${role} must be an object` };
|
|
const override: RoleModelOverride = {};
|
|
if (raw.model !== undefined) {
|
|
if (
|
|
typeof raw.model !== "object" ||
|
|
raw.model === null ||
|
|
typeof (raw.model as Record<string, unknown>).provider !== "string" ||
|
|
typeof (raw.model as Record<string, unknown>).id !== "string"
|
|
) {
|
|
return { ok: false, error: `${role}.model must be {provider, id}` };
|
|
}
|
|
override.model = {
|
|
provider: (raw.model as { provider: string }).provider,
|
|
id: (raw.model as { id: string }).id,
|
|
};
|
|
}
|
|
if (raw.thinkingLevel !== undefined) {
|
|
if (typeof raw.thinkingLevel !== "string") {
|
|
return { ok: false, error: `${role}.thinkingLevel must be a string` };
|
|
}
|
|
override.thinkingLevel = raw.thinkingLevel as ThinkingLevel;
|
|
}
|
|
return { ok: true, override };
|
|
}
|
|
|
|
/** A boolean map (tools / prompts switches): every value must be boolean. */
|
|
function parseBooleanMap(value: unknown, field: string): { map?: Record<string, boolean>; error?: string } {
|
|
if (value === undefined) return {};
|
|
const raw = asRecord(value);
|
|
if (!raw) return { error: `${field} must be an object` };
|
|
const out: Record<string, boolean> = {};
|
|
for (const [key, entry] of Object.entries(raw)) {
|
|
if (typeof entry !== "boolean") return { error: `${field}.${key} must be a boolean` };
|
|
out[key] = entry;
|
|
}
|
|
return { map: out };
|
|
}
|
|
|
|
/** agent: { models?, tools?, prompts? } — model/thinking validity against
|
|
* the catalog is checked by the route (validateChatSettings discipline);
|
|
* prompt switch-on membership is checked by the route (needs the store). */
|
|
export function parseAgentSettingsBody(body: unknown): SettingsBodyResult<AgentSettings> {
|
|
const raw = asRecord(body);
|
|
if (!raw) return { ok: false, error: "Invalid JSON body" };
|
|
const settings: AgentSettings = {};
|
|
if (raw.models !== undefined) {
|
|
const models = asRecord(raw.models);
|
|
if (!models) return { ok: false, error: "models must be an object" };
|
|
const known = BACKGROUND_MODEL_ROLES as readonly string[];
|
|
const parsed: NonNullable<AgentSettings["models"]> = {};
|
|
for (const [role, value] of Object.entries(models)) {
|
|
if (!known.includes(role)) {
|
|
return { ok: false, error: `unknown role "${role}" (supported: ${known.join(", ")})` };
|
|
}
|
|
const override = parseRoleOverride(value, role);
|
|
if (!override.ok) return { ok: false, error: override.error };
|
|
if (Object.keys(override.override).length) parsed[role as BackgroundModelRole] = override.override;
|
|
}
|
|
if (Object.keys(parsed).length) settings.models = parsed;
|
|
}
|
|
const tools = parseBooleanMap(raw.tools, "tools");
|
|
if (tools.error) return { ok: false, error: tools.error };
|
|
if (tools.map) {
|
|
// Unknown tool keys are rejected at WRITE time (user decision
|
|
// 2026-09-19): the delta is inert while unread, but once the bridge
|
|
// honors it a persisted `quiz: false` would genuinely drop the quiz —
|
|
// and the definitive list is the adjudicated job-tools-only set.
|
|
// The read-side sanitizer stays lenient for forward-compat.
|
|
const supported = TOGGLEABLE_TOOLS as readonly string[];
|
|
for (const key of Object.keys(tools.map)) {
|
|
if (!supported.includes(key)) {
|
|
return { ok: false, error: `unknown tool "${key}" (supported: ${supported.join(", ")})` };
|
|
}
|
|
}
|
|
settings.tools = tools.map;
|
|
}
|
|
const prompts = parseBooleanMap(raw.prompts, "prompts");
|
|
if (prompts.error) return { ok: false, error: prompts.error };
|
|
if (prompts.map) settings.prompts = prompts.map;
|
|
if (raw.search !== undefined) {
|
|
const search = asRecord(raw.search);
|
|
if (!search) return { ok: false, error: "search must be an object" };
|
|
if (search.engine !== undefined) {
|
|
if (search.engine !== "tavily" && search.engine !== "brave") {
|
|
return { ok: false, error: 'search.engine must be "tavily" or "brave"' };
|
|
}
|
|
settings.search = { engine: search.engine };
|
|
}
|
|
}
|
|
// Prober graph tools (wave 7, owner ruling 2026-09-23): an agent-section
|
|
// subsection, not a tool-toggles key — the mechanism is agent capability
|
|
// (tool registration), the subject is the graph. Strict write side:
|
|
// object shape only; `enabled` boolean when present. Absent = ON.
|
|
if (raw.graphTools !== undefined) {
|
|
const graphTools = asRecord(raw.graphTools);
|
|
if (!graphTools) return { ok: false, error: "graphTools must be an object" };
|
|
if (graphTools.enabled !== undefined && typeof graphTools.enabled !== "boolean") {
|
|
return { ok: false, error: "graphTools.enabled must be a boolean" };
|
|
}
|
|
settings.graphTools = graphTools.enabled === undefined ? {} : { enabled: graphTools.enabled };
|
|
}
|
|
return { ok: true, settings };
|
|
}
|
|
|
|
/** A hex-or-null color field (appearance colors): null = default. */
|
|
function parseHexOrNull(value: unknown, field: string): { color?: string | null; error?: string } {
|
|
if (value === null) return { color: null };
|
|
if (typeof value !== "string" || !isHexColor(value)) {
|
|
return { error: `${field} must be a #hex color or null` };
|
|
}
|
|
return { color: value };
|
|
}
|
|
|
|
/** appearance: { mode?, background?, fontFamily?, proseFontSize?,
|
|
* sessionNameSerif?, mdRendering? } */
|
|
export function parseAppearanceSettingsBody(body: unknown): SettingsBodyResult<AppearanceSettings> {
|
|
const raw = asRecord(body);
|
|
if (!raw) return { ok: false, error: "Invalid JSON body" };
|
|
const settings: AppearanceSettings = {};
|
|
if (raw.mode !== undefined) {
|
|
if (raw.mode !== "dark" && raw.mode !== "light") {
|
|
return { ok: false, error: "mode must be \"dark\" or \"light\"" };
|
|
}
|
|
settings.mode = raw.mode;
|
|
}
|
|
if (raw.background !== undefined) {
|
|
const parsed = parseHexOrNull(raw.background, "background");
|
|
if (parsed.error) return { ok: false, error: parsed.error };
|
|
settings.background = parsed.color ?? null;
|
|
}
|
|
if (raw.fontFamily !== undefined) {
|
|
if (raw.fontFamily !== null && (typeof raw.fontFamily !== "string" || !raw.fontFamily.trim())) {
|
|
return { ok: false, error: "fontFamily must be a non-empty string or null" };
|
|
}
|
|
settings.fontFamily = raw.fontFamily as string | null;
|
|
}
|
|
if (raw.proseFontSize !== undefined) {
|
|
if (
|
|
typeof raw.proseFontSize !== "number" ||
|
|
!Number.isInteger(raw.proseFontSize) ||
|
|
raw.proseFontSize < 12 ||
|
|
raw.proseFontSize > 24
|
|
) {
|
|
return { ok: false, error: "proseFontSize must be an integer between 12 and 24" };
|
|
}
|
|
settings.proseFontSize = raw.proseFontSize;
|
|
}
|
|
if (raw.sessionNameSerif !== undefined) {
|
|
if (typeof raw.sessionNameSerif !== "boolean") {
|
|
return { ok: false, error: "sessionNameSerif must be a boolean" };
|
|
}
|
|
settings.sessionNameSerif = raw.sessionNameSerif;
|
|
}
|
|
if (raw.mdRendering !== undefined) {
|
|
const md = asRecord(raw.mdRendering);
|
|
if (!md) return { ok: false, error: "mdRendering must be an object" };
|
|
const rendering: NonNullable<AppearanceSettings["mdRendering"]> = {};
|
|
if (md.headingWeight !== undefined) {
|
|
if (
|
|
typeof md.headingWeight !== "number" ||
|
|
!Number.isInteger(md.headingWeight) ||
|
|
md.headingWeight < 100 ||
|
|
md.headingWeight > 900
|
|
) {
|
|
return { ok: false, error: "mdRendering.headingWeight must be an integer between 100 and 900" };
|
|
}
|
|
rendering.headingWeight = md.headingWeight;
|
|
}
|
|
if (md.boldColor !== undefined) {
|
|
const parsed = parseHexOrNull(md.boldColor, "mdRendering.boldColor");
|
|
if (parsed.error) return { ok: false, error: parsed.error };
|
|
rendering.boldColor = parsed.color ?? null;
|
|
}
|
|
if (md.italicColor !== undefined) {
|
|
const parsed = parseHexOrNull(md.italicColor, "mdRendering.italicColor");
|
|
if (parsed.error) return { ok: false, error: parsed.error };
|
|
rendering.italicColor = parsed.color ?? null;
|
|
}
|
|
if (Object.keys(rendering).length) settings.mdRendering = rendering;
|
|
}
|
|
return { ok: true, settings };
|
|
}
|
|
|
|
/** graph: the collapsed section { enabled? } (wave 7, owner ruling
|
|
* 2026-09-23): the master switch ONLY — the embedding/clustering/labeling
|
|
* rungs and the BYOK embeddingModel died with the provider embedder
|
|
* (clustering is the free birth-session partition, labels are session
|
|
* names). Old-shape keys in a body are ignored, not rejected. */
|
|
export function parseGraphSettingsBody(body: unknown): SettingsBodyResult<GraphSettings> {
|
|
const raw = asRecord(body);
|
|
if (!raw) return { ok: false, error: "Invalid JSON body" };
|
|
// Wave 7 collapse (owner ruling 2026-09-23): the section is the master
|
|
// switch ONLY. PUT is replace-whole — a body like {enabled: true} IS the
|
|
// whole section now; the dead rungs' keys in old records are unread
|
|
// (the sanitizer drops them; see sanitizeGraph).
|
|
const settings: GraphSettings = {};
|
|
if (raw.enabled !== undefined) {
|
|
if (typeof raw.enabled !== "boolean") return { ok: false, error: "enabled must be a boolean" };
|
|
settings.enabled = raw.enabled;
|
|
}
|
|
return { ok: true, settings };
|
|
}
|
|
|
|
/** models: { strong?, cheap?, vision? } — the slot tiers (model slots
|
|
* wave, 2026-09-20). Each slot is the same shape as a role override; a
|
|
* present-but-empty slot object is rejected (absent slot = unset; say
|
|
* what you mean). Catalog validity is checked by the route
|
|
* (validateChatSettings discipline). */
|
|
export function parseModelsSettingsBody(body: unknown): SettingsBodyResult<ModelsSettings> {
|
|
const raw = asRecord(body);
|
|
if (!raw) return { ok: false, error: "Invalid JSON body" };
|
|
const settings: ModelsSettings = {};
|
|
for (const key of Object.keys(raw)) {
|
|
const known = MODEL_SLOTS as readonly string[];
|
|
if (!known.includes(key)) {
|
|
return { ok: false, error: `unknown slot "${key}" (supported: ${known.join(", ")})` };
|
|
}
|
|
const parsed = parseRoleOverride(raw[key], key);
|
|
if (!parsed.ok) return { ok: false, error: parsed.error };
|
|
// BYOK (owner, 2026-09-22): a slot IS a model choice — no factory
|
|
// floor exists, so a thinkingLevel alone can never run; every slot
|
|
// write must carry the model (the level is an optional rider). An
|
|
// absent slot key clears the tier (replace-whole section).
|
|
if (!parsed.override.model) {
|
|
return { ok: false, error: `slot "${key}" must set a model (thinkingLevel alone cannot run — absent slot = unset)` };
|
|
}
|
|
settings[key as (typeof MODEL_SLOTS)[number]] = parsed.override;
|
|
}
|
|
return { ok: true, settings };
|
|
}
|
|
|
|
/** The section→parser map for PUT /api/settings/[section] (the route also
|
|
* carries the special prompts sub-resource). */
|
|
export const SETTINGS_SECTIONS = {
|
|
general: parseGeneralSettingsBody,
|
|
chat: parseSettingsBody,
|
|
agent: parseAgentSettingsBody,
|
|
appearance: parseAppearanceSettingsBody,
|
|
graph: parseGraphSettingsBody,
|
|
models: parseModelsSettingsBody,
|
|
} as const;
|
|
|
|
export type SettingsSectionName = keyof typeof SETTINGS_SECTIONS;
|