UNPKG

n8n-nodes-comfyuinew

Version:
232 lines 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Comfyui = void 0; const n8n_workflow_1 = require("n8n-workflow"); const jimp_1 = require("jimp"); class Comfyui { constructor() { this.description = { displayName: 'ComfyUI', name: 'comfy', icon: 'file:comfy.svg', group: ['transform'], version: 1, description: 'Execute ComfyUI workflows', defaults: { name: 'ComfyUI', }, credentials: [ { name: 'comfyUIApi', required: true, }, ], inputs: ['main'], outputs: ['main'], properties: [ { displayName: 'Workflow JSON', name: 'workflow', type: 'string', typeOptions: { rows: 10, }, default: '', required: true, description: 'The ComfyUI workflow in JSON format', } ], }; } async execute() { const items = this.getInputData(); const credentials = await this.getCredentials('comfyUIApi'); const apiUrl = credentials.apiUrl; const apiKey = credentials.apiKey; console.log('[ComfyUI] Executing with API URL:', apiUrl); const headers = { 'Content-Type': 'application/json', }; if (apiKey) { console.log('[ComfyUI] Using API key authentication'); headers['Authorization'] = `Bearer ${apiKey}`; } let returnData = []; for (let i = 0; i < items.length; i++) { const workflow = this.getNodeParameter('workflow', i); const newData = await tryGenerateContentWithComfy(this, apiUrl, headers, workflow); newData.map(item => returnData.push(item)); } console.log("**************************************************************"); console.log("**************************************************************"); return [returnData]; } } exports.Comfyui = Comfyui; async function tryGenerateContentWithComfy(brain, apiUrl, headers, workflow) { var _a, _b; try { console.log('[ComfyUI] Checking API connection...'); await brain.helpers.request({ method: 'GET', url: `${apiUrl}/system_stats`, headers, json: true, }); console.log('[ComfyUI] Queueing prompt...'); const response = await brain.helpers.request({ method: 'POST', url: `${apiUrl}/prompt`, headers, body: { prompt: JSON.parse(workflow), }, json: true, }); if (!response.prompt_id) { throw new n8n_workflow_1.NodeApiError(brain.getNode(), { message: 'Failed to get prompt ID from ComfyUI' }); } const promptId = response.prompt_id; console.log('[ComfyUI] Prompt queued with ID:', promptId); let attempts = 0; let maxAttempts = 1; await new Promise(resolve => setTimeout(resolve, 5000)); while (attempts < maxAttempts) { console.log(`[ComfyUI] Checking execution status (attempt: ${attempts + 1})...`); await new Promise(resolve => setTimeout(resolve, 1000)); attempts++; maxAttempts += 1; const history = await brain.helpers.request({ method: 'GET', url: `${apiUrl}/history/${promptId}`, headers, json: true, }); const promptResult = history[promptId]; if (!promptResult) { console.log('[ComfyUI] Prompt not found in history'); continue; } if (promptResult.status === undefined) { console.log('[ComfyUI] Execution status not found'); continue; } if ((_a = promptResult.status) === null || _a === void 0 ? void 0 : _a.completed) { console.log('[ComfyUI] Execution completed'); if (((_b = promptResult.status) === null || _b === void 0 ? void 0 : _b.status_str) === 'error') { throw new n8n_workflow_1.NodeApiError(brain.getNode(), { message: '[ComfyUI] Workflow execution failed' }); } if (!promptResult.outputs) { throw new n8n_workflow_1.NodeApiError(brain.getNode(), { message: '[ComfyUI] No outputs found in workflow result' }); } const filesToProcess = Object.values(promptResult.outputs).flatMap((nodeOutput) => { const files = []; if (nodeOutput.images) { files.push(...nodeOutput.images); } if (nodeOutput.gifs) { files.push(...nodeOutput.gifs); } return files; }).filter((file) => file.type === 'output' || file.type === 'temp'); const outputs = await Promise.all(filesToProcess.map(async (file) => { console.log("FILE: ", file); const isVideo = file.filename.endsWith('.mp4') || file.filename.endsWith('.webm'); console.log(`[ComfyUI] Downloading ${file.type} file:`, file.filename); const fileUrl = `${apiUrl}/view?filename=${file.filename}&subfolder=${file.subfolder || ''}&type=${file.type || ''}`; console.log("fileUrl: ", fileUrl); try { const fileData = await brain.helpers.request({ method: 'GET', url: fileUrl, resolveWithFullResponse: true, encoding: null, headers, }); const localPath = fileData.headers["local-file-path"]; console.log("File stored at:", localPath); let item; if (isVideo) { const videoBuffer = Buffer.from(fileData); const base64 = videoBuffer.toString('base64'); const ext = file.filename.endsWith('.webm') ? 'webm' : 'mp4'; const mime = ext === 'mp4' ? 'video/mp4' : 'video/webm'; item = { json: { filename: file.filename, type: file.type, subfolder: file.subfolder || '', fileUrl: fileUrl, data: base64, }, binary: { data: { fileName: file.filename, data: base64, fileType: 'video', fileSize: Math.round(videoBuffer.length / 1024 * 10) / 10 + " kB", fileExtension: ext, mimeType: mime, } } }; } else { const image = await jimp_1.Jimp.read(fileData); const ext = file.filename.endsWith('.jpeg') ? 'jpeg' : 'png'; const mime = ext === 'jpeg' ? 'image/jpeg' : 'image/png'; let outputBuffer; if (ext === 'jpeg') { outputBuffer = await image.getBuffer("image/jpeg", { quality: 80 }); } else { outputBuffer = await image.getBuffer(`image/png`); } const outputBase64 = outputBuffer.toString('base64'); item = { json: { filename: file.filename, type: file.type, subfolder: file.subfolder || '', fileUrl: fileUrl, data: outputBase64, }, binary: { data: { fileName: file.filename, data: outputBase64, fileType: 'image', fileExtension: ext, mimeType: mime, fileSize: Math.round(outputBuffer.length / 1024 * 10) / 10 + " kB", } } }; } return item; } catch (error) { console.error(`[ComfyUI] Failed to download file ${file.filename}:`, error); return { json: { filename: file.filename, type: file.type, subfolder: file.subfolder || '', error: error.message, fileUrl: fileUrl, }, }; } })); console.log('[ComfyUI] All Files downloaded successfully!'); return outputs; } } throw new n8n_workflow_1.NodeApiError(brain.getNode(), { message: `Execution timeout` }); } catch (error) { console.error('[ComfyUI] Execution error:', error); throw new n8n_workflow_1.NodeApiError(brain.getNode(), { message: `ComfyUI API Error: ${error.message}` }); } } //# sourceMappingURL=Comfyui.node.js.map