@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.
Files changed (209) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +2000 -194
  3. package/dist/__tests__/file-path.test.js +7 -11
  4. package/dist/__tests__/knowledge-graph.test.js +3 -8
  5. package/dist/core/EntityManager.d.ts +266 -0
  6. package/dist/core/EntityManager.d.ts.map +1 -0
  7. package/dist/core/EntityManager.js +85 -133
  8. package/dist/core/GraphEventEmitter.d.ts +202 -0
  9. package/dist/core/GraphEventEmitter.d.ts.map +1 -0
  10. package/dist/core/GraphEventEmitter.js +346 -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 +643 -31
  14. package/dist/core/GraphTraversal.d.ts +141 -0
  15. package/dist/core/GraphTraversal.d.ts.map +1 -0
  16. package/dist/core/GraphTraversal.js +573 -0
  17. package/dist/core/HierarchyManager.d.ts +111 -0
  18. package/dist/core/HierarchyManager.d.ts.map +1 -0
  19. package/dist/{features → core}/HierarchyManager.js +14 -9
  20. package/dist/core/ManagerContext.d.ts +72 -0
  21. package/dist/core/ManagerContext.d.ts.map +1 -0
  22. package/dist/core/ManagerContext.js +118 -0
  23. package/dist/core/ObservationManager.d.ts +85 -0
  24. package/dist/core/ObservationManager.d.ts.map +1 -0
  25. package/dist/core/ObservationManager.js +51 -57
  26. package/dist/core/RelationManager.d.ts +131 -0
  27. package/dist/core/RelationManager.d.ts.map +1 -0
  28. package/dist/core/RelationManager.js +31 -7
  29. package/dist/core/SQLiteStorage.d.ts +354 -0
  30. package/dist/core/SQLiteStorage.d.ts.map +1 -0
  31. package/dist/core/SQLiteStorage.js +917 -0
  32. package/dist/core/StorageFactory.d.ts +45 -0
  33. package/dist/core/StorageFactory.d.ts.map +1 -0
  34. package/dist/core/StorageFactory.js +64 -0
  35. package/dist/core/TransactionManager.d.ts +464 -0
  36. package/dist/core/TransactionManager.d.ts.map +1 -0
  37. package/dist/core/TransactionManager.js +490 -13
  38. package/dist/core/index.d.ts +17 -0
  39. package/dist/core/index.d.ts.map +1 -0
  40. package/dist/core/index.js +12 -2
  41. package/dist/features/AnalyticsManager.d.ts +44 -0
  42. package/dist/features/AnalyticsManager.d.ts.map +1 -0
  43. package/dist/features/AnalyticsManager.js +3 -2
  44. package/dist/features/ArchiveManager.d.ts +133 -0
  45. package/dist/features/ArchiveManager.d.ts.map +1 -0
  46. package/dist/features/ArchiveManager.js +221 -14
  47. package/dist/features/CompressionManager.d.ts +117 -0
  48. package/dist/features/CompressionManager.d.ts.map +1 -0
  49. package/dist/features/CompressionManager.js +189 -20
  50. package/dist/features/IOManager.d.ts +225 -0
  51. package/dist/features/IOManager.d.ts.map +1 -0
  52. package/dist/features/IOManager.js +1041 -0
  53. package/dist/features/StreamingExporter.d.ts +123 -0
  54. package/dist/features/StreamingExporter.d.ts.map +1 -0
  55. package/dist/features/StreamingExporter.js +203 -0
  56. package/dist/features/TagManager.d.ts +147 -0
  57. package/dist/features/TagManager.d.ts.map +1 -0
  58. package/dist/features/index.d.ts +12 -0
  59. package/dist/features/index.d.ts.map +1 -0
  60. package/dist/features/index.js +5 -6
  61. package/dist/index.d.ts +9 -0
  62. package/dist/index.d.ts.map +1 -0
  63. package/dist/index.js +10 -10
  64. package/dist/memory.jsonl +1 -26
  65. package/dist/search/BasicSearch.d.ts +51 -0
  66. package/dist/search/BasicSearch.d.ts.map +1 -0
  67. package/dist/search/BasicSearch.js +9 -3
  68. package/dist/search/BooleanSearch.d.ts +98 -0
  69. package/dist/search/BooleanSearch.d.ts.map +1 -0
  70. package/dist/search/BooleanSearch.js +156 -9
  71. package/dist/search/EmbeddingService.d.ts +178 -0
  72. package/dist/search/EmbeddingService.d.ts.map +1 -0
  73. package/dist/search/EmbeddingService.js +358 -0
  74. package/dist/search/FuzzySearch.d.ts +118 -0
  75. package/dist/search/FuzzySearch.d.ts.map +1 -0
  76. package/dist/search/FuzzySearch.js +241 -25
  77. package/dist/search/QueryCostEstimator.d.ts +111 -0
  78. package/dist/search/QueryCostEstimator.d.ts.map +1 -0
  79. package/dist/search/QueryCostEstimator.js +355 -0
  80. package/dist/search/RankedSearch.d.ts +71 -0
  81. package/dist/search/RankedSearch.d.ts.map +1 -0
  82. package/dist/search/RankedSearch.js +54 -6
  83. package/dist/search/SavedSearchManager.d.ts +79 -0
  84. package/dist/search/SavedSearchManager.d.ts.map +1 -0
  85. package/dist/search/SearchFilterChain.d.ts +120 -0
  86. package/dist/search/SearchFilterChain.d.ts.map +1 -0
  87. package/dist/search/SearchFilterChain.js +2 -4
  88. package/dist/search/SearchManager.d.ts +326 -0
  89. package/dist/search/SearchManager.d.ts.map +1 -0
  90. package/dist/search/SearchManager.js +148 -0
  91. package/dist/search/SearchSuggestions.d.ts +27 -0
  92. package/dist/search/SearchSuggestions.d.ts.map +1 -0
  93. package/dist/search/SearchSuggestions.js +1 -1
  94. package/dist/search/SemanticSearch.d.ts +149 -0
  95. package/dist/search/SemanticSearch.d.ts.map +1 -0
  96. package/dist/search/SemanticSearch.js +323 -0
  97. package/dist/search/TFIDFEventSync.d.ts +85 -0
  98. package/dist/search/TFIDFEventSync.d.ts.map +1 -0
  99. package/dist/search/TFIDFEventSync.js +133 -0
  100. package/dist/search/TFIDFIndexManager.d.ts +151 -0
  101. package/dist/search/TFIDFIndexManager.d.ts.map +1 -0
  102. package/dist/search/TFIDFIndexManager.js +232 -17
  103. package/dist/search/VectorStore.d.ts +235 -0
  104. package/dist/search/VectorStore.d.ts.map +1 -0
  105. package/dist/search/VectorStore.js +311 -0
  106. package/dist/search/index.d.ts +21 -0
  107. package/dist/search/index.d.ts.map +1 -0
  108. package/dist/search/index.js +12 -0
  109. package/dist/server/MCPServer.d.ts +21 -0
  110. package/dist/server/MCPServer.d.ts.map +1 -0
  111. package/dist/server/MCPServer.js +4 -4
  112. package/dist/server/responseCompressor.d.ts +94 -0
  113. package/dist/server/responseCompressor.d.ts.map +1 -0
  114. package/dist/server/responseCompressor.js +127 -0
  115. package/dist/server/toolDefinitions.d.ts +27 -0
  116. package/dist/server/toolDefinitions.d.ts.map +1 -0
  117. package/dist/server/toolDefinitions.js +188 -17
  118. package/dist/server/toolHandlers.d.ts +41 -0
  119. package/dist/server/toolHandlers.d.ts.map +1 -0
  120. package/dist/server/toolHandlers.js +467 -75
  121. package/dist/types/index.d.ts +13 -0
  122. package/dist/types/index.d.ts.map +1 -0
  123. package/dist/types/index.js +1 -1
  124. package/dist/types/types.d.ts +1654 -0
  125. package/dist/types/types.d.ts.map +1 -0
  126. package/dist/types/types.js +9 -0
  127. package/dist/utils/compressedCache.d.ts +192 -0
  128. package/dist/utils/compressedCache.d.ts.map +1 -0
  129. package/dist/utils/compressedCache.js +309 -0
  130. package/dist/utils/compressionUtil.d.ts +214 -0
  131. package/dist/utils/compressionUtil.d.ts.map +1 -0
  132. package/dist/utils/compressionUtil.js +247 -0
  133. package/dist/utils/constants.d.ts +245 -0
  134. package/dist/utils/constants.d.ts.map +1 -0
  135. package/dist/utils/constants.js +124 -0
  136. package/dist/utils/entityUtils.d.ts +321 -0
  137. package/dist/utils/entityUtils.d.ts.map +1 -0
  138. package/dist/utils/entityUtils.js +434 -4
  139. package/dist/utils/errors.d.ts +95 -0
  140. package/dist/utils/errors.d.ts.map +1 -0
  141. package/dist/utils/errors.js +24 -0
  142. package/dist/utils/formatters.d.ts +145 -0
  143. package/dist/utils/formatters.d.ts.map +1 -0
  144. package/dist/utils/{paginationUtils.js → formatters.js} +54 -3
  145. package/dist/utils/index.d.ts +23 -0
  146. package/dist/utils/index.d.ts.map +1 -0
  147. package/dist/utils/index.js +69 -31
  148. package/dist/utils/indexes.d.ts +270 -0
  149. package/dist/utils/indexes.d.ts.map +1 -0
  150. package/dist/utils/indexes.js +526 -0
  151. package/dist/utils/logger.d.ts +24 -0
  152. package/dist/utils/logger.d.ts.map +1 -0
  153. package/dist/utils/operationUtils.d.ts +124 -0
  154. package/dist/utils/operationUtils.d.ts.map +1 -0
  155. package/dist/utils/operationUtils.js +175 -0
  156. package/dist/utils/parallelUtils.d.ts +72 -0
  157. package/dist/utils/parallelUtils.d.ts.map +1 -0
  158. package/dist/utils/parallelUtils.js +169 -0
  159. package/dist/utils/schemas.d.ts +374 -0
  160. package/dist/utils/schemas.d.ts.map +1 -0
  161. package/dist/utils/schemas.js +302 -2
  162. package/dist/utils/searchAlgorithms.d.ts +99 -0
  163. package/dist/utils/searchAlgorithms.d.ts.map +1 -0
  164. package/dist/utils/searchAlgorithms.js +167 -0
  165. package/dist/utils/searchCache.d.ts +108 -0
  166. package/dist/utils/searchCache.d.ts.map +1 -0
  167. package/dist/utils/taskScheduler.d.ts +290 -0
  168. package/dist/utils/taskScheduler.d.ts.map +1 -0
  169. package/dist/utils/taskScheduler.js +466 -0
  170. package/dist/workers/index.d.ts +12 -0
  171. package/dist/workers/index.d.ts.map +1 -0
  172. package/dist/workers/index.js +9 -0
  173. package/dist/workers/levenshteinWorker.d.ts +60 -0
  174. package/dist/workers/levenshteinWorker.d.ts.map +1 -0
  175. package/dist/workers/levenshteinWorker.js +98 -0
  176. package/package.json +17 -4
  177. package/dist/__tests__/edge-cases/edge-cases.test.js +0 -406
  178. package/dist/__tests__/integration/workflows.test.js +0 -449
  179. package/dist/__tests__/performance/benchmarks.test.js +0 -413
  180. package/dist/__tests__/unit/core/EntityManager.test.js +0 -334
  181. package/dist/__tests__/unit/core/GraphStorage.test.js +0 -205
  182. package/dist/__tests__/unit/core/RelationManager.test.js +0 -274
  183. package/dist/__tests__/unit/features/CompressionManager.test.js +0 -350
  184. package/dist/__tests__/unit/search/BasicSearch.test.js +0 -311
  185. package/dist/__tests__/unit/search/BooleanSearch.test.js +0 -432
  186. package/dist/__tests__/unit/search/FuzzySearch.test.js +0 -448
  187. package/dist/__tests__/unit/search/RankedSearch.test.js +0 -379
  188. package/dist/__tests__/unit/utils/levenshtein.test.js +0 -77
  189. package/dist/core/KnowledgeGraphManager.js +0 -423
  190. package/dist/features/BackupManager.js +0 -311
  191. package/dist/features/ExportManager.js +0 -305
  192. package/dist/features/ImportExportManager.js +0 -50
  193. package/dist/features/ImportManager.js +0 -328
  194. package/dist/memory-saved-searches.jsonl +0 -0
  195. package/dist/memory-tag-aliases.jsonl +0 -0
  196. package/dist/types/analytics.types.js +0 -6
  197. package/dist/types/entity.types.js +0 -7
  198. package/dist/types/import-export.types.js +0 -7
  199. package/dist/types/search.types.js +0 -7
  200. package/dist/types/tag.types.js +0 -6
  201. package/dist/utils/dateUtils.js +0 -89
  202. package/dist/utils/filterUtils.js +0 -155
  203. package/dist/utils/levenshtein.js +0 -62
  204. package/dist/utils/pathUtils.js +0 -115
  205. package/dist/utils/responseFormatter.js +0 -55
  206. package/dist/utils/tagUtils.js +0 -107
  207. package/dist/utils/tfidf.js +0 -90
  208. package/dist/utils/validationHelper.js +0 -99
  209. package/dist/utils/validationUtils.js +0 -109
