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>
328 lines
12 KiB
JavaScript
328 lines
12 KiB
JavaScript
/* The Search agents page: what Best match on the Roles tab is measured
|
|
* 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
|
|
* 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.
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
const { api, count } = window;
|
|
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 {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 newestFirst(rows) {
|
|
return [...rows].sort((a, b) => b.id - a.id);
|
|
}
|
|
|
|
// 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 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", 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 {
|
|
const saved = await api("/api/agents", {
|
|
method: "POST", body: Object.assign({}, row, { enabled: box.checked }),
|
|
});
|
|
Object.assign(row, saved);
|
|
} catch (err) {
|
|
box.checked = !box.checked;
|
|
box.disabled = false;
|
|
return;
|
|
}
|
|
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 label;
|
|
}
|
|
|
|
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}`;
|
|
}
|
|
|
|
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);
|
|
}
|
|
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 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", 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;
|
|
}
|
|
|
|
// 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 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}`);
|
|
return;
|
|
}
|
|
const body = {
|
|
name: $("#a-name", f).value.trim(),
|
|
intent: $("#a-intent", f).value,
|
|
gates,
|
|
weights: {
|
|
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
|
|
// card's switch is where an existing one is switched.
|
|
enabled: row ? row.enabled : true,
|
|
};
|
|
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) {
|
|
button.disabled = false;
|
|
return;
|
|
}
|
|
// The newest is first, so a new agent's card stays where its form was.
|
|
editing = null;
|
|
editor = null;
|
|
render();
|
|
const agent = agents.find((a) => a.id === saved.id) || saved;
|
|
await rescore(statusOf(agent.id), agent, "Saved");
|
|
}
|
|
|
|
async function removeAgent(row) {
|
|
if (!window.confirm(`Delete ${row.name}? Its votes and liked companies go with it.`)) {
|
|
return;
|
|
}
|
|
try {
|
|
await api(`/api/agents/${row.id}`, { method: "DELETE" });
|
|
} catch (err) {
|
|
return;
|
|
}
|
|
agents = agents.filter((a) => a.id !== row.id);
|
|
statuses.delete(row.id);
|
|
if (editing === row.id) {
|
|
editing = null;
|
|
editor = null;
|
|
}
|
|
render();
|
|
}
|
|
|
|
// 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 = `${said}, but scoring did not start.`;
|
|
return;
|
|
}
|
|
for (let i = 0; i < 300; i += 1) {
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
let snapshot;
|
|
try {
|
|
snapshot = await api("/api/jobs");
|
|
} catch (err) {
|
|
return;
|
|
}
|
|
const busy = [...snapshot.running, ...snapshot.queued].some((j) => j.id === job.id);
|
|
if (busy) continue;
|
|
const done = snapshot.recent.find((j) => j.id === job.id);
|
|
if (done && done.state !== "done") {
|
|
status.textContent = `${said}. Scoring ${done.state}.`;
|
|
if (done.error) window.toast.error(`Scoring ${done.state}: ${done.error}`);
|
|
return;
|
|
}
|
|
status.replaceChildren(`${said} and scored. `,
|
|
link(`/?agent=${agent.id}`, `Open Roles as ${agent.name} →`));
|
|
return;
|
|
}
|
|
status.replaceChildren(`${said}. Scoring is still running; `, runs(), ".");
|
|
}
|
|
|
|
// -------------------------------------------------------------------- init
|
|
|
|
async function load() {
|
|
agents = await api("/api/agents");
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
if (!$("#agents-list")) return;
|
|
$("#a-new").addEventListener("click", () => open(null));
|
|
try {
|
|
await load();
|
|
} catch (err) {
|
|
return;
|
|
}
|
|
// With none yet, the page is the form for the first one.
|
|
if (!agents.length) {
|
|
editing = NEW;
|
|
editor = form(null);
|
|
}
|
|
render();
|
|
});
|
|
})();
|