UNPKG

woaru

Version:

Universal Project Setup Autopilot - Analyze and automatically configure development tools for ANY programming language

282 lines 10.1 kB
import { AIReviewConfig, CodeContext, MultiLLMReviewResult, PromptTemplate } from '../types/ai-review'; /** * AIReviewAgent - Multi-LLM powered code review and analysis system * * The AIReviewAgent provides sophisticated AI-powered code review capabilities by leveraging * multiple Large Language Model (LLM) providers simultaneously. It performs comprehensive * code analysis, identifies potential issues, suggests improvements, and provides detailed * findings with actionable recommendations. The agent supports multiple AI providers * including OpenAI, Anthropic, and others, enabling robust and diverse code analysis. * * Key features: * - Multi-LLM analysis for comprehensive code reviews * - Hook system integration for extensible workflows * - Usage tracking and performance monitoring * - Provider-specific configuration and failover * - Internationalization support for multilingual analysis * - Secure API communication with error handling * * Supported analysis types: * - Code quality assessment and best practices * - Bug detection and potential security issues * - Performance optimization recommendations * - Architecture and design pattern suggestions * - Maintainability and readability improvements * - Language-specific idioms and conventions * * @example * ```typescript * // Configure AI review with multiple providers * const config: AIReviewConfig = { * providers: [ * { name: 'openai', enabled: true, apiKey: 'your-openai-key' }, * { name: 'anthropic', enabled: true, apiKey: 'your-anthropic-key' } * ], * maxTokens: 4000, * temperature: 0.1 * }; * * const reviewAgent = new AIReviewAgent(config); * * // Perform code review * const codeContext: CodeContext = { * fileName: 'UserService.ts', * language: 'typescript', * framework: 'express', * projectType: 'web-api' * }; * * const result = await reviewAgent.performMultiLLMReview(sourceCode, codeContext); * * // Process results from multiple providers * result.results.forEach(providerResult => { * console.log(`${providerResult.provider} findings:`); * providerResult.findings.forEach(finding => { * console.log(`- ${finding.type}: ${finding.message}`); * console.log(` Severity: ${finding.severity}`); * if (finding.suggestion) { * console.log(` Suggestion: ${finding.suggestion}`); * } * }); * }); * ``` * * @example * ```typescript * // Advanced usage with custom prompt templates * const customPrompts = { * security: { * name: 'Security Review', * description: 'Focus on security vulnerabilities', * system_prompt: 'You are a security expert reviewing code for vulnerabilities...', * user_prompt: 'Analyze this code for security issues: {{code}}' * } * }; * * const reviewAgent = new AIReviewAgent(config, customPrompts); * * // Perform focused security review * const securityResult = await reviewAgent.performMultiLLMReview( * authenticationCode, * { fileName: 'auth.ts', language: 'typescript', focus: 'security' } * ); * * // Handle review completion with metrics * console.log(`Review completed with ${securityResult.results.length} providers`); * console.log(`Total findings: ${securityResult.totalFindings}`); * console.log(`Success rate: ${securityResult.successRate}%`); * ``` * * @since 1.0.0 */ export declare class AIReviewAgent { private config; private promptTemplate; private promptTemplates; private enabledProviders; constructor(config: AIReviewConfig, promptTemplates?: Record<string, PromptTemplate>); /** * Performs comprehensive multi-LLM code review analysis with hook integration * * Executes parallel code analysis using all enabled LLM providers to generate * comprehensive code review findings. Each provider analyzes the code independently, * and results are aggregated with consensus scoring and conflict resolution. * The method integrates with the WOARU Hook System for extensible analysis workflows * and provides detailed performance metrics. * * Analysis workflow: * 1. **Pre-analysis hooks**: Context preparation and provider validation * 2. **Parallel LLM requests**: Simultaneous analysis across all enabled providers * 3. **Response processing**: JSON parsing, validation, and finding extraction * 4. **Result aggregation**: Consensus building and confidence scoring * 5. **Post-analysis hooks**: Result validation and metrics collection * 6. **Error handling**: Graceful provider failures with partial results * * Features: * - Parallel provider execution for optimal performance * - Automatic retry logic with exponential backoff * - Response validation and malformed data handling * - Usage tracking for cost and performance monitoring * - Internationalization support for multilingual contexts * - Comprehensive error logging and debugging * * 🪝 **Hook Integration**: Seamlessly integrates with WOARU's rule-based AI system * * @param code - Source code content to analyze (supports all major programming languages) * @param context - Contextual information about the code being analyzed * @param context.fileName - Name of the file being analyzed for context-aware suggestions * @param context.language - Programming language for language-specific analysis * @param context.framework - Framework context (e.g., 'react', 'express', 'django') * @param context.projectType - Project type for targeted recommendations * @returns Promise resolving to comprehensive multi-provider review results * * @throws {Error} When no providers are enabled or all providers fail * * @example * ```typescript * const reviewAgent = new AIReviewAgent(config); * * // Analyze a React component * const reactCode = ` * import React, { useState } from 'react'; * * function UserProfile({ userId }) { * const [user, setUser] = useState(null); * // ... component implementation * }`; * * const context: CodeContext = { * fileName: 'UserProfile.tsx', * language: 'typescript', * framework: 'react', * projectType: 'spa' * }; * * const result = await reviewAgent.performMultiLLMReview(reactCode, context); * * // Access aggregated results * console.log(`Total findings: ${result.totalFindings}`); * console.log(`Success rate: ${result.successRate}%`); * console.log(`Analysis duration: ${result.totalDuration}ms`); * * // Process provider-specific findings * result.results.forEach(providerResult => { * if (providerResult.success) { * console.log(`\n${providerResult.provider} Analysis:`); * providerResult.findings.forEach(finding => { * console.log(`- [${finding.severity}] ${finding.type}: ${finding.message}`); * if (finding.lineNumber) console.log(` Line: ${finding.lineNumber}`); * if (finding.suggestion) console.log(` 💡 ${finding.suggestion}`); * }); * } else { * console.warn(`${providerResult.provider} failed: ${providerResult.error}`); * } * }); * ``` * * @example * ```typescript * // Handle analysis with error recovery * try { * const result = await reviewAgent.performMultiLLMReview(complexCode, context); * * // Check if we have usable results despite some failures * if (result.successRate >= 50) { * const highSeverityIssues = result.results * .flatMap(r => r.findings) * .filter(f => f.severity === 'high' || f.severity === 'critical'); * * if (highSeverityIssues.length > 0) { * console.log('🚨 Critical issues found:'); * highSeverityIssues.forEach(issue => { * console.log(`- ${issue.message}`); * }); * } * } else { * console.warn('Analysis quality may be compromised due to provider failures'); * } * } catch (error) { * console.error('Multi-LLM review failed completely:', error.message); * } * ``` * * @since 1.0.0 */ performMultiLLMReview(code: string, context: CodeContext): Promise<MultiLLMReviewResult>; /** * Call a specific LLM provider */ private callLLMProvider; /** * Call Anthropic Claude API */ private _callAnthropic; /** * Call OpenAI GPT API */ private _callOpenAI; /** * Call Azure OpenAI API */ private _callAzureOpenAI; /** * Call Google Gemini API */ private _callGoogle; /** * Call local Ollama API */ private _callOllama; /** * Interpolate template placeholders with safe JSON escaping */ private interpolateTemplate; /** * Build the complete prompt for LLM */ /** * Build provider-specific prompt using dynamic templates */ private buildPromptForProvider; /** * Build default prompt (legacy compatibility) */ private buildDefaultPrompt; /** * Parse AI response into structured findings */ private parseAIResponse; /** * Aggregate results from multiple LLMs */ private aggregateResults; /** * Find issues that multiple LLMs agree on */ private findConsensusIssues; /** * Find unique findings per LLM */ private findUniqueFindings; /** * Check if two findings are similar (simple implementation) */ private areFindingsSimilar; /** * Calculate string similarity (simple Levenshtein-based) */ private calculateStringSimilarity; /** * Calculate Levenshtein distance */ private levenshteinDistance; /** * Estimate cost for API calls */ private estimateCost; /** * Create default prompt template */ private createDefaultPromptTemplate; } //# sourceMappingURL=AIReviewAgent.d.ts.map