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
This commit is contained in:
+47
-30
@@ -5,6 +5,10 @@
|
||||
// it) or start (the dormant session goes live; a carryover ending on
|
||||
// a GM turn sends automatically as her cue).
|
||||
|
||||
/** @typedef {import('./shapes.mjs').RunDetail} RunDetail */
|
||||
/** @typedef {import('./shapes.mjs').FilesResponse} FilesResponse */
|
||||
/** @typedef {import('./shapes.mjs').PromptsResponse} PromptsResponse */
|
||||
|
||||
import { $, el } from './dom.mjs';
|
||||
import { api } from './client.mjs';
|
||||
import { state } from './state.mjs';
|
||||
@@ -32,19 +36,23 @@ const EDIT_LIST = [
|
||||
];
|
||||
|
||||
// the editor's working set -- module-local, reset on every enter
|
||||
let files = null; // { name: { content, exists } }
|
||||
let drafts = {}; // unsaved edits by file name
|
||||
let currentFile = null;
|
||||
let fileSpecs = {};
|
||||
let repoPrompts = {};
|
||||
/** @type {Record<string, { mode: string, exists: boolean, content?: string }>} */ let files = {}; // the API surface, empty until enter
|
||||
/** @type {Record<string, string>} */ let drafts = {}; // unsaved edits by file name
|
||||
/** @type {?string} */ let currentFile = null;
|
||||
/** @type {Record<string, { name: string, mode: string, source: string, exists: boolean, content: string }>} */ let fileSpecs = {};
|
||||
/** @type {Record<string, string>} */ let repoPrompts = {};
|
||||
|
||||
const models = modelsBox('setup-models', { title: 'models -- per-run, over the deployment defaults' });
|
||||
|
||||
/** @param {?string} id @param {number} seq @returns {Promise<void>} */
|
||||
export async function enter(id, seq) {
|
||||
if (id == null) return; // unreachable: the router only enters setup with a run id
|
||||
state.runId = id;
|
||||
drafts = {};
|
||||
currentFile = null;
|
||||
let detail, filesResp, prompts;
|
||||
/** @type {RunDetail} */ let detail;
|
||||
/** @type {FilesResponse} */ let filesResp;
|
||||
/** @type {PromptsResponse} */ let prompts;
|
||||
try {
|
||||
[detail, filesResp, prompts] = await Promise.all([
|
||||
api(`/runs/${id}`),
|
||||
@@ -57,7 +65,7 @@ export async function enter(id, seq) {
|
||||
}
|
||||
if (seq !== state.seq) return;
|
||||
if (detail.phase !== 'dormant') return navigate(`/runs/${id}`); // live sessions live in play
|
||||
if (detail.run.owner !== state.me.id) return navigate(`/runs/${id}`); // not yours: read-only -- fork to edit
|
||||
if (!state.me || detail.run.owner !== state.me.id) return navigate(`/runs/${id}`); // not yours: read-only -- fork to edit (the router guarantees signed-in)
|
||||
|
||||
files = filesResp.files;
|
||||
repoPrompts = {
|
||||
@@ -69,7 +77,7 @@ export async function enter(id, seq) {
|
||||
$('setup-state').textContent = detail.draft
|
||||
? `${detail.run.package} \u00b7 ${id} -- draft; edits land when the session starts`
|
||||
: `${detail.run.package} \u00b7 ${id} -- session ${detail.sessions} dormant, the review window`;
|
||||
$('setup-start').disabled = false;
|
||||
/** @type {HTMLButtonElement} */ ($('setup-start')).disabled = false;
|
||||
$('setup-start').textContent = `start session ${detail.sessions}`;
|
||||
// draft-only affordances: deletion and save-as-package (a package is
|
||||
// a starting point, never a snapshot)
|
||||
@@ -78,7 +86,7 @@ export async function enter(id, seq) {
|
||||
|
||||
// prompt vars: the $TOKENS the prompts declare, filled per run
|
||||
const vars = scanVars(prompts, files);
|
||||
let currentVars = {};
|
||||
/** @type {Record<string, string>} */ let currentVars = {};
|
||||
try { currentVars = JSON.parse(files['settings.json']?.content ?? '{}').vars ?? {}; } catch { /* unreadable settings -- empty vars */ }
|
||||
const varsList = $('setup-vars-list');
|
||||
varsList.textContent = '';
|
||||
@@ -86,7 +94,7 @@ export async function enter(id, seq) {
|
||||
$('setup-vars-state').textContent = '';
|
||||
for (const tok of vars) {
|
||||
const row = el('label', 'field', `$${tok}`);
|
||||
const inp = el('input');
|
||||
const inp = /** @type {HTMLInputElement} */ (el('input'));
|
||||
inp.dataset.token = tok;
|
||||
inp.value = currentVars[tok] ?? '';
|
||||
row.appendChild(inp);
|
||||
@@ -120,13 +128,14 @@ export async function enter(id, seq) {
|
||||
}
|
||||
selectFile('system.md'); // how the model is prompted, first thing
|
||||
$('savepkg-state').textContent = '';
|
||||
$('savepkg-name').value = '';
|
||||
/** @type {HTMLInputElement} */ ($('savepkg-name')).value = '';
|
||||
}
|
||||
|
||||
// the prompts declare their own inputs: every $TOKEN in any prompt
|
||||
// file (repo-fed or the session's own, the carryover included -- it
|
||||
// expands at start like every authored surface) becomes a fillable
|
||||
// input in setup
|
||||
/** @param {PromptsResponse} prompts @param {Record<string, { content?: string }>} files @returns {string[]} */
|
||||
function scanVars(prompts, files) {
|
||||
const texts = [
|
||||
prompts.system, prompts.invariants, prompts.reminder,
|
||||
@@ -141,12 +150,14 @@ function scanVars(prompts, files) {
|
||||
return [...found].sort();
|
||||
}
|
||||
|
||||
/** @returns {Record<string, string>} */
|
||||
function collectVars() {
|
||||
const vars = {};
|
||||
for (const inp of $('setup-vars-list').querySelectorAll('input')) vars[inp.dataset.token] = inp.value.trim();
|
||||
/** @type {Record<string, string>} */ const vars = {};
|
||||
for (const inp of /** @type {NodeListOf<HTMLInputElement>} */ ($('setup-vars-list').querySelectorAll('input'))) vars[/** @type {string} */ (inp.dataset.token)] = inp.value.trim();
|
||||
return vars;
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
async function saveVars() {
|
||||
const vars = collectVars();
|
||||
if (!Object.keys(vars).length) return;
|
||||
@@ -154,6 +165,7 @@ async function saveVars() {
|
||||
$('setup-vars-state').textContent = 'saved';
|
||||
}
|
||||
|
||||
/** @param {string} f */
|
||||
function selectFile(f) {
|
||||
const prev = currentFile;
|
||||
if (prev === f) return;
|
||||
@@ -163,8 +175,8 @@ function selectFile(f) {
|
||||
const content = spec.source === 'repo'
|
||||
? (repoPrompts[f] ?? '')
|
||||
: (drafts[f] ?? spec.content ?? '');
|
||||
$('setup-edit').value = content;
|
||||
$('setup-edit').disabled = spec.mode !== 'rw';
|
||||
/** @type {HTMLTextAreaElement} */ ($('setup-edit')).value = content;
|
||||
/** @type {HTMLTextAreaElement} */ ($('setup-edit')).disabled = spec.mode !== 'rw';
|
||||
$('setup-save').hidden = spec.mode !== 'rw';
|
||||
$('setup-edit-state').textContent = spec.mode === 'rw' ? '' : 'read-only';
|
||||
renderHighlight();
|
||||
@@ -177,9 +189,9 @@ function selectFile(f) {
|
||||
const SIGIL_RE = /@(PC|Package)\b/g;
|
||||
const VAR_RE = /\$([A-Za-z_][A-Za-z0-9_]*)/g;
|
||||
function renderHighlight() {
|
||||
const ta = $('setup-edit');
|
||||
const ta = /** @type {HTMLTextAreaElement} */ ($('setup-edit'));
|
||||
const hl = $('edit-hl');
|
||||
const esc = ta.value.replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c]));
|
||||
const esc = ta.value.replace(/[&<>]/g, (c) => /** @type {Record<string, string>} */ ({ '&': '&', '<': '<', '>': '>' })[c]);
|
||||
hl.innerHTML = esc
|
||||
.replace(VAR_RE, '<span class="tok-var">$&</span>')
|
||||
.replace(SIGIL_RE, '<span class="tok-sigil">$&</span>')
|
||||
@@ -188,9 +200,10 @@ function renderHighlight() {
|
||||
hl.scrollLeft = ta.scrollLeft;
|
||||
}
|
||||
|
||||
/** @param {string} f */
|
||||
function stashDraft(f) {
|
||||
if (fileSpecs[f]?.mode !== 'rw') return; // read-only and repo-fed files never become drafts
|
||||
const v = $('setup-edit').value;
|
||||
const v = /** @type {HTMLTextAreaElement} */ ($('setup-edit')).value;
|
||||
if (v === (files[f]?.content ?? '')) { delete drafts[f]; markNav(); return; }
|
||||
drafts[f] = v;
|
||||
markNav();
|
||||
@@ -199,23 +212,24 @@ function stashDraft(f) {
|
||||
function markNav() {
|
||||
const items = $('setup-files').children;
|
||||
for (const item of items) {
|
||||
const name = item.querySelector('.file-name').textContent.replace(' (new)', '');
|
||||
const name = (/** @type {HTMLElement} */ (item.querySelector('.file-name'))).textContent.replace(' (new)', '');
|
||||
const dirty = drafts[name] !== undefined;
|
||||
item.classList.toggle('dirty', dirty);
|
||||
// the dirty marker is authored text, never css-generated -- the
|
||||
// rendered text must match the source (house rule, 2026-11-15)
|
||||
item.querySelector('.file-dirty').textContent = dirty ? '*' : '';
|
||||
(/** @type {HTMLElement} */ (item.querySelector('.file-dirty'))).textContent = dirty ? '*' : '';
|
||||
item.classList.toggle('current', name === currentFile);
|
||||
}
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
async function saveFile() {
|
||||
const f = currentFile;
|
||||
if (!f) return;
|
||||
$('setup-save').disabled = true;
|
||||
/** @type {HTMLButtonElement} */ ($('setup-save')).disabled = true;
|
||||
try {
|
||||
await api(`/runs/${state.runId}/files/${f}`, { method: 'PUT', body: { content: $('setup-edit').value } });
|
||||
files[f].content = $('setup-edit').value;
|
||||
await api(`/runs/${state.runId}/files/${f}`, { method: 'PUT', body: { content: /** @type {HTMLTextAreaElement} */ ($('setup-edit')).value } });
|
||||
files[f].content = /** @type {HTMLTextAreaElement} */ ($('setup-edit')).value;
|
||||
files[f].exists = true;
|
||||
delete drafts[f];
|
||||
$('setup-edit-state').textContent = 'saved';
|
||||
@@ -224,11 +238,12 @@ async function saveFile() {
|
||||
banner(e.message);
|
||||
$('setup-edit-state').textContent = 'not saved';
|
||||
}
|
||||
$('setup-save').disabled = false;
|
||||
/** @type {HTMLButtonElement} */ ($('setup-save')).disabled = false;
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
async function saveDirty() {
|
||||
stashDraft(currentFile);
|
||||
if (currentFile != null) stashDraft(currentFile); // unreachable guard: enter always selects a file first
|
||||
for (const [f, content] of Object.entries({ ...drafts })) {
|
||||
await api(`/runs/${state.runId}/files/${f}`, { method: 'PUT', body: { content } });
|
||||
files[f].content = content;
|
||||
@@ -241,26 +256,28 @@ async function saveDirty() {
|
||||
// save the run's craft as a new user package -- published to every user
|
||||
// of the deployment, gm.md included. Saves what the run has saved:
|
||||
// drafts flush first. The display name is the save's one decision.
|
||||
/** @returns {Promise<void>} */
|
||||
async function saveAsPackage() {
|
||||
const name = $('savepkg-name').value.trim();
|
||||
const name = /** @type {HTMLInputElement} */ ($('savepkg-name')).value.trim();
|
||||
if (!name) return banner('the package needs a display name');
|
||||
$('savepkg-btn').disabled = true;
|
||||
/** @type {HTMLButtonElement} */ ($('savepkg-btn')).disabled = true;
|
||||
$('savepkg-state').textContent = 'saving...';
|
||||
try {
|
||||
await saveDirty();
|
||||
const r = await api('/packages', { method: 'POST', body: { from: state.runId, name } });
|
||||
$('savepkg-state').textContent = `saved -- ${r.package.id}`;
|
||||
$('savepkg-name').value = '';
|
||||
/** @type {HTMLInputElement} */ ($('savepkg-name')).value = '';
|
||||
} catch (e) {
|
||||
banner(e.message);
|
||||
$('savepkg-state').textContent = 'not saved';
|
||||
}
|
||||
$('savepkg-btn').disabled = false;
|
||||
/** @type {HTMLButtonElement} */ ($('savepkg-btn')).disabled = false;
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
async function startRun() {
|
||||
const id = state.runId;
|
||||
const btn = $('setup-start');
|
||||
const btn = /** @type {HTMLButtonElement} */ ($('setup-start'));
|
||||
// the prompts' own inputs must be filled -- the guard at start is
|
||||
// the backstop, not the first line of defense
|
||||
for (const [tok, v] of Object.entries(collectVars())) {
|
||||
|
||||
Reference in New Issue
Block a user