-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Tag models with vision capabilities and describe images for text-only models #4114
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
Open
nourzakhama2003
wants to merge
4
commits into
dyad-sh:main
Choose a base branch
from
nourzakhama2003:feat/3982-vision-fallback-non-vision-models
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6ae5c75
feat: add vision capability fullback for text only model
nourzakhama2003 088632a
add switch for vision capbilties for text-only model
nourzakhama2003 7b07c35
set the switch to false as default value
nourzakhama2003 57b6777
gate vision in inline images
nourzakhama2003 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { useSettings } from "@/hooks/useSettings"; | ||
| import { Label } from "@/components/ui/label"; | ||
| import { Switch } from "@/components/ui/switch"; | ||
|
|
||
| export function VisionFallbackSwitch() { | ||
| const { settings, updateSettings } = useSettings(); | ||
| const enabled = settings?.enableVisionFallback ?? false; | ||
|
|
||
| return ( | ||
| <div className="flex items-center space-x-2"> | ||
| <Switch | ||
| id="vision-fallback" | ||
| aria-label="Describe images for text-only models" | ||
| checked={enabled} | ||
| onCheckedChange={() => { | ||
| updateSettings({ enableVisionFallback: !enabled }); | ||
| }} | ||
| /> | ||
| <Label htmlFor="vision-fallback"> | ||
| Describe images for text-only models | ||
| </Label> | ||
| </div> | ||
| ); | ||
| } | ||
208 changes: 208 additions & 0 deletions
208
src/ipc/handlers/__tests__/vision_fallback.integration.test.tsx
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,208 @@ | ||
| // Covers the vision fallback end to end: a model the catalog marks | ||
| // `supportsVision: false` must never receive raw image parts, and must instead | ||
| // receive a text description in its place. | ||
| // | ||
| // The catalog fixture in testing/fake-llm-server/index.ts supplies the flag | ||
| // here (the harness points the catalog fetch at the fake server), not | ||
| // MODEL_OPTIONS. | ||
| import { cleanup, screen } from "@testing-library/react"; | ||
| import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| setupHybridChatHarness, | ||
| type HybridChatHarness, | ||
| } from "@/testing/hybrid_chat_harness"; | ||
| import { h } from "@/testing/hybrid.setup"; | ||
|
|
||
| // 1x1 transparent PNG. | ||
| const PNG_BASE64 = | ||
| "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="; | ||
|
|
||
| function pngBytes(): Uint8Array<ArrayBuffer> { | ||
| const decoded = Buffer.from(PNG_BASE64, "base64"); | ||
| // Copy into a plain ArrayBuffer: Buffer's backing store is typed as | ||
| // ArrayBufferLike, which is not assignable to BlobPart. | ||
| const bytes = new Uint8Array(new ArrayBuffer(decoded.byteLength)); | ||
| bytes.set(decoded); | ||
| return bytes; | ||
| } | ||
|
|
||
| type DumpMessage = { content?: unknown }; | ||
| type DumpBody = { input?: DumpMessage[]; messages?: DumpMessage[] }; | ||
|
|
||
| /** Counts structured image parts across every message in the dumped request. */ | ||
| function imagePartCount(parsedBody: DumpBody): number { | ||
| const messages = parsedBody?.input ?? parsedBody?.messages ?? []; | ||
| return messages | ||
| .flatMap((message) => | ||
| Array.isArray(message?.content) | ||
| ? (message.content as { type?: string }[]) | ||
| : [], | ||
| ) | ||
| .filter((part) => part?.type === "image" || part?.type === "image_url") | ||
| .length; | ||
| } | ||
|
|
||
| const PRO_SETTINGS = { | ||
| isTestMode: true, | ||
| enableDyadPro: true, | ||
| providerSettings: { auto: { apiKey: { value: "testdyadkey" } } }, | ||
| }; | ||
|
|
||
| describe("vision fallback for a non-vision model (integration)", () => { | ||
| let harness: HybridChatHarness; | ||
|
|
||
| beforeAll(async () => { | ||
| harness = await setupHybridChatHarness({ | ||
| electronMock: h, | ||
| engine: true, | ||
| // Marked `supportsVision: false` in the fake catalog fixture. | ||
| selectedModel: { provider: "openai", name: "gpt-5.2-no-vision" }, | ||
|
|
||
| settings: { ...PRO_SETTINGS, enableVisionFallback: true }, | ||
| }); | ||
| }, 60_000); | ||
|
|
||
| afterEach(() => { | ||
| cleanup(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await harness?.dispose(); | ||
| }); | ||
|
|
||
| it("describes the image as text and sends no image part", async () => { | ||
| const chatId = await harness.createChat(); | ||
| harness.mount({ chatId }); | ||
|
|
||
| harness.setChatAttachments([ | ||
| { | ||
| name: "mockup.png", | ||
| content: pngBytes(), | ||
| mimeType: "image/png", | ||
| type: "chat-context", | ||
| }, | ||
| ]); | ||
| await screen.findByText("mockup.png"); | ||
|
|
||
| const streamEnd = harness.waitForNextStreamEnd(chatId); | ||
| const { send } = await harness.typeInChat("[dump]", { chatId }); | ||
| send(); | ||
| await streamEnd; | ||
|
|
||
| // The turn must complete rather than surfacing a raw provider error. | ||
| expect( | ||
| harness.bridge.sentEvents.filter( | ||
| (event) => event.channel === "chat:response:error", | ||
| ), | ||
| ).toHaveLength(0); | ||
|
|
||
| const req = harness.getServerDump({ type: "request" }); | ||
|
|
||
| // No raw image part reached the non-vision model... | ||
| expect(imagePartCount(req.parsed.body)).toBe(0); | ||
| // ...and a description took its place. | ||
| expect(req.text).toContain("dyad-image-description"); | ||
| // The "analyze the image" system block must not be emitted when no image | ||
| // is actually sent. | ||
| expect(req.text).not.toContain("# Image Analysis Instructions"); | ||
| }, 60_000); | ||
| }); | ||
|
|
||
| describe("vision fallback off (the default) (integration)", () => { | ||
| let harness: HybridChatHarness; | ||
|
|
||
| beforeAll(async () => { | ||
| harness = await setupHybridChatHarness({ | ||
| electronMock: h, | ||
| engine: true, | ||
| selectedModel: { provider: "openai", name: "gpt-5.2-no-vision" }, | ||
| // Explicit, though this is also what an untouched install does. | ||
| settings: { ...PRO_SETTINGS, enableVisionFallback: false }, | ||
| }); | ||
| }, 60_000); | ||
|
|
||
| afterEach(() => { | ||
| cleanup(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await harness?.dispose(); | ||
| }); | ||
|
|
||
| it("omits the image without telling the user to switch models", async () => { | ||
| const chatId = await harness.createChat(); | ||
| harness.mount({ chatId }); | ||
|
|
||
| harness.setChatAttachments([ | ||
| { | ||
| name: "mockup.png", | ||
| content: pngBytes(), | ||
| mimeType: "image/png", | ||
| type: "chat-context", | ||
| }, | ||
| ]); | ||
| await screen.findByText("mockup.png"); | ||
|
|
||
| const streamEnd = harness.waitForNextStreamEnd(chatId); | ||
| const { send } = await harness.typeInChat("[dump]", { chatId }); | ||
| send(); | ||
| await streamEnd; | ||
|
|
||
| const req = harness.getServerDump({ type: "request" }); | ||
|
|
||
| // Opting out must not send the image anywhere. | ||
| expect(imagePartCount(req.parsed.body)).toBe(0); | ||
| // The model is told the images were dropped by configuration... | ||
| expect(req.text).toContain("off in Settings"); | ||
| // ...and must not be told to nag the user about a missing capability. | ||
| expect(req.text).not.toContain("no vision-capable model is available"); | ||
| }, 60_000); | ||
| }); | ||
|
|
||
| describe("vision-capable model still receives images (integration)", () => { | ||
| let harness: HybridChatHarness; | ||
|
|
||
| beforeAll(async () => { | ||
| harness = await setupHybridChatHarness({ | ||
| electronMock: h, | ||
| engine: true, | ||
| // Not marked in the fake catalog, so it is treated as vision-capable. | ||
| selectedModel: { provider: "openai", name: "gpt-5.2" }, | ||
| settings: PRO_SETTINGS, | ||
| }); | ||
| }, 60_000); | ||
|
|
||
| afterEach(() => { | ||
| cleanup(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await harness?.dispose(); | ||
| }); | ||
|
|
||
| it("sends the image part and injects no description", async () => { | ||
| const chatId = await harness.createChat(); | ||
| harness.mount({ chatId }); | ||
|
|
||
| harness.setChatAttachments([ | ||
| { | ||
| name: "mockup.png", | ||
| content: pngBytes(), | ||
| mimeType: "image/png", | ||
| type: "chat-context", | ||
| }, | ||
| ]); | ||
| await screen.findByText("mockup.png"); | ||
|
|
||
| const streamEnd = harness.waitForNextStreamEnd(chatId); | ||
| const { send } = await harness.typeInChat("[dump]", { chatId }); | ||
| send(); | ||
| await streamEnd; | ||
|
|
||
| const req = harness.getServerDump({ type: "request" }); | ||
|
|
||
| expect(imagePartCount(req.parsed.body)).toBeGreaterThan(0); | ||
| expect(req.text).not.toContain("dyad-image-description"); | ||
| }, 60_000); | ||
| }); |
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
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.