File size: 11,654 Bytes
e026dc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26e8ad9
 
e026dc9
 
fb69858
 
 
 
 
a162882
 
e026dc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5771b6
 
 
 
e026dc9
 
 
 
 
 
 
 
 
addcf34
c5771b6
 
e026dc9
 
 
 
 
 
 
 
 
 
68556d7
 
e026dc9
 
c5771b6
 
a162882
e026dc9
 
 
 
 
a162882
 
 
 
 
 
 
 
 
 
8cab861
 
 
 
 
 
 
 
 
 
 
 
 
 
e026dc9
 
c5771b6
 
 
533051b
e026dc9
533051b
9de719c
533051b
 
 
 
29066e0
addcf34
 
533051b
 
8cab861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e026dc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68556d7
 
 
 
 
 
 
 
 
 
 
e026dc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fbc3f9
e026dc9
 
 
 
 
 
 
26e8ad9
 
 
 
ff98a02
26e8ad9
ff98a02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26e8ad9
 
 
 
 
 
 
 
 
e026dc9
 
 
 
 
 
 
 
addcf34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb69858
 
 
 
 
 
 
0fbc3f9
fb69858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fbc3f9
fb69858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c17c06
fb69858
 
 
 
 
 
 
c5771b6
 
 
 
 
 
 
 
 
 
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
import apiClient from "./client";
import type {
  GenerateResponse,
  BatchResponse,
  MatrixGenerateResponse,
  TestingMatrixResponse,
  AnglesResponse,
  ConceptsResponse,
  CompatibleConceptsResponse,
  AngleInfo,
  ConceptInfo,
  DbStatsResponse,
  AdsListResponse,
  AdCreativeDB,
  HealthResponse,
  ApiRootResponse,
  ImageCorrectResponse,
  ImageRegenerateResponse,
  ModelsListResponse,
  LoginResponse,
  Niche,
  CreativeAnalysisResponse,
  CreativeModifyResponse,
  FileUploadResponse,
  ModificationMode,
  CreativeAnalysisData,
  MotivatorGenerateRequest,
  MotivatorGenerateResponse,
} from "../../types/api";

// Health & Info
export const getHealth = async (): Promise<HealthResponse> => {
  const response = await apiClient.get<HealthResponse>("/health");
  return response.data;
};

export const getApiInfo = async (): Promise<ApiRootResponse> => {
  const response = await apiClient.get<ApiRootResponse>("/");
  return response.data;
};

// Generation Endpoints
export const generateAd = async (params: {
  niche: Niche;
  num_images: number;
  image_model?: string | null;
  target_audience?: string | null;
  offer?: string | null;
  use_trending?: boolean;
  trending_context?: string | null;
}): Promise<GenerateResponse> => {
  const response = await apiClient.post<GenerateResponse>("/generate", params);
  return response.data;
};

export const generateBatch = async (params: {
  niche: Niche;
  count: number;
  image_model?: string | null;
  method?: "standard" | "matrix" | null;
  target_audience?: string | null;
  offer?: string | null;
}): Promise<BatchResponse> => {
  const response = await apiClient.post<BatchResponse>("/generate/batch", params);
  return response.data;
};

// Matrix Endpoints
export const generateMatrixAd = async (params: {
  niche: Niche;
  angle_key?: string | null;
  concept_key?: string | null;
  custom_angle?: string | null;
  custom_concept?: string | null;
  num_images: number;
  image_model?: string | null;
  target_audience?: string | null;
  offer?: string | null;
  core_motivator?: string;
}): Promise<MatrixGenerateResponse> => {
  const response = await apiClient.post<MatrixGenerateResponse>("/matrix/generate", params);
  return response.data;
};

export const generateMotivators = async (
  params: MotivatorGenerateRequest
): Promise<MotivatorGenerateResponse> => {
  const response = await apiClient.post<MotivatorGenerateResponse>(
    "/api/motivator/generate",
    { ...params, count: params.count ?? 6 }
  );
  return response.data;
};

// Extensive Endpoints (async job pattern to avoid connection timeout on HF Spaces)
const EXTENSIVE_POLL_INTERVAL_MS = 5500;
const EXTENSIVE_POLL_TIMEOUT_MS = 20 * 60 * 1000; // 20 minutes

