export interface Health { status: 'ready' | 'not_ready' device: 'cpu' model: { loaded: boolean; assets_available: boolean; reason: string | null; model_id: string; revision: string; head_seed: number } limits: { max_upload_bytes: number; max_duration_seconds: number; min_duration_seconds: number; max_queue_jobs: number; job_ttl_seconds: number } queue: { active: number; queued: number } prediction_task: 'overall_static_valence_arousal' calibration: 'none' } export interface TimelinePoint { index: number; start_seconds: number; end_seconds: number; center_seconds: number; feature_bins: number valence: number; arousal: number; display_valence: number; display_arousal: number; out_of_range: boolean } export interface PredictionTimeline { kind: 'exploratory_static_windows'; window_seconds: 10; hop_seconds: 5 feature_context: 'reused_full_clip_mert_windows'; validated_dynamic_model: false; smoothed: false points: Array; disclaimer: string } export interface Prediction { timeline?: PredictionTimeline valence: number; arousal: number; display_valence: number; display_arousal: number out_of_range: boolean; duration_seconds: number; feature_bins: number; processing_seconds: number device: 'cpu'; calibration: 'none'; head_seed: number; model_revision: string } export interface Job { job_id: string status: 'queued' | 'running' | 'completed' | 'failed' stage: 'queued' | 'decoding' | 'loading_model' | 'extracting_features' | 'predicting' | 'completed' | 'failed' progress: number result: Prediction | null error: string | null } export interface Ticket { job_id: string; access_token: string; status: 'queued' } export const API_BASE = (import.meta.env.VITE_API_BASE_URL || 'http://127.0.0.1:8000').replace(/\/+$/, '') export const MAX_POLL_MS = 20 * 60 * 1000 export const POLL_INTERVAL_MS = 2000 export type UploadProblem = 'upload.errorM4a' | 'upload.errorEmpty' | 'upload.errorTooLarge' export function uploadProblem(file: { size: number; name?: string }, maxBytes = 20971520): UploadProblem | null { if (file.name && /\.m4a$/i.test(file.name)) return 'upload.errorM4a' if (!file.size) return 'upload.errorEmpty' if (file.size > maxBytes) return 'upload.errorTooLarge' return null } export function plotCoordinate(value: number): number { return Math.max(-1, Math.min(1, value)) } export async function request(path: string, signal: AbortSignal, init: RequestInit = {}): Promise { const response = await fetch(`${API_BASE}${path}`, { ...init, signal }) if (!response.ok) { let detail = `HTTP ${response.status}` try { const body: unknown = await response.json(); if (body && typeof body === 'object' && 'detail' in body && typeof body.detail === 'string') detail = body.detail } catch { /* Preserve HTTP status when response is not JSON. */ } throw new Error(detail) } return response.json() as Promise } export function errorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error) } export function jobHeaders(ticket: Ticket): HeadersInit { return { ...uploadHeaders(), 'X-Job-Token': ticket.access_token } } export function uploadHeaders(): Record { const token = import.meta.env.VITE_API_AUTH_TOKEN?.trim() return token ? { Authorization: `Bearer ${token}` } : {} }