/** * chibi AI 拼豆图像生成服务 * * 流程: 先消耗一次兑换码次数(无剩余次数抛 生成次数不足) -> buildPrompt * 组装提示词 -> 调用 OpenAI 兼容图像接口 (AI_IMAGE_API_URL / * AI_IMAGE_API_KEY),未配置接口地址时使用内置 mock 模式生成占位图。 */ import type Database from "better-sqlite3"; import { randomUUID } from "node:crypto"; import { deflateSync } from "node:zlib"; import type { AccessInfo } from "../types/index.js"; import { consumeCode, CodeError } from "../codes/service.js"; import { buildPrompt, type ChibiCategory, type Composition } from "./prompts.js"; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface GenerateRequest { /** 兑换码 */ code: string; /** 用户上传的拼豆原图 (base64 dataURL) */ image: string; /** 参考风格图 (base64 dataURL),可选 */ referenceImage?: string; category: ChibiCategory; subtype: string; gridSize: number; colorCount: number; composition: Composition; detailTags: string[]; customPrompt?: string; } export interface GenerateResult { /** 生成的图像 (base64 dataURL) */ image: string; /** 下载凭证 */ downloadGrant: string; } /** 生成成功时附带消耗后的 access 信息,供路由层返回。 */ export type GenerateOutcome = GenerateResult & { access: AccessInfo }; export class ChibiError extends Error { readonly status: number; constructor(message: string, status: number) { super(message); this.name = "ChibiError"; this.status = status; } } // --------------------------------------------------------------------------- // Mock 占位图 (纯函数实现的最小合法 PNG: 纯色 RGB + gridSize 尺寸) // --------------------------------------------------------------------------- const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); const CRC_TABLE = (() => { const table = new Uint32Array(256); for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) { c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; } table[n] = c >>> 0; } return table; })(); function crc32(buf: Buffer): number { let c = 0xffffffff; for (let i = 0; i < buf.length; i++) { c = CRC_TABLE[(c ^ buf[i]!) & 0xff]! ^ (c >>> 8); } return (c ^ 0xffffffff) >>> 0; } function pngChunk(type: string, data: Buffer): Buffer { const typeBuf = Buffer.from(type, "ascii"); const header = Buffer.alloc(4); header.writeUInt32BE(data.length, 0); const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])), 0); return Buffer.concat([header, typeBuf, data, crc]); } /** 生成 size×size 纯色 PNG,返回 base64 dataURL。 */ function makePlaceholderPng(size: number, rgb: readonly [number, number, number]): string { const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(size, 0); // width ihdr.writeUInt32BE(size, 4); // height ihdr.writeUInt8(8, 8); // bit depth ihdr.writeUInt8(2, 9); // color type: truecolor RGB // bytes 10/11/12: compression / filter / interlace,默认 0 const row = Buffer.alloc(1 + size * 3); row.writeUInt8(0, 0); // filter: none for (let x = 0; x < size; x++) { row.writeUInt8(rgb[0], 1 + x * 3); row.writeUInt8(rgb[1], 2 + x * 3); row.writeUInt8(rgb[2], 3 + x * 3); } const raw = Buffer.concat(Array.from({ length: size }, () => row)); const png = Buffer.concat([ PNG_SIGNATURE, pngChunk("IHDR", ihdr), pngChunk("IDAT", deflateSync(raw)), pngChunk("IEND", Buffer.alloc(0)), ]); return `data:image/png;base64,${png.toString("base64")}`; } /** 每类一个主题色,mock 模式下占位图随分类变色,便于肉眼区分。 */ const CATEGORY_COLORS: Record = { landscape: [96, 165, 250], // 天空蓝 person: [251, 146, 60], // 活力橙 pet: [167, 139, 250], // 香芋紫 object: [52, 211, 153], // 抹茶绿 food: [251, 113, 133], // 蜜桃粉 }; // --------------------------------------------------------------------------- // AI 接口调用 (OpenAI 兼容) // --------------------------------------------------------------------------- /** * 从 OpenAI 兼容响应中提取 base64 (或图像 url)。 * 支持两种形态: * /v1/images/generations -> { data: [{ b64_json | url }] } * /v1/chat/completions -> { choices: [{ message: { images: [{ b64_json | url }] } }] } */ function extractImageBase64(payload: unknown): string | null { if (!payload || typeof payload !== "object") return null; const root = payload as Record; const data = Array.isArray(root["data"]) ? root["data"] : []; const first = data[0] as Record | undefined; if (first) { if (typeof first["b64_json"] === "string") return first["b64_json"]; if (typeof first["url"] === "string") return first["url"]; } const choices = Array.isArray(root["choices"]) ? root["choices"] : []; const choice = choices[0] as Record | undefined; const message = choice?.["message"] as Record | undefined; const images = Array.isArray(message?.["images"]) ? message["images"] : []; const image = images[0] as Record | undefined; if (image) { if (typeof image["b64_json"] === "string") return image["b64_json"]; if (typeof image["url"] === "string") return image["url"]; } return null; } async function requestAiImage(prompt: string, req: GenerateRequest): Promise { const apiUrl = process.env["AI_IMAGE_API_URL"]; const apiKey = process.env["AI_IMAGE_API_KEY"]; const model = process.env["AI_IMAGE_MODEL"] ?? "gpt-image-1"; const headers: Record = { "Content-Type": "application/json" }; if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`; // chat/completions 走多模态消息 (携带原图/参考图),images/generations 走纯提示词 const isChat = (apiUrl ?? "").includes("/chat/completions"); let payload: unknown; if (isChat) { const content: Array> = [{ type: "text", text: prompt }]; for (const img of [req.image, req.referenceImage]) { if (img && img.startsWith("data:")) { content.push({ type: "image_url", image_url: { url: img } }); } } payload = { model, messages: [{ role: "user", content }] }; } else { payload = { model, prompt, n: 1, size: `${req.gridSize}x${req.gridSize}`, response_format: "b64_json", }; } let res: Awaited>; try { res = await fetch(apiUrl!, { method: "POST", headers, body: JSON.stringify(payload), signal: AbortSignal.timeout(120_000), }); } catch (err) { console.error("chibi AI image request failed:", err); throw new ChibiError("AI 图像生成失败,请稍后重试", 502); } if (!res.ok) { console.error("chibi AI image api error:", res.status, await res.text().catch(() => "")); throw new ChibiError("AI 图像生成失败,请稍后重试", 502); } const raw = (await res.json().catch(() => null)) as unknown; const b64OrUrl = extractImageBase64(raw); if (!b64OrUrl) { throw new ChibiError("AI 图像生成失败,请稍后重试", 502); } if (b64OrUrl.startsWith("http://") || b64OrUrl.startsWith("https://")) { // 接口返回 url 时下载并转为 base64 dataURL let imgRes: Awaited>; try { imgRes = await fetch(b64OrUrl, { signal: AbortSignal.timeout(60_000) }); } catch (err) { console.error("chibi AI image download failed:", err); throw new ChibiError("AI 图像生成失败,请稍后重试", 502); } if (!imgRes.ok) { throw new ChibiError("AI 图像生成失败,请稍后重试", 502); } const buf = Buffer.from(await imgRes.arrayBuffer()); const type = imgRes.headers.get("content-type") ?? "image/png"; return `data:${type};base64,${buf.toString("base64")}`; } return b64OrUrl.startsWith("data:") ? b64OrUrl : `data:image/png;base64,${b64OrUrl}`; } /** * 生成图像。未配置 AI_IMAGE_API_URL 时进入 mock 模式,返回 gridSize * 尺寸的纯色占位 PNG (dataURL)。 */ async function generateImage(prompt: string, req: GenerateRequest): Promise { const apiUrl = process.env["AI_IMAGE_API_URL"]; if (!apiUrl) { return makePlaceholderPng(req.gridSize, CATEGORY_COLORS[req.category]); } return requestAiImage(prompt, req); } // --------------------------------------------------------------------------- // 主入口 // --------------------------------------------------------------------------- /** * 生成 chibi 拼豆图。先原子消耗一次兑换码次数(无剩余次数时抛出 * ChibiError "生成次数不足", 402),再调用 AI 接口。 */ export async function generateChibiImage( db: Database.Database, req: GenerateRequest, ): Promise { // 1. 组装提示词 const prompt = buildPrompt({ category: req.category, subtype: req.subtype, gridSize: req.gridSize, colorCount: req.colorCount, composition: req.composition, detailTags: req.detailTags, customPrompt: req.customPrompt, hasReferenceImage: req.referenceImage !== undefined && req.referenceImage !== "", }); // 2. 消耗一次次数 (原子校验 + 扣减) let access: AccessInfo; try { access = consumeCode(db, req.code); } catch (err) { if (err instanceof CodeError) { // 未找到 / 禁用 / 过期 / 用尽 统一视为无法生成 throw new ChibiError("生成次数不足", 402); } throw err; } // 3. 生成图片 const image = await generateImage(prompt, req); return { image, downloadGrant: randomUUID(), access }; }