forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: add Gemini provider support for image generation #7945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { z } from "zod" | ||
|
|
||
| /** | ||
| * Image Generation Provider | ||
| */ | ||
| export const imageGenerationProviders = ["openrouter", "gemini"] as const | ||
| export const imageGenerationProviderSchema = z.enum(imageGenerationProviders) | ||
| export type ImageGenerationProvider = z.infer<typeof imageGenerationProviderSchema> | ||
|
|
||
| /** | ||
| * Image Generation Model Info | ||
| */ | ||
| export interface ImageGenerationModelInfo { | ||
| provider: ImageGenerationProvider | ||
| modelId: string | ||
| label: string | ||
| supportsEditMode?: boolean // Whether the model supports image editing (text + image input) | ||
| maxInputSize?: number // Maximum input image size in MB | ||
| outputFormats?: string[] // Supported output formats | ||
| } | ||
|
|
||
| /** | ||
| * Image Generation Models by Provider | ||
| */ | ||
| export const IMAGE_GENERATION_MODELS: Record<ImageGenerationProvider, ImageGenerationModelInfo[]> = { | ||
| openrouter: [ | ||
| { | ||
| provider: "openrouter", | ||
| modelId: "google/gemini-2.5-flash-image-preview", | ||
| label: "Gemini 2.5 Flash Image Preview", | ||
| supportsEditMode: true, | ||
| outputFormats: ["png", "jpeg"], | ||
| }, | ||
| { | ||
| provider: "openrouter", | ||
| modelId: "google/gemini-2.5-flash-image-preview:free", | ||
| label: "Gemini 2.5 Flash Image Preview (Free)", | ||
| supportsEditMode: true, | ||
| outputFormats: ["png", "jpeg"], | ||
| }, | ||
| ], | ||
| gemini: [ | ||
| { | ||
| provider: "gemini", | ||
| modelId: "gemini-2.5-flash-image-preview", | ||
| label: "Gemini 2.5 Flash Image Preview", | ||
| supportsEditMode: true, | ||
| outputFormats: ["png", "jpeg"], | ||
| }, | ||
| ], | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to get all models for a specific provider | ||
| */ | ||
| export function getImageGenerationModelsForProvider(provider: ImageGenerationProvider): ImageGenerationModelInfo[] { | ||
| return IMAGE_GENERATION_MODELS[provider] || [] | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to get all available image generation models | ||
| */ | ||
| export function getAllImageGenerationModels(): ImageGenerationModelInfo[] { | ||
| return Object.values(IMAGE_GENERATION_MODELS).flat() | ||
| } | ||
|
|
||
| /** | ||
| * Image Generation Result | ||
| */ | ||
| export interface ImageGenerationResult { | ||
| success: boolean | ||
| imageData?: string // Base64 encoded image data URL | ||
| imageFormat?: string // Format of the generated image (png, jpeg, etc.) | ||
| error?: string | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,8 @@ import { getReadablePath } from "../../utils/path" | |
| import { isPathOutsideWorkspace } from "../../utils/pathUtils" | ||
| import { EXPERIMENT_IDS, experiments } from "../../shared/experiments" | ||
| import { OpenRouterHandler } from "../../api/providers/openrouter" | ||
|
|
||
| // Hardcoded list of image generation models for now | ||
| const IMAGE_GENERATION_MODELS = ["google/gemini-2.5-flash-image", "openai/gpt-5-image", "openai/gpt-5-image-mini"] | ||
| import { GeminiHandler } from "../../api/providers/gemini" | ||
| import { ImageGenerationProvider, getImageGenerationModelsForProvider } from "@roo-code/types" | ||
|
|
||
| export async function generateImageTool( | ||
| cline: Task, | ||
|
|
@@ -128,25 +127,60 @@ export async function generateImageTool( | |
| // Check if file is write-protected | ||
| const isWriteProtected = cline.rooProtectedController?.isWriteProtected(relPath) || false | ||
|
|
||
| // Get OpenRouter API key from global settings (experimental image generation) | ||
| const openRouterApiKey = state?.openRouterImageApiKey | ||
| // Get the selected provider from settings (default to openrouter) | ||
| const selectedProvider = (state?.imageGenerationProvider || "openrouter") as ImageGenerationProvider | ||
|
|
||
| if (!openRouterApiKey) { | ||
| await cline.say( | ||
| "error", | ||
| "OpenRouter API key is required for image generation. Please configure it in the Image Generation experimental settings.", | ||
| ) | ||
| pushToolResult( | ||
| formatResponse.toolError( | ||
| // Get selected model from settings based on provider | ||
| let selectedModel: string | ||
| let apiKey: string | undefined | ||
|
|
||
| if (selectedProvider === "openrouter") { | ||
| apiKey = state?.openRouterImageApiKey | ||
| if (!apiKey) { | ||
| await cline.say( | ||
| "error", | ||
| "OpenRouter API key is required for image generation. Please configure it in the Image Generation experimental settings.", | ||
| ), | ||
| ) | ||
| ) | ||
| pushToolResult( | ||
| formatResponse.toolError( | ||
| "OpenRouter API key is required for image generation. Please configure it in the Image Generation experimental settings.", | ||
| ), | ||
| ) | ||
| return | ||
| } | ||
| // Get selected model or use default for OpenRouter | ||
| const models = getImageGenerationModelsForProvider("openrouter") | ||
| selectedModel = | ||
| state?.openRouterImageGenerationSelectedModel || | ||
| (models[0]?.modelId ?? "google/gemini-2.5-flash-image-preview") | ||
| } else if (selectedProvider === "gemini") { | ||
| // For Gemini, we can use the existing Gemini API key from the provider settings | ||
| // Check for a dedicated image generation API key first, then fall back to the provider's API key | ||
| apiKey = | ||
| state?.geminiImageApiKey || | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fallback to the main Gemini API key is a nice touch for user convenience. However, could we add a comment here explaining this fallback behavior so future maintainers understand the logic? |
||
| (state?.apiConfiguration?.apiProvider === "gemini" ? state?.apiConfiguration?.geminiApiKey : undefined) | ||
| if (!apiKey) { | ||
| await cline.say( | ||
| "error", | ||
| "Gemini API key is required for image generation. Please configure it in the Image Generation experimental settings or in the Gemini provider settings.", | ||
| ) | ||
| pushToolResult( | ||
| formatResponse.toolError( | ||
| "Gemini API key is required for image generation. Please configure it in the Image Generation experimental settings or in the Gemini provider settings.", | ||
| ), | ||
| ) | ||
| return | ||
| } | ||
| // Get selected model or use default for Gemini | ||
| const models = getImageGenerationModelsForProvider("gemini") | ||
| selectedModel = | ||
| state?.geminiImageGenerationSelectedModel || (models[0]?.modelId ?? "gemini-2.5-flash-image-preview") | ||
| } else { | ||
| await cline.say("error", `Unsupported image generation provider: ${selectedProvider}`) | ||
| pushToolResult(formatResponse.toolError(`Unsupported image generation provider: ${selectedProvider}`)) | ||
| return | ||
| } | ||
|
|
||
| // Get selected model from settings or use default | ||
| const selectedModel = state?.openRouterImageGenerationSelectedModel || IMAGE_GENERATION_MODELS[0] | ||
|
|
||
| // Determine if the path is outside the workspace | ||
| const fullPath = path.resolve(cline.cwd, removeClosingTag("path", relPath)) | ||
| const isOutsideWorkspace = isPathOutsideWorkspace(fullPath) | ||
|
|
@@ -176,16 +210,28 @@ export async function generateImageTool( | |
| return | ||
| } | ||
|
|
||
| // Create a temporary OpenRouter handler with minimal options | ||
| const openRouterHandler = new OpenRouterHandler({} as any) | ||
|
|
||
| // Call the generateImage method with the explicit API key and optional input image | ||
| const result = await openRouterHandler.generateImage( | ||
| prompt, | ||
| selectedModel, | ||
| openRouterApiKey, | ||
| inputImageData, | ||
| ) | ||
| // Generate image based on provider | ||
| let result | ||
|
|
||
| if (selectedProvider === "openrouter") { | ||
| // Create a temporary OpenRouter handler with minimal options | ||
| const openRouterHandler = new OpenRouterHandler({} as any) | ||
|
|
||
| // Call the generateImage method with the explicit API key and optional input image | ||
| result = await openRouterHandler.generateImage(prompt, selectedModel, apiKey!, inputImageData) | ||
| } else if (selectedProvider === "gemini") { | ||
| // Create a temporary Gemini handler with minimal options | ||
| const geminiHandler = new GeminiHandler({ geminiApiKey: apiKey } as any) | ||
|
|
||
| // Call the generateImage method with the optional input image | ||
| result = await geminiHandler.generateImage(prompt, selectedModel, apiKey, inputImageData) | ||
| } else { | ||
| // This should not happen due to earlier check, but for type safety | ||
| result = { | ||
| success: false, | ||
| error: `Unsupported provider: ${selectedProvider}`, | ||
| } | ||
| } | ||
|
|
||
| if (!result.success) { | ||
| await cline.say("error", result.error || "Failed to generate image") | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.