moflo 4.0.2 → 4.0.4
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.
- package/package.json +114 -110
- package/v3/@claude-flow/cli/dist/src/commands/hooks.js +4 -1
- package/v3/@claude-flow/cli/dist/src/memory/memory-bridge.js +61 -5
- package/v3/@claude-flow/cli/dist/src/memory/memory-initializer.js +1892 -1841
- package/v3/@claude-flow/memory/README.md +587 -0
- package/v3/@claude-flow/memory/dist/agent-memory-scope.d.ts +131 -0
- package/v3/@claude-flow/memory/dist/agent-memory-scope.js +223 -0
- package/v3/@claude-flow/memory/dist/agent-memory-scope.test.d.ts +8 -0
- package/v3/@claude-flow/memory/dist/agent-memory-scope.test.js +463 -0
- package/v3/@claude-flow/memory/dist/agentdb-adapter.d.ts +165 -0
- package/v3/@claude-flow/memory/dist/agentdb-adapter.js +806 -0
- package/v3/@claude-flow/memory/dist/agentdb-backend.d.ts +214 -0
- package/v3/@claude-flow/memory/dist/agentdb-backend.js +844 -0
- package/v3/@claude-flow/memory/dist/agentdb-backend.test.d.ts +7 -0
- package/v3/@claude-flow/memory/dist/agentdb-backend.test.js +258 -0
- package/v3/@claude-flow/memory/dist/application/commands/delete-memory.command.d.ts +65 -0
- package/v3/@claude-flow/memory/dist/application/commands/delete-memory.command.js +129 -0
- package/v3/@claude-flow/memory/dist/application/commands/store-memory.command.d.ts +48 -0
- package/v3/@claude-flow/memory/dist/application/commands/store-memory.command.js +72 -0
- package/v3/@claude-flow/memory/dist/application/index.d.ts +12 -0
- package/v3/@claude-flow/memory/dist/application/index.js +15 -0
- package/v3/@claude-flow/memory/dist/application/queries/search-memory.query.d.ts +72 -0
- package/v3/@claude-flow/memory/dist/application/queries/search-memory.query.js +143 -0
- package/v3/@claude-flow/memory/dist/application/services/memory-application-service.d.ts +121 -0
- package/v3/@claude-flow/memory/dist/application/services/memory-application-service.js +190 -0
- package/v3/@claude-flow/memory/dist/auto-memory-bridge.d.ts +226 -0
- package/v3/@claude-flow/memory/dist/auto-memory-bridge.js +709 -0
- package/v3/@claude-flow/memory/dist/auto-memory-bridge.test.d.ts +8 -0
- package/v3/@claude-flow/memory/dist/auto-memory-bridge.test.js +754 -0
- package/v3/@claude-flow/memory/dist/benchmark.test.d.ts +2 -0
- package/v3/@claude-flow/memory/dist/benchmark.test.js +277 -0
- package/v3/@claude-flow/memory/dist/cache-manager.d.ts +134 -0
- package/v3/@claude-flow/memory/dist/cache-manager.js +407 -0
- package/v3/@claude-flow/memory/dist/controller-registry.d.ts +216 -0
- package/v3/@claude-flow/memory/dist/controller-registry.js +893 -0
- package/v3/@claude-flow/memory/dist/controller-registry.test.d.ts +14 -0
- package/v3/@claude-flow/memory/dist/controller-registry.test.js +636 -0
- package/v3/@claude-flow/memory/dist/database-provider.d.ts +87 -0
- package/v3/@claude-flow/memory/dist/database-provider.js +375 -0
- package/v3/@claude-flow/memory/dist/database-provider.test.d.ts +7 -0
- package/v3/@claude-flow/memory/dist/database-provider.test.js +285 -0
- package/v3/@claude-flow/memory/dist/domain/entities/memory-entry.d.ts +143 -0
- package/v3/@claude-flow/memory/dist/domain/entities/memory-entry.js +226 -0
- package/v3/@claude-flow/memory/dist/domain/index.d.ts +11 -0
- package/v3/@claude-flow/memory/dist/domain/index.js +12 -0
- package/v3/@claude-flow/memory/dist/domain/repositories/memory-repository.interface.d.ts +102 -0
- package/v3/@claude-flow/memory/dist/domain/repositories/memory-repository.interface.js +11 -0
- package/v3/@claude-flow/memory/dist/domain/services/memory-domain-service.d.ts +105 -0
- package/v3/@claude-flow/memory/dist/domain/services/memory-domain-service.js +297 -0
- package/v3/@claude-flow/memory/dist/hnsw-index.d.ts +111 -0
- package/v3/@claude-flow/memory/dist/hnsw-index.js +781 -0
- package/v3/@claude-flow/memory/dist/hnsw-lite.d.ts +23 -0
- package/v3/@claude-flow/memory/dist/hnsw-lite.js +168 -0
- package/v3/@claude-flow/memory/dist/hybrid-backend.d.ts +245 -0
- package/v3/@claude-flow/memory/dist/hybrid-backend.js +569 -0
- package/v3/@claude-flow/memory/dist/hybrid-backend.test.d.ts +8 -0
- package/v3/@claude-flow/memory/dist/hybrid-backend.test.js +320 -0
- package/v3/@claude-flow/memory/dist/index.d.ts +207 -0
- package/v3/@claude-flow/memory/dist/index.js +361 -0
- package/v3/@claude-flow/memory/dist/infrastructure/index.d.ts +17 -0
- package/v3/@claude-flow/memory/dist/infrastructure/index.js +16 -0
- package/v3/@claude-flow/memory/dist/infrastructure/repositories/hybrid-memory-repository.d.ts +66 -0
- package/v3/@claude-flow/memory/dist/infrastructure/repositories/hybrid-memory-repository.js +409 -0
- package/v3/@claude-flow/memory/dist/learning-bridge.d.ts +137 -0
- package/v3/@claude-flow/memory/dist/learning-bridge.js +335 -0
- package/v3/@claude-flow/memory/dist/learning-bridge.test.d.ts +8 -0
- package/v3/@claude-flow/memory/dist/learning-bridge.test.js +578 -0
- package/v3/@claude-flow/memory/dist/memory-graph.d.ts +100 -0
- package/v3/@claude-flow/memory/dist/memory-graph.js +333 -0
- package/v3/@claude-flow/memory/dist/memory-graph.test.d.ts +8 -0
- package/v3/@claude-flow/memory/dist/memory-graph.test.js +609 -0
- package/v3/@claude-flow/memory/dist/migration.d.ts +68 -0
- package/v3/@claude-flow/memory/dist/migration.js +513 -0
- package/v3/@claude-flow/memory/dist/persistent-sona.d.ts +144 -0
- package/v3/@claude-flow/memory/dist/persistent-sona.js +332 -0
- package/v3/@claude-flow/memory/dist/query-builder.d.ts +211 -0
- package/v3/@claude-flow/memory/dist/query-builder.js +438 -0
- package/v3/@claude-flow/memory/dist/rvf-backend.d.ts +51 -0
- package/v3/@claude-flow/memory/dist/rvf-backend.js +481 -0
- package/v3/@claude-flow/memory/dist/rvf-learning-store.d.ts +139 -0
- package/v3/@claude-flow/memory/dist/rvf-learning-store.js +295 -0
- package/v3/@claude-flow/memory/dist/rvf-migration.d.ts +45 -0
- package/v3/@claude-flow/memory/dist/rvf-migration.js +234 -0
- package/v3/@claude-flow/memory/dist/sqlite-backend.d.ts +121 -0
- package/v3/@claude-flow/memory/dist/sqlite-backend.js +572 -0
- package/v3/@claude-flow/memory/dist/sqljs-backend.d.ts +128 -0
- package/v3/@claude-flow/memory/dist/sqljs-backend.js +601 -0
- package/v3/@claude-flow/memory/dist/types.d.ts +484 -0
- package/v3/@claude-flow/memory/dist/types.js +58 -0
- package/v3/@claude-flow/memory/package.json +42 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DatabaseProvider - Platform-aware database selection
|
|
3
|
+
*
|
|
4
|
+
* Automatically selects best backend:
|
|
5
|
+
* - Linux/macOS: better-sqlite3 (native, fast)
|
|
6
|
+
* - Windows: sql.js (WASM, universal) when native fails
|
|
7
|
+
* - Fallback: JSON file storage
|
|
8
|
+
*
|
|
9
|
+
* @module v3/memory/database-provider
|
|
10
|
+
*/
|
|
11
|
+
import { IMemoryBackend } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Available database provider types
|
|
14
|
+
*/
|
|
15
|
+
export type DatabaseProvider = 'sql.js' | 'json' | 'rvf' | 'auto';
|
|
16
|
+
/**
|
|
17
|
+
* Database creation options
|
|
18
|
+
*/
|
|
19
|
+
export interface DatabaseOptions {
|
|
20
|
+
/** Preferred provider (auto = platform-aware selection) */
|
|
21
|
+
provider?: DatabaseProvider;
|
|
22
|
+
/** Enable verbose logging */
|
|
23
|
+
verbose?: boolean;
|
|
24
|
+
/** Enable WAL mode (better-sqlite3 only) */
|
|
25
|
+
walMode?: boolean;
|
|
26
|
+
/** Enable query optimization */
|
|
27
|
+
optimize?: boolean;
|
|
28
|
+
/** Default namespace */
|
|
29
|
+
defaultNamespace?: string;
|
|
30
|
+
/** Maximum entries before auto-cleanup */
|
|
31
|
+
maxEntries?: number;
|
|
32
|
+
/** Auto-persist interval for sql.js (milliseconds) */
|
|
33
|
+
autoPersistInterval?: number;
|
|
34
|
+
/** Path to sql.js WASM file */
|
|
35
|
+
wasmPath?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Platform detection result
|
|
39
|
+
*/
|
|
40
|
+
interface PlatformInfo {
|
|
41
|
+
os: string;
|
|
42
|
+
isWindows: boolean;
|
|
43
|
+
isMacOS: boolean;
|
|
44
|
+
isLinux: boolean;
|
|
45
|
+
recommendedProvider: DatabaseProvider;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create a database instance with platform-aware provider selection
|
|
49
|
+
*
|
|
50
|
+
* @param path - Database file path (:memory: for in-memory)
|
|
51
|
+
* @param options - Database configuration options
|
|
52
|
+
* @returns Initialized database backend
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // Auto-select best provider for platform
|
|
57
|
+
* const db = await createDatabase('./data/memory.db');
|
|
58
|
+
*
|
|
59
|
+
* // Force specific provider
|
|
60
|
+
* const db = await createDatabase('./data/memory.db', {
|
|
61
|
+
* provider: 'sql.js'
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* // With custom options
|
|
65
|
+
* const db = await createDatabase('./data/memory.db', {
|
|
66
|
+
* verbose: true,
|
|
67
|
+
* optimize: true,
|
|
68
|
+
* autoPersistInterval: 10000
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function createDatabase(path: string, options?: DatabaseOptions): Promise<IMemoryBackend>;
|
|
73
|
+
/**
|
|
74
|
+
* Get platform information
|
|
75
|
+
*/
|
|
76
|
+
export declare function getPlatformInfo(): PlatformInfo;
|
|
77
|
+
/**
|
|
78
|
+
* Check which providers are available
|
|
79
|
+
*/
|
|
80
|
+
export declare function getAvailableProviders(): Promise<{
|
|
81
|
+
rvf: boolean;
|
|
82
|
+
betterSqlite3: boolean;
|
|
83
|
+
sqlJs: boolean;
|
|
84
|
+
json: boolean;
|
|
85
|
+
}>;
|
|
86
|
+
export {};
|
|
87
|
+
//# sourceMappingURL=database-provider.d.ts.map
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DatabaseProvider - Platform-aware database selection
|
|
3
|
+
*
|
|
4
|
+
* Automatically selects best backend:
|
|
5
|
+
* - Linux/macOS: better-sqlite3 (native, fast)
|
|
6
|
+
* - Windows: sql.js (WASM, universal) when native fails
|
|
7
|
+
* - Fallback: JSON file storage
|
|
8
|
+
*
|
|
9
|
+
* @module v3/memory/database-provider
|
|
10
|
+
*/
|
|
11
|
+
import { platform } from 'node:os';
|
|
12
|
+
import { existsSync } from 'node:fs';
|
|
13
|
+
import { SqlJsBackend } from './sqljs-backend.js';
|
|
14
|
+
/**
|
|
15
|
+
* Detect platform and recommend provider
|
|
16
|
+
*/
|
|
17
|
+
function detectPlatform() {
|
|
18
|
+
const os = platform();
|
|
19
|
+
const isWindows = os === 'win32';
|
|
20
|
+
const isMacOS = os === 'darwin';
|
|
21
|
+
const isLinux = os === 'linux';
|
|
22
|
+
// Recommend better-sqlite3 for Unix-like systems, sql.js for Windows
|
|
23
|
+
const recommendedProvider = 'sql.js';
|
|
24
|
+
return {
|
|
25
|
+
os,
|
|
26
|
+
isWindows,
|
|
27
|
+
isMacOS,
|
|
28
|
+
isLinux,
|
|
29
|
+
recommendedProvider,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Test if RVF backend is available (always true — pure-TS fallback)
|
|
34
|
+
*/
|
|
35
|
+
async function testRvf() {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
/** better-sqlite3 removed — always returns false */
|
|
39
|
+
async function testBetterSqlite3() {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Test if sql.js is available and working
|
|
44
|
+
*/
|
|
45
|
+
async function testSqlJs() {
|
|
46
|
+
try {
|
|
47
|
+
const initSqlJs = (await import('sql.js')).default;
|
|
48
|
+
const SQL = await initSqlJs();
|
|
49
|
+
const testDb = new SQL.Database();
|
|
50
|
+
testDb.close();
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Select best available provider
|
|
59
|
+
*/
|
|
60
|
+
async function selectProvider(preferred, verbose = false) {
|
|
61
|
+
if (preferred && preferred !== 'auto') {
|
|
62
|
+
if (verbose) {
|
|
63
|
+
console.log(`[DatabaseProvider] Using explicitly specified provider: ${preferred}`);
|
|
64
|
+
}
|
|
65
|
+
return preferred;
|
|
66
|
+
}
|
|
67
|
+
const platformInfo = detectPlatform();
|
|
68
|
+
if (verbose) {
|
|
69
|
+
console.log(`[DatabaseProvider] Platform detected: ${platformInfo.os}`);
|
|
70
|
+
console.log(`[DatabaseProvider] Recommended provider: ${platformInfo.recommendedProvider}`);
|
|
71
|
+
}
|
|
72
|
+
// Try RVF first (always available via pure-TS fallback, native when @ruvector/rvf installed)
|
|
73
|
+
if (await testRvf()) {
|
|
74
|
+
if (verbose) {
|
|
75
|
+
console.log('[DatabaseProvider] RVF backend available');
|
|
76
|
+
}
|
|
77
|
+
return 'rvf';
|
|
78
|
+
}
|
|
79
|
+
// Try sql.js (moflo: sql.js is the only SQLite backend)
|
|
80
|
+
if (await testSqlJs()) {
|
|
81
|
+
if (verbose) {
|
|
82
|
+
console.log('[DatabaseProvider] sql.js available and working');
|
|
83
|
+
}
|
|
84
|
+
return 'sql.js';
|
|
85
|
+
}
|
|
86
|
+
else if (verbose) {
|
|
87
|
+
console.log('[DatabaseProvider] sql.js not available, using JSON fallback');
|
|
88
|
+
}
|
|
89
|
+
// Final fallback to JSON
|
|
90
|
+
return 'json';
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Create a database instance with platform-aware provider selection
|
|
94
|
+
*
|
|
95
|
+
* @param path - Database file path (:memory: for in-memory)
|
|
96
|
+
* @param options - Database configuration options
|
|
97
|
+
* @returns Initialized database backend
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* // Auto-select best provider for platform
|
|
102
|
+
* const db = await createDatabase('./data/memory.db');
|
|
103
|
+
*
|
|
104
|
+
* // Force specific provider
|
|
105
|
+
* const db = await createDatabase('./data/memory.db', {
|
|
106
|
+
* provider: 'sql.js'
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* // With custom options
|
|
110
|
+
* const db = await createDatabase('./data/memory.db', {
|
|
111
|
+
* verbose: true,
|
|
112
|
+
* optimize: true,
|
|
113
|
+
* autoPersistInterval: 10000
|
|
114
|
+
* });
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export async function createDatabase(path, options = {}) {
|
|
118
|
+
const { provider = 'auto', verbose = false, walMode = true, optimize = true, defaultNamespace = 'default', maxEntries = 1000000, autoPersistInterval = 5000, wasmPath, } = options;
|
|
119
|
+
// Select provider
|
|
120
|
+
const selectedProvider = await selectProvider(provider, verbose);
|
|
121
|
+
if (verbose) {
|
|
122
|
+
console.log(`[DatabaseProvider] Creating database with provider: ${selectedProvider}`);
|
|
123
|
+
console.log(`[DatabaseProvider] Database path: ${path}`);
|
|
124
|
+
}
|
|
125
|
+
let backend;
|
|
126
|
+
switch (selectedProvider) {
|
|
127
|
+
case 'sql.js': {
|
|
128
|
+
const config = {
|
|
129
|
+
databasePath: path,
|
|
130
|
+
optimize,
|
|
131
|
+
defaultNamespace,
|
|
132
|
+
maxEntries,
|
|
133
|
+
verbose,
|
|
134
|
+
autoPersistInterval,
|
|
135
|
+
wasmPath,
|
|
136
|
+
};
|
|
137
|
+
backend = new SqlJsBackend(config);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case 'rvf': {
|
|
141
|
+
const { RvfBackend } = await import('./rvf-backend.js');
|
|
142
|
+
backend = new RvfBackend({
|
|
143
|
+
databasePath: path.replace(/\.(db|json)$/, '.rvf'),
|
|
144
|
+
dimensions: 1536,
|
|
145
|
+
verbose,
|
|
146
|
+
defaultNamespace,
|
|
147
|
+
autoPersistInterval,
|
|
148
|
+
});
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
case 'json': {
|
|
152
|
+
// Simple JSON file backend (minimal implementation)
|
|
153
|
+
backend = new JsonBackend(path, verbose);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
default:
|
|
157
|
+
throw new Error(`Unknown database provider: ${selectedProvider}`);
|
|
158
|
+
}
|
|
159
|
+
// Initialize the backend
|
|
160
|
+
await backend.initialize();
|
|
161
|
+
if (verbose) {
|
|
162
|
+
console.log(`[DatabaseProvider] Database initialized successfully`);
|
|
163
|
+
}
|
|
164
|
+
return backend;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Get platform information
|
|
168
|
+
*/
|
|
169
|
+
export function getPlatformInfo() {
|
|
170
|
+
return detectPlatform();
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Check which providers are available
|
|
174
|
+
*/
|
|
175
|
+
export async function getAvailableProviders() {
|
|
176
|
+
return {
|
|
177
|
+
rvf: true,
|
|
178
|
+
betterSqlite3: await testBetterSqlite3(),
|
|
179
|
+
sqlJs: await testSqlJs(),
|
|
180
|
+
json: true,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
// ===== JSON Fallback Backend =====
|
|
184
|
+
/**
|
|
185
|
+
* Simple JSON file backend for when no SQLite is available
|
|
186
|
+
*/
|
|
187
|
+
class JsonBackend {
|
|
188
|
+
entries = new Map();
|
|
189
|
+
path;
|
|
190
|
+
verbose;
|
|
191
|
+
initialized = false;
|
|
192
|
+
constructor(path, verbose = false) {
|
|
193
|
+
this.path = path;
|
|
194
|
+
this.verbose = verbose;
|
|
195
|
+
}
|
|
196
|
+
async initialize() {
|
|
197
|
+
if (this.initialized)
|
|
198
|
+
return;
|
|
199
|
+
// Load from file if exists
|
|
200
|
+
if (this.path !== ':memory:' && existsSync(this.path)) {
|
|
201
|
+
try {
|
|
202
|
+
const fs = await import('node:fs/promises');
|
|
203
|
+
const data = await fs.readFile(this.path, 'utf-8');
|
|
204
|
+
const entries = JSON.parse(data);
|
|
205
|
+
for (const entry of entries) {
|
|
206
|
+
// Convert embedding array back to Float32Array
|
|
207
|
+
if (entry.embedding) {
|
|
208
|
+
entry.embedding = new Float32Array(entry.embedding);
|
|
209
|
+
}
|
|
210
|
+
this.entries.set(entry.id, entry);
|
|
211
|
+
}
|
|
212
|
+
if (this.verbose) {
|
|
213
|
+
console.log(`[JsonBackend] Loaded ${this.entries.size} entries from ${this.path}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
if (this.verbose) {
|
|
218
|
+
console.error('[JsonBackend] Error loading file:', error);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
this.initialized = true;
|
|
223
|
+
}
|
|
224
|
+
async shutdown() {
|
|
225
|
+
await this.persist();
|
|
226
|
+
this.initialized = false;
|
|
227
|
+
}
|
|
228
|
+
async store(entry) {
|
|
229
|
+
this.entries.set(entry.id, entry);
|
|
230
|
+
await this.persist();
|
|
231
|
+
}
|
|
232
|
+
async get(id) {
|
|
233
|
+
return this.entries.get(id) || null;
|
|
234
|
+
}
|
|
235
|
+
async getByKey(namespace, key) {
|
|
236
|
+
for (const entry of this.entries.values()) {
|
|
237
|
+
if (entry.namespace === namespace && entry.key === key) {
|
|
238
|
+
return entry;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
async update(id, updateData) {
|
|
244
|
+
const entry = this.entries.get(id);
|
|
245
|
+
if (!entry)
|
|
246
|
+
return null;
|
|
247
|
+
const updated = { ...entry, ...updateData, updatedAt: Date.now(), version: entry.version + 1 };
|
|
248
|
+
this.entries.set(id, updated);
|
|
249
|
+
await this.persist();
|
|
250
|
+
return updated;
|
|
251
|
+
}
|
|
252
|
+
async delete(id) {
|
|
253
|
+
const result = this.entries.delete(id);
|
|
254
|
+
await this.persist();
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
async query(query) {
|
|
258
|
+
let results = Array.from(this.entries.values());
|
|
259
|
+
if (query.namespace) {
|
|
260
|
+
results = results.filter((e) => e.namespace === query.namespace);
|
|
261
|
+
}
|
|
262
|
+
if (query.key) {
|
|
263
|
+
results = results.filter((e) => e.key === query.key);
|
|
264
|
+
}
|
|
265
|
+
if (query.tags && query.tags.length > 0) {
|
|
266
|
+
results = results.filter((e) => query.tags.every((tag) => e.tags.includes(tag)));
|
|
267
|
+
}
|
|
268
|
+
return results.slice(0, query.limit);
|
|
269
|
+
}
|
|
270
|
+
async search(embedding, options) {
|
|
271
|
+
// Simple brute-force search
|
|
272
|
+
const results = [];
|
|
273
|
+
for (const entry of this.entries.values()) {
|
|
274
|
+
if (!entry.embedding)
|
|
275
|
+
continue;
|
|
276
|
+
const similarity = this.cosineSimilarity(embedding, entry.embedding);
|
|
277
|
+
if (options.threshold && similarity < options.threshold)
|
|
278
|
+
continue;
|
|
279
|
+
results.push({ entry, score: similarity, distance: 1 - similarity });
|
|
280
|
+
}
|
|
281
|
+
results.sort((a, b) => b.score - a.score);
|
|
282
|
+
return results.slice(0, options.k);
|
|
283
|
+
}
|
|
284
|
+
async bulkInsert(entries) {
|
|
285
|
+
for (const entry of entries) {
|
|
286
|
+
this.entries.set(entry.id, entry);
|
|
287
|
+
}
|
|
288
|
+
await this.persist();
|
|
289
|
+
}
|
|
290
|
+
async bulkDelete(ids) {
|
|
291
|
+
let count = 0;
|
|
292
|
+
for (const id of ids) {
|
|
293
|
+
if (this.entries.delete(id))
|
|
294
|
+
count++;
|
|
295
|
+
}
|
|
296
|
+
await this.persist();
|
|
297
|
+
return count;
|
|
298
|
+
}
|
|
299
|
+
async count(namespace) {
|
|
300
|
+
if (!namespace)
|
|
301
|
+
return this.entries.size;
|
|
302
|
+
let count = 0;
|
|
303
|
+
for (const entry of this.entries.values()) {
|
|
304
|
+
if (entry.namespace === namespace)
|
|
305
|
+
count++;
|
|
306
|
+
}
|
|
307
|
+
return count;
|
|
308
|
+
}
|
|
309
|
+
async listNamespaces() {
|
|
310
|
+
const namespaces = new Set();
|
|
311
|
+
for (const entry of this.entries.values()) {
|
|
312
|
+
namespaces.add(entry.namespace);
|
|
313
|
+
}
|
|
314
|
+
return Array.from(namespaces);
|
|
315
|
+
}
|
|
316
|
+
async clearNamespace(namespace) {
|
|
317
|
+
let count = 0;
|
|
318
|
+
for (const [id, entry] of this.entries.entries()) {
|
|
319
|
+
if (entry.namespace === namespace) {
|
|
320
|
+
this.entries.delete(id);
|
|
321
|
+
count++;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
await this.persist();
|
|
325
|
+
return count;
|
|
326
|
+
}
|
|
327
|
+
async getStats() {
|
|
328
|
+
return {
|
|
329
|
+
totalEntries: this.entries.size,
|
|
330
|
+
entriesByNamespace: {},
|
|
331
|
+
entriesByType: {},
|
|
332
|
+
memoryUsage: 0,
|
|
333
|
+
avgQueryTime: 0,
|
|
334
|
+
avgSearchTime: 0,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
async healthCheck() {
|
|
338
|
+
return {
|
|
339
|
+
status: 'healthy',
|
|
340
|
+
components: {
|
|
341
|
+
storage: { status: 'healthy', latency: 0 },
|
|
342
|
+
index: { status: 'healthy', latency: 0 },
|
|
343
|
+
cache: { status: 'healthy', latency: 0 },
|
|
344
|
+
},
|
|
345
|
+
timestamp: Date.now(),
|
|
346
|
+
issues: [],
|
|
347
|
+
recommendations: ['Consider using SQLite backend for better performance'],
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
async persist() {
|
|
351
|
+
if (this.path === ':memory:')
|
|
352
|
+
return;
|
|
353
|
+
const fs = await import('node:fs/promises');
|
|
354
|
+
const entries = Array.from(this.entries.values()).map((e) => ({
|
|
355
|
+
...e,
|
|
356
|
+
// Convert Float32Array to regular array for JSON serialization
|
|
357
|
+
embedding: e.embedding ? Array.from(e.embedding) : undefined,
|
|
358
|
+
}));
|
|
359
|
+
await fs.writeFile(this.path, JSON.stringify(entries, null, 2));
|
|
360
|
+
}
|
|
361
|
+
cosineSimilarity(a, b) {
|
|
362
|
+
let dot = 0;
|
|
363
|
+
let normA = 0;
|
|
364
|
+
let normB = 0;
|
|
365
|
+
for (let i = 0; i < a.length; i++) {
|
|
366
|
+
dot += a[i] * b[i];
|
|
367
|
+
normA += a[i] * a[i];
|
|
368
|
+
normB += b[i] * b[i];
|
|
369
|
+
}
|
|
370
|
+
if (normA === 0 || normB === 0)
|
|
371
|
+
return 0;
|
|
372
|
+
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
//# sourceMappingURL=database-provider.js.map
|