@aiready/context-analyzer 0.22.8 → 0.22.11

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.
Files changed (304) hide show
  1. package/.turbo/turbo-build.log +33 -32
  2. package/.turbo/turbo-format-check.log +2 -2
  3. package/.turbo/turbo-lint.log +9 -1
  4. package/.turbo/turbo-test.log +23 -23
  5. package/.turbo/turbo-type-check.log +1 -1
  6. package/dist/chunk-HD4Y3GYL.mjs +124 -0
  7. package/dist/{chunk-M2EGQ36M.mjs → chunk-ISWPFG2C.mjs} +43 -98
  8. package/dist/{chunk-BQCISA2F.mjs → chunk-QW6ULRML.mjs} +32 -6
  9. package/dist/{chunk-WHB7QI7N.mjs → chunk-T733LS62.mjs} +23 -4
  10. package/dist/{cli-action-CXIHOVAC.mjs → cli-action-FV4G4OB6.mjs} +3 -3
  11. package/dist/{cli-action-3CWN7PBE.mjs → cli-action-HYSWE7F4.mjs} +3 -3
  12. package/dist/{cli-action-MLFCIW2O.mjs → cli-action-VIQZSWSP.mjs} +3 -3
  13. package/dist/{cli-action-E7UGP4KE.mjs → cli-action-W7TESWAV.mjs} +6 -8
  14. package/dist/cli.js +236 -191
  15. package/dist/cli.mjs +1 -1
  16. package/dist/console-report-AP4JYNQY.mjs +74 -0
  17. package/dist/html-report-ULELSIYG.mjs +73 -0
  18. package/dist/index.d.mts +69 -337
  19. package/dist/index.d.ts +69 -337
  20. package/dist/index.js +90 -452
  21. package/dist/index.mjs +9 -157
  22. package/dist/interactive-setup-JGFBFI3M.mjs +75 -0
  23. package/dist/{orchestrator-3ERQS3NW.mjs → orchestrator-QNE2E4TE.mjs} +2 -2
  24. package/dist/summary-GQRWW3A2.mjs +7 -0
  25. package/dist/summary-JTBS7CPM.mjs +7 -0
  26. package/dist/summary-TZFB6ZFM.mjs +7 -0
  27. package/package.json +2 -2
  28. package/src/__tests__/file-classification.fixtures.ts +57 -0
  29. package/src/__tests__/file-classification.test.ts +24 -929
  30. package/src/__tests__/provider.test.ts +6 -72
  31. package/src/classifier.ts +42 -42
  32. package/src/cli-action.ts +11 -6
  33. package/src/index.ts +30 -27
  34. package/src/provider.ts +1 -1
  35. package/src/summary.ts +65 -10
  36. package/dist/__tests__/analyzer.test.d.ts +0 -2
  37. package/dist/__tests__/analyzer.test.d.ts.map +0 -1
  38. package/dist/__tests__/analyzer.test.js +0 -198
  39. package/dist/__tests__/analyzer.test.js.map +0 -1
  40. package/dist/__tests__/auto-detection.test.d.ts +0 -2
  41. package/dist/__tests__/auto-detection.test.d.ts.map +0 -1
  42. package/dist/__tests__/auto-detection.test.js +0 -132
  43. package/dist/__tests__/auto-detection.test.js.map +0 -1
  44. package/dist/__tests__/cluster-detector.test.d.ts +0 -2
  45. package/dist/__tests__/cluster-detector.test.d.ts.map +0 -1
  46. package/dist/__tests__/cluster-detector.test.js +0 -121
  47. package/dist/__tests__/cluster-detector.test.js.map +0 -1
  48. package/dist/__tests__/contract.test.d.ts +0 -2
  49. package/dist/__tests__/contract.test.d.ts.map +0 -1
  50. package/dist/__tests__/contract.test.js +0 -59
  51. package/dist/__tests__/contract.test.js.map +0 -1
  52. package/dist/__tests__/enhanced-cohesion.test.d.ts +0 -2
  53. package/dist/__tests__/enhanced-cohesion.test.d.ts.map +0 -1
  54. package/dist/__tests__/enhanced-cohesion.test.js +0 -119
  55. package/dist/__tests__/enhanced-cohesion.test.js.map +0 -1
  56. package/dist/__tests__/file-classification.test.d.ts +0 -2
  57. package/dist/__tests__/file-classification.test.d.ts.map +0 -1
  58. package/dist/__tests__/file-classification.test.js +0 -749
  59. package/dist/__tests__/file-classification.test.js.map +0 -1
  60. package/dist/__tests__/fragmentation-advanced.test.d.ts +0 -2
  61. package/dist/__tests__/fragmentation-advanced.test.d.ts.map +0 -1
  62. package/dist/__tests__/fragmentation-advanced.test.js +0 -44
  63. package/dist/__tests__/fragmentation-advanced.test.js.map +0 -1
  64. package/dist/__tests__/fragmentation-coupling.test.d.ts +0 -2
  65. package/dist/__tests__/fragmentation-coupling.test.d.ts.map +0 -1
  66. package/dist/__tests__/fragmentation-coupling.test.js +0 -52
  67. package/dist/__tests__/fragmentation-coupling.test.js.map +0 -1
  68. package/dist/__tests__/fragmentation-log.test.d.ts +0 -2
  69. package/dist/__tests__/fragmentation-log.test.d.ts.map +0 -1
  70. package/dist/__tests__/fragmentation-log.test.js +0 -29
  71. package/dist/__tests__/fragmentation-log.test.js.map +0 -1
  72. package/dist/__tests__/provider.test.d.ts +0 -2
  73. package/dist/__tests__/provider.test.d.ts.map +0 -1
  74. package/dist/__tests__/provider.test.js +0 -72
  75. package/dist/__tests__/provider.test.js.map +0 -1
  76. package/dist/__tests__/remediation.test.d.ts +0 -2
  77. package/dist/__tests__/remediation.test.d.ts.map +0 -1
  78. package/dist/__tests__/remediation.test.js +0 -61
  79. package/dist/__tests__/remediation.test.js.map +0 -1
  80. package/dist/__tests__/scoring.test.d.ts +0 -2
  81. package/dist/__tests__/scoring.test.d.ts.map +0 -1
  82. package/dist/__tests__/scoring.test.js +0 -298
  83. package/dist/__tests__/scoring.test.js.map +0 -1
  84. package/dist/__tests__/structural-cohesion.test.d.ts +0 -2
  85. package/dist/__tests__/structural-cohesion.test.d.ts.map +0 -1
  86. package/dist/__tests__/structural-cohesion.test.js +0 -35
  87. package/dist/__tests__/structural-cohesion.test.js.map +0 -1
  88. package/dist/analyzer.d.ts +0 -37
  89. package/dist/analyzer.d.ts.map +0 -1
  90. package/dist/analyzer.js +0 -283
  91. package/dist/analyzer.js.map +0 -1
  92. package/dist/analyzers/python-context.d.ts +0 -38
  93. package/dist/analyzers/python-context.d.ts.map +0 -1
  94. package/dist/analyzers/python-context.js +0 -234
  95. package/dist/analyzers/python-context.js.map +0 -1
  96. package/dist/ast-utils.d.ts +0 -16
  97. package/dist/ast-utils.d.ts.map +0 -1
  98. package/dist/ast-utils.js +0 -81
  99. package/dist/ast-utils.js.map +0 -1
  100. package/dist/chunk-22ZO4EKZ.mjs +0 -1297
  101. package/dist/chunk-2HE27YEV.mjs +0 -1739
  102. package/dist/chunk-45P4RDYP.mjs +0 -607
  103. package/dist/chunk-474DEGWW.mjs +0 -1792
  104. package/dist/chunk-4SYIJ7CU.mjs +0 -1538
  105. package/dist/chunk-4U4LDWGF.mjs +0 -360
  106. package/dist/chunk-4XQVYYPC.mjs +0 -1470
  107. package/dist/chunk-5CLU3HYU.mjs +0 -1475
  108. package/dist/chunk-5K73Q3OQ.mjs +0 -1520
  109. package/dist/chunk-5N5DCJOV.mjs +0 -583
  110. package/dist/chunk-6AVS4KTM.mjs +0 -1536
  111. package/dist/chunk-6FQYIG6I.mjs +0 -1298
  112. package/dist/chunk-6I4552YB.mjs +0 -1467
  113. package/dist/chunk-6LPITDKG.mjs +0 -1539
  114. package/dist/chunk-72QC5QUS.mjs +0 -549
  115. package/dist/chunk-736QSHJP.mjs +0 -1807
  116. package/dist/chunk-7LUSCLGR.mjs +0 -2058
  117. package/dist/chunk-7VK3XTSH.mjs +0 -1756
  118. package/dist/chunk-7ZEJGWLN.mjs +0 -1363
  119. package/dist/chunk-AECWO7NQ.mjs +0 -1539
  120. package/dist/chunk-AEK3MZC5.mjs +0 -709
  121. package/dist/chunk-AJC3FR6G.mjs +0 -1509
  122. package/dist/chunk-AMPK6SWS.mjs +0 -1754
  123. package/dist/chunk-BA7QGUHN.mjs +0 -1722
  124. package/dist/chunk-BCEZGRXI.mjs +0 -1297
  125. package/dist/chunk-BD4NWUVG.mjs +0 -1242
  126. package/dist/chunk-BEZPBI5C.mjs +0 -1829
  127. package/dist/chunk-BHCRDEE4.mjs +0 -1745
  128. package/dist/chunk-BW463GQB.mjs +0 -1767
  129. package/dist/chunk-CAX2MOUZ.mjs +0 -1801
  130. package/dist/chunk-CBWM3EK5.mjs +0 -1854
  131. package/dist/chunk-CCBNKQYB.mjs +0 -1812
  132. package/dist/chunk-CDIVYADN.mjs +0 -2110
  133. package/dist/chunk-CVGIDSMN.mjs +0 -1522
  134. package/dist/chunk-D25B5LZR.mjs +0 -1739
  135. package/dist/chunk-D3SIHB2V.mjs +0 -2118
  136. package/dist/chunk-DD7UVNE3.mjs +0 -678
  137. package/dist/chunk-DMRZMS2U.mjs +0 -964
  138. package/dist/chunk-DXG5NIYL.mjs +0 -1527
  139. package/dist/chunk-EBXG2Q5Y.mjs +0 -2059
  140. package/dist/chunk-EH3PMNZQ.mjs +0 -569
  141. package/dist/chunk-EMYD7NS6.mjs +0 -137
  142. package/dist/chunk-EVX2W2BK.mjs +0 -1896
  143. package/dist/chunk-EWFR366Y.mjs +0 -1740
  144. package/dist/chunk-EX7HCWAO.mjs +0 -625
  145. package/dist/chunk-FNPSK3CG.mjs +0 -1760
  146. package/dist/chunk-FO6YT6RG.mjs +0 -1751
  147. package/dist/chunk-FYI56A5M.mjs +0 -1892
  148. package/dist/chunk-G3CCJCBI.mjs +0 -1521
  149. package/dist/chunk-G7PO3DNK.mjs +0 -1072
  150. package/dist/chunk-GFADGYXZ.mjs +0 -1752
  151. package/dist/chunk-GTRIBVS6.mjs +0 -1467
  152. package/dist/chunk-GXTGOLZT.mjs +0 -92
  153. package/dist/chunk-H4HWBQU6.mjs +0 -1530
  154. package/dist/chunk-HDFXSEFW.mjs +0 -605
  155. package/dist/chunk-HOUDVRG2.mjs +0 -1422
  156. package/dist/chunk-HQNHM2X7.mjs +0 -997
  157. package/dist/chunk-I54HL4FZ.mjs +0 -781
  158. package/dist/chunk-I77HFFZU.mjs +0 -1876
  159. package/dist/chunk-IKRP7ECY.mjs +0 -1754
  160. package/dist/chunk-ILMLGJGI.mjs +0 -1295
  161. package/dist/chunk-IPIE5TXJ.mjs +0 -1741
  162. package/dist/chunk-IRWCPDWD.mjs +0 -779
  163. package/dist/chunk-J3MUOWHC.mjs +0 -1747
  164. package/dist/chunk-J5TA3AZU.mjs +0 -1795
  165. package/dist/chunk-JH535NPP.mjs +0 -1619
  166. package/dist/chunk-JUHHOSHG.mjs +0 -1808
  167. package/dist/chunk-JZ2SE4DB.mjs +0 -1116
  168. package/dist/chunk-K2WFOBAZ.mjs +0 -1821
  169. package/dist/chunk-K6U64EL3.mjs +0 -517
  170. package/dist/chunk-KDUUZQBK.mjs +0 -1692
  171. package/dist/chunk-KGFWKSGJ.mjs +0 -1442
  172. package/dist/chunk-KGVMS4R5.mjs +0 -1750
  173. package/dist/chunk-KWIS5FQP.mjs +0 -1739
  174. package/dist/chunk-KYSZF5N6.mjs +0 -1876
  175. package/dist/chunk-LERPI33Y.mjs +0 -2060
  176. package/dist/chunk-M64RHH4D.mjs +0 -1896
  177. package/dist/chunk-MBE4AQP5.mjs +0 -1362
  178. package/dist/chunk-MR7WXHIE.mjs +0 -1833
  179. package/dist/chunk-MZP3G7TF.mjs +0 -2118
  180. package/dist/chunk-N2GQWNFG.mjs +0 -1527
  181. package/dist/chunk-N6XBOOVA.mjs +0 -564
  182. package/dist/chunk-NJUW6VED.mjs +0 -610
  183. package/dist/chunk-NOHK5DLU.mjs +0 -2173
  184. package/dist/chunk-NQA3F2HJ.mjs +0 -1532
  185. package/dist/chunk-NXXQ2U73.mjs +0 -1467
  186. package/dist/chunk-OP4G6GLN.mjs +0 -1876
  187. package/dist/chunk-ORLC5Y4J.mjs +0 -1787
  188. package/dist/chunk-OTCQL7DY.mjs +0 -2045
  189. package/dist/chunk-OUYSZZ7X.mjs +0 -1846
  190. package/dist/chunk-OZE3FVZT.mjs +0 -1089
  191. package/dist/chunk-P3T3H27S.mjs +0 -1895
  192. package/dist/chunk-P5YV5WIX.mjs +0 -1803
  193. package/dist/chunk-P74BO725.mjs +0 -1296
  194. package/dist/chunk-PDN74MG3.mjs +0 -1834
  195. package/dist/chunk-PJD4VCIH.mjs +0 -1722
  196. package/dist/chunk-PVVCCE6W.mjs +0 -755
  197. package/dist/chunk-PVVMK56C.mjs +0 -1793
  198. package/dist/chunk-Q2GDZ2FZ.mjs +0 -1794
  199. package/dist/chunk-QDGPR3L6.mjs +0 -1518
  200. package/dist/chunk-RQCIJO5Z.mjs +0 -1116
  201. package/dist/chunk-RRB2C34Q.mjs +0 -1738
  202. package/dist/chunk-RYIB5CWD.mjs +0 -781
  203. package/dist/chunk-S6OPP4L5.mjs +0 -1791
  204. package/dist/chunk-SAVOSPM3.mjs +0 -1522
  205. package/dist/chunk-SFK6XTJE.mjs +0 -2110
  206. package/dist/chunk-SIX4KMF2.mjs +0 -1468
  207. package/dist/chunk-SPAM2YJE.mjs +0 -1537
  208. package/dist/chunk-T6ZCOPPI.mjs +0 -538
  209. package/dist/chunk-TPF75CNP.mjs +0 -581
  210. package/dist/chunk-TWWPY7FD.mjs +0 -1754
  211. package/dist/chunk-U5R2FTCR.mjs +0 -1803
  212. package/dist/chunk-UG7OPVHB.mjs +0 -1521
  213. package/dist/chunk-UMZTAWDA.mjs +0 -1812
  214. package/dist/chunk-UU4HZ7ZT.mjs +0 -1849
  215. package/dist/chunk-UXC6QUZ7.mjs +0 -1801
  216. package/dist/chunk-VBWXHKGD.mjs +0 -1895
  217. package/dist/chunk-VIJTZPBI.mjs +0 -1470
  218. package/dist/chunk-VLV6MXPL.mjs +0 -1750
  219. package/dist/chunk-VTALAPQZ.mjs +0 -1241
  220. package/dist/chunk-W2KNBN6W.mjs +0 -1849
  221. package/dist/chunk-W37E7MW5.mjs +0 -1403
  222. package/dist/chunk-W76FEISE.mjs +0 -1538
  223. package/dist/chunk-WCFQYXQA.mjs +0 -1532
  224. package/dist/chunk-WKOZOHOU.mjs +0 -2060
  225. package/dist/chunk-WTQJNY4U.mjs +0 -1785
  226. package/dist/chunk-XBFM2Z4O.mjs +0 -1792
  227. package/dist/chunk-XIXAWCMS.mjs +0 -1760
  228. package/dist/chunk-XTAXUNQN.mjs +0 -1742
  229. package/dist/chunk-XY77XABG.mjs +0 -1545
  230. package/dist/chunk-XZ645X5U.mjs +0 -1425
  231. package/dist/chunk-Y6FXYEAI.mjs +0 -10
  232. package/dist/chunk-YCGDIGOG.mjs +0 -1467
  233. package/dist/chunk-YYB6NZE3.mjs +0 -1869
  234. package/dist/chunk-Z5WY6A4P.mjs +0 -1754
  235. package/dist/chunk-ZAMFWKIB.mjs +0 -1842
  236. package/dist/classifier.d.ts +0 -114
  237. package/dist/classifier.d.ts.map +0 -1
  238. package/dist/classifier.js +0 -439
  239. package/dist/classifier.js.map +0 -1
  240. package/dist/cli-action-SA7SCYNV.mjs +0 -95
  241. package/dist/cli-action-VCXBZGZP.mjs +0 -95
  242. package/dist/cli-action-YAJOJCXJ.mjs +0 -95
  243. package/dist/cli.d.ts.map +0 -1
  244. package/dist/cli.js.map +0 -1
  245. package/dist/cluster-detector.d.ts +0 -8
  246. package/dist/cluster-detector.d.ts.map +0 -1
  247. package/dist/cluster-detector.js +0 -70
  248. package/dist/cluster-detector.js.map +0 -1
  249. package/dist/defaults.d.ts +0 -7
  250. package/dist/defaults.d.ts.map +0 -1
  251. package/dist/defaults.js +0 -54
  252. package/dist/defaults.js.map +0 -1
  253. package/dist/graph-builder.d.ts +0 -33
  254. package/dist/graph-builder.d.ts.map +0 -1
  255. package/dist/graph-builder.js +0 -225
  256. package/dist/graph-builder.js.map +0 -1
  257. package/dist/index.d.ts.map +0 -1
  258. package/dist/index.js.map +0 -1
  259. package/dist/metrics.d.ts +0 -34
  260. package/dist/metrics.d.ts.map +0 -1
  261. package/dist/metrics.js +0 -170
  262. package/dist/metrics.js.map +0 -1
  263. package/dist/orchestrator-3L3NAZYP.mjs +0 -10
  264. package/dist/orchestrator-5AL3XBPZ.mjs +0 -10
  265. package/dist/orchestrator-KMAKMHTD.mjs +0 -10
  266. package/dist/orchestrator-MONOZHVW.mjs +0 -10
  267. package/dist/orchestrator-R6MZT4Z2.mjs +0 -10
  268. package/dist/orchestrator-ZR7JSKWI.mjs +0 -10
  269. package/dist/provider.d.ts +0 -6
  270. package/dist/provider.d.ts.map +0 -1
  271. package/dist/provider.js +0 -48
  272. package/dist/provider.js.map +0 -1
  273. package/dist/python-context-3GZKN3LR.mjs +0 -162
  274. package/dist/python-context-GOH747QU.mjs +0 -202
  275. package/dist/python-context-O2EN3M6Z.mjs +0 -162
  276. package/dist/python-context-PAETRLDY.mjs +0 -185
  277. package/dist/python-context-TBI5FVFY.mjs +0 -203
  278. package/dist/python-context-UOPTQH44.mjs +0 -192
  279. package/dist/remediation.d.ts +0 -25
  280. package/dist/remediation.d.ts.map +0 -1
  281. package/dist/remediation.js +0 -98
  282. package/dist/remediation.js.map +0 -1
  283. package/dist/scoring.d.ts +0 -9
  284. package/dist/scoring.d.ts.map +0 -1
  285. package/dist/scoring.js +0 -142
  286. package/dist/scoring.js.map +0 -1
  287. package/dist/semantic-analysis.d.ts +0 -33
  288. package/dist/semantic-analysis.d.ts.map +0 -1
  289. package/dist/semantic-analysis.js +0 -303
  290. package/dist/semantic-analysis.js.map +0 -1
  291. package/dist/summary-7PZVW72O.mjs +0 -7
  292. package/dist/summary-LKUCJAIS.mjs +0 -7
  293. package/dist/summary.d.ts +0 -6
  294. package/dist/summary.d.ts.map +0 -1
  295. package/dist/summary.js +0 -92
  296. package/dist/summary.js.map +0 -1
  297. package/dist/types.d.ts +0 -124
  298. package/dist/types.d.ts.map +0 -1
  299. package/dist/types.js +0 -2
  300. package/dist/types.js.map +0 -1
  301. package/dist/utils/output-formatter.d.ts +0 -14
  302. package/dist/utils/output-formatter.d.ts.map +0 -1
  303. package/dist/utils/output-formatter.js +0 -338
  304. package/dist/utils/output-formatter.js.map +0 -1
