@bobmatnyc/ai-code-review
Version:
A TypeScript-based tool for automated code reviews using AI models from Google Gemini, Anthropic Claude, and OpenRouter
208 lines (207 loc) • 6.13 kB
TypeScript
/**
* @fileoverview Semantic chunking integration with robust fallback
*
* This module provides a seamless integration layer that attempts semantic
* chunking first, then gracefully falls back to the existing TokenAnalyzer
* approach when semantic analysis fails or is not available.
*/
import { SemanticAnalysisConfig } from './types';
import { ChunkGeneratorConfig } from './ChunkGenerator';
import { SemanticAnalysis, CodeChunk } from './types';
import { FileInfo } from '../../types/review';
/**
* Simple line-based chunking result for fallback
*/
interface LineBasedChunkingResult {
totalFiles: number;
estimatedTotalTokens: number;
chunks: Array<{
estimatedTokenCount: number;
priority: string;
startLine: number;
endLine: number;
}>;
}
/**
* Configuration for the integrated chunking system
*/
export interface ChunkingIntegrationConfig {
/** Whether to enable semantic chunking */
enableSemanticChunking: boolean;
/** Whether to enable fallback to TokenAnalyzer */
enableFallback: boolean;
/** Languages to force semantic analysis for */
forceSemantic: string[];
/** Languages to force traditional analysis for */
forceTraditional: string[];
/** Whether to prefer semantic for supported languages */
preferSemantic: boolean;
/** Performance threshold - max file size for semantic analysis (bytes) */
maxFileSizeForSemantic: number;
/** Whether to cache analysis results */
enableCaching: boolean;
/** Maximum tokens per consolidated batch */
maxTokensPerBatch: number;
/** Minimum threads per batch before consolidation */
minThreadsPerBatch: number;
/** Maximum threads per batch */
maxThreadsPerBatch: number;
/** Semantic analysis system configuration */
semanticConfig?: {
analyzer?: Partial<SemanticAnalysisConfig>;
chunkGenerator?: Partial<ChunkGeneratorConfig>;
};
}
/**
* System statistics interface
*/
interface SystemStats {
config: ChunkingIntegrationConfig;
supportedLanguages: string[];
cacheSize: number;
semanticSystemStats: {
size: number;
enabled: boolean;
};
}
/**
* Result of integrated chunking analysis
*/
export interface IntegratedChunkingResult {
/** Generated chunks */
chunks: CodeChunk[];
/** Analysis method used */
method: 'semantic' | 'traditional' | 'hybrid';
/** Whether fallback was used */
fallbackUsed: boolean;
/** Semantic analysis result (if available) */
semanticAnalysis?: SemanticAnalysis;
/** Line-based chunking result (if used) */
lineBasedResult?: LineBasedChunkingResult;
/** Errors encountered */
errors: string[];
/** Performance metrics */
metrics: {
analysisTimeMs: number;
chunkingTimeMs: number;
totalTokens: number;
chunksGenerated: number;
};
/** Reasoning for the chunking approach used */
reasoning: string;
}
/**
* Integrated semantic chunking system with robust fallback
*/
export declare class SemanticChunkingIntegration {
private config;
private semanticAnalyzer;
private chunkGenerator;
private cache;
constructor(config?: Partial<ChunkingIntegrationConfig>);
/**
* Check if a file can be analyzed semantically
*/
private canAnalyzeFile;
/**
* Detect language from file path
*/
private detectLanguageFromPath;
/**
* Check if a language is supported for semantic analysis
*/
private isLanguageSupported;
/**
* Consolidate semantic threads into efficient batches
*/
private consolidateSemanticThreads;
/**
* Group chunks by semantic affinity (related code structures)
*/
private groupChunksByAffinity;
/**
* Create batches from a group of related chunks
*/
private createBatchesFromGroup;
/**
* Merge multiple chunks into a single consolidated batch
*/
private mergeBatchChunks;
/**
* Analyze files and generate optimal chunks with fallback
*/
analyzeAndChunk(files: FileInfo[], options?: {
reviewType?: string;
modelName?: string;
forceSemantic?: boolean;
forceTraditional?: boolean;
useCache?: boolean;
}): Promise<IntegratedChunkingResult>;
/**
* Determine the best chunking strategy for the given files
*/
private determineChunkingStrategy;
/**
* Attempt semantic chunking for files
*/
private attemptSemanticChunking;
/**
* Perform traditional TokenAnalyzer-based chunking
*/
private performTraditionalChunking;
/**
* Get review focus for traditional chunking based on review type
*/
private getTraditionalReviewFocus;
/**
* Sanitize file name for use in chunk IDs
*/
private sanitizeFileName;
/**
* Generate cache key for analysis results
*/
private generateCacheKey;
/**
* Simple hash function for cache keys
*/
private simpleHash;
/**
* Check if semantic chunking is available for files
*/
canUseSemanticChunking(files: FileInfo[]): boolean;
/**
* Get system statistics
*/
getStats(): SystemStats;
/**
* Clear all caches
*/
clearCache(): void;
/**
* Update configuration
*/
updateConfig(config: Partial<ChunkingIntegrationConfig>): void;
/**
* Get current configuration
*/
getConfig(): ChunkingIntegrationConfig;
}
/**
* Default integration instance
*/
export declare const semanticChunkingIntegration: SemanticChunkingIntegration;
/**
* Convenience function for integrated chunking with fallback
*/
export declare function analyzeAndChunkWithFallback(files: FileInfo[], options?: {
reviewType?: string;
modelName?: string;
forceSemantic?: boolean;
forceTraditional?: boolean;
useCache?: boolean;
}): Promise<IntegratedChunkingResult>;
/**
* Convenience function to check semantic chunking availability
*/
export declare function isSemanticChunkingAvailable(files: FileInfo[]): boolean;
export {};