File size: 2,745 Bytes
5bf2d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios, { type AxiosInstance } from "axios";

/** Base API URL – from env or fallback for local dev */
const getBaseUrl = (): string =>
  typeof window !== "undefined"
    ? process.env.NEXT_PUBLIC_API_URL || ""
    : process.env.NEXT_PUBLIC_API_URL || process.env.API_URL || "http://localhost:8000";

/** API client instance */
function createApiClient(): AxiosInstance {
  const baseURL = getBaseUrl();
  return axios.create({
    baseURL: baseURL ? `${baseURL.replace(/\/$/, "")}/api/v1` : "/api/v1",
    headers: { "Content-Type": "application/json" },
    timeout: 60_000,
  });
}

const api = createApiClient();

// ─── Types (aligned with backend schemas) ─────────────────────────────────────

export interface GenerationRequest {
  prompt: string;
  lyrics?: string;
  duration?: number;
  style?: string;
  voice_preset?: string;
  vocal_volume?: number;
  instrumental_volume?: number;
}

/** Prompt analysis from backend (nested in metadata) */
export interface PromptAnalysis {
  original_prompt?: string;
  style?: string;
  tempo?: number | string;
  mood?: string;
  instrumentation?: string[];
  lyrics?: string;
  duration_hint?: number;
  enriched_prompt?: string;
}

export interface GenerationMetadata {
  prompt?: string;
  analysis?: PromptAnalysis;
}

export interface GenerationResponse {
  id: string;
  status: "pending" | "processing" | "completed" | "failed";
  prompt: string;
  audio_path?: string | null;
  metadata?: GenerationMetadata | null;
  processing_time_seconds?: number | null;
  error_message?: string | null;
  created_at?: string | null;
  completed_at?: string | null;
}

export interface GenerationListResponse {
  items: GenerationResponse[];
  total: number;
  page: number;
  page_size: number;
}

// ─── API methods ─────────────────────────────────────────────────────────────

export const generationsApi = {
  async create(data: GenerationRequest): Promise<GenerationResponse> {
    const { data: res } = await api.post<GenerationResponse>("/generations/", data);
    return res;
  },

  async get(id: string): Promise<GenerationResponse> {
    const { data } = await api.get<GenerationResponse>(`/generations/${id}`);
    return data;
  },

  async list(page: number = 1, pageSize: number = 20): Promise<GenerationListResponse> {
    const { data } = await api.get<GenerationListResponse>("/generations/", {
      params: { page, page_size: pageSize },
    });
    return data;
  },
};