-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat: support image generation via OpenRouter's dedicated image API #3786
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
Robinnnnn
wants to merge
2
commits into
chatboxai:main
Choose a base branch
from
Robinnnnn:openrouter-images-endpoint
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
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
103 changes: 103 additions & 0 deletions
103
src/shared/providers/definitions/models/openrouter.test.ts
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,103 @@ | ||
| import type { ProviderModelInfo } from 'src/shared/types' | ||
| import type { ModelDependencies } from 'src/shared/types/adapters' | ||
| import type { SentryScope } from 'src/shared/utils/sentry_adapter' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import OpenRouter from './openrouter' | ||
|
|
||
| const mockScope: SentryScope = { | ||
| setTag: vi.fn(), | ||
| setExtra: vi.fn(), | ||
| } | ||
|
|
||
| function createDependencies(): ModelDependencies { | ||
| return { | ||
| request: { | ||
| fetchWithOptions: vi.fn(), | ||
| apiRequest: vi.fn(), | ||
| }, | ||
| storage: { | ||
| saveImage: vi.fn(), | ||
| getImage: vi.fn(), | ||
| }, | ||
| sentry: { | ||
| captureException: vi.fn(), | ||
| withScope: vi.fn((callback: (scope: SentryScope) => void) => callback(mockScope)), | ||
| }, | ||
| getRemoteConfig: vi.fn(), | ||
| platformType: 'desktop', | ||
| oauth: { | ||
| refreshCredential: vi.fn(), | ||
| persistCredential: vi.fn(), | ||
| clearCredential: vi.fn(), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| function createModel(modelId: string, type?: ProviderModelInfo['type']) { | ||
| const model: ProviderModelInfo = { | ||
| modelId, | ||
| type, | ||
| } | ||
| return new OpenRouter( | ||
| { | ||
| apiKey: 'test-api-key', | ||
| model, | ||
| }, | ||
| createDependencies() | ||
| ) | ||
| } | ||
|
|
||
| // A 1x1 transparent PNG | ||
| const TEST_IMAGE_BASE64 = | ||
| 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==' | ||
|
|
||
| describe('OpenRouter image generation', () => { | ||
| afterEach(() => { | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| it('should generate images via the dedicated /images endpoint', async () => { | ||
| const fetchMock = vi.fn().mockResolvedValue( | ||
| new Response( | ||
| JSON.stringify({ | ||
| created: 1700000000, | ||
| data: [{ b64_json: TEST_IMAGE_BASE64 }], | ||
| }), | ||
| { status: 200, headers: { 'Content-Type': 'application/json' } } | ||
| ) | ||
| ) | ||
| vi.stubGlobal('fetch', fetchMock) | ||
|
|
||
| const model = createModel('google/gemini-2.5-flash-image', 'image') | ||
| const results = await model.paint({ prompt: 'a red apple', num: 1 }) | ||
|
|
||
| expect(results).toHaveLength(1) | ||
| expect(results[0]).toContain(`base64,${TEST_IMAGE_BASE64}`) | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(1) | ||
| const [url, init] = fetchMock.mock.calls[0] | ||
| expect(String(url)).toBe('https://openrouter.ai/api/v1/images') | ||
| const body = JSON.parse(init.body as string) | ||
| expect(body.model).toBe('google/gemini-2.5-flash-image') | ||
| expect(body.prompt).toBe('a red apple') | ||
| }) | ||
|
|
||
| it('should invoke the progressive callback with data urls', async () => { | ||
| vi.stubGlobal( | ||
| 'fetch', | ||
| vi.fn().mockResolvedValue( | ||
| new Response(JSON.stringify({ data: [{ b64_json: TEST_IMAGE_BASE64 }] }), { | ||
| status: 200, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }) | ||
| ) | ||
| ) | ||
|
|
||
| const model = createModel('google/gemini-2.5-flash-image', 'image') | ||
| const callback = vi.fn() | ||
| await model.paint({ prompt: 'a red apple', num: 1 }, undefined, callback) | ||
|
|
||
| expect(callback).toHaveBeenCalledTimes(1) | ||
| expect(callback.mock.calls[0][0]).toMatch(/^data:image\/png;base64,/) | ||
| }) | ||
| }) |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Forward the Image Creator inputs to OpenRouter
This override only selects the SDK image model, so
AbstractAISDKModel.paint()remains the request builder. That method sends onlypromptandn: it explicitly dropsparams.images, and it never passesparams.aspectRatio. The Image Creator supplies both fields when the user selects reference images or a non-auto ratio, while the OpenRouter image model supports them (prompt: { text, images }becomesinput_references, andaspectRatiobecomesaspect_ratio).As a result, users can make a billable generation that silently ignores the selected reference images and aspect ratio. Please forward both fields, either in the shared base implementation or in an OpenRouter
paint()override, and add a request-body test that assertsinput_referencesandaspect_ratio.