@@ -1,13 +1,14 @@
1
1
  /**
2
2
  * Hierarchy Manager
3
3
  *
4
- * Manages entity parent-child relationships and hierarchical operations.
4
+ * Handles entity hierarchy operations (parent-child relationships).
5
+ * Extracted from EntityManager (Phase 4: Consolidate God Objects).
5
6
  *
6
- * @module features/HierarchyManager
7
+ * @module core/HierarchyManager
7
8
  */
8
9
  import { EntityNotFoundError, CycleDetectedError } from '../utils/errors.js';
9
10
  /**
10
- * Manages hierarchical entity relationships.
11
+ * Manages hierarchical relationships between entities.
11
12
  */
12
13
  export class HierarchyManager {
13
14
  storage;
@@ -28,22 +29,26 @@ export class HierarchyManager {
28
29
  * @throws {CycleDetectedError} If setting parent would create a cycle
29
30
  */
30
31
  async setEntityParent(entityName, parentName) {
31
- const graph = await this.storage.loadGraph();
32
- const entity = graph.entities.find(e => e.name === entityName);
33
- if (!entity) {
32
+ // Use read-only graph for validation checks
33
+ const readGraph = await this.storage.loadGraph();
34
+ const entityExists = readGraph.entities.find(e => e.name === entityName);
35
+ if (!entityExists) {
34
36
  throw new EntityNotFoundError(entityName);
35
37
  }
36
38
  // If setting a parent, validate it exists and doesn't create a cycle
37
39
  if (parentName !== null) {
38
- const parent = graph.entities.find(e => e.name === parentName);
40
+ const parent = readGraph.entities.find(e => e.name === parentName);
39
41
  if (!parent) {
40
42
  throw new EntityNotFoundError(parentName);
41
43
  }
42
44
  // Check for cycles
43
- if (this.wouldCreateCycle(graph, entityName, parentName)) {
45
+ if (this.wouldCreateCycle(readGraph, entityName, parentName)) {
44
46
  throw new CycleDetectedError(entityName, parentName);
45
47
  }
46
48
  }
49
+ // Get mutable copy for write operation
50
+ const graph = await this.storage.getGraphForMutation();
51
+ const entity = graph.entities.find(e => e.name === entityName);
47
52
  entity.parentId = parentName || undefined;
48
53
  entity.lastModified = new Date().toISOString();
49
54
  await this.storage.saveGraph(graph);
@@ -198,7 +203,7 @@ export class HierarchyManager {
198
203
  *
199
204
  * @param entityName - Entity name
200
205
  * @returns Depth (number of ancestors)
201
- * @throws Error if entity not found
206
+ * @throws {EntityNotFoundError} If entity not found
202
207
  */
203
208
  async getEntityDepth(entityName) {
204
209
  const ancestors = await this.getAncestors(entityName);
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Manager Context
3
+ *
4
+ * Central context holding all manager instances with lazy initialization.
5
+ * Provides direct manager access for toolHandlers.
6
+ * Phase 4: Removed convenience methods - use managers directly.
7
+ *
8
+ * @module core/ManagerContext
9
+ */
10
+ import { GraphStorage } from './GraphStorage.js';
11
+ import { EntityManager } from './EntityManager.js';
12
+ import { RelationManager } from './RelationManager.js';
13
+ import { ObservationManager } from './ObservationManager.js';
14
+ import { HierarchyManager } from './HierarchyManager.js';
15
+ import { GraphTraversal } from './GraphTraversal.js';
16
+ import { SearchManager } from '../search/SearchManager.js';
17
+ import { SemanticSearch } from '../search/index.js';
18
+ import { IOManager } from '../features/IOManager.js';
19
+ import { TagManager } from '../features/TagManager.js';
20
+ import { AnalyticsManager } from '../features/AnalyticsManager.js';
21
+ import { CompressionManager } from '../features/CompressionManager.js';
22
+ import { ArchiveManager } from '../features/ArchiveManager.js';
23
+ /**
24
+ * Context holding all manager instances with lazy initialization.
25
+ * Provides direct manager access for toolHandlers.
26
+ */
27
+ export declare class ManagerContext {
28
+ readonly storage: GraphStorage;
29
+ private readonly savedSearchesFilePath;
30
+ private readonly tagAliasesFilePath;
31
+ private _entityManager?;
32
+ private _relationManager?;
33
+ private _observationManager?;
34
+ private _hierarchyManager?;
35
+ private _graphTraversal?;
36
+ private _searchManager?;
37
+ private _semanticSearch?;
38
+ private _ioManager?;
39
+ private _tagManager?;
40
+ private _analyticsManager?;
41
+ private _compressionManager?;
42
+ private _archiveManager?;
43
+ constructor(memoryFilePath: string);
44
+ /** EntityManager - Entity CRUD and tag operations */
45
+ get entityManager(): EntityManager;
46
+ /** RelationManager - Relation CRUD */
47
+ get relationManager(): RelationManager;
48
+ /** ObservationManager - Observation CRUD */
49
+ get observationManager(): ObservationManager;
50
+ /** HierarchyManager - Entity hierarchy operations */
51
+ get hierarchyManager(): HierarchyManager;
52
+ /** GraphTraversal - Phase 4 Sprint 6-8: Graph traversal algorithms */
53
+ get graphTraversal(): GraphTraversal;
54
+ /** SearchManager - All search operations */
55
+ get searchManager(): SearchManager;
56
+ /**
57
+ * SemanticSearch - Phase 4 Sprint 12: Semantic similarity search.
58
+ * Returns null if no embedding provider is configured.
59
+ */
60
+ get semanticSearch(): SemanticSearch | null;
61
+ /** IOManager - Import, export, and backup operations */
62
+ get ioManager(): IOManager;
63
+ /** TagManager - Tag alias management */
64
+ get tagManager(): TagManager;
65
+ /** AnalyticsManager - Graph statistics and validation */
66
+ get analyticsManager(): AnalyticsManager;
67
+ /** CompressionManager - Duplicate detection and entity merging */
68
+ get compressionManager(): CompressionManager;
69
+ /** ArchiveManager - Entity archival operations */
70
+ get archiveManager(): ArchiveManager;
71
+ }
72
+ //# sourceMappingURL=ManagerContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ManagerContext.d.ts","sourceRoot":"","sources":["../../src/core/ManagerContext.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,cAAc,EAA6C,MAAM,oBAAoB,CAAC;AAC/F,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAG/D;;;GAGG;AACH,qBAAa,cAAc;IACzB,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAC/B,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAS;IAC/C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAS;IAG5C,OAAO,CAAC,cAAc,CAAC,CAAgB;IACvC,OAAO,CAAC,gBAAgB,CAAC,CAAkB;IAC3C,OAAO,CAAC,mBAAmB,CAAC,CAAqB;IACjD,OAAO,CAAC,iBAAiB,CAAC,CAAmB;IAC7C,OAAO,CAAC,eAAe,CAAC,CAAiB;IACzC,OAAO,CAAC,cAAc,CAAC,CAAgB;IACvC,OAAO,CAAC,eAAe,CAAC,CAAwB;IAChD,OAAO,CAAC,UAAU,CAAC,CAAY;IAC/B,OAAO,CAAC,WAAW,CAAC,CAAa;IACjC,OAAO,CAAC,iBAAiB,CAAC,CAAmB;IAC7C,OAAO,CAAC,mBAAmB,CAAC,CAAqB;IACjD,OAAO,CAAC,eAAe,CAAC,CAAiB;gBAE7B,cAAc,EAAE,MAAM;IAYlC,qDAAqD;IACrD,IAAI,aAAa,IAAI,aAAa,CAEjC;IAED,sCAAsC;IACtC,IAAI,eAAe,IAAI,eAAe,CAErC;IAED,4CAA4C;IAC5C,IAAI,kBAAkB,IAAI,kBAAkB,CAE3C;IAED,qDAAqD;IACrD,IAAI,gBAAgB,IAAI,gBAAgB,CAEvC;IAED,sEAAsE;IACtE,IAAI,cAAc,IAAI,cAAc,CAEnC;IAED,4CAA4C;IAC5C,IAAI,aAAa,IAAI,aAAa,CAEjC;IAED;;;OAGG;IACH,IAAI,cAAc,IAAI,cAAc,GAAG,IAAI,CAa1C;IAED,wDAAwD;IACxD,IAAI,SAAS,IAAI,SAAS,CAEzB;IAED,wCAAwC;IACxC,IAAI,UAAU,IAAI,UAAU,CAE3B;IAED,yDAAyD;IACzD,IAAI,gBAAgB,IAAI,gBAAgB,CAEvC;IAED,kEAAkE;IAClE,IAAI,kBAAkB,IAAI,kBAAkB,CAE3C;IAED,kDAAkD;IAClD,IAAI,cAAc,IAAI,cAAc,CAEnC;CACF"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Manager Context
3
+ *
4
+ * Central context holding all manager instances with lazy initialization.
5
+ * Provides direct manager access for toolHandlers.
6
+ * Phase 4: Removed convenience methods - use managers directly.
7
+ *
8
+ * @module core/ManagerContext
9
+ */
10
+ import path from 'path';
11
+ import { GraphStorage } from './GraphStorage.js';
12
+ import { EntityManager } from './EntityManager.js';
13
+ import { RelationManager } from './RelationManager.js';
14
+ import { ObservationManager } from './ObservationManager.js';
15
+ import { HierarchyManager } from './HierarchyManager.js';
16
+ import { GraphTraversal } from './GraphTraversal.js';
17
+ import { SearchManager } from '../search/SearchManager.js';
18
+ import { SemanticSearch, createEmbeddingService, createVectorStore } from '../search/index.js';
19
+ import { IOManager } from '../features/IOManager.js';
20
+ import { TagManager } from '../features/TagManager.js';
21
+ import { AnalyticsManager } from '../features/AnalyticsManager.js';
22
+ import { CompressionManager } from '../features/CompressionManager.js';
23
+ import { ArchiveManager } from '../features/ArchiveManager.js';
24
+ import { getEmbeddingConfig } from '../utils/constants.js';
25
+ /**
26
+ * Context holding all manager instances with lazy initialization.
27
+ * Provides direct manager access for toolHandlers.
28
+ */
29
+ export class ManagerContext {
30
+ storage;
31
+ savedSearchesFilePath;
32
+ tagAliasesFilePath;
33
+ // Lazy-initialized managers
34
+ _entityManager;
35
+ _relationManager;
36
+ _observationManager;
37
+ _hierarchyManager;
38
+ _graphTraversal;
39
+ _searchManager;
40
+ _semanticSearch;
41
+ _ioManager;
42
+ _tagManager;
43
+ _analyticsManager;
44
+ _compressionManager;
45
+ _archiveManager;
46
+ constructor(memoryFilePath) {
47
+ // Derive paths for saved searches and tag aliases
48
+ const dir = path.dirname(memoryFilePath);
49
+ const basename = path.basename(memoryFilePath, path.extname(memoryFilePath));
50
+ this.savedSearchesFilePath = path.join(dir, `${basename}-saved-searches.jsonl`);
51
+ this.tagAliasesFilePath = path.join(dir, `${basename}-tag-aliases.jsonl`);
52
+ this.storage = new GraphStorage(memoryFilePath);
53
+ }
54
+ // ==================== MANAGER ACCESSORS ====================
55
+ // Use these for direct manager access in toolHandlers
56
+ /** EntityManager - Entity CRUD and tag operations */
57
+ get entityManager() {
58
+ return (this._entityManager ??= new EntityManager(this.storage));
59
+ }
60
+ /** RelationManager - Relation CRUD */
61
+ get relationManager() {
62
+ return (this._relationManager ??= new RelationManager(this.storage));
63
+ }
64
+ /** ObservationManager - Observation CRUD */
65
+ get observationManager() {
66
+ return (this._observationManager ??= new ObservationManager(this.storage));
67
+ }
68
+ /** HierarchyManager - Entity hierarchy operations */
69
+ get hierarchyManager() {
70
+ return (this._hierarchyManager ??= new HierarchyManager(this.storage));
71
+ }
72
+ /** GraphTraversal - Phase 4 Sprint 6-8: Graph traversal algorithms */
73
+ get graphTraversal() {
74
+ return (this._graphTraversal ??= new GraphTraversal(this.storage));
75
+ }
76
+ /** SearchManager - All search operations */
77
+ get searchManager() {
78
+ return (this._searchManager ??= new SearchManager(this.storage, this.savedSearchesFilePath));
79
+ }
80
+ /**
81
+ * SemanticSearch - Phase 4 Sprint 12: Semantic similarity search.
82
+ * Returns null if no embedding provider is configured.
83
+ */
84
+ get semanticSearch() {
85
+ if (this._semanticSearch === undefined) {
86
+ const config = getEmbeddingConfig();
87
+ const embeddingService = createEmbeddingService(config);
88
+ if (embeddingService) {
89
+ const vectorStore = createVectorStore('jsonl'); // Use in-memory for now
90
+ this._semanticSearch = new SemanticSearch(embeddingService, vectorStore);
91
+ }
92
+ else {
93
+ this._semanticSearch = null;
94
+ }
95
+ }
96
+ return this._semanticSearch;
97
+ }
98
+ /** IOManager - Import, export, and backup operations */
99
+ get ioManager() {
100
+ return (this._ioManager ??= new IOManager(this.storage));
101
+ }
102
+ /** TagManager - Tag alias management */
103
+ get tagManager() {
104
+ return (this._tagManager ??= new TagManager(this.tagAliasesFilePath));
105
+ }
106
+ /** AnalyticsManager - Graph statistics and validation */
107
+ get analyticsManager() {
108
+ return (this._analyticsManager ??= new AnalyticsManager(this.storage));
109
+ }
110
+ /** CompressionManager - Duplicate detection and entity merging */
111
+ get compressionManager() {
112
+ return (this._compressionManager ??= new CompressionManager(this.storage));
113
+ }
114
+ /** ArchiveManager - Entity archival operations */
115
+ get archiveManager() {
116
+ return (this._archiveManager ??= new ArchiveManager(this.storage));
117
+ }
118
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Observation Manager
3
+ *
4
+ * Handles observation CRUD operations for entities.
5
+ * Extracted from EntityManager (Phase 4: Consolidate God Objects).
6
+ *
7
+ * @module core/ObservationManager
8
+ */
9
+ import type { GraphStorage } from './GraphStorage.js';
10
+ /**
11
+ * Manages observation operations for entities in the knowledge graph.
12
+ */
13
+ export declare class ObservationManager {
14
+ private storage;
15
+ constructor(storage: GraphStorage);
16
+ /**
17
+ * Add observations to multiple entities in a single batch operation.
18
+ *
19
+ * This method performs the following operations:
20
+ * - Adds new observations to specified entities
21
+ * - Filters out duplicate observations (already present)
22
+ * - Updates lastModified timestamp only if new observations were added
23
+ * - ATOMIC: All updates are saved in a single operation
24
+ *
25
+ * @param observations - Array of entity names and observations to add
26
+ * @returns Promise resolving to array of results showing which observations were added
27
+ * @throws {EntityNotFoundError} If any entity is not found
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const manager = new ObservationManager(storage);
32
+ *
33
+ * // Add observations to multiple entities
34
+ * const results = await manager.addObservations([
35
+ * { entityName: 'Alice', contents: ['Completed project X', 'Started project Y'] },
36
+ * { entityName: 'Bob', contents: ['Joined team meeting'] }
37
+ * ]);
38
+ *
39
+ * // Check what was added (duplicates are filtered out)
40
+ * results.forEach(r => {
41
+ * console.log(`${r.entityName}: added ${r.addedObservations.length} new observations`);
42
+ * });
43
+ * ```
44
+ */
45
+ addObservations(observations: {
46
+ entityName: string;
47
+ contents: string[];
48
+ }[]): Promise<{
49
+ entityName: string;
50
+ addedObservations: string[];
51
+ }[]>;
52
+ /**
53
+ * Delete observations from multiple entities in a single batch operation.
54
+ *
55
+ * This method performs the following operations:
56
+ * - Removes specified observations from entities
57
+ * - Updates lastModified timestamp only if observations were deleted
58
+ * - Silently ignores entities that don't exist (no error thrown)
59
+ * - ATOMIC: All deletions are saved in a single operation
60
+ *
61
+ * @param deletions - Array of entity names and observations to delete
62
+ * @returns Promise that resolves when deletion is complete
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const manager = new ObservationManager(storage);
67
+ *
68
+ * // Delete observations from multiple entities
69
+ * await manager.deleteObservations([
70
+ * { entityName: 'Alice', observations: ['Old observation 1', 'Old observation 2'] },
71
+ * { entityName: 'Bob', observations: ['Outdated info'] }
72
+ * ]);
73
+ *
74
+ * // Safe to delete from non-existent entities (no error)
75
+ * await manager.deleteObservations([
76
+ * { entityName: 'NonExistent', observations: ['Some text'] }
77
+ * ]); // No error thrown
78
+ * ```
79
+ */
80
+ deleteObservations(deletions: {
81
+ entityName: string;
82
+ observations: string[];
83
+ }[]): Promise<void>;
84
+ }
85
+ //# sourceMappingURL=ObservationManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ObservationManager.d.ts","sourceRoot":"","sources":["../../src/core/ObservationManager.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAGtD;;GAEG;AACH,qBAAa,kBAAkB;IACjB,OAAO,CAAC,OAAO;gBAAP,OAAO,EAAE,YAAY;IAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACG,eAAe,CACnB,YAAY,EAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,GACzD,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,iBAAiB,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,CAAC;IAiCjE;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACG,kBAAkB,CACtB,SAAS,EAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,GAC1D,OAAO,CAAC,IAAI,CAAC;CAyBjB"}
@@ -1,13 +1,14 @@
1
1
  /**
2
2
  * Observation Manager
3
3
  *
4
- * Handles CRUD operations for observations within entities.
4
+ * Handles observation CRUD operations for entities.
5
+ * Extracted from EntityManager (Phase 4: Consolidate God Objects).
5
6
  *
6
7
  * @module core/ObservationManager
7
8
  */
8
9
  import { EntityNotFoundError } from '../utils/errors.js';
9
10
  /**
10
- * Manages observation operations with automatic timestamp handling.
11
+ * Manages observation operations for entities in the knowledge graph.
11
12
  */
12
13
  export class ObservationManager {
13
14
  storage;
@@ -18,112 +19,105 @@ export class ObservationManager {
18
19
  * Add observations to multiple entities in a single batch operation.
19
20
  *
20
21
  * This method performs the following operations:
21
- * - Validates that all specified entities exist
22
- * - Filters out duplicate observations (observations already present)
23
- * - Adds new observations to each entity
24
- * - Automatically updates lastModified timestamp for modified entities
22
+ * - Adds new observations to specified entities
23
+ * - Filters out duplicate observations (already present)
24
+ * - Updates lastModified timestamp only if new observations were added
25
+ * - ATOMIC: All updates are saved in a single operation
25
26
  *
26
- * @param observations - Array of objects, each containing entityName and array of observation contents
27
- * @returns Promise resolving to array of results showing added observations per entity
28
- * @throws {EntityNotFoundError} If any specified entity does not exist
27
+ * @param observations - Array of entity names and observations to add
28
+ * @returns Promise resolving to array of results showing which observations were added
29
+ * @throws {EntityNotFoundError} If any entity is not found
29
30
  *
30
31
  * @example
31
32
  * ```typescript
32
33
  * const manager = new ObservationManager(storage);
33
34
  *
34
- * // Add observations to single entity
35
+ * // Add observations to multiple entities
35
36
  * const results = await manager.addObservations([
36
- * {
37
- * entityName: 'Alice',
38
- * contents: ['Works on frontend', 'Expertise in React']
39
- * }
37
+ * { entityName: 'Alice', contents: ['Completed project X', 'Started project Y'] },
38
+ * { entityName: 'Bob', contents: ['Joined team meeting'] }
40
39
  * ]);
41
- * console.log(results[0].addedObservations); // ['Works on frontend', 'Expertise in React']
42
40
  *
43
- * // Add observations to multiple entities at once
44
- * await manager.addObservations([
45
- * { entityName: 'Bob', contents: ['Team lead', 'Experienced in Node.js'] },
46
- * { entityName: 'Charlie', contents: ['New hire', 'Learning the codebase'] }
47
- * ]);
48
- *
49
- * // Duplicate observations are filtered out
50
- * await manager.addObservations([
51
- * { entityName: 'Alice', contents: ['Works on frontend'] } // Already exists, won't be added
52
- * ]);
41
+ * // Check what was added (duplicates are filtered out)
42
+ * results.forEach(r => {
43
+ * console.log(`${r.entityName}: added ${r.addedObservations.length} new observations`);
44
+ * });
53
45
  * ```
54
46
  */
55
47
  async addObservations(observations) {
56
- const graph = await this.storage.loadGraph();
48
+ // Get mutable graph for atomic update
49
+ const graph = await this.storage.getGraphForMutation();
57
50
  const timestamp = new Date().toISOString();
58
- const results = observations.map(o => {
51
+ const results = [];
52
+ let hasChanges = false;
53
+ for (const o of observations) {
59
54
  const entity = graph.entities.find(e => e.name === o.entityName);
60
55
  if (!entity) {
61
56
  throw new EntityNotFoundError(o.entityName);
62
57
  }
63
58
  const newObservations = o.contents.filter(content => !entity.observations.includes(content));
64
- entity.observations.push(...newObservations);
65
- // Update lastModified if observations were added
66
59
  if (newObservations.length > 0) {
60
+ // Add new observations directly to the entity
61
+ entity.observations.push(...newObservations);
67
62
  entity.lastModified = timestamp;
63
+ hasChanges = true;
68
64
  }
69
- return {
70
- entityName: o.entityName,
71
- addedObservations: newObservations,
72
- };
73
- });
74
- await this.storage.saveGraph(graph);
65
+ results.push({ entityName: o.entityName, addedObservations: newObservations });
66
+ }
67
+ // Save all changes in a single atomic operation
68
+ if (hasChanges) {
69
+ await this.storage.saveGraph(graph);
70
+ }
75
71
  return results;
76
72
  }
77
73
  /**
78
- * Delete specific observations from multiple entities in a single batch operation.
74
+ * Delete observations from multiple entities in a single batch operation.
79
75
  *
80
76
  * This method performs the following operations:
81
- * - Removes specified observations from each entity
77
+ * - Removes specified observations from entities
78
+ * - Updates lastModified timestamp only if observations were deleted
82
79
  * - Silently ignores entities that don't exist (no error thrown)
83
- * - Silently ignores observations that don't exist in the entity
84
- * - Automatically updates lastModified timestamp for modified entities
80
+ * - ATOMIC: All deletions are saved in a single operation
85
81
  *
86
- * @param deletions - Array of objects, each containing entityName and array of observations to delete
82
+ * @param deletions - Array of entity names and observations to delete
87
83
  * @returns Promise that resolves when deletion is complete
88
84
  *
89
85
  * @example
90
86
  * ```typescript
91
87
  * const manager = new ObservationManager(storage);
92
88
  *
93
- * // Delete specific observations from single entity
89
+ * // Delete observations from multiple entities
94
90
  * await manager.deleteObservations([
95
- * {
96
- * entityName: 'Alice',
97
- * observations: ['Works on frontend', 'Expertise in React']
98
- * }
91
+ * { entityName: 'Alice', observations: ['Old observation 1', 'Old observation 2'] },
92
+ * { entityName: 'Bob', observations: ['Outdated info'] }
99
93
  * ]);
100
94
  *
101
- * // Delete observations from multiple entities at once
95
+ * // Safe to delete from non-existent entities (no error)
102
96
  * await manager.deleteObservations([
103
- * { entityName: 'Bob', observations: ['Team lead'] },
104
- * { entityName: 'Charlie', observations: ['New hire', 'Learning the codebase'] }
105
- * ]);
106
- *
107
- * // Safe to delete non-existent observations or from non-existent entities
108
- * await manager.deleteObservations([
109
- * { entityName: 'NonExistent', observations: ['Some observation'] } // No error
110
- * ]);
97
+ * { entityName: 'NonExistent', observations: ['Some text'] }
98
+ * ]); // No error thrown
111
99
  * ```
112
100
  */
113
101
  async deleteObservations(deletions) {
114
- const graph = await this.storage.loadGraph();
102
+ // Get mutable graph for atomic update
103
+ const graph = await this.storage.getGraphForMutation();
115
104
  const timestamp = new Date().toISOString();
105
+ let hasChanges = false;
116
106
  deletions.forEach(d => {
117
107
  const entity = graph.entities.find(e => e.name === d.entityName);
118
108
  if (entity) {
119
109
  const originalLength = entity.observations.length;
120
110
  entity.observations = entity.observations.filter(o => !d.observations.includes(o));
121
- // Update lastModified if observations were deleted
111
+ // Update lastModified timestamp if observations were deleted
122
112
  if (entity.observations.length < originalLength) {
123
113
  entity.lastModified = timestamp;
114
+ hasChanges = true;
124
115
  }
125
116
  }
126
117
  });
127
- await this.storage.saveGraph(graph);
118
+ // Save all changes in a single atomic operation
119
+ if (hasChanges) {
120
+ await this.storage.saveGraph(graph);
121
+ }
128
122
  }
129
123
  }
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Relation Manager
3
+ *
4
+ * Handles CRUD operations for relations in the knowledge graph.
5
+ *
6
+ * @module core/RelationManager
7
+ */
8
+ import type { Relation } from '../types/index.js';
9
+ import type { GraphStorage } from './GraphStorage.js';
10
+ /**
11
+ * Manages relation operations with automatic timestamp handling.
12
+ */
13
+ export declare class RelationManager {
14
+ private storage;
15
+ constructor(storage: GraphStorage);
16
+ /**
17
+ * Create multiple relations in a single batch operation.
18
+ *
19
+ * This method performs the following operations:
20
+ * - Validates that all referenced entities exist (prevents dangling relations)
21
+ * - Filters out duplicate relations (same from, to, and relationType)
22
+ * - Automatically adds createdAt and lastModified timestamps
23
+ *
24
+ * A relation is considered duplicate if another relation exists with the same:
25
+ * - from entity name
26
+ * - to entity name
27
+ * - relationType
28
+ *
29
+ * @param relations - Array of relations to create
30
+ * @returns Promise resolving to array of newly created relations (excludes duplicates)
31
+ * @throws {ValidationError} If any relation references non-existent entities
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const manager = new RelationManager(storage);
36
+ *
37
+ * // Create single relation
38
+ * const results = await manager.createRelations([{
39
+ * from: 'Alice',
40
+ * to: 'Bob',
41
+ * relationType: 'works_with'
42
+ * }]);
43
+ *
44
+ * // Create multiple relations at once
45
+ * await manager.createRelations([
46
+ * { from: 'Alice', to: 'Project_X', relationType: 'contributes_to' },
47
+ * { from: 'Bob', to: 'Project_X', relationType: 'leads' },
48
+ * { from: 'Charlie', to: 'Alice', relationType: 'reports_to' }
49
+ * ]);
50
+ *
51
+ * // Duplicate relations are filtered out
52
+ * await manager.createRelations([
53
+ * { from: 'Alice', to: 'Bob', relationType: 'works_with' } // Already exists, won't be added
54
+ * ]);
55
+ * ```
56
+ */
57
+ createRelations(relations: Relation[]): Promise<Relation[]>;
58
+ /**
59
+ * Delete multiple relations in a single batch operation.
60
+ *
61
+ * This method performs the following operations:
62
+ * - Removes all specified relations from the graph
63
+ * - Automatically updates lastModified timestamp for all affected entities
64
+ * - Silently ignores relations that don't exist (no error thrown)
65
+ *
66
+ * An entity is considered "affected" if it appears as either the source (from)
67
+ * or target (to) of any deleted relation.
68
+ *
69
+ * @param relations - Array of relations to delete. Each relation is matched by from, to, and relationType.
70
+ * @returns Promise that resolves when deletion is complete
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const manager = new RelationManager(storage);
75
+ *
76
+ * // Delete single relation
77
+ * await manager.deleteRelations([{
78
+ * from: 'Alice',
79
+ * to: 'Bob',
80
+ * relationType: 'works_with'
81
+ * }]);
82
+ *
83
+ * // Delete multiple relations at once
84
+ * await manager.deleteRelations([
85
+ * { from: 'Alice', to: 'Project_X', relationType: 'contributes_to' },
86
+ * { from: 'Bob', to: 'Project_X', relationType: 'leads' }
87
+ * ]);
88
+ * // Note: Alice, Bob, and Project_X will all have their lastModified timestamp updated
89
+ *
90
+ * // Safe to delete non-existent relations
91
+ * await manager.deleteRelations([
92
+ * { from: 'NonExistent', to: 'AlsoNonExistent', relationType: 'fake' } // No error
93
+ * ]);
94
+ * ```
95
+ */
96
+ deleteRelations(relations: Relation[]): Promise<void>;
97
+ /**
98
+ * Retrieve all relations involving a specific entity.
99
+ *
100
+ * This is a read-only operation that returns all relations where the specified
101
+ * entity appears as either the source (from) or target (to) of the relation.
102
+ * Entity names are case-sensitive.
103
+ *
104
+ * @param entityName - The unique name of the entity to find relations for
105
+ * @returns Promise resolving to array of Relation objects (empty array if no relations found)
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * const manager = new RelationManager(storage);
110
+ *
111
+ * // Get all relations for an entity
112
+ * const aliceRelations = await manager.getRelations('Alice');
113
+ * // Returns: [
114
+ * // { from: 'Alice', to: 'Bob', relationType: 'works_with' },
115
+ * // { from: 'Alice', to: 'Project_X', relationType: 'contributes_to' },
116
+ * // { from: 'Charlie', to: 'Alice', relationType: 'reports_to' }
117
+ * // ]
118
+ *
119
+ * // Process relations by type
120
+ * const relations = await manager.getRelations('Alice');
121
+ * const outgoing = relations.filter(r => r.from === 'Alice');
122
+ * const incoming = relations.filter(r => r.to === 'Alice');
123
+ *
124
+ * // Handle entity with no relations
125
+ * const noRelations = await manager.getRelations('IsolatedEntity');
126
+ * console.log(noRelations); // []
127
+ * ```
128
+ */
129
+ getRelations(entityName: string): Promise<Relation[]>;
130
+ }
131
+ //# sourceMappingURL=RelationManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RelationManager.d.ts","sourceRoot":"","sources":["../../src/core/RelationManager.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAKtD;;GAEG;AACH,qBAAa,eAAe;IACd,OAAO,CAAC,OAAO;gBAAP,OAAO,EAAE,YAAY;IAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwCG;IACG,eAAe,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAiEjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;IACG,eAAe,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAqC3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA+BG;IACG,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;CAK5D"}