File size: 2,003 Bytes
94004a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Batch dubbing API — wraps the /batch/* backend endpoints.
 *
 * Used by BatchQueue and BatchAddDialog to enqueue, monitor, and
 * manage batch dub jobs.
 */
import { apiJson, apiPost, apiDelete, API } from './client';

export interface BatchJob {
  id: string;
  status: 'queued' | 'running' | 'done' | 'failed' | 'cancelled';
  filename: string;
  langs: string[];
  voice_id?: string;
  preserve_bg: boolean;
  created_at: number;
  started_at?: number;
  finished_at?: number;
  error?: string;
  progress?: {
    stage: string;
    percent: number;
    current_lang?: string;
    current_segment?: number;
    total_segments?: number;
    segments_count?: number;
  };
  outputs?: Record<string, string>;
}

/** List batch jobs, optionally filtered by status. */
export async function listBatchJobs(status?: string, limit = 50): Promise<BatchJob[]> {
  const qs = new URLSearchParams();
  if (status) qs.set('status', status);
  qs.set('limit', String(limit));
  return apiJson<BatchJob[]>(`/batch/jobs?${qs.toString()}`);
}

/** Get a single batch job by ID. */
export async function getBatchJob(id: string): Promise<BatchJob> {
  return apiJson<BatchJob>(`/batch/jobs/${id}`);
}

/** Enqueue a video for batch dubbing. */
export async function enqueueBatchJob(
  file: File,
  langs: string[],
  voiceId?: string,
  preserveBg = true,
): Promise<{ job_id: string; status: string; queue_position: number }> {
  const form = new FormData();
  form.append('video', file);
  form.append('langs', langs.join(','));
  if (voiceId) form.append('voice_id', voiceId);
  form.append('preserve_bg', String(preserveBg));
  return apiPost('/batch/enqueue', form);
}

/** Cancel a batch job. */
export async function cancelBatchJob(id: string): Promise<unknown> {
  return apiPost(`/batch/jobs/${id}/cancel`, {});
}

/** Delete a batch job and its files. */
export async function deleteBatchJob(id: string): Promise<unknown> {
  const res = await apiDelete(`/batch/jobs/${id}`);
  return res.json();
}