Viewing as leads the Roles rail; settings get a rail, agent cards, one Sources section at a time
A search agent is a persona, and every like or pass belongs to the one being viewed. The rail now opens on Viewing as: a full-width chooser, the name alone with one agent, a note with none, remembered in the URL and localStorage. The vote buttons, the reader's affinity control, the feed, the tag counts and the reader fetch all carry that agent; before, votes sent none and every one landed on the default agent. Best first and Newest first are back as the segmented control under Order, independent of the agent. The rail selects no longer clip descenders: Chrome ignores line-height on a select, so the fixed box is gone in favour of padding and a min-height. Settings render inside a sticky rail (Search agents; Sources with Job boards, Company boards, Runs, Messages; Account), a strip under the header on a phone, so Runs is one click away however long the company list gets. Sources shows one section at a time, chosen by the fragment, so the old redirects still land. Search agents are cards, newest first: name, an On/Off switch with its word, the intent in full, the weights, Edit in place, New agent on top, and a link to Roles as that agent. Switching one on rescores it. replace_scores writes nothing for an agent deleted while score was running, instead of failing the run on the foreign key. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
063771d617
commit
c4f2bcc0c9
20 files changed
+836
-326
No files matched your search
+203
-103
@@ -1,9 +1,14 @@
|
||||
/* The Search agents page: what Best match on the Roles tab is measured
|
||||
* against, one agent at a time.
|
||||
*
|
||||
* The list is `/api/agents`; the form edits the agent picked from it, New
|
||||
* clears it, Delete removes it. Votes, liked companies and scores are the
|
||||
* agent's, so a delete takes them with it, and the confirm says so.
|
||||
* 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
|
||||
* 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,
|
||||
* so a delete takes them with it, and the confirm says so.
|
||||
*
|
||||
* Every request goes through `api()` from app.js, so a failure is one toast
|
||||
* carrying the server's own sentence.
|
||||
@@ -15,23 +20,69 @@
|
||||
const $ = (sel, root = document) => root.querySelector(sel);
|
||||
|
||||
const DEFAULT_WEIGHTS = { fit: 0.6, recency: 0.2, affinity: 0.2, taste: 0.2 };
|
||||
const NEW = "new";
|
||||
|
||||
/** @type {object[]} every agent, as the server lists them */
|
||||
let agents = [];
|
||||
/** @type {object | null} the agent the form edits, null for a new one */
|
||||
let agent = null;
|
||||
/** @type {number | "new" | null} the card in edit mode: an agent's id, NEW
|
||||
* for the blank one on top, null for none */
|
||||
let editing = null;
|
||||
/** @type {HTMLElement | null} that card, kept across renders so a switch
|
||||
* flipped on another card does not wipe what is being typed */
|
||||
let editor = null;
|
||||
/** @type {Map<number, HTMLElement>} each card's status line, kept across
|
||||
* renders for the same reason: a "Scoring…" outlives the card it is on */
|
||||
const statuses = new Map();
|
||||
|
||||
function byId(rows) {
|
||||
return [...rows].sort((a, b) => a.id - b.id);
|
||||
function newestFirst(rows) {
|
||||
return [...rows].sort((a, b) => b.id - a.id);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- the list
|
||||
// What the Roles tab and the company list use when no agent is picked: the
|
||||
// enabled one with the lowest id, else the lowest id (`Store.default_agent`).
|
||||
function defaultAgent() {
|
||||
const byId = [...agents].sort((a, b) => a.id - b.id);
|
||||
return byId.find((a) => a.enabled) || byId[0] || null;
|
||||
}
|
||||
|
||||
function el(tag, className, text) {
|
||||
const node = document.createElement(tag);
|
||||
if (className) node.className = className;
|
||||
if (text !== undefined) node.textContent = text;
|
||||
return node;
|
||||
}
|
||||
|
||||
function link(href, text) {
|
||||
const a = el("a", "", text);
|
||||
a.href = href;
|
||||
return a;
|
||||
}
|
||||
|
||||
function statusOf(id) {
|
||||
if (!statuses.has(id)) {
|
||||
const line = el("p", "count agent-status");
|
||||
line.setAttribute("role", "status");
|
||||
statuses.set(id, line);
|
||||
}
|
||||
return statuses.get(id);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- a card
|
||||
|
||||
// A switch with its state in words: a bare checkbox did not say what it
|
||||
// switched. Switching one on scores it, since the pulls it sat out left it
|
||||
// with no scores or stale ones.
|
||||
function onSwitch(row) {
|
||||
const box = document.createElement("input");
|
||||
const label = el("label", "switch");
|
||||
label.title = "On: scored with every pull and offered in the order menu on Roles.";
|
||||
const box = el("input");
|
||||
box.type = "checkbox";
|
||||
box.setAttribute("role", "switch");
|
||||
box.checked = Boolean(row.enabled);
|
||||
box.setAttribute("aria-label", `Enable ${row.name}`);
|
||||
box.setAttribute("aria-label", row.name);
|
||||
const track = el("span", "switch-track");
|
||||
track.setAttribute("aria-hidden", "true");
|
||||
label.append(box, track, el("span", "switch-label", row.enabled ? "On" : "Off"));
|
||||
box.addEventListener("change", async () => {
|
||||
box.disabled = true;
|
||||
try {
|
||||
@@ -41,111 +92,162 @@
|
||||
Object.assign(row, saved);
|
||||
} catch (err) {
|
||||
box.checked = !box.checked;
|
||||
box.disabled = false;
|
||||
return;
|
||||
}
|
||||
box.disabled = false;
|
||||
render();
|
||||
if (row.enabled) rescore(statusOf(row.id), row, "Switched on");
|
||||
else statusOf(row.id).textContent = "Off: not scored, and not offered on Roles.";
|
||||
});
|
||||
return box;
|
||||
return label;
|
||||
}
|
||||
|
||||
function render() {
|
||||
const body = $("#agents-list");
|
||||
body.replaceChildren();
|
||||
for (const row of byId(agents)) {
|
||||
const div = document.createElement("div");
|
||||
div.className = "crow";
|
||||
if (!row.enabled) div.dataset.hidden = "true";
|
||||
if (agent && agent.id === row.id) div.dataset.current = "true";
|
||||
const name = document.createElement("span");
|
||||
name.className = "name";
|
||||
name.textContent = row.name;
|
||||
div.appendChild(name);
|
||||
div.appendChild(onSwitch(row));
|
||||
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}`;
|
||||
}
|
||||
|
||||
const sub = document.createElement("span");
|
||||
sub.className = "sub";
|
||||
const w = Object.assign({}, DEFAULT_WEIGHTS, row.weights);
|
||||
sub.append(`fit ${w.fit} · newness ${w.recency} · companies ${w.affinity} · liked ${w.taste}`, " ");
|
||||
const edit = document.createElement("button");
|
||||
edit.type = "button";
|
||||
edit.className = "linky";
|
||||
edit.textContent = "Edit";
|
||||
edit.addEventListener("click", () => fill(row));
|
||||
const remove = document.createElement("button");
|
||||
remove.type = "button";
|
||||
remove.className = "linky";
|
||||
remove.textContent = "Delete";
|
||||
remove.addEventListener("click", () => removeAgent(row));
|
||||
sub.append(edit, " ", remove);
|
||||
div.appendChild(sub);
|
||||
body.appendChild(div);
|
||||
function card(row, fallback) {
|
||||
const div = el("article", "agent-card");
|
||||
if (!row.enabled) div.dataset.off = "true";
|
||||
|
||||
const head = el("div", "agent-card-head");
|
||||
head.appendChild(el("h2", "agent-name", row.name));
|
||||
if (fallback) {
|
||||
const badge = el("span", "badge", "default");
|
||||
badge.title = "What Roles and the company list use when no agent is picked.";
|
||||
head.appendChild(badge);
|
||||
}
|
||||
$("#agents-count").textContent = agents.length
|
||||
? count(agents.length, "search agent")
|
||||
: "No search agents yet. Describe one below.";
|
||||
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."));
|
||||
div.appendChild(el("p", "agent-weights", weightsLine(row)));
|
||||
|
||||
const actions = el("div", "agent-card-actions");
|
||||
const edit = el("button", "secondary", "Edit");
|
||||
edit.type = "button";
|
||||
edit.addEventListener("click", () => open(row));
|
||||
actions.appendChild(edit);
|
||||
// An agent that is off is not in the order menu, so Roles would open in
|
||||
// another agent's order: the link is only there while this one is on.
|
||||
if (row.enabled) actions.appendChild(link(`/?agent=${row.id}`, "Open Roles as this agent →"));
|
||||
const remove = el("button", "linky", "Delete");
|
||||
remove.type = "button";
|
||||
remove.addEventListener("click", () => removeAgent(row));
|
||||
actions.appendChild(remove);
|
||||
div.appendChild(actions);
|
||||
div.appendChild(statusOf(row.id));
|
||||
return div;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- the form
|
||||
|
||||
function fill(row) {
|
||||
agent = row;
|
||||
function form(row) {
|
||||
const div = el("article", "agent-card editing");
|
||||
const f = $("#agent-form-template").content.querySelector("form").cloneNode(true);
|
||||
const weights = Object.assign({}, DEFAULT_WEIGHTS, row ? row.weights : {});
|
||||
$("#agent-form-title").textContent = row ? `Edit ${row.name}` : "New agent";
|
||||
$("#a-name").value = row ? row.name : "";
|
||||
$("#a-intent").value = row ? row.intent : "";
|
||||
$("#a-gates").value = JSON.stringify(row ? row.gates : [], null, 2);
|
||||
$("#a-fit").value = weights.fit;
|
||||
$("#a-recency").value = weights.recency;
|
||||
$("#a-affinity").value = weights.affinity;
|
||||
$("#a-taste").value = weights.taste;
|
||||
$("#a-delete").hidden = !row;
|
||||
$("#agent-status").textContent = "";
|
||||
render();
|
||||
if (row) $("#agent-form").scrollIntoView({ behavior: "smooth", block: "start" });
|
||||
$("#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-fit", f).value = weights.fit;
|
||||
$("#a-recency", f).value = weights.recency;
|
||||
$("#a-affinity", f).value = weights.affinity;
|
||||
$("#a-taste", f).value = weights.taste;
|
||||
$("#a-delete", f).hidden = !row;
|
||||
f.addEventListener("input", () => { f.dataset.dirty = "true"; });
|
||||
f.addEventListener("submit", (event) => save(event, row));
|
||||
$("#a-cancel", f).addEventListener("click", () => { close(); render(); });
|
||||
$("#a-delete", f).addEventListener("click", () => row && removeAgent(row));
|
||||
div.appendChild(f);
|
||||
if (row) div.appendChild(statusOf(row.id));
|
||||
return div;
|
||||
}
|
||||
|
||||
async function save(event) {
|
||||
// Put the open form away, asking first if it holds changes. False when the
|
||||
// answer was to keep them.
|
||||
function close() {
|
||||
const f = editor && $("form", editor);
|
||||
if (f && f.dataset.dirty && !window.confirm("Discard the changes to this agent?")) {
|
||||
return false;
|
||||
}
|
||||
editing = null;
|
||||
editor = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Edit `row` in its own card, or a new agent in a blank card on top.
|
||||
function open(row) {
|
||||
const target = row ? row.id : NEW;
|
||||
if (editing !== target) {
|
||||
if (!close()) return;
|
||||
editing = target;
|
||||
editor = form(row);
|
||||
render();
|
||||
}
|
||||
editor.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
||||
$("#a-name", editor).focus({ preventScroll: true });
|
||||
}
|
||||
|
||||
function render() {
|
||||
const cards = [];
|
||||
if (editing === NEW) cards.push(editor);
|
||||
const fallback = defaultAgent();
|
||||
for (const row of newestFirst(agents)) {
|
||||
cards.push(editing === row.id ? editor : card(row, agents.length > 1 && row === fallback));
|
||||
}
|
||||
$("#agents-list").replaceChildren(...cards);
|
||||
$("#agents-count").textContent = agents.length
|
||||
? count(agents.length, "search agent")
|
||||
: "No search agents yet. New agent describes one.";
|
||||
}
|
||||
|
||||
async function save(event, row) {
|
||||
event.preventDefault();
|
||||
const status = $("#agent-status");
|
||||
const f = event.target;
|
||||
let gates;
|
||||
try {
|
||||
gates = JSON.parse($("#a-gates").value || "[]");
|
||||
gates = JSON.parse($("#a-gates", f).value || "[]");
|
||||
} catch (err) {
|
||||
window.toast.error(`Gates are not valid JSON: ${err.message}`);
|
||||
return;
|
||||
}
|
||||
const body = {
|
||||
name: $("#a-name").value.trim(),
|
||||
intent: $("#a-intent").value,
|
||||
name: $("#a-name", f).value.trim(),
|
||||
intent: $("#a-intent", f).value,
|
||||
gates,
|
||||
weights: {
|
||||
fit: Number($("#a-fit").value),
|
||||
recency: Number($("#a-recency").value),
|
||||
affinity: Number($("#a-affinity").value),
|
||||
taste: Number($("#a-taste").value),
|
||||
fit: Number($("#a-fit", f).value),
|
||||
recency: Number($("#a-recency", f).value),
|
||||
affinity: Number($("#a-affinity", f).value),
|
||||
taste: Number($("#a-taste", f).value),
|
||||
},
|
||||
// Saving an agent means it is wanted, so a new one starts on; the
|
||||
// list's checkbox is where an existing one is switched.
|
||||
enabled: agent ? agent.enabled : true,
|
||||
// card's switch is where an existing one is switched.
|
||||
enabled: row ? row.enabled : true,
|
||||
};
|
||||
if (agent) body.id = agent.id;
|
||||
const button = $("#agent-form button[type=submit]");
|
||||
if (row) body.id = row.id;
|
||||
const button = $("button[type=submit]", f);
|
||||
button.disabled = true;
|
||||
let saved;
|
||||
try {
|
||||
saved = await api("/api/agents", { method: "POST", body });
|
||||
await load();
|
||||
} catch (err) {
|
||||
status.textContent = "";
|
||||
button.disabled = false;
|
||||
return;
|
||||
}
|
||||
agent = agents.find((a) => a.id === saved.id) || saved;
|
||||
$("#agent-form-title").textContent = `Edit ${agent.name}`;
|
||||
$("#a-delete").hidden = false;
|
||||
// The newest is first, so a new agent's card stays where its form was.
|
||||
editing = null;
|
||||
editor = null;
|
||||
render();
|
||||
await rescore(status);
|
||||
button.disabled = false;
|
||||
const agent = agents.find((a) => a.id === saved.id) || saved;
|
||||
await rescore(statusOf(agent.id), agent, "Saved");
|
||||
}
|
||||
|
||||
async function removeAgent(row) {
|
||||
@@ -158,19 +260,24 @@
|
||||
return;
|
||||
}
|
||||
agents = agents.filter((a) => a.id !== row.id);
|
||||
if (agent && agent.id === row.id) fill(null);
|
||||
else render();
|
||||
statuses.delete(row.id);
|
||||
if (editing === row.id) {
|
||||
editing = null;
|
||||
editor = null;
|
||||
}
|
||||
render();
|
||||
}
|
||||
|
||||
// Start the `score` job and wait for it, so "Saved and scored" means the
|
||||
// Roles tab already orders by what was saved.
|
||||
async function rescore(status) {
|
||||
status.textContent = "Saved. Scoring…";
|
||||
// Start the `score` job and wait for it, so "scored" means the Roles tab
|
||||
// already orders by what was saved. `said` is what happened first.
|
||||
async function rescore(status, agent, said) {
|
||||
const runs = () => link("/settings/sources#runs", "follow it under Runs");
|
||||
status.replaceChildren(`${said}. Scoring every open role; `, runs(), ".");
|
||||
let job;
|
||||
try {
|
||||
job = await api("/api/jobs", { method: "POST", body: { kind: "score", payload: {} } });
|
||||
} catch (err) {
|
||||
status.textContent = "Saved, but scoring did not start.";
|
||||
status.textContent = `${said}, but scoring did not start.`;
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < 300; i += 1) {
|
||||
@@ -185,22 +292,15 @@
|
||||
if (busy) continue;
|
||||
const done = snapshot.recent.find((j) => j.id === job.id);
|
||||
if (done && done.state !== "done") {
|
||||
status.textContent = `Saved. Scoring ${done.state}.`;
|
||||
status.textContent = `${said}. Scoring ${done.state}.`;
|
||||
if (done.error) window.toast.error(`Scoring ${done.state}: ${done.error}`);
|
||||
return;
|
||||
}
|
||||
status.replaceChildren("Saved and scored. ");
|
||||
const link = document.createElement("a");
|
||||
link.href = `/?agent=${agent.id}`;
|
||||
link.textContent = "See the best matches";
|
||||
status.appendChild(link);
|
||||
status.replaceChildren(`${said} and scored. `,
|
||||
link(`/?agent=${agent.id}`, `Open Roles as ${agent.name} →`));
|
||||
return;
|
||||
}
|
||||
status.replaceChildren("Saved. Scoring is still running; ");
|
||||
const link = document.createElement("a");
|
||||
link.href = "/settings/sources#runs";
|
||||
link.textContent = "follow it under Runs";
|
||||
status.append(link, ".");
|
||||
status.replaceChildren(`${said}. Scoring is still running; `, runs(), ".");
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- init
|
||||
@@ -211,17 +311,17 @@
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
if (!$("#agents-list")) return;
|
||||
$("#a-new").addEventListener("click", () => open(null));
|
||||
try {
|
||||
await load();
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
// The default agent, the enabled one with the lowest id, is what the
|
||||
// Roles tab and the company list fall back to, so it is what opens.
|
||||
const first = byId(agents).find((a) => a.enabled) || byId(agents)[0] || null;
|
||||
fill(first);
|
||||
$("#agent-form").addEventListener("submit", save);
|
||||
$("#a-new").addEventListener("click", () => fill(null));
|
||||
$("#a-delete").addEventListener("click", () => agent && removeAgent(agent));
|
||||
// With none yet, the page is the form for the first one.
|
||||
if (!agents.length) {
|
||||
editing = NEW;
|
||||
editor = form(null);
|
||||
}
|
||||
render();
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user