@aiready/context-analyzer 0.6.0 → 0.7.1

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.
@@ -0,0 +1,997 @@
1
+ // src/index.ts
2
+ import { scanFiles, readFileContent } from "@aiready/core";
3
+
4
+ // src/analyzer.ts
5
+ import { estimateTokens, parseFileExports } from "@aiready/core";
6
+
7
+ // src/semantic-analysis.ts
8
+ function buildCoUsageMatrix(graph) {
9
+ const coUsageMatrix = /* @__PURE__ */ new Map();
10
+ for (const [sourceFile, node] of graph.nodes) {
11
+ const imports = node.imports;
12
+ for (let i = 0; i < imports.length; i++) {
13
+ const fileA = imports[i];
14
+ if (!coUsageMatrix.has(fileA)) {
15
+ coUsageMatrix.set(fileA, /* @__PURE__ */ new Map());
16
+ }
17
+ for (let j = i + 1; j < imports.length; j++) {
18
+ const fileB = imports[j];
19
+ const fileAUsage = coUsageMatrix.get(fileA);
20
+ fileAUsage.set(fileB, (fileAUsage.get(fileB) || 0) + 1);
21
+ if (!coUsageMatrix.has(fileB)) {
22
+ coUsageMatrix.set(fileB, /* @__PURE__ */ new Map());
23
+ }
24
+ const fileBUsage = coUsageMatrix.get(fileB);
25
+ fileBUsage.set(fileA, (fileBUsage.get(fileA) || 0) + 1);
26
+ }
27
+ }
28
+ }
29
+ return coUsageMatrix;
30
+ }
31
+ function buildTypeGraph(graph) {
32
+ const typeGraph = /* @__PURE__ */ new Map();
33
+ for (const [file, node] of graph.nodes) {
34
+ for (const exp of node.exports) {
35
+ if (exp.typeReferences) {
36
+ for (const typeRef of exp.typeReferences) {
37
+ if (!typeGraph.has(typeRef)) {
38
+ typeGraph.set(typeRef, /* @__PURE__ */ new Set());
39
+ }
40
+ typeGraph.get(typeRef).add(file);
41
+ }
42
+ }
43
+ }
44
+ }
45
+ return typeGraph;
46
+ }
47
+ function findSemanticClusters(coUsageMatrix, minCoUsage = 3) {
48
+ const clusters = /* @__PURE__ */ new Map();
49
+ const visited = /* @__PURE__ */ new Set();
50
+ for (const [file, coUsages] of coUsageMatrix) {
51
+ if (visited.has(file)) continue;
52
+ const cluster = [file];
53
+ visited.add(file);
54
+ for (const [relatedFile, count] of coUsages) {
55
+ if (count >= minCoUsage && !visited.has(relatedFile)) {
56
+ cluster.push(relatedFile);
57
+ visited.add(relatedFile);
58
+ }
59
+ }
60
+ if (cluster.length > 1) {
61
+ clusters.set(file, cluster);
62
+ }
63
+ }
64
+ return clusters;
65
+ }
66
+ function calculateDomainConfidence(signals) {
67
+ const weights = {
68
+ coUsage: 0.35,
69
+ // Strongest signal: actual usage patterns
70
+ typeReference: 0.3,
71
+ // Strong signal: shared types
72
+ exportName: 0.15,
73
+ // Medium signal: identifier semantics
74
+ importPath: 0.1,
75
+ // Weaker signal: path structure
76
+ folderStructure: 0.1
77
+ // Weakest signal: organization convention
78
+ };
79
+ let confidence = 0;
80
+ if (signals.coUsage) confidence += weights.coUsage;
81
+ if (signals.typeReference) confidence += weights.typeReference;
82
+ if (signals.exportName) confidence += weights.exportName;
83
+ if (signals.importPath) confidence += weights.importPath;
84
+ if (signals.folderStructure) confidence += weights.folderStructure;
85
+ return confidence;
86
+ }
87
+ function inferDomainFromSemantics(file, exportName, graph, coUsageMatrix, typeGraph, exportTypeRefs) {
88
+ const assignments = [];
89
+ const domainSignals = /* @__PURE__ */ new Map();
90
+ const coUsages = coUsageMatrix.get(file) || /* @__PURE__ */ new Map();
91
+ const strongCoUsages = Array.from(coUsages.entries()).filter(([_, count]) => count >= 3).map(([coFile]) => coFile);
92
+ for (const coFile of strongCoUsages) {
93
+ const coNode = graph.nodes.get(coFile);
94
+ if (coNode) {
95
+ for (const exp of coNode.exports) {
96
+ if (exp.inferredDomain && exp.inferredDomain !== "unknown") {
97
+ const domain = exp.inferredDomain;
98
+ if (!domainSignals.has(domain)) {
99
+ domainSignals.set(domain, {
100
+ coUsage: false,
101
+ typeReference: false,
102
+ exportName: false,
103
+ importPath: false,
104
+ folderStructure: false
105
+ });
106
+ }
107
+ domainSignals.get(domain).coUsage = true;
108
+ }
109
+ }
110
+ }
111
+ }
112
+ if (exportTypeRefs) {
113
+ for (const typeRef of exportTypeRefs) {
114
+ const filesWithType = typeGraph.get(typeRef);
115
+ if (filesWithType) {
116
+ for (const typeFile of filesWithType) {
117
+ if (typeFile !== file) {
118
+ const typeNode = graph.nodes.get(typeFile);
119
+ if (typeNode) {
120
+ for (const exp of typeNode.exports) {
121
+ if (exp.inferredDomain && exp.inferredDomain !== "unknown") {
122
+ const domain = exp.inferredDomain;
123
+ if (!domainSignals.has(domain)) {
124
+ domainSignals.set(domain, {
125
+ coUsage: false,
126
+ typeReference: false,
127
+ exportName: false,
128
+ importPath: false,
129
+ folderStructure: false
130
+ });
131
+ }
132
+ domainSignals.get(domain).typeReference = true;
133
+ }
134
+ }
135
+ }
136
+ }
137
+ }
138
+ }
139
+ }
140
+ }
141
+ for (const [domain, signals] of domainSignals) {
142
+ const confidence = calculateDomainConfidence(signals);
143
+ if (confidence >= 0.3) {
144
+ assignments.push({ domain, confidence, signals });
145
+ }
146
+ }
147
+ assignments.sort((a, b) => b.confidence - a.confidence);
148
+ return assignments;
149
+ }
150
+ function getCoUsageData(file, coUsageMatrix) {
151
+ const coImportedWith = coUsageMatrix.get(file) || /* @__PURE__ */ new Map();
152
+ const sharedImporters = [];
153
+ return {
154
+ file,
155
+ coImportedWith,
156
+ sharedImporters
157
+ };
158
+ }
159
+ function findConsolidationCandidates(graph, coUsageMatrix, typeGraph, minCoUsage = 5, minSharedTypes = 2) {
160
+ const candidates = [];
161
+ for (const [fileA, coUsages] of coUsageMatrix) {
162
+ const nodeA = graph.nodes.get(fileA);
163
+ if (!nodeA) continue;
164
+ for (const [fileB, coUsageCount] of coUsages) {
165
+ if (fileB <= fileA) continue;
166
+ if (coUsageCount < minCoUsage) continue;
167
+ const nodeB = graph.nodes.get(fileB);
168
+ if (!nodeB) continue;
169
+ const typesA = new Set(nodeA.exports.flatMap((e) => e.typeReferences || []));
170
+ const typesB = new Set(nodeB.exports.flatMap((e) => e.typeReferences || []));
171
+ const sharedTypes = Array.from(typesA).filter((t) => typesB.has(t));
172
+ if (sharedTypes.length >= minSharedTypes) {
173
+ const strength = coUsageCount / 10 + sharedTypes.length / 5;
174
+ candidates.push({
175
+ files: [fileA, fileB],
176
+ reason: `High co-usage (${coUsageCount}x) and ${sharedTypes.length} shared types`,
177
+ strength
178
+ });
179
+ } else if (coUsageCount >= minCoUsage * 2) {
180
+ const strength = coUsageCount / 10;
181
+ candidates.push({
182
+ files: [fileA, fileB],
183
+ reason: `Very high co-usage (${coUsageCount}x)`,
184
+ strength
185
+ });
186
+ }
187
+ }
188
+ }
189
+ candidates.sort((a, b) => b.strength - a.strength);
190
+ return candidates;
191
+ }
192
+
193
+ // src/analyzer.ts
194
+ function extractDomainKeywordsFromPaths(files) {
195
+ const folderNames = /* @__PURE__ */ new Set();
196
+ for (const { file } of files) {
197
+ const segments = file.split("/");
198
+ const skipFolders = /* @__PURE__ */ new Set(["src", "lib", "dist", "build", "node_modules", "test", "tests", "__tests__", "spec", "e2e", "scripts", "components", "utils", "helpers", "util", "helper", "api", "apis"]);
199
+ for (const segment of segments) {
200
+ const normalized = segment.toLowerCase();
201
+ if (normalized && !skipFolders.has(normalized) && !normalized.includes(".")) {
202
+ const singular = singularize(normalized);
203
+ folderNames.add(singular);
204
+ }
205
+ }
206
+ }
207
+ return Array.from(folderNames);
208
+ }
209
+ function singularize(word) {
210
+ const irregulars = {
211
+ people: "person",
212
+ children: "child",
213
+ men: "man",
214
+ women: "woman"
215
+ };
216
+ if (irregulars[word]) {
217
+ return irregulars[word];
218
+ }
219
+ if (word.endsWith("ies")) {
220
+ return word.slice(0, -3) + "y";
221
+ }
222
+ if (word.endsWith("ses")) {
223
+ return word.slice(0, -2);
224
+ }
225
+ if (word.endsWith("s") && word.length > 3) {
226
+ return word.slice(0, -1);
227
+ }
228
+ return word;
229
+ }
230
+ function buildDependencyGraph(files, domainOptions) {
231
+ const nodes = /* @__PURE__ */ new Map();
232
+ const edges = /* @__PURE__ */ new Map();
233
+ const autoDetectedKeywords = extractDomainKeywordsFromPaths(files);
234
+ const enhancedOptions = {
235
+ ...domainOptions,
236
+ domainKeywords: [
237
+ ...domainOptions?.domainKeywords || [],
238
+ ...autoDetectedKeywords
239
+ ]
240
+ };
241
+ for (const { file, content } of files) {
242
+ const imports = extractImportsFromContent(content);
243
+ const exports = extractExportsWithAST(content, file, enhancedOptions, imports);
244
+ const tokenCost = estimateTokens(content);
245
+ const linesOfCode = content.split("\n").length;
246
+ nodes.set(file, {
247
+ file,
248
+ imports,
249
+ exports,
250
+ tokenCost,
251
+ linesOfCode
252
+ });
253
+ edges.set(file, new Set(imports));
254
+ }
255
+ const graph = { nodes, edges };
256
+ const coUsageMatrix = buildCoUsageMatrix(graph);
257
+ const typeGraph = buildTypeGraph(graph);
258
+ graph.coUsageMatrix = coUsageMatrix;
259
+ graph.typeGraph = typeGraph;
260
+ for (const [file, node] of nodes) {
261
+ for (const exp of node.exports) {
262
+ const semanticAssignments = inferDomainFromSemantics(
263
+ file,
264
+ exp.name,
265
+ graph,
266
+ coUsageMatrix,
267
+ typeGraph,
268
+ exp.typeReferences
269
+ );
270
+ exp.domains = semanticAssignments;
271
+ if (semanticAssignments.length > 0) {
272
+ exp.inferredDomain = semanticAssignments[0].domain;
273
+ }
274
+ }
275
+ }
276
+ return graph;
277
+ }
278
+ function extractImportsFromContent(content) {
279
+ const imports = [];
280
+ const patterns = [
281
+ /import\s+.*?\s+from\s+['"](.+?)['"]/g,
282
+ // import ... from '...'
283
+ /import\s+['"](.+?)['"]/g,
284
+ // import '...'
285
+ /require\(['"](.+?)['"]\)/g
286
+ // require('...')
287
+ ];
288
+ for (const pattern of patterns) {
289
+ let match;
290
+ while ((match = pattern.exec(content)) !== null) {
291
+ const importPath = match[1];
292
+ if (importPath && !importPath.startsWith("node:")) {
293
+ imports.push(importPath);
294
+ }
295
+ }
296
+ }
297
+ return [...new Set(imports)];
298
+ }
299
+ function calculateImportDepth(file, graph, visited = /* @__PURE__ */ new Set(), depth = 0) {
300
+ if (visited.has(file)) {
301
+ return depth;
302
+ }
303
+ const dependencies = graph.edges.get(file);
304
+ if (!dependencies || dependencies.size === 0) {
305
+ return depth;
306
+ }
307
+ visited.add(file);
308
+ let maxDepth = depth;
309
+ for (const dep of dependencies) {
310
+ const depDepth = calculateImportDepth(dep, graph, visited, depth + 1);
311
+ maxDepth = Math.max(maxDepth, depDepth);
312
+ }
313
+ visited.delete(file);
314
+ return maxDepth;
315
+ }
316
+ function getTransitiveDependencies(file, graph, visited = /* @__PURE__ */ new Set()) {
317
+ if (visited.has(file)) {
318
+ return [];
319
+ }
320
+ visited.add(file);
321
+ const dependencies = graph.edges.get(file);
322
+ if (!dependencies || dependencies.size === 0) {
323
+ return [];
324
+ }
325
+ const allDeps = [];
326
+ for (const dep of dependencies) {
327
+ allDeps.push(dep);
328
+ allDeps.push(...getTransitiveDependencies(dep, graph, visited));
329
+ }
330
+ return [...new Set(allDeps)];
331
+ }
332
+ function calculateContextBudget(file, graph) {
333
+ const node = graph.nodes.get(file);
334
+ if (!node) return 0;
335
+ let totalTokens = node.tokenCost;
336
+ const deps = getTransitiveDependencies(file, graph);
337
+ for (const dep of deps) {
338
+ const depNode = graph.nodes.get(dep);
339
+ if (depNode) {
340
+ totalTokens += depNode.tokenCost;
341
+ }
342
+ }
343
+ return totalTokens;
344
+ }
345
+ function detectCircularDependencies(graph) {
346
+ const cycles = [];
347
+ const visited = /* @__PURE__ */ new Set();
348
+ const recursionStack = /* @__PURE__ */ new Set();
349
+ function dfs(file, path) {
350
+ if (recursionStack.has(file)) {
351
+ const cycleStart = path.indexOf(file);
352
+ if (cycleStart !== -1) {
353
+ cycles.push([...path.slice(cycleStart), file]);
354
+ }
355
+ return;
356
+ }
357
+ if (visited.has(file)) {
358
+ return;
359
+ }
360
+ visited.add(file);
361
+ recursionStack.add(file);
362
+ path.push(file);
363
+ const dependencies = graph.edges.get(file);
364
+ if (dependencies) {
365
+ for (const dep of dependencies) {
366
+ dfs(dep, [...path]);
367
+ }
368
+ }
369
+ recursionStack.delete(file);
370
+ }
371
+ for (const file of graph.nodes.keys()) {
372
+ if (!visited.has(file)) {
373
+ dfs(file, []);
374
+ }
375
+ }
376
+ return cycles;
377
+ }
378
+ function calculateCohesion(exports, filePath) {
379
+ return calculateEnhancedCohesion(exports, filePath);
380
+ }
381
+ function isTestFile(filePath) {
382
+ const lower = filePath.toLowerCase();
383
+ return lower.includes("test") || lower.includes("spec") || lower.includes("mock") || lower.includes("fixture") || lower.includes("__tests__") || lower.includes(".test.") || lower.includes(".spec.");
384
+ }
385
+ function calculateFragmentation(files, domain) {
386
+ if (files.length <= 1) return 0;
387
+ const directories = new Set(files.map((f) => f.split("/").slice(0, -1).join("/")));
388
+ return (directories.size - 1) / (files.length - 1);
389
+ }
390
+ function detectModuleClusters(graph) {
391
+ const domainMap = /* @__PURE__ */ new Map();
392
+ for (const [file, node] of graph.nodes.entries()) {
393
+ const domains = node.exports.map((e) => e.inferredDomain || "unknown");
394
+ const primaryDomain = domains[0] || "unknown";
395
+ if (!domainMap.has(primaryDomain)) {
396
+ domainMap.set(primaryDomain, []);
397
+ }
398
+ domainMap.get(primaryDomain).push(file);
399
+ }
400
+ const clusters = [];
401
+ for (const [domain, files] of domainMap.entries()) {
402
+ if (files.length < 2) continue;
403
+ const totalTokens = files.reduce((sum, file) => {
404
+ const node = graph.nodes.get(file);
405
+ return sum + (node?.tokenCost || 0);
406
+ }, 0);
407
+ const fragmentationScore = calculateFragmentation(files, domain);
408
+ const avgCohesion = files.reduce((sum, file) => {
409
+ const node = graph.nodes.get(file);
410
+ return sum + (node ? calculateCohesion(node.exports, file) : 0);
411
+ }, 0) / files.length;
412
+ const targetFiles = Math.max(1, Math.ceil(files.length / 3));
413
+ const consolidationPlan = generateConsolidationPlan(
414
+ domain,
415
+ files,
416
+ targetFiles
417
+ );
418
+ clusters.push({
419
+ domain,
420
+ files,
421
+ totalTokens,
422
+ fragmentationScore,
423
+ avgCohesion,
424
+ suggestedStructure: {
425
+ targetFiles,
426
+ consolidationPlan
427
+ }
428
+ });
429
+ }
430
+ return clusters.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
431
+ }
432
+ function extractExports(content, filePath, domainOptions, fileImports) {
433
+ const exports = [];
434
+ const patterns = [
435
+ /export\s+function\s+(\w+)/g,
436
+ /export\s+class\s+(\w+)/g,
437
+ /export\s+const\s+(\w+)/g,
438
+ /export\s+type\s+(\w+)/g,
439
+ /export\s+interface\s+(\w+)/g,
440
+ /export\s+default/g
441
+ ];
442
+ const types = [
443
+ "function",
444
+ "class",
445
+ "const",
446
+ "type",
447
+ "interface",
448
+ "default"
449
+ ];
450
+ patterns.forEach((pattern, index) => {
451
+ let match;
452
+ while ((match = pattern.exec(content)) !== null) {
453
+ const name = match[1] || "default";
454
+ const type = types[index];
455
+ const inferredDomain = inferDomain(name, filePath, domainOptions, fileImports);
456
+ exports.push({ name, type, inferredDomain });
457
+ }
458
+ });
459
+ return exports;
460
+ }
461
+ function inferDomain(name, filePath, domainOptions, fileImports) {
462
+ const lower = name.toLowerCase();
463
+ const tokens = Array.from(
464
+ new Set(
465
+ lower.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[^a-z0-9]+/gi, " ").split(" ").filter(Boolean)
466
+ )
467
+ );
468
+ const defaultKeywords = [
469
+ "authentication",
470
+ "authorization",
471
+ "payment",
472
+ "invoice",
473
+ "customer",
474
+ "product",
475
+ "order",
476
+ "cart",
477
+ "user",
478
+ "admin",
479
+ "repository",
480
+ "controller",
481
+ "service",
482
+ "config",
483
+ "model",
484
+ "view",
485
+ "auth"
486
+ ];
487
+ const domainKeywords = domainOptions?.domainKeywords && domainOptions.domainKeywords.length ? [...domainOptions.domainKeywords, ...defaultKeywords] : defaultKeywords;
488
+ const patterns = (domainOptions?.domainPatterns || []).map((p) => {
489
+ try {
490
+ return new RegExp(p, "i");
491
+ } catch {
492
+ return null;
493
+ }
494
+ }).filter((r) => !!r);
495
+ for (const re of patterns) {
496
+ if (re.test(lower)) {
497
+ const matched = domainKeywords.find((k) => re.test(k));
498
+ if (matched) return matched;
499
+ return "unknown";
500
+ }
501
+ }
502
+ for (const keyword of domainKeywords) {
503
+ if (tokens.includes(keyword)) {
504
+ return keyword;
505
+ }
506
+ }
507
+ for (const keyword of domainKeywords) {
508
+ if (lower.includes(keyword)) {
509
+ return keyword;
510
+ }
511
+ }
512
+ if (fileImports && fileImports.length > 0) {
513
+ for (const importPath of fileImports) {
514
+ const allSegments = importPath.split("/");
515
+ const relevantSegments = allSegments.filter((s) => {
516
+ if (!s) return false;
517
+ if (s === "." || s === "..") return false;
518
+ if (s.startsWith("@") && s.length === 1) return false;
519
+ return true;
520
+ }).map((s) => s.startsWith("@") ? s.slice(1) : s);
521
+ for (const segment of relevantSegments) {
522
+ const segLower = segment.toLowerCase();
523
+ const singularSegment = singularize(segLower);
524
+ for (const keyword of domainKeywords) {
525
+ if (singularSegment === keyword || segLower === keyword || segLower.includes(keyword)) {
526
+ return keyword;
527
+ }
528
+ }
529
+ }
530
+ }
531
+ }
532
+ if (filePath) {
533
+ if (domainOptions?.pathDomainMap) {
534
+ const segments = filePath.toLowerCase().split("/");
535
+ for (const seg of segments) {
536
+ if (domainOptions.pathDomainMap[seg]) {
537
+ return domainOptions.pathDomainMap[seg];
538
+ }
539
+ }
540
+ }
541
+ const pathSegments = filePath.toLowerCase().split("/");
542
+ for (const segment of pathSegments) {
543
+ const singularSegment = singularize(segment);
544
+ for (const keyword of domainKeywords) {
545
+ if (singularSegment === keyword || segment === keyword || segment.includes(keyword)) {
546
+ return keyword;
547
+ }
548
+ }
549
+ }
550
+ }
551
+ return "unknown";
552
+ }
553
+ function generateConsolidationPlan(domain, files, targetFiles) {
554
+ const plan = [];
555
+ if (files.length <= targetFiles) {
556
+ return [`No consolidation needed for ${domain}`];
557
+ }
558
+ plan.push(
559
+ `Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s):`
560
+ );
561
+ const dirGroups = /* @__PURE__ */ new Map();
562
+ for (const file of files) {
563
+ const dir = file.split("/").slice(0, -1).join("/");
564
+ if (!dirGroups.has(dir)) {
565
+ dirGroups.set(dir, []);
566
+ }
567
+ dirGroups.get(dir).push(file);
568
+ }
569
+ plan.push(`1. Create unified ${domain} module file`);
570
+ plan.push(
571
+ `2. Move related functionality from ${files.length} scattered files`
572
+ );
573
+ plan.push(`3. Update imports in dependent files`);
574
+ plan.push(
575
+ `4. Remove old files after consolidation (verify with tests first)`
576
+ );
577
+ return plan;
578
+ }
579
+ function extractExportsWithAST(content, filePath, domainOptions, fileImports) {
580
+ try {
581
+ const { exports: astExports } = parseFileExports(content, filePath);
582
+ return astExports.map((exp) => ({
583
+ name: exp.name,
584
+ type: exp.type,
585
+ inferredDomain: inferDomain(exp.name, filePath, domainOptions, fileImports),
586
+ imports: exp.imports,
587
+ dependencies: exp.dependencies
588
+ }));
589
+ } catch (error) {
590
+ return extractExports(content, filePath, domainOptions, fileImports);
591
+ }
592
+ }
593
+ function calculateEnhancedCohesion(exports, filePath) {
594
+ if (exports.length === 0) return 1;
595
+ if (exports.length === 1) return 1;
596
+ if (filePath && isTestFile(filePath)) {
597
+ return 1;
598
+ }
599
+ const domainCohesion = calculateDomainCohesion(exports);
600
+ const hasImportData = exports.some((e) => e.imports && e.imports.length > 0);
601
+ if (!hasImportData) {
602
+ return domainCohesion;
603
+ }
604
+ const importCohesion = calculateImportBasedCohesion(exports);
605
+ return importCohesion * 0.6 + domainCohesion * 0.4;
606
+ }
607
+ function calculateImportBasedCohesion(exports) {
608
+ const exportsWithImports = exports.filter((e) => e.imports && e.imports.length > 0);
609
+ if (exportsWithImports.length < 2) {
610
+ return 1;
611
+ }
612
+ let totalSimilarity = 0;
613
+ let comparisons = 0;
614
+ for (let i = 0; i < exportsWithImports.length; i++) {
615
+ for (let j = i + 1; j < exportsWithImports.length; j++) {
616
+ const exp1 = exportsWithImports[i];
617
+ const exp2 = exportsWithImports[j];
618
+ const similarity = calculateJaccardSimilarity(exp1.imports, exp2.imports);
619
+ totalSimilarity += similarity;
620
+ comparisons++;
621
+ }
622
+ }
623
+ return comparisons > 0 ? totalSimilarity / comparisons : 1;
624
+ }
625
+ function calculateJaccardSimilarity(arr1, arr2) {
626
+ if (arr1.length === 0 && arr2.length === 0) return 1;
627
+ if (arr1.length === 0 || arr2.length === 0) return 0;
628
+ const set1 = new Set(arr1);
629
+ const set2 = new Set(arr2);
630
+ const intersection = new Set([...set1].filter((x) => set2.has(x)));
631
+ const union = /* @__PURE__ */ new Set([...set1, ...set2]);
632
+ return intersection.size / union.size;
633
+ }
634
+ function calculateDomainCohesion(exports) {
635
+ const domains = exports.map((e) => e.inferredDomain || "unknown");
636
+ const domainCounts = /* @__PURE__ */ new Map();
637
+ for (const domain of domains) {
638
+ domainCounts.set(domain, (domainCounts.get(domain) || 0) + 1);
639
+ }
640
+ const total = domains.length;
641
+ let entropy = 0;
642
+ for (const count of domainCounts.values()) {
643
+ const p = count / total;
644
+ if (p > 0) {
645
+ entropy -= p * Math.log2(p);
646
+ }
647
+ }
648
+ const maxEntropy = Math.log2(total);
649
+ return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
650
+ }
651
+
652
+ // src/index.ts
653
+ async function getSmartDefaults(directory, userOptions) {
654
+ const files = await scanFiles({
655
+ rootDir: directory,
656
+ include: userOptions.include,
657
+ exclude: userOptions.exclude
658
+ });
659
+ const estimatedBlocks = files.length;
660
+ let maxDepth;
661
+ let maxContextBudget;
662
+ let minCohesion;
663
+ let maxFragmentation;
664
+ if (estimatedBlocks < 100) {
665
+ maxDepth = 4;
666
+ maxContextBudget = 8e3;
667
+ minCohesion = 0.5;
668
+ maxFragmentation = 0.5;
669
+ } else if (estimatedBlocks < 500) {
670
+ maxDepth = 5;
671
+ maxContextBudget = 15e3;
672
+ minCohesion = 0.45;
673
+ maxFragmentation = 0.6;
674
+ } else if (estimatedBlocks < 2e3) {
675
+ maxDepth = 7;
676
+ maxContextBudget = 25e3;
677
+ minCohesion = 0.4;
678
+ maxFragmentation = 0.7;
679
+ } else {
680
+ maxDepth = 10;
681
+ maxContextBudget = 4e4;
682
+ minCohesion = 0.35;
683
+ maxFragmentation = 0.8;
684
+ }
685
+ return {
686
+ maxDepth,
687
+ maxContextBudget,
688
+ minCohesion,
689
+ maxFragmentation,
690
+ focus: "all",
691
+ includeNodeModules: false,
692
+ rootDir: userOptions.rootDir || directory,
693
+ include: userOptions.include,
694
+ exclude: userOptions.exclude
695
+ };
696
+ }
697
+ async function analyzeContext(options) {
698
+ const {
699
+ maxDepth = 5,
700
+ maxContextBudget = 1e4,
701
+ minCohesion = 0.6,
702
+ maxFragmentation = 0.5,
703
+ focus = "all",
704
+ includeNodeModules = false,
705
+ ...scanOptions
706
+ } = options;
707
+ const files = await scanFiles({
708
+ ...scanOptions,
709
+ // Only add node_modules to exclude if includeNodeModules is false
710
+ // The DEFAULT_EXCLUDE already includes node_modules, so this is only needed
711
+ // if user overrides the default exclude list
712
+ exclude: includeNodeModules && scanOptions.exclude ? scanOptions.exclude.filter((pattern) => pattern !== "**/node_modules/**") : scanOptions.exclude
713
+ });
714
+ const fileContents = await Promise.all(
715
+ files.map(async (file) => ({
716
+ file,
717
+ content: await readFileContent(file)
718
+ }))
719
+ );
720
+ const graph = buildDependencyGraph(fileContents, {
721
+ domainKeywords: options.domainKeywords,
722
+ domainPatterns: options.domainPatterns,
723
+ pathDomainMap: options.pathDomainMap
724
+ });
725
+ const circularDeps = detectCircularDependencies(graph);
726
+ const clusters = detectModuleClusters(graph);
727
+ const fragmentationMap = /* @__PURE__ */ new Map();
728
+ for (const cluster of clusters) {
729
+ for (const file of cluster.files) {
730
+ fragmentationMap.set(file, cluster.fragmentationScore);
731
+ }
732
+ }
733
+ const results = [];
734
+ for (const { file } of fileContents) {
735
+ const node = graph.nodes.get(file);
736
+ if (!node) continue;
737
+ const importDepth = focus === "depth" || focus === "all" ? calculateImportDepth(file, graph) : 0;
738
+ const dependencyList = focus === "depth" || focus === "all" ? getTransitiveDependencies(file, graph) : [];
739
+ const contextBudget = focus === "all" ? calculateContextBudget(file, graph) : node.tokenCost;
740
+ const cohesionScore = focus === "cohesion" || focus === "all" ? calculateCohesion(node.exports, file) : 1;
741
+ const fragmentationScore = fragmentationMap.get(file) || 0;
742
+ const relatedFiles = [];
743
+ for (const cluster of clusters) {
744
+ if (cluster.files.includes(file)) {
745
+ relatedFiles.push(...cluster.files.filter((f) => f !== file));
746
+ break;
747
+ }
748
+ }
749
+ const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
750
+ file,
751
+ importDepth,
752
+ contextBudget,
753
+ cohesionScore,
754
+ fragmentationScore,
755
+ maxDepth,
756
+ maxContextBudget,
757
+ minCohesion,
758
+ maxFragmentation,
759
+ circularDeps
760
+ });
761
+ const domains = [
762
+ ...new Set(node.exports.map((e) => e.inferredDomain || "unknown"))
763
+ ];
764
+ results.push({
765
+ file,
766
+ tokenCost: node.tokenCost,
767
+ linesOfCode: node.linesOfCode,
768
+ importDepth,
769
+ dependencyCount: dependencyList.length,
770
+ dependencyList,
771
+ circularDeps: circularDeps.filter((cycle) => cycle.includes(file)),
772
+ cohesionScore,
773
+ domains,
774
+ exportCount: node.exports.length,
775
+ contextBudget,
776
+ fragmentationScore,
777
+ relatedFiles,
778
+ severity,
779
+ issues,
780
+ recommendations,
781
+ potentialSavings
782
+ });
783
+ }
784
+ const issuesOnly = results.filter((r) => r.severity !== "info");
785
+ const sorted = issuesOnly.sort((a, b) => {
786
+ const severityOrder = { critical: 0, major: 1, minor: 2, info: 3 };
787
+ const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
788
+ if (severityDiff !== 0) return severityDiff;
789
+ return b.contextBudget - a.contextBudget;
790
+ });
791
+ return sorted.length > 0 ? sorted : results;
792
+ }
793
+ function generateSummary(results) {
794
+ if (results.length === 0) {
795
+ return {
796
+ totalFiles: 0,
797
+ totalTokens: 0,
798
+ avgContextBudget: 0,
799
+ maxContextBudget: 0,
800
+ avgImportDepth: 0,
801
+ maxImportDepth: 0,
802
+ deepFiles: [],
803
+ avgFragmentation: 0,
804
+ fragmentedModules: [],
805
+ avgCohesion: 0,
806
+ lowCohesionFiles: [],
807
+ criticalIssues: 0,
808
+ majorIssues: 0,
809
+ minorIssues: 0,
810
+ totalPotentialSavings: 0,
811
+ topExpensiveFiles: []
812
+ };
813
+ }
814
+ const totalFiles = results.length;
815
+ const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
816
+ const totalContextBudget = results.reduce(
817
+ (sum, r) => sum + r.contextBudget,
818
+ 0
819
+ );
820
+ const avgContextBudget = totalContextBudget / totalFiles;
821
+ const maxContextBudget = Math.max(...results.map((r) => r.contextBudget));
822
+ const avgImportDepth = results.reduce((sum, r) => sum + r.importDepth, 0) / totalFiles;
823
+ const maxImportDepth = Math.max(...results.map((r) => r.importDepth));
824
+ const deepFiles = results.filter((r) => r.importDepth >= 5).map((r) => ({ file: r.file, depth: r.importDepth })).sort((a, b) => b.depth - a.depth).slice(0, 10);
825
+ const avgFragmentation = results.reduce((sum, r) => sum + r.fragmentationScore, 0) / totalFiles;
826
+ const moduleMap = /* @__PURE__ */ new Map();
827
+ for (const result of results) {
828
+ for (const domain of result.domains) {
829
+ if (!moduleMap.has(domain)) {
830
+ moduleMap.set(domain, []);
831
+ }
832
+ moduleMap.get(domain).push(result);
833
+ }
834
+ }
835
+ const fragmentedModules = [];
836
+ for (const [domain, files] of moduleMap.entries()) {
837
+ if (files.length < 2) continue;
838
+ const fragmentationScore = files.reduce((sum, f) => sum + f.fragmentationScore, 0) / files.length;
839
+ if (fragmentationScore < 0.3) continue;
840
+ const totalTokens2 = files.reduce((sum, f) => sum + f.tokenCost, 0);
841
+ const avgCohesion2 = files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length;
842
+ const targetFiles = Math.max(1, Math.ceil(files.length / 3));
843
+ fragmentedModules.push({
844
+ domain,
845
+ files: files.map((f) => f.file),
846
+ totalTokens: totalTokens2,
847
+ fragmentationScore,
848
+ avgCohesion: avgCohesion2,
849
+ suggestedStructure: {
850
+ targetFiles,
851
+ consolidationPlan: [
852
+ `Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s)`,
853
+ `Current token cost: ${totalTokens2.toLocaleString()}`,
854
+ `Estimated savings: ${Math.floor(totalTokens2 * 0.3).toLocaleString()} tokens (30%)`
855
+ ]
856
+ }
857
+ });
858
+ }
859
+ fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
860
+ const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / totalFiles;
861
+ const lowCohesionFiles = results.filter((r) => r.cohesionScore < 0.6).map((r) => ({ file: r.file, score: r.cohesionScore })).sort((a, b) => a.score - b.score).slice(0, 10);
862
+ const criticalIssues = results.filter((r) => r.severity === "critical").length;
863
+ const majorIssues = results.filter((r) => r.severity === "major").length;
864
+ const minorIssues = results.filter((r) => r.severity === "minor").length;
865
+ const totalPotentialSavings = results.reduce(
866
+ (sum, r) => sum + r.potentialSavings,
867
+ 0
868
+ );
869
+ const topExpensiveFiles = results.sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
870
+ file: r.file,
871
+ contextBudget: r.contextBudget,
872
+ severity: r.severity
873
+ }));
874
+ return {
875
+ totalFiles,
876
+ totalTokens,
877
+ avgContextBudget,
878
+ maxContextBudget,
879
+ avgImportDepth,
880
+ maxImportDepth,
881
+ deepFiles,
882
+ avgFragmentation,
883
+ fragmentedModules: fragmentedModules.slice(0, 10),
884
+ avgCohesion,
885
+ lowCohesionFiles,
886
+ criticalIssues,
887
+ majorIssues,
888
+ minorIssues,
889
+ totalPotentialSavings,
890
+ topExpensiveFiles
891
+ };
892
+ }
893
+ function analyzeIssues(params) {
894
+ const {
895
+ file,
896
+ importDepth,
897
+ contextBudget,
898
+ cohesionScore,
899
+ fragmentationScore,
900
+ maxDepth,
901
+ maxContextBudget,
902
+ minCohesion,
903
+ maxFragmentation,
904
+ circularDeps
905
+ } = params;
906
+ const issues = [];
907
+ const recommendations = [];
908
+ let severity = "info";
909
+ let potentialSavings = 0;
910
+ if (circularDeps.length > 0) {
911
+ severity = "critical";
912
+ issues.push(
913
+ `Part of ${circularDeps.length} circular dependency chain(s)`
914
+ );
915
+ recommendations.push("Break circular dependencies by extracting interfaces or using dependency injection");
916
+ potentialSavings += contextBudget * 0.2;
917
+ }
918
+ if (importDepth > maxDepth * 1.5) {
919
+ severity = severity === "critical" ? "critical" : "critical";
920
+ issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
921
+ recommendations.push("Flatten dependency tree or use facade pattern");
922
+ potentialSavings += contextBudget * 0.3;
923
+ } else if (importDepth > maxDepth) {
924
+ severity = severity === "critical" ? "critical" : "major";
925
+ issues.push(`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`);
926
+ recommendations.push("Consider reducing dependency depth");
927
+ potentialSavings += contextBudget * 0.15;
928
+ }
929
+ if (contextBudget > maxContextBudget * 1.5) {
930
+ severity = severity === "critical" ? "critical" : "critical";
931
+ issues.push(`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`);
932
+ recommendations.push("Split into smaller modules or reduce dependency tree");
933
+ potentialSavings += contextBudget * 0.4;
934
+ } else if (contextBudget > maxContextBudget) {
935
+ severity = severity === "critical" || severity === "major" ? severity : "major";
936
+ issues.push(`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`);
937
+ recommendations.push("Reduce file size or dependencies");
938
+ potentialSavings += contextBudget * 0.2;
939
+ }
940
+ if (cohesionScore < minCohesion * 0.5) {
941
+ severity = severity === "critical" ? "critical" : "major";
942
+ issues.push(`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`);
943
+ recommendations.push("Split file by domain - separate unrelated functionality");
944
+ potentialSavings += contextBudget * 0.25;
945
+ } else if (cohesionScore < minCohesion) {
946
+ severity = severity === "critical" || severity === "major" ? severity : "minor";
947
+ issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
948
+ recommendations.push("Consider grouping related exports together");
949
+ potentialSavings += contextBudget * 0.1;
950
+ }
951
+ if (fragmentationScore > maxFragmentation) {
952
+ severity = severity === "critical" || severity === "major" ? severity : "minor";
953
+ issues.push(`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`);
954
+ recommendations.push("Consolidate with related files in same domain");
955
+ potentialSavings += contextBudget * 0.3;
956
+ }
957
+ if (issues.length === 0) {
958
+ issues.push("No significant issues detected");
959
+ recommendations.push("File is well-structured for AI context usage");
960
+ }
961
+ if (isBuildArtifact(file)) {
962
+ issues.push("Detected build artifact (bundled/output file)");
963
+ recommendations.push("Exclude build outputs (e.g., cdk.out, dist, build, .next) from analysis");
964
+ severity = downgradeSeverity(severity);
965
+ potentialSavings = 0;
966
+ }
967
+ return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
968
+ }
969
+ function isBuildArtifact(filePath) {
970
+ const lower = filePath.toLowerCase();
971
+ return lower.includes("/node_modules/") || lower.includes("/dist/") || lower.includes("/build/") || lower.includes("/out/") || lower.includes("/output/") || lower.includes("/cdk.out/") || lower.includes("/.next/") || /\/asset\.[^/]+\//.test(lower);
972
+ }
973
+ function downgradeSeverity(s) {
974
+ switch (s) {
975
+ case "critical":
976
+ return "minor";
977
+ case "major":
978
+ return "minor";
979
+ case "minor":
980
+ return "info";
981
+ default:
982
+ return "info";
983
+ }
984
+ }
985
+
986
+ export {
987
+ buildCoUsageMatrix,
988
+ buildTypeGraph,
989
+ findSemanticClusters,
990
+ calculateDomainConfidence,
991
+ inferDomainFromSemantics,
992
+ getCoUsageData,
993
+ findConsolidationCandidates,
994
+ getSmartDefaults,
995
+ analyzeContext,
996
+ generateSummary
997
+ };