@duytransipher/gitnexus 1.4.6-sipher.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 (224) hide show
  1. package/LICENSE +73 -0
  2. package/README.md +261 -0
  3. package/dist/cli/ai-context.d.ts +23 -0
  4. package/dist/cli/ai-context.js +265 -0
  5. package/dist/cli/analyze.d.ts +12 -0
  6. package/dist/cli/analyze.js +345 -0
  7. package/dist/cli/augment.d.ts +13 -0
  8. package/dist/cli/augment.js +33 -0
  9. package/dist/cli/clean.d.ts +10 -0
  10. package/dist/cli/clean.js +60 -0
  11. package/dist/cli/eval-server.d.ts +37 -0
  12. package/dist/cli/eval-server.js +389 -0
  13. package/dist/cli/index.d.ts +2 -0
  14. package/dist/cli/index.js +137 -0
  15. package/dist/cli/lazy-action.d.ts +6 -0
  16. package/dist/cli/lazy-action.js +18 -0
  17. package/dist/cli/list.d.ts +6 -0
  18. package/dist/cli/list.js +30 -0
  19. package/dist/cli/mcp.d.ts +8 -0
  20. package/dist/cli/mcp.js +36 -0
  21. package/dist/cli/serve.d.ts +4 -0
  22. package/dist/cli/serve.js +6 -0
  23. package/dist/cli/setup.d.ts +8 -0
  24. package/dist/cli/setup.js +367 -0
  25. package/dist/cli/sipher-patched.d.ts +2 -0
  26. package/dist/cli/sipher-patched.js +77 -0
  27. package/dist/cli/skill-gen.d.ts +26 -0
  28. package/dist/cli/skill-gen.js +549 -0
  29. package/dist/cli/status.d.ts +6 -0
  30. package/dist/cli/status.js +36 -0
  31. package/dist/cli/tool.d.ts +60 -0
  32. package/dist/cli/tool.js +180 -0
  33. package/dist/cli/wiki.d.ts +15 -0
  34. package/dist/cli/wiki.js +365 -0
  35. package/dist/config/ignore-service.d.ts +26 -0
  36. package/dist/config/ignore-service.js +284 -0
  37. package/dist/config/supported-languages.d.ts +15 -0
  38. package/dist/config/supported-languages.js +16 -0
  39. package/dist/core/augmentation/engine.d.ts +26 -0
  40. package/dist/core/augmentation/engine.js +240 -0
  41. package/dist/core/embeddings/embedder.d.ts +60 -0
  42. package/dist/core/embeddings/embedder.js +251 -0
  43. package/dist/core/embeddings/embedding-pipeline.d.ts +51 -0
  44. package/dist/core/embeddings/embedding-pipeline.js +356 -0
  45. package/dist/core/embeddings/index.d.ts +9 -0
  46. package/dist/core/embeddings/index.js +9 -0
  47. package/dist/core/embeddings/text-generator.d.ts +24 -0
  48. package/dist/core/embeddings/text-generator.js +182 -0
  49. package/dist/core/embeddings/types.d.ts +87 -0
  50. package/dist/core/embeddings/types.js +32 -0
  51. package/dist/core/graph/graph.d.ts +2 -0
  52. package/dist/core/graph/graph.js +66 -0
  53. package/dist/core/graph/types.d.ts +66 -0
  54. package/dist/core/graph/types.js +1 -0
  55. package/dist/core/ingestion/ast-cache.d.ts +11 -0
  56. package/dist/core/ingestion/ast-cache.js +35 -0
  57. package/dist/core/ingestion/call-processor.d.ts +23 -0
  58. package/dist/core/ingestion/call-processor.js +793 -0
  59. package/dist/core/ingestion/call-routing.d.ts +68 -0
  60. package/dist/core/ingestion/call-routing.js +129 -0
  61. package/dist/core/ingestion/cluster-enricher.d.ts +38 -0
  62. package/dist/core/ingestion/cluster-enricher.js +170 -0
  63. package/dist/core/ingestion/community-processor.d.ts +39 -0
  64. package/dist/core/ingestion/community-processor.js +312 -0
  65. package/dist/core/ingestion/constants.d.ts +16 -0
  66. package/dist/core/ingestion/constants.js +16 -0
  67. package/dist/core/ingestion/entry-point-scoring.d.ts +40 -0
  68. package/dist/core/ingestion/entry-point-scoring.js +353 -0
  69. package/dist/core/ingestion/export-detection.d.ts +18 -0
  70. package/dist/core/ingestion/export-detection.js +231 -0
  71. package/dist/core/ingestion/filesystem-walker.d.ts +28 -0
  72. package/dist/core/ingestion/filesystem-walker.js +81 -0
  73. package/dist/core/ingestion/framework-detection.d.ts +54 -0
  74. package/dist/core/ingestion/framework-detection.js +411 -0
  75. package/dist/core/ingestion/heritage-processor.d.ts +28 -0
  76. package/dist/core/ingestion/heritage-processor.js +251 -0
  77. package/dist/core/ingestion/import-processor.d.ts +34 -0
  78. package/dist/core/ingestion/import-processor.js +398 -0
  79. package/dist/core/ingestion/language-config.d.ts +46 -0
  80. package/dist/core/ingestion/language-config.js +167 -0
  81. package/dist/core/ingestion/mro-processor.d.ts +45 -0
  82. package/dist/core/ingestion/mro-processor.js +369 -0
  83. package/dist/core/ingestion/named-binding-extraction.d.ts +61 -0
  84. package/dist/core/ingestion/named-binding-extraction.js +363 -0
  85. package/dist/core/ingestion/parsing-processor.d.ts +19 -0
  86. package/dist/core/ingestion/parsing-processor.js +315 -0
  87. package/dist/core/ingestion/pipeline.d.ts +6 -0
  88. package/dist/core/ingestion/pipeline.js +401 -0
  89. package/dist/core/ingestion/process-processor.d.ts +51 -0
  90. package/dist/core/ingestion/process-processor.js +315 -0
  91. package/dist/core/ingestion/resolution-context.d.ts +53 -0
  92. package/dist/core/ingestion/resolution-context.js +132 -0
  93. package/dist/core/ingestion/resolvers/csharp.d.ts +22 -0
  94. package/dist/core/ingestion/resolvers/csharp.js +109 -0
  95. package/dist/core/ingestion/resolvers/go.d.ts +19 -0
  96. package/dist/core/ingestion/resolvers/go.js +42 -0
  97. package/dist/core/ingestion/resolvers/index.d.ts +18 -0
  98. package/dist/core/ingestion/resolvers/index.js +13 -0
  99. package/dist/core/ingestion/resolvers/jvm.d.ts +23 -0
  100. package/dist/core/ingestion/resolvers/jvm.js +87 -0
  101. package/dist/core/ingestion/resolvers/php.d.ts +15 -0
  102. package/dist/core/ingestion/resolvers/php.js +35 -0
  103. package/dist/core/ingestion/resolvers/python.d.ts +19 -0
  104. package/dist/core/ingestion/resolvers/python.js +52 -0
  105. package/dist/core/ingestion/resolvers/ruby.d.ts +12 -0
  106. package/dist/core/ingestion/resolvers/ruby.js +15 -0
  107. package/dist/core/ingestion/resolvers/rust.d.ts +15 -0
  108. package/dist/core/ingestion/resolvers/rust.js +73 -0
  109. package/dist/core/ingestion/resolvers/standard.d.ts +28 -0
  110. package/dist/core/ingestion/resolvers/standard.js +123 -0
  111. package/dist/core/ingestion/resolvers/utils.d.ts +33 -0
  112. package/dist/core/ingestion/resolvers/utils.js +122 -0
  113. package/dist/core/ingestion/structure-processor.d.ts +2 -0
  114. package/dist/core/ingestion/structure-processor.js +36 -0
  115. package/dist/core/ingestion/symbol-table.d.ts +63 -0
  116. package/dist/core/ingestion/symbol-table.js +85 -0
  117. package/dist/core/ingestion/tree-sitter-queries.d.ts +15 -0
  118. package/dist/core/ingestion/tree-sitter-queries.js +888 -0
  119. package/dist/core/ingestion/type-env.d.ts +49 -0
  120. package/dist/core/ingestion/type-env.js +613 -0
  121. package/dist/core/ingestion/type-extractors/c-cpp.d.ts +2 -0
  122. package/dist/core/ingestion/type-extractors/c-cpp.js +385 -0
  123. package/dist/core/ingestion/type-extractors/csharp.d.ts +2 -0
  124. package/dist/core/ingestion/type-extractors/csharp.js +383 -0
  125. package/dist/core/ingestion/type-extractors/go.d.ts +2 -0
  126. package/dist/core/ingestion/type-extractors/go.js +467 -0
  127. package/dist/core/ingestion/type-extractors/index.d.ts +22 -0
  128. package/dist/core/ingestion/type-extractors/index.js +31 -0
  129. package/dist/core/ingestion/type-extractors/jvm.d.ts +3 -0
  130. package/dist/core/ingestion/type-extractors/jvm.js +681 -0
  131. package/dist/core/ingestion/type-extractors/php.d.ts +2 -0
  132. package/dist/core/ingestion/type-extractors/php.js +549 -0
  133. package/dist/core/ingestion/type-extractors/python.d.ts +2 -0
  134. package/dist/core/ingestion/type-extractors/python.js +455 -0
  135. package/dist/core/ingestion/type-extractors/ruby.d.ts +2 -0
  136. package/dist/core/ingestion/type-extractors/ruby.js +389 -0
  137. package/dist/core/ingestion/type-extractors/rust.d.ts +2 -0
  138. package/dist/core/ingestion/type-extractors/rust.js +456 -0
  139. package/dist/core/ingestion/type-extractors/shared.d.ts +145 -0
  140. package/dist/core/ingestion/type-extractors/shared.js +810 -0
  141. package/dist/core/ingestion/type-extractors/swift.d.ts +2 -0
  142. package/dist/core/ingestion/type-extractors/swift.js +137 -0
  143. package/dist/core/ingestion/type-extractors/types.d.ts +127 -0
  144. package/dist/core/ingestion/type-extractors/types.js +1 -0
  145. package/dist/core/ingestion/type-extractors/typescript.d.ts +2 -0
  146. package/dist/core/ingestion/type-extractors/typescript.js +494 -0
  147. package/dist/core/ingestion/utils.d.ts +138 -0
  148. package/dist/core/ingestion/utils.js +1290 -0
  149. package/dist/core/ingestion/workers/parse-worker.d.ts +122 -0
  150. package/dist/core/ingestion/workers/parse-worker.js +1126 -0
  151. package/dist/core/ingestion/workers/worker-pool.d.ts +16 -0
  152. package/dist/core/ingestion/workers/worker-pool.js +128 -0
  153. package/dist/core/lbug/csv-generator.d.ts +33 -0
  154. package/dist/core/lbug/csv-generator.js +366 -0
  155. package/dist/core/lbug/lbug-adapter.d.ts +103 -0
  156. package/dist/core/lbug/lbug-adapter.js +769 -0
  157. package/dist/core/lbug/schema.d.ts +53 -0
  158. package/dist/core/lbug/schema.js +430 -0
  159. package/dist/core/search/bm25-index.d.ts +23 -0
  160. package/dist/core/search/bm25-index.js +96 -0
  161. package/dist/core/search/hybrid-search.d.ts +49 -0
  162. package/dist/core/search/hybrid-search.js +118 -0
  163. package/dist/core/tree-sitter/parser-loader.d.ts +5 -0
  164. package/dist/core/tree-sitter/parser-loader.js +63 -0
  165. package/dist/core/wiki/generator.d.ts +120 -0
  166. package/dist/core/wiki/generator.js +939 -0
  167. package/dist/core/wiki/graph-queries.d.ts +80 -0
  168. package/dist/core/wiki/graph-queries.js +238 -0
  169. package/dist/core/wiki/html-viewer.d.ts +10 -0
  170. package/dist/core/wiki/html-viewer.js +297 -0
  171. package/dist/core/wiki/llm-client.d.ts +43 -0
  172. package/dist/core/wiki/llm-client.js +186 -0
  173. package/dist/core/wiki/prompts.d.ts +53 -0
  174. package/dist/core/wiki/prompts.js +174 -0
  175. package/dist/lib/utils.d.ts +1 -0
  176. package/dist/lib/utils.js +3 -0
  177. package/dist/mcp/compatible-stdio-transport.d.ts +25 -0
  178. package/dist/mcp/compatible-stdio-transport.js +200 -0
  179. package/dist/mcp/core/embedder.d.ts +27 -0
  180. package/dist/mcp/core/embedder.js +108 -0
  181. package/dist/mcp/core/lbug-adapter.d.ts +57 -0
  182. package/dist/mcp/core/lbug-adapter.js +455 -0
  183. package/dist/mcp/local/local-backend.d.ts +181 -0
  184. package/dist/mcp/local/local-backend.js +1722 -0
  185. package/dist/mcp/resources.d.ts +31 -0
  186. package/dist/mcp/resources.js +411 -0
  187. package/dist/mcp/server.d.ts +23 -0
  188. package/dist/mcp/server.js +296 -0
  189. package/dist/mcp/staleness.d.ts +15 -0
  190. package/dist/mcp/staleness.js +29 -0
  191. package/dist/mcp/tools.d.ts +24 -0
  192. package/dist/mcp/tools.js +292 -0
  193. package/dist/server/api.d.ts +10 -0
  194. package/dist/server/api.js +344 -0
  195. package/dist/server/mcp-http.d.ts +13 -0
  196. package/dist/server/mcp-http.js +100 -0
  197. package/dist/storage/git.d.ts +6 -0
  198. package/dist/storage/git.js +35 -0
  199. package/dist/storage/repo-manager.d.ts +138 -0
  200. package/dist/storage/repo-manager.js +299 -0
  201. package/dist/types/pipeline.d.ts +32 -0
  202. package/dist/types/pipeline.js +18 -0
  203. package/dist/unreal/bridge.d.ts +4 -0
  204. package/dist/unreal/bridge.js +113 -0
  205. package/dist/unreal/config.d.ts +6 -0
  206. package/dist/unreal/config.js +55 -0
  207. package/dist/unreal/types.d.ts +105 -0
  208. package/dist/unreal/types.js +1 -0
  209. package/hooks/claude/gitnexus-hook.cjs +238 -0
  210. package/hooks/claude/pre-tool-use.sh +79 -0
  211. package/hooks/claude/session-start.sh +42 -0
  212. package/package.json +100 -0
  213. package/scripts/ensure-cli-executable.cjs +21 -0
  214. package/scripts/patch-tree-sitter-swift.cjs +74 -0
  215. package/scripts/setup-unreal-gitnexus.ps1 +191 -0
  216. package/skills/gitnexus-cli.md +82 -0
  217. package/skills/gitnexus-debugging.md +89 -0
  218. package/skills/gitnexus-exploring.md +78 -0
  219. package/skills/gitnexus-guide.md +64 -0
  220. package/skills/gitnexus-impact-analysis.md +97 -0
  221. package/skills/gitnexus-pr-review.md +163 -0
  222. package/skills/gitnexus-refactoring.md +121 -0
  223. package/vendor/leiden/index.cjs +355 -0
  224. package/vendor/leiden/utils.cjs +392 -0
