Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/types/src/image-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
* Image generation model constants
*/

/**
* API method used for image generation
*/
export type ImageGenerationApiMethod = "chat_completions" | "images_api"

export interface ImageGenerationModel {
value: string
label: string
provider: ImageGenerationProvider
apiMethod?: ImageGenerationApiMethod
}

export const IMAGE_GENERATION_MODELS: ImageGenerationModel[] = [
Expand All @@ -17,6 +23,7 @@ export const IMAGE_GENERATION_MODELS: ImageGenerationModel[] = [
// Roo Code Cloud models
{ value: "google/gemini-2.5-flash-image", label: "Gemini 2.5 Flash Image", provider: "roo" },
{ value: "google/gemini-3-pro-image", label: "Gemini 3 Pro Image", provider: "roo" },
{ value: "bfl/flux-2-pro", label: "BFL Flux 2 Pro", provider: "roo", apiMethod: "images_api" },
]

/**
Expand Down
5 changes: 4 additions & 1 deletion src/api/providers/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
}

/**
* Generate an image using OpenRouter's image generation API
* Generate an image using OpenRouter's image generation API (chat completions with modalities)
* Note: OpenRouter only supports the chat completions approach, not the /images/generations endpoint
* @param prompt The text prompt for image generation
* @param model The model to use for generation
* @param apiKey The OpenRouter API key (must be explicitly provided)
Expand All @@ -456,6 +457,8 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
}

const baseURL = this.options.openRouterBaseUrl || "https://openrouter.ai/api/v1"

// OpenRouter only supports chat completions approach for image generation
return generateImageWithProvider({
baseURL,
authToken: apiKey,
Expand Down
29 changes: 25 additions & 4 deletions src/api/providers/roo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"

import { rooDefaultModelId, getApiProtocol } from "@roo-code/types"
import { rooDefaultModelId, getApiProtocol, type ImageGenerationApiMethod } from "@roo-code/types"
import { CloudService } from "@roo-code/cloud"

import type { ApiHandlerOptions, ModelRecord } from "../../shared/api"
Expand All @@ -15,7 +15,7 @@ import type { ApiHandlerCreateMessageMetadata } from "../index"
import { BaseOpenAiCompatibleProvider } from "./base-openai-compatible-provider"
import { getModels, getModelsFromCache } from "../providers/fetchers/modelCache"
import { handleOpenAIError } from "./utils/openai-error-handler"
import { generateImageWithProvider, ImageGenerationResult } from "./utils/image-generation"
import { generateImageWithProvider, generateImageWithImagesApi, ImageGenerationResult } from "./utils/image-generation"
import { t } from "../../i18n"

// Extend OpenAI's CompletionUsage to include Roo specific fields
Expand Down Expand Up @@ -273,9 +273,15 @@ export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
* @param prompt The text prompt for image generation
* @param model The model to use for generation
* @param inputImage Optional base64 encoded input image data URL
* @param apiMethod The API method to use (chat_completions or images_api)
* @returns The generated image data and format, or an error
*/
async generateImage(prompt: string, model: string, inputImage?: string): Promise<ImageGenerationResult> {
async generateImage(
prompt: string,
model: string,
inputImage?: string,
apiMethod?: ImageGenerationApiMethod,
): Promise<ImageGenerationResult> {
const sessionToken = getSessionToken()

if (!sessionToken || sessionToken === "unauthenticated") {
Expand All @@ -285,8 +291,23 @@ export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
}
}

const baseURL = `${this.fetcherBaseURL}/v1`

// Use the specified API method, defaulting to chat_completions for backward compatibility
if (apiMethod === "images_api") {
return generateImageWithImagesApi({
baseURL,
authToken: sessionToken,
model,
prompt,
inputImage,
outputFormat: "png",
})
}

// Default to chat completions approach
return generateImageWithProvider({
baseURL: `${this.fetcherBaseURL}/v1`,
baseURL,
authToken: sessionToken,
model,
prompt,
Expand Down
Loading
Loading