rag-lite-ts 1.0.1 → 2.0.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 (204) hide show
  1. package/README.md +651 -109
  2. package/dist/cli/indexer.js +262 -46
  3. package/dist/cli/search.js +54 -32
  4. package/dist/cli.js +185 -28
  5. package/dist/config.d.ts +34 -73
  6. package/dist/config.js +50 -255
  7. package/dist/core/abstract-embedder.d.ts +125 -0
  8. package/dist/core/abstract-embedder.js +264 -0
  9. package/dist/core/actionable-error-messages.d.ts +60 -0
  10. package/dist/core/actionable-error-messages.js +397 -0
  11. package/dist/core/adapters.d.ts +93 -0
  12. package/dist/core/adapters.js +139 -0
  13. package/dist/core/batch-processing-optimizer.d.ts +155 -0
  14. package/dist/core/batch-processing-optimizer.js +541 -0
  15. package/dist/core/chunker.d.ts +119 -0
  16. package/dist/core/chunker.js +73 -0
  17. package/dist/core/cli-database-utils.d.ts +53 -0
  18. package/dist/core/cli-database-utils.js +239 -0
  19. package/dist/core/config.d.ts +102 -0
  20. package/dist/core/config.js +247 -0
  21. package/dist/core/content-errors.d.ts +111 -0
  22. package/dist/core/content-errors.js +362 -0
  23. package/dist/core/content-manager.d.ts +343 -0
  24. package/dist/core/content-manager.js +1504 -0
  25. package/dist/core/content-performance-optimizer.d.ts +150 -0
  26. package/dist/core/content-performance-optimizer.js +516 -0
  27. package/dist/core/content-resolver.d.ts +104 -0
  28. package/dist/core/content-resolver.js +285 -0
  29. package/dist/core/cross-modal-search.d.ts +164 -0
  30. package/dist/core/cross-modal-search.js +342 -0
  31. package/dist/core/database-connection-manager.d.ts +109 -0
  32. package/dist/core/database-connection-manager.js +304 -0
  33. package/dist/core/db.d.ts +245 -0
  34. package/dist/core/db.js +952 -0
  35. package/dist/core/embedder-factory.d.ts +176 -0
  36. package/dist/core/embedder-factory.js +338 -0
  37. package/dist/{error-handler.d.ts → core/error-handler.d.ts} +23 -2
  38. package/dist/{error-handler.js → core/error-handler.js} +51 -8
  39. package/dist/core/index.d.ts +59 -0
  40. package/dist/core/index.js +69 -0
  41. package/dist/core/ingestion.d.ts +213 -0
  42. package/dist/core/ingestion.js +812 -0
  43. package/dist/core/interfaces.d.ts +408 -0
  44. package/dist/core/interfaces.js +106 -0
  45. package/dist/core/lazy-dependency-loader.d.ts +152 -0
  46. package/dist/core/lazy-dependency-loader.js +453 -0
  47. package/dist/core/mode-detection-service.d.ts +150 -0
  48. package/dist/core/mode-detection-service.js +565 -0
  49. package/dist/core/mode-model-validator.d.ts +92 -0
  50. package/dist/core/mode-model-validator.js +203 -0
  51. package/dist/core/model-registry.d.ts +120 -0
  52. package/dist/core/model-registry.js +415 -0
  53. package/dist/core/model-validator.d.ts +217 -0
  54. package/dist/core/model-validator.js +782 -0
  55. package/dist/{path-manager.d.ts → core/path-manager.d.ts} +5 -0
  56. package/dist/{path-manager.js → core/path-manager.js} +5 -0
  57. package/dist/core/polymorphic-search-factory.d.ts +154 -0
  58. package/dist/core/polymorphic-search-factory.js +344 -0
  59. package/dist/core/raglite-paths.d.ts +121 -0
  60. package/dist/core/raglite-paths.js +145 -0
  61. package/dist/core/reranking-config.d.ts +42 -0
  62. package/dist/core/reranking-config.js +156 -0
  63. package/dist/core/reranking-factory.d.ts +92 -0
  64. package/dist/core/reranking-factory.js +591 -0
  65. package/dist/core/reranking-strategies.d.ts +325 -0
  66. package/dist/core/reranking-strategies.js +720 -0
  67. package/dist/core/resource-cleanup.d.ts +163 -0
  68. package/dist/core/resource-cleanup.js +371 -0
  69. package/dist/core/resource-manager.d.ts +212 -0
  70. package/dist/core/resource-manager.js +564 -0
  71. package/dist/core/search-pipeline.d.ts +111 -0
  72. package/dist/core/search-pipeline.js +287 -0
  73. package/dist/core/search.d.ts +131 -0
  74. package/dist/core/search.js +296 -0
  75. package/dist/core/streaming-operations.d.ts +145 -0
  76. package/dist/core/streaming-operations.js +409 -0
  77. package/dist/core/types.d.ts +66 -0
  78. package/dist/core/types.js +6 -0
  79. package/dist/core/universal-embedder.d.ts +177 -0
  80. package/dist/core/universal-embedder.js +139 -0
  81. package/dist/core/validation-messages.d.ts +99 -0
  82. package/dist/core/validation-messages.js +334 -0
  83. package/dist/{vector-index.d.ts → core/vector-index.d.ts} +4 -0
  84. package/dist/{vector-index.js → core/vector-index.js} +21 -3
  85. package/dist/dom-polyfills.d.ts +6 -0
  86. package/dist/dom-polyfills.js +40 -0
  87. package/dist/factories/index.d.ts +43 -0
  88. package/dist/factories/index.js +44 -0
  89. package/dist/factories/text-factory.d.ts +560 -0
  90. package/dist/factories/text-factory.js +968 -0
  91. package/dist/file-processor.d.ts +90 -4
  92. package/dist/file-processor.js +723 -20
  93. package/dist/index-manager.d.ts +3 -2
  94. package/dist/index-manager.js +13 -11
  95. package/dist/index.d.ts +72 -8
  96. package/dist/index.js +102 -16
  97. package/dist/indexer.js +1 -1
  98. package/dist/ingestion.d.ts +44 -154
  99. package/dist/ingestion.js +75 -671
  100. package/dist/mcp-server.d.ts +35 -3
  101. package/dist/mcp-server.js +1186 -79
  102. package/dist/multimodal/clip-embedder.d.ts +314 -0
  103. package/dist/multimodal/clip-embedder.js +945 -0
  104. package/dist/multimodal/index.d.ts +6 -0
  105. package/dist/multimodal/index.js +6 -0
  106. package/dist/preprocess.js +1 -1
  107. package/dist/run-error-recovery-tests.d.ts +7 -0
  108. package/dist/run-error-recovery-tests.js +101 -0
  109. package/dist/search-standalone.js +1 -1
  110. package/dist/search.d.ts +51 -69
  111. package/dist/search.js +117 -412
  112. package/dist/test-utils.d.ts +8 -26
  113. package/dist/text/chunker.d.ts +33 -0
  114. package/dist/{chunker.js → text/chunker.js} +98 -75
  115. package/dist/{embedder.d.ts → text/embedder.d.ts} +22 -1
  116. package/dist/{embedder.js → text/embedder.js} +84 -10
  117. package/dist/text/index.d.ts +8 -0
  118. package/dist/text/index.js +9 -0
  119. package/dist/text/preprocessors/index.d.ts +17 -0
  120. package/dist/text/preprocessors/index.js +38 -0
  121. package/dist/text/preprocessors/mdx.d.ts +25 -0
  122. package/dist/text/preprocessors/mdx.js +101 -0
  123. package/dist/text/preprocessors/mermaid.d.ts +68 -0
  124. package/dist/text/preprocessors/mermaid.js +330 -0
  125. package/dist/text/preprocessors/registry.d.ts +56 -0
  126. package/dist/text/preprocessors/registry.js +180 -0
  127. package/dist/text/reranker.d.ts +59 -0
  128. package/dist/{reranker.js → text/reranker.js} +138 -53
  129. package/dist/text/sentence-transformer-embedder.d.ts +96 -0
  130. package/dist/text/sentence-transformer-embedder.js +340 -0
  131. package/dist/{tokenizer.d.ts → text/tokenizer.d.ts} +1 -0
  132. package/dist/{tokenizer.js → text/tokenizer.js} +7 -2
  133. package/dist/types.d.ts +40 -1
  134. package/dist/utils/vector-math.d.ts +31 -0
  135. package/dist/utils/vector-math.js +70 -0
  136. package/package.json +16 -4
  137. package/dist/api-errors.d.ts.map +0 -1
  138. package/dist/api-errors.js.map +0 -1
  139. package/dist/chunker.d.ts +0 -47
  140. package/dist/chunker.d.ts.map +0 -1
  141. package/dist/chunker.js.map +0 -1
  142. package/dist/cli/indexer.d.ts.map +0 -1
  143. package/dist/cli/indexer.js.map +0 -1
  144. package/dist/cli/search.d.ts.map +0 -1
  145. package/dist/cli/search.js.map +0 -1
  146. package/dist/cli.d.ts.map +0 -1
  147. package/dist/cli.js.map +0 -1
  148. package/dist/config.d.ts.map +0 -1
  149. package/dist/config.js.map +0 -1
  150. package/dist/db.d.ts +0 -90
  151. package/dist/db.d.ts.map +0 -1
  152. package/dist/db.js +0 -340
  153. package/dist/db.js.map +0 -1
  154. package/dist/embedder.d.ts.map +0 -1
  155. package/dist/embedder.js.map +0 -1
  156. package/dist/error-handler.d.ts.map +0 -1
  157. package/dist/error-handler.js.map +0 -1
  158. package/dist/file-processor.d.ts.map +0 -1
  159. package/dist/file-processor.js.map +0 -1
  160. package/dist/index-manager.d.ts.map +0 -1
  161. package/dist/index-manager.js.map +0 -1
  162. package/dist/index.d.ts.map +0 -1
  163. package/dist/index.js.map +0 -1
  164. package/dist/indexer.d.ts.map +0 -1
  165. package/dist/indexer.js.map +0 -1
  166. package/dist/ingestion.d.ts.map +0 -1
  167. package/dist/ingestion.js.map +0 -1
  168. package/dist/mcp-server.d.ts.map +0 -1
  169. package/dist/mcp-server.js.map +0 -1
  170. package/dist/path-manager.d.ts.map +0 -1
  171. package/dist/path-manager.js.map +0 -1
  172. package/dist/preprocess.d.ts.map +0 -1
  173. package/dist/preprocess.js.map +0 -1
  174. package/dist/preprocessors/index.d.ts.map +0 -1
  175. package/dist/preprocessors/index.js.map +0 -1
  176. package/dist/preprocessors/mdx.d.ts.map +0 -1
  177. package/dist/preprocessors/mdx.js.map +0 -1
  178. package/dist/preprocessors/mermaid.d.ts.map +0 -1
  179. package/dist/preprocessors/mermaid.js.map +0 -1
  180. package/dist/preprocessors/registry.d.ts.map +0 -1
  181. package/dist/preprocessors/registry.js.map +0 -1
  182. package/dist/reranker.d.ts +0 -40
  183. package/dist/reranker.d.ts.map +0 -1
  184. package/dist/reranker.js.map +0 -1
  185. package/dist/resource-manager-demo.d.ts +0 -7
  186. package/dist/resource-manager-demo.d.ts.map +0 -1
  187. package/dist/resource-manager-demo.js +0 -52
  188. package/dist/resource-manager-demo.js.map +0 -1
  189. package/dist/resource-manager.d.ts +0 -129
  190. package/dist/resource-manager.d.ts.map +0 -1
  191. package/dist/resource-manager.js +0 -389
  192. package/dist/resource-manager.js.map +0 -1
  193. package/dist/search-standalone.d.ts.map +0 -1
  194. package/dist/search-standalone.js.map +0 -1
  195. package/dist/search.d.ts.map +0 -1
  196. package/dist/search.js.map +0 -1
  197. package/dist/test-utils.d.ts.map +0 -1
  198. package/dist/test-utils.js.map +0 -1
  199. package/dist/tokenizer.d.ts.map +0 -1
  200. package/dist/tokenizer.js.map +0 -1
  201. package/dist/types.d.ts.map +0 -1
  202. package/dist/types.js.map +0 -1
  203. package/dist/vector-index.d.ts.map +0 -1
  204. package/dist/vector-index.js.map +0 -1
