import type { BoneItem, DraftInfo, Job, Line, Model, Options, Prompts, RunState, SessionInfo, Settings } from './types' const BASE = '/anatomy_fix' async function unwrap(response: Response): Promise { let body: unknown = null try { body = await response.json() } catch { // not JSON: the status text has to do } if (!response.ok) { const message = body && typeof body === 'object' && 'error' in body ? String((body as { error: unknown }).error) : '' throw new Error(message || `${response.status} ${response.statusText}`) } return body as T } function post(path: string, body: unknown): Promise { return fetch(BASE + path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }).then((response) => unwrap(response)) } export type ImageWhich = 'image' | 'draft' | 'levels' | 'guide' | 'result' | `variant-${number}` export const api = { options: () => fetch(`${BASE}/options`).then((response) => unwrap(response)), upload: (file: Blob, name: string) => fetch(`${BASE}/session?name=${encodeURIComponent(name)}`, { method: 'POST', body: file }).then((response) => unwrap(response), ), /** ComfyUI: the image the node last received. */ node: (node: string) => post('/session/node', { node }), draft: (session: string, job: Job) => post('/draft', { session, job }), fan: (line: Line, kind: string, thumb: string, spread: number, width: number) => post<{ bones: Omit[] }>('/fan', { line, kind, thumb, spread, width }), run: (session: string, job: Job, model: Model & Prompts) => post('/run', { session, job, model }), runState: (session: string) => post('/run/state', { session }), stop: (session: string) => post('/run/stop', { session }), pick: (session: string, seed: number) => post<{ version: number }>('/pick', { session, seed }), apply: (session: string) => post('/apply', { session }), undo: (session: string) => post('/undo', { session }), save: (session: string, tab: string) => post<{ path: string }>('/save', { session, tab }), saveSettings: (settings: Settings) => post<{ settings: Settings }>('/settings', settings), imageUrl: (session: string, which: ImageWhich, version: number) => `${BASE}/image/${session}/${which}?v=${version}`, }