@aiready/context-analyzer 0.21.5 → 0.21.7

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.
Files changed (163) hide show
  1. package/.aiready/aiready-report-20260314-222254.json +39216 -0
  2. package/.aiready/aiready-report-20260314-223947.json +3413 -0
  3. package/.aiready/aiready-report-20260314-224112.json +3413 -0
  4. package/.aiready/aiready-report-20260314-224302.json +2973 -0
  5. package/.aiready/aiready-report-20260314-224939.json +3092 -0
  6. package/.aiready/aiready-report-20260314-225154.json +3092 -0
  7. package/.turbo/turbo-build.log +26 -24
  8. package/.turbo/turbo-lint.log +5 -6
  9. package/.turbo/turbo-test.log +41 -119
  10. package/dist/__tests__/analyzer.test.js +55 -14
  11. package/dist/__tests__/analyzer.test.js.map +1 -1
  12. package/dist/__tests__/cluster-detector.test.d.ts +2 -0
  13. package/dist/__tests__/cluster-detector.test.d.ts.map +1 -0
  14. package/dist/__tests__/cluster-detector.test.js +121 -0
  15. package/dist/__tests__/cluster-detector.test.js.map +1 -0
  16. package/dist/__tests__/contract.test.d.ts +2 -0
  17. package/dist/__tests__/contract.test.d.ts.map +1 -0
  18. package/dist/__tests__/contract.test.js +59 -0
  19. package/dist/__tests__/contract.test.js.map +1 -0
  20. package/dist/__tests__/enhanced-cohesion.test.js +12 -2
  21. package/dist/__tests__/enhanced-cohesion.test.js.map +1 -1
  22. package/dist/__tests__/file-classification.test.d.ts +2 -0
  23. package/dist/__tests__/file-classification.test.d.ts.map +1 -0
  24. package/dist/__tests__/file-classification.test.js +749 -0
  25. package/dist/__tests__/file-classification.test.js.map +1 -0
  26. package/dist/__tests__/fragmentation-advanced.test.js +2 -8
  27. package/dist/__tests__/fragmentation-advanced.test.js.map +1 -1
  28. package/dist/__tests__/fragmentation-coupling.test.js +2 -2
  29. package/dist/__tests__/fragmentation-coupling.test.js.map +1 -1
  30. package/dist/__tests__/fragmentation-log.test.js +3 -7
  31. package/dist/__tests__/fragmentation-log.test.js.map +1 -1
  32. package/dist/__tests__/provider.test.d.ts +2 -0
  33. package/dist/__tests__/provider.test.d.ts.map +1 -0
  34. package/dist/__tests__/provider.test.js +72 -0
  35. package/dist/__tests__/provider.test.js.map +1 -0
  36. package/dist/__tests__/remediation.test.d.ts +2 -0
  37. package/dist/__tests__/remediation.test.d.ts.map +1 -0
  38. package/dist/__tests__/remediation.test.js +61 -0
  39. package/dist/__tests__/remediation.test.js.map +1 -0
  40. package/dist/__tests__/scoring.test.js +196 -16
  41. package/dist/__tests__/scoring.test.js.map +1 -1
  42. package/dist/__tests__/structural-cohesion.test.js +8 -2
  43. package/dist/__tests__/structural-cohesion.test.js.map +1 -1
  44. package/dist/analyzer.d.ts +31 -94
  45. package/dist/analyzer.d.ts.map +1 -1
  46. package/dist/analyzer.js +260 -678
  47. package/dist/analyzer.js.map +1 -1
  48. package/dist/analyzers/python-context.d.ts.map +1 -1
  49. package/dist/analyzers/python-context.js +10 -8
  50. package/dist/analyzers/python-context.js.map +1 -1
  51. package/dist/ast-utils.d.ts +16 -0
  52. package/dist/ast-utils.d.ts.map +1 -0
  53. package/dist/ast-utils.js +81 -0
  54. package/dist/ast-utils.js.map +1 -0
  55. package/dist/chunk-2HE27YEV.mjs +1739 -0
  56. package/dist/chunk-64U3PNO3.mjs +94 -0
  57. package/dist/chunk-CDIVYADN.mjs +2110 -0
  58. package/dist/chunk-D25B5LZR.mjs +1739 -0
  59. package/dist/chunk-D3SIHB2V.mjs +2118 -0
  60. package/dist/chunk-FNPSK3CG.mjs +1760 -0
  61. package/dist/chunk-GXTGOLZT.mjs +92 -0
  62. package/dist/chunk-KDUUZQBK.mjs +1692 -0
  63. package/dist/chunk-KWIS5FQP.mjs +1739 -0
  64. package/dist/chunk-LERPI33Y.mjs +2060 -0
  65. package/dist/chunk-MZP3G7TF.mjs +2118 -0
  66. package/dist/chunk-NOHK5DLU.mjs +2173 -0
  67. package/dist/chunk-ORLC5Y4J.mjs +1787 -0
  68. package/dist/chunk-OTCQL7DY.mjs +2045 -0
  69. package/dist/chunk-RRB2C34Q.mjs +1738 -0
  70. package/dist/chunk-SFK6XTJE.mjs +2110 -0
  71. package/dist/chunk-U5R2FTCR.mjs +1803 -0
  72. package/dist/chunk-UU4HZ7ZT.mjs +1849 -0
  73. package/dist/chunk-WKOZOHOU.mjs +2060 -0
  74. package/dist/chunk-XIXAWCMS.mjs +1760 -0
  75. package/dist/chunk-XTAXUNQN.mjs +1742 -0
  76. package/dist/classifier.d.ts +114 -0
  77. package/dist/classifier.d.ts.map +1 -0
  78. package/dist/classifier.js +439 -0
  79. package/dist/classifier.js.map +1 -0
  80. package/dist/cli.js +681 -1170
  81. package/dist/cli.js.map +1 -1
  82. package/dist/cli.mjs +63 -533
  83. package/dist/cluster-detector.d.ts +8 -0
  84. package/dist/cluster-detector.d.ts.map +1 -0
  85. package/dist/cluster-detector.js +70 -0
  86. package/dist/cluster-detector.js.map +1 -0
  87. package/dist/defaults.d.ts +7 -0
  88. package/dist/defaults.d.ts.map +1 -0
  89. package/dist/defaults.js +54 -0
  90. package/dist/defaults.js.map +1 -0
  91. package/dist/graph-builder.d.ts +33 -0
  92. package/dist/graph-builder.d.ts.map +1 -0
  93. package/dist/graph-builder.js +225 -0
  94. package/dist/graph-builder.js.map +1 -0
  95. package/dist/index.d.mts +93 -106
  96. package/dist/index.d.ts +93 -106
  97. package/dist/index.d.ts.map +1 -1
  98. package/dist/index.js +932 -745
  99. package/dist/index.js.map +1 -1
  100. package/dist/index.mjs +262 -28
  101. package/dist/metrics.d.ts +34 -0
  102. package/dist/metrics.d.ts.map +1 -0
  103. package/dist/metrics.js +170 -0
  104. package/dist/metrics.js.map +1 -0
  105. package/dist/provider.d.ts +6 -0
  106. package/dist/provider.d.ts.map +1 -0
  107. package/dist/provider.js +48 -0
  108. package/dist/provider.js.map +1 -0
  109. package/dist/python-context-3GZKN3LR.mjs +162 -0
  110. package/dist/python-context-O2EN3M6Z.mjs +162 -0
  111. package/dist/remediation.d.ts +25 -0
  112. package/dist/remediation.d.ts.map +1 -0
  113. package/dist/remediation.js +98 -0
  114. package/dist/remediation.js.map +1 -0
  115. package/dist/scoring.d.ts +3 -7
  116. package/dist/scoring.d.ts.map +1 -1
  117. package/dist/scoring.js +57 -48
  118. package/dist/scoring.js.map +1 -1
  119. package/dist/semantic-analysis.d.ts +12 -23
  120. package/dist/semantic-analysis.d.ts.map +1 -1
  121. package/dist/semantic-analysis.js +172 -110
  122. package/dist/semantic-analysis.js.map +1 -1
  123. package/dist/summary.d.ts +6 -0
  124. package/dist/summary.d.ts.map +1 -0
  125. package/dist/summary.js +92 -0
  126. package/dist/summary.js.map +1 -0
  127. package/dist/types.d.ts +9 -2
  128. package/dist/types.d.ts.map +1 -1
  129. package/dist/utils/output-formatter.d.ts +14 -0
  130. package/dist/utils/output-formatter.d.ts.map +1 -0
  131. package/dist/utils/output-formatter.js +338 -0
  132. package/dist/utils/output-formatter.js.map +1 -0
  133. package/package.json +2 -2
  134. package/src/__tests__/analyzer.test.ts +1 -1
  135. package/src/__tests__/auto-detection.test.ts +1 -1
  136. package/src/__tests__/contract.test.ts +1 -1
  137. package/src/__tests__/enhanced-cohesion.test.ts +1 -1
  138. package/src/__tests__/file-classification.test.ts +1 -1
  139. package/src/__tests__/fragmentation-advanced.test.ts +1 -1
  140. package/src/__tests__/fragmentation-coupling.test.ts +1 -1
  141. package/src/__tests__/fragmentation-log.test.ts +1 -1
  142. package/src/__tests__/provider.test.ts +1 -1
  143. package/src/__tests__/structural-cohesion.test.ts +1 -1
  144. package/src/analyzer.ts +112 -317
  145. package/src/analyzers/python-context.ts +7 -76
  146. package/src/ast-utils.ts +2 -2
  147. package/src/classifier.ts +13 -328
  148. package/src/cli-action.ts +110 -0
  149. package/src/cli.ts +3 -701
  150. package/src/cluster-detector.ts +28 -1
  151. package/src/defaults.ts +3 -0
  152. package/src/graph-builder.ts +10 -91
  153. package/src/heuristics.ts +216 -0
  154. package/src/index.ts +6 -0
  155. package/src/issue-analyzer.ts +158 -0
  156. package/src/metrics.ts +9 -0
  157. package/src/scoring.ts +3 -5
  158. package/src/semantic-analysis.ts +8 -14
  159. package/src/summary.ts +62 -106
  160. package/src/types.ts +52 -20
  161. package/src/utils/dependency-graph-utils.ts +126 -0
  162. package/src/utils/output-formatter.ts +411 -0
  163. package/src/utils/string-utils.ts +21 -0
