9router/open-sse/handlers/imageProviders/openai.js

34 lines
1.3 KiB
JavaScript
Raw Normal View History

2026-05-04 00:29:02 -04:00
// OpenAI-compatible adapter (used by openai, minimax, openrouter, recraft)
import { PROVIDER_MEDIA } from "../../providers/index.js";
2026-05-04 00:29:02 -04:00
const imageCfg = (id) => PROVIDER_MEDIA[id]?.imageConfig || {};
const imageUrl = (id) => imageCfg(id).baseUrl;
2026-05-04 00:29:02 -04:00
export default function createOpenAIAdapter(providerId) {
const cfg = imageCfg(providerId);
2026-05-04 00:29:02 -04:00
return {
buildUrl: () => imageUrl(providerId),
2026-05-04 00:29:02 -04:00
buildHeaders: (creds) => {
const headers = { "Content-Type": "application/json", ...(cfg.headers || {}) };
2026-05-04 00:29:02 -04:00
const key = creds?.apiKey || creds?.accessToken;
if (key) headers["Authorization"] = `Bearer ${key}`;
return headers;
},
buildBody: (model, body) => {
const { prompt, n = 1, size = "1024x1024", quality, style, response_format } = body;
const full = { model, prompt, n, size };
if (quality) full.quality = quality;
if (style) full.style = style;
if (response_format) full.response_format = response_format;
// bodyFields whitelist (e.g. xAI accepts only model/prompt/n/response_format)
if (Array.isArray(cfg.bodyFields)) {
const req = {};
for (const f of cfg.bodyFields) if (full[f] !== undefined) req[f] = full[f];
return req;
}
return full;
2026-05-04 00:29:02 -04:00
},
normalize: (responseBody) => responseBody,
};
}