@danielsimonjr/memory-mcp 0.48.0 → 9.8.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/LICENSE +22 -0
- package/README.md +2000 -194
- package/dist/__tests__/file-path.test.js +7 -11
- package/dist/__tests__/knowledge-graph.test.js +3 -8
- package/dist/core/EntityManager.d.ts +266 -0
- package/dist/core/EntityManager.d.ts.map +1 -0
- package/dist/core/EntityManager.js +85 -133
- package/dist/core/GraphEventEmitter.d.ts +202 -0
- package/dist/core/GraphEventEmitter.d.ts.map +1 -0
- package/dist/core/GraphEventEmitter.js +346 -0
- package/dist/core/GraphStorage.d.ts +395 -0
- package/dist/core/GraphStorage.d.ts.map +1 -0
- package/dist/core/GraphStorage.js +643 -31
- package/dist/core/GraphTraversal.d.ts +141 -0
- package/dist/core/GraphTraversal.d.ts.map +1 -0
- package/dist/core/GraphTraversal.js +573 -0
- package/dist/core/HierarchyManager.d.ts +111 -0
- package/dist/core/HierarchyManager.d.ts.map +1 -0
- package/dist/{features → core}/HierarchyManager.js +14 -9
- package/dist/core/ManagerContext.d.ts +72 -0
- package/dist/core/ManagerContext.d.ts.map +1 -0
- package/dist/core/ManagerContext.js +118 -0
- package/dist/core/ObservationManager.d.ts +85 -0
- package/dist/core/ObservationManager.d.ts.map +1 -0
- package/dist/core/ObservationManager.js +51 -57
- package/dist/core/RelationManager.d.ts +131 -0
- package/dist/core/RelationManager.d.ts.map +1 -0
- package/dist/core/RelationManager.js +31 -7
- package/dist/core/SQLiteStorage.d.ts +354 -0
- package/dist/core/SQLiteStorage.d.ts.map +1 -0
- package/dist/core/SQLiteStorage.js +917 -0
- package/dist/core/StorageFactory.d.ts +45 -0
- package/dist/core/StorageFactory.d.ts.map +1 -0
- package/dist/core/StorageFactory.js +64 -0
- package/dist/core/TransactionManager.d.ts +464 -0
- package/dist/core/TransactionManager.d.ts.map +1 -0
- package/dist/core/TransactionManager.js +490 -13
- package/dist/core/index.d.ts +17 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +12 -2
- package/dist/features/AnalyticsManager.d.ts +44 -0
- package/dist/features/AnalyticsManager.d.ts.map +1 -0
- package/dist/features/AnalyticsManager.js +3 -2
- package/dist/features/ArchiveManager.d.ts +133 -0
- package/dist/features/ArchiveManager.d.ts.map +1 -0
- package/dist/features/ArchiveManager.js +221 -14
- package/dist/features/CompressionManager.d.ts +117 -0
- package/dist/features/CompressionManager.d.ts.map +1 -0
- package/dist/features/CompressionManager.js +189 -20
- package/dist/features/IOManager.d.ts +225 -0
- package/dist/features/IOManager.d.ts.map +1 -0
- package/dist/features/IOManager.js +1041 -0
- package/dist/features/StreamingExporter.d.ts +123 -0
- package/dist/features/StreamingExporter.d.ts.map +1 -0
- package/dist/features/StreamingExporter.js +203 -0
- package/dist/features/TagManager.d.ts +147 -0
- package/dist/features/TagManager.d.ts.map +1 -0
- package/dist/features/index.d.ts +12 -0
- package/dist/features/index.d.ts.map +1 -0
- package/dist/features/index.js +5 -6
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -10
- package/dist/memory.jsonl +1 -26
- package/dist/search/BasicSearch.d.ts +51 -0
- package/dist/search/BasicSearch.d.ts.map +1 -0
- package/dist/search/BasicSearch.js +9 -3
- package/dist/search/BooleanSearch.d.ts +98 -0
- package/dist/search/BooleanSearch.d.ts.map +1 -0
- package/dist/search/BooleanSearch.js +156 -9
- package/dist/search/EmbeddingService.d.ts +178 -0
- package/dist/search/EmbeddingService.d.ts.map +1 -0
- package/dist/search/EmbeddingService.js +358 -0
- package/dist/search/FuzzySearch.d.ts +118 -0
- package/dist/search/FuzzySearch.d.ts.map +1 -0
- package/dist/search/FuzzySearch.js +241 -25
- package/dist/search/QueryCostEstimator.d.ts +111 -0
- package/dist/search/QueryCostEstimator.d.ts.map +1 -0
- package/dist/search/QueryCostEstimator.js +355 -0
- package/dist/search/RankedSearch.d.ts +71 -0
- package/dist/search/RankedSearch.d.ts.map +1 -0
- package/dist/search/RankedSearch.js +54 -6
- package/dist/search/SavedSearchManager.d.ts +79 -0
- package/dist/search/SavedSearchManager.d.ts.map +1 -0
- package/dist/search/SearchFilterChain.d.ts +120 -0
- package/dist/search/SearchFilterChain.d.ts.map +1 -0
- package/dist/search/SearchFilterChain.js +2 -4
- package/dist/search/SearchManager.d.ts +326 -0
- package/dist/search/SearchManager.d.ts.map +1 -0
- package/dist/search/SearchManager.js +148 -0
- package/dist/search/SearchSuggestions.d.ts +27 -0
- package/dist/search/SearchSuggestions.d.ts.map +1 -0
- package/dist/search/SearchSuggestions.js +1 -1
- package/dist/search/SemanticSearch.d.ts +149 -0
- package/dist/search/SemanticSearch.d.ts.map +1 -0
- package/dist/search/SemanticSearch.js +323 -0
- package/dist/search/TFIDFEventSync.d.ts +85 -0
- package/dist/search/TFIDFEventSync.d.ts.map +1 -0
- package/dist/search/TFIDFEventSync.js +133 -0
- package/dist/search/TFIDFIndexManager.d.ts +151 -0
- package/dist/search/TFIDFIndexManager.d.ts.map +1 -0
- package/dist/search/TFIDFIndexManager.js +232 -17
- package/dist/search/VectorStore.d.ts +235 -0
- package/dist/search/VectorStore.d.ts.map +1 -0
- package/dist/search/VectorStore.js +311 -0
- package/dist/search/index.d.ts +21 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +12 -0
- package/dist/server/MCPServer.d.ts +21 -0
- package/dist/server/MCPServer.d.ts.map +1 -0
- package/dist/server/MCPServer.js +4 -4
- package/dist/server/responseCompressor.d.ts +94 -0
- package/dist/server/responseCompressor.d.ts.map +1 -0
- package/dist/server/responseCompressor.js +127 -0
- package/dist/server/toolDefinitions.d.ts +27 -0
- package/dist/server/toolDefinitions.d.ts.map +1 -0
- package/dist/server/toolDefinitions.js +188 -17
- package/dist/server/toolHandlers.d.ts +41 -0
- package/dist/server/toolHandlers.d.ts.map +1 -0
- package/dist/server/toolHandlers.js +467 -75
- package/dist/types/index.d.ts +13 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -1
- package/dist/types/types.d.ts +1654 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/types.js +9 -0
- package/dist/utils/compressedCache.d.ts +192 -0
- package/dist/utils/compressedCache.d.ts.map +1 -0
- package/dist/utils/compressedCache.js +309 -0
- package/dist/utils/compressionUtil.d.ts +214 -0
- package/dist/utils/compressionUtil.d.ts.map +1 -0
- package/dist/utils/compressionUtil.js +247 -0
- package/dist/utils/constants.d.ts +245 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +124 -0
- package/dist/utils/entityUtils.d.ts +321 -0
- package/dist/utils/entityUtils.d.ts.map +1 -0
- package/dist/utils/entityUtils.js +434 -4
- package/dist/utils/errors.d.ts +95 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +24 -0
- package/dist/utils/formatters.d.ts +145 -0
- package/dist/utils/formatters.d.ts.map +1 -0
- package/dist/utils/{paginationUtils.js → formatters.js} +54 -3
- package/dist/utils/index.d.ts +23 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +69 -31
- package/dist/utils/indexes.d.ts +270 -0
- package/dist/utils/indexes.d.ts.map +1 -0
- package/dist/utils/indexes.js +526 -0
- package/dist/utils/logger.d.ts +24 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/operationUtils.d.ts +124 -0
- package/dist/utils/operationUtils.d.ts.map +1 -0
- package/dist/utils/operationUtils.js +175 -0
- package/dist/utils/parallelUtils.d.ts +72 -0
- package/dist/utils/parallelUtils.d.ts.map +1 -0
- package/dist/utils/parallelUtils.js +169 -0
- package/dist/utils/schemas.d.ts +374 -0
- package/dist/utils/schemas.d.ts.map +1 -0
- package/dist/utils/schemas.js +302 -2
- package/dist/utils/searchAlgorithms.d.ts +99 -0
- package/dist/utils/searchAlgorithms.d.ts.map +1 -0
- package/dist/utils/searchAlgorithms.js +167 -0
- package/dist/utils/searchCache.d.ts +108 -0
- package/dist/utils/searchCache.d.ts.map +1 -0
- package/dist/utils/taskScheduler.d.ts +290 -0
- package/dist/utils/taskScheduler.d.ts.map +1 -0
- package/dist/utils/taskScheduler.js +466 -0
- package/dist/workers/index.d.ts +12 -0
- package/dist/workers/index.d.ts.map +1 -0
- package/dist/workers/index.js +9 -0
- package/dist/workers/levenshteinWorker.d.ts +60 -0
- package/dist/workers/levenshteinWorker.d.ts.map +1 -0
- package/dist/workers/levenshteinWorker.js +98 -0
- package/package.json +17 -4
- package/dist/__tests__/edge-cases/edge-cases.test.js +0 -406
- package/dist/__tests__/integration/workflows.test.js +0 -449
- package/dist/__tests__/performance/benchmarks.test.js +0 -413
- package/dist/__tests__/unit/core/EntityManager.test.js +0 -334
- package/dist/__tests__/unit/core/GraphStorage.test.js +0 -205
- package/dist/__tests__/unit/core/RelationManager.test.js +0 -274
- package/dist/__tests__/unit/features/CompressionManager.test.js +0 -350
- package/dist/__tests__/unit/search/BasicSearch.test.js +0 -311
- package/dist/__tests__/unit/search/BooleanSearch.test.js +0 -432
- package/dist/__tests__/unit/search/FuzzySearch.test.js +0 -448
- package/dist/__tests__/unit/search/RankedSearch.test.js +0 -379
- package/dist/__tests__/unit/utils/levenshtein.test.js +0 -77
- package/dist/core/KnowledgeGraphManager.js +0 -423
- package/dist/features/BackupManager.js +0 -311
- package/dist/features/ExportManager.js +0 -305
- package/dist/features/ImportExportManager.js +0 -50
- package/dist/features/ImportManager.js +0 -328
- package/dist/memory-saved-searches.jsonl +0 -0
- package/dist/memory-tag-aliases.jsonl +0 -0
- package/dist/types/analytics.types.js +0 -6
- package/dist/types/entity.types.js +0 -7
- package/dist/types/import-export.types.js +0 -7
- package/dist/types/search.types.js +0 -7
- package/dist/types/tag.types.js +0 -6
- package/dist/utils/dateUtils.js +0 -89
- package/dist/utils/filterUtils.js +0 -155
- package/dist/utils/levenshtein.js +0 -62
- package/dist/utils/pathUtils.js +0 -115
- package/dist/utils/responseFormatter.js +0 -55
- package/dist/utils/tagUtils.js +0 -107
- package/dist/utils/tfidf.js +0 -90
- package/dist/utils/validationHelper.js +0 -99
- package/dist/utils/validationUtils.js +0 -109
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SearchSuggestions.d.ts","sourceRoot":"","sources":["../../src/search/SearchSuggestions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAW5D;;GAEG;AACH,qBAAa,iBAAiB;IAChB,OAAO,CAAC,OAAO;gBAAP,OAAO,EAAE,YAAY;IAEzC;;;;;;;;;OASG;IACG,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,cAAc,GAAE,MAAU,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAoCzF"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Semantic Search Manager
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 Sprint 12: Orchestrates embedding service and vector store
|
|
5
|
+
* to provide semantic similarity search capabilities.
|
|
6
|
+
*
|
|
7
|
+
* @module search/SemanticSearch
|
|
8
|
+
*/
|
|
9
|
+
import type { Entity, EmbeddingService, IVectorStore, SemanticSearchResult, SemanticIndexOptions, ReadonlyKnowledgeGraph } from '../types/index.js';
|
|
10
|
+
/**
|
|
11
|
+
* Convert an entity to a text representation for embedding.
|
|
12
|
+
*
|
|
13
|
+
* Creates a structured text that captures the entity's key information
|
|
14
|
+
* for generating meaningful embeddings.
|
|
15
|
+
*
|
|
16
|
+
* @param entity - Entity to convert
|
|
17
|
+
* @returns Text representation suitable for embedding
|
|
18
|
+
*/
|
|
19
|
+
export declare function entityToText(entity: Entity): string;
|
|
20
|
+
/**
|
|
21
|
+
* Semantic Search Manager
|
|
22
|
+
*
|
|
23
|
+
* Provides semantic similarity search by converting entities to embeddings
|
|
24
|
+
* and storing them in a vector store. Supports search by query text and
|
|
25
|
+
* finding similar entities.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const semanticSearch = new SemanticSearch(embeddingService, vectorStore);
|
|
30
|
+
* await semanticSearch.indexAll(graph);
|
|
31
|
+
* const results = await semanticSearch.search(graph, "machine learning");
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class SemanticSearch {
|
|
35
|
+
/** Embedding service for generating vectors */
|
|
36
|
+
private embeddingService;
|
|
37
|
+
/** Vector store for storing and searching embeddings */
|
|
38
|
+
private vectorStore;
|
|
39
|
+
/** Whether embeddings have been indexed */
|
|
40
|
+
private indexed;
|
|
41
|
+
/** Number of entities currently indexed */
|
|
42
|
+
private indexedCount;
|
|
43
|
+
/**
|
|
44
|
+
* Create a semantic search manager.
|
|
45
|
+
*
|
|
46
|
+
* @param embeddingService - Service for generating embeddings
|
|
47
|
+
* @param vectorStore - Store for vector storage and search
|
|
48
|
+
*/
|
|
49
|
+
constructor(embeddingService: EmbeddingService, vectorStore?: IVectorStore);
|
|
50
|
+
/**
|
|
51
|
+
* Index all entities in the knowledge graph.
|
|
52
|
+
*
|
|
53
|
+
* Generates embeddings for all entities and stores them in the vector store.
|
|
54
|
+
* Can be called incrementally - only indexes entities that aren't already indexed.
|
|
55
|
+
*
|
|
56
|
+
* Phase 9B: Supports cancellation via AbortSignal in options.
|
|
57
|
+
*
|
|
58
|
+
* @param graph - Knowledge graph to index
|
|
59
|
+
* @param options - Indexing options (includes signal for cancellation)
|
|
60
|
+
* @returns Index statistics
|
|
61
|
+
* @throws {OperationCancelledError} If operation is cancelled via signal (Phase 9B)
|
|
62
|
+
*/
|
|
63
|
+
indexAll(graph: ReadonlyKnowledgeGraph, options?: SemanticIndexOptions): Promise<{
|
|
64
|
+
indexed: number;
|
|
65
|
+
skipped: number;
|
|
66
|
+
errors: number;
|
|
67
|
+
}>;
|
|
68
|
+
/**
|
|
69
|
+
* Index a single entity.
|
|
70
|
+
*
|
|
71
|
+
* @param entity - Entity to index
|
|
72
|
+
* @returns True if indexed successfully
|
|
73
|
+
*/
|
|
74
|
+
indexEntity(entity: Entity): Promise<boolean>;
|
|
75
|
+
/**
|
|
76
|
+
* Remove an entity from the index.
|
|
77
|
+
*
|
|
78
|
+
* @param entityName - Name of entity to remove
|
|
79
|
+
* @returns True if found and removed
|
|
80
|
+
*/
|
|
81
|
+
removeEntity(entityName: string): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Search for entities semantically similar to a query.
|
|
84
|
+
*
|
|
85
|
+
* @param graph - Knowledge graph to search in
|
|
86
|
+
* @param query - Search query text
|
|
87
|
+
* @param limit - Maximum number of results (default: 10)
|
|
88
|
+
* @param minSimilarity - Minimum similarity threshold (default: 0)
|
|
89
|
+
* @returns Array of search results with similarity scores
|
|
90
|
+
*/
|
|
91
|
+
search(graph: ReadonlyKnowledgeGraph, query: string, limit?: number, minSimilarity?: number): Promise<SemanticSearchResult[]>;
|
|
92
|
+
/**
|
|
93
|
+
* Find entities similar to a given entity.
|
|
94
|
+
*
|
|
95
|
+
* @param graph - Knowledge graph to search in
|
|
96
|
+
* @param entityName - Name of entity to find similar entities for
|
|
97
|
+
* @param limit - Maximum number of results (default: 10)
|
|
98
|
+
* @param minSimilarity - Minimum similarity threshold (default: 0)
|
|
99
|
+
* @returns Array of search results with similarity scores
|
|
100
|
+
*/
|
|
101
|
+
findSimilar(graph: ReadonlyKnowledgeGraph, entityName: string, limit?: number, minSimilarity?: number): Promise<SemanticSearchResult[]>;
|
|
102
|
+
/**
|
|
103
|
+
* Get the embedding service.
|
|
104
|
+
*
|
|
105
|
+
* @returns Embedding service instance
|
|
106
|
+
*/
|
|
107
|
+
getEmbeddingService(): EmbeddingService;
|
|
108
|
+
/**
|
|
109
|
+
* Get the vector store.
|
|
110
|
+
*
|
|
111
|
+
* @returns Vector store instance
|
|
112
|
+
*/
|
|
113
|
+
getVectorStore(): IVectorStore;
|
|
114
|
+
/**
|
|
115
|
+
* Check if the index has been built.
|
|
116
|
+
*
|
|
117
|
+
* @returns True if indexAll has been called
|
|
118
|
+
*/
|
|
119
|
+
isIndexed(): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Get the number of indexed entities.
|
|
122
|
+
*
|
|
123
|
+
* @returns Number of entities in the vector store
|
|
124
|
+
*/
|
|
125
|
+
getIndexedCount(): number;
|
|
126
|
+
/**
|
|
127
|
+
* Clear all indexed embeddings.
|
|
128
|
+
*/
|
|
129
|
+
clearIndex(): void;
|
|
130
|
+
/**
|
|
131
|
+
* Check if semantic search is available.
|
|
132
|
+
*
|
|
133
|
+
* @returns True if embedding service is ready
|
|
134
|
+
*/
|
|
135
|
+
isAvailable(): Promise<boolean>;
|
|
136
|
+
/**
|
|
137
|
+
* Get semantic search statistics.
|
|
138
|
+
*
|
|
139
|
+
* @returns Statistics about the semantic search index
|
|
140
|
+
*/
|
|
141
|
+
getStats(): {
|
|
142
|
+
indexed: boolean;
|
|
143
|
+
indexedCount: number;
|
|
144
|
+
provider: string;
|
|
145
|
+
model: string;
|
|
146
|
+
dimensions: number;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=SemanticSearch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SemanticSearch.d.ts","sourceRoot":"","sources":["../../src/search/SemanticSearch.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,MAAM,EACN,gBAAgB,EAChB,YAAY,EACZ,oBAAoB,EACpB,oBAAoB,EACpB,sBAAsB,EACvB,MAAM,mBAAmB,CAAC;AAK3B;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAkBnD;AAED;;;;;;;;;;;;;GAaG;AACH,qBAAa,cAAc;IACzB,+CAA+C;IAC/C,OAAO,CAAC,gBAAgB,CAAmB;IAE3C,wDAAwD;IACxD,OAAO,CAAC,WAAW,CAAe;IAElC,2CAA2C;IAC3C,OAAO,CAAC,OAAO,CAAS;IAExB,2CAA2C;IAC3C,OAAO,CAAC,YAAY,CAAK;IAEzB;;;;;OAKG;gBACS,gBAAgB,EAAE,gBAAgB,EAAE,WAAW,CAAC,EAAE,YAAY;IAK1E;;;;;;;;;;;;OAYG;IACG,QAAQ,CACZ,KAAK,EAAE,sBAAsB,EAC7B,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;IAwEhE;;;;;OAKG;IACG,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAYnD;;;;;OAKG;IACH,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO;IAQzC;;;;;;;;OAQG;IACG,MAAM,CACV,KAAK,EAAE,sBAAsB,EAC7B,KAAK,EAAE,MAAM,EACb,KAAK,GAAE,MAA6C,EACpD,aAAa,GAAE,MAA8C,GAC5D,OAAO,CAAC,oBAAoB,EAAE,CAAC;IAsClC;;;;;;;;OAQG;IACG,WAAW,CACf,KAAK,EAAE,sBAAsB,EAC7B,UAAU,EAAE,MAAM,EAClB,KAAK,GAAE,MAA6C,EACpD,aAAa,GAAE,MAA8C,GAC5D,OAAO,CAAC,oBAAoB,EAAE,CAAC;IAoDlC;;;;OAIG;IACH,mBAAmB,IAAI,gBAAgB;IAIvC;;;;OAIG;IACH,cAAc,IAAI,YAAY;IAI9B;;;;OAIG;IACH,SAAS,IAAI,OAAO;IAIpB;;;;OAIG;IACH,eAAe,IAAI,MAAM;IAIzB;;OAEG;IACH,UAAU,IAAI,IAAI;IAMlB;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAIrC;;;;OAIG;IACH,QAAQ,IAAI;QACV,OAAO,EAAE,OAAO,CAAC;QACjB,YAAY,EAAE,MAAM,CAAC;QACrB,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;QACd,UAAU,EAAE,MAAM,CAAC;KACpB;CASF"}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Semantic Search Manager
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 Sprint 12: Orchestrates embedding service and vector store
|
|
5
|
+
* to provide semantic similarity search capabilities.
|
|
6
|
+
*
|
|
7
|
+
* @module search/SemanticSearch
|
|
8
|
+
*/
|
|
9
|
+
import { InMemoryVectorStore } from './VectorStore.js';
|
|
10
|
+
import { EMBEDDING_DEFAULTS, SEMANTIC_SEARCH_LIMITS } from '../utils/constants.js';
|
|
11
|
+
import { checkCancellation } from '../utils/index.js';
|
|
12
|
+
/**
|
|
13
|
+
* Convert an entity to a text representation for embedding.
|
|
14
|
+
*
|
|
15
|
+
* Creates a structured text that captures the entity's key information
|
|
16
|
+
* for generating meaningful embeddings.
|
|
17
|
+
*
|
|
18
|
+
* @param entity - Entity to convert
|
|
19
|
+
* @returns Text representation suitable for embedding
|
|
20
|
+
*/
|
|
21
|
+
export function entityToText(entity) {
|
|
22
|
+
const parts = [];
|
|
23
|
+
// Name and type are most important
|
|
24
|
+
parts.push(`${entity.name} (${entity.entityType})`);
|
|
25
|
+
// Add observations (limited to prevent overly long text)
|
|
26
|
+
if (entity.observations.length > 0) {
|
|
27
|
+
const observationText = entity.observations.slice(0, 10).join('. ');
|
|
28
|
+
parts.push(observationText);
|
|
29
|
+
}
|
|
30
|
+
// Add tags if present
|
|
31
|
+
if (entity.tags && entity.tags.length > 0) {
|
|
32
|
+
parts.push(`Tags: ${entity.tags.join(', ')}`);
|
|
33
|
+
}
|
|
34
|
+
return parts.join('\n');
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Semantic Search Manager
|
|
38
|
+
*
|
|
39
|
+
* Provides semantic similarity search by converting entities to embeddings
|
|
40
|
+
* and storing them in a vector store. Supports search by query text and
|
|
41
|
+
* finding similar entities.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const semanticSearch = new SemanticSearch(embeddingService, vectorStore);
|
|
46
|
+
* await semanticSearch.indexAll(graph);
|
|
47
|
+
* const results = await semanticSearch.search(graph, "machine learning");
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export class SemanticSearch {
|
|
51
|
+
/** Embedding service for generating vectors */
|
|
52
|
+
embeddingService;
|
|
53
|
+
/** Vector store for storing and searching embeddings */
|
|
54
|
+
vectorStore;
|
|
55
|
+
/** Whether embeddings have been indexed */
|
|
56
|
+
indexed = false;
|
|
57
|
+
/** Number of entities currently indexed */
|
|
58
|
+
indexedCount = 0;
|
|
59
|
+
/**
|
|
60
|
+
* Create a semantic search manager.
|
|
61
|
+
*
|
|
62
|
+
* @param embeddingService - Service for generating embeddings
|
|
63
|
+
* @param vectorStore - Store for vector storage and search
|
|
64
|
+
*/
|
|
65
|
+
constructor(embeddingService, vectorStore) {
|
|
66
|
+
this.embeddingService = embeddingService;
|
|
67
|
+
this.vectorStore = vectorStore || new InMemoryVectorStore();
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Index all entities in the knowledge graph.
|
|
71
|
+
*
|
|
72
|
+
* Generates embeddings for all entities and stores them in the vector store.
|
|
73
|
+
* Can be called incrementally - only indexes entities that aren't already indexed.
|
|
74
|
+
*
|
|
75
|
+
* Phase 9B: Supports cancellation via AbortSignal in options.
|
|
76
|
+
*
|
|
77
|
+
* @param graph - Knowledge graph to index
|
|
78
|
+
* @param options - Indexing options (includes signal for cancellation)
|
|
79
|
+
* @returns Index statistics
|
|
80
|
+
* @throws {OperationCancelledError} If operation is cancelled via signal (Phase 9B)
|
|
81
|
+
*/
|
|
82
|
+
async indexAll(graph, options = {}) {
|
|
83
|
+
const { forceReindex = false, onProgress, batchSize = EMBEDDING_DEFAULTS.DEFAULT_BATCH_SIZE, signal, } = options;
|
|
84
|
+
// Check for early cancellation
|
|
85
|
+
checkCancellation(signal, 'indexAll');
|
|
86
|
+
let indexed = 0;
|
|
87
|
+
let skipped = 0;
|
|
88
|
+
let errors = 0;
|
|
89
|
+
const entities = graph.entities;
|
|
90
|
+
const total = entities.length;
|
|
91
|
+
// Collect entities to index
|
|
92
|
+
const toIndex = [];
|
|
93
|
+
for (const entity of entities) {
|
|
94
|
+
if (forceReindex || !this.vectorStore.has(entity.name)) {
|
|
95
|
+
toIndex.push(entity);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
skipped++;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Process in batches
|
|
102
|
+
for (let i = 0; i < toIndex.length; i += batchSize) {
|
|
103
|
+
// Check for cancellation between batches
|
|
104
|
+
checkCancellation(signal, 'indexAll');
|
|
105
|
+
const batch = toIndex.slice(i, i + batchSize);
|
|
106
|
+
const texts = batch.map(entityToText);
|
|
107
|
+
try {
|
|
108
|
+
const embeddings = await this.embeddingService.embedBatch(texts);
|
|
109
|
+
for (let j = 0; j < batch.length; j++) {
|
|
110
|
+
this.vectorStore.add(batch[j].name, embeddings[j]);
|
|
111
|
+
indexed++;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
// Try individual embeddings on batch failure
|
|
116
|
+
for (const entity of batch) {
|
|
117
|
+
// Check for cancellation during fallback
|
|
118
|
+
checkCancellation(signal, 'indexAll');
|
|
119
|
+
try {
|
|
120
|
+
const text = entityToText(entity);
|
|
121
|
+
const embedding = await this.embeddingService.embed(text);
|
|
122
|
+
this.vectorStore.add(entity.name, embedding);
|
|
123
|
+
indexed++;
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
errors++;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Report progress
|
|
131
|
+
if (onProgress) {
|
|
132
|
+
onProgress(indexed + skipped + errors, total);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
this.indexed = true;
|
|
136
|
+
this.indexedCount = this.vectorStore.size();
|
|
137
|
+
return { indexed, skipped, errors };
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Index a single entity.
|
|
141
|
+
*
|
|
142
|
+
* @param entity - Entity to index
|
|
143
|
+
* @returns True if indexed successfully
|
|
144
|
+
*/
|
|
145
|
+
async indexEntity(entity) {
|
|
146
|
+
try {
|
|
147
|
+
const text = entityToText(entity);
|
|
148
|
+
const embedding = await this.embeddingService.embed(text);
|
|
149
|
+
this.vectorStore.add(entity.name, embedding);
|
|
150
|
+
this.indexedCount = this.vectorStore.size();
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Remove an entity from the index.
|
|
159
|
+
*
|
|
160
|
+
* @param entityName - Name of entity to remove
|
|
161
|
+
* @returns True if found and removed
|
|
162
|
+
*/
|
|
163
|
+
removeEntity(entityName) {
|
|
164
|
+
const removed = this.vectorStore.remove(entityName);
|
|
165
|
+
if (removed) {
|
|
166
|
+
this.indexedCount = this.vectorStore.size();
|
|
167
|
+
}
|
|
168
|
+
return removed;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Search for entities semantically similar to a query.
|
|
172
|
+
*
|
|
173
|
+
* @param graph - Knowledge graph to search in
|
|
174
|
+
* @param query - Search query text
|
|
175
|
+
* @param limit - Maximum number of results (default: 10)
|
|
176
|
+
* @param minSimilarity - Minimum similarity threshold (default: 0)
|
|
177
|
+
* @returns Array of search results with similarity scores
|
|
178
|
+
*/
|
|
179
|
+
async search(graph, query, limit = SEMANTIC_SEARCH_LIMITS.DEFAULT_LIMIT, minSimilarity = SEMANTIC_SEARCH_LIMITS.MIN_SIMILARITY) {
|
|
180
|
+
// Ensure limit is within bounds
|
|
181
|
+
const effectiveLimit = Math.min(limit, SEMANTIC_SEARCH_LIMITS.MAX_LIMIT);
|
|
182
|
+
// Generate embedding for query
|
|
183
|
+
const queryEmbedding = await this.embeddingService.embed(query);
|
|
184
|
+
// Search vector store
|
|
185
|
+
const vectorResults = this.vectorStore.search(queryEmbedding, effectiveLimit * 2); // Get extra for filtering
|
|
186
|
+
// Convert to SemanticSearchResult with entity lookup
|
|
187
|
+
const entityMap = new Map();
|
|
188
|
+
for (const entity of graph.entities) {
|
|
189
|
+
entityMap.set(entity.name, entity);
|
|
190
|
+
}
|
|
191
|
+
const results = [];
|
|
192
|
+
for (const result of vectorResults) {
|
|
193
|
+
if (result.score < minSimilarity) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const entity = entityMap.get(result.name);
|
|
197
|
+
if (entity) {
|
|
198
|
+
results.push({
|
|
199
|
+
entity,
|
|
200
|
+
similarity: result.score,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if (results.length >= effectiveLimit) {
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return results;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Find entities similar to a given entity.
|
|
211
|
+
*
|
|
212
|
+
* @param graph - Knowledge graph to search in
|
|
213
|
+
* @param entityName - Name of entity to find similar entities for
|
|
214
|
+
* @param limit - Maximum number of results (default: 10)
|
|
215
|
+
* @param minSimilarity - Minimum similarity threshold (default: 0)
|
|
216
|
+
* @returns Array of search results with similarity scores
|
|
217
|
+
*/
|
|
218
|
+
async findSimilar(graph, entityName, limit = SEMANTIC_SEARCH_LIMITS.DEFAULT_LIMIT, minSimilarity = SEMANTIC_SEARCH_LIMITS.MIN_SIMILARITY) {
|
|
219
|
+
// Get the entity's embedding
|
|
220
|
+
const embedding = this.vectorStore.get(entityName);
|
|
221
|
+
if (!embedding) {
|
|
222
|
+
// Try to find and index the entity
|
|
223
|
+
const entity = graph.entities.find(e => e.name === entityName);
|
|
224
|
+
if (entity) {
|
|
225
|
+
await this.indexEntity(entity);
|
|
226
|
+
return this.findSimilar(graph, entityName, limit, minSimilarity);
|
|
227
|
+
}
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
// Ensure limit is within bounds
|
|
231
|
+
const effectiveLimit = Math.min(limit, SEMANTIC_SEARCH_LIMITS.MAX_LIMIT);
|
|
232
|
+
// Search vector store (request extra to filter out self)
|
|
233
|
+
const vectorResults = this.vectorStore.search(embedding, effectiveLimit + 1);
|
|
234
|
+
// Convert to SemanticSearchResult with entity lookup
|
|
235
|
+
const entityMap = new Map();
|
|
236
|
+
for (const entity of graph.entities) {
|
|
237
|
+
entityMap.set(entity.name, entity);
|
|
238
|
+
}
|
|
239
|
+
const results = [];
|
|
240
|
+
for (const result of vectorResults) {
|
|
241
|
+
// Skip self
|
|
242
|
+
if (result.name === entityName) {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (result.score < minSimilarity) {
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
const entity = entityMap.get(result.name);
|
|
249
|
+
if (entity) {
|
|
250
|
+
results.push({
|
|
251
|
+
entity,
|
|
252
|
+
similarity: result.score,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
if (results.length >= effectiveLimit) {
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return results;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Get the embedding service.
|
|
263
|
+
*
|
|
264
|
+
* @returns Embedding service instance
|
|
265
|
+
*/
|
|
266
|
+
getEmbeddingService() {
|
|
267
|
+
return this.embeddingService;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Get the vector store.
|
|
271
|
+
*
|
|
272
|
+
* @returns Vector store instance
|
|
273
|
+
*/
|
|
274
|
+
getVectorStore() {
|
|
275
|
+
return this.vectorStore;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Check if the index has been built.
|
|
279
|
+
*
|
|
280
|
+
* @returns True if indexAll has been called
|
|
281
|
+
*/
|
|
282
|
+
isIndexed() {
|
|
283
|
+
return this.indexed;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Get the number of indexed entities.
|
|
287
|
+
*
|
|
288
|
+
* @returns Number of entities in the vector store
|
|
289
|
+
*/
|
|
290
|
+
getIndexedCount() {
|
|
291
|
+
return this.indexedCount;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Clear all indexed embeddings.
|
|
295
|
+
*/
|
|
296
|
+
clearIndex() {
|
|
297
|
+
this.vectorStore.clear();
|
|
298
|
+
this.indexed = false;
|
|
299
|
+
this.indexedCount = 0;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Check if semantic search is available.
|
|
303
|
+
*
|
|
304
|
+
* @returns True if embedding service is ready
|
|
305
|
+
*/
|
|
306
|
+
async isAvailable() {
|
|
307
|
+
return this.embeddingService.isReady();
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Get semantic search statistics.
|
|
311
|
+
*
|
|
312
|
+
* @returns Statistics about the semantic search index
|
|
313
|
+
*/
|
|
314
|
+
getStats() {
|
|
315
|
+
return {
|
|
316
|
+
indexed: this.indexed,
|
|
317
|
+
indexedCount: this.indexedCount,
|
|
318
|
+
provider: this.embeddingService.provider,
|
|
319
|
+
model: this.embeddingService.model,
|
|
320
|
+
dimensions: this.embeddingService.dimensions,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TF-IDF Event Sync
|
|
3
|
+
*
|
|
4
|
+
* Phase 10 Sprint 3: Hooks TFIDFIndexManager to graph events for automatic
|
|
5
|
+
* incremental index updates when entities change.
|
|
6
|
+
*
|
|
7
|
+
* @module search/TFIDFEventSync
|
|
8
|
+
*/
|
|
9
|
+
import type { GraphEventEmitter } from '../core/GraphEventEmitter.js';
|
|
10
|
+
import type { TFIDFIndexManager } from './TFIDFIndexManager.js';
|
|
11
|
+
import type { IGraphStorage } from '../types/index.js';
|
|
12
|
+
/**
|
|
13
|
+
* Phase 10 Sprint 3: Synchronizes TF-IDF index with graph changes via events.
|
|
14
|
+
*
|
|
15
|
+
* Listens to graph events and triggers incremental index updates automatically.
|
|
16
|
+
* More efficient than rebuilding the entire index on every change.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const storage = new GraphStorage('/data/memory.jsonl');
|
|
21
|
+
* const indexManager = new TFIDFIndexManager('/data');
|
|
22
|
+
*
|
|
23
|
+
* // Load or build index
|
|
24
|
+
* await indexManager.loadIndex();
|
|
25
|
+
*
|
|
26
|
+
* // Enable automatic sync
|
|
27
|
+
* const sync = new TFIDFEventSync(indexManager, storage.events, storage);
|
|
28
|
+
* sync.enable();
|
|
29
|
+
*
|
|
30
|
+
* // Now entities added to storage will automatically update the index
|
|
31
|
+
* await storage.appendEntity({ name: 'New', entityType: 'test', observations: [] });
|
|
32
|
+
*
|
|
33
|
+
* // Disable when done
|
|
34
|
+
* sync.disable();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class TFIDFEventSync {
|
|
38
|
+
private indexManager;
|
|
39
|
+
private eventEmitter;
|
|
40
|
+
private storage;
|
|
41
|
+
private unsubscribers;
|
|
42
|
+
private enabled;
|
|
43
|
+
/**
|
|
44
|
+
* Create a new TFIDFEventSync instance.
|
|
45
|
+
*
|
|
46
|
+
* @param indexManager - TFIDFIndexManager to sync
|
|
47
|
+
* @param eventEmitter - GraphEventEmitter to listen to
|
|
48
|
+
* @param storage - Storage to fetch entity data from (for updates)
|
|
49
|
+
*/
|
|
50
|
+
constructor(indexManager: TFIDFIndexManager, eventEmitter: GraphEventEmitter, storage: IGraphStorage);
|
|
51
|
+
/**
|
|
52
|
+
* Enable automatic index synchronization.
|
|
53
|
+
*
|
|
54
|
+
* Subscribes to entity:created, entity:updated, and entity:deleted events.
|
|
55
|
+
*/
|
|
56
|
+
enable(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Disable automatic index synchronization.
|
|
59
|
+
*
|
|
60
|
+
* Unsubscribes from all events.
|
|
61
|
+
*/
|
|
62
|
+
disable(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Check if synchronization is enabled.
|
|
65
|
+
*
|
|
66
|
+
* @returns True if enabled
|
|
67
|
+
*/
|
|
68
|
+
isEnabled(): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Handle entity:created event.
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
private handleEntityCreated;
|
|
74
|
+
/**
|
|
75
|
+
* Handle entity:updated event.
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
private handleEntityUpdated;
|
|
79
|
+
/**
|
|
80
|
+
* Handle entity:deleted event.
|
|
81
|
+
* @private
|
|
82
|
+
*/
|
|
83
|
+
private handleEntityDeleted;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=TFIDFEventSync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TFIDFEventSync.d.ts","sourceRoot":"","sources":["../../src/search/TFIDFEventSync.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAOvD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,YAAY,CAAoB;IACxC,OAAO,CAAC,YAAY,CAAoB;IACxC,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,aAAa,CAAyB;IAC9C,OAAO,CAAC,OAAO,CAAkB;IAEjC;;;;;;OAMG;gBAED,YAAY,EAAE,iBAAiB,EAC/B,YAAY,EAAE,iBAAiB,EAC/B,OAAO,EAAE,aAAa;IAOxB;;;;OAIG;IACH,MAAM,IAAI,IAAI;IAqBd;;;;OAIG;IACH,OAAO,IAAI,IAAI;IAYf;;;;OAIG;IACH,SAAS,IAAI,OAAO;IAIpB;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAY3B;;;OAGG;YACW,mBAAmB;IAkBjC;;;OAGG;IACH,OAAO,CAAC,mBAAmB;CAO5B"}
|