import { detectCapabilities } from "../core/capabilities.js"; import { ImageGenError, errorFromStatus, sanitizeSecret } from "../core/errors.js"; import type { EditParams, GenerateParams, ImageGenConfig, ImageOperationResult, ImageProvider, ModelInfo, VariationParams } from "../core/types.js"; import { downloadImage, fetchWithTimeout, parseJsonLimited, readResponseTextLimited } from "../utils/download.js"; import { decodeBase64Image, loadLocalImage, saveImageBuffer } from "../utils/files.js"; interface OpenAIModelResponse { data?: unknown[]; object?: string } interface OpenAIImageItem { url?: string; image_url?: string; image?: string; b64_json?: string; revised_prompt?: string } interface OpenAIImageResponse { data?: OpenAIImageItem[]; created?: number } export class OpenAICompatibleProvider implements ImageProvider { readonly type = "openai-compatible"; readonly name = "OpenAI-compatible"; constructor(private readonly config: ImageGenConfig) {} async discoverModels(signal?: AbortSignal): Promise { const response = await this.request("GET", "/models", undefined, "获取模型列表", signal); if (response.status === 404) { if (this.config.model) return [this.modelFromId(this.config.model, { fallback: true })]; throw errorFromStatus(404, "获取模型列表"); } if (!response.ok) { if (this.config.model && (response.status === 404 || response.status === 405)) return [this.modelFromId(this.config.model, { fallback: true })]; throw errorFromStatus(response.status, "获取模型列表"); } let json: OpenAIModelResponse; try { json = await parseJsonLimited(response, this.config.maxResponseBytes) as OpenAIModelResponse; } catch (error) { if (this.config.model && error instanceof ImageGenError && error.code === "invalid_json") return [this.modelFromId(this.config.model, { fallback: true })]; throw error; } if (!Array.isArray(json.data)) { if (this.config.model) return [this.modelFromId(this.config.model, { fallback: true })]; throw new ImageGenError("invalid_json", "/models 响应不包含 data 数组。", "该服务商可能不支持 /models;请在添加 Provider 时手动指定模型 ID。 "); } const models = json.data.map((item) => this.modelFromRaw(item)).filter((m): m is ModelInfo => Boolean(m)); if (models.length === 0 && this.config.model) return [this.modelFromId(this.config.model, { fallback: true })]; return models; } async generate(params: GenerateParams, signal?: AbortSignal): Promise { // auto:不发送 response_format 字段,让服务商用它最快/最顺手的方式返回; // 仅当用户显式选择 url 或 b64_json 时才发送字段。 const responseFormat = this.config.responseFormat === "b64_json" || this.config.responseFormat === "url" ? this.config.responseFormat : undefined; const caps = params.modelCapabilities; const body = compact({ model: params.model, prompt: params.prompt, size: caps?.supportsSize === false ? undefined : params.size, quality: caps?.supportsQuality === false ? undefined : params.quality, n: caps?.supportsMultipleImages === false ? 1 : params.n ?? 1, response_format: responseFormat, output_format: params.output_format, aspect_ratio: caps?.supportsAspectRatio === false ? undefined : params.aspect_ratio }); const json = await this.postImage("/images/generations", body, "生成图片", signal); return this.saveImageResponse(json, params.model ?? "unknown"); } async edit(params: EditParams, signal?: AbortSignal): Promise { const image = await resolveImageInput(params.image, this.config, signal); const form = new FormData(); form.set("model", params.model ?? ""); form.set("prompt", params.prompt); if (params.size && params.modelCapabilities?.supportsSize !== false) form.set("size", params.size); if (params.output_format) form.set("output_format", params.output_format); form.set("image", new Blob([toArrayBuffer(image.buffer)], { type: image.mimeType }), image.name); const json = await this.postImage("/images/edits", form, "编辑图片", signal); return this.saveImageResponse(json, params.model ?? "unknown"); } async variation(params: VariationParams, signal?: AbortSignal): Promise { const image = await resolveImageInput(params.image, this.config, signal); const form = new FormData(); form.set("model", params.model ?? ""); if (params.size && params.modelCapabilities?.supportsSize !== false) form.set("size", params.size); if (params.n && params.modelCapabilities?.supportsMultipleImages !== false) form.set("n", String(params.n)); form.set("image", new Blob([toArrayBuffer(image.buffer)], { type: image.mimeType }), image.name); const json = await this.postImage("/images/variations", form, "生成图片变体", signal); return this.saveImageResponse(json, params.model ?? "unknown"); } private async postImage(path: string, body: BodyInit | Record, action: string, signal?: AbortSignal): Promise { const isForm = body instanceof FormData; let response = await this.request("POST", path, isForm ? body : JSON.stringify(body), action, signal, isForm ? undefined : "application/json"); let detail = response.ok ? undefined : await responseErrorDetail(response, this.config); // 不同 OpenAI-compatible 服务对 response_format 的支持不一致; // auto 模式默认不发字段,若服务端反而要求字段,尝试补上 url; // 若用户明确 url/b64_json 而服务端拒绝该值,则降级为不发字段。 const jsonBody = !isForm && typeof body === "object" && body !== null && !ArrayBuffer.isView(body) && !(body instanceof ArrayBuffer) && !(body instanceof Blob) && !(body instanceof URLSearchParams) ? body as Record : undefined; if (!response.ok && jsonBody && isResponseFormatError(detail)) { if (jsonBody.response_format === undefined) { const retryBody = { ...jsonBody, response_format: "url" }; await sleep(800, signal); response = await this.request("POST", path, JSON.stringify(retryBody), action, signal, "application/json"); detail = response.ok ? undefined : await responseErrorDetail(response, this.config); } else { const retryBody = { ...jsonBody }; delete retryBody.response_format; await sleep(800, signal); response = await this.request("POST", path, JSON.stringify(retryBody), action, signal, "application/json"); detail = response.ok ? undefined : await responseErrorDetail(response, this.config); } } if (!response.ok) throw errorFromStatus(response.status, action, detail); const json = await parseJsonLimited(response, this.config.maxResponseBytes) as OpenAIImageResponse; if (!Array.isArray(json.data)) throw new ImageGenError("invalid_json", `${action}响应不包含 data 数组。`, "确认服务商图片接口响应是否 OpenAI-compatible。 "); return json; } private async request(method: string, path: string, body: BodyInit | undefined, action: string, signal?: AbortSignal, contentType?: string): Promise { const headers: Record = { Authorization: `Bearer ${this.config.apiKey}` }; if (contentType) headers["content-type"] = contentType; try { const init: RequestInit = { method, headers }; if (body !== undefined) init.body = body; if (signal !== undefined) init.signal = signal; return await fetchWithTimeout(`${this.config.baseUrl}${path}`, init, this.config.timeoutMs, { stallMs: this.config.timeoutMs, totalMs: this.config.totalTimeoutMs ?? 0 }); } catch (e) { if (e instanceof ImageGenError) throw new ImageGenError(e.code, sanitizeSecret(e.message, this.config.apiKey), e.suggestion, e.status); throw e; } } private modelFromRaw(item: unknown): ModelInfo | undefined { if (!item || typeof item !== "object") return undefined; const record = item as Record; const id = typeof record.id === "string" ? record.id : typeof record.name === "string" ? record.name : undefined; if (!id) return undefined; return this.modelFromId(id, record); } private modelFromId(id: string, metadata: Record): ModelInfo { // fallback 仅由用户在配置界面明确手动指定默认模型时产生,视为图片生成模型。 const fallback = metadata.fallback === true; const caps = detectCapabilities(id, metadata, this.config.manualCapabilities[id] ?? (fallback ? ["image_generation"] : undefined)); return { id, provider: this.type, rawMetadata: metadata, ...caps }; } private async saveImageResponse(json: OpenAIImageResponse, model: string): Promise { const images = []; for (const [index, item] of json.data?.entries() ?? []) { if (item.b64_json) { const decoded = decodeBase64Image(item.b64_json); images.push({ path: await saveImageBuffer(decoded.buffer, this.config.outputDir, decoded.ext), mimeType: decoded.mimeType, index }); } else { const rawUrl = extractImageValue(item.url) ?? extractImageValue(item.image_url) ?? extractImageValue(item.image) ?? extractImageValue((item as Record).image_base64); if (!rawUrl) continue; if (rawUrl.startsWith("data:")) { const decoded = decodeBase64Image(rawUrl); images.push({ path: await saveImageBuffer(decoded.buffer, this.config.outputDir, decoded.ext), mimeType: decoded.mimeType, index }); continue; } const imageUrl = resolveImageUrl(rawUrl, this.config.baseUrl); const authorization = isSameOrigin(imageUrl, this.config.baseUrl) ? `Bearer ${this.config.apiKey}` : undefined; const saved = await downloadImage(imageUrl, this.config.outputDir, this.config.downloadTimeoutMs ?? this.config.timeoutMs, this.config.maxDownloadBytes, authorization, { totalMs: this.config.totalTimeoutMs ?? 0 }); images.push({ path: saved.path, url: imageUrl, mimeType: saved.mimeType, index }); } } if (images.length === 0) throw new ImageGenError("invalid_image", "API 未返回可保存的图片。", "确认响应包含 url 或 b64_json。 "); return { provider: this.config.providerName, providerId: this.config.providerId, model, images, rawInfo: { created: json.created } }; } } async function resolveImageInput(input: string, config: ImageGenConfig, signal?: AbortSignal): Promise<{ name: string; buffer: Buffer; mimeType: string }> { if (/^https?:\/\//i.test(input)) { const authorization = isSameOrigin(input, config.baseUrl) ? `Bearer ${config.apiKey}` : undefined; const saved = await downloadImage(input, config.outputDir, config.downloadTimeoutMs ?? config.timeoutMs, config.maxDownloadBytes, authorization, { totalMs: config.totalTimeoutMs ?? 0 }); const local = await loadLocalImage(saved.path); return local; } return loadLocalImage(input); } async function responseErrorDetail(response: Response, config: ImageGenConfig): Promise { try { const raw = await readResponseTextLimited(response, Math.min(config.maxResponseBytes, 32_768)); let detail = raw; try { const json = JSON.parse(raw) as { error?: { message?: unknown }; message?: unknown }; detail = typeof json.error?.message === "string" ? json.error.message : typeof json.message === "string" ? json.message : raw; } catch { /* 非 JSON 错误页仅保留单行短摘要。 */ } const safe = sanitizeSecret(detail, config.apiKey).replace(/\s+/g, " ").trim(); return safe ? safe.slice(0, 500) : undefined; } catch { return undefined; } } function extractImageValue(value: unknown): string | undefined { if (typeof value === "string") return value; if (value && typeof value === "object") { const record = value as Record; if (typeof record.url === "string") return record.url; if (typeof record.b64_json === "string") return `data:image/png;base64,${record.b64_json}`; } return undefined; } function isResponseFormatError(detail?: string): boolean { return Boolean(detail && /response_format|b64_json|base64.*format|unsupported.*param|unrecognized.*param/i.test(detail)); } function sleep(ms: number, signal?: AbortSignal): Promise { return new Promise((resolve, reject) => { if (signal?.aborted) { reject(new ImageGenError("network_error", "请求已取消。", "重新发起操作即可。 ")); return; } const t = setTimeout(resolve, ms); signal?.addEventListener("abort", () => { clearTimeout(t); reject(new ImageGenError("network_error", "请求已取消。", "重新发起操作即可。 ")); }, { once: true }); }); } function isSameOrigin(imageUrl: string, baseUrl: string): boolean { try { return new URL(imageUrl).origin === new URL(baseUrl).origin; } catch { return false; } } function resolveImageUrl(raw: string, baseUrl: string): string { try { return new URL(raw, `${baseUrl}/`).toString(); } catch { throw new ImageGenError("invalid_image", "Provider 返回的图片地址无效。", "确认响应中的图片 URL 或图片字段是有效 http/https 地址。 "); } } function compact(obj: Record): Record { return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined && v !== "")); } function toArrayBuffer(buffer: Buffer): ArrayBuffer { return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength) as ArrayBuffer; }