@aiready/context-analyzer 0.21.6 → 0.21.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,36 +1,68 @@
1
- import { ToolProvider, Severity, ScanOptions, CostConfig, ToolScoringOutput } from '@aiready/core';
1
+ import { ToolProvider, Severity, ScanOptions, FileContent, CostConfig, ToolScoringOutput } from '@aiready/core';
2
2
 
3
3
  /**
4
4
  * Context Analyzer Tool Provider
5
5
  */
6
6
  declare const ContextAnalyzerProvider: ToolProvider;
7
7
 
8
+ /**
9
+ * Options for the Context Analyzer tool.
10
+ * Controls thresholds for import depth, context budget, and cohesion.
11
+ */
8
12
  interface ContextAnalyzerOptions extends ScanOptions {
13
+ /** Maximum acceptable import depth (default: 5) */
9
14
  maxDepth?: number;
15
+ /** Maximum acceptable token budget for a single context (default: 25000) */
10
16
  maxContextBudget?: number;
17
+ /** Minimum acceptable cohesion score between 0 and 1 (default: 0.6) */
11
18
  minCohesion?: number;
19
+ /** Maximum acceptable fragmentation score between 0 and 1 (default: 0.5) */
12
20
  maxFragmentation?: number;
21
+ /** Analysis focus area: fragmentation, cohesion, depth, or all (default: 'all') */
13
22
  focus?: 'fragmentation' | 'cohesion' | 'depth' | 'all';
23
+ /** Whether to include node_modules in the analysis (default: false) */
14
24
  includeNodeModules?: boolean;
15
25
  }
26
+ /**
27
+ * The result of a context analysis for a single file or module.
28
+ * Includes metrics for tokens, dependencies, cohesion, and AI impact.
29
+ */
16
30
  interface ContextAnalysisResult {
31
+ /** The file path being analyzed */
17
32
  file: string;
33
+ /** Total number of tokens in this file */
18
34
  tokenCost: number;
35
+ /** Total lines of code in the file */
19
36
  linesOfCode: number;
37
+ /** Maximum depth of the import tree for this file */
20
38
  importDepth: number;
39
+ /** Total number of transitive dependencies */
21
40
  dependencyCount: number;
41
+ /** List of all files in the dependency tree */
22
42
  dependencyList: string[];
43
+ /** Detected circular dependency chains */
23
44
  circularDeps: string[][];
45
+ /** Cohesion score from 0 to 1 (1 is perfect cohesion) */
24
46
  cohesionScore: number;
47
+ /** Detected domain categories for the module */
25
48
  domains: string[];
49
+ /** Number of exported symbols */
26
50
  exportCount: number;
51
+ /** Total tokens required to understand this file and all its dependencies */
27
52
  contextBudget: number;
53
+ /** Fragmentation score from 0 to 1 (0 is well-grouped) */
28
54
  fragmentationScore: number;
55
+ /** List of files that should be loaded together for full context */
29
56
  relatedFiles: string[];
57
+ /** The semantic classification of the file (e.g. 'barrel-export', 'service-file') */
30
58
  fileClassification: FileClassification;
59
+ /** Overall severity of identified issues */
31
60
  severity: Severity | 'critical' | 'major' | 'minor' | 'info';
61
+ /** List of specific structural problems found */
32
62
  issues: string[];
63
+ /** Actionable suggestions for improving context readiness */
33
64
  recommendations: string[];
65
+ /** Estimated tokens that could be saved by following recommendations */
34
66
  potentialSavings: number;
35
67
  }
36
68
  /**
@@ -129,19 +161,28 @@ interface TypeDependency {
129
161
  }
130
162
 
131
163
  /**
132
- * Calculate cohesion score (how related are exports in a file)
133
- * Legacy wrapper for backward compatibility with exact test expectations
164
+ * Calculate cohesion score (how related are exports in a file).
165
+ * Legacy wrapper for backward compatibility with exact test expectations.
166
+ *
167
+ * @param exports - List of exported symbols
168
+ * @param filePath - Path to the file being analyzed
169
+ * @param options - Additional options for cohesion calculation
170
+ * @returns Cohesion score between 0 and 1
134
171
  */
135
172
  declare function calculateCohesion(exports: ExportInfo[], filePath?: string, options?: any): number;
136
173
  /**
137
174
  * Analyze AI context window cost for a codebase
138
175
  */
176
+ /**
177
+ * Performs deep context analysis of a project.
178
+ * Scans files, builds a dependency graph, calculates context budgets,
179
+ * and identifies structural issues like high fragmentation or depth.
180
+ *
181
+ * @param options - Analysis parameters including root directory and focus areas
182
+ * @returns Comprehensive analysis results with metrics and identified issues
183
+ */
139
184
  declare function analyzeContext(options: ContextAnalyzerOptions): Promise<ContextAnalysisResult[]>;
