ai-seo
Version:
AI-native JSON-LD schema utility with automated URL-to-Schema generation, intelligent content analysis, caching, rate limiting, performance monitoring, and AI optimization (ChatGPT, Voice). Complete automation & scale features. Zero runtime dependencies.
1,372 lines (1,121 loc) • 62.3 kB
JavaScript
#!/usr/bin/env node
/**
* AI-SEO CLI - Developer Tools for Schema Management
* V1.7.0 - Developer Experience Revolution
*/
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { AI } from '../index.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// CLI Version and Info
const CLI_VERSION = '1.10.4';
const CLI_NAME = 'ai-seo';
// Color codes for terminal output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m'
};
// Utility functions
function colorize(text, color) {
return `${colors[color]}${text}${colors.reset}`;
}
function log(message, color = 'reset') {
console.log(colorize(message, color));
}
function error(message) {
console.error(colorize(`❌ Error: ${message}`, 'red'));
process.exit(1);
}
function formatErrorMessage(err) {
if (err instanceof Error && err.message) {
return err.message;
}
if (typeof err === 'string') {
return err;
}
try {
return JSON.stringify(err);
} catch (stringifyError) {
return 'Unknown error';
}
}
const performanceNow = (() => {
const perf = globalThis.performance;
return perf && typeof perf.now === 'function'
? perf.now.bind(perf)
: Date.now;
})();
process.on('unhandledRejection', (reason) => {
error(`Unhandled rejection: ${formatErrorMessage(reason)}`);
});
process.on('uncaughtException', (err) => {
error(`Uncaught exception: ${formatErrorMessage(err)}`);
});
function success(message) {
log(`✅ ${message}`, 'green');
}
function warning(message) {
log(`⚠️ ${message}`, 'yellow');
}
function info(message) {
log(`ℹ️ ${message}`, 'cyan');
}
// CLI Commands
export class CLI {
constructor() {
this.commands = new Map();
this.registerCommands();
}
registerCommands() {
this.commands.set('init', this.initCommand);
this.commands.set('analyze', this.analyzeCommand);
this.commands.set('validate', this.validateCommand);
this.commands.set('optimize', this.optimizeCommand);
this.commands.set('generate', this.generateCommand);
this.commands.set('generate-url', this.generateUrlCommand);
this.commands.set('generate-url-batch', this.generateUrlBatchCommand);
this.commands.set('build', this.buildCommand);
this.commands.set('interactive', this.interactiveCommand);
this.commands.set('deploy', this.deployCommand);
this.commands.set('bulk', this.bulkCommand);
this.commands.set('autonomous', this.autonomousCommand);
this.commands.set('context', this.contextCommand);
this.commands.set('ai-search', this.aiSearchCommand);
this.commands.set('help', this.helpCommand);
this.commands.set('version', this.versionCommand);
}
async run(args) {
const [,, command, ...params] = args;
if (!command || command === 'help') {
this.helpCommand();
return;
}
if (this.commands.has(command)) {
try {
await this.commands.get(command).call(this, params);
} catch (err) {
error(`Failed to execute command '${command}': ${err.message}`);
}
} else {
error(`Unknown command: ${command}. Run 'ai-seo help' for available commands.`);
}
}
// Initialize AI-SEO in a project
initCommand(params) {
const [framework = 'vanilla'] = params;
log(colorize('🚀 Initializing AI-SEO in your project...', 'bright'));
const templates = {
vanilla: {
config: {
framework: 'vanilla',
ai: {
optimization: {
target: ['chatgpt', 'bard', 'claude'],
semanticEnhancement: true,
voiceOptimization: false
}
},
validation: {
strict: true,
suggestions: true
},
performance: {
caching: true,
lazyLoading: true
}
},
example: `import { AI, product, initSEO } from 'ai-seo';
// Basic setup
const schema = product()
.name('Your Product')
.description('Amazing product description')
.build();
// AI-optimize for search engines
const optimized = AI.optimizeForLLM(schema, {
target: ['chatgpt', 'bard', 'claude'],
semanticEnhancement: true
});
// Inject into page
initSEO({ schema: optimized });`
},
nextjs: {
config: {
framework: 'nextjs',
ai: {
optimization: {
target: ['chatgpt', 'bard', 'claude'],
semanticEnhancement: true,
voiceOptimization: true
}
},
ssr: true,
validation: {
strict: true,
suggestions: true
}
},
example: `// app/layout.js
import { SSR, organization } from 'ai-seo';
export default function RootLayout({ children }) {
const schema = organization()
.name('Your Company')
.url('https://yoursite.com')
.build();
const { jsonLd } = SSR.frameworks.nextJS.generateHeadContent(schema);
return (
<html>
<head>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: jsonLd }}
/>
</head>
<body>{children}</body>
</html>
);
}`
},
react: {
config: {
framework: 'react',
ai: {
optimization: {
target: ['chatgpt', 'bard', 'claude'],
semanticEnhancement: true
}
},
hooks: true
},
example: `import { Frameworks, product } from 'ai-seo';
function ProductPage({ productData }) {
const { schema, cleanup } = Frameworks.React.useSEO(() =>
product()
.name(productData.name)
.brand(productData.brand)
.build()
);
return <div>Product: {productData.name}</div>;
}`
}
};
const template = templates[framework] || templates.vanilla;
// Create config file
const configPath = 'ai-seo.config.json';
writeFileSync(configPath, JSON.stringify(template.config, null, 2));
success(`Created ${configPath}`);
// Create example file
const examplePath = `ai-seo.example.${framework === 'vanilla' ? 'js' : 'jsx'}`;
writeFileSync(examplePath, template.example);
success(`Created ${examplePath}`);
info('🎉 AI-SEO initialized successfully!');
info('📖 Edit ai-seo.config.json to customize settings');
info(`📝 Check ${examplePath} for usage examples`);
info('🚀 Run `ai-seo analyze` to analyze your content');
}
// Analyze content or URLs
async analyzeCommand(params) {
const [input, ...options] = params;
if (!input) {
error('Please provide content or URL to analyze.');
log('\n💡 Usage: ai-seo analyze <content|url>', 'cyan');
log(' Example: ai-seo analyze "Premium wireless headphones with noise cancellation"\n');
return;
}
log('🔍 Analyzing content with AI...', 'bright');
try {
let content = input;
// If input looks like a URL, fetch content (simplified for demo)
if (input.startsWith('http')) {
info(`Fetching content from: ${input}`);
// In real implementation, would fetch and extract content
content = `Sample content from ${input}`;
warning('URL fetching not implemented in this demo - using sample content');
}
// Analyze with AI
const analysis = AI.analyzeContent(content, {
includeKeywords: true,
includeEntities: true,
includeSentiment: true
});
if (!analysis) {
error('Failed to analyze content');
}
// Display results
log('\n📊 Analysis Results:', 'bright');
log(`🎯 Recommended Schema Type: ${colorize(analysis.recommendedType, 'green')}`);
log(`📈 Confidence Score: ${colorize((analysis.confidence * 100).toFixed(1) + '%', analysis.confidence > 0.7 ? 'green' : 'yellow')}`);
if (analysis.keywords) {
log(`\n🔑 Top Keywords: ${analysis.keywords.slice(0, 5).map(k => colorize(k, 'cyan')).join(', ')}`);
}
if (analysis.entities) {
if (analysis.entities.people.length > 0) {
log(`👥 People: ${analysis.entities.people.slice(0, 3).join(', ')}`);
}
if (analysis.entities.organizations.length > 0) {
log(`🏢 Organizations: ${analysis.entities.organizations.slice(0, 3).join(', ')}`);
}
}
if (analysis.sentiment) {
const sentimentColor = analysis.sentiment.label === 'positive' ? 'green' :
analysis.sentiment.label === 'negative' ? 'red' : 'yellow';
log(`😊 Sentiment: ${colorize(analysis.sentiment.label, sentimentColor)} (${(analysis.sentiment.score * 100).toFixed(1)}%)`);
}
// Type scores
log('\n📋 Schema Type Scores:', 'bright');
Object.entries(analysis.typeScores)
.sort(([,a], [,b]) => b - a)
.slice(0, 5)
.forEach(([type, score]) => {
const percentage = (score * 100).toFixed(1);
const color = score > 0.5 ? 'green' : score > 0.3 ? 'yellow' : 'red';
log(` ${type}: ${colorize(percentage + '%', color)}`);
});
info('\n💡 Run `ai-seo generate` to create schemas from this analysis');
} catch (err) {
error(`Analysis failed: ${err.message}`);
}
}
// Validate existing schema
async validateCommand(params) {
const [schemaPath, ...options] = params;
if (!schemaPath) {
error('Please provide a schema file path.');
log('\n💡 Usage: ai-seo validate <schema-file> [--strict]', 'cyan');
log(' Example: ai-seo validate product-schema.json --strict\n');
return;
}
if (!existsSync(schemaPath)) {
error(`Schema file not found: ${schemaPath}`);
log('\n💡 Check the file path and try again.', 'cyan');
log(` Looking for: ${schemaPath}`);
return;
}
log(`🔍 Validating schema: ${schemaPath}`, 'bright');
try {
const schemaContent = readFileSync(schemaPath, 'utf8');
const schema = JSON.parse(schemaContent);
// Use existing validation from main library
const mainLib = await import('../index.js');
const { validateSchemaEnhanced } = mainLib;
const validation = validateSchemaEnhanced(schema, {
strict: options.includes('--strict'),
suggestions: true
});
// Display results
log('\n📊 Validation Results:', 'bright');
log(`📈 Quality Score: ${colorize(validation.score + '/100', validation.score > 80 ? 'green' : validation.score > 60 ? 'yellow' : 'red')}`);
if (validation.errors.length > 0) {
log('\n❌ Errors:', 'red');
validation.errors.forEach(error => {
log(` • ${error}`, 'red');
});
}
if (validation.warnings.length > 0) {
log('\n⚠️ Warnings:', 'yellow');
validation.warnings.forEach(warning => {
log(` • ${warning}`, 'yellow');
});
}
if (validation.suggestions.length > 0) {
log('\n💡 Suggestions:', 'cyan');
validation.suggestions.forEach(suggestion => {
log(` • ${suggestion}`, 'cyan');
});
}
if (validation.errors.length === 0) {
success('Schema validation passed!');
} else {
warning('Schema has validation issues. See errors above.');
}
} catch (err) {
error(`Validation failed: ${err.message}`);
}
}
// Optimize existing schema with AI
async optimizeCommand(params) {
const [schemaPath, ...options] = params;
if (!schemaPath) {
error('Please provide a schema file path.');
log('\n💡 Usage: ai-seo optimize <schema-file> [--voice]', 'cyan');
log(' Example: ai-seo optimize product-schema.json --voice\n');
return;
}
if (!existsSync(schemaPath)) {
error(`Schema file not found: ${schemaPath}`);
log('\n💡 Check the file path and try again.', 'cyan');
log(` Looking for: ${schemaPath}\n`);
return;
}
log(`🧠 Optimizing schema with AI: ${schemaPath}`, 'bright');
try {
const schemaContent = readFileSync(schemaPath, 'utf8');
const schema = JSON.parse(schemaContent);
// AI optimize
const optimized = AI.optimizeForLLM(schema, {
target: ['chatgpt', 'bard', 'claude'],
semanticEnhancement: true,
voiceOptimization: options.includes('--voice')
});
// Save optimized version
const outputPath = schemaPath.replace('.json', '.optimized.json');
writeFileSync(outputPath, JSON.stringify(optimized, null, 2));
success(`Optimized schema saved to: ${outputPath}`);
// Show improvements
const originalSize = JSON.stringify(schema).length;
const optimizedSize = JSON.stringify(optimized).length;
const sizeDiff = optimizedSize - originalSize;
log('\n📊 Optimization Results:', 'bright');
log(`📦 Size change: ${sizeDiff > 0 ? '+' : ''}${sizeDiff} bytes`);
log(`🧠 AI enhancements: ${colorize('Added', 'green')}`);
log(`🎯 LLM optimization: ${colorize('Applied', 'green')}`);
if (options.includes('--voice')) {
log(`🎙️ Voice search: ${colorize('Optimized', 'green')}`);
}
info('💡 Use the optimized schema for better AI search engine compatibility');
} catch (err) {
error(`Optimization failed: ${err.message}`);
}
}
// Generate schema from URL - v1.12.0
async generateUrlCommand(params) {
const { generateUrlCommand } = await import('./commands/generate-url.js');
return generateUrlCommand(params[0], {
type: params.includes('--type') ? params[params.indexOf('--type') + 1] : undefined,
related: !params.includes('--no-related'),
optimize: params.includes('--optimize') ? params[params.indexOf('--optimize') + 1] : undefined,
validate: params.includes('--validate'),
output: params.includes('--output') ? params[params.indexOf('--output') + 1] : undefined,
debug: params.includes('--debug')
});
}
// Generate schemas from multiple URLs - v1.12.0
async generateUrlBatchCommand(params) {
const { generateUrlBatchCommand } = await import('./commands/generate-url-batch.js');
return generateUrlBatchCommand(params[0], {
concurrency: params.includes('--concurrency') ? parseInt(params[params.indexOf('--concurrency') + 1]) : 3,
output: params.includes('--output') ? params[params.indexOf('--output') + 1] : undefined,
outputDir: params.includes('--output-dir') ? params[params.indexOf('--output-dir') + 1] : undefined,
quiet: params.includes('--quiet'),
debug: params.includes('--debug')
});
}
// Generate schema from content
async generateCommand(params) {
const [input, ...options] = params;
if (!input) {
error('Please provide content to generate schema from.');
log('\n💡 Usage: ai-seo generate <content|file> [--multiple] [--metrics]', 'cyan');
log(' Example: ai-seo generate "5-star restaurant in downtown" --multiple\n');
return;
}
log('🤖 Generating schema with AI...', 'bright');
try {
let content = input;
// If input is a file path, read it
if (existsSync(input)) {
content = readFileSync(input, 'utf8');
info(`Reading content from: ${input}`);
}
// Generate with AI
const results = AI.generateFromContent(content, {
confidence: 0.6,
multipleTypes: options.includes('--multiple'),
includeMetrics: options.includes('--metrics')
});
if (!results || (Array.isArray(results) && results.length === 0)) {
error('Could not generate schema from content. Try providing more descriptive content.');
return;
}
const schemas = Array.isArray(results) ? results : [results];
log('\n🎯 Generated Schemas:', 'bright');
schemas.forEach((result, index) => {
log(`\n📋 Schema ${index + 1}: ${colorize(result.type, 'cyan')}`);
log(`📈 Confidence: ${colorize((result.confidence * 100).toFixed(1) + '%', result.confidence > 0.7 ? 'green' : 'yellow')}`);
// Save schema
const filename = `schema-${result.type.toLowerCase()}-${Date.now()}.json`;
writeFileSync(filename, JSON.stringify(result.schema, null, 2));
success(`Saved to: ${filename}`);
if (result.metrics && options.includes('--metrics')) {
log(`📊 Readability: ${result.metrics.readabilityScore.toFixed(1)}`);
log(`🔑 Keyword Density: ${(result.metrics.keywordDensity * 100).toFixed(1)}%`);
}
});
info('\n💡 Review and customize the generated schemas before using in production');
} catch (err) {
error(`Generation failed: ${err.message}`);
}
}
// Build optimized schemas for production
buildCommand(params) {
log('🏗️ Building optimized schemas for production...', 'bright');
// This would implement a build system for schemas
// For now, show what it would do
info('📦 Collecting schemas...');
info('🧠 Applying AI optimizations...');
info('🗜️ Compressing and minifying...');
info('📊 Generating performance report...');
success('Build completed! (Feature coming in full implementation)');
warning('This is a preview - full build system coming soon');
}
// Interactive mode for guided schema creation - V1.8.0 NEW
async interactiveCommand(params) {
log(colorize('\n🎯 AI-SEO Interactive Mode - Guided Schema Creation\n', 'bright'));
try {
// Import readline for interactive prompts
const readline = await import('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const prompt = (question) => new Promise((resolve) => {
rl.question(colorize(question, 'cyan'), resolve);
});
// Step 1: Schema type selection
log('📋 Step 1: Choose your schema type');
log('1. Product');
log('2. Article/Blog Post');
log('3. Local Business');
log('4. Event');
log('5. Organization');
log('6. Custom/Other');
const typeChoice = await prompt('\nSelect schema type (1-6): ');
const schemaTypes = ['', 'Product', 'Article', 'LocalBusiness', 'Event', 'Organization', 'Thing'];
const selectedType = schemaTypes[parseInt(typeChoice)] || 'Thing';
log(`\n✅ Selected: ${selectedType}`);
// Step 2: Basic information
log(colorize('\n📝 Step 2: Basic Information', 'bright'));
const name = await prompt('Name/Title: ');
const description = await prompt('Description: ');
// Step 3: Platform deployment
log(colorize('\n🌐 Step 3: Platform Deployment', 'bright'));
log('Which platforms do you want to deploy to?');
log('1. WordPress');
log('2. Shopify');
log('3. Webflow');
log('4. Google Tag Manager');
log('5. Custom/Manual');
const platformChoice = await prompt('Select platforms (comma-separated, e.g., 1,2,4): ');
const platformMap = { '1': 'wordpress', '2': 'shopify', '3': 'webflow', '4': 'gtm', '5': 'custom' };
const selectedPlatforms = platformChoice.split(',')
.map(p => platformMap[p.trim()])
.filter(Boolean);
// Step 4: AI optimization
log(colorize('\n🧠 Step 4: AI Optimization', 'bright'));
const aiOptimization = await prompt('Enable AI optimization for LLMs? (y/n): ');
const voiceOptimization = await prompt('Enable voice search optimization? (y/n): ');
rl.close();
// Create schema
log(colorize('\n🔧 Creating your schema...', 'bright'));
const baseSchema = {
'@context': 'https://schema.org',
'@type': selectedType,
name: name,
description: description
};
// Apply AI optimization if requested
let finalSchema = baseSchema;
if (aiOptimization.toLowerCase() === 'y') {
const { AI } = await import('../index.js');
finalSchema = AI.optimizeForLLM(baseSchema, {
target: ['chatgpt', 'bard', 'claude'],
semanticEnhancement: true,
voiceOptimization: voiceOptimization.toLowerCase() === 'y'
});
success('AI optimization applied');
}
// Generate platform deployments
if (selectedPlatforms.length > 0) {
log(colorize('\n🚀 Generating platform deployments...', 'bright'));
const { MultiPlatform } = await import('../index.js');
const deployments = MultiPlatform.deploy(finalSchema, selectedPlatforms);
selectedPlatforms.forEach(platform => {
if (deployments.deployments[platform]) {
log(colorize(`\n📦 ${platform.toUpperCase()} Integration:`, 'green'));
log('Code generated successfully!');
log('Instructions:');
deployments.deployments[platform].instructions.forEach(instruction => {
log(` • ${instruction}`);
});
}
});
}
// Display final schema
log(colorize('\n📋 Final Schema:', 'bright'));
log(JSON.stringify(finalSchema, null, 2));
success('\n🎉 Interactive schema creation complete!');
log('Your schema is ready to deploy to your selected platforms.');
} catch (err) {
error(`Interactive mode failed: ${err.message}`);
}
}
// Deploy schema to multiple platforms - V1.8.0 NEW
async deployCommand(params) {
const [schemaPath, platformsStr, ...options] = params;
if (!schemaPath || !platformsStr) {
error('Missing required parameters.');
log('\n💡 Usage: ai-seo deploy <schema-file> <platforms> [options]', 'cyan');
log(' Example: ai-seo deploy product.json wordpress,shopify,webflow');
log(' Available platforms: wordpress, shopify, webflow, gtm\n');
return;
}
log(colorize('🚀 Multi-Platform Schema Deployment', 'bright'));
try {
// Read schema file
if (!existsSync(schemaPath)) {
error(`Schema file not found: ${schemaPath}`);
return;
}
const schemaContent = readFileSync(schemaPath, 'utf8');
const schema = JSON.parse(schemaContent);
// Parse platforms
const platforms = platformsStr.split(',').map(p => p.trim().toLowerCase());
const validPlatforms = ['wordpress', 'shopify', 'webflow', 'gtm'];
const invalidPlatforms = platforms.filter(p => !validPlatforms.includes(p));
if (invalidPlatforms.length > 0) {
error(`Invalid platforms: ${invalidPlatforms.join(', ')}`);
log(`Valid platforms: ${validPlatforms.join(', ')}`);
return;
}
log(`📋 Schema: ${schema['@type']} - "${schema.name || 'Untitled'}"`);
log(`🌐 Platforms: ${platforms.join(', ')}`);
// Import MultiPlatform module
const { MultiPlatform } = await import('../index.js');
// Generate deployments
const deployments = MultiPlatform.deploy(schema, platforms, {
wordpress: { pluginName: `AI SEO ${schema['@type']}` },
shopify: { templateType: 'product' },
webflow: { placement: 'head' },
gtm: { eventName: 'ai_seo_schema_ready' }
});
// Output results
log(colorize('\n📦 Generated Deployments:', 'bright'));
Object.entries(deployments.deployments).forEach(([platform, deployment]) => {
log(colorize(`\n🔧 ${platform.toUpperCase()}:`, 'green'));
// Write deployment file
const filename = deployment.filename || `${platform}-deployment.${platform === 'wordpress' ? 'php' : 'txt'}`;
writeFileSync(filename, deployment.code);
success(`File saved: ${filename}`);
// Show instructions
log('Instructions:');
deployment.instructions.forEach(instruction => {
log(` • ${instruction}`);
});
});
log(colorize(`\n📊 Summary:`, 'bright'));
log(`• Platforms: ${deployments.summary.platforms}`);
log(`• Generated: ${deployments.summary.generated}`);
log(`• Timestamp: ${deployments.summary.timestamp}`);
success('\n🎉 Multi-platform deployment complete!');
} catch (err) {
error(`Deployment failed: ${err.message}`);
}
}
// Bulk schema management - V1.8.0 NEW
async bulkCommand(params) {
const [action, ...options] = params;
if (!action) {
error('Missing action parameter.');
log('\n💡 Usage: ai-seo bulk <action> [options]', 'cyan');
log(' Actions:');
log(' validate <directory> - Validate all schemas in directory');
log(' optimize <directory> - Optimize all schemas with AI');
log(' deploy <directory> <platforms> - Deploy all schemas');
log(' analyze <directory> - Analyze schema quality\n');
return;
}
log(colorize('📦 Bulk Schema Management', 'bright'));
try {
const { readdirSync, statSync } = await import('fs');
const { join } = await import('path');
switch (action) {
case 'validate': {
const [directory = '.'] = options;
log(`🔍 Validating schemas in: ${directory}`);
const files = readdirSync(directory)
.filter(file => file.endsWith('.json'))
.map(file => join(directory, file));
let validCount = 0;
let errorCount = 0;
for (const file of files) {
try {
const content = readFileSync(file, 'utf8');
const schema = JSON.parse(content);
if (schema['@context'] && schema['@type']) {
log(`✅ ${file}: Valid`);
validCount++;
} else {
log(`❌ ${file}: Missing @context or @type`);
errorCount++;
}
} catch (err) {
log(`❌ ${file}: ${err.message}`);
errorCount++;
}
}
log(colorize(`\n📊 Results: ${validCount} valid, ${errorCount} errors`, 'bright'));
break;
}
case 'optimize': {
const [directory = '.'] = options;
log(`🧠 Optimizing schemas in: ${directory}`);
const { AI } = await import('../index.js');
const files = readdirSync(directory)
.filter(file => file.endsWith('.json'))
.map(file => join(directory, file));
let optimizedCount = 0;
for (const file of files) {
try {
const content = readFileSync(file, 'utf8');
const schema = JSON.parse(content);
const optimized = AI.optimizeForLLM(schema, {
target: ['chatgpt', 'bard', 'claude'],
semanticEnhancement: true
});
const outputFile = file.replace('.json', '-optimized.json');
writeFileSync(outputFile, JSON.stringify(optimized, null, 2));
log(`✅ ${file} → ${outputFile}`);
optimizedCount++;
} catch (err) {
log(`❌ ${file}: ${err.message}`);
}
}
success(`🎉 Optimized ${optimizedCount} schemas`);
break;
}
case 'analyze': {
const [directory = '.'] = options;
log(`📊 Analyzing schemas in: ${directory}`);
const files = readdirSync(directory)
.filter(file => file.endsWith('.json'));
const stats = {
total: files.length,
types: {},
avgSize: 0,
totalSize: 0
};
files.forEach(file => {
try {
const filePath = join(directory, file);
const content = readFileSync(filePath, 'utf8');
const schema = JSON.parse(content);
const size = statSync(filePath).size;
stats.types[schema['@type']] = (stats.types[schema['@type']] || 0) + 1;
stats.totalSize += size;
} catch (err) {
// Skip invalid files
}
});
stats.avgSize = Math.round(stats.totalSize / stats.total);
log(colorize('\n📈 Analysis Results:', 'bright'));
log(`• Total schemas: ${stats.total}`);
log(`• Average size: ${stats.avgSize} bytes`);
log(`• Total size: ${stats.totalSize} bytes`);
log('\nSchema types:');
Object.entries(stats.types).forEach(([type, count]) => {
log(` • ${type}: ${count}`);
});
break;
}
default:
error(`Unknown bulk action: ${action}`);
}
} catch (err) {
error(`Bulk operation failed: ${err.message}`);
}
}
// Autonomous schema management - V1.9.0 NEW
async autonomousCommand(params) {
const [action, ...options] = params;
if (!action) {
log(colorize('🤖 Autonomous Schema Management', 'bright'));
log('Actions:');
log(' start - Start autonomous schema discovery and management');
log(' stop - Stop autonomous management');
log(' status - Show current status and statistics');
log(' report - Generate detailed management report');
log(' config <options> - Configure autonomous settings');
return;
}
try {
const { AutonomousManager } = await import('../index.js');
switch (action) {
case 'start': {
log('🚀 Starting autonomous schema management...', 'bright');
AutonomousManager.start();
success('Autonomous schema management started!');
info('The system will now automatically discover and manage schemas.');
info('Use `ai-seo autonomous status` to monitor progress.');
break;
}
case 'stop': {
log('🛑 Stopping autonomous schema management...', 'bright');
AutonomousManager.stop();
success('Autonomous schema management stopped.');
break;
}
case 'status': {
log(colorize('📊 Autonomous Management Status', 'bright'));
const stats = AutonomousManager.getStats();
log(`🔄 Status: ${colorize(stats.isRunning ? 'Running' : 'Stopped', stats.isRunning ? 'green' : 'red')}`);
log(`🔍 Discovered Schemas: ${colorize(stats.discovered, 'cyan')}`);
log(`🤖 Managed Schemas: ${colorize(stats.managed, 'cyan')}`);
log(`📚 Learning Data Points: ${colorize(stats.learningDataPoints, 'cyan')}`);
if (stats.managed > 0) {
log('\n🏥 Schema Health:');
log(` ✅ Healthy: ${colorize(stats.healthySchemas, 'green')}`);
log(` ⚠️ Warning: ${colorize(stats.warningSchemas, 'yellow')}`);
log(` ❌ Critical: ${colorize(stats.criticalSchemas, 'red')}`);
}
if (!stats.isRunning) {
info('Use `ai-seo autonomous start` to begin autonomous management.');
}
break;
}
case 'report': {
log(colorize('📋 Generating Autonomous Management Report...', 'bright'));
const report = AutonomousManager.getReport();
// Save report to file
const reportContent = JSON.stringify(report, null, 2);
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `autonomous-report-${timestamp}.json`;
const { writeFileSync } = await import('fs');
writeFileSync(filename, reportContent);
success(`Report saved to: ${filename}`);
// Show summary
log(colorize('\n📊 Report Summary:', 'bright'));
log(`• Discovered: ${report.stats.discovered} schemas`);
log(`• Managed: ${report.stats.managed} schemas`);
log(`• Health Checks: ${report.healthChecks.length} performed`);
log(`• Learning Points: ${report.recentLearning.length} recent entries`);
break;
}
case 'config': {
log(colorize('⚙️ Autonomous Configuration', 'bright'));
const configOptions = options.join(' ').split(' ').reduce((acc, option) => {
const [key, value] = option.split('=');
if (key && value) {
acc[key.replace('--', '')] = value === 'true' ? true : value === 'false' ? false : value;
}
return acc;
}, {});
if (Object.keys(configOptions).length === 0) {
log('Current configuration:');
log(' autoDiscovery: true');
log(' autoUpdates: true');
log(' healthMonitoring: true');
log(' learningMode: true');
log(' updateInterval: 30000ms');
log('\nTo change settings:');
log(' ai-seo autonomous config --autoDiscovery=false --updateInterval=60000');
} else {
// Apply configuration
AutonomousManager.options = { ...AutonomousManager.options, ...configOptions };
success('Configuration updated!');
Object.entries(configOptions).forEach(([key, value]) => {
log(` ${key}: ${colorize(value, 'cyan')}`);
});
}
break;
}
default:
error(`Unknown autonomous action: ${action}`);
}
} catch (err) {
error(`Autonomous command failed: ${err.message}`);
}
}
// Context-aware AI suggestions - V1.9.0 NEW
async contextCommand(params) {
const [action, ...options] = params;
if (!action) {
log(colorize('🧠 AI Context Engine', 'bright'));
log('Actions:');
log(' analyze <input> - Analyze input and get context-aware suggestions');
log(' feedback <id> <action> - Provide feedback on suggestions (accepted/rejected)');
log(' metrics - Show context engine performance metrics');
log(' preferences - Show and manage user preferences');
log(' history - Show recent context analysis history');
return;
}
try {
const { ContextEngine } = await import('../index.js');
switch (action) {
case 'analyze': {
const [input, ...analysisOptions] = options;
if (!input) {
error('Please provide input to analyze.');
log('\n💡 Usage: ai-seo context analyze <input>', 'cyan');
log(' Example: ai-seo context analyze "product schema content"\n');
return;
}
log('🧠 Analyzing context with AI...', 'bright');
// Read from file if input is a file path
let content = input;
const { existsSync, readFileSync } = await import('fs');
if (existsSync(input)) {
content = readFileSync(input, 'utf8');
info(`Reading content from: ${input}`);
}
const result = await ContextEngine.analyzeContext(content, {
includeHistory: analysisOptions.includes('--history'),
deepAnalysis: analysisOptions.includes('--deep')
});
// Display context analysis
log(colorize('\n📊 Context Analysis:', 'bright'));
log(`🎯 Input Type: ${colorize(result.context.type, 'cyan')}`);
log(`📈 Confidence Score: ${colorize((result.metadata.confidenceScore * 100).toFixed(1) + '%', 'green')}`);
log(`🔍 Context Depth: ${colorize(result.metadata.contextDepth, 'cyan')}`);
if (result.context.pageContext && result.context.pageContext.contentType !== 'unknown') {
log(`📄 Page Content Type: ${colorize(result.context.pageContext.contentType, 'cyan')}`);
}
if (result.context.semanticContext && result.context.semanticContext.keywords.length > 0) {
log(`🔑 Keywords: ${result.context.semanticContext.keywords.slice(0, 5).map(k => colorize(k, 'yellow')).join(', ')}`);
}
// Display suggestions
if (result.suggestions.length > 0) {
log(colorize('\n💡 AI Suggestions:', 'bright'));
result.suggestions.forEach((suggestion, index) => {
log(`\n${index + 1}. ${colorize(suggestion.title, 'green')}`);
log(` ${suggestion.description}`);
log(` Confidence: ${colorize((suggestion.confidence * 100).toFixed(1) + '%', suggestion.confidence > 0.8 ? 'green' : 'yellow')}`);
log(` Action: ${colorize(suggestion.action, 'cyan')}`);
});
info('\nUse `ai-seo context feedback <suggestion-id> accepted/rejected` to improve future suggestions.');
} else {
warning('No suggestions generated. Try providing more specific content or lowering the suggestion threshold.');
}
break;
}
case 'feedback': {
const [suggestionId, feedbackAction, ...metadata] = options;
if (!suggestionId || !feedbackAction) {
error('Missing required parameters.');
log('\n💡 Usage: ai-seo context feedback <suggestion-id> <accepted|rejected> [metadata]', 'cyan');
log(' Example: ai-seo context feedback abc123 accepted\n');
return;
}
if (!['accepted', 'rejected'].includes(feedbackAction)) {
error('Feedback action must be "accepted" or "rejected".');
log('\n💡 Valid actions: accepted, rejected', 'cyan');
return;
}
const metadataObj = metadata.reduce((acc, item) => {
const [key, value] = item.split('=');
if (key && value) acc[key] = value;
return acc;
}, {});
ContextEngine.recordFeedback(suggestionId, feedbackAction, metadataObj);
success(`Feedback recorded: ${suggestionId} - ${feedbackAction}`);
info('This feedback will help improve future AI suggestions.');
break;
}
case 'metrics': {
log(colorize('📈 Context Engine Metrics', 'bright'));
const metrics = ContextEngine.getMetrics();
log(`📊 Total Suggestions: ${colorize(metrics.totalSuggestions, 'cyan')}`);
log(`✅ Accepted: ${colorize(metrics.acceptedSuggestions, 'green')}`);
log(`❌ Rejected: ${colorize(metrics.rejectedSuggestions, 'red')}`);
log(`📈 Acceptance Rate: ${colorize(metrics.acceptanceRate + '%', metrics.acceptanceRate > 70 ? 'green' : 'yellow')}`);
log(`🎯 Average Confidence: ${colorize((metrics.averageConfidence * 100).toFixed(1) + '%', 'cyan')}`);
log(`💾 Cache Size: ${colorize(metrics.cacheSize, 'cyan')}`);
log(`👤 User Preferences: ${colorize(metrics.userPreferences, 'cyan')}`);
log(`📚 Context History: ${colorize(metrics.contextHistory, 'cyan')}`);
if (metrics.acceptanceRate < 50) {
warning('Low acceptance rate detected. Consider adjusting suggestion threshold or providing more specific feedback.');
}
break;
}
case 'preferences': {
log(colorize('👤 User Preferences', 'bright'));
const metrics = ContextEngine.getMetrics();
if (metrics.userPreferences === 0) {
info('No user preferences recorded yet.');
info('Use the context analysis and feedback features to build your preference profile.');
} else {
log(`📊 Total Preferences: ${colorize(metrics.userPreferences, 'cyan')}`);
info('Preferences are automatically learned from your feedback on suggestions.');
info('Use `ai-seo context feedback` to continue improving your experience.');
}
break;
}
case 'history': {
log(colorize('📚 Context Analysis History', 'bright'));
const metrics = ContextEngine.getMetrics();
if (metrics.contextHistory === 0) {
info('No context history available yet.');
info('Use `ai-seo context analyze` to start building your context history.');
} else {
log(`📊 Total History Entries: ${colorize(metrics.contextHistory, 'cyan')}`);
info('Context history helps improve future suggestions by learning from past analyses.');
}
break;
}
default:
error(`Unknown context action: ${action}`);
}
} catch (err) {
error(`Context command failed: ${err.message}`);
}
}
// AI Search Engine optimization - V1.10.0 NEW
async aiSearchCommand(params) {
const [action, ...options] = params;
if (!action) {
log(colorize('🔍 AI Search Engine Optimization', 'bright'));
log('Actions:');
log(' optimize <schema.json> [targets] - Optimize schema for AI search engines');
log(' test <schema.json> - Test schema optimization effectiveness');
log(' deploy <schema.json> [platforms] - Deploy optimized schemas to platforms');
log(' analytics - Show AI search optimization analytics');
log(' targets - List available AI search targets');
log(' benchmark <schema.json> - Benchmark optimization performance');
return;
}
try {
const { AISearchOptimizer } = await import('../index.js');
switch (action) {
case 'optimize': {
const [schemaPath, targetsStr, ...optimizeOptions] = options;
if (!schemaPath) {
error('Please provide a schema file path.');
log('\n💡 Usage: ai-seo ai-search optimize <schema-file> [targets]', 'cyan');
log(' Example: ai-seo ai-search optimize schema.json chatgpt\n');
log('📖 Run "ai-seo ai-search targets" to see available AI search engines');
return;
}
const { existsSync, readFileSync, writeFileSync } = await import('fs');
if (!existsSync(schemaPath)) {
error(`Schema file not found: ${schemaPath}`);
log('\n💡 Make sure the file path is correct and the file exists.', 'cyan');
log(' Try: ls ' + schemaPath.split('/').slice(0, -1).join('/') || '.');
return;
}
log('🔍 Optimizing schema for AI search engines...', 'bright');
const schemaContent = readFileSync(schemaPath, 'utf8');
const schema = JSON.parse(schemaContent);
// Parse targets
const targets = targetsStr ? targetsStr.split(',').map(t => t.trim()) : ['chatgpt', 'bard', 'perplexity'];
const result = await AISearchOptimizer.optimizeForAll(schema, {
targets,
adaptiveOptimization: optimizeOptions.includes('--adaptive'),
realTimeUpdates: optimizeOptions.includes('--realtime')
});
// Display results
log(colorize('\n🎯 AI Search Optimization Results:', 'bright'));
log(`📊 Processing Time: ${colorize(result.metadata.processingTime.toFixed(2) + 'ms', 'cyan')}`);
log(`🎯 Targets Processed: ${colorize(result.metadata.targets.join(', '), 'cyan')}`);
log(`✅ Success: ${colorize(result.metadata.success ? 'Yes' : 'No', result.metadata.success ? 'green' : 'red')}`);
// Show optimized schemas
Object.entries(result.optimized).forEach(([target, optimizedSchema]) => {
if (optimizedSchema && !optimizedSchema.error) {
log(`\n🤖 ${target.toUpperCase()} Optimization:`);
if (optimizedSchema._aiOptimization) {
log(` ✨ Optimizations: ${colorize(optimizedSchema._aiOptimization.optimizations.length, 'green')}`);
log(` 🔧 Applied: ${optimizedSchema._aiOptimization.optimizations.join(', ')}`);
}
// Save optimized schema
const outputPath = schemaPath.replace('.json', `-${target}-optimized.json`);
writeFileSync(outputPath, JSON.stringify(optimizedSchema, null, 2));
success(`Saved ${target} optimized schema: ${outputPath}`);
} else {
warning(`${target} optimization failed: ${optimizedSchema.error || 'Unknown error'}`);
}
});
info('\n💡 Use `ai-seo ai-search deploy` to deploy optimized schemas to platforms');
break;
}
case 'test': {
const [schemaPath, ...testOptions] = options;
if (!schemaPath) {
error('Please provide a schema file path. Example: ai-seo ai-search test schema.json');
return;
}
const { existsSync, readFileSync } = await import('fs');
if (!existsSync(schemaPath)) {
error(`Schema file not found: ${schemaPath}`);
return;
}
log('🧪 Testing AI search optimization effectiveness...', 'bright');
const schemaContent = readFileSync(schemaPath, 'utf8');
const schema = JSON.parse(schemaContent);
// Test with ChatGPT optimizer (most complete implementation)
const result = await AISearchOptimizer.optimizeFor('chatgpt', schema);
// Analyze optimization effectiveness
const effectiveness = this.analyzeOptimizationEffectiveness(schema, result.optimized);
log(colorize('\n📊 Optimization Test Results:', 'bright'));
log(`🎯 Overall Score: ${colorize(effectiveness.overallScore + '/100', effectiveness.overallScore > 80 ? 'green' : effectiveness.overallScore > 60 ? 'yellow' : 'red')}`);
log(`📈 Conversational Readiness: ${colorize(effectiveness.conversationalScore + '%', 'cyan')}`);
log(`🔍 AI Discoverability: ${colorize(effectiveness.discoverabilityScore + '%', 'cyan')}`);
log(`📚 Context Richness: ${colorize(effectiveness.contextScore + '%', 'cyan')}`);
if (effectiveness.improvements.length > 0) {
log(colorize('\n💡 Suggested Improvements:', 'bright'));
effectiveness.improvements.forEach(improvement => {
log(` • ${improvement}`);
});
}
if (effectiveness.overallScore > 80) {
success('Schema is well-optimized for AI search engines!');
} else {
warning('Schema could benefit from additional AI optimization.');
}
break;
}
case 'deploy': {
const [schemaPath, platformsStr, ...deployOptions] = options;
if (!schemaPath) {
error('Please provide a schema file path. Example: ai-seo ai-search deploy schema.json web,chatgpt-plugin');
return;
}
const { existsSync, readFileSync } = await import('fs');
if (!existsSync(schemaPath)) {
error(`Schema file not found: ${schemaPath}`);
return;
}
log('🚀 Deploying AI-optimized schemas...', 'bright');
const schemaContent = readFileSync(schemaPath, 'utf8');
const schema = JSON.parse(schemaContent);
// First optimize for all AI search engines
const optimized = await AISearchOptimizer.optimizeForAll(schema);
// Parse platforms
const platforms = platformsStr ? platformsStr.split(',').map(p => p.trim()) : ['web'];
const deployResult = await AISearchOptimizer.deploy(optimized, {
platforms,
monitoring: deployOptions.includes('--monitor'),
analytics: deployOptions.includes('--analytics')
});
log(colorize('\n📦 Deployment Results:', 'bright'));
Object.entries(deployResult.deployments).forEach(([platform, result]) => {
if (result.error) {
warning(`${platform}: ${result.error}`);
} else if (result.status === 'not_implemented') {
info(`${platform}: ${result.message}`);
} else {
success(`${platform}: Deployed successfully`);
if (result.deployed) {
log(` 📊 Schemas deployed: ${result.success}/${result.total}`);
}
}
});
success('\n🎉 AI search optimization deployment complete!');
break;
}
case 'analytics': {
log(colorize('📊 AI Search Optimization Analytics', 'bright'));
const analytics = AISearchOptimizer.getAnalytics();
if (analytics.totalOptimizations === 0) {
info('No optimization analytics available yet.');
info('Use `ai-seo ai-search optimize` to start generating analytics data.');
return;
}
log(`📈 Total Optimizations: ${colorize(analytics.totalOptimizations, 'cyan')}`);
log(`⚡ Average Processing Time: ${colorize(analytics.averageProcessingTime.toFixed(2) + 'ms', 'cyan')}`);
log(`✅ Success Rate: ${colorize(analytics.successRate.toFixed(1) + '%', analytics.successRate > 90 ? 'green' : 'yellow')}`);
if (Object.keys(analytics.popularTarge