/* The Ranking page: what Best match on the Roles tab is measured against. * * The UI shows one ranking. Underneath it is the first enabled search agent, * the oldest by id, since the `agents` table still holds as many as the CLI * or the config put there; this page edits that one and never lists, adds or * deletes. An empty table gets its first agent on the first save. * * 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); /** The name a ranking made here is saved under; the UI never shows it unless the CLI adds a second one. */ const NEW_NAME = "My ranking"; const DEFAULT_WEIGHTS = { fit: 0.6, recency: 0.2, affinity: 0.2 }; /** @type {object | null} the agent this page edits, null until one is saved */ let agent = null; /* The first enabled agent, else the first at all: editing it here means it is wanted, so a save switches it on. */ function pick(agents) { const byId = [...agents].sort((a, b) => a.id - b.id); return byId.find((a) => a.enabled) || byId[0] || null; } function fill(others) { $("#a-intent").value = agent ? agent.intent : ""; $("#a-gates").value = JSON.stringify(agent ? agent.gates : [], null, 2); const weights = agent ? agent.weights : DEFAULT_WEIGHTS; $("#a-fit").value = weights.fit; $("#a-recency").value = weights.recency; $("#a-affinity").value = weights.affinity; const note = $("#ranking-others"); note.hidden = !(agent && others > 0); if (!note.hidden) { note.textContent = `This edits "${agent.name}". ${count(others, "other ranking")} ` + "can be picked by name in the order menu on Roles."; } } async function save(event) { event.preventDefault(); const status = $("#ranking-status"); let gates; try { gates = JSON.parse($("#a-gates").value || "[]"); } catch (err) { window.toast.error(`Gates are not valid JSON: ${err.message}`); return; } const body = { name: agent ? agent.name : NEW_NAME, intent: $("#a-intent").value, gates, weights: { fit: Number($("#a-fit").value), recency: Number($("#a-recency").value), affinity: Number($("#a-affinity").value), }, enabled: true, }; if (agent) body.id = agent.id; const button = $("#ranking-form button[type=submit]"); button.disabled = true; try { agent = await api("/api/agents", { method: "POST", body }); } catch (err) { status.textContent = ""; button.disabled = false; return; } await rescore(status); button.disabled = false; } // 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…"; let job; try { job = await api("/api/jobs", { method: "POST", body: { kind: "score", payload: {} } }); } catch (err) { status.textContent = "Saved, 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 = `Saved. 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); return; } status.replaceChildren("Saved. Scoring is still running; "); const link = document.createElement("a"); link.href = "/settings/pulls"; link.textContent = "follow it under Pulls"; status.append(link, "."); } document.addEventListener("DOMContentLoaded", async () => { let agents; try { agents = await api("/api/agents"); } catch (err) { return; } agent = pick(agents); fill(agents.filter((a) => a.enabled && (!agent || a.id !== agent.id)).length); $("#ranking-form").addEventListener("submit", save); }); })();