Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(desktop): recognize AVIF files as images (#18867)

YuTengjinge863261cb747
diff --git a/apps/desktop/src/main/controllers/LocalFileCtr.ts b/apps/desktop/src/main/controllers/LocalFileCtr.ts
index f1b049d5221..3fff1aa68b2 100644
--- a/apps/desktop/src/main/controllers/LocalFileCtr.ts
+++ b/apps/desktop/src/main/controllers/LocalFileCtr.ts
@@ -72,16 +72,17 @@ const PROJECT_FILE_GLOB_LIMIT = 5000;
/**
* Image extensions `readFile` uploads to file storage instead of refusing as
- * binary. The agent then sees the image (vision) via an `image_url` part,
+ * binary. The runtime can then route the image to downstream media analysis
* rather than hitting "Unsupported binary file".
*
- * Limited to the formats vision providers accept (Anthropic/OpenAI:
- * png/jpeg/gif/webp) β€” anything else would be silently dropped by the
- * model-runtime builders, which is worse than the binary refusal. SVG is
- * intentionally absent: it's text, and reading the source is more useful to
- * the model than a rasterization we can't produce here.
+ * AVIF is included for Analyze Media, whose request-boundary normalization
+ * converts unsupported image formats for its fallback vision model. This does
+ * not imply native AVIF support across providers. SVG is intentionally absent:
+ * it's text, and reading the source is more useful to the model than a
+ * rasterization we can't produce here.
*/
const LOCAL_IMAGE_EXT_TO_MIME: Record<string, string> = {
+ avif: 'image/avif',
gif: 'image/gif',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
diff --git a/apps/desktop/src/main/controllers/__tests__/LocalFileCtr.readFile.test.ts b/apps/desktop/src/main/controllers/__tests__/LocalFileCtr.readFile.test.ts
index 4fb5da0f3af..7076409e3b0 100644
--- a/apps/desktop/src/main/controllers/__tests__/LocalFileCtr.readFile.test.ts
+++ b/apps/desktop/src/main/controllers/__tests__/LocalFileCtr.readFile.test.ts
@@ -154,6 +154,24 @@ describe('LocalFileCtr β€” readFile / readFiles (real fs)', () => {
expect(result.content).toBe('[Image: cat.png]');
});
+ it('should upload AVIF as an image for downstream media analysis', async () => {
+ mockUploadService.uploadLocalFile.mockResolvedValue({
+ id: 'file-avif',
+ url: 'https://files.example.com/sample.avif',
+ });
+ const filePath = path.join(tmpDir, 'sample.avif');
+ await writeFile(filePath, Buffer.from('avif'));
+
+ const result = await localFileCtr.readFile({ path: filePath });
+
+ expect(mockUploadService.uploadLocalFile).toHaveBeenCalledWith(filePath);
+ expect(result.isImage).toBe(true);
+ expect(result.fileType).toBe('image/avif');
+ expect(result.imageFileId).toBe('file-avif');
+ expect(result.imageUrl).toBe('https://files.example.com/sample.avif');
+ expect(result.content).toBe('[Image: sample.avif]');
+ });
+
it('should resolve a relative image path against cwd', async () => {
mockUploadService.uploadLocalFile.mockResolvedValue({
id: 'file-2',
diff --git a/packages/context-engine/src/processors/MessageContent.ts b/packages/context-engine/src/processors/MessageContent.ts
index 918463f967e..ffdb40b2ee5 100644
--- a/packages/context-engine/src/processors/MessageContent.ts
+++ b/packages/context-engine/src/processors/MessageContent.ts
@@ -31,6 +31,14 @@ const log = debug('context-engine:processor:MessageContentProcessor');
export const VISION_DOWNGRADE_PLACEHOLDER =
'[image omitted: native vision is not supported. Do not infer or describe the image. If the request depends on it, use an available visual-analysis tool before answering; otherwise state that the image cannot be inspected.]';
+/**
+ * AVIF must stay as a media ref even when the active model supports vision.
+ * Model capability flags do not describe per-format support, while Analyze Media
+ * normalizes AVIF at its request boundary.
+ */
+const requiresVisualAnalysis = (mediaType: unknown): boolean =>
+ typeof mediaType === 'string' && mediaType.split(';', 1)[0].trim().toLowerCase() === 'image/avif';
+
/**
* Heterogeneous-agent image uploads mirror each persisted image URL into tool
* text as `![mediaType](url)`. Non-vision models must receive only the opaque
@@ -511,6 +519,12 @@ export class MessageContentProcessor extends BaseProcessor {
if (images.length === 0) return message;
const canUseVision = !!this.config.isCanUseVision?.(this.config.model, this.config.provider);
+ const fallbackImages = canUseVision
+ ? images.filter((image) => requiresVisualAnalysis(image.mediaType))
+ : images;
+ const visionImages = canUseVision
+ ? images.filter((image) => !requiresVisualAnalysis(image.mediaType))
+ : [];
// Normalize text content (historical messages may already be multimodal).
let textContent = '';
@@ -523,13 +537,14 @@ export class MessageContentProcessor extends BaseProcessor {
.join('\n\n');
}
- // Vision not supported: drop the image parts but surface a placeholder so
- // the model can pass a stable opaque ref to the visual-analysis fallback.
+ // Native vision unavailable or image format unsupported: surface stable
+ // opaque refs so the model can call the visual-analysis fallback.
// The signed URL stays out of model-visible text, which prevents the model
// from copying opaque image data between tool calls.
- if (!canUseVision) {
- const fallbackTextContent = stripUploadedImageMarkdown(textContent, images);
- const placeholders = images
+ let processedTextContent = textContent;
+ if (fallbackImages.length > 0) {
+ const fallbackTextContent = stripUploadedImageMarkdown(textContent, fallbackImages);
+ const placeholders = fallbackImages
.map(({ sourceIndex, url }) => {
// Inline image data is safe to send as a structured vision input but
// must never be copied into text or exposed as a reusable media ref.
@@ -544,20 +559,20 @@ export class MessageContentProcessor extends BaseProcessor {
return `[image omitted: native vision is not supported. Media ref: ${ref}. Do not infer or describe the image. Use an available visual-analysis tool with this ref before answering.]`;
})
.join('\n');
- const content = fallbackTextContent
+ processedTextContent = fallbackTextContent
? `${fallbackTextContent}\n\n${placeholders}`
: placeholders;
-
- return { ...message, content };
}
+ if (visionImages.length === 0) return { ...message, content: processedTextContent };
+
const contentParts: UserMessageContentPart[] = [];
- if (textContent) {
- contentParts.push({ text: textContent, type: 'text' });
+ if (processedTextContent) {
+ contentParts.push({ text: processedTextContent, type: 'text' });
}
- contentParts.push(...(await this.processImageList(images)));
+ contentParts.push(...(await this.processImageList(visionImages)));
return { ...message, content: contentParts };
}
diff --git a/packages/context-engine/src/processors/__tests__/MessageContent.test.ts b/packages/context-engine/src/processors/__tests__/MessageContent.test.ts
index c16630de768..35f542e966c 100644
--- a/packages/context-engine/src/processors/__tests__/MessageContent.test.ts
+++ b/packages/context-engine/src/processors/__tests__/MessageContent.test.ts
@@ -393,6 +393,57 @@ describe('MessageContentProcessor', () => {
expect(result.messages[0].tool_call_id).toBe('call_abc');
});
+ it('should keep AVIF as a media ref while forwarding supported tool images', async () => {
+ mockIsCanUseVision.mockReturnValue(true);
+
+ const processor = new MessageContentProcessor({
+ model: 'gpt-4-vision',
+ provider: 'openai',
+ isCanUseVision: mockIsCanUseVision,
+ fileContext: { enabled: false },
+ });
+ const avifUrl = 'https://example.com/f/image-id.avif?signature=secret';
+ const pngUrl = 'https://example.com/f/image-id.png?signature=secret';
+ const messages: UIChatMessage[] = [
+ {
+ content: 'Read image results',
+ id: 'tool-avif',
+ pluginState: {
+ images: [
+ { mediaType: 'image/avif', url: avifUrl },
+ { mediaType: 'image/png', url: pngUrl },
+ ],
+ },
+ role: 'tool',
+ tool_call_id: 'call_avif',
+ createdAt: Date.now(),
+ updatedAt: Date.now(),
+ } as any,
+ ];
+
+ const result = await processor.process(createContext(messages));
+ const content = result.messages[0].content;
+
+ expect(content).toBeInstanceOf(Array);
+ const contentParts = content as Array<{
+ image_url?: { detail: string; url: string };
+ text?: string;
+ type: string;
+ }>;
+ const textContent = contentParts.find((part) => part.type === 'text')?.text;
+
+ expect(textContent).toContain(
+ createMediaFileRef({ index: 0, messageId: 'tool-avif', type: 'image' }),
+ );
+ expect(textContent).toContain('visual-analysis tool');
+ expect(textContent).not.toContain(avifUrl);
+ expect(contentParts).toContainEqual({
+ image_url: { detail: 'auto', url: pngUrl },
+ type: 'image_url',
+ });
+ expect(result.messages[0].tool_call_id).toBe('call_avif');
+ });
+
it('should remove uploaded image markdown URLs before adding a media ref', async () => {
mockIsCanUseVision.mockReturnValue(false);