export const getExtensiveJobStatus = async (jobId: string): Promise<{ job_id: string; status: "running" | "completed" | "failed"; error?: string }> => {
  const response = await apiClient.get(`/extensive/status/${jobId}`);
  return response.data;
};

export const getExtensiveJobResult = async (jobId: string): Promise<BatchResponse> => {
  const response = await apiClient.get<BatchResponse>(`/extensive/result/${jobId}`);
  return response.data;
};

export const generateExtensiveAd = async (params: {
  niche: Niche;
  custom_niche?: string | null;
  target_audience?: string | null;
  offer?: string | null;
  num_images: number;
  image_model?: string | null;
  num_strategies: number;
}): Promise<BatchResponse> => {
  const requestParams = {
    niche: params.niche,
    num_images: params.num_images || 1,
    num_strategies: params.num_strategies || 1,
    ...(params.custom_niche && { custom_niche: params.custom_niche }),
    ...(params.target_audience && { target_audience: params.target_audience }),
    ...(params.offer && { offer: params.offer }),
    ...(params.image_model && { image_model: params.image_model }),
  };
  const response = await apiClient.post<{ job_id: string; message?: string }>("/extensive/generate", requestParams);
  const jobId = response.data?.job_id;
  if (!jobId) {
    throw new Error("Server did not return a job ID");
  }
  const deadline = Date.now() + EXTENSIVE_POLL_TIMEOUT_MS;
  for (;;) {
    const status = await getExtensiveJobStatus(jobId);
    if (status.status === "completed") {
      return getExtensiveJobResult(jobId);
    }
    if (status.status === "failed") {
      throw new Error(status.error || "Extensive generation failed");
    }
    if (Date.now() >= deadline) {
      throw new Error("Extensive generation timed out. Try fewer strategies or images.");
    }
    await new Promise((r) => setTimeout(r, EXTENSIVE_POLL_INTERVAL_MS));
  }
};

export const generateTestingMatrix = async (params: {
  niche: Niche;
  angle_count: number;
  concept_count: number;
  strategy: "balanced" | "top_performers" | "diverse";
}): Promise<TestingMatrixResponse> => {
  const response = await apiClient.post<TestingMatrixResponse>("/matrix/testing", params);
  return response.data;
};

export const getAllAngles = async (): Promise<AnglesResponse> => {
  const response = await apiClient.get<AnglesResponse>("/matrix/angles");
  return response.data;
};

export const getAllConcepts = async (): Promise<ConceptsResponse> => {
  const response = await apiClient.get<ConceptsResponse>("/matrix/concepts");
  return response.data;
};

export const getAngle = async (angleKey: string): Promise<AngleInfo> => {
  const response = await apiClient.get<AngleInfo>(`/matrix/angle/${angleKey}`);
  return response.data;
};

export const getConcept = async (conceptKey: string): Promise<ConceptInfo> => {
  const response = await apiClient.get<ConceptInfo>(`/matrix/concept/${conceptKey}`);
  return response.data;
};

export const getCompatibleConcepts = async (angleKey: string): Promise<CompatibleConceptsResponse> => {
  const response = await apiClient.get<CompatibleConceptsResponse>(`/matrix/compatible/${angleKey}`);
  return response.data;
};

// Refine custom angle or concept using AI
export const refineCustomAngleOrConcept = async (params: {
  text: string;
  type: "angle" | "concept";
  niche: Niche;
  goal?: string;
}): Promise<{ status: string; type: "angle" | "concept"; refined?: any; error?: string }> => {
  const response = await apiClient.post("/matrix/refine-custom", params);
  return response.data;
};

// Database Endpoints
export const getDbStats = async (): Promise<DbStatsResponse> => {
  const response = await apiClient.get<DbStatsResponse>("/db/stats");
  return response.data;
};

export const listAds = async (params?: {
  niche?: string | null;
  generation_method?: string | null;
  limit?: number;
  offset?: number;
}): Promise<AdsListResponse> => {
  const response = await apiClient.get<AdsListResponse>("/db/ads", { params });
  return response.data;
};

export const getAd = async (adId: string): Promise<AdCreativeDB> => {
  const response = await apiClient.get<AdCreativeDB>(`/db/ad/${adId}`);
  return response.data;
};

export const deleteAd = async (adId: string): Promise<{ success: boolean; deleted_id: string }> => {
  const response = await apiClient.delete<{ success: boolean; deleted_id: string }>(`/db/ad/${adId}`);
  return response.data;
};

