@danielsimonjr/memoryjs 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +266 -0
  3. package/dist/core/EntityManager.d.ts +268 -0
  4. package/dist/core/EntityManager.d.ts.map +1 -0
  5. package/dist/core/EntityManager.js +512 -0
  6. package/dist/core/EntityManager.js.map +1 -0
  7. package/dist/core/GraphEventEmitter.d.ts +202 -0
  8. package/dist/core/GraphEventEmitter.d.ts.map +1 -0
  9. package/dist/core/GraphEventEmitter.js +347 -0
  10. package/dist/core/GraphEventEmitter.js.map +1 -0
  11. package/dist/core/GraphStorage.d.ts +395 -0
  12. package/dist/core/GraphStorage.d.ts.map +1 -0
  13. package/dist/core/GraphStorage.js +786 -0
  14. package/dist/core/GraphStorage.js.map +1 -0
  15. package/dist/core/GraphTraversal.d.ts +141 -0
  16. package/dist/core/GraphTraversal.d.ts.map +1 -0
  17. package/dist/core/GraphTraversal.js +574 -0
  18. package/dist/core/GraphTraversal.js.map +1 -0
  19. package/dist/core/HierarchyManager.d.ts +111 -0
  20. package/dist/core/HierarchyManager.d.ts.map +1 -0
  21. package/dist/core/HierarchyManager.js +225 -0
  22. package/dist/core/HierarchyManager.js.map +1 -0
  23. package/dist/core/ManagerContext.d.ts +76 -0
  24. package/dist/core/ManagerContext.d.ts.map +1 -0
  25. package/dist/core/ManagerContext.js +129 -0
  26. package/dist/core/ManagerContext.js.map +1 -0
  27. package/dist/core/ObservationManager.d.ts +85 -0
  28. package/dist/core/ObservationManager.d.ts.map +1 -0
  29. package/dist/core/ObservationManager.js +124 -0
  30. package/dist/core/ObservationManager.js.map +1 -0
  31. package/dist/core/RelationManager.d.ts +131 -0
  32. package/dist/core/RelationManager.d.ts.map +1 -0
  33. package/dist/core/RelationManager.js +212 -0
  34. package/dist/core/RelationManager.js.map +1 -0
  35. package/dist/core/SQLiteStorage.d.ts +354 -0
  36. package/dist/core/SQLiteStorage.d.ts.map +1 -0
  37. package/dist/core/SQLiteStorage.js +919 -0
  38. package/dist/core/SQLiteStorage.js.map +1 -0
  39. package/dist/core/StorageFactory.d.ts +45 -0
  40. package/dist/core/StorageFactory.d.ts.map +1 -0
  41. package/dist/core/StorageFactory.js +65 -0
  42. package/dist/core/StorageFactory.js.map +1 -0
  43. package/dist/core/TransactionManager.d.ts +464 -0
  44. package/dist/core/TransactionManager.d.ts.map +1 -0
  45. package/dist/core/TransactionManager.js +869 -0
  46. package/dist/core/TransactionManager.js.map +1 -0
  47. package/dist/core/index.d.ts +17 -0
  48. package/dist/core/index.d.ts.map +1 -0
  49. package/dist/core/index.js +20 -0
  50. package/dist/core/index.js.map +1 -0
  51. package/dist/features/AnalyticsManager.d.ts +44 -0
  52. package/dist/features/AnalyticsManager.d.ts.map +1 -0
  53. package/dist/features/AnalyticsManager.js +224 -0
  54. package/dist/features/AnalyticsManager.js.map +1 -0
  55. package/dist/features/ArchiveManager.d.ts +133 -0
  56. package/dist/features/ArchiveManager.d.ts.map +1 -0
  57. package/dist/features/ArchiveManager.js +282 -0
  58. package/dist/features/ArchiveManager.js.map +1 -0
  59. package/dist/features/CompressionManager.d.ts +119 -0
  60. package/dist/features/CompressionManager.d.ts.map +1 -0
  61. package/dist/features/CompressionManager.js +470 -0
  62. package/dist/features/CompressionManager.js.map +1 -0
  63. package/dist/features/IOManager.d.ts +225 -0
  64. package/dist/features/IOManager.d.ts.map +1 -0
  65. package/dist/features/IOManager.js +1093 -0
  66. package/dist/features/IOManager.js.map +1 -0
  67. package/dist/features/KeywordExtractor.d.ts +61 -0
  68. package/dist/features/KeywordExtractor.d.ts.map +1 -0
  69. package/dist/features/KeywordExtractor.js +127 -0
  70. package/dist/features/KeywordExtractor.js.map +1 -0
  71. package/dist/features/ObservationNormalizer.d.ts +90 -0
  72. package/dist/features/ObservationNormalizer.d.ts.map +1 -0
  73. package/dist/features/ObservationNormalizer.js +194 -0
  74. package/dist/features/ObservationNormalizer.js.map +1 -0
  75. package/dist/features/StreamingExporter.d.ts +128 -0
  76. package/dist/features/StreamingExporter.d.ts.map +1 -0
  77. package/dist/features/StreamingExporter.js +212 -0
  78. package/dist/features/StreamingExporter.js.map +1 -0
  79. package/dist/features/TagManager.d.ts +147 -0
  80. package/dist/features/TagManager.d.ts.map +1 -0
  81. package/dist/features/TagManager.js +211 -0
  82. package/dist/features/TagManager.js.map +1 -0
  83. package/dist/features/index.d.ts +14 -0
  84. package/dist/features/index.d.ts.map +1 -0
  85. package/dist/features/index.js +15 -0
  86. package/dist/features/index.js.map +1 -0
  87. package/dist/index.d.ts +15 -0
  88. package/dist/index.d.ts.map +1 -0
  89. package/dist/index.js +20 -0
  90. package/dist/index.js.map +1 -0
  91. package/dist/search/BM25Search.d.ts +148 -0
  92. package/dist/search/BM25Search.d.ts.map +1 -0
  93. package/dist/search/BM25Search.js +340 -0
  94. package/dist/search/BM25Search.js.map +1 -0
  95. package/dist/search/BasicSearch.d.ts +51 -0
  96. package/dist/search/BasicSearch.d.ts.map +1 -0
  97. package/dist/search/BasicSearch.js +138 -0
  98. package/dist/search/BasicSearch.js.map +1 -0
  99. package/dist/search/BooleanSearch.d.ts +98 -0
  100. package/dist/search/BooleanSearch.d.ts.map +1 -0
  101. package/dist/search/BooleanSearch.js +431 -0
  102. package/dist/search/BooleanSearch.js.map +1 -0
  103. package/dist/search/EarlyTerminationManager.d.ts +140 -0
  104. package/dist/search/EarlyTerminationManager.d.ts.map +1 -0
  105. package/dist/search/EarlyTerminationManager.js +280 -0
  106. package/dist/search/EarlyTerminationManager.js.map +1 -0
  107. package/dist/search/EmbeddingCache.d.ts +175 -0
  108. package/dist/search/EmbeddingCache.d.ts.map +1 -0
  109. package/dist/search/EmbeddingCache.js +247 -0
  110. package/dist/search/EmbeddingCache.js.map +1 -0
  111. package/dist/search/EmbeddingService.d.ts +277 -0
  112. package/dist/search/EmbeddingService.d.ts.map +1 -0
  113. package/dist/search/EmbeddingService.js +531 -0
  114. package/dist/search/EmbeddingService.js.map +1 -0
  115. package/dist/search/FuzzySearch.d.ts +118 -0
  116. package/dist/search/FuzzySearch.d.ts.map +1 -0
  117. package/dist/search/FuzzySearch.js +313 -0
  118. package/dist/search/FuzzySearch.js.map +1 -0
  119. package/dist/search/HybridScorer.d.ts +181 -0
  120. package/dist/search/HybridScorer.d.ts.map +1 -0
  121. package/dist/search/HybridScorer.js +258 -0
  122. package/dist/search/HybridScorer.js.map +1 -0
  123. package/dist/search/HybridSearchManager.d.ts +80 -0
  124. package/dist/search/HybridSearchManager.d.ts.map +1 -0
  125. package/dist/search/HybridSearchManager.js +188 -0
  126. package/dist/search/HybridSearchManager.js.map +1 -0
  127. package/dist/search/IncrementalIndexer.d.ts +201 -0
  128. package/dist/search/IncrementalIndexer.d.ts.map +1 -0
  129. package/dist/search/IncrementalIndexer.js +343 -0
  130. package/dist/search/IncrementalIndexer.js.map +1 -0
  131. package/dist/search/OptimizedInvertedIndex.d.ts +163 -0
  132. package/dist/search/OptimizedInvertedIndex.d.ts.map +1 -0
  133. package/dist/search/OptimizedInvertedIndex.js +359 -0
  134. package/dist/search/OptimizedInvertedIndex.js.map +1 -0
  135. package/dist/search/ParallelSearchExecutor.d.ts +172 -0
  136. package/dist/search/ParallelSearchExecutor.d.ts.map +1 -0
  137. package/dist/search/ParallelSearchExecutor.js +310 -0
  138. package/dist/search/ParallelSearchExecutor.js.map +1 -0
  139. package/dist/search/QuantizedVectorStore.d.ts +171 -0
  140. package/dist/search/QuantizedVectorStore.d.ts.map +1 -0
  141. package/dist/search/QuantizedVectorStore.js +308 -0
  142. package/dist/search/QuantizedVectorStore.js.map +1 -0
  143. package/dist/search/QueryAnalyzer.d.ts +76 -0
  144. package/dist/search/QueryAnalyzer.d.ts.map +1 -0
  145. package/dist/search/QueryAnalyzer.js +228 -0
  146. package/dist/search/QueryAnalyzer.js.map +1 -0
  147. package/dist/search/QueryCostEstimator.d.ts +244 -0
  148. package/dist/search/QueryCostEstimator.d.ts.map +1 -0
  149. package/dist/search/QueryCostEstimator.js +653 -0
  150. package/dist/search/QueryCostEstimator.js.map +1 -0
  151. package/dist/search/QueryPlanCache.d.ts +220 -0
  152. package/dist/search/QueryPlanCache.d.ts.map +1 -0
  153. package/dist/search/QueryPlanCache.js +380 -0
  154. package/dist/search/QueryPlanCache.js.map +1 -0
  155. package/dist/search/QueryPlanner.d.ts +58 -0
  156. package/dist/search/QueryPlanner.d.ts.map +1 -0
  157. package/dist/search/QueryPlanner.js +138 -0
  158. package/dist/search/QueryPlanner.js.map +1 -0
  159. package/dist/search/RankedSearch.d.ts +71 -0
  160. package/dist/search/RankedSearch.d.ts.map +1 -0
  161. package/dist/search/RankedSearch.js +239 -0
  162. package/dist/search/RankedSearch.js.map +1 -0
  163. package/dist/search/ReflectionManager.d.ts +120 -0
  164. package/dist/search/ReflectionManager.d.ts.map +1 -0
  165. package/dist/search/ReflectionManager.js +232 -0
  166. package/dist/search/ReflectionManager.js.map +1 -0
  167. package/dist/search/SavedSearchManager.d.ts +79 -0
  168. package/dist/search/SavedSearchManager.d.ts.map +1 -0
  169. package/dist/search/SavedSearchManager.js +147 -0
  170. package/dist/search/SavedSearchManager.js.map +1 -0
  171. package/dist/search/SearchFilterChain.d.ts +120 -0
  172. package/dist/search/SearchFilterChain.d.ts.map +1 -0
  173. package/dist/search/SearchFilterChain.js +186 -0
  174. package/dist/search/SearchFilterChain.js.map +1 -0
  175. package/dist/search/SearchManager.d.ts +326 -0
  176. package/dist/search/SearchManager.d.ts.map +1 -0
  177. package/dist/search/SearchManager.js +454 -0
  178. package/dist/search/SearchManager.js.map +1 -0
  179. package/dist/search/SearchSuggestions.d.ts +27 -0
  180. package/dist/search/SearchSuggestions.d.ts.map +1 -0
  181. package/dist/search/SearchSuggestions.js +58 -0
  182. package/dist/search/SearchSuggestions.js.map +1 -0
  183. package/dist/search/SemanticSearch.d.ts +149 -0
  184. package/dist/search/SemanticSearch.d.ts.map +1 -0
  185. package/dist/search/SemanticSearch.js +324 -0
  186. package/dist/search/SemanticSearch.js.map +1 -0
  187. package/dist/search/SymbolicSearch.d.ts +61 -0
  188. package/dist/search/SymbolicSearch.d.ts.map +1 -0
  189. package/dist/search/SymbolicSearch.js +164 -0
  190. package/dist/search/SymbolicSearch.js.map +1 -0
  191. package/dist/search/TFIDFEventSync.d.ts +85 -0
  192. package/dist/search/TFIDFEventSync.d.ts.map +1 -0
  193. package/dist/search/TFIDFEventSync.js +134 -0
  194. package/dist/search/TFIDFEventSync.js.map +1 -0
  195. package/dist/search/TFIDFIndexManager.d.ts +151 -0
  196. package/dist/search/TFIDFIndexManager.d.ts.map +1 -0
  197. package/dist/search/TFIDFIndexManager.js +433 -0
  198. package/dist/search/TFIDFIndexManager.js.map +1 -0
  199. package/dist/search/VectorStore.d.ts +235 -0
  200. package/dist/search/VectorStore.d.ts.map +1 -0
  201. package/dist/search/VectorStore.js +312 -0
  202. package/dist/search/VectorStore.js.map +1 -0
  203. package/dist/search/index.d.ts +35 -0
  204. package/dist/search/index.d.ts.map +1 -0
  205. package/dist/search/index.js +53 -0
  206. package/dist/search/index.js.map +1 -0
  207. package/dist/types/index.d.ts +13 -0
  208. package/dist/types/index.d.ts.map +1 -0
  209. package/dist/types/index.js +13 -0
  210. package/dist/types/index.js.map +1 -0
  211. package/dist/types/types.d.ts +1811 -0
  212. package/dist/types/types.d.ts.map +1 -0
  213. package/dist/types/types.js +10 -0
  214. package/dist/types/types.js.map +1 -0
  215. package/dist/utils/BatchProcessor.d.ts +271 -0
  216. package/dist/utils/BatchProcessor.d.ts.map +1 -0
  217. package/dist/utils/BatchProcessor.js +377 -0
  218. package/dist/utils/BatchProcessor.js.map +1 -0
  219. package/dist/utils/MemoryMonitor.d.ts +176 -0
  220. package/dist/utils/MemoryMonitor.d.ts.map +1 -0
  221. package/dist/utils/MemoryMonitor.js +306 -0
  222. package/dist/utils/MemoryMonitor.js.map +1 -0
  223. package/dist/utils/WorkerPoolManager.d.ts +233 -0
  224. package/dist/utils/WorkerPoolManager.d.ts.map +1 -0
  225. package/dist/utils/WorkerPoolManager.js +421 -0
  226. package/dist/utils/WorkerPoolManager.js.map +1 -0
  227. package/dist/utils/compressedCache.d.ts +221 -0
  228. package/dist/utils/compressedCache.d.ts.map +1 -0
  229. package/dist/utils/compressedCache.js +349 -0
  230. package/dist/utils/compressedCache.js.map +1 -0
  231. package/dist/utils/compressionUtil.d.ts +214 -0
  232. package/dist/utils/compressionUtil.d.ts.map +1 -0
  233. package/dist/utils/compressionUtil.js +248 -0
  234. package/dist/utils/compressionUtil.js.map +1 -0
  235. package/dist/utils/constants.d.ts +245 -0
  236. package/dist/utils/constants.d.ts.map +1 -0
  237. package/dist/utils/constants.js +253 -0
  238. package/dist/utils/constants.js.map +1 -0
  239. package/dist/utils/entityUtils.d.ts +379 -0
  240. package/dist/utils/entityUtils.d.ts.map +1 -0
  241. package/dist/utils/entityUtils.js +649 -0
  242. package/dist/utils/entityUtils.js.map +1 -0
  243. package/dist/utils/errors.d.ts +95 -0
  244. package/dist/utils/errors.d.ts.map +1 -0
  245. package/dist/utils/errors.js +146 -0
  246. package/dist/utils/errors.js.map +1 -0
  247. package/dist/utils/formatters.d.ts +145 -0
  248. package/dist/utils/formatters.d.ts.map +1 -0
  249. package/dist/utils/formatters.js +133 -0
  250. package/dist/utils/formatters.js.map +1 -0
  251. package/dist/utils/index.d.ts +26 -0
  252. package/dist/utils/index.d.ts.map +1 -0
  253. package/dist/utils/index.js +88 -0
  254. package/dist/utils/index.js.map +1 -0
  255. package/dist/utils/indexes.d.ts +270 -0
  256. package/dist/utils/indexes.d.ts.map +1 -0
  257. package/dist/utils/indexes.js +527 -0
  258. package/dist/utils/indexes.js.map +1 -0
  259. package/dist/utils/logger.d.ts +31 -0
  260. package/dist/utils/logger.d.ts.map +1 -0
  261. package/dist/utils/logger.js +41 -0
  262. package/dist/utils/logger.js.map +1 -0
  263. package/dist/utils/operationUtils.d.ts +124 -0
  264. package/dist/utils/operationUtils.d.ts.map +1 -0
  265. package/dist/utils/operationUtils.js +176 -0
  266. package/dist/utils/operationUtils.js.map +1 -0
  267. package/dist/utils/parallelUtils.d.ts +76 -0
  268. package/dist/utils/parallelUtils.d.ts.map +1 -0
  269. package/dist/utils/parallelUtils.js +192 -0
  270. package/dist/utils/parallelUtils.js.map +1 -0
  271. package/dist/utils/schemas.d.ts +556 -0
  272. package/dist/utils/schemas.d.ts.map +1 -0
  273. package/dist/utils/schemas.js +485 -0
  274. package/dist/utils/schemas.js.map +1 -0
  275. package/dist/utils/searchAlgorithms.d.ts +99 -0
  276. package/dist/utils/searchAlgorithms.d.ts.map +1 -0
  277. package/dist/utils/searchAlgorithms.js +168 -0
  278. package/dist/utils/searchAlgorithms.js.map +1 -0
  279. package/dist/utils/searchCache.d.ts +108 -0
  280. package/dist/utils/searchCache.d.ts.map +1 -0
  281. package/dist/utils/searchCache.js +210 -0
  282. package/dist/utils/searchCache.js.map +1 -0
  283. package/dist/utils/taskScheduler.d.ts +294 -0
  284. package/dist/utils/taskScheduler.d.ts.map +1 -0
  285. package/dist/utils/taskScheduler.js +487 -0
  286. package/dist/utils/taskScheduler.js.map +1 -0
  287. package/dist/workers/index.d.ts +12 -0
  288. package/dist/workers/index.d.ts.map +1 -0
  289. package/dist/workers/index.js +10 -0
  290. package/dist/workers/index.js.map +1 -0
  291. package/dist/workers/levenshteinWorker.d.ts +60 -0
  292. package/dist/workers/levenshteinWorker.d.ts.map +1 -0
  293. package/dist/workers/levenshteinWorker.js +99 -0
  294. package/dist/workers/levenshteinWorker.js.map +1 -0
  295. package/package.json +69 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Anthropic, PBC (Original Memory MCP Server)
