Add background parameter to GenerateImageTool for transparency support

The previous commit tried to enable transparency by allowing output_format: "png",
but transparency in the OpenAI Image API is controlled by a separate "background"
parameter, not the output format. Without it, the model was drawing checkerboard
patterns to visually represent transparency instead of producing actual alpha channels.

Add the background parameter ("transparent", "opaque", "auto") to the tool schema
and pass it through to the API. Update the system prompt to instruct the AI to use
both background: "transparent" and output_format: "png"/"webp" together.
This commit is contained in:
Alex Gleason committed 2026-03-17 19:22:31 -05:00
1 parent 119b28d0f5
commit 459311bd7b
2 files changed
+6 -2

No files matched your search

+2 -1
View File
@@ -192,7 +192,8 @@ Shakespeare supports AI-powered image generation. Users can configure an image m
**Important Notes:**
- Not all models support image generation. If users experience issues with image generation, they should try selecting a different image model in Settings > AI
- The generate_image tool will only be available once a compatible image model is configured
- When generating images for projects, prefer "jpeg" or "webp" output formats with compression set between 70-90 for optimal file size. Use "png" format when transparency is needed (e.g., logos, icons, overlays). You must supply the output_format parameter when calling the generate_image tool.
- When generating images for projects, prefer "jpeg" or "webp" output formats with compression set between 70-90. You must supply these parameters when calling the generate_image tool to ensure optimal image quality and file size for web usage.
- **Transparency**: To generate images with transparent backgrounds (e.g., logos, icons, overlays), set \`background: "transparent"\` and use \`output_format: "png"\` or \`output_format: "webp"\`. Both parameters must be set together — \`output_format\` alone does not enable transparency.
- **Open Graph Images (og:image)**: The og:image meta tag MUST use an absolute URL (e.g., \`https://example.com/og-image.jpg\`). Relative paths will not work for social media sharing. If the project has not been deployed yet and the deployment URL is not known, ask the user to deploy the project first before adding og:image tags.
## Working Around CORS Issues
+4 -1
View File
@@ -11,6 +11,7 @@ interface GenerateImageParams {
output_format?: "png" | "jpeg" | "webp";
output_compression?: number;
size?: "auto" | "1024x1024" | "1536x1024" | "1024x1536" | "256x256" | "512x512" | "1792x1024" | "1024x1792";
background?: "transparent" | "opaque" | "auto";
}
export type ImageGenerationMode = 'chat' | 'image';
@@ -23,6 +24,7 @@ export class GenerateImageTool implements Tool<GenerateImageParams> {
output_format: z.enum(["png", "jpeg", "webp"]).optional().describe('Output format for the image (e.g., "png", "jpeg", "webp"). Support varies by model - leave blank if unsure'),
output_compression: z.number().min(1).max(100).optional().describe('Compression quality for the image (0 to 100). Only applicable for "jpeg" and "webp" formats'),
size: z.enum(["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]).optional().describe('Size of the generated image (e.g., "1024x1024", "1536x1024"). Support varies by model - leave blank if unsure'),
background: z.enum(["transparent", "opaque", "auto"]).optional().describe('Background setting for the generated image. Use "transparent" for images that need transparency (e.g., logos, icons, overlays). Only supported with "png" or "webp" output formats'),
});
constructor(
@@ -36,7 +38,7 @@ export class GenerateImageTool implements Tool<GenerateImageParams> {
) {}
async execute(args: GenerateImageParams): Promise<ToolResult> {
const { prompt, output_format, output_compression, size } = args;
const { prompt, output_format, output_compression, size, background } = args;
try {
const client = createAIClient(this.provider, this.user, this.corsProxy);
@@ -114,6 +116,7 @@ export class GenerateImageTool implements Tool<GenerateImageParams> {
output_format,
output_compression,
size,
background,
});
// Extract cost from response