Spaces:
Running
Running
File size: 18,127 Bytes
5f3e9f5 | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | import type {
CacheStats,
GenerateResponse,
GenerateSettings,
HistoryEntry,
ListResponse,
ListCategory,
ListCategoryResponse,
PreflightResponse,
SseEvent,
BackendRunStartResponse,
BackendRunDetail,
PendingClientQueueItem,
SavedThumbnailTemplate,
YoutubeVideosResponse,
} from './types'
// Base URL for the Flask backend. Starts from the build-time env var, but can
// be overridden at runtime from the Settings page via `setBackendBaseUrl()` β
// that way users can point the UI at a different host without a rebuild.
const DEFAULT_API_BASE: string = (import.meta.env.VITE_BACKEND_URL as string | undefined) ?? ''
let API_BASE: string = DEFAULT_API_BASE
export function setBackendBaseUrl(url: string): void {
API_BASE = (url ?? '').trim() || DEFAULT_API_BASE
}
export function getBackendBaseUrl(): string {
return API_BASE
}
function buildUrl(path: string): string {
if (!API_BASE) return path
return `${API_BASE.replace(/\/$/, '')}${path}`
}
async function parseJson<T>(res: Response): Promise<T> {
const text = await res.text()
if (!text) return {} as T
try {
return JSON.parse(text) as T
} catch {
throw new Error(`Invalid JSON response (${res.status}): ${text.slice(0, 200)}`)
}
}
async function postJson<T>(path: string, body: unknown): Promise<T> {
const res = await fetch(buildUrl(path), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
const text = await res.text()
if (!res.ok && res.status === 409) {
const rejected = tryParseRejection(text)
if (rejected) throw rejected
}
if (!text) {
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
return {} as T
}
let data: T & { error?: string }
try {
data = JSON.parse(text) as T & { error?: string }
} catch {
throw new Error(`Invalid JSON response (${res.status}): ${text.slice(0, 200)}`)
}
if (!res.ok || data.error) {
throw new Error(data.error || `${res.status} ${res.statusText}`)
}
return data
}
async function getJson<T>(path: string): Promise<T> {
const res = await fetch(buildUrl(path))
const data = await parseJson<T & { error?: string }>(res)
if (!res.ok) throw new Error((data as { error?: string }).error || res.statusText)
return data
}
// βββ Preflight cache βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// User complaint: "preflight being hit every 2-3s from the UI". Nothing in
// the codebase polls that fast but components fetch preflight on mount,
// so a tab-happy user can easily trigger 5+ calls in a second. Cache the
// response for 30s and coalesce concurrent lookups to a single request.
const PREFLIGHT_CACHE_MS = 30_000
interface PreflightCache {
value: PreflightResponse | null
fetchedAt: number
inFlight: Promise<PreflightResponse> | null
}
let _preflightCache: PreflightCache = { value: null, fetchedAt: 0, inFlight: null }
export function invalidatePreflightCache(): void {
_preflightCache = { value: null, fetchedAt: 0, inFlight: null }
}
export const api = {
generate: (text: string, settings: GenerateSettings = {}) =>
postJson<GenerateResponse>('/generate', { text, ...settings }),
startTextToVideoRun: (text: string, settings: GenerateSettings = {}) =>
postJson<BackendRunStartResponse>('/runs/text-to-video', { text, ...settings }),
startHtmlToVideoRun: (html: string, settings: GenerateSettings = {}) =>
postJson<BackendRunStartResponse>('/runs/html-to-video', { html, ...settings }),
/**
* Submit pre-captured screenshots to the same MP4/PPTX export pipeline
* used by Text β Video. The screenshots are uploaded as
* ``screenshots[]`` parts; ``settings`` (resolution, fps, project info,
* thumbnails, β¦) ride along as form fields so the backend can apply
* the canonical ``class_X_subject_chapter_Y_exercise_<year>`` filename
* scheme used everywhere else.
*/
startScreenshotsToVideoRun: async (
screenshots: File[],
settings: GenerateSettings = {},
): Promise<BackendRunStartResponse> => {
const fd = new FormData()
for (const f of screenshots) fd.append('screenshots[]', f, f.name)
for (const [key, value] of Object.entries(settings)) {
if (value === undefined || value === null) continue
if (typeof value === 'boolean') fd.append(key, value ? '1' : '0')
else fd.append(key, String(value))
}
const res = await fetch(buildUrl('/runs/screenshots-to-video'), {
method: 'POST',
body: fd,
})
const text = await res.text()
if (!res.ok && res.status === 409) {
const rejected = tryParseRejection(text)
if (rejected) throw rejected
}
if (!text) {
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
return {} as BackendRunStartResponse
}
let data: BackendRunStartResponse & { error?: string }
try {
data = JSON.parse(text) as BackendRunStartResponse & { error?: string }
} catch {
throw new Error(`Invalid JSON response (${res.status}): ${text.slice(0, 200)}`)
}
if (!res.ok || data.error) {
throw new Error(data.error || `${res.status} ${res.statusText}`)
}
return data
},
getRun: (runId: string) =>
getJson<BackendRunDetail>(`/runs/${encodeURIComponent(runId)}`),
/**
* Subscribe to a server-sent event stream for a run. Used by the
* Processes page to react to status changes immediately instead of
* polling /runs/<id>. The promise resolves when the run reaches a
* terminal state (or the consumer aborts via signal).
*/
streamRunEvents: (runId: string, opts: Pick<SseStreamOptions, 'signal' | 'onEvent'>) =>
streamSseGet(`/runs/${encodeURIComponent(runId)}/events`, opts),
getPendingClientQueue: () =>
getJson<{ success: boolean; items: PendingClientQueueItem[] }>('/runs/pending-client-queue'),
savePendingClientQueue: async (items: PendingClientQueueItem[]) => {
const res = await fetch(buildUrl('/runs/pending-client-queue'), {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items }),
})
const data = await parseJson<{ success?: boolean; items?: PendingClientQueueItem[]; error?: string }>(res)
if (!res.ok || data.error) throw new Error(data.error || res.statusText)
return data
},
generateHtml: (html: string, settings: GenerateSettings = {}) =>
postJson<GenerateResponse>('/generate-html', { html, ...settings }),
cancel: (operationId: string) =>
postJson<{ success: boolean; message: string }>(`/cancel/${encodeURIComponent(operationId)}`, {}),
cancelRun: (
runId: string,
options?: {
mode?: 'now' | 'after_html' | 'after_screenshots' | 'after_pptx' | 'after_video'
delete_outputs?: boolean
},
) =>
postJson<{ success: boolean; message: string }>(`/runs/${encodeURIComponent(runId)}/cancel`, options ?? {}),
beautify: (html: string) => postJson<{ html: string; validation: unknown }>('/beautify', { html }),
minify: (html: string) =>
postJson<{ html: string; original_size: number; minified_size: number; reduction_percent: number }>(
'/minify',
{ html },
),
regenerate: (htmlFilename: string, settings: GenerateSettings = {}) =>
postJson<GenerateResponse>('/regenerate', { html_filename: htmlFilename, ...settings }),
list: () => getJson<ListResponse>('/list'),
/**
* Per-category, paginated, mtime-sorted listing. Use this when the
* Library tab only needs one category β avoids the full /list scan.
*/
listCategory: (
category: ListCategory,
opts: { page?: number; size?: number; since?: number } = {},
) => {
const params = new URLSearchParams()
if (opts.page) params.set('page', String(opts.page))
if (opts.size) params.set('size', String(opts.size))
if (opts.since) params.set('since', String(opts.since))
const qs = params.toString()
return getJson<ListCategoryResponse>(`/list/${category}${qs ? `?${qs}` : ''}`)
},
history: () => getJson<HistoryEntry[]>('/history'),
youtubeVideos: () => getJson<YoutubeVideosResponse>('/youtube/videos'),
clearHistory: () => postJson<{ success: boolean; message: string }>('/history/clear', {}),
deleteFile: async (type: 'screenshot' | 'html' | 'presentation' | 'video', filename: string) => {
// Encode each path segment separately. encodeURIComponent would escape
// `/` as `%2F`, which Werkzeug's dev server does NOT decode back to `/`
// in PATH_INFO β so the <path:filename> converter would get a literal
// `%2F` and fail to match any file on disk. Screenshots inside batch
// subfolders (e.g. `batch 3/5(1).png`) rely on the split-and-join
// treatment.
const encoded = filename.split('/').map(encodeURIComponent).join('/')
const res = await fetch(buildUrl(`/delete/${type}/${encoded}`), {
method: 'DELETE',
})
return parseJson<{ success?: boolean; error?: string }>(res)
},
/**
* Preflight probes are expensive on Windows (they spawn POWERPNT.EXE to
* verify COM availability). A single client cache with a 30s TTL stops
* us from hammering the backend every time a component mounts.
*
* Pass `{ fresh: true }` to bypass the cache β the "Refresh" button on
* the Home preflight tile uses this, and the Settings page's "Ping"
* button does too.
*/
preflight: (opts?: { fresh?: boolean }): Promise<PreflightResponse> => {
// Cached path β reuse a recent response without hitting the network.
if (!opts?.fresh) {
const now = Date.now()
if (
_preflightCache.value &&
now - _preflightCache.fetchedAt < PREFLIGHT_CACHE_MS
) {
return Promise.resolve(_preflightCache.value)
}
}
// Always coalesce concurrent callers onto the same in-flight request,
// even when one of them passed `fresh: true`. A fresh probe is still
// a newer-than-cache result, so a cached waiter is perfectly happy
// with it β we just had to stop the previous "fresh bypasses the
// in-flight check" branch, which could issue 4 parallel
// POWERPNT-spawning probes if four components mounted together asked
// for fresh data at once.
if (_preflightCache.inFlight) return _preflightCache.inFlight
const p = getJson<PreflightResponse>(opts?.fresh ? '/preflight?fresh=1' : '/preflight')
.then((r) => {
_preflightCache = { value: r, fetchedAt: Date.now(), inFlight: null }
return r
})
.catch((e) => {
_preflightCache = { ..._preflightCache, inFlight: null }
throw e
})
_preflightCache = { ..._preflightCache, inFlight: p }
return p
},
cacheStats: () => getJson<CacheStats>('/cache/stats'),
clearCache: () => postJson<{ success: boolean; message: string }>('/cache/clear', {}),
/**
* Backend version metadata β git SHA + boot timestamp. Cached for the
* page's lifetime (the value can't change without a backend restart, at
* which point the SSE/polling layer reconnects anyway).
*/
version: () =>
getJson<{ service: string; sha: string; started_at: string }>('/version'),
screenshotUrl: (filename: string) =>
buildUrl(`/screenshots/${filename.split('/').map(encodeURIComponent).join('/')}`),
htmlUrl: (filename: string) => buildUrl(`/html/${encodeURIComponent(filename)}`),
downloadUrl: (filepath: string) =>
buildUrl(`/download/${filepath.split(/[\\/]/).map(encodeURIComponent).join('/')}`),
thumbnailUrl: (filename: string) =>
buildUrl(`/thumbnails/${encodeURIComponent(filename)}`),
listThumbnailTemplates: (className?: string, subject?: string) => {
const params = new URLSearchParams()
if (className) params.set('className', className)
if (subject) params.set('subject', subject)
const qs = params.toString()
return getJson<{ success: boolean; templates: SavedThumbnailTemplate[] }>(
`/thumbnail-templates${qs ? `?${qs}` : ''}`,
)
},
saveThumbnailTemplate: (template: Omit<SavedThumbnailTemplate, 'id' | 'createdAt' | 'updatedAt'>) =>
postJson<{ success: boolean; template: SavedThumbnailTemplate }>('/thumbnail-templates', template),
deleteThumbnailTemplate: (id: string) =>
fetch(buildUrl(`/thumbnail-templates/${encodeURIComponent(id)}`), { method: 'DELETE' })
.then((res) => parseJson<{ success?: boolean; error?: string }>(res)),
/** POST a JSON template payload and get back the matching PNG.
*
* Used as a backend parity check (and as a server-rendered fallback) for
* the same `ThumbnailTemplateState` produced by the frontend editor. */
renderThumbnailTemplate: async (
payload: Record<string, unknown>,
): Promise<Blob> => {
const r = await fetch(buildUrl('/render-thumbnail-template'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
})
if (!r.ok) {
const msg = await r.text()
throw new Error(`Render failed (${r.status}): ${msg}`)
}
return r.blob()
},
uploadThumbnail: async (
file: File,
): Promise<{ success: boolean; filename: string; url: string; size_bytes: number }> => {
const fd = new FormData()
fd.append('file', file)
const r = await fetch(buildUrl('/upload-thumbnail'), { method: 'POST', body: fd })
if (!r.ok) {
const msg = await r.text()
throw new Error(`Upload failed (${r.status}): ${msg}`)
}
return r.json()
},
downloadZip: async (files: string[], name = 'screenshots'): Promise<Blob> => {
const res = await fetch(buildUrl('/download-zip'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ files, name }),
})
if (!res.ok) throw new Error(`Failed to download ZIP: ${res.status}`)
return res.blob()
},
}
/**
* Stream Server-Sent Events from a POST endpoint. Flask's `/generate-sse` and
* `/image-to-screenshots-sse` accept a POST body and stream `data: {...}\n\n`
* lines β `EventSource` only supports GET, so we drive this manually with fetch
* + ReadableStream.
*/
export interface SseStreamOptions {
body: BodyInit
headers?: Record<string, string>
signal?: AbortSignal
onEvent: (ev: SseEvent) => void
}
/**
* Thrown when the backend rejects a run because another one is in flight or
* the same payload was just submitted within the dedup window. The 409 body
* shape comes from `src/utils/run_guard.py::RunRejected`.
*/
export class RunRejectedError extends Error {
reason: 'in_flight' | 'duplicate' | 'unknown'
operationId: string | null
constructor(reason: 'in_flight' | 'duplicate' | 'unknown', message: string, operationId: string | null = null) {
super(message)
this.name = 'RunRejectedError'
this.reason = reason
this.operationId = operationId
}
}
function tryParseRejection(text: string): RunRejectedError | null {
try {
const data = JSON.parse(text) as {
reason?: string
error?: string
message?: string
operation_id?: string
}
const reasonRaw = (data.reason ?? '').toLowerCase()
const reason: RunRejectedError['reason'] =
reasonRaw === 'in_flight' || reasonRaw === 'duplicate' ? reasonRaw : 'unknown'
const message = data.error || data.message || 'Run rejected by backend'
return new RunRejectedError(reason, message, data.operation_id ?? null)
} catch {
return null
}
}
export async function streamSse(path: string, opts: SseStreamOptions): Promise<void> {
const res = await fetch(buildUrl(path), {
method: 'POST',
headers: { Accept: 'text/event-stream', ...(opts.headers ?? {}) },
body: opts.body,
signal: opts.signal,
})
if (!res.ok || !res.body) {
const text = !res.ok ? await res.text().catch(() => '') : ''
if (res.status === 409) {
const rejected = tryParseRejection(text)
if (rejected) throw rejected
}
throw new Error(`SSE request failed: ${res.status} ${res.statusText} ${text.slice(0, 200)}`)
}
const reader = res.body.getReader()
const decoder = new TextDecoder('utf-8')
let buffer = ''
while (true) {
const { value, done } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
// SSE messages are separated by blank lines.
const parts = buffer.split(/\r?\n\r?\n/)
buffer = parts.pop() ?? ''
for (const part of parts) {
const dataLines = part
.split(/\r?\n/)
.filter((l) => l.startsWith('data:'))
.map((l) => l.slice(5).trimStart())
if (dataLines.length === 0) continue
const raw = dataLines.join('\n')
try {
const parsed = JSON.parse(raw) as SseEvent
opts.onEvent(parsed)
} catch {
// Ignore non-JSON keepalives / comments.
}
}
}
}
export async function streamSseGet(
path: string,
opts: Pick<SseStreamOptions, 'signal' | 'onEvent'>,
): Promise<void> {
const res = await fetch(buildUrl(path), {
method: 'GET',
headers: { Accept: 'text/event-stream' },
signal: opts.signal,
})
if (!res.ok || !res.body) {
const text = !res.ok ? await res.text().catch(() => '') : ''
throw new Error(`SSE request failed: ${res.status} ${res.statusText} ${text.slice(0, 200)}`)
}
const reader = res.body.getReader()
const decoder = new TextDecoder('utf-8')
let buffer = ''
while (true) {
const { value, done } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
const parts = buffer.split(/\r?\n\r?\n/)
buffer = parts.pop() ?? ''
for (const part of parts) {
const dataLines = part
.split(/\r?\n/)
.filter((l) => l.startsWith('data:'))
.map((l) => l.slice(5).trimStart())
if (dataLines.length === 0) continue
const raw = dataLines.join('\n')
try {
opts.onEvent(JSON.parse(raw) as SseEvent)
} catch {
// Ignore non-JSON keepalives / comments.
}
}
}
}
|