Files
jobboard/app/static/app.js
T
WolfandClaude Opus 5.5 59a301c351 Wire the app like the boilerplate: door, queue, schedules, eight tabs
app/main.py assembles it in the boilerplate's order, auth.connect first,
then the queue with the scheduler and the pages. TABS is Roles,
Applications, Agents, Companies, Jobs, Schedules, Messages and Account:
the alpha page's three views become templates on the boilerplate's
base.html, driven by static/roles.js (the old app.js) through api() and
toasts.js, over the unchanged JSON routes. Companies lists every board with
its open roles and the affinity control. A 422 is one sentence, so a toast
never reads [object Object].

Config takes the boilerplate's shape: APP_CONFIG or config.yaml, a missing
file is the defaults, an unknown key is an error. roles.yaml is gone;
config.yaml is the committed default and carries the starter agents.

pull is now a job that asks for the pull cascade, because a schedule
enqueues a job kind; its default schedule, every 6h, is declared beside it
in app/jobs/cascade.py. pull_batch's targets are keyword-only so the Jobs
tab offers no batch button. `app worker` is the boilerplate's; `app job`
still drains a queue of its own and fires no schedules.

test_coupling holds pipeline, auth and desk apart (desk may import
pipeline) and plugins to desk; test_architecture keeps app's layers, now
with main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 12:48:12 +02:00

74 lines
2.8 KiB
JavaScript

/* The one way this app talks to its own API.
*
* const kinds = await api("/api/jobs/kinds");
* await api("/api/jobs", { method: "POST", body: { kind: "tick" } });
*
* Every button on every tab goes through `api`, so a failure is always one
* toast carrying the server's own sentence. That is the other half of the
* bargain the exception handler in `app/main.py` makes: it promises every
* error body is `{"detail": "<one readable sentence>"}`, and this is what
* cashes the promise. Wording a failure in the browser as well would be two
* descriptions of one thing, and the client's would be the one that was wrong.
*
* `body` may be a plain object; it is JSON-encoded with the header set, since
* forgetting the header is the failure that looks like a server bug.
*/
(function () {
"use strict";
async function api(path, options) {
const o = Object.assign({}, options);
const headers = Object.assign({ Accept: "application/json" }, o.headers);
if (o.body !== undefined && typeof o.body !== "string") {
headers["Content-Type"] = "application/json";
o.body = JSON.stringify(o.body);
}
o.headers = headers;
let response;
try {
response = await fetch(path, o);
} catch (e) {
// The network, not the server: there is no `detail` to read.
window.toast.error("Could not reach the server. Is it still running?");
throw e;
}
const text = await response.text();
let data = null;
if (text) {
try { data = JSON.parse(text); } catch (e) { data = null; }
}
if (!response.ok) {
const detail = (data && data.detail)
|| `The server answered ${response.status} and said nothing about why.`;
window.toast.error(String(detail));
const error = new Error(detail);
error.status = response.status;
error.detail = detail;
throw error;
}
return data;
}
/* `window.esc` is toasts.js's — it loads first and had to have one anyway,
and two copies of an escaper is one of them being fixed. See base.html
for the load order that makes this safe. */
/* The queue's own elements do their own fetching, so their failures never
pass through `api()`. They raise an event carrying the server's `detail`
instead, and this is the same bargain kept one level out: one toast, the
server's sentence, nothing reworded here.
`desk-jobs:notice` is a notice arriving on the element's socket, which
live.js already delivers as `app:notice` — so it is deliberately not
listened for. Two toasts for one failure is the bug that listening would
buy. */
document.addEventListener("desk-schedules:error", (e) => {
const detail = (e.detail && e.detail.detail) || "That change was refused.";
window.toast.error(String(detail));
});
window.api = api;
})();