File size: 3,828 Bytes
b8b98dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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'],
        };
    }
}