@aiready/context-analyzer 0.1.0 → 0.1.2
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 +7 -7
- package/.turbo/turbo-test.log +36 -0
- package/README.md +47 -0
- package/dist/chunk-72QC5QUS.mjs +549 -0
- package/dist/chunk-EH3PMNZQ.mjs +569 -0
- package/dist/chunk-N6XBOOVA.mjs +564 -0
- package/dist/cli.js +24 -10
- package/dist/cli.mjs +24 -10
- package/package.json +2 -2
- package/src/__tests__/analyzer.test.ts +9 -9
- package/src/cli.ts +32 -10
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { scanFiles, readFileContent } from "@aiready/core";
|
|
3
|
+
|
|
4
|
+
// src/analyzer.ts
|
|
5
|
+
import { estimateTokens } from "@aiready/core";
|
|
6
|
+
function resolveImportPath(importPath, fromFile, nodes) {
|
|
7
|
+
if (!importPath.startsWith("./")) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const relativePath = importPath.slice(2);
|
|
11
|
+
const resolvedPath = relativePath.endsWith(".ts") ? relativePath : `${relativePath}.ts`;
|
|
12
|
+
if (nodes.has(resolvedPath)) {
|
|
13
|
+
return resolvedPath;
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
function buildDependencyGraph(files) {
|
|
18
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
19
|
+
const edges = /* @__PURE__ */ new Map();
|
|
20
|
+
for (const { file, content } of files) {
|
|
21
|
+
const imports = extractImportsFromContent(content);
|
|
22
|
+
const exports = extractExports(content);
|
|
23
|
+
const tokenCost = estimateTokens(content);
|
|
24
|
+
const linesOfCode = content.split("\n").length;
|
|
25
|
+
nodes.set(file, {
|
|
26
|
+
file,
|
|
27
|
+
imports,
|
|
28
|
+
exports,
|
|
29
|
+
tokenCost,
|
|
30
|
+
linesOfCode
|
|
31
|
+
});
|
|
32
|
+
edges.set(file, new Set(imports));
|
|
33
|
+
}
|
|
34
|
+
return { nodes, edges };
|
|
35
|
+
}
|
|
36
|
+
function extractImportsFromContent(content) {
|
|
37
|
+
const imports = [];
|
|
38
|
+
const patterns = [
|
|
39
|
+
/import\s+.*?\s+from\s+['"](.+?)['"]/g,
|
|
40
|
+
// import ... from '...'
|
|
41
|
+
/import\s+['"](.+?)['"]/g,
|
|
42
|
+
// import '...'
|
|
43
|
+
/require\(['"](.+?)['"]\)/g
|
|
44
|
+
// require('...')
|
|
45
|
+
];
|
|
46
|
+
for (const pattern of patterns) {
|
|
47
|
+
let match;
|
|
48
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
49
|
+
const importPath = match[1];
|
|
50
|
+
if (importPath && !importPath.startsWith("@") && !importPath.startsWith("node:")) {
|
|
51
|
+
imports.push(importPath);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return [...new Set(imports)];
|
|
56
|
+
}
|
|
57
|
+
function calculateImportDepth(file, graph, visited = /* @__PURE__ */ new Set()) {
|
|
58
|
+
if (visited.has(file)) {
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
const dependencies = graph.edges.get(file);
|
|
62
|
+
if (!dependencies || dependencies.size === 0) {
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
visited.add(file);
|
|
66
|
+
let maxDepth = 0;
|
|
67
|
+
for (const dep of dependencies) {
|
|
68
|
+
const resolvedDep = resolveImportPath(dep, file, graph.nodes);
|
|
69
|
+
if (resolvedDep) {
|
|
70
|
+
const depDepth = calculateImportDepth(resolvedDep, graph, visited);
|
|
71
|
+
maxDepth = Math.max(maxDepth, depDepth + 1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
visited.delete(file);
|
|
75
|
+
return maxDepth;
|
|
76
|
+
}
|
|
77
|
+
function getTransitiveDependencies(file, graph, visited = /* @__PURE__ */ new Set(), collected = /* @__PURE__ */ new Set()) {
|
|
78
|
+
if (visited.has(file)) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
visited.add(file);
|
|
82
|
+
const dependencies = graph.edges.get(file);
|
|
83
|
+
if (!dependencies || dependencies.size === 0) {
|
|
84
|
+
visited.delete(file);
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
const allDeps = [];
|
|
88
|
+
for (const dep of dependencies) {
|
|
89
|
+
if (!collected.has(dep)) {
|
|
90
|
+
collected.add(dep);
|
|
91
|
+
allDeps.push(dep);
|
|
92
|
+
}
|
|
93
|
+
const resolvedDep = resolveImportPath(dep, file, graph.nodes);
|
|
94
|
+
if (resolvedDep) {
|
|
95
|
+
const transitiveDeps = getTransitiveDependencies(resolvedDep, graph, visited, collected);
|
|
96
|
+
allDeps.push(...transitiveDeps);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
visited.delete(file);
|
|
100
|
+
return allDeps;
|
|
101
|
+
}
|
|
102
|
+
function calculateContextBudget(file, graph) {
|
|
103
|
+
const node = graph.nodes.get(file);
|
|
104
|
+
if (!node) return 0;
|
|
105
|
+
let totalTokens = node.tokenCost;
|
|
106
|
+
const deps = getTransitiveDependencies(file, graph);
|
|
107
|
+
for (const dep of deps) {
|
|
108
|
+
const depNode = graph.nodes.get(dep);
|
|
109
|
+
if (depNode) {
|
|
110
|
+
totalTokens += depNode.tokenCost;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return totalTokens;
|
|
114
|
+
}
|
|
115
|
+
function detectCircularDependencies(graph) {
|
|
116
|
+
const cycles = [];
|
|
117
|
+
const visited = /* @__PURE__ */ new Set();
|
|
118
|
+
const recursionStack = /* @__PURE__ */ new Set();
|
|
119
|
+
function dfs(file, path) {
|
|
120
|
+
if (recursionStack.has(file)) {
|
|
121
|
+
const cycleStart = path.indexOf(file);
|
|
122
|
+
if (cycleStart !== -1) {
|
|
123
|
+
cycles.push([...path.slice(cycleStart), file]);
|
|
124
|
+
}
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (visited.has(file)) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
visited.add(file);
|
|
131
|
+
recursionStack.add(file);
|
|
132
|
+
path.push(file);
|
|
133
|
+
const dependencies = graph.edges.get(file);
|
|
134
|
+
if (dependencies) {
|
|
135
|
+
for (const dep of dependencies) {
|
|
136
|
+
const resolvedDep = resolveImportPath(dep, file, graph.nodes);
|
|
137
|
+
if (resolvedDep) {
|
|
138
|
+
dfs(resolvedDep, [...path]);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
recursionStack.delete(file);
|
|
143
|
+
}
|
|
144
|
+
for (const file of graph.nodes.keys()) {
|
|
145
|
+
if (!visited.has(file)) {
|
|
146
|
+
dfs(file, []);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return cycles;
|
|
150
|
+
}
|
|
151
|
+
function calculateCohesion(exports) {
|
|
152
|
+
if (exports.length === 0) return 1;
|
|
153
|
+
if (exports.length === 1) return 1;
|
|
154
|
+
const domains = exports.map((e) => e.inferredDomain || "unknown");
|
|
155
|
+
const domainCounts = /* @__PURE__ */ new Map();
|
|
156
|
+
for (const domain of domains) {
|
|
157
|
+
domainCounts.set(domain, (domainCounts.get(domain) || 0) + 1);
|
|
158
|
+
}
|
|
159
|
+
const total = domains.length;
|
|
160
|
+
let entropy = 0;
|
|
161
|
+
for (const count of domainCounts.values()) {
|
|
162
|
+
const p = count / total;
|
|
163
|
+
if (p > 0) {
|
|
164
|
+
entropy -= p * Math.log2(p);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const maxEntropy = Math.log2(total);
|
|
168
|
+
return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
|
|
169
|
+
}
|
|
170
|
+
function calculateFragmentation(files, domain) {
|
|
171
|
+
if (files.length <= 1) return 0;
|
|
172
|
+
const directories = new Set(files.map((f) => f.split("/").slice(0, -1).join("/")));
|
|
173
|
+
return (directories.size - 1) / (files.length - 1);
|
|
174
|
+
}
|
|
175
|
+
function detectModuleClusters(graph) {
|
|
176
|
+
const domainMap = /* @__PURE__ */ new Map();
|
|
177
|
+
for (const [file, node] of graph.nodes.entries()) {
|
|
178
|
+
const domains = node.exports.map((e) => e.inferredDomain || "unknown");
|
|
179
|
+
const primaryDomain = domains[0] || "unknown";
|
|
180
|
+
if (!domainMap.has(primaryDomain)) {
|
|
181
|
+
domainMap.set(primaryDomain, []);
|
|
182
|
+
}
|
|
183
|
+
domainMap.get(primaryDomain).push(file);
|
|
184
|
+
}
|
|
185
|
+
const clusters = [];
|
|
186
|
+
for (const [domain, files] of domainMap.entries()) {
|
|
187
|
+
if (files.length < 2) continue;
|
|
188
|
+
const totalTokens = files.reduce((sum, file) => {
|
|
189
|
+
const node = graph.nodes.get(file);
|
|
190
|
+
return sum + (node?.tokenCost || 0);
|
|
191
|
+
}, 0);
|
|
192
|
+
const fragmentationScore = calculateFragmentation(files, domain);
|
|
193
|
+
const avgCohesion = files.reduce((sum, file) => {
|
|
194
|
+
const node = graph.nodes.get(file);
|
|
195
|
+
return sum + (node ? calculateCohesion(node.exports) : 0);
|
|
196
|
+
}, 0) / files.length;
|
|
197
|
+
const targetFiles = Math.max(1, Math.ceil(files.length / 3));
|
|
198
|
+
const consolidationPlan = generateConsolidationPlan(
|
|
199
|
+
domain,
|
|
200
|
+
files,
|
|
201
|
+
targetFiles
|
|
202
|
+
);
|
|
203
|
+
clusters.push({
|
|
204
|
+
domain,
|
|
205
|
+
files,
|
|
206
|
+
totalTokens,
|
|
207
|
+
fragmentationScore,
|
|
208
|
+
avgCohesion,
|
|
209
|
+
suggestedStructure: {
|
|
210
|
+
targetFiles,
|
|
211
|
+
consolidationPlan
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return clusters.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
|
|
216
|
+
}
|
|
217
|
+
function extractExports(content) {
|
|
218
|
+
const exports = [];
|
|
219
|
+
const patterns = [
|
|
220
|
+
/export\s+function\s+(\w+)/g,
|
|
221
|
+
/export\s+class\s+(\w+)/g,
|
|
222
|
+
/export\s+const\s+(\w+)/g,
|
|
223
|
+
/export\s+type\s+(\w+)/g,
|
|
224
|
+
/export\s+interface\s+(\w+)/g,
|
|
225
|
+
/export\s+default/g
|
|
226
|
+
];
|
|
227
|
+
const types = [
|
|
228
|
+
"function",
|
|
229
|
+
"class",
|
|
230
|
+
"const",
|
|
231
|
+
"type",
|
|
232
|
+
"interface",
|
|
233
|
+
"default"
|
|
234
|
+
];
|
|
235
|
+
patterns.forEach((pattern, index) => {
|
|
236
|
+
let match;
|
|
237
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
238
|
+
const name = match[1] || "default";
|
|
239
|
+
const type = types[index];
|
|
240
|
+
const inferredDomain = inferDomain(name);
|
|
241
|
+
exports.push({ name, type, inferredDomain });
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
return exports;
|
|
245
|
+
}
|
|
246
|
+
function inferDomain(name) {
|
|
247
|
+
const lower = name.toLowerCase();
|
|
248
|
+
const domainKeywords = [
|
|
249
|
+
"user",
|
|
250
|
+
"auth",
|
|
251
|
+
"order",
|
|
252
|
+
"product",
|
|
253
|
+
"payment",
|
|
254
|
+
"cart",
|
|
255
|
+
"invoice",
|
|
256
|
+
"customer",
|
|
257
|
+
"admin",
|
|
258
|
+
"api",
|
|
259
|
+
"util",
|
|
260
|
+
"helper",
|
|
261
|
+
"config",
|
|
262
|
+
"service",
|
|
263
|
+
"repository",
|
|
264
|
+
"controller",
|
|
265
|
+
"model",
|
|
266
|
+
"view"
|
|
267
|
+
];
|
|
268
|
+
for (const keyword of domainKeywords) {
|
|
269
|
+
if (lower.includes(keyword)) {
|
|
270
|
+
return keyword;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return "unknown";
|
|
274
|
+
}
|
|
275
|
+
function generateConsolidationPlan(domain, files, targetFiles) {
|
|
276
|
+
const plan = [];
|
|
277
|
+
if (files.length <= targetFiles) {
|
|
278
|
+
return [`No consolidation needed for ${domain}`];
|
|
279
|
+
}
|
|
280
|
+
plan.push(
|
|
281
|
+
`Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s):`
|
|
282
|
+
);
|
|
283
|
+
const dirGroups = /* @__PURE__ */ new Map();
|
|
284
|
+
for (const file of files) {
|
|
285
|
+
const dir = file.split("/").slice(0, -1).join("/");
|
|
286
|
+
if (!dirGroups.has(dir)) {
|
|
287
|
+
dirGroups.set(dir, []);
|
|
288
|
+
}
|
|
289
|
+
dirGroups.get(dir).push(file);
|
|
290
|
+
}
|
|
291
|
+
plan.push(`1. Create unified ${domain} module file`);
|
|
292
|
+
plan.push(
|
|
293
|
+
`2. Move related functionality from ${files.length} scattered files`
|
|
294
|
+
);
|
|
295
|
+
plan.push(`3. Update imports in dependent files`);
|
|
296
|
+
plan.push(
|
|
297
|
+
`4. Remove old files after consolidation (verify with tests first)`
|
|
298
|
+
);
|
|
299
|
+
return plan;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/index.ts
|
|
303
|
+
async function analyzeContext(options) {
|
|
304
|
+
const {
|
|
305
|
+
maxDepth = 5,
|
|
306
|
+
maxContextBudget = 1e4,
|
|
307
|
+
minCohesion = 0.6,
|
|
308
|
+
maxFragmentation = 0.5,
|
|
309
|
+
focus = "all",
|
|
310
|
+
includeNodeModules = false,
|
|
311
|
+
...scanOptions
|
|
312
|
+
} = options;
|
|
313
|
+
const files = await scanFiles({
|
|
314
|
+
...scanOptions,
|
|
315
|
+
exclude: includeNodeModules ? scanOptions.exclude : [...scanOptions.exclude || [], "**/node_modules/**"]
|
|
316
|
+
});
|
|
317
|
+
const fileContents = await Promise.all(
|
|
318
|
+
files.map(async (file) => ({
|
|
319
|
+
file,
|
|
320
|
+
content: await readFileContent(file)
|
|
321
|
+
}))
|
|
322
|
+
);
|
|
323
|
+
const graph = buildDependencyGraph(fileContents);
|
|
324
|
+
const circularDeps = detectCircularDependencies(graph);
|
|
325
|
+
const clusters = detectModuleClusters(graph);
|
|
326
|
+
const fragmentationMap = /* @__PURE__ */ new Map();
|
|
327
|
+
for (const cluster of clusters) {
|
|
328
|
+
for (const file of cluster.files) {
|
|
329
|
+
fragmentationMap.set(file, cluster.fragmentationScore);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const results = [];
|
|
333
|
+
for (const { file } of fileContents) {
|
|
334
|
+
const node = graph.nodes.get(file);
|
|
335
|
+
if (!node) continue;
|
|
336
|
+
const importDepth = focus === "depth" || focus === "all" ? calculateImportDepth(file, graph) : 0;
|
|
337
|
+
const dependencyList = focus === "depth" || focus === "all" ? getTransitiveDependencies(file, graph) : [];
|
|
338
|
+
const contextBudget = focus === "all" ? calculateContextBudget(file, graph) : node.tokenCost;
|
|
339
|
+
const cohesionScore = focus === "cohesion" || focus === "all" ? calculateCohesion(node.exports) : 1;
|
|
340
|
+
const fragmentationScore = fragmentationMap.get(file) || 0;
|
|
341
|
+
const relatedFiles = [];
|
|
342
|
+
for (const cluster of clusters) {
|
|
343
|
+
if (cluster.files.includes(file)) {
|
|
344
|
+
relatedFiles.push(...cluster.files.filter((f) => f !== file));
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
|
|
349
|
+
file,
|
|
350
|
+
importDepth,
|
|
351
|
+
contextBudget,
|
|
352
|
+
cohesionScore,
|
|
353
|
+
fragmentationScore,
|
|
354
|
+
maxDepth,
|
|
355
|
+
maxContextBudget,
|
|
356
|
+
minCohesion,
|
|
357
|
+
maxFragmentation,
|
|
358
|
+
circularDeps
|
|
359
|
+
});
|
|
360
|
+
const domains = [
|
|
361
|
+
...new Set(node.exports.map((e) => e.inferredDomain || "unknown"))
|
|
362
|
+
];
|
|
363
|
+
results.push({
|
|
364
|
+
file,
|
|
365
|
+
tokenCost: node.tokenCost,
|
|
366
|
+
linesOfCode: node.linesOfCode,
|
|
367
|
+
importDepth,
|
|
368
|
+
dependencyCount: dependencyList.length,
|
|
369
|
+
dependencyList,
|
|
370
|
+
circularDeps: circularDeps.filter((cycle) => cycle.includes(file)),
|
|
371
|
+
cohesionScore,
|
|
372
|
+
domains,
|
|
373
|
+
exportCount: node.exports.length,
|
|
374
|
+
contextBudget,
|
|
375
|
+
fragmentationScore,
|
|
376
|
+
relatedFiles,
|
|
377
|
+
severity,
|
|
378
|
+
issues,
|
|
379
|
+
recommendations,
|
|
380
|
+
potentialSavings
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
return results.sort((a, b) => {
|
|
384
|
+
const severityOrder = { critical: 0, major: 1, minor: 2, info: 3 };
|
|
385
|
+
const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
|
|
386
|
+
if (severityDiff !== 0) return severityDiff;
|
|
387
|
+
return b.contextBudget - a.contextBudget;
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
function generateSummary(results) {
|
|
391
|
+
if (results.length === 0) {
|
|
392
|
+
return {
|
|
393
|
+
totalFiles: 0,
|
|
394
|
+
totalTokens: 0,
|
|
395
|
+
avgContextBudget: 0,
|
|
396
|
+
maxContextBudget: 0,
|
|
397
|
+
avgImportDepth: 0,
|
|
398
|
+
maxImportDepth: 0,
|
|
399
|
+
deepFiles: [],
|
|
400
|
+
avgFragmentation: 0,
|
|
401
|
+
fragmentedModules: [],
|
|
402
|
+
avgCohesion: 0,
|
|
403
|
+
lowCohesionFiles: [],
|
|
404
|
+
criticalIssues: 0,
|
|
405
|
+
majorIssues: 0,
|
|
406
|
+
minorIssues: 0,
|
|
407
|
+
totalPotentialSavings: 0,
|
|
408
|
+
topExpensiveFiles: []
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
const totalFiles = results.length;
|
|
412
|
+
const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
|
|
413
|
+
const totalContextBudget = results.reduce(
|
|
414
|
+
(sum, r) => sum + r.contextBudget,
|
|
415
|
+
0
|
|
416
|
+
);
|
|
417
|
+
const avgContextBudget = totalContextBudget / totalFiles;
|
|
418
|
+
const maxContextBudget = Math.max(...results.map((r) => r.contextBudget));
|
|
419
|
+
const avgImportDepth = results.reduce((sum, r) => sum + r.importDepth, 0) / totalFiles;
|
|
420
|
+
const maxImportDepth = Math.max(...results.map((r) => r.importDepth));
|
|
421
|
+
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);
|
|
422
|
+
const avgFragmentation = results.reduce((sum, r) => sum + r.fragmentationScore, 0) / totalFiles;
|
|
423
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
424
|
+
for (const result of results) {
|
|
425
|
+
for (const domain of result.domains) {
|
|
426
|
+
if (!moduleMap.has(domain)) {
|
|
427
|
+
moduleMap.set(domain, []);
|
|
428
|
+
}
|
|
429
|
+
moduleMap.get(domain).push(result);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const fragmentedModules = [];
|
|
433
|
+
for (const [domain, files] of moduleMap.entries()) {
|
|
434
|
+
if (files.length < 2) continue;
|
|
435
|
+
const fragmentationScore = files.reduce((sum, f) => sum + f.fragmentationScore, 0) / files.length;
|
|
436
|
+
if (fragmentationScore < 0.3) continue;
|
|
437
|
+
const totalTokens2 = files.reduce((sum, f) => sum + f.tokenCost, 0);
|
|
438
|
+
const avgCohesion2 = files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length;
|
|
439
|
+
const targetFiles = Math.max(1, Math.ceil(files.length / 3));
|
|
440
|
+
fragmentedModules.push({
|
|
441
|
+
domain,
|
|
442
|
+
files: files.map((f) => f.file),
|
|
443
|
+
totalTokens: totalTokens2,
|
|
444
|
+
fragmentationScore,
|
|
445
|
+
avgCohesion: avgCohesion2,
|
|
446
|
+
suggestedStructure: {
|
|
447
|
+
targetFiles,
|
|
448
|
+
consolidationPlan: [
|
|
449
|
+
`Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s)`,
|
|
450
|
+
`Current token cost: ${totalTokens2.toLocaleString()}`,
|
|
451
|
+
`Estimated savings: ${Math.floor(totalTokens2 * 0.3).toLocaleString()} tokens (30%)`
|
|
452
|
+
]
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
|
|
457
|
+
const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / totalFiles;
|
|
458
|
+
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);
|
|
459
|
+
const criticalIssues = results.filter((r) => r.severity === "critical").length;
|
|
460
|
+
const majorIssues = results.filter((r) => r.severity === "major").length;
|
|
461
|
+
const minorIssues = results.filter((r) => r.severity === "minor").length;
|
|
462
|
+
const totalPotentialSavings = results.reduce(
|
|
463
|
+
(sum, r) => sum + r.potentialSavings,
|
|
464
|
+
0
|
|
465
|
+
);
|
|
466
|
+
const topExpensiveFiles = results.sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
|
|
467
|
+
file: r.file,
|
|
468
|
+
contextBudget: r.contextBudget,
|
|
469
|
+
severity: r.severity
|
|
470
|
+
}));
|
|
471
|
+
return {
|
|
472
|
+
totalFiles,
|
|
473
|
+
totalTokens,
|
|
474
|
+
avgContextBudget,
|
|
475
|
+
maxContextBudget,
|
|
476
|
+
avgImportDepth,
|
|
477
|
+
maxImportDepth,
|
|
478
|
+
deepFiles,
|
|
479
|
+
avgFragmentation,
|
|
480
|
+
fragmentedModules: fragmentedModules.slice(0, 10),
|
|
481
|
+
avgCohesion,
|
|
482
|
+
lowCohesionFiles,
|
|
483
|
+
criticalIssues,
|
|
484
|
+
majorIssues,
|
|
485
|
+
minorIssues,
|
|
486
|
+
totalPotentialSavings,
|
|
487
|
+
topExpensiveFiles
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
function analyzeIssues(params) {
|
|
491
|
+
const {
|
|
492
|
+
file,
|
|
493
|
+
importDepth,
|
|
494
|
+
contextBudget,
|
|
495
|
+
cohesionScore,
|
|
496
|
+
fragmentationScore,
|
|
497
|
+
maxDepth,
|
|
498
|
+
maxContextBudget,
|
|
499
|
+
minCohesion,
|
|
500
|
+
maxFragmentation,
|
|
501
|
+
circularDeps
|
|
502
|
+
} = params;
|
|
503
|
+
const issues = [];
|
|
504
|
+
const recommendations = [];
|
|
505
|
+
let severity = "info";
|
|
506
|
+
let potentialSavings = 0;
|
|
507
|
+
if (circularDeps.length > 0) {
|
|
508
|
+
severity = "critical";
|
|
509
|
+
issues.push(
|
|
510
|
+
`Part of ${circularDeps.length} circular dependency chain(s)`
|
|
511
|
+
);
|
|
512
|
+
recommendations.push("Break circular dependencies by extracting interfaces or using dependency injection");
|
|
513
|
+
potentialSavings += contextBudget * 0.2;
|
|
514
|
+
}
|
|
515
|
+
if (importDepth > maxDepth * 1.5) {
|
|
516
|
+
severity = severity === "critical" ? "critical" : "critical";
|
|
517
|
+
issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
|
|
518
|
+
recommendations.push("Flatten dependency tree or use facade pattern");
|
|
519
|
+
potentialSavings += contextBudget * 0.3;
|
|
520
|
+
} else if (importDepth > maxDepth) {
|
|
521
|
+
severity = severity === "critical" ? "critical" : "major";
|
|
522
|
+
issues.push(`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`);
|
|
523
|
+
recommendations.push("Consider reducing dependency depth");
|
|
524
|
+
potentialSavings += contextBudget * 0.15;
|
|
525
|
+
}
|
|
526
|
+
if (contextBudget > maxContextBudget * 1.5) {
|
|
527
|
+
severity = severity === "critical" ? "critical" : "critical";
|
|
528
|
+
issues.push(`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`);
|
|
529
|
+
recommendations.push("Split into smaller modules or reduce dependency tree");
|
|
530
|
+
potentialSavings += contextBudget * 0.4;
|
|
531
|
+
} else if (contextBudget > maxContextBudget) {
|
|
532
|
+
severity = severity === "critical" || severity === "major" ? severity : "major";
|
|
533
|
+
issues.push(`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`);
|
|
534
|
+
recommendations.push("Reduce file size or dependencies");
|
|
535
|
+
potentialSavings += contextBudget * 0.2;
|
|
536
|
+
}
|
|
537
|
+
if (cohesionScore < minCohesion * 0.5) {
|
|
538
|
+
severity = severity === "critical" ? "critical" : "major";
|
|
539
|
+
issues.push(`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`);
|
|
540
|
+
recommendations.push("Split file by domain - separate unrelated functionality");
|
|
541
|
+
potentialSavings += contextBudget * 0.25;
|
|
542
|
+
} else if (cohesionScore < minCohesion) {
|
|
543
|
+
severity = severity === "critical" || severity === "major" ? severity : "minor";
|
|
544
|
+
issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
|
|
545
|
+
recommendations.push("Consider grouping related exports together");
|
|
546
|
+
potentialSavings += contextBudget * 0.1;
|
|
547
|
+
}
|
|
548
|
+
if (fragmentationScore > maxFragmentation) {
|
|
549
|
+
severity = severity === "critical" || severity === "major" ? severity : "minor";
|
|
550
|
+
issues.push(`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`);
|
|
551
|
+
recommendations.push("Consolidate with related files in same domain");
|
|
552
|
+
potentialSavings += contextBudget * 0.3;
|
|
553
|
+
}
|
|
554
|
+
if (issues.length === 0) {
|
|
555
|
+
issues.push("No significant issues detected");
|
|
556
|
+
recommendations.push("File is well-structured for AI context usage");
|
|
557
|
+
}
|
|
558
|
+
return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
export {
|
|
562
|
+
analyzeContext,
|
|
563
|
+
generateSummary
|
|
564
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -564,6 +564,7 @@ function analyzeIssues(params) {
|
|
|
564
564
|
var import_chalk = __toESM(require("chalk"));
|
|
565
565
|
var import_fs = require("fs");
|
|
566
566
|
var import_path = require("path");
|
|
567
|
+
var import_core3 = require("@aiready/core");
|
|
567
568
|
var program = new import_commander.Command();
|
|
568
569
|
program.name("aiready-context").description("Analyze AI context window cost and code structure").version("0.1.0").argument("<directory>", "Directory to analyze").option("--max-depth <number>", "Maximum acceptable import depth", "5").option(
|
|
569
570
|
"--max-context <number>",
|
|
@@ -585,17 +586,30 @@ program.name("aiready-context").description("Analyze AI context window cost and
|
|
|
585
586
|
console.log(import_chalk.default.blue("\u{1F50D} Analyzing context window costs...\n"));
|
|
586
587
|
const startTime = Date.now();
|
|
587
588
|
try {
|
|
588
|
-
const
|
|
589
|
+
const config = (0, import_core3.loadConfig)(directory);
|
|
590
|
+
const defaults = {
|
|
591
|
+
maxDepth: 5,
|
|
592
|
+
maxContextBudget: 1e4,
|
|
593
|
+
minCohesion: 0.6,
|
|
594
|
+
maxFragmentation: 0.5,
|
|
595
|
+
focus: "all",
|
|
596
|
+
includeNodeModules: false,
|
|
597
|
+
include: void 0,
|
|
598
|
+
exclude: void 0
|
|
599
|
+
};
|
|
600
|
+
const mergedConfig = (0, import_core3.mergeConfigWithDefaults)(config, defaults);
|
|
601
|
+
const finalOptions = {
|
|
589
602
|
rootDir: directory,
|
|
590
|
-
maxDepth: parseInt(options.maxDepth),
|
|
591
|
-
maxContextBudget: parseInt(options.maxContext),
|
|
592
|
-
minCohesion: parseFloat(options.minCohesion),
|
|
593
|
-
maxFragmentation: parseFloat(options.maxFragmentation),
|
|
594
|
-
focus: options.focus,
|
|
595
|
-
includeNodeModules: options.includeNodeModules,
|
|
596
|
-
include: options.include?.split(","),
|
|
597
|
-
exclude: options.exclude?.split(",")
|
|
598
|
-
}
|
|
603
|
+
maxDepth: options.maxDepth ? parseInt(options.maxDepth) : mergedConfig.maxDepth,
|
|
604
|
+
maxContextBudget: options.maxContext ? parseInt(options.maxContext) : mergedConfig.maxContextBudget,
|
|
605
|
+
minCohesion: options.minCohesion ? parseFloat(options.minCohesion) : mergedConfig.minCohesion,
|
|
606
|
+
maxFragmentation: options.maxFragmentation ? parseFloat(options.maxFragmentation) : mergedConfig.maxFragmentation,
|
|
607
|
+
focus: options.focus || mergedConfig.focus,
|
|
608
|
+
includeNodeModules: options.includeNodeModules !== void 0 ? options.includeNodeModules : mergedConfig.includeNodeModules,
|
|
609
|
+
include: options.include?.split(",") || mergedConfig.include,
|
|
610
|
+
exclude: options.exclude?.split(",") || mergedConfig.exclude
|
|
611
|
+
};
|
|
612
|
+
const results = await analyzeContext(finalOptions);
|
|
599
613
|
const elapsedTime = ((Date.now() - startTime) / 1e3).toFixed(2);
|
|
600
614
|
const summary = generateSummary(results);
|
|
601
615
|
if (options.output === "json") {
|
package/dist/cli.mjs
CHANGED
|
@@ -9,6 +9,7 @@ import { Command } from "commander";
|
|
|
9
9
|
import chalk from "chalk";
|
|
10
10
|
import { writeFileSync } from "fs";
|
|
11
11
|
import { join } from "path";
|
|
12
|
+
import { loadConfig, mergeConfigWithDefaults } from "@aiready/core";
|
|
12
13
|
var program = new Command();
|
|
13
14
|
program.name("aiready-context").description("Analyze AI context window cost and code structure").version("0.1.0").argument("<directory>", "Directory to analyze").option("--max-depth <number>", "Maximum acceptable import depth", "5").option(
|
|
14
15
|
"--max-context <number>",
|
|
@@ -30,17 +31,30 @@ program.name("aiready-context").description("Analyze AI context window cost and
|
|
|
30
31
|
console.log(chalk.blue("\u{1F50D} Analyzing context window costs...\n"));
|
|
31
32
|
const startTime = Date.now();
|
|
32
33
|
try {
|
|
33
|
-
const
|
|
34
|
+
const config = loadConfig(directory);
|
|
35
|
+
const defaults = {
|
|
36
|
+
maxDepth: 5,
|
|
37
|
+
maxContextBudget: 1e4,
|
|
38
|
+
minCohesion: 0.6,
|
|
39
|
+
maxFragmentation: 0.5,
|
|
40
|
+
focus: "all",
|
|
41
|
+
includeNodeModules: false,
|
|
42
|
+
include: void 0,
|
|
43
|
+
exclude: void 0
|
|
44
|
+
};
|
|
45
|
+
const mergedConfig = mergeConfigWithDefaults(config, defaults);
|
|
46
|
+
const finalOptions = {
|
|
34
47
|
rootDir: directory,
|
|
35
|
-
maxDepth: parseInt(options.maxDepth),
|
|
36
|
-
maxContextBudget: parseInt(options.maxContext),
|
|
37
|
-
minCohesion: parseFloat(options.minCohesion),
|
|
38
|
-
maxFragmentation: parseFloat(options.maxFragmentation),
|
|
39
|
-
focus: options.focus,
|
|
40
|
-
includeNodeModules: options.includeNodeModules,
|
|
41
|
-
include: options.include?.split(","),
|
|
42
|
-
exclude: options.exclude?.split(",")
|
|
43
|
-
}
|
|
48
|
+
maxDepth: options.maxDepth ? parseInt(options.maxDepth) : mergedConfig.maxDepth,
|
|
49
|
+
maxContextBudget: options.maxContext ? parseInt(options.maxContext) : mergedConfig.maxContextBudget,
|
|
50
|
+
minCohesion: options.minCohesion ? parseFloat(options.minCohesion) : mergedConfig.minCohesion,
|
|
51
|
+
maxFragmentation: options.maxFragmentation ? parseFloat(options.maxFragmentation) : mergedConfig.maxFragmentation,
|
|
52
|
+
focus: options.focus || mergedConfig.focus,
|
|
53
|
+
includeNodeModules: options.includeNodeModules !== void 0 ? options.includeNodeModules : mergedConfig.includeNodeModules,
|
|
54
|
+
include: options.include?.split(",") || mergedConfig.include,
|
|
55
|
+
exclude: options.exclude?.split(",") || mergedConfig.exclude
|
|
56
|
+
};
|
|
57
|
+
const results = await analyzeContext(finalOptions);
|
|
44
58
|
const elapsedTime = ((Date.now() - startTime) / 1e3).toFixed(2);
|
|
45
59
|
const summary = generateSummary(results);
|
|
46
60
|
if (options.output === "json") {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiready/context-analyzer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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,7 +49,7 @@
|
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"commander": "^12.1.0",
|
|
51
51
|
"chalk": "^5.3.0",
|
|
52
|
-
"@aiready/core": "0.2.
|
|
52
|
+
"@aiready/core": "0.2.3"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^22.10.2",
|