mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 23:32:12 +03:00
Image routes now resolve a requested model the same way across /v1/images/generations
and /v1/images/edits, via a shared resolver: built-in id -> custom provider prefix ->
bare combo/alias name (e.g. "image" -> its single image target). Previously a bare
combo name fell through to "Invalid image model".
/v1/images/edits gains two capabilities for custom OpenAI-compatible providers:
- multipart edit forwarding to the node's {base_url}/images/edits (was hard-rejected
unless chatgpt-web);
- JSON/data-URL edit input (images:[{image_url:"data:..."}]), converted to the same
fields the multipart reader produces (was "Invalid multipart body").
The chatgpt-web conversation-continuation edit flow is unchanged.
This commit is contained in:
committed by
GitHub
parent
c116bfbc7f
commit
ec4f8c4d42
@@ -1,4 +1,7 @@
|
||||
import { handleImageEdit } from "@omniroute/open-sse/handlers/imageGeneration.ts";
|
||||
import {
|
||||
handleImageEdit,
|
||||
handleOpenAIImageEdit,
|
||||
} from "@omniroute/open-sse/handlers/imageGeneration.ts";
|
||||
import { getProviderCredentials, clearRecoveredProviderState } from "@/sse/services/auth";
|
||||
import { parseImageModel, getImageProvider } from "@omniroute/open-sse/config/imageRegistry.ts";
|
||||
import { errorResponse, unavailableResponse } from "@omniroute/open-sse/utils/error.ts";
|
||||
@@ -6,20 +9,29 @@ import { HTTP_STATUS } from "@omniroute/open-sse/config/constants.ts";
|
||||
import * as log from "@/sse/utils/logger";
|
||||
import { toJsonErrorPayload } from "@/shared/utils/upstreamError";
|
||||
import { enforceApiKeyPolicy } from "@/shared/utils/apiKeyPolicy";
|
||||
import {
|
||||
resolveImageRouteModel,
|
||||
extractImageEditInputFromJson,
|
||||
} from "@/lib/images/imageRouteModel";
|
||||
|
||||
/**
|
||||
* /v1/images/edits — multipart edit endpoint matching OpenAI's images-edit API.
|
||||
* /v1/images/edits — OpenAI-compatible image-edit endpoint.
|
||||
*
|
||||
* Open WebUI's "Image Edit" toggle (images.edit.engine = "openai") posts here
|
||||
* with `prompt` + `image` (file). For chatgpt-web, an "edit" only makes sense
|
||||
* if the uploaded image was originally generated through OmniRoute — we then
|
||||
* have its `{conversationId, parentMessageId}` cached and can continue the
|
||||
* saved chatgpt.com conversation node, which is the only way to actually edit
|
||||
* the image instead of generating an unrelated one from scratch.
|
||||
* Two upstream shapes are supported:
|
||||
* - **chatgpt-web**: an "edit" only makes sense if the uploaded image was originally
|
||||
* generated through OmniRoute — we then have its `{conversationId, parentMessageId}`
|
||||
* cached and can continue the saved chatgpt.com conversation node (the only way to
|
||||
* actually edit the image instead of generating an unrelated one).
|
||||
* - **custom OpenAI-compatible providers** (#3214/#3215): forward a multipart edit to
|
||||
* the node's `{base_url}/images/edits`, mirroring how generations forwards.
|
||||
*
|
||||
* Without this route, multipart bodies trip Next.js's Server Action handler
|
||||
* (which intercepts ALL POSTs with multipart/form-data content-type) and the
|
||||
* client gets a confusing "Failed to find Server Action" 500.
|
||||
* Input is accepted as multipart/form-data (Open WebUI's "Image Edit" toggle) or as JSON
|
||||
* with data-URL images (`images: [{ image_url: "data:..." }]`), since some OpenAI-compatible
|
||||
* clients send the latter. The model may be a built-in id, a `provider/model`, a custom
|
||||
* provider prefix, or a combo/alias name — all resolved the same as generations.
|
||||
*
|
||||
* Without this route, multipart bodies trip Next.js's Server Action handler (which
|
||||
* intercepts ALL multipart POSTs) and the client gets a confusing 500.
|
||||
*/
|
||||
|
||||
export async function OPTIONS() {
|
||||
@@ -42,14 +54,16 @@ function publicBaseUrlHeaders(headers: Headers): Record<string, string> {
|
||||
return out;
|
||||
}
|
||||
|
||||
async function readMultipartImage(formData: FormData): Promise<{
|
||||
interface EditInput {
|
||||
prompt: string;
|
||||
model: string | null;
|
||||
size: string | null;
|
||||
responseFormat: string | null;
|
||||
imageBytes: Buffer | null;
|
||||
imageMime: string | null;
|
||||
}> {
|
||||
}
|
||||
|
||||
async function readMultipartImage(formData: FormData): Promise<EditInput> {
|
||||
const promptRaw = formData.get("prompt");
|
||||
const prompt = typeof promptRaw === "string" ? promptRaw.trim() : "";
|
||||
const modelRaw = formData.get("model");
|
||||
@@ -72,21 +86,45 @@ async function readMultipartImage(formData: FormData): Promise<{
|
||||
return { prompt, model, size, responseFormat, imageBytes, imageMime };
|
||||
}
|
||||
|
||||
/** Read the edit input from either multipart/form-data or a JSON/data-URL body. */
|
||||
async function readEditInput(request: Request): Promise<EditInput | null> {
|
||||
const contentType = request.headers.get("content-type") || "";
|
||||
if (contentType.includes("multipart/form-data")) {
|
||||
try {
|
||||
return await readMultipartImage(await request.formData());
|
||||
} catch (err) {
|
||||
log.warn("IMAGE", `Invalid multipart body: ${err instanceof Error ? err.message : err}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (contentType.includes("application/json")) {
|
||||
try {
|
||||
return extractImageEditInputFromJson(await request.json());
|
||||
} catch (err) {
|
||||
log.warn("IMAGE", `Invalid JSON edit body: ${err instanceof Error ? err.message : err}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function jsonResponse(data: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
let formData: FormData;
|
||||
try {
|
||||
formData = await request.formData();
|
||||
} catch (err) {
|
||||
log.warn(
|
||||
"IMAGE",
|
||||
`Invalid multipart body: ${err instanceof Error ? err.message : String(err)}`
|
||||
const input = await readEditInput(request);
|
||||
if (!input) {
|
||||
return errorResponse(
|
||||
HTTP_STATUS.BAD_REQUEST,
|
||||
"Invalid request body. Send multipart/form-data or JSON with a data-URL image."
|
||||
);
|
||||
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid multipart body");
|
||||
}
|
||||
|
||||
const { prompt, model, size, responseFormat, imageBytes, imageMime } =
|
||||
await readMultipartImage(formData);
|
||||
|
||||
const { prompt, model, size, responseFormat, imageBytes, imageMime } = input;
|
||||
if (!prompt) {
|
||||
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Missing required field: prompt");
|
||||
}
|
||||
@@ -99,74 +137,128 @@ export async function POST(request: Request) {
|
||||
const policy = await enforceApiKeyPolicy(request, fullModel);
|
||||
if (policy.rejection) return policy.rejection;
|
||||
|
||||
const parsed = parseImageModel(fullModel);
|
||||
const providerConfig = getImageProvider(parsed.provider);
|
||||
if (!providerConfig) {
|
||||
return errorResponse(HTTP_STATUS.BAD_REQUEST, `Unknown image provider: ${parsed.provider}`);
|
||||
}
|
||||
if (providerConfig.format !== "chatgpt-web") {
|
||||
// We only implement edit for chatgpt-web today; everything else routes
|
||||
// through generations which doesn't accept image inputs. Surface a
|
||||
// useful error rather than silently dropping the image.
|
||||
return errorResponse(
|
||||
HTTP_STATUS.BAD_REQUEST,
|
||||
`Image edit is only supported for chatgpt-web models (got ${parsed.provider})`
|
||||
);
|
||||
}
|
||||
|
||||
const allowedConnections =
|
||||
policy.apiKeyInfo?.allowedConnections && policy.apiKeyInfo.allowedConnections.length > 0
|
||||
? policy.apiKeyInfo.allowedConnections
|
||||
: null;
|
||||
|
||||
// Resolve combo/alias, custom-provider prefix, and built-in ids consistently with
|
||||
// /v1/images/generations (#3215).
|
||||
const resolvedModel = await resolveImageRouteModel(fullModel);
|
||||
const parsed = parseImageModel(resolvedModel);
|
||||
const providerConfig = parsed.provider ? getImageProvider(parsed.provider) : null;
|
||||
|
||||
// chatgpt-web keeps its conversation-continuation edit flow unchanged.
|
||||
if (providerConfig?.format === "chatgpt-web") {
|
||||
const credentials = await getProviderCredentials(
|
||||
parsed.provider,
|
||||
null,
|
||||
allowedConnections,
|
||||
resolvedModel
|
||||
);
|
||||
if (!credentials) {
|
||||
return errorResponse(
|
||||
HTTP_STATUS.UNAUTHORIZED,
|
||||
`No credentials for provider: ${parsed.provider}`
|
||||
);
|
||||
}
|
||||
if (credentials.allRateLimited) {
|
||||
return unavailableResponse(
|
||||
HTTP_STATUS.RATE_LIMITED,
|
||||
`[${parsed.provider}] All accounts rate limited`,
|
||||
credentials.retryAfter,
|
||||
credentials.retryAfterHuman
|
||||
);
|
||||
}
|
||||
|
||||
const result = await handleImageEdit({
|
||||
provider: parsed.provider,
|
||||
model: parsed.model,
|
||||
body: {
|
||||
prompt,
|
||||
size: size ?? undefined,
|
||||
response_format: responseFormat ?? undefined,
|
||||
n: 1,
|
||||
},
|
||||
imageBytes,
|
||||
imageMime,
|
||||
credentials,
|
||||
log,
|
||||
signal: request.signal,
|
||||
clientHeaders: publicBaseUrlHeaders(request.headers),
|
||||
});
|
||||
|
||||
if (result.success) {
|
||||
await clearRecoveredProviderState(credentials);
|
||||
return jsonResponse((result as any).data);
|
||||
}
|
||||
return jsonResponse(
|
||||
toJsonErrorPayload((result as any).error, "Image edit provider error"),
|
||||
(result as any).status
|
||||
);
|
||||
}
|
||||
|
||||
// Built-in non-chatgpt-web providers do not expose an OpenAI-compatible edit endpoint.
|
||||
if (providerConfig) {
|
||||
return errorResponse(
|
||||
HTTP_STATUS.BAD_REQUEST,
|
||||
`Image edit is not supported for built-in provider "${parsed.provider}". ` +
|
||||
`Use chatgpt-web or a custom OpenAI-compatible image provider.`
|
||||
);
|
||||
}
|
||||
|
||||
// Custom OpenAI-compatible node (no built-in config): forward to {base_url}/images/edits.
|
||||
const slash = resolvedModel.indexOf("/");
|
||||
const customProviderId = slash > 0 ? resolvedModel.slice(0, slash) : null;
|
||||
const customModel = slash > 0 ? resolvedModel.slice(slash + 1) : null;
|
||||
if (!customProviderId || !customModel) {
|
||||
return errorResponse(
|
||||
HTTP_STATUS.BAD_REQUEST,
|
||||
`Unknown image provider for model "${fullModel}". Use provider/model, a custom ` +
|
||||
`provider prefix, or a combo/alias name.`
|
||||
);
|
||||
}
|
||||
|
||||
const credentials = await getProviderCredentials(
|
||||
parsed.provider,
|
||||
customProviderId,
|
||||
null,
|
||||
allowedConnections,
|
||||
fullModel
|
||||
resolvedModel
|
||||
);
|
||||
if (!credentials) {
|
||||
return errorResponse(
|
||||
HTTP_STATUS.UNAUTHORIZED,
|
||||
`No credentials for provider: ${parsed.provider}`
|
||||
HTTP_STATUS.BAD_REQUEST,
|
||||
`No credentials for custom image provider: ${customProviderId}`
|
||||
);
|
||||
}
|
||||
if (credentials.allRateLimited) {
|
||||
return unavailableResponse(
|
||||
HTTP_STATUS.RATE_LIMITED,
|
||||
`[${parsed.provider}] All accounts rate limited`,
|
||||
`[${customProviderId}] All accounts rate limited`,
|
||||
credentials.retryAfter,
|
||||
credentials.retryAfterHuman
|
||||
);
|
||||
}
|
||||
|
||||
const result = await handleImageEdit({
|
||||
provider: parsed.provider,
|
||||
model: parsed.model,
|
||||
body: {
|
||||
prompt,
|
||||
size: size ?? undefined,
|
||||
response_format: responseFormat ?? undefined,
|
||||
n: 1,
|
||||
},
|
||||
const result = await handleOpenAIImageEdit({
|
||||
provider: customProviderId,
|
||||
model: customModel,
|
||||
credentials,
|
||||
prompt,
|
||||
imageBytes,
|
||||
imageMime,
|
||||
credentials,
|
||||
size,
|
||||
responseFormat,
|
||||
n: 1,
|
||||
log,
|
||||
signal: request.signal,
|
||||
clientHeaders: publicBaseUrlHeaders(request.headers),
|
||||
});
|
||||
|
||||
if (result.success) {
|
||||
await clearRecoveredProviderState(credentials);
|
||||
return new Response(JSON.stringify((result as any).data), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return jsonResponse((result as any).data);
|
||||
}
|
||||
|
||||
const errorPayload = toJsonErrorPayload((result as any).error, "Image edit provider error");
|
||||
return new Response(JSON.stringify(errorPayload), {
|
||||
status: (result as any).status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return jsonResponse(
|
||||
toJsonErrorPayload((result as any).error, "Image edit provider error"),
|
||||
(result as any).status
|
||||
);
|
||||
}
|
||||
|
||||
@@ -20,7 +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 { resolveImageRouteModel } from "@/lib/images/imageRouteModel";
|
||||
import { runWithProxyContext } from "@omniroute/open-sse/utils/proxyFetch.ts";
|
||||
|
||||
/**
|
||||
@@ -118,43 +118,6 @@ 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" });
|
||||
// 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);
|
||||
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 {
|
||||
@@ -174,13 +137,11 @@ 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);
|
||||
}
|
||||
// #3205/#3215: resolve a combo/alias name (`image`) or 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. Shared with /images/edits.
|
||||
body.model = await resolveImageRouteModel(body.model);
|
||||
|
||||
// Parse model to get provider
|
||||
let { provider } = parseImageModel(body.model);
|
||||
|
||||
Reference in New Issue
Block a user