zeroclaw/web/src/lib/api.ts
Argenis b8ebe7bcd3
feat(gateway): add cron run history API and dashboard panel (#3440)
Add GET /api/cron/{id}/runs?limit=N endpoint that returns recent stored
runs for a cron job, with server-side limit clamping to 1-100 (default 20).

Frontend adds a CronRun type, API client function, and an expandable
run history panel on the Cron page showing status, timestamps, duration,
and output for each run, with loading, empty, error, and refresh states.

Closes #3299

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 17:06:44 -04:00

262 lines
7.9 KiB
TypeScript

import type {
StatusResponse,
ToolSpec,
CronJob,
CronRun,
Integration,
DiagResult,
MemoryEntry,
CostSummary,
CliTool,
HealthSnapshot,
} from '../types/api';
import { clearToken, getToken, setToken } from './auth';
// ---------------------------------------------------------------------------
// Base fetch wrapper
// ---------------------------------------------------------------------------
export class UnauthorizedError extends Error {
constructor() {
super('Unauthorized');
this.name = 'UnauthorizedError';
}
}
export async function apiFetch<T = unknown>(
path: string,
options: RequestInit = {},
): Promise<T> {
const token = getToken();
const headers = new Headers(options.headers);
if (token) {
headers.set('Authorization', `Bearer ${token}`);
}
if (
options.body &&
typeof options.body === 'string' &&
!headers.has('Content-Type')
) {
headers.set('Content-Type', 'application/json');
}
const response = await fetch(path, { ...options, headers });
if (response.status === 401) {
clearToken();
window.dispatchEvent(new Event('zeroclaw-unauthorized'));
throw new UnauthorizedError();
}
if (!response.ok) {
const text = await response.text().catch(() => '');
throw new Error(`API ${response.status}: ${text || response.statusText}`);
}
// Some endpoints may return 204 No Content
if (response.status === 204) {
return undefined as unknown as T;
}
return response.json() as Promise<T>;
}
function unwrapField<T>(value: T | Record<string, T>, key: string): T {
if (value !== null && typeof value === 'object' && !Array.isArray(value) && key in value) {
const unwrapped = (value as Record<string, T | undefined>)[key];
if (unwrapped !== undefined) {
return unwrapped;
}
}
return value as T;
}
// ---------------------------------------------------------------------------
// Pairing
// ---------------------------------------------------------------------------
export async function pair(code: string): Promise<{ token: string }> {
const response = await fetch('/pair', {
method: 'POST',
headers: { 'X-Pairing-Code': code },
});
if (!response.ok) {
const text = await response.text().catch(() => '');
throw new Error(`Pairing failed (${response.status}): ${text || response.statusText}`);
}
const data = (await response.json()) as { token: string };
setToken(data.token);
return data;
}
// ---------------------------------------------------------------------------
// Public health (no auth required)
// ---------------------------------------------------------------------------
export async function getPublicHealth(): Promise<{ require_pairing: boolean; paired: boolean }> {
const response = await fetch('/health');
if (!response.ok) {
throw new Error(`Health check failed (${response.status})`);
}
return response.json() as Promise<{ require_pairing: boolean; paired: boolean }>;
}
// ---------------------------------------------------------------------------
// Status / Health
// ---------------------------------------------------------------------------
export function getStatus(): Promise<StatusResponse> {
return apiFetch<StatusResponse>('/api/status');
}
export function getHealth(): Promise<HealthSnapshot> {
return apiFetch<HealthSnapshot | { health: HealthSnapshot }>('/api/health').then((data) =>
unwrapField(data, 'health'),
);
}
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
export function getConfig(): Promise<string> {
return apiFetch<string | { format?: string; content: string }>('/api/config').then((data) =>
typeof data === 'string' ? data : data.content,
);
}
export function putConfig(toml: string): Promise<void> {
return apiFetch<void>('/api/config', {
method: 'PUT',
headers: { 'Content-Type': 'application/toml' },
body: toml,
});
}
// ---------------------------------------------------------------------------
// Tools
// ---------------------------------------------------------------------------
export function getTools(): Promise<ToolSpec[]> {
return apiFetch<ToolSpec[] | { tools: ToolSpec[] }>('/api/tools').then((data) =>
unwrapField(data, 'tools'),
);
}
// ---------------------------------------------------------------------------
// Cron
// ---------------------------------------------------------------------------
export function getCronJobs(): Promise<CronJob[]> {
return apiFetch<CronJob[] | { jobs: CronJob[] }>('/api/cron').then((data) =>
unwrapField(data, 'jobs'),
);
}
export function addCronJob(body: {
name?: string;
command: string;
schedule: string;
enabled?: boolean;
}): Promise<CronJob> {
return apiFetch<CronJob | { status: string; job: CronJob }>('/api/cron', {
method: 'POST',
body: JSON.stringify(body),
}).then((data) => (typeof (data as { job?: CronJob }).job === 'object' ? (data as { job: CronJob }).job : (data as CronJob)));
}
export function deleteCronJob(id: string): Promise<void> {
return apiFetch<void>(`/api/cron/${encodeURIComponent(id)}`, {
method: 'DELETE',
});
}
export function getCronRuns(
jobId: string,
limit: number = 20,
): Promise<CronRun[]> {
const params = new URLSearchParams({ limit: String(limit) });
return apiFetch<CronRun[] | { runs: CronRun[] }>(
`/api/cron/${encodeURIComponent(jobId)}/runs?${params}`,
).then((data) => unwrapField(data, 'runs'));
}
// ---------------------------------------------------------------------------
// Integrations
// ---------------------------------------------------------------------------
export function getIntegrations(): Promise<Integration[]> {
return apiFetch<Integration[] | { integrations: Integration[] }>('/api/integrations').then(
(data) => unwrapField(data, 'integrations'),
);
}
// ---------------------------------------------------------------------------
// Doctor / Diagnostics
// ---------------------------------------------------------------------------
export function runDoctor(): Promise<DiagResult[]> {
return apiFetch<DiagResult[] | { results: DiagResult[]; summary?: unknown }>('/api/doctor', {
method: 'POST',
body: JSON.stringify({}),
}).then((data) => (Array.isArray(data) ? data : data.results));
}
// ---------------------------------------------------------------------------
// Memory
// ---------------------------------------------------------------------------
export function getMemory(
query?: string,
category?: string,
): Promise<MemoryEntry[]> {
const params = new URLSearchParams();
if (query) params.set('query', query);
if (category) params.set('category', category);
const qs = params.toString();
return apiFetch<MemoryEntry[] | { entries: MemoryEntry[] }>(`/api/memory${qs ? `?${qs}` : ''}`).then(
(data) => unwrapField(data, 'entries'),
);
}
export function storeMemory(
key: string,
content: string,
category?: string,
): Promise<void> {
return apiFetch<unknown>('/api/memory', {
method: 'POST',
body: JSON.stringify({ key, content, category }),
}).then(() => undefined);
}
export function deleteMemory(key: string): Promise<void> {
return apiFetch<void>(`/api/memory/${encodeURIComponent(key)}`, {
method: 'DELETE',
});
}
// ---------------------------------------------------------------------------
// Cost
// ---------------------------------------------------------------------------
export function getCost(): Promise<CostSummary> {
return apiFetch<CostSummary | { cost: CostSummary }>('/api/cost').then((data) =>
unwrapField(data, 'cost'),
);
}
// ---------------------------------------------------------------------------
// CLI Tools
// ---------------------------------------------------------------------------
export function getCliTools(): Promise<CliTool[]> {
return apiFetch<CliTool[] | { cli_tools: CliTool[] }>('/api/cli-tools').then((data) =>
unwrapField(data, 'cli_tools'),
);
}