feat(api): combo/alias resolution + OpenAI-compatible edits for image routes (#3214, #3215) (#3219)

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:
Diego Rodrigues de Sa e Souza
2026-06-05 09:37:32 -03:00
committed by GitHub
parent c116bfbc7f
commit ec4f8c4d42
5 changed files with 537 additions and 114 deletions

View File

@@ -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
);
}

View File

@@ -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);

View File

@@ -0,0 +1,161 @@
/**
* Shared model resolution for the image routes (#3214 / #3215).
*
* `/v1/images/generations` and `/v1/images/edits` must resolve a requested model the
* same way, and as close as practical to how chat routing resolves models:
*
* 1. Built-in image model id / alias (`cgpt-web/...`, `gpt-image-1`, …) — untouched.
* 2. Custom provider *prefix* form (`myImg/gpt-image-2`) — rewritten to the internal
* `<nodeId>/<model>` id (#3205 did this inline in the generations route only).
* 3. Bare combo / alias name with no slash (`image`) — resolved to the combo's single
* image target, then that target is itself prefix-resolved.
*
* Anything that does not match falls through unchanged, so existing built-in and
* already-internal ids keep working.
*/
import { parseImageModel } from "@omniroute/open-sse/config/imageRegistry.ts";
import { resolveComboTargets } from "@omniroute/open-sse/services/combo.ts";
import { getComboByName, getCombos } from "@/lib/db/combos";
import { getProviderNodes } from "@/lib/db/providers";
/**
* Rewrite a `prefix/model` custom image model to its internal `<nodeId>/<model>` form.
* Returns the original string when no openai-compatible node prefix matches (so built-in
* and already-internal ids pass through). Mirrors `src/sse/services/model.ts` (match on
* `node.prefix` OR `node.id`).
*/
export 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" });
// node.id (internal UUID) is already a valid internal id; only rewrite when a
// user-defined prefix differs from the node id.
const matched = nodes.find((node: { prefix?: unknown }) => node.prefix === prefixPart);
if (matched && typeof matched.id === "string" && matched.id && matched.id !== prefixPart) {
return `${matched.id}/${rest}`;
}
} catch {
// DB unavailable (pre-migration / tests) — leave the model untouched.
}
return modelStr;
}
/**
* Resolve a bare combo/alias name (e.g. `image`) to its first image model target's
* model string, or null when the name is not a combo / has no usable target.
*/
export async function resolveSingleImageComboTarget(name: string): Promise<string | null> {
if (typeof name !== "string" || !name.trim()) return null;
try {
const combo = await getComboByName(name);
if (!combo) return null;
const allCombos = await getCombos();
const targets = resolveComboTargets(combo as never, allCombos as never);
const first = targets.find(
(t: { modelStr?: unknown }) => typeof t?.modelStr === "string" && (t.modelStr as string).trim()
);
return (first?.modelStr as string) ?? null;
} catch {
return null;
}
}
/**
* Full image-route model resolver. See module header for the resolution order.
*/
export async function resolveImageRouteModel(modelStr: string): Promise<string> {
if (typeof modelStr !== "string" || !modelStr.trim()) return modelStr;
// 1. Built-in image model (alias or provider/model) — leave untouched.
if (parseImageModel(modelStr).provider) return modelStr;
// 3. Bare combo/alias name (no slash): resolve to its single image target, then
// prefix-resolve that target (it may itself be a `prefix/model` custom id).
if (!modelStr.includes("/")) {
const target = await resolveSingleImageComboTarget(modelStr);
if (target && target !== modelStr) return resolveImageModelPrefix(target);
return modelStr;
}
// 2. Custom provider prefix form — rewrite to internal `<nodeId>/<model>`.
return resolveImageModelPrefix(modelStr);
}
interface ParsedImageEditInput {
prompt: string;
model: string | null;
size: string | null;
responseFormat: string | null;
imageBytes: Buffer | null;
imageMime: string | null;
}
/** Parse a `data:<mime>;base64,<data>` URL into raw bytes + mime, or null when invalid. */
export function parseDataUrl(value: unknown): { bytes: Buffer; mime: string } | null {
if (typeof value !== "string") return null;
const match = /^data:([^;,]+)?(;base64)?,(.*)$/s.exec(value.trim());
if (!match) return null;
const mime = match[1] || "image/png";
const isBase64 = Boolean(match[2]);
const payload = match[3] ?? "";
try {
const bytes = isBase64
? Buffer.from(payload, "base64")
: Buffer.from(decodeURIComponent(payload), "utf8");
if (bytes.length === 0) return null;
return { bytes, mime };
} catch {
return null;
}
}
/**
* Extract an OpenAI-compatible image-edit payload from a JSON body. Some clients send
* edit input as JSON with data-URL images instead of multipart/form-data; accept the
* common shapes (`image: "data:..."`, `images: [{ image_url: "data:..." }]` or
* `images: ["data:..."]`) and surface the same fields the multipart reader produces.
*/
export function extractImageEditInputFromJson(body: unknown): ParsedImageEditInput {
const obj = (body && typeof body === "object" ? body : {}) as Record<string, unknown>;
const str = (v: unknown): string | null =>
typeof v === "string" && v.trim() ? v.trim() : null;
const prompt = typeof obj.prompt === "string" ? obj.prompt.trim() : "";
const model = str(obj.model);
const size = str(obj.size);
const responseFormat = str(obj.response_format);
const candidates: unknown[] = [];
if (obj.image !== undefined) candidates.push(obj.image);
const images = obj.images;
if (Array.isArray(images)) {
for (const entry of images) {
if (typeof entry === "string") candidates.push(entry);
else if (entry && typeof entry === "object") {
const e = entry as Record<string, unknown>;
candidates.push(e.image_url ?? e.url ?? e.b64_json);
}
}
}
let imageBytes: Buffer | null = null;
let imageMime: string | null = null;
for (const candidate of candidates) {
const parsed = parseDataUrl(candidate);
if (parsed) {
imageBytes = parsed.bytes;
imageMime = parsed.mime;
break;
}
}
return { prompt, model, size, responseFormat, imageBytes, imageMime };
}