@aiready/context-analyzer 0.3.7 → 0.4.0

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
  
3
- > @aiready/context-analyzer@0.3.7 build /Users/pengcao/projects/aiready/packages/context-analyzer
3
+ > @aiready/context-analyzer@0.4.0 build /Users/pengcao/projects/aiready/packages/context-analyzer
4
4
  > tsup src/index.ts src/cli.ts --format cjs,esm --dts
5
5
 
6
6
  CLI Building entry: src/cli.ts, src/index.ts
@@ -9,16 +9,16 @@
9
9
  CLI Target: es2020
10
10
  CJS Build start
11
11
  ESM Build start
12
- CJS dist/cli.js 32.57 KB
13
- CJS dist/index.js 18.33 KB
14
- CJS ⚡️ Build success in 61ms
15
- ESM dist/index.mjs 124.00 B
16
- ESM dist/chunk-T6ZCOPPI.mjs 17.25 KB
17
- ESM dist/cli.mjs 13.36 KB
18
- ESM ⚡️ Build success in 61ms
12
+ CJS dist/index.js 20.26 KB
13
+ CJS dist/cli.js 35.91 KB
14
+ CJS ⚡️ Build success in 50ms
15
+ ESM dist/index.mjs 164.00 B
16
+ ESM dist/chunk-HDFXSEFW.mjs 19.11 KB
17
+ ESM dist/cli.mjs 15.74 KB
18
+ ESM ⚡️ Build success in 50ms
19
19
  DTS Build start
20
- DTS ⚡️ Build success in 615ms
20
+ DTS ⚡️ Build success in 534ms
21
21
  DTS dist/cli.d.ts 20.00 B
22
- DTS dist/index.d.ts 2.14 KB
22
+ DTS dist/index.d.ts 2.37 KB
23
23
  DTS dist/cli.d.mts 20.00 B
24
- DTS dist/index.d.mts 2.14 KB
24
+ DTS dist/index.d.mts 2.37 KB
@@ -1,6 +1,6 @@
1
1
 
2
2
  
3
- > @aiready/context-analyzer@0.1.0 test /Users/pengcao/projects/aiready/packages/context-analyzer
3
+ > @aiready/context-analyzer@0.3.8 test /Users/pengcao/projects/aiready/packages/context-analyzer
4
4
  > vitest run
5
5
 
6
6
 
@@ -30,7 +30,7 @@
30
30
 
31
31
   Test Files  1 passed (1)
32
32
   Tests  13 passed (13)
33
-  Start at  03:16:28
34
-  Duration  314ms (transform 75ms, setup 0ms, collect 86ms, tests 4ms, environment 0ms, prepare 38ms)
33
+  Start at  03:56:27
34
+  Duration  286ms (transform 65ms, setup 0ms, collect 76ms, tests 3ms, environment 0ms, prepare 45ms)
35
35
 
