import { ComfyUIClient } from '../src/comfyui-client.js';
import { getFluxWorkflow, getFluxDiffusersWorkflow, getFluxGuidanceWorkflow } from '../src/flux-workflow.js';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function main() {
const host = process.env.COMFYUI_HOST || '127.0.0.1';
const port = process.env.COMFYUI_PORT || '8188';
const client = new ComfyUIClient(`${host}:${port}`);
try {
// Connect to ComfyUI
console.log('Connecting to ComfyUI...');
await client.connect();
console.log('Connected successfully!');
// Example 1: Basic generation using FP8 models
console.log('\n=== Example 1: Basic Image Generation (FP8) ===');
const basicWorkflow = getFluxDiffusersWorkflow({
prompt: 'A majestic mountain landscape at sunset, highly detailed, professional photography',
negative_prompt: 'blurry, low quality',
width: 1024,
height: 1024,
steps: 4,
cfg_scale: 1.0,
seed: 42,
sampler_name: 'euler',
scheduler: 'simple',
});
console.log('Generating basic image...');
const basicImages = await client.generateImage(basicWorkflow, path.join(__dirname, 'output'));
console.log('Basic image generated:', basicImages[0]?.path);
// Example 2: Using guidance workflow (if supported)
console.log('\n=== Example 2: Flux with Guidance ===');
const guidanceWorkflow = getFluxGuidanceWorkflow({
prompt: 'A futuristic city with flying cars, cyberpunk style, neon lights, rain',
width: 1024,
height: 1024,
steps: 4,
guidance: 3.5,
seed: 123,
sampler_name: 'euler',
scheduler: 'simple',
});
console.log('Generating image with guidance...');
try {
const guidanceImages = await client.generateImage(guidanceWorkflow, path.join(__dirname, 'output'));
console.log('Guidance image generated:', guidanceImages[0]?.path);
} catch (error) {
console.log('Guidance workflow failed (this might require specific ComfyUI nodes):', error.message);
}
// Example 3: Batch generation with different prompts
console.log('\n=== Example 3: Batch Generation ===');
const prompts = [
'A serene Japanese garden with cherry blossoms',
'A steampunk airship floating above clouds',
'An underwater scene with bioluminescent creatures',
];
for (let i = 0; i < prompts.length; i++) {
console.log(`Generating image ${i + 1}/${prompts.length}: "${prompts[i].substring(0, 50)}..."`);
const workflow = getFluxWorkflow({
prompt: prompts[i],
width: 1024,
height: 1024,
steps: 4,
cfg_scale: 7.5,
seed: -1, // Random seed for variety
});
const images = await client.generateImage(workflow, path.join(__dirname, 'output'));
console.log(`Image ${i + 1} saved:`, images[0]?.path);
}
} catch (error) {
console.error('Error:', error);
} finally {
// Disconnect from ComfyUI
client.disconnect();
console.log('\nDisconnected from ComfyUI');
}
}
// Run the example
main().catch(console.error);