import type { LogLine, Session } from "../types"; async function parseError(res: Response): Promise { try { const j = await res.json(); if (typeof j.detail === "string") return j.detail; if (j.detail?.message) return j.detail.message; return JSON.stringify(j.detail || j); } catch { return res.statusText || `HTTP ${res.status}`; } } export async function createSession( repoUrl: string, githubToken?: string, ref?: string ): Promise { const res = await fetch("/api/sessions", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ repo_url: repoUrl, github_token: githubToken || undefined, ref: ref || undefined, }), }); if (!res.ok) throw new Error(await parseError(res)); return res.json(); } export async function getSession(id: string): Promise { const res = await fetch(`/api/sessions/${id}`); if (!res.ok) throw new Error(await parseError(res)); return res.json(); } export async function getLogs(id: string, after = 0): Promise<{ lines: LogLine[]; next_after: number }> { const res = await fetch(`/api/sessions/${id}/logs?after=${after}`); if (!res.ok) throw new Error(await parseError(res)); return res.json(); } export async function deleteSession(id: string): Promise { const res = await fetch(`/api/sessions/${id}`, { method: "DELETE" }); if (!res.ok && res.status !== 404) throw new Error(await parseError(res)); }