e60fd63d2b
the provider round (2026-11-18, batteries-included): providers are first-class entities, the config is server-written state (state/config.json), and a fresh clone boots playable -- the opencode/free singleton auto-added and discovered on the empty list, playable with zero secrets. Every request resolves a full spec {provider, model, effort}: no unset effort, the per-run params layer retired, the mind verb the only sampling source. The registry is per-provider sections (state/models.json), refine caches adjudications under the double spec tuple (state/refine-cache.json), run picks are invalidatable (a removed provider or drifted model blocks the run with the note naming what died), and the housekeeper inherits the player spec's provider default (#16 closed). The no-legacy pass died the same day it shipped: bootImport deleted (server.json is bind + contwin), the registry's v1/v2 in-memory migrations deleted (models.json rewritten on disk into the per-provider shape), bare-string model picks deleted (Chloe's run.json migrated to spec objects, shape-preserving), the clerk's create model arg deleted, apiKeyEnv and the contextWindow alias gone. The live arc proven from a bare boot: invite minted, o/f auto-added, a custom provider with a bogus key (the recognizable 401), the auth fixed (13 models), the default pair selected, and a real glm-5.3 turn through the full path. Two live findings: opencode.ai serves 89 models keyless but 401s an empty Bearer header (the auth header rides only when a key exists), and the failure log line rides stderr.
the provider-flow round (2026-11-19, GM-designed): discovery IS the probe. The add takes only the endpoint -- {slug, baseUrl} or {builtin: true}, an apiKey in the body 400s (the auth route is the only key path) -- and a working keyless listing never sees a key field. A 401/403 stands the provider broken: the entity carries a probe record (outcome ok/auth/error, detail, at), the card opens the ask, and only the explicit acts move the record -- add, key, remove-key, discover; usage errors are transient and the op's to fix, nothing in the runtime flips provider state. The auth route is probe-then-save: the candidate key checks on reception, saves only on success -- a still-refused key never reaches the config, the previous state returns, the ask repeats. Every other discovery failure refuses the add outright with rollback: an endpoint that cannot answer is not a provider, nothing stands (the old code let it stand on any failure). The remove-key control (custom providers, armed) clears and re-probes: a provider gone public lands working keyless, a locked one re-opens the ask -- which is the key-replacement path. o/f never asks in any state: its auth failures are upstream breakage to show, not a prompt. The universal retry rule: NO automatic retries anywhere -- every inference call is user-initiated (the send, the housekeeping run, the refine) and every act has its manual retry; the curator's one auto-retry (the 2026-11-17 hiccup lesson) is deleted, the machine-label fallback the designed outcome. And failures never cache (GM: a failure is the absence of a measurement, not a measurement) -- the adjudication cache skips inference:false entries, the receipt line says "never cached, retried at every refine", and a transient battery error can never pin a false verdict; gev's 400s cost one cheap probe per refine and self-heal the day the backend learns tools. En-route fix the smoke earned: the create route validated body.spec AFTER the clerk built the run dir -- a refused spec left an orphan run (restart-check's exact count caught the sixth run); the spec validates first now, nothing lands on refusal.
the sheet's transport at the lint: the record is the only channel, the tag is view machinery composed by renderView, never authored by anyone -- the authored gate names the tag in any content (hers would never apply and primes the guarded emission; GM prose rides the view and story raw, the GM-side strips do not know it), names a sheet record on her turn, and checks object-ness on GM-turn records and boundary state notes both kinds; her emitted sheet in a carried tail stays legitimate history (the log keeps guard-conflicted emissions verbatim, and a stop can move one into the slidwin). The carryover editor teaches its own format now: a per-file hint between the editor and the save bar (one JSON object per line; status and agenda ride her turns; the sheet rides yours as the "sheet" field, never as a tag; the sigils and $VARS expand at start on the authored opening only).
the packages: template (the base for new packages -- the mood-indexed voice examples, the settings comment convention, non-playable as-is) and box (the sheet's first consumer: an incremental-game economy as ERP, GP and BP as two currencies with the room pushing her to fund her own punishments; the opening turn third-person per the typography ruling, ending on the display that needs a second read; the amnesiac rings empty by design; the erased name reading "(redacted)" after sigil expansion). The review found and fixed: both carryovers were pretty-printed JSON with duplicate keys -- unstartable under every per-line reader, flattened to true JSONL; the box's opening sheet rode her authored prose -- dead on every surface (the scan reads GM-turn records only, the strips eat it) and priming the exact emission the guard conflicts -- moved to the GM turn's sheet record, the override's shape; the gm.md "take of leave" typo fixed in all three packages.
the lint's rule 4 scoped to the substrate (GM, 2026-11-19): a banned word in a package's prose is fine -- the packages are the fiction and their words are the story's; README and the root prompts stay patrolled, and the box's "Or windows" is an architectural plural in a padded cell.
verified: typecheck green both programs; smoke ALL PASS (+27 the provider-flow battery: apiKey-on-add 400, the dead-endpoint refusal with config rollback, the auth-standing card with its probe record, probe-then-save both ways, the remove-key round trip, the o/f guards, the failures-never-cache proof over mock-fail -- verdict with the marker, absent from the cache file, re-probed fresh while the passing three serve from cache, not selectable, no orphan run; +8 the sheet-transport lint battery); restart + reminder checks green; a browser pass driving the whole add-flow machine on a mock+debug server (the hidden key field opening on the 401, the key landing through the auth route, the remove-key round trip re-opening the ask, the dead add refusing with the banner and no card, zero console errors beyond the expected 401/500 resource lines); check-repo clean with the box's windows. detention stays untracked WIP, untouched.
119 lines
4.7 KiB
JavaScript
119 lines
4.7 KiB
JavaScript
// server/providers.mjs -- the provider entities.
|
|
//
|
|
// A provider is a first-class thing (GM, 2026-11-18): a user-named
|
|
// slug, a baseUrl, optional auth, and the op's default model spec for
|
|
// it -- {model, effort}, never params (those are per-turn, the <mind>
|
|
// verb's alone). state/config.json is the truth, written only by the
|
|
// server; the SPA is the surface, zero hand edits. No legacy shapes
|
|
// are read: the config is born in this shape, and shape changes come
|
|
// with one-time migrations, not permanent tolerance (the no-legacy
|
|
// rule, GM, 2026-11-18).
|
|
//
|
|
// The built-in singleton: opencode/free -- the anonymous, keyless
|
|
// fallback, the harness thesis in one entry (playable with zero
|
|
// secrets). It never carries auth: if it starts answering 401, that
|
|
// is upstream breakage to show as an error, never a prompt to fix.
|
|
// It is always available: removable only while another provider
|
|
// exists, auto-added (with its discovery) whenever the provider list
|
|
// is empty -- the self-heal rule, broadly: an error that can be
|
|
// mechanically fixed to a reasonable degree without requiring human
|
|
// input gets fixed.
|
|
/** @typedef {import('./shapes.mjs').ProviderCfg} ProviderCfg */
|
|
/** @typedef {import('./shapes.mjs').DeployCfg} DeployCfg */
|
|
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { makeProvider } from './provider.mjs';
|
|
|
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
const CONFIG_PATH = path.join(ROOT, 'state', 'config.json');
|
|
|
|
/** The singleton: fixed slug, fixed base, structurally authless. */
|
|
export const OF_SLUG = 'opencode/free';
|
|
export const OF_BASE = 'https://opencode.ai/inference/openai/v1';
|
|
|
|
/** A slug: lowercase letters, digits, dashes; at most one slash
|
|
* (the singleton's own two-segment shape is the precedent). */
|
|
export const SLUG_RE = /^[a-z0-9][a-z0-9-]*(?:\/[a-z0-9][a-z0-9-]*)?$/;
|
|
|
|
/** @param {string} slug @returns {boolean} */
|
|
export function validSlug(slug) {
|
|
return SLUG_RE.test(slug) && slug.length <= 64;
|
|
}
|
|
|
|
/** @param {string} baseUrl @returns {boolean} */
|
|
export function validBaseUrl(baseUrl) {
|
|
try {
|
|
const u = new URL(baseUrl);
|
|
return u.protocol === 'https:' || u.protocol === 'http:';
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// --- the config file --------------------------------------------------------
|
|
|
|
/** @returns {DeployCfg} */
|
|
export function readConfig() {
|
|
try {
|
|
const cfg = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
|
|
if (cfg && Array.isArray(cfg.providers)) return cfg;
|
|
} catch { /* absent or unreadable -- the empty state is legal */ }
|
|
return { providers: [] };
|
|
}
|
|
|
|
/** @param {DeployCfg} cfg */
|
|
export function writeConfig(cfg) {
|
|
fs.mkdirSync(path.dirname(CONFIG_PATH), { recursive: true });
|
|
fs.writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2) + '\n');
|
|
}
|
|
|
|
/** @param {DeployCfg} cfg @param {string} slug @returns {?ProviderCfg} */
|
|
export function findProvider(cfg, slug) {
|
|
return cfg.providers.find((p) => p.slug === slug) ?? null;
|
|
}
|
|
|
|
/** The base-to-slug map -- what the routing errors and any
|
|
* base-keyed lookup read. */
|
|
/** @param {DeployCfg} cfg @returns {Map<string, string>} */
|
|
export function slugMap(cfg) {
|
|
return new Map(cfg.providers.map((p) => [p.baseUrl.replace(/\/+$/, ''), p.slug]));
|
|
}
|
|
|
|
/** Apply a mutated config: persist it and rebuild the context's
|
|
* clients. Every mutation goes through here -- the file and the map
|
|
* can never disagree.
|
|
* @param {import('./shapes.mjs').Ctx} ctx @param {DeployCfg} cfg @returns {DeployCfg} */
|
|
export function applyConfig(ctx, cfg) {
|
|
writeConfig(cfg);
|
|
ctx.providerCfg = cfg;
|
|
ctx.providers = new Map(cfg.providers.map((p) => [p.slug, makeProvider(p)]));
|
|
return cfg;
|
|
}
|
|
|
|
// --- the singleton ----------------------------------------------------------
|
|
|
|
/** Add the opencode/free singleton. Idempotent: returns null when it
|
|
* is already present (the add modal disables the choice, this is
|
|
* the backstop).
|
|
* @param {DeployCfg} cfg @returns {?ProviderCfg} */
|
|
export function addBuiltin(cfg) {
|
|
if (findProvider(cfg, OF_SLUG)) return null;
|
|
/** @type {ProviderCfg} */
|
|
const p = { slug: OF_SLUG, baseUrl: OF_BASE, default: null };
|
|
cfg.providers.push(p);
|
|
return p;
|
|
}
|
|
|
|
/** The removal guard (GM, 2026-11-18): the singleton goes only when
|
|
* a custom provider stands in its place -- the empty list is an
|
|
* error state, and the fallback is what keeps it unreachable.
|
|
* @param {DeployCfg} cfg @param {string} slug @returns {string} an error message, or '' when removable */
|
|
export function removalGuard(cfg, slug) {
|
|
if (slug !== OF_SLUG) return '';
|
|
if (cfg.providers.length > 1) return '';
|
|
return 'opencode/free is the anonymous fallback -- it is removable only while another provider is configured';
|
|
}
|
|
|