@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.
Files changed (158) hide show
  1. package/README.md +88 -102
  2. package/dist/index.js +6 -0
  3. package/dist/server/toolDefinitions.d.ts.map +1 -1
  4. package/dist/server/toolDefinitions.js +745 -3
  5. package/dist/server/toolHandlers.d.ts +1 -9
  6. package/dist/server/toolHandlers.d.ts.map +1 -1
  7. package/dist/server/toolHandlers.js +859 -11
  8. package/package.json +9 -6
  9. package/dist/core/EntityManager.d.ts +0 -263
  10. package/dist/core/EntityManager.d.ts.map +0 -1
  11. package/dist/core/EntityManager.js +0 -478
  12. package/dist/core/GraphStorage.d.ts +0 -344
  13. package/dist/core/GraphStorage.d.ts.map +0 -1
  14. package/dist/core/GraphStorage.js +0 -708
  15. package/dist/core/GraphTraversal.d.ts +0 -136
  16. package/dist/core/GraphTraversal.d.ts.map +0 -1
  17. package/dist/core/GraphTraversal.js +0 -556
  18. package/dist/core/HierarchyManager.d.ts +0 -111
  19. package/dist/core/HierarchyManager.d.ts.map +0 -1
  20. package/dist/core/HierarchyManager.js +0 -224
  21. package/dist/core/ManagerContext.d.ts +0 -72
  22. package/dist/core/ManagerContext.d.ts.map +0 -1
  23. package/dist/core/ManagerContext.js +0 -118
  24. package/dist/core/ObservationManager.d.ts +0 -85
  25. package/dist/core/ObservationManager.d.ts.map +0 -1
  26. package/dist/core/ObservationManager.js +0 -123
  27. package/dist/core/RelationManager.d.ts +0 -131
  28. package/dist/core/RelationManager.d.ts.map +0 -1
  29. package/dist/core/RelationManager.js +0 -210
  30. package/dist/core/SQLiteStorage.d.ts +0 -354
  31. package/dist/core/SQLiteStorage.d.ts.map +0 -1
  32. package/dist/core/SQLiteStorage.js +0 -917
  33. package/dist/core/StorageFactory.d.ts +0 -45
  34. package/dist/core/StorageFactory.d.ts.map +0 -1
  35. package/dist/core/StorageFactory.js +0 -64
  36. package/dist/core/TransactionManager.d.ts +0 -259
  37. package/dist/core/TransactionManager.d.ts.map +0 -1
  38. package/dist/core/TransactionManager.js +0 -392
  39. package/dist/core/index.d.ts +0 -16
  40. package/dist/core/index.d.ts.map +0 -1
  41. package/dist/core/index.js +0 -17
  42. package/dist/features/AnalyticsManager.d.ts +0 -44
  43. package/dist/features/AnalyticsManager.d.ts.map +0 -1
  44. package/dist/features/AnalyticsManager.js +0 -223
  45. package/dist/features/ArchiveManager.d.ts +0 -121
  46. package/dist/features/ArchiveManager.d.ts.map +0 -1
  47. package/dist/features/ArchiveManager.js +0 -240
  48. package/dist/features/CompressionManager.d.ts +0 -109
  49. package/dist/features/CompressionManager.d.ts.map +0 -1
  50. package/dist/features/CompressionManager.js +0 -404
  51. package/dist/features/IOManager.d.ts +0 -221
  52. package/dist/features/IOManager.d.ts.map +0 -1
  53. package/dist/features/IOManager.js +0 -997
  54. package/dist/features/StreamingExporter.d.ts +0 -105
  55. package/dist/features/StreamingExporter.d.ts.map +0 -1
  56. package/dist/features/StreamingExporter.js +0 -150
  57. package/dist/features/TagManager.d.ts +0 -147
  58. package/dist/features/TagManager.d.ts.map +0 -1
  59. package/dist/features/TagManager.js +0 -210
  60. package/dist/features/index.d.ts +0 -12
  61. package/dist/features/index.d.ts.map +0 -1
  62. package/dist/features/index.js +0 -11
  63. package/dist/search/BasicSearch.d.ts +0 -51
  64. package/dist/search/BasicSearch.d.ts.map +0 -1
  65. package/dist/search/BasicSearch.js +0 -137
  66. package/dist/search/BooleanSearch.d.ts +0 -98
  67. package/dist/search/BooleanSearch.d.ts.map +0 -1
  68. package/dist/search/BooleanSearch.js +0 -430
  69. package/dist/search/EmbeddingService.d.ts +0 -178
  70. package/dist/search/EmbeddingService.d.ts.map +0 -1
  71. package/dist/search/EmbeddingService.js +0 -358
  72. package/dist/search/FuzzySearch.d.ts +0 -118
  73. package/dist/search/FuzzySearch.d.ts.map +0 -1
  74. package/dist/search/FuzzySearch.js +0 -312
  75. package/dist/search/RankedSearch.d.ts +0 -71
  76. package/dist/search/RankedSearch.d.ts.map +0 -1
  77. package/dist/search/RankedSearch.js +0 -238
  78. package/dist/search/SavedSearchManager.d.ts +0 -79
  79. package/dist/search/SavedSearchManager.d.ts.map +0 -1
  80. package/dist/search/SavedSearchManager.js +0 -145
  81. package/dist/search/SearchFilterChain.d.ts +0 -120
  82. package/dist/search/SearchFilterChain.d.ts.map +0 -1
  83. package/dist/search/SearchFilterChain.js +0 -185
  84. package/dist/search/SearchManager.d.ts +0 -285
  85. package/dist/search/SearchManager.d.ts.map +0 -1
  86. package/dist/search/SearchManager.js +0 -338
  87. package/dist/search/SearchSuggestions.d.ts +0 -27
  88. package/dist/search/SearchSuggestions.d.ts.map +0 -1
  89. package/dist/search/SearchSuggestions.js +0 -57
  90. package/dist/search/SemanticSearch.d.ts +0 -146
  91. package/dist/search/SemanticSearch.d.ts.map +0 -1
  92. package/dist/search/SemanticSearch.js +0 -313
  93. package/dist/search/TFIDFIndexManager.d.ts +0 -63
  94. package/dist/search/TFIDFIndexManager.d.ts.map +0 -1
  95. package/dist/search/TFIDFIndexManager.js +0 -215
  96. package/dist/search/VectorStore.d.ts +0 -235
  97. package/dist/search/VectorStore.d.ts.map +0 -1
  98. package/dist/search/VectorStore.js +0 -311
  99. package/dist/search/index.d.ts +0 -18
  100. package/dist/search/index.d.ts.map +0 -1
  101. package/dist/search/index.js +0 -21
  102. package/dist/types/index.d.ts +0 -13
  103. package/dist/types/index.d.ts.map +0 -1
  104. package/dist/types/index.js +0 -12
  105. package/dist/types/types.d.ts +0 -1234
  106. package/dist/types/types.d.ts.map +0 -1
  107. package/dist/types/types.js +0 -9
  108. package/dist/utils/compressedCache.d.ts +0 -192
  109. package/dist/utils/compressedCache.d.ts.map +0 -1
  110. package/dist/utils/compressedCache.js +0 -309
  111. package/dist/utils/compressionUtil.d.ts +0 -214
  112. package/dist/utils/compressionUtil.d.ts.map +0 -1
  113. package/dist/utils/compressionUtil.js +0 -247
  114. package/dist/utils/constants.d.ts +0 -245
  115. package/dist/utils/constants.d.ts.map +0 -1
  116. package/dist/utils/constants.js +0 -252
  117. package/dist/utils/entityUtils.d.ts +0 -321
  118. package/dist/utils/entityUtils.d.ts.map +0 -1
  119. package/dist/utils/entityUtils.js +0 -538
  120. package/dist/utils/errors.d.ts +0 -77
  121. package/dist/utils/errors.d.ts.map +0 -1
  122. package/dist/utils/errors.js +0 -121
  123. package/dist/utils/formatters.d.ts +0 -145
  124. package/dist/utils/formatters.d.ts.map +0 -1
  125. package/dist/utils/formatters.js +0 -132
  126. package/dist/utils/index.d.ts +0 -22
  127. package/dist/utils/index.d.ts.map +0 -1
  128. package/dist/utils/index.js +0 -75
  129. package/dist/utils/indexes.d.ts +0 -270
  130. package/dist/utils/indexes.d.ts.map +0 -1
  131. package/dist/utils/indexes.js +0 -526
  132. package/dist/utils/logger.d.ts +0 -24
  133. package/dist/utils/logger.d.ts.map +0 -1
  134. package/dist/utils/logger.js +0 -33
  135. package/dist/utils/parallelUtils.d.ts +0 -72
  136. package/dist/utils/parallelUtils.d.ts.map +0 -1
  137. package/dist/utils/parallelUtils.js +0 -169
  138. package/dist/utils/schemas.d.ts +0 -374
  139. package/dist/utils/schemas.d.ts.map +0 -1
  140. package/dist/utils/schemas.js +0 -484
  141. package/dist/utils/searchAlgorithms.d.ts +0 -99
  142. package/dist/utils/searchAlgorithms.d.ts.map +0 -1
  143. package/dist/utils/searchAlgorithms.js +0 -167
  144. package/dist/utils/searchCache.d.ts +0 -108
  145. package/dist/utils/searchCache.d.ts.map +0 -1
  146. package/dist/utils/searchCache.js +0 -209
  147. package/dist/utils/taskScheduler.d.ts +0 -290
  148. package/dist/utils/taskScheduler.d.ts.map +0 -1
  149. package/dist/utils/taskScheduler.js +0 -466
  150. package/dist/workers/WorkerPool.d.ts +0 -81
  151. package/dist/workers/WorkerPool.d.ts.map +0 -1
  152. package/dist/workers/WorkerPool.js +0 -121
  153. package/dist/workers/index.d.ts +0 -12
  154. package/dist/workers/index.d.ts.map +0 -1
  155. package/dist/workers/index.js +0 -9
  156. package/dist/workers/levenshteinWorker.d.ts +0 -60
  157. package/dist/workers/levenshteinWorker.d.ts.map +0 -1
  158. package/dist/workers/levenshteinWorker.js +0 -98
