Score roles per search agent, and run pull into extract, embed and score
embed stores one vector per open role; score rates every open role for every enabled agent from require and prefer gates, fit against the agent's intent, recency and company affinity, keeping gated rows with a reason. Schema v3 adds vectors and the cached intent vector. The page gets an agent order, the score on each row, and an Agents tab. Two starter agents in roles.yaml are imported once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4dfb8831aa
commit
bc60324fab
22 files changed
+1457
-44
No files matched your search
+54
-7
@@ -1,4 +1,4 @@
|
||||
"""The JSON routes the page uses: roles, companies, applications.
|
||||
"""The JSON routes the page uses: roles, companies, applications, agents.
|
||||
|
||||
`/api/jobs*`, `/_jobs/jobs.js` and `/ws/jobs` are `pipeline.web`'s, mounted by
|
||||
`connect` in `app.py`. This is roledesk's own API, over the same store.
|
||||
@@ -9,10 +9,12 @@ from __future__ import annotations
|
||||
from datetime import timedelta
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
from fastapi import APIRouter, HTTPException, Query, Response
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from roledesk.models import AFFINITY, Application, Company, Role, Status
|
||||
from roledesk.models import (
|
||||
AFFINITY, Agent, Application, Company, Gate, ScoredRole, Status, Weights,
|
||||
)
|
||||
from roledesk.web.deps import StoreDep
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["roledesk"])
|
||||
@@ -30,14 +32,21 @@ def list_roles(
|
||||
remote: str = "",
|
||||
days: int | None = None,
|
||||
show_muted: bool = False,
|
||||
agent: int | None = None,
|
||||
gated: bool = False,
|
||||
limit: Annotated[int, Query(ge=1, le=1000)] = 200,
|
||||
) -> list[Role]:
|
||||
) -> list[ScoredRole]:
|
||||
"""Open roles, newest first. See `Store.roles` for what each filter does.
|
||||
|
||||
A muted company's roles are left out unless `show_muted` is set."""
|
||||
A muted company's roles are left out unless `show_muted` is set. With
|
||||
`agent`, ordered by that agent's score and carrying `score` and
|
||||
`gated_reason`; a gated role only with `gated=1`."""
|
||||
if agent is not None and store.agent(agent) is None:
|
||||
raise HTTPException(404, f"no such agent: {agent}")
|
||||
return store.roles(limit=limit, q=q or None, company=company or None,
|
||||
place=place or None, function=function or None,
|
||||
remote=remote or None, days=days, hide_muted=not show_muted)
|
||||
remote=remote or None, days=days, hide_muted=not show_muted,
|
||||
agent=agent, gated=gated)
|
||||
|
||||
|
||||
# --------------------------------------------------------------- companies
|
||||
@@ -122,3 +131,41 @@ def set_status(app_id: str, body: StatusBody, store: StoreDep) -> Application:
|
||||
raise HTTPException(422, str(exc)) from exc
|
||||
except KeyError:
|
||||
raise HTTPException(404, f"no such application: {app_id}") from None
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ agents
|
||||
|
||||
@router.get("/agents")
|
||||
def list_agents(store: StoreDep) -> list[Agent]:
|
||||
return store.agents()
|
||||
|
||||
|
||||
class AgentBody(BaseModel):
|
||||
"""Create when `id` is missing, replace that agent's fields when given."""
|
||||
|
||||
id: int | None = None
|
||||
name: str = Field(min_length=1)
|
||||
intent: str = ""
|
||||
gates: list[Gate] = []
|
||||
weights: Weights = Weights()
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
@router.post("/agents")
|
||||
def save_agent(body: AgentBody, store: StoreDep) -> Agent:
|
||||
"""Scores are not recomputed here: that is the `score` task, which the
|
||||
page starts after a save."""
|
||||
try:
|
||||
return store.save_agent(body.name, body.intent, body.gates, body.weights,
|
||||
body.enabled, agent_id=body.id)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(409, str(exc)) from exc
|
||||
except KeyError:
|
||||
raise HTTPException(404, f"no such agent: {body.id}") from None
|
||||
|
||||
|
||||
@router.delete("/agents/{agent_id}", status_code=204)
|
||||
def delete_agent(agent_id: int, store: StoreDep) -> Response:
|
||||
if not store.delete_agent(agent_id):
|
||||
raise HTTPException(404, f"no such agent: {agent_id}")
|
||||
return Response(status_code=204)
|
||||
@@ -24,6 +24,7 @@ def create_app(config_path: str | None = None) -> FastAPI:
|
||||
# `Store` first, so the file is at the current schema before the queue
|
||||
# adds its own table to it.
|
||||
store = Store(config.database)
|
||||
jobs.seed_agents(config, store)
|
||||
app = FastAPI(title="roledesk", docs_url="/api/docs")
|
||||
connect(app, Jobs(config.database), config, store,
|
||||
workers=config.workers.model_dump(),
|
||||
|
||||
@@ -159,6 +159,40 @@ button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible
|
||||
}
|
||||
|
||||
.role.muted { opacity: .55; }
|
||||
.role.gated { opacity: .7; }
|
||||
.show-muted[hidden] { display: none; }
|
||||
.role-score.badge:empty { display: none; }
|
||||
.role-score.badge { color: var(--signal); border-color: var(--signal-weak); font-variant-numeric: tabular-nums; }
|
||||
.role-gated { color: var(--mute); font-size: .85rem; }
|
||||
|
||||
/* ---------------------------------------------------------------- agents */
|
||||
|
||||
.agents { list-style: none; margin: 0 0 1rem; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
|
||||
.agent {
|
||||
display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
|
||||
background: var(--panel); border: 1px solid var(--rule); border-radius: 6px; padding: .5rem .75rem;
|
||||
}
|
||||
.agent-meta { color: var(--faint); font-size: .85rem; flex: 1 1 12rem; }
|
||||
.agent-form {
|
||||
display: flex; flex-direction: column; gap: .6rem; max-width: 44rem;
|
||||
background: var(--panel); border: 1px solid var(--rule); border-radius: 6px; padding: .75rem 1rem;
|
||||
}
|
||||
.agent-form h2 { font-size: 1rem; margin: 0; }
|
||||
.agent-form label { display: flex; flex-direction: column; gap: .2rem; font-size: .9rem; }
|
||||
.agent-form label.inline { flex-direction: row; align-items: center; gap: .4rem; }
|
||||
.agent-form input[type="text"], .agent-form textarea, .agent-form input[type="number"] {
|
||||
font: inherit; color: inherit; background: var(--bg); border: 1px solid var(--rule);
|
||||
border-radius: 4px; padding: .35rem .5rem; width: 100%;
|
||||
}
|
||||
.agent-form textarea { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .85rem; }
|
||||
.agent-form .hint { color: var(--faint); font-size: .8rem; overflow-wrap: anywhere; }
|
||||
.weights { display: flex; gap: .75rem; flex-wrap: wrap; border: 1px solid var(--rule); border-radius: 4px; }
|
||||
.weights label { flex: 1 1 6rem; }
|
||||
.agent-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
|
||||
.agent-actions button, .agent button {
|
||||
background: var(--bg); border: 1px solid var(--rule); border-radius: 4px; padding: .3rem .8rem; cursor: pointer;
|
||||
}
|
||||
.agent-actions button.primary { background: var(--signal); border-color: var(--signal); color: #fff; }
|
||||
|
||||
/* ------------------------------------------------------------ applications */
|
||||
|
||||
|
||||
+161
-3
@@ -1,5 +1,5 @@
|
||||
// roledesk's page: the filter bar, the role list, the tracker, and the tab
|
||||
// switch between them. The template renders an empty shell (see index.html);
|
||||
// roledesk's page: the filter bar, the role list, the tracker, the search
|
||||
// agents, and the tab switch between them. The template renders an empty shell (see index.html);
|
||||
// everything on screen comes from the API, and every action — Save, an
|
||||
// affinity click, a status change — is a fetch from here, not a form post.
|
||||
"use strict";
|
||||
@@ -12,6 +12,8 @@ let companies = new Map();
|
||||
let trackedByRole = new Map();
|
||||
/** @type {object[]} the last /api/roles response, already muted-filtered by the server */
|
||||
let roles = [];
|
||||
/** @type {object[]} every search agent, from /api/agents */
|
||||
let agents = [];
|
||||
|
||||
const $ = (sel, root = document) => root.querySelector(sel);
|
||||
const $$ = (sel, root = document) => Array.from(root.querySelectorAll(sel));
|
||||
@@ -42,7 +44,9 @@ function initTabs() {
|
||||
const name = tab.dataset.tab;
|
||||
$("#roles-view").hidden = name !== "roles";
|
||||
$("#applications-view").hidden = name !== "applications";
|
||||
$("#agents-view").hidden = name !== "agents";
|
||||
if (name === "applications") loadApplications();
|
||||
if (name === "agents") loadAgents();
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -64,6 +68,11 @@ function filterParams() {
|
||||
if (remote) params.set("remote", remote);
|
||||
if (days) params.set("days", days);
|
||||
if ($("#f-show-muted").checked) params.set("show_muted", "true");
|
||||
const agent = $("#f-agent").value;
|
||||
if (agent) {
|
||||
params.set("agent", agent);
|
||||
if ($("#f-gated").checked) params.set("gated", "1");
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
@@ -104,6 +113,13 @@ function renderRoles() {
|
||||
node.querySelector(".role-remote").textContent = role.remote || "";
|
||||
node.querySelector(".role-function").textContent = role.function || "";
|
||||
node.querySelector(".role-age").textContent = relativeAge(role.first_seen);
|
||||
if (role.score !== null && role.score !== undefined) {
|
||||
const badge = node.querySelector(".role-score");
|
||||
badge.textContent = role.gated_reason ? "gated" : role.score.toFixed(2);
|
||||
badge.title = "Score for the selected agent";
|
||||
node.querySelector(".role-gated").textContent = role.gated_reason || "";
|
||||
if (role.gated_reason) li.classList.add("gated");
|
||||
}
|
||||
|
||||
node.querySelector(".affinity").replaceWith(
|
||||
affinityControl(role.company_key, company ? company.affinity : 0));
|
||||
@@ -260,6 +276,140 @@ async function setStatus(id, status) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- agents
|
||||
|
||||
async function loadAgents() {
|
||||
agents = await api("/api/agents");
|
||||
renderAgentSelect();
|
||||
renderAgents();
|
||||
}
|
||||
|
||||
// The filter bar's order: "Newest", or one agent's score. Keeps the current
|
||||
// choice when the list is refreshed, unless that agent is gone.
|
||||
function renderAgentSelect() {
|
||||
const select = $("#f-agent");
|
||||
const current = select.value;
|
||||
select.replaceChildren(new Option("Newest", ""));
|
||||
for (const agent of agents) {
|
||||
const label = agent.enabled ? agent.name : `${agent.name} (off)`;
|
||||
select.appendChild(new Option(label, String(agent.id)));
|
||||
}
|
||||
select.value = agents.some((a) => String(a.id) === current) ? current : "";
|
||||
$("#show-gated").hidden = !select.value;
|
||||
}
|
||||
|
||||
function renderAgents() {
|
||||
const list = $("#agents-list");
|
||||
list.replaceChildren();
|
||||
for (const agent of agents) {
|
||||
const li = document.createElement("li");
|
||||
li.className = "agent";
|
||||
const name = document.createElement("strong");
|
||||
name.textContent = agent.name;
|
||||
const meta = document.createElement("span");
|
||||
meta.className = "agent-meta";
|
||||
const gates = agent.gates.map((g) => `${g.mode} ${g.field}`).join(", ") || "no gates";
|
||||
meta.textContent = `${agent.enabled ? "" : "off · "}${gates}`;
|
||||
const edit = document.createElement("button");
|
||||
edit.type = "button";
|
||||
edit.textContent = "Edit";
|
||||
edit.addEventListener("click", () => fillAgentForm(agent));
|
||||
li.append(name, meta, edit);
|
||||
list.appendChild(li);
|
||||
}
|
||||
$("#agents-count").textContent = `${agents.length} agent${agents.length === 1 ? "" : "s"}`;
|
||||
}
|
||||
|
||||
function fillAgentForm(agent) {
|
||||
$("#agent-form-title").textContent = agent ? `Edit ${agent.name}` : "New agent";
|
||||
$("#a-id").value = agent ? String(agent.id) : "";
|
||||
$("#a-name").value = agent ? agent.name : "";
|
||||
$("#a-intent").value = agent ? agent.intent : "";
|
||||
$("#a-gates").value = JSON.stringify(agent ? agent.gates : [], null, 2);
|
||||
const weights = agent ? agent.weights : { fit: 0.6, recency: 0.2, affinity: 0.2 };
|
||||
$("#a-fit").value = weights.fit;
|
||||
$("#a-recency").value = weights.recency;
|
||||
$("#a-affinity").value = weights.affinity;
|
||||
$("#a-enabled").checked = agent ? agent.enabled : true;
|
||||
$("#a-delete").hidden = !agent;
|
||||
$("#agent-status").textContent = "";
|
||||
}
|
||||
|
||||
async function saveAgent(event) {
|
||||
event.preventDefault();
|
||||
const status = $("#agent-status");
|
||||
let gates;
|
||||
try {
|
||||
gates = JSON.parse($("#a-gates").value || "[]");
|
||||
} catch (err) {
|
||||
status.textContent = `Gates are not valid JSON: ${err.message}`;
|
||||
return;
|
||||
}
|
||||
const id = $("#a-id").value;
|
||||
const body = {
|
||||
name: $("#a-name").value.trim(),
|
||||
intent: $("#a-intent").value,
|
||||
gates,
|
||||
weights: {
|
||||
fit: Number($("#a-fit").value),
|
||||
recency: Number($("#a-recency").value),
|
||||
affinity: Number($("#a-affinity").value),
|
||||
},
|
||||
enabled: $("#a-enabled").checked,
|
||||
};
|
||||
if (id) body.id = Number(id);
|
||||
try {
|
||||
const saved = await api("/api/agents", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
await loadAgents();
|
||||
fillAgentForm(saved);
|
||||
await rescore(status);
|
||||
} catch (err) {
|
||||
status.textContent = err.message;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteAgent() {
|
||||
const id = $("#a-id").value;
|
||||
if (!id || !confirm("Delete this agent and its scores?")) return;
|
||||
try {
|
||||
await api(`/api/agents/${encodeURIComponent(id)}`, { method: "DELETE" });
|
||||
fillAgentForm(null);
|
||||
await loadAgents();
|
||||
await loadRoles();
|
||||
} catch (err) {
|
||||
$("#agent-status").textContent = err.message;
|
||||
}
|
||||
}
|
||||
|
||||
// Start the `score` job and refetch the roles once it has finished, so the
|
||||
// order on the Roles tab is the saved agent's.
|
||||
async function rescore(status) {
|
||||
status.textContent = "Saved. Scoring…";
|
||||
const job = await api("/api/jobs", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ kind: "score", payload: {} }),
|
||||
});
|
||||
for (let i = 0; i < 300; i += 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
const snapshot = await api("/api/jobs");
|
||||
const busy = [...snapshot.running, ...snapshot.queued].some((j) => j.id === job.id);
|
||||
if (!busy) {
|
||||
const done = snapshot.recent.find((j) => j.id === job.id);
|
||||
status.textContent = done && done.state !== "done"
|
||||
? `Scoring ${done.state}: ${done.error || ""}`
|
||||
: "Saved and scored.";
|
||||
await loadRoles();
|
||||
return;
|
||||
}
|
||||
}
|
||||
status.textContent = "Saved. Scoring is still running; see Jobs below.";
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- init
|
||||
|
||||
async function loadCompanies() {
|
||||
@@ -269,7 +419,7 @@ async function loadCompanies() {
|
||||
|
||||
async function init() {
|
||||
initTabs();
|
||||
await Promise.all([loadCompanies(), loadApplications()]);
|
||||
await Promise.all([loadCompanies(), loadApplications(), loadAgents()]);
|
||||
await loadRoles();
|
||||
|
||||
const refetch = debounce(loadRoles, 300);
|
||||
@@ -280,6 +430,14 @@ async function init() {
|
||||
$("#f-remote").addEventListener("change", loadRoles);
|
||||
$("#f-days").addEventListener("change", loadRoles);
|
||||
$("#f-show-muted").addEventListener("change", loadRoles);
|
||||
$("#f-agent").addEventListener("change", () => {
|
||||
$("#show-gated").hidden = !$("#f-agent").value;
|
||||
loadRoles();
|
||||
});
|
||||
$("#f-gated").addEventListener("change", loadRoles);
|
||||
$("#agent-form").addEventListener("submit", saveAgent);
|
||||
$("#a-new").addEventListener("click", () => fillAgentForm(null));
|
||||
$("#a-delete").addEventListener("click", deleteAgent);
|
||||
$("#filters").addEventListener("submit", (e) => e.preventDefault());
|
||||
}
|
||||
|
||||
|
||||
@@ -13,11 +13,15 @@
|
||||
<nav class="tabs" id="tabs">
|
||||
<button type="button" class="tab" data-tab="roles" aria-pressed="true">Roles</button>
|
||||
<button type="button" class="tab" data-tab="applications" aria-pressed="false">Applications</button>
|
||||
<button type="button" class="tab" data-tab="agents" aria-pressed="false">Agents</button>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<section id="roles-view" class="view">
|
||||
<form id="filters" class="filters" autocomplete="off">
|
||||
<select id="f-agent" aria-label="Order">
|
||||
<option value="">Newest</option>
|
||||
</select>
|
||||
<input type="search" id="f-q" placeholder="Search titles" aria-label="Search titles">
|
||||
<input type="text" id="f-company" placeholder="Company" aria-label="Company">
|
||||
<input type="text" id="f-place" placeholder="Place" aria-label="Place">
|
||||
@@ -43,6 +47,9 @@
|
||||
<label class="show-muted">
|
||||
<input type="checkbox" id="f-show-muted"> Show muted
|
||||
</label>
|
||||
<label class="show-muted" id="show-gated" hidden>
|
||||
<input type="checkbox" id="f-gated"> Show gated
|
||||
</label>
|
||||
</form>
|
||||
|
||||
<p id="roles-count" class="count" role="status"></p>
|
||||
@@ -59,6 +66,33 @@
|
||||
</table>
|
||||
</section>
|
||||
|
||||
<section id="agents-view" class="view" hidden>
|
||||
<p id="agents-count" class="count" role="status"></p>
|
||||
<ul id="agents-list" class="agents"></ul>
|
||||
<form id="agent-form" class="agent-form" autocomplete="off">
|
||||
<h2 id="agent-form-title">New agent</h2>
|
||||
<input type="hidden" id="a-id">
|
||||
<label>Name <input type="text" id="a-name" required></label>
|
||||
<label>Intent <span class="hint">a CV paragraph, or what this search is for</span>
|
||||
<textarea id="a-intent" rows="5"></textarea></label>
|
||||
<label>Gates <span class="hint">JSON: [{"field": "place", "values": ["Berlin"], "mode": "require"}, {"field": "remote", "values": ["remote"], "mode": "prefer", "weight": 0.2}]</span>
|
||||
<textarea id="a-gates" rows="5" spellcheck="false">[]</textarea></label>
|
||||
<fieldset class="weights">
|
||||
<legend>Weights</legend>
|
||||
<label>Fit <input type="number" id="a-fit" min="0" step="0.05" value="0.6"></label>
|
||||
<label>Recency <input type="number" id="a-recency" min="0" step="0.05" value="0.2"></label>
|
||||
<label>Affinity <input type="number" id="a-affinity" min="0" step="0.05" value="0.2"></label>
|
||||
</fieldset>
|
||||
<label class="inline"><input type="checkbox" id="a-enabled" checked> Enabled</label>
|
||||
<div class="agent-actions">
|
||||
<button type="submit" class="primary">Save and score</button>
|
||||
<button type="button" id="a-new">New</button>
|
||||
<button type="button" id="a-delete" hidden>Delete</button>
|
||||
</div>
|
||||
<p id="agent-status" class="count" role="status"></p>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section id="jobs">
|
||||
<h2>Jobs</h2>
|
||||
<script type="module" src="/_jobs/jobs.js"></script>
|
||||
@@ -76,6 +110,8 @@
|
||||
<span class="role-place"></span>
|
||||
<span class="role-remote badge"></span>
|
||||
<span class="role-function badge"></span>
|
||||
<span class="role-score badge"></span>
|
||||
<span class="role-gated"></span>
|
||||
<span class="role-age"></span>
|
||||
</div>
|
||||
<button type="button" class="save"></button>
|
||||
|
||||
Reference in new issue
Block a user