|
"use server" |
|
|
|
import { RenderRequest, RenderedScene } from "@/types" |
|
|
|
|
|
|
|
const apiUrl = process.env.RENDERING_ENGINE_API |
|
|
|
const cacheDurationInSec = 30 * 60 |
|
|
|
export async function newRender({ |
|
prompt, |
|
// negativePrompt, |
|
width, |
|
height |
|
}: { |
|
prompt: string |
|
// negativePrompt: string[] |
|
width: number |
|
height: number |
|
}) { |
|
console.log(`newRender(${prompt})`) |
|
if (!prompt) { |
|
console.error(`cannot call the rendering API without a prompt, aborting..`) |
|
throw new Error(`cannot call the rendering API without a prompt, aborting..`) |
|
} |
|
|
|
let defaulResult: RenderedScene = { |
|
renderId: "", |
|
status: "error", |
|
assetUrl: "", |
|
alt: prompt || "", |
|
maskUrl: "", |
|
error: "failed to fetch the data", |
|
segments: [] |
|
} |
|
|
|
|
|
try { |
|
console.log(`calling POST ${apiUrl}/render with prompt: ${prompt}`) |
|
|
|
const res = await fetch(`${apiUrl}/render`, { |
|
method: "POST", |
|
headers: { |
|
Accept: "application/json", |
|
"Content-Type": "application/json", |
|
|
|
}, |
|
body: JSON.stringify({ |
|
prompt, |
|
|
|
nbFrames: 1, |
|
nbSteps: 25, |
|
actionnables: [], |
|
segmentation: "disabled", |
|
width, |
|
height, |
|
|
|
|
|
|
|
|
|
|
|
upscalingFactor: 1, |
|
|
|
|
|
analyze: false, |
|
|
|
cache: "ignore" |
|
} as Partial<RenderRequest>), |
|
cache: 'no-store', |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (res.status !== 200) { |
|
|
|
throw new Error('Failed to fetch data') |
|
} |
|
|
|
const response = (await res.json()) as RenderedScene |
|
|
|
return response |
|
} catch (err) { |
|
console.error(err) |
|
return defaulResult |
|
} |
|
} |
|
|
|
export async function getRender(renderId: string) { |
|
if (!renderId) { |
|
console.error(`cannot call the rendering API without a renderId, aborting..`) |
|
throw new Error(`cannot call the rendering API without a renderId, aborting..`) |
|
} |
|
|
|
let defaulResult: RenderedScene = { |
|
renderId: "", |
|
status: "error", |
|
assetUrl: "", |
|
alt: "", |
|
maskUrl: "", |
|
error: "failed to fetch the data", |
|
segments: [] |
|
} |
|
|
|
try { |
|
|
|
const res = await fetch(`${apiUrl}/render/${renderId}`, { |
|
method: "GET", |
|
headers: { |
|
Accept: "application/json", |
|
"Content-Type": "application/json", |
|
|
|
}, |
|
cache: 'no-store', |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (res.status !== 200) { |
|
|
|
throw new Error('Failed to fetch data') |
|
} |
|
|
|
const response = (await res.json()) as RenderedScene |
|
|
|
return response |
|
} catch (err) { |
|
console.error(err) |
|
|
|
return defaulResult |
|
} |
|
|
|
|
|
} |
|
|