@thoughtflow/codebase 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (278) hide show
  1. package/dist/contexts/context-decomposer.d.ts +13 -0
  2. package/dist/contexts/context-decomposer.d.ts.map +1 -0
  3. package/dist/contexts/context-decomposer.js +97 -0
  4. package/dist/contexts/context-decomposer.js.map +1 -0
  5. package/dist/contexts/context-document-manager.d.ts +21 -0
  6. package/dist/contexts/context-document-manager.d.ts.map +1 -0
  7. package/dist/contexts/context-document-manager.js +69 -0
  8. package/dist/contexts/context-document-manager.js.map +1 -0
  9. package/dist/contexts/context-router.d.ts +13 -0
  10. package/dist/contexts/context-router.d.ts.map +1 -0
  11. package/dist/contexts/context-router.js +94 -0
  12. package/dist/contexts/context-router.js.map +1 -0
  13. package/dist/index.d.ts +53 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +76 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/libs/codebase/deep-analyzer.d.ts +58 -0
  18. package/dist/libs/codebase/deep-analyzer.d.ts.map +1 -0
  19. package/dist/libs/codebase/deep-analyzer.js +239 -0
  20. package/dist/libs/codebase/deep-analyzer.js.map +1 -0
  21. package/dist/libs/codebase/graph/dependency-extractor.d.ts +20 -0
  22. package/dist/libs/codebase/graph/dependency-extractor.d.ts.map +1 -0
  23. package/dist/libs/codebase/graph/dependency-extractor.js +174 -0
  24. package/dist/libs/codebase/graph/dependency-extractor.js.map +1 -0
  25. package/dist/libs/codebase/graph/graph-analysis.d.ts +42 -0
  26. package/dist/libs/codebase/graph/graph-analysis.d.ts.map +1 -0
  27. package/dist/libs/codebase/graph/graph-analysis.js +222 -0
  28. package/dist/libs/codebase/graph/graph-analysis.js.map +1 -0
  29. package/dist/libs/codebase/graph/graph-visualizer.d.ts +12 -0
  30. package/dist/libs/codebase/graph/graph-visualizer.d.ts.map +1 -0
  31. package/dist/libs/codebase/graph/graph-visualizer.js +99 -0
  32. package/dist/libs/codebase/graph/graph-visualizer.js.map +1 -0
  33. package/dist/libs/codebase/indexer/batch-processor.d.ts +18 -0
  34. package/dist/libs/codebase/indexer/batch-processor.d.ts.map +1 -0
  35. package/dist/libs/codebase/indexer/batch-processor.js +39 -0
  36. package/dist/libs/codebase/indexer/batch-processor.js.map +1 -0
  37. package/dist/libs/codebase/indexer/chunker-markdown.d.ts +19 -0
  38. package/dist/libs/codebase/indexer/chunker-markdown.d.ts.map +1 -0
  39. package/dist/libs/codebase/indexer/chunker-markdown.js +138 -0
  40. package/dist/libs/codebase/indexer/chunker-markdown.js.map +1 -0
  41. package/dist/libs/codebase/indexer/chunker.d.ts +21 -0
  42. package/dist/libs/codebase/indexer/chunker.d.ts.map +1 -0
  43. package/dist/libs/codebase/indexer/chunker.js +133 -0
  44. package/dist/libs/codebase/indexer/chunker.js.map +1 -0
  45. package/dist/libs/codebase/indexer/explorer.d.ts +29 -0
  46. package/dist/libs/codebase/indexer/explorer.d.ts.map +1 -0
  47. package/dist/libs/codebase/indexer/explorer.js +278 -0
  48. package/dist/libs/codebase/indexer/explorer.js.map +1 -0
  49. package/dist/libs/codebase/indexer/indexer.d.ts +35 -0
  50. package/dist/libs/codebase/indexer/indexer.d.ts.map +1 -0
  51. package/dist/libs/codebase/indexer/indexer.js +172 -0
  52. package/dist/libs/codebase/indexer/indexer.js.map +1 -0
  53. package/dist/libs/codebase/knowledge/cross-referencer.d.ts +32 -0
  54. package/dist/libs/codebase/knowledge/cross-referencer.d.ts.map +1 -0
  55. package/dist/libs/codebase/knowledge/cross-referencer.js +132 -0
  56. package/dist/libs/codebase/knowledge/cross-referencer.js.map +1 -0
  57. package/dist/libs/codebase/knowledge/markdown-extractor.d.ts +34 -0
  58. package/dist/libs/codebase/knowledge/markdown-extractor.d.ts.map +1 -0
  59. package/dist/libs/codebase/knowledge/markdown-extractor.js +273 -0
  60. package/dist/libs/codebase/knowledge/markdown-extractor.js.map +1 -0
  61. package/dist/libs/codebase/knowledge/summarizer.d.ts +25 -0
  62. package/dist/libs/codebase/knowledge/summarizer.d.ts.map +1 -0
  63. package/dist/libs/codebase/knowledge/summarizer.js +61 -0
  64. package/dist/libs/codebase/knowledge/summarizer.js.map +1 -0
  65. package/dist/libs/codebase/search/bm25.d.ts +23 -0
  66. package/dist/libs/codebase/search/bm25.d.ts.map +1 -0
  67. package/dist/libs/codebase/search/bm25.js +93 -0
  68. package/dist/libs/codebase/search/bm25.js.map +1 -0
  69. package/dist/libs/codebase/search/hybrid-search.d.ts +22 -0
  70. package/dist/libs/codebase/search/hybrid-search.d.ts.map +1 -0
  71. package/dist/libs/codebase/search/hybrid-search.js +80 -0
  72. package/dist/libs/codebase/search/hybrid-search.js.map +1 -0
  73. package/dist/libs/codebase-index.class.d.ts +66 -0
  74. package/dist/libs/codebase-index.class.d.ts.map +1 -0
  75. package/dist/libs/codebase-index.class.js +595 -0
  76. package/dist/libs/codebase-index.class.js.map +1 -0
  77. package/dist/tools/codebase/codebase-analyze.tool.d.ts +8 -0
  78. package/dist/tools/codebase/codebase-analyze.tool.d.ts.map +1 -0
  79. package/dist/tools/codebase/codebase-analyze.tool.js +20 -0
  80. package/dist/tools/codebase/codebase-analyze.tool.js.map +1 -0
  81. package/dist/tools/codebase/codebase-crossref.tool.d.ts +8 -0
  82. package/dist/tools/codebase/codebase-crossref.tool.d.ts.map +1 -0
  83. package/dist/tools/codebase/codebase-crossref.tool.js +20 -0
  84. package/dist/tools/codebase/codebase-crossref.tool.js.map +1 -0
  85. package/dist/tools/codebase/codebase-flow.tool.d.ts +8 -0
  86. package/dist/tools/codebase/codebase-flow.tool.d.ts.map +1 -0
  87. package/dist/tools/codebase/codebase-flow.tool.js +20 -0
  88. package/dist/tools/codebase/codebase-flow.tool.js.map +1 -0
  89. package/dist/tools/codebase/codebase-graph-circular.tool.d.ts +8 -0
  90. package/dist/tools/codebase/codebase-graph-circular.tool.d.ts.map +1 -0
  91. package/dist/tools/codebase/codebase-graph-circular.tool.js +17 -0
  92. package/dist/tools/codebase/codebase-graph-circular.tool.js.map +1 -0
  93. package/dist/tools/codebase/codebase-graph-query.tool.d.ts +8 -0
  94. package/dist/tools/codebase/codebase-graph-query.tool.d.ts.map +1 -0
  95. package/dist/tools/codebase/codebase-graph-query.tool.js +21 -0
  96. package/dist/tools/codebase/codebase-graph-query.tool.js.map +1 -0
  97. package/dist/tools/codebase/codebase-graph-stats.tool.d.ts +8 -0
  98. package/dist/tools/codebase/codebase-graph-stats.tool.d.ts.map +1 -0
  99. package/dist/tools/codebase/codebase-graph-stats.tool.js +17 -0
  100. package/dist/tools/codebase/codebase-graph-stats.tool.js.map +1 -0
  101. package/dist/tools/codebase/codebase-graph-visualize.tool.d.ts +8 -0
  102. package/dist/tools/codebase/codebase-graph-visualize.tool.d.ts.map +1 -0
  103. package/dist/tools/codebase/codebase-graph-visualize.tool.js +21 -0
  104. package/dist/tools/codebase/codebase-graph-visualize.tool.js.map +1 -0
  105. package/dist/tools/codebase/codebase-impact.tool.d.ts +8 -0
  106. package/dist/tools/codebase/codebase-impact.tool.d.ts.map +1 -0
  107. package/dist/tools/codebase/codebase-impact.tool.js +21 -0
  108. package/dist/tools/codebase/codebase-impact.tool.js.map +1 -0
  109. package/dist/tools/codebase/codebase-index.tool.d.ts +8 -0
  110. package/dist/tools/codebase/codebase-index.tool.d.ts.map +1 -0
  111. package/dist/tools/codebase/codebase-index.tool.js +21 -0
  112. package/dist/tools/codebase/codebase-index.tool.js.map +1 -0
  113. package/dist/tools/codebase/codebase-knowledge.tool.d.ts +8 -0
  114. package/dist/tools/codebase/codebase-knowledge.tool.d.ts.map +1 -0
  115. package/dist/tools/codebase/codebase-knowledge.tool.js +22 -0
  116. package/dist/tools/codebase/codebase-knowledge.tool.js.map +1 -0
  117. package/dist/tools/codebase/codebase-search.tool.d.ts +8 -0
  118. package/dist/tools/codebase/codebase-search.tool.d.ts.map +1 -0
  119. package/dist/tools/codebase/codebase-search.tool.js +24 -0
  120. package/dist/tools/codebase/codebase-search.tool.js.map +1 -0
  121. package/dist/tools/codebase/codebase-status.tool.d.ts +8 -0
  122. package/dist/tools/codebase/codebase-status.tool.d.ts.map +1 -0
  123. package/dist/tools/codebase/codebase-status.tool.js +19 -0
  124. package/dist/tools/codebase/codebase-status.tool.js.map +1 -0
  125. package/dist/tools/neural/comparative-analysis.tool.d.ts +37 -0
  126. package/dist/tools/neural/comparative-analysis.tool.d.ts.map +1 -0
  127. package/dist/tools/neural/comparative-analysis.tool.js +241 -0
  128. package/dist/tools/neural/comparative-analysis.tool.js.map +1 -0
  129. package/dist/tools/neural/index.d.ts +12 -0
  130. package/dist/tools/neural/index.d.ts.map +1 -0
  131. package/dist/tools/neural/index.js +28 -0
  132. package/dist/tools/neural/index.js.map +1 -0
  133. package/dist/tools/neural/neural-deadcode.tool.d.ts +10 -0
  134. package/dist/tools/neural/neural-deadcode.tool.d.ts.map +1 -0
  135. package/dist/tools/neural/neural-deadcode.tool.js +133 -0
  136. package/dist/tools/neural/neural-deadcode.tool.js.map +1 -0
  137. package/dist/tools/neural/neural-debug.tool.d.ts +11 -0
  138. package/dist/tools/neural/neural-debug.tool.d.ts.map +1 -0
  139. package/dist/tools/neural/neural-debug.tool.js +205 -0
  140. package/dist/tools/neural/neural-debug.tool.js.map +1 -0
  141. package/dist/tools/neural/neural-explain.tool.d.ts +9 -0
  142. package/dist/tools/neural/neural-explain.tool.d.ts.map +1 -0
  143. package/dist/tools/neural/neural-explain.tool.js +100 -0
  144. package/dist/tools/neural/neural-explain.tool.js.map +1 -0
  145. package/dist/tools/neural/neural-optimize.tool.d.ts +9 -0
  146. package/dist/tools/neural/neural-optimize.tool.d.ts.map +1 -0
  147. package/dist/tools/neural/neural-optimize.tool.js +117 -0
  148. package/dist/tools/neural/neural-optimize.tool.js.map +1 -0
  149. package/dist/tools/neural/neural-pattern.tool.d.ts +10 -0
  150. package/dist/tools/neural/neural-pattern.tool.d.ts.map +1 -0
  151. package/dist/tools/neural/neural-pattern.tool.js +169 -0
  152. package/dist/tools/neural/neural-pattern.tool.js.map +1 -0
  153. package/dist/tools/neural/neural-review.tool.d.ts +11 -0
  154. package/dist/tools/neural/neural-review.tool.d.ts.map +1 -0
  155. package/dist/tools/neural/neural-review.tool.js +152 -0
  156. package/dist/tools/neural/neural-review.tool.js.map +1 -0
  157. package/dist/tools/neural/neural-security.tool.d.ts +16 -0
  158. package/dist/tools/neural/neural-security.tool.d.ts.map +1 -0
  159. package/dist/tools/neural/neural-security.tool.js +256 -0
  160. package/dist/tools/neural/neural-security.tool.js.map +1 -0
  161. package/dist/tools/neural/neural-similar.tool.d.ts +9 -0
  162. package/dist/tools/neural/neural-similar.tool.d.ts.map +1 -0
  163. package/dist/tools/neural/neural-similar.tool.js +139 -0
  164. package/dist/tools/neural/neural-similar.tool.js.map +1 -0
  165. package/dist/tools/neural/neural-smells.tool.d.ts +9 -0
  166. package/dist/tools/neural/neural-smells.tool.d.ts.map +1 -0
  167. package/dist/tools/neural/neural-smells.tool.js +123 -0
  168. package/dist/tools/neural/neural-smells.tool.js.map +1 -0
  169. package/dist/tools/neural/neural-suggest.tool.d.ts +9 -0
  170. package/dist/tools/neural/neural-suggest.tool.d.ts.map +1 -0
  171. package/dist/tools/neural/neural-suggest.tool.js +64 -0
  172. package/dist/tools/neural/neural-suggest.tool.js.map +1 -0
  173. package/dist/tools/quality/index.d.ts +8 -0
  174. package/dist/tools/quality/index.d.ts.map +1 -0
  175. package/dist/tools/quality/index.js +24 -0
  176. package/dist/tools/quality/index.js.map +1 -0
  177. package/dist/tools/quality/quality-complexity.tool.d.ts +11 -0
  178. package/dist/tools/quality/quality-complexity.tool.d.ts.map +1 -0
  179. package/dist/tools/quality/quality-complexity.tool.js +152 -0
  180. package/dist/tools/quality/quality-complexity.tool.js.map +1 -0
  181. package/dist/tools/quality/quality-coupling.tool.d.ts +9 -0
  182. package/dist/tools/quality/quality-coupling.tool.d.ts.map +1 -0
  183. package/dist/tools/quality/quality-coupling.tool.js +51 -0
  184. package/dist/tools/quality/quality-coupling.tool.js.map +1 -0
  185. package/dist/tools/quality/quality-coverage.tool.d.ts +9 -0
  186. package/dist/tools/quality/quality-coverage.tool.d.ts.map +1 -0
  187. package/dist/tools/quality/quality-coverage.tool.js +104 -0
  188. package/dist/tools/quality/quality-coverage.tool.js.map +1 -0
  189. package/dist/tools/quality/quality-deps.tool.d.ts +11 -0
  190. package/dist/tools/quality/quality-deps.tool.d.ts.map +1 -0
  191. package/dist/tools/quality/quality-deps.tool.js +119 -0
  192. package/dist/tools/quality/quality-deps.tool.js.map +1 -0
  193. package/dist/tools/quality/quality-duplication.tool.d.ts +9 -0
  194. package/dist/tools/quality/quality-duplication.tool.d.ts.map +1 -0
  195. package/dist/tools/quality/quality-duplication.tool.js +100 -0
  196. package/dist/tools/quality/quality-duplication.tool.js.map +1 -0
  197. package/dist/tools/quality/quality-size.tool.d.ts +9 -0
  198. package/dist/tools/quality/quality-size.tool.d.ts.map +1 -0
  199. package/dist/tools/quality/quality-size.tool.js +94 -0
  200. package/dist/tools/quality/quality-size.tool.js.map +1 -0
  201. package/dist/tools/quality/quality-types.tool.d.ts +9 -0
  202. package/dist/tools/quality/quality-types.tool.d.ts.map +1 -0
  203. package/dist/tools/quality/quality-types.tool.js +95 -0
  204. package/dist/tools/quality/quality-types.tool.js.map +1 -0
  205. package/dist/tools/workspace/analyze-project.tool.d.ts +36 -0
  206. package/dist/tools/workspace/analyze-project.tool.d.ts.map +1 -0
  207. package/dist/tools/workspace/analyze-project.tool.js +454 -0
  208. package/dist/tools/workspace/analyze-project.tool.js.map +1 -0
  209. package/dist/tools/workspace/codebase-research.tool.d.ts +10 -0
  210. package/dist/tools/workspace/codebase-research.tool.d.ts.map +1 -0
  211. package/dist/tools/workspace/codebase-research.tool.js +89 -0
  212. package/dist/tools/workspace/codebase-research.tool.js.map +1 -0
  213. package/dist/tools/workspace/project-scanner.tool.d.ts +55 -0
  214. package/dist/tools/workspace/project-scanner.tool.d.ts.map +1 -0
  215. package/dist/tools/workspace/project-scanner.tool.js +978 -0
  216. package/dist/tools/workspace/project-scanner.tool.js.map +1 -0
  217. package/dist/tools/workspace/search-code.tool.d.ts +14 -0
  218. package/dist/tools/workspace/search-code.tool.d.ts.map +1 -0
  219. package/dist/tools/workspace/search-code.tool.js +286 -0
  220. package/dist/tools/workspace/search-code.tool.js.map +1 -0
  221. package/package.json +17 -0
  222. package/src/contexts/context-decomposer.ts +113 -0
  223. package/src/contexts/context-document-manager.ts +70 -0
  224. package/src/contexts/context-router.ts +121 -0
  225. package/src/index.ts +65 -0
  226. package/src/libs/codebase/deep-analyzer.ts +287 -0
  227. package/src/libs/codebase/graph/dependency-extractor.ts +165 -0
  228. package/src/libs/codebase/graph/graph-analysis.ts +231 -0
  229. package/src/libs/codebase/graph/graph-visualizer.ts +124 -0
  230. package/src/libs/codebase/indexer/batch-processor.ts +47 -0
  231. package/src/libs/codebase/indexer/chunker-markdown.ts +175 -0
  232. package/src/libs/codebase/indexer/chunker.ts +126 -0
  233. package/src/libs/codebase/indexer/explorer.ts +269 -0
  234. package/src/libs/codebase/indexer/indexer.ts +174 -0
  235. package/src/libs/codebase/knowledge/cross-referencer.ts +182 -0
  236. package/src/libs/codebase/knowledge/markdown-extractor.ts +270 -0
  237. package/src/libs/codebase/knowledge/summarizer.ts +85 -0
  238. package/src/libs/codebase/search/bm25.ts +109 -0
  239. package/src/libs/codebase/search/hybrid-search.ts +109 -0
  240. package/src/libs/codebase-index.class.ts +715 -0
  241. package/src/tools/codebase/codebase-analyze.tool.ts +18 -0
  242. package/src/tools/codebase/codebase-crossref.tool.ts +18 -0
  243. package/src/tools/codebase/codebase-flow.tool.ts +18 -0
  244. package/src/tools/codebase/codebase-graph-circular.tool.ts +15 -0
  245. package/src/tools/codebase/codebase-graph-query.tool.ts +19 -0
  246. package/src/tools/codebase/codebase-graph-stats.tool.ts +15 -0
  247. package/src/tools/codebase/codebase-graph-visualize.tool.ts +19 -0
  248. package/src/tools/codebase/codebase-impact.tool.ts +19 -0
  249. package/src/tools/codebase/codebase-index.tool.ts +19 -0
  250. package/src/tools/codebase/codebase-knowledge.tool.ts +20 -0
  251. package/src/tools/codebase/codebase-search.tool.ts +22 -0
  252. package/src/tools/codebase/codebase-status.tool.ts +17 -0
  253. package/src/tools/neural/comparative-analysis.tool.ts +309 -0
  254. package/src/tools/neural/index.ts +11 -0
  255. package/src/tools/neural/neural-deadcode.tool.ts +105 -0
  256. package/src/tools/neural/neural-debug.tool.ts +199 -0
  257. package/src/tools/neural/neural-explain.tool.ts +67 -0
  258. package/src/tools/neural/neural-optimize.tool.ts +93 -0
  259. package/src/tools/neural/neural-pattern.tool.ts +141 -0
  260. package/src/tools/neural/neural-review.tool.ts +145 -0
  261. package/src/tools/neural/neural-security.tool.ts +226 -0
  262. package/src/tools/neural/neural-similar.tool.ts +113 -0
  263. package/src/tools/neural/neural-smells.tool.ts +98 -0
  264. package/src/tools/neural/neural-suggest.tool.ts +65 -0
  265. package/src/tools/quality/index.ts +7 -0
  266. package/src/tools/quality/quality-complexity.tool.ts +132 -0
  267. package/src/tools/quality/quality-coupling.tool.ts +57 -0
  268. package/src/tools/quality/quality-coverage.tool.ts +78 -0
  269. package/src/tools/quality/quality-deps.tool.ts +89 -0
  270. package/src/tools/quality/quality-duplication.tool.ts +71 -0
  271. package/src/tools/quality/quality-size.tool.ts +66 -0
  272. package/src/tools/quality/quality-types.tool.ts +66 -0
  273. package/src/tools/workspace/analyze-project.tool.ts +429 -0
  274. package/src/tools/workspace/codebase-research.tool.ts +100 -0
  275. package/src/tools/workspace/project-scanner.tool.ts +1058 -0
  276. package/src/tools/workspace/search-code.tool.ts +344 -0
  277. package/tsconfig.build.json +12 -0
  278. package/tsconfig.json +9 -0
