Repository navigation
fix(images): use node base_url + resolve prefix for custom image providers (#3205) #3208
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ import { v1ImageGenerationSchema } from "@/shared/validation/schemas"; | |
| import { isValidationFailure, validateBody } from "@/shared/validation/helpers"; | ||
|
|
||
| import { getAllCustomModels, resolveProxyForConnection } from "@/lib/localDb"; | ||
| import { getProviderNodes } from "@/lib/db/providers"; | ||
| import { runWithProxyContext } from "@omniroute/open-sse/utils/proxyFetch.ts"; | ||
|
|
||
| /** | ||
|
|
@@ -117,6 +118,43 @@ function publicBaseUrlHeaders(headers: Headers): Record<string, string> { | |
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve a `prefix/model` image request to the internal `<nodeId>/<model>` | ||
| * form (#3205). | ||
| * | ||
| * The custom-model lookup below only matches the full internal id | ||
| * (`<nodeId>/<modelId>`), so a request that uses the user-defined provider | ||
| * prefix (e.g. `myImg/gpt-image-2`) never matched and fell through to | ||
| * "Invalid image model". This mirrors how chat resolves prefixes in | ||
| * `src/sse/services/model.ts` (match on `node.prefix` OR `node.id`). | ||
| * | ||
| * Returns the rewritten model string, or the original string when no node | ||
| * prefix matches (so built-in and already-internal ids are untouched). | ||
| */ | ||
| async function resolveImageModelPrefix(modelStr: string): Promise<string> { | ||
| if (typeof modelStr !== "string") return modelStr; | ||
| const slash = modelStr.indexOf("/"); | ||
| if (slash <= 0) return modelStr; | ||
|
|
||
| const prefixPart = modelStr.slice(0, slash); | ||
| const rest = modelStr.slice(slash + 1); | ||
| if (!rest) return modelStr; | ||
|
|
||
| try { | ||
| const nodes = await getProviderNodes({ type: "openai-compatible" }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: If this code path is exercised frequently, consider caching the node list (e.g. via |
||
| // Prefer an explicit user-defined prefix match; node.id (internal UUID) is | ||
| // already handled by the exact-id loop, so only rewrite when the prefix | ||
| // differs from the node id. | ||
| const matched = nodes.find((node: any) => node.prefix === prefixPart); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING:
|
||
| if (matched && matched.id && matched.id !== prefixPart) { | ||
| return `${matched.id}/${rest}`; | ||
| } | ||
| } catch { | ||
| // DB unavailable (pre-migration / tests) — leave the model untouched. | ||
| } | ||
| return modelStr; | ||
| } | ||
|
|
||
| export async function POST(request) { | ||
| let rawBody; | ||
| try { | ||
|
|
@@ -136,6 +174,14 @@ export async function POST(request) { | |
| const policy = await enforceApiKeyPolicy(request, body.model); | ||
| if (policy.rejection) return policy.rejection; | ||
|
|
||
| // #3205: rewrite a user-prefixed custom image model (`myImg/gpt-image-2`) to | ||
| // its internal `<nodeId>/<model>` form so the custom-model lookup and | ||
| // handler's resolvedProvider extraction resolve correctly. Built-in and | ||
| // already-internal ids pass through unchanged. | ||
| if (!parseImageModel(body.model).provider) { | ||
| body.model = await resolveImageModelPrefix(body.model); | ||
|
Comment on lines
+181
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a client uses the newly supported user prefix ( Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| // Parse model to get provider | ||
| let { provider } = parseImageModel(body.model); | ||
| let isCustomModel = false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| import { resolveImageBaseUrl } from "@omniroute/open-sse/handlers/imageGeneration.ts"; | ||
|
|
||
| const GEMINI_FALLBACK = | ||
| "https://generativelanguage.googleapis.com/v1beta/openai/images/generations"; | ||
|
|
||
| test("#3205: custom node baseUrl from providerSpecificData is used (not Gemini fallback)", () => { | ||
| const credentials = { providerSpecificData: { baseUrl: "https://example.com/v1" } }; | ||
| const resolved = resolveImageBaseUrl(credentials, GEMINI_FALLBACK); | ||
|
|
||
| assert.ok( | ||
| resolved.startsWith("https://example.com/"), | ||
| `expected resolved URL to point to example.com, got: ${resolved}` | ||
| ); | ||
| assert.ok( | ||
| !resolved.includes("generativelanguage.googleapis.com"), | ||
| `resolved URL must not fall back to the Gemini endpoint, got: ${resolved}` | ||
| ); | ||
| // A node configured as https://example.com/v1 should yield the OpenAI-compatible | ||
| // images path appended. | ||
| assert.equal(resolved, "https://example.com/v1/images/generations"); | ||
| }); | ||
|
|
||
| test("#3205: providerSpecificData.baseUrl wins over top-level credentials.baseUrl", () => { | ||
| const credentials = { | ||
| baseUrl: "https://toplevel.example/v1", | ||
| providerSpecificData: { baseUrl: "https://psd.example/v1" }, | ||
| }; | ||
| const resolved = resolveImageBaseUrl(credentials, GEMINI_FALLBACK); | ||
| assert.equal(resolved, "https://psd.example/v1/images/generations"); | ||
| }); | ||
|
|
||
| test("#3205: trailing slash on node baseUrl is normalized (no double slash)", () => { | ||
| const credentials = { providerSpecificData: { baseUrl: "https://example.com/v1/" } }; | ||
| const resolved = resolveImageBaseUrl(credentials, GEMINI_FALLBACK); | ||
| assert.equal(resolved, "https://example.com/v1/images/generations"); | ||
| }); | ||
|
|
||
| test("#3205: an already-complete images URL is not double-appended", () => { | ||
| const credentials = { | ||
| providerSpecificData: { baseUrl: "https://example.com/v1/images/generations" }, | ||
| }; | ||
| const resolved = resolveImageBaseUrl(credentials, GEMINI_FALLBACK); | ||
| assert.equal(resolved, "https://example.com/v1/images/generations"); | ||
| }); | ||
|
|
||
| test("#3205: top-level credentials.baseUrl is honored when no providerSpecificData", () => { | ||
| const credentials = { baseUrl: "https://legacy.example/v1" }; | ||
| const resolved = resolveImageBaseUrl(credentials, GEMINI_FALLBACK); | ||
| assert.equal(resolved, "https://legacy.example/v1/images/generations"); | ||
| }); | ||
|
|
||
| test("#3205: falls back to provided default when no node baseUrl present", () => { | ||
| const resolved = resolveImageBaseUrl({}, GEMINI_FALLBACK); | ||
| assert.equal(resolved, GEMINI_FALLBACK); | ||
| const resolvedNull = resolveImageBaseUrl(null, GEMINI_FALLBACK); | ||
| assert.equal(resolvedNull, GEMINI_FALLBACK); | ||
| }); |
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.
Custom OpenAI-compatible nodes can store a non-default endpoint path in
providerSpecificData.chatPath, but this resolver only uses the base URL and then always appends/images/generations. For an image node configured with a gateway-specific path such asbaseUrl=https://gateway.example/v1andchatPath=/custom/images, requests are sent to/v1/images/generationsinstead of the configured endpoint, so those otherwise valid custom providers still fail.Useful? React with 👍 / 👎.