140
185
 
141
- interface FileContent {
142
- file: string;
143
- content: string;
144
- }
145
186
  /**
146
187
  * Auto-detect domain keywords from workspace folder structure
147
188
  */
@@ -172,6 +213,15 @@ declare function detectCircularDependencies(graph: DependencyGraph): string[][];
172
213
  /**
173
214
  * Calculate cohesion score (how related are exports in a file)
174
215
  */
216
+ /**
217
+ * Calculates a cohesion score (0-1) for a module based on its exports,
218
+ * shared imports, and internal structure. High cohesion indicates
219
+ * a well-focused module that is easy for AI models to reason about.
220
+ *
221
+ * @param exports - Exported symbols and their metadata
222
+ * @param imports - Imported symbols and their sources
223
+ * @returns Cohesion score between 0 and 1
224
+ */
175
225
  declare function calculateEnhancedCohesion(exports: ExportInfo[], filePath?: string, options?: {
176
226
  coUsageMatrix?: Map<string, Map<string, number>>;
177
227
  weights?: {
@@ -227,76 +277,6 @@ declare const Classification: {
227
277
  * @returns The determined file classification
228
278
  */
229
279
  declare function classifyFile(node: DependencyNode, cohesionScore?: number, domains?: string[]): FileClassification;
230
- /**
231
- * Detect if a file is a barrel export (index.ts)
232
- *
233
- * @param node The dependency node to check
234
- * @returns True if the file appears to be a barrel export
235
- */
236
- declare function isBarrelExport(node: DependencyNode): boolean;
237
- /**
238
- * Detect if a file is primarily type definitions
239
- *
240
- * @param node The dependency node to check
241
- * @returns True if the file appears to be primarily types
242
- */
243
- declare function isTypeDefinition(node: DependencyNode): boolean;
244
- /**
245
- * Detect if a file is a utility module
246
- *
247
- * @param node The dependency node to check
248
- * @returns True if the file appears to be a utility module
249
- */
250
- declare function isUtilityModule(node: DependencyNode): boolean;
251
- /**
252
- * Detect if a file is a Lambda/API handler
253
- *
254
- * @param node The dependency node to check
255
- * @returns True if the file appears to be a Lambda handler
256
- */
257
- declare function isLambdaHandler(node: DependencyNode): boolean;
258
- /**
259
- * Detect if a file is a service file
260
- *
261
- * @param node The dependency node to check
262
- * @returns True if the file appears to be a service file
263
- */
264
- declare function isServiceFile(node: DependencyNode): boolean;
265
- /**
266
- * Detect if a file is an email template/layout
267
- *
268
- * @param node The dependency node to check
269
- * @returns True if the file appears to be an email template
270
- */
271
- declare function isEmailTemplate(node: DependencyNode): boolean;
272
- /**
273
- * Detect if a file is a parser/transformer
274
- *
275
- * @param node The dependency node to check
276
- * @returns True if the file appears to be a parser
277
- */
278
- declare function isParserFile(node: DependencyNode): boolean;
279
- /**
280
- * Detect if a file is a session/state management file
281
- *
282
- * @param node The dependency node to check
283
- * @returns True if the file appears to be a session/state file
284
- */
285
- declare function isSessionFile(node: DependencyNode): boolean;
286
- /**
287
- * Detect if a file is a configuration or schema file
288
- *
289
- * @param node The dependency node to check
290
- * @returns True if the file appears to be a config file
291
- */
292
- declare function isConfigFile(node: DependencyNode): boolean;
293
- /**
294
- * Detect if a file is a Next.js App Router page
295
- *
296
- * @param node The dependency node to check
297
- * @returns True if the file appears to be a Next.js page
298
- */
299
- declare function isNextJsPage(node: DependencyNode): boolean;
300
280
  /**
301
281
  * Adjust cohesion score based on file classification
302
282
  *
@@ -317,6 +297,10 @@ declare function adjustFragmentationForClassification(baseFragmentation: number,
317
297
 
318
298
  /**
319
299
  * Group files by domain to detect module clusters
300
+ * @param graph - The dependency graph to analyze
301
+ * @param options - Optional configuration options
302
+ * @param options.useLogScale - Whether to use logarithmic scaling for calculations
303
+ * @returns Array of module clusters
320
304
  */
321
305
  declare function detectModuleClusters(graph: DependencyGraph, options?: {
322
306
  useLogScale?: boolean;
@@ -355,6 +339,9 @@ declare function mapScoreToRating(score: number): string;
355
339
  /**
356
340
  * Generate smart defaults for context analysis based on repository size
357
341
  * Automatically tunes thresholds to target ~10 most serious issues
342
+ * @param directory - The root directory to analyze
343
+ * @param userOptions - Partial user-provided options to merge with defaults
344
+ * @returns Complete ContextAnalyzerOptions with smart defaults
358
345
  */
359
346
  declare function getSmartDefaults(directory: string, userOptions: Partial<ContextAnalyzerOptions>): Promise<ContextAnalyzerOptions>;
360
347
 
@@ -365,14 +352,21 @@ declare function generateSummary(results: ContextAnalysisResult[], options?: any
365
352
 
366
353
  /**
367
354
  * Build co-usage matrix: track which files are imported together
355
+ * @param graph - The dependency graph to analyze
356
+ * @returns Map of file to co-usage counts
368
357
  */
369
358
  declare function buildCoUsageMatrix(graph: DependencyGraph): Map<string, Map<string, number>>;
370
359
  /**
371
360
  * Extract type dependencies from AST exports
361
+ * @param graph - The dependency graph to analyze
362
+ * @returns Map of type references to files that use them
372
363
  */
373
364
  declare function buildTypeGraph(graph: DependencyGraph): Map<string, Set<string>>;
374
365
  /**
375
366
  * Find semantic clusters using co-usage patterns
367
+ * @param coUsageMatrix - The co-usage matrix from buildCoUsageMatrix
368
+ * @param minCoUsage - Minimum co-usage count to consider (default: 3)
369
+ * @returns Map of cluster representative files to their cluster members
376
370
  */
377
371
  declare function findSemanticClusters(coUsageMatrix: Map<string, Map<string, number>>, minCoUsage?: number): Map<string, string[]>;
378
372
  /**
@@ -408,4 +402,4 @@ declare function generateHTMLReport(summary: ReturnType<typeof generateSummary>,
408
402
  */
409
403
  declare function runInteractiveSetup(directory: string, current: any): Promise<any>;
410
404
 
411
- export { Classification, type CoUsageData, type ContextAnalysisResult, type ContextAnalyzerOptions, ContextAnalyzerProvider, type ContextSummary, type DependencyGraph, type DependencyNode, type DomainAssignment, type DomainSignals, type ExportInfo, type FileClassification, type ModuleCluster, type TypeDependency, adjustCohesionForClassification, adjustFragmentationForClassification, analyzeContext, buildCoUsageMatrix, buildDependencyGraph, buildTypeGraph, calculateCohesion, calculateContextBudget, calculateContextScore, calculateDirectoryDistance, calculateDomainConfidence, calculateEnhancedCohesion, calculateFragmentation, calculateImportDepth, calculatePathEntropy, calculateStructuralCohesionFromCoUsage, classifyFile, detectCircularDependencies, detectModuleClusters, displayConsoleReport, extractDomainKeywordsFromPaths, extractExports, findConsolidationCandidates, findSemanticClusters, generateHTMLReport, generateSummary, getClassificationRecommendations, getCoUsageData, getGeneralRecommendations, getSmartDefaults, getTransitiveDependencies, inferDomain, inferDomainFromSemantics, isBarrelExport, isConfigFile, isEmailTemplate, isLambdaHandler, isNextJsPage, isParserFile, isServiceFile, isSessionFile, isTypeDefinition, isUtilityModule, mapScoreToRating, runInteractiveSetup };
405
+ export { Classification, type CoUsageData, type ContextAnalysisResult, type ContextAnalyzerOptions, ContextAnalyzerProvider, type ContextSummary, type DependencyGraph, type DependencyNode, type DomainAssignment, type DomainSignals, type ExportInfo, type FileClassification, type ModuleCluster, type TypeDependency, adjustCohesionForClassification, adjustFragmentationForClassification, analyzeContext, buildCoUsageMatrix, buildDependencyGraph, buildTypeGraph, calculateCohesion, calculateContextBudget, calculateContextScore, calculateDirectoryDistance, calculateDomainConfidence, calculateEnhancedCohesion, calculateFragmentation, calculateImportDepth, calculatePathEntropy, calculateStructuralCohesionFromCoUsage, classifyFile, detectCircularDependencies, detectModuleClusters, displayConsoleReport, extractDomainKeywordsFromPaths, extractExports, findConsolidationCandidates, findSemanticClusters, generateHTMLReport, generateSummary, getClassificationRecommendations, getCoUsageData, getGeneralRecommendations, getSmartDefaults, getTransitiveDependencies, inferDomain, inferDomainFromSemantics, mapScoreToRating, runInteractiveSetup };