Edit an agent's prompts as rows and its gates as a Where block and rules

The card lists each prompt and says the gates in words. The form edits
prompts as name and text rows, and the gates as a Where block (countries
required, then one of countries, remote or places, and prefer hybrid) over
a row per remaining rule. A gate the Where block cannot say lands in a row,
so a load and a save lose nothing. The gates JSON box stays under Advanced,
read only, as a view of what Save sends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-09-29 16:17:15 +02:00
1 parent b6afe24eaa
commit 0996b03f8f
3 files changed
+434 -31

No files matched your search

+356 -20
View File
@@ -2,9 +2,12 @@
* against, one agent at a time.
*
* Every agent from `/api/agents` is a card, newest first: its name, its
* switch, what it looks for, its weights, and a link to the Roles tab in its
* order. What it looks for is the paragraph, since that is what fit is
* measured against; the weights only share out the terms. Edit turns a card
* switch, its prompts, its gates in words, its weights, and a link to the
* Roles tab in its order. The prompts are the body, since fit is measured
* against the one a role fits best; the weights only share out the terms.
* The form edits the prompts as rows, and the gates as a Where block for the
* usual location rules over a row per remaining rule; a gate Where cannot
* say lands in a row, so a load and a save lose nothing. Edit turns a card
* into the form from the page's `<template>`, in place, and New agent puts a
* blank card on top in the same state. One card edits at a time, so the
* form's ids stay unique. Votes, liked companies and scores are the agent's,
@@ -25,6 +28,15 @@
// best `summarise_top` at `summarise_min` or more a summary.
const DEFAULT_LIMITS = { extract_top: 500, extract_min: 0.55, summarise_top: 30, summarise_min: 0.65 };
const NEW = "new";
// Europe as the starter agents in config.example.yaml spell it: the EEA,
// the UK and Switzerland, and EU for a role open anywhere in Europe.
const EUROPE = ["AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR",
"HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE",
"IS", "LI", "NO", "GB", "CH", "EU"];
const FIELDS = ["place", "country", "remote", "seniority", "function", "salary_min",
"currency", "permit", "languages", "company"];
const MODES = [["require", "must"], ["any", "one of"], ["prefer", "prefer"]];
const HYBRID_WEIGHT = 0.05;
/** @type {object[]} every agent, as the server lists them */
let agents = [];
@@ -66,6 +78,36 @@
return a;
}
function linky(text) {
const button = el("button", "linky", text);
button.type = "button";
return button;
}
let regionNames = null;
try {
regionNames = new Intl.DisplayNames(["en"], { type: "region" });
} catch (err) {
regionNames = null;
}
// A country code in words, the code itself where the browser has no name.
function countryName(code) {
if (code === "EU") return "anywhere in Europe";
try {
return (regionNames && regionNames.of(code)) || code;
} catch (err) {
return code;
}
}
// Until the backend returns `prompts`: an intent is one prompt named after its agent.
function promptsOf(row) {
if (row.prompts) return row.prompts;
const intent = (row.intent || "").trim();
return intent ? [{ name: row.name, text: row.intent }] : [];
}
function statusOf(id) {
if (!statuses.has(id)) {
const line = el("p", "count agent-status");
@@ -112,8 +154,43 @@
function weightsLine(row) {
const w = Object.assign({}, DEFAULT_WEIGHTS, row.weights);
const gates = row.gates && row.gates.length ? ` · ${count(row.gates.length, "gate")}` : "";
return `fit ${w.fit} · newness ${w.recency} · companies ${w.affinity} · liked ${w.taste}${gates}`;
return `fit ${w.fit} · newness ${w.recency} · companies ${w.affinity} · liked ${w.taste}`;
}
// One gate as a phrase: "in Denmark", "remote", "seniority senior, staff".
// Europe, whole, is one word rather than 33 countries.
function phrase(gate) {
const values = gate.values.map(String);
if (gate.field === "country") {
const europe = EUROPE.every((c) => values.includes(c));
const rest = values.filter((v) => !(europe && EUROPE.includes(v))).map(countryName);
return `in ${(europe ? ["Europe", ...rest] : rest).join(", ")}`;
}
if (gate.field === "place") return `in ${values.join(", ")}`;
if (gate.field === "remote") return values.join(" or ");
if (gate.field === "company") return `at ${values.join(", ")}`;
if (gate.field === "salary_min") return `paying at least ${values[0]}`;
return `${gate.field} ${values.join(", ")}`;
}
// The gates in words: what a role must meet, then the one group it must
// meet one of, then what lifts it. "In Europe, and in Denmark or remote or
// in Malmö, Lund. Prefers hybrid."
function rulesLine(gates) {
const of = (mode) => gates.filter((g) => g.mode === mode).map(phrase);
const must = of("require");
const any = of("any");
const prefer = of("prefer");
const sentences = [];
const first = [];
if (must.length) first.push(must.join(", "));
if (any.length) first.push(`${must.length ? "and " : ""}${any.join(" or ")}`);
if (first.length) {
const text = first.join(", ");
sentences.push(`${text[0].toUpperCase()}${text.slice(1)}.`);
}
if (prefer.length) sentences.push(`Prefers ${prefer.join("; ")}.`);
return sentences.join(" ");
}
// What the model reads after a pull, one clause per stage. A stage that is
@@ -153,10 +230,19 @@
head.appendChild(onSwitch(row));
div.appendChild(head);
const intent = (row.intent || "").trim();
div.appendChild(intent
? el("p", "agent-intent", intent)
: el("p", "agent-intent empty", "Nothing described yet, so fit has nothing to measure against. Edit says what this agent looks for."));
const prompts = promptsOf(row).filter((p) => p.text.trim());
if (!prompts.length) {
div.appendChild(el("p", "agent-intent empty", "Nothing described yet, so fit has nothing to measure against. Edit says what this agent looks for."));
}
for (const prompt of prompts) {
// A lone prompt named after its agent, as a migrated intent is, says its name once.
if (prompts.length > 1 || prompt.name !== row.name) {
div.appendChild(el("h3", "agent-prompt-name", prompt.name));
}
div.appendChild(el("p", "agent-intent", prompt.text.trim()));
}
const rules = rulesLine(row.gates || []);
if (rules) div.appendChild(el("p", "agent-rules", rules));
div.appendChild(el("p", "agent-weights", weightsLine(row)));
div.appendChild(limitsLine(row));
@@ -179,6 +265,182 @@
// ---------------------------------------------------------------- the form
// Tell the form something changed that no field typed: a chip or a row
// added or removed. It marks the form dirty and redraws the gates view.
function changed(node) {
node.dispatchEvent(new Event("input", { bubbles: true }));
}
// A list of values as chips, the rail's tags with news's filter pills' ×,
// and a box after them that adds on Enter or a comma. Country codes are
// `upper`, and all of Europe is one chip. What is typed but not added yet
// counts, so Save does not drop it.
function chipInput(values, label, placeholder, upper) {
const list = values.map(String);
const box = el("div", "chips");
const input = el("input");
input.type = "text";
input.spellcheck = false;
input.placeholder = placeholder;
input.setAttribute("aria-label", label);
const split = (text) => text.split(",").map((v) => (upper ? v.trim().toUpperCase() : v.trim())).filter(Boolean);
const europe = () => upper && EUROPE.every((c) => list.includes(c));
function chip(text, title, drop) {
const button = el("button", "tag", `${text} `);
button.type = "button";
button.title = title;
button.setAttribute("aria-label", `Remove ${text}`);
const x = el("span", "", "×");
x.setAttribute("aria-hidden", "true");
button.appendChild(x);
button.addEventListener("click", () => {
drop();
draw();
changed(box);
input.focus();
});
return button;
}
function draw() {
const whole = europe();
const chips = whole
? [chip("Europe", EUROPE.join(" "), () => list.splice(0, list.length, ...list.filter((v) => !EUROPE.includes(v))))]
: [];
for (const value of list) {
if (whole && EUROPE.includes(value)) continue;
chips.push(chip(value, upper ? countryName(value) : "", () => list.splice(list.indexOf(value), 1)));
}
box.replaceChildren(...chips, input);
}
function add(more) {
const before = list.length;
for (const value of more) if (!list.includes(value)) list.push(value);
draw();
if (list.length !== before) changed(box);
}
input.addEventListener("keydown", (event) => {
if (event.key !== "Enter" && event.key !== ",") return;
event.preventDefault();
const typed = split(input.value);
input.value = "";
add(typed);
});
input.addEventListener("blur", () => {
const typed = split(input.value);
input.value = "";
add(typed);
});
draw();
return {
node: box,
add,
whole: europe,
values: () => [...list, ...split(input.value).filter((v) => !list.includes(v))],
};
}
// A prompt as a row: its name and Remove on a line, its text under them.
function promptRow(prompt, remove) {
const row = el("div", "prompt-row");
const name = el("input");
name.type = "text";
name.placeholder = "AI and data leadership";
name.value = prompt.name;
name.setAttribute("aria-label", "Prompt name");
const text = el("textarea");
text.rows = 5;
text.placeholder = "VP, Head or Director of Data and AI. Owns the data platform and the ML teams, sets the roadmap with the business, in a product company.";
text.value = prompt.text;
text.setAttribute("aria-label", "What this prompt looks for");
const drop = linky("Remove");
drop.addEventListener("click", () => remove(row));
row.append(name, drop, text);
return { node: row, read: () => ({ name: name.value.trim(), text: text.value }) };
}
function select(options, value, label) {
const box = el("select");
for (const [key, text] of options) {
const option = el("option", "", text);
option.value = key;
box.appendChild(option);
}
box.value = value;
box.setAttribute("aria-label", label);
return box;
}
// A gate as a row: field, mode, values, the weight while it is prefer, and
// Remove. Values untouched since the load are sent as they came, so a
// value with a comma in it survives a save.
function ruleRow(gate, remove) {
const row = el("div", "rule-row");
const field = select(FIELDS.map((f) => [f, f.replace("_", " ")]), gate.field, "Field");
const mode = select(MODES, gate.mode, "Mode");
const values = el("input");
values.type = "text";
values.required = true;
values.spellcheck = false;
values.placeholder = "senior, staff";
values.value = gate.values.join(", ");
values.setAttribute("aria-label", "Values, separated by commas");
const loaded = values.value;
const weight = el("input");
weight.type = "number";
weight.min = "0";
weight.step = "0.05";
weight.value = gate.mode === "prefer" && gate.weight !== undefined ? gate.weight : HYBRID_WEIGHT;
weight.setAttribute("aria-label", "Weight");
weight.title = "What a role that meets it gains.";
// A must or one-of gate carries a weight nobody reads; kept as it came.
const kept = gate.mode === "prefer" ? undefined : gate.weight;
const drop = linky("Remove");
drop.addEventListener("click", () => remove(row));
const shown = () => { weight.hidden = mode.value !== "prefer"; };
mode.addEventListener("change", shown);
shown();
row.append(field, mode, values, weight, drop);
function read() {
const parts = values.value === loaded
? gate.values
: values.value.split(",").map((v) => v.trim()).filter(Boolean)
.map((v) => (field.value === "salary_min" ? Number(v) : v));
const out = { field: field.value, values: parts, mode: mode.value };
if (mode.value === "prefer") out.weight = Number(weight.value);
else if (kept !== undefined && kept !== 1) out.weight = kept;
return out;
}
return { node: row, read };
}
// Share out an agent's gates between the Where block and Other rules. Each
// Where line takes the first gate it can say whole; the rest are rows. A
// must or one-of gate with a weight other than the default stays a row,
// since Where would drop it.
function distribute(gates) {
const where = { in: null, anyCountry: null, remote: null, places: null, hybrid: null };
const rest = [];
const plain = (g) => g.weight === undefined || g.weight === 1;
const only = (g, value) => g.values.length === 1 && g.values[0] === value;
for (const g of gates) {
let slot = null;
if (g.field === "country" && g.mode === "require" && plain(g)) slot = "in";
else if (g.field === "country" && g.mode === "any" && plain(g)) slot = "anyCountry";
else if (g.field === "remote" && g.mode === "any" && plain(g) && only(g, "remote")) slot = "remote";
else if (g.field === "place" && g.mode === "any" && plain(g)) slot = "places";
else if (g.field === "remote" && g.mode === "prefer" && only(g, "hybrid")) slot = "hybrid";
if (slot && !where[slot]) where[slot] = g;
else rest.push(g);
}
return { where, rest };
}
function form(row) {
const div = el("article", "agent-card editing");
const f = $("#agent-form-template").content.querySelector("form").cloneNode(true);
@@ -186,8 +448,80 @@
const limits = Object.assign({}, DEFAULT_LIMITS, row ? row.limits : {});
$("#agent-form-title", f).textContent = row ? `Edit ${row.name}` : "New agent";
$("#a-name", f).value = row ? row.name : "";
$("#a-intent", f).value = row ? row.intent : "";
$("#a-gates", f).value = JSON.stringify(row ? row.gates : [], null, 2);
// A new agent, or one with none, starts with one empty prompt.
const prompts = [];
const dropPrompt = (node) => {
prompts.splice(prompts.findIndex((p) => p.node === node), 1);
node.remove();
changed(f);
};
const addPrompt = (prompt) => {
const entry = promptRow(prompt, dropPrompt);
prompts.push(entry);
$("#a-prompts", f).appendChild(entry.node);
return entry;
};
const loaded = row ? promptsOf(row) : [];
(loaded.length ? loaded : [{ name: "", text: "" }]).forEach(addPrompt);
$("#a-add-prompt", f).addEventListener("click", () => {
$("input", addPrompt({ name: "", text: "" }).node).focus();
changed(f);
});
const { where, rest } = distribute(row ? row.gates : []);
const within = chipInput(where.in ? where.in.values : [], "Only roles in these countries", "DK, SE", true);
const anyCountry = chipInput(where.anyCountry ? where.anyCountry.values : [], "Or in one of these countries", "DK", true);
const places = chipInput(where.places ? where.places.values : [], "Or in one of these places", "Malmö, Lund", false);
$("#a-in", f).replaceWith(within.node);
$("#a-any-country", f).replaceWith(anyCountry.node);
$("#a-places", f).replaceWith(places.node);
// Europe is offered while it is not whole in the list already.
const europe = $("#a-europe", f);
const offerEurope = () => { europe.hidden = within.whole(); };
europe.addEventListener("click", () => within.add(EUROPE));
offerEurope();
$("#a-remote", f).checked = Boolean(where.remote);
$("#a-hybrid", f).checked = Boolean(where.hybrid);
$("#a-hybrid-weight", f).value = where.hybrid && where.hybrid.weight !== undefined ? where.hybrid.weight : HYBRID_WEIGHT;
const rules = [];
const dropRule = (node) => {
rules.splice(rules.findIndex((r) => r.node === node), 1);
node.remove();
changed(f);
};
const addRule = (gate) => {
const entry = ruleRow(gate, dropRule);
rules.push(entry);
$("#a-rules", f).appendChild(entry.node);
return entry;
};
rest.forEach(addRule);
$("#a-add-rule", f).addEventListener("click", () => {
$("select", addRule({ field: "seniority", values: [], mode: "prefer", weight: HYBRID_WEIGHT }).node).focus();
changed(f);
});
// The gates Save sends: the Where lines that say something, then the rows.
function gates() {
const out = [];
const line = (chips, field, mode) => {
const values = chips.values();
if (values.length) out.push({ field, values, mode });
};
line(within, "country", "require");
line(anyCountry, "country", "any");
if ($("#a-remote", f).checked) out.push({ field: "remote", values: ["remote"], mode: "any" });
line(places, "place", "any");
if ($("#a-hybrid", f).checked) {
out.push({ field: "remote", values: ["hybrid"], mode: "prefer", weight: Number($("#a-hybrid-weight", f).value) });
}
return out.concat(rules.map((r) => r.read()));
}
const view = () => { $("#a-gates", f).value = JSON.stringify(gates(), null, 2); };
view();
$("#a-fit", f).value = weights.fit;
$("#a-recency", f).value = weights.recency;
$("#a-affinity", f).value = weights.affinity;
@@ -197,8 +531,9 @@
$("#a-summarise-top", f).value = limits.summarise_top;
$("#a-summarise-min", f).value = limits.summarise_min;
$("#a-delete", f).hidden = !row;
f.addEventListener("input", () => { f.dataset.dirty = "true"; });
f.addEventListener("submit", (event) => save(event, row));
f.addEventListener("input", () => { f.dataset.dirty = "true"; view(); offerEurope(); });
f.addEventListener("change", view);
f.addEventListener("submit", (event) => save(event, row, () => prompts.map((p) => p.read()), gates));
$("#a-cancel", f).addEventListener("click", () => { close(); render(); });
$("#a-delete", f).addEventListener("click", () => row && removeAgent(row));
div.appendChild(f);
@@ -244,19 +579,20 @@
: "No search agents yet. New agent describes one.";
}
async function save(event, row) {
// `readPrompts` and `readGates` are the form's rows as the API takes them.
// A prompt with neither name nor text is an empty row, left out.
async function save(event, row, readPrompts, readGates) {
event.preventDefault();
const f = event.target;
let gates;
try {
gates = JSON.parse($("#a-gates", f).value || "[]");
} catch (err) {
window.toast.error(`Gates are not valid JSON: ${err.message}`);
const prompts = readPrompts().filter((p) => p.name || p.text.trim());
if (prompts.some((p) => !p.name)) {
window.toast.error("Every prompt needs a name.");
return;
}
const gates = readGates();
const body = {
name: $("#a-name", f).value.trim(),
intent: $("#a-intent", f).value,
prompts,
gates,
weights: {
fit: Number($("#a-fit", f).value),