Owner decision 2026-09-22: 'it doesn't make sense to have any default
model configured anywhere; the application is bring your own keys.'
config.ts: MODELS deleted; ModelRole becomes an explicit 9-role union;
DEFAULT_THINKING_ORDER (medium, low, high, minimal, xhigh, max) bakes
the unset-level default — first entry the model supports; explicitly
set levels keep pi's native nearest-clamp.
core/settings-resolution: the factory layer dissolves — the chain is
role override → slot → NOTHING. resolveSettings(record) takes no
factory. Empty chains resolve to null/undefined; a thinkingLevel-only
slot is inert (stored, never run). resolveConversationSpec: chat.model
override over the strong slot, null when both empty. resolveRoleSpec
spans all nine roles for the job/extraction engines.
Refusal envelope (core/settings.ts): modelsNotConfigured(missing) —
{error, code: 'models_not_configured', missing: [slot tiers]} +
ModelsNotConfiguredError with .envelope. Per-need gates, each honest:
- chat-handler: universal turn gate (new + continuing turns) returns
503 before beginTurn — no liveness, no write-ahead persist, no graph
trigger before the gate fires.
- runner.runJob: unresolvable engine lands the job as error with the
envelope wording; no phantom loop, no result steer.
- extraction: refuseIfStagesUnconfigured throws before any stage
(turn-service info-skips so coverage retries next turn; the import
route surfaces per-file code+missing); labeler-less runs skip
cluster naming with a console line and still merge.
Write side: slot entries must carry a model — level-only slot writes
are 400s (absent slot key = clear). piEngineForSpec resolves unset
thinking levels via DEFAULT_THINKING_ORDER; piEngineFor now throws
honestly; thinkingLevelFor/modelIdFor deleted; teacher-agent, pi-run,
visual-maker, pdf-reader all read engine.level.
model-catalog: defaults/slots/rolePins null when the chain is empty
(contracts nullable) — no phantom ids in payloads. Stores drop
MODEL_PINS entirely.
Tests: pin-leaners converted to explicit injection (per-chat model
pins on scripted chats — exactly what the setup flow writes; faux
engines for agent-construction tests). New coverage: the 503 gate
envelope, the runner refusal, the extraction refusal + labeler skip.
Gates: typecheck, lint, 815/815, build.
199 lines
9.6 KiB
TypeScript
199 lines
9.6 KiB
TypeScript
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
/** Repository root (this file is at <root>/config.ts). */
|
|
export const ROOT = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
/**
|
|
* THE data-location seam (the Electron contract, 2026-09-17): LH_DATA_DIR
|
|
* relocates every MUTABLE record — chat sessions, graph ledgers, the
|
|
* embedding model cache — when set. The desktop shell points it at
|
|
* userData so nothing is written inside the read-only .app bundle
|
|
* (macOS translocation guarantees the bundle is read-only anyway).
|
|
* Unset (plain web dev / repo work) it resolves to the repo's data/,
|
|
* byte-for-byte as before. Read-only assets (prompts) stay ROOT-anchored
|
|
* on purpose: they ship inside the app bundle by design.
|
|
*/
|
|
const DATA_ROOT = process.env.LH_DATA_DIR
|
|
? path.resolve(process.env.LH_DATA_DIR)
|
|
: path.join(ROOT, "data");
|
|
|
|
export const PATHS = {
|
|
root: ROOT,
|
|
/** Teacher system prompt — the agent's instructions (src/agent/). */
|
|
teacherPrompt: path.join(ROOT, "src", "agent", "system-prompt.md"),
|
|
data: DATA_ROOT,
|
|
/** Chat sessions (AI SDK UIMessage transcripts, one JSON file per chat). */
|
|
chats: path.join(DATA_ROOT, "chats"),
|
|
graph: path.join(DATA_ROOT, "graph"),
|
|
/** transformers.js model cache (server-side embeddings, ~300MB).
|
|
* null in dev = keep the package default (the model is already
|
|
* downloaded there); under LH_DATA_DIR the packaged app must never
|
|
* try to cache inside its own bundle. */
|
|
modelCache: process.env.LH_DATA_DIR ? path.join(DATA_ROOT, "models") : null,
|
|
} as const;
|
|
|
|
// ─── Models ────────────────────────────────────────────────────────────────
|
|
// BYOK ALL THE WAY DOWN (owner, 2026-09-22): there are NO factory default
|
|
// models. Every engine resolves from the user's model slots (settings.json
|
|
// models section: strong/cheap/vision) over per-role/ChatSettings overrides,
|
|
// and a chain that ends empty REFUSES honestly (503 models_not_configured).
|
|
// .env stays for secrets (API keys) only. What remains here are ENGINE
|
|
// CONSTRUCTION FACTS: the thinking-level scale and the default order.
|
|
|
|
export type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
|
|
/** Every agent role in the app. The conversation pair (prober/teacher)
|
|
* resolves chat.model → strong slot; background roles resolve their own
|
|
* override → tier slot (core/settings-resolution.ts ROLE_SLOT). */
|
|
export type ModelRole =
|
|
| "prober"
|
|
| "teacher"
|
|
| "researcher"
|
|
| "demoBuilder"
|
|
| "visualMaker"
|
|
| "pdfReader"
|
|
| "extractor"
|
|
| "auditor"
|
|
| "labeler";
|
|
|
|
/** The DEFAULT thinking level for UNSET levels only (owner, 2026-09-22):
|
|
* a slot/override/chat that names a model but no thinking level resolves
|
|
* to the FIRST entry in this order that the model supports
|
|
* (getSupportedThinkingLevels); a non-reasoning model resolves "off"
|
|
* regardless (pi reports exactly ["off"]). EXPLICITLY set levels never
|
|
* consult this list — they clamp nearest via pi's native
|
|
* clampThinkingLevel on model switches. */
|
|
export const DEFAULT_THINKING_ORDER = [
|
|
"medium",
|
|
"low",
|
|
"high",
|
|
"minimal",
|
|
"xhigh",
|
|
"max",
|
|
] as const satisfies readonly ThinkingLevel[];
|
|
|
|
/**
|
|
* The extraction pipeline (vision §1 construction): the sole graph writer.
|
|
* Triggered per chat once this many NEW quiz answers have landed since the
|
|
* chat's last extraction checkpoint. The trigger mechanism is a v1
|
|
* placeholder — deliberately isolated behind one seam
|
|
* (server/turn-service.ts) so it can be swapped without touching the
|
|
* pipeline.
|
|
*/
|
|
/**
|
|
* The extraction trigger (vision-extraction-coverage.md, 2026-09-15): a run
|
|
* fires when a chat's UNCOVERED transcript tokens reach the chunk budget.
|
|
* Runs are token-budgeted batches of the oldest uncovered messages — this
|
|
* knob is the transcript share of the extractor's context (it also carries
|
|
* the whole graph + instructions), so it bounds both cost and chunk size.
|
|
* Replaces the old every-3-quiz-answers trigger, which never fired for
|
|
* quiz-less sessions.
|
|
*/
|
|
export const EXTRACTION = { chunkTokens: 4096 } as const;
|
|
|
|
/**
|
|
* The LIVE graph store (src/store/graph.ts) — owned by the app's extraction
|
|
* pipeline. Line shapes: src/core/contracts.ts.
|
|
*/
|
|
export const LEDGER_FILES = {
|
|
nodes: path.join(PATHS.graph, "nodes.jsonl"),
|
|
edges: path.join(PATHS.graph, "edges.jsonl"),
|
|
events: path.join(PATHS.graph, "events.jsonl"),
|
|
mappings: path.join(PATHS.graph, "mappings.jsonl"),
|
|
sessions: path.join(PATHS.graph, "sessions.jsonl"),
|
|
/** Per-message extraction coverage (contracts/graph.ts CoverageEntry;
|
|
* the checkpoint's replacement for chat extractions — notes keep
|
|
* sessions.jsonl, whole-file as they are). */
|
|
coverage: path.join(PATHS.graph, "coverage.jsonl"),
|
|
/** LLM-named community records (contracts/graph.ts ClusterLabeling). */
|
|
clusterLabels: path.join(PATHS.graph, "cluster-labels.jsonl"),
|
|
/** Cluster-naming run usage (contracts/graph.ts NamingRunUsage) — the
|
|
* money channel's third stage; graph-global, so its own ledger. */
|
|
namingRuns: path.join(PATHS.graph, "naming-runs.jsonl"),
|
|
} as const;
|
|
|
|
/**
|
|
* Notes-import bookkeeping (NOT a ledger): one JSON map, src-id → NotesSource
|
|
* record (contracts/graph.ts) — the file path + import time + the run's usage
|
|
* log. Read-modify-write like a chat file, not append-only like the ledgers.
|
|
*/
|
|
export const SOURCES_FILE = path.join(PATHS.graph, "sources.json");
|
|
/** The layout ledger (read-modify-write like sources.json): nodeId →
|
|
* {x,y} resting positions from the incremental settle (core/layout.ts).
|
|
* Carries an updatedAt stamp for future live-physics conflict handling. */
|
|
export const POSITIONS_FILE = path.join(PATHS.graph, "positions.json");
|
|
|
|
/** The embedding ledger: one L2-normalized vector per node id, all from
|
|
* ONE model (swapping models re-embeds everything — vectors from
|
|
* different models live in incompatible spaces). Read-modify-write
|
|
* JSON like positions/sources. */
|
|
export const EMBEDDINGS_FILE = path.join(PATHS.graph, "embeddings.json");
|
|
|
|
/**
|
|
* Graph render settings (the settings-panel controls): one small JSON blob,
|
|
* read-modify-write. Shared shape/sanitizer: src/core/render-settings.ts.
|
|
*/
|
|
export const RENDER_SETTINGS_FILE = path.join(PATHS.graph, "render-settings.json");
|
|
|
|
/**
|
|
* DEFAULT settings — THE default model + thinking level + knowledge-context
|
|
* budget (owner, 2026-09-18): the new-session surfaces' controls are the
|
|
* settings page for the default model. There is NO baked-in default —
|
|
* BYOK first boot shows "no model" until the user picks one, and their
|
|
* last choice IS the default from then on. One small JSON blob,
|
|
* read-modify-write, written only through app/api/settings/defaults.
|
|
*/
|
|
export const DEFAULT_SETTINGS_FILE = path.join(DATA_ROOT, "default-settings.json");
|
|
|
|
/**
|
|
* THE USER SETTINGS RECORD (settings-storage wave, 2026-09-19): one
|
|
* versioned, DELTA-shaped JSON blob at the data-dir seam holding every
|
|
* non-secret product preference — general, chat defaults, agent (background
|
|
* model/thinking overrides + tool toggles + prompt-override switches),
|
|
* appearance, graph. Explicit user choices only: ABSENT keys resolve to the
|
|
* factory defaults compiled into src/core/settings.ts (FACTORY_SETTINGS), so
|
|
* a software update that adds a knob (new tool, new role, new appearance
|
|
* field) needs no migration — old records simply lack the new keys. The
|
|
* version field is a PRIVATE schema counter for breaking shape changes
|
|
* only (field renamed/meaning changed), unrelated to the app release
|
|
* version. Secrets (API keys, login tokens) NEVER live here — auth.json /
|
|
* .env.local keep that role. Migrated FROM: default-settings.json folds
|
|
* into the record's `chat` section on first read (the old file is left in
|
|
* place, inert).
|
|
*/
|
|
export const SETTINGS_FILE = path.join(DATA_ROOT, "settings.json");
|
|
|
|
/**
|
|
* User prompt-override files (settings-storage wave, 2026-09-19): the
|
|
* per-segment system-prompt overrides mirroring the SOURCE layout
|
|
* (src/agent/prompts/) — the shared base as prompts/system-prompt.md,
|
|
* fragments as prompts/teacher/<file> / prompts/prober/<file>. File
|
|
* PRESENCE alone is not the override: settings.json's agent.prompts map
|
|
* (segmentPath → boolean) is the explicit switch, so a user can keep a
|
|
* customized file on disk but run the source default to compare. The
|
|
* segment list itself is ALWAYS derived from source at runtime (the
|
|
* loader in src/store/prompt-overrides.ts) — code updates that add,
|
|
* remove, or reorder fragments adapt automatically; an override keyed to
|
|
* a vanished segment stops composing but stays on disk.
|
|
*/
|
|
export const PROMPTS_OVERRIDE_DIR = path.join(DATA_ROOT, "prompts");
|
|
|
|
/**
|
|
* THE AUTH/KEY STORE (auth-keys wave, 2026-09-19): BYOK provider keys in a
|
|
* flat {VAR: value} JSON map — the SAME file the Electron app has always
|
|
* hand-edited (owner decision: option A, zero migration). Desktop main.cjs
|
|
* passes LH_AUTH_FILE pointing at userData/auth.json (mode 0600); in dev
|
|
* (no Electron) the default is the repo-root auth.json (gitignored, same
|
|
* pattern as .env.local — a worktree may symlink it to the master copy).
|
|
* The SERVER reads/writes this file directly at call time (no restart to
|
|
* change a key); it reflects the values into process.env at boot with
|
|
* FILL-GAP precedence — real environment variables (incl. .env.local in
|
|
* dev) always win (src/server/keys.ts). Hand-edits still work: the file is
|
|
* re-read from disk on every API access. Secrets NEVER go in settings.json
|
|
* — this file keeps that role, alongside .env.local.
|
|
*/
|
|
export const AUTH_FILE = process.env.LH_AUTH_FILE
|
|
? path.resolve(process.env.LH_AUTH_FILE)
|
|
: path.join(ROOT, "auth.json");
|