@@ -0,0 +1,304 @@
1
+ /**
2
+ * Database Connection Manager - Centralized connection handling
3
+ * Prevents database locking issues by managing shared connections
4
+ * Addresses production issues with MCP server + CLI concurrent usage
5
+ */
6
+ import { openDatabase } from './db.js';
7
+ /**
8
+ * Database Connection Manager
9
+ * Manages shared database connections to prevent locking issues
10
+ */
11
+ export class DatabaseConnectionManager {
12
+ static connections = new Map();
13
+ static cleanupInterval = null;
14
+ static CLEANUP_INTERVAL = 30000; // 30 seconds
15
+ static MAX_IDLE_TIME = 60000; // 1 minute
16
+ /**
17
+ * Get a shared database connection
18
+ * Creates new connection if none exists, otherwise returns existing
19
+ */
20
+ static async getConnection(dbPath) {
21
+ const normalizedPath = this.normalizePath(dbPath);
22
+ let connectionInfo = this.connections.get(normalizedPath);
23
+ if (!connectionInfo || connectionInfo.isClosing) {
24
+ // Create new connection
25
+ const connection = await openDatabase(dbPath);
26
+ connectionInfo = {
27
+ connection,
28
+ refCount: 1,
29
+ lastAccessed: Date.now(),
30
+ isClosing: false
31
+ };
32
+ this.connections.set(normalizedPath, connectionInfo);
33
+ // Start cleanup timer if this is the first connection
34
+ if (this.connections.size === 1 && !this.cleanupInterval) {
35
+ this.startCleanupTimer();
36
+ }
37
+ console.log(`📊 Database connection created: ${normalizedPath} (total: ${this.connections.size})`);
38
+ }
39
+ else {
40
+ // Reuse existing connection
41
+ connectionInfo.refCount++;
42
+ connectionInfo.lastAccessed = Date.now();
43
+ console.log(`🔄 Database connection reused: ${normalizedPath} (refs: ${connectionInfo.refCount})`);
44
+ }
45
+ return connectionInfo.connection;
46
+ }
47
+ /**
48
+ * Release a database connection reference
49
+ * Connection is kept alive for potential reuse
50
+ */
51
+ static async releaseConnection(dbPath) {
52
+ const normalizedPath = this.normalizePath(dbPath);
53
+ const connectionInfo = this.connections.get(normalizedPath);
54
+ if (connectionInfo && !connectionInfo.isClosing) {
55
+ connectionInfo.refCount = Math.max(0, connectionInfo.refCount - 1);
56
+ connectionInfo.lastAccessed = Date.now();
57
+ console.log(`📉 Database connection released: ${normalizedPath} (refs: ${connectionInfo.refCount})`);
58
+ // Don't immediately close - let cleanup timer handle it
59
+ // This prevents rapid open/close cycles
60
+ }
61
+ }
62
+ /**
63
+ * Force close a specific database connection
64
+ * Use with caution - only for cleanup or error recovery
65
+ */
66
+ static async forceCloseConnection(dbPath) {
67
+ const normalizedPath = this.normalizePath(dbPath);
68
+ const connectionInfo = this.connections.get(normalizedPath);
69
+ if (connectionInfo && !connectionInfo.isClosing) {
70
+ connectionInfo.isClosing = true;
71
+ try {
72
+ await connectionInfo.connection.close();
73
+ console.log(`🔒 Database connection force closed: ${normalizedPath}`);
74
+ }
75
+ catch (error) {
76
+ console.warn(`⚠️ Warning: Error force closing connection ${normalizedPath}:`, error);
77
+ }
78
+ finally {
79
+ this.connections.delete(normalizedPath);
80
+ }
81
+ }
82
+ }
83
+ /**
84
+ * Close all database connections
85
+ * Used during application shutdown
86
+ */
87
+ static async closeAllConnections() {
88
+ if (this.connections.size === 0) {
89
+ return; // Nothing to close
90
+ }
91
+ const isCLI = process.env.RAG_CLI_MODE === 'true';
92
+ if (!isCLI) {
93
+ console.log(`🧹 Closing all database connections (${this.connections.size} active)`);
94
+ }
95
+ const closePromises = [];
96
+ for (const [path, connectionInfo] of this.connections.entries()) {
97
+ if (!connectionInfo.isClosing) {
98
+ connectionInfo.isClosing = true;
99
+ closePromises.push(connectionInfo.connection.close()
100
+ .then(() => {
101
+ if (!isCLI) {
102
+ console.log(`✅ Closed connection: ${path}`);
103
+ }
104
+ })
105
+ .catch(error => {
106
+ if (!isCLI) {
107
+ console.warn(`⚠️ Error closing connection ${path}:`, error);
108
+ }
109
+ }));
110
+ }
111
+ }
112
+ await Promise.all(closePromises);
113
+ this.connections.clear();
114
+ if (this.cleanupInterval) {
115
+ clearInterval(this.cleanupInterval);
116
+ this.cleanupInterval = null;
117
+ }
118
+ if (!isCLI) {
119
+ console.log('✅ All database connections closed');
120
+ }
121
+ }
122
+ /**
123
+ * Get connection statistics for monitoring
124
+ */
125
+ static getConnectionStats() {
126
+ const now = Date.now();
127
+ const connections = Array.from(this.connections.entries()).map(([path, info]) => ({
128
+ path,
129
+ refCount: info.refCount,
130
+ lastAccessed: new Date(info.lastAccessed),
131
+ idleTime: now - info.lastAccessed
132
+ }));
133
+ return {
134
+ totalConnections: this.connections.size,
135
+ connections
136
+ };
137
+ }
138
+ /**
139
+ * Check if a connection exists for a given path
140
+ */
141
+ static hasConnection(dbPath) {
142
+ const normalizedPath = this.normalizePath(dbPath);
143
+ const connectionInfo = this.connections.get(normalizedPath);
144
+ return connectionInfo !== undefined && !connectionInfo.isClosing;
145
+ }
146
+ /**
147
+ * Wait for database to become available
148
+ * Useful for handling temporary locking issues
149
+ */
150
+ static async waitForDatabaseAccess(dbPath, maxWaitMs = 5000) {
151
+ const startTime = Date.now();
152
+ while (Date.now() - startTime < maxWaitMs) {
153
+ try {
154
+ // Try to get a connection
155
+ const connection = await this.getConnection(dbPath);
156
+ await this.releaseConnection(dbPath);
157
+ return; // Success
158
+ }
159
+ catch (error) {
160
+ if (error instanceof Error && error.message.includes('SQLITE_BUSY')) {
161
+ // Wait a bit and retry
162
+ await new Promise(resolve => setTimeout(resolve, 100));
163
+ continue;
164
+ }
165
+ // Other errors should be thrown immediately
166
+ throw error;
167
+ }
168
+ }
169
+ throw new Error(`Database ${dbPath} is busy after ${maxWaitMs}ms. Please try again later.`);
170
+ }
171
+ // =============================================================================
172
+ // PRIVATE METHODS
173
+ // =============================================================================
174
+ /**
175
+ * Normalize database path for consistent key usage
176
+ */
177
+ static normalizePath(dbPath) {
178
+ // Convert to absolute path and normalize separators
179
+ // Use URL constructor for cross-platform path normalization
180
+ try {
181
+ const url = new URL(`file://${dbPath.replace(/\\/g, '/')}`);
182
+ return url.pathname;
183
+ }
184
+ catch {
185
+ // Fallback: simple normalization
186
+ return dbPath.replace(/\\/g, '/');
187
+ }
188
+ }
189
+ /**
190
+ * Start the cleanup timer for idle connections
191
+ * Only start if not in CLI mode to prevent hanging
192
+ */
193
+ static startCleanupTimer() {
194
+ // Don't start cleanup timer for CLI commands - they should exit quickly
195
+ const isCLI = process.argv.some(arg => arg.includes('cli.js') ||
196
+ arg.includes('raglite') ||
197
+ process.env.RAG_CLI_MODE === 'true');
198
+ if (!isCLI) {
199
+ this.cleanupInterval = setInterval(() => {
200
+ this.cleanupIdleConnections();
201
+ }, this.CLEANUP_INTERVAL);
202
+ }
203
+ }
204
+ /**
205
+ * Clean up idle connections that haven't been used recently
206
+ */
207
+ static async cleanupIdleConnections() {
208
+ const now = Date.now();
209
+ const connectionsToClose = [];
210
+ for (const [path, connectionInfo] of this.connections.entries()) {
211
+ const idleTime = now - connectionInfo.lastAccessed;
212
+ // Close connections that are idle and have no active references
213
+ if (connectionInfo.refCount === 0 && idleTime > this.MAX_IDLE_TIME && !connectionInfo.isClosing) {
214
+ connectionsToClose.push(path);
215
+ }
216
+ }
217
+ if (connectionsToClose.length > 0) {
218
+ console.log(`🧹 Cleaning up ${connectionsToClose.length} idle database connections`);
219
+ for (const path of connectionsToClose) {
220
+ await this.forceCloseConnection(path);
221
+ }
222
+ }
223
+ // Stop cleanup timer if no connections remain
224
+ if (this.connections.size === 0 && this.cleanupInterval) {
225
+ clearInterval(this.cleanupInterval);
226
+ this.cleanupInterval = null;
227
+ }
228
+ }
229
+ }
230
+ /**
231
+ * Convenience function to get a managed database connection
232
+ * Use this instead of openDatabase() directly
233
+ */
234
+ export async function getManagedConnection(dbPath) {
235
+ return DatabaseConnectionManager.getConnection(dbPath);
236
+ }
237
+ /**
238
+ * Convenience function to release a managed database connection
239
+ * Use this instead of connection.close() directly
240
+ */
241
+ export async function releaseManagedConnection(dbPath) {
242
+ return DatabaseConnectionManager.releaseConnection(dbPath);
243
+ }
244
+ /**
245
+ * Enhanced database connection wrapper
246
+ * Automatically manages connection lifecycle
247
+ */
248
+ export class ManagedDatabaseConnection {
249
+ dbPath;
250
+ connection = null;
251
+ constructor(dbPath) {
252
+ this.dbPath = dbPath;
253
+ }
254
+ /**
255
+ * Get the underlying database connection
256
+ * Automatically acquires managed connection on first use
257
+ */
258
+ async getConnection() {
259
+ if (!this.connection) {
260
+ this.connection = await DatabaseConnectionManager.getConnection(this.dbPath);
261
+ }
262
+ return this.connection;
263
+ }
264
+ /**
265
+ * Execute a database operation with automatic connection management
266
+ */
267
+ async execute(operation) {
268
+ const connection = await this.getConnection();
269
+ return operation(connection);
270
+ }
271
+ /**
272
+ * Release the managed connection
273
+ * Connection may be kept alive for reuse by other components
274
+ */
275
+ async release() {
276
+ if (this.connection) {
277
+ await DatabaseConnectionManager.releaseConnection(this.dbPath);
278
+ this.connection = null;
279
+ }
280
+ }
281
+ /**
282
+ * Force close the connection
283
+ * Use only during cleanup or error recovery
284
+ */
285
+ async forceClose() {
286
+ if (this.connection) {
287
+ await DatabaseConnectionManager.forceCloseConnection(this.dbPath);
288
+ this.connection = null;
289
+ }
290
+ }
291
+ }
292
+ // =============================================================================
293
+ // PROCESS CLEANUP HANDLERS
294
+ // =============================================================================
295
+ /**
296
+ * Ensure all connections are closed on process exit
297
+ */
298
+ process.on('exit', () => {
299
+ // Synchronous cleanup only
300
+ console.log('🔄 Process exiting, cleaning up database connections...');
301
+ });
302
+ // Don't set up global process handlers - let applications handle their own cleanup
303
+ // This prevents CLI commands from hanging due to active event listeners
304
+ //# sourceMappingURL=database-connection-manager.js.map
@@ -0,0 +1,245 @@
1
+ /**
2
+ * CORE MODULE — Shared between text-only (rag-lite-ts) and future multimodal (rag-lite-mm)
3
+ * Model-agnostic. No transformer or modality-specific logic.
4
+ */
5
+ import sqlite3 from 'sqlite3';
6
+ import type { SystemInfo } from '../types.js';
7
+ export interface DatabaseConnection {
8
+ db: sqlite3.Database;
9
+ run: (sql: string, params?: any[]) => Promise<sqlite3.RunResult>;
10
+ get: (sql: string, params?: any[]) => Promise<any>;
11
+ all: (sql: string, params?: any[]) => Promise<any[]>;
12
+ close: () => Promise<void>;
13
+ }
14
+ export interface ContentChunk {
15
+ id: number;
16
+ embedding_id: string;
17
+ document_id: number;
18
+ content: string;
19
+ content_type: string;
20
+ chunk_index: number;
21
+ metadata?: Record<string, any>;
22
+ created_at: string;
23
+ document_source: string;
24
+ document_title: string;
25
+ document_content_type: string;
26
+ document_content_id?: string;
27
+ }
28
+ /**
29
+ * Opens a SQLite database connection with promisified methods
30
+ * @param dbPath - Path to the SQLite database file
31
+ * @returns Promise that resolves to a database connection object
32
+ */
33
+ export declare function openDatabase(dbPath: string): Promise<DatabaseConnection>;
34
+ /**
35
+ * Initializes the database schema with all required tables and indexes
36
+ * Enhanced to support content types for multimodal use
37
+ * @param connection - Database connection object
38
+ */
39
+ export declare function initializeSchema(connection: DatabaseConnection): Promise<void>;
40
+ /**
41
+ * Inserts a new document into the database with content type support
42
+ * @param connection - Database connection object
43
+ * @param source - Source path of the document
44
+ * @param title - Title of the document
45
+ * @param contentType - Type of content ('text', 'image', etc.)
46
+ * @param metadata - Optional metadata object
47
+ * @param contentId - Optional content ID referencing content_metadata table
48
+ * @returns Promise that resolves to the document ID
49
+ */
50
+ export declare function insertDocument(connection: DatabaseConnection, source: string, title: string, contentType?: string, metadata?: Record<string, any>, contentId?: string): Promise<number>;
51
+ /**
52
+ * Inserts or updates a chunk in the database with content type support (upsert operation)
53
+ * @param connection - Database connection object
54
+ * @param embeddingId - Unique embedding ID for the chunk
55
+ * @param documentId - ID of the parent document
56
+ * @param content - Content of the chunk (text, image path, etc.)
57
+ * @param chunkIndex - Index of the chunk within the document
58
+ * @param contentType - Type of content ('text', 'image', etc.)
59
+ * @param metadata - Optional metadata object
60
+ */
61
+ export declare function insertChunk(connection: DatabaseConnection, embeddingId: string, documentId: number, content: string, chunkIndex: number, contentType?: string, metadata?: Record<string, any>): Promise<void>;
62
+ /**
63
+ * Inserts a new document or returns existing document ID if it already exists
64
+ * Enhanced with content type support
65
+ * @param connection - Database connection object
66
+ * @param source - Source path of the document
67
+ * @param title - Title of the document
68
+ * @param contentType - Type of content ('text', 'image', etc.)
69
+ * @param metadata - Optional metadata object
70
+ * @param contentId - Optional content ID referencing content_metadata table
71
+ * @returns Promise that resolves to the document ID
72
+ */
73
+ export declare function upsertDocument(connection: DatabaseConnection, source: string, title: string, contentType?: string, metadata?: Record<string, any>, contentId?: string): Promise<number>;
74
+ /**
75
+ * Retrieves chunks by their embedding IDs with document metadata
76
+ * Enhanced to include content type information
77
+ * @param connection - Database connection object
78
+ * @param embeddingIds - Array of embedding IDs to retrieve
79
+ * @returns Promise that resolves to an array of chunk results with document metadata
80
+ */
81
+ export declare function getChunksByEmbeddingIds(connection: DatabaseConnection, embeddingIds: string[]): Promise<ContentChunk[]>;
82
+ /**
83
+ * Gets the complete system information from system_info table
84
+ * @param connection - Database connection object
85
+ * @returns Promise that resolves to SystemInfo object or null if not set
86
+ */
87
+ export declare function getSystemInfo(connection: DatabaseConnection): Promise<SystemInfo | null>;
88
+ /**
89
+ * Sets the complete system information in system_info table
90
+ * @param connection - Database connection object
91
+ * @param systemInfo - SystemInfo object to store
92
+ */
93
+ export declare function setSystemInfo(connection: DatabaseConnection, systemInfo: Partial<SystemInfo>): Promise<void>;
94
+ /**
95
+ * @deprecated Use getSystemInfo() instead. This function is kept for existing code compatibility.
96
+ * Gets the current model version from system_info table
97
+ * @param connection - Database connection object
98
+ * @returns Promise that resolves to the model version string or null if not set
99
+ */
100
+ export declare function getModelVersion(connection: DatabaseConnection): Promise<string | null>;
101
+ /**
102
+ * @deprecated Use setSystemInfo() instead. This function is kept for existing code compatibility.
103
+ * Sets the model version in system_info table
104
+ * @param connection - Database connection object
105
+ * @param modelVersion - Model version string to store
106
+ */
107
+ export declare function setModelVersion(connection: DatabaseConnection, modelVersion: string): Promise<void>;
108
+ /**
109
+ * @deprecated Use getSystemInfo() instead. This function is kept for existing code compatibility.
110
+ * Gets the stored model information from system_info table
111
+ * @param connection - Database connection object
112
+ * @returns Promise that resolves to model info object or null if not set
113
+ */
114
+ export declare function getStoredModelInfo(connection: DatabaseConnection): Promise<{
115
+ modelName: string;
116
+ dimensions: number;
117
+ } | null>;
118
+ /**
119
+ * @deprecated Use setSystemInfo() instead. This function is kept for existing code compatibility.
120
+ * Sets the model information in system_info table
121
+ * @param connection - Database connection object
122
+ * @param modelName - Name of the embedding model
123
+ * @param dimensions - Number of dimensions for the model
124
+ */
125
+ export declare function setStoredModelInfo(connection: DatabaseConnection, modelName: string, dimensions: number): Promise<void>;
126
+ /**
127
+ * Retrieves documents by content type
128
+ * @param connection - Database connection object
129
+ * @param contentType - Content type to filter by
130
+ * @returns Promise that resolves to an array of documents
131
+ */
132
+ export declare function getDocumentsByContentType(connection: DatabaseConnection, contentType: string): Promise<Array<{
133
+ id: number;
134
+ source: string;
135
+ title: string;
136
+ content_type: string;
137
+ metadata?: Record<string, any>;
138
+ created_at: string;
139
+ }>>;
140
+ /**
141
+ * Retrieves chunks by content type
142
+ * @param connection - Database connection object
143
+ * @param contentType - Content type to filter by
144
+ * @returns Promise that resolves to an array of chunks with document metadata
145
+ */
146
+ export declare function getChunksByContentType(connection: DatabaseConnection, contentType: string): Promise<ContentChunk[]>;
147
+ /**
148
+ * Gets content type statistics from the database
149
+ * @param connection - Database connection object
150
+ * @returns Promise that resolves to content type statistics
151
+ */
152
+ export declare function getContentTypeStatistics(connection: DatabaseConnection): Promise<{
153
+ documents: Record<string, number>;
154
+ chunks: Record<string, number>;
155
+ total: {
156
+ documents: number;
157
+ chunks: number;
158
+ };
159
+ }>;
160
+ /**
161
+ * Updates document metadata
162
+ * @param connection - Database connection object
163
+ * @param documentId - ID of the document to update
164
+ * @param metadata - New metadata object
165
+ */
166
+ export declare function updateDocumentMetadata(connection: DatabaseConnection, documentId: number, metadata: Record<string, any>): Promise<void>;
167
+ /**
168
+ * Updates chunk metadata
169
+ * @param connection - Database connection object
170
+ * @param chunkId - ID of the chunk to update
171
+ * @param metadata - New metadata object
172
+ */
173
+ export declare function updateChunkMetadata(connection: DatabaseConnection, chunkId: number, metadata: Record<string, any>): Promise<void>;
174
+ /**
175
+ * Content metadata interface for unified content system
176
+ */
177
+ export interface ContentMetadata {
178
+ id: string;
179
+ storageType: 'filesystem' | 'content_dir';
180
+ originalPath?: string;
181
+ contentPath: string;
182
+ displayName: string;
183
+ contentType: string;
184
+ fileSize: number;
185
+ contentHash: string;
186
+ createdAt: Date;
187
+ }
188
+ /**
189
+ * Inserts content metadata into the content_metadata table
190
+ * @param connection - Database connection object
191
+ * @param contentMetadata - Content metadata to insert
192
+ */
193
+ export declare function insertContentMetadata(connection: DatabaseConnection, contentMetadata: Omit<ContentMetadata, 'createdAt'>): Promise<void>;
194
+ /**
195
+ * Gets content metadata by content ID
196
+ * @param connection - Database connection object
197
+ * @param contentId - Content ID to retrieve
198
+ * @returns Promise that resolves to ContentMetadata or null if not found
199
+ */
200
+ export declare function getContentMetadata(connection: DatabaseConnection, contentId: string): Promise<ContentMetadata | null>;
201
+ /**
202
+ * Gets content metadata by content hash (for deduplication)
203
+ * @param connection - Database connection object
204
+ * @param contentHash - Content hash to search for
205
+ * @returns Promise that resolves to ContentMetadata or null if not found
206
+ */
207
+ export declare function getContentMetadataByHash(connection: DatabaseConnection, contentHash: string): Promise<ContentMetadata | null>;
208
+ /**
209
+ * Gets all content metadata by storage type
210
+ * @param connection - Database connection object
211
+ * @param storageType - Storage type to filter by
212
+ * @returns Promise that resolves to array of ContentMetadata
213
+ */
214
+ export declare function getContentMetadataByStorageType(connection: DatabaseConnection, storageType: 'filesystem' | 'content_dir'): Promise<ContentMetadata[]>;
215
+ /**
216
+ * Deletes content metadata by content ID
217
+ * @param connection - Database connection object
218
+ * @param contentId - Content ID to delete
219
+ * @returns Promise that resolves to true if deleted, false if not found
220
+ */
221
+ export declare function deleteContentMetadata(connection: DatabaseConnection, contentId: string): Promise<boolean>;
222
+ /**
223
+ * Gets storage statistics from storage_stats table
224
+ * @param connection - Database connection object
225
+ * @returns Promise that resolves to storage statistics
226
+ */
227
+ export declare function getStorageStats(connection: DatabaseConnection): Promise<{
228
+ contentDirFiles: number;
229
+ contentDirSize: number;
230
+ filesystemRefs: number;
231
+ lastCleanup: Date | null;
232
+ updatedAt: Date;
233
+ } | null>;
234
+ /**
235
+ * Updates storage statistics in storage_stats table
236
+ * @param connection - Database connection object
237
+ * @param stats - Partial storage statistics to update
238
+ */
239
+ export declare function updateStorageStats(connection: DatabaseConnection, stats: {
240
+ contentDirFiles?: number;
241
+ contentDirSize?: number;
242
+ filesystemRefs?: number;
243
+ lastCleanup?: Date;
244
+ }): Promise<void>;
245
+ //# sourceMappingURL=db.d.ts.map