chore: 项目基线提交 - 拼豆图纸生成器 V1.0 核心代码

- 前端: React+Vite 首页/AI专区, 算法库(MARD色板/量化/背景去除), API对接
- 后端: Express+SQLite, 兑换码/订单/支付/chibi生成, 风控+云端同步
- CI/CD: GitHub Actions + Docker + nginx 配置
- 文档: PRD/DESIGN/LAUNCH/ROADMAP/E2E/竞品对照等 15 份
- 工程: 任务看板(taskboard) + 健康巡检(agent-health) + Git流程(gitflow)
This commit is contained in:
Sisyphus (主代理) committed 2026-08-13 21:40:27 +08:00
commit 02762400fb
174 files changed
+28059

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
import { Router } from "express";
import type { Request, Response } from "express";
import type Database from "better-sqlite3";
import { getDb } from "../db/index.js";
import { generateChibiImage, ChibiError, type GenerateRequest } from "./generate.js";
import { findTemplate, type ChibiCategory, type Composition } from "./prompts.js";
const CATEGORIES: readonly ChibiCategory[] = ["landscape", "person", "pet", "object", "food"];
const COMPOSITIONS: readonly Composition[] = ["主体特写", "半幅构图", "完整构图"];
/** Resolve the database instance: injected DB for tests, else global singleton. */
function resolveDb(injected?: Database.Database): Database.Database {
return injected ?? getDb();
}
/**
* Create the chibi router. Accepts an optional injected database for testing.
*
* POST /api/chibi/generate
* Request: { code, image, referenceImage?, category, subtype, gridSize,
* colorCount, composition, detailTags, customPrompt? }
* Response: 200 { image, access, downloadGrant }
* 400 参数错误 (中文提示)
* 402 { error: "生成次数不足" }
* 500 服务器内部错误
* 502 AI 图像生成失败
*/
export function createChibiRouter(injectedDb?: Database.Database): Router {
const router = Router();
router.post("/generate", async (req: Request, res: Response): Promise<void> => {
try {
const body = (req.body ?? {}) as Record<string, unknown>;
// ---- 基础字段校验 ----
const code = typeof body.code === "string" ? body.code : "";
if (!code) {
res.status(400).json({ error: "请输入兑换码" });
return;
}
const image = typeof body.image === "string" ? body.image : "";
if (!image) {
res.status(400).json({ error: "请上传图片" });
return;
}
const referenceImage =
typeof body.referenceImage === "string" && body.referenceImage !== ""
? body.referenceImage
: undefined;
// ---- 分类 / 细分模板 ----
const category = body.category as ChibiCategory;
if (!CATEGORIES.includes(category)) {
res.status(400).json({ error: "未知的分类" });
return;
}
const subtype = typeof body.subtype === "string" ? body.subtype : "";
try {
findTemplate(category, subtype);
} catch {
res.status(400).json({ error: "未知的模板类型" });
return;
}
// ---- 构图 / 网格 / 颜色 ----
const composition = body.composition as Composition;
if (!COMPOSITIONS.includes(composition)) {
res.status(400).json({ error: "未知的构图方式" });
return;
}
const gridSize = Number(body.gridSize);
if (!Number.isInteger(gridSize) || gridSize < 16 || gridSize > 512) {
res.status(400).json({ error: "网格尺寸无效" });
return;
}
const colorCount = Number(body.colorCount);
if (!Number.isInteger(colorCount) || colorCount < 4 || colorCount > 64) {
res.status(400).json({ error: "颜色数量无效" });
return;
}
// ---- 细节标签 / 自定义提示词 ----
const detailTags = Array.isArray(body.detailTags)
? body.detailTags.filter((t): t is string => typeof t === "string")
: [];
const customPrompt =
typeof body.customPrompt === "string" && body.customPrompt.trim() !== ""
? body.customPrompt
: undefined;
const payload: GenerateRequest = {
code,
image,
referenceImage,
category,
subtype,
gridSize,
colorCount,
composition,
detailTags,
customPrompt,
};
const result = await generateChibiImage(resolveDb(injectedDb), payload);
res.status(200).json({
image: result.image,
access: result.access,
downloadGrant: result.downloadGrant,
});
} catch (err) {
if (err instanceof ChibiError) {
res.status(err.status).json({ error: err.message });
} else {
console.error("POST /api/chibi/generate error:", err);
res.status(500).json({ error: "服务器内部错误,请稍后重试" });
}
}
});
return router;
}