import { hostedReadiness, studioDatabase } from '@/app/lib/studio-config'; import { authOrigin, getCustomerUser } from '@/app/lib/lumen-auth'; import { resolveImageModel } from '@/app/lib/image-models'; import { cleanUpload, MAX_PIXELS, MAX_PROMPT, MAX_UPLOAD_BYTES, rawUpload, readStudioForm, StudioError } from '@/app/lib/studio-media'; import { feedbackKey, studioFeedback, studioGenerate, studioModelKey } from '@/app/lib/studio-providers'; import { reserveStudioCall, studioLimits } from '@/app/lib/studio-quota'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; export const maxDuration = 75; const headers = { 'cache-control': 'private, no-store', 'x-content-type-options': 'nosniff' }; function unavailable() { return new StudioError('studio_unavailable', 503, 'Hosted studio generation isn’t available right now. You can still work with your own image.'); } function outputError(error: unknown) { const safe = error instanceof StudioError ? error : unavailable(); return Response.json({ error: safe.message, code: safe.code }, { status: safe.status, headers }); } export async function GET() { const signedIn = Boolean(await getCustomerUser().catch(() => null)); return Response.json({ signedIn, ...await hostedReadiness(), maxUploadBytes: MAX_UPLOAD_BYTES, maxPixels: MAX_PIXELS, maxPromptLength: MAX_PROMPT }, { headers }); } export async function POST(req: Request) { try { const canonical = authOrigin(); if (req.headers.get('origin') !== canonical || new URL(req.url).origin !== canonical) throw new StudioError('invalid_origin', 403, 'Open the studio directly to send this request.'); const user = await getCustomerUser().catch(() => null); if (!user) throw new StudioError('sign_in', 401, 'Sign in to use the hosted image models.'); const limits = studioLimits(); if (!limits) throw unavailable(); const content = await readStudioForm(req); const action = content.get('action'); if (action !== 'generate' && action !== 'revise' && action !== 'feedback') { throw new StudioError('invalid_request', 400, 'Choose generate, revise, or feedback.'); } const model = resolveImageModel(content.get('model')); if (action !== 'feedback' && !model) throw new StudioError('invalid_model', 400, 'Choose one of the studio models.'); const key = action === 'feedback' ? feedbackKey() : model && studioModelKey(model); if (!key) throw unavailable(); const promptEntry = content.get('prompt'); if (promptEntry !== null && typeof promptEntry !== 'string') { throw new StudioError('invalid_prompt', 400, 'Enter a text prompt.'); } const prompt = promptEntry?.trim() ?? ''; if (prompt.length > MAX_PROMPT || (!prompt && action !== 'feedback')) { throw new StudioError('invalid_prompt', 400, 'Describe what you want in 1–1,200 characters.'); } const source = content.get('image'); if (typeof source === 'string' || (action !== 'generate' && !source)) { throw new StudioError('invalid_image', 400, 'Choose an uploaded image first.'); } const raw = source ? await rawUpload(source) : undefined; const db = studioDatabase(); if (!await reserveStudioCall(db, user.id, limits)) { throw new StudioError('studio_limit', 429, 'The free studio’s usage limit has been reached. Your local image is still available.'); } const image = raw ? await cleanUpload(raw) : undefined; if (action === 'feedback' && image) return Response.json({ feedback: await studioFeedback(image, prompt, key) }, { headers }); if (!model) throw unavailable(); const result = await studioGenerate({ model, prompt, image }, key); return new Response(new Uint8Array(result.bytes), { headers: { ...headers, 'content-type': result.mime } }); } catch (error) { return outputError(error); } }