#!/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 */ // 桥接模式:所有日志输出到 stderr,stdout 只输出最终 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); }