@@ -0,0 +1,769 @@
1
+ import fs from 'fs/promises';
2
+ import { createReadStream } from 'fs';
3
+ import { createInterface } from 'readline';
4
+ import path from 'path';
5
+ import lbug from '@ladybugdb/core';
6
+ import { NODE_TABLES, REL_TABLE_NAME, SCHEMA_QUERIES, EMBEDDING_TABLE_NAME, } from './schema.js';
7
+ import { streamAllCSVsToDisk } from './csv-generator.js';
8
+ let db = null;
9
+ let conn = null;
10
+ let currentDbPath = null;
11
+ let ftsLoaded = false;
12
+ /** Expose the current Database for pool adapter reuse in tests. */
13
+ export const getDatabase = () => db;
14
+ // Global session lock for operations that touch module-level lbug globals.
15
+ // This guarantees no DB switch can happen while an operation is running.
16
+ let sessionLock = Promise.resolve();
17
+ const runWithSessionLock = async (operation) => {
18
+ const previous = sessionLock;
19
+ let release = null;
20
+ sessionLock = new Promise(resolve => {
21
+ release = resolve;
22
+ });
23
+ await previous;
24
+ try {
25
+ return await operation();
26
+ }
27
+ finally {
28
+ release?.();
29
+ }
30
+ };
31
+ const normalizeCopyPath = (filePath) => filePath.replace(/\\/g, '/');
32
+ export const initLbug = async (dbPath) => {
33
+ return runWithSessionLock(() => ensureLbugInitialized(dbPath));
34
+ };
35
+ /**
36
+ * Execute multiple queries against one repo DB atomically.
37
+ * While the callback runs, no other request can switch the active DB.
38
+ */
39
+ export const withLbugDb = async (dbPath, operation) => {
40
+ return runWithSessionLock(async () => {
41
+ await ensureLbugInitialized(dbPath);
42
+ return operation();
43
+ });
44
+ };
45
+ const ensureLbugInitialized = async (dbPath) => {
46
+ if (conn && currentDbPath === dbPath) {
47
+ return { db, conn };
48
+ }
49
+ await doInitLbug(dbPath);
50
+ return { db, conn };
51
+ };
52
+ const doInitLbug = async (dbPath) => {
53
+ // Different database requested — close the old one first
54
+ if (conn || db) {
55
+ try {
56
+ if (conn)
57
+ await conn.close();
58
+ }
59
+ catch { }
60
+ try {
61
+ if (db)
62
+ await db.close();
63
+ }
64
+ catch { }
65
+ conn = null;
66
+ db = null;
67
+ currentDbPath = null;
68
+ ftsLoaded = false;
69
+ }
70
+ // LadybugDB stores the database as a single file (not a directory).
71
+ // If the path already exists, it must be a valid LadybugDB database file.
72
+ // Remove stale empty directories or files from older versions.
73
+ try {
74
+ const stat = await fs.lstat(dbPath);
75
+ if (stat.isSymbolicLink()) {
76
+ // Never follow symlinks — just remove the link itself
77
+ await fs.unlink(dbPath);
78
+ }
79
+ else if (stat.isDirectory()) {
80
+ // Verify path is within expected storage directory before deleting
81
+ const realPath = await fs.realpath(dbPath);
82
+ const parentDir = path.dirname(dbPath);
83
+ const realParent = await fs.realpath(parentDir);
84
+ if (!realPath.startsWith(realParent + path.sep) && realPath !== realParent) {
85
+ throw new Error(`Refusing to delete ${dbPath}: resolved path ${realPath} is outside storage directory`);
86
+ }
87
+ // Old-style directory database or empty leftover - remove it
88
+ await fs.rm(dbPath, { recursive: true, force: true });
89
+ }
90
+ // If it's a file, assume it's an existing LadybugDB database - LadybugDB will open it
91
+ }
92
+ catch {
93
+ // Path doesn't exist, which is what LadybugDB wants for a new database
94
+ }
95
+ // Ensure parent directory exists
96
+ const parentDir = path.dirname(dbPath);
97
+ await fs.mkdir(parentDir, { recursive: true });
98
+ db = new lbug.Database(dbPath);
99
+ conn = new lbug.Connection(db);
100
+ for (const schemaQuery of SCHEMA_QUERIES) {
101
+ try {
102
+ await conn.query(schemaQuery);
103
+ }
104
+ catch (err) {
105
+ // Only ignore "already exists" errors - log everything else
106
+ const msg = err instanceof Error ? err.message : String(err);
107
+ if (!msg.includes('already exists')) {
108
+ console.warn(`⚠️ Schema creation warning: ${msg.slice(0, 120)}`);
109
+ }
110
+ }
111
+ }
112
+ currentDbPath = dbPath;
113
+ return { db, conn };
114
+ };
115
+ export const loadGraphToLbug = async (graph, repoPath, storagePath, onProgress) => {
116
+ if (!conn) {
117
+ throw new Error('LadybugDB not initialized. Call initLbug first.');
118
+ }
119
+ const log = onProgress || (() => { });
120
+ const csvDir = path.join(storagePath, 'csv');
121
+ log('Streaming CSVs to disk...');
122
+ const csvResult = await streamAllCSVsToDisk(graph, repoPath, csvDir);
123
+ const validTables = new Set(NODE_TABLES);
124
+ const getNodeLabel = (nodeId) => {
125
+ if (nodeId.startsWith('comm_'))
126
+ return 'Community';
127
+ if (nodeId.startsWith('proc_'))
128
+ return 'Process';
129
+ return nodeId.split(':')[0];
130
+ };
131
+ // Bulk COPY all node CSVs (sequential — LadybugDB allows only one write txn at a time)
132
+ const nodeFiles = [...csvResult.nodeFiles.entries()];
133
+ const totalSteps = nodeFiles.length + 1; // +1 for relationships
134
+ let stepsDone = 0;
135
+ for (const [table, { csvPath, rows }] of nodeFiles) {
136
+ stepsDone++;
137
+ log(`Loading nodes ${stepsDone}/${totalSteps}: ${table} (${rows.toLocaleString()} rows)`);
138
+ const normalizedPath = normalizeCopyPath(csvPath);
139
+ const copyQuery = getCopyQuery(table, normalizedPath);
140
+ try {
141
+ await conn.query(copyQuery);
142
+ }
143
+ catch (err) {
144
+ try {
145
+ const retryQuery = copyQuery.replace('auto_detect=false)', 'auto_detect=false, IGNORE_ERRORS=true)');
146
+ await conn.query(retryQuery);
147
+ }
148
+ catch (retryErr) {
149
+ const retryMsg = retryErr instanceof Error ? retryErr.message : String(retryErr);
150
+ throw new Error(`COPY failed for ${table}: ${retryMsg.slice(0, 200)}`);
151
+ }
152
+ }
153
+ }
154
+ // Bulk COPY relationships — split by FROM→TO label pair (LadybugDB requires it)
155
+ // Stream-read the relation CSV line by line to avoid exceeding V8 max string length
156
+ let relHeader = '';
157
+ const relsByPair = new Map();
158
+ let skippedRels = 0;
159
+ let totalValidRels = 0;
160
+ await new Promise((resolve, reject) => {
161
+ const rl = createInterface({ input: createReadStream(csvResult.relCsvPath, 'utf-8'), crlfDelay: Infinity });
162
+ let isFirst = true;
163
+ rl.on('line', (line) => {
164
+ if (isFirst) {
165
+ relHeader = line;
166
+ isFirst = false;
167
+ return;
168
+ }
169
+ if (!line.trim())
170
+ return;
171
+ const match = line.match(/"([^"]*)","([^"]*)"/);
172
+ if (!match) {
173
+ skippedRels++;
174
+ return;
175
+ }
176
+ const fromLabel = getNodeLabel(match[1]);
177
+ const toLabel = getNodeLabel(match[2]);
178
+ if (!validTables.has(fromLabel) || !validTables.has(toLabel)) {
179
+ skippedRels++;
180
+ return;
181
+ }
182
+ const pairKey = `${fromLabel}|${toLabel}`;
183
+ let list = relsByPair.get(pairKey);
184
+ if (!list) {
185
+ list = [];
186
+ relsByPair.set(pairKey, list);
187
+ }
188
+ list.push(line);
189
+ totalValidRels++;
190
+ });
191
+ rl.on('close', resolve);
192
+ rl.on('error', reject);
193
+ });
194
+ const insertedRels = totalValidRels;
195
+ const warnings = [];
196
+ if (insertedRels > 0) {
197
+ log(`Loading edges: ${insertedRels.toLocaleString()} across ${relsByPair.size} types`);
198
+ let pairIdx = 0;
199
+ let failedPairEdges = 0;
200
+ const failedPairLines = [];
201
+ for (const [pairKey, lines] of relsByPair) {
202
+ pairIdx++;
203
+ const [fromLabel, toLabel] = pairKey.split('|');
204
+ const pairCsvPath = path.join(csvDir, `rel_${fromLabel}_${toLabel}.csv`);
205
+ await fs.writeFile(pairCsvPath, relHeader + '\n' + lines.join('\n'), 'utf-8');
206
+ const normalizedPath = normalizeCopyPath(pairCsvPath);
207
+ const copyQuery = `COPY ${REL_TABLE_NAME} FROM "${normalizedPath}" (from="${fromLabel}", to="${toLabel}", HEADER=true, ESCAPE='"', DELIM=',', QUOTE='"', PARALLEL=false, auto_detect=false)`;
208
+ if (pairIdx % 5 === 0 || lines.length > 1000) {
209
+ log(`Loading edges: ${pairIdx}/${relsByPair.size} types (${fromLabel} -> ${toLabel})`);
210
+ }
211
+ try {
212
+ await conn.query(copyQuery);
213
+ }
214
+ catch (err) {
215
+ try {
216
+ const retryQuery = copyQuery.replace('auto_detect=false)', 'auto_detect=false, IGNORE_ERRORS=true)');
217
+ await conn.query(retryQuery);
218
+ }
219
+ catch (retryErr) {
220
+ const retryMsg = retryErr instanceof Error ? retryErr.message : String(retryErr);
221
+ warnings.push(`${fromLabel}->${toLabel} (${lines.length} edges): ${retryMsg.slice(0, 80)}`);
222
+ failedPairEdges += lines.length;
223
+ failedPairLines.push(...lines);
224
+ }
225
+ }
226
+ try {
227
+ await fs.unlink(pairCsvPath);
228
+ }
229
+ catch { }
230
+ }
231
+ if (failedPairLines.length > 0) {
232
+ log(`Inserting ${failedPairEdges} edges individually (missing schema pairs)`);
233
+ await fallbackRelationshipInserts([relHeader, ...failedPairLines], validTables, getNodeLabel);
234
+ }
235
+ }
236
+ // Cleanup all CSVs
237
+ try {
238
+ await fs.unlink(csvResult.relCsvPath);
239
+ }
240
+ catch { }
241
+ for (const [, { csvPath }] of csvResult.nodeFiles) {
242
+ try {
243
+ await fs.unlink(csvPath);
244
+ }
245
+ catch { }
246
+ }
247
+ try {
248
+ const remaining = await fs.readdir(csvDir);
249
+ for (const f of remaining) {
250
+ try {
251
+ await fs.unlink(path.join(csvDir, f));
252
+ }
253
+ catch { }
254
+ }
255
+ }
256
+ catch { }
257
+ try {
258
+ await fs.rmdir(csvDir);
259
+ }
260
+ catch { }
261
+ return { success: true, insertedRels, skippedRels, warnings };
262
+ };
263
+ // LadybugDB default ESCAPE is '\' (backslash), but our CSV uses RFC 4180 escaping ("" for literal quotes).
264
+ // Source code content is full of backslashes which confuse the auto-detection.
265
+ // We MUST explicitly set ESCAPE='"' to use RFC 4180 escaping, and disable auto_detect to prevent
266
+ // LadybugDB from overriding our settings based on sample rows.
267
+ const COPY_CSV_OPTS = `(HEADER=true, ESCAPE='"', DELIM=',', QUOTE='"', PARALLEL=false, auto_detect=false)`;
268
+ // Multi-language table names that were created with backticks in CODE_ELEMENT_BASE
269
+ // and must always be referenced with backticks in queries
270
+ const BACKTICK_TABLES = new Set([
271
+ 'Struct', 'Enum', 'Macro', 'Typedef', 'Union', 'Namespace', 'Trait', 'Impl',
272
+ 'TypeAlias', 'Const', 'Static', 'Property', 'Record', 'Delegate', 'Annotation',
273
+ 'Constructor', 'Template', 'Module',
274
+ ]);
275
+ const escapeTableName = (table) => {
276
+ return BACKTICK_TABLES.has(table) ? `\`${table}\`` : table;
277
+ };
278
+ /** Fallback: insert relationships one-by-one if COPY fails */
279
+ const fallbackRelationshipInserts = async (validRelLines, validTables, getNodeLabel) => {
280
+ if (!conn)
281
+ return;
282
+ const escapeLabel = (label) => {
283
+ return BACKTICK_TABLES.has(label) ? `\`${label}\`` : label;
284
+ };
285
+ for (let i = 1; i < validRelLines.length; i++) {
286
+ const line = validRelLines[i];
287
+ try {
288
+ const match = line.match(/"([^"]*)","([^"]*)","([^"]*)",([0-9.]+),"([^"]*)",([0-9-]+)/);
289
+ if (!match)
290
+ continue;
291
+ const [, fromId, toId, relType, confidenceStr, reason, stepStr] = match;
292
+ const fromLabel = getNodeLabel(fromId);
293
+ const toLabel = getNodeLabel(toId);
294
+ if (!validTables.has(fromLabel) || !validTables.has(toLabel))
295
+ continue;
296
+ const confidence = parseFloat(confidenceStr) || 1.0;
297
+ const step = parseInt(stepStr) || 0;
298
+ const esc = (s) => s.replace(/'/g, "''").replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/\r/g, '\\r');
299
+ await conn.query(`
300
+ MATCH (a:${escapeLabel(fromLabel)} {id: '${esc(fromId)}' }),
301
+ (b:${escapeLabel(toLabel)} {id: '${esc(toId)}' })
302
+ CREATE (a)-[:${REL_TABLE_NAME} {type: '${esc(relType)}', confidence: ${confidence}, reason: '${esc(reason)}', step: ${step}}]->(b)
303
+ `);
304
+ }
305
+ catch {
306
+ // skip
307
+ }
308
+ }
309
+ };
310
+ /** Tables with isExported column (TypeScript/JS-native types) */
311
+ const TABLES_WITH_EXPORTED = new Set(['Function', 'Class', 'Interface', 'Method', 'CodeElement']);
312
+ const getCopyQuery = (table, filePath) => {
313
+ const t = escapeTableName(table);
314
+ if (table === 'File') {
315
+ return `COPY ${t}(id, name, filePath, content) FROM "${filePath}" ${COPY_CSV_OPTS}`;
316
+ }
317
+ if (table === 'Folder') {
318
+ return `COPY ${t}(id, name, filePath) FROM "${filePath}" ${COPY_CSV_OPTS}`;
319
+ }
320
+ if (table === 'Community') {
321
+ return `COPY ${t}(id, label, heuristicLabel, keywords, description, enrichedBy, cohesion, symbolCount) FROM "${filePath}" ${COPY_CSV_OPTS}`;
322
+ }
323
+ if (table === 'Process') {
324
+ return `COPY ${t}(id, label, heuristicLabel, processType, stepCount, communities, entryPointId, terminalId) FROM "${filePath}" ${COPY_CSV_OPTS}`;
325
+ }
326
+ if (table === 'Method') {
327
+ return `COPY ${t}(id, name, filePath, startLine, endLine, isExported, content, description, parameterCount, returnType) FROM "${filePath}" ${COPY_CSV_OPTS}`;
328
+ }
329
+ // TypeScript/JS code element tables have isExported; multi-language tables do not
330
+ if (TABLES_WITH_EXPORTED.has(table)) {
331
+ return `COPY ${t}(id, name, filePath, startLine, endLine, isExported, content, description) FROM "${filePath}" ${COPY_CSV_OPTS}`;
332
+ }
333
+ // Multi-language tables (Struct, Impl, Trait, Macro, etc.)
334
+ return `COPY ${t}(id, name, filePath, startLine, endLine, content, description) FROM "${filePath}" ${COPY_CSV_OPTS}`;
335
+ };
336
+ /**
337
+ * Insert a single node to LadybugDB
338
+ * @param label - Node type (File, Function, Class, etc.)
339
+ * @param properties - Node properties
340
+ * @param dbPath - Path to LadybugDB database (optional if already initialized)
341
+ */
342
+ export const insertNodeToLbug = async (label, properties, dbPath) => {
343
+ // Use provided dbPath or fall back to module-level db
344
+ const targetDbPath = dbPath || (db ? undefined : null);
345
+ if (!targetDbPath && !db) {
346
+ throw new Error('LadybugDB not initialized. Provide dbPath or call initLbug first.');
347
+ }
348
+ try {
349
+ const escapeValue = (v) => {
350
+ if (v === null || v === undefined)
351
+ return 'NULL';
352
+ if (typeof v === 'number')
353
+ return String(v);
354
+ // Escape backslashes first (for Windows paths), then single quotes
355
+ return `'${String(v).replace(/\\/g, '\\\\').replace(/'/g, "''").replace(/\n/g, '\\n').replace(/\r/g, '\\r')}'`;
356
+ };
357
+ // Build INSERT query based on node type
358
+ const t = escapeTableName(label);
359
+ let query;
360
+ if (label === 'File') {
361
+ query = `CREATE (n:File {id: ${escapeValue(properties.id)}, name: ${escapeValue(properties.name)}, filePath: ${escapeValue(properties.filePath)}, content: ${escapeValue(properties.content || '')}})`;
362
+ }
363
+ else if (label === 'Folder') {
364
+ query = `CREATE (n:Folder {id: ${escapeValue(properties.id)}, name: ${escapeValue(properties.name)}, filePath: ${escapeValue(properties.filePath)}})`;
365
+ }
366
+ else if (TABLES_WITH_EXPORTED.has(label)) {
367
+ const descPart = properties.description ? `, description: ${escapeValue(properties.description)}` : '';
368
+ query = `CREATE (n:${t} {id: ${escapeValue(properties.id)}, name: ${escapeValue(properties.name)}, filePath: ${escapeValue(properties.filePath)}, startLine: ${properties.startLine || 0}, endLine: ${properties.endLine || 0}, isExported: ${!!properties.isExported}, content: ${escapeValue(properties.content || '')}${descPart}})`;
369
+ }
370
+ else {
371
+ // Multi-language tables (Struct, Impl, Trait, Macro, etc.) — no isExported
372
+ const descPart = properties.description ? `, description: ${escapeValue(properties.description)}` : '';
373
+ query = `CREATE (n:${t} {id: ${escapeValue(properties.id)}, name: ${escapeValue(properties.name)}, filePath: ${escapeValue(properties.filePath)}, startLine: ${properties.startLine || 0}, endLine: ${properties.endLine || 0}, content: ${escapeValue(properties.content || '')}${descPart}})`;
374
+ }
375
+ // Use per-query connection if dbPath provided (avoids lock conflicts)
376
+ if (targetDbPath) {
377
+ const tempDb = new lbug.Database(targetDbPath);
378
+ const tempConn = new lbug.Connection(tempDb);
379
+ try {
380
+ await tempConn.query(query);
381
+ return true;
382
+ }
383
+ finally {
384
+ try {
385
+ await tempConn.close();
386
+ }
387
+ catch { }
388
+ try {
389
+ await tempDb.close();
390
+ }
391
+ catch { }
392
+ }
393
+ }
394
+ else if (conn) {
395
+ // Use existing persistent connection (when called from analyze)
396
+ await conn.query(query);
397
+ return true;
398
+ }
399
+ return false;
400
+ }
401
+ catch (e) {
402
+ // Node may already exist or other error
403
+ console.error(`Failed to insert ${label} node:`, e.message);
404
+ return false;
405
+ }
406
+ };
407
+ /**
408
+ * Batch insert multiple nodes to LadybugDB using a single connection
409
+ * @param nodes - Array of {label, properties} to insert
410
+ * @param dbPath - Path to LadybugDB database
411
+ * @returns Object with success count and error count
412
+ */
413
+ export const batchInsertNodesToLbug = async (nodes, dbPath) => {
414
+ if (nodes.length === 0)
415
+ return { inserted: 0, failed: 0 };
416
+ const escapeValue = (v) => {
417
+ if (v === null || v === undefined)
418
+ return 'NULL';
419
+ if (typeof v === 'number')
420
+ return String(v);
421
+ // Escape backslashes first (for Windows paths), then single quotes, then newlines
422
+ return `'${String(v).replace(/\\/g, '\\\\').replace(/'/g, "''").replace(/\n/g, '\\n').replace(/\r/g, '\\r')}'`;
423
+ };
424
+ // Open a single connection for all inserts
425
+ const tempDb = new lbug.Database(dbPath);
426
+ const tempConn = new lbug.Connection(tempDb);
427
+ let inserted = 0;
428
+ let failed = 0;
429
+ try {
430
+ for (const { label, properties } of nodes) {
431
+ try {
432
+ let query;
433
+ // Use MERGE instead of CREATE for upsert behavior (handles duplicates gracefully)
434
+ const t = escapeTableName(label);
435
+ if (label === 'File') {
436
+ query = `MERGE (n:File {id: ${escapeValue(properties.id)}}) SET n.name = ${escapeValue(properties.name)}, n.filePath = ${escapeValue(properties.filePath)}, n.content = ${escapeValue(properties.content || '')}`;
437
+ }
438
+ else if (label === 'Folder') {
439
+ query = `MERGE (n:Folder {id: ${escapeValue(properties.id)}}) SET n.name = ${escapeValue(properties.name)}, n.filePath = ${escapeValue(properties.filePath)}`;
440
+ }
441
+ else if (TABLES_WITH_EXPORTED.has(label)) {
442
+ const descPart = properties.description ? `, n.description = ${escapeValue(properties.description)}` : '';
443
+ query = `MERGE (n:${t} {id: ${escapeValue(properties.id)}}) SET n.name = ${escapeValue(properties.name)}, n.filePath = ${escapeValue(properties.filePath)}, n.startLine = ${properties.startLine || 0}, n.endLine = ${properties.endLine || 0}, n.isExported = ${!!properties.isExported}, n.content = ${escapeValue(properties.content || '')}${descPart}`;
444
+ }
445
+ else {
446
+ const descPart = properties.description ? `, n.description = ${escapeValue(properties.description)}` : '';
447
+ query = `MERGE (n:${t} {id: ${escapeValue(properties.id)}}) SET n.name = ${escapeValue(properties.name)}, n.filePath = ${escapeValue(properties.filePath)}, n.startLine = ${properties.startLine || 0}, n.endLine = ${properties.endLine || 0}, n.content = ${escapeValue(properties.content || '')}${descPart}`;
448
+ }
449
+ await tempConn.query(query);
450
+ inserted++;
451
+ }
452
+ catch (e) {
453
+ // Don't console.error here - it corrupts MCP JSON-RPC on stderr
454
+ failed++;
455
+ }
456
+ }
457
+ }
458
+ finally {
459
+ try {
460
+ await tempConn.close();
461
+ }
462
+ catch { }
463
+ try {
464
+ await tempDb.close();
465
+ }
466
+ catch { }
467
+ }
468
+ return { inserted, failed };
469
+ };
470
+ export const executeQuery = async (cypher) => {
471
+ if (!conn) {
472
+ throw new Error('LadybugDB not initialized. Call initLbug first.');
473
+ }
474
+ const queryResult = await conn.query(cypher);
475
+ // LadybugDB uses getAll() instead of hasNext()/getNext()
476
+ // Query returns QueryResult for single queries, QueryResult[] for multi-statement
477
+ const result = Array.isArray(queryResult) ? queryResult[0] : queryResult;
478
+ const rows = await result.getAll();
479
+ return rows;
480
+ };
481
+ export const executeWithReusedStatement = async (cypher, paramsList) => {
482
+ if (!conn) {
483
+ throw new Error('LadybugDB not initialized. Call initLbug first.');
484
+ }
485
+ if (paramsList.length === 0)
486
+ return;
487
+ const SUB_BATCH_SIZE = 4;
488
+ for (let i = 0; i < paramsList.length; i += SUB_BATCH_SIZE) {
489
+ const subBatch = paramsList.slice(i, i + SUB_BATCH_SIZE);
490
+ const stmt = await conn.prepare(cypher);
491
+ if (!stmt.isSuccess()) {
492
+ const errMsg = await stmt.getErrorMessage();
493
+ throw new Error(`Prepare failed: ${errMsg}`);
494
+ }
495
+ try {
496
+ for (const params of subBatch) {
497
+ await conn.execute(stmt, params);
498
+ }
499
+ }
500
+ catch (e) {
501
+ // Log the error and continue with next batch
502
+ console.warn('Batch execution error:', e);
503
+ }
504
+ // Note: LadybugDB PreparedStatement doesn't require explicit close()
505
+ }
506
+ };
507
+ export const getLbugStats = async () => {
508
+ if (!conn)
509
+ return { nodes: 0, edges: 0 };
510
+ let totalNodes = 0;
511
+ for (const tableName of NODE_TABLES) {
512
+ try {
513
+ const queryResult = await conn.query(`MATCH (n:${escapeTableName(tableName)}) RETURN count(n) AS cnt`);
514
+ const nodeResult = Array.isArray(queryResult) ? queryResult[0] : queryResult;
515
+ const nodeRows = await nodeResult.getAll();
516
+ if (nodeRows.length > 0) {
517
+ totalNodes += Number(nodeRows[0]?.cnt ?? nodeRows[0]?.[0] ?? 0);
518
+ }
519
+ }
520
+ catch {
521
+ // ignore
522
+ }
523
+ }
524
+ let totalEdges = 0;
525
+ try {
526
+ const queryResult = await conn.query(`MATCH ()-[r:${REL_TABLE_NAME}]->() RETURN count(r) AS cnt`);
527
+ const edgeResult = Array.isArray(queryResult) ? queryResult[0] : queryResult;
528
+ const edgeRows = await edgeResult.getAll();
529
+ if (edgeRows.length > 0) {
530
+ totalEdges = Number(edgeRows[0]?.cnt ?? edgeRows[0]?.[0] ?? 0);
531
+ }
532
+ }
533
+ catch {
534
+ // ignore
535
+ }
536
+ return { nodes: totalNodes, edges: totalEdges };
537
+ };
538
+ /**
539
+ * Load cached embeddings from LadybugDB before a rebuild.
540
+ * Returns all embedding vectors so they can be re-inserted after the graph is reloaded,
541
+ * avoiding expensive re-embedding of unchanged nodes.
542
+ */
543
+ export const loadCachedEmbeddings = async () => {
544
+ if (!conn) {
545
+ return { embeddingNodeIds: new Set(), embeddings: [] };
546
+ }
547
+ const embeddingNodeIds = new Set();
548
+ const embeddings = [];
549
+ try {
550
+ const rows = await conn.query(`MATCH (e:${EMBEDDING_TABLE_NAME}) RETURN e.nodeId AS nodeId, e.embedding AS embedding`);
551
+ const result = Array.isArray(rows) ? rows[0] : rows;
552
+ for (const row of await result.getAll()) {
553
+ const nodeId = String(row.nodeId ?? row[0] ?? '');
554
+ if (!nodeId)
555
+ continue;
556
+ embeddingNodeIds.add(nodeId);
557
+ const embedding = row.embedding ?? row[1];
558
+ if (embedding) {
559
+ embeddings.push({
560
+ nodeId,
561
+ embedding: Array.isArray(embedding) ? embedding.map(Number) : Array.from(embedding).map(Number),
562
+ });
563
+ }
564
+ }
565
+ }
566
+ catch { /* embedding table may not exist */ }
567
+ return { embeddingNodeIds, embeddings };
568
+ };
569
+ export const closeLbug = async () => {
570
+ if (conn) {
571
+ try {
572
+ await conn.close();
573
+ }
574
+ catch { }
575
+ conn = null;
576
+ }
577
+ if (db) {
578
+ try {
579
+ await db.close();
580
+ }
581
+ catch { }
582
+ db = null;
583
+ }
584
+ currentDbPath = null;
585
+ ftsLoaded = false;
586
+ };
587
+ export const isLbugReady = () => conn !== null && db !== null;
588
+ /**
589
+ * Delete all nodes (and their relationships) for a specific file from LadybugDB
590
+ * @param filePath - The file path to delete nodes for
591
+ * @param dbPath - Optional path to LadybugDB for per-query connection
592
+ * @returns Object with counts of deleted nodes
593
+ */
594
+ export const deleteNodesForFile = async (filePath, dbPath) => {
595
+ const usePerQuery = !!dbPath;
596
+ // Set up connection (either use existing or create per-query)
597
+ let tempDb = null;
598
+ let tempConn = null;
599
+ let targetConn = conn;
600
+ if (usePerQuery) {
601
+ tempDb = new lbug.Database(dbPath);
602
+ tempConn = new lbug.Connection(tempDb);
603
+ targetConn = tempConn;
604
+ }
605
+ else if (!conn) {
606
+ throw new Error('LadybugDB not initialized. Provide dbPath or call initLbug first.');
607
+ }
608
+ try {
609
+ let deletedNodes = 0;
610
+ const escapedPath = filePath.replace(/'/g, "''");
611
+ // Delete nodes from each table that has filePath
612
+ // DETACH DELETE removes the node and all its relationships
613
+ for (const tableName of NODE_TABLES) {
614
+ // Skip tables that don't have filePath (Community, Process)
615
+ if (tableName === 'Community' || tableName === 'Process')
616
+ continue;
617
+ try {
618
+ // First count how many we'll delete
619
+ const tn = escapeTableName(tableName);
620
+ const countResult = await targetConn.query(`MATCH (n:${tn}) WHERE n.filePath = '${escapedPath}' RETURN count(n) AS cnt`);
621
+ const result = Array.isArray(countResult) ? countResult[0] : countResult;
622
+ const rows = await result.getAll();
623
+ const count = Number(rows[0]?.cnt ?? rows[0]?.[0] ?? 0);
624
+ if (count > 0) {
625
+ // Delete nodes (and implicitly their relationships via DETACH)
626
+ await targetConn.query(`MATCH (n:${tn}) WHERE n.filePath = '${escapedPath}' DETACH DELETE n`);
627
+ deletedNodes += count;
628
+ }
629
+ }
630
+ catch (e) {
631
+ // Some tables may not support this query, skip
632
+ }
633
+ }
634
+ // Also delete any embeddings for nodes in this file
635
+ try {
636
+ await targetConn.query(`MATCH (e:${EMBEDDING_TABLE_NAME}) WHERE e.nodeId STARTS WITH '${escapedPath}' DELETE e`);
637
+ }
638
+ catch {
639
+ // Embedding table may not exist or nodeId format may differ
640
+ }
641
+ return { deletedNodes };
642
+ }
643
+ finally {
644
+ // Close per-query connection if used
645
+ if (tempConn) {
646
+ try {
647
+ await tempConn.close();
648
+ }
649
+ catch { }
650
+ }
651
+ if (tempDb) {
652
+ try {
653
+ await tempDb.close();
654
+ }
655
+ catch { }
656
+ }
657
+ }
658
+ };
659
+ export const getEmbeddingTableName = () => EMBEDDING_TABLE_NAME;
660
+ // ============================================================================
661
+ // Full-Text Search (FTS) Functions
662
+ // ============================================================================
663
+ /**
664
+ * Load the FTS extension (required before using FTS functions).
665
+ * Safe to call multiple times — tracks loaded state via module-level ftsLoaded.
666
+ */
667
+ export const loadFTSExtension = async () => {
668
+ if (ftsLoaded)
669
+ return;
670
+ if (!conn) {
671
+ throw new Error('LadybugDB not initialized. Call initLbug first.');
672
+ }
673
+ try {
674
+ await conn.query('INSTALL fts');
675
+ await conn.query('LOAD EXTENSION fts');
676
+ ftsLoaded = true;
677
+ }
678
+ catch (err) {
679
+ const msg = err?.message || '';
680
+ if (msg.includes('already loaded') || msg.includes('already installed') || msg.includes('already exists')) {
681
+ ftsLoaded = true;
682
+ }
683
+ else {
684
+ console.error('GitNexus: FTS extension load failed:', msg);
685
+ }
686
+ }
687
+ };
688
+ /**
689
+ * Create a full-text search index on a table
690
+ * @param tableName - The node table name (e.g., 'File', 'CodeSymbol')
691
+ * @param indexName - Name for the FTS index
692
+ * @param properties - List of properties to index (e.g., ['name', 'code'])
693
+ * @param stemmer - Stemming algorithm (default: 'porter')
694
+ */
695
+ export const createFTSIndex = async (tableName, indexName, properties, stemmer = 'porter') => {
696
+ if (!conn) {
697
+ throw new Error('LadybugDB not initialized. Call initLbug first.');
698
+ }
699
+ await loadFTSExtension();
700
+ const propList = properties.map(p => `'${p}'`).join(', ');
701
+ const query = `CALL CREATE_FTS_INDEX('${tableName}', '${indexName}', [${propList}], stemmer := '${stemmer}')`;
702
+ try {
703
+ await conn.query(query);
704
+ }
705
+ catch (e) {
706
+ if (!e.message?.includes('already exists')) {
707
+ throw e;
708
+ }
709
+ }
710
+ };
711
+ /**
712
+ * Query a full-text search index
713
+ * @param tableName - The node table name
714
+ * @param indexName - FTS index name
715
+ * @param query - Search query string
716
+ * @param limit - Maximum results
717
+ * @param conjunctive - If true, all terms must match (AND); if false, any term matches (OR)
718
+ * @returns Array of { node properties, score }
719
+ */
720
+ export const queryFTS = async (tableName, indexName, query, limit = 20, conjunctive = false) => {
721
+ if (!conn) {
722
+ throw new Error('LadybugDB not initialized. Call initLbug first.');
723
+ }
724
+ // Escape backslashes and single quotes to prevent Cypher injection
725
+ const escapedQuery = query.replace(/\\/g, '\\\\').replace(/'/g, "''");
726
+ const cypher = `
727
+ CALL QUERY_FTS_INDEX('${tableName}', '${indexName}', '${escapedQuery}', conjunctive := ${conjunctive})
728
+ RETURN node, score
729
+ ORDER BY score DESC
730
+ LIMIT ${limit}
731
+ `;
732
+ try {
733
+ const queryResult = await conn.query(cypher);
734
+ const result = Array.isArray(queryResult) ? queryResult[0] : queryResult;
735
+ const rows = await result.getAll();
736
+ return rows.map((row) => {
737
+ const node = row.node || row[0] || {};
738
+ const score = row.score ?? row[1] ?? 0;
739
+ return {
740
+ nodeId: node.nodeId || node.id || '',
741
+ name: node.name || '',
742
+ filePath: node.filePath || '',
743
+ score: typeof score === 'number' ? score : parseFloat(score) || 0,
744
+ ...node,
745
+ };
746
+ });
747
+ }
748
+ catch (e) {
749
+ // Return empty if index doesn't exist yet
750
+ if (e.message?.includes('does not exist')) {
751
+ return [];
752
+ }
753
+ throw e;
754
+ }
755
+ };
756
+ /**
757
+ * Drop an FTS index
758
+ */
759
+ export const dropFTSIndex = async (tableName, indexName) => {
760
+ if (!conn) {
761
+ throw new Error('LadybugDB not initialized. Call initLbug first.');
762
+ }
763
+ try {
764
+ await conn.query(`CALL DROP_FTS_INDEX('${tableName}', '${indexName}')`);
765
+ }
766
+ catch {
767
+ // Index may not exist
768
+ }
769
+ };