|
"use server" |
|
|
|
import Gorgon from "@gorgonjs/gorgon" |
|
|
|
import { RenderedScene } from "./types" |
|
import { Engine, EngineType } from "./engines" |
|
|
|
|
|
|
|
const apiUrl = process.env.RENDERING_ENGINE_API |
|
|
|
|
|
const cacheDurationInSec = 30 * 60 |
|
|
|
export async function render({ |
|
prompt, |
|
actionnables = [], |
|
engine, |
|
}: { |
|
prompt: string |
|
actionnables: string[] |
|
engine: Engine |
|
}) { |
|
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..`) |
|
} |
|
if (!Array.isArray(actionnables) || !actionnables.length) { |
|
console.error(`cannot call the rendering API without actionnables, aborting..`) |
|
throw new Error(`cannot call the rendering API without actionnables, aborting..`) |
|
} |
|
|
|
const nbFrames = engine.type === "video" ? 8 : 1 |
|
|
|
const cacheKey = `render/${JSON.stringify({ prompt, actionnables, nbFrames, type: engine.type })}` |
|
|
|
return await Gorgon.get(cacheKey, async () => { |
|
let defaulResult: RenderedScene = { |
|
assetUrl: "", |
|
maskBase64: "", |
|
error: "", |
|
segments: [] |
|
} |
|
|
|
try { |
|
console.log(`calling ${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, |
|
nbSteps: 20, |
|
actionnables, |
|
segmentation: "firstframe", |
|
}), |
|
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) |
|
Gorgon.clear(cacheKey) |
|
return defaulResult |
|
} |
|
}, cacheDurationInSec * 1000) |
|
} |