/** * @file Intelligent Prefetch Engine * @description ML-like prefetch prediction using user behavior patterns, * navigation history, and heuristics to predict and prefetch resources. * * Features: * - Behavior-based prediction * - Navigation pattern learning * - Viewport-based prefetching * - Network-aware decisions * - Probability scoring * - Prefetch budgeting */ /** * Navigation event for pattern learning */ export interface NavigationEvent { from: string; to: string; timestamp: number; duration: number; interactionType: 'click' | 'hover' | 'focus' | 'scroll' | 'programmatic'; } /** * Prefetch candidate */ export interface PrefetchCandidate { url: string; type: 'route' | 'data' | 'asset'; probability: number; priority: 'high' | 'medium' | 'low'; size?: number; reason: string[]; } /** * Prediction result */ export interface PredictionResult { candidates: PrefetchCandidate[]; confidence: number; factors: PredictionFactor[]; timestamp: number; } /** * Prediction factor */ export interface PredictionFactor { name: string; weight: number; value: number; description: string; } /** * User behavior metrics */ export interface BehaviorMetrics { sessionDuration: number; pageViews: number; avgTimeOnPage: number; scrollDepth: number; clickRate: number; hoverPatterns: Map; navigationPatterns: Map; } /** * Intelligent prefetch configuration */ export interface IntelligentPrefetchConfig { /** Enable prediction */ enabled: boolean; /** Minimum probability to trigger prefetch */ probabilityThreshold: number; /** Maximum concurrent prefetches */ maxConcurrentPrefetches: number; /** Prefetch budget in bytes */ prefetchBudget: number; /** Enable network quality awareness */ networkAware: boolean; /** Minimum network quality for prefetch */ minNetworkQuality: '4g' | '3g' | '2g' | 'slow-2g'; /** Enable data saver respect */ respectDataSaver: boolean; /** Learning rate for pattern updates */ learningRate: number; /** History size for pattern learning */ maxHistorySize: number; /** Debug mode */ debug: boolean; } /** * ML-like prefetch prediction engine */ export declare class IntelligentPrefetchEngine { private config; private navigationHistory; private transitionProbabilities; private hoverHistory; private prefetchedUrls; private currentBudgetUsed; private readonly sessionStartTime; private pageViewCount; private listeners; constructor(config?: Partial); /** * Record a navigation event */ recordNavigation(event: Omit): void; /** * Record a hover event */ recordHover(url: string): void; /** * Get prefetch predictions for current page */ predict(currentUrl: string, availableUrls: string[]): PredictionResult; /** * Mark URL as prefetched */ markPrefetched(url: string, size?: number): void; /** * Check if URL was prefetched */ wasPrefetched(url: string): boolean; /** * Get behavior metrics */ getBehaviorMetrics(): BehaviorMetrics; /** * Subscribe to predictions */ subscribe(callback: (prediction: PredictionResult) => void): () => void; /** * Reset learning data */ reset(): void; /** * Get remaining budget */ getRemainingBudget(): number; private calculateProbability; private getPredictionFactors; private getTransitionProbability; private getHoverScore; private getRecencyScore; private getUrlSimilarity; private getTimeBasedScore; private updateTransitionProbabilities; private shouldPrefetch; private calculatePriority; private calculateConfidence; private inferResourceType; private normalizeUrl; private getNavigationPatterns; private calculateScrollDepth; private calculateClickRate; private getNetworkInfo; private deduplicateFactors; private emptyPrediction; private notifyListeners; private loadStoredPatterns; private persistPatterns; private clearStoredPatterns; private log; } /** * Get or create the global intelligent prefetch engine */ export declare function getIntelligentPrefetchEngine(config?: Partial): IntelligentPrefetchEngine; /** * Reset the engine instance */ export declare function resetIntelligentPrefetchEngine(): void;