UNPKG

n8n-nodes-hjhcomfyui

Version:

n8n node to integrate with ComfyUI stable diffusion workflows

194 lines 8.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Hjhcomfyui = void 0; const n8n_workflow_1 = require("n8n-workflow"); class Hjhcomfyui { constructor() { this.description = { displayName: 'HjhComfyUI', name: 'hjhcomfyui', icon: 'file:hjhcomfyui.svg', group: ['transform'], version: 1, description: 'Execute HjhComfyUI workflows', defaults: { name: 'HjhComfyUI', }, credentials: [ { name: 'hjhComfyUIApi', required: true, }, ], inputs: ['main'], outputs: ['main'], properties: [ { displayName: 'Use Hjh Services', name: 'useHjhServices', type: 'boolean', default: true, description: 'Whether to use Hjh services in the workflow', }, { displayName: 'Hjh Services', name: 'services', type: 'string', typeOptions: { rows: 10, }, default: '', description: 'The Hjh services in JSON format', }, { displayName: 'Hjh Workflow JSON', name: 'workflow', type: 'string', typeOptions: { rows: 10, }, default: '', required: true, description: 'The HjhComfyUI workflow in JSON format', }, { displayName: 'Timeout', name: 'timeout', type: 'number', default: 30, description: 'Maximum time in minutes to wait for workflow completion', }, ], }; } async execute() { var _a, _b; const credentials = await this.getCredentials('hjhComfyUIApi'); const useHjhServices = this.getNodeParameter('useHjhServices', 0); const services = this.getNodeParameter('services', 0); const workflow = this.getNodeParameter('workflow', 0); const timeout = this.getNodeParameter('timeout', 0); let apiUrl = ""; let apiKey = ""; if (useHjhServices) { if (services) { let services1 = JSON.parse(services); apiUrl = services1.comfyui; } else { apiUrl = credentials.apiUrl; apiKey = credentials.apiKey; } } else { apiUrl = credentials.apiUrl; apiKey = credentials.apiKey; } console.log('[HjhComfyUI] Executing with API URL:', apiUrl); const headers = { 'Content-Type': 'application/json', }; if (apiKey) { console.log('[HjhComfyUI] Using API key authentication'); headers['Authorization'] = `Bearer ${apiKey}`; } try { console.log('[HjhComfyUI] Checking API connection...'); await this.helpers.request({ method: 'GET', url: `${apiUrl}/system_stats`, headers, json: true, }); console.log('[HjhComfyUI] Queueing prompt...'); const response = await this.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(this.getNode(), { message: 'Failed to get prompt ID from HjhComfyUI' }); } const promptId = response.prompt_id; console.log('[HjhComfyUI] Prompt queued with ID:', promptId); let attempts = 0; const maxAttempts = 60 * timeout; await new Promise(resolve => setTimeout(resolve, 5000)); while (attempts < maxAttempts) { console.log(`[HjhComfyUI] Checking execution status (attempt ${attempts + 1}/${maxAttempts})...`); await new Promise(resolve => setTimeout(resolve, 1000)); attempts++; const history = await this.helpers.request({ method: 'GET', url: `${apiUrl}/history/${promptId}`, headers, json: true, }); const promptResult = history[promptId]; if (!promptResult) { console.log('[HjhComfyUI] Prompt not found in history'); continue; } if (promptResult.status === undefined) { console.log('[HjhComfyUI] Execution status not found'); continue; } if ((_a = promptResult.status) === null || _a === void 0 ? void 0 : _a.completed) { console.log('[HjhComfyUI] Execution completed'); if (((_b = promptResult.status) === null || _b === void 0 ? void 0 : _b.status_str) === 'error') { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: '[HjhComfyUI] Workflow execution failed' }); } if (!promptResult.outputs) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: '[HjhComfyUI] No outputs found in workflow result' }); } let files; files = Object.values(promptResult.outputs) .flatMap((nodeOutput) => nodeOutput.images || []) .filter((image) => image.type === 'output' || image.type === 'temp') .map((file) => { console.log(`[HjhComfyUIJson] Downloading ${file.type} image:`, file.filename); console.log(file); return file.subfolder + "\\" + file.filename; }); let responseData = { code: 200, message: 'ok', description: 'ok', files: files, promptResult: promptResult, promptId: promptId, }; console.log('[HjhComfyUI] All images downloaded successfully'); return [this.helpers.returnJsonArray(responseData)]; } } throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `Execution timeout after ${timeout} minutes` }); } catch (error) { console.error('[HjhComfyUIJson] Execution error:', error); this.logger.error('[HjhComfyUIJson] Execution error:', error); if (!this.continueOnFail()) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `HjhComfyUIJson API Error: ${error.message} ${apiUrl} ${apiKey} ` + error.toString(), description: `HjhComfyUIJson API Error: ${error.message} ${apiUrl} ${apiKey} ` + error.toString() }, { message: `HjhComfyUIJson API Error: ${error.message} ${apiUrl} ${apiKey} ` + error.toString(), description: `HjhComfyUIJson API Error: ${error.message} ${apiUrl} ${apiKey} ` + error.toString() }); } let errorItems = { code: 200, files: [], message: error.message, description: `HjhComfyUIJson API Error: ${error.message} ${apiUrl} ${apiKey} ${error.toString()} ` + error.stack, }; return [this.helpers.returnJsonArray(errorItems)]; } } } exports.Hjhcomfyui = Hjhcomfyui; //# sourceMappingURL=Hjhcomfyui.node.js.map