UNPKG

z-ai-web-dev-sdk

Version:

SDK for Z AI Web Dev

198 lines (193 loc) 6.97 kB
#!/usr/bin/env node import fs from 'fs/promises'; import path from 'path'; import { fileURLToPath } from 'url'; // 动态导入 ZAI,支持全局安装 async function getZAI() { try { // 尝试从包名导入(全局安装时) const packageName = 'z-ai-web-dev-sdk'; const { default: ZAI } = await import(packageName); return ZAI; } catch (error1) { try { // 尝试从全局node_modules直接导入(容器环境修复) const globalPackagePath = '/usr/lib/node_modules/z-ai-web-dev-sdk/dist/index.js'; const { default: ZAI } = await import(globalPackagePath); return ZAI; } catch (error2) { try { // 动态获取全局路径 const { execSync } = await import('child_process'); const globalRoot = execSync('npm root -g').toString().trim(); const globalPackagePath = path.join(globalRoot, 'z-ai-web-dev-sdk', 'dist', 'index.js'); const { default: ZAI } = await import(globalPackagePath); return ZAI; } catch (error3) { // 最后回退到相对路径导入(开发时) const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const indexPath = path.resolve(__dirname, './index.js'); const { default: ZAI } = await import(indexPath); return ZAI; } } } } function showHelp() { console.log(` Z-AI SDK CLI - 图片生成工具 用法: z-ai-generate [选项] 选项: --prompt, -p <文本> 必需:图片描述文本 --output, -o <路径> 必需:输出图片文件路径(png 格式) --model, -m <模型> 可选:模型代码 (默认: cogview-4-250304) --size, -s <尺寸> 可选:图片尺寸 (默认: 1024x1024) 支持的尺寸: 1024x1024, 768x1344, 864x1152, 1344x768, 1152x864, 1440x720, 720x1440 --help, -h 显示帮助信息 示例: z-ai-generate --prompt "一只可爱的小猫" --output "./cat.png" z-ai-generate -p "美丽的风景" -o "./landscape.jpg" -q hd -s 1344x768 注意: - 需要在项目目录、用户主目录或 /etc 下创建 .z-ai-config 配置文件 - 配置文件格式:{"baseUrl": "your-api-url", "apiKey": "your-api-key"} `); } function parseArgs(args) { const options = { prompt: '', output: '', model: 'cogview-4-250304', // quality: 'standard', size: '1024x1024', }; for (let i = 0; i < args.length; i++) { const arg = args[i]; const nextArg = args[i + 1]; switch (arg) { case '--prompt': case '-p': if (!nextArg) { throw new Error('--prompt 参数缺少值'); } options.prompt = nextArg; i++; break; case '--output': case '-o': if (!nextArg) { throw new Error('--output 参数缺少值'); } options.output = nextArg; i++; break; case '--model': case '-m': if (!nextArg) { throw new Error('--model 参数缺少值'); } options.model = nextArg; i++; break; // case '--quality': // case '-q': // if (!nextArg) { // throw new Error('--quality 参数缺少值'); // } // if (nextArg !== 'standard' && nextArg !== 'hd') { // throw new Error('--quality 必须是 standard 或 hd'); // } // options.quality = nextArg as 'standard' | 'hd'; // i++; // break; case '--size': case '-s': if (!nextArg) { throw new Error('--size 参数缺少值'); } const validSizes = ['1024x1024', '768x1344', '864x1152', '1344x768', '1152x864', '1440x720', '720x1440']; if (!validSizes.includes(nextArg)) { throw new Error(`--size 必须是以下值之一: ${validSizes.join(', ')}`); } options.size = nextArg; i++; break; case '--help': case '-h': options.help = true; break; default: if (arg.startsWith('-')) { throw new Error(`未知参数: ${arg}`); } break; } } return options; } function base64ToBuffer(base64Data) { return Buffer.from(base64Data, 'base64'); } async function saveImage(base64Data, outputPath) { try { const buffer = base64ToBuffer(base64Data); // 确保输出目录存在 const outputDir = path.dirname(outputPath); await fs.mkdir(outputDir, { recursive: true }); // 保存图片 await fs.writeFile(outputPath, buffer); console.log(`✅ 图片已保存到: ${outputPath}`); } catch (error) { throw new Error(`保存图片失败: ${error}`); } } async function main() { try { const args = process.argv.slice(2); const options = parseArgs(args); if (options.help) { showHelp(); return; } if (!options.prompt) { console.error('❌ 错误: 缺少必需参数 --prompt'); showHelp(); process.exit(1); } if (!options.output) { console.error('❌ 错误: 缺少必需参数 --output'); showHelp(); process.exit(1); } console.log('🚀 正在初始化 Z-AI SDK...'); const ZAI = await getZAI(); const client = await ZAI.create(); console.log(`🎨 正在生成图片: "${options.prompt}"`); // console.log(`🎯 质量: ${options.quality}`); console.log(`📐 尺寸: ${options.size}`); const response = await client.images.generations.create({ model: options.model, prompt: options.prompt, // quality: options.quality, size: options.size, }); if (!response.data || response.data.length === 0) { throw new Error('API 未返回图片数据'); } const imageBase64 = response.data[0].base64; await saveImage(imageBase64, options.output); console.log('🎉 图片生成完成!'); } catch (error) { console.error('❌ 错误:', error instanceof Error ? error.message : error); process.exit(1); } } // 直接运行主函数 main();