import type { FontMatch, Look, Model, Models, Options, Recipe, SessionInfo, Settings, Shape } from './types' const BASE = '/text_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)) } /** How far a whole folder has got. */ export interface BatchState { running: boolean done: number total: number changed: number failed: number output: string current: string warnings: string[] } export interface ItemBody { recipe: Recipe text: string erase?: boolean } 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 }), pick: (session: string, x: number, y: number, reach: number, extend?: Recipe) => post<{ recipe: Recipe | null; selection: Shape | null; reading?: string }>('/pick', { session, x, y, reach, extend }), /** The detector's models: what is here, or fetch the ones a detector needs. */ models: (action: 'status' | 'download', choice?: 'light' | 'full') => post('/models', { action, choice }), resolve: (session: string, items: ItemBody[]) => post<{ selections: (Shape | null)[] }>('/resolve', { session, items }), find: (session: string) => post<{ found: Shape[] }>('/find', { session }), /** The fonts most like the old letters of a line, closest first. */ match: (session: string, recipe: Recipe) => post<{ fonts: FontMatch[] }>('/match', { session, recipe }), preview: (session: string, items: ItemBody[], look: Look) => post<{ version: number; empty: boolean; count: number }>('/preview', { session, items, look }), blend: (session: string, items: ItemBody[], look: Look, model: Model & { prompt: string; negative: string }) => post<{ version: number blended: boolean count?: number warning?: string variants?: number seeds?: number[] removed?: string[] }>('/blend', { session, items, look, model, }), /** One of the variants of the last model pass becomes the result. */ choose: (session: string, index: number) => post<{ version: number; index: number }>('/choose', { session, index }), apply: (session: string) => post('/apply', { session }), undo: (session: string) => post('/undo', { session }), save: (session: string, tab: string) => post<{ path: string }>('/save', { session, tab }), /** Save the look under a name, or delete a saved one; answers with all of them. */ presets: (action: 'save' | 'delete', name: string, look?: Look) => post<{ presets: Record }>('/presets', { action, name, look }), /** A whole folder: start it, stop it, or ask how far it got. */ batch: ( body: | { action: 'status' | 'stop' } | { action: 'start'; input: string; lines: string; blend: boolean; look: Look; model: Model }, ) => post('/batch', body), saveSettings: (settings: Settings) => post<{ settings: Settings }>('/settings', settings), imageUrl: (session: string, which: 'image' | 'result' | `variant-${number}`, version: number) => `${BASE}/image/${session}/${which}?v=${version}`, fontUrl: (name: string) => `${BASE}/font?name=${encodeURIComponent(name)}`, }