0ddb9a218d
The design conversation settled the verb question (developer, 2026-11-19): a "create new package" with a base selector is fork under another name, so the SPA gains one list at /packages with three acts per card -- start (the run-create flow, unchanged), fork (any package, system or user, by anyone: the copy lands in state/packages owned by the forker, pc stripped, display name asked inline, the fork landing straight in the editor -- nobody forks to immediately start), and edit (the owner's own user packages only). /new retires to a redirect onto /packages; the source link (code.fleshless.org/hackmaster/tiny-worlds) rides the header.
The template's unplayability became machinery instead of a naming special-case: settings.json carries the "scaffold" flag (the records-bool precedent -- authored content carries the marker, machinery reads it). Create refuses a scaffold loudly on both ends ("fork it to author, then flip scaffold off in the editor"), the listing marks it, the SPA disables its start. The developer's ruling over my clear-the-flag draft: the flag RIDES FORKS UNTOUCHED -- a scaffold fork stays a scaffold until its author flips it off, so the flag is the package's doneness state, and publishing your own scaffold for others to fork is free. The settings editor is raw JSON for now (native UI later, the developer's "ready to run" toggle its eventual shape), with a hint teaching the keys.
The package editor (/packages/:name): every file of a user package is authoring material -- settings included -- with the cheap save lints (memory.json and settings.json must parse as JSON objects; the carryover passes the authored lint, the run surface's exact gate), the deep checks staying at the start gate where they already lived. The gates mirror the run precedent: system packages are repo-side and refuse ("fork to edit"); an ownerless user package is readable, forkable, never editable -- and an ownerless package IS forkable, the copy is yours.
The refactor the editor forced: the setup view's file-editor machinery (nav, textarea + the sigil/var highlight overlay, drafts, dirty marks, the save flow) extracted into widgets.mjs's fileEditor factory, parameterized over DOM ids and a PUT call -- setup keeps its run-side couplings (the phase-gated rw, the addressed session), the package editor is the simpler parameterization, and the editor CSS generalized from id selectors to classes so both surfaces share it.
En-route bug found and fixed: cmdCreate always copied the REPO's handbook.md into the run, so a user package's handbook -- which save-package faithfully copies out -- was silently ignored on re-create; the author's craft edit died in the round-trip. Create now prefers the package's own handbook when it carries one, else the repo's shared copy. The smoke proves the round-trip: a handbook marker authored through the package editor surfaces in the created run's files.
Browser-pass find: the pkgedit header state span and the editor's save-state span shared the id pkgedit-state, so the widget's select() wiped the header line -- the editor's span renamed (pkgedit-edit-state), verified rendering.
Verified: typecheck green both programs; smoke ALL PASS (+20 -- the fork battery: the template fork owner-stamped scaffold-preserved pc-stripped, create refusing the scaffold on both ends, the playable fork carrying no scaffold, fork validation, the editor gates owner-200/non-owner-403/system-repo-side-403/unknown-404, the raw-JSON and carryover save lints, the ready-to-run flip, create from the flipped fork, the handbook round-trip; and the ownerless pair: the editor refuses, the fork succeeds); restart + reminder checks green (6 runs); a browser pass on --mock --debug end to end -- the paste token re-landing after the state wipe, /new redirecting with the URL honest, the template card disabled with the scaffold note, boutique disabled as incomplete, the fork form prefill and submit landing in the editor, the scaffold state line, the settings flip and save making the card startable, the start-form flow intact, zero console errors; check-repo clean; the debug server back on the real state. Docs: README's packages passage (fork, scaffold, the editor, the gates), the booting flow, the layout tree (the clerk's fork-package verb, the view list, widgets.mjs); DICTIONARY's package, scaffold, and fork entries. The developer's queue line ('"create new package" in SPA') is struck by them in the same round. packages/detention stays untracked WIP, untouched.
205 lines
14 KiB
JavaScript
205 lines
14 KiB
JavaScript
// server/shapes.mjs -- the shared type declarations. No runtime code:
|
|
// this module exists so the tsc checker (JSDoc mode, no emit) and the
|
|
// humans reading the code agree on the load-bearing shapes. Every
|
|
// module types against these; the smoke owns their behavioral truth.
|
|
//
|
|
// The naming follows the code's own words: a log line is an event, a
|
|
// wire message is exactly what the API carried, the view is what the
|
|
// model sees on any call.
|
|
export {};
|
|
|
|
/** A role on the chat wire. */
|
|
/** @typedef {'user'|'assistant'|'system'|'tool'} Role */
|
|
|
|
/** One function call attached to an assistant message. */
|
|
/** @typedef {{ id: string, type: 'function', function: { name: string, arguments: string } }} ToolCall */
|
|
|
|
/** A tool definition on the wire: what the API receives in `tools`. */
|
|
/** @typedef {{ type: 'function', function: { name: string, description: string, parameters: { type: string, properties: Record<string, unknown>, required?: string[] } } }} ToolDef */
|
|
|
|
/** A chat message exactly as it rides the wire (OpenAI-compatible). */
|
|
/** @typedef {{ role: Role, content: ?string, tool_calls?: ToolCall[], reasoning_content?: string }} WireMessage */
|
|
|
|
/** Token usage, as the provider reports it and the log records it.
|
|
* completion_tokens_details carries the reasoning split when the
|
|
* provider states it (the discovery battery measures it). */
|
|
/** @typedef {{ prompt_tokens: number, completion_tokens: number, completion_tokens_details?: { reasoning_tokens?: ?number } }} Usage */
|
|
|
|
/** msg: one recorded turn (m is exactly what the wire carried). The
|
|
* reminder and conflictOnly flags are the only server-side marks --
|
|
* everything else about a turn lives in its content. via is the
|
|
* request record: the resolved model and the effective params (the
|
|
* merged layer actually sent) -- model-facing surfaces never render
|
|
* it, the raw surfaces always do. via.model is the bare model id;
|
|
* via.provider names the route (absent in pre-spec logs). override
|
|
* is the GM's block edit, arriving with a non-OOC GM turn: the log's
|
|
* record of it. The composition overwrites her newest blocks with
|
|
* these values (the scan reads it newest-wins); the log itself never
|
|
* changes. */
|
|
/** @typedef {{ t: 'msg', m: WireMessage, reminder?: boolean, conflictOnly?: boolean, usage?: Usage, via?: { model: string, provider?: string, params: RoleParams }, override?: { status?: string[], agenda?: string[] }, sheet?: Record<string, unknown> }} MsgEvent */
|
|
|
|
/** req: a failed provider request -- the resolved model (and its
|
|
* provider route), the effective params, and the failure. No
|
|
* message resulted (the turn fails loud, the retry is the user's);
|
|
* this is the record of what was attempted, surfaced on the raw
|
|
* surfaces like a note. */
|
|
/** @typedef {{ t: 'req', model: string, provider?: string, params: RoleParams, fail: string }} ReqEvent */
|
|
|
|
/** tool: a tool result, paired to its call (pairing is a context
|
|
* invariant, checked at render). */
|
|
/** @typedef {{ t: 'tool', m: { role: 'tool', tool_call_id: string, content: string }, ask?: boolean }} ToolEvent */
|
|
|
|
/** note: the machine's own annotations -- never shown to the model,
|
|
* rendered as garnish in the SPA. Historical logs carry older kind
|
|
* names and the delta-shaped fields from before the full-block
|
|
* protocol; readers must not assume this union is exhaustive. */
|
|
/** @typedef {{ t: 'note', kind: 'session', n: number }} SessionNote */
|
|
/** @typedef {{ t: 'note', kind: 'ask', question: string }} AskNote */
|
|
/** @typedef {{ t: 'note', kind: 'pick', chosen: number, count: number }} PickNote */
|
|
/** @typedef {{ t: 'note', kind: 'status', applied?: { lost: string[], gained: string[] }, set?: string[], conflicts?: string[] }} StatusNote */
|
|
/** @typedef {{ t: 'note', kind: 'agenda', applied?: { added: string[], dropped: string[], evicted: string[] }, set?: string[], evicted?: string[], conflicts?: string[] }} AgendaNote */
|
|
/** @typedef {{ t: 'note', kind: 'mind', params: RoleParams }} MindNote */
|
|
/** state: the boundary snapshot -- stop writes it at the carryover's
|
|
* head when the closing log holds any status or agenda, so the new
|
|
* session derives state even when her newest emission fell outside
|
|
* the slidwin tail. Machinery like every note: never rendered, the
|
|
* scan reads it newest-wins (it loses to any emission inside the
|
|
* tail by position). */
|
|
/** @typedef {{ t: 'note', kind: 'state', status?: string[], agenda?: string[], sheet?: Record<string, unknown> }} StateNote */
|
|
/** start: the frozen-opening boundary (rewind, developer, 2026-11-17) --
|
|
* written once at start, right after the consumed carryover: the
|
|
* opening (authored or carried) above it is what a rewind can never
|
|
* reach. A session whose log predates the build carries none, and
|
|
* its rewind refuses loudly. */
|
|
/** @typedef {{ t: 'note', kind: 'start' }} StartNote */
|
|
/** @typedef {SessionNote|AskNote|PickNote|StatusNote|AgendaNote|MindNote|StateNote|StartNote} NoteEvent */
|
|
|
|
/** bundle: one frozen prompt part (system, backdrop, character, rings). */
|
|
/** @typedef {{ t: 'bundle', m: { role: 'user'|'system', content: string }, part: string }} BundleEvent */
|
|
|
|
/** opening: her opening status -- authored carryovers only, at most one. */
|
|
/** @typedef {{ t: 'opening', status: string[] }} OpeningEvent */
|
|
|
|
/** Any line of a session log or carryover, in either direction. */
|
|
/** @typedef {MsgEvent|ToolEvent|NoteEvent|BundleEvent|OpeningEvent|ReqEvent} LogEvent */
|
|
|
|
/** The pending ask, held on the session object while a turn waits. */
|
|
/** @typedef {{ callId: string, question: string, calls: ToolCall[] }} PendingAsk */
|
|
|
|
/** A live session: the log in memory, its derived state, the appender.
|
|
* The log is the truth; everything on this object is rebuilt from it.
|
|
* mind is the one-shot mind layer (the <mind> verb): the params her
|
|
* NEXT turn rides at, cleared when that turn completes -- derived
|
|
* from the last mind note, so a restart mid-window loses nothing. */
|
|
/** @typedef {{ runId: string, n: number, stamp: string, file: string, lines: LogEvent[], pendingAsk: ?PendingAsk, mind: ?RoleParams, usage: ?{ promptTokens: number, completionTokens: number }, progress: ?TurnProgress, append: (entry: LogEvent) => void }} LiveSession */
|
|
/** The turn meter's live record: what the streaming callback knows. `phase` is what is arriving now; `started` is the provider call's epoch ms. */
|
|
/** @typedef {{ phase: 'thinking'|'prose', thinking: number, prose: number, started: number }} TurnProgress */
|
|
|
|
/** One model spec: the complete inference request identity (developer,
|
|
* 2026-11-18) -- which provider routes it, the model id, and the
|
|
* reasoning effort, all three required (there is no unset effort:
|
|
* it substantially changes inference quality, so it is a property
|
|
* of the model pick, never a free-floating knob). The fourth spec
|
|
* member, sampling params, is optional and never stored: the <mind>
|
|
* verb sets it for one specific turn. */
|
|
/** @typedef {{ provider: string, model: string, effort: string }} ModelSpec */
|
|
|
|
/** run.json: lineage, ownership, and the run's model picks -- full
|
|
* specs, per role. (The retired per-run params layer is gone
|
|
* entirely, developer, 2026-11-18: params are per-turn via <mind>, and the
|
|
* no-legacy rule migrated the last carrier away.) */
|
|
/** @typedef {{ package: string, created?: string, owner?: ?string, forked?: number, models?: { player?: ?ModelSpec, housekeeper?: ?ModelSpec } }} RunMeta */
|
|
|
|
/** The named sampling knobs -- the <mind> verb's four (hers to set
|
|
* for one turn), plus the wire-side extras the substrate itself
|
|
* rides: reasoning_effort always comes from the model spec, never
|
|
* the mind layer (hidden from the model, not her call). Numbers
|
|
* ride the wire verbatim. */
|
|
/** @typedef {{ temperature?: number, top_p?: number, top_k?: number, repetition_penalty?: number, max_tokens?: number, reasoning_effort?: string }} RoleParams */
|
|
|
|
/** One model of a discovered provider's registry section. Preliminary
|
|
* entries carry the scrape only (status unrefined); refined entries
|
|
* carry the battery's verdicts and the curator's presentation. The
|
|
* section key is the provider slug -- no routing tag needed, the
|
|
* section names the route. think is the battery's best measured
|
|
* thinking-token count. efforts is the reasoning-effort probe: the
|
|
* measured mean thinking per level of the ladder; effortDefault is
|
|
* the level with the highest observed mean -- pre-fill and hint
|
|
* only (developer, 2026-11-18: with effort a property of the pick, nothing
|
|
* rides implicitly anymore; it pre-selects the selection prompt's
|
|
* dropdown and informs the curator's recommendation, no more). */
|
|
/** @typedef {{ id: string, input: ?number, output: ?number, status?: 'unrefined', inference?: boolean, fail?: string, think?: ?number, reasoning?: 'off'|'low'|'standard'|'deep'|null, offSwitch?: Record<string, unknown>|null, efforts?: ?Record<string, number>, effortDefault?: ?string, latency?: ?number, family?: ?string, label?: ?string, description?: ?string, roles?: { player: boolean, housekeeper: boolean } }} RegistryModel */
|
|
|
|
/** One provider's registry section (state/models.json): written by
|
|
* that provider's discovery and refine alone, deletable whole on
|
|
* provider removal. recommendation is the curator's overall-best
|
|
* {model, effort} -- intel only, shown post-refine, applied by the
|
|
* op's click, never auto-landed and never deterministic on a bad
|
|
* model day (developer, 2026-11-18: on a bad day the op gets no advice,
|
|
* not machine advice). */
|
|
/** @typedef {{ phase: 'preliminary'|'refined', discovered: string, refined: ?string, models: RegistryModel[], curator?: ?string, recommendation?: ?{ model: string, effort: string } }} RegistrySection */
|
|
|
|
/** state/models.json: the per-provider registry -- the truth of what
|
|
* is selectable, one section per configured provider slug. A
|
|
* provider absent from the config has no section (removal deletes
|
|
* it); a provider not yet discovered has none either. The singular
|
|
* and flat-vector era shapes migrate in memory on open. */
|
|
/** @typedef {{ providers: Record<string, RegistrySection> }} ModelRegistry
|
|
|
|
/** settings.json: identity (pc/name are the @ sigils), declared vars,
|
|
* the records flag. */
|
|
/** @typedef {{ pc?: string, name?: string, records?: boolean, scaffold?: boolean, vars?: Record<string, string> }} RunSettings */
|
|
|
|
/** A housekeeping manifest -- the housekeeper's whole write surface. */
|
|
/** @typedef {{ ring: 'a'|'b', type: string, label: string, detail?: string, reason: string }} InsertOp */
|
|
/** @typedef {{ id: string, reason: string }} RefOp */
|
|
/** @typedef {{ id: string, detail: string, reason: string }} RewordOp */
|
|
/** @typedef {{ type: string, label: string, detail: string }} RecordOp */
|
|
/** @typedef {{ session: string, summary: string, insert?: InsertOp[], touch?: RefOp[], promote?: RefOp[], demote?: RefOp[], reword?: RewordOp[], delete?: RefOp[], archive?: RefOp[], record?: RecordOp[], inventions?: string[], open_questions?: string[] }} Manifest */
|
|
|
|
/** The housekeeping job state (GET /housekeeping); discovery's refine
|
|
* job reuses the shape under its own map key. */
|
|
/** @typedef {{ state: 'idle'|'running'|'applied'|'pending'|'rejected', phase?: 'stop'|'rerun'|'refine', session?: number, target?: number, started?: string, stopped?: boolean, manifest?: string, reason?: string, steps?: { total: number, done: number, label: string, chars?: number }, lines?: string[] }} JobState */
|
|
|
|
/** The clerk boundary's result: exit code plus separated streams. */
|
|
/** @typedef {{ code: number, out: string, err: string }} ClerkResult */
|
|
|
|
/** One message of the rendered per-call view -- what the model sees. */
|
|
/** @typedef {{ role: Role, content: ?string, tool_calls?: ToolCall[], tool_call_id?: string }} ViewMessage */
|
|
|
|
/** A chat completion response (OpenAI-compatible). */
|
|
/** @typedef {{ choices: { message: WireMessage, finish_reason: ?string }[], usage?: Usage }} ChatResponse */
|
|
|
|
/** An Error carrying an HTTP status (and optional detail payload) --
|
|
* the server's only error shape. */
|
|
/** @typedef {Error & { status?: number, detail?: unknown }} HttpError */
|
|
|
|
/** The acting user, as the auth spine resolves a bearer token. */
|
|
/** @typedef {{ id: string, name: string, superuser: boolean }} User */
|
|
|
|
/** The acting user as the loops and jobs log it: attribution needs
|
|
* only the id (users.mjs's row, with its numeric superuser flag,
|
|
* flows in fine). */
|
|
/** @typedef {{ id: string }} Actor */
|
|
|
|
/** The provider client (provider.mjs's own return type). */
|
|
/** @typedef {ReturnType<typeof import('./provider.mjs').makeProvider>} Provider */
|
|
|
|
/** One configured provider: a slug, a baseUrl, optional auth, and
|
|
* the op's default model spec for it ({model, effort} -- params are
|
|
* per-turn via <mind>, never in the provider default). The
|
|
* opencode/free singleton is this shape with the slug and baseUrl
|
|
* fixed and auth structurally absent. */
|
|
/** @typedef {{ slug: string, baseUrl: string, apiKey?: string, default?: ?{ model: string, effort: string }, probe?: ?{ outcome: 'ok' | 'auth' | 'error', detail: string, at: string } }} ProviderCfg */
|
|
|
|
/** state/config.json: the deployment's provider entities -- server
|
|
* written only (the SPA is the surface, zero hand edits). */
|
|
/** @typedef {{ providers: ProviderCfg[] }} DeployCfg */
|
|
|
|
/** The server context: the one object threaded through every module.
|
|
* locks/enqueue are the per-run activity lock (main.mjs). providers
|
|
* is the provider clients keyed by slug (the config's truth,
|
|
* rebuilt on every config mutation); providerCfg is the config
|
|
* itself. */
|
|
/** @typedef {{ mock: boolean, providerCfg: DeployCfg, providers: Map<string, Provider>, sessions: Map<string, LiveSession>, jobs: Map<string, JobState>, runMeta: (runId: string) => ?RunMeta, locks: Map<string, Promise<void>>, enqueue: <T>(runId: string, fn: () => Promise<T>) => Promise<T> }} Ctx */
|