@vpxa/kb 0.1.1

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 (275) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1140 -0
  3. package/bin/kb.mjs +10 -0
  4. package/package.json +67 -0
  5. package/packages/analyzers/dist/blast-radius-analyzer.d.ts +23 -0
  6. package/packages/analyzers/dist/blast-radius-analyzer.js +114 -0
  7. package/packages/analyzers/dist/dependency-analyzer.d.ts +29 -0
  8. package/packages/analyzers/dist/dependency-analyzer.js +425 -0
  9. package/packages/analyzers/dist/diagram-generator.d.ts +13 -0
  10. package/packages/analyzers/dist/diagram-generator.js +86 -0
  11. package/packages/analyzers/dist/entry-point-analyzer.d.ts +19 -0
  12. package/packages/analyzers/dist/entry-point-analyzer.js +239 -0
  13. package/packages/analyzers/dist/index.d.ts +14 -0
  14. package/packages/analyzers/dist/index.js +23 -0
  15. package/packages/analyzers/dist/knowledge-producer.d.ts +32 -0
  16. package/packages/analyzers/dist/knowledge-producer.js +113 -0
  17. package/packages/analyzers/dist/pattern-analyzer.d.ts +12 -0
  18. package/packages/analyzers/dist/pattern-analyzer.js +359 -0
  19. package/packages/analyzers/dist/regex-call-graph.d.ts +17 -0
  20. package/packages/analyzers/dist/regex-call-graph.js +428 -0
  21. package/packages/analyzers/dist/structure-analyzer.d.ts +11 -0
  22. package/packages/analyzers/dist/structure-analyzer.js +258 -0
  23. package/packages/analyzers/dist/symbol-analyzer.d.ts +10 -0
  24. package/packages/analyzers/dist/symbol-analyzer.js +442 -0
  25. package/packages/analyzers/dist/ts-call-graph.d.ts +27 -0
  26. package/packages/analyzers/dist/ts-call-graph.js +160 -0
  27. package/packages/analyzers/dist/types.d.ts +98 -0
  28. package/packages/analyzers/dist/types.js +1 -0
  29. package/packages/chunker/dist/call-graph-extractor.d.ts +22 -0
  30. package/packages/chunker/dist/call-graph-extractor.js +90 -0
  31. package/packages/chunker/dist/chunker-factory.d.ts +7 -0
  32. package/packages/chunker/dist/chunker-factory.js +36 -0
  33. package/packages/chunker/dist/chunker.interface.d.ts +10 -0
  34. package/packages/chunker/dist/chunker.interface.js +1 -0
  35. package/packages/chunker/dist/code-chunker.d.ts +14 -0
  36. package/packages/chunker/dist/code-chunker.js +134 -0
  37. package/packages/chunker/dist/generic-chunker.d.ts +12 -0
  38. package/packages/chunker/dist/generic-chunker.js +72 -0
  39. package/packages/chunker/dist/index.d.ts +8 -0
  40. package/packages/chunker/dist/index.js +21 -0
  41. package/packages/chunker/dist/markdown-chunker.d.ts +14 -0
  42. package/packages/chunker/dist/markdown-chunker.js +122 -0
  43. package/packages/chunker/dist/treesitter-chunker.d.ts +47 -0
  44. package/packages/chunker/dist/treesitter-chunker.js +234 -0
  45. package/packages/cli/dist/commands/analyze.d.ts +3 -0
  46. package/packages/cli/dist/commands/analyze.js +112 -0
  47. package/packages/cli/dist/commands/context-cmds.d.ts +3 -0
  48. package/packages/cli/dist/commands/context-cmds.js +155 -0
  49. package/packages/cli/dist/commands/environment.d.ts +3 -0
  50. package/packages/cli/dist/commands/environment.js +204 -0
  51. package/packages/cli/dist/commands/execution.d.ts +3 -0
  52. package/packages/cli/dist/commands/execution.js +137 -0
  53. package/packages/cli/dist/commands/graph.d.ts +3 -0
  54. package/packages/cli/dist/commands/graph.js +81 -0
  55. package/packages/cli/dist/commands/init.d.ts +8 -0
  56. package/packages/cli/dist/commands/init.js +87 -0
  57. package/packages/cli/dist/commands/knowledge.d.ts +3 -0
  58. package/packages/cli/dist/commands/knowledge.js +139 -0
  59. package/packages/cli/dist/commands/search.d.ts +3 -0
  60. package/packages/cli/dist/commands/search.js +267 -0
  61. package/packages/cli/dist/commands/system.d.ts +3 -0
  62. package/packages/cli/dist/commands/system.js +241 -0
  63. package/packages/cli/dist/commands/workspace.d.ts +3 -0
  64. package/packages/cli/dist/commands/workspace.js +388 -0
  65. package/packages/cli/dist/context.d.ts +5 -0
  66. package/packages/cli/dist/context.js +14 -0
  67. package/packages/cli/dist/helpers.d.ts +52 -0
  68. package/packages/cli/dist/helpers.js +458 -0
  69. package/packages/cli/dist/index.d.ts +8 -0
  70. package/packages/cli/dist/index.js +69 -0
  71. package/packages/cli/dist/kb-init.d.ts +57 -0
  72. package/packages/cli/dist/kb-init.js +82 -0
  73. package/packages/cli/dist/types.d.ts +7 -0
  74. package/packages/cli/dist/types.js +1 -0
  75. package/packages/core/dist/constants.d.ts +49 -0
  76. package/packages/core/dist/constants.js +43 -0
  77. package/packages/core/dist/content-detector.d.ts +9 -0
  78. package/packages/core/dist/content-detector.js +79 -0
  79. package/packages/core/dist/errors.d.ts +18 -0
  80. package/packages/core/dist/errors.js +40 -0
  81. package/packages/core/dist/index.d.ts +6 -0
  82. package/packages/core/dist/index.js +9 -0
  83. package/packages/core/dist/logger.d.ts +9 -0
  84. package/packages/core/dist/logger.js +34 -0
  85. package/packages/core/dist/types.d.ts +108 -0
  86. package/packages/core/dist/types.js +1 -0
  87. package/packages/embeddings/dist/embedder.interface.d.ts +24 -0
  88. package/packages/embeddings/dist/embedder.interface.js +1 -0
  89. package/packages/embeddings/dist/index.d.ts +3 -0
  90. package/packages/embeddings/dist/index.js +5 -0
  91. package/packages/embeddings/dist/onnx-embedder.d.ts +24 -0
  92. package/packages/embeddings/dist/onnx-embedder.js +82 -0
  93. package/packages/indexer/dist/file-hasher.d.ts +11 -0
  94. package/packages/indexer/dist/file-hasher.js +13 -0
  95. package/packages/indexer/dist/filesystem-crawler.d.ts +27 -0
  96. package/packages/indexer/dist/filesystem-crawler.js +125 -0
  97. package/packages/indexer/dist/graph-extractor.d.ts +22 -0
  98. package/packages/indexer/dist/graph-extractor.js +111 -0
  99. package/packages/indexer/dist/incremental-indexer.d.ts +47 -0
  100. package/packages/indexer/dist/incremental-indexer.js +278 -0
  101. package/packages/indexer/dist/index.d.ts +5 -0
  102. package/packages/indexer/dist/index.js +14 -0
  103. package/packages/server/dist/api.d.ts +8 -0
  104. package/packages/server/dist/api.js +9 -0
  105. package/packages/server/dist/config.d.ts +3 -0
  106. package/packages/server/dist/config.js +75 -0
  107. package/packages/server/dist/curated-manager.d.ts +86 -0
  108. package/packages/server/dist/curated-manager.js +357 -0
  109. package/packages/server/dist/index.d.ts +2 -0
  110. package/packages/server/dist/index.js +134 -0
  111. package/packages/server/dist/replay-interceptor.d.ts +11 -0
  112. package/packages/server/dist/replay-interceptor.js +38 -0
  113. package/packages/server/dist/resources/resources.d.ts +4 -0
  114. package/packages/server/dist/resources/resources.js +40 -0
  115. package/packages/server/dist/server.d.ts +21 -0
  116. package/packages/server/dist/server.js +247 -0
  117. package/packages/server/dist/tools/analyze.tools.d.ts +11 -0
  118. package/packages/server/dist/tools/analyze.tools.js +288 -0
  119. package/packages/server/dist/tools/forge.tools.d.ts +12 -0
  120. package/packages/server/dist/tools/forge.tools.js +501 -0
  121. package/packages/server/dist/tools/forget.tool.d.ts +4 -0
  122. package/packages/server/dist/tools/forget.tool.js +43 -0
  123. package/packages/server/dist/tools/graph.tool.d.ts +4 -0
  124. package/packages/server/dist/tools/graph.tool.js +110 -0
  125. package/packages/server/dist/tools/list.tool.d.ts +4 -0
  126. package/packages/server/dist/tools/list.tool.js +56 -0
  127. package/packages/server/dist/tools/lookup.tool.d.ts +4 -0
  128. package/packages/server/dist/tools/lookup.tool.js +53 -0
  129. package/packages/server/dist/tools/onboard.tool.d.ts +5 -0
  130. package/packages/server/dist/tools/onboard.tool.js +112 -0
  131. package/packages/server/dist/tools/produce.tool.d.ts +3 -0
  132. package/packages/server/dist/tools/produce.tool.js +74 -0
  133. package/packages/server/dist/tools/read.tool.d.ts +4 -0
  134. package/packages/server/dist/tools/read.tool.js +49 -0
  135. package/packages/server/dist/tools/reindex.tool.d.ts +7 -0
  136. package/packages/server/dist/tools/reindex.tool.js +70 -0
  137. package/packages/server/dist/tools/remember.tool.d.ts +4 -0
  138. package/packages/server/dist/tools/remember.tool.js +45 -0
  139. package/packages/server/dist/tools/replay.tool.d.ts +3 -0
  140. package/packages/server/dist/tools/replay.tool.js +89 -0
  141. package/packages/server/dist/tools/search.tool.d.ts +5 -0
  142. package/packages/server/dist/tools/search.tool.js +331 -0
  143. package/packages/server/dist/tools/status.tool.d.ts +4 -0
  144. package/packages/server/dist/tools/status.tool.js +68 -0
  145. package/packages/server/dist/tools/toolkit.tools.d.ts +35 -0
  146. package/packages/server/dist/tools/toolkit.tools.js +1674 -0
  147. package/packages/server/dist/tools/update.tool.d.ts +4 -0
  148. package/packages/server/dist/tools/update.tool.js +42 -0
  149. package/packages/server/dist/tools/utility.tools.d.ts +15 -0
  150. package/packages/server/dist/tools/utility.tools.js +461 -0
  151. package/packages/store/dist/graph-store.interface.d.ts +104 -0
  152. package/packages/store/dist/graph-store.interface.js +1 -0
  153. package/packages/store/dist/index.d.ts +6 -0
  154. package/packages/store/dist/index.js +9 -0
  155. package/packages/store/dist/lance-store.d.ts +32 -0
  156. package/packages/store/dist/lance-store.js +258 -0
  157. package/packages/store/dist/sqlite-graph-store.d.ts +43 -0
  158. package/packages/store/dist/sqlite-graph-store.js +374 -0
  159. package/packages/store/dist/store-factory.d.ts +9 -0
  160. package/packages/store/dist/store-factory.js +14 -0
  161. package/packages/store/dist/store.interface.d.ts +48 -0
  162. package/packages/store/dist/store.interface.js +1 -0
  163. package/packages/tools/dist/batch.d.ts +21 -0
  164. package/packages/tools/dist/batch.js +45 -0
  165. package/packages/tools/dist/changelog.d.ts +34 -0
  166. package/packages/tools/dist/changelog.js +112 -0
  167. package/packages/tools/dist/check.d.ts +26 -0
  168. package/packages/tools/dist/check.js +59 -0
  169. package/packages/tools/dist/checkpoint.d.ts +17 -0
  170. package/packages/tools/dist/checkpoint.js +43 -0
  171. package/packages/tools/dist/codemod.d.ts +37 -0
  172. package/packages/tools/dist/codemod.js +69 -0
  173. package/packages/tools/dist/compact.d.ts +41 -0
  174. package/packages/tools/dist/compact.js +60 -0
  175. package/packages/tools/dist/data-transform.d.ts +10 -0
  176. package/packages/tools/dist/data-transform.js +124 -0
  177. package/packages/tools/dist/dead-symbols.d.ts +21 -0
  178. package/packages/tools/dist/dead-symbols.js +71 -0
  179. package/packages/tools/dist/delegate.d.ts +34 -0
  180. package/packages/tools/dist/delegate.js +130 -0
  181. package/packages/tools/dist/diff-parse.d.ts +26 -0
  182. package/packages/tools/dist/diff-parse.js +153 -0
  183. package/packages/tools/dist/digest.d.ts +53 -0
  184. package/packages/tools/dist/digest.js +242 -0
  185. package/packages/tools/dist/encode.d.ts +14 -0
  186. package/packages/tools/dist/encode.js +46 -0
  187. package/packages/tools/dist/env-info.d.ts +28 -0
  188. package/packages/tools/dist/env-info.js +58 -0
  189. package/packages/tools/dist/eval.d.ts +13 -0
  190. package/packages/tools/dist/eval.js +79 -0
  191. package/packages/tools/dist/evidence-map.d.ts +79 -0
  192. package/packages/tools/dist/evidence-map.js +203 -0
  193. package/packages/tools/dist/file-summary.d.ts +32 -0
  194. package/packages/tools/dist/file-summary.js +106 -0
  195. package/packages/tools/dist/file-walk.d.ts +4 -0
  196. package/packages/tools/dist/file-walk.js +75 -0
  197. package/packages/tools/dist/find-examples.d.ts +25 -0
  198. package/packages/tools/dist/find-examples.js +48 -0
  199. package/packages/tools/dist/find.d.ts +47 -0
  200. package/packages/tools/dist/find.js +120 -0
  201. package/packages/tools/dist/forge-classify.d.ts +44 -0
  202. package/packages/tools/dist/forge-classify.js +319 -0
  203. package/packages/tools/dist/forge-ground.d.ts +64 -0
  204. package/packages/tools/dist/forge-ground.js +184 -0
  205. package/packages/tools/dist/git-context.d.ts +22 -0
  206. package/packages/tools/dist/git-context.js +46 -0
  207. package/packages/tools/dist/graph-query.d.ts +89 -0
  208. package/packages/tools/dist/graph-query.js +194 -0
  209. package/packages/tools/dist/health.d.ts +14 -0
  210. package/packages/tools/dist/health.js +118 -0
  211. package/packages/tools/dist/http-request.d.ts +23 -0
  212. package/packages/tools/dist/http-request.js +58 -0
  213. package/packages/tools/dist/index.d.ts +49 -0
  214. package/packages/tools/dist/index.js +273 -0
  215. package/packages/tools/dist/lane.d.ts +39 -0
  216. package/packages/tools/dist/lane.js +227 -0
  217. package/packages/tools/dist/measure.d.ts +38 -0
  218. package/packages/tools/dist/measure.js +119 -0
  219. package/packages/tools/dist/onboard.d.ts +41 -0
  220. package/packages/tools/dist/onboard.js +1139 -0
  221. package/packages/tools/dist/parse-output.d.ts +80 -0
  222. package/packages/tools/dist/parse-output.js +158 -0
  223. package/packages/tools/dist/process-manager.d.ts +18 -0
  224. package/packages/tools/dist/process-manager.js +69 -0
  225. package/packages/tools/dist/queue.d.ts +38 -0
  226. package/packages/tools/dist/queue.js +126 -0
  227. package/packages/tools/dist/regex-test.d.ts +31 -0
  228. package/packages/tools/dist/regex-test.js +39 -0
  229. package/packages/tools/dist/rename.d.ts +29 -0
  230. package/packages/tools/dist/rename.js +70 -0
  231. package/packages/tools/dist/replay.d.ts +56 -0
  232. package/packages/tools/dist/replay.js +108 -0
  233. package/packages/tools/dist/schema-validate.d.ts +23 -0
  234. package/packages/tools/dist/schema-validate.js +141 -0
  235. package/packages/tools/dist/scope-map.d.ts +52 -0
  236. package/packages/tools/dist/scope-map.js +72 -0
  237. package/packages/tools/dist/snippet.d.ts +34 -0
  238. package/packages/tools/dist/snippet.js +80 -0
  239. package/packages/tools/dist/stash.d.ts +12 -0
  240. package/packages/tools/dist/stash.js +60 -0
  241. package/packages/tools/dist/stratum-card.d.ts +31 -0
  242. package/packages/tools/dist/stratum-card.js +239 -0
  243. package/packages/tools/dist/symbol.d.ts +28 -0
  244. package/packages/tools/dist/symbol.js +87 -0
  245. package/packages/tools/dist/test-run.d.ts +23 -0
  246. package/packages/tools/dist/test-run.js +55 -0
  247. package/packages/tools/dist/text-utils.d.ts +16 -0
  248. package/packages/tools/dist/text-utils.js +31 -0
  249. package/packages/tools/dist/time-utils.d.ts +18 -0
  250. package/packages/tools/dist/time-utils.js +135 -0
  251. package/packages/tools/dist/trace.d.ts +24 -0
  252. package/packages/tools/dist/trace.js +114 -0
  253. package/packages/tools/dist/truncation.d.ts +22 -0
  254. package/packages/tools/dist/truncation.js +45 -0
  255. package/packages/tools/dist/watch.d.ts +30 -0
  256. package/packages/tools/dist/watch.js +61 -0
  257. package/packages/tools/dist/web-fetch.d.ts +45 -0
  258. package/packages/tools/dist/web-fetch.js +249 -0
  259. package/packages/tools/dist/web-search.d.ts +23 -0
  260. package/packages/tools/dist/web-search.js +46 -0
  261. package/packages/tools/dist/workset.d.ts +45 -0
  262. package/packages/tools/dist/workset.js +77 -0
  263. package/packages/tui/dist/App.d.ts +8 -0
  264. package/packages/tui/dist/App.js +52659 -0
  265. package/packages/tui/dist/index.d.ts +19 -0
  266. package/packages/tui/dist/index.js +54742 -0
  267. package/packages/tui/dist/panels/CuratedPanel.d.ts +8 -0
  268. package/packages/tui/dist/panels/CuratedPanel.js +34452 -0
  269. package/packages/tui/dist/panels/LogPanel.d.ts +3 -0
  270. package/packages/tui/dist/panels/LogPanel.js +51894 -0
  271. package/packages/tui/dist/panels/SearchPanel.d.ts +10 -0
  272. package/packages/tui/dist/panels/SearchPanel.js +34985 -0
  273. package/packages/tui/dist/panels/StatusPanel.d.ts +8 -0
  274. package/packages/tui/dist/panels/StatusPanel.js +34465 -0
  275. package/skills/knowledge-base/SKILL.md +316 -0
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Tree-sitter AST-based code chunker.
3
+ *
4
+ * Uses native tree-sitter bindings for precise declaration boundary detection.
5
+ * Falls back gracefully to regex-based CodeChunker if tree-sitter or grammars
6
+ * are not installed (they are optional dependencies).
7
+ */
8
+ import type { ChunkMetadata, RawChunk } from '@kb/core';
9
+ import type { IChunker } from './chunker.interface.js';
10
+ /**
11
+ * Singleton runtime that loads tree-sitter parser + language grammars once.
12
+ * Call `TreeSitterRuntime.initialize()` at startup; if it returns null,
13
+ * tree-sitter is not available and the factory will fall back to CodeChunker.
14
+ */
15
+ export declare class TreeSitterRuntime {
16
+ private static instance;
17
+ private parser;
18
+ private languages;
19
+ static initialize(): Promise<TreeSitterRuntime | null>;
20
+ static get(): TreeSitterRuntime | null;
21
+ private init;
22
+ /** Parse source code with the grammar for the given file extension. */
23
+ parse(content: string, ext: string): any | null;
24
+ /** Check if a grammar is loaded for the given file extension. */
25
+ hasLanguage(ext: string): boolean;
26
+ }
27
+ /**
28
+ * Initialize the tree-sitter runtime. Call once at startup.
29
+ * Returns true if tree-sitter is available, false otherwise.
30
+ */
31
+ export declare function initializeTreeSitter(): Promise<boolean>;
32
+ /**
33
+ * AST-based code chunker that uses tree-sitter for precise declaration
34
+ * boundary detection. Produces cleaner chunks than the regex-based CodeChunker.
35
+ */
36
+ export declare class TreeSitterChunker implements IChunker {
37
+ private readonly maxChunkSize;
38
+ private readonly runtime;
39
+ private readonly regexFallback;
40
+ constructor(runtime: TreeSitterRuntime, options?: {
41
+ maxChunkSize?: number;
42
+ });
43
+ chunk(content: string, metadata: ChunkMetadata): RawChunk[];
44
+ private chunkFromAst;
45
+ private splitByLines;
46
+ }
47
+ //# sourceMappingURL=treesitter-chunker.d.ts.map
@@ -0,0 +1,234 @@
1
+ import { extname } from "node:path";
2
+ import { CHUNK_SIZES } from "@kb/core";
3
+ import { CodeChunker } from "./code-chunker.js";
4
+ const LANGUAGE_MODULES = {
5
+ ".ts": { package: "tree-sitter-typescript", subLanguage: "typescript" },
6
+ ".tsx": { package: "tree-sitter-typescript", subLanguage: "tsx" },
7
+ ".mts": { package: "tree-sitter-typescript", subLanguage: "typescript" },
8
+ ".cts": { package: "tree-sitter-typescript", subLanguage: "typescript" },
9
+ ".js": { package: "tree-sitter-javascript" },
10
+ ".jsx": { package: "tree-sitter-javascript" },
11
+ ".mjs": { package: "tree-sitter-javascript" },
12
+ ".cjs": { package: "tree-sitter-javascript" },
13
+ ".py": { package: "tree-sitter-python" },
14
+ ".go": { package: "tree-sitter-go" },
15
+ ".rs": { package: "tree-sitter-rust" },
16
+ ".java": { package: "tree-sitter-java" }
17
+ };
18
+ const DECLARATION_TYPES = /* @__PURE__ */ new Set([
19
+ // TypeScript / JavaScript
20
+ "function_declaration",
21
+ "class_declaration",
22
+ "interface_declaration",
23
+ "type_alias_declaration",
24
+ "enum_declaration",
25
+ "lexical_declaration",
26
+ "export_statement",
27
+ "variable_declaration",
28
+ "abstract_class_declaration",
29
+ // Python
30
+ "function_definition",
31
+ "class_definition",
32
+ "decorated_definition",
33
+ // Go
34
+ "function_declaration",
35
+ "method_declaration",
36
+ "type_declaration",
37
+ // Rust
38
+ "function_item",
39
+ "impl_item",
40
+ "struct_item",
41
+ "enum_item",
42
+ "trait_item",
43
+ "mod_item",
44
+ "type_item",
45
+ // Java
46
+ "class_declaration",
47
+ "interface_declaration",
48
+ "method_declaration",
49
+ "enum_declaration"
50
+ ]);
51
+ class TreeSitterRuntime {
52
+ static instance = null;
53
+ // biome-ignore lint/suspicious/noExplicitAny: tree-sitter parser has no published types
54
+ parser = null;
55
+ // biome-ignore lint/suspicious/noExplicitAny: tree-sitter language has no published types
56
+ languages = /* @__PURE__ */ new Map();
57
+ static async initialize() {
58
+ if (TreeSitterRuntime.instance) return TreeSitterRuntime.instance;
59
+ const runtime = new TreeSitterRuntime();
60
+ if (await runtime.init()) {
61
+ TreeSitterRuntime.instance = runtime;
62
+ return runtime;
63
+ }
64
+ return null;
65
+ }
66
+ static get() {
67
+ return TreeSitterRuntime.instance;
68
+ }
69
+ async init() {
70
+ try {
71
+ const parserMod = await import("tree-sitter");
72
+ const ParserClass = parserMod.default ?? parserMod;
73
+ this.parser = new ParserClass();
74
+ for (const [ext, config] of Object.entries(LANGUAGE_MODULES)) {
75
+ try {
76
+ const mod = await import(config.package);
77
+ const resolved = mod.default ?? mod;
78
+ const lang = config.subLanguage ? resolved[config.subLanguage] : resolved;
79
+ if (lang) this.languages.set(ext, lang);
80
+ } catch {
81
+ }
82
+ }
83
+ const loaded = this.languages.size > 0;
84
+ if (loaded) {
85
+ console.error(
86
+ `[KB] Tree-sitter initialized with ${this.languages.size} languages: ${[...this.languages.keys()].join(", ")}`
87
+ );
88
+ }
89
+ return loaded;
90
+ } catch {
91
+ return false;
92
+ }
93
+ }
94
+ /** Parse source code with the grammar for the given file extension. */
95
+ // biome-ignore lint/suspicious/noExplicitAny: tree-sitter tree has no published types
96
+ parse(content, ext) {
97
+ const lang = this.languages.get(ext);
98
+ if (!lang) return null;
99
+ this.parser.setLanguage(lang);
100
+ return this.parser.parse(content);
101
+ }
102
+ /** Check if a grammar is loaded for the given file extension. */
103
+ hasLanguage(ext) {
104
+ return this.languages.has(ext);
105
+ }
106
+ }
107
+ async function initializeTreeSitter() {
108
+ const runtime = await TreeSitterRuntime.initialize();
109
+ return runtime !== null;
110
+ }
111
+ class TreeSitterChunker {
112
+ maxChunkSize;
113
+ runtime;
114
+ regexFallback;
115
+ constructor(runtime, options) {
116
+ this.runtime = runtime;
117
+ this.maxChunkSize = options?.maxChunkSize ?? CHUNK_SIZES.code.max;
118
+ this.regexFallback = new CodeChunker(options);
119
+ }
120
+ chunk(content, metadata) {
121
+ const ext = extname(metadata.sourcePath).toLowerCase();
122
+ const tree = this.runtime.parse(content, ext);
123
+ if (!tree) {
124
+ return this.regexFallback.chunk(content, metadata);
125
+ }
126
+ try {
127
+ return this.chunkFromAst(tree, content, metadata);
128
+ } catch {
129
+ return this.regexFallback.chunk(content, metadata);
130
+ }
131
+ }
132
+ // biome-ignore lint/suspicious/noExplicitAny: tree-sitter tree has no published types
133
+ chunkFromAst(tree, content, metadata) {
134
+ const rootNode = tree.rootNode;
135
+ const chunks = [];
136
+ const declarations = [];
137
+ for (let i = 0; i < rootNode.childCount; i++) {
138
+ const child = rootNode.child(i);
139
+ if (!child) continue;
140
+ if (DECLARATION_TYPES.has(child.type)) {
141
+ let startByte = child.startPosition ? child.startIndex : child.startByte ?? 0;
142
+ let startRow = child.startPosition?.row ?? 0;
143
+ let prev = child.previousSibling;
144
+ while (prev && (prev.type === "comment" || prev.type === "line_comment" || prev.type === "block_comment" || prev.type === "decorator")) {
145
+ startByte = prev.startIndex ?? prev.startByte ?? startByte;
146
+ startRow = prev.startPosition?.row ?? startRow;
147
+ prev = prev.previousSibling;
148
+ }
149
+ declarations.push({
150
+ startByte,
151
+ endByte: child.endIndex ?? child.endByte ?? content.length,
152
+ startRow,
153
+ endRow: child.endPosition?.row ?? content.split("\n").length - 1
154
+ });
155
+ }
156
+ }
157
+ if (declarations.length === 0) {
158
+ return this.regexFallback.chunk(content, metadata);
159
+ }
160
+ if (declarations[0].startByte > 0) {
161
+ const preamble = content.slice(0, declarations[0].startByte).trim();
162
+ if (preamble.length > 0) {
163
+ chunks.push({
164
+ text: `// File: ${metadata.sourcePath}
165
+ ${preamble}`,
166
+ sourcePath: metadata.sourcePath,
167
+ contentType: metadata.contentType,
168
+ chunkIndex: 0,
169
+ totalChunks: 0,
170
+ startLine: 1,
171
+ endLine: declarations[0].startRow + 1
172
+ });
173
+ }
174
+ }
175
+ for (const decl of declarations) {
176
+ let text = content.slice(decl.startByte, decl.endByte).trim();
177
+ const header = `// File: ${metadata.sourcePath}
178
+ `;
179
+ text = header + text;
180
+ if (text.length > this.maxChunkSize) {
181
+ const subChunks = this.splitByLines(text, this.maxChunkSize);
182
+ let currentLine = decl.startRow + 1;
183
+ for (const sub of subChunks) {
184
+ const subLines = sub.split("\n").length;
185
+ chunks.push({
186
+ text: sub,
187
+ sourcePath: metadata.sourcePath,
188
+ contentType: metadata.contentType,
189
+ chunkIndex: chunks.length,
190
+ totalChunks: 0,
191
+ startLine: currentLine,
192
+ endLine: currentLine + subLines - 1
193
+ });
194
+ currentLine += subLines;
195
+ }
196
+ } else {
197
+ chunks.push({
198
+ text,
199
+ sourcePath: metadata.sourcePath,
200
+ contentType: metadata.contentType,
201
+ chunkIndex: chunks.length,
202
+ totalChunks: 0,
203
+ startLine: decl.startRow + 1,
204
+ endLine: decl.endRow + 1
205
+ });
206
+ }
207
+ }
208
+ return chunks.map((c, i) => ({ ...c, chunkIndex: i, totalChunks: chunks.length }));
209
+ }
210
+ splitByLines(text, maxSize) {
211
+ const lines = text.split("\n");
212
+ const result = [];
213
+ let current = [];
214
+ let currentSize = 0;
215
+ for (const line of lines) {
216
+ if (currentSize + line.length + 1 > maxSize && current.length > 0) {
217
+ result.push(current.join("\n"));
218
+ current = [line];
219
+ currentSize = line.length;
220
+ } else {
221
+ current.push(line);
222
+ currentSize += line.length + 1;
223
+ }
224
+ }
225
+ if (current.length > 0) result.push(current.join("\n"));
226
+ return result;
227
+ }
228
+ }
229
+ export {
230
+ TreeSitterChunker,
231
+ TreeSitterRuntime,
232
+ initializeTreeSitter
233
+ };
234
+ //# sourceMappingURL=treesitter-chunker.js.map
@@ -0,0 +1,3 @@
1
+ import type { Command } from '../types.js';
2
+ export declare const analyzeCommands: Command[];
3
+ //# sourceMappingURL=analyze.d.ts.map
@@ -0,0 +1,112 @@
1
+ import { resolve } from "node:path";
2
+ const analyzeCommands = [
3
+ {
4
+ name: "analyze",
5
+ description: "Run analyzer output for a path",
6
+ usage: "kb analyze <type> <path>",
7
+ run: async (args) => {
8
+ const type = args.shift()?.trim() ?? "";
9
+ const targetPath = args.shift()?.trim() ?? "";
10
+ if (!type || !targetPath) {
11
+ console.error("Usage: kb analyze <type> <path>");
12
+ console.error(
13
+ "Types: structure, deps, symbols, patterns, entry-points, blast-radius, diagram"
14
+ );
15
+ process.exit(1);
16
+ }
17
+ const {
18
+ BlastRadiusAnalyzer,
19
+ DependencyAnalyzer,
20
+ DiagramGenerator,
21
+ EntryPointAnalyzer,
22
+ PatternAnalyzer,
23
+ StructureAnalyzer,
24
+ SymbolAnalyzer
25
+ } = await import("@kb/analyzers");
26
+ const absolutePath = resolve(targetPath);
27
+ let result;
28
+ switch (type) {
29
+ case "structure":
30
+ result = await new StructureAnalyzer().analyze(absolutePath, { format: "markdown" });
31
+ break;
32
+ case "deps":
33
+ case "dependencies":
34
+ result = await new DependencyAnalyzer().analyze(absolutePath, { format: "markdown" });
35
+ break;
36
+ case "symbols":
37
+ result = await new SymbolAnalyzer().analyze(absolutePath, { format: "markdown" });
38
+ break;
39
+ case "patterns":
40
+ result = await new PatternAnalyzer().analyze(absolutePath, { format: "markdown" });
41
+ break;
42
+ case "entry-points":
43
+ result = await new EntryPointAnalyzer().analyze(absolutePath, { format: "markdown" });
44
+ break;
45
+ case "blast-radius":
46
+ result = await new BlastRadiusAnalyzer().analyze(process.cwd(), {
47
+ files: [targetPath],
48
+ format: "markdown"
49
+ });
50
+ break;
51
+ case "diagram":
52
+ result = await new DiagramGenerator().analyze(absolutePath, {
53
+ diagramType: "architecture"
54
+ });
55
+ break;
56
+ default:
57
+ console.error(`Unknown analyze type: ${type}`);
58
+ console.error(
59
+ "Types: structure, deps, symbols, patterns, entry-points, blast-radius, diagram"
60
+ );
61
+ process.exit(1);
62
+ }
63
+ console.log(result.output);
64
+ }
65
+ },
66
+ {
67
+ name: "onboard",
68
+ description: "Run all analyses for first-time codebase onboarding",
69
+ usage: "kb onboard <path> [--generate] [--out-dir <dir>]",
70
+ run: async (args) => {
71
+ const { onboard } = await import("@kb/tools");
72
+ let targetPath = "";
73
+ let mode = "memory";
74
+ let outDir;
75
+ for (let i = 0; i < args.length; i++) {
76
+ const arg = args[i].trim();
77
+ if (arg === "--generate") {
78
+ mode = "generate";
79
+ } else if (arg === "--out-dir" && i + 1 < args.length) {
80
+ outDir = args[++i].trim();
81
+ } else if (!arg.startsWith("--")) {
82
+ targetPath = arg;
83
+ }
84
+ }
85
+ if (!targetPath) {
86
+ targetPath = process.cwd();
87
+ }
88
+ const absolutePath = resolve(targetPath);
89
+ console.log(`Onboarding: ${absolutePath} (mode: ${mode})`);
90
+ console.log("Running analyses...\n");
91
+ const result = await onboard({
92
+ path: absolutePath,
93
+ mode,
94
+ outDir
95
+ });
96
+ for (const step of result.steps) {
97
+ const icon = step.status === "success" ? "\u2713" : "\u2717";
98
+ const info = step.status === "success" ? `${step.durationMs}ms, ${step.output.length} chars` : step.error;
99
+ console.log(` ${icon} ${step.name} \u2014 ${info}`);
100
+ }
101
+ console.log(`
102
+ Total: ${result.totalDurationMs}ms`);
103
+ if (result.outDir) {
104
+ console.log(`Output written to: ${result.outDir}`);
105
+ }
106
+ }
107
+ }
108
+ ];
109
+ export {
110
+ analyzeCommands
111
+ };
112
+ //# sourceMappingURL=analyze.js.map
@@ -0,0 +1,3 @@
1
+ import type { Command } from '../types.js';
2
+ export declare const contextCommands: Command[];
3
+ //# sourceMappingURL=context-cmds.d.ts.map
@@ -0,0 +1,155 @@
1
+ import { resolve } from "node:path";
2
+ import {
3
+ checkpointLatest,
4
+ checkpointList,
5
+ checkpointLoad,
6
+ checkpointSave,
7
+ diffParse,
8
+ fileSummary,
9
+ gitContext,
10
+ parseOutput
11
+ } from "@kb/tools";
12
+ import {
13
+ extractBoolFlag,
14
+ extractNumFlag,
15
+ extractStrFlag,
16
+ parseRecordString,
17
+ printCheckpoint,
18
+ printDiffFiles,
19
+ printFileSummary,
20
+ printGitContext,
21
+ printParsedOutput,
22
+ readStdin
23
+ } from "../helpers.js";
24
+ const contextCommands = [
25
+ {
26
+ name: "parse-output",
27
+ description: "Parse build or tool output from stdin",
28
+ usage: "kb parse-output [--tool tsc|vitest|biome|git-status]",
29
+ run: async (args) => {
30
+ const tool = extractStrFlag(args, "--tool", "").trim() || void 0;
31
+ const input = await readStdin();
32
+ if (!input.trim()) {
33
+ console.error("Usage: kb parse-output [--tool tsc|vitest|biome|git-status]");
34
+ process.exit(1);
35
+ }
36
+ printParsedOutput(parseOutput(input, tool));
37
+ }
38
+ },
39
+ {
40
+ name: "git",
41
+ description: "Show git branch, status, recent commits, and optional diff stats",
42
+ usage: "kb git [--cwd path] [--commit-count N] [--diff]",
43
+ run: async (args) => {
44
+ const cwd = extractStrFlag(args, "--cwd", "").trim() || void 0;
45
+ const commitCount = extractNumFlag(args, "--commit-count", 5);
46
+ const includeDiff = extractBoolFlag(args, "--diff");
47
+ const result = await gitContext({ cwd, commitCount, includeDiff });
48
+ printGitContext(result);
49
+ }
50
+ },
51
+ {
52
+ name: "diff",
53
+ description: "Parse unified diff text from stdin into structured file changes",
54
+ usage: "git diff | kb diff",
55
+ run: async () => {
56
+ const input = await readStdin();
57
+ if (!input.trim()) {
58
+ console.error("Usage: git diff | kb diff");
59
+ process.exit(1);
60
+ }
61
+ printDiffFiles(diffParse({ diff: input }));
62
+ }
63
+ },
64
+ {
65
+ name: "summarize",
66
+ description: "Show a structural summary of a file",
67
+ usage: "kb summarize <path>",
68
+ run: async (args) => {
69
+ const targetPath = args.shift()?.trim();
70
+ if (!targetPath) {
71
+ console.error("Usage: kb summarize <path>");
72
+ process.exit(1);
73
+ }
74
+ const summary = await fileSummary({ path: resolve(targetPath) });
75
+ printFileSummary(summary);
76
+ }
77
+ },
78
+ {
79
+ name: "checkpoint",
80
+ description: "Save and restore lightweight session checkpoints",
81
+ usage: "kb checkpoint <save|load|list|latest> [label-or-id] [--data json] [--notes text]",
82
+ run: async (args) => {
83
+ const action = args.shift()?.trim();
84
+ if (!action) {
85
+ console.error(
86
+ "Usage: kb checkpoint <save|load|list|latest> [label-or-id] [--data json] [--notes text]"
87
+ );
88
+ process.exit(1);
89
+ }
90
+ switch (action) {
91
+ case "save": {
92
+ const label = args.shift()?.trim();
93
+ const dataFlag = extractStrFlag(args, "--data", "");
94
+ const notes = extractStrFlag(args, "--notes", "").trim() || void 0;
95
+ const stdinData = dataFlag.trim() ? "" : await readStdin();
96
+ if (!label) {
97
+ console.error("Usage: kb checkpoint save <label> [--data json] [--notes text]");
98
+ process.exit(1);
99
+ }
100
+ const checkpoint = checkpointSave(label, parseRecordString(dataFlag || stdinData), {
101
+ notes
102
+ });
103
+ printCheckpoint(checkpoint);
104
+ return;
105
+ }
106
+ case "load": {
107
+ const id = args.shift()?.trim();
108
+ if (!id) {
109
+ console.error("Usage: kb checkpoint load <id>");
110
+ process.exit(1);
111
+ }
112
+ const checkpoint = checkpointLoad(id);
113
+ if (!checkpoint) {
114
+ console.log(`No checkpoint found: ${id}`);
115
+ return;
116
+ }
117
+ printCheckpoint(checkpoint);
118
+ return;
119
+ }
120
+ case "list": {
121
+ const checkpoints = checkpointList();
122
+ if (checkpoints.length === 0) {
123
+ console.log("No checkpoints saved.");
124
+ return;
125
+ }
126
+ console.log(`Checkpoints (${checkpoints.length})`);
127
+ console.log("\u2500".repeat(60));
128
+ for (const checkpoint of checkpoints) {
129
+ console.log(`${checkpoint.id}`);
130
+ console.log(` Label: ${checkpoint.label}`);
131
+ console.log(` Created: ${checkpoint.createdAt}`);
132
+ }
133
+ return;
134
+ }
135
+ case "latest": {
136
+ const checkpoint = checkpointLatest();
137
+ if (!checkpoint) {
138
+ console.log("No checkpoints saved.");
139
+ return;
140
+ }
141
+ printCheckpoint(checkpoint);
142
+ return;
143
+ }
144
+ default:
145
+ console.error(`Unknown checkpoint action: ${action}`);
146
+ console.error("Actions: save, load, list, latest");
147
+ process.exit(1);
148
+ }
149
+ }
150
+ }
151
+ ];
152
+ export {
153
+ contextCommands
154
+ };
155
+ //# sourceMappingURL=context-cmds.js.map
@@ -0,0 +1,3 @@
1
+ import type { Command } from '../types.js';
2
+ export declare const environmentCommands: Command[];
3
+ //# sourceMappingURL=environment.d.ts.map