feat: Google Gemini image generation for project covers
Schema:
- SystemSettings: geminiApiKey, geminiModel, imageProvider fields
- imageProvider: "dalle" (default) or "gemini"
Gemini Client (packages/api/src/gemini-client.ts):
- Direct HTTP call to Gemini REST API with responseModalities: [TEXT, IMAGE]
- Returns base64 data URL
- Error parsing with user-friendly messages
Router (project.ts):
- generateCover: routes to DALL-E or Gemini based on imageProvider setting
- New isImageGenConfigured query returning { configured, provider }
Admin UI (SystemSettingsClient.tsx):
- "Image Generation" section with provider radio buttons (DALL-E / Gemini)
- Conditional fields: DALL-E config or Gemini API key + model
- Separate save button for image settings
Security:
- geminiApiKey sanitized in audit logs (SENSITIVE_FIELDS)
- API key stored server-side only, never sent to client
Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
type GeminiSettings = {
|
||||
geminiApiKey?: string | null;
|
||||
geminiModel?: string | null;
|
||||
};
|
||||
|
||||
/** Returns true if the settings have a Gemini API key configured. */
|
||||
export function isGeminiConfigured(settings: GeminiSettings | null | undefined): boolean {
|
||||
return !!settings?.geminiApiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an image using the Google Gemini API.
|
||||
* @returns A base64 data URL of the generated image.
|
||||
*/
|
||||
export async function generateGeminiImage(
|
||||
apiKey: string,
|
||||
prompt: string,
|
||||
model = "gemini-2.0-flash-preview-image-generation",
|
||||
): Promise<string> {
|
||||
const fullPrompt = `Generate a professional, cinematic cover image for a 3D production project. ${prompt}`;
|
||||
|
||||
const response = await fetch(
|
||||
`https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
contents: [{ parts: [{ text: fullPrompt }] }],
|
||||
generationConfig: { responseModalities: ["TEXT", "IMAGE"] },
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const body = await response.text();
|
||||
let msg = body;
|
||||
try {
|
||||
const parsed = JSON.parse(body) as { error?: { message?: string } };
|
||||
if (parsed.error?.message) msg = parsed.error.message;
|
||||
} catch {
|
||||
/* keep raw */
|
||||
}
|
||||
throw new Error(`HTTP ${response.status}: ${msg}`);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as {
|
||||
candidates?: Array<{
|
||||
content?: {
|
||||
parts?: Array<{
|
||||
inlineData?: { data: string; mimeType: string };
|
||||
text?: string;
|
||||
}>;
|
||||
};
|
||||
}>;
|
||||
};
|
||||
|
||||
const imagePart = data.candidates?.[0]?.content?.parts?.find(
|
||||
(p) => p.inlineData,
|
||||
);
|
||||
|
||||
if (!imagePart?.inlineData?.data) {
|
||||
throw new Error("No image data returned from Gemini");
|
||||
}
|
||||
|
||||
const base64 = imagePart.inlineData.data;
|
||||
const mimeType = imagePart.inlineData.mimeType ?? "image/png";
|
||||
return `data:${mimeType};base64,${base64}`;
|
||||
}
|
||||
|
||||
/** Turns Gemini API errors into actionable human-readable messages. */
|
||||
export function parseGeminiError(err: unknown): string {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
const lower = msg.toLowerCase();
|
||||
|
||||
if (lower.includes("400") || lower.includes("invalid")) {
|
||||
return "Invalid request — check the Gemini model name and prompt.";
|
||||
}
|
||||
if (lower.includes("401") || lower.includes("unauthorized") || lower.includes("api_key_invalid") || lower.includes("api key not valid")) {
|
||||
return "Invalid API key — make sure you copied it correctly from Google AI Studio.";
|
||||
}
|
||||
if (lower.includes("403") || lower.includes("forbidden") || lower.includes("permission")) {
|
||||
return "Access denied — your API key may not have permission to use image generation.";
|
||||
}
|
||||
if (lower.includes("404") || lower.includes("not found")) {
|
||||
return "Model not found — verify the Gemini model name is correct.";
|
||||
}
|
||||
if (lower.includes("429") || lower.includes("rate limit") || lower.includes("quota")) {
|
||||
return "Rate limit or quota exceeded — wait a moment and try again.";
|
||||
}
|
||||
if (lower.includes("econnrefused") || lower.includes("enotfound") || lower.includes("fetch failed")) {
|
||||
return "Cannot reach the Gemini API — check your network connection.";
|
||||
}
|
||||
return msg.replace(/^Error: /, "").slice(0, 300);
|
||||
}
|
||||
Reference in New Issue
Block a user