sofia-cloud / src /app /api /generate /video /route.ts
Gmagl
feat: complete Telegram AI Girlfriend mechanics & deduplication
b8b98dd
Raw
History Blame Contribute Delete
3.72 kB
import { NextRequest, NextResponse } from "next/server";
import ZAI from "z-ai-web-dev-sdk";
import { db } from "@/lib/db";
const VIDEO_CENSOR_RULES: Record<string, string> = {
youtube: "family friendly, no violence, no nudity",
tiktok: "age appropriate, no dangerous content",
instagram: "community guidelines compliant",
general: "safe for work"
};
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { prompt, optimizedPrompt, platform = "general", style = "cinematic" } = body;
if (!prompt && !optimizedPrompt) {
return NextResponse.json({ success: false, error: "Se requiere un prompt" }, { status: 400 });
}
let finalPrompt = optimizedPrompt || prompt;
// Inject Character Visual Persona
const character = await db.character.findFirst({ where: { isActive: true } });
if (character?.styleDescription) {
finalPrompt = `${character.styleDescription}. ${finalPrompt}`;
}
const censorRule = VIDEO_CENSOR_RULES[platform] || VIDEO_CENSOR_RULES.general;
// Simulate TikTokAIVideoGenerator rules: focus on vertical, fast-paced, TTS-friendly hook
let videoStyleFlags = "short clip";
if (platform === "tiktok" || platform === "instagram") {
videoStyleFlags = "vertical 9:16 aspect ratio, fast-paced, engaging visual hook, trendy lighting";
}
// Skip censorship for private/exclusive monetization platforms
const uncensoredPlatforms = ["onlyfans", "fansly", "telegram-uncensored", "patreon-uncensored"];
if (uncensoredPlatforms.includes(platform)) {
finalPrompt = `${finalPrompt}. ${videoStyleFlags}`;
} else {
finalPrompt = `${finalPrompt}. ${censorRule}. ${videoStyleFlags}`;
}
const contentRecord = await db.content.create({
data: {
type: "video",
title: prompt.substring(0, 50),
description: prompt,
prompt: prompt,
optimizedPrompt: finalPrompt,
platform: platform,
characterId: character?.id || null,
status: "processing"
}
});
try {
const zai = await ZAI.create();
const response = await (zai as any).videos.generations.create({ prompt: finalPrompt });
const videoData = response.data?.[0];
if (!videoData) throw new Error("No se recibio video");
await db.content.update({
where: { id: contentRecord.id },
data: { status: "completed", filePath: "generated" }
});
await db.agentTask.create({
data: { type: "generate_video", status: "completed", input: prompt, output: "Video generado", completedAt: new Date() }
});
return NextResponse.json({ success: true, video: { id: contentRecord.id, prompt: finalPrompt, platform, style } });
} catch (genError) {
await db.content.update({ where: { id: contentRecord.id }, data: { status: "failed" } });
return NextResponse.json({ success: false, error: String(genError) }, { status: 500 });
}
} catch (error) {
return NextResponse.json({ success: false, error: "Error interno" }, { status: 500 });
}
}
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const platform = searchParams.get("platform");
const limit = parseInt(searchParams.get("limit") || "20");
const where: Record<string, any> = { type: "video" };
if (platform) where.platform = platform;
const videos = await db.content.findMany({ where, orderBy: { createdAt: "desc" }, take: limit });
return NextResponse.json({ success: true, videos, total: videos.length });
} catch {
return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
}
}