// Strategies
export const getStrategies = async (niche: Niche): Promise<any> => {
  const response = await apiClient.get(`/strategies/${niche}`);
  return response.data;
};

// Image Correction Endpoints
export const correctImage = async (params: {
  image_id: string;
  image_url?: string;
  user_instructions?: string;
  auto_analyze?: boolean;
}): Promise<ImageCorrectResponse> => {
  const response = await apiClient.post<ImageCorrectResponse>("/api/correct", params);
  return response.data;
};

// Image Regeneration Endpoints
export const regenerateImage = async (params: {
  image_id: string;
  image_model?: string | null;
  preview_only?: boolean;
}): Promise<ImageRegenerateResponse> => {
  const response = await apiClient.post<ImageRegenerateResponse>("/api/regenerate", {
    ...params,
    preview_only: params.preview_only ?? true, // Default to preview mode
  });
  return response.data;
};

// Confirm Image Selection after Regeneration
export const confirmImageSelection = async (params: {
  image_id: string;
  selection: "new" | "original";
  new_image_url?: string | null;
  new_r2_url?: string | null;
  new_filename?: string | null;
  new_model?: string | null;
  new_seed?: number | null;
}): Promise<{ status: string; message: string; selection: string; new_image_url?: string }> => {
  const response = await apiClient.post("/api/regenerate/confirm", params);
  return response.data;
};

// Get Available Image Models
export const getImageModels = async (): Promise<ModelsListResponse> => {
  const response = await apiClient.get<ModelsListResponse>("/api/models");
  return response.data;
};

// Auth Endpoints
export const login = async (username: string, password: string): Promise<LoginResponse> => {
  const response = await apiClient.post<LoginResponse>("/auth/login", {
    username,
    password,
  });
  return response.data;
};

// Edit Ad Copy Endpoint
export const editAdCopy = async (params: {
  ad_id: string;
  field: "title" | "headline" | "primary_text" | "description" | "body_story" | "cta";
  value: string;
  mode: "manual" | "ai";
  user_suggestion?: string;
}): Promise<{ edited_value: string; success: boolean }> => {
  const response = await apiClient.post<{ edited_value: string; success: boolean }>("/db/ad/edit", params);
  return response.data;
};

// Download Image Endpoint (proxy to avoid CORS)
export const downloadImageProxy = async (params: {
  image_url?: string;
  image_id?: string;
}): Promise<Blob> => {
  const response = await apiClient.get("/api/download-image", {
    params,
    responseType: "blob",
  });
  return response.data as Blob;
};

// Creative Modifier Endpoints

// Upload a creative image
export const uploadCreative = async (file: File): Promise<FileUploadResponse> => {
  const formData = new FormData();
  formData.append("file", file);

  const response = await apiClient.post<FileUploadResponse>(
    "/api/creative/upload",
    formData,
    {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    }
  );
  return response.data;
};

// Analyze a creative image (via URL)
export const analyzeCreativeByUrl = async (params: {
  image_url: string;
}): Promise<CreativeAnalysisResponse> => {
  const response = await apiClient.post<CreativeAnalysisResponse>(
    "/api/creative/analyze",
    params
  );
  return response.data;
};

// Analyze a creative image (via file upload)
export const analyzeCreativeByFile = async (
  file: File
): Promise<CreativeAnalysisResponse> => {
  const formData = new FormData();
  formData.append("file", file);

  const response = await apiClient.post<CreativeAnalysisResponse>(
    "/api/creative/analyze/upload",
    formData,
    {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    }
  );
  return response.data;
};

// Modify a creative with new angle/concept
export const modifyCreative = async (params: {
  image_url: string;
  analysis?: CreativeAnalysisData | null;
  angle?: string;
  concept?: string;
  mode: ModificationMode;
  image_model?: string | null;
  user_prompt?: string;
}): Promise<CreativeModifyResponse> => {
  const response = await apiClient.post<CreativeModifyResponse>(
    "/api/creative/modify",
    params
  );
  return response.data;
};

// Bulk Export Endpoint
export const exportBulkAds = async (adIds: string[]): Promise<Blob> => {
  const response = await apiClient.post(
    "/api/export/bulk",
    { ad_ids: adIds },
    { responseType: "blob" }
  );
  return response.data as Blob;
};