Refactor image generation tool

This commit is contained in:
Alex Gleason committed 2025-12-07 23:40:29 -06:00
1 parent e1028e9f5e
commit 0b076d2114
4 files changed
+25 -22

No files matched your search

+5 -2
View File
@@ -194,15 +194,18 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
try {
const { provider, model } = parseProviderModel(settings.imageModel, settings.providers);
// Find the model data to get modalities
// Find the model data to determine the generation mode
const providerModel = models.find(m => m.fullId === settings.imageModel);
// Determine mode: 'chat' if model supports image output modality, otherwise 'image'
const mode = providerModel?.modalities?.includes('image') ? 'chat' : 'image';
tools.generate_image = new GenerateImageTool(
fs,
tmpPath,
provider,
model,
providerModel?.modalities,
mode,
user,
config.corsProxy
);
+1 -1
View File
@@ -170,7 +170,7 @@ When a project is first created, you (the AI) choose an appropriate template fro
Shakespeare supports AI-powered image generation. Users can configure an image model in **Settings > AI** under the "Advanced" section.
**Important Notes:**
- Only models that support image generation (with "image" in their modalities) can be used
- Models that support image generation can use either the chat completions endpoint (for models with "image" output modality) or the dedicated images generation endpoint
- If users experience issues with image generation, they should try selecting a different image model in Settings > AI
- Recommended models include gpt-image-1, dall-e-3, or similar high-quality yet affordable options
- The generate_image tool will only be available once a compatible image model is configured
+14 -14
View File
@@ -54,7 +54,7 @@ describe('GenerateImageTool', () => {
'/tmp',
mockProvider,
'dall-e-3',
undefined,
'image',
undefined,
undefined
);
@@ -70,7 +70,7 @@ describe('GenerateImageTool', () => {
'/tmp',
mockProvider,
'dall-e-3',
undefined,
'image',
undefined,
undefined
);
@@ -85,7 +85,7 @@ describe('GenerateImageTool', () => {
'/tmp',
mockProvider,
'dall-e-3',
undefined,
'image',
undefined,
undefined
);
@@ -94,13 +94,13 @@ describe('GenerateImageTool', () => {
expect(result.success).toBe(false);
});
it('should generate image with standard OpenAI provider', async () => {
it('should generate image with standard OpenAI provider using image mode', async () => {
const tool = new GenerateImageTool(
mockFS,
'/tmp',
mockProvider,
'dall-e-3',
undefined,
'image',
undefined,
undefined
);
@@ -122,7 +122,7 @@ describe('GenerateImageTool', () => {
);
});
it('should handle models with image output modality', async () => {
it('should handle models with chat mode for image generation', async () => {
const openRouterProvider: AIProvider = {
id: 'openrouter',
name: 'OpenRouter',
@@ -135,7 +135,7 @@ describe('GenerateImageTool', () => {
'/tmp',
openRouterProvider,
'flux-1.1-pro',
['image'], // Model supports image output modality
'chat', // Use chat mode for image generation
undefined,
undefined
);
@@ -175,7 +175,7 @@ describe('GenerateImageTool', () => {
'/tmp',
mockProvider,
'dall-e-3',
undefined,
'image',
undefined,
undefined
);
@@ -189,7 +189,7 @@ describe('GenerateImageTool', () => {
);
});
it('should throw error when model with image modality returns invalid response', async () => {
it('should throw error when chat mode returns invalid response', async () => {
const openRouterProvider: AIProvider = {
id: 'openrouter',
name: 'OpenRouter',
@@ -202,7 +202,7 @@ describe('GenerateImageTool', () => {
'/tmp',
openRouterProvider,
'flux-1.1-pro',
['image'], // Model supports image output modality
'chat', // Use chat mode
undefined,
undefined
);
@@ -222,7 +222,7 @@ describe('GenerateImageTool', () => {
);
});
it('should determine file extension from data URI for models with image modality', async () => {
it('should determine file extension from data URI for chat mode', async () => {
const openRouterProvider: AIProvider = {
id: 'openrouter',
name: 'OpenRouter',
@@ -235,7 +235,7 @@ describe('GenerateImageTool', () => {
'/tmp',
openRouterProvider,
'flux-1.1-pro',
['image'], // Model supports image output modality
'chat', // Use chat mode
undefined,
undefined
);
@@ -265,7 +265,7 @@ describe('GenerateImageTool', () => {
expect(result).toMatch(/^Generated image: \/tmp\/generated-\d+\.jpeg$/);
});
it('should throw error when model with image modality returns invalid data URI', async () => {
it('should throw error when chat mode returns invalid data URI', async () => {
const openRouterProvider: AIProvider = {
id: 'openrouter',
name: 'OpenRouter',
@@ -278,7 +278,7 @@ describe('GenerateImageTool', () => {
'/tmp',
openRouterProvider,
'flux-1.1-pro',
['image'], // Model supports image output modality
'chat', // Use chat mode
undefined,
undefined
);
+5 -5
View File
@@ -10,6 +10,8 @@ interface GenerateImageParams {
prompt: string;
}
export type ImageGenerationMode = 'chat' | 'image';
export class GenerateImageTool implements Tool<GenerateImageParams> {
readonly description = 'Generate an image from a text prompt and save it to the virtual filesystem';
@@ -22,7 +24,7 @@ export class GenerateImageTool implements Tool<GenerateImageParams> {
private tmpPath: string,
private provider: AIProvider,
private model: string,
private modalities: string[] | undefined,
private mode: ImageGenerationMode,
private user?: NUser,
private corsProxy?: string,
) {}
@@ -36,10 +38,8 @@ export class GenerateImageTool implements Tool<GenerateImageParams> {
let imageData: Uint8Array;
let extension = 'png';
// Check if the model supports image output modality (chat completions path)
const supportsImageOutput = this.modalities?.includes('image');
if (supportsImageOutput) {
// Use chat completions endpoint for models that support image output modality
if (this.mode === 'chat') {
const response = await client.chat.completions.create({
model: this.model,
messages: [