UNPKG

@xiaoluo_aigc0708/aigc-sdk

Version:

AI智能建筑 - 完整的AIGC图片生成SDK

215 lines 7.92 kB
import { AIGCError, WorkflowType } from '../../types/src/index'; /** * ComfyUI客户端 */ export class ComfyUIClient { constructor(config) { this.config = config; } /** * 发送工作流请求 */ async sendWorkflowRequest(request) { const apiUrl = 'http://100.72.216.20:7865/api/comfy/run_workflow'; try { const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(request), }); if (!response.ok) { throw new Error(`ComfyUI API请求失败: ${response.status}`); } const result = await response.json(); return { success: true, data: { prompt_id: result.prompt_id, images: result.images || [], detail: result.detail || 'Workflow sent successfully' } }; } catch (error) { console.error('❌ ComfyUI请求失败:', error); throw new AIGCError(`ComfyUI请求失败: ${error.message}`, 'COMFYUI_REQUEST_FAILED', error); } } /** * 获取图片URL */ getImageUrl(filename) { const comfyuiUrl = 'http://100.72.216.20:8288'; return `${comfyuiUrl}/view?filename=${encodeURIComponent(filename)}`; } /** * 验证工作流参数 */ validateWorkflowParams(params) { return validateWorkflowParams(params); } /** * 获取工作流信息 */ getWorkflowInfo() { return getWorkflowInfo(); } } /** * 工作流构建器 - 完全按照后端API规范 */ export class WorkflowBuilder { // 1. 文生图(无参考) static createTextToImageRequest(params) { const { prompt, width = 1024, height = 1024, batch_size = 2 } = params; return { workflow: WorkflowType.TEXT_TO_IMAGE, params: { "13.user_prompt": prompt, // 提示词 "18.width": width, // 需要生成的图片宽度 "18.height": height, // 需要生成的图片高度 "18.batch_size": batch_size, // 需要生成的图片数量 "20.filename_prefix": "render_text" // 保存图片的前缀 } }; } // 2. 文生图(有参考) static createTextToImageWithRefRequest(params) { const { prompt, width = 1024, height = 1024, batch_size = 2, ref_image_url } = params; return { workflow: WorkflowType.TEXT_TO_IMAGE_WITH_REF, params: { "9.user_prompt": prompt, // 提示词 "3.image_url": ref_image_url, // 输入的参考图片URL "17.width": width, // 需要生成的图片宽度 "17.height": height, // 需要生成的图片高度 "17.batch_size": batch_size, // 需要生成的图片数量 "20.filename_prefix": "render_ref-text" // 保存图片的前缀 } }; } // 3. 图生图(无参考) static createImageToImageRequest(params) { const { prompt, batch_size = 2, image_url } = params; return { workflow: WorkflowType.IMAGE_TO_IMAGE, params: { "67.image_url": image_url, // 输入的基底图片URL "60.text": prompt, // 提示词 "42.batch_size": batch_size, // 需要生成的图片数量 "68.filename_prefix": "render_image-text" // 保存图片的前缀 } }; } // 4. 图生图(有参考) static createImageToImageWithRefRequest(params) { const { prompt, batch_size = 2, image_url, ref_image_url } = params; return { workflow: WorkflowType.IMAGE_TO_IMAGE_WITH_REF, params: { "115.image_url": image_url, // 输入的基底图片URL "116.image_url": ref_image_url, // 输入的参考图片URL "107.text": prompt, // 提示词 "20.batch_size": batch_size, // 需要生成的图片数量 "117.filename_prefix": "render_image-ref-text" // 保存图片的前缀 } }; } } // 智能工作流选择器 - 从UI参数自动选择合适的工作流 export function selectWorkflowAndCreateRequest(params) { const { prompt, width = 1024, height = 1024, n_iter = 2, // 文生图使用 n_iter batch_size = 2, // 图生图使用 batch_size input_image, style_image } = params; // 根据输入参数智能选择工作流 if (input_image) { // 图生图模式 - 使用 batch_size const finalBatchSize = batch_size || n_iter || 2; if (style_image) { // 图生图(有参考) return WorkflowBuilder.createImageToImageWithRefRequest({ prompt, batch_size: finalBatchSize, image_url: input_image, ref_image_url: style_image }); } else { // 图生图(无参考) return WorkflowBuilder.createImageToImageRequest({ prompt, batch_size: finalBatchSize, image_url: input_image }); } } else { // 文生图模式 - 使用 n_iter 作为 batch_size const finalBatchSize = n_iter || batch_size || 2; if (style_image) { // 文生图(有参考) return WorkflowBuilder.createTextToImageWithRefRequest({ prompt, width, height, batch_size: finalBatchSize, ref_image_url: style_image }); } else { // 文生图(无参考) return WorkflowBuilder.createTextToImageRequest({ prompt, width, height, batch_size: finalBatchSize }); } } } // 工作流参数验证 export function validateWorkflowParams(params) { const errors = []; if (!params.prompt || params.prompt.trim() === '') { errors.push('提示词不能为空'); } // 如果是图生图模式,必须有基底图片 if (params.input_image && !params.input_image.startsWith('http')) { errors.push('基底图片URL格式不正确'); } // 如果有风格参考,检查URL格式 if (params.style_image && !params.style_image.startsWith('http')) { errors.push('风格参考图片URL格式不正确'); } return { isValid: errors.length === 0, errors }; } // 导出工作流类型用于调试 export function getWorkflowInfo() { return { workflows: { [WorkflowType.TEXT_TO_IMAGE]: { name: '文生图(无参考)', params: ['13.user_prompt', '18.width', '18.height', '18.batch_size', '20.filename_prefix'] }, [WorkflowType.TEXT_TO_IMAGE_WITH_REF]: { name: '文生图(有参考)', params: ['9.user_prompt', '3.image_url', '17.width', '17.height', '17.batch_size', '20.filename_prefix'] }, [WorkflowType.IMAGE_TO_IMAGE]: { name: '图生图(无参考)', params: ['67.image_url', '60.text', '42.batch_size', '68.filename_prefix'] }, [WorkflowType.IMAGE_TO_IMAGE_WITH_REF]: { name: '图生图(有参考)', params: ['115.image_url', '116.image_url', '107.text', '20.batch_size', '117.filename_prefix'] } }, apiEndpoint: 'http://100.72.216.20:7865/api/comfy/run_workflow' }; } //# sourceMappingURL=index.js.map