mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-17 04:45:33 -08:00
feat: run each loop phase on its own strength (Thrifty, Balanced, Best)
AI Assisted
This commit is contained in:
1 parent
e963513a8a
commit
81d14c8e78
41 files changed
+3290
-81
No files matched your search
@@ -36,6 +36,8 @@
|
||||
<link rel="modulepreload" href="/screens/agents/agents-list-editor.js" />
|
||||
<link rel="modulepreload" href="/screens/agents/agents-summary.js" />
|
||||
<link rel="modulepreload" href="/screens/agents/agents-form.js" />
|
||||
<link rel="modulepreload" href="/screens/agents/agents-strengths.js" />
|
||||
<link rel="modulepreload" href="/strengths.js" />
|
||||
<link rel="modulepreload" href="/screens/cleanup.js" />
|
||||
<link rel="modulepreload" href="/screens/help-dialog.js" />
|
||||
<link rel="modulepreload" href="/screens/workspace.js" />
|
||||
@@ -2251,6 +2253,15 @@
|
||||
wait on a prompt nobody sees, so Start stays grayed out.
|
||||
</li>
|
||||
</ul>
|
||||
<h4>Strength per phase</h4>
|
||||
<p>
|
||||
Turn on <strong>Set strength per phase</strong> in the setup to run each
|
||||
phase on <strong>Thrifty</strong>, <strong>Balanced</strong>, <strong>Best</strong>
|
||||
or <strong>Match my session</strong>. A phase you leave untouched runs on
|
||||
the launch model. The one retry after a crash or a quiet agent (when retry
|
||||
is on) goes one step up (Match my session stays on the launch model),
|
||||
and the phase's helpers follow.
|
||||
</p>
|
||||
<h4>What is locked while it runs</h4>
|
||||
<p>
|
||||
Answers, sign-offs, launches, Workspace, Helpers and
|
||||
@@ -3305,6 +3316,11 @@
|
||||
opens one agent's form, and <strong>Add agent</strong> a blank one.
|
||||
</p>
|
||||
<ul>
|
||||
<li>
|
||||
<strong>Strengths in the loop</strong>: under Models, map Thrifty,
|
||||
Balanced and Best to one of the agent's own models. The Implementation
|
||||
Loop's per-phase strength reads them. Untouched rows stay as shipped.
|
||||
</li>
|
||||
<li>
|
||||
<strong>Models</strong>: each built-in agent (Claude, Devin, Codex)
|
||||
shows the models that come with Plan2Code, which every update
|
||||
|
||||
@@ -15,6 +15,7 @@ import { hooks } from "../state.js";
|
||||
import {
|
||||
MODEL_CLIS,
|
||||
emptyLists,
|
||||
emptyStrengths,
|
||||
newAgent,
|
||||
normalizeAgent,
|
||||
cliOf,
|
||||
@@ -91,7 +92,16 @@ export function mountAgentsView(container, opts = {}) {
|
||||
$,
|
||||
// The last server reply for each list. Unknown keys on an agent ride along
|
||||
// untouched; builtins and templates only feed the setup pick list.
|
||||
saved: { agents: [], user: emptyLists(), shipped: emptyLists(), builtins: [], templates: [] },
|
||||
saved: { agents: [], user: emptyLists(), shipped: emptyLists(), builtins: [], templates: [], strengths: emptyStrengths() },
|
||||
// The person's own strength rows per form key, once the fold changed them
|
||||
// (agents-strengths.js); a form with no entry has not touched its fold.
|
||||
strengthDrafts: new Map(),
|
||||
strengthsOpen: false,
|
||||
// The open fold's redraw, set by the fold itself.
|
||||
strengthsRefresh: null,
|
||||
// Whether GET /models answered: without it the page holds none of the
|
||||
// person's models or strengths, so the fold stays away and no save sends them.
|
||||
modelsLoaded: false,
|
||||
// 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.
|
||||
drafts: new Map(),
|
||||
@@ -148,15 +158,20 @@ export function mountAgentsView(container, opts = {}) {
|
||||
}
|
||||
|
||||
function edited() {
|
||||
if (ctx.openKey) renderDirty(ctx, ctx.openKey);
|
||||
if (ctx.openKey) {
|
||||
// A model added or removed changes what the strengths fold may pick.
|
||||
if (ctx.strengthsRefresh) ctx.strengthsRefresh();
|
||||
renderDirty(ctx, ctx.openKey);
|
||||
}
|
||||
}
|
||||
|
||||
// Leaving a form for another row drops its draft, as closing it always did:
|
||||
// a dirty one asks first (the dialog's Save / Discard / Keep editing).
|
||||
async function leaveForm() {
|
||||
if (!ctx.openKey) return true;
|
||||
if (isDirty(ctx.saved, ctx.drafts, ctx.openKey) && !(await confirmUnsaved())) return false;
|
||||
if (isDirty(ctx.saved, ctx.drafts, ctx.openKey, ctx.strengthDrafts) && !(await confirmUnsaved())) return false;
|
||||
ctx.drafts.delete(ctx.openKey);
|
||||
ctx.strengthDrafts.delete(ctx.openKey);
|
||||
ctx.openKey = null;
|
||||
return true;
|
||||
}
|
||||
@@ -183,6 +198,7 @@ export function mountAgentsView(container, opts = {}) {
|
||||
ctx.formError = null;
|
||||
ctx.formReplace = null;
|
||||
ctx.confirming = false;
|
||||
ctx.strengthsOpen = false;
|
||||
ctx.argsOpen = key === "agent:new";
|
||||
// A built-in's only section is its models: folded it would show nothing.
|
||||
ctx.modelsOpen = key.startsWith("cli:");
|
||||
@@ -198,7 +214,10 @@ export function mountAgentsView(container, opts = {}) {
|
||||
// one just deleted).
|
||||
function closeForm() {
|
||||
const left = ctx.openKey;
|
||||
if (ctx.openKey) ctx.drafts.delete(ctx.openKey);
|
||||
if (ctx.openKey) {
|
||||
ctx.drafts.delete(ctx.openKey);
|
||||
ctx.strengthDrafts.delete(ctx.openKey);
|
||||
}
|
||||
ctx.openKey = null;
|
||||
ctx.formError = null;
|
||||
ctx.formReplace = null;
|
||||
@@ -286,6 +305,7 @@ export function mountAgentsView(container, opts = {}) {
|
||||
// Fresh from what was just saved: the form redraws on a clean draft.
|
||||
function savedAs(key) {
|
||||
ctx.drafts.delete(ctx.openKey);
|
||||
ctx.strengthDrafts.delete(ctx.openKey);
|
||||
ctx.openKey = key;
|
||||
ctx.drafts.set(key, structuredClone(savedEntry(ctx.saved, key)));
|
||||
ctx.formError = null;
|
||||
@@ -294,15 +314,35 @@ export function mountAgentsView(container, opts = {}) {
|
||||
drawForm(ctx);
|
||||
}
|
||||
|
||||
// The strengths mapping to send for `owner` (a built-in's cli or an added
|
||||
// agent's id): every other agent's saved rows that still name a known agent
|
||||
// (a deleted agent's rows drop out here), plus the owner's own rows. Those are
|
||||
// the form's draft when its fold changed, else what is saved, keeping only
|
||||
// rows whose model is still in `modelIds`.
|
||||
function strengthsToSend(owner, key, modelIds) {
|
||||
const known = new Set([...MODEL_CLIS, ...ctx.saved.agents.map((a) => a.id)]);
|
||||
const out = {};
|
||||
for (const [id, rows] of Object.entries(ctx.saved.strengths.user)) if (id !== owner && known.has(id)) out[id] = rows;
|
||||
const mine = ctx.strengthDrafts.has(key) ? ctx.strengthDrafts.get(key) : ctx.saved.strengths.user[owner] || {};
|
||||
const kept = Object.fromEntries(Object.entries(mine).filter(([, row]) => row && modelIds.includes(row.model)));
|
||||
if (Object.keys(kept).length) out[owner] = kept;
|
||||
return out;
|
||||
}
|
||||
|
||||
async function saveModelsFor(cli) {
|
||||
const sent = ctx.drafts.get("cli:" + cli).filter((m) => m.id);
|
||||
const key = "cli:" + cli;
|
||||
const sent = ctx.drafts.get(key).filter((m) => m.id);
|
||||
try {
|
||||
const { res, body } = await postJson("/models", { models: { ...ctx.saved.user, [cli]: sent } });
|
||||
const modelIds = [...ctx.saved.shipped[cli], ...sent].map((m) => m.id);
|
||||
const payload = { models: { ...ctx.saved.user, [cli]: sent } };
|
||||
if (ctx.modelsLoaded) payload.strengths = strengthsToSend(cli, key, modelIds);
|
||||
const { res, body } = await postJson("/models", payload);
|
||||
if (!res.ok) {
|
||||
formMark({ ok: false, error: body.error || "Could not save." });
|
||||
return false;
|
||||
}
|
||||
ctx.saved.user[cli] = (body.user && body.user[cli]) || sent;
|
||||
if (ctx.modelsLoaded) ctx.saved.strengths.user = body.strengths || {};
|
||||
} catch {
|
||||
formMark({ ok: false, error: "Could not save." });
|
||||
return false;
|
||||
@@ -343,6 +383,32 @@ export function mountAgentsView(container, opts = {}) {
|
||||
return false;
|
||||
}
|
||||
const id = ctx.saved.agents[at].id;
|
||||
// The fold's rows ride a second save into models.json, under the id the
|
||||
// agent has now (a new agent only gets one from the first).
|
||||
const modelIds = draft.models.map((m) => m.id).filter(Boolean);
|
||||
const savedRows = ctx.saved.strengths.user[id] || {};
|
||||
const stale = Object.values(savedRows).some((row) => row && !modelIds.includes(row.model));
|
||||
if (ctx.modelsLoaded && (ctx.strengthDrafts.has(key) || stale)) {
|
||||
// The agent itself is saved by now: on a failure the form moves to its
|
||||
// saved id, keeps the fold's unsaved rows and says what did not land.
|
||||
const strengthsFailed = (message) => {
|
||||
const mine = ctx.strengthDrafts.get(key);
|
||||
ctx.found.delete(key);
|
||||
ctx.found.delete("agent:" + id);
|
||||
savedAs("agent:" + id);
|
||||
if (mine !== undefined) ctx.strengthDrafts.set("agent:" + id, mine);
|
||||
drawForm(ctx);
|
||||
formMark({ ok: false, error: message });
|
||||
return false;
|
||||
};
|
||||
try {
|
||||
const { res, body } = await postJson("/models", { models: ctx.saved.user, strengths: strengthsToSend(id, key, modelIds) });
|
||||
if (!res.ok) return strengthsFailed(body.error || "Saved, but the strengths could not be.");
|
||||
ctx.saved.strengths.user = body.strengths || {};
|
||||
} catch {
|
||||
return strengthsFailed("Saved, but the strengths could not be.");
|
||||
}
|
||||
}
|
||||
ctx.found.delete(key);
|
||||
ctx.found.delete("agent:" + id);
|
||||
savedAs("agent:" + id);
|
||||
@@ -379,7 +445,7 @@ export function mountAgentsView(container, opts = {}) {
|
||||
|
||||
// An open form whose draft differs from what is saved.
|
||||
function hasUnsaved() {
|
||||
return ctx.openKey !== null && isDirty(ctx.saved, ctx.drafts, ctx.openKey);
|
||||
return ctx.openKey !== null && isDirty(ctx.saved, ctx.drafts, ctx.openKey, ctx.strengthDrafts);
|
||||
}
|
||||
|
||||
// What the unsaved prompt calls the open form.
|
||||
@@ -395,6 +461,7 @@ export function mountAgentsView(container, opts = {}) {
|
||||
if (!ctx.openKey) return;
|
||||
if (ctx.openKey === "agent:new") return closeForm();
|
||||
ctx.drafts.set(ctx.openKey, structuredClone(savedEntry(ctx.saved, ctx.openKey)));
|
||||
ctx.strengthDrafts.delete(ctx.openKey);
|
||||
ctx.formError = null;
|
||||
ctx.formReplace = null;
|
||||
ctx.confirming = false;
|
||||
@@ -414,6 +481,7 @@ export function mountAgentsView(container, opts = {}) {
|
||||
const seq = ++loadSeq;
|
||||
ctx.openKey = null;
|
||||
ctx.drafts.clear();
|
||||
ctx.strengthDrafts.clear();
|
||||
ctx.found.clear();
|
||||
ctx.formError = null;
|
||||
ctx.formReplace = null;
|
||||
@@ -437,7 +505,14 @@ export function mountAgentsView(container, opts = {}) {
|
||||
}
|
||||
ctx.saved.user = emptyLists();
|
||||
ctx.saved.shipped = emptyLists();
|
||||
ctx.saved.strengths = emptyStrengths();
|
||||
ctx.modelsLoaded = Boolean(models);
|
||||
if (models) {
|
||||
// A failed or missing strengths answer reads as nothing mapped.
|
||||
for (const part of ["shipped", "user"]) {
|
||||
const rows = models.strengths && models.strengths[part];
|
||||
if (rows && typeof rows === "object" && !Array.isArray(rows)) ctx.saved.strengths[part] = rows;
|
||||
}
|
||||
for (const cli of MODEL_CLIS) {
|
||||
if (Array.isArray(models.user?.[cli])) ctx.saved.user[cli] = models.user[cli];
|
||||
if (Array.isArray(models.shipped?.[cli])) ctx.saved.shipped[cli] = models.shipped[cli];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -29,6 +29,13 @@ const AFTER_LAST = [
|
||||
["run-finalize", "Run Finalize automatically", "Finalize starts by itself once every phase is built."],
|
||||
];
|
||||
|
||||
const STRENGTH_WORDS = [
|
||||
["thrifty", "Thrifty"],
|
||||
["balanced", "Balanced"],
|
||||
["best", "Best"],
|
||||
];
|
||||
const MATCH_SESSION = "Match my session";
|
||||
|
||||
const DEFAULT_NOTIFY_PARAM = "message";
|
||||
// What the setup can send a notification to. `name` is the label for the field
|
||||
// the person names, on the kinds that have one.
|
||||
@@ -57,7 +64,7 @@ export async function openLoopSetup(spec) {
|
||||
const dir = spec.dir;
|
||||
page.loopStart = null;
|
||||
page.loopNotifyTest = null;
|
||||
page.loopSetup = { spec: dir, loaded: false, failed: false, form: null, facts: null, problems: [], starting: false };
|
||||
page.loopSetup = { spec: dir, loaded: false, failed: false, form: null, facts: null, problems: [], starting: false, resolvedFor: undefined };
|
||||
tellApp("loop-facts");
|
||||
redraw();
|
||||
const mine = page.loopSetup;
|
||||
@@ -80,7 +87,7 @@ export async function openLoopSetup(spec) {
|
||||
redraw();
|
||||
return;
|
||||
}
|
||||
const { phasesLeft, branch, pending, suggestedQuickCheck, remembered, saved, ...setup } = answer;
|
||||
const { phasesLeft, branch, pending, suggestedQuickCheck, remembered, saved, picks, resolved, ...setup } = answer;
|
||||
mine.form = {
|
||||
review: setup.review !== false,
|
||||
fixMode: setup.fixMode === "levels" ? "levels" : "smart",
|
||||
@@ -94,18 +101,86 @@ export async function openLoopSetup(spec) {
|
||||
notifyKind: NOTIFY_KINDS.some((k) => k.id === setup.notifyKind) ? setup.notifyKind : "query",
|
||||
notifyUrl: typeof setup.notifyUrl === "string" ? setup.notifyUrl : "",
|
||||
notifyParam: setup.notifyParam || DEFAULT_NOTIFY_PARAM,
|
||||
phaseStrength: setup.phaseStrength === true,
|
||||
// Every spec's picks, as saved; only this spec's key is changed here.
|
||||
strengths: setup.strengths && typeof setup.strengths === "object" && !Array.isArray(setup.strengths) ? structuredClone(setup.strengths) : {},
|
||||
};
|
||||
if (Array.isArray(picks)) mine.form.strengths[dir] = structuredClone(picks);
|
||||
// A first visit with nothing typed starts from the project's own check.
|
||||
if (!remembered && !mine.form.quickCheck && suggestedQuickCheck) mine.form.quickCheck = suggestedQuickCheck;
|
||||
mine.facts = {
|
||||
spec: dir,
|
||||
phasesLeft: Array.isArray(phasesLeft) ? phasesLeft : [],
|
||||
branch: typeof branch === "string" ? branch : "",
|
||||
pending: Number.isInteger(pending) ? pending : null,
|
||||
// The loop agent's strengths: undefined until the app has named the agent
|
||||
// and the console has answered, then null (no such agent) or { word: { model, effort? } }.
|
||||
resolved: undefined,
|
||||
};
|
||||
mine.loaded = true;
|
||||
redraw();
|
||||
}
|
||||
|
||||
const loopAgent = () => (page.loopFacts && typeof page.loopFacts.agent === "string" ? page.loopFacts.agent : "");
|
||||
|
||||
/**
|
||||
* Read the loop agent's strengths once the app has said which agent it is. The
|
||||
* setup and the app answer in either order, so every draw asks (cheaply) whether
|
||||
* the agent changed since the last read.
|
||||
*/
|
||||
function ensureResolved(s) {
|
||||
if (!page.loopFacts || s.resolvedFor === loopAgent()) return;
|
||||
const agent = loopAgent();
|
||||
s.resolvedFor = agent;
|
||||
if (!agent) {
|
||||
s.facts.resolved = null;
|
||||
return;
|
||||
}
|
||||
fetch("/loop-setup?spec=" + encodeURIComponent(s.spec) + "&agent=" + encodeURIComponent(agent), { cache: "no-store" })
|
||||
.then((res) => (res.ok ? res.json() : null))
|
||||
.catch(() => null)
|
||||
.then((answer) => {
|
||||
if (page.loopSetup !== s || s.resolvedFor !== agent) return;
|
||||
s.facts.resolved = answer && answer.resolved && typeof answer.resolved === "object" ? answer.resolved : null;
|
||||
redraw();
|
||||
});
|
||||
}
|
||||
|
||||
const hasMapping = (resolved) => Boolean(resolved) && Object.keys(resolved).length > 0;
|
||||
|
||||
/** This spec's saved word for a phase, or "" for Match my session. */
|
||||
function pickOf(form, spec, phase) {
|
||||
const list = form.strengths && form.strengths[spec];
|
||||
const hit = Array.isArray(list) ? list.find((p) => p.n === phase.n && p.name === phase.name) : null;
|
||||
return hit ? hit.strength : "";
|
||||
}
|
||||
|
||||
function setPick(form, spec, phase, word) {
|
||||
const rest = (form.strengths[spec] || []).filter((p) => p.n !== phase.n);
|
||||
if (word) rest.push({ n: phase.n, name: phase.name, strength: word });
|
||||
rest.sort((a, b) => a.n - b.n);
|
||||
form.strengths[spec] = rest;
|
||||
}
|
||||
|
||||
/** The phases left whose saved pick is not mapped for the loop's agent. */
|
||||
function unmappedPicks(form, facts, spec) {
|
||||
const resolved = facts.resolved;
|
||||
return facts.phasesLeft.filter((p) => {
|
||||
const word = pickOf(form, spec, p);
|
||||
return word && !(resolved && resolved[word]);
|
||||
});
|
||||
}
|
||||
|
||||
/** "sonnet, low": what a pick becomes. Match my session shows the launch choice. */
|
||||
export function pickModelText(resolved, word, appFacts) {
|
||||
if (!word) {
|
||||
if (!appFacts) return "";
|
||||
return [appFacts.model || "default model", appFacts.effort].filter(Boolean).join(", ");
|
||||
}
|
||||
const row = resolved && resolved[word];
|
||||
return row ? [row.model, row.effort].filter(Boolean).join(", ") : "";
|
||||
}
|
||||
|
||||
function close() {
|
||||
page.loopSetup = null;
|
||||
page.loopStart = null;
|
||||
@@ -125,6 +200,13 @@ export function startBlock(form, facts, appFacts) {
|
||||
if (appFacts.permissionMode === "checked") return "The loop needs permission checks off. Start a New Session with checks off first.";
|
||||
if (appFacts.promptSupport === false) return "This agent cannot be started with a first prompt.";
|
||||
if (!facts.phasesLeft.length) return "Nothing left to build in this spec.";
|
||||
// A saved pick the agent no longer maps holds Start until it is changed (FR-14).
|
||||
// An agent with nothing mapped grays the section instead, and its picks do not count.
|
||||
if (form.phaseStrength && facts.resolved === undefined) return "Asking the app...";
|
||||
if (form.phaseStrength && hasMapping(facts.resolved)) {
|
||||
const bad = unmappedPicks(form, facts, facts.spec);
|
||||
if (bad.length) return `Pick a new strength for ${bad.map((p) => `Phase ${p.n}`).join(", ")}: not mapped for this agent.`;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -256,8 +338,91 @@ export function renderLoopSetup(main) {
|
||||
main.appendChild(pane);
|
||||
}
|
||||
|
||||
/** A plain select: [value, text, disabled?] options, the chosen value, a change handler. */
|
||||
function select(id, label, options, value, onChange) {
|
||||
const box = el("select", "loop-select");
|
||||
box.id = id;
|
||||
box.setAttribute("aria-label", label);
|
||||
for (const [v, text, disabled] of options) {
|
||||
const o = el("option", null, text);
|
||||
o.value = v;
|
||||
o.disabled = Boolean(disabled);
|
||||
box.appendChild(o);
|
||||
}
|
||||
box.value = value;
|
||||
box.addEventListener("change", () => onChange(box.value));
|
||||
return box;
|
||||
}
|
||||
|
||||
/** The "Strength per phase" field: the toggle, Set all to, and one row per phase left. */
|
||||
function strengthField(s, redo) {
|
||||
const f = s.form;
|
||||
const resolved = s.facts.resolved;
|
||||
const box = field("Strength per phase");
|
||||
const mapped = hasMapping(resolved);
|
||||
const loading = resolved === undefined;
|
||||
const off = !loading && !mapped;
|
||||
const sw = toggle("loop-phase-strength", "Set strength per phase", f.phaseStrength && !off, (v) => ((f.phaseStrength = v), redo()));
|
||||
if (off) {
|
||||
sw.classList.add("is-gray");
|
||||
sw.querySelector("input").disabled = true;
|
||||
}
|
||||
box.appendChild(sw);
|
||||
if (off) {
|
||||
const line = el("p", "loop-hint loop-strength-none");
|
||||
line.append("This agent has no strengths mapped yet. ");
|
||||
const link = el("button", "linky", "Map them in Agents & models");
|
||||
link.type = "button";
|
||||
link.addEventListener("click", () => hooks.openPrefs("agents"));
|
||||
line.appendChild(link);
|
||||
box.appendChild(line);
|
||||
return box;
|
||||
}
|
||||
if (loading) {
|
||||
box.appendChild(el("p", "loop-hint", "Asking the app which agent this loop uses..."));
|
||||
return box;
|
||||
}
|
||||
if (!f.phaseStrength) return box;
|
||||
|
||||
// An unmapped word stays in the list, grayed, so a saved one is seen and changed.
|
||||
const options = [
|
||||
["", MATCH_SESSION],
|
||||
...STRENGTH_WORDS.map(([word, label]) => [word, resolved[word] ? label : label + " (not mapped)", !resolved[word]]),
|
||||
];
|
||||
const all = el("div", "loop-strength-all");
|
||||
all.appendChild(el("label", "loop-strength-label", "Set all to"));
|
||||
all.firstChild.htmlFor = "loop-strength-all";
|
||||
const everyone = select("loop-strength-all", "Set all phases to", [["-", "Choose..."], ...options.map(([v, t, d]) => [v === "" ? "match" : v, t, d])], "-", (v) => {
|
||||
if (v === "-") return;
|
||||
for (const p of s.facts.phasesLeft) setPick(f, s.spec, p, v === "match" ? "" : v);
|
||||
redraw();
|
||||
});
|
||||
all.appendChild(everyone);
|
||||
box.appendChild(all);
|
||||
|
||||
const rows = el("div", "loop-strength-rows");
|
||||
for (const p of s.facts.phasesLeft) {
|
||||
const word = pickOf(f, s.spec, p);
|
||||
const row = el("div", "loop-strength-row");
|
||||
row.dataset.phase = String(p.n);
|
||||
row.appendChild(el("span", "loop-strength-phase", `Phase ${p.n}: ${p.name}`));
|
||||
row.appendChild(
|
||||
select(`loop-strength-${p.n}`, `Phase ${p.n} strength`, options, word, (v) => {
|
||||
setPick(f, s.spec, p, v);
|
||||
s.problems = [];
|
||||
redraw();
|
||||
})
|
||||
);
|
||||
row.appendChild(el("span", "loop-strength-model", pickModelText(resolved, word, page.loopFacts)));
|
||||
rows.appendChild(row);
|
||||
}
|
||||
box.appendChild(rows);
|
||||
return box;
|
||||
}
|
||||
|
||||
function drawBody(pane, s) {
|
||||
const f = s.form;
|
||||
ensureResolved(s);
|
||||
const redo = () => {
|
||||
s.problems = [];
|
||||
redraw();
|
||||
@@ -287,6 +452,8 @@ function drawBody(pane, s) {
|
||||
// The fields (FR-2, FR-4).
|
||||
const form = el("div", "loop-form");
|
||||
|
||||
form.appendChild(strengthField(s, redo));
|
||||
|
||||
const review = field("Review");
|
||||
review.appendChild(toggle("loop-review", "Review each phase with a fresh agent before it counts", f.review, (v) => ((f.review = v), redo())));
|
||||
form.appendChild(review);
|
||||
@@ -503,9 +670,19 @@ function gitButton() {
|
||||
return b;
|
||||
}
|
||||
|
||||
/** The form as the console saves it. */
|
||||
function setupBody(f) {
|
||||
return { ...f, quickCheck: f.quickCheck.trim(), notifyUrl: f.notifyUrl.trim(), notifyParam: f.notifyParam.trim() || DEFAULT_NOTIFY_PARAM };
|
||||
/** The form as the console saves it (exported for the page tests). */
|
||||
export function setupBody(f, s) {
|
||||
const body = { ...f, quickCheck: f.quickCheck.trim(), notifyUrl: f.notifyUrl.trim(), notifyParam: f.notifyParam.trim() || DEFAULT_NOTIFY_PARAM };
|
||||
// Only this spec's key changes: its rows that are not Match my session. With the
|
||||
// toggle off, or an agent with nothing mapped, the saved picks stay as they are
|
||||
// but the loop is told nothing about strengths (NFR-3).
|
||||
const strengths = structuredClone(f.strengths || {});
|
||||
const mine = (strengths[s.spec] || []).filter((p) => p.strength);
|
||||
if (mine.length) strengths[s.spec] = mine;
|
||||
else delete strengths[s.spec];
|
||||
body.strengths = strengths;
|
||||
body.phaseStrength = f.phaseStrength && hasMapping(s.facts.resolved);
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -519,7 +696,7 @@ async function sendTest(s) {
|
||||
s.problems = [];
|
||||
page.loopNotifyTest = { waiting: true };
|
||||
redraw();
|
||||
const { status, body: answer } = await postJson("/loop-setup", setupBody(f));
|
||||
const { status, body: answer } = await postJson("/loop-setup", setupBody(f, s));
|
||||
if (page.loopSetup !== s) return;
|
||||
if (status !== 200 || !answer || answer.ok !== true) {
|
||||
page.loopNotifyTest = null;
|
||||
@@ -537,7 +714,7 @@ async function startLoop(s) {
|
||||
page.loopStart = null;
|
||||
s.starting = true;
|
||||
redraw();
|
||||
const { status, body: answer } = await postJson("/loop-setup", setupBody(f));
|
||||
const { status, body: answer } = await postJson("/loop-setup", setupBody(f, s));
|
||||
if (page.loopSetup !== s) return;
|
||||
if (status !== 200 || !answer || answer.ok !== true) {
|
||||
s.starting = false;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
// The loop's strength rule, shared by the page and the server (strengths.mjs re-exports it).
|
||||
// Turns Thrifty, Balanced and Best into a model (and effort) for one agent. Pure functions, no file reads: the caller passes the
|
||||
// shipped and user mappings (the `strengths` key of models.json) and the
|
||||
// agent's Models list. Held to scripts/fixtures/strength-cases.json; the Rust
|
||||
// twin is `resolve_strengths` / `next_strength` in
|
||||
// plan2code-app/src-tauri/src/models.rs, held to the same file.
|
||||
|
||||
export const STRENGTH_ORDER = ["thrifty", "balanced", "best"];
|
||||
const EFFORTS = ["low", "medium", "high"];
|
||||
|
||||
// One row as the resolver keeps it, or null when the row is not usable: the
|
||||
// model must be a string in `models`; an effort survives only when the agent
|
||||
// takes one and it is low, medium or high.
|
||||
function validRow(row, models, hasEffort) {
|
||||
if (!row || typeof row !== "object" || Array.isArray(row)) return null;
|
||||
if (typeof row.model !== "string" || !models.includes(row.model)) return null;
|
||||
const out = { model: row.model };
|
||||
if (hasEffort && EFFORTS.includes(row.effort)) out.effort = row.effort;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* { thrifty?, balanced?, best? } for one agent: per word the user's row if it is
|
||||
* valid, else the shipped row if it is valid. A word with neither is left out
|
||||
* (unmapped).
|
||||
*/
|
||||
export function resolveStrengths({ shipped, user, models, hasEffort } = {}) {
|
||||
const list = Array.isArray(models) ? models : [];
|
||||
const obj = (v) => (v && typeof v === "object" && !Array.isArray(v) ? v : {});
|
||||
const resolved = {};
|
||||
for (const word of STRENGTH_ORDER) {
|
||||
const row = validRow(obj(user)[word], list, !!hasEffort) || validRow(obj(shipped)[word], list, !!hasEffort);
|
||||
if (row) resolved[word] = row;
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
/** The first mapped word after `word` in STRENGTH_ORDER, else null. */
|
||||
export function nextStrength(resolved, word) {
|
||||
const at = STRENGTH_ORDER.indexOf(word);
|
||||
if (at < 0) return null;
|
||||
const have = resolved && typeof resolved === "object" ? resolved : {};
|
||||
for (const next of STRENGTH_ORDER.slice(at + 1)) if (have[next]) return next;
|
||||
return null;
|
||||
}
|
||||
@@ -155,3 +155,13 @@
|
||||
.split { grid-template-columns: 1fr; }
|
||||
.split-list { border-right: 0; border-bottom: 1px solid var(--rule); max-height: 260px; overflow: auto; }
|
||||
}
|
||||
|
||||
/* ------------------------------------------------- strengths in the loop */
|
||||
|
||||
.strength-body { display: grid; gap: 6px; }
|
||||
.strength-row { display: grid; grid-template-columns: 5.5em minmax(0, 1fr) auto; gap: 6px; align-items: center; }
|
||||
.strength-row:has(select + select) { grid-template-columns: 5.5em minmax(0, 1fr) minmax(0, 8em) auto; }
|
||||
.strength-row select { min-width: 0; }
|
||||
.strength-word { font-size: 0.9em; }
|
||||
.strength-default { margin: 0; font-size: 0.8em; color: var(--ink-3); }
|
||||
.strength-empty, .strength-offer { margin: 0; }
|
||||
@@ -629,6 +629,15 @@
|
||||
.loop-radios { display: grid; gap: 6px; }
|
||||
.loop-radio-body { display: grid; gap: 1px; }
|
||||
.loop-radio-detail { font-size: 12.5px; color: var(--ink-3); }
|
||||
.loop-switch.is-gray { opacity: 0.55; cursor: default; }
|
||||
.loop-strength-none .linky { font-size: inherit; }
|
||||
.loop-strength-all { display: flex; align-items: center; gap: 10px; margin-top: 4px; font-size: 13px; color: var(--ink-2); }
|
||||
.loop-strength-rows { display: grid; gap: 6px; }
|
||||
.loop-strength-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(150px, 190px) minmax(70px, 120px); align-items: center; gap: 10px; font-size: 13.5px; }
|
||||
.loop-strength-phase { overflow-wrap: anywhere; color: var(--ink); }
|
||||
.loop-strength-model { font-size: 12.5px; color: var(--ink-3); overflow-wrap: anywhere; }
|
||||
.loop-select { font: inherit; font-size: 13.5px; padding: 5px 8px; color: var(--ink); background: var(--panel-2); border: 1px solid var(--rule); border-radius: 6px; max-width: 100%; }
|
||||
@media (max-width: 560px) { .loop-strength-row { grid-template-columns: 1fr 1fr; } .loop-strength-phase { grid-column: 1 / -1; } }
|
||||
.loop-ticks { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-left: 4px; }
|
||||
.loop-text { font: inherit; font-size: 14px; padding: 6px 10px; color: var(--ink); background: var(--panel-2); border: 1px solid var(--rule); border-radius: 6px; max-width: 520px; width: 100%; }
|
||||
.loop-text.loop-narrow { width: 90px; }
|
||||
|
||||
Reference in new issue
Block a user