package/dist/index.d.mts 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
  /**
@@ -128,10 +160,29 @@ interface TypeDependency {
128
160
  usedBy: string[];
129
161
  }
130
162
 
131
- interface FileContent {
132
- file: string;
133
- content: string;
134
- }
163
+ /**
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
171
+ */
172
+ declare function calculateCohesion(exports: ExportInfo[], filePath?: string, options?: any): number;
173
+ /**
174
+ * Analyze AI context window cost for a codebase
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
+ */
184
+ declare function analyzeContext(options: ContextAnalyzerOptions): Promise<ContextAnalysisResult[]>;
185
+
135
186
  /**
136
187
  * Auto-detect domain keywords from workspace folder structure
137
188
  */
@@ -162,6 +213,15 @@ declare function detectCircularDependencies(graph: DependencyGraph): string[][];
162
213
  /**
163
214
  * Calculate cohesion score (how related are exports in a file)
164
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
+ */
165
225
  declare function calculateEnhancedCohesion(exports: ExportInfo[], filePath?: string, options?: {
166
226
  coUsageMatrix?: Map<string, Map<string, number>>;
167
227
  weights?: {
@@ -217,76 +277,6 @@ declare const Classification: {
217
277
  * @returns The determined file classification
218
278
  */
219
279
  declare function classifyFile(node: DependencyNode, cohesionScore?: number, domains?: string[]): FileClassification;
220
- /**
221
- * Detect if a file is a barrel export (index.ts)
222
- *
223
- * @param node The dependency node to check
224
- * @returns True if the file appears to be a barrel export
225
- */
226
- declare function isBarrelExport(node: DependencyNode): boolean;
227
- /**
228
- * Detect if a file is primarily type definitions
229
- *
230
- * @param node The dependency node to check
231
- * @returns True if the file appears to be primarily types
232
- */
233
- declare function isTypeDefinition(node: DependencyNode): boolean;
234
- /**
235
- * Detect if a file is a utility module
236
- *
237
- * @param node The dependency node to check
238
- * @returns True if the file appears to be a utility module
239
- */
240
- declare function isUtilityModule(node: DependencyNode): boolean;
241
- /**
242
- * Detect if a file is a Lambda/API handler
243
- *
244
- * @param node The dependency node to check
245
- * @returns True if the file appears to be a Lambda handler
246
- */
247
- declare function isLambdaHandler(node: DependencyNode): boolean;
248
- /**
249
- * Detect if a file is a service file
250
- *
251
- * @param node The dependency node to check
252
- * @returns True if the file appears to be a service file
253
- */
254
- declare function isServiceFile(node: DependencyNode): boolean;
255
- /**
256
- * Detect if a file is an email template/layout
257
- *
258
- * @param node The dependency node to check
259
- * @returns True if the file appears to be an email template
260
- */
261
- declare function isEmailTemplate(node: DependencyNode): boolean;
262
- /**
263
- * Detect if a file is a parser/transformer
264
- *
265
- * @param node The dependency node to check
266
- * @returns True if the file appears to be a parser
267
- */
268
- declare function isParserFile(node: DependencyNode): boolean;
269
- /**
270
- * Detect if a file is a session/state management file
271
- *
272
- * @param node The dependency node to check
273
- * @returns True if the file appears to be a session/state file
274
- */
275
- declare function isSessionFile(node: DependencyNode): boolean;
276
- /**
277
- * Detect if a file is a configuration or schema file
278
- *
279
- * @param node The dependency node to check
280
- * @returns True if the file appears to be a config file
281
- */
282
- declare function isConfigFile(node: DependencyNode): boolean;
283
- /**
284
- * Detect if a file is a Next.js App Router page
285
- *
286
- * @param node The dependency node to check
287
- * @returns True if the file appears to be a Next.js page
288
- */
289
- declare function isNextJsPage(node: DependencyNode): boolean;
290
280
  /**
291
281
  * Adjust cohesion score based on file classification
292
282
  *
@@ -307,6 +297,10 @@ declare function adjustFragmentationForClassification(baseFragmentation: number,
307
297
 
308
298
  /**
309
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
310
304
  */
311
305
  declare function detectModuleClusters(graph: DependencyGraph, options?: {
312
306
  useLogScale?: boolean;
@@ -336,36 +330,6 @@ declare function getGeneralRecommendations(metrics: {
336
330
  severity: any;
337
331
  };
338
332
 
339
- /**
340
- * Calculate cohesion score (how related are exports in a file)
341
- * Legacy wrapper for backward compatibility with exact test expectations
342
- */
343
- declare function calculateCohesion(exports: ExportInfo[], filePath?: string, options?: any): number;
344
- /**
345
- * Analyze issues for a single file
346
- */
347
- declare function analyzeIssues(params: {
348
- file: string;
349
- importDepth: number;
350
- contextBudget: number;
351
- cohesionScore: number;
352
- fragmentationScore: number;
353
- maxDepth: number;
354
- maxContextBudget: number;
355
- minCohesion: number;
356
- maxFragmentation: number;
357
- circularDeps: string[][];
358
- }): {
359
- severity: Severity;
360
- issues: string[];
361
- recommendations: string[];
362
- potentialSavings: number;
363
- };
364
- /**
365
- * Analyze AI context window cost for a codebase
366
- */
367
- declare function analyzeContext(options: ContextAnalyzerOptions): Promise<ContextAnalysisResult[]>;
368
-
369
333
  /**
370
334
  * Calculate AI Readiness Score for context efficiency (0-100)
371
335
  */
@@ -375,6 +339,9 @@ declare function mapScoreToRating(score: number): string;
375
339
  /**
376
340
  * Generate smart defaults for context analysis based on repository size
377
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
378
345
  */
379
346
  declare function getSmartDefaults(directory: string, userOptions: Partial<ContextAnalyzerOptions>): Promise<ContextAnalyzerOptions>;
380
347
 
@@ -385,14 +352,21 @@ declare function generateSummary(results: ContextAnalysisResult[], options?: any
385
352
 
386
353
  /**
387
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
388
357
  */
389
358
  declare function buildCoUsageMatrix(graph: DependencyGraph): Map<string, Map<string, number>>;
390
359
  /**
391
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
392
363
  */
393
364
  declare function buildTypeGraph(graph: DependencyGraph): Map<string, Set<string>>;
394
365
  /**
395
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
396
370
  */
397
371
  declare function findSemanticClusters(coUsageMatrix: Map<string, Map<string, number>>, minCoUsage?: number): Map<string, string[]>;
398
372
  /**
@@ -415,4 +389,17 @@ declare function inferDomain(name: string, filePath?: string, domainOptions?: {
415
389
  declare function getCoUsageData(file: string, coUsageMatrix: Map<string, Map<string, number>>): CoUsageData;
416
390
  declare function findConsolidationCandidates(graph: DependencyGraph, coUsageMatrix: Map<string, Map<string, number>>, typeGraph: Map<string, Set<string>>, minCoUsage?: number, minSharedTypes?: number): any[];
417
391
 
418
- 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, analyzeIssues, buildCoUsageMatrix, buildDependencyGraph, buildTypeGraph, calculateCohesion, calculateContextBudget, calculateContextScore, calculateDirectoryDistance, calculateDomainConfidence, calculateEnhancedCohesion, calculateFragmentation, calculateImportDepth, calculatePathEntropy, calculateStructuralCohesionFromCoUsage, classifyFile, detectCircularDependencies, detectModuleClusters, extractDomainKeywordsFromPaths, extractExports, findConsolidationCandidates, findSemanticClusters, generateSummary, getClassificationRecommendations, getCoUsageData, getGeneralRecommendations, getSmartDefaults, getTransitiveDependencies, inferDomain, inferDomainFromSemantics, isBarrelExport, isConfigFile, isEmailTemplate, isLambdaHandler, isNextJsPage, isParserFile, isServiceFile, isSessionFile, isTypeDefinition, isUtilityModule, mapScoreToRating };
392
+ /**
393
+ * Display analysis report in console
394
+ */
395
+ declare function displayConsoleReport(summary: ReturnType<typeof generateSummary>, results: Awaited<ReturnType<typeof analyzeContext>>, maxResults?: number): void;
396
+ /**
397
+ * Generate HTML report
398
+ */
399
+ declare function generateHTMLReport(summary: ReturnType<typeof generateSummary>, results: Awaited<ReturnType<typeof analyzeContext>>): string;
400
+ /**
401
+ * Interactive setup: detect common frameworks and suggest excludes & focus areas
402
+ */
403
+ declare function runInteractiveSetup(directory: string, current: any): Promise<any>;
404
+
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 };
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
  /**
@@ -128,10 +160,29 @@ interface TypeDependency {
128
160
  usedBy: string[];
129
161
  }
130
162
 
131
- interface FileContent {
132
- file: string;
133
- content: string;
134
- }
163
+ /**
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
171
+ */
172
+ declare function calculateCohesion(exports: ExportInfo[], filePath?: string, options?: any): number;
173
+ /**
174
+ * Analyze AI context window cost for a codebase
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
+ */
184
+ declare function analyzeContext(options: ContextAnalyzerOptions): Promise<ContextAnalysisResult[]>;
185
+
135
186
  /**
136
187
  * Auto-detect domain keywords from workspace folder structure
137
188
  */
@@ -162,6 +213,15 @@ declare function detectCircularDependencies(graph: DependencyGraph): string[][];
162
213
  /**
163
214
  * Calculate cohesion score (how related are exports in a file)
164
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
+ */
165
225
  declare function calculateEnhancedCohesion(exports: ExportInfo[], filePath?: string, options?: {
166
226
  coUsageMatrix?: Map<string, Map<string, number>>;
167
227
  weights?: {
@@ -217,76 +277,6 @@ declare const Classification: {
217
277
  * @returns The determined file classification
218
278
  */
219
279
  declare function classifyFile(node: DependencyNode, cohesionScore?: number, domains?: string[]): FileClassification;
220
- /**
221
- * Detect if a file is a barrel export (index.ts)
222
- *
223
- * @param node The dependency node to check
224
- * @returns True if the file appears to be a barrel export
225
- */
226
- declare function isBarrelExport(node: DependencyNode): boolean;
227
- /**
228
- * Detect if a file is primarily type definitions
229
- *
230
- * @param node The dependency node to check
231
- * @returns True if the file appears to be primarily types
232
- */
233
- declare function isTypeDefinition(node: DependencyNode): boolean;
234
- /**
235
- * Detect if a file is a utility module
236
- *
237
- * @param node The dependency node to check
238
- * @returns True if the file appears to be a utility module
239
- */
240
- declare function isUtilityModule(node: DependencyNode): boolean;
241
- /**
242
- * Detect if a file is a Lambda/API handler
243
- *
244
- * @param node The dependency node to check
245
- * @returns True if the file appears to be a Lambda handler
246
- */
247
- declare function isLambdaHandler(node: DependencyNode): boolean;
248
- /**
249
- * Detect if a file is a service file
250
- *
251
- * @param node The dependency node to check
252
- * @returns True if the file appears to be a service file
253
- */
254
- declare function isServiceFile(node: DependencyNode): boolean;
255
- /**
256
- * Detect if a file is an email template/layout
257
- *
258
- * @param node The dependency node to check
259
- * @returns True if the file appears to be an email template
260
- */
261
- declare function isEmailTemplate(node: DependencyNode): boolean;
262
- /**
263
- * Detect if a file is a parser/transformer
264
- *
265
- * @param node The dependency node to check
266
- * @returns True if the file appears to be a parser
267
- */
268
- declare function isParserFile(node: DependencyNode): boolean;
269
- /**
270
- * Detect if a file is a session/state management file
271
- *
272
- * @param node The dependency node to check
273
- * @returns True if the file appears to be a session/state file
274
- */
275
- declare function isSessionFile(node: DependencyNode): boolean;
276
- /**
277
- * Detect if a file is a configuration or schema file
278
- *
279
- * @param node The dependency node to check
280
- * @returns True if the file appears to be a config file
281
- */
282
- declare function isConfigFile(node: DependencyNode): boolean;
283
- /**
284
- * Detect if a file is a Next.js App Router page
285
- *
286
- * @param node The dependency node to check
287
- * @returns True if the file appears to be a Next.js page
288
- */
289
- declare function isNextJsPage(node: DependencyNode): boolean;
290
280
  /**
291
281
  * Adjust cohesion score based on file classification
292
282
  *
@@ -307,6 +297,10 @@ declare function adjustFragmentationForClassification(baseFragmentation: number,
307
297
 
308
298
  /**
309
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
310
304
  */
311
305
  declare function detectModuleClusters(graph: DependencyGraph, options?: {
312
306
  useLogScale?: boolean;
@@ -336,36 +330,6 @@ declare function getGeneralRecommendations(metrics: {
336
330
  severity: any;
337
331
  };
338
332
 
339
- /**
340
- * Calculate cohesion score (how related are exports in a file)
341
- * Legacy wrapper for backward compatibility with exact test expectations
342
- */
343
- declare function calculateCohesion(exports: ExportInfo[], filePath?: string, options?: any): number;
344
- /**
345
- * Analyze issues for a single file
346
- */
347
- declare function analyzeIssues(params: {
348
- file: string;
349
- importDepth: number;
350
- contextBudget: number;
351
- cohesionScore: number;
352
- fragmentationScore: number;
353
- maxDepth: number;
354
- maxContextBudget: number;
355
- minCohesion: number;
356
- maxFragmentation: number;
357
- circularDeps: string[][];
358
- }): {
359
- severity: Severity;
360
- issues: string[];
361
- recommendations: string[];
362
- potentialSavings: number;
363
- };
364
- /**
365
- * Analyze AI context window cost for a codebase
366
- */
367
- declare function analyzeContext(options: ContextAnalyzerOptions): Promise<ContextAnalysisResult[]>;
368
-
369
333
  /**
370
334
  * Calculate AI Readiness Score for context efficiency (0-100)
371
335
  */
@@ -375,6 +339,9 @@ declare function mapScoreToRating(score: number): string;
375
339
  /**
376
340
  * Generate smart defaults for context analysis based on repository size
377
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
378
345
  */
379
346
  declare function getSmartDefaults(directory: string, userOptions: Partial<ContextAnalyzerOptions>): Promise<ContextAnalyzerOptions>;
380
347
 
@@ -385,14 +352,21 @@ declare function generateSummary(results: ContextAnalysisResult[], options?: any
385
352
 
386
353
  /**
387
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
388
357
  */
389
358
  declare function buildCoUsageMatrix(graph: DependencyGraph): Map<string, Map<string, number>>;
390
359
  /**
391
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
392
363
  */
393
364
  declare function buildTypeGraph(graph: DependencyGraph): Map<string, Set<string>>;
394
365
  /**
395
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
396
370
  */
397
371
  declare function findSemanticClusters(coUsageMatrix: Map<string, Map<string, number>>, minCoUsage?: number): Map<string, string[]>;
398
372
  /**
@@ -415,4 +389,17 @@ declare function inferDomain(name: string, filePath?: string, domainOptions?: {
415
389
  declare function getCoUsageData(file: string, coUsageMatrix: Map<string, Map<string, number>>): CoUsageData;
416
390
  declare function findConsolidationCandidates(graph: DependencyGraph, coUsageMatrix: Map<string, Map<string, number>>, typeGraph: Map<string, Set<string>>, minCoUsage?: number, minSharedTypes?: number): any[];
417
391
 
418
- 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, analyzeIssues, buildCoUsageMatrix, buildDependencyGraph, buildTypeGraph, calculateCohesion, calculateContextBudget, calculateContextScore, calculateDirectoryDistance, calculateDomainConfidence, calculateEnhancedCohesion, calculateFragmentation, calculateImportDepth, calculatePathEntropy, calculateStructuralCohesionFromCoUsage, classifyFile, detectCircularDependencies, detectModuleClusters, extractDomainKeywordsFromPaths, extractExports, findConsolidationCandidates, findSemanticClusters, generateSummary, getClassificationRecommendations, getCoUsageData, getGeneralRecommendations, getSmartDefaults, getTransitiveDependencies, inferDomain, inferDomainFromSemantics, isBarrelExport, isConfigFile, isEmailTemplate, isLambdaHandler, isNextJsPage, isParserFile, isServiceFile, isSessionFile, isTypeDefinition, isUtilityModule, mapScoreToRating };
392
+ /**
393
+ * Display analysis report in console
394
+ */
395
+ declare function displayConsoleReport(summary: ReturnType<typeof generateSummary>, results: Awaited<ReturnType<typeof analyzeContext>>, maxResults?: number): void;
396
+ /**
397
+ * Generate HTML report
398
+ */
399
+ declare function generateHTMLReport(summary: ReturnType<typeof generateSummary>, results: Awaited<ReturnType<typeof analyzeContext>>): string;
400
+ /**
401
+ * Interactive setup: detect common frameworks and suggest excludes & focus areas
402
+ */
403
+ declare function runInteractiveSetup(directory: string, current: any): Promise<any>;
404
+
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 };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,KAAK,EACV,sBAAsB,EACtB,qBAAqB,EACrB,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,WAAW,EACX,cAAc,EACf,MAAM,SAAS,CAAC;AACjB,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,oBAAoB,EACpB,yBAAyB,EACzB,wBAAwB,EACxB,cAAc,EACd,2BAA2B,EAC5B,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EACV,sBAAsB,EACtB,qBAAqB,EACrB,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,WAAW,EACX,cAAc,GACf,CAAC;AAEF,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,oBAAoB,EACpB,yBAAyB,EACzB,wBAAwB,EACxB,cAAc,EACd,2BAA2B,GAC5B,CAAC;AAEF;;;GAGG;AACH,iBAAe,gBAAgB,CAC7B,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,OAAO,CAAC,sBAAsB,CAAC,GAC3C,OAAO,CAAC,sBAAsB,CAAC,CAuDjC;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,qBAAqB,EAAE,CAAC,CA8LlC;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,qBAAqB,EAAE,GAC/B,cAAc,CAgKhB;AAkHD,OAAO,EAAE,gBAAgB,EAAE,CAAC;AAgC5B,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAKrD,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,qBAAqB,CAAC;AACpC,OAAO,EAAE,uBAAuB,EAAE,CAAC;AACnC,cAAc,0BAA0B,CAAC"}