@aiready/context-analyzer 0.21.13 → 0.21.15
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 +10 -10
- package/.turbo/turbo-test.log +26 -25
- package/dist/chunk-BEZPBI5C.mjs +1829 -0
- package/dist/cli.js +28 -0
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +28 -0
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
- package/src/__tests__/boilerplate-barrel.test.ts +103 -0
- package/src/classifier.ts +12 -1
- package/src/heuristics.ts +35 -1
- package/src/remediation.ts +6 -0
- package/src/types.ts +1 -0
|
@@ -0,0 +1,1829 @@
|
|
|
1
|
+
import {
|
|
2
|
+
calculateImportDepthFromEdges,
|
|
3
|
+
detectGraphCycles,
|
|
4
|
+
getTransitiveDependenciesFromEdges
|
|
5
|
+
} from "./chunk-64U3PNO3.mjs";
|
|
6
|
+
|
|
7
|
+
// src/utils/string-utils.ts
|
|
8
|
+
function singularize(word) {
|
|
9
|
+
const irregulars = {
|
|
10
|
+
people: "person",
|
|
11
|
+
children: "child",
|
|
12
|
+
men: "man",
|
|
13
|
+
women: "woman"
|
|
14
|
+
};
|
|
15
|
+
if (irregulars[word]) return irregulars[word];
|
|
16
|
+
if (word.endsWith("ies")) return word.slice(0, -3) + "y";
|
|
17
|
+
if (word.endsWith("ses")) return word.slice(0, -2);
|
|
18
|
+
if (word.endsWith("s") && word.length > 3) return word.slice(0, -1);
|
|
19
|
+
return word;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/semantic-analysis.ts
|
|
23
|
+
function buildCoUsageMatrix(graph) {
|
|
24
|
+
const coUsageMatrix = /* @__PURE__ */ new Map();
|
|
25
|
+
for (const [, node] of graph.nodes) {
|
|
26
|
+
const imports = node.imports;
|
|
27
|
+
for (let i = 0; i < imports.length; i++) {
|
|
28
|
+
const fileA = imports[i];
|
|
29
|
+
if (!coUsageMatrix.has(fileA)) coUsageMatrix.set(fileA, /* @__PURE__ */ new Map());
|
|
30
|
+
for (let j = i + 1; j < imports.length; j++) {
|
|
31
|
+
const fileB = imports[j];
|
|
32
|
+
const fileAUsage = coUsageMatrix.get(fileA);
|
|
33
|
+
fileAUsage.set(fileB, (fileAUsage.get(fileB) || 0) + 1);
|
|
34
|
+
if (!coUsageMatrix.has(fileB)) coUsageMatrix.set(fileB, /* @__PURE__ */ new Map());
|
|
35
|
+
const fileBUsage = coUsageMatrix.get(fileB);
|
|
36
|
+
fileBUsage.set(fileA, (fileBUsage.get(fileA) || 0) + 1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return coUsageMatrix;
|
|
41
|
+
}
|
|
42
|
+
function buildTypeGraph(graph) {
|
|
43
|
+
const typeGraph = /* @__PURE__ */ new Map();
|
|
44
|
+
for (const [file, node] of graph.nodes) {
|
|
45
|
+
for (const exp of node.exports) {
|
|
46
|
+
if (exp.typeReferences) {
|
|
47
|
+
for (const typeRef of exp.typeReferences) {
|
|
48
|
+
if (!typeGraph.has(typeRef)) typeGraph.set(typeRef, /* @__PURE__ */ new Set());
|
|
49
|
+
typeGraph.get(typeRef).add(file);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return typeGraph;
|
|
55
|
+
}
|
|
56
|
+
function findSemanticClusters(coUsageMatrix, minCoUsage = 3) {
|
|
57
|
+
const clusters = /* @__PURE__ */ new Map();
|
|
58
|
+
const visited = /* @__PURE__ */ new Set();
|
|
59
|
+
for (const [file, coUsages] of coUsageMatrix) {
|
|
60
|
+
if (visited.has(file)) continue;
|
|
61
|
+
const cluster = [file];
|
|
62
|
+
visited.add(file);
|
|
63
|
+
for (const [relatedFile, count] of coUsages) {
|
|
64
|
+
if (count >= minCoUsage && !visited.has(relatedFile)) {
|
|
65
|
+
cluster.push(relatedFile);
|
|
66
|
+
visited.add(relatedFile);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (cluster.length > 1) clusters.set(file, cluster);
|
|
70
|
+
}
|
|
71
|
+
return clusters;
|
|
72
|
+
}
|
|
73
|
+
function inferDomainFromSemantics(file, exportName, graph, coUsageMatrix, typeGraph, exportTypeRefs) {
|
|
74
|
+
const domainSignals = /* @__PURE__ */ new Map();
|
|
75
|
+
const coUsages = coUsageMatrix.get(file) || /* @__PURE__ */ new Map();
|
|
76
|
+
const strongCoUsages = Array.from(coUsages.entries()).filter(([, count]) => count >= 3).map(([coFile]) => coFile);
|
|
77
|
+
for (const coFile of strongCoUsages) {
|
|
78
|
+
const coNode = graph.nodes.get(coFile);
|
|
79
|
+
if (coNode) {
|
|
80
|
+
for (const exp of coNode.exports) {
|
|
81
|
+
if (exp.inferredDomain && exp.inferredDomain !== "unknown") {
|
|
82
|
+
const domain = exp.inferredDomain;
|
|
83
|
+
if (!domainSignals.has(domain)) {
|
|
84
|
+
domainSignals.set(domain, {
|
|
85
|
+
coUsage: false,
|
|
86
|
+
typeReference: false,
|
|
87
|
+
exportName: false,
|
|
88
|
+
importPath: false,
|
|
89
|
+
folderStructure: false
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
domainSignals.get(domain).coUsage = true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (exportTypeRefs) {
|
|
98
|
+
for (const typeRef of exportTypeRefs) {
|
|
99
|
+
const filesWithType = typeGraph.get(typeRef);
|
|
100
|
+
if (filesWithType) {
|
|
101
|
+
for (const typeFile of filesWithType) {
|
|
102
|
+
if (typeFile === file) continue;
|
|
103
|
+
const typeNode = graph.nodes.get(typeFile);
|
|
104
|
+
if (typeNode) {
|
|
105
|
+
for (const exp of typeNode.exports) {
|
|
106
|
+
if (exp.inferredDomain && exp.inferredDomain !== "unknown") {
|
|
107
|
+
const domain = exp.inferredDomain;
|
|
108
|
+
if (!domainSignals.has(domain)) {
|
|
109
|
+
domainSignals.set(domain, {
|
|
110
|
+
coUsage: false,
|
|
111
|
+
typeReference: false,
|
|
112
|
+
exportName: false,
|
|
113
|
+
importPath: false,
|
|
114
|
+
folderStructure: false
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
domainSignals.get(domain).typeReference = true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const assignments = [];
|
|
126
|
+
for (const [domain, signals] of domainSignals) {
|
|
127
|
+
const confidence = calculateDomainConfidence(signals);
|
|
128
|
+
if (confidence >= 0.3) assignments.push({ domain, confidence, signals });
|
|
129
|
+
}
|
|
130
|
+
assignments.sort((a, b) => b.confidence - a.confidence);
|
|
131
|
+
return assignments;
|
|
132
|
+
}
|
|
133
|
+
function calculateDomainConfidence(signals) {
|
|
134
|
+
const weights = {
|
|
135
|
+
coUsage: 0.35,
|
|
136
|
+
typeReference: 0.3,
|
|
137
|
+
exportName: 0.15,
|
|
138
|
+
importPath: 0.1,
|
|
139
|
+
folderStructure: 0.1
|
|
140
|
+
};
|
|
141
|
+
let confidence = 0;
|
|
142
|
+
if (signals.coUsage) confidence += weights.coUsage;
|
|
143
|
+
if (signals.typeReference) confidence += weights.typeReference;
|
|
144
|
+
if (signals.exportName) confidence += weights.exportName;
|
|
145
|
+
if (signals.importPath) confidence += weights.importPath;
|
|
146
|
+
if (signals.folderStructure) confidence += weights.folderStructure;
|
|
147
|
+
return confidence;
|
|
148
|
+
}
|
|
149
|
+
function extractExports(content, filePath, domainOptions, fileImports) {
|
|
150
|
+
const exports = [];
|
|
151
|
+
const patterns = [
|
|
152
|
+
/export\s+function\s+(\w+)/g,
|
|
153
|
+
/export\s+class\s+(\w+)/g,
|
|
154
|
+
/export\s+const\s+(\w+)/g,
|
|
155
|
+
/export\s+type\s+(\w+)/g,
|
|
156
|
+
/export\s+interface\s+(\w+)/g,
|
|
157
|
+
/export\s+default/g
|
|
158
|
+
];
|
|
159
|
+
const types = [
|
|
160
|
+
"function",
|
|
161
|
+
"class",
|
|
162
|
+
"const",
|
|
163
|
+
"type",
|
|
164
|
+
"interface",
|
|
165
|
+
"default"
|
|
166
|
+
];
|
|
167
|
+
patterns.forEach((pattern, index) => {
|
|
168
|
+
let match;
|
|
169
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
170
|
+
const name = match[1] || "default";
|
|
171
|
+
const type = types[index];
|
|
172
|
+
const inferredDomain = inferDomain(
|
|
173
|
+
name,
|
|
174
|
+
filePath,
|
|
175
|
+
domainOptions,
|
|
176
|
+
fileImports
|
|
177
|
+
);
|
|
178
|
+
exports.push({ name, type, inferredDomain });
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
return exports;
|
|
182
|
+
}
|
|
183
|
+
function inferDomain(name, filePath, domainOptions, fileImports) {
|
|
184
|
+
const lower = name.toLowerCase();
|
|
185
|
+
const tokens = Array.from(
|
|
186
|
+
new Set(
|
|
187
|
+
lower.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[^a-z0-9]+/gi, " ").split(" ").filter(Boolean)
|
|
188
|
+
)
|
|
189
|
+
);
|
|
190
|
+
const defaultKeywords = [
|
|
191
|
+
"authentication",
|
|
192
|
+
"authorization",
|
|
193
|
+
"payment",
|
|
194
|
+
"invoice",
|
|
195
|
+
"customer",
|
|
196
|
+
"product",
|
|
197
|
+
"order",
|
|
198
|
+
"cart",
|
|
199
|
+
"user",
|
|
200
|
+
"admin",
|
|
201
|
+
"repository",
|
|
202
|
+
"controller",
|
|
203
|
+
"service",
|
|
204
|
+
"config",
|
|
205
|
+
"model",
|
|
206
|
+
"view",
|
|
207
|
+
"auth"
|
|
208
|
+
];
|
|
209
|
+
const domainKeywords = domainOptions?.domainKeywords?.length ? [...domainOptions.domainKeywords, ...defaultKeywords] : defaultKeywords;
|
|
210
|
+
for (const keyword of domainKeywords) {
|
|
211
|
+
if (tokens.includes(keyword)) return keyword;
|
|
212
|
+
}
|
|
213
|
+
for (const keyword of domainKeywords) {
|
|
214
|
+
if (lower.includes(keyword)) return keyword;
|
|
215
|
+
}
|
|
216
|
+
if (fileImports) {
|
|
217
|
+
for (const importPath of fileImports) {
|
|
218
|
+
const segments = importPath.split("/");
|
|
219
|
+
for (const segment of segments) {
|
|
220
|
+
const segLower = segment.toLowerCase();
|
|
221
|
+
const singularSegment = singularize(segLower);
|
|
222
|
+
for (const keyword of domainKeywords) {
|
|
223
|
+
if (singularSegment === keyword || segLower === keyword || segLower.includes(keyword))
|
|
224
|
+
return keyword;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (filePath) {
|
|
230
|
+
const segments = filePath.split("/");
|
|
231
|
+
for (const segment of segments) {
|
|
232
|
+
const segLower = segment.toLowerCase();
|
|
233
|
+
const singularSegment = singularize(segLower);
|
|
234
|
+
for (const keyword of domainKeywords) {
|
|
235
|
+
if (singularSegment === keyword || segLower === keyword) return keyword;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return "unknown";
|
|
240
|
+
}
|
|
241
|
+
function getCoUsageData(file, coUsageMatrix) {
|
|
242
|
+
return {
|
|
243
|
+
file,
|
|
244
|
+
coImportedWith: coUsageMatrix.get(file) || /* @__PURE__ */ new Map(),
|
|
245
|
+
sharedImporters: []
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function findConsolidationCandidates(graph, coUsageMatrix, typeGraph, minCoUsage = 5, minSharedTypes = 2) {
|
|
249
|
+
const candidates = [];
|
|
250
|
+
for (const [fileA, coUsages] of coUsageMatrix) {
|
|
251
|
+
const nodeA = graph.nodes.get(fileA);
|
|
252
|
+
if (!nodeA) continue;
|
|
253
|
+
for (const [fileB, count] of coUsages) {
|
|
254
|
+
if (fileB <= fileA || count < minCoUsage) continue;
|
|
255
|
+
const nodeB = graph.nodes.get(fileB);
|
|
256
|
+
if (!nodeB) continue;
|
|
257
|
+
const typesA = new Set(
|
|
258
|
+
nodeA.exports.flatMap((e) => e.typeReferences || [])
|
|
259
|
+
);
|
|
260
|
+
const typesB = new Set(
|
|
261
|
+
nodeB.exports.flatMap((e) => e.typeReferences || [])
|
|
262
|
+
);
|
|
263
|
+
const sharedTypes = Array.from(typesA).filter((t) => typesB.has(t));
|
|
264
|
+
if (sharedTypes.length >= minSharedTypes || count >= minCoUsage * 2) {
|
|
265
|
+
candidates.push({
|
|
266
|
+
files: [fileA, fileB],
|
|
267
|
+
reason: `High co-usage (${count}x)`,
|
|
268
|
+
strength: count / 10
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return candidates.sort((a, b) => b.strength - a.strength);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// src/metrics.ts
|
|
277
|
+
import { calculateImportSimilarity } from "@aiready/core";
|
|
278
|
+
|
|
279
|
+
// src/ast-utils.ts
|
|
280
|
+
import { parseFileExports } from "@aiready/core";
|
|
281
|
+
function extractExportsWithAST(content, filePath, domainOptions, fileImports) {
|
|
282
|
+
try {
|
|
283
|
+
const { exports: astExports } = parseFileExports(content, filePath);
|
|
284
|
+
if (astExports.length === 0 && !isTestFile(filePath)) {
|
|
285
|
+
return extractExports(content, filePath, domainOptions, fileImports);
|
|
286
|
+
}
|
|
287
|
+
return astExports.map((exp) => ({
|
|
288
|
+
name: exp.name,
|
|
289
|
+
type: exp.type,
|
|
290
|
+
inferredDomain: inferDomain(
|
|
291
|
+
exp.name,
|
|
292
|
+
filePath,
|
|
293
|
+
domainOptions,
|
|
294
|
+
fileImports
|
|
295
|
+
),
|
|
296
|
+
imports: exp.imports,
|
|
297
|
+
dependencies: exp.dependencies,
|
|
298
|
+
typeReferences: exp.typeReferences
|
|
299
|
+
}));
|
|
300
|
+
} catch {
|
|
301
|
+
return extractExports(content, filePath, domainOptions, fileImports);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function isTestFile(filePath) {
|
|
305
|
+
const lower = filePath.toLowerCase();
|
|
306
|
+
return lower.includes(".test.") || lower.includes(".spec.") || lower.includes("/__tests__/") || lower.includes("/tests/") || lower.includes("/test/") || lower.includes("test-") || lower.includes("-test") || lower.includes("/__mocks__/") || lower.includes("/mocks/") || lower.includes("/fixtures/") || lower.includes(".mock.") || lower.includes(".fixture.") || lower.includes("/test-utils/");
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// src/metrics.ts
|
|
310
|
+
function calculateEnhancedCohesion(exports, filePath, options) {
|
|
311
|
+
if (exports.length <= 1) return 1;
|
|
312
|
+
if (filePath && isTestFile(filePath)) return 1;
|
|
313
|
+
const domains = exports.map((e) => e.inferredDomain || "unknown");
|
|
314
|
+
const domainCounts = /* @__PURE__ */ new Map();
|
|
315
|
+
for (const domain of domains)
|
|
316
|
+
domainCounts.set(domain, (domainCounts.get(domain) || 0) + 1);
|
|
317
|
+
if (domainCounts.size === 1 && domains[0] !== "unknown") {
|
|
318
|
+
if (!options?.weights) return 1;
|
|
319
|
+
}
|
|
320
|
+
const probs = Array.from(domainCounts.values()).map(
|
|
321
|
+
(count) => count / exports.length
|
|
322
|
+
);
|
|
323
|
+
let domainEntropy = 0;
|
|
324
|
+
for (const prob of probs) {
|
|
325
|
+
if (prob > 0) domainEntropy -= prob * Math.log2(prob);
|
|
326
|
+
}
|
|
327
|
+
const maxEntropy = Math.log2(Math.max(2, domainCounts.size));
|
|
328
|
+
const domainScore = 1 - domainEntropy / maxEntropy;
|
|
329
|
+
let importScoreTotal = 0;
|
|
330
|
+
let pairsWithData = 0;
|
|
331
|
+
let anyImportData = false;
|
|
332
|
+
for (let i = 0; i < exports.length; i++) {
|
|
333
|
+
for (let j = i + 1; j < exports.length; j++) {
|
|
334
|
+
const exp1Imports = exports[i].imports;
|
|
335
|
+
const exp2Imports = exports[j].imports;
|
|
336
|
+
if (exp1Imports || exp2Imports) {
|
|
337
|
+
anyImportData = true;
|
|
338
|
+
const sim = calculateImportSimilarity(
|
|
339
|
+
{ ...exports[i], imports: exp1Imports || [] },
|
|
340
|
+
{ ...exports[j], imports: exp2Imports || [] }
|
|
341
|
+
);
|
|
342
|
+
importScoreTotal += sim;
|
|
343
|
+
pairsWithData++;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const avgImportScore = pairsWithData > 0 ? importScoreTotal / pairsWithData : 0;
|
|
348
|
+
let score = anyImportData ? domainScore * 0.4 + avgImportScore * 0.6 : domainScore;
|
|
349
|
+
if (anyImportData && score === 0 && domainScore === 0) {
|
|
350
|
+
score = 0.1;
|
|
351
|
+
}
|
|
352
|
+
let structuralScore = 0;
|
|
353
|
+
for (const exp of exports) {
|
|
354
|
+
if (exp.dependencies && exp.dependencies.length > 0) {
|
|
355
|
+
structuralScore += 1;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (structuralScore > 0) {
|
|
359
|
+
score = Math.min(1, score + 0.1);
|
|
360
|
+
}
|
|
361
|
+
if (!options?.weights && !anyImportData && domainCounts.size === 1) return 1;
|
|
362
|
+
return score;
|
|
363
|
+
}
|
|
364
|
+
function calculateStructuralCohesionFromCoUsage(file, coUsageMatrix) {
|
|
365
|
+
if (!coUsageMatrix) return 1;
|
|
366
|
+
const coUsages = coUsageMatrix.get(file);
|
|
367
|
+
if (!coUsages || coUsages.size === 0) return 1;
|
|
368
|
+
let total = 0;
|
|
369
|
+
for (const count of coUsages.values()) total += count;
|
|
370
|
+
if (total === 0) return 1;
|
|
371
|
+
const probs = [];
|
|
372
|
+
for (const count of coUsages.values()) {
|
|
373
|
+
if (count > 0) probs.push(count / total);
|
|
374
|
+
}
|
|
375
|
+
if (probs.length <= 1) return 1;
|
|
376
|
+
let entropy = 0;
|
|
377
|
+
for (const prob of probs) {
|
|
378
|
+
entropy -= prob * Math.log2(prob);
|
|
379
|
+
}
|
|
380
|
+
const maxEntropy = Math.log2(probs.length);
|
|
381
|
+
return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
|
|
382
|
+
}
|
|
383
|
+
function calculateFragmentation(files, domain, options) {
|
|
384
|
+
if (files.length <= 1) return 0;
|
|
385
|
+
const directories = new Set(
|
|
386
|
+
files.map((file) => file.split("/").slice(0, -1).join("/"))
|
|
387
|
+
);
|
|
388
|
+
const uniqueDirs = directories.size;
|
|
389
|
+
let score = options?.useLogScale ? uniqueDirs <= 1 ? 0 : Math.log(uniqueDirs) / Math.log(options.logBase || Math.E) / (Math.log(files.length) / Math.log(options.logBase || Math.E)) : (uniqueDirs - 1) / (files.length - 1);
|
|
390
|
+
if (options?.sharedImportRatio && options.sharedImportRatio > 0.5) {
|
|
391
|
+
const discount = (options.sharedImportRatio - 0.5) * 0.4;
|
|
392
|
+
score = score * (1 - discount);
|
|
393
|
+
}
|
|
394
|
+
return score;
|
|
395
|
+
}
|
|
396
|
+
function calculatePathEntropy(files) {
|
|
397
|
+
if (!files || files.length === 0) return 0;
|
|
398
|
+
const dirCounts = /* @__PURE__ */ new Map();
|
|
399
|
+
for (const file of files) {
|
|
400
|
+
const dir = file.split("/").slice(0, -1).join("/") || ".";
|
|
401
|
+
dirCounts.set(dir, (dirCounts.get(dir) || 0) + 1);
|
|
402
|
+
}
|
|
403
|
+
const counts = Array.from(dirCounts.values());
|
|
404
|
+
if (counts.length <= 1) return 0;
|
|
405
|
+
const total = counts.reduce((sum, value) => sum + value, 0);
|
|
406
|
+
let entropy = 0;
|
|
407
|
+
for (const count of counts) {
|
|
408
|
+
const prob = count / total;
|
|
409
|
+
entropy -= prob * Math.log2(prob);
|
|
410
|
+
}
|
|
411
|
+
const maxEntropy = Math.log2(counts.length);
|
|
412
|
+
return maxEntropy > 0 ? entropy / maxEntropy : 0;
|
|
413
|
+
}
|
|
414
|
+
function calculateDirectoryDistance(files) {
|
|
415
|
+
if (!files || files.length <= 1) return 0;
|
|
416
|
+
const pathSegments = (pathStr) => pathStr.split("/").filter(Boolean);
|
|
417
|
+
const commonAncestorDepth = (pathA, pathB) => {
|
|
418
|
+
const minLen = Math.min(pathA.length, pathB.length);
|
|
419
|
+
let i = 0;
|
|
420
|
+
while (i < minLen && pathA[i] === pathB[i]) i++;
|
|
421
|
+
return i;
|
|
422
|
+
};
|
|
423
|
+
let totalNormalized = 0;
|
|
424
|
+
let comparisons = 0;
|
|
425
|
+
for (let i = 0; i < files.length; i++) {
|
|
426
|
+
for (let j = i + 1; j < files.length; j++) {
|
|
427
|
+
const segA = pathSegments(files[i]);
|
|
428
|
+
const segB = pathSegments(files[j]);
|
|
429
|
+
const shared = commonAncestorDepth(segA, segB);
|
|
430
|
+
const maxDepth = Math.max(segA.length, segB.length);
|
|
431
|
+
totalNormalized += 1 - (maxDepth > 0 ? shared / maxDepth : 0);
|
|
432
|
+
comparisons++;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return comparisons > 0 ? totalNormalized / comparisons : 0;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// src/graph-builder.ts
|
|
439
|
+
import { estimateTokens, parseFileExports as parseFileExports2 } from "@aiready/core";
|
|
440
|
+
import { join, dirname, normalize } from "path";
|
|
441
|
+
function resolveImport(source, importingFile, allFiles) {
|
|
442
|
+
if (!source.startsWith(".") && !source.startsWith("/")) {
|
|
443
|
+
if (allFiles.has(source)) return source;
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
const dir = dirname(importingFile);
|
|
447
|
+
const absolutePath = normalize(join(dir, source));
|
|
448
|
+
if (allFiles.has(absolutePath)) return absolutePath;
|
|
449
|
+
const extensions = [".ts", ".tsx", ".js", ".jsx"];
|
|
450
|
+
for (const ext of extensions) {
|
|
451
|
+
const withExt = absolutePath + ext;
|
|
452
|
+
if (allFiles.has(withExt)) return withExt;
|
|
453
|
+
}
|
|
454
|
+
for (const ext of extensions) {
|
|
455
|
+
const indexFile = normalize(join(absolutePath, `index${ext}`));
|
|
456
|
+
if (allFiles.has(indexFile)) return indexFile;
|
|
457
|
+
}
|
|
458
|
+
return null;
|
|
459
|
+
}
|
|
460
|
+
function extractDomainKeywordsFromPaths(files) {
|
|
461
|
+
const folderNames = /* @__PURE__ */ new Set();
|
|
462
|
+
for (const { file } of files) {
|
|
463
|
+
const segments = file.split("/");
|
|
464
|
+
const skipFolders = /* @__PURE__ */ new Set([
|
|
465
|
+
"src",
|
|
466
|
+
"lib",
|
|
467
|
+
"dist",
|
|
468
|
+
"build",
|
|
469
|
+
"node_modules",
|
|
470
|
+
"test",
|
|
471
|
+
"tests",
|
|
472
|
+
"__tests__",
|
|
473
|
+
"spec",
|
|
474
|
+
"e2e",
|
|
475
|
+
"scripts",
|
|
476
|
+
"components",
|
|
477
|
+
"utils",
|
|
478
|
+
"helpers",
|
|
479
|
+
"util",
|
|
480
|
+
"helper",
|
|
481
|
+
"api",
|
|
482
|
+
"apis"
|
|
483
|
+
]);
|
|
484
|
+
for (const segment of segments) {
|
|
485
|
+
const normalized = segment.toLowerCase();
|
|
486
|
+
if (normalized && !skipFolders.has(normalized) && !normalized.includes(".")) {
|
|
487
|
+
folderNames.add(singularize(normalized));
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return Array.from(folderNames);
|
|
492
|
+
}
|
|
493
|
+
function buildDependencyGraph(files, options) {
|
|
494
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
495
|
+
const edges = /* @__PURE__ */ new Map();
|
|
496
|
+
const autoDetectedKeywords = options?.domainKeywords ?? extractDomainKeywordsFromPaths(files);
|
|
497
|
+
const allFilePaths = new Set(files.map((f) => f.file));
|
|
498
|
+
for (const { file, content } of files) {
|
|
499
|
+
const { imports: astImports } = parseFileExports2(content, file);
|
|
500
|
+
const resolvedImports = astImports.map((i) => resolveImport(i.source, file, allFilePaths)).filter((path) => path !== null);
|
|
501
|
+
const importSources = astImports.map((i) => i.source);
|
|
502
|
+
const exports = extractExportsWithAST(
|
|
503
|
+
content,
|
|
504
|
+
file,
|
|
505
|
+
{ domainKeywords: autoDetectedKeywords },
|
|
506
|
+
importSources
|
|
507
|
+
);
|
|
508
|
+
const tokenCost = estimateTokens(content);
|
|
509
|
+
const linesOfCode = content.split("\n").length;
|
|
510
|
+
nodes.set(file, {
|
|
511
|
+
file,
|
|
512
|
+
imports: importSources,
|
|
513
|
+
exports,
|
|
514
|
+
tokenCost,
|
|
515
|
+
linesOfCode
|
|
516
|
+
});
|
|
517
|
+
edges.set(file, new Set(resolvedImports));
|
|
518
|
+
}
|
|
519
|
+
const graph = { nodes, edges };
|
|
520
|
+
const coUsageMatrix = buildCoUsageMatrix(graph);
|
|
521
|
+
const typeGraph = buildTypeGraph(graph);
|
|
522
|
+
graph.coUsageMatrix = coUsageMatrix;
|
|
523
|
+
graph.typeGraph = typeGraph;
|
|
524
|
+
for (const [file, node] of nodes) {
|
|
525
|
+
for (const exp of node.exports) {
|
|
526
|
+
const semanticAssignments = inferDomainFromSemantics(
|
|
527
|
+
file,
|
|
528
|
+
exp.name,
|
|
529
|
+
graph,
|
|
530
|
+
coUsageMatrix,
|
|
531
|
+
typeGraph,
|
|
532
|
+
exp.typeReferences
|
|
533
|
+
);
|
|
534
|
+
exp.domains = semanticAssignments;
|
|
535
|
+
if (semanticAssignments.length > 0) {
|
|
536
|
+
exp.inferredDomain = semanticAssignments[0].domain;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return graph;
|
|
541
|
+
}
|
|
542
|
+
function calculateImportDepth(file, graph, visited = /* @__PURE__ */ new Set(), depth = 0) {
|
|
543
|
+
return calculateImportDepthFromEdges(file, graph.edges, visited, depth);
|
|
544
|
+
}
|
|
545
|
+
function getTransitiveDependencies(file, graph, visited = /* @__PURE__ */ new Set()) {
|
|
546
|
+
return getTransitiveDependenciesFromEdges(file, graph.edges, visited);
|
|
547
|
+
}
|
|
548
|
+
function calculateContextBudget(file, graph) {
|
|
549
|
+
const node = graph.nodes.get(file);
|
|
550
|
+
if (!node) return 0;
|
|
551
|
+
let totalTokens = node.tokenCost;
|
|
552
|
+
const deps = getTransitiveDependencies(file, graph);
|
|
553
|
+
for (const dep of deps) {
|
|
554
|
+
const depNode = graph.nodes.get(dep);
|
|
555
|
+
if (depNode) {
|
|
556
|
+
totalTokens += depNode.tokenCost;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return totalTokens;
|
|
560
|
+
}
|
|
561
|
+
function detectCircularDependencies(graph) {
|
|
562
|
+
return detectGraphCycles(graph.edges);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// src/heuristics.ts
|
|
566
|
+
var BARREL_EXPORT_MIN_EXPORTS = 5;
|
|
567
|
+
var BARREL_EXPORT_TOKEN_LIMIT = 1e3;
|
|
568
|
+
var HANDLER_NAME_PATTERNS = [
|
|
569
|
+
"handler",
|
|
570
|
+
".handler.",
|
|
571
|
+
"-handler.",
|
|
572
|
+
"lambda",
|
|
573
|
+
".lambda.",
|
|
574
|
+
"-lambda."
|
|
575
|
+
];
|
|
576
|
+
var SERVICE_NAME_PATTERNS = [
|
|
577
|
+
"service",
|
|
578
|
+
".service.",
|
|
579
|
+
"-service.",
|
|
580
|
+
"_service."
|
|
581
|
+
];
|
|
582
|
+
var EMAIL_NAME_PATTERNS = [
|
|
583
|
+
"-email-",
|
|
584
|
+
".email.",
|
|
585
|
+
"_email_",
|
|
586
|
+
"-template",
|
|
587
|
+
".template.",
|
|
588
|
+
"_template",
|
|
589
|
+
"-mail.",
|
|
590
|
+
".mail."
|
|
591
|
+
];
|
|
592
|
+
var PARSER_NAME_PATTERNS = [
|
|
593
|
+
"parser",
|
|
594
|
+
".parser.",
|
|
595
|
+
"-parser.",
|
|
596
|
+
"_parser.",
|
|
597
|
+
"transform",
|
|
598
|
+
"converter",
|
|
599
|
+
"mapper",
|
|
600
|
+
"serializer"
|
|
601
|
+
];
|
|
602
|
+
var SESSION_NAME_PATTERNS = ["session", "state", "context", "store"];
|
|
603
|
+
var NEXTJS_METADATA_EXPORTS = [
|
|
604
|
+
"metadata",
|
|
605
|
+
"generatemetadata",
|
|
606
|
+
"faqjsonld",
|
|
607
|
+
"jsonld",
|
|
608
|
+
"icon"
|
|
609
|
+
];
|
|
610
|
+
var CONFIG_NAME_PATTERNS = [
|
|
611
|
+
".config.",
|
|
612
|
+
"tsconfig",
|
|
613
|
+
"jest.config",
|
|
614
|
+
"package.json",
|
|
615
|
+
"aiready.json",
|
|
616
|
+
"next.config",
|
|
617
|
+
"sst.config"
|
|
618
|
+
];
|
|
619
|
+
function isBoilerplateBarrel(node) {
|
|
620
|
+
const { exports, tokenCost } = node;
|
|
621
|
+
if (!exports || exports.length === 0) return false;
|
|
622
|
+
const isPurelyReexports = exports.every((exp) => !!exp.source);
|
|
623
|
+
if (!isPurelyReexports) return false;
|
|
624
|
+
if (tokenCost > 500) return false;
|
|
625
|
+
const sources = new Set(exports.map((exp) => exp.source));
|
|
626
|
+
const isSingleSourcePassThrough = sources.size === 1;
|
|
627
|
+
const isMeaninglessAggregation = sources.size > 0 && sources.size < 3;
|
|
628
|
+
return isSingleSourcePassThrough || isMeaninglessAggregation;
|
|
629
|
+
}
|
|
630
|
+
function isBarrelExport(node) {
|
|
631
|
+
if (isBoilerplateBarrel(node)) return false;
|
|
632
|
+
const { file, exports } = node;
|
|
633
|
+
const fileName = file.split("/").pop()?.toLowerCase();
|
|
634
|
+
const isIndexFile = fileName === "index.ts" || fileName === "index.js";
|
|
635
|
+
const isSmallAndManyExports = node.tokenCost < BARREL_EXPORT_TOKEN_LIMIT && (exports || []).length > BARREL_EXPORT_MIN_EXPORTS;
|
|
636
|
+
const isReexportPattern = (exports || []).length >= BARREL_EXPORT_MIN_EXPORTS && (exports || []).every(
|
|
637
|
+
(exp) => ["const", "function", "type", "interface"].includes(exp.type)
|
|
638
|
+
);
|
|
639
|
+
return !!isIndexFile || !!isSmallAndManyExports || !!isReexportPattern;
|
|
640
|
+
}
|
|
641
|
+
function isTypeDefinition(node) {
|
|
642
|
+
const { file } = node;
|
|
643
|
+
if (file.endsWith(".d.ts")) return true;
|
|
644
|
+
const nodeExports = node.exports || [];
|
|
645
|
+
const hasExports = nodeExports.length > 0;
|
|
646
|
+
const areAllTypes = hasExports && nodeExports.every(
|
|
647
|
+
(exp) => exp.type === "type" || exp.type === "interface"
|
|
648
|
+
);
|
|
649
|
+
const isTypePath = /\/(types|interfaces|models)\//i.test(file);
|
|
650
|
+
return !!areAllTypes || isTypePath && hasExports;
|
|
651
|
+
}
|
|
652
|
+
function isUtilityModule(node) {
|
|
653
|
+
const { file } = node;
|
|
654
|
+
const isUtilPath = /\/(utils|helpers|util|helper)\//i.test(file);
|
|
655
|
+
const fileName = file.split("/").pop()?.toLowerCase() || "";
|
|
656
|
+
const isUtilName = /(utils\.|helpers\.|util\.|helper\.)/i.test(fileName);
|
|
657
|
+
return isUtilPath || isUtilName;
|
|
658
|
+
}
|
|
659
|
+
function isLambdaHandler(node) {
|
|
660
|
+
const { file, exports } = node;
|
|
661
|
+
const fileName = file.split("/").pop()?.toLowerCase() || "";
|
|
662
|
+
const isHandlerName = HANDLER_NAME_PATTERNS.some(
|
|
663
|
+
(pattern) => fileName.includes(pattern)
|
|
664
|
+
);
|
|
665
|
+
const isHandlerPath = /\/(handlers|lambdas|lambda|functions)\//i.test(file);
|
|
666
|
+
const hasHandlerExport = (exports || []).some(
|
|
667
|
+
(exp) => ["handler", "main", "lambdahandler"].includes(exp.name.toLowerCase()) || exp.name.toLowerCase().endsWith("handler")
|
|
668
|
+
);
|
|
669
|
+
return isHandlerName || isHandlerPath || hasHandlerExport;
|
|
670
|
+
}
|
|
671
|
+
function isServiceFile(node) {
|
|
672
|
+
const { file, exports } = node;
|
|
673
|
+
const fileName = file.split("/").pop()?.toLowerCase() || "";
|
|
674
|
+
const isServiceName = SERVICE_NAME_PATTERNS.some(
|
|
675
|
+
(pattern) => fileName.includes(pattern)
|
|
676
|
+
);
|
|
677
|
+
const isServicePath = file.toLowerCase().includes("/services/");
|
|
678
|
+
const hasServiceNamedExport = (exports || []).some(
|
|
679
|
+
(exp) => exp.name.toLowerCase().includes("service")
|
|
680
|
+
);
|
|
681
|
+
const hasClassExport = (exports || []).some(
|
|
682
|
+
(exp) => exp.type === "class"
|
|
683
|
+
);
|
|
684
|
+
return isServiceName || isServicePath || hasServiceNamedExport && hasClassExport;
|
|
685
|
+
}
|
|
686
|
+
function isEmailTemplate(node) {
|
|
687
|
+
const { file, exports } = node;
|
|
688
|
+
const fileName = file.split("/").pop()?.toLowerCase() || "";
|
|
689
|
+
const isEmailName = EMAIL_NAME_PATTERNS.some(
|
|
690
|
+
(pattern) => fileName.includes(pattern)
|
|
691
|
+
);
|
|
692
|
+
const isEmailPath = /\/(emails|mail|notifications)\//i.test(file);
|
|
693
|
+
const hasTemplateFunction = (exports || []).some(
|
|
694
|
+
(exp) => exp.type === "function" && (exp.name.toLowerCase().startsWith("render") || exp.name.toLowerCase().startsWith("generate"))
|
|
695
|
+
);
|
|
696
|
+
return isEmailPath || isEmailName || hasTemplateFunction;
|
|
697
|
+
}
|
|
698
|
+
function isParserFile(node) {
|
|
699
|
+
const { file, exports } = node;
|
|
700
|
+
const fileName = file.split("/").pop()?.toLowerCase() || "";
|
|
701
|
+
const isParserName = PARSER_NAME_PATTERNS.some(
|
|
702
|
+
(pattern) => fileName.includes(pattern)
|
|
703
|
+
);
|
|
704
|
+
const isParserPath = /\/(parsers|transformers)\//i.test(file);
|
|
705
|
+
const hasParseFunction = (exports || []).some(
|
|
706
|
+
(exp) => exp.type === "function" && (exp.name.toLowerCase().startsWith("parse") || exp.name.toLowerCase().startsWith("transform"))
|
|
707
|
+
);
|
|
708
|
+
return isParserName || isParserPath || hasParseFunction;
|
|
709
|
+
}
|
|
710
|
+
function isSessionFile(node) {
|
|
711
|
+
const { file, exports } = node;
|
|
712
|
+
const fileName = file.split("/").pop()?.toLowerCase() || "";
|
|
713
|
+
const isSessionName = SESSION_NAME_PATTERNS.some(
|
|
714
|
+
(pattern) => fileName.includes(pattern)
|
|
715
|
+
);
|
|
716
|
+
const isSessionPath = /\/(sessions|state)\//i.test(file);
|
|
717
|
+
const hasSessionExport = (exports || []).some(
|
|
718
|
+
(exp) => ["session", "state", "store"].some(
|
|
719
|
+
(pattern) => exp.name.toLowerCase().includes(pattern)
|
|
720
|
+
)
|
|
721
|
+
);
|
|
722
|
+
return isSessionName || isSessionPath || hasSessionExport;
|
|
723
|
+
}
|
|
724
|
+
function isNextJsPage(node) {
|
|
725
|
+
const { file, exports } = node;
|
|
726
|
+
const lowerPath = file.toLowerCase();
|
|
727
|
+
const fileName = file.split("/").pop()?.toLowerCase() || "";
|
|
728
|
+
const isInAppDir = lowerPath.includes("/app/") || lowerPath.startsWith("app/");
|
|
729
|
+
if (!isInAppDir || fileName !== "page.tsx" && fileName !== "page.ts")
|
|
730
|
+
return false;
|
|
731
|
+
const hasDefaultExport = (exports || []).some(
|
|
732
|
+
(exp) => exp.type === "default"
|
|
733
|
+
);
|
|
734
|
+
const hasNextJsExport = (exports || []).some(
|
|
735
|
+
(exp) => NEXTJS_METADATA_EXPORTS.includes(exp.name.toLowerCase())
|
|
736
|
+
);
|
|
737
|
+
return hasDefaultExport || hasNextJsExport;
|
|
738
|
+
}
|
|
739
|
+
function isConfigFile(node) {
|
|
740
|
+
const { file, exports } = node;
|
|
741
|
+
const lowerPath = file.toLowerCase();
|
|
742
|
+
const fileName = file.split("/").pop()?.toLowerCase() || "";
|
|
743
|
+
const isConfigName = CONFIG_NAME_PATTERNS.some(
|
|
744
|
+
(pattern) => fileName.includes(pattern)
|
|
745
|
+
);
|
|
746
|
+
const isConfigPath = /\/(config|settings|schemas)\//i.test(lowerPath);
|
|
747
|
+
const hasSchemaExport = (exports || []).some(
|
|
748
|
+
(exp) => ["schema", "config", "setting"].some(
|
|
749
|
+
(pattern) => exp.name.toLowerCase().includes(pattern)
|
|
750
|
+
)
|
|
751
|
+
);
|
|
752
|
+
return isConfigName || isConfigPath || hasSchemaExport;
|
|
753
|
+
}
|
|
754
|
+
function isHubAndSpokeFile(node) {
|
|
755
|
+
const { file } = node;
|
|
756
|
+
return /\/packages\/[a-zA-Z0-9-]+\/src\//.test(file);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// src/classifier.ts
|
|
760
|
+
var Classification = {
|
|
761
|
+
BARREL: "barrel-export",
|
|
762
|
+
BOILERPLATE: "boilerplate-barrel",
|
|
763
|
+
TYPE_DEFINITION: "type-definition",
|
|
764
|
+
NEXTJS_PAGE: "nextjs-page",
|
|
765
|
+
LAMBDA_HANDLER: "lambda-handler",
|
|
766
|
+
SERVICE: "service-file",
|
|
767
|
+
EMAIL_TEMPLATE: "email-template",
|
|
768
|
+
PARSER: "parser-file",
|
|
769
|
+
COHESIVE_MODULE: "cohesive-module",
|
|
770
|
+
UTILITY_MODULE: "utility-module",
|
|
771
|
+
SPOKE_MODULE: "spoke-module",
|
|
772
|
+
MIXED_CONCERNS: "mixed-concerns",
|
|
773
|
+
UNKNOWN: "unknown"
|
|
774
|
+
};
|
|
775
|
+
function classifyFile(node, cohesionScore = 1, domains = []) {
|
|
776
|
+
if (isBoilerplateBarrel(node)) {
|
|
777
|
+
return Classification.BOILERPLATE;
|
|
778
|
+
}
|
|
779
|
+
if (isBarrelExport(node)) {
|
|
780
|
+
return Classification.BARREL;
|
|
781
|
+
}
|
|
782
|
+
if (isTypeDefinition(node)) {
|
|
783
|
+
return Classification.TYPE_DEFINITION;
|
|
784
|
+
}
|
|
785
|
+
if (isNextJsPage(node)) {
|
|
786
|
+
return Classification.NEXTJS_PAGE;
|
|
787
|
+
}
|
|
788
|
+
if (isLambdaHandler(node)) {
|
|
789
|
+
return Classification.LAMBDA_HANDLER;
|
|
790
|
+
}
|
|
791
|
+
if (isServiceFile(node)) {
|
|
792
|
+
return Classification.SERVICE;
|
|
793
|
+
}
|
|
794
|
+
if (isEmailTemplate(node)) {
|
|
795
|
+
return Classification.EMAIL_TEMPLATE;
|
|
796
|
+
}
|
|
797
|
+
if (isParserFile(node)) {
|
|
798
|
+
return Classification.PARSER;
|
|
799
|
+
}
|
|
800
|
+
if (isSessionFile(node)) {
|
|
801
|
+
if (cohesionScore >= 0.25 && domains.length <= 1)
|
|
802
|
+
return Classification.COHESIVE_MODULE;
|
|
803
|
+
return Classification.UTILITY_MODULE;
|
|
804
|
+
}
|
|
805
|
+
if (isUtilityModule(node)) {
|
|
806
|
+
return Classification.UTILITY_MODULE;
|
|
807
|
+
}
|
|
808
|
+
if (isConfigFile(node)) {
|
|
809
|
+
return Classification.COHESIVE_MODULE;
|
|
810
|
+
}
|
|
811
|
+
if (isHubAndSpokeFile(node)) {
|
|
812
|
+
return Classification.SPOKE_MODULE;
|
|
813
|
+
}
|
|
814
|
+
if (domains.length <= 1 && domains[0] !== "unknown") {
|
|
815
|
+
return Classification.COHESIVE_MODULE;
|
|
816
|
+
}
|
|
817
|
+
if (domains.length > 1 && cohesionScore < 0.4) {
|
|
818
|
+
return Classification.MIXED_CONCERNS;
|
|
819
|
+
}
|
|
820
|
+
if (cohesionScore >= 0.7) {
|
|
821
|
+
return Classification.COHESIVE_MODULE;
|
|
822
|
+
}
|
|
823
|
+
return Classification.UNKNOWN;
|
|
824
|
+
}
|
|
825
|
+
function adjustCohesionForClassification(baseCohesion, classification, node) {
|
|
826
|
+
switch (classification) {
|
|
827
|
+
case Classification.BOILERPLATE:
|
|
828
|
+
return 0.2;
|
|
829
|
+
// Redundant indirection is low cohesion (architectural theater)
|
|
830
|
+
case Classification.BARREL:
|
|
831
|
+
return 1;
|
|
832
|
+
case Classification.TYPE_DEFINITION:
|
|
833
|
+
return 1;
|
|
834
|
+
case Classification.NEXTJS_PAGE:
|
|
835
|
+
return 1;
|
|
836
|
+
case Classification.UTILITY_MODULE: {
|
|
837
|
+
if (node && hasRelatedExportNames(
|
|
838
|
+
(node.exports || []).map((e) => e.name.toLowerCase())
|
|
839
|
+
)) {
|
|
840
|
+
return Math.max(0.8, Math.min(1, baseCohesion + 0.45));
|
|
841
|
+
}
|
|
842
|
+
return Math.max(0.75, Math.min(1, baseCohesion + 0.35));
|
|
843
|
+
}
|
|
844
|
+
case Classification.SERVICE:
|
|
845
|
+
return Math.max(0.72, Math.min(1, baseCohesion + 0.3));
|
|
846
|
+
case Classification.LAMBDA_HANDLER:
|
|
847
|
+
return Math.max(0.75, Math.min(1, baseCohesion + 0.35));
|
|
848
|
+
case Classification.EMAIL_TEMPLATE:
|
|
849
|
+
return Math.max(0.72, Math.min(1, baseCohesion + 0.3));
|
|
850
|
+
case Classification.PARSER:
|
|
851
|
+
return Math.max(0.7, Math.min(1, baseCohesion + 0.3));
|
|
852
|
+
case Classification.SPOKE_MODULE:
|
|
853
|
+
return Math.max(baseCohesion, 0.6);
|
|
854
|
+
case Classification.COHESIVE_MODULE:
|
|
855
|
+
return Math.max(baseCohesion, 0.7);
|
|
856
|
+
case Classification.MIXED_CONCERNS:
|
|
857
|
+
return baseCohesion;
|
|
858
|
+
default:
|
|
859
|
+
return Math.min(1, baseCohesion + 0.1);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
function hasRelatedExportNames(exportNames) {
|
|
863
|
+
if (exportNames.length < 2) return true;
|
|
864
|
+
const stems = /* @__PURE__ */ new Set();
|
|
865
|
+
const domains = /* @__PURE__ */ new Set();
|
|
866
|
+
const verbs = [
|
|
867
|
+
"get",
|
|
868
|
+
"set",
|
|
869
|
+
"create",
|
|
870
|
+
"update",
|
|
871
|
+
"delete",
|
|
872
|
+
"fetch",
|
|
873
|
+
"save",
|
|
874
|
+
"load",
|
|
875
|
+
"parse",
|
|
876
|
+
"format",
|
|
877
|
+
"validate"
|
|
878
|
+
];
|
|
879
|
+
const domainPatterns = [
|
|
880
|
+
"user",
|
|
881
|
+
"order",
|
|
882
|
+
"product",
|
|
883
|
+
"session",
|
|
884
|
+
"email",
|
|
885
|
+
"file",
|
|
886
|
+
"db",
|
|
887
|
+
"api",
|
|
888
|
+
"config"
|
|
889
|
+
];
|
|
890
|
+
for (const name of exportNames) {
|
|
891
|
+
for (const verb of verbs) {
|
|
892
|
+
if (name.startsWith(verb) && name.length > verb.length) {
|
|
893
|
+
stems.add(name.slice(verb.length).toLowerCase());
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
for (const domain of domainPatterns) {
|
|
897
|
+
if (name.includes(domain)) domains.add(domain);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
if (stems.size === 1 || domains.size === 1) return true;
|
|
901
|
+
return false;
|
|
902
|
+
}
|
|
903
|
+
function adjustFragmentationForClassification(baseFragmentation, classification) {
|
|
904
|
+
switch (classification) {
|
|
905
|
+
case Classification.BOILERPLATE:
|
|
906
|
+
return baseFragmentation * 1.5;
|
|
907
|
+
// Redundant barrels increase fragmentation
|
|
908
|
+
case Classification.BARREL:
|
|
909
|
+
return 0;
|
|
910
|
+
case Classification.TYPE_DEFINITION:
|
|
911
|
+
return 0;
|
|
912
|
+
case Classification.UTILITY_MODULE:
|
|
913
|
+
case Classification.SERVICE:
|
|
914
|
+
case Classification.LAMBDA_HANDLER:
|
|
915
|
+
case Classification.EMAIL_TEMPLATE:
|
|
916
|
+
case Classification.PARSER:
|
|
917
|
+
case Classification.NEXTJS_PAGE:
|
|
918
|
+
return baseFragmentation * 0.2;
|
|
919
|
+
case Classification.SPOKE_MODULE:
|
|
920
|
+
return baseFragmentation * 0.15;
|
|
921
|
+
// Heavily discount intentional monorepo separation
|
|
922
|
+
case Classification.COHESIVE_MODULE:
|
|
923
|
+
return baseFragmentation * 0.3;
|
|
924
|
+
case Classification.MIXED_CONCERNS:
|
|
925
|
+
return baseFragmentation;
|
|
926
|
+
default:
|
|
927
|
+
return baseFragmentation * 0.7;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
// src/cluster-detector.ts
|
|
932
|
+
function detectModuleClusters(graph, options) {
|
|
933
|
+
const domainMap = /* @__PURE__ */ new Map();
|
|
934
|
+
for (const [file, node] of graph.nodes.entries()) {
|
|
935
|
+
const primaryDomain = node.exports[0]?.inferredDomain || "unknown";
|
|
936
|
+
if (!domainMap.has(primaryDomain)) {
|
|
937
|
+
domainMap.set(primaryDomain, []);
|
|
938
|
+
}
|
|
939
|
+
domainMap.get(primaryDomain).push(file);
|
|
940
|
+
}
|
|
941
|
+
const clusters = [];
|
|
942
|
+
const generateSuggestedStructure = (files, tokens, fragmentation) => {
|
|
943
|
+
const targetFiles = Math.max(1, Math.ceil(tokens / 1e4));
|
|
944
|
+
const plan = [];
|
|
945
|
+
if (fragmentation > 0.5) {
|
|
946
|
+
plan.push(
|
|
947
|
+
`Consolidate ${files.length} files scattered across multiple directories into ${targetFiles} core module(s)`
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
if (tokens > 2e4) {
|
|
951
|
+
plan.push(
|
|
952
|
+
`Domain logic is very large (${Math.round(tokens / 1e3)}k tokens). Ensure clear sub-domain boundaries.`
|
|
953
|
+
);
|
|
954
|
+
}
|
|
955
|
+
return { targetFiles, consolidationPlan: plan };
|
|
956
|
+
};
|
|
957
|
+
for (const [domain, files] of domainMap.entries()) {
|
|
958
|
+
if (files.length < 2 || domain === "unknown") continue;
|
|
959
|
+
const totalTokens = files.reduce((sum, file) => {
|
|
960
|
+
const node = graph.nodes.get(file);
|
|
961
|
+
return sum + (node?.tokenCost || 0);
|
|
962
|
+
}, 0);
|
|
963
|
+
let sharedImportRatio = 0;
|
|
964
|
+
if (files.length >= 2) {
|
|
965
|
+
const allImportSets = files.map(
|
|
966
|
+
(f) => new Set(graph.nodes.get(f)?.imports || [])
|
|
967
|
+
);
|
|
968
|
+
let intersection = new Set(allImportSets[0]);
|
|
969
|
+
const union = new Set(allImportSets[0]);
|
|
970
|
+
for (let i = 1; i < allImportSets.length; i++) {
|
|
971
|
+
const nextSet = allImportSets[i];
|
|
972
|
+
intersection = new Set([...intersection].filter((x) => nextSet.has(x)));
|
|
973
|
+
for (const x of nextSet) union.add(x);
|
|
974
|
+
}
|
|
975
|
+
sharedImportRatio = union.size > 0 ? intersection.size / union.size : 0;
|
|
976
|
+
}
|
|
977
|
+
const rawFragmentation = calculateFragmentation(files, domain, {
|
|
978
|
+
...options,
|
|
979
|
+
sharedImportRatio
|
|
980
|
+
});
|
|
981
|
+
let totalCohesion = 0;
|
|
982
|
+
let totalAdjustedFragmentation = 0;
|
|
983
|
+
files.forEach((f) => {
|
|
984
|
+
const node = graph.nodes.get(f);
|
|
985
|
+
if (node) {
|
|
986
|
+
const cohesion = calculateEnhancedCohesion(node.exports);
|
|
987
|
+
totalCohesion += cohesion;
|
|
988
|
+
const classification = classifyFile(node, cohesion);
|
|
989
|
+
totalAdjustedFragmentation += adjustFragmentationForClassification(
|
|
990
|
+
rawFragmentation,
|
|
991
|
+
classification
|
|
992
|
+
);
|
|
993
|
+
}
|
|
994
|
+
});
|
|
995
|
+
const avgCohesion = totalCohesion / files.length;
|
|
996
|
+
const fragmentationScore = totalAdjustedFragmentation / files.length;
|
|
997
|
+
clusters.push({
|
|
998
|
+
domain,
|
|
999
|
+
files,
|
|
1000
|
+
totalTokens,
|
|
1001
|
+
fragmentationScore,
|
|
1002
|
+
avgCohesion,
|
|
1003
|
+
suggestedStructure: generateSuggestedStructure(
|
|
1004
|
+
files,
|
|
1005
|
+
totalTokens,
|
|
1006
|
+
fragmentationScore
|
|
1007
|
+
)
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
return clusters;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
// src/remediation.ts
|
|
1014
|
+
function getClassificationRecommendations(classification, file, issues) {
|
|
1015
|
+
switch (classification) {
|
|
1016
|
+
case "boilerplate-barrel":
|
|
1017
|
+
return [
|
|
1018
|
+
"Redundant indirection detected (architectural theater)",
|
|
1019
|
+
"Remove this pass-through barrel export to reduce cognitive load",
|
|
1020
|
+
"Consider combining into meaningful domain exports if necessary"
|
|
1021
|
+
];
|
|
1022
|
+
case "barrel-export":
|
|
1023
|
+
return [
|
|
1024
|
+
"Barrel export file detected - multiple domains are expected here",
|
|
1025
|
+
"Consider if this barrel export improves or hinders discoverability"
|
|
1026
|
+
];
|
|
1027
|
+
case "type-definition":
|
|
1028
|
+
return [
|
|
1029
|
+
"Type definition file - centralized types improve consistency",
|
|
1030
|
+
"Consider splitting if file becomes too large (>500 lines)"
|
|
1031
|
+
];
|
|
1032
|
+
case "cohesive-module":
|
|
1033
|
+
return [
|
|
1034
|
+
"Module has good cohesion despite its size",
|
|
1035
|
+
"Consider documenting the module boundaries for AI assistants"
|
|
1036
|
+
];
|
|
1037
|
+
case "utility-module":
|
|
1038
|
+
return [
|
|
1039
|
+
"Utility module detected - multiple domains are acceptable here",
|
|
1040
|
+
"Consider grouping related utilities by prefix or domain for better discoverability"
|
|
1041
|
+
];
|
|
1042
|
+
case "service-file":
|
|
1043
|
+
return [
|
|
1044
|
+
"Service file detected - orchestration of multiple dependencies is expected",
|
|
1045
|
+
"Consider documenting service boundaries and dependencies"
|
|
1046
|
+
];
|
|
1047
|
+
case "lambda-handler":
|
|
1048
|
+
return [
|
|
1049
|
+
"Lambda handler detected - coordination of services is expected",
|
|
1050
|
+
"Ensure handler has clear single responsibility"
|
|
1051
|
+
];
|
|
1052
|
+
case "email-template":
|
|
1053
|
+
return [
|
|
1054
|
+
"Email template detected - references multiple domains for rendering",
|
|
1055
|
+
"Template structure is cohesive by design"
|
|
1056
|
+
];
|
|
1057
|
+
case "parser-file":
|
|
1058
|
+
return [
|
|
1059
|
+
"Parser/transformer file detected - handles multiple data sources",
|
|
1060
|
+
"Consider documenting input/output schemas"
|
|
1061
|
+
];
|
|
1062
|
+
case "nextjs-page":
|
|
1063
|
+
return [
|
|
1064
|
+
"Next.js App Router page detected - metadata/JSON-LD/component pattern is cohesive",
|
|
1065
|
+
"Multiple exports (metadata, faqJsonLd, default) serve single page purpose"
|
|
1066
|
+
];
|
|
1067
|
+
case "spoke-module":
|
|
1068
|
+
return [
|
|
1069
|
+
"Spoke module detected - intentional monorepo separation is good for modularity",
|
|
1070
|
+
"Ensure this spoke only exports what is necessary for the hub or other spokes"
|
|
1071
|
+
];
|
|
1072
|
+
case "mixed-concerns":
|
|
1073
|
+
return [
|
|
1074
|
+
"Consider splitting this file by domain",
|
|
1075
|
+
"Identify independent responsibilities and extract them",
|
|
1076
|
+
"Review import dependencies to understand coupling"
|
|
1077
|
+
];
|
|
1078
|
+
default:
|
|
1079
|
+
return issues;
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
function getGeneralRecommendations(metrics, thresholds) {
|
|
1083
|
+
const recommendations = [];
|
|
1084
|
+
const issues = [];
|
|
1085
|
+
let severity = "info";
|
|
1086
|
+
if (metrics.contextBudget > thresholds.maxContextBudget) {
|
|
1087
|
+
issues.push(
|
|
1088
|
+
`High context budget: ${Math.round(metrics.contextBudget / 1e3)}k tokens`
|
|
1089
|
+
);
|
|
1090
|
+
recommendations.push(
|
|
1091
|
+
"Reduce dependencies or split the file to lower context window requirements"
|
|
1092
|
+
);
|
|
1093
|
+
severity = "major";
|
|
1094
|
+
}
|
|
1095
|
+
if (metrics.importDepth > thresholds.maxDepth) {
|
|
1096
|
+
issues.push(`Deep import chain: ${metrics.importDepth} levels`);
|
|
1097
|
+
recommendations.push("Flatten the dependency graph by reducing nesting");
|
|
1098
|
+
if (severity !== "critical") severity = "major";
|
|
1099
|
+
}
|
|
1100
|
+
if (metrics.circularDeps.length > 0) {
|
|
1101
|
+
issues.push(
|
|
1102
|
+
`Circular dependencies detected: ${metrics.circularDeps.length}`
|
|
1103
|
+
);
|
|
1104
|
+
recommendations.push(
|
|
1105
|
+
"Refactor to remove circular imports (use dependency injection or interfaces)"
|
|
1106
|
+
);
|
|
1107
|
+
severity = "critical";
|
|
1108
|
+
}
|
|
1109
|
+
if (metrics.cohesionScore < thresholds.minCohesion) {
|
|
1110
|
+
issues.push(`Low cohesion score: ${metrics.cohesionScore.toFixed(2)}`);
|
|
1111
|
+
recommendations.push(
|
|
1112
|
+
"Extract unrelated exports into separate domain-specific modules"
|
|
1113
|
+
);
|
|
1114
|
+
if (severity === "info") severity = "minor";
|
|
1115
|
+
}
|
|
1116
|
+
if (metrics.fragmentationScore > thresholds.maxFragmentation) {
|
|
1117
|
+
issues.push(
|
|
1118
|
+
`High domain fragmentation: ${metrics.fragmentationScore.toFixed(2)}`
|
|
1119
|
+
);
|
|
1120
|
+
recommendations.push(
|
|
1121
|
+
"Consolidate domain-related files into fewer directories"
|
|
1122
|
+
);
|
|
1123
|
+
if (severity === "info") severity = "minor";
|
|
1124
|
+
}
|
|
1125
|
+
return { recommendations, issues, severity };
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// src/orchestrator.ts
|
|
1129
|
+
import { scanFiles, readFileContent } from "@aiready/core";
|
|
1130
|
+
|
|
1131
|
+
// src/issue-analyzer.ts
|
|
1132
|
+
import { Severity } from "@aiready/core";
|
|
1133
|
+
function analyzeIssues(params) {
|
|
1134
|
+
const {
|
|
1135
|
+
file,
|
|
1136
|
+
importDepth,
|
|
1137
|
+
contextBudget,
|
|
1138
|
+
cohesionScore,
|
|
1139
|
+
fragmentationScore,
|
|
1140
|
+
maxDepth,
|
|
1141
|
+
maxContextBudget,
|
|
1142
|
+
minCohesion,
|
|
1143
|
+
maxFragmentation,
|
|
1144
|
+
circularDeps
|
|
1145
|
+
} = params;
|
|
1146
|
+
const issues = [];
|
|
1147
|
+
const recommendations = [];
|
|
1148
|
+
let severity = Severity.Info;
|
|
1149
|
+
let potentialSavings = 0;
|
|
1150
|
+
if (circularDeps.length > 0) {
|
|
1151
|
+
severity = Severity.Critical;
|
|
1152
|
+
issues.push(`Part of ${circularDeps.length} circular dependency chain(s)`);
|
|
1153
|
+
recommendations.push(
|
|
1154
|
+
"Break circular dependencies by extracting interfaces or using dependency injection"
|
|
1155
|
+
);
|
|
1156
|
+
potentialSavings += contextBudget * 0.2;
|
|
1157
|
+
}
|
|
1158
|
+
if (importDepth > maxDepth * 1.5) {
|
|
1159
|
+
severity = Severity.Critical;
|
|
1160
|
+
issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
|
|
1161
|
+
recommendations.push("Flatten dependency tree or use facade pattern");
|
|
1162
|
+
potentialSavings += contextBudget * 0.3;
|
|
1163
|
+
} else if (importDepth > maxDepth) {
|
|
1164
|
+
if (severity !== Severity.Critical) severity = Severity.Major;
|
|
1165
|
+
issues.push(
|
|
1166
|
+
`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`
|
|
1167
|
+
);
|
|
1168
|
+
recommendations.push("Consider reducing dependency depth");
|
|
1169
|
+
potentialSavings += contextBudget * 0.15;
|
|
1170
|
+
}
|
|
1171
|
+
if (contextBudget > maxContextBudget * 1.5) {
|
|
1172
|
+
severity = Severity.Critical;
|
|
1173
|
+
issues.push(
|
|
1174
|
+
`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`
|
|
1175
|
+
);
|
|
1176
|
+
recommendations.push(
|
|
1177
|
+
"Split into smaller modules or reduce dependency tree"
|
|
1178
|
+
);
|
|
1179
|
+
potentialSavings += contextBudget * 0.4;
|
|
1180
|
+
} else if (contextBudget > maxContextBudget) {
|
|
1181
|
+
if (severity !== Severity.Critical) severity = Severity.Major;
|
|
1182
|
+
issues.push(
|
|
1183
|
+
`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`
|
|
1184
|
+
);
|
|
1185
|
+
recommendations.push("Reduce file size or dependencies");
|
|
1186
|
+
potentialSavings += contextBudget * 0.2;
|
|
1187
|
+
}
|
|
1188
|
+
if (cohesionScore < minCohesion * 0.5) {
|
|
1189
|
+
if (severity !== Severity.Critical) severity = Severity.Major;
|
|
1190
|
+
issues.push(
|
|
1191
|
+
`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`
|
|
1192
|
+
);
|
|
1193
|
+
recommendations.push(
|
|
1194
|
+
"Split file by domain - separate unrelated functionality"
|
|
1195
|
+
);
|
|
1196
|
+
potentialSavings += contextBudget * 0.25;
|
|
1197
|
+
} else if (cohesionScore < minCohesion) {
|
|
1198
|
+
if (severity === Severity.Info) severity = Severity.Minor;
|
|
1199
|
+
issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
|
|
1200
|
+
recommendations.push("Consider grouping related exports together");
|
|
1201
|
+
potentialSavings += contextBudget * 0.1;
|
|
1202
|
+
}
|
|
1203
|
+
if (fragmentationScore > maxFragmentation) {
|
|
1204
|
+
if (severity === Severity.Info || severity === Severity.Minor)
|
|
1205
|
+
severity = Severity.Minor;
|
|
1206
|
+
issues.push(
|
|
1207
|
+
`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`
|
|
1208
|
+
);
|
|
1209
|
+
recommendations.push("Consolidate with related files in same domain");
|
|
1210
|
+
potentialSavings += contextBudget * 0.3;
|
|
1211
|
+
}
|
|
1212
|
+
if (issues.length === 0) {
|
|
1213
|
+
issues.push("No significant issues detected");
|
|
1214
|
+
recommendations.push("File is well-structured for AI context usage");
|
|
1215
|
+
}
|
|
1216
|
+
if (isBuildArtifact(file)) {
|
|
1217
|
+
issues.push("Detected build artifact (bundled/output file)");
|
|
1218
|
+
recommendations.push("Exclude build outputs from analysis");
|
|
1219
|
+
severity = Severity.Info;
|
|
1220
|
+
potentialSavings = 0;
|
|
1221
|
+
}
|
|
1222
|
+
return {
|
|
1223
|
+
severity,
|
|
1224
|
+
issues,
|
|
1225
|
+
recommendations,
|
|
1226
|
+
potentialSavings: Math.floor(potentialSavings)
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
function isBuildArtifact(filePath) {
|
|
1230
|
+
const lower = filePath.toLowerCase();
|
|
1231
|
+
return lower.includes("/node_modules/") || lower.includes("/dist/") || lower.includes("/build/") || lower.includes("/out/") || lower.includes("/.next/");
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
// src/mapper.ts
|
|
1235
|
+
function mapNodeToResult(node, graph, clusters, allCircularDeps, options) {
|
|
1236
|
+
const file = node.file;
|
|
1237
|
+
const tokenCost = node.tokenCost;
|
|
1238
|
+
const importDepth = calculateImportDepth(file, graph);
|
|
1239
|
+
const transitiveDeps = getTransitiveDependencies(file, graph);
|
|
1240
|
+
const contextBudget = calculateContextBudget(file, graph);
|
|
1241
|
+
const circularDeps = allCircularDeps.filter((cycle) => cycle.includes(file));
|
|
1242
|
+
const cluster = clusters.find((c) => c.files.includes(file));
|
|
1243
|
+
const rawFragmentationScore = cluster ? cluster.fragmentationScore : 0;
|
|
1244
|
+
const rawCohesionScore = calculateEnhancedCohesion(
|
|
1245
|
+
node.exports,
|
|
1246
|
+
file,
|
|
1247
|
+
options
|
|
1248
|
+
);
|
|
1249
|
+
const fileClassification = classifyFile(node, rawCohesionScore);
|
|
1250
|
+
const cohesionScore = adjustCohesionForClassification(
|
|
1251
|
+
rawCohesionScore,
|
|
1252
|
+
fileClassification
|
|
1253
|
+
);
|
|
1254
|
+
const fragmentationScore = adjustFragmentationForClassification(
|
|
1255
|
+
rawFragmentationScore,
|
|
1256
|
+
fileClassification
|
|
1257
|
+
);
|
|
1258
|
+
const { severity, issues, recommendations, potentialSavings } = analyzeIssues(
|
|
1259
|
+
{
|
|
1260
|
+
file,
|
|
1261
|
+
importDepth,
|
|
1262
|
+
contextBudget,
|
|
1263
|
+
cohesionScore,
|
|
1264
|
+
fragmentationScore,
|
|
1265
|
+
maxDepth: options.maxDepth,
|
|
1266
|
+
maxContextBudget: options.maxContextBudget,
|
|
1267
|
+
minCohesion: options.minCohesion,
|
|
1268
|
+
maxFragmentation: options.maxFragmentation,
|
|
1269
|
+
circularDeps
|
|
1270
|
+
}
|
|
1271
|
+
);
|
|
1272
|
+
const classRecs = getClassificationRecommendations(
|
|
1273
|
+
fileClassification,
|
|
1274
|
+
file,
|
|
1275
|
+
issues
|
|
1276
|
+
);
|
|
1277
|
+
const allRecommendations = Array.from(
|
|
1278
|
+
/* @__PURE__ */ new Set([...recommendations, ...classRecs])
|
|
1279
|
+
);
|
|
1280
|
+
return {
|
|
1281
|
+
file,
|
|
1282
|
+
tokenCost,
|
|
1283
|
+
linesOfCode: node.linesOfCode,
|
|
1284
|
+
importDepth,
|
|
1285
|
+
dependencyCount: transitiveDeps.length,
|
|
1286
|
+
dependencyList: transitiveDeps,
|
|
1287
|
+
circularDeps,
|
|
1288
|
+
cohesionScore,
|
|
1289
|
+
domains: Array.from(
|
|
1290
|
+
new Set(
|
|
1291
|
+
node.exports.flatMap((e) => e.domains?.map((d) => d.domain) || [])
|
|
1292
|
+
)
|
|
1293
|
+
),
|
|
1294
|
+
exportCount: node.exports.length,
|
|
1295
|
+
contextBudget,
|
|
1296
|
+
fragmentationScore,
|
|
1297
|
+
relatedFiles: cluster ? cluster.files : [],
|
|
1298
|
+
fileClassification,
|
|
1299
|
+
severity,
|
|
1300
|
+
issues,
|
|
1301
|
+
recommendations: allRecommendations,
|
|
1302
|
+
potentialSavings
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
// src/orchestrator.ts
|
|
1307
|
+
function calculateCohesion(exports, filePath, options) {
|
|
1308
|
+
return calculateEnhancedCohesion(exports, filePath, options);
|
|
1309
|
+
}
|
|
1310
|
+
async function analyzeContext(options) {
|
|
1311
|
+
const {
|
|
1312
|
+
maxDepth = 5,
|
|
1313
|
+
maxContextBudget = 25e3,
|
|
1314
|
+
minCohesion = 0.6,
|
|
1315
|
+
maxFragmentation = 0.5,
|
|
1316
|
+
includeNodeModules = false,
|
|
1317
|
+
...scanOptions
|
|
1318
|
+
} = options;
|
|
1319
|
+
const files = await scanFiles({
|
|
1320
|
+
...scanOptions,
|
|
1321
|
+
exclude: includeNodeModules && scanOptions.exclude ? scanOptions.exclude.filter(
|
|
1322
|
+
(pattern) => pattern !== "**/node_modules/**"
|
|
1323
|
+
) : scanOptions.exclude
|
|
1324
|
+
});
|
|
1325
|
+
const pythonFiles = files.filter((f) => f.toLowerCase().endsWith(".py"));
|
|
1326
|
+
const fileContents = await Promise.all(
|
|
1327
|
+
files.map(async (file) => ({
|
|
1328
|
+
file,
|
|
1329
|
+
content: await readFileContent(file)
|
|
1330
|
+
}))
|
|
1331
|
+
);
|
|
1332
|
+
const graph = buildDependencyGraph(
|
|
1333
|
+
fileContents.filter((f) => !f.file.toLowerCase().endsWith(".py"))
|
|
1334
|
+
);
|
|
1335
|
+
let pythonResults = [];
|
|
1336
|
+
if (pythonFiles.length > 0) {
|
|
1337
|
+
const { analyzePythonContext } = await import("./python-context-3GZKN3LR.mjs");
|
|
1338
|
+
const pythonMetrics = await analyzePythonContext(
|
|
1339
|
+
pythonFiles,
|
|
1340
|
+
scanOptions.rootDir || options.rootDir || "."
|
|
1341
|
+
);
|
|
1342
|
+
pythonResults = pythonMetrics.map((metric) => {
|
|
1343
|
+
const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
|
|
1344
|
+
file: metric.file,
|
|
1345
|
+
importDepth: metric.importDepth,
|
|
1346
|
+
contextBudget: metric.contextBudget,
|
|
1347
|
+
cohesionScore: metric.cohesion,
|
|
1348
|
+
fragmentationScore: 0,
|
|
1349
|
+
maxDepth,
|
|
1350
|
+
maxContextBudget,
|
|
1351
|
+
minCohesion,
|
|
1352
|
+
maxFragmentation,
|
|
1353
|
+
circularDeps: []
|
|
1354
|
+
});
|
|
1355
|
+
return {
|
|
1356
|
+
file: metric.file,
|
|
1357
|
+
tokenCost: 0,
|
|
1358
|
+
linesOfCode: 0,
|
|
1359
|
+
importDepth: metric.importDepth,
|
|
1360
|
+
dependencyCount: 0,
|
|
1361
|
+
dependencyList: [],
|
|
1362
|
+
circularDeps: [],
|
|
1363
|
+
cohesionScore: metric.cohesion,
|
|
1364
|
+
domains: [],
|
|
1365
|
+
exportCount: 0,
|
|
1366
|
+
contextBudget: metric.contextBudget,
|
|
1367
|
+
fragmentationScore: 0,
|
|
1368
|
+
relatedFiles: [],
|
|
1369
|
+
fileClassification: "unknown",
|
|
1370
|
+
severity,
|
|
1371
|
+
issues,
|
|
1372
|
+
recommendations,
|
|
1373
|
+
potentialSavings
|
|
1374
|
+
};
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
const clusters = detectModuleClusters(graph);
|
|
1378
|
+
const allCircularDeps = detectCircularDependencies(graph);
|
|
1379
|
+
const results = Array.from(graph.nodes.values()).map(
|
|
1380
|
+
(node) => mapNodeToResult(node, graph, clusters, allCircularDeps, {
|
|
1381
|
+
maxDepth,
|
|
1382
|
+
maxContextBudget,
|
|
1383
|
+
minCohesion,
|
|
1384
|
+
maxFragmentation
|
|
1385
|
+
})
|
|
1386
|
+
);
|
|
1387
|
+
return [...results, ...pythonResults];
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// src/summary.ts
|
|
1391
|
+
import { GLOBAL_SCAN_OPTIONS } from "@aiready/core";
|
|
1392
|
+
function generateSummary(results, options = {}) {
|
|
1393
|
+
const config = options ? Object.fromEntries(
|
|
1394
|
+
Object.entries(options).filter(
|
|
1395
|
+
([key]) => !GLOBAL_SCAN_OPTIONS.includes(key) || key === "rootDir"
|
|
1396
|
+
)
|
|
1397
|
+
) : {};
|
|
1398
|
+
const totalFiles = results.length;
|
|
1399
|
+
const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
|
|
1400
|
+
const avgContextBudget = totalFiles > 0 ? results.reduce((sum, r) => sum + r.contextBudget, 0) / totalFiles : 0;
|
|
1401
|
+
const deepFiles = results.filter((r) => r.importDepth > 5).map((r) => ({ file: r.file, depth: r.importDepth }));
|
|
1402
|
+
const maxImportDepth = Math.max(0, ...results.map((r) => r.importDepth));
|
|
1403
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
1404
|
+
results.forEach((r) => {
|
|
1405
|
+
const parts = r.file.split("/");
|
|
1406
|
+
let domain = "root";
|
|
1407
|
+
if (parts.length > 2) {
|
|
1408
|
+
domain = parts.slice(0, 2).join("/");
|
|
1409
|
+
}
|
|
1410
|
+
if (!moduleMap.has(domain)) moduleMap.set(domain, []);
|
|
1411
|
+
moduleMap.get(domain).push(r);
|
|
1412
|
+
});
|
|
1413
|
+
const fragmentedModules = [];
|
|
1414
|
+
moduleMap.forEach((files, domain) => {
|
|
1415
|
+
const clusterTokens = files.reduce((sum, f) => sum + f.tokenCost, 0);
|
|
1416
|
+
const filePaths = files.map((f) => f.file);
|
|
1417
|
+
const avgEntropy = calculatePathEntropy(filePaths);
|
|
1418
|
+
const fragmentationScore = Math.min(1, avgEntropy * (files.length / 10));
|
|
1419
|
+
if (fragmentationScore > 0.4) {
|
|
1420
|
+
fragmentedModules.push({
|
|
1421
|
+
domain,
|
|
1422
|
+
files: filePaths,
|
|
1423
|
+
fragmentationScore,
|
|
1424
|
+
totalTokens: clusterTokens,
|
|
1425
|
+
avgCohesion: files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length,
|
|
1426
|
+
suggestedStructure: {
|
|
1427
|
+
targetFiles: Math.ceil(files.length / 2),
|
|
1428
|
+
consolidationPlan: [
|
|
1429
|
+
`Consolidate ${files.length} files in ${domain} into fewer modules`
|
|
1430
|
+
]
|
|
1431
|
+
}
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
});
|
|
1435
|
+
fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
|
|
1436
|
+
const avgFragmentation = fragmentedModules.length > 0 ? fragmentedModules.reduce((sum, m) => sum + m.fragmentationScore, 0) / fragmentedModules.length : 0;
|
|
1437
|
+
const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / (totalFiles || 1);
|
|
1438
|
+
const lowCohesionFiles = results.filter((r) => r.cohesionScore < 0.4).map((r) => ({ file: r.file, score: r.cohesionScore }));
|
|
1439
|
+
const criticalIssues = results.filter(
|
|
1440
|
+
(r) => r.severity === "critical"
|
|
1441
|
+
).length;
|
|
1442
|
+
const majorIssues = results.filter((r) => r.severity === "major").length;
|
|
1443
|
+
const minorIssues = results.filter((r) => r.severity === "minor").length;
|
|
1444
|
+
const totalPotentialSavings = results.reduce(
|
|
1445
|
+
(sum, r) => sum + (r.potentialSavings || 0),
|
|
1446
|
+
0
|
|
1447
|
+
);
|
|
1448
|
+
const topExpensiveFiles = [...results].sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
|
|
1449
|
+
file: r.file,
|
|
1450
|
+
contextBudget: r.contextBudget,
|
|
1451
|
+
severity: r.severity
|
|
1452
|
+
}));
|
|
1453
|
+
return {
|
|
1454
|
+
totalFiles,
|
|
1455
|
+
totalTokens,
|
|
1456
|
+
avgContextBudget,
|
|
1457
|
+
maxContextBudget: Math.max(0, ...results.map((r) => r.contextBudget)),
|
|
1458
|
+
avgImportDepth: results.reduce((sum, r) => sum + r.importDepth, 0) / (totalFiles || 1),
|
|
1459
|
+
maxImportDepth,
|
|
1460
|
+
deepFiles,
|
|
1461
|
+
avgFragmentation,
|
|
1462
|
+
fragmentedModules,
|
|
1463
|
+
avgCohesion,
|
|
1464
|
+
lowCohesionFiles,
|
|
1465
|
+
criticalIssues,
|
|
1466
|
+
majorIssues,
|
|
1467
|
+
minorIssues,
|
|
1468
|
+
totalPotentialSavings,
|
|
1469
|
+
topExpensiveFiles,
|
|
1470
|
+
config
|
|
1471
|
+
};
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
// src/utils/output-formatter.ts
|
|
1475
|
+
import chalk from "chalk";
|
|
1476
|
+
import { existsSync, readFileSync } from "fs";
|
|
1477
|
+
import { join as join2 } from "path";
|
|
1478
|
+
import prompts from "prompts";
|
|
1479
|
+
function displayConsoleReport(summary, results, maxResults = 10) {
|
|
1480
|
+
const divider = "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500";
|
|
1481
|
+
const totalIssues = summary.criticalIssues + summary.majorIssues + summary.minorIssues;
|
|
1482
|
+
console.log(chalk.bold("\u{1F4CA} Context Analysis Summary:\n"));
|
|
1483
|
+
console.log(` \u2022 Total Files: ${chalk.cyan(summary.totalFiles)}`);
|
|
1484
|
+
console.log(
|
|
1485
|
+
` \u2022 Total Tokens: ${chalk.cyan(summary.totalTokens.toLocaleString())}`
|
|
1486
|
+
);
|
|
1487
|
+
console.log(
|
|
1488
|
+
` \u2022 Avg Budget: ${chalk.cyan(summary.avgContextBudget.toFixed(0))} tokens`
|
|
1489
|
+
);
|
|
1490
|
+
console.log(
|
|
1491
|
+
` \u2022 Potential Saving: ${chalk.green(summary.totalPotentialSavings.toLocaleString())} tokens`
|
|
1492
|
+
);
|
|
1493
|
+
console.log();
|
|
1494
|
+
if (totalIssues > 0) {
|
|
1495
|
+
console.log(chalk.bold("\u26A0\uFE0F Issues Detected:\n"));
|
|
1496
|
+
console.log(` \u2022 ${chalk.red("\u{1F534} Critical:")} ${summary.criticalIssues}`);
|
|
1497
|
+
console.log(` \u2022 ${chalk.yellow("\u{1F7E1} Major:")} ${summary.majorIssues}`);
|
|
1498
|
+
console.log(` \u2022 ${chalk.blue("\u{1F535} Minor:")} ${summary.minorIssues}`);
|
|
1499
|
+
console.log();
|
|
1500
|
+
} else {
|
|
1501
|
+
console.log(chalk.green("\u2705 No significant context issues detected!\n"));
|
|
1502
|
+
}
|
|
1503
|
+
if (summary.fragmentedModules.length > 0) {
|
|
1504
|
+
console.log(chalk.bold("\u{1F9E9} Top Fragmented Modules:\n"));
|
|
1505
|
+
summary.fragmentedModules.slice(0, maxResults).forEach((mod) => {
|
|
1506
|
+
const scoreColor = mod.fragmentationScore > 0.7 ? chalk.red : mod.fragmentationScore > 0.4 ? chalk.yellow : chalk.green;
|
|
1507
|
+
console.log(
|
|
1508
|
+
` ${scoreColor("\u25A0")} ${chalk.white(mod.domain)} ${chalk.dim(`(${mod.files.length} files, ${(mod.fragmentationScore * 100).toFixed(0)}% frag)`)}`
|
|
1509
|
+
);
|
|
1510
|
+
});
|
|
1511
|
+
console.log();
|
|
1512
|
+
}
|
|
1513
|
+
if (summary.topExpensiveFiles.length > 0) {
|
|
1514
|
+
console.log(chalk.bold("\u{1F4B8} Most Expensive Files (Context Budget):\n"));
|
|
1515
|
+
summary.topExpensiveFiles.slice(0, maxResults).forEach((item) => {
|
|
1516
|
+
const fileName = item.file.split("/").slice(-2).join("/");
|
|
1517
|
+
const severityColor = item.severity === "critical" ? chalk.red : item.severity === "major" ? chalk.yellow : chalk.blue;
|
|
1518
|
+
console.log(
|
|
1519
|
+
` ${severityColor("\u25CF")} ${chalk.white(fileName)} ${chalk.dim(`- ${item.contextBudget.toLocaleString()} tokens`)}`
|
|
1520
|
+
);
|
|
1521
|
+
});
|
|
1522
|
+
console.log();
|
|
1523
|
+
}
|
|
1524
|
+
if (totalIssues > 0) {
|
|
1525
|
+
console.log(chalk.bold("\u{1F4A1} Top Recommendations:\n"));
|
|
1526
|
+
const topFiles = results.filter((r) => r.severity === "critical" || r.severity === "major").slice(0, 3);
|
|
1527
|
+
topFiles.forEach((result, index) => {
|
|
1528
|
+
const fileName = result.file.split("/").slice(-2).join("/");
|
|
1529
|
+
console.log(chalk.cyan(` ${index + 1}. ${fileName}`));
|
|
1530
|
+
result.recommendations.slice(0, 2).forEach((rec) => {
|
|
1531
|
+
console.log(chalk.dim(` \u2022 ${rec}`));
|
|
1532
|
+
});
|
|
1533
|
+
});
|
|
1534
|
+
console.log();
|
|
1535
|
+
}
|
|
1536
|
+
console.log(chalk.cyan(divider));
|
|
1537
|
+
console.log(
|
|
1538
|
+
chalk.dim(
|
|
1539
|
+
"\n\u2B50 Like aiready? Star us on GitHub: https://github.com/caopengau/aiready-context-analyzer"
|
|
1540
|
+
)
|
|
1541
|
+
);
|
|
1542
|
+
console.log(
|
|
1543
|
+
chalk.dim(
|
|
1544
|
+
"\u{1F41B} Found a bug? Report it: https://github.com/caopengau/aiready-context-analyzer/issues\n"
|
|
1545
|
+
)
|
|
1546
|
+
);
|
|
1547
|
+
}
|
|
1548
|
+
function generateHTMLReport(summary, results) {
|
|
1549
|
+
const totalIssues = summary.criticalIssues + summary.majorIssues + summary.minorIssues;
|
|
1550
|
+
void results;
|
|
1551
|
+
return `<!DOCTYPE html>
|
|
1552
|
+
<html lang="en">
|
|
1553
|
+
<head>
|
|
1554
|
+
<meta charset="UTF-8">
|
|
1555
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
1556
|
+
<title>aiready Context Analysis Report</title>
|
|
1557
|
+
<style>
|
|
1558
|
+
body {
|
|
1559
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
|
1560
|
+
line-height: 1.6;
|
|
1561
|
+
color: #333;
|
|
1562
|
+
max-width: 1200px;
|
|
1563
|
+
margin: 0 auto;
|
|
1564
|
+
padding: 20px;
|
|
1565
|
+
background-color: #f5f5f5;
|
|
1566
|
+
}
|
|
1567
|
+
h1, h2, h3 { color: #2c3e50; }
|
|
1568
|
+
.header {
|
|
1569
|
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
1570
|
+
color: white;
|
|
1571
|
+
padding: 30px;
|
|
1572
|
+
border-radius: 8px;
|
|
1573
|
+
margin-bottom: 30px;
|
|
1574
|
+
}
|
|
1575
|
+
.summary {
|
|
1576
|
+
display: grid;
|
|
1577
|
+
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
|
1578
|
+
gap: 20px;
|
|
1579
|
+
margin-bottom: 30px;
|
|
1580
|
+
}
|
|
1581
|
+
.card {
|
|
1582
|
+
background: white;
|
|
1583
|
+
padding: 20px;
|
|
1584
|
+
border-radius: 8px;
|
|
1585
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
1586
|
+
}
|
|
1587
|
+
.metric {
|
|
1588
|
+
font-size: 2em;
|
|
1589
|
+
font-weight: bold;
|
|
1590
|
+
color: #667eea;
|
|
1591
|
+
}
|
|
1592
|
+
.label {
|
|
1593
|
+
color: #666;
|
|
1594
|
+
font-size: 0.9em;
|
|
1595
|
+
margin-top: 5px;
|
|
1596
|
+
}
|
|
1597
|
+
.issue-critical { color: #e74c3c; }
|
|
1598
|
+
.issue-major { color: #f39c12; }
|
|
1599
|
+
.issue-minor { color: #3498db; }
|
|
1600
|
+
table {
|
|
1601
|
+
width: 100%;
|
|
1602
|
+
border-collapse: collapse;
|
|
1603
|
+
background: white;
|
|
1604
|
+
border-radius: 8px;
|
|
1605
|
+
overflow: hidden;
|
|
1606
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
1607
|
+
}
|
|
1608
|
+
th, td {
|
|
1609
|
+
padding: 12px;
|
|
1610
|
+
text-align: left;
|
|
1611
|
+
border-bottom: 1px solid #eee;
|
|
1612
|
+
}
|
|
1613
|
+
th {
|
|
1614
|
+
background-color: #667eea;
|
|
1615
|
+
color: white;
|
|
1616
|
+
font-weight: 600;
|
|
1617
|
+
}
|
|
1618
|
+
tr:hover { background-color: #f8f9fa; }
|
|
1619
|
+
.footer {
|
|
1620
|
+
text-align: center;
|
|
1621
|
+
margin-top: 40px;
|
|
1622
|
+
padding: 20px;
|
|
1623
|
+
color: #666;
|
|
1624
|
+
font-size: 0.9em;
|
|
1625
|
+
}
|
|
1626
|
+
</style>
|
|
1627
|
+
</head>
|
|
1628
|
+
<body>
|
|
1629
|
+
<div class="header">
|
|
1630
|
+
<h1>\u{1F50D} AIReady Context Analysis Report</h1>
|
|
1631
|
+
<p>Generated on ${(/* @__PURE__ */ new Date()).toLocaleString()}</p>
|
|
1632
|
+
</div>
|
|
1633
|
+
|
|
1634
|
+
<div class="summary">
|
|
1635
|
+
<div class="card">
|
|
1636
|
+
<div class="metric">${summary.totalFiles}</div>
|
|
1637
|
+
<div class="label">Files Analyzed</div>
|
|
1638
|
+
</div>
|
|
1639
|
+
<div class="card">
|
|
1640
|
+
<div class="metric">${summary.totalTokens.toLocaleString()}</div>
|
|
1641
|
+
<div class="label">Total Tokens</div>
|
|
1642
|
+
</div>
|
|
1643
|
+
<div class="card">
|
|
1644
|
+
<div class="metric">${summary.avgContextBudget.toFixed(0)}</div>
|
|
1645
|
+
<div class="label">Avg Context Budget</div>
|
|
1646
|
+
</div>
|
|
1647
|
+
<div class="card">
|
|
1648
|
+
<div class="metric ${totalIssues > 0 ? "issue-major" : ""}">${totalIssues}</div>
|
|
1649
|
+
<div class="label">Total Issues</div>
|
|
1650
|
+
</div>
|
|
1651
|
+
</div>
|
|
1652
|
+
|
|
1653
|
+
${totalIssues > 0 ? `
|
|
1654
|
+
<div class="card" style="margin-bottom: 30px;">
|
|
1655
|
+
<h2>\u26A0\uFE0F Issues Summary</h2>
|
|
1656
|
+
<p>
|
|
1657
|
+
<span class="issue-critical">\u{1F534} Critical: ${summary.criticalIssues}</span>
|
|
1658
|
+
<span class="issue-major">\u{1F7E1} Major: ${summary.majorIssues}</span>
|
|
1659
|
+
<span class="issue-minor">\u{1F535} Minor: ${summary.minorIssues}</span>
|
|
1660
|
+
</p>
|
|
1661
|
+
<p><strong>Potential Savings:</strong> ${summary.totalPotentialSavings.toLocaleString()} tokens</p>
|
|
1662
|
+
</div>
|
|
1663
|
+
` : ""}
|
|
1664
|
+
|
|
1665
|
+
${summary.fragmentedModules.length > 0 ? `
|
|
1666
|
+
<div class="card" style="margin-bottom: 30px;">
|
|
1667
|
+
<h2>\u{1F9E9} Fragmented Modules</h2>
|
|
1668
|
+
<table>
|
|
1669
|
+
<thead>
|
|
1670
|
+
<tr>
|
|
1671
|
+
<th>Domain</th>
|
|
1672
|
+
<th>Files</th>
|
|
1673
|
+
<th>Fragmentation</th>
|
|
1674
|
+
<th>Token Cost</th>
|
|
1675
|
+
</tr>
|
|
1676
|
+
</thead>
|
|
1677
|
+
<tbody>
|
|
1678
|
+
${summary.fragmentedModules.map(
|
|
1679
|
+
(m) => `
|
|
1680
|
+
<tr>
|
|
1681
|
+
<td>${m.domain}</td>
|
|
1682
|
+
<td>${m.files.length}</td>
|
|
1683
|
+
<td>${(m.fragmentationScore * 100).toFixed(0)}%</td>
|
|
1684
|
+
<td>${m.totalTokens.toLocaleString()}</td>
|
|
1685
|
+
</tr>
|
|
1686
|
+
`
|
|
1687
|
+
).join("")}
|
|
1688
|
+
</tbody>
|
|
1689
|
+
</table>
|
|
1690
|
+
</div>
|
|
1691
|
+
` : ""}
|
|
1692
|
+
|
|
1693
|
+
${summary.topExpensiveFiles.length > 0 ? `
|
|
1694
|
+
<div class="card" style="margin-bottom: 30px;">
|
|
1695
|
+
<h2>\u{1F4B8} Most Expensive Files</h2>
|
|
1696
|
+
<table>
|
|
1697
|
+
<thead>
|
|
1698
|
+
<tr>
|
|
1699
|
+
<th>File</th>
|
|
1700
|
+
<th>Context Budget</th>
|
|
1701
|
+
<th>Severity</th>
|
|
1702
|
+
</tr>
|
|
1703
|
+
</thead>
|
|
1704
|
+
<tbody>
|
|
1705
|
+
${summary.topExpensiveFiles.map(
|
|
1706
|
+
(f) => `
|
|
1707
|
+
<tr>
|
|
1708
|
+
<td>${f.file}</td>
|
|
1709
|
+
<td>${f.contextBudget.toLocaleString()} tokens</td>
|
|
1710
|
+
<td class="issue-${f.severity}">${f.severity.toUpperCase()}</td>
|
|
1711
|
+
</tr>
|
|
1712
|
+
`
|
|
1713
|
+
).join("")}
|
|
1714
|
+
</tbody>
|
|
1715
|
+
</table>
|
|
1716
|
+
</div>
|
|
1717
|
+
` : ""}
|
|
1718
|
+
|
|
1719
|
+
<div class="footer">
|
|
1720
|
+
<p>Generated by <strong>@aiready/context-analyzer</strong></p>
|
|
1721
|
+
<p>Like AIReady? <a href="https://github.com/caopengau/aiready-context-analyzer">Star us on GitHub</a></p>
|
|
1722
|
+
<p>Found a bug? <a href="https://github.com/caopengau/aiready-context-analyzer/issues">Report it here</a></p>
|
|
1723
|
+
</div>
|
|
1724
|
+
</body>
|
|
1725
|
+
</html>`;
|
|
1726
|
+
}
|
|
1727
|
+
async function runInteractiveSetup(directory, current) {
|
|
1728
|
+
console.log(chalk.yellow("\u{1F9ED} Interactive mode: let\u2019s tailor the analysis."));
|
|
1729
|
+
const pkgPath = join2(directory, "package.json");
|
|
1730
|
+
let deps = {};
|
|
1731
|
+
if (existsSync(pkgPath)) {
|
|
1732
|
+
try {
|
|
1733
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
1734
|
+
deps = { ...pkg.dependencies || {}, ...pkg.devDependencies || {} };
|
|
1735
|
+
} catch (e) {
|
|
1736
|
+
void e;
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
const hasNextJs = existsSync(join2(directory, ".next")) || !!deps["next"];
|
|
1740
|
+
const hasCDK = existsSync(join2(directory, "cdk.out")) || !!deps["aws-cdk-lib"] || Object.keys(deps).some((d) => d.startsWith("@aws-cdk/"));
|
|
1741
|
+
const recommendedExcludes = new Set(current.exclude || []);
|
|
1742
|
+
if (hasNextJs && !Array.from(recommendedExcludes).some((p) => p.includes(".next"))) {
|
|
1743
|
+
recommendedExcludes.add("**/.next/**");
|
|
1744
|
+
}
|
|
1745
|
+
if (hasCDK && !Array.from(recommendedExcludes).some((p) => p.includes("cdk.out"))) {
|
|
1746
|
+
recommendedExcludes.add("**/cdk.out/**");
|
|
1747
|
+
}
|
|
1748
|
+
const { applyExcludes } = await prompts({
|
|
1749
|
+
type: "toggle",
|
|
1750
|
+
name: "applyExcludes",
|
|
1751
|
+
message: `Detected ${hasNextJs ? "Next.js " : ""}${hasCDK ? "AWS CDK " : ""}frameworks. Apply recommended excludes?`,
|
|
1752
|
+
initial: true,
|
|
1753
|
+
active: "yes",
|
|
1754
|
+
inactive: "no"
|
|
1755
|
+
});
|
|
1756
|
+
const nextOptions = { ...current };
|
|
1757
|
+
if (applyExcludes) {
|
|
1758
|
+
nextOptions.exclude = Array.from(recommendedExcludes);
|
|
1759
|
+
}
|
|
1760
|
+
const { focusArea } = await prompts({
|
|
1761
|
+
type: "select",
|
|
1762
|
+
name: "focusArea",
|
|
1763
|
+
message: "Which areas to focus?",
|
|
1764
|
+
choices: [
|
|
1765
|
+
{ title: "Frontend (web app)", value: "frontend" },
|
|
1766
|
+
{ title: "Backend (API/infra)", value: "backend" },
|
|
1767
|
+
{ title: "Both", value: "both" }
|
|
1768
|
+
],
|
|
1769
|
+
initial: 2
|
|
1770
|
+
});
|
|
1771
|
+
if (focusArea === "frontend") {
|
|
1772
|
+
nextOptions.include = ["**/*.{ts,tsx,js,jsx}"];
|
|
1773
|
+
nextOptions.exclude = Array.from(
|
|
1774
|
+
/* @__PURE__ */ new Set([
|
|
1775
|
+
...nextOptions.exclude || [],
|
|
1776
|
+
"**/cdk.out/**",
|
|
1777
|
+
"**/infra/**",
|
|
1778
|
+
"**/server/**",
|
|
1779
|
+
"**/backend/**"
|
|
1780
|
+
])
|
|
1781
|
+
);
|
|
1782
|
+
} else if (focusArea === "backend") {
|
|
1783
|
+
nextOptions.include = [
|
|
1784
|
+
"**/api/**",
|
|
1785
|
+
"**/server/**",
|
|
1786
|
+
"**/backend/**",
|
|
1787
|
+
"**/infra/**",
|
|
1788
|
+
"**/*.{ts,js,py,java}"
|
|
1789
|
+
];
|
|
1790
|
+
}
|
|
1791
|
+
console.log(chalk.green("\u2713 Interactive configuration applied."));
|
|
1792
|
+
return nextOptions;
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
export {
|
|
1796
|
+
buildCoUsageMatrix,
|
|
1797
|
+
buildTypeGraph,
|
|
1798
|
+
findSemanticClusters,
|
|
1799
|
+
inferDomainFromSemantics,
|
|
1800
|
+
calculateDomainConfidence,
|
|
1801
|
+
extractExports,
|
|
1802
|
+
inferDomain,
|
|
1803
|
+
getCoUsageData,
|
|
1804
|
+
findConsolidationCandidates,
|
|
1805
|
+
calculateEnhancedCohesion,
|
|
1806
|
+
calculateStructuralCohesionFromCoUsage,
|
|
1807
|
+
calculateFragmentation,
|
|
1808
|
+
calculatePathEntropy,
|
|
1809
|
+
calculateDirectoryDistance,
|
|
1810
|
+
extractDomainKeywordsFromPaths,
|
|
1811
|
+
buildDependencyGraph,
|
|
1812
|
+
calculateImportDepth,
|
|
1813
|
+
getTransitiveDependencies,
|
|
1814
|
+
calculateContextBudget,
|
|
1815
|
+
detectCircularDependencies,
|
|
1816
|
+
Classification,
|
|
1817
|
+
classifyFile,
|
|
1818
|
+
adjustCohesionForClassification,
|
|
1819
|
+
adjustFragmentationForClassification,
|
|
1820
|
+
detectModuleClusters,
|
|
1821
|
+
getClassificationRecommendations,
|
|
1822
|
+
getGeneralRecommendations,
|
|
1823
|
+
calculateCohesion,
|
|
1824
|
+
analyzeContext,
|
|
1825
|
+
generateSummary,
|
|
1826
|
+
displayConsoleReport,
|
|
1827
|
+
generateHTMLReport,
|
|
1828
|
+
runInteractiveSetup
|
|
1829
|
+
};
|