The same dashboard is served by the daemon, which holds every runtime handle, and by the UI process, which holds two remote contracts. A section with nothing behind it still answers -- an empty list, a "disabled" marker, a 501 -- and the browser cannot tell that from a deployment where nothing has happened yet. GET /api/capabilities says which sections this process can back, and the nav drops the rest rather than offering a page that is permanently empty. It fails open in both directions: an unreported section stays visible, and a failed read hides nothing, because an entry that silently vanished is worse than a page that explains itself. Mappings and bindings move the other way. Both are ratified State Store contracts carried by the client the UI process already holds, so leaving them nil degraded two pages that have an owner; compose wires them. A table test now enumerates every route whose capability is daemon-local and pins what it answers without one (requirement 4 of archie-core-8cda.5.4): task logs, the log feed and its stream, version and the update flow, channels and reload, curators, memory, skills, workflows and work requests. Adding a route with no owner now fails that test rather than the operator's browser.
313 lines
13 KiB
React
313 lines
13 KiB
React
// Single place that knows how to talk to archied. Every feature folder goes
|
|
// through this, so auth handling and error shape live in one file.
|
|
import { randomUUID } from "./uuid.jsx";
|
|
|
|
async function req(path) {
|
|
// A daemon that accepts the connection but never answers would otherwise
|
|
// leave the UI stuck in a loading state with no way back; the retry
|
|
// affordances rely on fetch settling, so bound it.
|
|
const res = await fetch(path, {
|
|
headers: { Accept: "application/json" },
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (res.status === 401) {
|
|
throw new ApiError("unauthorised", 401);
|
|
}
|
|
if (!res.ok) {
|
|
throw new ApiError(`${res.status} ${res.statusText}`, res.status);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
// qs builds a query string, omitting empty values so the server sees an
|
|
// absent filter rather than an empty one.
|
|
function qs(params) {
|
|
const q = new URLSearchParams();
|
|
for (const [k, v] of Object.entries(params || {})) {
|
|
if (v !== "" && v != null) q.set(k, v);
|
|
}
|
|
const s = q.toString();
|
|
return s ? `?${s}` : "";
|
|
}
|
|
|
|
// errorMessage prefers what the server actually said. archied answers these
|
|
// with http.Error, i.e. a plain-text reason -- "task is not awaiting
|
|
// approval", "max retries reached (3/3)" -- and discarding it in favour of
|
|
// "409 Conflict" throws away the only part an operator can act on.
|
|
async function errorMessage(res) {
|
|
try {
|
|
const body = (await res.text()).trim();
|
|
if (body) return body.split("\n")[0].slice(0, 200);
|
|
} catch {
|
|
// Body already consumed or unreadable; fall through to the status.
|
|
}
|
|
return `${res.status} ${res.statusText}`;
|
|
}
|
|
|
|
export class ApiError extends Error {
|
|
constructor(message, status) {
|
|
super(message);
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
// classifyActionError turns a failed operator action into a rendering
|
|
// decision. archied's own handlers already return distinguishable status
|
|
// codes for a refused mutation (400/403/409 -- bad input, missing CSRF,
|
|
// cross-origin, conflicting state) versus a broken one (5xx, or no status at
|
|
// all when fetch itself failed -- network drop, timeout); 401 additionally
|
|
// means the session -- archied's own token cookie, or an upstream
|
|
// forward-auth proxy sitting in front of it -- has expired rather than that
|
|
// the action was rejected or the daemon is unwell.
|
|
export function classifyActionError(err) {
|
|
const status = err instanceof ApiError ? err.status : undefined;
|
|
const message = err?.message || "Action failed";
|
|
if (status === 401) return { kind: "session-expired", message };
|
|
if (status >= 400 && status < 500) return { kind: "refused", message };
|
|
return { kind: "broken", message };
|
|
}
|
|
|
|
export const api = {
|
|
summary: () => req("/api/summary"),
|
|
tasks: () => req("/api/tasks"),
|
|
taskMeta: () => req("/api/task-meta"),
|
|
task: (id) => req(`/api/tasks/${id}`),
|
|
taskAction: async (id, action) => {
|
|
const res = await fetch(`/api/tasks/${id}/action`, {
|
|
method: "POST",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify({ action }),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
setup: () => req("/api/setup"),
|
|
capabilities: () => req("/api/capabilities"),
|
|
workflows: () => req("/api/workflows"),
|
|
workRequest: async (request) => {
|
|
const res = await fetch("/api/work-requests", {
|
|
method: "POST",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify(request), signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
skills: () => req("/api/skills"),
|
|
channels: () => req("/api/channels"),
|
|
curators: () => req("/api/curators"),
|
|
channelReload: async (id) => {
|
|
const res = await fetch(`/api/channels/${encodeURIComponent(id)}/reload`, {
|
|
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: "{}", signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
config: () => req("/api/config"),
|
|
version: () => req("/api/version"),
|
|
configUpdate: async (updates) => {
|
|
const res = await fetch("/api/config", {
|
|
method: "PATCH",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify({ updates }),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
configRepoUpdate: async (owner, name, field, value) => {
|
|
const res = await fetch(`/api/config/repos/${encodeURIComponent(owner)}/${encodeURIComponent(name)}`, {
|
|
method: "PATCH",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify({ field, value }),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
configReset: async (key) => {
|
|
const res = await fetch("/api/config/reset", {
|
|
method: "POST",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify({ key }),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
logs: (params) => req("/api/logs" + qs(params)),
|
|
taskLogs: (id, params) => req(`/api/tasks/${id}/logs` + qs(params)),
|
|
captures: (limit) => req("/api/captures" + qs({ limit })),
|
|
mappings: () => req("/api/mappings"),
|
|
mappingCreate: async (mapping) => {
|
|
const res = await fetch("/api/mappings", {
|
|
method: "POST",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify(mapping),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
mappingUpdate: async (id, mapping) => {
|
|
const res = await fetch(`/api/mappings/${id}`, {
|
|
method: "PATCH",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify(mapping),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
mappingDelete: async (id) => {
|
|
const res = await fetch(`/api/mappings/${id}`, {
|
|
method: "DELETE",
|
|
headers: { Accept: "application/json", "X-Archie-CSRF": "1" },
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
},
|
|
mappingPreview: async (captureId, fields) => {
|
|
const res = await fetch("/api/mappings/preview", {
|
|
method: "POST",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify({ capture_id: captureId, fields }),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
bindings: () => req("/api/bindings"),
|
|
bindingCreate: async (binding) => {
|
|
const res = await fetch("/api/bindings", {
|
|
method: "POST",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify(binding),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
bindingUpdate: async (id, binding) => {
|
|
const res = await fetch(`/api/bindings/${id}`, {
|
|
method: "PATCH",
|
|
headers: { Accept: "application/json", "Content-Type": "application/json", "X-Archie-CSRF": "1" },
|
|
body: JSON.stringify(binding),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
bindingDelete: async (id) => {
|
|
const res = await fetch(`/api/bindings/${id}`, {
|
|
method: "DELETE",
|
|
headers: { Accept: "application/json", "X-Archie-CSRF": "1" },
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
},
|
|
bindingApprove: async (id) => {
|
|
const res = await fetch(`/api/bindings/${id}/approve`, {
|
|
method: "POST",
|
|
headers: { Accept: "application/json", "X-Archie-CSRF": "1" },
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
memory: () => req("/api/memory"),
|
|
chatSessions: () => req("/api/chat/sessions"),
|
|
chatMessages: (id) => req(`/api/chat/sessions/${encodeURIComponent(id)}/messages`),
|
|
chatTurns: (id) => req(`/api/chat/sessions/${encodeURIComponent(id)}/turns`),
|
|
chatCancel: async (sessionID) => {
|
|
const res = await fetch("/api/chat/cancel", {
|
|
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
body: JSON.stringify({ session_id: sessionID }), signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
chatMessage: async (channelID, text) => {
|
|
const res = await fetch("/api/chat/message", {
|
|
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
body: JSON.stringify({ channel_id: channelID, source_id: randomUUID(), text, page: location.hash.slice(1) || "/" }),
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
chatPersona: async (sessionID, name) => {
|
|
const res = await fetch("/api/chat/persona", {
|
|
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
body: JSON.stringify({ session_id: sessionID, name }), signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
chatUpdate: () => req("/api/chat/update"),
|
|
chatUpdateDefer: async (snapshot) => {
|
|
const res = await fetch("/api/chat/update/defer", {
|
|
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
body: JSON.stringify({ snapshot }), signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
chatUpdateInstall: async (snapshot) => {
|
|
const res = await fetch("/api/chat/update/install", {
|
|
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
body: JSON.stringify({ snapshot }), signal: AbortSignal.timeout(120000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
chatDangerous: () => req("/api/chat/dangerous"),
|
|
chatDangerousRequest: async (kind, spec) => {
|
|
const res = await fetch(`/api/chat/dangerous/${encodeURIComponent(kind)}`, {
|
|
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
body: JSON.stringify({ spec }), signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
chatDangerousDecision: async (id, decision) => {
|
|
const res = await fetch(`/api/chat/dangerous/${encodeURIComponent(id)}/decision`, {
|
|
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
|
body: JSON.stringify({ decision }), signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return res.json();
|
|
},
|
|
};
|
|
|
|
/**
|
|
* Subscribe to archied's event stream. Returns an unsubscribe function.
|
|
* Reconnection is the browser's job via EventSource, but a closed stream is
|
|
* surfaced to the caller so the UI can show it rather than silently freezing.
|
|
*/
|
|
export function subscribeEvents(onEvent, onStateChange) {
|
|
const src = new EventSource("/events");
|
|
src.onopen = () => onStateChange?.("live");
|
|
src.onerror = () => onStateChange?.("reconnecting");
|
|
src.onmessage = (e) => {
|
|
try {
|
|
onEvent(JSON.parse(e.data));
|
|
} catch {
|
|
/* a malformed frame should not kill the stream */
|
|
}
|
|
};
|
|
return () => src.close();
|
|
}
|
|
|
|
export function subscribeLogs(onEntry, onStateChange) {
|
|
const src = new EventSource("/api/logs/stream");
|
|
src.onopen = () => onStateChange?.("live");
|
|
src.onerror = () => onStateChange?.("reconnecting");
|
|
src.onmessage = (event) => {
|
|
try { onEntry(JSON.parse(event.data)); } catch { /* malformed log frame */ }
|
|
};
|
|
return () => src.close();
|
|
}
|