Spaces:
Build error
Build error
| import ZAI from "z-ai-web-dev-sdk"; | |
| import { Character } from "@prisma/client"; | |
| interface GenerateSocialPostParams { | |
| character: Character; | |
| topic?: string; | |
| platform: "instagram" | "twitter" | "tiktok" | "onlyfans"; | |
| tone?: string; | |
| imageContext?: string; // e.g. "a photo of her at the beach looking at the sunset" | |
| } | |
| interface SocialPostResult { | |
| caption: string; | |
| hook?: string; | |
| hashtags?: string[]; | |
| } | |
| export async function generateSocialPost({ | |
| character, | |
| topic, | |
| platform, | |
| tone, | |
| imageContext, | |
| }: GenerateSocialPostParams): Promise<SocialPostResult> { | |
| const zai = await ZAI.create(); | |
| // 1. Build Persona Context (Inspired by ai-virtual-persona-tools) | |
| const personaContext = ` | |
| You are acting as the AI Influencer: ${character.name}. | |
| Bio: ${character.shortBio || ""} | |
| Backstory: ${character.backstory || ""} | |
| Personality Traits: ${character.traits || ""} ${character.personality || ""} | |
| Interests: ${character.interests || ""} | |
| Catchphrases: ${character.catchphrases || ""} | |
| System Prompt Context: ${character.systemPrompt || ""} | |
| `.trim(); | |
| // 2. Build Platform Constraints (Inspired by Automated-Social-Media-Content-Generator) | |
| let platformRules = ""; | |
| switch (platform) { | |
| case "twitter": | |
| platformRules = "Keep it under 280 characters. Be witty, direct, and engaging. Use max 2 hashtags. No emojis unless it fits perfectly."; | |
| break; | |
| case "instagram": | |
| platformRules = "Write an engaging, visually descriptive caption. Include a strong hook in the first sentence. Ask a question at the end to drive comments. Use 5-10 relevant hashtags."; | |
| break; | |
| case "tiktok": | |
| platformRules = "Keep it very short and conversational. Focus on the hook. Use trending TikTok style phrasing (e.g., POV, 'not me...', etc.). Use 3-5 hashtags."; | |
| break; | |
| case "onlyfans": | |
| platformRules = "Write a suggestive, intimate, and exclusive caption. Address the reader directly as 'babe' or 'honey' if it fits the persona. Use emojis to express emotion. No hashtags."; | |
| break; | |
| } | |
| // 3. Build Actionable Prompt | |
| const actionPrompt = ` | |
| Task: Write a social media post for ${platform.toUpperCase()}. | |
| Topic/Context: ${topic ? topic : (imageContext ? `I am posting a picture of: ${imageContext}` : "A casual daily update.")} | |
| Tone: ${tone || (platform === "onlyfans" ? character.chatPremiumTone : character.chatBasicTone) || "Authentic and engaging"} | |
| Rules: | |
| ${platformRules} | |
| Output Format: | |
| Return a strictly valid JSON object with the following structure: | |
| { | |
| "hook": "The first engaging sentence (if applicable)", | |
| "caption": "The main body of the text, excluding hashtags", | |
| "hashtags": ["list", "of", "hashtags", "without", "#"] | |
| } | |
| `; | |
| try { | |
| const completion = await zai.chat.completions.create({ | |
| messages: [ | |
| { role: "system", content: personaContext }, | |
| { role: "user", content: actionPrompt } | |
| ], | |
| response_format: { type: "json_object" }, | |
| temperature: 0.8, // Slightly higher for creativity in marketing | |
| }); | |
| const responseContent = completion.choices[0]?.message?.content; | |
| if (!responseContent) throw new Error("No content generated"); | |
| const parsed: SocialPostResult = JSON.parse(responseContent); | |
| return parsed; | |
| } catch (error) { | |
| console.error("Error generating social post:", error); | |
| // Fallback simple caption on failure | |
| return { | |
| caption: `Just another day in the life of ${character.name}! ✨`, | |
| hashtags: [character.name.replace(/\s+/g, ''), 'aiinfluencer'], | |
| }; | |
| } | |
| } | |