Files
jobboard/app/static/agents.js
T
Wolf 562fa3ab09 Merge branch 'r2-backend'
# Conflicts:
#	app/storage/schema.py
2026-09-29 15:22:03 +02:00

351 lines
13 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 };
// What the model reads after a pull, per agent: the best `extract_top`
// roles scoring at least `extract_min` get their gate columns filled, the
// 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";
/** @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 limitsLine(row) {
const l = Object.assign({}, DEFAULT_LIMITS, row.limits);
return `reads ${l.extract_top} at ${l.extract_min} · summarises ${l.summarise_top} at ${l.summarise_min}`;
}
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)));
div.appendChild(el("p", "agent-limits", limitsLine(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 : {});
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-fit", f).value = weights.fit;
$("#a-recency", f).value = weights.recency;
$("#a-affinity", f).value = weights.affinity;
$("#a-taste", f).value = weights.taste;
$("#a-extract-top", f).value = limits.extract_top;
$("#a-extract-min", f).value = limits.extract_min;
$("#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));
$("#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),
},
limits: {
extract_top: Number($("#a-extract-top", f).value),
extract_min: Number($("#a-extract-min", f).value),
summarise_top: Number($("#a-summarise-top", f).value),
summarise_min: Number($("#a-summarise-min", 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();
}
// Ask for a `score` and wait for the job the server answers with, so
// "scored" means the Roles tab already orders by what was saved. While a
// score is running that is a follow-up queued after it, since the running
// one read the agent before the save. `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/score", { method: "POST" });
} 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();
});
})();