contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
124 lines (123 loc) • 6.06 kB
JavaScript
import { Command } from 'commander';
import { ToolManager } from '../services/tools/ToolManager.js';
import { BrowserServiceManager } from '../services/tools/BrowserService.js';
export function createTextToImageCommand() {
const command = new Command('text-to-image');
command
.description('Generate images from text using AI-generated HTML/CSS layouts with Tailwind CSS')
.option('-t, --text <text>', 'Text content to render as image (not required in --html-only mode)')
.option('-d, --design <design>', 'Description of the desired visual design and style (not required if --html-file-path is provided)')
.option('--html-file-path <path>', 'Path to existing HTML file to use for rendering (alternative to --design)')
.option('--html-only', 'HTML-only mode: Skip text processing, render HTML file directly for maximum performance')
.option('-o, --output <output>', 'Output file path (default: text_image.png)', 'text_image.png')
.option('-w, --width <width>', 'Image width in pixels', '1200')
.option('-h, --height <height>', 'Image height in pixels', '630')
.action(async (options) => {
try {
// Validate required parameters
if (!options.htmlOnly && !options.text) {
console.error('❌ Error: Text content is required unless using --html-only mode. Use -t or --text option.');
console.log('Example: contaigents text-to-image -t "Hello World!" -d "Modern card design" -o my_image.png');
process.exit(1);
}
if (options.htmlOnly && !options.htmlFilePath) {
console.error('❌ Error: HTML-only mode requires --html-file-path to be specified.');
console.log('💡 HTML-only Example:');
console.log(' • contaigents text-to-image --html-only --html-file-path "template.html" -o output.png');
process.exit(1);
}
if (!options.htmlOnly && !options.design && !options.htmlFilePath) {
console.error('❌ Error: Either design description (-d) or HTML file path (--html-file-path) is required.');
console.log('💡 Design Examples:');
console.log(' • "Modern minimalist card with gradient background"');
console.log(' • "Retro 80s neon style with synthwave colors"');
console.log(' • "Professional business card with clean typography"');
console.log('💡 HTML File Example:');
console.log(' • contaigents text-to-image -t "Hello!" --html-file-path "my_template.html" -o output.png');
console.log('💡 HTML-only Example:');
console.log(' • contaigents text-to-image --html-only --html-file-path "template.html" -o output.png');
process.exit(1);
}
if (options.htmlOnly) {
console.log('⚡ HTML-only mode: Maximum performance rendering');
}
else {
console.log('🎨 Generating AI-designed text-to-image...');
}
if (options.text) {
console.log(`📝 Text: "${options.text}"`);
}
if (options.design) {
console.log(`🎨 Design: "${options.design}"`);
}
if (options.htmlFilePath) {
console.log(`📂 HTML File: "${options.htmlFilePath}"`);
}
if (options.htmlOnly) {
console.log(`⚡ Mode: HTML-only (no text processing)`);
}
console.log(`📁 Output: ${options.output}`);
console.log(`📐 Dimensions: ${options.width}x${options.height}`);
console.log('');
// Initialize tool manager
const toolManager = new ToolManager(process.cwd());
// Prepare tool call parameters
const parameters = {
output_path: options.output,
width: parseInt(options.width),
height: parseInt(options.height)
};
// Add text if provided (not required in HTML-only mode)
if (options.text) {
parameters.text = options.text;
}
// Add design_prompt or html_file_path based on what was provided
if (options.design) {
parameters.design_prompt = options.design;
}
if (options.htmlFilePath) {
parameters.html_file_path = options.htmlFilePath;
}
if (options.htmlOnly) {
parameters.html_only = true;
}
// Execute tool
const result = await toolManager.executeTool({
tool_name: 'text_to_image',
parameters
});
if (result.success) {
console.log('✅ AI-designed image generated successfully!');
console.log(`📊 File: ${result.data.output_file_path}`);
console.log(`📐 Dimensions: ${result.data.dimensions}`);
console.log(`📦 Size: ${formatFileSize(result.data.file_size)}`);
console.log(`🎨 Design: ${result.data.design_prompt}`);
}
else {
console.error('❌ Image generation failed:', result.error);
process.exit(1);
}
}
catch (error) {
console.error(`❌ Error: ${error.message}`);
process.exit(1);
}
finally {
// Cleanup browser service
await BrowserServiceManager.cleanup();
}
});
return command;
}
/**
* Format file size in human readable format
*/
function formatFileSize(bytes) {
if (bytes === 0)
return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}