@@ -1,538 +0,0 @@
1
- /**
2
- * Entity Utilities
3
- *
4
- * Consolidated module for entity-related utilities including:
5
- * - Entity lookup and manipulation functions
6
- * - Tag normalization and matching
7
- * - Date parsing and validation
8
- * - Entity filtering by various criteria
9
- * - Path utilities and validation
10
- *
11
- * @module utils/entityUtils
12
- */
13
- import { promises as fs } from 'fs';
14
- import path from 'path';
15
- import { fileURLToPath } from 'url';
16
- import { EntityNotFoundError, FileOperationError } from './errors.js';
17
- export function findEntityByName(graph, name, throwIfNotFound = true) {
18
- const entity = graph.entities.find(e => e.name === name);
19
- if (!entity && throwIfNotFound) {
20
- throw new EntityNotFoundError(name);
21
- }
22
- return entity ?? null;
23
- }
24
- /**
25
- * Finds multiple entities by name.
26
- *
27
- * @param graph - The knowledge graph to search
28
- * @param names - Array of entity names to find
29
- * @param throwIfAnyNotFound - Whether to throw if any entity doesn't exist (default: true)
30
- * @returns Array of found entities (may be shorter than names if throwIfAnyNotFound is false)
31
- * @throws EntityNotFoundError if any entity not found and throwIfAnyNotFound is true
32
- */
33
- export function findEntitiesByNames(graph, names, throwIfAnyNotFound = true) {
34
- const entities = [];
35
- for (const name of names) {
36
- const entity = findEntityByName(graph, name, false);
37
- if (entity) {
38
- entities.push(entity);
39
- }
40
- else if (throwIfAnyNotFound) {
41
- throw new EntityNotFoundError(name);
42
- }
43
- }
44
- return entities;
45
- }
46
- /**
47
- * Checks if an entity exists in the graph.
48
- *
49
- * @param graph - The knowledge graph to search
50
- * @param name - The entity name to check
51
- * @returns true if entity exists, false otherwise
52
- */
53
- export function entityExists(graph, name) {
54
- return graph.entities.some(e => e.name === name);
55
- }
56
- /**
57
- * Gets the index of an entity in the graph's entities array.
58
- *
59
- * @param graph - The knowledge graph to search
60
- * @param name - The entity name to find
61
- * @returns The index if found, -1 otherwise
62
- */
63
- export function getEntityIndex(graph, name) {
64
- return graph.entities.findIndex(e => e.name === name);
65
- }
66
- /**
67
- * Removes an entity from the graph by name.
68
- * Mutates the graph's entities array in place.
69
- *
70
- * @param graph - The knowledge graph to modify
71
- * @param name - The entity name to remove
72
- * @returns true if entity was removed, false if not found
73
- */
74
- export function removeEntityByName(graph, name) {
75
- const index = getEntityIndex(graph, name);
76
- if (index === -1)
77
- return false;
78
- graph.entities.splice(index, 1);
79
- return true;
80
- }
81
- /**
82
- * Gets all entity names as a Set for fast lookup.
83
- *
84
- * @param graph - The knowledge graph
85
- * @returns Set of all entity names
86
- */
87
- export function getEntityNameSet(graph) {
88
- return new Set(graph.entities.map(e => e.name));
89
- }
90
- /**
91
- * Groups entities by their type.
92
- *
93
- * @param entities - Array of entities to group
94
- * @returns Map of entity type to array of entities
95
- */
96
- export function groupEntitiesByType(entities) {
97
- const groups = new Map();
98
- for (const entity of entities) {
99
- const type = entity.entityType;
100
- if (!groups.has(type)) {
101
- groups.set(type, []);
102
- }
103
- groups.get(type).push(entity);
104
- }
105
- return groups;
106
- }
107
- /**
108
- * Updates the lastModified timestamp on an entity.
109
- * Mutates the entity in place.
110
- *
111
- * @param entity - The entity to update
112
- * @returns The updated entity (same reference)
113
- */
114
- export function touchEntity(entity) {
115
- entity.lastModified = new Date().toISOString();
116
- return entity;
117
- }
118
- // ==================== Tag Normalization and Matching ====================
119
- /**
120
- * Normalizes a single tag to lowercase and trimmed.
121
- *
122
- * @param tag - Tag to normalize
123
- * @returns Normalized tag
124
- */
125
- export function normalizeTag(tag) {
126
- return tag.toLowerCase().trim();
127
- }
128
- /**
129
- * Normalizes an array of tags to lowercase.
130
- * Handles undefined/null input gracefully.
131
- *
132
- * @param tags - Array of tags to normalize, or undefined
133
- * @returns Normalized tags array, or empty array if input is undefined/null
134
- */
135
- export function normalizeTags(tags) {
136
- if (!tags || tags.length === 0)
137
- return [];
138
- return tags.map(tag => tag.toLowerCase());
139
- }
140
- /**
141
- * Checks if an entity's tags include any of the specified search tags.
142
- * Both inputs are normalized before comparison.
143
- *
144
- * @param entityTags - Tags on the entity (may be undefined)
145
- * @param searchTags - Tags to search for (may be undefined)
146
- * @returns true if any search tag matches any entity tag, false if no match or either is empty
147
- */
148
- export function hasMatchingTag(entityTags, searchTags) {
149
- if (!entityTags || entityTags.length === 0)
150
- return false;
151
- if (!searchTags || searchTags.length === 0)
152
- return false;
153
- const normalizedEntity = normalizeTags(entityTags);
154
- const normalizedSearch = normalizeTags(searchTags);
155
- return normalizedSearch.some(tag => normalizedEntity.includes(tag));
156
- }
157
- /**
158
- * Checks if entity tags include ALL of the specified required tags.
159
- *
160
- * @param entityTags - Tags on the entity (may be undefined)
161
- * @param requiredTags - All tags that must be present
162
- * @returns true if all required tags are present
163
- */
164
- export function hasAllTags(entityTags, requiredTags) {
165
- if (!entityTags || entityTags.length === 0)
166
- return false;
167
- if (requiredTags.length === 0)
168
- return true;
169
- const normalizedEntity = normalizeTags(entityTags);
170
- return normalizeTags(requiredTags).every(tag => normalizedEntity.includes(tag));
171
- }
172
- /**
173
- * Filters entities by tag match.
174
- * Returns all entities if searchTags is empty or undefined.
175
- *
176
- * @param entities - Array of entities with optional tags property
177
- * @param searchTags - Tags to filter by
178
- * @returns Filtered entities that have at least one matching tag
179
- */
180
- export function filterByTags(entities, searchTags) {
181
- if (!searchTags || searchTags.length === 0) {
182
- return entities;
183
- }
184
- const normalizedSearch = normalizeTags(searchTags);
185
- return entities.filter(entity => {
186
- if (!entity.tags || entity.tags.length === 0)
187
- return false;
188
- const normalizedEntity = normalizeTags(entity.tags);
189
- return normalizedSearch.some(tag => normalizedEntity.includes(tag));
190
- });
191
- }
192
- /**
193
- * Adds new tags to an existing tag array, avoiding duplicates.
194
- * All tags are normalized to lowercase.
195
- *
196
- * @param existingTags - Current tags (may be undefined)
197
- * @param newTags - Tags to add
198
- * @returns Combined tags array with no duplicates
199
- */
200
- export function addUniqueTags(existingTags, newTags) {
201
- const existing = normalizeTags(existingTags);
202
- const toAdd = normalizeTags(newTags);
203
- const uniqueNew = toAdd.filter(tag => !existing.includes(tag));
204
- return [...existing, ...uniqueNew];
205
- }
206
- /**
207
- * Removes specified tags from an existing tag array.
208
- * Comparison is case-insensitive.
209
- *
210
- * @param existingTags - Current tags (may be undefined)
211
- * @param tagsToRemove - Tags to remove
212
- * @returns Tags array with specified tags removed
213
- */
214
- export function removeTags(existingTags, tagsToRemove) {
215
- if (!existingTags || existingTags.length === 0)
216
- return [];
217
- const toRemoveNormalized = normalizeTags(tagsToRemove);
218
- return existingTags.filter(tag => !toRemoveNormalized.includes(tag.toLowerCase()));
219
- }
220
- // ==================== Date Utilities ====================
221
- /**
222
- * Check if a date falls within a specified range.
223
- *
224
- * @param date - ISO 8601 date string to check (may be undefined)
225
- * @param start - Optional start date (inclusive)
226
- * @param end - Optional end date (inclusive)
227
- * @returns True if date is within range or no filters are set
228
- *
229
- * @example
230
- * ```typescript
231
- * isWithinDateRange('2024-06-15T00:00:00Z', '2024-01-01T00:00:00Z', '2024-12-31T23:59:59Z'); // true
232
- * isWithinDateRange('2024-06-15T00:00:00Z', '2024-07-01T00:00:00Z'); // false
233
- * isWithinDateRange(undefined); // true (no filters)
234
- * isWithinDateRange(undefined, '2024-01-01T00:00:00Z'); // false (has filter but no date)
235
- * ```
236
- */
237
- export function isWithinDateRange(date, start, end) {
238
- // If no filters set, always pass
239
- if (!start && !end) {
240
- return true;
241
- }
242
- // If date is undefined but we have filters, fail
243
- if (!date) {
244
- return false;
245
- }
246
- const dateObj = new Date(date);
247
- if (isNaN(dateObj.getTime())) {
248
- return false;
249
- }
250
- if (start) {
251
- const startObj = new Date(start);
252
- if (isNaN(startObj.getTime())) {
253
- return false;
254
- }
255
- if (dateObj < startObj) {
256
- return false;
257
- }
258
- }
259
- if (end) {
260
- const endObj = new Date(end);
261
- if (isNaN(endObj.getTime())) {
262
- return false;
263
- }
264
- if (dateObj > endObj) {
265
- return false;
266
- }
267
- }
268
- return true;
269
- }
270
- /**
271
- * Parse and validate date range strings.
272
- *
273
- * @param startDate - Optional ISO 8601 start date
274
- * @param endDate - Optional ISO 8601 end date
275
- * @returns Parsed Date objects or null
276
- */
277
- export function parseDateRange(startDate, endDate) {
278
- let start = null;
279
- let end = null;
280
- if (startDate) {
281
- start = new Date(startDate);
282
- if (isNaN(start.getTime())) {
283
- start = null;
284
- }
285
- }
286
- if (endDate) {
287
- end = new Date(endDate);
288
- if (isNaN(end.getTime())) {
289
- end = null;
290
- }
291
- }
292
- return { start, end };
293
- }
294
- /**
295
- * Validate if a string is a valid ISO 8601 date.
296
- *
297
- * @param date - Date string to validate
298
- * @returns True if valid ISO 8601 date
299
- */
300
- export function isValidISODate(date) {
301
- const dateObj = new Date(date);
302
- return !isNaN(dateObj.getTime()) && dateObj.toISOString() === date;
303
- }
304
- /**
305
- * Get current timestamp in ISO 8601 format.
306
- *
307
- * @returns Current timestamp string
308
- */
309
- export function getCurrentTimestamp() {
310
- return new Date().toISOString();
311
- }
312
- // ==================== Filter Utilities ====================
313
- /**
314
- * Checks if an entity's importance is within the specified range.
315
- * Entities without importance are treated as not matching if any filter is set.
316
- *
317
- * @param importance - The entity's importance value (may be undefined)
318
- * @param minImportance - Minimum importance filter (inclusive)
319
- * @param maxImportance - Maximum importance filter (inclusive)
320
- * @returns true if importance is within range or no filters are set
321
- *
322
- * @example
323
- * ```typescript
324
- * // Check if entity passes importance filter
325
- * if (isWithinImportanceRange(entity.importance, 5, 10)) {
326
- * // Entity has importance between 5 and 10
327
- * }
328
- * ```
329
- */
330
- export function isWithinImportanceRange(importance, minImportance, maxImportance) {
331
- // If no filters set, always pass
332
- if (minImportance === undefined && maxImportance === undefined) {
333
- return true;
334
- }
335
- // Check minimum importance
336
- if (minImportance !== undefined) {
337
- if (importance === undefined || importance < minImportance) {
338
- return false;
339
- }
340
- }
341
- // Check maximum importance
342
- if (maxImportance !== undefined) {
343
- if (importance === undefined || importance > maxImportance) {
344
- return false;
345
- }
346
- }
347
- return true;
348
- }
349
- /**
350
- * Filters entities by importance range.
351
- * Returns all entities if no importance filters are specified.
352
- *
353
- * @param entities - Array of entities to filter
354
- * @param minImportance - Minimum importance filter (inclusive)
355
- * @param maxImportance - Maximum importance filter (inclusive)
356
- * @returns Filtered entities within the importance range
357
- */
358
- export function filterByImportance(entities, minImportance, maxImportance) {
359
- if (minImportance === undefined && maxImportance === undefined) {
360
- return entities;
361
- }
362
- return entities.filter(e => isWithinImportanceRange(e.importance, minImportance, maxImportance));
363
- }
364
- /**
365
- * Filters entities by creation date range.
366
- *
367
- * @param entities - Array of entities to filter
368
- * @param startDate - Start of date range (inclusive)
369
- * @param endDate - End of date range (inclusive)
370
- * @returns Filtered entities created within the date range
371
- */
372
- export function filterByCreatedDate(entities, startDate, endDate) {
373
- if (!startDate && !endDate) {
374
- return entities;
375
- }
376
- return entities.filter(e => isWithinDateRange(e.createdAt, startDate, endDate));
377
- }
378
- /**
379
- * Filters entities by last modified date range.
380
- *
381
- * @param entities - Array of entities to filter
382
- * @param startDate - Start of date range (inclusive)
383
- * @param endDate - End of date range (inclusive)
384
- * @returns Filtered entities modified within the date range
385
- */
386
- export function filterByModifiedDate(entities, startDate, endDate) {
387
- if (!startDate && !endDate) {
388
- return entities;
389
- }
390
- return entities.filter(e => isWithinDateRange(e.lastModified, startDate, endDate));
391
- }
392
- /**
393
- * Filters entities by entity type.
394
- *
395
- * @param entities - Array of entities to filter
396
- * @param entityType - Entity type to filter by (case-sensitive)
397
- * @returns Filtered entities of the specified type
398
- */
399
- export function filterByEntityType(entities, entityType) {
400
- if (!entityType) {
401
- return entities;
402
- }
403
- return entities.filter(e => e.entityType === entityType);
404
- }
405
- /**
406
- * Checks if an entity passes all the specified filters.
407
- * Short-circuits on first failing filter for performance.
408
- *
409
- * Note: Tag filtering should be handled separately using hasMatchingTag
410
- * as it requires special normalization logic.
411
- *
412
- * @param entity - Entity to check
413
- * @param filters - Filters to apply
414
- * @returns true if entity passes all filters
415
- */
416
- export function entityPassesFilters(entity, filters) {
417
- // Importance filter
418
- if (!isWithinImportanceRange(entity.importance, filters.minImportance, filters.maxImportance)) {
419
- return false;
420
- }
421
- // Entity type filter
422
- if (filters.entityType && entity.entityType !== filters.entityType) {
423
- return false;
424
- }
425
- // Created date filter
426
- if (!isWithinDateRange(entity.createdAt, filters.createdAfter, filters.createdBefore)) {
427
- return false;
428
- }
429
- // Modified date filter
430
- if (!isWithinDateRange(entity.lastModified, filters.modifiedAfter, filters.modifiedBefore)) {
431
- return false;
432
- }
433
- return true;
434
- }
435
- // ==================== Path Utilities ====================
436
- /**
437
- * Validate and normalize a file path to prevent path traversal attacks.
438
- *
439
- * This function:
440
- * - Normalizes the path to canonical form
441
- * - Converts relative paths to absolute paths
442
- * - Detects and prevents path traversal attempts (..)
443
- *
444
- * @param filePath - The file path to validate
445
- * @param baseDir - Optional base directory for relative paths (defaults to process.cwd())
446
- * @returns Validated absolute file path
447
- * @throws {FileOperationError} If path traversal is detected or path is invalid
448
- *
449
- * @example
450
- * ```typescript
451
- * // Valid paths
452
- * validateFilePath('/var/data/memory.jsonl'); // Returns absolute path
453
- * validateFilePath('data/memory.jsonl'); // Returns absolute path from cwd
454
- *
455
- * // Invalid paths (throws FileOperationError)
456
- * validateFilePath('../../../etc/passwd'); // Path traversal detected
457
- * validateFilePath('/var/data/../../../etc/passwd'); // Path traversal detected
458
- * ```
459
- */
460
- export function validateFilePath(filePath, baseDir = process.cwd()) {
461
- // Normalize path to remove redundant separators and resolve . and ..
462
- const normalized = path.normalize(filePath);
463
- // Convert to absolute path
464
- const absolute = path.isAbsolute(normalized)
465
- ? normalized
466
- : path.join(baseDir, normalized);
467
- // After normalization, check if path still contains .. which would indicate
468
- // traversal beyond the base directory
469
- const finalNormalized = path.normalize(absolute);
470
- // Split path into segments and check for suspicious patterns
471
- const segments = finalNormalized.split(path.sep);
472
- if (segments.includes('..')) {
473
- throw new FileOperationError(`Path traversal detected in file path: ${filePath}`, filePath);
474
- }
475
- return finalNormalized;
476
- }
477
- /**
478
- * Default memory file path (in same directory as compiled code).
479
- */
480
- export const defaultMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '../memory.jsonl');
481
- /**
482
- * Ensure memory file path with backward compatibility migration.
483
- *
484
- * Handles:
485
- * 1. Custom MEMORY_FILE_PATH environment variable (with path traversal protection)
486
- * 2. Backward compatibility: migrates memory.json to memory.jsonl
487
- * 3. Absolute vs relative path resolution
488
- *
489
- * @returns Resolved and validated memory file path
490
- * @throws {FileOperationError} If path traversal is detected in MEMORY_FILE_PATH
491
- *
492
- * @example
493
- * ```typescript
494
- * // Use environment variable
495
- * process.env.MEMORY_FILE_PATH = '/data/memory.jsonl';
496
- * const path = await ensureMemoryFilePath(); // '/data/memory.jsonl'
497
- *
498
- * // Use default path
499
- * delete process.env.MEMORY_FILE_PATH;
500
- * const path = await ensureMemoryFilePath(); // './memory.jsonl'
501
- *
502
- * // Invalid path (throws error)
503
- * process.env.MEMORY_FILE_PATH = '../../../etc/passwd';
504
- * await ensureMemoryFilePath(); // Throws FileOperationError
505
- * ```
506
- */
507
- export async function ensureMemoryFilePath() {
508
- if (process.env.MEMORY_FILE_PATH) {
509
- // Custom path provided, validate and resolve to absolute
510
- const baseDir = path.dirname(fileURLToPath(import.meta.url)) + '/../';
511
- const validatedPath = validateFilePath(process.env.MEMORY_FILE_PATH, baseDir);
512
- return validatedPath;
513
- }
514
- // No custom path set, check for backward compatibility migration
515
- const oldMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '../memory.json');
516
- const newMemoryPath = defaultMemoryPath;
517
- try {
518
- // Check if old file exists
519
- await fs.access(oldMemoryPath);
520
- try {
521
- // Check if new file exists
522
- await fs.access(newMemoryPath);
523
- // Both files exist, use new one (no migration needed)
524
- return newMemoryPath;
525
- }
526
- catch {
527
- // Old file exists, new file doesn't - migrate
528
- console.log('[INFO] Found legacy memory.json file, migrating to memory.jsonl for JSONL format compatibility');
529
- await fs.rename(oldMemoryPath, newMemoryPath);
530
- console.log('[INFO] Successfully migrated memory.json to memory.jsonl');
531
- return newMemoryPath;
532
- }
533
- }
534
- catch {
535
- // Old file doesn't exist, use new path
536
- return newMemoryPath;
537
- }
538
- }
@@ -1,77 +0,0 @@
1
- /**
2
- * Custom Error Types
3
- *
4
- * Defines custom error classes for better error handling and debugging.
5
- *
6
- * @module utils/errors
7
- */
8
- /**
9
- * Base error class for all knowledge graph errors.
10
- * Extends the native Error class with additional context.
11
- */
12
- export declare class KnowledgeGraphError extends Error {
13
- readonly code?: string | undefined;
14
- constructor(message: string, code?: string | undefined);
15
- }
16
- /**
17
- * Error thrown when an entity is not found.
18
- */
19
- export declare class EntityNotFoundError extends KnowledgeGraphError {
20
- constructor(entityName: string);
21
- }
22
- /**
23
- * Error thrown when a relation is not found.
24
- */
25
- export declare class RelationNotFoundError extends KnowledgeGraphError {
26
- constructor(from: string, to: string, relationType?: string);
27
- }
28
- /**
29
- * Error thrown when attempting to create a duplicate entity.
30
- */
31
- export declare class DuplicateEntityError extends KnowledgeGraphError {
32
- constructor(entityName: string);
33
- }
34
- /**
35
- * Error thrown when validation fails.
36
- */
37
- export declare class ValidationError extends KnowledgeGraphError {
38
- readonly errors: string[];
39
- constructor(message: string, errors: string[]);
40
- }
41
- /**
42
- * Error thrown when a cycle is detected in hierarchies.
43
- */
44
- export declare class CycleDetectedError extends KnowledgeGraphError {
45
- constructor(entityName: string, parentName: string);
46
- }
47
- /**
48
- * Error thrown when an invalid importance value is provided.
49
- */
50
- export declare class InvalidImportanceError extends KnowledgeGraphError {
51
- constructor(value: number, min?: number, max?: number);
52
- }
53
- /**
54
- * Error thrown when a file operation fails.
55
- */
56
- export declare class FileOperationError extends KnowledgeGraphError {
57
- constructor(operation: string, filePath: string, cause?: Error);
58
- }
59
- /**
60
- * Error thrown when an import operation fails.
61
- */
62
- export declare class ImportError extends KnowledgeGraphError {
63
- constructor(format: string, message: string);
64
- }
65
- /**
66
- * Error thrown when an export operation fails.
67
- */
68
- export declare class ExportError extends KnowledgeGraphError {
69
- constructor(format: string, message: string);
70
- }
71
- /**
72
- * Error thrown when insufficient entities are provided for an operation.
73
- */
74
- export declare class InsufficientEntitiesError extends KnowledgeGraphError {
75
- constructor(operation: string, required: number, provided: number);
76
- }
77
- //# sourceMappingURL=errors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;GAGG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;aACC,IAAI,CAAC,EAAE,MAAM;gBAA9C,OAAO,EAAE,MAAM,EAAkB,IAAI,CAAC,EAAE,MAAM,YAAA;CAQ3D;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,mBAAmB;gBAC9C,UAAU,EAAE,MAAM;CAI/B;AAED;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,mBAAmB;gBAChD,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM;CAO5D;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,mBAAmB;gBAC/C,UAAU,EAAE,MAAM;CAI/B;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,mBAAmB;aAGpC,MAAM,EAAE,MAAM,EAAE;gBADhC,OAAO,EAAE,MAAM,EACC,MAAM,EAAE,MAAM,EAAE;CAKnC;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,mBAAmB;gBAC7C,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;CAOnD;AAED;;GAEG;AACH,qBAAa,sBAAuB,SAAQ,mBAAmB;gBACjD,KAAK,EAAE,MAAM,EAAE,GAAG,GAAE,MAAU,EAAE,GAAG,GAAE,MAAW;CAO7D;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,mBAAmB;gBAEvD,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,KAAK;CAWhB;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,mBAAmB;gBACtC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAI5C;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,mBAAmB;gBACtC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAI5C;AAED;;GAEG;AACH,qBAAa,yBAA0B,SAAQ,mBAAmB;gBACpD,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;CAOlE"}