@@ -1,583 +0,0 @@
1
- // src/index.ts
2
- import { scanFiles, readFileContent } from "@aiready/core";
3
-
4
- // src/analyzer.ts
5
- import { estimateTokens } from "@aiready/core";
6
- function buildDependencyGraph(files) {
7
- const nodes = /* @__PURE__ */ new Map();
8
- const edges = /* @__PURE__ */ new Map();
9
- for (const { file, content } of files) {
10
- const imports = extractImportsFromContent(content);
11
- const exports = extractExports(content);
12
- const tokenCost = estimateTokens(content);
13
- const linesOfCode = content.split("\n").length;
14
- nodes.set(file, {
15
- file,
16
- imports,
17
- exports,
18
- tokenCost,
19
- linesOfCode
20
- });
21
- edges.set(file, new Set(imports));
22
- }
23
- return { nodes, edges };
24
- }
25
- function extractImportsFromContent(content) {
26
- const imports = [];
27
- const patterns = [
28
- /import\s+.*?\s+from\s+['"](.+?)['"]/g,
29
- // import ... from '...'
30
- /import\s+['"](.+?)['"]/g,
31
- // import '...'
32
- /require\(['"](.+?)['"]\)/g
33
- // require('...')
34
- ];
35
- for (const pattern of patterns) {
36
- let match;
37
- while ((match = pattern.exec(content)) !== null) {
38
- const importPath = match[1];
39
- if (importPath && !importPath.startsWith("@") && !importPath.startsWith("node:")) {
40
- imports.push(importPath);
41
- }
42
- }
43
- }
44
- return [...new Set(imports)];
45
- }
46
- function calculateImportDepth(file, graph, visited = /* @__PURE__ */ new Set(), depth = 0) {
47
- if (visited.has(file)) {
48
- return depth;
49
- }
50
- const dependencies = graph.edges.get(file);
51
- if (!dependencies || dependencies.size === 0) {
52
- return depth;
53
- }
54
- visited.add(file);
55
- let maxDepth = depth;
56
- for (const dep of dependencies) {
57
- const depDepth = calculateImportDepth(dep, graph, visited, depth + 1);
58
- maxDepth = Math.max(maxDepth, depDepth);
59
- }
60
- visited.delete(file);
61
- return maxDepth;
62
- }
63
- function getTransitiveDependencies(file, graph, visited = /* @__PURE__ */ new Set()) {
64
- if (visited.has(file)) {
65
- return [];
66
- }
67
- visited.add(file);
68
- const dependencies = graph.edges.get(file);
69
- if (!dependencies || dependencies.size === 0) {
70
- return [];
71
- }
72
- const allDeps = [];
73
- for (const dep of dependencies) {
74
- allDeps.push(dep);
75
- allDeps.push(...getTransitiveDependencies(dep, graph, visited));
76
- }
77
- return [...new Set(allDeps)];
78
- }
79
- function calculateContextBudget(file, graph) {
80
- const node = graph.nodes.get(file);
81
- if (!node) return 0;
82
- let totalTokens = node.tokenCost;
83
- const deps = getTransitiveDependencies(file, graph);
84
- for (const dep of deps) {
85
- const depNode = graph.nodes.get(dep);
86
- if (depNode) {
87
- totalTokens += depNode.tokenCost;
88
- }
89
- }
90
- return totalTokens;
91
- }
92
- function detectCircularDependencies(graph) {
93
- const cycles = [];
94
- const visited = /* @__PURE__ */ new Set();
95
- const recursionStack = /* @__PURE__ */ new Set();
96
- function dfs(file, path) {
97
- if (recursionStack.has(file)) {
98
- const cycleStart = path.indexOf(file);
99
- if (cycleStart !== -1) {
100
- cycles.push([...path.slice(cycleStart), file]);
101
- }
102
- return;
103
- }
104
- if (visited.has(file)) {
105
- return;
106
- }
107
- visited.add(file);
108
- recursionStack.add(file);
109
- path.push(file);
110
- const dependencies = graph.edges.get(file);
111
- if (dependencies) {
112
- for (const dep of dependencies) {
113
- dfs(dep, [...path]);
114
- }
115
- }
116
- recursionStack.delete(file);
117
- }
118
- for (const file of graph.nodes.keys()) {
119
- if (!visited.has(file)) {
120
- dfs(file, []);
121
- }
122
- }
123
- return cycles;
124
- }
125
- function calculateCohesion(exports) {
126
- if (exports.length === 0) return 1;
127
- if (exports.length === 1) return 1;
128
- const domains = exports.map((e) => e.inferredDomain || "unknown");
129
- const domainCounts = /* @__PURE__ */ new Map();
130
- for (const domain of domains) {
131
- domainCounts.set(domain, (domainCounts.get(domain) || 0) + 1);
132
- }
133
- const total = domains.length;
134
- let entropy = 0;
135
- for (const count of domainCounts.values()) {
136
- const p = count / total;
137
- if (p > 0) {
138
- entropy -= p * Math.log2(p);
139
- }
140
- }
141
- const maxEntropy = Math.log2(total);
142
- return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
143
- }
144
- function calculateFragmentation(files, domain) {
145
- if (files.length <= 1) return 0;
146
- const directories = new Set(files.map((f) => f.split("/").slice(0, -1).join("/")));
147
- return (directories.size - 1) / (files.length - 1);
148
- }
149
- function detectModuleClusters(graph) {
150
- const domainMap = /* @__PURE__ */ new Map();
151
- for (const [file, node] of graph.nodes.entries()) {
152
- const domains = node.exports.map((e) => e.inferredDomain || "unknown");
153
- const primaryDomain = domains[0] || "unknown";
154
- if (!domainMap.has(primaryDomain)) {
155
- domainMap.set(primaryDomain, []);
156
- }
157
- domainMap.get(primaryDomain).push(file);
158
- }
159
- const clusters = [];
160
- for (const [domain, files] of domainMap.entries()) {
161
- if (files.length < 2) continue;
162
- const totalTokens = files.reduce((sum, file) => {
163
- const node = graph.nodes.get(file);
164
- return sum + (node?.tokenCost || 0);
165
- }, 0);
166
- const fragmentationScore = calculateFragmentation(files, domain);
167
- const avgCohesion = files.reduce((sum, file) => {
168
- const node = graph.nodes.get(file);
169
- return sum + (node ? calculateCohesion(node.exports) : 0);
170
- }, 0) / files.length;
171
- const targetFiles = Math.max(1, Math.ceil(files.length / 3));
172
- const consolidationPlan = generateConsolidationPlan(
173
- domain,
174
- files,
175
- targetFiles
176
- );
177
- clusters.push({
178
- domain,
179
- files,
180
- totalTokens,
181
- fragmentationScore,
182
- avgCohesion,
183
- suggestedStructure: {
184
- targetFiles,
185
- consolidationPlan
186
- }
187
- });
188
- }
189
- return clusters.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
190
- }
191
- function extractExports(content) {
192
- const exports = [];
193
- const patterns = [
194
- /export\s+function\s+(\w+)/g,
195
- /export\s+class\s+(\w+)/g,
196
- /export\s+const\s+(\w+)/g,
197
- /export\s+type\s+(\w+)/g,
198
- /export\s+interface\s+(\w+)/g,
199
- /export\s+default/g
200
- ];
201
- const types = [
202
- "function",
203
- "class",
204
- "const",
205
- "type",
206
- "interface",
207
- "default"
208
- ];
209
- patterns.forEach((pattern, index) => {
210
- let match;
211
- while ((match = pattern.exec(content)) !== null) {
212
- const name = match[1] || "default";
213
- const type = types[index];
214
- const inferredDomain = inferDomain(name);
215
- exports.push({ name, type, inferredDomain });
216
- }
217
- });
218
- return exports;
219
- }
220
- function inferDomain(name) {
221
- const lower = name.toLowerCase();
222
- const domainKeywords = [
223
- "user",
224
- "auth",
225
- "order",
226
- "product",
227
- "payment",
228
- "cart",
229
- "invoice",
230
- "customer",
231
- "admin",
232
- "api",
233
- "util",
234
- "helper",
235
- "config",
236
- "service",
237
- "repository",
238
- "controller",
239
- "model",
240
- "view"
241
- ];
242
- for (const keyword of domainKeywords) {
243
- if (lower.includes(keyword)) {
244
- return keyword;
245
- }
246
- }
247
- return "unknown";
248
- }
249
- function generateConsolidationPlan(domain, files, targetFiles) {
250
- const plan = [];
251
- if (files.length <= targetFiles) {
252
- return [`No consolidation needed for ${domain}`];
253
- }
254
- plan.push(
255
- `Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s):`
256
- );
257
- const dirGroups = /* @__PURE__ */ new Map();
258
- for (const file of files) {
259
- const dir = file.split("/").slice(0, -1).join("/");
260
- if (!dirGroups.has(dir)) {
261
- dirGroups.set(dir, []);
262
- }
263
- dirGroups.get(dir).push(file);
264
- }
265
- plan.push(`1. Create unified ${domain} module file`);
266
- plan.push(
267
- `2. Move related functionality from ${files.length} scattered files`
268
- );
269
- plan.push(`3. Update imports in dependent files`);
270
- plan.push(
271
- `4. Remove old files after consolidation (verify with tests first)`
272
- );
273
- return plan;
274
- }
275
-
276
- // src/index.ts
277
- async function getSmartDefaults(directory, userOptions) {
278
- const files = await scanFiles({
279
- rootDir: directory,
280
- include: userOptions.include,
281
- exclude: userOptions.exclude
282
- });
283
- const estimatedBlocks = files.length;
284
- let maxDepth;
285
- let maxContextBudget;
286
- let minCohesion;
287
- let maxFragmentation;
288
- if (estimatedBlocks < 100) {
289
- maxDepth = 3;
290
- maxContextBudget = 5e3;
291
- minCohesion = 0.7;
292
- maxFragmentation = 0.3;
293
- } else if (estimatedBlocks < 500) {
294
- maxDepth = 4;
295
- maxContextBudget = 8e3;
296
- minCohesion = 0.65;
297
- maxFragmentation = 0.4;
298
- } else if (estimatedBlocks < 2e3) {
299
- maxDepth = 5;
300
- maxContextBudget = 12e3;
301
- minCohesion = 0.6;
302
- maxFragmentation = 0.5;
303
- } else {
304
- maxDepth = 6;
305
- maxContextBudget = 2e4;
306
- minCohesion = 0.55;
307
- maxFragmentation = 0.6;
308
- }
309
- return {
310
- maxDepth,
311
- maxContextBudget,
312
- minCohesion,
313
- maxFragmentation,
314
- focus: "all",
315
- includeNodeModules: false,
316
- rootDir: userOptions.rootDir || directory,
317
- include: userOptions.include,
318
- exclude: userOptions.exclude
319
- };
320
- }
321
- async function analyzeContext(options) {
322
- const {
323
- maxDepth = 5,
324
- maxContextBudget = 1e4,
325
- minCohesion = 0.6,
326
- maxFragmentation = 0.5,
327
- focus = "all",
328
- includeNodeModules = false,
329
- ...scanOptions
330
- } = options;
331
- const files = await scanFiles({
332
- ...scanOptions,
333
- exclude: includeNodeModules ? scanOptions.exclude : [...scanOptions.exclude || [], "**/node_modules/**"]
334
- });
335
- const fileContents = await Promise.all(
336
- files.map(async (file) => ({
337
- file,
338
- content: await readFileContent(file)
339
- }))
340
- );
341
- const graph = buildDependencyGraph(fileContents);
342
- const circularDeps = detectCircularDependencies(graph);
343
- const clusters = detectModuleClusters(graph);
344
- const fragmentationMap = /* @__PURE__ */ new Map();
345
- for (const cluster of clusters) {
346
- for (const file of cluster.files) {
347
- fragmentationMap.set(file, cluster.fragmentationScore);
348
- }
349
- }
350
- const results = [];
351
- for (const { file } of fileContents) {
352
- const node = graph.nodes.get(file);
353
- if (!node) continue;
354
- const importDepth = focus === "depth" || focus === "all" ? calculateImportDepth(file, graph) : 0;
355
- const dependencyList = focus === "depth" || focus === "all" ? getTransitiveDependencies(file, graph) : [];
356
- const contextBudget = focus === "all" ? calculateContextBudget(file, graph) : node.tokenCost;
357
- const cohesionScore = focus === "cohesion" || focus === "all" ? calculateCohesion(node.exports) : 1;
358
- const fragmentationScore = fragmentationMap.get(file) || 0;
359
- const relatedFiles = [];
360
- for (const cluster of clusters) {
361
- if (cluster.files.includes(file)) {
362
- relatedFiles.push(...cluster.files.filter((f) => f !== file));
363
- break;
364
- }
365
- }
366
- const { severity, issues, recommendations, potentialSavings } = analyzeIssues({
367
- file,
368
- importDepth,
369
- contextBudget,
370
- cohesionScore,
371
- fragmentationScore,
372
- maxDepth,
373
- maxContextBudget,
374
- minCohesion,
375
- maxFragmentation,
376
- circularDeps
377
- });
378
- const domains = [
379
- ...new Set(node.exports.map((e) => e.inferredDomain || "unknown"))
380
- ];
381
- results.push({
382
- file,
383
- tokenCost: node.tokenCost,
384
- linesOfCode: node.linesOfCode,
385
- importDepth,
386
- dependencyCount: dependencyList.length,
387
- dependencyList,
388
- circularDeps: circularDeps.filter((cycle) => cycle.includes(file)),
389
- cohesionScore,
390
- domains,
391
- exportCount: node.exports.length,
392
- contextBudget,
393
- fragmentationScore,
394
- relatedFiles,
395
- severity,
396
- issues,
397
- recommendations,
398
- potentialSavings
399
- });
400
- }
401
- return results.sort((a, b) => {
402
- const severityOrder = { critical: 0, major: 1, minor: 2, info: 3 };
403
- const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
404
- if (severityDiff !== 0) return severityDiff;
405
- return b.contextBudget - a.contextBudget;
406
- });
407
- }
408
- function generateSummary(results) {
409
- if (results.length === 0) {
410
- return {
411
- totalFiles: 0,
412
- totalTokens: 0,
413
- avgContextBudget: 0,
414
- maxContextBudget: 0,
415
- avgImportDepth: 0,
416
- maxImportDepth: 0,
417
- deepFiles: [],
418
- avgFragmentation: 0,
419
- fragmentedModules: [],
420
- avgCohesion: 0,
421
- lowCohesionFiles: [],
422
- criticalIssues: 0,
423
- majorIssues: 0,
424
- minorIssues: 0,
425
- totalPotentialSavings: 0,
426
- topExpensiveFiles: []
427
- };
428
- }
429
- const totalFiles = results.length;
430
- const totalTokens = results.reduce((sum, r) => sum + r.tokenCost, 0);
431
- const totalContextBudget = results.reduce(
432
- (sum, r) => sum + r.contextBudget,
433
- 0
434
- );
435
- const avgContextBudget = totalContextBudget / totalFiles;
436
- const maxContextBudget = Math.max(...results.map((r) => r.contextBudget));
437
- const avgImportDepth = results.reduce((sum, r) => sum + r.importDepth, 0) / totalFiles;
438
- const maxImportDepth = Math.max(...results.map((r) => r.importDepth));
439
- const deepFiles = results.filter((r) => r.importDepth >= 5).map((r) => ({ file: r.file, depth: r.importDepth })).sort((a, b) => b.depth - a.depth).slice(0, 10);
440
- const avgFragmentation = results.reduce((sum, r) => sum + r.fragmentationScore, 0) / totalFiles;
441
- const moduleMap = /* @__PURE__ */ new Map();
442
- for (const result of results) {
443
- for (const domain of result.domains) {
444
- if (!moduleMap.has(domain)) {
445
- moduleMap.set(domain, []);
446
- }
447
- moduleMap.get(domain).push(result);
448
- }
449
- }
450
- const fragmentedModules = [];
451
- for (const [domain, files] of moduleMap.entries()) {
452
- if (files.length < 2) continue;
453
- const fragmentationScore = files.reduce((sum, f) => sum + f.fragmentationScore, 0) / files.length;
454
- if (fragmentationScore < 0.3) continue;
455
- const totalTokens2 = files.reduce((sum, f) => sum + f.tokenCost, 0);
456
- const avgCohesion2 = files.reduce((sum, f) => sum + f.cohesionScore, 0) / files.length;
457
- const targetFiles = Math.max(1, Math.ceil(files.length / 3));
458
- fragmentedModules.push({
459
- domain,
460
- files: files.map((f) => f.file),
461
- totalTokens: totalTokens2,
462
- fragmentationScore,
463
- avgCohesion: avgCohesion2,
464
- suggestedStructure: {
465
- targetFiles,
466
- consolidationPlan: [
467
- `Consolidate ${files.length} ${domain} files into ${targetFiles} cohesive file(s)`,
468
- `Current token cost: ${totalTokens2.toLocaleString()}`,
469
- `Estimated savings: ${Math.floor(totalTokens2 * 0.3).toLocaleString()} tokens (30%)`
470
- ]
471
- }
472
- });
473
- }
474
- fragmentedModules.sort((a, b) => b.fragmentationScore - a.fragmentationScore);
475
- const avgCohesion = results.reduce((sum, r) => sum + r.cohesionScore, 0) / totalFiles;
476
- const lowCohesionFiles = results.filter((r) => r.cohesionScore < 0.6).map((r) => ({ file: r.file, score: r.cohesionScore })).sort((a, b) => a.score - b.score).slice(0, 10);
477
- const criticalIssues = results.filter((r) => r.severity === "critical").length;
478
- const majorIssues = results.filter((r) => r.severity === "major").length;
479
- const minorIssues = results.filter((r) => r.severity === "minor").length;
480
- const totalPotentialSavings = results.reduce(
481
- (sum, r) => sum + r.potentialSavings,
482
- 0
483
- );
484
- const topExpensiveFiles = results.sort((a, b) => b.contextBudget - a.contextBudget).slice(0, 10).map((r) => ({
485
- file: r.file,
486
- contextBudget: r.contextBudget,
487
- severity: r.severity
488
- }));
489
- return {
490
- totalFiles,
491
- totalTokens,
492
- avgContextBudget,
493
- maxContextBudget,
494
- avgImportDepth,
495
- maxImportDepth,
496
- deepFiles,
497
- avgFragmentation,
498
- fragmentedModules: fragmentedModules.slice(0, 10),
499
- avgCohesion,
500
- lowCohesionFiles,
501
- criticalIssues,
502
- majorIssues,
503
- minorIssues,
504
- totalPotentialSavings,
505
- topExpensiveFiles
506
- };
507
- }
508
- function analyzeIssues(params) {
509
- const {
510
- file,
511
- importDepth,
512
- contextBudget,
513
- cohesionScore,
514
- fragmentationScore,
515
- maxDepth,
516
- maxContextBudget,
517
- minCohesion,
518
- maxFragmentation,
519
- circularDeps
520
- } = params;
521
- const issues = [];
522
- const recommendations = [];
523
- let severity = "info";
524
- let potentialSavings = 0;
525
- if (circularDeps.length > 0) {
526
- severity = "critical";
527
- issues.push(
528
- `Part of ${circularDeps.length} circular dependency chain(s)`
529
- );
530
- recommendations.push("Break circular dependencies by extracting interfaces or using dependency injection");
531
- potentialSavings += contextBudget * 0.2;
532
- }
533
- if (importDepth > maxDepth * 1.5) {
534
- severity = severity === "critical" ? "critical" : "critical";
535
- issues.push(`Import depth ${importDepth} exceeds limit by 50%`);
536
- recommendations.push("Flatten dependency tree or use facade pattern");
537
- potentialSavings += contextBudget * 0.3;
538
- } else if (importDepth > maxDepth) {
539
- severity = severity === "critical" ? "critical" : "major";
540
- issues.push(`Import depth ${importDepth} exceeds recommended maximum ${maxDepth}`);
541
- recommendations.push("Consider reducing dependency depth");
542
- potentialSavings += contextBudget * 0.15;
543
- }
544
- if (contextBudget > maxContextBudget * 1.5) {
545
- severity = severity === "critical" ? "critical" : "critical";
546
- issues.push(`Context budget ${contextBudget.toLocaleString()} tokens is 50% over limit`);
547
- recommendations.push("Split into smaller modules or reduce dependency tree");
548
- potentialSavings += contextBudget * 0.4;
549
- } else if (contextBudget > maxContextBudget) {
550
- severity = severity === "critical" || severity === "major" ? severity : "major";
551
- issues.push(`Context budget ${contextBudget.toLocaleString()} exceeds ${maxContextBudget.toLocaleString()}`);
552
- recommendations.push("Reduce file size or dependencies");
553
- potentialSavings += contextBudget * 0.2;
554
- }
555
- if (cohesionScore < minCohesion * 0.5) {
556
- severity = severity === "critical" ? "critical" : "major";
557
- issues.push(`Very low cohesion (${(cohesionScore * 100).toFixed(0)}%) - mixed concerns`);
558
- recommendations.push("Split file by domain - separate unrelated functionality");
559
- potentialSavings += contextBudget * 0.25;
560
- } else if (cohesionScore < minCohesion) {
561
- severity = severity === "critical" || severity === "major" ? severity : "minor";
562
- issues.push(`Low cohesion (${(cohesionScore * 100).toFixed(0)}%)`);
563
- recommendations.push("Consider grouping related exports together");
564
- potentialSavings += contextBudget * 0.1;
565
- }
566
- if (fragmentationScore > maxFragmentation) {
567
- severity = severity === "critical" || severity === "major" ? severity : "minor";
568
- issues.push(`High fragmentation (${(fragmentationScore * 100).toFixed(0)}%) - scattered implementation`);
569
- recommendations.push("Consolidate with related files in same domain");
570
- potentialSavings += contextBudget * 0.3;
571
- }
572
- if (issues.length === 0) {
573
- issues.push("No significant issues detected");
574
- recommendations.push("File is well-structured for AI context usage");
575
- }
576
- return { severity, issues, recommendations, potentialSavings: Math.floor(potentialSavings) };
577
- }
578
-
579
- export {
580
- getSmartDefaults,
581
- analyzeContext,
582
- generateSummary
583
- };