The top bar is Roles and Applications plus health's cog. Behind the cog,
/settings/{ranking,searches,companies,pulls,messages,account} share one row
of sub-tabs, an underlined row on a desktop and pills on a phone. The old
paths answer 301 with their query string.
Ranking edits the first enabled agent, or creates one, with the intent,
the weights and the gates under Advanced. The order menu on Roles reads
Newest and Best match with one enabled agent, and names them with several.
config.yaml keeps the Copenhagen starter; the other two are commented
examples in config.example.yaml.
roles.js is split into roles.js, applications.js, ranking.js and
companies.js; count, cell, debounce, the company list and the affinity
control live once in app.js. app.css gets the phone frame after health and
newsdesk: one-row bar, edge-to-edge panels, 44px targets and 16px fields on
a coarse pointer, hover only where there is one, safe areas. The app is
installable from a manifest and roledesk-coloured icons. Every lede touched
is written for the person using the app.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
135 lines
4.5 KiB
JavaScript
135 lines
4.5 KiB
JavaScript
/* 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);
|
|
});
|
|
})();
|