feat: run each loop phase on its own strength (Thrifty, Balanced, Best)

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-04 19:23:52 -08:00
1 parent e963513a8a
commit 81d14c8e78
41 files changed
+3290 -81

No files matched your search

@@ -11,6 +11,22 @@ export const MODEL_CLIS = ["claude", "devin", "codex"];
// What the table calls each built-in, and the command it runs (the launcher's CLIS ids).
export const BUILT_IN = { claude: "Claude", devin: "Devin", codex: "Codex" };
// The built-ins that take an effort beside the model (agents.rs has_effort).
export const HAS_EFFORT = ["claude", "codex"];
// The loop's strength mapping as the page keeps it: the shipped rows and the
// person's own, each { <agent id>: { thrifty?, balanced?, best? } }.
export function emptyStrengths() {
return { shipped: {}, user: {} };
}
// The agent a form's strengths belong to: a built-in's cli, an added agent's id.
// An agent not yet saved has no id, so nothing of anyone else's can be its rows.
export function strengthOwner(key) {
if (key === "agent:new") return null;
return key.startsWith("cli:") ? cliOf(key) : agentId(key);
}
export function emptyLists() {
return Object.fromEntries(MODEL_CLIS.map((cli) => [cli, []]));
}
@@ -44,8 +60,15 @@ export function canonical(value) {
);
}
export function isDirty(saved, drafts, key) {
return drafts.has(key) && canonical(drafts.get(key)) !== canonical(savedEntry(saved, key));
// `strengthDrafts` holds, per form key, the person's own strength rows once the
// fold changed them; absent means the fold was left alone.
export function isDirty(saved, drafts, key, strengthDrafts) {
if (!drafts.has(key)) return false;
if (canonical(drafts.get(key)) !== canonical(savedEntry(saved, key))) return true;
const rows = strengthDrafts && strengthDrafts.get(key);
if (rows === undefined) return false;
const was = (saved.strengths && saved.strengths.user[strengthOwner(key)]) || {};
return canonical(rows) !== canonical(was);
}
export function nameOf(saved, drafts, key) {
@@ -10,7 +10,8 @@
import { el } from "../../render.js";
import { listEditor } from "./agents-list-editor.js";
import { BUILT_IN, cliOf, savedEntry, isDirty } from "./agents-data.js";
import { BUILT_IN, HAS_EFFORT, cliOf, savedEntry, isDirty } from "./agents-data.js";
import { strengthsFold } from "./agents-strengths.js";
function agentField(label, field, child) {
const wrap = el("div", "agent-field");
@@ -89,7 +90,11 @@ function builtInFields(ctx, cli, draft) {
"models",
listEditor({ list: draft, kind: "models", field: "models", optionalName: false, addLabel: "Add a model", onChange: ctx.edited })
);
return [fold(ctx, "modelsOpen", "Models", "agent-fold", [note, comes, yours])];
const models = () => [...ctx.saved.shipped[cli], ...draft];
return [
fold(ctx, "modelsOpen", "Models", "agent-fold", [note, comes, yours]),
...(ctx.modelsLoaded ? [strengthsFold(ctx, cli, draft, { models, hasEffort: HAS_EFFORT.includes(cli) })] : []),
];
}
function addedFields(ctx, draft) {
@@ -112,6 +117,9 @@ function addedFields(ctx, draft) {
command,
agentField("Start prompt", "prompt", agentInput(ctx, draft, "prompt", "Start prompt", "/plan2code (blank starts with nothing typed)", false)),
models,
...(ctx.modelsLoaded
? [strengthsFold(ctx, ctx.openKey === "agent:new" ? "" : savedEntry(ctx.saved, ctx.openKey).id, draft, { models: () => draft.models, hasEffort: false })]
: []),
args,
];
}
@@ -165,7 +173,7 @@ function formFoot(ctx, key) {
export function renderDirty(ctx, key) {
const foot = ctx.$("form").querySelector(".prefs-agents-foot");
if (!foot || key !== ctx.openKey) return;
const dirty = isDirty(ctx.saved, ctx.drafts, key);
const dirty = isDirty(ctx.saved, ctx.drafts, key, ctx.strengthDrafts);
for (const sel of [".prefs-agents-save", ".prefs-agents-discard", ".prefs-unsaved-badge"]) foot.querySelector(sel).hidden = !dirty;
}
@@ -0,0 +1,140 @@
// Plan2Code Web Console - the "Strengths in the loop" fold of an agent's form.
//
// Maps the loop's three words (Thrifty, Balanced, Best) to one of the agent's
// own models, and to an effort where the agent takes one. Choices are
// select-only from the agent's Models list. An untouched row follows what
// ships; only a row the person changed is kept in the form's strength draft
// (ctx.strengthDrafts, by form key), which the form's Save sends. The rule that
// reads a row is resolveStrengths, the same module the server uses.
import { el } from "../../render.js";
import { STRENGTH_ORDER, resolveStrengths } from "../../strengths.js";
import { canonical, strengthOwner } from "./agents-data.js";
const WORDS = { thrifty: "Thrifty", balanced: "Balanced", best: "Best" };
const EFFORTS = ["low", "medium", "high"];
function option(value, text) {
const o = el("option", null, text);
o.value = value;
return o;
}
/**
* The fold for the open form. `models` is the agent's Models list ({ id, label }
* rows) or a function returning it, read at every redraw so a model added in the
* same form shows up. `hasEffort` adds the effort select.
*/
export function strengthsFold(ctx, agentId, draft, { models, hasEffort }) {
const key = ctx.openKey;
const owner = agentId || strengthOwner(key);
const shipped = (ctx.saved.strengths.shipped && ctx.saved.strengths.shipped[owner]) || {};
const modelList = () => {
const rows = typeof models === "function" ? models() : models;
const seen = new Set();
return (rows || []).filter((m) => m && m.id && !seen.has(m.id) && seen.add(m.id));
};
// The person's own rows: the draft once changed, else what is saved.
const userRows = () => {
const rows = ctx.strengthDrafts.has(key) ? ctx.strengthDrafts.get(key) : ctx.saved.strengths.user && ctx.saved.strengths.user[owner];
return rows && typeof rows === "object" ? rows : {};
};
const details = el("details", "agent-fold prefs-agents-strengths");
details.open = ctx.strengthsOpen;
details.addEventListener("toggle", () => (ctx.strengthsOpen = details.open));
details.appendChild(el("summary", null, "Strengths in the loop"));
const body = el("div", "strength-body");
details.appendChild(body);
function setRow(word, row) {
const next = { ...userRows() };
if (row) next[word] = row;
else delete next[word];
ctx.strengthDrafts.set(key, next);
ctx.edited();
}
function draw() {
const active = document.activeElement;
const back = active && body.contains(active) ? { word: active.closest("[data-word]")?.dataset.word, field: active.dataset.field } : null;
body.replaceChildren();
const list = modelList();
if (!list.length) {
body.appendChild(el("p", "help strength-empty", "Add a model under Models first"));
return;
}
const base = { shipped, models: list.map((m) => m.id), hasEffort };
const user = userRows();
const resolved = resolveStrengths({ ...base, user });
const dflt = resolveStrengths({ ...base, user: {} });
for (const word of STRENGTH_ORDER) {
const row = el("div", "strength-row");
row.dataset.word = word;
row.appendChild(el("span", "strength-word", WORDS[word]));
const model = el("select");
model.dataset.field = "model";
model.setAttribute("aria-label", `${WORDS[word]} model`);
// Nothing ships for this word: "not mapped" is a real choice. With a default,
// clearing a row is Reset's job.
if (!dflt[word]) model.appendChild(option("", "not mapped"));
for (const m of list) model.appendChild(option(m.id, m.label && m.label !== m.id ? `${m.label} (${m.id})` : m.id));
model.value = resolved[word] ? resolved[word].model : "";
model.addEventListener("change", () => {
if (!model.value) return setRow(word, null);
const next = { model: model.value };
if (hasEffort && resolved[word] && resolved[word].effort) next.effort = resolved[word].effort;
setRow(word, canonical(next) === canonical(dflt[word]) ? null : next);
});
row.appendChild(model);
if (hasEffort) {
const effort = el("select");
effort.dataset.field = "effort";
effort.setAttribute("aria-label", `${WORDS[word]} effort`);
effort.disabled = !resolved[word];
effort.appendChild(option("", "no effort"));
for (const e of EFFORTS) effort.appendChild(option(e, e));
effort.value = (resolved[word] && resolved[word].effort) || "";
effort.addEventListener("change", () => {
const next = { model: resolved[word].model };
if (effort.value) next.effort = effort.value;
setRow(word, canonical(next) === canonical(dflt[word]) ? null : next);
});
row.appendChild(effort);
}
if (Object.prototype.hasOwnProperty.call(user, word)) {
const reset = el("button", "btn small ghost strength-reset", "Reset");
reset.type = "button";
reset.dataset.field = "reset";
reset.setAttribute("aria-label", `Reset ${WORDS[word]}`);
reset.addEventListener("click", () => setRow(word, null));
row.appendChild(reset);
} else if (resolved[word]) {
row.appendChild(el("span", "help strength-default", "default"));
}
body.appendChild(row);
}
const offered = STRENGTH_ORDER.filter((w) => resolved[w]).map((w) => WORDS[w]);
body.appendChild(
el(
"p",
"help strength-offer",
offered.length ? `The loop will offer: ${offered.join(", ")}` : "The loop will not offer a strength for this agent until one is mapped."
)
);
if (back) {
// A Reset that was pressed is gone with its row's change: the model select takes the focus.
const row = body.querySelector(`[data-word="${back.word}"]`);
(row?.querySelector(`[data-field="${back.field}"]`) || row?.querySelector('[data-field="model"]'))?.focus();
}
}
ctx.strengthsRefresh = draw;
draw();
return details;
}