import { API, apiUrl, apiJson, apiPost, apiFetch } from './client'; import type { DubHistoryResponse, DubTranslateResponse } from './types'; export async function dubUpload( file: File | Blob, jobId: string, { signal }: { signal?: AbortSignal } = {}, ): Promise { const fd = new FormData(); fd.append('video', file); fd.append('job_id', jobId); return apiPost('/dub/upload', fd, { signal }); } export interface IngestUrlOptions { signal?: AbortSignal; /** Ask yt-dlp to also pull caption tracks (incl. YouTube auto-translations). */ fetchSubs?: boolean; /** Limit caption fetch to specific lang codes; defaults to all available. */ subLangs?: string[]; } export async function dubIngestUrl( url: string, jobId: string, opts: IngestUrlOptions = {}, ): Promise { const { signal, fetchSubs, subLangs } = opts; return apiPost( '/dub/ingest-url', { url, job_id: jobId, fetch_subs: fetchSubs || undefined, sub_langs: subLangs && subLangs.length ? subLangs : undefined, }, { signal }, ); } export function transcribeStreamUrl(jobId: string): string { return `${API}/dub/transcribe-stream/${jobId}`; } export async function dubAbort(jobId: string): Promise { try { await apiFetch(`/dub/abort/${jobId}`, { method: 'POST' }); } catch { /* best-effort */ } } export async function dubCleanupSegments(jobId: string): Promise { return apiPost(`/dub/cleanup-segments/${jobId}`); } export interface DubImportSrtResponse { segments: Array<{ id: number; start: number; end: number; text: string; text_original: string; speaker_id: string; }>; stats: { imported: number; skipped_malformed: number; dropped_overlap: number; clamped_to_duration: number; }; } export async function dubImportSrt(jobId: string, file: File | Blob): Promise { const fd = new FormData(); fd.append('file', file); return apiPost(`/dub/import-srt/${jobId}`, fd); } export async function dubTranslate(body: Record): Promise { return apiPost('/dub/translate', body); } export async function dubGenerate(jobId: string, body: Record): Promise { return apiPost(`/dub/generate/${jobId}`, body); } export function tasksStreamUrl(taskId: string): string { return apiUrl(`/tasks/stream/${taskId}`); } export async function tasksCancel(taskId: string): Promise { return apiFetch(`/tasks/cancel/${taskId}`, { method: 'POST' }); } export async function listDubHistory(): Promise { return apiJson('/dub/history'); } export async function clearDubHistory(): Promise { return apiFetch('/dub/history', { method: 'DELETE' }); }