Files

67 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

// web/view-pkgedit.mjs -- the package editor: the owner's authoring
// surface over one user package. Every file is authoring material
// (pre-run, settings included -- raw JSON for now, native UI later,
// developer 2026-11-19); the save lints are the cheap ones, the start
// gate judges the whole package when a run is created.
import { $ } from './dom.mjs';
import { api } from './client.mjs';
import { state } from './state.mjs';
import { banner } from './chrome.mjs';
import { navigate } from './router.mjs';
import { fileEditor } from './widgets.mjs';
// the package's own files, in authoring order -- settings first: the
// ready-to-run flag ("scaffold") is the first thing a fork's author
// needs to see
const EDIT_LIST = ['settings.json', 'backdrop.md', 'character.md', 'handbook.md', 'memory.json', 'carryover.jsonl', 'gm.md'];
/** @type {Record<string, string>} */
const FILE_HINTS = {
'settings.json':
'raw JSON for now (native UI later). The keys: "name" (display), "records" (the storage db on/off), '
+ '"scaffold" (true holds the package out of play -- flip it off when the package is ready to run), '
+ '"pc" (optional default; a run\'s create always asks).',
'carryover.jsonl':
'one JSON object per line. A turn is {"t": "msg", "m": {"role": "assistant" or "user", "content": "..."}}. '
+ 'Her turn carries the <status> and <agenda> blocks. @PC, @Package and $VARS expand at start.',
'gm.md': 'the package author\'s notes -- seeded into every run as the GM\'s notepad, never sent to the model.',
};
/** @type {?string} */ let pkgName = null; // the addressed package -- the view owns it
const editor = fileEditor(
{ nav: 'pkgedit-files', edit: 'pkgedit-edit', hl: 'pkgedit-hl', save: 'pkgedit-save', state: 'pkgedit-edit-state', hint: 'pkgedit-hint' },
{
put: (f, content) => api(`/packages/${pkgName}/files/${f}`, { method: 'PUT', body: { content } }),
hints: FILE_HINTS,
},
);
/** @param {?string} name @param {number} seq @returns {Promise<void>} */
export async function enter(name, seq) {
if (name == null) return navigate('/packages'); // unreachable: the router only enters with a name
let resp;
try {
resp = await api(`/packages/${name}/files`);
} catch (e) {
if (e.status === 403 || e.status === 404) {
navigate('/packages');
banner(e.status === 403 ? 'that package is not yours to edit' : `no such package: ${name}`);
return;
}
return banner(`could not reach the server: ${e.message}`); // stay put -- transient
}
if (seq !== state.seq) return;
pkgName = name;
$('pkgedit-title').textContent = resp.display;
$('pkgedit-state').textContent = `${name}${resp.scaffold ? ' -- scaffold: not startable until "scaffold" is off' : ' -- ready to run'}`;
editor.load(EDIT_LIST.map((f) => ({
name: f,
mode: resp.files[f]?.mode ?? 'rw',
exists: !!resp.files[f]?.exists,
content: resp.files[f]?.content ?? '',
})));
editor.select('settings.json');
}