LazyBoy2/web/src/api.ts

90 lines
3.0 KiB
TypeScript
Raw Normal View History

2026-09-15 05:20:44 +00:00
export type HumanQuestion = {
question?: string; reason?: string; options?: unknown[]; kind?: string;
task_id?: string; question_id?: string; site_url?: string; surface?: string;
handover?: boolean; submitted?: boolean;
};
2026-09-15 03:20:42 +00:00
export type AgentActivity = {
state: "idle" | "queued" | "running" | "waiting_input";
running: boolean;
queued: boolean;
observed_at_ms: number;
active_task_ids: string[];
2026-09-15 05:20:44 +00:00
question?: HumanQuestion | null;
2026-09-15 03:20:42 +00:00
};
2026-09-14 09:08:20 +00:00
export type AgentRow = {
id: string;
name: string;
expertise?: string;
preview?: string;
running?: boolean;
2026-09-15 03:20:42 +00:00
activity?: AgentActivity;
2026-09-14 09:08:20 +00:00
};
2026-09-15 03:20:42 +00:00
export type TranscriptItem = { role: "user" | "assistant"; content: string; at?: string };
2026-09-14 09:08:20 +00:00
export type AgentDetail = {
id: string;
name: string;
expertise: string;
preview: string;
transcript: TranscriptItem[];
running: boolean;
2026-09-15 03:20:42 +00:00
activity: AgentActivity;
2026-09-14 09:08:20 +00:00
};
export type TeamEvent = {
id: number;
kind: string;
task_id?: string | null;
payload: Record<string, unknown>;
};
async function request<T>(path: string, init?: RequestInit): Promise<T> {
const res = await fetch(path, {
...init,
headers: { "content-type": "application/json", ...(init?.headers || {}) },
});
const raw = await res.text();
let data: unknown = null;
try {
data = raw ? JSON.parse(raw) : null;
} catch {
data = raw;
}
if (!res.ok) {
const err = data as { error?: string } | null;
throw new Error(err?.error || raw || String(res.status));
}
return data as T;
}
export const api = {
health: () => request<{ ok: boolean; service: boolean; model: string }>("/api/health"),
agents: async () => {
const data = await request<AgentRow[] | { agents?: AgentRow[] }>("/api/agents");
const agents = Array.isArray(data) ? data : data.agents || [];
return { agents };
},
createAgent: (name: string) => request<AgentRow>("/api/agents", { method: "POST", body: JSON.stringify({ name }) }),
2026-09-15 03:20:42 +00:00
deleteAgent: (id: string) => request<{ ok: boolean; id: string; name: string }>(`/api/agents/${id}`, { method: "DELETE" }),
2026-09-14 09:08:20 +00:00
agent: (id: string) => request<AgentDetail>(`/api/agents/${id}`),
2026-09-15 03:20:42 +00:00
activity: (id: string) => request<AgentActivity>(`/api/agents/${id}/activity`, { signal: AbortSignal.timeout(8000) }),
2026-09-14 09:08:20 +00:00
send: (id: string, text: string) =>
request<{ queued?: string }>(`/api/agents/${id}/messages`, { method: "POST", body: JSON.stringify({ text }) }),
2026-09-15 05:20:44 +00:00
handoverDone: (id: string, taskId: string, questionId: string, action: "done" | "cancel" = "done") => request(`/api/agents/${id}/handover`, { method: "POST", body: JSON.stringify({ task_id: taskId, question_id: questionId, action }), signal: AbortSignal.timeout(8000) }),
2026-09-14 09:08:20 +00:00
stop: (id: string) => request(`/api/agents/${id}/stop`, { method: "POST", body: "{}" }),
2026-09-15 03:20:42 +00:00
computer: (action: "start" | "restart" | "update" = "start") =>
request<{
ready: boolean;
viewer_url: string;
error?: string;
updated?: boolean;
revision?: string;
}>("/api/computer", {
2026-09-14 09:08:20 +00:00
method: "POST",
2026-09-15 03:20:42 +00:00
body: JSON.stringify({ action }),
2026-09-14 09:08:20 +00:00
}),
};