36
36
  [?25h
package/README.md CHANGED
@@ -265,7 +265,7 @@ By default, these patterns are excluded (unless `--include-node-modules` is used
265
265
  **/node_modules/**
266
266
 
267
267
  # Build outputs
268
- **/dist/**, **/build/**, **/out/**, **/output/**, **/target/**, **/bin/**, **/obj/**
268
+ **/dist/**, **/build/**, **/out/**, **/output/**, **/target/**, **/bin/**, **/obj/**, **/cdk.out/**
269
269
 
270
270
  # Framework-specific build dirs
271
271
  **/.next/**, **/.nuxt/**, **/.vuepress/**, **/.cache/**, **/.turbo/**
@@ -357,6 +357,20 @@ Shareable report with tables and visualizations. Perfect for stakeholders:
357
357
  aiready-context ./src --output html --output-file context-report.html
358
358
  ```
359
359
 
360
+ ## 🧭 Interactive Mode
361
+
362
+ For first-time users, enable interactive guidance to apply smart defaults and focus areas:
363
+
364
+ ```bash
365
+ # Suggest excludes for common frameworks (Next.js, AWS CDK) and choose focus
366
+ aiready-context ./src --interactive
367
+ ```
368
+
369
+ Interactive mode:
370
+ - Detects frameworks and recommends excludes (e.g., .next, cdk.out)
371
+ - Lets you choose focus areas: frontend, backend, or both
372
+ - Applies configuration without modifying your files
373
+
360
374
  ## 🔗 Integration
361
375
 
362
376
  ### CI/CD
@@ -0,0 +1,583 @@
1
+ // src/index.ts
2
+ import { scanFiles, readFileContent } from "@aiready/core";
3
+
4
+ // src/analyzer.ts
5
+ import { estimateTokens } from "@aiready/core";
6
+ function buildDependencyGraph(files) {
7
+ const nodes = /* @__PURE__ */ new Map();
8
+ const edges = /* @__PURE__ */ new Map();
9
+ for (const { file, content } of files) {
10
+ const imports = extractImportsFromContent(content);
11
+ const exports = extractExports(content);
12
+ const tokenCost = estimateTokens(content);
13
+ const linesOfCode = content.split("\n").length;
14
+ nodes.set(file, {
15
+ file,
16
+ imports,
17
+ exports,
18
+ tokenCost,
19
+ linesOfCode
20
+ });
21
+ edges.set(file, new Set(imports));
22
+ }
23
+ return { nodes, edges };
24
+ }
25
+ function extractImportsFromContent(content) {
26
+ const imports = [];
27
+ const patterns = [
28
+ /import\s+.*?\s+from\s+['"](.+?)['"]/g,
29
+ // import ... from '...'
30
+ /import\s+['"](.+?)['"]/g,
31
+ // import '...'
32
+ /require\(['"](.+?)['"]\)/g
33
+ // require('...')
34
+ ];
35
+ for (const pattern of patterns) {
36
+ let match;
37
+ while ((match = pattern.exec(content)) !== null) {
38
+ const importPath = match[1];
39
+ if (importPath && !importPath.startsWith("@") && !importPath.startsWith("node:")) {
40
+ imports.push(importPath);
41
+ }
42
+ }
43
+ }
44
+ return [...new Set(imports)];
45
+ }
46
+ function calculateImportDepth(file, graph, visited = /* @__PURE__ */ new Set(), depth = 0) {
47
+ if (visited.has(file)) {
48
+ return depth;
49
+ }
50
+ const dependencies = graph.edges.get(file);
51
+ if (!dependencies || dependencies.size === 0) {
52
+ return depth;
53
+ }
54
+ visited.add(file);
55
+ let maxDepth = depth;
56
+ for (const dep of dependencies) {
57
+ const depDepth = calculateImportDepth(dep, graph, visited, depth + 1);
58
+ maxDepth = Math.max(maxDepth, depDepth);
59
+ }
60
+ visited.delete(file);
61
+ return maxDepth;
62
+ }
63
+ function getTransitiveDependencies(file, graph, visited = /* @__PURE__ */ new Set()) {
64
+ if (visited.has(file)) {
65
+ return [];
66
+ }
67
+ visited.add(file);
68
+ const dependencies = graph.edges.get(file);
69
+ if (!dependencies || dependencies.size === 0) {
70
+ return [];
71
+ }
72
+ const allDeps = [];
73
+ for (const dep of dependencies) {
74
+ allDeps.push(dep);
75
+ allDeps.push(...getTransitiveDependencies(dep, graph, visited));
76
+ }
77
+ return [...new Set(allDeps)];
78
+ }
79
+ function calculateContextBudget(file, graph) {
80
+ const node = graph.nodes.get(file);
81
+ if (!node) return 0;
82
+ let totalTokens = node.tokenCost;
83
+ const deps = getTransitiveDependencies(file, graph);
84
+ for (const dep of deps) {
85
+ const depNode = graph.nodes.get(dep);
86
+ if (depNode) {
87
+ totalTokens += depNode.tokenCost;
88
+ }
89
+ }
90
+ return totalTokens;
91
+ }
92
+ function detectCircularDependencies(graph) {
93
+ const cycles = [];
94
+ const visited = /* @__PURE__ */ new Set();
95
+ const recursionStack = /* @__PURE__ */ new Set();
96
+ function dfs(file, path) {
97
+ if (recursionStack.has(file)) {
98
+ const cycleStart = path.indexOf(file);
99
+ if (cycleStart !== -1) {
100
+ cycles.push([...path.slice(cycleStart), file]);
101
+ }
102
+ return;
103
+ }
104
+ if (visited.has(file)) {
105
+ return;
106
+ }
107
+ visited.add(file);
108
+ recursionStack.add(file);
109
+ path.push(file);
110
+ const dependencies = graph.edges.get(file);
111
+ if (dependencies) {
112
+ for (const dep of dependencies) {
113
+ dfs(dep, [...path]);
114
+ }
115
+ }
116
+ recursionStack.delete(file);
117
+ }
118
+ for (const file of graph.nodes.keys()) {
119
+ if (!visited.has(file)) {
120
+ dfs(file, []);
121
+ }
122
+ }
123
+ return cycles;
124
+ }
125
+ function calculateCohesion(exports) {
126
+ if (exports.length === 0) return 1;
127
+ if (exports.length === 1) return 1;
128
+ const domains = exports.map((e) => e.inferredDomain || "unknown");
129
+ const domainCounts = /* @__PURE__ */ new Map();
130
+ for (const domain of domains) {
131
+ domainCounts.set(domain, (domainCounts.get(domain) || 0) + 1);
132
+ }
133
+ const total = domains.length;
134
+ let entropy = 0;
135
+ for (const count of domainCounts.values()) {
136
+ const p = count / total;
137
+ if (p > 0) {
138
+ entropy -= p * Math.log2(p);
139
+ }
140
+ }
141
+ const maxEntropy = Math.log2(total);
142
+ return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
143
+ }
144
+ function calculateFragmentation(files, domain) {
145
+ if (files.length <= 1) return 0;
146
+ const directories = new Set(files.map((f) => f.split("/").slice(0, -1).join("/")));
147
+ return (directories.size - 1) / (files.length - 1);
148
+ }
149
+ function detectModuleClusters(graph) {
150
+ const domainMap = /* @__PURE__ */ new Map();
151
+ for (const [file, node] of graph.nodes.entries()) {
152
+ const domains = node.exports.map((e) => e.inferredDomain || "unknown");
153
+ const primaryDomain = domains[0] || "unknown";
154
+ if (!domainMap.has(primaryDomain)) {
155
+ domainMap.set(primaryDomain, []);
156
+ }
157
+ domainMap.get(primaryDomain).push(file);
158
+ }
159
+ const clusters = [];
160
+ for (const [domain, files] of domainMap.entries()) {
161
+ if (files.length < 2) continue;
162
+ const totalTokens = files.reduce((sum, file) => {
163
+ const node = graph.nodes.get(file);
164
+ return sum + (node?.tokenCost || 0);
165
+ }, 0);
166
+ const fragmentationScore = calculateFragmentation(files, domain);
167
+ const avgCohesion = files.reduce((sum, file) => {
168
+ const node = graph.nodes.get(file);
169
+ return sum + (node ? calculateCohesion(node.exports) : 0);
170
+ }, 0) / files.length;
171
+ const targetFiles = Math.max(1, Math.ceil(files.length / 3));
172
+ const consolidationPlan = generateConsolidationPlan(
173
+ domain,
174
+ files,
175
+ targetFiles
176
+ );
177
+ clusters.push({
178
+ domain,
179
+ files,
180
+ totalTokens,
181
+ fragmentationScore,
182
+ avgCohesion,
183
+ suggestedStructure: {
184
+ targetFiles,
185
+ consolidationPlan
186
+ }
187
+ });
188
+ }
189
+ return clusters.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
190
+ }
191
+ function extractExports(content) {
192
+ const exports = [];
193
+ const patterns = [
194
+ /export\s+function\s+(\w+)/g,
195
+ /export\s+class\s+(\w+)/g,
196
+ /export\s+const\s+(\w+)/g,
197
+ /export\s+type\s+(\w+)/g,
198
+ /export\s+interface\s+(\w+)/g,
199
+ /export\s+default/g
200
+ ];
201
+ const types = [
202
+ "function",
203
+ "class",
204
+ "const",
205
+ "type",
206
+ "interface",
207
+ "default"
208
+ ];
209
+ patterns.forEach((pattern, index) => {
210
+ let match;
211
+ while ((match = pattern.exec(content)) !== null) {
212
+ const name = match[1] || "default";
213
+ const type = types[index];
214
+ const inferredDomain = inferDomain(name);
215
+ exports.push({ name, type, inferredDomain });
216
+ }
217
+ });
218
+ return exports;
219
+ }
220
+ function inferDomain(name) {
221
+ const lower = name.toLowerCase();
222
+ const domainKeywords = [
223
+ "user",
224
+ "auth",
225
+ "order",
226
+ "product",
227
+ "payment",
228
+ "cart",
229
+ "invoice",
230
+ "customer",
231
+ "admin",
232
+ "api",
233
+ "util",
234
+ "helper",
235
+ "config",
236
+ "service",
237
+ "repository",
238
+ "controller",
239
+ "model",
240
+ "view"
241
+ ];
242
+ for (const keyword of domainKeywords) {
243
+ if (lower.includes(keyword)) {
244
+ return keyword;
245
+ }
246
+ }
247
+ return "unknown";
248
+ }
249
+ function generateConsolidationPlan(domain, files, targetFiles) {
250
+ const plan = [];
251
+ if (files.length <= targetFiles) {
252
+ return [`No consolidation needed for ${domain}`];
253
+ }
254
+ plan.push(
255
+ `Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s):`
256
+ );
257
+ const dirGroups = /* @__PURE__ */ new Map();
258
+ for (const file of files) {
259
+ const dir = file.split("/").slice(0, -1).join("/");
260
+ if (!dirGroups.has(dir)) {
261
+ dirGroups.set(dir, []);
262
+ }
263
+ dirGroups.get(dir).push(file);
264
+ }
265
+ plan.push(`1. Create unified ${domain} module file`);
266
+ plan.push(
267
+ `2. Move related functionality from ${files.length} scattered files`
268
+ );
269
+ plan.push(`3. Update imports in dependent files`);
270
+ plan.push(
271
+ `4. Remove old files after consolidation (verify with tests first)`
272
+ );
273
+ return plan;
274
+ }
275
+
276
+ // src/index.ts
277
+ async function getSmartDefaults(directory, userOptions) {
278
+ const files = await scanFiles({
279
+ rootDir: directory,
280
+ include: userOptions.include,
281
+ exclude: userOptions.exclude
282
+ });
283
+ const estimatedBlocks = files.length;
284
+ let maxDepth;
285
+ let maxContextBudget;
286
+ let minCohesion;
287
+ let maxFragmentation;
288
+ if (estimatedBlocks < 100) {
289
+ maxDepth = 3;
290
+ maxContextBudget = 5e3;
291
+ minCohesion = 0.7;
292
+ maxFragmentation = 0.3;
293
+ } else if (estimatedBlocks < 500) {
294
+ maxDepth = 4;
295
+ maxContextBudget = 8e3;
296
+ minCohesion = 0.65;
297
+ maxFragmentation = 0.4;
298
+ } else if (estimatedBlocks < 2e3) {
299
+ maxDepth = 5;
300
+ maxContextBudget = 12e3;
301
+ minCohesion = 0.6;
302
+ maxFragmentation = 0.5;
303
+ } else {
304
+ maxDepth = 6;
305
+ maxContextBudget = 2e4;
306
+ minCohesion = 0.55;
307
+ maxFragmentation = 0.6;
308
+ }
309
+ return {
310
+ maxDepth,
311
+ maxContextBudget,
312
+ minCohesion,
313
+ maxFragmentation,
314
+ focus: "all",
315
+ includeNodeModules: false,
316
+ rootDir: userOptions.rootDir || directory,
317
+ include: userOptions.include,
318
+ exclude: userOptions.exclude
319
+ };
320
+ }
321
+ async function analyzeContext(options) {
322
+ const {
323
+ maxDepth = 5,
324
+ maxContextBudget = 1e4,
325
+ minCohesion = 0.6,
326
+ maxFragmentation = 0.5,
327
+ focus = "all",
328
+ includeNodeModules = false,
329
+ ...scanOptions
330
+ } = options;
331
+ const files = await scanFiles({
332
+ ...scanOptions,
333
+ exclude: includeNodeModules ? scanOptions.exclude : [...scanOptions.exclude || [], "**/node_modules/**"]
334
+ });
335
+ const fileContents = await Promise.all(
336
+ files.map(async (file) => ({
337
+ file,
338
+ content: await readFileContent(file)
339
+ }))
340
+ );
341
+ const graph = buildDependencyGraph(fileContents);
342
+ const circularDeps = detectCircularDependencies(graph);
343
+ const clusters = detectModuleClusters(graph);
344
+ const fragmentationMap = /* @__PURE__ */ new Map();
345
+ for (const cluster of clusters) {
346
+ for (const file of cluster.files) {
347
+ fragmentationMap.set(file, cluster.fragmentationScore);
348
+ }
349
+ }
350
+ const results = [];
351
+ for (const { file } of fileContents) {
352
+ const node = graph.nodes.get(file);
353
+ if (!node) continue;
354
+ const importDepth = focus === "depth" || focus === "all" ? calculateImportDepth(file, graph) : 0;
355
+ const dependencyList = focus === "depth" || focus === "all" ? getTransitiveDependencies(file, graph) : [];
356
+ const contextBudget = focus === "all" ? calculateContextBudget(file, graph) : node.tokenCost;
357
+ const cohesionScore = focus === "cohesion" || focus === "all" ? calculateCohesion(node.exports) : 1;
358
+ const fragmentationScore = fragmentationMap.get(file) || 0;
359
+ const relatedFiles = [];
360
+ for (const cluster of clusters) {
361
+ if (cluster.files.includes(file)) {
362
+ relatedFiles.push(...cluster.files.filter((f) => f !== file));
363
+ break;
364
+ }
365
+ }
366
+ const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
367
+ file,
368
+ importDepth,
369
+ contextBudget,
370
+ cohesionScore,
371
+ fragmentationScore,
372
+ maxDepth,
373
+ maxContextBudget,
374
+ minCohesion,
375
+ maxFragmentation,
376
+ circularDeps
377
+ });
378
+ const domains = [
379
+ ...new Set(node.exports.map((e) => e.inferredDomain || "unknown"))
380
+ ];
381
+ results.push({
382
+ file,
383
+ tokenCost: node.tokenCost,
384
+ linesOfCode: node.linesOfCode,
385
+ importDepth,
386
+ dependencyCount: dependencyList.length,
387
+ dependencyList,
388
+ circularDeps: circularDeps.filter((cycle) => cycle.includes(file)),
389
+ cohesionScore,
390
+ domains,
391
+ exportCount: node.exports.length,
392
+ contextBudget,
393
+ fragmentationScore,
394
+ relatedFiles,
395
+ severity,
396
+ issues,
397
+ recommendations,
398
+ potentialSavings
399
+ });
400
+ }
401
+ return results.sort((a, b) => {
402
+ const severityOrder = { critical: 0, major: 1, minor: 2, info: 3 };
403
+ const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
404
+ if (severityDiff !== 0) return severityDiff;
405
+ return b.contextBudget - a.contextBudget;
406
+ });
407
+ }
408
+ function generateSummary(results) {
409
+ if (results.length === 0) {
410
+ return {
411
+ totalFiles: 0,
412
+ totalTokens: 0,
413
+ avgContextBudget: 0,
414
+ maxContextBudget: 0,
415
+ avgImportDepth: 0,
416
+ maxImportDepth: 0,
417
+ deepFiles: [],
418
+ avgFragmentation: 0,
419
+ fragmentedModules: [],
420
+ avgCohesion: 0,
421
+ lowCohesionFiles: [],
422
+ criticalIssues: 0,
423
+ majorIssues: 0,
424
+ minorIssues: 0,
425
+ totalPotentialSavings: 0,
426
+ topExpensiveFiles: []
427
+ };
428
+ }
429
+ const totalFiles = results.length;
430
+ const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
431
+ const totalContextBudget = results.reduce(
432
+ (sum, r) => sum + r.contextBudget,
433
+ 0
434
+ );
435
+ const avgContextBudget = totalContextBudget / totalFiles;
436
+ const maxContextBudget = Math.max(...results.map((r) => r.contextBudget));
437
+ const avgImportDepth = results.reduce((sum, r) => sum + r.importDepth, 0) / totalFiles;
438
+ const maxImportDepth = Math.max(...results.map((r) => r.importDepth));
439
+ 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);
440
+ const avgFragmentation = results.reduce((sum, r) => sum + r.fragmentationScore, 0) / totalFiles;
441
+ const moduleMap = /* @__PURE__ */ new Map();
442
+ for (const result of results) {
443
+ for (const domain of result.domains) {
444
+ if (!moduleMap.has(domain)) {
445
+ moduleMap.set(domain, []);
446
+ }
447
+ moduleMap.get(domain).push(result);
448
+ }
449
+ }
450
+ const fragmentedModules = [];
451
+ for (const [domain, files] of moduleMap.entries()) {
452
+ if (files.length < 2) continue;
453
+ const fragmentationScore = files.reduce((sum, f) => sum + f.fragmentationScore, 0) / files.length;
454
+ if (fragmentationScore < 0.3) continue;
455
+ const totalTokens2 = files.reduce((sum, f) => sum + f.tokenCost, 0);
456
+ const avgCohesion2 = files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length;
457
+ const targetFiles = Math.max(1, Math.ceil(files.length / 3));
458
+ fragmentedModules.push({
459
+ domain,
460
+ files: files.map((f) => f.file),
461
+ totalTokens: totalTokens2,
462
+ fragmentationScore,
463
+ avgCohesion: avgCohesion2,
464
+ suggestedStructure: {
465
+ targetFiles,
466
+ consolidationPlan: [
467
+ `Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s)`,
468
+ `Current token cost: ${totalTokens2.toLocaleString()}`,
469
+ `Estimated savings: ${Math.floor(totalTokens2 * 0.3).toLocaleString()} tokens (30%)`
470
+ ]
471
+ }
472
+ });
473
+ }
474
+ fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
475
+ const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / totalFiles;
476
+ 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);
477
+ const criticalIssues = results.filter((r) => r.severity === "critical").length;
478
+ const majorIssues = results.filter((r) => r.severity === "major").length;
479
+ const minorIssues = results.filter((r) => r.severity === "minor").length;
480
+ const totalPotentialSavings = results.reduce(
481
+ (sum, r) => sum + r.potentialSavings,
482
+ 0
483
+ );
484
+ const topExpensiveFiles = results.sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
485
+ file: r.file,
486
+ contextBudget: r.contextBudget,
487
+ severity: r.severity
488
+ }));
489
+ return {
490
+ totalFiles,
491
+ totalTokens,
492
+ avgContextBudget,
493
+ maxContextBudget,
494
+ avgImportDepth,
495
+ maxImportDepth,
496
+ deepFiles,
497
+ avgFragmentation,
498
+ fragmentedModules: fragmentedModules.slice(0, 10),
499
+ avgCohesion,
500
+ lowCohesionFiles,
501
+ criticalIssues,
502
+ majorIssues,
503
+ minorIssues,
504
+ totalPotentialSavings,
505
+ topExpensiveFiles
506
+ };
507
+ }
508
+ function analyzeIssues(params) {
509
+ const {
510
+ file,
511
+ importDepth,
512
+ contextBudget,
513
+ cohesionScore,
514
+ fragmentationScore,
515
+ maxDepth,
516
+ maxContextBudget,
517
+ minCohesion,
518
+ maxFragmentation,
519
+ circularDeps
520
+ } = params;
521
+ const issues = [];
522
+ const recommendations = [];
523
+ let severity = "info";
524
+ let potentialSavings = 0;
525
+ if (circularDeps.length > 0) {
526
+ severity = "critical";
527
+ issues.push(
528
+ `Part of ${circularDeps.length} circular dependency chain(s)`
529
+ );
530
+ recommendations.push("Break circular dependencies by extracting interfaces or using dependency injection");
531
+ potentialSavings += contextBudget * 0.2;
532
+ }
533
+ if (importDepth > maxDepth * 1.5) {
534
+ severity = severity === "critical" ? "critical" : "critical";
535
+ issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
536
+ recommendations.push("Flatten dependency tree or use facade pattern");
537
+ potentialSavings += contextBudget * 0.3;
538
+ } else if (importDepth > maxDepth) {
539
+ severity = severity === "critical" ? "critical" : "major";
540
+ issues.push(`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`);
541
+ recommendations.push("Consider reducing dependency depth");
542
+ potentialSavings += contextBudget * 0.15;
543
+ }
544
+ if (contextBudget > maxContextBudget * 1.5) {
545
+ severity = severity === "critical" ? "critical" : "critical";
546
+ issues.push(`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`);
547
+ recommendations.push("Split into smaller modules or reduce dependency tree");
548
+ potentialSavings += contextBudget * 0.4;
549
+ } else if (contextBudget > maxContextBudget) {
550
+ severity = severity === "critical" || severity === "major" ? severity : "major";
551
+ issues.push(`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`);
552
+ recommendations.push("Reduce file size or dependencies");
553
+ potentialSavings += contextBudget * 0.2;
554
+ }
555
+ if (cohesionScore < minCohesion * 0.5) {
556
+ severity = severity === "critical" ? "critical" : "major";
557
+ issues.push(`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`);
558
+ recommendations.push("Split file by domain - separate unrelated functionality");
559
+ potentialSavings += contextBudget * 0.25;
560
+ } else if (cohesionScore < minCohesion) {
561
+ severity = severity === "critical" || severity === "major" ? severity : "minor";
562
+ issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
563
+ recommendations.push("Consider grouping related exports together");
564
+ potentialSavings += contextBudget * 0.1;
565
+ }
566
+ if (fragmentationScore > maxFragmentation) {
567
+ severity = severity === "critical" || severity === "major" ? severity : "minor";
568
+ issues.push(`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`);
569
+ recommendations.push("Consolidate with related files in same domain");
570
+ potentialSavings += contextBudget * 0.3;
571
+ }
572
+ if (issues.length === 0) {
573
+ issues.push("No significant issues detected");
574
+ recommendations.push("File is well-structured for AI context usage");
575
+ }
576
+ return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
577
+ }
578
+
579
+ export {
580
+ getSmartDefaults,
581
+ analyzeContext,
582
+ generateSummary
583
+ };