| |
| |
| |
| |
| |
| const BACKEND_BASE_URL = typeof window !== "undefined" && window.location.port === "3000" |
| ? "http://localhost:8000" |
| : (typeof window !== "undefined" ? window.location.origin : "http://localhost:8000"); |
|
|
| const API_BASE_URL = `${BACKEND_BASE_URL}/api`; |
|
|
| export interface JobStatus { |
| job_id: string; |
| status: string; |
| stage: string; |
| } |
|
|
| export interface JobResult { |
| job_id: string; |
| status: string; |
| stage?: string; |
| assets?: { |
| image?: string; |
| depth?: string; |
| audio?: string; |
| video?: string; |
| }; |
| error?: string; |
| error_at?: string; |
| scene_description?: string; |
| } |
|
|
| function fixAssetUrl(assetUrl?: string): string | undefined { |
| if (!assetUrl) return undefined; |
| |
| if (assetUrl.startsWith("/")) { |
| return `${BACKEND_BASE_URL}${assetUrl}`; |
| } |
| |
| return assetUrl; |
| } |
|
|
| |
| |
| |
| import { getStoredApiKey } from "./keyStore"; |
|
|
| export async function generateAssets(prompt: string): Promise<{ job_id: string }> { |
| |
| const headers: Record<string, string> = { "Content-Type": "application/json" }; |
| const visitorKey = getStoredApiKey(); |
| if (visitorKey) { |
| |
| headers["X-Groq-Key"] = visitorKey; |
| } |
|
|
| const response = await fetch(`${API_BASE_URL}/generate`, { |
| method: "POST", |
| headers, |
| body: JSON.stringify({ prompt }), |
| }); |
| if (!response.ok) { |
| throw new Error("Failed to start generation"); |
| } |
| return response.json(); |
| } |
|
|
| export async function getJobStatus(jobId: string): Promise<JobStatus> { |
| const response = await fetch(`${API_BASE_URL}/status/${jobId}`); |
| if (!response.ok) { |
| throw new Error("Failed to fetch job status"); |
| } |
| return response.json(); |
| } |
|
|
| export async function getJobResult(jobId: string): Promise<JobResult> { |
| const response = await fetch(`${API_BASE_URL}/result/${jobId}`); |
| if (!response.ok) { |
| throw new Error("Failed to fetch job result"); |
| } |
| const data = await response.json(); |
| |
| if (data.assets) { |
| data.assets = { |
| image: fixAssetUrl(data.assets.image), |
| depth: fixAssetUrl(data.assets.depth), |
| audio: fixAssetUrl(data.assets.audio), |
| video: fixAssetUrl(data.assets.video), |
| }; |
| } |
| return data; |
| } |
|
|
| export async function healthCheck(): Promise<{ status: string; timestamp: string }> { |
| const response = await fetch(`${API_BASE_URL}/health`); |
| if (!response.ok) { |
| throw new Error("Health check failed"); |
| } |
| return response.json(); |
| } |
|
|