@@ -0,0 +1,231 @@
1
+ import { DependencyGraph, DependencyEdge } from "./dependency-extractor";
2
+ import * as path from "node:path";
3
+
4
+ export interface ResolvedEdge {
5
+ from: string;
6
+ to: string;
7
+ resolvedPath?: string;
8
+ isExternal: boolean;
9
+ type: string;
10
+ lineNumber: number;
11
+ }
12
+
13
+ export class DependencyResolver {
14
+ resolve(graph: DependencyGraph, rootPath: string): ResolvedEdge[] {
15
+ const resolved: ResolvedEdge[] = [];
16
+
17
+ for (const edge of graph.edges) {
18
+ if (edge.to.startsWith(".") || edge.to.startsWith("/")) {
19
+ const fromDir = path.dirname(edge.from);
20
+ const candidate = path.join(fromDir, edge.to).replace(/\\/g, "/");
21
+ resolved.push({
22
+ from: edge.from,
23
+ to: edge.to,
24
+ resolvedPath: candidate,
25
+ isExternal: false,
26
+ type: edge.type,
27
+ lineNumber: edge.lineNumber,
28
+ });
29
+ } else {
30
+ resolved.push({
31
+ from: edge.from,
32
+ to: edge.to,
33
+ isExternal: true,
34
+ type: edge.type,
35
+ lineNumber: edge.lineNumber,
36
+ });
37
+ }
38
+ }
39
+
40
+ return resolved;
41
+ }
42
+
43
+ getAdjacencyList(graph: DependencyGraph): Map<string, Set<string>> {
44
+ const adj = new Map<string, Set<string>>();
45
+ for (const edge of graph.edges) {
46
+ if (!adj.has(edge.from)) adj.set(edge.from, new Set());
47
+ adj.get(edge.from)!.add(edge.to);
48
+ }
49
+ return adj;
50
+ }
51
+
52
+ getReverseAdjacency(graph: DependencyGraph): Map<string, Set<string>> {
53
+ const rev = new Map<string, Set<string>>();
54
+ for (const edge of graph.edges) {
55
+ if (!rev.has(edge.to)) rev.set(edge.to, new Set());
56
+ rev.get(edge.to)!.add(edge.from);
57
+ }
58
+ return rev;
59
+ }
60
+ }
61
+
62
+ export class CircularDetector {
63
+ findCycles(graph: DependencyGraph): string[][] {
64
+ const adj = new Map<string, Set<string>>();
65
+ const allNodes = new Set<string>();
66
+
67
+ for (const edge of graph.edges) {
68
+ allNodes.add(edge.from);
69
+ allNodes.add(edge.to);
70
+ if (!adj.has(edge.from)) adj.set(edge.from, new Set());
71
+ adj.get(edge.from)!.add(edge.to);
72
+ }
73
+
74
+ const index = new Map<string, number>();
75
+ const lowlink = new Map<string, number>();
76
+ const onStack = new Set<string>();
77
+ const stack: string[] = [];
78
+ const sccs: string[][] = [];
79
+ let idx = 0;
80
+
81
+ const strongconnect = (v: string): void => {
82
+ index.set(v, idx);
83
+ lowlink.set(v, idx);
84
+ idx++;
85
+ stack.push(v);
86
+ onStack.add(v);
87
+
88
+ const neighbors = adj.get(v);
89
+ if (neighbors) {
90
+ for (const w of neighbors) {
91
+ if (!index.has(w)) {
92
+ strongconnect(w);
93
+ lowlink.set(v, Math.min(lowlink.get(v)!, lowlink.get(w)!));
94
+ } else if (onStack.has(w)) {
95
+ lowlink.set(v, Math.min(lowlink.get(v)!, index.get(w)!));
96
+ }
97
+ }
98
+ }
99
+
100
+ if (lowlink.get(v) === index.get(v)) {
101
+ const scc: string[] = [];
102
+ let w: string;
103
+ do {
104
+ w = stack.pop()!;
105
+ onStack.delete(w);
106
+ scc.push(w);
107
+ } while (w !== v);
108
+
109
+ if (scc.length > 1) {
110
+ sccs.push(scc);
111
+ }
112
+ }
113
+ };
114
+
115
+ for (const node of allNodes) {
116
+ if (!index.has(node)) {
117
+ strongconnect(node);
118
+ }
119
+ }
120
+
121
+ return sccs;
122
+ }
123
+ }
124
+
125
+ export interface ImpactResult {
126
+ target: string;
127
+ hops: Array<{ distance: number; files: string[] }>;
128
+ totalAffectedFiles: number;
129
+ }
130
+
131
+ export class ImpactAnalyzer {
132
+ analyze(targetFile: string, graph: DependencyGraph, maxHops: number = 3): ImpactResult {
133
+ const rev = new Map<string, Set<string>>();
134
+
135
+ for (const edge of graph.edges) {
136
+ const key = edge.to;
137
+ if (!rev.has(key)) rev.set(key, new Set());
138
+ rev.get(key)!.add(edge.from);
139
+ }
140
+
141
+ const visited = new Set<string>();
142
+ const hops: Array<{ distance: number; files: string[] }> = [];
143
+ let currentLayer = [targetFile];
144
+ visited.add(targetFile);
145
+
146
+ for (let d = 1; d <= maxHops && currentLayer.length > 0; d++) {
147
+ const nextLayer: string[] = [];
148
+ for (const file of currentLayer) {
149
+ const dependents = rev.get(file);
150
+ if (!dependents) continue;
151
+ for (const dep of dependents) {
152
+ if (!visited.has(dep)) {
153
+ visited.add(dep);
154
+ nextLayer.push(dep);
155
+ }
156
+ }
157
+ }
158
+
159
+ if (nextLayer.length > 0) {
160
+ hops.push({ distance: d, files: nextLayer.sort() });
161
+ }
162
+ currentLayer = nextLayer;
163
+ }
164
+
165
+ return {
166
+ target: targetFile,
167
+ hops,
168
+ totalAffectedFiles: visited.size - 1,
169
+ };
170
+ }
171
+ }
172
+
173
+ export interface FlowResult {
174
+ entrypoint: string;
175
+ path: FlowStep[];
176
+ maxDepth: number;
177
+ }
178
+
179
+ export interface FlowStep {
180
+ file: string;
181
+ callees: string[];
182
+ depth: number;
183
+ }
184
+
185
+ export class CallTracer {
186
+ traceForward(
187
+ entrypoint: string,
188
+ graph: DependencyGraph,
189
+ maxDepth: number = 5
190
+ ): FlowResult {
191
+ const adj = new Map<string, Set<string>>();
192
+ for (const edge of graph.edges) {
193
+ if (!adj.has(edge.from)) adj.set(edge.from, new Set());
194
+ adj.get(edge.from)!.add(edge.to);
195
+ }
196
+
197
+ const visited = new Set<string>();
198
+ const path: FlowStep[] = [];
199
+ const queue: Array<{ file: string; depth: number }> = [{ file: entrypoint, depth: 0 }];
200
+ visited.add(entrypoint);
201
+
202
+ while (queue.length > 0) {
203
+ const current = queue.shift()!;
204
+ if (current.depth > maxDepth) continue;
205
+
206
+ const callees = adj.get(current.file);
207
+ const calleeList = callees ? Array.from(callees) : [];
208
+
209
+ path.push({
210
+ file: current.file,
211
+ callees: calleeList,
212
+ depth: current.depth,
213
+ });
214
+
215
+ if (callees) {
216
+ for (const callee of callees) {
217
+ if (!visited.has(callee)) {
218
+ visited.add(callee);
219
+ queue.push({ file: callee, depth: current.depth + 1 });
220
+ }
221
+ }
222
+ }
223
+ }
224
+
225
+ return {
226
+ entrypoint,
227
+ path,
228
+ maxDepth,
229
+ };
230
+ }
231
+ }
@@ -0,0 +1,124 @@
1
+ import { DependencyGraph } from "./dependency-extractor";
2
+
3
+ export interface VisualizeOptions {
4
+ maxNodes?: number;
5
+ highlightCircular?: boolean;
6
+ colorByLanguage?: boolean;
7
+ direction?: 'TD' | 'LR' | 'RL' | 'BT';
8
+ }
9
+
10
+ export class GraphVisualizer {
11
+ toMermaid(graph: DependencyGraph, options: VisualizeOptions = {}): string {
12
+ const maxNodes = options.maxNodes ?? 100;
13
+ const highlightCircular = options.highlightCircular ?? true;
14
+ const direction = options.direction ?? 'TD';
15
+
16
+ const circulars = new Set<string>();
17
+ if (highlightCircular) {
18
+ const { CircularDetector } = require("./graph-analysis");
19
+ const cycles = new CircularDetector().findCycles(graph);
20
+ for (const cycle of cycles) {
21
+ for (const node of cycle) {
22
+ circulars.add(node);
23
+ }
24
+ }
25
+ }
26
+
27
+ const nodeSet = new Set<string>();
28
+ const nodeEdges: Array<{ from: string; to: string }> = [];
29
+
30
+ for (const edge of graph.edges) {
31
+ if (edge.to.startsWith("http") || (!edge.to.startsWith(".") && !edge.to.startsWith("/"))) continue;
32
+ nodeSet.add(edge.from);
33
+ nodeSet.add(edge.to);
34
+ nodeEdges.push({ from: edge.from, to: edge.to });
35
+ }
36
+
37
+ const sortedNodes = Array.from(nodeSet).sort().slice(0, maxNodes);
38
+ const filteredEdges = nodeEdges.filter(
39
+ (e) => sortedNodes.includes(e.from) && sortedNodes.includes(e.to)
40
+ );
41
+
42
+ const lines: string[] = [`graph ${direction}`];
43
+ const nodeIds = new Map<string, string>();
44
+
45
+ for (const node of sortedNodes) {
46
+ const id = `N${sortedNodes.indexOf(node)}`;
47
+ nodeIds.set(node, id);
48
+ const shortName = node.split("/").pop()?.replace(/\.[^.]+$/, "") ?? node;
49
+ const label = shortName.length > 30 ? shortName.slice(0, 27) + "..." : shortName;
50
+ const isCircular = circulars.has(node);
51
+ const style = isCircular ? ":::circular" : "";
52
+ lines.push(` ${id}[${JSON.stringify(label)}]${style}`);
53
+ }
54
+
55
+ if (highlightCircular && circulars.size > 0) {
56
+ lines.push(` classDef circular fill:#ff6b6b,stroke:#c92a2a,color:white`);
57
+ }
58
+
59
+ for (const edge of filteredEdges) {
60
+ const fromId = nodeIds.get(edge.from);
61
+ const toId = nodeIds.get(edge.to);
62
+ if (fromId && toId) {
63
+ lines.push(` ${fromId} --> ${toId}`);
64
+ }
65
+ }
66
+
67
+ return lines.join("\n");
68
+ }
69
+
70
+ toHtml(graph: DependencyGraph, title: string = "Dependency Graph"): string {
71
+ const nodes = new Set<string>();
72
+ const edges: Array<{ from: string; to: string }> = [];
73
+
74
+ for (const edge of graph.edges) {
75
+ if (edge.to.startsWith("http") || (!edge.to.startsWith(".") && !edge.to.startsWith("/"))) continue;
76
+ nodes.add(edge.from);
77
+ nodes.add(edge.to);
78
+ edges.push({ from: edge.from, to: edge.to });
79
+ }
80
+
81
+ const nodeArray = Array.from(nodes).sort().slice(0, 150);
82
+ const filteredEdges = edges.filter(
83
+ (e) => nodeArray.includes(e.from) && nodeArray.includes(e.to)
84
+ );
85
+
86
+ const nodeJson = JSON.stringify(
87
+ nodeArray.map((n) => ({
88
+ id: n,
89
+ label: n.split("/").pop()?.replace(/\.[^.]+$/, "") ?? n,
90
+ }))
91
+ );
92
+
93
+ const edgeJson = JSON.stringify(
94
+ filteredEdges.map((e) => ({ from: e.from, to: e.to }))
95
+ );
96
+
97
+ return `<!DOCTYPE html>
98
+ <html lang="en">
99
+ <head>
100
+ <meta charset="UTF-8">
101
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
102
+ <title>${title}</title>
103
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.9.0/mermaid.min.js"></script>
104
+ <style>
105
+ body { margin: 0; padding: 20px; font-family: monospace; background: #1e1e2e; color: #cdd6f4; }
106
+ #container { max-width: 100%; overflow: auto; }
107
+ h1 { color: #89b4fa; }
108
+ .stats { color: #a6adc8; font-size: 14px; margin-bottom: 20px; }
109
+ .mermaid { background: #181825; padding: 20px; border-radius: 8px; }
110
+ </style>
111
+ </head>
112
+ <body>
113
+ <h1>${title}</h1>
114
+ <div class="stats">${nodeArray.length} nodes, ${filteredEdges.length} edges</div>
115
+ <div id="container">
116
+ <pre class="mermaid">
117
+ ${this.toMermaid(graph, { maxNodes: 150 })}
118
+ </pre>
119
+ </div>
120
+ <script>mermaid.initialize({ startOnLoad: true, theme: 'dark' });</script>
121
+ </body>
122
+ </html>`;
123
+ }
124
+ }
@@ -0,0 +1,47 @@
1
+ export interface BatchProgress {
2
+ phase: string;
3
+ current: number;
4
+ total: number;
5
+ elapsedMs: number;
6
+ }
7
+
8
+ export class BatchProcessor {
9
+ private progress: BatchProgress = { phase: "idle", current: 0, total: 0, elapsedMs: 0 };
10
+ private startTime = 0;
11
+ private checkpointCallback?: (progress: BatchProgress) => Promise<void>;
12
+
13
+ onCheckpoint(cb: (progress: BatchProgress) => Promise<void>): void {
14
+ this.checkpointCallback = cb;
15
+ }
16
+
17
+ start(total: number, phase: string): void {
18
+ this.progress = { phase, current: 0, total, elapsedMs: 0 };
19
+ this.startTime = Date.now();
20
+ }
21
+
22
+ async advance(delta: number): Promise<void> {
23
+ this.progress.current = Math.min(this.progress.current + delta, this.progress.total);
24
+ this.progress.elapsedMs = Date.now() - this.startTime;
25
+ if (this.checkpointCallback && this.progress.current % 50 === 0) {
26
+ await this.checkpointCallback(this.progress);
27
+ }
28
+ }
29
+
30
+ getProgress(): BatchProgress {
31
+ return { ...this.progress };
32
+ }
33
+
34
+ complete(): BatchProgress {
35
+ this.progress.current = this.progress.total;
36
+ this.progress.elapsedMs = Date.now() - this.startTime;
37
+ return { ...this.progress };
38
+ }
39
+
40
+ chunk<T>(items: T[], size: number): T[][] {
41
+ const batches: T[][] = [];
42
+ for (let i = 0; i < items.length; i += size) {
43
+ batches.push(items.slice(i, i + size));
44
+ }
45
+ return batches;
46
+ }
47
+ }
@@ -0,0 +1,175 @@
1
+ import type { DiscoveredFile } from "./explorer";
2
+
3
+ export interface MarkdownChunk {
4
+ id: string;
5
+ text: string;
6
+ startLine: number;
7
+ endLine: number;
8
+ headingLevel: number;
9
+ headingText: string;
10
+ parentHeading?: string;
11
+ frontmatter?: Record<string, unknown>;
12
+ docType?: 'guideline' | 'architecture' | 'decision' | 'business' | 'technical' | 'audit' | 'unknown';
13
+ }
14
+
15
+ export class MarkdownChunker {
16
+ chunkFile(file: DiscoveredFile, content: string): MarkdownChunk[] {
17
+ const lines = content.split("\n");
18
+ const frontmatter = this.parseFrontmatter(lines);
19
+ const contentStart = frontmatter ? frontmatter.endLine + 1 : 0;
20
+
21
+ const sections = this.splitSections(lines, contentStart);
22
+ const docType = this.classifyDocType(file, frontmatter?.data, content);
23
+
24
+ return sections.map((section, index) => ({
25
+ id: `${file.relativePath}:md:${index}`,
26
+ text: section.text.slice(0, 3000),
27
+ startLine: section.startLine + 1,
28
+ endLine: section.endLine + 1,
29
+ headingLevel: section.level,
30
+ headingText: section.heading,
31
+ parentHeading: section.parent,
32
+ frontmatter: frontmatter?.data,
33
+ docType,
34
+ }));
35
+ }
36
+
37
+ private parseFrontmatter(lines: string[]): { data: Record<string, unknown>; endLine: number } | null {
38
+ if (lines.length === 0 || lines[0].trim() !== "---") return null;
39
+
40
+ let endLine = -1;
41
+ for (let i = 1; i < lines.length; i++) {
42
+ if (lines[i].trim() === "---") {
43
+ endLine = i;
44
+ break;
45
+ }
46
+ }
47
+ if (endLine === -1) return null;
48
+
49
+ const data: Record<string, unknown> = {};
50
+ for (let i = 1; i < endLine; i++) {
51
+ const line = lines[i].trim();
52
+ const colonIdx = line.indexOf(":");
53
+ if (colonIdx === -1) continue;
54
+ const key = line.slice(0, colonIdx).trim();
55
+ const value = line.slice(colonIdx + 1).trim();
56
+ if (value.startsWith("[") && value.endsWith("]")) {
57
+ data[key] = value
58
+ .slice(1, -1)
59
+ .split(",")
60
+ .map((s) => s.trim().replace(/^["']|["']$/g, ""));
61
+ } else {
62
+ data[key] = value.replace(/^["']|["']$/g, "");
63
+ }
64
+ }
65
+
66
+ return { data, endLine };
67
+ }
68
+
69
+ private splitSections(
70
+ lines: string[],
71
+ startLine: number
72
+ ): Array<{ heading: string; level: number; parent?: string; text: string; startLine: number; endLine: number }> {
73
+ const sections: Array<{
74
+ heading: string;
75
+ level: number;
76
+ parent?: string;
77
+ text: string;
78
+ startLine: number;
79
+ endLine: number;
80
+ }> = [];
81
+
82
+ const headingStack: Array<{ text: string; level: number; line: number }> = [];
83
+ let currentContent: string[] = [];
84
+ let currentStart = startLine;
85
+ let currentHeading = "";
86
+ let currentLevel = 0;
87
+
88
+ for (let i = startLine; i < lines.length; i++) {
89
+ const line = lines[i];
90
+ const headingMatch = line.match(/^(#{1,6})\s+(.+)/);
91
+
92
+ if (headingMatch) {
93
+ if (currentContent.length > 0) {
94
+ sections.push({
95
+ heading: currentHeading || "root",
96
+ level: currentLevel,
97
+ parent: headingStack.length > 0 ? headingStack[headingStack.length - 1].text : undefined,
98
+ text: currentContent.join("\n"),
99
+ startLine: currentStart,
100
+ endLine: i - 1,
101
+ });
102
+ }
103
+
104
+ const level = headingMatch[1].length;
105
+ const headingText = headingMatch[2].trim();
106
+
107
+ while (headingStack.length > 0 && headingStack[headingStack.length - 1].level >= level) {
108
+ headingStack.pop();
109
+ }
110
+ headingStack.push({ text: headingText, level, line: i });
111
+
112
+ currentHeading = headingText;
113
+ currentLevel = level;
114
+ currentContent = [line];
115
+ currentStart = i;
116
+ } else {
117
+ currentContent.push(line);
118
+ }
119
+ }
120
+
121
+ if (currentContent.length > 0) {
122
+ sections.push({
123
+ heading: currentHeading || "root",
124
+ level: currentLevel,
125
+ parent: headingStack.length > 0 ? headingStack[headingStack.length - 1].text : undefined,
126
+ text: currentContent.join("\n"),
127
+ startLine: currentStart,
128
+ endLine: lines.length - 1,
129
+ });
130
+ }
131
+
132
+ return sections;
133
+ }
134
+
135
+ private classifyDocType(
136
+ file: DiscoveredFile,
137
+ frontmatter?: Record<string, unknown>,
138
+ content?: string
139
+ ): MarkdownChunk['docType'] {
140
+ const fp = file.relativePath.toLowerCase();
141
+ const txt = content?.toLowerCase() ?? '';
142
+
143
+ if (fp.includes('architecture') || fp.includes('arch/') || txt.includes('architecture decision')) {
144
+ return 'architecture';
145
+ }
146
+ if (fp.includes('decision') || fp.includes('adr') || txt.includes('decision record')) {
147
+ return 'decision';
148
+ }
149
+ if (fp.includes('business') || fp.includes('biznes') || fp.includes('bp-')) {
150
+ return 'business';
151
+ }
152
+ if (fp.includes('api') || fp.includes('endpoint') || txt.includes('api specification')) {
153
+ return 'technical';
154
+ }
155
+ if (fp.includes('audit') || txt.includes('audit report')) {
156
+ return 'audit';
157
+ }
158
+ if (fp.includes('guideline') || fp.includes('guide') || fp.includes('standard') || fp.includes('convention')) {
159
+ return 'guideline';
160
+ }
161
+ if (fp.includes('readme') || fp.includes('contributing') || txt.includes('documentation')) {
162
+ return 'technical';
163
+ }
164
+
165
+ const fm = frontmatter as Record<string, unknown> | undefined;
166
+ if (fm?.type) {
167
+ const t = String(fm.type).toLowerCase();
168
+ if (['architecture', 'decision', 'business', 'technical', 'audit', 'guideline'].includes(t)) {
169
+ return t as MarkdownChunk['docType'];
170
+ }
171
+ }
172
+
173
+ return 'unknown';
174
+ }
175
+ }
@@ -0,0 +1,126 @@
1
+ import type { DiscoveredFile } from "./explorer";
2
+
3
+ export interface CodeChunk {
4
+ id: string;
5
+ text: string;
6
+ startLine: number;
7
+ endLine: number;
8
+ type: 'function' | 'class' | 'interface' | 'import-block' | 'export' | 'other';
9
+ name?: string;
10
+ }
11
+
12
+ export interface ChunkResult {
13
+ chunks: CodeChunk[];
14
+ language: string;
15
+ totalLines: number;
16
+ }
17
+
18
+ const LANG_MAP: Record<string, string> = {
19
+ ".ts": "TypeScript", ".tsx": "Tsx", ".mts": "TypeScript", ".cts": "TypeScript",
20
+ ".js": "JavaScript", ".jsx": "JavaScript", ".mjs": "JavaScript", ".cjs": "JavaScript",
21
+ ".html": "Html",
22
+ };
23
+
24
+ async function parseWithAstGrep(lang: string, content: string) {
25
+ const { Lang, parse } = await import("@ast-grep/napi");
26
+ return parse(Lang[lang as keyof typeof Lang], content);
27
+ }
28
+
29
+ const IMPORT_DQ = 'import $$$ from "$SOURCE"';
30
+ const IMPORT_SQ = "import $$$ from '$SOURCE'";
31
+
32
+ export class CodeChunker {
33
+ async chunkFile(file: DiscoveredFile, content: string): Promise<ChunkResult> {
34
+ const lang = LANG_MAP[file.extension];
35
+ const totalLines = content.split("\n").length;
36
+
37
+ if (!lang) {
38
+ return this.lineBasedFallback(file, content, totalLines);
39
+ }
40
+
41
+ try {
42
+ const root = (await parseWithAstGrep(lang, content)).root();
43
+ const chunks: CodeChunk[] = [];
44
+
45
+ const importNodes = [...root.findAll(IMPORT_DQ), ...root.findAll(IMPORT_SQ)];
46
+ if (importNodes.length > 0) {
47
+ const text = importNodes.map((n) => n.text()).join("\n");
48
+ chunks.push({
49
+ id: `${file.relativePath}:imports`,
50
+ text: text.slice(0, 2000),
51
+ startLine: importNodes[0].range().start.line + 1,
52
+ endLine: importNodes[importNodes.length - 1].range().end.line + 1,
53
+ type: 'import-block',
54
+ });
55
+ }
56
+
57
+ const fnRule = { rule: { kind: "function_declaration", pattern: "function $NAME" } };
58
+ for (const node of root.findAll(fnRule as any)) {
59
+ const name = node.getMatch('NAME')?.text() ?? '(anonymous)';
60
+ chunks.push({
61
+ id: `${file.relativePath}:fn:${name}`,
62
+ text: node.text().slice(0, 2000),
63
+ startLine: node.range().start.line + 1,
64
+ endLine: node.range().end.line + 1,
65
+ type: 'function',
66
+ name,
67
+ });
68
+ }
69
+
70
+ const clsRule = { rule: { kind: "class_declaration", pattern: "class $NAME" } };
71
+ for (const node of root.findAll(clsRule as any)) {
72
+ const name = node.getMatch('NAME')?.text() ?? '(anonymous)';
73
+ chunks.push({
74
+ id: `${file.relativePath}:class:${name}`,
75
+ text: node.text().slice(0, 2000),
76
+ startLine: node.range().start.line + 1,
77
+ endLine: node.range().end.line + 1,
78
+ type: 'class',
79
+ name,
80
+ });
81
+ }
82
+
83
+ if (chunks.length === 0 || this.coverageRatio(chunks, content) < 0.1) {
84
+ const lineChunks = this.lineBasedChunks(file, content, totalLines);
85
+ return { chunks: lineChunks, language: file.language ?? 'unknown', totalLines };
86
+ }
87
+
88
+ return { chunks, language: file.language ?? 'unknown', totalLines };
89
+ } catch {
90
+ return this.lineBasedFallback(file, content, totalLines);
91
+ }
92
+ }
93
+
94
+ private coverageRatio(chunks: CodeChunk[], content: string): number {
95
+ const covered = chunks.reduce((sum, c) => sum + c.text.length, 0);
96
+ return content.length > 0 ? covered / content.length : 0;
97
+ }
98
+
99
+ private lineBasedFallback(file: DiscoveredFile, content: string, totalLines: number): ChunkResult {
100
+ return {
101
+ chunks: this.lineBasedChunks(file, content, totalLines),
102
+ language: file.language ?? 'unknown',
103
+ totalLines,
104
+ };
105
+ }
106
+
107
+ private lineBasedChunks(file: DiscoveredFile, content: string, totalLines: number): CodeChunk[] {
108
+ const lines = content.split("\n");
109
+ const chunkSize = 100;
110
+ const chunks: CodeChunk[] = [];
111
+
112
+ for (let i = 0; i < lines.length; i += chunkSize) {
113
+ const end = Math.min(i + chunkSize, lines.length);
114
+ const text = lines.slice(i, end).join("\n");
115
+ chunks.push({
116
+ id: `${file.relativePath}:${i + 1}-${end}`,
117
+ text: text.slice(0, 2000),
118
+ startLine: i + 1,
119
+ endLine: end,
120
+ type: 'other',
121
+ });
122
+ }
123
+
124
+ return chunks;
125
+ }
126
+ }