@danielsimonjr/memory-mcp 12.2.3 → 12.5.2
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/README.md +88 -102
- package/dist/index.js +6 -0
- package/dist/server/toolDefinitions.d.ts.map +1 -1
- package/dist/server/toolDefinitions.js +745 -3
- package/dist/server/toolHandlers.d.ts +1 -9
- package/dist/server/toolHandlers.d.ts.map +1 -1
- package/dist/server/toolHandlers.js +859 -11
- package/package.json +9 -6
- package/dist/core/EntityManager.d.ts +0 -263
- package/dist/core/EntityManager.d.ts.map +0 -1
- package/dist/core/EntityManager.js +0 -478
- package/dist/core/GraphStorage.d.ts +0 -344
- package/dist/core/GraphStorage.d.ts.map +0 -1
- package/dist/core/GraphStorage.js +0 -708
- package/dist/core/GraphTraversal.d.ts +0 -136
- package/dist/core/GraphTraversal.d.ts.map +0 -1
- package/dist/core/GraphTraversal.js +0 -556
- package/dist/core/HierarchyManager.d.ts +0 -111
- package/dist/core/HierarchyManager.d.ts.map +0 -1
- package/dist/core/HierarchyManager.js +0 -224
- package/dist/core/ManagerContext.d.ts +0 -72
- package/dist/core/ManagerContext.d.ts.map +0 -1
- package/dist/core/ManagerContext.js +0 -118
- package/dist/core/ObservationManager.d.ts +0 -85
- package/dist/core/ObservationManager.d.ts.map +0 -1
- package/dist/core/ObservationManager.js +0 -123
- package/dist/core/RelationManager.d.ts +0 -131
- package/dist/core/RelationManager.d.ts.map +0 -1
- package/dist/core/RelationManager.js +0 -210
- package/dist/core/SQLiteStorage.d.ts +0 -354
- package/dist/core/SQLiteStorage.d.ts.map +0 -1
- package/dist/core/SQLiteStorage.js +0 -917
- package/dist/core/StorageFactory.d.ts +0 -45
- package/dist/core/StorageFactory.d.ts.map +0 -1
- package/dist/core/StorageFactory.js +0 -64
- package/dist/core/TransactionManager.d.ts +0 -259
- package/dist/core/TransactionManager.d.ts.map +0 -1
- package/dist/core/TransactionManager.js +0 -392
- package/dist/core/index.d.ts +0 -16
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -17
- package/dist/features/AnalyticsManager.d.ts +0 -44
- package/dist/features/AnalyticsManager.d.ts.map +0 -1
- package/dist/features/AnalyticsManager.js +0 -223
- package/dist/features/ArchiveManager.d.ts +0 -121
- package/dist/features/ArchiveManager.d.ts.map +0 -1
- package/dist/features/ArchiveManager.js +0 -240
- package/dist/features/CompressionManager.d.ts +0 -109
- package/dist/features/CompressionManager.d.ts.map +0 -1
- package/dist/features/CompressionManager.js +0 -404
- package/dist/features/IOManager.d.ts +0 -221
- package/dist/features/IOManager.d.ts.map +0 -1
- package/dist/features/IOManager.js +0 -997
- package/dist/features/StreamingExporter.d.ts +0 -105
- package/dist/features/StreamingExporter.d.ts.map +0 -1
- package/dist/features/StreamingExporter.js +0 -150
- package/dist/features/TagManager.d.ts +0 -147
- package/dist/features/TagManager.d.ts.map +0 -1
- package/dist/features/TagManager.js +0 -210
- package/dist/features/index.d.ts +0 -12
- package/dist/features/index.d.ts.map +0 -1
- package/dist/features/index.js +0 -11
- package/dist/search/BasicSearch.d.ts +0 -51
- package/dist/search/BasicSearch.d.ts.map +0 -1
- package/dist/search/BasicSearch.js +0 -137
- package/dist/search/BooleanSearch.d.ts +0 -98
- package/dist/search/BooleanSearch.d.ts.map +0 -1
- package/dist/search/BooleanSearch.js +0 -430
- package/dist/search/EmbeddingService.d.ts +0 -178
- package/dist/search/EmbeddingService.d.ts.map +0 -1
- package/dist/search/EmbeddingService.js +0 -358
- package/dist/search/FuzzySearch.d.ts +0 -118
- package/dist/search/FuzzySearch.d.ts.map +0 -1
- package/dist/search/FuzzySearch.js +0 -312
- package/dist/search/RankedSearch.d.ts +0 -71
- package/dist/search/RankedSearch.d.ts.map +0 -1
- package/dist/search/RankedSearch.js +0 -238
- package/dist/search/SavedSearchManager.d.ts +0 -79
- package/dist/search/SavedSearchManager.d.ts.map +0 -1
- package/dist/search/SavedSearchManager.js +0 -145
- package/dist/search/SearchFilterChain.d.ts +0 -120
- package/dist/search/SearchFilterChain.d.ts.map +0 -1
- package/dist/search/SearchFilterChain.js +0 -185
- package/dist/search/SearchManager.d.ts +0 -285
- package/dist/search/SearchManager.d.ts.map +0 -1
- package/dist/search/SearchManager.js +0 -338
- package/dist/search/SearchSuggestions.d.ts +0 -27
- package/dist/search/SearchSuggestions.d.ts.map +0 -1
- package/dist/search/SearchSuggestions.js +0 -57
- package/dist/search/SemanticSearch.d.ts +0 -146
- package/dist/search/SemanticSearch.d.ts.map +0 -1
- package/dist/search/SemanticSearch.js +0 -313
- package/dist/search/TFIDFIndexManager.d.ts +0 -63
- package/dist/search/TFIDFIndexManager.d.ts.map +0 -1
- package/dist/search/TFIDFIndexManager.js +0 -215
- package/dist/search/VectorStore.d.ts +0 -235
- package/dist/search/VectorStore.d.ts.map +0 -1
- package/dist/search/VectorStore.js +0 -311
- package/dist/search/index.d.ts +0 -18
- package/dist/search/index.d.ts.map +0 -1
- package/dist/search/index.js +0 -21
- package/dist/types/index.d.ts +0 -13
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -12
- package/dist/types/types.d.ts +0 -1234
- package/dist/types/types.d.ts.map +0 -1
- package/dist/types/types.js +0 -9
- package/dist/utils/compressedCache.d.ts +0 -192
- package/dist/utils/compressedCache.d.ts.map +0 -1
- package/dist/utils/compressedCache.js +0 -309
- package/dist/utils/compressionUtil.d.ts +0 -214
- package/dist/utils/compressionUtil.d.ts.map +0 -1
- package/dist/utils/compressionUtil.js +0 -247
- package/dist/utils/constants.d.ts +0 -245
- package/dist/utils/constants.d.ts.map +0 -1
- package/dist/utils/constants.js +0 -252
- package/dist/utils/entityUtils.d.ts +0 -321
- package/dist/utils/entityUtils.d.ts.map +0 -1
- package/dist/utils/entityUtils.js +0 -538
- package/dist/utils/errors.d.ts +0 -77
- package/dist/utils/errors.d.ts.map +0 -1
- package/dist/utils/errors.js +0 -121
- package/dist/utils/formatters.d.ts +0 -145
- package/dist/utils/formatters.d.ts.map +0 -1
- package/dist/utils/formatters.js +0 -132
- package/dist/utils/index.d.ts +0 -22
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -75
- package/dist/utils/indexes.d.ts +0 -270
- package/dist/utils/indexes.d.ts.map +0 -1
- package/dist/utils/indexes.js +0 -526
- package/dist/utils/logger.d.ts +0 -24
- package/dist/utils/logger.d.ts.map +0 -1
- package/dist/utils/logger.js +0 -33
- package/dist/utils/parallelUtils.d.ts +0 -72
- package/dist/utils/parallelUtils.d.ts.map +0 -1
- package/dist/utils/parallelUtils.js +0 -169
- package/dist/utils/schemas.d.ts +0 -374
- package/dist/utils/schemas.d.ts.map +0 -1
- package/dist/utils/schemas.js +0 -484
- package/dist/utils/searchAlgorithms.d.ts +0 -99
- package/dist/utils/searchAlgorithms.d.ts.map +0 -1
- package/dist/utils/searchAlgorithms.js +0 -167
- package/dist/utils/searchCache.d.ts +0 -108
- package/dist/utils/searchCache.d.ts.map +0 -1
- package/dist/utils/searchCache.js +0 -209
- package/dist/utils/taskScheduler.d.ts +0 -290
- package/dist/utils/taskScheduler.d.ts.map +0 -1
- package/dist/utils/taskScheduler.js +0 -466
- package/dist/workers/WorkerPool.d.ts +0 -81
- package/dist/workers/WorkerPool.d.ts.map +0 -1
- package/dist/workers/WorkerPool.js +0 -121
- package/dist/workers/index.d.ts +0 -12
- package/dist/workers/index.d.ts.map +0 -1
- package/dist/workers/index.js +0 -9
- package/dist/workers/levenshteinWorker.d.ts +0 -60
- package/dist/workers/levenshteinWorker.d.ts.map +0 -1
- package/dist/workers/levenshteinWorker.js +0 -98
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Search Suggestions
|
|
3
|
-
*
|
|
4
|
-
* Provides "did you mean?" suggestions using Levenshtein distance.
|
|
5
|
-
*
|
|
6
|
-
* @module search/SearchSuggestions
|
|
7
|
-
*/
|
|
8
|
-
import { levenshteinDistance } from '../utils/index.js';
|
|
9
|
-
/**
|
|
10
|
-
* Generates search suggestions based on entity names and types.
|
|
11
|
-
*/
|
|
12
|
-
export class SearchSuggestions {
|
|
13
|
-
storage;
|
|
14
|
-
constructor(storage) {
|
|
15
|
-
this.storage = storage;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Get "did you mean?" suggestions for a query.
|
|
19
|
-
*
|
|
20
|
-
* Returns similar entity names and types that might be what the user intended.
|
|
21
|
-
* Excludes exact matches (similarity < 1.0) and very dissimilar strings (similarity > 0.5).
|
|
22
|
-
*
|
|
23
|
-
* @param query - The search query
|
|
24
|
-
* @param maxSuggestions - Maximum number of suggestions to return (default 5)
|
|
25
|
-
* @returns Array of suggested entity/type names sorted by similarity
|
|
26
|
-
*/
|
|
27
|
-
async getSearchSuggestions(query, maxSuggestions = 5) {
|
|
28
|
-
const graph = await this.storage.loadGraph();
|
|
29
|
-
const queryLower = query.toLowerCase();
|
|
30
|
-
const suggestions = [];
|
|
31
|
-
// Check entity names
|
|
32
|
-
for (const entity of graph.entities) {
|
|
33
|
-
const distance = levenshteinDistance(queryLower, entity.name.toLowerCase());
|
|
34
|
-
const maxLength = Math.max(queryLower.length, entity.name.length);
|
|
35
|
-
const similarity = 1 - distance / maxLength;
|
|
36
|
-
if (similarity > 0.5 && similarity < 1.0) {
|
|
37
|
-
// Not exact match but similar
|
|
38
|
-
suggestions.push({ text: entity.name, similarity });
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
// Check entity types
|
|
42
|
-
const uniqueTypes = [...new Set(graph.entities.map(e => e.entityType))];
|
|
43
|
-
for (const type of uniqueTypes) {
|
|
44
|
-
const distance = levenshteinDistance(queryLower, type.toLowerCase());
|
|
45
|
-
const maxLength = Math.max(queryLower.length, type.length);
|
|
46
|
-
const similarity = 1 - distance / maxLength;
|
|
47
|
-
if (similarity > 0.5 && similarity < 1.0) {
|
|
48
|
-
suggestions.push({ text: type, similarity });
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
// Sort by similarity and return top suggestions
|
|
52
|
-
return suggestions
|
|
53
|
-
.sort((a, b) => b.similarity - a.similarity)
|
|
54
|
-
.slice(0, maxSuggestions)
|
|
55
|
-
.map(s => s.text);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
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
|
-
* @param graph - Knowledge graph to index
|
|
57
|
-
* @param options - Indexing options
|
|
58
|
-
* @returns Index statistics
|
|
59
|
-
*/
|
|
60
|
-
indexAll(graph: ReadonlyKnowledgeGraph, options?: SemanticIndexOptions): Promise<{
|
|
61
|
-
indexed: number;
|
|
62
|
-
skipped: number;
|
|
63
|
-
errors: number;
|
|
64
|
-
}>;
|
|
65
|
-
/**
|
|
66
|
-
* Index a single entity.
|
|
67
|
-
*
|
|
68
|
-
* @param entity - Entity to index
|
|
69
|
-
* @returns True if indexed successfully
|
|
70
|
-
*/
|
|
71
|
-
indexEntity(entity: Entity): Promise<boolean>;
|
|
72
|
-
/**
|
|
73
|
-
* Remove an entity from the index.
|
|
74
|
-
*
|
|
75
|
-
* @param entityName - Name of entity to remove
|
|
76
|
-
* @returns True if found and removed
|
|
77
|
-
*/
|
|
78
|
-
removeEntity(entityName: string): boolean;
|
|
79
|
-
/**
|
|
80
|
-
* Search for entities semantically similar to a query.
|
|
81
|
-
*
|
|
82
|
-
* @param graph - Knowledge graph to search in
|
|
83
|
-
* @param query - Search query text
|
|
84
|
-
* @param limit - Maximum number of results (default: 10)
|
|
85
|
-
* @param minSimilarity - Minimum similarity threshold (default: 0)
|
|
86
|
-
* @returns Array of search results with similarity scores
|
|
87
|
-
*/
|
|
88
|
-
search(graph: ReadonlyKnowledgeGraph, query: string, limit?: number, minSimilarity?: number): Promise<SemanticSearchResult[]>;
|
|
89
|
-
/**
|
|
90
|
-
* Find entities similar to a given entity.
|
|
91
|
-
*
|
|
92
|
-
* @param graph - Knowledge graph to search in
|
|
93
|
-
* @param entityName - Name of entity to find similar entities for
|
|
94
|
-
* @param limit - Maximum number of results (default: 10)
|
|
95
|
-
* @param minSimilarity - Minimum similarity threshold (default: 0)
|
|
96
|
-
* @returns Array of search results with similarity scores
|
|
97
|
-
*/
|
|
98
|
-
findSimilar(graph: ReadonlyKnowledgeGraph, entityName: string, limit?: number, minSimilarity?: number): Promise<SemanticSearchResult[]>;
|
|
99
|
-
/**
|
|
100
|
-
* Get the embedding service.
|
|
101
|
-
*
|
|
102
|
-
* @returns Embedding service instance
|
|
103
|
-
*/
|
|
104
|
-
getEmbeddingService(): EmbeddingService;
|
|
105
|
-
/**
|
|
106
|
-
* Get the vector store.
|
|
107
|
-
*
|
|
108
|
-
* @returns Vector store instance
|
|
109
|
-
*/
|
|
110
|
-
getVectorStore(): IVectorStore;
|
|
111
|
-
/**
|
|
112
|
-
* Check if the index has been built.
|
|
113
|
-
*
|
|
114
|
-
* @returns True if indexAll has been called
|
|
115
|
-
*/
|
|
116
|
-
isIndexed(): boolean;
|
|
117
|
-
/**
|
|
118
|
-
* Get the number of indexed entities.
|
|
119
|
-
*
|
|
120
|
-
* @returns Number of entities in the vector store
|
|
121
|
-
*/
|
|
122
|
-
getIndexedCount(): number;
|
|
123
|
-
/**
|
|
124
|
-
* Clear all indexed embeddings.
|
|
125
|
-
*/
|
|
126
|
-
clearIndex(): void;
|
|
127
|
-
/**
|
|
128
|
-
* Check if semantic search is available.
|
|
129
|
-
*
|
|
130
|
-
* @returns True if embedding service is ready
|
|
131
|
-
*/
|
|
132
|
-
isAvailable(): Promise<boolean>;
|
|
133
|
-
/**
|
|
134
|
-
* Get semantic search statistics.
|
|
135
|
-
*
|
|
136
|
-
* @returns Statistics about the semantic search index
|
|
137
|
-
*/
|
|
138
|
-
getStats(): {
|
|
139
|
-
indexed: boolean;
|
|
140
|
-
indexedCount: number;
|
|
141
|
-
provider: string;
|
|
142
|
-
model: string;
|
|
143
|
-
dimensions: number;
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
//# sourceMappingURL=SemanticSearch.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAI3B;;;;;;;;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;;;;;;;;;OASG;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;IA8DhE;;;;;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"}
|
|
@@ -1,313 +0,0 @@
|
|
|
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
|
-
/**
|
|
12
|
-
* Convert an entity to a text representation for embedding.
|
|
13
|
-
*
|
|
14
|
-
* Creates a structured text that captures the entity's key information
|
|
15
|
-
* for generating meaningful embeddings.
|
|
16
|
-
*
|
|
17
|
-
* @param entity - Entity to convert
|
|
18
|
-
* @returns Text representation suitable for embedding
|
|
19
|
-
*/
|
|
20
|
-
export function entityToText(entity) {
|
|
21
|
-
const parts = [];
|
|
22
|
-
// Name and type are most important
|
|
23
|
-
parts.push(`${entity.name} (${entity.entityType})`);
|
|
24
|
-
// Add observations (limited to prevent overly long text)
|
|
25
|
-
if (entity.observations.length > 0) {
|
|
26
|
-
const observationText = entity.observations.slice(0, 10).join('. ');
|
|
27
|
-
parts.push(observationText);
|
|
28
|
-
}
|
|
29
|
-
// Add tags if present
|
|
30
|
-
if (entity.tags && entity.tags.length > 0) {
|
|
31
|
-
parts.push(`Tags: ${entity.tags.join(', ')}`);
|
|
32
|
-
}
|
|
33
|
-
return parts.join('\n');
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Semantic Search Manager
|
|
37
|
-
*
|
|
38
|
-
* Provides semantic similarity search by converting entities to embeddings
|
|
39
|
-
* and storing them in a vector store. Supports search by query text and
|
|
40
|
-
* finding similar entities.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* const semanticSearch = new SemanticSearch(embeddingService, vectorStore);
|
|
45
|
-
* await semanticSearch.indexAll(graph);
|
|
46
|
-
* const results = await semanticSearch.search(graph, "machine learning");
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
export class SemanticSearch {
|
|
50
|
-
/** Embedding service for generating vectors */
|
|
51
|
-
embeddingService;
|
|
52
|
-
/** Vector store for storing and searching embeddings */
|
|
53
|
-
vectorStore;
|
|
54
|
-
/** Whether embeddings have been indexed */
|
|
55
|
-
indexed = false;
|
|
56
|
-
/** Number of entities currently indexed */
|
|
57
|
-
indexedCount = 0;
|
|
58
|
-
/**
|
|
59
|
-
* Create a semantic search manager.
|
|
60
|
-
*
|
|
61
|
-
* @param embeddingService - Service for generating embeddings
|
|
62
|
-
* @param vectorStore - Store for vector storage and search
|
|
63
|
-
*/
|
|
64
|
-
constructor(embeddingService, vectorStore) {
|
|
65
|
-
this.embeddingService = embeddingService;
|
|
66
|
-
this.vectorStore = vectorStore || new InMemoryVectorStore();
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Index all entities in the knowledge graph.
|
|
70
|
-
*
|
|
71
|
-
* Generates embeddings for all entities and stores them in the vector store.
|
|
72
|
-
* Can be called incrementally - only indexes entities that aren't already indexed.
|
|
73
|
-
*
|
|
74
|
-
* @param graph - Knowledge graph to index
|
|
75
|
-
* @param options - Indexing options
|
|
76
|
-
* @returns Index statistics
|
|
77
|
-
*/
|
|
78
|
-
async indexAll(graph, options = {}) {
|
|
79
|
-
const { forceReindex = false, onProgress, batchSize = EMBEDDING_DEFAULTS.DEFAULT_BATCH_SIZE, } = options;
|
|
80
|
-
let indexed = 0;
|
|
81
|
-
let skipped = 0;
|
|
82
|
-
let errors = 0;
|
|
83
|
-
const entities = graph.entities;
|
|
84
|
-
const total = entities.length;
|
|
85
|
-
// Collect entities to index
|
|
86
|
-
const toIndex = [];
|
|
87
|
-
for (const entity of entities) {
|
|
88
|
-
if (forceReindex || !this.vectorStore.has(entity.name)) {
|
|
89
|
-
toIndex.push(entity);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
skipped++;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
// Process in batches
|
|
96
|
-
for (let i = 0; i < toIndex.length; i += batchSize) {
|
|
97
|
-
const batch = toIndex.slice(i, i + batchSize);
|
|
98
|
-
const texts = batch.map(entityToText);
|
|
99
|
-
try {
|
|
100
|
-
const embeddings = await this.embeddingService.embedBatch(texts);
|
|
101
|
-
for (let j = 0; j < batch.length; j++) {
|
|
102
|
-
this.vectorStore.add(batch[j].name, embeddings[j]);
|
|
103
|
-
indexed++;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
catch (error) {
|
|
107
|
-
// Try individual embeddings on batch failure
|
|
108
|
-
for (const entity of batch) {
|
|
109
|
-
try {
|
|
110
|
-
const text = entityToText(entity);
|
|
111
|
-
const embedding = await this.embeddingService.embed(text);
|
|
112
|
-
this.vectorStore.add(entity.name, embedding);
|
|
113
|
-
indexed++;
|
|
114
|
-
}
|
|
115
|
-
catch {
|
|
116
|
-
errors++;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
// Report progress
|
|
121
|
-
if (onProgress) {
|
|
122
|
-
onProgress(indexed + skipped + errors, total);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
this.indexed = true;
|
|
126
|
-
this.indexedCount = this.vectorStore.size();
|
|
127
|
-
return { indexed, skipped, errors };
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Index a single entity.
|
|
131
|
-
*
|
|
132
|
-
* @param entity - Entity to index
|
|
133
|
-
* @returns True if indexed successfully
|
|
134
|
-
*/
|
|
135
|
-
async indexEntity(entity) {
|
|
136
|
-
try {
|
|
137
|
-
const text = entityToText(entity);
|
|
138
|
-
const embedding = await this.embeddingService.embed(text);
|
|
139
|
-
this.vectorStore.add(entity.name, embedding);
|
|
140
|
-
this.indexedCount = this.vectorStore.size();
|
|
141
|
-
return true;
|
|
142
|
-
}
|
|
143
|
-
catch {
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Remove an entity from the index.
|
|
149
|
-
*
|
|
150
|
-
* @param entityName - Name of entity to remove
|
|
151
|
-
* @returns True if found and removed
|
|
152
|
-
*/
|
|
153
|
-
removeEntity(entityName) {
|
|
154
|
-
const removed = this.vectorStore.remove(entityName);
|
|
155
|
-
if (removed) {
|
|
156
|
-
this.indexedCount = this.vectorStore.size();
|
|
157
|
-
}
|
|
158
|
-
return removed;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Search for entities semantically similar to a query.
|
|
162
|
-
*
|
|
163
|
-
* @param graph - Knowledge graph to search in
|
|
164
|
-
* @param query - Search query text
|
|
165
|
-
* @param limit - Maximum number of results (default: 10)
|
|
166
|
-
* @param minSimilarity - Minimum similarity threshold (default: 0)
|
|
167
|
-
* @returns Array of search results with similarity scores
|
|
168
|
-
*/
|
|
169
|
-
async search(graph, query, limit = SEMANTIC_SEARCH_LIMITS.DEFAULT_LIMIT, minSimilarity = SEMANTIC_SEARCH_LIMITS.MIN_SIMILARITY) {
|
|
170
|
-
// Ensure limit is within bounds
|
|
171
|
-
const effectiveLimit = Math.min(limit, SEMANTIC_SEARCH_LIMITS.MAX_LIMIT);
|
|
172
|
-
// Generate embedding for query
|
|
173
|
-
const queryEmbedding = await this.embeddingService.embed(query);
|
|
174
|
-
// Search vector store
|
|
175
|
-
const vectorResults = this.vectorStore.search(queryEmbedding, effectiveLimit * 2); // Get extra for filtering
|
|
176
|
-
// Convert to SemanticSearchResult with entity lookup
|
|
177
|
-
const entityMap = new Map();
|
|
178
|
-
for (const entity of graph.entities) {
|
|
179
|
-
entityMap.set(entity.name, entity);
|
|
180
|
-
}
|
|
181
|
-
const results = [];
|
|
182
|
-
for (const result of vectorResults) {
|
|
183
|
-
if (result.score < minSimilarity) {
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
const entity = entityMap.get(result.name);
|
|
187
|
-
if (entity) {
|
|
188
|
-
results.push({
|
|
189
|
-
entity,
|
|
190
|
-
similarity: result.score,
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
if (results.length >= effectiveLimit) {
|
|
194
|
-
break;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return results;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Find entities similar to a given entity.
|
|
201
|
-
*
|
|
202
|
-
* @param graph - Knowledge graph to search in
|
|
203
|
-
* @param entityName - Name of entity to find similar entities for
|
|
204
|
-
* @param limit - Maximum number of results (default: 10)
|
|
205
|
-
* @param minSimilarity - Minimum similarity threshold (default: 0)
|
|
206
|
-
* @returns Array of search results with similarity scores
|
|
207
|
-
*/
|
|
208
|
-
async findSimilar(graph, entityName, limit = SEMANTIC_SEARCH_LIMITS.DEFAULT_LIMIT, minSimilarity = SEMANTIC_SEARCH_LIMITS.MIN_SIMILARITY) {
|
|
209
|
-
// Get the entity's embedding
|
|
210
|
-
const embedding = this.vectorStore.get(entityName);
|
|
211
|
-
if (!embedding) {
|
|
212
|
-
// Try to find and index the entity
|
|
213
|
-
const entity = graph.entities.find(e => e.name === entityName);
|
|
214
|
-
if (entity) {
|
|
215
|
-
await this.indexEntity(entity);
|
|
216
|
-
return this.findSimilar(graph, entityName, limit, minSimilarity);
|
|
217
|
-
}
|
|
218
|
-
return [];
|
|
219
|
-
}
|
|
220
|
-
// Ensure limit is within bounds
|
|
221
|
-
const effectiveLimit = Math.min(limit, SEMANTIC_SEARCH_LIMITS.MAX_LIMIT);
|
|
222
|
-
// Search vector store (request extra to filter out self)
|
|
223
|
-
const vectorResults = this.vectorStore.search(embedding, effectiveLimit + 1);
|
|
224
|
-
// Convert to SemanticSearchResult with entity lookup
|
|
225
|
-
const entityMap = new Map();
|
|
226
|
-
for (const entity of graph.entities) {
|
|
227
|
-
entityMap.set(entity.name, entity);
|
|
228
|
-
}
|
|
229
|
-
const results = [];
|
|
230
|
-
for (const result of vectorResults) {
|
|
231
|
-
// Skip self
|
|
232
|
-
if (result.name === entityName) {
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
|
-
if (result.score < minSimilarity) {
|
|
236
|
-
continue;
|
|
237
|
-
}
|
|
238
|
-
const entity = entityMap.get(result.name);
|
|
239
|
-
if (entity) {
|
|
240
|
-
results.push({
|
|
241
|
-
entity,
|
|
242
|
-
similarity: result.score,
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
if (results.length >= effectiveLimit) {
|
|
246
|
-
break;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
return results;
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Get the embedding service.
|
|
253
|
-
*
|
|
254
|
-
* @returns Embedding service instance
|
|
255
|
-
*/
|
|
256
|
-
getEmbeddingService() {
|
|
257
|
-
return this.embeddingService;
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Get the vector store.
|
|
261
|
-
*
|
|
262
|
-
* @returns Vector store instance
|
|
263
|
-
*/
|
|
264
|
-
getVectorStore() {
|
|
265
|
-
return this.vectorStore;
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Check if the index has been built.
|
|
269
|
-
*
|
|
270
|
-
* @returns True if indexAll has been called
|
|
271
|
-
*/
|
|
272
|
-
isIndexed() {
|
|
273
|
-
return this.indexed;
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Get the number of indexed entities.
|
|
277
|
-
*
|
|
278
|
-
* @returns Number of entities in the vector store
|
|
279
|
-
*/
|
|
280
|
-
getIndexedCount() {
|
|
281
|
-
return this.indexedCount;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Clear all indexed embeddings.
|
|
285
|
-
*/
|
|
286
|
-
clearIndex() {
|
|
287
|
-
this.vectorStore.clear();
|
|
288
|
-
this.indexed = false;
|
|
289
|
-
this.indexedCount = 0;
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Check if semantic search is available.
|
|
293
|
-
*
|
|
294
|
-
* @returns True if embedding service is ready
|
|
295
|
-
*/
|
|
296
|
-
async isAvailable() {
|
|
297
|
-
return this.embeddingService.isReady();
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Get semantic search statistics.
|
|
301
|
-
*
|
|
302
|
-
* @returns Statistics about the semantic search index
|
|
303
|
-
*/
|
|
304
|
-
getStats() {
|
|
305
|
-
return {
|
|
306
|
-
indexed: this.indexed,
|
|
307
|
-
indexedCount: this.indexedCount,
|
|
308
|
-
provider: this.embeddingService.provider,
|
|
309
|
-
model: this.embeddingService.model,
|
|
310
|
-
dimensions: this.embeddingService.dimensions,
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TF-IDF Index Manager
|
|
3
|
-
*
|
|
4
|
-
* Manages pre-calculated TF-IDF indexes for fast ranked search.
|
|
5
|
-
* Handles index building, incremental updates, and persistence.
|
|
6
|
-
*
|
|
7
|
-
* @module search/TFIDFIndexManager
|
|
8
|
-
*/
|
|
9
|
-
import type { TFIDFIndex, KnowledgeGraph, ReadonlyKnowledgeGraph } from '../types/index.js';
|
|
10
|
-
/**
|
|
11
|
-
* Manages TF-IDF index lifecycle: building, updating, and persistence.
|
|
12
|
-
*/
|
|
13
|
-
export declare class TFIDFIndexManager {
|
|
14
|
-
private indexPath;
|
|
15
|
-
private index;
|
|
16
|
-
constructor(storageDir: string);
|
|
17
|
-
/**
|
|
18
|
-
* Build a complete TF-IDF index from a knowledge graph.
|
|
19
|
-
*
|
|
20
|
-
* @param graph - Knowledge graph to index
|
|
21
|
-
* @returns Newly built TF-IDF index
|
|
22
|
-
*/
|
|
23
|
-
buildIndex(graph: ReadonlyKnowledgeGraph): Promise<TFIDFIndex>;
|
|
24
|
-
/**
|
|
25
|
-
* Update the index incrementally when entities change.
|
|
26
|
-
*
|
|
27
|
-
* More efficient than rebuilding the entire index.
|
|
28
|
-
*
|
|
29
|
-
* @param graph - Updated knowledge graph
|
|
30
|
-
* @param changedEntityNames - Names of entities that changed
|
|
31
|
-
*/
|
|
32
|
-
updateIndex(graph: ReadonlyKnowledgeGraph, changedEntityNames: Set<string>): Promise<TFIDFIndex>;
|
|
33
|
-
/**
|
|
34
|
-
* Load index from disk.
|
|
35
|
-
*
|
|
36
|
-
* @returns Loaded index or null if not found
|
|
37
|
-
*/
|
|
38
|
-
loadIndex(): Promise<TFIDFIndex | null>;
|
|
39
|
-
/**
|
|
40
|
-
* Save index to disk.
|
|
41
|
-
*
|
|
42
|
-
* @param index - Index to save (uses cached index if not provided)
|
|
43
|
-
*/
|
|
44
|
-
saveIndex(index?: TFIDFIndex): Promise<void>;
|
|
45
|
-
/**
|
|
46
|
-
* Get the current cached index.
|
|
47
|
-
*
|
|
48
|
-
* @returns Cached index or null if not loaded
|
|
49
|
-
*/
|
|
50
|
-
getIndex(): TFIDFIndex | null;
|
|
51
|
-
/**
|
|
52
|
-
* Clear the cached index and delete from disk.
|
|
53
|
-
*/
|
|
54
|
-
clearIndex(): Promise<void>;
|
|
55
|
-
/**
|
|
56
|
-
* Check if the index needs rebuilding based on graph state.
|
|
57
|
-
*
|
|
58
|
-
* @param graph - Current knowledge graph
|
|
59
|
-
* @returns True if index should be rebuilt
|
|
60
|
-
*/
|
|
61
|
-
needsRebuild(graph: KnowledgeGraph): boolean;
|
|
62
|
-
}
|
|
63
|
-
//# sourceMappingURL=TFIDFIndexManager.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"TFIDFIndexManager.d.ts","sourceRoot":"","sources":["../../src/search/TFIDFIndexManager.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,UAAU,EAAkB,cAAc,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAgB5G;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,KAAK,CAA2B;gBAE5B,UAAU,EAAE,MAAM;IAI9B;;;;;OAKG;IACG,UAAU,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,UAAU,CAAC;IAgDpE;;;;;;;OAOG;IACG,WAAW,CAAC,KAAK,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC;IAgEtG;;;;OAIG;IACG,SAAS,IAAI,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IAmB7C;;;;OAIG;IACG,SAAS,CAAC,KAAK,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBlD;;;;OAIG;IACH,QAAQ,IAAI,UAAU,GAAG,IAAI;IAI7B;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IASjC;;;;;OAKG;IACH,YAAY,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO;CAmB7C"}
|