@aiready/context-analyzer 0.1.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 +24 -0
- package/CONTRIBUTING.md +134 -0
- package/LICENSE +21 -0
- package/README.md +412 -0
- package/dist/chunk-K6U64EL3.mjs +517 -0
- package/dist/chunk-T6ZCOPPI.mjs +538 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +942 -0
- package/dist/cli.mjs +387 -0
- package/dist/index.d.mts +79 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +563 -0
- package/dist/index.mjs +8 -0
- package/package.json +72 -0
- package/src/__tests__/analyzer.test.ts +175 -0
- package/src/analyzer.ts +426 -0
- package/src/cli.ts +451 -0
- package/src/index.ts +396 -0
- package/src/types.ts +104 -0
- package/tsconfig.json +8 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,942 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/cli.ts
|
|
27
|
+
var import_commander = require("commander");
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var import_core2 = require("@aiready/core");
|
|
31
|
+
|
|
32
|
+
// src/analyzer.ts
|
|
33
|
+
var import_core = require("@aiready/core");
|
|
34
|
+
function buildDependencyGraph(files) {
|
|
35
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
36
|
+
const edges = /* @__PURE__ */ new Map();
|
|
37
|
+
for (const { file, content } of files) {
|
|
38
|
+
const imports = extractImportsFromContent(content);
|
|
39
|
+
const exports2 = extractExports(content);
|
|
40
|
+
const tokenCost = (0, import_core.estimateTokens)(content);
|
|
41
|
+
const linesOfCode = content.split("\n").length;
|
|
42
|
+
nodes.set(file, {
|
|
43
|
+
file,
|
|
44
|
+
imports,
|
|
45
|
+
exports: exports2,
|
|
46
|
+
tokenCost,
|
|
47
|
+
linesOfCode
|
|
48
|
+
});
|
|
49
|
+
edges.set(file, new Set(imports));
|
|
50
|
+
}
|
|
51
|
+
return { nodes, edges };
|
|
52
|
+
}
|
|
53
|
+
function extractImportsFromContent(content) {
|
|
54
|
+
const imports = [];
|
|
55
|
+
const patterns = [
|
|
56
|
+
/import\s+.*?\s+from\s+['"](.+?)['"]/g,
|
|
57
|
+
// import ... from '...'
|
|
58
|
+
/import\s+['"](.+?)['"]/g,
|
|
59
|
+
// import '...'
|
|
60
|
+
/require\(['"](.+?)['"]\)/g
|
|
61
|
+
// require('...')
|
|
62
|
+
];
|
|
63
|
+
for (const pattern of patterns) {
|
|
64
|
+
let match;
|
|
65
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
66
|
+
const importPath = match[1];
|
|
67
|
+
if (importPath && !importPath.startsWith("@") && !importPath.startsWith("node:")) {
|
|
68
|
+
imports.push(importPath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return [...new Set(imports)];
|
|
73
|
+
}
|
|
74
|
+
function calculateImportDepth(file, graph, visited = /* @__PURE__ */ new Set(), depth = 0) {
|
|
75
|
+
if (visited.has(file)) {
|
|
76
|
+
return depth;
|
|
77
|
+
}
|
|
78
|
+
const dependencies = graph.edges.get(file);
|
|
79
|
+
if (!dependencies || dependencies.size === 0) {
|
|
80
|
+
return depth;
|
|
81
|
+
}
|
|
82
|
+
visited.add(file);
|
|
83
|
+
let maxDepth = depth;
|
|
84
|
+
for (const dep of dependencies) {
|
|
85
|
+
const depDepth = calculateImportDepth(dep, graph, visited, depth + 1);
|
|
86
|
+
maxDepth = Math.max(maxDepth, depDepth);
|
|
87
|
+
}
|
|
88
|
+
visited.delete(file);
|
|
89
|
+
return maxDepth;
|
|
90
|
+
}
|
|
91
|
+
function getTransitiveDependencies(file, graph, visited = /* @__PURE__ */ new Set()) {
|
|
92
|
+
if (visited.has(file)) {
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
visited.add(file);
|
|
96
|
+
const dependencies = graph.edges.get(file);
|
|
97
|
+
if (!dependencies || dependencies.size === 0) {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
const allDeps = [];
|
|
101
|
+
for (const dep of dependencies) {
|
|
102
|
+
allDeps.push(dep);
|
|
103
|
+
allDeps.push(...getTransitiveDependencies(dep, graph, visited));
|
|
104
|
+
}
|
|
105
|
+
return [...new Set(allDeps)];
|
|
106
|
+
}
|
|
107
|
+
function calculateContextBudget(file, graph) {
|
|
108
|
+
const node = graph.nodes.get(file);
|
|
109
|
+
if (!node) return 0;
|
|
110
|
+
let totalTokens = node.tokenCost;
|
|
111
|
+
const deps = getTransitiveDependencies(file, graph);
|
|
112
|
+
for (const dep of deps) {
|
|
113
|
+
const depNode = graph.nodes.get(dep);
|
|
114
|
+
if (depNode) {
|
|
115
|
+
totalTokens += depNode.tokenCost;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return totalTokens;
|
|
119
|
+
}
|
|
120
|
+
function detectCircularDependencies(graph) {
|
|
121
|
+
const cycles = [];
|
|
122
|
+
const visited = /* @__PURE__ */ new Set();
|
|
123
|
+
const recursionStack = /* @__PURE__ */ new Set();
|
|
124
|
+
function dfs(file, path) {
|
|
125
|
+
if (recursionStack.has(file)) {
|
|
126
|
+
const cycleStart = path.indexOf(file);
|
|
127
|
+
if (cycleStart !== -1) {
|
|
128
|
+
cycles.push([...path.slice(cycleStart), file]);
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (visited.has(file)) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
visited.add(file);
|
|
136
|
+
recursionStack.add(file);
|
|
137
|
+
path.push(file);
|
|
138
|
+
const dependencies = graph.edges.get(file);
|
|
139
|
+
if (dependencies) {
|
|
140
|
+
for (const dep of dependencies) {
|
|
141
|
+
dfs(dep, [...path]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
recursionStack.delete(file);
|
|
145
|
+
}
|
|
146
|
+
for (const file of graph.nodes.keys()) {
|
|
147
|
+
if (!visited.has(file)) {
|
|
148
|
+
dfs(file, []);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return cycles;
|
|
152
|
+
}
|
|
153
|
+
function calculateCohesion(exports2) {
|
|
154
|
+
if (exports2.length === 0) return 1;
|
|
155
|
+
if (exports2.length === 1) return 1;
|
|
156
|
+
const domains = exports2.map((e) => e.inferredDomain || "unknown");
|
|
157
|
+
const domainCounts = /* @__PURE__ */ new Map();
|
|
158
|
+
for (const domain of domains) {
|
|
159
|
+
domainCounts.set(domain, (domainCounts.get(domain) || 0) + 1);
|
|
160
|
+
}
|
|
161
|
+
const total = domains.length;
|
|
162
|
+
let entropy = 0;
|
|
163
|
+
for (const count of domainCounts.values()) {
|
|
164
|
+
const p = count / total;
|
|
165
|
+
if (p > 0) {
|
|
166
|
+
entropy -= p * Math.log2(p);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const maxEntropy = Math.log2(total);
|
|
170
|
+
return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
|
|
171
|
+
}
|
|
172
|
+
function calculateFragmentation(files, domain) {
|
|
173
|
+
if (files.length <= 1) return 0;
|
|
174
|
+
const directories = new Set(files.map((f) => f.split("/").slice(0, -1).join("/")));
|
|
175
|
+
return (directories.size - 1) / (files.length - 1);
|
|
176
|
+
}
|
|
177
|
+
function detectModuleClusters(graph) {
|
|
178
|
+
const domainMap = /* @__PURE__ */ new Map();
|
|
179
|
+
for (const [file, node] of graph.nodes.entries()) {
|
|
180
|
+
const domains = node.exports.map((e) => e.inferredDomain || "unknown");
|
|
181
|
+
const primaryDomain = domains[0] || "unknown";
|
|
182
|
+
if (!domainMap.has(primaryDomain)) {
|
|
183
|
+
domainMap.set(primaryDomain, []);
|
|
184
|
+
}
|
|
185
|
+
domainMap.get(primaryDomain).push(file);
|
|
186
|
+
}
|
|
187
|
+
const clusters = [];
|
|
188
|
+
for (const [domain, files] of domainMap.entries()) {
|
|
189
|
+
if (files.length < 2) continue;
|
|
190
|
+
const totalTokens = files.reduce((sum, file) => {
|
|
191
|
+
const node = graph.nodes.get(file);
|
|
192
|
+
return sum + (node?.tokenCost || 0);
|
|
193
|
+
}, 0);
|
|
194
|
+
const fragmentationScore = calculateFragmentation(files, domain);
|
|
195
|
+
const avgCohesion = files.reduce((sum, file) => {
|
|
196
|
+
const node = graph.nodes.get(file);
|
|
197
|
+
return sum + (node ? calculateCohesion(node.exports) : 0);
|
|
198
|
+
}, 0) / files.length;
|
|
199
|
+
const targetFiles = Math.max(1, Math.ceil(files.length / 3));
|
|
200
|
+
const consolidationPlan = generateConsolidationPlan(
|
|
201
|
+
domain,
|
|
202
|
+
files,
|
|
203
|
+
targetFiles
|
|
204
|
+
);
|
|
205
|
+
clusters.push({
|
|
206
|
+
domain,
|
|
207
|
+
files,
|
|
208
|
+
totalTokens,
|
|
209
|
+
fragmentationScore,
|
|
210
|
+
avgCohesion,
|
|
211
|
+
suggestedStructure: {
|
|
212
|
+
targetFiles,
|
|
213
|
+
consolidationPlan
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
return clusters.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
|
|
218
|
+
}
|
|
219
|
+
function extractExports(content) {
|
|
220
|
+
const exports2 = [];
|
|
221
|
+
const patterns = [
|
|
222
|
+
/export\s+function\s+(\w+)/g,
|
|
223
|
+
/export\s+class\s+(\w+)/g,
|
|
224
|
+
/export\s+const\s+(\w+)/g,
|
|
225
|
+
/export\s+type\s+(\w+)/g,
|
|
226
|
+
/export\s+interface\s+(\w+)/g,
|
|
227
|
+
/export\s+default/g
|
|
228
|
+
];
|
|
229
|
+
const types = [
|
|
230
|
+
"function",
|
|
231
|
+
"class",
|
|
232
|
+
"const",
|
|
233
|
+
"type",
|
|
234
|
+
"interface",
|
|
235
|
+
"default"
|
|
236
|
+
];
|
|
237
|
+
patterns.forEach((pattern, index) => {
|
|
238
|
+
let match;
|
|
239
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
240
|
+
const name = match[1] || "default";
|
|
241
|
+
const type = types[index];
|
|
242
|
+
const inferredDomain = inferDomain(name);
|
|
243
|
+
exports2.push({ name, type, inferredDomain });
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
return exports2;
|
|
247
|
+
}
|
|
248
|
+
function inferDomain(name) {
|
|
249
|
+
const lower = name.toLowerCase();
|
|
250
|
+
const domainKeywords = [
|
|
251
|
+
"user",
|
|
252
|
+
"auth",
|
|
253
|
+
"order",
|
|
254
|
+
"product",
|
|
255
|
+
"payment",
|
|
256
|
+
"cart",
|
|
257
|
+
"invoice",
|
|
258
|
+
"customer",
|
|
259
|
+
"admin",
|
|
260
|
+
"api",
|
|
261
|
+
"util",
|
|
262
|
+
"helper",
|
|
263
|
+
"config",
|
|
264
|
+
"service",
|
|
265
|
+
"repository",
|
|
266
|
+
"controller",
|
|
267
|
+
"model",
|
|
268
|
+
"view"
|
|
269
|
+
];
|
|
270
|
+
for (const keyword of domainKeywords) {
|
|
271
|
+
if (lower.includes(keyword)) {
|
|
272
|
+
return keyword;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return "unknown";
|
|
276
|
+
}
|
|
277
|
+
function generateConsolidationPlan(domain, files, targetFiles) {
|
|
278
|
+
const plan = [];
|
|
279
|
+
if (files.length <= targetFiles) {
|
|
280
|
+
return [`No consolidation needed for ${domain}`];
|
|
281
|
+
}
|
|
282
|
+
plan.push(
|
|
283
|
+
`Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s):`
|
|
284
|
+
);
|
|
285
|
+
const dirGroups = /* @__PURE__ */ new Map();
|
|
286
|
+
for (const file of files) {
|
|
287
|
+
const dir = file.split("/").slice(0, -1).join("/");
|
|
288
|
+
if (!dirGroups.has(dir)) {
|
|
289
|
+
dirGroups.set(dir, []);
|
|
290
|
+
}
|
|
291
|
+
dirGroups.get(dir).push(file);
|
|
292
|
+
}
|
|
293
|
+
plan.push(`1. Create unified ${domain} module file`);
|
|
294
|
+
plan.push(
|
|
295
|
+
`2. Move related functionality from ${files.length} scattered files`
|
|
296
|
+
);
|
|
297
|
+
plan.push(`3. Update imports in dependent files`);
|
|
298
|
+
plan.push(
|
|
299
|
+
`4. Remove old files after consolidation (verify with tests first)`
|
|
300
|
+
);
|
|
301
|
+
return plan;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// src/index.ts
|
|
305
|
+
async function analyzeContext(options) {
|
|
306
|
+
const {
|
|
307
|
+
maxDepth = 5,
|
|
308
|
+
maxContextBudget = 1e4,
|
|
309
|
+
minCohesion = 0.6,
|
|
310
|
+
maxFragmentation = 0.5,
|
|
311
|
+
focus = "all",
|
|
312
|
+
includeNodeModules = false,
|
|
313
|
+
...scanOptions
|
|
314
|
+
} = options;
|
|
315
|
+
const files = await (0, import_core2.scanFiles)({
|
|
316
|
+
...scanOptions,
|
|
317
|
+
exclude: includeNodeModules ? scanOptions.exclude : [...scanOptions.exclude || [], "**/node_modules/**"]
|
|
318
|
+
});
|
|
319
|
+
const fileContents = await Promise.all(
|
|
320
|
+
files.map(async (file) => ({
|
|
321
|
+
file,
|
|
322
|
+
content: await (0, import_core2.readFileContent)(file)
|
|
323
|
+
}))
|
|
324
|
+
);
|
|
325
|
+
const graph = buildDependencyGraph(fileContents);
|
|
326
|
+
const circularDeps = detectCircularDependencies(graph);
|
|
327
|
+
const clusters = detectModuleClusters(graph);
|
|
328
|
+
const fragmentationMap = /* @__PURE__ */ new Map();
|
|
329
|
+
for (const cluster of clusters) {
|
|
330
|
+
for (const file of cluster.files) {
|
|
331
|
+
fragmentationMap.set(file, cluster.fragmentationScore);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const results = [];
|
|
335
|
+
for (const { file } of fileContents) {
|
|
336
|
+
const node = graph.nodes.get(file);
|
|
337
|
+
if (!node) continue;
|
|
338
|
+
const importDepth = focus === "depth" || focus === "all" ? calculateImportDepth(file, graph) : 0;
|
|
339
|
+
const dependencyList = focus === "depth" || focus === "all" ? getTransitiveDependencies(file, graph) : [];
|
|
340
|
+
const contextBudget = focus === "all" ? calculateContextBudget(file, graph) : node.tokenCost;
|
|
341
|
+
const cohesionScore = focus === "cohesion" || focus === "all" ? calculateCohesion(node.exports) : 1;
|
|
342
|
+
const fragmentationScore = fragmentationMap.get(file) || 0;
|
|
343
|
+
const relatedFiles = [];
|
|
344
|
+
for (const cluster of clusters) {
|
|
345
|
+
if (cluster.files.includes(file)) {
|
|
346
|
+
relatedFiles.push(...cluster.files.filter((f) => f !== file));
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
|
|
351
|
+
file,
|
|
352
|
+
importDepth,
|
|
353
|
+
contextBudget,
|
|
354
|
+
cohesionScore,
|
|
355
|
+
fragmentationScore,
|
|
356
|
+
maxDepth,
|
|
357
|
+
maxContextBudget,
|
|
358
|
+
minCohesion,
|
|
359
|
+
maxFragmentation,
|
|
360
|
+
circularDeps
|
|
361
|
+
});
|
|
362
|
+
const domains = [
|
|
363
|
+
...new Set(node.exports.map((e) => e.inferredDomain || "unknown"))
|
|
364
|
+
];
|
|
365
|
+
results.push({
|
|
366
|
+
file,
|
|
367
|
+
tokenCost: node.tokenCost,
|
|
368
|
+
linesOfCode: node.linesOfCode,
|
|
369
|
+
importDepth,
|
|
370
|
+
dependencyCount: dependencyList.length,
|
|
371
|
+
dependencyList,
|
|
372
|
+
circularDeps: circularDeps.filter((cycle) => cycle.includes(file)),
|
|
373
|
+
cohesionScore,
|
|
374
|
+
domains,
|
|
375
|
+
exportCount: node.exports.length,
|
|
376
|
+
contextBudget,
|
|
377
|
+
fragmentationScore,
|
|
378
|
+
relatedFiles,
|
|
379
|
+
severity,
|
|
380
|
+
issues,
|
|
381
|
+
recommendations,
|
|
382
|
+
potentialSavings
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
return results.sort((a, b) => {
|
|
386
|
+
const severityOrder = { critical: 0, major: 1, minor: 2, info: 3 };
|
|
387
|
+
const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
|
|
388
|
+
if (severityDiff !== 0) return severityDiff;
|
|
389
|
+
return b.contextBudget - a.contextBudget;
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
function generateSummary(results) {
|
|
393
|
+
if (results.length === 0) {
|
|
394
|
+
return {
|
|
395
|
+
totalFiles: 0,
|
|
396
|
+
totalTokens: 0,
|
|
397
|
+
avgContextBudget: 0,
|
|
398
|
+
maxContextBudget: 0,
|
|
399
|
+
avgImportDepth: 0,
|
|
400
|
+
maxImportDepth: 0,
|
|
401
|
+
deepFiles: [],
|
|
402
|
+
avgFragmentation: 0,
|
|
403
|
+
fragmentedModules: [],
|
|
404
|
+
avgCohesion: 0,
|
|
405
|
+
lowCohesionFiles: [],
|
|
406
|
+
criticalIssues: 0,
|
|
407
|
+
majorIssues: 0,
|
|
408
|
+
minorIssues: 0,
|
|
409
|
+
totalPotentialSavings: 0,
|
|
410
|
+
topExpensiveFiles: []
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
const totalFiles = results.length;
|
|
414
|
+
const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
|
|
415
|
+
const totalContextBudget = results.reduce(
|
|
416
|
+
(sum, r) => sum + r.contextBudget,
|
|
417
|
+
0
|
|
418
|
+
);
|
|
419
|
+
const avgContextBudget = totalContextBudget / totalFiles;
|
|
420
|
+
const maxContextBudget = Math.max(...results.map((r) => r.contextBudget));
|
|
421
|
+
const avgImportDepth = results.reduce((sum, r) => sum + r.importDepth, 0) / totalFiles;
|
|
422
|
+
const maxImportDepth = Math.max(...results.map((r) => r.importDepth));
|
|
423
|
+
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);
|
|
424
|
+
const avgFragmentation = results.reduce((sum, r) => sum + r.fragmentationScore, 0) / totalFiles;
|
|
425
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
426
|
+
for (const result of results) {
|
|
427
|
+
for (const domain of result.domains) {
|
|
428
|
+
if (!moduleMap.has(domain)) {
|
|
429
|
+
moduleMap.set(domain, []);
|
|
430
|
+
}
|
|
431
|
+
moduleMap.get(domain).push(result);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const fragmentedModules = [];
|
|
435
|
+
for (const [domain, files] of moduleMap.entries()) {
|
|
436
|
+
if (files.length < 2) continue;
|
|
437
|
+
const fragmentationScore = files.reduce((sum, f) => sum + f.fragmentationScore, 0) / files.length;
|
|
438
|
+
if (fragmentationScore < 0.3) continue;
|
|
439
|
+
const totalTokens2 = files.reduce((sum, f) => sum + f.tokenCost, 0);
|
|
440
|
+
const avgCohesion2 = files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length;
|
|
441
|
+
const targetFiles = Math.max(1, Math.ceil(files.length / 3));
|
|
442
|
+
fragmentedModules.push({
|
|
443
|
+
domain,
|
|
444
|
+
files: files.map((f) => f.file),
|
|
445
|
+
totalTokens: totalTokens2,
|
|
446
|
+
fragmentationScore,
|
|
447
|
+
avgCohesion: avgCohesion2,
|
|
448
|
+
suggestedStructure: {
|
|
449
|
+
targetFiles,
|
|
450
|
+
consolidationPlan: [
|
|
451
|
+
`Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s)`,
|
|
452
|
+
`Current token cost: ${totalTokens2.toLocaleString()}`,
|
|
453
|
+
`Estimated savings: ${Math.floor(totalTokens2 * 0.3).toLocaleString()} tokens (30%)`
|
|
454
|
+
]
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
|
|
459
|
+
const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / totalFiles;
|
|
460
|
+
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);
|
|
461
|
+
const criticalIssues = results.filter((r) => r.severity === "critical").length;
|
|
462
|
+
const majorIssues = results.filter((r) => r.severity === "major").length;
|
|
463
|
+
const minorIssues = results.filter((r) => r.severity === "minor").length;
|
|
464
|
+
const totalPotentialSavings = results.reduce(
|
|
465
|
+
(sum, r) => sum + r.potentialSavings,
|
|
466
|
+
0
|
|
467
|
+
);
|
|
468
|
+
const topExpensiveFiles = results.sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
|
|
469
|
+
file: r.file,
|
|
470
|
+
contextBudget: r.contextBudget,
|
|
471
|
+
severity: r.severity
|
|
472
|
+
}));
|
|
473
|
+
return {
|
|
474
|
+
totalFiles,
|
|
475
|
+
totalTokens,
|
|
476
|
+
avgContextBudget,
|
|
477
|
+
maxContextBudget,
|
|
478
|
+
avgImportDepth,
|
|
479
|
+
maxImportDepth,
|
|
480
|
+
deepFiles,
|
|
481
|
+
avgFragmentation,
|
|
482
|
+
fragmentedModules: fragmentedModules.slice(0, 10),
|
|
483
|
+
avgCohesion,
|
|
484
|
+
lowCohesionFiles,
|
|
485
|
+
criticalIssues,
|
|
486
|
+
majorIssues,
|
|
487
|
+
minorIssues,
|
|
488
|
+
totalPotentialSavings,
|
|
489
|
+
topExpensiveFiles
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
function analyzeIssues(params) {
|
|
493
|
+
const {
|
|
494
|
+
file,
|
|
495
|
+
importDepth,
|
|
496
|
+
contextBudget,
|
|
497
|
+
cohesionScore,
|
|
498
|
+
fragmentationScore,
|
|
499
|
+
maxDepth,
|
|
500
|
+
maxContextBudget,
|
|
501
|
+
minCohesion,
|
|
502
|
+
maxFragmentation,
|
|
503
|
+
circularDeps
|
|
504
|
+
} = params;
|
|
505
|
+
const issues = [];
|
|
506
|
+
const recommendations = [];
|
|
507
|
+
let severity = "info";
|
|
508
|
+
let potentialSavings = 0;
|
|
509
|
+
if (circularDeps.length > 0) {
|
|
510
|
+
severity = "critical";
|
|
511
|
+
issues.push(
|
|
512
|
+
`Part of ${circularDeps.length} circular dependency chain(s)`
|
|
513
|
+
);
|
|
514
|
+
recommendations.push("Break circular dependencies by extracting interfaces or using dependency injection");
|
|
515
|
+
potentialSavings += contextBudget * 0.2;
|
|
516
|
+
}
|
|
517
|
+
if (importDepth > maxDepth * 1.5) {
|
|
518
|
+
severity = severity === "critical" ? "critical" : "critical";
|
|
519
|
+
issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
|
|
520
|
+
recommendations.push("Flatten dependency tree or use facade pattern");
|
|
521
|
+
potentialSavings += contextBudget * 0.3;
|
|
522
|
+
} else if (importDepth > maxDepth) {
|
|
523
|
+
severity = severity === "critical" ? "critical" : "major";
|
|
524
|
+
issues.push(`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`);
|
|
525
|
+
recommendations.push("Consider reducing dependency depth");
|
|
526
|
+
potentialSavings += contextBudget * 0.15;
|
|
527
|
+
}
|
|
528
|
+
if (contextBudget > maxContextBudget * 1.5) {
|
|
529
|
+
severity = severity === "critical" ? "critical" : "critical";
|
|
530
|
+
issues.push(`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`);
|
|
531
|
+
recommendations.push("Split into smaller modules or reduce dependency tree");
|
|
532
|
+
potentialSavings += contextBudget * 0.4;
|
|
533
|
+
} else if (contextBudget > maxContextBudget) {
|
|
534
|
+
severity = severity === "critical" || severity === "major" ? severity : "major";
|
|
535
|
+
issues.push(`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`);
|
|
536
|
+
recommendations.push("Reduce file size or dependencies");
|
|
537
|
+
potentialSavings += contextBudget * 0.2;
|
|
538
|
+
}
|
|
539
|
+
if (cohesionScore < minCohesion * 0.5) {
|
|
540
|
+
severity = severity === "critical" ? "critical" : "major";
|
|
541
|
+
issues.push(`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`);
|
|
542
|
+
recommendations.push("Split file by domain - separate unrelated functionality");
|
|
543
|
+
potentialSavings += contextBudget * 0.25;
|
|
544
|
+
} else if (cohesionScore < minCohesion) {
|
|
545
|
+
severity = severity === "critical" || severity === "major" ? severity : "minor";
|
|
546
|
+
issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
|
|
547
|
+
recommendations.push("Consider grouping related exports together");
|
|
548
|
+
potentialSavings += contextBudget * 0.1;
|
|
549
|
+
}
|
|
550
|
+
if (fragmentationScore > maxFragmentation) {
|
|
551
|
+
severity = severity === "critical" || severity === "major" ? severity : "minor";
|
|
552
|
+
issues.push(`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`);
|
|
553
|
+
recommendations.push("Consolidate with related files in same domain");
|
|
554
|
+
potentialSavings += contextBudget * 0.3;
|
|
555
|
+
}
|
|
556
|
+
if (issues.length === 0) {
|
|
557
|
+
issues.push("No significant issues detected");
|
|
558
|
+
recommendations.push("File is well-structured for AI context usage");
|
|
559
|
+
}
|
|
560
|
+
return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// src/cli.ts
|
|
564
|
+
var import_chalk = __toESM(require("chalk"));
|
|
565
|
+
var import_fs = require("fs");
|
|
566
|
+
var import_path = require("path");
|
|
567
|
+
var program = new import_commander.Command();
|
|
568
|
+
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
|
+
"--max-context <number>",
|
|
570
|
+
"Maximum acceptable context budget (tokens)",
|
|
571
|
+
"10000"
|
|
572
|
+
).option("--min-cohesion <number>", "Minimum acceptable cohesion score (0-1)", "0.6").option(
|
|
573
|
+
"--max-fragmentation <number>",
|
|
574
|
+
"Maximum acceptable fragmentation (0-1)",
|
|
575
|
+
"0.5"
|
|
576
|
+
).option(
|
|
577
|
+
"--focus <type>",
|
|
578
|
+
"Analysis focus: fragmentation, cohesion, depth, all",
|
|
579
|
+
"all"
|
|
580
|
+
).option("--include-node-modules", "Include node_modules in analysis", false).option("--include <patterns>", "File patterns to include (comma-separated)").option("--exclude <patterns>", "File patterns to exclude (comma-separated)").option(
|
|
581
|
+
"-o, --output <format>",
|
|
582
|
+
"Output format: console, json, html",
|
|
583
|
+
"console"
|
|
584
|
+
).option("--output-file <path>", "Output file path (for json/html)").action(async (directory, options) => {
|
|
585
|
+
console.log(import_chalk.default.blue("\u{1F50D} Analyzing context window costs...\n"));
|
|
586
|
+
const startTime = Date.now();
|
|
587
|
+
try {
|
|
588
|
+
const results = await analyzeContext({
|
|
589
|
+
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
|
+
});
|
|
599
|
+
const elapsedTime = ((Date.now() - startTime) / 1e3).toFixed(2);
|
|
600
|
+
const summary = generateSummary(results);
|
|
601
|
+
if (options.output === "json") {
|
|
602
|
+
const jsonOutput = {
|
|
603
|
+
summary,
|
|
604
|
+
results,
|
|
605
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
606
|
+
analysisTime: elapsedTime
|
|
607
|
+
};
|
|
608
|
+
if (options.outputFile) {
|
|
609
|
+
(0, import_fs.writeFileSync)(
|
|
610
|
+
options.outputFile,
|
|
611
|
+
JSON.stringify(jsonOutput, null, 2)
|
|
612
|
+
);
|
|
613
|
+
console.log(
|
|
614
|
+
import_chalk.default.green(`
|
|
615
|
+
\u2713 JSON report saved to ${options.outputFile}`)
|
|
616
|
+
);
|
|
617
|
+
} else {
|
|
618
|
+
console.log(JSON.stringify(jsonOutput, null, 2));
|
|
619
|
+
}
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
if (options.output === "html") {
|
|
623
|
+
const html = generateHTMLReport(summary, results);
|
|
624
|
+
const outputPath = options.outputFile || (0, import_path.join)(process.cwd(), "context-report.html");
|
|
625
|
+
(0, import_fs.writeFileSync)(outputPath, html);
|
|
626
|
+
console.log(import_chalk.default.green(`
|
|
627
|
+
\u2713 HTML report saved to ${outputPath}`));
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
displayConsoleReport(summary, results, elapsedTime);
|
|
631
|
+
} catch (error) {
|
|
632
|
+
console.error(import_chalk.default.red("\n\u274C Analysis failed:"));
|
|
633
|
+
console.error(import_chalk.default.red(error instanceof Error ? error.message : String(error)));
|
|
634
|
+
process.exit(1);
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
program.parse();
|
|
638
|
+
function displayConsoleReport(summary, results, elapsedTime) {
|
|
639
|
+
const terminalWidth = process.stdout.columns || 80;
|
|
640
|
+
const dividerWidth = Math.min(60, terminalWidth - 2);
|
|
641
|
+
const divider = "\u2501".repeat(dividerWidth);
|
|
642
|
+
console.log(import_chalk.default.cyan(divider));
|
|
643
|
+
console.log(import_chalk.default.bold.white(" CONTEXT ANALYSIS SUMMARY"));
|
|
644
|
+
console.log(import_chalk.default.cyan(divider) + "\n");
|
|
645
|
+
console.log(import_chalk.default.white(`\u{1F4C1} Files analyzed: ${import_chalk.default.bold(summary.totalFiles)}`));
|
|
646
|
+
console.log(
|
|
647
|
+
import_chalk.default.white(`\u{1F4CA} Total tokens: ${import_chalk.default.bold(summary.totalTokens.toLocaleString())}`)
|
|
648
|
+
);
|
|
649
|
+
console.log(
|
|
650
|
+
import_chalk.default.yellow(
|
|
651
|
+
`\u{1F4B0} Avg context budget: ${import_chalk.default.bold(summary.avgContextBudget.toFixed(0))} tokens/file`
|
|
652
|
+
)
|
|
653
|
+
);
|
|
654
|
+
console.log(
|
|
655
|
+
import_chalk.default.white(`\u23F1 Analysis time: ${import_chalk.default.bold(elapsedTime + "s")}
|
|
656
|
+
`)
|
|
657
|
+
);
|
|
658
|
+
const totalIssues = summary.criticalIssues + summary.majorIssues + summary.minorIssues;
|
|
659
|
+
if (totalIssues > 0) {
|
|
660
|
+
console.log(import_chalk.default.bold("\u26A0\uFE0F Issues Found:\n"));
|
|
661
|
+
if (summary.criticalIssues > 0) {
|
|
662
|
+
console.log(
|
|
663
|
+
import_chalk.default.red(` \u{1F534} Critical: ${import_chalk.default.bold(summary.criticalIssues)}`)
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
if (summary.majorIssues > 0) {
|
|
667
|
+
console.log(
|
|
668
|
+
import_chalk.default.yellow(` \u{1F7E1} Major: ${import_chalk.default.bold(summary.majorIssues)}`)
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
if (summary.minorIssues > 0) {
|
|
672
|
+
console.log(import_chalk.default.blue(` \u{1F535} Minor: ${import_chalk.default.bold(summary.minorIssues)}`));
|
|
673
|
+
}
|
|
674
|
+
console.log(
|
|
675
|
+
import_chalk.default.green(
|
|
676
|
+
`
|
|
677
|
+
\u{1F4A1} Potential savings: ${import_chalk.default.bold(summary.totalPotentialSavings.toLocaleString())} tokens
|
|
678
|
+
`
|
|
679
|
+
)
|
|
680
|
+
);
|
|
681
|
+
} else {
|
|
682
|
+
console.log(import_chalk.default.green("\u2705 No significant issues found!\n"));
|
|
683
|
+
}
|
|
684
|
+
if (summary.deepFiles.length > 0) {
|
|
685
|
+
console.log(import_chalk.default.bold("\u{1F4CF} Deep Import Chains:\n"));
|
|
686
|
+
console.log(
|
|
687
|
+
import_chalk.default.gray(` Average depth: ${summary.avgImportDepth.toFixed(1)}`)
|
|
688
|
+
);
|
|
689
|
+
console.log(import_chalk.default.gray(` Maximum depth: ${summary.maxImportDepth}
|
|
690
|
+
`));
|
|
691
|
+
summary.deepFiles.slice(0, 5).forEach((item) => {
|
|
692
|
+
const fileName = item.file.split("/").slice(-2).join("/");
|
|
693
|
+
console.log(
|
|
694
|
+
` ${import_chalk.default.cyan("\u2192")} ${import_chalk.default.white(fileName)} ${import_chalk.default.dim(`(depth: ${item.depth})`)}`
|
|
695
|
+
);
|
|
696
|
+
});
|
|
697
|
+
console.log();
|
|
698
|
+
}
|
|
699
|
+
if (summary.fragmentedModules.length > 0) {
|
|
700
|
+
console.log(import_chalk.default.bold("\u{1F9E9} Fragmented Modules:\n"));
|
|
701
|
+
console.log(
|
|
702
|
+
import_chalk.default.gray(
|
|
703
|
+
` Average fragmentation: ${(summary.avgFragmentation * 100).toFixed(0)}%
|
|
704
|
+
`
|
|
705
|
+
)
|
|
706
|
+
);
|
|
707
|
+
summary.fragmentedModules.slice(0, 5).forEach((module2) => {
|
|
708
|
+
console.log(
|
|
709
|
+
` ${import_chalk.default.yellow("\u25CF")} ${import_chalk.default.white(module2.domain)} - ${import_chalk.default.dim(`${module2.files.length} files, ${(module2.fragmentationScore * 100).toFixed(0)}% scattered`)}`
|
|
710
|
+
);
|
|
711
|
+
console.log(
|
|
712
|
+
import_chalk.default.dim(
|
|
713
|
+
` Token cost: ${module2.totalTokens.toLocaleString()}, Cohesion: ${(module2.avgCohesion * 100).toFixed(0)}%`
|
|
714
|
+
)
|
|
715
|
+
);
|
|
716
|
+
});
|
|
717
|
+
console.log();
|
|
718
|
+
}
|
|
719
|
+
if (summary.lowCohesionFiles.length > 0) {
|
|
720
|
+
console.log(import_chalk.default.bold("\u{1F500} Low Cohesion Files:\n"));
|
|
721
|
+
console.log(
|
|
722
|
+
import_chalk.default.gray(
|
|
723
|
+
` Average cohesion: ${(summary.avgCohesion * 100).toFixed(0)}%
|
|
724
|
+
`
|
|
725
|
+
)
|
|
726
|
+
);
|
|
727
|
+
summary.lowCohesionFiles.slice(0, 5).forEach((item) => {
|
|
728
|
+
const fileName = item.file.split("/").slice(-2).join("/");
|
|
729
|
+
const scorePercent = (item.score * 100).toFixed(0);
|
|
730
|
+
const color = item.score < 0.4 ? import_chalk.default.red : import_chalk.default.yellow;
|
|
731
|
+
console.log(
|
|
732
|
+
` ${color("\u25CB")} ${import_chalk.default.white(fileName)} ${import_chalk.default.dim(`(${scorePercent}% cohesion)`)}`
|
|
733
|
+
);
|
|
734
|
+
});
|
|
735
|
+
console.log();
|
|
736
|
+
}
|
|
737
|
+
if (summary.topExpensiveFiles.length > 0) {
|
|
738
|
+
console.log(import_chalk.default.bold("\u{1F4B8} Most Expensive Files (Context Budget):\n"));
|
|
739
|
+
summary.topExpensiveFiles.slice(0, 5).forEach((item) => {
|
|
740
|
+
const fileName = item.file.split("/").slice(-2).join("/");
|
|
741
|
+
const severityColor = item.severity === "critical" ? import_chalk.default.red : item.severity === "major" ? import_chalk.default.yellow : import_chalk.default.blue;
|
|
742
|
+
console.log(
|
|
743
|
+
` ${severityColor("\u25CF")} ${import_chalk.default.white(fileName)} ${import_chalk.default.dim(`- ${item.contextBudget.toLocaleString()} tokens`)}`
|
|
744
|
+
);
|
|
745
|
+
});
|
|
746
|
+
console.log();
|
|
747
|
+
}
|
|
748
|
+
if (totalIssues > 0) {
|
|
749
|
+
console.log(import_chalk.default.bold("\u{1F4A1} Top Recommendations:\n"));
|
|
750
|
+
const topFiles = results.filter((r) => r.severity === "critical" || r.severity === "major").slice(0, 3);
|
|
751
|
+
topFiles.forEach((result, index) => {
|
|
752
|
+
const fileName = result.file.split("/").slice(-2).join("/");
|
|
753
|
+
console.log(import_chalk.default.cyan(` ${index + 1}. ${fileName}`));
|
|
754
|
+
result.recommendations.slice(0, 2).forEach((rec) => {
|
|
755
|
+
console.log(import_chalk.default.dim(` \u2022 ${rec}`));
|
|
756
|
+
});
|
|
757
|
+
});
|
|
758
|
+
console.log();
|
|
759
|
+
}
|
|
760
|
+
console.log(import_chalk.default.cyan(divider));
|
|
761
|
+
console.log(
|
|
762
|
+
import_chalk.default.dim(
|
|
763
|
+
"\n\u{1F48E} Want historical trends and refactoring plans? \u2192 aiready.dev/pro"
|
|
764
|
+
)
|
|
765
|
+
);
|
|
766
|
+
console.log(
|
|
767
|
+
import_chalk.default.dim("\u{1F4BC} Enterprise: CI/CD integration \u2192 aiready.dev/demo\n")
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
function generateHTMLReport(summary, results) {
|
|
771
|
+
const totalIssues = summary.criticalIssues + summary.majorIssues + summary.minorIssues;
|
|
772
|
+
return `<!DOCTYPE html>
|
|
773
|
+
<html lang="en">
|
|
774
|
+
<head>
|
|
775
|
+
<meta charset="UTF-8">
|
|
776
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
777
|
+
<title>AIReady Context Analysis Report</title>
|
|
778
|
+
<style>
|
|
779
|
+
body {
|
|
780
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
|
781
|
+
line-height: 1.6;
|
|
782
|
+
color: #333;
|
|
783
|
+
max-width: 1200px;
|
|
784
|
+
margin: 0 auto;
|
|
785
|
+
padding: 20px;
|
|
786
|
+
background-color: #f5f5f5;
|
|
787
|
+
}
|
|
788
|
+
h1, h2, h3 { color: #2c3e50; }
|
|
789
|
+
.header {
|
|
790
|
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
791
|
+
color: white;
|
|
792
|
+
padding: 30px;
|
|
793
|
+
border-radius: 8px;
|
|
794
|
+
margin-bottom: 30px;
|
|
795
|
+
}
|
|
796
|
+
.summary {
|
|
797
|
+
display: grid;
|
|
798
|
+
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
|
799
|
+
gap: 20px;
|
|
800
|
+
margin-bottom: 30px;
|
|
801
|
+
}
|
|
802
|
+
.card {
|
|
803
|
+
background: white;
|
|
804
|
+
padding: 20px;
|
|
805
|
+
border-radius: 8px;
|
|
806
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
807
|
+
}
|
|
808
|
+
.metric {
|
|
809
|
+
font-size: 2em;
|
|
810
|
+
font-weight: bold;
|
|
811
|
+
color: #667eea;
|
|
812
|
+
}
|
|
813
|
+
.label {
|
|
814
|
+
color: #666;
|
|
815
|
+
font-size: 0.9em;
|
|
816
|
+
margin-top: 5px;
|
|
817
|
+
}
|
|
818
|
+
.issue-critical { color: #e74c3c; }
|
|
819
|
+
.issue-major { color: #f39c12; }
|
|
820
|
+
.issue-minor { color: #3498db; }
|
|
821
|
+
table {
|
|
822
|
+
width: 100%;
|
|
823
|
+
border-collapse: collapse;
|
|
824
|
+
background: white;
|
|
825
|
+
border-radius: 8px;
|
|
826
|
+
overflow: hidden;
|
|
827
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
828
|
+
}
|
|
829
|
+
th, td {
|
|
830
|
+
padding: 12px;
|
|
831
|
+
text-align: left;
|
|
832
|
+
border-bottom: 1px solid #eee;
|
|
833
|
+
}
|
|
834
|
+
th {
|
|
835
|
+
background-color: #667eea;
|
|
836
|
+
color: white;
|
|
837
|
+
font-weight: 600;
|
|
838
|
+
}
|
|
839
|
+
tr:hover { background-color: #f8f9fa; }
|
|
840
|
+
.footer {
|
|
841
|
+
text-align: center;
|
|
842
|
+
margin-top: 40px;
|
|
843
|
+
padding: 20px;
|
|
844
|
+
color: #666;
|
|
845
|
+
font-size: 0.9em;
|
|
846
|
+
}
|
|
847
|
+
</style>
|
|
848
|
+
</head>
|
|
849
|
+
<body>
|
|
850
|
+
<div class="header">
|
|
851
|
+
<h1>\u{1F50D} AIReady Context Analysis Report</h1>
|
|
852
|
+
<p>Generated on ${(/* @__PURE__ */ new Date()).toLocaleString()}</p>
|
|
853
|
+
</div>
|
|
854
|
+
|
|
855
|
+
<div class="summary">
|
|
856
|
+
<div class="card">
|
|
857
|
+
<div class="metric">${summary.totalFiles}</div>
|
|
858
|
+
<div class="label">Files Analyzed</div>
|
|
859
|
+
</div>
|
|
860
|
+
<div class="card">
|
|
861
|
+
<div class="metric">${summary.totalTokens.toLocaleString()}</div>
|
|
862
|
+
<div class="label">Total Tokens</div>
|
|
863
|
+
</div>
|
|
864
|
+
<div class="card">
|
|
865
|
+
<div class="metric">${summary.avgContextBudget.toFixed(0)}</div>
|
|
866
|
+
<div class="label">Avg Context Budget</div>
|
|
867
|
+
</div>
|
|
868
|
+
<div class="card">
|
|
869
|
+
<div class="metric ${totalIssues > 0 ? "issue-major" : ""}">${totalIssues}</div>
|
|
870
|
+
<div class="label">Total Issues</div>
|
|
871
|
+
</div>
|
|
872
|
+
</div>
|
|
873
|
+
|
|
874
|
+
${totalIssues > 0 ? `
|
|
875
|
+
<div class="card" style="margin-bottom: 30px;">
|
|
876
|
+
<h2>\u26A0\uFE0F Issues Summary</h2>
|
|
877
|
+
<p>
|
|
878
|
+
<span class="issue-critical">\u{1F534} Critical: ${summary.criticalIssues}</span>
|
|
879
|
+
<span class="issue-major">\u{1F7E1} Major: ${summary.majorIssues}</span>
|
|
880
|
+
<span class="issue-minor">\u{1F535} Minor: ${summary.minorIssues}</span>
|
|
881
|
+
</p>
|
|
882
|
+
<p><strong>Potential Savings:</strong> ${summary.totalPotentialSavings.toLocaleString()} tokens</p>
|
|
883
|
+
</div>
|
|
884
|
+
` : ""}
|
|
885
|
+
|
|
886
|
+
${summary.fragmentedModules.length > 0 ? `
|
|
887
|
+
<div class="card" style="margin-bottom: 30px;">
|
|
888
|
+
<h2>\u{1F9E9} Fragmented Modules</h2>
|
|
889
|
+
<table>
|
|
890
|
+
<thead>
|
|
891
|
+
<tr>
|
|
892
|
+
<th>Domain</th>
|
|
893
|
+
<th>Files</th>
|
|
894
|
+
<th>Fragmentation</th>
|
|
895
|
+
<th>Token Cost</th>
|
|
896
|
+
</tr>
|
|
897
|
+
</thead>
|
|
898
|
+
<tbody>
|
|
899
|
+
${summary.fragmentedModules.map((m) => `
|
|
900
|
+
<tr>
|
|
901
|
+
<td>${m.domain}</td>
|
|
902
|
+
<td>${m.files.length}</td>
|
|
903
|
+
<td>${(m.fragmentationScore * 100).toFixed(0)}%</td>
|
|
904
|
+
<td>${m.totalTokens.toLocaleString()}</td>
|
|
905
|
+
</tr>
|
|
906
|
+
`).join("")}
|
|
907
|
+
</tbody>
|
|
908
|
+
</table>
|
|
909
|
+
</div>
|
|
910
|
+
` : ""}
|
|
911
|
+
|
|
912
|
+
${summary.topExpensiveFiles.length > 0 ? `
|
|
913
|
+
<div class="card" style="margin-bottom: 30px;">
|
|
914
|
+
<h2>\u{1F4B8} Most Expensive Files</h2>
|
|
915
|
+
<table>
|
|
916
|
+
<thead>
|
|
917
|
+
<tr>
|
|
918
|
+
<th>File</th>
|
|
919
|
+
<th>Context Budget</th>
|
|
920
|
+
<th>Severity</th>
|
|
921
|
+
</tr>
|
|
922
|
+
</thead>
|
|
923
|
+
<tbody>
|
|
924
|
+
${summary.topExpensiveFiles.map((f) => `
|
|
925
|
+
<tr>
|
|
926
|
+
<td>${f.file}</td>
|
|
927
|
+
<td>${f.contextBudget.toLocaleString()} tokens</td>
|
|
928
|
+
<td class="issue-${f.severity}">${f.severity.toUpperCase()}</td>
|
|
929
|
+
</tr>
|
|
930
|
+
`).join("")}
|
|
931
|
+
</tbody>
|
|
932
|
+
</table>
|
|
933
|
+
</div>
|
|
934
|
+
` : ""}
|
|
935
|
+
|
|
936
|
+
<div class="footer">
|
|
937
|
+
<p>Generated by <strong>@aiready/context-analyzer</strong></p>
|
|
938
|
+
<p>Want historical trends? Visit <a href="https://aiready.dev">aiready.dev</a></p>
|
|
939
|
+
</div>
|
|
940
|
+
</body>
|
|
941
|
+
</html>`;
|
|
942
|
+
}
|