4
+ Copyright (c) 2025-2026 Daniel Simon Jr. (Enhanced Knowledge Graph Library)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,266 @@
1
+ # @danielsimonjr/memoryjs
2
+
3
+ Core knowledge graph library for managing entities, relations, and observations with advanced search capabilities.
4
+
5
+ ## Features
6
+
7
+ - **Entity Management**: Create, read, update, delete entities with observations
8
+ - **Relation Management**: Connect entities with typed relationships
9
+ - **Hierarchical Organization**: Parent-child entity nesting
10
+ - **Multiple Storage Backends**: JSONL (default) or SQLite
11
+ - **Advanced Search**: Basic, ranked (TF-IDF), boolean, fuzzy, semantic, and hybrid search
12
+ - **Tag Management**: Tag aliasing, bulk operations
13
+ - **Graph Algorithms**: Shortest path, centrality, connected components
14
+ - **Import/Export**: JSON, CSV, GraphML formats with compression
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @danielsimonjr/memoryjs
20
+ ```
21
+
22
+ ## Quick Start
23
+
24
+ ```typescript
25
+ import { ManagerContext } from '@danielsimonjr/memoryjs';
26
+
27
+ // Initialize with JSONL storage (default)
28
+ const ctx = new ManagerContext({
29
+ storagePath: './memory.jsonl'
30
+ });
31
+
32
+ // Create entities
33
+ await ctx.entityManager.createEntities([
34
+ { name: 'TypeScript', entityType: 'language', observations: ['A typed superset of JavaScript'] },
35
+ { name: 'Node.js', entityType: 'runtime', observations: ['JavaScript runtime built on V8'] }
36
+ ]);
37
+
38
+ // Create relations
39
+ await ctx.relationManager.createRelations([
40
+ { from: 'TypeScript', to: 'Node.js', relationType: 'runs_on' }
41
+ ]);
42
+
43
+ // Search entities
44
+ const results = await ctx.searchManager.search('JavaScript');
45
+ ```
46
+
47
+ ## Storage Options
48
+
49
+ ### JSONL (Default)
50
+
51
+ ```typescript
52
+ const ctx = new ManagerContext({
53
+ storagePath: './memory.jsonl'
54
+ });
55
+ ```
56
+
57
+ ### SQLite
58
+
59
+ ```typescript
60
+ const ctx = new ManagerContext({
61
+ storageType: 'sqlite',
62
+ storagePath: './memory.db'
63
+ });
64
+ ```
65
+
66
+ SQLite provides:
67
+ - FTS5 full-text search with BM25 ranking
68
+ - Referential integrity (ON DELETE CASCADE)
69
+ - WAL mode for better concurrency
70
+ - ACID transactions
71
+
72
+ ## Core Components
73
+
74
+ ### ManagerContext
75
+
76
+ Central access point for all managers:
77
+
78
+ ```typescript
79
+ ctx.entityManager // Entity CRUD + hierarchy
80
+ ctx.relationManager // Relation management
81
+ ctx.searchManager // All search operations
82
+ ctx.tagManager // Tag aliases
83
+ ctx.ioManager // Import/export/backup
84
+ ctx.graphTraversal // Graph algorithms
85
+ ctx.semanticSearch // Vector similarity search (optional)
86
+ ```
87
+
88
+ ### Entity Structure
89
+
90
+ ```typescript
91
+ interface Entity {
92
+ name: string; // Unique identifier
93
+ entityType: string; // Classification
94
+ observations: string[]; // Facts about the entity
95
+ parentId?: string; // For hierarchy
96
+ tags?: string[]; // Categories
97
+ importance?: number; // 0-10 scale
98
+ createdAt?: string; // ISO 8601
99
+ lastModified?: string;
100
+ }
101
+ ```
102
+
103
+ ### Relation Structure
104
+
105
+ ```typescript
106
+ interface Relation {
107
+ from: string; // Source entity name
108
+ to: string; // Target entity name
109
+ relationType: string; // Connection type
110
+ }
111
+ ```
112
+
113
+ ## Search Capabilities
114
+
115
+ ### Basic Search
116
+
117
+ ```typescript
118
+ // Find entities by name or observation content
119
+ const results = await ctx.searchManager.search('TypeScript');
120
+ ```
121
+
122
+ ### Ranked Search (TF-IDF)
123
+
124
+ ```typescript
125
+ // Get relevance-scored results
126
+ const ranked = await ctx.searchManager.searchRanked('JavaScript runtime', { limit: 10 });
127
+ ```
128
+
129
+ ### Boolean Search
130
+
131
+ ```typescript
132
+ // AND, OR, NOT operators
133
+ const results = await ctx.searchManager.booleanSearch('TypeScript AND runtime');
134
+ const excluded = await ctx.searchManager.booleanSearch('JavaScript NOT browser');
135
+ ```
136
+
137
+ ### Fuzzy Search
138
+
139
+ ```typescript
140
+ // Typo-tolerant search
141
+ const results = await ctx.searchManager.fuzzySearch('Typscript', { threshold: 0.7 });
142
+ ```
143
+
144
+ ### Hybrid Search
145
+
146
+ Combines semantic (vector), lexical (TF-IDF), and symbolic (metadata) signals:
147
+
148
+ ```typescript
149
+ const results = await ctx.searchManager.hybridSearch('programming concepts', {
150
+ weights: { semantic: 0.5, lexical: 0.3, symbolic: 0.2 },
151
+ filters: { entityTypes: ['concept'], minImportance: 5 }
152
+ });
153
+ ```
154
+
155
+ ## Graph Algorithms
156
+
157
+ ```typescript
158
+ // Shortest path between entities
159
+ const path = await ctx.graphTraversal.findShortestPath('A', 'Z');
160
+
161
+ // All paths up to max depth
162
+ const paths = await ctx.graphTraversal.findAllPaths('A', 'Z', { maxDepth: 5 });
163
+
164
+ // Centrality analysis
165
+ const centrality = await ctx.graphTraversal.getCentrality({ algorithm: 'pagerank' });
166
+
167
+ // Connected components
168
+ const components = await ctx.graphTraversal.getConnectedComponents();
169
+ ```
170
+
171
+ ## Import/Export
172
+
173
+ ```typescript
174
+ // Export to JSON
175
+ const json = await ctx.ioManager.exportGraph('json');
176
+
177
+ // Export to CSV
178
+ const csv = await ctx.ioManager.exportGraph('csv');
179
+
180
+ // Export to GraphML (with compression)
181
+ await ctx.ioManager.exportGraph('graphml', {
182
+ outputPath: './graph.graphml.br',
183
+ compress: true
184
+ });
185
+
186
+ // Import from file
187
+ await ctx.ioManager.importGraph('json', jsonData, { mergeStrategy: 'merge' });
188
+ ```
189
+
190
+ ## Hierarchical Organization
191
+
192
+ ```typescript
193
+ // Set parent
194
+ await ctx.entityManager.setEntityParent('Component', 'Module');
195
+
196
+ // Get hierarchy
197
+ const children = await ctx.entityManager.getChildren('Module');
198
+ const ancestors = await ctx.entityManager.getAncestors('Component');
199
+ const subtree = await ctx.entityManager.getSubtree('Module');
200
+ ```
201
+
202
+ ## Tag Management
203
+
204
+ ```typescript
205
+ // Add/remove tags
206
+ await ctx.entityManager.addTags('Entity1', ['tag1', 'tag2']);
207
+ await ctx.entityManager.removeTags('Entity1', ['tag1']);
208
+
209
+ // Tag aliases (synonyms)
210
+ await ctx.tagManager.addTagAlias('js', 'javascript');
211
+
212
+ // Bulk operations
213
+ await ctx.entityManager.addTagsToMultipleEntities(['E1', 'E2'], ['shared-tag']);
214
+ ```
215
+
216
+ ## API Reference
217
+
218
+ ### EntityManager
219
+
220
+ | Method | Description |
221
+ |--------|-------------|
222
+ | `createEntities(entities)` | Create multiple entities |
223
+ | `deleteEntities(names)` | Delete entities by name |
224
+ | `getEntityByName(name)` | Get single entity |
225
+ | `addObservations(name, observations)` | Add observations to entity |
226
+ | `deleteObservations(name, observations)` | Remove observations |
227
+ | `addTags(name, tags)` | Add tags to entity |
228
+ | `removeTags(name, tags)` | Remove tags from entity |
229
+ | `setImportance(name, score)` | Set importance (0-10) |
230
+ | `setEntityParent(name, parentName)` | Set hierarchy parent |
231
+ | `getChildren(name)` | Get child entities |
232
+ | `getAncestors(name)` | Get ancestor chain |
233
+ | `getDescendants(name)` | Get all descendants |
234
+
235
+ ### SearchManager
236
+
237
+ | Method | Description |
238
+ |--------|-------------|
239
+ | `search(query, options)` | Basic search |
240
+ | `searchRanked(query, options)` | TF-IDF ranked search |
241
+ | `booleanSearch(query, options)` | Boolean operators |
242
+ | `fuzzySearch(query, options)` | Typo-tolerant |
243
+ | `hybridSearch(query, options)` | Multi-signal search |
244
+ | `smartSearch(query, options)` | AI-assisted refinement |
245
+
246
+ ### IOManager
247
+
248
+ | Method | Description |
249
+ |--------|-------------|
250
+ | `exportGraph(format, options)` | Export to format |
251
+ | `importGraph(format, data, options)` | Import from format |
252
+ | `createBackup(options)` | Create backup |
253
+ | `restoreBackup(path)` | Restore from backup |
254
+
255
+ ## Requirements
256
+
257
+ - Node.js >= 18.0.0
258
+ - TypeScript >= 5.0 (for development)
259
+
260
+ ## License
261
+
262
+ MIT
263
+
264
+ ## Related
265
+
266
+ - [@danielsimonjr/memory-mcp](https://github.com/danielsimonjr/memory-mcp) - MCP server built on this library
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Entity Manager
3
+ *
4
+ * Handles CRUD operations for entities in the knowledge graph.
5
+ * Focused on core entity and tag operations only (Phase 4: Consolidate God Objects).
6
+ *
7
+ * @module core/EntityManager
8
+ */
9
+ import type { Entity, LongRunningOperationOptions } from '../types/index.js';
10
+ import type { GraphStorage } from './GraphStorage.js';
11
+ /**
12
+ * Manages entity operations with automatic timestamp handling.
13
+ */
14
+ export declare class EntityManager {
15
+ private storage;
16
+ constructor(storage: GraphStorage);
17
+ /**
18
+ * Create multiple entities in a single batch operation.
19
+ *
20
+ * This method performs the following operations:
21
+ * - Filters out entities that already exist (duplicate names)
22
+ * - Automatically adds createdAt and lastModified timestamps
23
+ * - Normalizes all tags to lowercase for consistent searching
24
+ * - Validates importance values (must be between 0-10)
25
+ *
26
+ * Phase 9B: Supports progress tracking and cancellation via LongRunningOperationOptions.
27
+ *
28
+ * @param entities - Array of entities to create. Each entity must have a unique name.
29
+ * @param options - Optional progress/cancellation options (Phase 9B)
30
+ * @returns Promise resolving to array of newly created entities (excludes duplicates)
31
+ * @throws {InvalidImportanceError} If any entity has importance outside the valid range [0-10]
32
+ * @throws {OperationCancelledError} If operation is cancelled via signal (Phase 9B)
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const manager = new EntityManager(storage);
37
+ *
38
+ * // Create single entity
39
+ * const results = await manager.createEntities([{
40
+ * name: 'Alice',
41
+ * entityType: 'person',
42
+ * observations: ['Works as engineer', 'Lives in Seattle'],
43
+ * importance: 7,
44
+ * tags: ['Team', 'Engineering']
45
+ * }]);
46
+ *
47
+ * // Create multiple entities at once
48
+ * const users = await manager.createEntities([
49
+ * { name: 'Bob', entityType: 'person', observations: [] },
50
+ * { name: 'Charlie', entityType: 'person', observations: [] }
51
+ * ]);
52
+ *
53
+ * // With progress tracking and cancellation (Phase 9B)
54
+ * const controller = new AbortController();
55
+ * const results = await manager.createEntities(largeEntityArray, {
56
+ * signal: controller.signal,
57
+ * onProgress: (p) => console.log(`${p.percentage}% complete`),
58
+ * });
59
+ * ```
60
+ */
61
+ createEntities(entities: Entity[], options?: LongRunningOperationOptions): Promise<Entity[]>;
62
+ /**
63
+ * Delete multiple entities by name in a single batch operation.
64
+ *
65
+ * This method performs cascading deletion:
66
+ * - Removes all specified entities from the graph
67
+ * - Automatically removes all relations where these entities are source or target
68
+ * - Silently ignores entity names that don't exist (no error thrown)
69
+ *
70
+ * @param entityNames - Array of entity names to delete
71
+ * @returns Promise that resolves when deletion is complete
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * const manager = new EntityManager(storage);
76
+ *
77
+ * // Delete single entity
78
+ * await manager.deleteEntities(['Alice']);
79
+ *
80
+ * // Delete multiple entities at once
81
+ * await manager.deleteEntities(['Bob', 'Charlie', 'Dave']);
82
+ *
83
+ * // Safe to delete non-existent entities (no error)
84
+ * await manager.deleteEntities(['NonExistent']); // No error thrown
85
+ * ```
86
+ */
87
+ deleteEntities(entityNames: string[]): Promise<void>;
88
+ /**
89
+ * Retrieve a single entity by its unique name.
90
+ *
91
+ * This is a read-only operation that does not modify the graph.
92
+ * Entity names are case-sensitive.
93
+ *
94
+ * @param name - The unique name of the entity to retrieve
95
+ * @returns Promise resolving to the Entity object if found, or null if not found
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * const manager = new EntityManager(storage);
100
+ *
101
+ * // Get an existing entity
102
+ * const alice = await manager.getEntity('Alice');
103
+ * if (alice) {
104
+ * console.log(alice.observations);
105
+ * console.log(alice.importance);
106
+ * }
107
+ *
108
+ * // Handle non-existent entity
109
+ * const missing = await manager.getEntity('NonExistent');
110
+ * console.log(missing); // null
111
+ * ```
112
+ */
113
+ getEntity(name: string): Promise<Entity | null>;
114
+ /**
115
+ * Update one or more fields of an existing entity.
116
+ *
117
+ * This method allows partial updates - only the fields specified in the updates
118
+ * object will be changed. All other fields remain unchanged.
119
+ * The lastModified timestamp is automatically updated.
120
+ *
121
+ * @param name - The unique name of the entity to update
122
+ * @param updates - Partial entity object containing only the fields to update
123
+ * @returns Promise resolving to the fully updated Entity object
124
+ * @throws {EntityNotFoundError} If no entity with the given name exists
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const manager = new EntityManager(storage);
129
+ *
130
+ * // Update importance only
131
+ * const updated = await manager.updateEntity('Alice', {
132
+ * importance: 9
133
+ * });
134
+ *
135
+ * // Update multiple fields
136
+ * await manager.updateEntity('Bob', {
137
+ * entityType: 'senior_engineer',
138
+ * tags: ['leadership', 'architecture'],
139
+ * observations: ['Led project X', 'Designed system Y']
140
+ * });
141
+ *
142
+ * // Add observations (requires reading existing entity first)
143
+ * const entity = await manager.getEntity('Charlie');
144
+ * if (entity) {
145
+ * await manager.updateEntity('Charlie', {
146
+ * observations: [...entity.observations, 'New observation']
147
+ * });
148
+ * }
149
+ * ```
150
+ */
151
+ updateEntity(name: string, updates: Partial<Entity>): Promise<Entity>;
152
+ /**
153
+ * Update multiple entities in a single batch operation.
154
+ *
155
+ * This method is more efficient than calling updateEntity multiple times
156
+ * as it loads and saves the graph only once. All updates are applied atomically.
157
+ * The lastModified timestamp is automatically updated for all entities.
158
+ *
159
+ * @param updates - Array of updates, each containing entity name and changes
160
+ * @returns Promise resolving to array of updated entities
161
+ * @throws {EntityNotFoundError} If any entity is not found
162
+ * @throws {ValidationError} If any update data is invalid
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * const manager = new EntityManager(storage);
167
+ *
168
+ * // Update multiple entities at once
169
+ * const updated = await manager.batchUpdate([
170
+ * { name: 'Alice', updates: { importance: 9 } },
171
+ * { name: 'Bob', updates: { importance: 8, tags: ['senior'] } },
172
+ * { name: 'Charlie', updates: { entityType: 'lead_engineer' } }
173
+ * ]);
174
+ *
175
+ * console.log(`Updated ${updated.length} entities`);
176
+ *
177
+ * // Efficiently update many entities (single graph load/save)
178
+ * const massUpdate = employees.map(name => ({
179
+ * name,
180
+ * updates: { tags: ['team-2024'] }
181
+ * }));
182
+ * await manager.batchUpdate(massUpdate);
183
+ * ```
184
+ */
185
+ batchUpdate(updates: Array<{
186
+ name: string;
187
+ updates: Partial<Entity>;
188
+ }>): Promise<Entity[]>;
189
+ /**
190
+ * Add tags to an entity.
191
+ *
192
+ * Tags are normalized to lowercase and duplicates are filtered out.
193
+ *
194
+ * @param entityName - Name of the entity
195
+ * @param tags - Tags to add
196
+ * @returns Result with entity name and added tags
197
+ * @throws {EntityNotFoundError} If entity is not found
198
+ */
199
+ addTags(entityName: string, tags: string[]): Promise<{
200
+ entityName: string;
201
+ addedTags: string[];
202
+ }>;
203
+ /**
204
+ * Remove tags from an entity.
205
+ *
206
+ * @param entityName - Name of the entity
207
+ * @param tags - Tags to remove
208
+ * @returns Result with entity name and removed tags
209
+ * @throws {EntityNotFoundError} If entity is not found
210
+ */
211
+ removeTags(entityName: string, tags: string[]): Promise<{
212
+ entityName: string;
213
+ removedTags: string[];
214
+ }>;
215
+ /**
216
+ * Set importance level for an entity.
217
+ *
218
+ * @param entityName - Name of the entity
219
+ * @param importance - Importance level (0-10)
220
+ * @returns Result with entity name and importance
221
+ * @throws {EntityNotFoundError} If entity is not found
222
+ * @throws {Error} If importance is out of range
223
+ */
224
+ setImportance(entityName: string, importance: number): Promise<{
225
+ entityName: string;
226
+ importance: number;
227
+ }>;
228
+ /**
229
+ * Add tags to multiple entities in a single operation.
230
+ *
231
+ * OPTIMIZED: Uses Map for O(1) entity lookups instead of O(n) find() per entity.
232
+ *
233
+ * @param entityNames - Names of entities to tag
234
+ * @param tags - Tags to add to each entity
235
+ * @returns Array of results showing which tags were added to each entity
236
+ */
237
+ addTagsToMultipleEntities(entityNames: string[], tags: string[]): Promise<{
238
+ entityName: string;
239
+ addedTags: string[];
240
+ }[]>;
241
+ /**
242
+ * Replace a tag with a new tag across all entities (rename tag).
243
+ *
244
+ * @param oldTag - Tag to replace
245
+ * @param newTag - New tag value
246
+ * @returns Result with affected entities and count
247
+ */
248
+ replaceTag(oldTag: string, newTag: string): Promise<{
249
+ affectedEntities: string[];
250
+ count: number;
251
+ }>;
252
+ /**
253
+ * Merge two tags into one target tag across all entities.
254
+ *
255
+ * Combines tag1 and tag2 into targetTag. Any entity with either tag1 or tag2
256
+ * will have both removed and targetTag added (if not already present).
257
+ *
258
+ * @param tag1 - First tag to merge
259
+ * @param tag2 - Second tag to merge
260
+ * @param targetTag - Target tag to merge into
261
+ * @returns Object with affected entity names and count
262
+ */
263
+ mergeTags(tag1: string, tag2: string, targetTag: string): Promise<{
264
+ affectedEntities: string[];
265
+ count: number;
266
+ }>;
267
+ }
268
+ //# sourceMappingURL=EntityManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EntityManager.d.ts","sourceRoot":"","sources":["../../src/core/EntityManager.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AAC7E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAyBtD;;GAEG;AACH,qBAAa,aAAa;IACZ,OAAO,CAAC,OAAO;gBAAP,OAAO,EAAE,YAAY;IAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2CG;IACG,cAAc,CAClB,QAAQ,EAAE,MAAM,EAAE,EAClB,OAAO,CAAC,EAAE,2BAA2B,GACpC,OAAO,CAAC,MAAM,EAAE,CAAC;IA+EpB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACG,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAoB1D;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACG,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAKrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACG,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IAuB3E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgCG;IACG,WAAW,CACf,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;KAAE,CAAC,GACzD,OAAO,CAAC,MAAM,EAAE,CAAC;IAuCpB;;;;;;;;;OASG;IACG,OAAO,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAsBvG;;;;;;;OAOG;IACG,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAgC5G;;;;;;;;OAQG;IACG,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,CAAC;IAkBhH;;;;;;;;OAQG;IACG,yBAAyB,CAAC,WAAW,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,CAAC;IAuC9H;;;;;;OAMG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAuBxG;;;;;;;;;;OAUG;IACG,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,gBAAgB,EAAE,MAAM,EAAE,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;CAmCvH"}