/** * Smart Dependencies Tool - 83% Token Reduction * * Achieves token reduction through: * 1. Dependency graph caching (reuse across multiple queries) * 2. Incremental updates (only rebuild changed nodes) * 3. Compact graph representation (edges only, not full AST) * 4. Smart query modes (impact, circular, unused - return only what's needed) * 5. External vs internal separation (filter by relevance) * * Target: 83% reduction vs parsing and returning full file contents * * Week 5 - Phase 2 Track 2A */ import { CacheEngine } from '../../core/cache-engine.js'; import { TokenCounter } from '../../core/token-counter.js'; import { MetricsCollector } from '../../core/metrics.js'; /** * Represents an import in a file */ export interface DependencyImport { source: string; specifiers: string[]; isExternal: boolean; isDynamic: boolean; line: number; } /** * Represents an export in a file */ export interface DependencyExport { name: string; type: 'named' | 'default' | 'namespace'; isReexport: boolean; source?: string; line: number; } /** * Node in the dependency graph */ export interface DependencyNode { file: string; hash: string; imports: DependencyImport[]; exports: DependencyExport[]; importedBy: string[]; importedByCount: number; lastAnalyzed: number; } /** * Circular dependency chain */ export interface CircularDependency { cycle: string[]; depth: number; severity: 'low' | 'medium' | 'high'; } /** * Unused import/export detection */ export interface UnusedDependency { file: string; type: 'import' | 'export'; name: string; source?: string; line: number; reason: string; } /** * Dependency impact analysis */ export interface DependencyImpact { file: string; directDependents: string[]; indirectDependents: string[]; totalImpact: number; criticalPath: string[][]; } export interface SmartDependenciesOptions { cwd?: string; files?: string[]; exclude?: string[]; mode?: 'graph' | 'circular' | 'unused' | 'impact'; targetFile?: string; includeExternal?: boolean; maxDepth?: number; useCache?: boolean; incrementalUpdate?: boolean; ttl?: number; format?: 'compact' | 'detailed'; includeMetadata?: boolean; } export interface SmartDependenciesResult { success: boolean; mode: string; metadata: { totalFiles: number; analyzedFiles: number; externalDependencies: number; internalDependencies: number; tokensSaved: number; tokenCount: number; originalTokenCount: number; compressionRatio: number; duration: number; cacheHit: boolean; incrementalUpdate: boolean; }; graph?: Map; circular?: CircularDependency[]; unused?: UnusedDependency[]; impact?: DependencyImpact; error?: string; } export declare class SmartDependenciesTool { private cache; private tokenCounter; private metrics; constructor(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector); /** * Main entry point for dependency analysis * Alias for analyze() to maintain API consistency with other tools */ run(options?: SmartDependenciesOptions): Promise; /** * Core dependency analysis implementation */ analyze(options?: SmartDependenciesOptions): Promise; /** * Build dependency graph or load from cache */ private buildOrLoadGraph; /** * Build complete dependency graph */ private buildFullGraph; /** * Analyze a single file for dependencies */ private analyzeFile; /** * Extract imports from AST */ private extractImports; /** * Extract exports from AST */ private extractExports; /** * Build reverse dependencies (which files import this file) */ private buildReverseDependencies; /** * Detect files that have changed since last analysis */ private detectChangedFiles; /** * Incrementally update graph with changed files */ private incrementalGraphUpdate; /** * Detect circular dependencies */ private detectCircularDependencies; /** * Detect unused imports and exports */ private detectUnusedDependencies; /** * Analyze impact of changing a file */ private analyzeImpact; /** * Transform graph to compact output format */ private transformGraphOutput; /** * Create compact graph representation (edges only) */ private compactGraphRepresentation; /** * Utility: Check if dependency is external (node_modules) */ private isExternalDependency; /** * Utility: Resolve relative path */ private resolveRelativePath; /** * Count external dependencies */ private countExternalDeps; /** * Count internal dependencies */ private countInternalDeps; /** * Estimate tokens for graph representation */ private estimateGraphTokens; /** * Estimate tokens for full file contents */ private estimateFullFileTokens; /** * Cache graph */ private cacheGraph; /** * Serialize graph for caching */ private serializeGraph; /** * Deserialize graph from cache */ private deserializeGraph; /** * Get dependency statistics */ getStats(): { totalAnalyses: number; cacheHits: number; incrementalUpdates: number; totalTokensSaved: number; averageReduction: number; }; } /** * Factory function for getting SmartDependenciesTool instance with injected dependencies */ export declare function getSmartDependenciesTool(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector): SmartDependenciesTool; /** * CLI-friendly function for running smart dependencies analysis */ export declare function runSmartDependencies(options: SmartDependenciesOptions): Promise; /** * MCP Tool Definition */ export declare const SMART_DEPENDENCIES_TOOL_DEFINITION: { name: string; description: string; inputSchema: { type: string; properties: { cwd: { type: string; description: string; }; files: { type: string; items: { type: string; }; description: string; }; mode: { type: string; enum: string[]; description: string; default: string; }; targetFile: { type: string; description: string; }; includeExternal: { type: string; description: string; default: boolean; }; maxDepth: { type: string; description: string; }; useCache: { type: string; description: string; default: boolean; }; incrementalUpdate: { type: string; description: string; default: boolean; }; format: { type: string; enum: string[]; description: string; default: string; }; }; }; }; //# sourceMappingURL=smart-dependencies.d.ts.map