@aiready/context-analyzer 0.3.8 → 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.
- package/.turbo/turbo-build.log +8 -8
- package/.turbo/turbo-test.log +3 -3
- package/README.md +15 -1
- package/dist/chunk-HDFXSEFW.mjs +605 -0
- package/dist/cli.js +80 -2
- package/dist/cli.mjs +60 -4
- package/dist/index.js +22 -0
- package/dist/index.mjs +1 -1
- package/package.json +4 -2
- package/src/cli.ts +72 -2
- package/src/index.ts +39 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @aiready/context-analyzer@0.
|
|
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
|
[34mCLI[39m Building entry: src/cli.ts, src/index.ts
|
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
[34mCLI[39m Target: es2020
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
11
|
[34mESM[39m Build start
|
|
12
|
-
[
|
|
12
|
+
[32mCJS[39m [1mdist/index.js [22m[32m20.26 KB[39m
|
|
13
|
+
[32mCJS[39m [1mdist/cli.js [22m[32m35.91 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 50ms
|
|
13
15
|
[32mESM[39m [1mdist/index.mjs [22m[32m164.00 B[39m
|
|
14
|
-
[32mESM[39m [1mdist/
|
|
15
|
-
[32mESM[39m
|
|
16
|
-
[
|
|
17
|
-
[32mCJS[39m [1mdist/cli.js [22m[32m32.57 KB[39m
|
|
18
|
-
[32mCJS[39m ⚡️ Build success in 23ms
|
|
16
|
+
[32mESM[39m [1mdist/chunk-HDFXSEFW.mjs [22m[32m19.11 KB[39m
|
|
17
|
+
[32mESM[39m [1mdist/cli.mjs [22m[32m15.74 KB[39m
|
|
18
|
+
[32mESM[39m ⚡️ Build success in 50ms
|
|
19
19
|
DTS Build start
|
|
20
|
-
DTS ⚡️ Build success in
|
|
20
|
+
DTS ⚡️ Build success in 534ms
|
|
21
21
|
DTS dist/cli.d.ts 20.00 B
|
|
22
22
|
DTS dist/index.d.ts 2.37 KB
|
|
23
23
|
DTS dist/cli.d.mts 20.00 B
|
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @aiready/context-analyzer@0.
|
|
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
|
[2m Test Files [22m [1m[32m1 passed[39m[22m[90m (1)[39m
|
|
32
32
|
[2m Tests [22m [1m[32m13 passed[39m[22m[90m (13)[39m
|
|
33
|
-
[2m Start at [22m 03:
|
|
34
|
-
[2m Duration [22m
|
|
33
|
+
[2m Start at [22m 03:56:27
|
|
34
|
+
[2m Duration [22m 286ms[2m (transform 65ms, setup 0ms, collect 76ms, tests 3ms, environment 0ms, prepare 45ms)[22m
|
|
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,605 @@
|
|
|
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
|
+
if (isBuildArtifact(file)) {
|
|
577
|
+
issues.push("Detected build artifact (bundled/output file)");
|
|
578
|
+
recommendations.push("Exclude build outputs (e.g., cdk.out, dist, build, .next) from analysis");
|
|
579
|
+
severity = downgradeSeverity(severity);
|
|
580
|
+
potentialSavings = 0;
|
|
581
|
+
}
|
|
582
|
+
return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
|
|
583
|
+
}
|
|
584
|
+
function isBuildArtifact(filePath) {
|
|
585
|
+
const lower = filePath.toLowerCase();
|
|
586
|
+
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);
|
|
587
|
+
}
|
|
588
|
+
function downgradeSeverity(s) {
|
|
589
|
+
switch (s) {
|
|
590
|
+
case "critical":
|
|
591
|
+
return "minor";
|
|
592
|
+
case "major":
|
|
593
|
+
return "minor";
|
|
594
|
+
case "minor":
|
|
595
|
+
return "info";
|
|
596
|
+
default:
|
|
597
|
+
return "info";
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
export {
|
|
602
|
+
getSmartDefaults,
|
|
603
|
+
analyzeContext,
|
|
604
|
+
generateSummary
|
|
605
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -557,14 +557,37 @@ function analyzeIssues(params) {
|
|
|
557
557
|
issues.push("No significant issues detected");
|
|
558
558
|
recommendations.push("File is well-structured for AI context usage");
|
|
559
559
|
}
|
|
560
|
+
if (isBuildArtifact(file)) {
|
|
561
|
+
issues.push("Detected build artifact (bundled/output file)");
|
|
562
|
+
recommendations.push("Exclude build outputs (e.g., cdk.out, dist, build, .next) from analysis");
|
|
563
|
+
severity = downgradeSeverity(severity);
|
|
564
|
+
potentialSavings = 0;
|
|
565
|
+
}
|
|
560
566
|
return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
|
|
561
567
|
}
|
|
568
|
+
function isBuildArtifact(filePath) {
|
|
569
|
+
const lower = filePath.toLowerCase();
|
|
570
|
+
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);
|
|
571
|
+
}
|
|
572
|
+
function downgradeSeverity(s) {
|
|
573
|
+
switch (s) {
|
|
574
|
+
case "critical":
|
|
575
|
+
return "minor";
|
|
576
|
+
case "major":
|
|
577
|
+
return "minor";
|
|
578
|
+
case "minor":
|
|
579
|
+
return "info";
|
|
580
|
+
default:
|
|
581
|
+
return "info";
|
|
582
|
+
}
|
|
583
|
+
}
|
|
562
584
|
|
|
563
585
|
// src/cli.ts
|
|
564
586
|
var import_chalk = __toESM(require("chalk"));
|
|
565
587
|
var import_fs = require("fs");
|
|
566
588
|
var import_path = require("path");
|
|
567
589
|
var import_core3 = require("@aiready/core");
|
|
590
|
+
var import_prompts = __toESM(require("prompts"));
|
|
568
591
|
var program = new import_commander.Command();
|
|
569
592
|
program.name("aiready-context").description("Analyze AI context window cost and code structure").version("0.1.0").addHelpText("after", "\nCONFIGURATION:\n Supports config files: aiready.json, aiready.config.json, .aiready.json, .aireadyrc.json, aiready.config.js, .aireadyrc.js\n CLI options override config file settings").argument("<directory>", "Directory to analyze").option("--max-depth <number>", "Maximum acceptable import depth").option(
|
|
570
593
|
"--max-context <number>",
|
|
@@ -579,7 +602,7 @@ program.name("aiready-context").description("Analyze AI context window cost and
|
|
|
579
602
|
"-o, --output <format>",
|
|
580
603
|
"Output format: console, json, html",
|
|
581
604
|
"console"
|
|
582
|
-
).option("--output-file <path>", "Output file path (for json/html)").action(async (directory, options) => {
|
|
605
|
+
).option("--output-file <path>", "Output file path (for json/html)").option("--interactive", "Run interactive setup to suggest excludes and focus areas").action(async (directory, options) => {
|
|
583
606
|
console.log(import_chalk.default.blue("\u{1F50D} Analyzing context window costs...\n"));
|
|
584
607
|
const startTime = Date.now();
|
|
585
608
|
try {
|
|
@@ -594,7 +617,7 @@ program.name("aiready-context").description("Analyze AI context window cost and
|
|
|
594
617
|
exclude: void 0,
|
|
595
618
|
maxResults: 10
|
|
596
619
|
};
|
|
597
|
-
|
|
620
|
+
let finalOptions = (0, import_core3.loadMergedConfig)(directory, defaults, {
|
|
598
621
|
maxDepth: options.maxDepth ? parseInt(options.maxDepth) : void 0,
|
|
599
622
|
maxContextBudget: options.maxContext ? parseInt(options.maxContext) : void 0,
|
|
600
623
|
minCohesion: options.minCohesion ? parseFloat(options.minCohesion) : void 0,
|
|
@@ -605,6 +628,9 @@ program.name("aiready-context").description("Analyze AI context window cost and
|
|
|
605
628
|
exclude: options.exclude?.split(","),
|
|
606
629
|
maxResults: options.maxResults ? parseInt(options.maxResults) : void 0
|
|
607
630
|
});
|
|
631
|
+
if (options.interactive) {
|
|
632
|
+
finalOptions = await runInteractiveSetup(directory, finalOptions);
|
|
633
|
+
}
|
|
608
634
|
const results = await analyzeContext(finalOptions);
|
|
609
635
|
const elapsedTime = (0, import_core3.getElapsedTime)(startTime);
|
|
610
636
|
const summary = generateSummary(results);
|
|
@@ -939,3 +965,55 @@ function generateHTMLReport(summary, results) {
|
|
|
939
965
|
</body>
|
|
940
966
|
</html>`;
|
|
941
967
|
}
|
|
968
|
+
async function runInteractiveSetup(directory, current) {
|
|
969
|
+
console.log(import_chalk.default.yellow("\u{1F9ED} Interactive mode: let\u2019s tailor the analysis."));
|
|
970
|
+
const pkgPath = (0, import_path.join)(directory, "package.json");
|
|
971
|
+
let deps = {};
|
|
972
|
+
if ((0, import_fs.existsSync)(pkgPath)) {
|
|
973
|
+
try {
|
|
974
|
+
const pkg = JSON.parse((0, import_fs.readFileSync)(pkgPath, "utf-8"));
|
|
975
|
+
deps = { ...pkg.dependencies || {}, ...pkg.devDependencies || {} };
|
|
976
|
+
} catch {
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
const hasNextJs = (0, import_fs.existsSync)((0, import_path.join)(directory, ".next")) || !!deps["next"];
|
|
980
|
+
const hasCDK = (0, import_fs.existsSync)((0, import_path.join)(directory, "cdk.out")) || !!deps["aws-cdk-lib"] || Object.keys(deps).some((d) => d.startsWith("@aws-cdk/"));
|
|
981
|
+
const recommendedExcludes = new Set(current.exclude || []);
|
|
982
|
+
if (hasNextJs && !Array.from(recommendedExcludes).some((p) => p.includes(".next"))) {
|
|
983
|
+
recommendedExcludes.add("**/.next/**");
|
|
984
|
+
}
|
|
985
|
+
if (hasCDK && !Array.from(recommendedExcludes).some((p) => p.includes("cdk.out"))) {
|
|
986
|
+
recommendedExcludes.add("**/cdk.out/**");
|
|
987
|
+
}
|
|
988
|
+
const { applyExcludes } = await (0, import_prompts.default)({
|
|
989
|
+
type: "toggle",
|
|
990
|
+
name: "applyExcludes",
|
|
991
|
+
message: `Detected ${hasNextJs ? "Next.js " : ""}${hasCDK ? "AWS CDK " : ""}frameworks. Apply recommended excludes?`,
|
|
992
|
+
initial: true,
|
|
993
|
+
active: "yes",
|
|
994
|
+
inactive: "no"
|
|
995
|
+
});
|
|
996
|
+
let nextOptions = { ...current };
|
|
997
|
+
if (applyExcludes) {
|
|
998
|
+
nextOptions.exclude = Array.from(recommendedExcludes);
|
|
999
|
+
}
|
|
1000
|
+
const { focusArea } = await (0, import_prompts.default)({
|
|
1001
|
+
type: "select",
|
|
1002
|
+
name: "focusArea",
|
|
1003
|
+
message: "Which areas to focus?",
|
|
1004
|
+
choices: [
|
|
1005
|
+
{ title: "Frontend (web app)", value: "frontend" },
|
|
1006
|
+
{ title: "Backend (API/infra)", value: "backend" },
|
|
1007
|
+
{ title: "Both", value: "both" }
|
|
1008
|
+
],
|
|
1009
|
+
initial: 2
|
|
1010
|
+
});
|
|
1011
|
+
if (focusArea === "frontend") {
|
|
1012
|
+
nextOptions.include = ["**/*.{ts,tsx,js,jsx}"];
|
|
1013
|
+
nextOptions.exclude = Array.from(/* @__PURE__ */ new Set([...nextOptions.exclude || [], "**/cdk.out/**", "**/infra/**", "**/server/**", "**/backend/**"]));
|
|
1014
|
+
} else if (focusArea === "backend") {
|
|
1015
|
+
nextOptions.include = ["**/api/**", "**/server/**", "**/backend/**", "**/infra/**", "**/*.{ts,js,py,java}"];
|
|
1016
|
+
}
|
|
1017
|
+
console.log(import_chalk.default.green("\u2713 Interactive configuration applied."));
|
|
1018
|
+
return nextOptions;
|
|
1019
|
+
}
|
package/dist/cli.mjs
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
import {
|
|
3
3
|
analyzeContext,
|
|
4
4
|
generateSummary
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-HDFXSEFW.mjs";
|
|
6
6
|
|
|
7
7
|
// src/cli.ts
|
|
8
8
|
import { Command } from "commander";
|
|
9
9
|
import chalk from "chalk";
|
|
10
|
-
import { writeFileSync } from "fs";
|
|
10
|
+
import { writeFileSync, existsSync, readFileSync } from "fs";
|
|
11
11
|
import { join } from "path";
|
|
12
12
|
import { loadMergedConfig, handleJSONOutput, handleCLIError, getElapsedTime } from "@aiready/core";
|
|
13
|
+
import prompts from "prompts";
|
|
13
14
|
var program = new Command();
|
|
14
15
|
program.name("aiready-context").description("Analyze AI context window cost and code structure").version("0.1.0").addHelpText("after", "\nCONFIGURATION:\n Supports config files: aiready.json, aiready.config.json, .aiready.json, .aireadyrc.json, aiready.config.js, .aireadyrc.js\n CLI options override config file settings").argument("<directory>", "Directory to analyze").option("--max-depth <number>", "Maximum acceptable import depth").option(
|
|
15
16
|
"--max-context <number>",
|
|
@@ -24,7 +25,7 @@ program.name("aiready-context").description("Analyze AI context window cost and
|
|
|
24
25
|
"-o, --output <format>",
|
|
25
26
|
"Output format: console, json, html",
|
|
26
27
|
"console"
|
|
27
|
-
).option("--output-file <path>", "Output file path (for json/html)").action(async (directory, options) => {
|
|
28
|
+
).option("--output-file <path>", "Output file path (for json/html)").option("--interactive", "Run interactive setup to suggest excludes and focus areas").action(async (directory, options) => {
|
|
28
29
|
console.log(chalk.blue("\u{1F50D} Analyzing context window costs...\n"));
|
|
29
30
|
const startTime = Date.now();
|
|
30
31
|
try {
|
|
@@ -39,7 +40,7 @@ program.name("aiready-context").description("Analyze AI context window cost and
|
|
|
39
40
|
exclude: void 0,
|
|
40
41
|
maxResults: 10
|
|
41
42
|
};
|
|
42
|
-
|
|
43
|
+
let finalOptions = loadMergedConfig(directory, defaults, {
|
|
43
44
|
maxDepth: options.maxDepth ? parseInt(options.maxDepth) : void 0,
|
|
44
45
|
maxContextBudget: options.maxContext ? parseInt(options.maxContext) : void 0,
|
|
45
46
|
minCohesion: options.minCohesion ? parseFloat(options.minCohesion) : void 0,
|
|
@@ -50,6 +51,9 @@ program.name("aiready-context").description("Analyze AI context window cost and
|
|
|
50
51
|
exclude: options.exclude?.split(","),
|
|
51
52
|
maxResults: options.maxResults ? parseInt(options.maxResults) : void 0
|
|
52
53
|
});
|
|
54
|
+
if (options.interactive) {
|
|
55
|
+
finalOptions = await runInteractiveSetup(directory, finalOptions);
|
|
56
|
+
}
|
|
53
57
|
const results = await analyzeContext(finalOptions);
|
|
54
58
|
const elapsedTime = getElapsedTime(startTime);
|
|
55
59
|
const summary = generateSummary(results);
|
|
@@ -384,3 +388,55 @@ function generateHTMLReport(summary, results) {
|
|
|
384
388
|
</body>
|
|
385
389
|
</html>`;
|
|
386
390
|
}
|
|
391
|
+
async function runInteractiveSetup(directory, current) {
|
|
392
|
+
console.log(chalk.yellow("\u{1F9ED} Interactive mode: let\u2019s tailor the analysis."));
|
|
393
|
+
const pkgPath = join(directory, "package.json");
|
|
394
|
+
let deps = {};
|
|
395
|
+
if (existsSync(pkgPath)) {
|
|
396
|
+
try {
|
|
397
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
398
|
+
deps = { ...pkg.dependencies || {}, ...pkg.devDependencies || {} };
|
|
399
|
+
} catch {
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const hasNextJs = existsSync(join(directory, ".next")) || !!deps["next"];
|
|
403
|
+
const hasCDK = existsSync(join(directory, "cdk.out")) || !!deps["aws-cdk-lib"] || Object.keys(deps).some((d) => d.startsWith("@aws-cdk/"));
|
|
404
|
+
const recommendedExcludes = new Set(current.exclude || []);
|
|
405
|
+
if (hasNextJs && !Array.from(recommendedExcludes).some((p) => p.includes(".next"))) {
|
|
406
|
+
recommendedExcludes.add("**/.next/**");
|
|
407
|
+
}
|
|
408
|
+
if (hasCDK && !Array.from(recommendedExcludes).some((p) => p.includes("cdk.out"))) {
|
|
409
|
+
recommendedExcludes.add("**/cdk.out/**");
|
|
410
|
+
}
|
|
411
|
+
const { applyExcludes } = await prompts({
|
|
412
|
+
type: "toggle",
|
|
413
|
+
name: "applyExcludes",
|
|
414
|
+
message: `Detected ${hasNextJs ? "Next.js " : ""}${hasCDK ? "AWS CDK " : ""}frameworks. Apply recommended excludes?`,
|
|
415
|
+
initial: true,
|
|
416
|
+
active: "yes",
|
|
417
|
+
inactive: "no"
|
|
418
|
+
});
|
|
419
|
+
let nextOptions = { ...current };
|
|
420
|
+
if (applyExcludes) {
|
|
421
|
+
nextOptions.exclude = Array.from(recommendedExcludes);
|
|
422
|
+
}
|
|
423
|
+
const { focusArea } = await prompts({
|
|
424
|
+
type: "select",
|
|
425
|
+
name: "focusArea",
|
|
426
|
+
message: "Which areas to focus?",
|
|
427
|
+
choices: [
|
|
428
|
+
{ title: "Frontend (web app)", value: "frontend" },
|
|
429
|
+
{ title: "Backend (API/infra)", value: "backend" },
|
|
430
|
+
{ title: "Both", value: "both" }
|
|
431
|
+
],
|
|
432
|
+
initial: 2
|
|
433
|
+
});
|
|
434
|
+
if (focusArea === "frontend") {
|
|
435
|
+
nextOptions.include = ["**/*.{ts,tsx,js,jsx}"];
|
|
436
|
+
nextOptions.exclude = Array.from(/* @__PURE__ */ new Set([...nextOptions.exclude || [], "**/cdk.out/**", "**/infra/**", "**/server/**", "**/backend/**"]));
|
|
437
|
+
} else if (focusArea === "backend") {
|
|
438
|
+
nextOptions.include = ["**/api/**", "**/server/**", "**/backend/**", "**/infra/**", "**/*.{ts,js,py,java}"];
|
|
439
|
+
}
|
|
440
|
+
console.log(chalk.green("\u2713 Interactive configuration applied."));
|
|
441
|
+
return nextOptions;
|
|
442
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -599,8 +599,30 @@ function analyzeIssues(params) {
|
|
|
599
599
|
issues.push("No significant issues detected");
|
|
600
600
|
recommendations.push("File is well-structured for AI context usage");
|
|
601
601
|
}
|
|
602
|
+
if (isBuildArtifact(file)) {
|
|
603
|
+
issues.push("Detected build artifact (bundled/output file)");
|
|
604
|
+
recommendations.push("Exclude build outputs (e.g., cdk.out, dist, build, .next) from analysis");
|
|
605
|
+
severity = downgradeSeverity(severity);
|
|
606
|
+
potentialSavings = 0;
|
|
607
|
+
}
|
|
602
608
|
return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
|
|
603
609
|
}
|
|
610
|
+
function isBuildArtifact(filePath) {
|
|
611
|
+
const lower = filePath.toLowerCase();
|
|
612
|
+
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);
|
|
613
|
+
}
|
|
614
|
+
function downgradeSeverity(s) {
|
|
615
|
+
switch (s) {
|
|
616
|
+
case "critical":
|
|
617
|
+
return "minor";
|
|
618
|
+
case "major":
|
|
619
|
+
return "minor";
|
|
620
|
+
case "minor":
|
|
621
|
+
return "info";
|
|
622
|
+
default:
|
|
623
|
+
return "info";
|
|
624
|
+
}
|
|
625
|
+
}
|
|
604
626
|
// Annotate the CommonJS export names for ESM import in node:
|
|
605
627
|
0 && (module.exports = {
|
|
606
628
|
analyzeContext,
|
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiready/context-analyzer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "AI context window cost analysis - detect fragmented code, deep import chains, and expensive context budgets",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -49,10 +49,12 @@
|
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"commander": "^12.1.0",
|
|
51
51
|
"chalk": "^5.3.0",
|
|
52
|
-
"
|
|
52
|
+
"prompts": "^2.4.2",
|
|
53
|
+
"@aiready/core": "0.5.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
56
|
"@types/node": "^22.10.2",
|
|
57
|
+
"@types/prompts": "^2.4.9",
|
|
56
58
|
"tsup": "^8.3.5",
|
|
57
59
|
"typescript": "^5.7.3",
|
|
58
60
|
"vitest": "^2.1.9",
|
package/src/cli.ts
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
import { Command } from 'commander';
|
|
4
4
|
import { analyzeContext, generateSummary } from './index';
|
|
5
5
|
import chalk from 'chalk';
|
|
6
|
-
import { writeFileSync } from 'fs';
|
|
6
|
+
import { writeFileSync, existsSync, readFileSync } from 'fs';
|
|
7
7
|
import { join } from 'path';
|
|
8
8
|
import { loadMergedConfig, handleJSONOutput, handleCLIError, getElapsedTime } from '@aiready/core';
|
|
9
|
+
import prompts from 'prompts';
|
|
9
10
|
|
|
10
11
|
const program = new Command();
|
|
11
12
|
|
|
@@ -39,6 +40,7 @@ program
|
|
|
39
40
|
'console'
|
|
40
41
|
)
|
|
41
42
|
.option('--output-file <path>', 'Output file path (for json/html)')
|
|
43
|
+
.option('--interactive', 'Run interactive setup to suggest excludes and focus areas')
|
|
42
44
|
.action(async (directory, options) => {
|
|
43
45
|
console.log(chalk.blue('🔍 Analyzing context window costs...\n'));
|
|
44
46
|
|
|
@@ -59,7 +61,7 @@ program
|
|
|
59
61
|
};
|
|
60
62
|
|
|
61
63
|
// Load and merge config with CLI options
|
|
62
|
-
|
|
64
|
+
let finalOptions = loadMergedConfig(directory, defaults, {
|
|
63
65
|
maxDepth: options.maxDepth ? parseInt(options.maxDepth) : undefined,
|
|
64
66
|
maxContextBudget: options.maxContext ? parseInt(options.maxContext) : undefined,
|
|
65
67
|
minCohesion: options.minCohesion ? parseFloat(options.minCohesion) : undefined,
|
|
@@ -71,6 +73,11 @@ program
|
|
|
71
73
|
maxResults: options.maxResults ? parseInt(options.maxResults) : undefined,
|
|
72
74
|
}) as any;
|
|
73
75
|
|
|
76
|
+
// Optional: interactive setup to refine options for first-time users
|
|
77
|
+
if (options.interactive) {
|
|
78
|
+
finalOptions = await runInteractiveSetup(directory, finalOptions);
|
|
79
|
+
}
|
|
80
|
+
|
|
74
81
|
const results = await analyzeContext(finalOptions);
|
|
75
82
|
|
|
76
83
|
const elapsedTime = getElapsedTime(startTime);
|
|
@@ -455,3 +462,66 @@ function generateHTMLReport(
|
|
|
455
462
|
</body>
|
|
456
463
|
</html>`;
|
|
457
464
|
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Interactive setup: detect common frameworks and suggest excludes & focus areas
|
|
468
|
+
*/
|
|
469
|
+
async function runInteractiveSetup(directory: string, current: any): Promise<any> {
|
|
470
|
+
console.log(chalk.yellow('🧭 Interactive mode: let’s tailor the analysis.'));
|
|
471
|
+
|
|
472
|
+
const pkgPath = join(directory, 'package.json');
|
|
473
|
+
let deps: Record<string, string> = {};
|
|
474
|
+
if (existsSync(pkgPath)) {
|
|
475
|
+
try {
|
|
476
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
|
|
477
|
+
deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
|
|
478
|
+
} catch {}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const hasNextJs = existsSync(join(directory, '.next')) || !!deps['next'];
|
|
482
|
+
const hasCDK = existsSync(join(directory, 'cdk.out')) || !!deps['aws-cdk-lib'] || Object.keys(deps).some(d => d.startsWith('@aws-cdk/'));
|
|
483
|
+
|
|
484
|
+
const recommendedExcludes = new Set<string>(current.exclude || []);
|
|
485
|
+
if (hasNextJs && !Array.from(recommendedExcludes).some((p) => p.includes('.next'))) {
|
|
486
|
+
recommendedExcludes.add('**/.next/**');
|
|
487
|
+
}
|
|
488
|
+
if (hasCDK && !Array.from(recommendedExcludes).some((p) => p.includes('cdk.out'))) {
|
|
489
|
+
recommendedExcludes.add('**/cdk.out/**');
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const { applyExcludes } = await prompts({
|
|
493
|
+
type: 'toggle',
|
|
494
|
+
name: 'applyExcludes',
|
|
495
|
+
message: `Detected ${hasNextJs ? 'Next.js ' : ''}${hasCDK ? 'AWS CDK ' : ''}frameworks. Apply recommended excludes?`,
|
|
496
|
+
initial: true,
|
|
497
|
+
active: 'yes',
|
|
498
|
+
inactive: 'no',
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
let nextOptions = { ...current };
|
|
502
|
+
if (applyExcludes) {
|
|
503
|
+
nextOptions.exclude = Array.from(recommendedExcludes);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const { focusArea } = await prompts({
|
|
507
|
+
type: 'select',
|
|
508
|
+
name: 'focusArea',
|
|
509
|
+
message: 'Which areas to focus?',
|
|
510
|
+
choices: [
|
|
511
|
+
{ title: 'Frontend (web app)', value: 'frontend' },
|
|
512
|
+
{ title: 'Backend (API/infra)', value: 'backend' },
|
|
513
|
+
{ title: 'Both', value: 'both' },
|
|
514
|
+
],
|
|
515
|
+
initial: 2,
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
if (focusArea === 'frontend') {
|
|
519
|
+
nextOptions.include = ['**/*.{ts,tsx,js,jsx}'];
|
|
520
|
+
nextOptions.exclude = Array.from(new Set([...(nextOptions.exclude || []), '**/cdk.out/**', '**/infra/**', '**/server/**', '**/backend/**']));
|
|
521
|
+
} else if (focusArea === 'backend') {
|
|
522
|
+
nextOptions.include = ['**/api/**', '**/server/**', '**/backend/**', '**/infra/**', '**/*.{ts,js,py,java}'];
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
console.log(chalk.green('✓ Interactive configuration applied.'));
|
|
526
|
+
return nextOptions;
|
|
527
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -453,7 +453,46 @@ function analyzeIssues(params: {
|
|
|
453
453
|
recommendations.push('File is well-structured for AI context usage');
|
|
454
454
|
}
|
|
455
455
|
|
|
456
|
+
// Detect build artifacts and downgrade severity to reduce noise
|
|
457
|
+
if (isBuildArtifact(file)) {
|
|
458
|
+
issues.push('Detected build artifact (bundled/output file)');
|
|
459
|
+
recommendations.push('Exclude build outputs (e.g., cdk.out, dist, build, .next) from analysis');
|
|
460
|
+
severity = downgradeSeverity(severity);
|
|
461
|
+
// Build artifacts do not represent actionable savings
|
|
462
|
+
potentialSavings = 0;
|
|
463
|
+
}
|
|
464
|
+
|
|
456
465
|
return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
|
|
457
466
|
}
|
|
458
467
|
|
|
459
468
|
export { getSmartDefaults };
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Heuristic: identify common build artifact paths
|
|
472
|
+
*/
|
|
473
|
+
function isBuildArtifact(filePath: string): boolean {
|
|
474
|
+
const lower = filePath.toLowerCase();
|
|
475
|
+
return (
|
|
476
|
+
lower.includes('/node_modules/') ||
|
|
477
|
+
lower.includes('/dist/') ||
|
|
478
|
+
lower.includes('/build/') ||
|
|
479
|
+
lower.includes('/out/') ||
|
|
480
|
+
lower.includes('/output/') ||
|
|
481
|
+
lower.includes('/cdk.out/') ||
|
|
482
|
+
lower.includes('/.next/') ||
|
|
483
|
+
/\/asset\.[^/]+\//.test(lower) // e.g., cdk.out/asset.*
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function downgradeSeverity(s: ContextAnalysisResult['severity']): ContextAnalysisResult['severity'] {
|
|
488
|
+
switch (s) {
|
|
489
|
+
case 'critical':
|
|
490
|
+
return 'minor';
|
|
491
|
+
case 'major':
|
|
492
|
+
return 'minor';
|
|
493
|
+
case 'minor':
|
|
494
|
+
return 'info';
|
|
495
|
+
default:
|
|
496
|
+
return 'info';
|
|
497
|
+
}
|
|
498
|
+
}
|