316 lines
9.4 KiB
JavaScript
316 lines
9.4 KiB
JavaScript
import { Buffer } from 'node:buffer';
|
||
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
|
||
const repoRoot = process.cwd();
|
||
const outputDir = path.join(
|
||
repoRoot,
|
||
'public',
|
||
'branding',
|
||
'taonier-logo-hands-concepts',
|
||
);
|
||
const defaultTimeoutMs = 420000;
|
||
|
||
const concepts = [
|
||
{
|
||
id: 'taonier-hands-v2-cradle',
|
||
title: '掌心星核',
|
||
prompt:
|
||
'无文字扁平矢量 Logo,产品名“陶泥儿”。图形是上下两片抽象软掌,轻轻护住中央小星核,像把脑洞捏成作品。主流 App icon,简单、亲和、醒目、小尺寸清晰。珊瑚红、青绿、奶油白,最多 3 色。不要真实手指、播放三角、聊天气泡、笑脸、眼睛、花朵、褐色、文字、字母、3D。',
|
||
},
|
||
{
|
||
id: 'taonier-hands-v2-clap',
|
||
title: '合掌成型',
|
||
prompt:
|
||
'无文字扁平矢量 Logo,产品名“陶泥儿”。用上下两片圆润软形表现合掌捏合,中间一个小圆点正在成型,表达 AI 把灵感变成小游戏作品。图形完整、现代、亲和、可记忆。珊瑚红、薄荷青、奶白,最多 3 色。不要真实手掌、聊天气泡、播放键、笑脸、眼睛、花朵、褐色、碎元素、3D、文字。',
|
||
},
|
||
{
|
||
id: 'taonier-hands-v2-bowl',
|
||
title: '软掌托碗',
|
||
prompt:
|
||
'无文字扁平矢量 Logo,产品名“陶泥儿”。下方一片软掌像托碗,上方一片小软形轻压,中央浮出小星点,表达轻托脑洞、一捏成型。品牌感、主流、温暖、干净。青绿主形、珊瑚红辅助、奶白中心,最多 3 色。不要眼睛、嘴巴、聊天气泡、播放键、真实手、花朵、褐色、3D、文字。',
|
||
},
|
||
{
|
||
id: 'taonier-hands-v2-seal',
|
||
title: '双掌印记',
|
||
prompt:
|
||
'无文字扁平矢量 Logo,产品名“陶泥儿”。两片抽象软掌上下扣合,形成一个圆润印记,中间留出小星形负空间,像双手捏出的创意标记。简洁、亲和、高识别、适合 App icon。珊瑚红、奶油白、青绿,最多 3 色。不要真实手指、宗教手势、医疗标识、聊天气泡、播放三角、眼睛、花朵、褐色、3D、文字。',
|
||
},
|
||
{
|
||
id: 'taonier-hands-v2-pop',
|
||
title: '掌心开捏',
|
||
prompt:
|
||
'无文字扁平矢量 Logo,产品名“陶泥儿”。上下两片软掌像打开的胶囊,中央小星点从掌心弹出,表达脑洞被捏出来。年轻、亲和、醒目、主流娱乐创作 App 风格。亮珊瑚红、薄荷青、奶白,最多 3 色。不要聊天气泡、播放键、笑脸、眼睛、花朵、真实手指、褐色、碎元素、3D、文字。',
|
||
},
|
||
];
|
||
|
||
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);
|
||
}
|
||
}
|
||
|
||
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 || defaultTimeoutMs),
|
||
10,
|
||
),
|
||
};
|
||
}
|
||
|
||
function buildUrl(baseUrl) {
|
||
return baseUrl.endsWith('/v1')
|
||
? `${baseUrl}/images/generations`
|
||
: `${baseUrl}/v1/images/generations`;
|
||
}
|
||
|
||
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 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}`);
|
||
}
|
||
return Buffer.from(await response.arrayBuffer());
|
||
} catch (error) {
|
||
if (error?.name === 'AbortError') {
|
||
throw new Error(`Generated image download timed out after ${timeoutMs}ms`);
|
||
}
|
||
throw error;
|
||
} finally {
|
||
clearTimeout(timer);
|
||
}
|
||
}
|
||
|
||
async function generateConcept(env, concept) {
|
||
const requestBody = {
|
||
model: 'gpt-image-2-all',
|
||
prompt: concept.prompt,
|
||
n: 1,
|
||
size: '1024x1024',
|
||
};
|
||
const payload = await fetchJson(
|
||
buildUrl(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 bytes;
|
||
if (urls[0]) {
|
||
bytes = await downloadUrl(urls[0], env.timeoutMs);
|
||
} else if (b64Images[0]) {
|
||
bytes = Buffer.from(b64Images[0], 'base64');
|
||
} else {
|
||
throw new Error(`VectorEngine returned no image for ${concept.id}`);
|
||
}
|
||
|
||
mkdirSync(outputDir, { recursive: true });
|
||
const extension = inferExtensionFromBytes(bytes);
|
||
const outputPath = path.join(outputDir, `${concept.id}.${extension}`);
|
||
writeFileSync(outputPath, bytes);
|
||
return outputPath;
|
||
}
|
||
|
||
const dryRun = args.has('--dry-run') || !args.has('--live');
|
||
const onlyIds = String(args.get('--only') || '')
|
||
.split(',')
|
||
.map((value) => value.trim())
|
||
.filter(Boolean);
|
||
const selected = concepts.filter(
|
||
(concept) => !onlyIds.length || onlyIds.includes(concept.id),
|
||
);
|
||
|
||
if (dryRun) {
|
||
console.log(
|
||
JSON.stringify(
|
||
{
|
||
mode: 'dry-run',
|
||
outputDir,
|
||
count: selected.length,
|
||
requests: selected.map((concept) => ({
|
||
id: concept.id,
|
||
title: concept.title,
|
||
body: {
|
||
model: 'gpt-image-2-all',
|
||
prompt: concept.prompt,
|
||
n: 1,
|
||
size: '1024x1024',
|
||
},
|
||
})),
|
||
},
|
||
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 concept of selected) {
|
||
console.log(`Generating ${concept.id}...`);
|
||
generated.push(await generateConcept(env, concept));
|
||
}
|
||
|
||
console.log(
|
||
JSON.stringify(
|
||
{
|
||
ok: true,
|
||
count: generated.length,
|
||
files: generated,
|
||
verifiedFiles: readdirSync(outputDir).sort(),
|
||
},
|
||
null,
|
||
2,
|
||
),
|
||
);
|