mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 20:45:33 -08:00
Let people define custom agent CLIs and start them from the app and the launcher
AI Assisted
This commit is contained in:
1 parent
3e22d53bf5
commit
94063fc0a9
27 files changed
+4025
-95
No files matched your search
@@ -2648,6 +2648,22 @@
|
||||
<span class="help" id="models-status" role="status"></span>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset class="modal-ranges looks-models looks-agents">
|
||||
<legend>Agents</legend>
|
||||
<div class="models-note">
|
||||
<p>Agent CLIs Plan2Code did not find on its own. The <code>plan2code</code> command and the desktop app list the ones that are installed.</p>
|
||||
<ul>
|
||||
<li><code>/plan2code</code> works only in agents that load the installed Plan2Code skills. A blank start prompt starts the agent with nothing typed.</li>
|
||||
<li>The list is saved as plain text in <code>~/.plan2code/agents.json</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="agents-list" id="agents-list" aria-label="Agents you added"></div>
|
||||
<div class="models-actions">
|
||||
<button type="button" class="btn small" id="agents-add">Add agent</button>
|
||||
<button type="button" class="btn small danger" id="agents-replace" hidden>Save anyway</button>
|
||||
<span class="help" id="agents-status" role="status"></span>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
<div class="modal-actions">
|
||||
<button type="button" class="btn ghost" id="looks-reset">Back to default</button>
|
||||
|
||||
@@ -90,6 +90,354 @@ async function loadModelsData() {
|
||||
renderModels();
|
||||
}
|
||||
|
||||
// Agents: CLIs Plan2Code did not find on its own, saved whole to ~/.plan2code/agents.json.
|
||||
// Each card edits its entry in place (unknown keys ride along untouched); every change
|
||||
// saves the whole list. A new card is not sent until it has a name and a command.
|
||||
let agentsData = [];
|
||||
|
||||
// The server's complaint about one card, drawn on it: { index, field, error } (index into agentsData).
|
||||
let agentsError = null;
|
||||
|
||||
// The entry whose Delete is waiting for a second click.
|
||||
let agentsConfirming = null;
|
||||
|
||||
// Every change saves, so two saves can be in flight at once: only the newest one's answer is drawn.
|
||||
let agentsSaveSeq = 0;
|
||||
|
||||
// The list as last loaded or saved, as sent: a change that leaves it the same (a field of a new
|
||||
// card still held back) sends nothing.
|
||||
let agentsLastSent = null;
|
||||
|
||||
function setAgentsStatus(text, isError) {
|
||||
const status = $("agents-status");
|
||||
status.textContent = text;
|
||||
status.classList.toggle("error", Boolean(isError));
|
||||
}
|
||||
|
||||
function newAgent() {
|
||||
return { name: "", command: "", args: [], prompt: "/plan2code", bypassArgs: [], modelArgs: ["--model", "{model}"], models: [] };
|
||||
}
|
||||
|
||||
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("change", () => {
|
||||
agent[key] = trim ? input.value.trim() : input.value;
|
||||
saveAgents();
|
||||
});
|
||||
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("change", () => { agent[key][i] = input.value.trim(); saveAgents(); });
|
||||
const remove = el("button", "btn small ghost", "Remove");
|
||||
remove.type = "button";
|
||||
remove.addEventListener("click", () => {
|
||||
agent[key].splice(i, 1);
|
||||
draw();
|
||||
saveAgents();
|
||||
});
|
||||
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();
|
||||
box.querySelector(".agent-row:last-of-type input")?.focus();
|
||||
});
|
||||
box.appendChild(add);
|
||||
};
|
||||
draw();
|
||||
return agentField(label, key, box);
|
||||
}
|
||||
|
||||
// The models list: an id and an optional name for the menu on each row.
|
||||
function agentModelsEditor(agent) {
|
||||
const box = el("div", "agent-rows");
|
||||
box.dataset.field = "models";
|
||||
const draw = () => {
|
||||
box.replaceChildren();
|
||||
agent.models.forEach((m, i) => {
|
||||
const row = el("div", "agent-row model");
|
||||
const id = el("input");
|
||||
id.value = m.id || "";
|
||||
id.placeholder = "Model id, for {model}";
|
||||
id.setAttribute("aria-label", "Model id");
|
||||
id.dataset.field = "models";
|
||||
const label = el("input");
|
||||
label.value = m.label || "";
|
||||
label.placeholder = "Name shown in the menu (optional)";
|
||||
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("change", () => { m.id = id.value.trim(); saveAgents(); });
|
||||
label.addEventListener("change", () => {
|
||||
const text = label.value.trim();
|
||||
if (text) m.label = text;
|
||||
else delete m.label;
|
||||
saveAgents();
|
||||
});
|
||||
remove.addEventListener("click", () => {
|
||||
agent.models.splice(i, 1);
|
||||
draw();
|
||||
saveAgents();
|
||||
});
|
||||
row.append(id, label, remove);
|
||||
box.appendChild(row);
|
||||
});
|
||||
const add = el("button", "btn small ghost", "Add a model");
|
||||
add.type = "button";
|
||||
add.addEventListener("click", () => {
|
||||
agent.models.push({ id: "" });
|
||||
draw();
|
||||
box.querySelector(".agent-row:last-of-type input")?.focus();
|
||||
});
|
||||
box.appendChild(add);
|
||||
};
|
||||
draw();
|
||||
return agentField("Models", "models", box);
|
||||
}
|
||||
|
||||
// Test, and Delete with its confirm step on the card itself (never the browser's confirm()).
|
||||
function agentFoot(agent) {
|
||||
const foot = el("div", "agent-foot");
|
||||
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.";
|
||||
}
|
||||
});
|
||||
foot.append(test);
|
||||
if (agentsConfirming === agent) {
|
||||
const ask = el("span", "help", "Delete this agent?");
|
||||
const yes = el("button", "btn small danger", "Yes, delete");
|
||||
yes.type = "button";
|
||||
const keep = el("button", "btn small ghost", "Keep");
|
||||
keep.type = "button";
|
||||
yes.addEventListener("click", async () => {
|
||||
agentsConfirming = null;
|
||||
agentsData.splice(agentsData.indexOf(agent), 1);
|
||||
agentsError = null;
|
||||
renderAgents();
|
||||
await saveAgents();
|
||||
});
|
||||
keep.addEventListener("click", () => {
|
||||
agentsConfirming = null;
|
||||
renderAgents();
|
||||
});
|
||||
foot.append(ask, yes, keep);
|
||||
} else {
|
||||
const del = el("button", "btn small ghost danger", "Delete");
|
||||
del.type = "button";
|
||||
del.addEventListener("click", () => {
|
||||
agentsConfirming = agent;
|
||||
renderAgents();
|
||||
});
|
||||
foot.append(del);
|
||||
}
|
||||
foot.append(result);
|
||||
return foot;
|
||||
}
|
||||
|
||||
// Which field or button has focus, so a redraw (an id assigned, an error marked) puts the
|
||||
// person back where they were, with what they had typed: { card, nth, value, start, end }.
|
||||
const AGENT_FOCUSABLE = "input, button";
|
||||
|
||||
function agentsFocus(list) {
|
||||
const active = document.activeElement;
|
||||
const card = active && list.contains(active) ? active.closest(".agent-card") : null;
|
||||
if (!card) return null;
|
||||
const at = { card: [...list.children].indexOf(card), nth: [...card.querySelectorAll(AGENT_FOCUSABLE)].indexOf(active) };
|
||||
if (active instanceof HTMLInputElement) Object.assign(at, { value: active.value, start: active.selectionStart, end: active.selectionEnd });
|
||||
return at;
|
||||
}
|
||||
|
||||
function restoreAgentsFocus(list, at) {
|
||||
const card = at && list.children[at.card];
|
||||
const target = card && card.querySelectorAll(AGENT_FOCUSABLE)[at.nth];
|
||||
if (!target) return;
|
||||
if (!(target instanceof HTMLInputElement) || at.value === undefined) {
|
||||
target.focus();
|
||||
return;
|
||||
}
|
||||
const drawn = target.value;
|
||||
target.value = at.value;
|
||||
target.focus();
|
||||
try {
|
||||
target.setSelectionRange(at.start, at.end);
|
||||
} catch {}
|
||||
// Text typed before the redraw is not in agentsData yet, and the browser fires no change
|
||||
// for a value set from script, so leaving the field sends it on.
|
||||
if (at.value !== drawn) target.addEventListener("blur", () => target.dispatchEvent(new Event("change")), { once: true });
|
||||
}
|
||||
|
||||
function renderAgents() {
|
||||
const list = $("agents-list");
|
||||
const focus = agentsFocus(list);
|
||||
list.replaceChildren();
|
||||
agentsData.forEach((agent, i) => {
|
||||
for (const key of ["args", "bypassArgs", "modelArgs", "models"]) {
|
||||
if (!Array.isArray(agent[key])) agent[key] = [];
|
||||
}
|
||||
const card = el("div", "agent-card");
|
||||
const top = el("div", "agent-top");
|
||||
top.append(
|
||||
agentField("Name", "name", agentInput(agent, "name", "Name", "Name shown in the menu", true)),
|
||||
agentField("Command", "command", agentInput(agent, "command", "Command", "Program to run, such as myagent", true)),
|
||||
);
|
||||
card.append(
|
||||
top,
|
||||
agentArgsEditor(agent, "args", "Arguments", "--message={prompt}", "Add an argument"),
|
||||
agentField("Start prompt", "prompt", agentInput(agent, "prompt", "Start prompt", "/plan2code (blank starts with nothing typed)", false)),
|
||||
agentArgsEditor(agent, "bypassArgs", "Bypass arguments", "--yes-always", "Add an argument"),
|
||||
agentArgsEditor(agent, "modelArgs", "Model arguments", "--model={model}", "Add an argument"),
|
||||
agentModelsEditor(agent),
|
||||
);
|
||||
if (agentsError && agentsError.index === i) {
|
||||
card.classList.add("invalid");
|
||||
for (const input of card.querySelectorAll("input")) {
|
||||
if (input.dataset.field === agentsError.field) input.classList.add("invalid");
|
||||
}
|
||||
card.appendChild(el("p", "agent-error", agentsError.error));
|
||||
}
|
||||
card.appendChild(agentFoot(agent));
|
||||
list.appendChild(card);
|
||||
});
|
||||
restoreAgentsFocus(list, focus);
|
||||
}
|
||||
|
||||
// The list as the server wants it: blank rows dropped, and a new card without both a name and
|
||||
// a command (one still being typed) left out, so its first field is not refused as half an
|
||||
// agent. `kept` maps each sent entry back to its place.
|
||||
function agentsPayload() {
|
||||
const kept = [];
|
||||
const agents = [];
|
||||
agentsData.forEach((a, i) => {
|
||||
if (!a.id && (!String(a.name || "").trim() || !String(a.command || "").trim())) return;
|
||||
kept.push(i);
|
||||
agents.push({
|
||||
...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),
|
||||
});
|
||||
});
|
||||
return { kept, agents };
|
||||
}
|
||||
|
||||
// `replace` is the person's Save anyway, over a file holding entries this page could not read.
|
||||
async function saveAgents({ replace = false } = {}) {
|
||||
const { kept, agents } = agentsPayload();
|
||||
const sent = JSON.stringify(agents);
|
||||
if (!replace && agentsError === null && sent === agentsLastSent) return;
|
||||
const seq = ++agentsSaveSeq;
|
||||
try {
|
||||
const res = await fetch("/agents", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(replace ? { agents, replace: true } : { agents }),
|
||||
});
|
||||
const body = await res.json();
|
||||
if (seq !== agentsSaveSeq) return;
|
||||
if (res.status === 409) {
|
||||
const warnings = Array.isArray(body.warnings) ? body.warnings : [];
|
||||
setAgentsStatus([body.error || "Not saved.", ...warnings].join(" "), true);
|
||||
$("agents-replace").hidden = false;
|
||||
return;
|
||||
}
|
||||
$("agents-replace").hidden = true;
|
||||
if (!res.ok) {
|
||||
const at = Number.isInteger(body.index) ? kept[body.index] : undefined;
|
||||
agentsError = at === undefined ? null : { index: at, field: body.field, error: body.error || "Could not save." };
|
||||
setAgentsStatus(at === undefined ? body.error || "Could not save." : "Not saved. Fix the marked agent.", true);
|
||||
renderAgents();
|
||||
return;
|
||||
}
|
||||
// Ids are assigned on the first save: take the server's entries, and redraw only then
|
||||
// (or to clear a mark), so typing in another card is not interrupted.
|
||||
const fresh = agents.some((a) => !a.id) || agentsError !== null;
|
||||
agentsError = null;
|
||||
agentsLastSent = sent;
|
||||
setAgentsStatus("Saved.", false);
|
||||
if (fresh) {
|
||||
kept.forEach((at, n) => { agentsData[at] = body.agents[n]; });
|
||||
agentsLastSent = JSON.stringify(agentsPayload().agents);
|
||||
renderAgents();
|
||||
}
|
||||
} catch {
|
||||
if (seq === agentsSaveSeq) setAgentsStatus("Could not save.", true);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadAgents() {
|
||||
agentsError = null;
|
||||
agentsConfirming = null;
|
||||
agentsLastSent = null;
|
||||
$("agents-replace").hidden = true;
|
||||
try {
|
||||
const res = await fetch("/agents");
|
||||
if (res.ok) {
|
||||
const body = await res.json();
|
||||
agentsData = Array.isArray(body.agents) ? body.agents : [];
|
||||
for (const agent of agentsData) {
|
||||
for (const key of ["args", "bypassArgs", "modelArgs", "models"]) if (!Array.isArray(agent[key])) agent[key] = [];
|
||||
}
|
||||
agentsLastSent = JSON.stringify(agentsPayload().agents);
|
||||
const warnings = Array.isArray(body.warnings) ? body.warnings : [];
|
||||
setAgentsStatus(warnings.join(" "), warnings.length > 0);
|
||||
} else {
|
||||
setAgentsStatus("Agents could not be loaded.", true);
|
||||
}
|
||||
} catch {
|
||||
setAgentsStatus("Agents could not be loaded.", true);
|
||||
}
|
||||
renderAgents();
|
||||
}
|
||||
|
||||
// Following the system, the saved looks painted before anything is drawn, and
|
||||
// the sound cues let go on the next touch. `boot` is the page's boot data.
|
||||
export function startLooks(boot) {
|
||||
@@ -119,6 +467,7 @@ export function wireLooks() {
|
||||
renderSwatches();
|
||||
setModelsStatus("", false);
|
||||
loadModelsData();
|
||||
loadAgents();
|
||||
$("looks-modal").showModal();
|
||||
});
|
||||
$("looks-done").addEventListener("click", () => $("looks-modal").close());
|
||||
@@ -188,6 +537,15 @@ export function wireLooks() {
|
||||
renderModels();
|
||||
$("models-user").querySelector(".models-row:last-child input")?.focus();
|
||||
});
|
||||
$("agents-add").addEventListener("click", () => {
|
||||
agentsData.push(newAgent());
|
||||
renderAgents();
|
||||
$("agents-list").querySelector(".agent-card:last-child input")?.focus();
|
||||
});
|
||||
$("agents-replace").addEventListener("click", () => {
|
||||
$("agents-replace").hidden = true;
|
||||
saveAgents({ replace: true });
|
||||
});
|
||||
$("models-reset").addEventListener("click", () => {
|
||||
if (!modelsData) return;
|
||||
modelsData.user = Object.fromEntries(MODEL_CLIS.map((cli) => [cli, []]));
|
||||
|
||||
@@ -108,6 +108,23 @@
|
||||
.models-row input { min-width: 0; }
|
||||
.models-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
||||
.models-actions .help.error { color: var(--danger, #d64545); }
|
||||
.agents-list { display: grid; gap: 10px; }
|
||||
.agents-list:empty { display: none; }
|
||||
.agent-card { display: grid; gap: 8px; padding: 10px; border: 1px solid var(--rule-2); border-radius: 8px; }
|
||||
.agent-card.invalid { border-color: var(--danger, #d64545); }
|
||||
.agent-card input { min-width: 0; }
|
||||
.agent-card input.invalid { border-color: var(--danger, #d64545); }
|
||||
.agent-top { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 6px; }
|
||||
.agent-field { display: grid; gap: 4px; min-width: 0; }
|
||||
.agent-field > span, .agent-field > label { font-size: 0.85em; opacity: 0.85; }
|
||||
.agent-rows { display: grid; gap: 4px; }
|
||||
.agent-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px; align-items: center; }
|
||||
.agent-row.model { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; }
|
||||
.agent-foot { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
||||
.agent-foot .help { margin: 0; }
|
||||
.agent-foot .agent-test-result { flex: 1 1 auto; }
|
||||
.agent-error { margin: 0; color: var(--danger, #d64545); font-size: 0.9em; }
|
||||
#agents-replace[hidden] { display: none; }
|
||||
|
||||
/* User Preferences: Cleanup's list of what would go, shown before anything
|
||||
is deleted. */
|
||||
|
||||
Reference in new issue
Block a user