416 lines
12 KiB
JavaScript
416 lines
12 KiB
JavaScript
import { Buffer } from 'node:buffer';
|
||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
const __dirname = path.dirname(__filename);
|
||
const repoRoot = path.resolve(__dirname, '..');
|
||
const outputDir = path.join(
|
||
repoRoot,
|
||
'public',
|
||
'branding',
|
||
'taonier-logo-braincore-concepts',
|
||
);
|
||
const timeoutMsDefault = 180000;
|
||
|
||
const args = new Map();
|
||
for (let index = 2; index < process.argv.length; index += 1) {
|
||
const raw = process.argv[index];
|
||
if (!raw.startsWith('--')) {
|
||
continue;
|
||
}
|
||
const next = process.argv[index + 1];
|
||
if (next && !next.startsWith('--')) {
|
||
args.set(raw, next);
|
||
index += 1;
|
||
} else {
|
||
args.set(raw, true);
|
||
}
|
||
}
|
||
|
||
const basePrompt = [
|
||
'请生成一枚全新的「陶泥儿」产品商标图形标概念稿,但画面中绝对不要出现任何中文字、英文字母、数字、符号字样或品牌字标。',
|
||
'产品定位:精品 AI UGC 轻休闲小游戏创作与传播平台,用户可以像捏陶泥一样把脑洞、梗和灵感塑造成小游戏或趣味内容。',
|
||
'核心隐喻:脑洞星核。一团奶白色、暖棕色调的不规则软方圆陶泥包裹一枚暖金色创意星核,表达灵感被捏造成型。',
|
||
'风格:扁平矢量商标为主,带非常轻微的软陶质感;结构简洁、边缘柔和、轮廓清晰,适合商标、App 图标、社区头像。',
|
||
'主色:奶白、米白、暖棕、陶土棕、少量暖金;整体温暖高级,不使用粉色、青色、蓝色、紫色、荧光色或彩虹色。',
|
||
'数字感:只允许在星核周围出现 2 到 4 个极小暖金星点或像素点,暗示 AI 生成、UGC 传播和轻游戏趣味。',
|
||
'构图:正方形画布,居中图形标,干净浅米白背景,留足安全边距;缩小到 64px 时仍能看清软方圆轮廓和星核。',
|
||
'禁止:脸、眼睛、嘴巴、表情、角色身体、吉祥物立绘、儿童黏土玩具感、陶艺工具、手、笔刷、复杂场景、按钮、UI、边框、水印、文字、商标字标、拟物甜品感。',
|
||
'必须保持全新构图,不参考任何既有陶泥儿 logo 方案,不做旋涡环形旧稿,不做三色花瓣旧稿。',
|
||
];
|
||
|
||
const variants = [
|
||
{
|
||
id: '01-minimal-braincore',
|
||
title: '极简脑洞星核',
|
||
prompt: [
|
||
...basePrompt,
|
||
'本张重点:极简。只保留一个奶白不规则软方圆陶泥主体、一个居中的暖金四角星核、2 个极小暖金星点。不要额外装饰。',
|
||
],
|
||
},
|
||
{
|
||
id: '02-soft-square-clay-seal',
|
||
title: '软方圆陶泥印记',
|
||
prompt: [
|
||
...basePrompt,
|
||
'本张重点:陶泥印记。主体像被轻轻按压成型的软方圆印章,边缘有自然手捏起伏,但不能像儿童玩具。星核略微偏心。',
|
||
],
|
||
},
|
||
{
|
||
id: '03-warm-brown-embedded-core',
|
||
title: '暖棕星核嵌入',
|
||
prompt: [
|
||
...basePrompt,
|
||
'本张重点:嵌入感。用暖棕内凹形或陶土棕阴影承托暖金星核,像灵感被嵌进陶泥里,仍保持扁平商标质感。',
|
||
],
|
||
},
|
||
{
|
||
id: '04-subtle-pinch-marks',
|
||
title: '轻微捏痕版本',
|
||
prompt: [
|
||
...basePrompt,
|
||
'本张重点:捏痕。在陶泥主体边缘加入 2 到 3 个极轻微暖棕捏痕或凹口,表现可塑性;捏痕必须抽象、克制、可矢量化。',
|
||
],
|
||
},
|
||
{
|
||
id: '05-premium-geometric-balance',
|
||
title: '精品几何比例',
|
||
prompt: [
|
||
...basePrompt,
|
||
'本张重点:精品比例。整体更接近高级互联网 App 图标,几何平衡、负形干净、软方圆轮廓稳定,陶泥质感只保留一点点。',
|
||
],
|
||
},
|
||
{
|
||
id: '06-soft-clay-texture',
|
||
title: '柔软陶泥质感',
|
||
prompt: [
|
||
...basePrompt,
|
||
'本张重点:柔软质感。在不破坏扁平矢量感的前提下,加入细腻奶油陶泥的微妙高光和暖棕渐层,不能变成 3D 玩具。',
|
||
],
|
||
},
|
||
{
|
||
id: '07-app-icon-ready',
|
||
title: 'App 图标优先',
|
||
prompt: [
|
||
...basePrompt,
|
||
'本张重点:App 图标。图形占画面约 72%,轮廓饱满有记忆点,星核清晰醒目,适合放入圆角方形 App icon。',
|
||
],
|
||
},
|
||
{
|
||
id: '08-trademark-monochrome-ready',
|
||
title: '商标黑白提炼',
|
||
prompt: [
|
||
...basePrompt,
|
||
'本张重点:商标注册。优先保证黑白化后仍清楚,减少渐变和细节,用奶白主体、暖棕负形和暖金星核形成强轮廓。',
|
||
],
|
||
},
|
||
];
|
||
|
||
function readDotenv(fileName) {
|
||
const filePath = path.join(repoRoot, fileName);
|
||
if (!existsSync(filePath)) {
|
||
return {};
|
||
}
|
||
|
||
const values = {};
|
||
for (const line of readFileSync(filePath, 'utf8').split(/\r?\n/u)) {
|
||
const trimmed = line.trim();
|
||
if (!trimmed || trimmed.startsWith('#')) {
|
||
continue;
|
||
}
|
||
const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/u.exec(trimmed);
|
||
if (!match) {
|
||
continue;
|
||
}
|
||
let value = match[2].trim();
|
||
if (
|
||
(value.startsWith('"') && value.endsWith('"')) ||
|
||
(value.startsWith("'") && value.endsWith("'"))
|
||
) {
|
||
value = value.slice(1, -1);
|
||
}
|
||
values[match[1]] = value;
|
||
}
|
||
return values;
|
||
}
|
||
|
||
function resolveEnv() {
|
||
const loaded = {
|
||
...readDotenv('.env.example'),
|
||
...readDotenv('.env.local'),
|
||
...readDotenv('.env.secrets.local'),
|
||
...process.env,
|
||
};
|
||
return {
|
||
baseUrl: String(loaded.VECTOR_ENGINE_BASE_URL || '')
|
||
.trim()
|
||
.replace(/\/+$/u, ''),
|
||
apiKey: String(loaded.VECTOR_ENGINE_API_KEY || '').trim(),
|
||
timeoutMs: Number.parseInt(
|
||
String(loaded.VECTOR_ENGINE_IMAGE_REQUEST_TIMEOUT_MS || timeoutMsDefault),
|
||
10,
|
||
),
|
||
};
|
||
}
|
||
|
||
function buildVectorEngineImagesGenerationUrl(baseUrl) {
|
||
return baseUrl.endsWith('/v1')
|
||
? `${baseUrl}/images/generations`
|
||
: `${baseUrl}/v1/images/generations`;
|
||
}
|
||
|
||
function buildRequestBody(variant) {
|
||
return {
|
||
model: 'gpt-image-2-all',
|
||
prompt: variant.prompt.join('\n'),
|
||
n: 1,
|
||
size: '1024x1024',
|
||
};
|
||
}
|
||
|
||
function collectStringsByKey(value, targetKey, output) {
|
||
if (Array.isArray(value)) {
|
||
value.forEach((entry) => collectStringsByKey(entry, targetKey, output));
|
||
return;
|
||
}
|
||
if (!value || typeof value !== 'object') {
|
||
return;
|
||
}
|
||
|
||
for (const [key, nested] of Object.entries(value)) {
|
||
if (key === targetKey) {
|
||
if (typeof nested === 'string' && nested.trim()) {
|
||
output.push(nested.trim());
|
||
}
|
||
if (Array.isArray(nested)) {
|
||
nested.forEach((entry) => {
|
||
if (typeof entry === 'string' && entry.trim()) {
|
||
output.push(entry.trim());
|
||
}
|
||
});
|
||
}
|
||
}
|
||
collectStringsByKey(nested, targetKey, output);
|
||
}
|
||
}
|
||
|
||
function extractImageUrls(payload) {
|
||
const urls = [];
|
||
collectStringsByKey(payload, 'url', urls);
|
||
collectStringsByKey(payload, 'image', urls);
|
||
collectStringsByKey(payload, 'image_url', urls);
|
||
return [...new Set(urls)].filter((url) => /^https?:\/\//u.test(url));
|
||
}
|
||
|
||
function extractBase64Images(payload) {
|
||
const values = [];
|
||
collectStringsByKey(payload, 'b64_json', values);
|
||
return values;
|
||
}
|
||
|
||
function inferExtensionFromContentType(contentType) {
|
||
const normalized = contentType.split(';')[0]?.trim().toLowerCase();
|
||
if (normalized === 'image/png') {
|
||
return 'png';
|
||
}
|
||
if (normalized === 'image/webp') {
|
||
return 'webp';
|
||
}
|
||
if (normalized === 'image/gif') {
|
||
return 'gif';
|
||
}
|
||
return 'jpg';
|
||
}
|
||
|
||
function inferExtensionFromBytes(bytes) {
|
||
if (bytes.subarray(0, 8).equals(Buffer.from('\x89PNG\r\n\x1A\n', 'binary'))) {
|
||
return 'png';
|
||
}
|
||
if (bytes.subarray(0, 3).equals(Buffer.from([0xff, 0xd8, 0xff]))) {
|
||
return 'jpg';
|
||
}
|
||
if (
|
||
bytes.subarray(0, 4).toString('ascii') === 'RIFF' &&
|
||
bytes.subarray(8, 12).toString('ascii') === 'WEBP'
|
||
) {
|
||
return 'webp';
|
||
}
|
||
return 'png';
|
||
}
|
||
|
||
async function fetchJson(url, options, timeoutMs) {
|
||
const abortController = new AbortController();
|
||
const timer = setTimeout(() => abortController.abort(), timeoutMs);
|
||
try {
|
||
const response = await fetch(url, {
|
||
...options,
|
||
signal: abortController.signal,
|
||
});
|
||
const text = await response.text();
|
||
if (!response.ok) {
|
||
throw new Error(`VectorEngine ${response.status}: ${text.slice(0, 600)}`);
|
||
}
|
||
return JSON.parse(text);
|
||
} catch (error) {
|
||
if (error?.name === 'AbortError') {
|
||
throw new Error(`VectorEngine request timed out after ${timeoutMs}ms`);
|
||
}
|
||
throw error;
|
||
} finally {
|
||
clearTimeout(timer);
|
||
}
|
||
}
|
||
|
||
async function downloadUrl(url, timeoutMs) {
|
||
const abortController = new AbortController();
|
||
const timer = setTimeout(() => abortController.abort(), timeoutMs);
|
||
try {
|
||
const response = await fetch(url, { signal: abortController.signal });
|
||
if (!response.ok) {
|
||
throw new Error(`download ${response.status}`);
|
||
}
|
||
const bytes = Buffer.from(await response.arrayBuffer());
|
||
return {
|
||
bytes,
|
||
extension: inferExtensionFromContentType(
|
||
response.headers.get('content-type') || 'image/jpeg',
|
||
),
|
||
};
|
||
} catch (error) {
|
||
if (error?.name === 'AbortError') {
|
||
throw new Error(`Generated image download timed out after ${timeoutMs}ms`);
|
||
}
|
||
throw error;
|
||
} finally {
|
||
clearTimeout(timer);
|
||
}
|
||
}
|
||
|
||
async function generateOne(env, variant) {
|
||
const requestBody = buildRequestBody(variant);
|
||
const payload = await fetchJson(
|
||
buildVectorEngineImagesGenerationUrl(env.baseUrl),
|
||
{
|
||
method: 'POST',
|
||
headers: {
|
||
Authorization: `Bearer ${env.apiKey}`,
|
||
Accept: 'application/json',
|
||
'Content-Type': 'application/json',
|
||
},
|
||
body: JSON.stringify(requestBody),
|
||
},
|
||
env.timeoutMs,
|
||
);
|
||
|
||
const urls = extractImageUrls(payload);
|
||
const b64Images = extractBase64Images(payload);
|
||
let image;
|
||
if (urls[0]) {
|
||
image = await downloadUrl(urls[0], env.timeoutMs);
|
||
} else if (b64Images[0]) {
|
||
const bytes = Buffer.from(b64Images[0], 'base64');
|
||
image = {
|
||
bytes,
|
||
extension: inferExtensionFromBytes(bytes),
|
||
};
|
||
} else {
|
||
throw new Error(`VectorEngine returned no image for ${variant.id}`);
|
||
}
|
||
|
||
mkdirSync(outputDir, { recursive: true });
|
||
const outputPath = path.join(outputDir, `taonier-braincore-${variant.id}.${image.extension}`);
|
||
writeFileSync(outputPath, image.bytes);
|
||
return outputPath;
|
||
}
|
||
|
||
function writeManifest(files) {
|
||
const manifestPath = path.join(outputDir, 'taonier-logo-braincore-manifest.json');
|
||
writeFileSync(
|
||
manifestPath,
|
||
`${JSON.stringify(
|
||
{
|
||
model: 'gpt-image-2-all',
|
||
size: '1024x1024',
|
||
generatedAt: new Date().toISOString(),
|
||
creativeDirection: {
|
||
name: '陶泥儿脑洞星核图形标',
|
||
textPolicy: 'no Chinese, no English, no wordmark',
|
||
palette: '奶白、米白、暖棕、陶土棕、少量暖金',
|
||
motif: '不规则软方圆陶泥团 + 脑洞星核 + 极少量星点',
|
||
},
|
||
variants: variants.map((variant) => ({
|
||
id: variant.id,
|
||
title: variant.title,
|
||
file: files.find((file) => path.basename(file).includes(variant.id))
|
||
? path.basename(files.find((file) => path.basename(file).includes(variant.id)))
|
||
: null,
|
||
prompt: variant.prompt.join('\n'),
|
||
})),
|
||
},
|
||
null,
|
||
2,
|
||
)}\n`,
|
||
'utf8',
|
||
);
|
||
return manifestPath;
|
||
}
|
||
|
||
const dryRun = args.has('--dry-run') || !args.has('--live');
|
||
const limit = Number.parseInt(String(args.get('--limit') || variants.length), 10);
|
||
const selectedVariants = variants.slice(0, limit);
|
||
|
||
if (dryRun) {
|
||
console.log(
|
||
JSON.stringify(
|
||
{
|
||
mode: 'dry-run',
|
||
outputDir,
|
||
count: selectedVariants.length,
|
||
requests: selectedVariants.map((variant) => ({
|
||
id: variant.id,
|
||
title: variant.title,
|
||
body: buildRequestBody(variant),
|
||
})),
|
||
},
|
||
null,
|
||
2,
|
||
),
|
||
);
|
||
process.exit(0);
|
||
}
|
||
|
||
const env = resolveEnv();
|
||
if (!env.baseUrl || !env.apiKey) {
|
||
console.error(
|
||
JSON.stringify({
|
||
ok: false,
|
||
error: 'Missing VECTOR_ENGINE_BASE_URL or VECTOR_ENGINE_API_KEY',
|
||
hasBaseUrl: Boolean(env.baseUrl),
|
||
hasApiKey: Boolean(env.apiKey),
|
||
}),
|
||
);
|
||
process.exit(1);
|
||
}
|
||
|
||
const generated = [];
|
||
for (const variant of selectedVariants) {
|
||
console.log(`Generating ${variant.id} ${variant.title}...`);
|
||
generated.push(await generateOne(env, variant));
|
||
}
|
||
|
||
const manifestPath = writeManifest(generated);
|
||
console.log(
|
||
JSON.stringify(
|
||
{
|
||
ok: true,
|
||
count: generated.length,
|
||
files: generated,
|
||
manifest: manifestPath,
|
||
},
|
||
null,
|
||
2,
|
||
),
|
||
);
|