jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
// 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. */
|
|
|
|
|
/** @typedef {{ prompt_tokens: number, completion_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. */
|
|
|
|
|
/** @typedef {{ t: 'msg', m: WireMessage, reminder?: boolean, conflictOnly?: boolean, usage?: Usage }} MsgEvent */
|
|
|
|
|
|
|
|
|
|
/** 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 from before renames; 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[] }, conflicts?: string[] }} StatusNote */
|
|
|
|
|
/** @typedef {{ t: 'note', kind: 'agenda', applied: { added: string[], dropped: string[], evicted: string[] }, conflicts?: string[] }} AgendaNote */
|
|
|
|
|
/** @typedef {SessionNote|AskNote|PickNote|StatusNote|AgendaNote} 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} 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. */
|
|
|
|
|
/** @typedef {{ runId: string, n: number, stamp: string, file: string, lines: LogEvent[], pendingAsk: ?PendingAsk, usage: ?{ promptTokens: number, completionTokens: number }, append: (entry: LogEvent) => void }} LiveSession */
|
|
|
|
|
|
|
|
|
|
/** run.json: lineage, ownership, per-run model overrides. */
|
|
|
|
|
/** @typedef {{ package: string, created: string, owner?: ?string, forked?: number, models?: { player?: ?string, housekeeper?: ?string } }} RunMeta */
|
|
|
|
|
|
|
|
|
|
/** settings.json: identity (pc/name are the @ sigils), declared vars,
|
|
|
|
|
* the records flag. */
|
|
|
|
|
/** @typedef {{ pc?: string, name?: string, records?: 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 */
|
jsdoc typing round, phases 3-4: the clerk (tool/memory.mjs) and the SPA (web/) typed under strict -- row shapes local to their owners (the clerk's five tables in its header; the SPA's response envelopes in the new web/shapes.mjs, the server's shared shapes imported at their use sites: User, LogEvent, JobState, HttpError); the db boundary cast at every .get()/.all(), fail as @returns {never} so guard-narrowing works; web via a second tsconfig (tsconfig.web.json: DOM libs in their own program, node types riding along because shapes.mjs's Provider typedef pulls provider.mjs in), npm run typecheck runs both; every non-annotation delta audited onto a small intended list (COUNT(*) Number() wraps, impossible-path guards, ?? ''/?? undefined null-absorbers, the RewordOp reason fix -- the validator demands it, the reminder-find naming its tag); smoke 110/110 + restart + reminder + repo checks + a zero-console-error browser pass green; the typing round is complete
2026-11-15 19:57:07 +00:00
|
|
|
/** @typedef {{ id: string, detail: string, reason: string }} RewordOp */
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/** @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). */
|
|
|
|
|
/** @typedef {{ state: 'idle'|'running'|'applied'|'pending'|'rejected', phase?: 'stop'|'rerun', session?: number, target?: number, started?: string, stopped?: boolean, manifest?: string, reason?: 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 }[], 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 */
|
|
|
|
|
|
|
|
|
|
/** server.json as parsed. */
|
|
|
|
|
/** @typedef {{ bind?: string, port?: number, endpoint?: { baseUrl?: string, apiKey?: string, apiKeyEnv?: string }, models: { player?: ?string, housekeeper?: ?string }, contwin?: number, contextWindow?: number, params?: Record<string, unknown> }} ServerCfg */
|
|
|
|
|
|
|
|
|
|
/** The server context: the one object threaded through every module.
|
|
|
|
|
* locks/enqueue are the per-run activity lock (main.mjs). */
|
|
|
|
|
/** @typedef {{ mock: boolean, cfg: ServerCfg, provider: 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 */
|