import { NextRequest, NextResponse } from 'next/server'; import { MetaCrawlClient } from '@/lib/providers/meta-crawl-client'; /** * POST /api/meta/video * * Generate a video from a text prompt using Meta AI. * Video generation takes 30-60+ seconds, so this endpoint may take a while. */ export async function POST(req: NextRequest) { try { const { prompt, cookies: clientCookies } = await req.json(); if (!prompt) { return NextResponse.json({ error: "Prompt is required" }, { status: 400 }); } // Get cookies from request body or cookie header let cookieString = clientCookies || req.cookies.get('meta_cookies')?.value; if (!cookieString) { return NextResponse.json( { error: "Meta AI cookies not found. Please configure settings." }, { status: 401 } ); } console.log(`[Meta Video API] Starting video generation for prompt: "${prompt.substring(0, 50)}..."`); const client = new MetaCrawlClient(); // Check if crawl4ai service is available const isHealthy = await client.healthCheck(); if (!isHealthy) { return NextResponse.json( { error: "Meta AI video service is not available. Make sure crawl4ai is running." }, { status: 503 } ); } // Generate video - this can take 30-60+ seconds const result = await client.generateVideo(prompt, cookieString); if (!result.success || result.videos.length === 0) { throw new Error(result.error || "No videos generated"); } console.log(`[Meta Video API] Successfully generated ${result.videos.length} video(s)`); return NextResponse.json({ success: true, videos: result.videos, conversation_id: result.conversation_id }); } catch (error: unknown) { const err = error as Error; console.error("[Meta Video API] Error:", err.message); const msg = err.message || ""; const isAuthError = msg.includes("401") || msg.includes("403") || msg.includes("auth") || msg.includes("cookies") || msg.includes("expired"); return NextResponse.json( { error: err.message || "Video generation failed" }, { status: isAuthError ? 401 : 500 } ); } }