cang-ying/engines/image-api-bridge.js
Guanghu Domestic Migration a2fa7d57d8 chore: import sanitized domestic snapshot for REPO-005
Source snapshot: 23037fa3a2e9b0597162735755e92fc763bf4d94
2026-07-17 15:55:48 +08:00

65 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* image-api-bridge.js · Python ↔ Node 图片API桥接
* D144 · 铸渊 ICE-GL-ZY001
*
* Python 通过 subprocess 调用本脚本stdin 输入 JSON 命令stdout 输出 JSON 结果。
* 解决 char-hero-design-packer.py 依赖 JS image-api-adapter 的问题。
*
* 用法:
* echo '{"action":"generate_character_ref","charId":"CHAR-003","l0":"...","l1":"..."}' | node image-api-bridge.js
* echo '{"action":"generate_image","prompt":"...","size":"2048x2048","filename":"test.png"}' | node image-api-bridge.js
*/
// 桥接模式:所有日志输出到 stderrstdout 只输出最终 JSON 结果
console.log = (...args) => process.stderr.write(args.join(' ') + '\n');
console.warn = (...args) => process.stderr.write(args.join(' ') + '\n');
console.error = (...args) => process.stderr.write(args.join(' ') + '\n');
const { generateImage, generateCharacterRef } = require('./image-api-adapter');
let input = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', chunk => input += chunk);
process.stdin.on('end', async () => {
try {
const req = JSON.parse(input || '{}');
let result;
switch (req.action) {
case 'generate_character_ref': {
const { charId, l0, l1 } = req;
if (!charId) throw new Error('缺少 charId');
result = await generateCharacterRef({
charId,
l0Descriptor: l0 || '中国古代修仙少年16岁亚洲面孔',
l1Config: l1 ? { clothing: l1 } : undefined,
});
break;
}
case 'generate_image': {
const { prompt, size, filename, outputPath } = req;
if (!prompt) throw new Error('缺少 prompt');
result = await generateImage({ prompt, size, filename, outputPath });
break;
}
default:
throw new Error(`未知操作: ${req.action}`);
}
process.stdout.write(JSON.stringify({ ok: true, data: result }) + '\n');
process.exit(0);
} catch (err) {
process.stdout.write(JSON.stringify({ ok: false, error: err.message }) + '\n');
process.exit(1);
}
});
// stdin 已关闭pipe 模式),直接触发 end
if (process.stdin.isTTY) {
process.stderr.write('用法: echo \'{"action":"..."}\' | node image-api-bridge.js\n');
process.exit(1);
}