Files
jobboard/app/static/jobs.js
T

157 lines
6.2 KiB
JavaScript

/* The Runs section of Sources: the "Run the pull now" button, and the
* switches for the stages of a pull: extract, summarise and judge, which ask a
* model, and fetch text, which reads a role's own page and asks none.
*
* The switches are `/api/stages`: one row per stage, drawn as the search
* rows above are (`.srow`, `.switch`), and a switch is a PUT that stores the
* setting and starts nothing.
*
* What the queue is doing and when it runs next are `<desk-jobs>`' and
* `<desk-schedules>`' business, and they draw themselves. The single stages
* have no buttons: the cascade is the unit of work, and `app job <stage>`
* keeps the per-stage entry points.
*
* Both read the `app:jobs` snapshot from live.js rather than asking the
* elements, which keep to a shadow root and owe nobody an answer.
*
* Failures are the server's sentence through `api()`, which toasts it -- so
* there is no error handling here beyond keeping the button usable.
*/
(function () {
"use strict";
const pullButton = document.getElementById("pull-now");
let pending = false; // the POST is in the air
let pulling = false; // the latest snapshot has a live pull cascade
/* A pull queued by the button or a schedule is the `pull` job, which
begins a cascade of its own named `pull on its own`, and the chain's
tasks join it; `pull` is the name a cascade started as one would have.
Live while any task has a job queued or running: the pipeline keeps a
finished cascade in the snapshot for a few seconds, and that is not a
pull still going. As newsdesk's Fetch: the whole cascade, not the `pull`
task, which is over seconds in with five tasks to go. */
const isPull = (c) => c.name === "pull" || c.name === "pull on its own";
const live = (c) => (c.tasks || []).some((t) => t.running || t.queued);
function paint() {
if (!pullButton) return;
pullButton.disabled = pending || pulling;
pullButton.title = pulling ? "A pull is running." : "";
}
if (pullButton) {
pullButton.addEventListener("click", async () => {
pending = true;
paint();
try {
const job = await window.api("/api/jobs", { method: "POST", body: { kind: "pull" } });
window.toast.good(`${job.label || "pull"}: queued.`);
} catch (e) {
/* already toasted */
} finally {
pending = false;
paint();
}
});
}
// ------------------------------------------------- what the model reads
// `depth` is whose setting says how many roles a stage reads: every stage
// but summarise reads each agent's cut, summarise its own size.
const DOES = {
extract: "fills in location, remote area, seniority, languages and the like from what the posting says, for every role in the cut",
summarise: "writes a short summary and tags for the top of the feed, and for every role you liked or saved",
judge: "scores every role in the cut against a search agent's CV, for CV match",
fetch_text: "reads the own page of every role in the cut that came without text, with no model, before extract and judge",
};
const NAMES = { fetch_text: "Fetch text" };
const el = (tag, className, text) => {
const node = document.createElement(tag);
if (className) node.className = className;
if (text !== undefined) node.textContent = text;
return node;
};
/** The On/Off switch of a search agent's card, on a stage. */
function stageSwitch(stage, rerender) {
const label = el("label", "switch");
const box = el("input");
box.type = "checkbox";
box.setAttribute("role", "switch");
box.checked = stage.on;
box.setAttribute("aria-label", `Run ${stage.name} in the pull`);
const track = el("span", "switch-track");
track.setAttribute("aria-hidden", "true");
label.append(box, track, el("span", "switch-label", stage.on ? "On" : "Off"));
box.addEventListener("change", async () => {
box.disabled = true;
try {
Object.assign(stage, await window.api(`/api/stages/${stage.name}`,
{ method: "PUT", body: { on: box.checked } }));
} catch (err) {
box.checked = !box.checked;
box.disabled = false;
return;
}
rerender();
});
return label;
}
function renderStages(stages) {
const list = document.getElementById("stages-list");
list.replaceChildren();
for (const stage of stages) {
const row = el("div", "srow");
if (!stage.on) row.dataset.muted = "true";
const name = el("span", "name");
name.append(el("span", "", NAMES[stage.name] || stage.name[0].toUpperCase() + stage.name.slice(1)),
...(stage.model ? [el("span", "badge", stage.model)] : []));
row.append(name, stageSwitch(stage, () => renderStages(stages)));
const sub = el("span", "sub");
/* Depth is per search agent: the cut for fetch text, extract and judge,
summarise's own size. Fetch text keeps per_board, per_run and
retry_days in config.yaml, which only bound its load on a host. */
sub.append(`${DOES[stage.name]}. How many it reads is each search agent's `
+ `${stage.name === "summarise" ? "summarise size" : "cut"}, under `);
const agents = el("a", "", "Search agents");
agents.href = "/settings/agents";
sub.append(agents);
sub.append(stage.source === "config"
? ". Following config.yaml until this switch is used."
: ".");
row.appendChild(sub);
list.appendChild(row);
}
const first = stages[0];
document.getElementById("stages-answer").textContent = first
? first.answer[0].toUpperCase() + first.answer.slice(1) + "."
+ (first.backend ? "" : " The switches still work, and a pull passes these stages until a model is reachable.")
: "";
}
async function loadStages() {
if (!document.getElementById("stages-list")) return;
try {
renderStages(await window.api("/api/stages"));
} catch (err) {
/* already toasted */
}
}
function update(state) {
pulling = (state.cascades || []).some((c) => isPull(c) && live(c));
paint();
}
loadStages();
document.addEventListener("app:jobs", (e) => update(e.detail || {}));
// A snapshot that arrived before this script loaded.
if (window.live && window.live.jobs) update(window.live.jobs);
})();