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

This commit is contained in:
2026-11-15 15:28:56 +00:00
parent d737e47f82
commit f81b188ce1
22 changed files with 594 additions and 90 deletions
+37 -4
View File
@@ -7,12 +7,20 @@
// before every GM turn and rides it as a <status> block; the player's
// <lost>/<gained> deltas apply after its final message, and any
// conflicts ride the next GM turn as a <conflict> block.
/** @typedef {import('./shapes.mjs').Ctx} Ctx */
/** @typedef {import('./shapes.mjs').LiveSession} LiveSession */
/** @typedef {import('./shapes.mjs').Actor} Actor */
/** @typedef {import('./shapes.mjs').ViewMessage} ViewMessage */
import { clerkJson } from './clerk.mjs';
import { toolList, runTool, assembleReminder } from './bundle.mjs';
import { renderView } from './session.mjs';
import { parseVariants, pickVariant } from './variants.mjs';
export class HttpError extends Error {
/**
* @param {number} status @param {string} message @param {unknown} [detail]
*/
constructor(status, message, detail) {
super(message);
this.status = status;
@@ -20,17 +28,25 @@ export class HttpError extends Error {
}
}
/** @param {Ctx} ctx @param {string} runId @returns {LiveSession} */
function needSession(ctx, runId) {
const s = ctx.sessions.get(runId);
if (!s) throw new HttpError(409, `no open session for run '${runId}' -- POST /api/runs/${runId}/sessions first`);
return s;
}
/** The turn request's response: what the SPA needs to render the turn. */
/** @typedef {{ tools: string[], pendingAsk?: string, variants?: { count: number, chosen: number }, conflicts?: string[], player?: string }} TurnOut */
/**
* @param {Ctx} ctx @param {string} runId @param {string} line @param {?Actor} user
* @returns {Promise<TurnOut>}
*/
export async function gmTurn(ctx, runId, line, user) {
const s = needSession(ctx, runId);
if (s.pendingAsk) throw new HttpError(409, 'a question is pending -- answer it first (POST /ask)');
const status = clerkJson(runId, 'status'); // fresh per turn
const agenda = clerkJson(runId, 'agenda'); // fresh per turn
const status = /** @type {string[]} */ (clerkJson(runId, 'status')); // fresh per turn
const agenda = /** @type {string[]} */ (clerkJson(runId, 'agenda')); // fresh per turn
// the wire renders the queue as the model cites it: numbered from the top
const agendaWire = agenda.map((d, i) => `${i}. ${d}`).join('; ');
const content = `${line.trim()}\n\n<status>${status.join(', ')}</status>\n<agenda>${agendaWire}</agenda>`;
@@ -45,10 +61,18 @@ export async function gmTurn(ctx, runId, line, user) {
// the opening auto-send: a carryover that ends on a GM turn is her
// cue -- the first request of the assembled contwin fires with no new
// line (the cue is already the log's last word)
/**
* @param {Ctx} ctx @param {string} runId @param {?Actor} user
* @returns {Promise<TurnOut>}
*/
export async function openingCall(ctx, runId, user) {
return runLoop(ctx, runId, user);
}
/**
* @param {Ctx} ctx @param {string} runId @param {string} answer @param {?Actor} user
* @returns {Promise<TurnOut>}
*/
export async function answerAsk(ctx, runId, answer, user) {
const s = needSession(ctx, runId);
if (!s.pendingAsk) throw new HttpError(409, 'no pending question');
@@ -65,9 +89,11 @@ export async function answerAsk(ctx, runId, answer, user) {
return runLoop(ctx, runId, user);
}
/** @param {Ctx} ctx @param {string} runId @param {?Actor} user @returns {Promise<TurnOut>} */
async function runLoop(ctx, runId, user) {
const s = ctx.sessions.get(runId);
const s = needSession(ctx, runId);
const tools = toolList(runId);
/** @type {TurnOut} */
const out = { tools: [] };
let heals = 0;
for (;;) {
@@ -155,8 +181,9 @@ async function runLoop(ctx, runId, user) {
}
}
/** @param {Ctx} ctx @param {string} runId @returns {?string} */
function resolveModel(ctx, runId) {
const meta = ctx.runMeta(runId) ?? {};
const meta = ctx.runMeta(runId) ?? /** @type {import('./shapes.mjs').RunMeta} */ ({});
return meta.models?.player ?? ctx.cfg.models?.player ?? null;
}
@@ -173,7 +200,13 @@ function resolveModel(ctx, runId) {
// path), the answer lands with status and agenda deltas (the tag
// transports), then the drops, a bad drop, its self-heal, and
// everything after is plain prose.
/** @typedef {import('./shapes.mjs').WireMessage} WireMessage */
/**
* @param {LiveSession} s @param {ViewMessage[]} view @returns {WireMessage}
*/
function mockModel(s, view) {
/** @param {string} name @param {Record<string, unknown>} args @param {string} id @returns {WireMessage} */
const call = (name, args, id) => ({
role: 'assistant',
content: null,