Update the web console server, screens, help, prefs and loop setup, with matching page and logic suites

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-02 12:00:12 -08:00
1 parent 2468027a5a
commit 8e34bf1005
119 files changed
+9957 -3728

No files matched your search

@@ -0,0 +1,713 @@
// Plan2Code Web Console - User Preferences' Agents & models tab.
//
// A summary table of every agent (the built-in CLIs, the ones the person
// added, and a row kept for the v3 orchestrator) whose Edit opens that agent's
// full form. Nothing saves as it changes: a form edits a draft, and its Save
// sends the saved list with only that draft swapped in, so a half-typed
// command line never reaches agents.json or models.json.
import { el } from "../render.js";
import { hooks } from "../state.js";
const $ = (id) => document.getElementById(id);
// The CLIs the launcher reads a model list for, in table order (routes.mjs MODEL_CLIS).
const MODEL_CLIS = ["claude", "devin", "codex"];
// What the table calls each built-in, and the command it runs (the launcher's CLIS ids).
const BUILT_IN = { claude: "Claude", devin: "Devin", codex: "Codex" };
// The last server reply for each list. Unknown keys on an agent ride along untouched.
let saved = { agents: [], user: emptyLists(), shipped: emptyLists() };
// At most one per form being edited: "cli:<cli>" for a built-in's own models,
// "agent:<id>" for an added agent, "agent:new" for one being added.
const drafts = new Map();
// The form on screen (a drafts key), or null while the summary shows.
let openKey = null;
// The server's complaint about the open form, { field, error }, and a 409's
// warnings while Save anyway is on offer.
let formError = null;
let formReplace = null;
// Delete agent waiting for its second click; the arguments fold's state, kept across redraws.
let confirming = false;
let argsOpen = false;
// The badge each summary row last showed, by row key: { command, state }.
const found = new Map();
// Each open of the tab loads afresh: only the newest load's answers are drawn.
let loadSeq = 0;
function emptyLists() {
return Object.fromEntries(MODEL_CLIS.map((cli) => [cli, []]));
}
// Whether the status line says "Deleted.", which the next form opened clears
// (load warnings stay until the next open).
let deletedNote = false;
function setAgentsStatus(text, isError, { deleted = false } = {}) {
const status = $("agents-status");
status.textContent = text;
status.classList.toggle("error", Boolean(isError));
deletedNote = deleted;
}
function newAgent() {
return { name: "", command: "", args: [], prompt: "/plan2code", bypassArgs: [], modelArgs: ["--model", "{model}"], models: [] };
}
function normalizeAgent(agent) {
for (const key of ["args", "bypassArgs", "modelArgs", "models"]) if (!Array.isArray(agent[key])) agent[key] = [];
return agent;
}
const cliOf = (key) => key.slice("cli:".length);
const agentId = (key) => key.slice("agent:".length);
function savedEntry(key) {
if (key.startsWith("cli:")) return saved.user[cliOf(key)] || [];
if (key === "agent:new") return newAgent();
return saved.agents.find((a) => a.id === agentId(key));
}
// JSON with object keys sorted, so a label removed and typed back counts as unchanged.
function canonical(value) {
return JSON.stringify(value, (_, v) =>
v && typeof v === "object" && !Array.isArray(v) ? Object.fromEntries(Object.keys(v).sort().map((k) => [k, v[k]])) : v
);
}
function isDirty(key) {
return drafts.has(key) && canonical(drafts.get(key)) !== canonical(savedEntry(key));
}
function nameOf(key) {
if (key.startsWith("cli:")) return BUILT_IN[cliOf(key)];
const draft = drafts.get(key);
return String((draft && draft.name) || "").trim() || savedEntry(key)?.name || "a new agent";
}
const modelNames = (list) => list.map((m) => m.label || m.id);
/* ------------------------------------------------------------- summary */
// One row per agent, in order: the built-ins, the added agents, the orchestrator.
function summaryRows() {
return [
...MODEL_CLIS.map((cli) => ({
key: "cli:" + cli,
name: BUILT_IN[cli],
command: cli,
models: [...saved.shipped[cli], ...saved.user[cli]],
noneShip: saved.shipped[cli].length === 0,
})),
...saved.agents.map((a) => ({ key: "agent:" + a.id, name: a.name, command: a.command, models: a.models })),
];
}
function renderSummary() {
const body = $("agents-table").tBodies[0];
body.replaceChildren();
const check = [];
for (const row of summaryRows()) {
const tr = el("tr");
tr.dataset.key = row.key;
const name = el("td", "prefs-agents-name");
name.append(el("span", null, row.name), el("code", null, row.command));
if (row.noneShip) name.append(el("span", "help", `No models ship for ${row.name} yet; add your own in Edit.`));
const models = el("td", "prefs-agents-models");
const names = modelNames(row.models);
models.textContent = names.length ? `${names.length}: ${names.join(", ")}` : "None";
const badge = el("span", "prefs-badge");
const last = found.get(row.key);
if (last && last.command === row.command) drawBadge(badge, last.state);
else {
badge.textContent = "Checking...";
check.push(row);
}
const edit = el("button", "btn small", "Edit");
edit.type = "button";
edit.setAttribute("aria-label", "Edit " + row.name);
edit.addEventListener("click", () => openForm(row.key));
const editCell = el("td", "prefs-agents-edit");
editCell.append(edit);
const badgeCell = el("td");
badgeCell.append(badge);
tr.append(name, models, badgeCell, editCell);
body.appendChild(tr);
}
const soon = el("tr", "prefs-agents-soon");
const soonName = el("td", "prefs-agents-name");
soonName.append(el("span", null, "v3 orchestrator"), el("span", "help", "Nothing to set yet."));
soon.append(soonName, el("td", "prefs-agents-models"), el("td"), el("td"));
body.appendChild(soon);
if (check.length) checkFound(check, loadSeq);
}
const BADGES = {
ok: "Found",
missing: "Not found on PATH",
unknown: "Not checked",
};
function drawBadge(badge, state) {
badge.textContent = BADGES[state];
badge.classList.remove("ok", "missing");
if (state !== "unknown") badge.classList.add(state);
}
// Whether each row's command is on PATH, all at once. /agents/test only
// resolves the command and never runs it.
async function checkFound(rows, seq) {
await Promise.all(
rows.map(async (row) => {
let state = "unknown";
try {
const res = await fetch("/agents/test", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ command: row.command }),
});
if (res.ok) state = (await res.json()).found ? "ok" : "missing";
} catch {}
if (seq !== loadSeq) return;
found.set(row.key, { command: row.command, state });
const badge = $("agents-table").querySelector(`tr[data-key="${CSS.escape(row.key)}"] .prefs-badge`);
if (badge) drawBadge(badge, state);
})
);
}
/* ------------------------------------------------------------- the form */
function showSummary(on) {
$("agents-table").hidden = !on;
$("agents-add").hidden = !on;
$("agents-intro").hidden = !on;
$("agents-form").hidden = on;
}
function openForm(key) {
if (deletedNote) setAgentsStatus("", false);
openKey = key;
formError = null;
formReplace = null;
confirming = false;
argsOpen = key === "agent:new";
if (!drafts.has(key)) drafts.set(key, structuredClone(key === "agent:new" ? newAgent() : savedEntry(key)));
showSummary(false);
drawForm();
$("agents-form").querySelector("input")?.focus();
}
// Back on the summary, focus goes to the row just left (its Edit), or to Add
// agent when that row is gone (a new agent not saved, one just deleted).
function closeForm() {
const left = openKey;
if (openKey) drafts.delete(openKey);
openKey = null;
formError = null;
formReplace = null;
confirming = false;
$("agents-form").replaceChildren();
showSummary(true);
renderSummary();
const row = left && $("agents-table").querySelector(`tr[data-key="${CSS.escape(left)}"] .prefs-agents-edit button`);
(row || $("agents-add")).focus();
}
function edited() {
if (openKey) renderDirty(openKey);
}
function agentField(label, field, child) {
const wrap = el("div", "agent-field");
wrap.dataset.field = field;
wrap.append(el("span", null, label), child);
return wrap;
}
function agentInput(agent, key, label, placeholder, trim) {
const input = el("input");
input.value = agent[key] || "";
input.placeholder = placeholder;
input.setAttribute("aria-label", label);
input.dataset.field = key;
input.addEventListener("input", () => {
agent[key] = trim ? input.value.trim() : input.value;
edited();
});
return input;
}
// A list of text rows (arguments), with Remove on each and an Add under them.
function agentArgsEditor(agent, key, label, placeholder, addLabel) {
const box = el("div", "agent-rows");
box.dataset.field = key;
const draw = () => {
box.replaceChildren();
agent[key].forEach((value, i) => {
const row = el("div", "agent-row");
const input = el("input");
input.value = value;
input.placeholder = placeholder;
input.setAttribute("aria-label", label);
input.dataset.field = key;
input.addEventListener("input", () => {
agent[key][i] = input.value.trim();
edited();
});
const remove = el("button", "btn small ghost", "Remove");
remove.type = "button";
remove.addEventListener("click", () => {
agent[key].splice(i, 1);
draw();
edited();
});
row.append(input, remove);
box.appendChild(row);
});
const add = el("button", "btn small ghost", addLabel);
add.type = "button";
add.addEventListener("click", () => {
agent[key].push("");
draw();
edited();
box.querySelector(".agent-row:last-of-type input")?.focus();
});
box.appendChild(add);
};
draw();
return agentField(label, key, box);
}
// A models list: an id and a name for the menu on each row. An added agent's
// rows drop a blank name; a built-in's keep it, as models.json does.
function agentModelsEditor(list, { label: fieldLabel = "Models", optionalName = true } = {}) {
const box = el("div", "agent-rows");
box.dataset.field = "models";
const draw = () => {
box.replaceChildren();
list.forEach((m, i) => {
const row = el("div", "agent-row model");
const id = el("input");
id.value = m.id || "";
id.placeholder = optionalName ? "Model id, for {model}" : "Model id";
id.setAttribute("aria-label", "Model id");
id.dataset.field = "models";
const label = el("input");
label.value = m.label || "";
label.placeholder = optionalName ? "Name shown in the menu (optional)" : "Name shown in the menu";
label.setAttribute("aria-label", "Name shown in the menu");
label.dataset.field = "models";
const remove = el("button", "btn small ghost", "Remove");
remove.type = "button";
id.addEventListener("input", () => {
m.id = id.value.trim();
edited();
});
label.addEventListener("input", () => {
const text = label.value.trim();
if (text || !optionalName) m.label = text;
else delete m.label;
edited();
});
remove.addEventListener("click", () => {
list.splice(i, 1);
draw();
edited();
});
row.append(id, label, remove);
box.appendChild(row);
});
const add = el("button", "btn small ghost", "Add a model");
add.type = "button";
add.addEventListener("click", () => {
list.push(optionalName ? { id: "" } : { id: "", label: "" });
draw();
edited();
box.querySelector(".agent-row:last-of-type input")?.focus();
});
box.appendChild(add);
};
draw();
return agentField(fieldLabel, "models", box);
}
// The command's Test: where it would run from. Never saves.
function agentTest(agent) {
const wrap = el("div", "prefs-agents-test");
const test = el("button", "btn small", "Test");
test.type = "button";
const result = el("span", "help agent-test-result");
result.setAttribute("role", "status");
test.addEventListener("click", async () => {
const command = String(agent.command || "").trim();
if (!command) {
result.textContent = "Type a command first.";
return;
}
result.textContent = "Looking...";
try {
const res = await fetch("/agents/test", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ command }),
});
const body = await res.json();
if (!res.ok) result.textContent = body.error || "Could not test.";
else result.textContent = body.found ? "Found at " + body.path : "Not found on PATH";
} catch {
result.textContent = "Could not test.";
}
});
wrap.append(test, result);
return wrap;
}
function builtInFields(cli, draft) {
const shipped = el("div", "models-list prefs-agents-shipped");
shipped.setAttribute("aria-label", "Comes with Plan2Code");
for (const m of saved.shipped[cli]) {
const row = el("div", "models-row locked");
row.append(el("span", null, m.id), el("span", null, m.label || ""));
shipped.appendChild(row);
}
const note = el("div", "models-note");
note.innerHTML =
"<ul>" +
"<li>Restart <code>plan2code</code> to see a change in its model menu.</li>" +
"<li>At any time, run <code>plan2code --model &lt;id&gt;</code>, or pick your model inside your agent before running a skill.</li>" +
"<li>Updating Plan2Code refreshes the built-in list. Models you added stay, unless the built-in list now includes them.</li>" +
"<li>Devin models are limited to Low, Medium and High effort.</li>" +
"</ul>";
const comes = el("div", "agent-field");
comes.append(el("span", null, "Comes with Plan2Code"));
if (saved.shipped[cli].length) comes.append(shipped);
else comes.append(el("p", "help prefs-agents-none", `No models ship for ${BUILT_IN[cli]} yet; add your own below.`));
return [note, comes, agentModelsEditor(draft, { label: "Models you added", optionalName: false })];
}
function addedFields(draft) {
const command = agentField("Command", "command", agentInput(draft, "command", "Command", "Program to run, such as myagent", true));
command.append(agentTest(draft));
const fold = el("details", "prefs-agents-args");
fold.open = argsOpen;
fold.addEventListener("toggle", () => (argsOpen = fold.open));
fold.append(
el("summary", null, "Command-line arguments"),
agentArgsEditor(draft, "args", "Arguments", "--message={prompt}", "Add an argument"),
agentArgsEditor(draft, "bypassArgs", "Bypass arguments", "--yes-always", "Add an argument"),
agentArgsEditor(draft, "modelArgs", "Model arguments", "--model={model}", "Add an argument"),
);
return [
agentField("Name", "name", agentInput(draft, "name", "Name", "Name shown in the menu", true)),
command,
agentField("Start prompt", "prompt", agentInput(draft, "prompt", "Start prompt", "/plan2code (blank starts with nothing typed)", false)),
agentModelsEditor(draft.models),
fold,
];
}
// Save, Discard and the Unsaved badge (shown only while the draft differs),
// the Saved mark's spot, Save anyway after a 409, and Delete agent with its
// confirm on the form itself (never the browser's confirm()).
function formFoot(key) {
const foot = el("div", "agent-foot prefs-agents-foot");
const save = el("button", "btn small primary prefs-agents-save", "Save");
save.type = "button";
save.addEventListener("click", () => saveForm(key));
const discard = el("button", "btn small ghost prefs-agents-discard", "Discard");
discard.type = "button";
discard.addEventListener("click", () => discardOpen());
const badge = el("span", "prefs-unsaved-badge", "Unsaved");
foot.append(save, discard, badge);
if (formReplace) {
const anyway = el("button", "btn small danger prefs-agents-replace", "Save anyway");
anyway.type = "button";
anyway.addEventListener("click", () => saveForm(key, { replace: true }));
foot.append(anyway);
}
if (key.startsWith("agent:") && key !== "agent:new") {
if (confirming) {
const yes = el("button", "btn small danger", "Yes, delete");
yes.type = "button";
yes.addEventListener("click", () => deleteAgent(key));
const keep = el("button", "btn small ghost", "Keep");
keep.type = "button";
keep.addEventListener("click", () => {
confirming = false;
drawForm();
});
foot.append(el("span", "help", "Delete this agent?"), yes, keep);
} else {
const del = el("button", "btn small ghost danger prefs-agents-delete", "Delete agent");
del.type = "button";
del.addEventListener("click", () => {
confirming = true;
drawForm();
});
foot.append(del);
}
}
return foot;
}
function renderDirty(key) {
const foot = $("agents-form").querySelector(".prefs-agents-foot");
if (!foot || key !== openKey) return;
const dirty = isDirty(key);
for (const sel of [".prefs-agents-save", ".prefs-agents-discard", ".prefs-unsaved-badge"]) foot.querySelector(sel).hidden = !dirty;
}
// Which field or button has focus, so a redraw (an id assigned, an error
// marked) puts the person back where they were: { nth, start, end }. What
// they typed is already in the draft the redraw reads.
const FORM_FOCUSABLE = "input, button, summary";
function formFocus(form) {
const active = document.activeElement;
if (!active || !form.contains(active)) return null;
const at = { nth: [...form.querySelectorAll(FORM_FOCUSABLE)].indexOf(active) };
if (active instanceof HTMLInputElement) Object.assign(at, { start: active.selectionStart, end: active.selectionEnd });
return at;
}
function restoreFormFocus(form, at) {
if (!at) return;
const target = form.querySelectorAll(FORM_FOCUSABLE)[at.nth];
// A button the redraw hid (Save, once saved) hands focus to the heading.
if (!target || target.closest("[hidden]")) return form.querySelector(".prefs-agents-title")?.focus();
target.focus();
if (target instanceof HTMLInputElement && at.start !== undefined) {
try {
target.setSelectionRange(at.start, at.end);
} catch {}
}
}
function drawForm() {
const form = $("agents-form");
const key = openKey;
const draft = drafts.get(key);
const focus = formFocus(form);
form.replaceChildren();
const back = el("button", "btn small ghost prefs-agents-back", "Back to summary");
back.type = "button";
back.addEventListener("click", async () => {
if (await hooks.confirmUnsaved()) closeForm();
});
const title = el("h3", "prefs-agents-title");
title.tabIndex = -1;
if (key.startsWith("cli:")) title.append(el("span", null, BUILT_IN[cliOf(key)]), el("code", null, cliOf(key)));
else title.append(el("span", null, key === "agent:new" ? "New agent" : savedEntry(key).name));
const fields = el("div", "prefs-agents-fields");
fields.append(...(key.startsWith("cli:") ? builtInFields(cliOf(key), draft) : addedFields(draft)));
form.append(back, title, fields);
if (formError) {
for (const input of form.querySelectorAll("input")) {
if (input.dataset.field === formError.field) input.classList.add("invalid");
}
form.append(el("p", "agent-error", formError.error));
}
if (formReplace) form.append(el("p", "agent-error", formReplace.join(" ")));
form.append(formFoot(key));
renderDirty(key);
restoreFormFocus(form, focus);
}
/* ------------------------------------------------------------- saving */
// The list as the server wants it: blank rows and models without an id dropped.
function cleanAgent(a) {
return {
...a,
args: a.args.filter((v) => v !== ""),
bypassArgs: a.bypassArgs.filter((v) => v !== ""),
modelArgs: a.modelArgs.filter((v) => v !== ""),
models: a.models.filter((m) => m.id),
};
}
async function postJson(url, payload) {
const res = await fetch(url, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(payload),
});
return { res, body: await res.json() };
}
// The form's foot, for the Saved mark (or the error in its place).
const formMark = (result) => hooks.markSaved($("agents-form").querySelector(".prefs-agents-foot"), result);
// Fresh from what was just saved: the form redraws on a clean draft.
function savedAs(key) {
drafts.delete(openKey);
openKey = key;
drafts.set(key, structuredClone(savedEntry(key)));
formError = null;
formReplace = null;
drawForm();
}
async function saveModelsFor(cli) {
const sent = drafts.get("cli:" + cli).filter((m) => m.id);
try {
const { res, body } = await postJson("/models", { models: { ...saved.user, [cli]: sent } });
if (!res.ok) {
formMark({ ok: false, error: body.error || "Could not save." });
return false;
}
saved.user[cli] = (body.user && body.user[cli]) || sent;
} catch {
formMark({ ok: false, error: "Could not save." });
return false;
}
savedAs("cli:" + cli);
formMark({ ok: true, text: "Saved. Restart plan2code to use it." });
return true;
}
async function saveAgentFor(key, { replace = false } = {}) {
const draft = drafts.get(key);
const isNew = key === "agent:new";
if (isNew && (!String(draft.name || "").trim() || !String(draft.command || "").trim())) {
formError = { field: String(draft.name || "").trim() ? "command" : "name", error: "Give it a name and a command first." };
drawForm();
return false;
}
const at = isNew ? saved.agents.length : saved.agents.findIndex((a) => a.id === agentId(key));
const agents = saved.agents.map((a) => cleanAgent(a));
agents.splice(at, isNew ? 0 : 1, cleanAgent(draft));
try {
const { res, body } = await postJson("/agents", replace ? { agents, replace: true } : { agents });
if (res.status === 409) {
formError = { field: null, error: body.error || "Not saved." };
formReplace = Array.isArray(body.warnings) ? body.warnings : [];
drawForm();
return false;
}
if (!res.ok) {
formError = { field: body.index === at ? body.field : null, error: body.error || "Could not save." };
formReplace = null;
drawForm();
return false;
}
saved.agents = (Array.isArray(body.agents) ? body.agents : agents).map(normalizeAgent);
} catch {
formMark({ ok: false, error: "Could not save." });
return false;
}
const id = saved.agents[at].id;
found.delete(key);
found.delete("agent:" + id);
savedAs("agent:" + id);
formMark({ ok: true });
return true;
}
function saveForm(key, options) {
return key.startsWith("cli:") ? saveModelsFor(cliOf(key)) : saveAgentFor(key, options);
}
async function deleteAgent(key) {
const agents = saved.agents.filter((a) => a.id !== agentId(key)).map(cleanAgent);
confirming = false;
try {
const { res, body } = await postJson("/agents", { agents });
if (!res.ok) {
formError = { field: null, error: body.error || "Could not delete." };
drawForm();
return;
}
saved.agents = (Array.isArray(body.agents) ? body.agents : agents).map(normalizeAgent);
} catch {
formError = { field: null, error: "Could not delete." };
drawForm();
return;
}
found.delete(key);
closeForm();
setAgentsStatus("Deleted.", false, { deleted: true });
}
/* ------------------------------------------------------------- the dialog's side */
// An open form whose draft differs from what is saved.
export function hasUnsaved() {
return openKey !== null && isDirty(openKey);
}
// What the unsaved prompt calls the open form.
export function unsavedName() {
return openKey ? nameOf(openKey) : "";
}
export function saveOpen() {
return openKey ? saveForm(openKey) : Promise.resolve(true);
}
export function discardOpen() {
if (!openKey) return;
if (openKey === "agent:new") return closeForm();
drafts.set(openKey, structuredClone(savedEntry(openKey)));
formError = null;
formReplace = null;
confirming = false;
drawForm();
}
// Back into the form after Keep editing or a failed Save: the marked field, else the first.
export function focusOpen() {
const form = $("agents-form");
(form.querySelector("input.invalid") || form.querySelector("input"))?.focus();
}
// Each time the dialog opens: the summary, from the server.
export async function loadAgentsView() {
const seq = ++loadSeq;
openKey = null;
drafts.clear();
found.clear();
formError = null;
formReplace = null;
confirming = false;
$("agents-form").replaceChildren();
showSummary(true);
setAgentsStatus("", false);
const [agents, models] = await Promise.all([
fetch("/agents").then((r) => (r.ok ? r.json() : null)).catch(() => null),
fetch("/models").then((r) => (r.ok ? r.json() : null)).catch(() => null),
]);
if (seq !== loadSeq) return;
const problems = [];
if (agents) {
saved.agents = (Array.isArray(agents.agents) ? agents.agents : []).map(normalizeAgent);
if (Array.isArray(agents.warnings)) problems.push(...agents.warnings);
} else {
saved.agents = [];
problems.push("Agents could not be loaded.");
}
saved.user = emptyLists();
saved.shipped = emptyLists();
if (models) {
for (const cli of MODEL_CLIS) {
if (Array.isArray(models.user?.[cli])) saved.user[cli] = models.user[cli];
if (Array.isArray(models.shipped?.[cli])) saved.shipped[cli] = models.shipped[cli];
}
} else {
problems.push("Models could not be loaded.");
}
setAgentsStatus(problems.join(" "), problems.length > 0);
renderSummary();
}
export function wireAgentsView() {
$("agents-add").addEventListener("click", () => openForm("agent:new"));
}