Files
EmailManager/services/geminiService.ts
2025-12-10 12:10:20 +01:00

53 lines
1.6 KiB
TypeScript

import { GoogleGenAI } from "@google/genai";
const getClient = () => {
const apiKey = process.env.API_KEY;
if (!apiKey) return null;
return new GoogleGenAI({ apiKey });
};
export const generateEmailContent = async (
prompt: string,
section: 'header' | 'body' | 'footer'
): Promise<string> => {
const client = getClient();
if (!client) {
throw new Error("API Key not found");
}
// Model selection based on text task complexity
const modelId = 'gemini-2.5-flash';
const systemInstruction = `
You are an expert email marketing developer.
You are generating HTML content for an email template ${section}.
RULES:
1. Return ONLY the HTML code. Do not include markdown ticks, 'html' labels, or explanations.
2. Use inline CSS styles for compatibility.
3. Use the Handlebars syntax for placeholders: {{variable_name}}.
4. If the user asks for a specific variable, format it correctly.
5. For Header: Include logo placeholders or standard menu links if asked.
6. For Footer: Include unsubscribe links and copyright info if asked.
7. For Body: Focus on clean, readable content.
`;
try {
const response = await client.models.generateContent({
model: modelId,
contents: prompt,
config: {
systemInstruction: systemInstruction,
temperature: 0.7,
}
});
let text = response.text || "";
// Cleanup if model adds markdown despite instructions
text = text.replace(/```html/g, '').replace(/```/g, '').trim();
return text;
} catch (error) {
console.error("Gemini API Error:", error);
throw new Error("Failed to generate content");
}
};