Merge the remote pages: scope line, Remote select, remote chips and badge (wp-remote-b)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	app/static/agents.js
This commit is contained in:
Wolf committed 2026-09-30 16:22:22 +02:00
commit 5c3c187dca
7 files changed
+383 -86

No files matched your search

+101 -75
View File
@@ -24,7 +24,7 @@
(function () {
"use strict";
const { api, count } = window;
const { api, count, remoteHelpers: rh } = window;
const $ = (sel, root = document) => root.querySelector(sel);
const DEFAULT_WEIGHTS = { fit: 0.6, recency: 0.2, affinity: 0.2, taste: 0.2 };
@@ -33,12 +33,9 @@
// 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";
// Europe as the starter agents in config.example.yaml spell it: the EEA,
// the UK and Switzerland, and EU for a role open anywhere in Europe.
const EUROPE = ["AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR",
"HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE",
"IS", "LI", "NO", "GB", "CH", "EU"];
const FIELDS = ["place", "country", "remote", "seniority", "function", "salary_min",
// Europe as the starter agents spell it (static/remote.js).
const EUROPE = rh.EUROPE;
const FIELDS = ["place", "country", "remote", "remote_from", "seniority", "function", "salary_min",
"currency", "permit", "languages", "company", "title"];
const MODES = [["require", "must"], ["any", "one of"], ["prefer", "prefer"], ["exclude", "not"]];
const HYBRID_WEIGHT = 0.05;
@@ -107,11 +104,33 @@
}
// The countries Where offers, by name: Europe's, without EU, which is not
// a country but "open anywhere in Europe" and is said by Remote.
// a country but "open anywhere in Europe" and is picked in the remote line.
const COUNTRIES = EUROPE.filter((c) => c !== "EU")
.map((c) => [c, countryName(c)])
.sort((a, b) => a[1].localeCompare(b[1]));
/** @type {Map<string, string>} the words of the `remote_from` values, by
* value, as `/api/places/labels` says them; a card and a chip fall back to
* `rh.fallbackLabel` until they are in */
const placeNames = new Map();
const placeLabel = (value) => placeNames.get(value) || rh.fallbackLabel(value, countryName);
// The labels of the `remote_from` tokens in `gates` that are not known yet.
// A failure is toasted by `api()` and leaves the fallbacks.
async function nameRemote(gates) {
const missing = [...new Set(gates.filter((g) => g.field === "remote_from")
.flatMap((g) => g.values.map(String)))]
.filter((v) => v !== rh.ANYWHERE && !placeNames.has(v));
if (!missing.length) return;
const p = new URLSearchParams();
missing.forEach((t) => p.append("tokens", t));
try {
for (const { token, label } of await api(`/api/places/labels?${p}`)) placeNames.set(token, label);
} catch (err) {
// the fallbacks stand
}
}
// "a, b or c": the values of one line.
function orList(items) {
return items.length < 2 ? items.join("") : `${items.slice(0, -1).join(", ")} or ${items[items.length - 1]}`;
@@ -196,33 +215,45 @@
}
if (gate.field === "place") return `in ${orList(towns(values))}`;
if (gate.field === "remote") return orList(values);
if (gate.field === "remote_from") return rh.remoteSentence(values, placeLabel);
if (gate.field === "company") return `at ${orList(values)}`;
if (gate.field === "salary_min") return `paying at least ${values[0]}`;
return `with ${gate.field.replace("_", " ")} ${values.join(", ")}`;
}
// The Where block in words, one sentence of the lines a role may match,
// with the one-of rows among them since they are the same group: "Keeps
// roles anywhere in Denmark, in Malmö, Lund or Helsingborg, or remote within
// Europe, and leaves out the rest." The lines add up, which the first
// wording ("Roles in ...") left the owner to guess.
// The scope of the Where block in words: "Within Europe", "Within Denmark or
// Sweden", "Everywhere".
function scopeWords(scope) {
if (scope.kind === "everywhere") return "Everywhere";
if (scope.kind === "europe") return "Within Europe";
return `Within ${phrase({ field: "country", values: scope.values }).replace(/^in /, "")}`;
}
// The Where block in words, one sentence led by its scope, then the lines a
// role may match, with the one-of rows among them since they are the same
// group: "Within Europe: keeps roles anywhere in Denmark, in Malmö, Lund or
// Helsingborg, or remote, and leaves out the rest." The lines add up, which
// the first wording ("Roles in ...") left the owner to guess.
function whereSentence(where, others) {
const parts = [];
if (where.countries) parts.push(`anywhere ${phrase(where.countries)}`);
if (where.places) parts.push(phrase(where.places));
if (where.remote) parts.push(where.europe ? "remote within Europe" : "remote");
if (where.remote.mode === "scope") parts.push("remote");
if (where.remote.mode === "from" && where.remote.values.length) {
parts.push(rh.remoteSentence(where.remote.values, placeLabel));
}
parts.push(...others.map(phrase));
if (!parts.length) return "";
if (!parts.length) return where.scope.kind === "everywhere" ? "" : `${scopeWords(where.scope)}: keeps every role.`;
const list = parts.length < 2 ? parts[0] : `${parts.slice(0, -1).join(", ")}, or ${parts[parts.length - 1]}`;
return `Keeps roles ${list}, and leaves out the rest.`;
return `${scopeWords(where.scope)}: keeps roles ${list}, and leaves out the rest.`;
}
// The gates in words: where a role may be, then what it must meet, then
// what lifts it. "Keeps roles anywhere in Denmark, or remote within Europe,
// what lifts it. "Within Europe: keeps roles anywhere in Denmark, or remote,
// and leaves out the rest. Only roles with seniority senior. Prefers
// hybrid."
function rulesLine(gates) {
const { where, rest } = distribute(gates);
const { where, rest } = rh.distribute(gates);
const of = (mode) => rest.filter((g) => g.mode === mode);
const must = of("require").map(phrase);
const prefer = of("prefer").map((g) => phrase(g).replace(/^with /, ""));
@@ -382,6 +413,33 @@
return { node: chips.node, values: () => [...chips.list] };
}
// "Only when open to someone in": the values of a `remote_from` gate as
// chips, picked from the place suggestions that a remote hire can live in:
// countries and regions. `values()` are the gate's values (`country:DK`),
// the chips say them in words.
function remoteInput(values, onChange) {
const root = el("div");
root.id = "a-remote-from";
const chips = window.chipInput(root, {
suggest: async (q) => {
const rows = await api(`/api/suggest/places?${new URLSearchParams({ q })}`);
const picks = [];
for (const s of rows) {
const value = rh.fromSuggestion(s);
if (value !== null) picks.push({ value, label: s.label, hint: s.kind });
}
return picks;
},
onChange: (items) => {
items.forEach((i) => placeNames.set(i.value, i.label));
onChange();
},
placeholder: "Add a country or a region", label: "Remote, open to someone in",
});
chips.set(values.map((v) => ({ value: v, label: placeLabel(v) })));
return { node: root, values: () => chips.values().map((i) => i.value) };
}
// Words as chips of free text, added on Enter or a comma, and one chip for
// the spellings that fold alike. What is typed but not added yet counts, so
// Save does not drop it. Towns and fetch terms both.
@@ -493,47 +551,6 @@
return { node: row, read };
}
// Share out an agent's gates between the Where block and Other rules. Each
// Where line takes the first gate it can say whole; the rest are rows. A
// must or one-of gate with a weight other than the default stays a row,
// since Where would drop it. Remote within Europe is a must on country
// that is Europe and the countries line exactly, and only with Remote
// ticked; any other must on country is a row, and Remote reads anywhere.
function distribute(gates) {
const where = { countries: null, places: null, remote: null, europe: null, hybrid: null, titles: null };
const rest = [];
const musts = [];
const plain = (g) => g.weight === undefined || g.weight === 1;
const only = (g, value) => g.values.length === 1 && g.values[0] === value;
for (const g of gates) {
let slot = null;
if (g.field === "country" && g.mode === "require" && plain(g)) {
musts.push(g);
continue;
}
if (g.field === "country" && g.mode === "any" && plain(g)) slot = "countries";
else if (g.field === "remote" && g.mode === "any" && plain(g) && only(g, "remote")) slot = "remote";
else if (g.field === "place" && g.mode === "any" && plain(g)) slot = "places";
else if (g.field === "remote" && g.mode === "prefer" && only(g, "hybrid")) slot = "hybrid";
else if (g.field === "title" && g.mode === "exclude" && plain(g)) slot = "titles";
if (slot && !where[slot]) where[slot] = g;
else rest.push(g);
}
const within = new Set(europeWith(where.countries ? where.countries.values : []));
for (const g of musts) {
const same = g.values.length === within.size && g.values.every((v) => within.has(v));
if (where.remote && !where.europe && same) where.europe = g;
else rest.push(g);
}
return { where, rest };
}
// What Remote within Europe sends: Europe, and the countries line too,
// since the must holds for every role, not only the remote ones.
function europeWith(countries) {
return [...EUROPE, ...countries.filter((c) => !EUROPE.includes(c))];
}
function form(row) {
const div = el("article", "agent-card editing");
const f = $("#agent-form-template").content.querySelector("form").cloneNode(true);
@@ -562,24 +579,29 @@
changed(f);
});
const { where, rest } = distribute(row ? row.gates : []);
const { where, rest } = rh.distribute(row ? row.gates : []);
const scope = $("#a-scope", f);
scope.value = where.scope.kind;
const scoped = countryInput(where.scope.values);
const countries = countryInput(where.countries ? where.countries.values : []);
const places = wordInput(where.places ? where.places.values : [], "Add a town");
const titles = wordInput(where.titles ? where.titles.values : [], "account executive");
const terms = wordInput(row ? row.fetch_terms || [] : [], "machine learning, data science");
$("#a-titles", f).replaceWith(titles.node);
$("#a-terms", f).replaceWith(terms.node);
$("#a-scope-countries", f).replaceWith(scoped.node);
$("#a-countries", f).replaceWith(countries.node);
$("#a-towns", f).replaceWith(places.node);
const remote = $("#a-remote", f);
const scope = $("#a-remote-scope", f);
remote.checked = Boolean(where.remote);
// Anywhere when a must on country is a row, so ticking Remote does not
// stack a second one on it; else Europe, the usual answer.
const mustRow = rest.some((g) => g.field === "country" && g.mode === "require");
scope.value = where.europe || !(where.remote || mustRow) ? "europe" : "anywhere";
const offerScope = () => { scope.disabled = !remote.checked; };
offerScope();
const remoteMode = $("#a-remote", f);
remoteMode.value = where.remote.mode;
const remoteFrom = remoteInput(where.remote.mode === "from" ? where.remote.values : [], () => changed(f));
$("#a-remote-from", f).replaceWith(remoteFrom.node);
// Only the pick that is on shows its chips.
const offer = () => {
scoped.node.hidden = scope.value !== "countries";
remoteFrom.node.hidden = remoteMode.value !== "from";
};
offer();
// The hybrid weight shows only when it is not the default; either way it
// is sent back as it came.
const hybridWeight = where.hybrid && where.hybrid.weight !== undefined ? where.hybrid.weight : HYBRID_WEIGHT;
@@ -607,17 +629,20 @@
});
// The gates the Where block says, in the order the starter agents have
// them: the must on country, the lines, then hybrid; and the titles left
// out, the block under it.
// them: the scope, the lines, then hybrid; and the titles left out, the
// block under it.
function whereGates() {
const out = [];
const within = countries.values();
const towns = places.values();
if (remote.checked && scope.value === "europe") {
out.push({ field: "country", values: europeWith(within), mode: "require" });
const from = remoteFrom.values();
if (scope.value === "europe") out.push({ field: "country", values: rh.europeWith(within), mode: "require" });
if (scope.value === "countries" && scoped.values().length) {
out.push({ field: "country", values: scoped.values(), mode: "require" });
}
if (within.length) out.push({ field: "country", values: within, mode: "any" });
if (remote.checked) out.push({ field: "remote", values: ["remote"], mode: "any" });
if (remoteMode.value === "scope") out.push({ field: "remote", values: ["remote"], mode: "any" });
if (remoteMode.value === "from" && from.length) out.push({ field: "remote_from", values: from, mode: "any" });
if (towns.length) out.push({ field: "place", values: towns, mode: "any" });
if ($("#a-hybrid", f).checked) {
out.push({ field: "remote", values: ["hybrid"], mode: "prefer", weight: Number($("#a-hybrid-weight", f).value) });
@@ -630,7 +655,7 @@
// under Where says them all, as the card will, rows included.
const gates = () => whereGates().concat(rules.map((r) => r.read()));
const view = () => {
offerScope();
offer();
$("#a-gates", f).value = JSON.stringify(gates(), null, 2);
$("#a-where-sum", f).textContent = rulesLine(gates()) || "Keeps roles anywhere.";
};
@@ -804,6 +829,7 @@
async function load() {
agents = await api("/api/agents");
await nameRemote(agents.flatMap((a) => a.gates || []));
// A card is only worded by it, so a failure leaves the cards as they were.
try {
stageOn = Object.fromEntries((await api("/api/stages")).map((s) => [s.name, s.on]));