LazyBoy2/web/src/api.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2026-09-14 09:08:20 +00:00
export type AgentRow = {
id: string;
name: string;
expertise?: string;
preview?: string;
running?: boolean;
};
export type TranscriptItem = { role: "user" | "assistant"; content: string };
export type AgentDetail = {
id: string;
name: string;
expertise: string;
preview: string;
transcript: TranscriptItem[];
running: boolean;
};
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 }) }),
agent: (id: string) => request<AgentDetail>(`/api/agents/${id}`),
send: (id: string, text: string) =>
request<{ queued?: string }>(`/api/agents/${id}/messages`, { method: "POST", body: JSON.stringify({ text }) }),
stop: (id: string) => request(`/api/agents/${id}/stop`, { method: "POST", body: "{}" }),
computer: () =>
request<{ ready: boolean; viewer_url: string; error?: string }>("/api/computer", {
method: "POST",
body: "{}",
}),
};