File size: 4,236 Bytes
2a261b0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | import type { FontMatch, Look, Model, Models, Options, Recipe, SessionInfo, Settings, Shape } from './types'
const BASE = '/text_fix'
async function unwrap<T>(response: Response): Promise<T> {
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<T>(path: string, body: unknown): Promise<T> {
return fetch(BASE + path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}).then((response) => unwrap<T>(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<Options>(response)),
upload: (file: Blob, name: string) =>
fetch(`${BASE}/session?name=${encodeURIComponent(name)}`, { method: 'POST', body: file }).then((response) =>
unwrap<SessionInfo>(response),
),
/** ComfyUI: the image the node last received. */
node: (node: string) => post<SessionInfo>('/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>('/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<SessionInfo>('/apply', { session }),
undo: (session: string) => post<SessionInfo>('/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<string, Look> }>('/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<BatchState>('/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)}`,
}
|