/** * Analyze code with AI */ import chalk from 'chalk'; import ora from 'ora'; import * as fs from 'fs/promises'; import { OllamaClient, CodeAnalyzer } from '@fsfalmansour/neohub-core'; import { ConfigManager } from '../config'; import { formatMarkdown } from '../utils/markdown'; interface AnalyzeOptions { path: string; type?: 'review' | 'explain' | 'security' | 'performance'; } export async function analyzeCommand(options: AnalyzeOptions): Promise { const config = new ConfigManager(); const ollama = new OllamaClient(config.getOllamaConfig()); try { const spinner = ora('Analyzing code...').start(); // Check if path is file or directory const stats = await fs.stat(options.path); let contexts; if (stats.isDirectory()) { contexts = await CodeAnalyzer.analyzeDirectory(options.path); spinner.text = `Analyzing ${contexts.length} files...`; } else { contexts = [await CodeAnalyzer.readFile(options.path)]; } // Create analysis prompt based on type const analysisType = options.type || 'review'; const prompts: Record = { review: 'Review this code for potential issues, bugs, and improvements:', explain: 'Explain what this code does in detail:', security: 'Analyze this code for security vulnerabilities:', performance: 'Analyze this code for performance issues and optimizations:', }; const contextSummary = CodeAnalyzer.createContextSummary(contexts); const prompt = `${prompts[analysisType]}\n\n${contextSummary}`; // Get AI analysis const response = await ollama.complete(prompt, { temperature: 0.2, num_predict: 2048, }); spinner.succeed(chalk.green('Analysis complete')); // Display results console.log(chalk.bold.cyan(`\nšŸ“Š ${analysisType.toUpperCase()} Analysis\n`)); console.log(chalk.gray('─'.repeat(80))); console.log(formatMarkdown(response)); console.log(chalk.gray('─'.repeat(80))); console.log(''); } catch (error) { console.error(chalk.red('Error: ' + (error as Error).message)); process.exit(1); } }