const $ = id => document.getElementById(id);
const config = await (await fetch('/api/config?ref=1')).json();
// --- left panel ---------------------------------------------------------------
$('model').textContent = `${config.model} · ${config.effort} · claude code`;
$('prompt').value = config.prompt;
$('reset').onclick = () => { $('prompt').value = config.prompt; };
for (const chip of config.chips) {
const b = document.createElement('button');
b.textContent = chip;
b.onclick = () => {
const p = $('prompt');
p.value = `${p.value.trimEnd()}\n- ${chip}\n`;
p.scrollTop = p.scrollHeight;
};
$('chips').append(b);
}
const selected = new Set(config.inspo.map(i => i.url));
function renderInspo() {
$('inspo').replaceChildren(...config.inspo.map(img => {
const b = document.createElement('button');
b.title = img.name;
b.setAttribute('aria-pressed', selected.has(img.url));
b.innerHTML = `
`;
b.onclick = () => { selected.has(img.url) ? selected.delete(img.url) : selected.add(img.url); renderInspo(); };
return b;
}));
$('inspo-count').textContent = `${selected.size}/${config.inspo.length}`;
$('inspo-toggle').textContent = selected.size ? 'None' : 'All';
}
$('inspo-toggle').onclick = () => {
if (selected.size) selected.clear(); else config.inspo.forEach(i => selected.add(i.url));
renderInspo();
};
renderInspo();
// --- preview tabs ----------------------------------------------------------------
// Direction tabs come from folders the agent writes; the last tab is the activity log.
let tabs = []; // { name, url, version, fresh }
let log = []; // { kind, text }
let active = 'log'; // direction name or 'log'
let pinned = false; // user picked a tab, stop jumping to new directions
const escape = s => String(s).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
const status = s => { $('status').textContent = s; $('status').title = s; };
function renderTabs() {
const all = [...tabs.map((t, i) => ({ key: t.name, label: `${i + 1}. ${t.name}`, fresh: t.fresh })), { key: 'log', label: 'Log' }];
$('tabs').replaceChildren(...all.map(t => {
const b = document.createElement('button');
b.setAttribute('aria-selected', t.key === active);
b.innerHTML = `${t.key === 'log' ? '' : ``}${escape(t.label)}`;
b.onclick = () => { active = t.key; pinned = true; if (t.key !== 'log') tabs.find(x => x.name === t.key).fresh = false; renderTabs(); renderStage(); };
return b;
}));
}
let shown = null; // url+version currently in the iframe
function renderStage() {
const t = tabs.find(x => x.name === active);
$('open').hidden = !t;
if (t) {
$('open').href = t.url;
const src = `${t.url}?v=${t.version ?? 0}`;
if (shown === src) return;
shown = src;
const frame = document.createElement('iframe');
frame.sandbox = 'allow-scripts';
frame.src = src;
$('stage').replaceChildren(frame);
return;
}
shown = null;
if (!log.length) {
$('stage').innerHTML = '
Directions show up here as the agent writes them.
';
return;
}
const div = document.createElement('div');
div.className = 'log';
div.innerHTML = log.map(l => `${escape(l.text)}
`).join('');
$('stage').replaceChildren(div);
div.scrollTop = div.scrollHeight;
}
// --- runs ------------------------------------------------------------------------
let source = null;
let runId = null;
function setRunning(running) {
$('go').textContent = running ? 'Stop' : 'Generate';
$('go').classList.toggle('stop', running);
}
function attach(id, run) {
source?.close();
runId = id;
tabs = []; log = []; active = 'log'; pinned = false; shown = null;
if (run) {
$('prompt').value = run.prompt;
selected.clear(); run.images.forEach(u => selected.add(u)); renderInspo();
}
renderTabs(); renderStage();
setRunning(true);
status('starting…');
source = new EventSource(`/api/events?id=${encodeURIComponent(id)}`);
source.onmessage = e => handle(JSON.parse(e.data));
source.onerror = () => { source.close(); setRunning(false); };
}
function handle(ev) {
if (ev.type === 'activity') {
log.push(ev);
if (ev.kind === 'tool') status(ev.text);
if (active === 'log') renderStage();
} else if (ev.type === 'direction') {
tabs.push({ name: ev.name, url: ev.url, version: ev.mtime, fresh: true });
log.push({ kind: 'event', text: `new direction: ${ev.name}` });
if (!pinned) active = ev.name;
renderTabs(); renderStage();
} else if (ev.type === 'updated') {
const t = tabs.find(x => x.name === ev.name);
t.version = ev.mtime;
if (active === ev.name) renderStage(); else { t.fresh = true; renderTabs(); }
} else if (ev.type === 'end') {
const m = ev.meta;
if (m.notes) log.push({ kind: 'say', text: m.notes });
if (m.error) log.push({ kind: 'error', text: m.error });
const mins = m.durationMs ? `${Math.round(m.durationMs / 60000)} min · ` : '';
const cost = m.cost != null ? `$${m.cost.toFixed(2)}` : '';
status(m.error ? `error: ${m.error.split('\n').at(-1)}` : m.stopped ? `stopped · ${mins}${cost}` : `done · ${tabs.length} directions · ${mins}${m.turns ?? '?'} turns · ${cost}`);
source.close();
setRunning(false);
renderTabs(); renderStage();
loadRuns();
}
}
$('go').onclick = async () => {
if ($('go').classList.contains('stop')) {
await fetch(`/api/stop?id=${encodeURIComponent(runId)}`, { method: 'POST' });
return;
}
$('go').disabled = true;
status('setting up workspace…');
const res = await fetch('/api/run', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ ref: config.ref, prompt: $('prompt').value, images: [...selected] }),
});
$('go').disabled = false;
const { id } = await res.json();
attach(id);
loadRuns();
};
let runs = [];
async function loadRuns() {
runs = await (await fetch('/api/runs')).json();
$('runs').replaceChildren(new Option('Past runs', ''), ...runs.map(r => new Option(
`${r.startedAt.slice(5, 16).replace('T', ' ')} · ${r.live ? 'running' : `${r.directions?.length ?? 0} dirs`}${r.error ? ' · error' : ''}`, r.id)));
return runs;
}
$('runs').onchange = () => {
const run = runs.find(r => r.id === $('runs').value);
if (run) attach(run.id, run);
};
// Pick back up a run that's still going (e.g. after a page reload).
const running = (await loadRuns()).find(r => r.live);
if (running) attach(running.id, running);