@cmdoss/memwal-sdk 0.6.2 → 0.7.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.
- package/ARCHITECTURE.md +547 -547
- package/BENCHMARKS.md +238 -238
- package/README.md +181 -181
- package/dist/ai-sdk/tools.d.ts +2 -2
- package/dist/ai-sdk/tools.js +2 -2
- package/dist/client/PersonalDataWallet.d.ts.map +1 -1
- package/dist/client/SimplePDWClient.d.ts +1 -1
- package/dist/client/SimplePDWClient.d.ts.map +1 -1
- package/dist/client/SimplePDWClient.js +16 -7
- package/dist/client/SimplePDWClient.js.map +1 -1
- package/dist/client/namespaces/EmbeddingsNamespace.d.ts +1 -1
- package/dist/client/namespaces/EmbeddingsNamespace.js +1 -1
- package/dist/client/namespaces/MemoryNamespace.d.ts +27 -0
- package/dist/client/namespaces/MemoryNamespace.d.ts.map +1 -1
- package/dist/client/namespaces/MemoryNamespace.js +104 -0
- package/dist/client/namespaces/MemoryNamespace.js.map +1 -1
- package/dist/client/namespaces/consolidated/AINamespace.d.ts +2 -2
- package/dist/client/namespaces/consolidated/AINamespace.js +2 -2
- package/dist/client/namespaces/consolidated/BlockchainNamespace.d.ts.map +1 -1
- package/dist/client/namespaces/consolidated/BlockchainNamespace.js +22 -2
- package/dist/client/namespaces/consolidated/BlockchainNamespace.js.map +1 -1
- package/dist/graph/GraphService.js +1 -1
- package/dist/graph/GraphService.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/retrieval/MemoryRetrievalService.d.ts +31 -0
- package/dist/retrieval/MemoryRetrievalService.d.ts.map +1 -1
- package/dist/retrieval/MemoryRetrievalService.js +44 -4
- package/dist/retrieval/MemoryRetrievalService.js.map +1 -1
- package/dist/services/EmbeddingService.d.ts +28 -1
- package/dist/services/EmbeddingService.d.ts.map +1 -1
- package/dist/services/EmbeddingService.js +54 -0
- package/dist/services/EmbeddingService.js.map +1 -1
- package/dist/services/IndexManager.d.ts +5 -1
- package/dist/services/IndexManager.d.ts.map +1 -1
- package/dist/services/IndexManager.js +17 -40
- package/dist/services/IndexManager.js.map +1 -1
- package/dist/services/QueryService.js +1 -1
- package/dist/services/QueryService.js.map +1 -1
- package/dist/services/StorageService.d.ts +10 -0
- package/dist/services/StorageService.d.ts.map +1 -1
- package/dist/services/StorageService.js +13 -0
- package/dist/services/StorageService.js.map +1 -1
- package/dist/services/storage/QuiltBatchManager.d.ts +101 -1
- package/dist/services/storage/QuiltBatchManager.d.ts.map +1 -1
- package/dist/services/storage/QuiltBatchManager.js +410 -20
- package/dist/services/storage/QuiltBatchManager.js.map +1 -1
- package/dist/services/storage/index.d.ts +1 -1
- package/dist/services/storage/index.d.ts.map +1 -1
- package/dist/services/storage/index.js.map +1 -1
- package/dist/utils/LRUCache.d.ts +106 -0
- package/dist/utils/LRUCache.d.ts.map +1 -0
- package/dist/utils/LRUCache.js +281 -0
- package/dist/utils/LRUCache.js.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/memoryIndexOnChain.d.ts +212 -0
- package/dist/utils/memoryIndexOnChain.d.ts.map +1 -0
- package/dist/utils/memoryIndexOnChain.js +312 -0
- package/dist/utils/memoryIndexOnChain.js.map +1 -0
- package/dist/utils/rebuildIndexNode.d.ts +29 -0
- package/dist/utils/rebuildIndexNode.d.ts.map +1 -1
- package/dist/utils/rebuildIndexNode.js +366 -98
- package/dist/utils/rebuildIndexNode.js.map +1 -1
- package/dist/vector/HnswWasmService.d.ts +20 -5
- package/dist/vector/HnswWasmService.d.ts.map +1 -1
- package/dist/vector/HnswWasmService.js +73 -40
- package/dist/vector/HnswWasmService.js.map +1 -1
- package/dist/vector/IHnswService.d.ts +10 -1
- package/dist/vector/IHnswService.d.ts.map +1 -1
- package/dist/vector/IHnswService.js.map +1 -1
- package/dist/vector/NodeHnswService.d.ts +16 -0
- package/dist/vector/NodeHnswService.d.ts.map +1 -1
- package/dist/vector/NodeHnswService.js +84 -5
- package/dist/vector/NodeHnswService.js.map +1 -1
- package/dist/vector/createHnswService.d.ts +1 -1
- package/dist/vector/createHnswService.js +1 -1
- package/dist/vector/index.d.ts +1 -1
- package/dist/vector/index.js +1 -1
- package/package.json +157 -157
- package/src/access/index.ts +8 -8
- package/src/aggregation/index.ts +8 -8
- package/src/ai-sdk/tools.ts +2 -2
- package/src/client/SimplePDWClient.ts +23 -8
- package/src/client/namespaces/EmbeddingsNamespace.ts +1 -1
- package/src/client/namespaces/MemoryNamespace.ts +137 -0
- package/src/client/namespaces/consolidated/AINamespace.ts +2 -2
- package/src/client/namespaces/consolidated/BlockchainNamespace.ts +20 -2
- package/src/client/signers/DappKitSigner.ts +207 -207
- package/src/core/types/index.ts +1 -1
- package/src/generated/pdw/deps/sui/object.ts +12 -12
- package/src/generated/pdw/deps/sui/vec_map.ts +32 -32
- package/src/generated/pdw/memory.ts +1087 -1087
- package/src/generated/pdw/wallet.ts +123 -123
- package/src/generated/utils/index.ts +159 -159
- package/src/graph/GraphService.ts +1 -1
- package/src/index.ts +25 -1
- package/src/permissions/index.ts +9 -9
- package/src/retrieval/MemoryRetrievalService.ts +78 -4
- package/src/services/EmbeddingService.ts +66 -1
- package/src/services/IndexManager.ts +18 -45
- package/src/services/QueryService.ts +1 -1
- package/src/services/StorageService.ts +15 -0
- package/src/services/storage/QuiltBatchManager.ts +492 -22
- package/src/services/storage/index.ts +6 -1
- package/src/utils/LRUCache.ts +378 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/memoryIndexOnChain.ts +507 -0
- package/src/utils/rebuildIndexNode.ts +453 -106
- package/src/vector/HnswWasmService.ts +95 -43
- package/src/vector/IHnswService.ts +10 -1
- package/src/vector/NodeHnswService.ts +103 -5
- package/src/vector/createHnswService.ts +1 -1
- package/src/vector/index.ts +1 -1
- package/src/wallet/index.ts +17 -17
|
@@ -182,10 +182,15 @@ export class MemoryRetrievalService {
|
|
|
182
182
|
private contentCache = new Map<string, { content: string; metadata: any; timestamp: number }>();
|
|
183
183
|
private analyticsCache = new Map<string, { analytics: any; timestamp: number }>();
|
|
184
184
|
|
|
185
|
-
// Cache TTL settings
|
|
186
|
-
private readonly QUERY_CACHE_TTL
|
|
187
|
-
private readonly CONTENT_CACHE_TTL
|
|
188
|
-
private readonly ANALYTICS_CACHE_TTL
|
|
185
|
+
// Cache TTL settings (configurable)
|
|
186
|
+
private readonly QUERY_CACHE_TTL: number;
|
|
187
|
+
private readonly CONTENT_CACHE_TTL: number;
|
|
188
|
+
private readonly ANALYTICS_CACHE_TTL: number;
|
|
189
|
+
|
|
190
|
+
// Cache size limits to prevent memory leaks (configurable)
|
|
191
|
+
private readonly MAX_QUERY_CACHE_SIZE: number;
|
|
192
|
+
private readonly MAX_CONTENT_CACHE_SIZE: number;
|
|
193
|
+
private readonly MAX_ANALYTICS_CACHE_SIZE: number;
|
|
189
194
|
|
|
190
195
|
constructor(config?: {
|
|
191
196
|
embeddingService?: EmbeddingService;
|
|
@@ -196,7 +201,29 @@ export class MemoryRetrievalService {
|
|
|
196
201
|
encryptionService?: EncryptionService;
|
|
197
202
|
batchManager?: BatchManager;
|
|
198
203
|
decryptionConfig?: DecryptionConfig;
|
|
204
|
+
/** Cache configuration for memory management */
|
|
205
|
+
cacheConfig?: {
|
|
206
|
+
/** Query cache TTL in ms (default: 5 minutes) */
|
|
207
|
+
queryCacheTtlMs?: number;
|
|
208
|
+
/** Content cache TTL in ms (default: 30 minutes) */
|
|
209
|
+
contentCacheTtlMs?: number;
|
|
210
|
+
/** Analytics cache TTL in ms (default: 1 hour) */
|
|
211
|
+
analyticsCacheTtlMs?: number;
|
|
212
|
+
/** Max query cache entries (default: 100) */
|
|
213
|
+
maxQueryCacheSize?: number;
|
|
214
|
+
/** Max content cache entries (default: 50) */
|
|
215
|
+
maxContentCacheSize?: number;
|
|
216
|
+
/** Max analytics cache entries (default: 100) */
|
|
217
|
+
maxAnalyticsCacheSize?: number;
|
|
218
|
+
};
|
|
199
219
|
}) {
|
|
220
|
+
// Initialize cache settings with configurable defaults
|
|
221
|
+
this.QUERY_CACHE_TTL = config?.cacheConfig?.queryCacheTtlMs ?? 5 * 60 * 1000; // 5 minutes
|
|
222
|
+
this.CONTENT_CACHE_TTL = config?.cacheConfig?.contentCacheTtlMs ?? 30 * 60 * 1000; // 30 minutes
|
|
223
|
+
this.ANALYTICS_CACHE_TTL = config?.cacheConfig?.analyticsCacheTtlMs ?? 60 * 60 * 1000; // 1 hour
|
|
224
|
+
this.MAX_QUERY_CACHE_SIZE = config?.cacheConfig?.maxQueryCacheSize ?? 100;
|
|
225
|
+
this.MAX_CONTENT_CACHE_SIZE = config?.cacheConfig?.maxContentCacheSize ?? 50; // Content can be large
|
|
226
|
+
this.MAX_ANALYTICS_CACHE_SIZE = config?.cacheConfig?.maxAnalyticsCacheSize ?? 100;
|
|
200
227
|
// Initialize services (can be injected or created with default configs)
|
|
201
228
|
this.embeddingService = config?.embeddingService ?? new EmbeddingService();
|
|
202
229
|
this.storageManager = config?.storageManager ?? new StorageManager();
|
|
@@ -684,9 +711,56 @@ export class MemoryRetrievalService {
|
|
|
684
711
|
}
|
|
685
712
|
|
|
686
713
|
private cacheQuery(key: string, result: RetrievalContext): void {
|
|
714
|
+
this.enforceCacheLimit(this.queryCache, this.MAX_QUERY_CACHE_SIZE, this.QUERY_CACHE_TTL);
|
|
687
715
|
this.queryCache.set(key, { result, timestamp: Date.now() });
|
|
688
716
|
}
|
|
689
717
|
|
|
718
|
+
/**
|
|
719
|
+
* Cache content with size limit enforcement
|
|
720
|
+
*/
|
|
721
|
+
private cacheContent(key: string, content: string, metadata: any): void {
|
|
722
|
+
this.enforceCacheLimit(this.contentCache, this.MAX_CONTENT_CACHE_SIZE, this.CONTENT_CACHE_TTL);
|
|
723
|
+
this.contentCache.set(key, { content, metadata, timestamp: Date.now() });
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Cache analytics with size limit enforcement
|
|
728
|
+
*/
|
|
729
|
+
private cacheAnalytics(key: string, analytics: any): void {
|
|
730
|
+
this.enforceCacheLimit(this.analyticsCache, this.MAX_ANALYTICS_CACHE_SIZE, this.ANALYTICS_CACHE_TTL);
|
|
731
|
+
this.analyticsCache.set(key, { analytics, timestamp: Date.now() });
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Enforce cache size limit using LRU eviction (oldest entries first)
|
|
736
|
+
* Also removes expired entries based on TTL
|
|
737
|
+
*/
|
|
738
|
+
private enforceCacheLimit<T extends { timestamp: number }>(
|
|
739
|
+
cache: Map<string, T>,
|
|
740
|
+
maxSize: number,
|
|
741
|
+
ttl: number
|
|
742
|
+
): void {
|
|
743
|
+
// First, clean up expired entries
|
|
744
|
+
const now = Date.now();
|
|
745
|
+
|
|
746
|
+
for (const [key, value] of cache.entries()) {
|
|
747
|
+
if (now - value.timestamp > ttl) {
|
|
748
|
+
cache.delete(key);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// If still over limit, remove oldest entries (LRU)
|
|
753
|
+
if (cache.size >= maxSize) {
|
|
754
|
+
const entries = Array.from(cache.entries())
|
|
755
|
+
.sort((a, b) => a[1].timestamp - b[1].timestamp);
|
|
756
|
+
|
|
757
|
+
const toRemove = cache.size - maxSize + 1; // +1 to make room for new entry
|
|
758
|
+
for (let i = 0; i < toRemove && i < entries.length; i++) {
|
|
759
|
+
cache.delete(entries[i][0]);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
690
764
|
// ==================== UTILITY METHODS ====================
|
|
691
765
|
|
|
692
766
|
private calculateCacheHitRate(): number {
|
|
@@ -83,7 +83,7 @@ export interface EmbeddingConfig {
|
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
85
|
* Embedding dimensions (optional, provider-dependent)
|
|
86
|
-
* - Google: Up to
|
|
86
|
+
* - Google: Up to 3072
|
|
87
87
|
* - OpenAI: 256, 512, 1024, 1536, 3072 (depending on model)
|
|
88
88
|
* - OpenRouter: Depends on the underlying model
|
|
89
89
|
* - Cohere: Model-specific
|
|
@@ -704,3 +704,68 @@ export class EmbeddingService {
|
|
|
704
704
|
}
|
|
705
705
|
|
|
706
706
|
export default EmbeddingService;
|
|
707
|
+
|
|
708
|
+
// ==================== Singleton Pattern ====================
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Generate config key for singleton cache
|
|
712
|
+
*/
|
|
713
|
+
function getConfigKey(config: EmbeddingConfig): string {
|
|
714
|
+
const provider = config.provider || 'google';
|
|
715
|
+
const modelName = typeof config.model === 'string'
|
|
716
|
+
? config.model
|
|
717
|
+
: (config.modelName || 'default');
|
|
718
|
+
const dimensions = config.dimensions || 'default';
|
|
719
|
+
return `${provider}:${modelName}:${dimensions}`;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/** Singleton cache */
|
|
723
|
+
const sharedInstances = new Map<string, EmbeddingService>();
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Get or create a shared EmbeddingService instance (Singleton)
|
|
727
|
+
*
|
|
728
|
+
* All clients with same provider/model/dimensions share one instance.
|
|
729
|
+
* Reduces memory usage and connection overhead.
|
|
730
|
+
*
|
|
731
|
+
* @example
|
|
732
|
+
* ```typescript
|
|
733
|
+
* // Instead of: new EmbeddingService({ apiKey, modelName })
|
|
734
|
+
* const embedding = getSharedEmbeddingService({ apiKey, modelName });
|
|
735
|
+
* ```
|
|
736
|
+
*/
|
|
737
|
+
export function getSharedEmbeddingService(config: EmbeddingConfig): EmbeddingService {
|
|
738
|
+
const key = getConfigKey(config);
|
|
739
|
+
|
|
740
|
+
let instance = sharedInstances.get(key);
|
|
741
|
+
if (!instance) {
|
|
742
|
+
console.log(`🔧 [Singleton] Creating shared EmbeddingService: ${key}`);
|
|
743
|
+
instance = new EmbeddingService(config);
|
|
744
|
+
sharedInstances.set(key, instance);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
return instance;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Clear all shared instances (for testing)
|
|
752
|
+
*/
|
|
753
|
+
export function clearSharedEmbeddingServices(): void {
|
|
754
|
+
sharedInstances.clear();
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Get singleton stats
|
|
759
|
+
*/
|
|
760
|
+
export function getSharedEmbeddingStats(): {
|
|
761
|
+
instanceCount: number;
|
|
762
|
+
instances: Array<{ key: string; stats: ReturnType<EmbeddingService['getStats']> }>;
|
|
763
|
+
} {
|
|
764
|
+
return {
|
|
765
|
+
instanceCount: sharedInstances.size,
|
|
766
|
+
instances: Array.from(sharedInstances.entries()).map(([key, svc]) => ({
|
|
767
|
+
key,
|
|
768
|
+
stats: svc.getStats(),
|
|
769
|
+
})),
|
|
770
|
+
};
|
|
771
|
+
}
|
|
@@ -108,6 +108,10 @@ export interface IndexManagerOptions {
|
|
|
108
108
|
* 2. If failed, rebuild from blockchain + Walrus (slow but complete)
|
|
109
109
|
* 3. Sync any new memories since last save
|
|
110
110
|
* 4. Auto-save periodically
|
|
111
|
+
*
|
|
112
|
+
* Memory Optimization:
|
|
113
|
+
* - Removed duplicate vectorCache - now uses VectorService.getAllCachedVectors()
|
|
114
|
+
* - Vectors are only stored once in VectorService's HnswWasmService LRU cache
|
|
111
115
|
*/
|
|
112
116
|
export class IndexManager {
|
|
113
117
|
private vectorService: VectorService;
|
|
@@ -120,7 +124,7 @@ export class IndexManager {
|
|
|
120
124
|
};
|
|
121
125
|
private autoSaveTimer?: ReturnType<typeof setInterval>;
|
|
122
126
|
private indexStates: Map<string, IndexState> = new Map();
|
|
123
|
-
|
|
127
|
+
// Note: vectorCache removed - now using VectorService.getAllCachedVectors() to avoid duplication
|
|
124
128
|
|
|
125
129
|
// Storage adapter (can be replaced for non-browser environments)
|
|
126
130
|
private storage: {
|
|
@@ -417,16 +421,11 @@ export class IndexManager {
|
|
|
417
421
|
efConstruction: pkg.hnswConfig.efConstruction,
|
|
418
422
|
});
|
|
419
423
|
|
|
420
|
-
//
|
|
421
|
-
const vectorMap = new Map<number, number[]>();
|
|
422
|
-
this.vectorCache.set(spaceId, vectorMap);
|
|
423
|
-
|
|
424
|
-
// Restore vectors and metadata
|
|
424
|
+
// Restore vectors and metadata (VectorService caches vectors internally)
|
|
425
425
|
const metadataMap = new Map<number, any>(pkg.metadata);
|
|
426
426
|
|
|
427
427
|
for (const { vectorId, vector } of pkg.vectors) {
|
|
428
428
|
await this.vectorService.addVector(spaceId, vectorId, vector, metadataMap.get(vectorId));
|
|
429
|
-
vectorMap.set(vectorId, vector);
|
|
430
429
|
}
|
|
431
430
|
|
|
432
431
|
// Update index state
|
|
@@ -482,11 +481,7 @@ export class IndexManager {
|
|
|
482
481
|
efConstruction: 200,
|
|
483
482
|
});
|
|
484
483
|
|
|
485
|
-
//
|
|
486
|
-
const vectorMap = new Map<number, number[]>();
|
|
487
|
-
this.vectorCache.set(spaceId, vectorMap);
|
|
488
|
-
|
|
489
|
-
// Process each memory
|
|
484
|
+
// Process each memory (VectorService caches vectors internally)
|
|
490
485
|
let processed = 0;
|
|
491
486
|
const batchSize = 10;
|
|
492
487
|
|
|
@@ -514,7 +509,7 @@ export class IndexManager {
|
|
|
514
509
|
|
|
515
510
|
const vectorId = memory.vectorId ?? Date.now() % 4294967295;
|
|
516
511
|
|
|
517
|
-
// Add to index
|
|
512
|
+
// Add to index (VectorService caches vector internally)
|
|
518
513
|
await this.vectorService.addVector(spaceId, vectorId, embedding, {
|
|
519
514
|
blobId: memory.blobId,
|
|
520
515
|
memoryId: memory.id,
|
|
@@ -524,9 +519,6 @@ export class IndexManager {
|
|
|
524
519
|
timestamp: memory.createdAt || Date.now(),
|
|
525
520
|
});
|
|
526
521
|
|
|
527
|
-
// Cache vector for serialization
|
|
528
|
-
vectorMap.set(vectorId, embedding);
|
|
529
|
-
|
|
530
522
|
processed++;
|
|
531
523
|
} catch (error) {
|
|
532
524
|
console.warn(`Failed to rebuild memory ${memory.id}:`, error);
|
|
@@ -579,13 +571,6 @@ export class IndexManager {
|
|
|
579
571
|
|
|
580
572
|
console.log(`🔄 Syncing ${newMemories.length} new memories...`);
|
|
581
573
|
|
|
582
|
-
// Get or create vector cache
|
|
583
|
-
let vectorMap = this.vectorCache.get(spaceId);
|
|
584
|
-
if (!vectorMap) {
|
|
585
|
-
vectorMap = new Map();
|
|
586
|
-
this.vectorCache.set(spaceId, vectorMap);
|
|
587
|
-
}
|
|
588
|
-
|
|
589
574
|
let synced = 0;
|
|
590
575
|
|
|
591
576
|
for (const memory of newMemories) {
|
|
@@ -601,6 +586,7 @@ export class IndexManager {
|
|
|
601
586
|
|
|
602
587
|
const vectorId = memory.vectorId ?? Date.now() % 4294967295;
|
|
603
588
|
|
|
589
|
+
// Add to index (VectorService caches vector internally)
|
|
604
590
|
await this.vectorService.addVector(spaceId, vectorId, embedding, {
|
|
605
591
|
blobId: memory.blobId,
|
|
606
592
|
memoryId: memory.id,
|
|
@@ -610,7 +596,6 @@ export class IndexManager {
|
|
|
610
596
|
timestamp: memory.createdAt || Date.now(),
|
|
611
597
|
});
|
|
612
598
|
|
|
613
|
-
vectorMap.set(vectorId, embedding);
|
|
614
599
|
synced++;
|
|
615
600
|
} catch (error) {
|
|
616
601
|
console.warn(`Failed to sync memory ${memory.id}:`, error);
|
|
@@ -633,11 +618,8 @@ export class IndexManager {
|
|
|
633
618
|
// Get all vectors and metadata
|
|
634
619
|
const allVectors = this.vectorService.getAllVectors(spaceId);
|
|
635
620
|
|
|
636
|
-
//
|
|
637
|
-
|
|
638
|
-
if (vectorMap.size === 0) {
|
|
639
|
-
vectorMap = this.vectorCache.get(spaceId) || new Map();
|
|
640
|
-
}
|
|
621
|
+
// Get vectors from VectorService cache (single source of truth)
|
|
622
|
+
const vectorMap = this.vectorService.getAllCachedVectors(spaceId);
|
|
641
623
|
|
|
642
624
|
if (vectorMap.size === 0) {
|
|
643
625
|
console.warn('Vector cache is empty, cannot save index');
|
|
@@ -727,11 +709,8 @@ export class IndexManager {
|
|
|
727
709
|
|
|
728
710
|
const allVectors = this.vectorService.getAllVectors(spaceId);
|
|
729
711
|
|
|
730
|
-
//
|
|
731
|
-
|
|
732
|
-
if (vectorMap.size === 0) {
|
|
733
|
-
vectorMap = this.vectorCache.get(spaceId) || new Map();
|
|
734
|
-
}
|
|
712
|
+
// Get vectors from VectorService cache (single source of truth)
|
|
713
|
+
const vectorMap = this.vectorService.getAllCachedVectors(spaceId);
|
|
735
714
|
|
|
736
715
|
if (vectorMap.size === 0) {
|
|
737
716
|
console.warn('Vector cache is empty, cannot save index');
|
|
@@ -855,6 +834,7 @@ export class IndexManager {
|
|
|
855
834
|
|
|
856
835
|
/**
|
|
857
836
|
* Add vector and cache it for serialization
|
|
837
|
+
* Note: VectorService now handles caching internally, this method is kept for API compatibility
|
|
858
838
|
*/
|
|
859
839
|
async addVectorWithCache(
|
|
860
840
|
spaceId: string,
|
|
@@ -862,15 +842,8 @@ export class IndexManager {
|
|
|
862
842
|
vector: number[],
|
|
863
843
|
metadata?: any
|
|
864
844
|
): Promise<void> {
|
|
845
|
+
// VectorService.addVector now caches vectors internally
|
|
865
846
|
await this.vectorService.addVector(spaceId, vectorId, vector, metadata);
|
|
866
|
-
|
|
867
|
-
// Cache vector for later serialization
|
|
868
|
-
let vectorMap = this.vectorCache.get(spaceId);
|
|
869
|
-
if (!vectorMap) {
|
|
870
|
-
vectorMap = new Map();
|
|
871
|
-
this.vectorCache.set(spaceId, vectorMap);
|
|
872
|
-
}
|
|
873
|
-
vectorMap.set(vectorId, vector);
|
|
874
847
|
}
|
|
875
848
|
|
|
876
849
|
/**
|
|
@@ -940,7 +913,7 @@ export class IndexManager {
|
|
|
940
913
|
const key = `${this.options.storageKeyPrefix}${spaceId}`;
|
|
941
914
|
this.storage.removeItem(key);
|
|
942
915
|
this.indexStates.delete(spaceId);
|
|
943
|
-
|
|
916
|
+
// Note: VectorService cache is managed by HnswWasmService LRU cache
|
|
944
917
|
console.log(`Cleared index state for ${spaceId}`);
|
|
945
918
|
}
|
|
946
919
|
|
|
@@ -988,7 +961,7 @@ export class IndexManager {
|
|
|
988
961
|
} {
|
|
989
962
|
return {
|
|
990
963
|
indexState: this.getIndexState(spaceId),
|
|
991
|
-
vectorCacheSize: this.
|
|
964
|
+
vectorCacheSize: this.vectorService.getAllCachedVectors(spaceId).size,
|
|
992
965
|
isAutoSaveEnabled: !!this.autoSaveTimer,
|
|
993
966
|
};
|
|
994
967
|
}
|
|
@@ -999,6 +972,6 @@ export class IndexManager {
|
|
|
999
972
|
async cleanup(): Promise<void> {
|
|
1000
973
|
this.stopAutoSave();
|
|
1001
974
|
this.indexStates.clear();
|
|
1002
|
-
|
|
975
|
+
// Note: VectorService cleanup is handled by its own cleanup() method
|
|
1003
976
|
}
|
|
1004
977
|
}
|
|
@@ -136,7 +136,7 @@ export class QueryService {
|
|
|
136
136
|
console.log(` Model: ${stats.model}, Dimensions: ${stats.dimensions}`);
|
|
137
137
|
}
|
|
138
138
|
console.log(` Storage: ${!!storageService ? 'available' : 'not available'}`);
|
|
139
|
-
console.log(` Knowledge Graph: ${!!graphService ? 'available' : '
|
|
139
|
+
console.log(` Knowledge Graph: ${!!graphService ? 'available' : 'pending initialization...'}`);
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
/**
|
|
@@ -916,6 +916,21 @@ export class StorageService {
|
|
|
916
916
|
return this.quiltBatch.listQuiltPatches(quiltId);
|
|
917
917
|
}
|
|
918
918
|
|
|
919
|
+
/**
|
|
920
|
+
* Get all memory packages from a Quilt as JSON
|
|
921
|
+
* Used for indexing Quilt memories into local HNSW index
|
|
922
|
+
*/
|
|
923
|
+
async getAllMemoryPackages(quiltId: string) {
|
|
924
|
+
return this.quiltBatch.getAllMemoryPackages(quiltId);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Get the QuiltBatchManager instance for advanced operations
|
|
929
|
+
*/
|
|
930
|
+
get quiltBatchManager(): QuiltBatchManager {
|
|
931
|
+
return this.quiltBatch;
|
|
932
|
+
}
|
|
933
|
+
|
|
919
934
|
// ==================== BLOB ATTRIBUTES (Delegate to BlobAttributesManager) ====================
|
|
920
935
|
|
|
921
936
|
/**
|