@danielsimonjr/memory-mcp 0.7.2 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/edge-cases/edge-cases.test.js +406 -0
- package/dist/__tests__/file-path.test.js +5 -5
- package/dist/__tests__/integration/workflows.test.js +449 -0
- package/dist/__tests__/knowledge-graph.test.js +8 -3
- package/dist/__tests__/performance/benchmarks.test.js +411 -0
- package/dist/__tests__/unit/core/EntityManager.test.js +334 -0
- package/dist/__tests__/unit/core/GraphStorage.test.js +205 -0
- package/dist/__tests__/unit/core/RelationManager.test.js +274 -0
- package/dist/__tests__/unit/features/CompressionManager.test.js +350 -0
- package/dist/__tests__/unit/search/BasicSearch.test.js +311 -0
- package/dist/__tests__/unit/search/BooleanSearch.test.js +432 -0
- package/dist/__tests__/unit/search/FuzzySearch.test.js +448 -0
- package/dist/__tests__/unit/search/RankedSearch.test.js +379 -0
- package/dist/__tests__/unit/utils/levenshtein.test.js +77 -0
- package/dist/core/EntityManager.js +554 -0
- package/dist/core/GraphStorage.js +172 -0
- package/dist/core/KnowledgeGraphManager.js +400 -0
- package/dist/core/ObservationManager.js +129 -0
- package/dist/core/RelationManager.js +186 -0
- package/dist/core/TransactionManager.js +389 -0
- package/dist/core/index.js +9 -0
- package/dist/features/AnalyticsManager.js +222 -0
- package/dist/features/ArchiveManager.js +74 -0
- package/dist/features/BackupManager.js +311 -0
- package/dist/features/CompressionManager.js +310 -0
- package/dist/features/ExportManager.js +305 -0
- package/dist/features/HierarchyManager.js +219 -0
- package/dist/features/ImportExportManager.js +50 -0
- package/dist/features/ImportManager.js +328 -0
- package/dist/features/TagManager.js +210 -0
- package/dist/features/index.js +12 -0
- package/dist/index.js +13 -996
- package/dist/memory.jsonl +225 -0
- package/dist/search/BasicSearch.js +161 -0
- package/dist/search/BooleanSearch.js +304 -0
- package/dist/search/FuzzySearch.js +115 -0
- package/dist/search/RankedSearch.js +206 -0
- package/dist/search/SavedSearchManager.js +145 -0
- package/dist/search/SearchManager.js +305 -0
- package/dist/search/SearchSuggestions.js +57 -0
- package/dist/search/TFIDFIndexManager.js +217 -0
- package/dist/search/index.js +10 -0
- package/dist/server/MCPServer.js +889 -0
- package/dist/types/analytics.types.js +6 -0
- package/dist/types/entity.types.js +7 -0
- package/dist/types/import-export.types.js +7 -0
- package/dist/types/index.js +12 -0
- package/dist/types/search.types.js +7 -0
- package/dist/types/tag.types.js +6 -0
- package/dist/utils/constants.js +127 -0
- package/dist/utils/dateUtils.js +89 -0
- package/dist/utils/errors.js +121 -0
- package/dist/utils/index.js +13 -0
- package/dist/utils/levenshtein.js +62 -0
- package/dist/utils/logger.js +33 -0
- package/dist/utils/pathUtils.js +115 -0
- package/dist/utils/schemas.js +184 -0
- package/dist/utils/searchCache.js +209 -0
- package/dist/utils/tfidf.js +90 -0
- package/dist/utils/validationUtils.js +109 -0
- package/package.json +50 -48
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation Schemas
|
|
3
|
+
*
|
|
4
|
+
* Zod schemas for input validation across the memory system.
|
|
5
|
+
* Provides runtime type safety and data validation.
|
|
6
|
+
*
|
|
7
|
+
* @module utils/schemas
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
import { IMPORTANCE_RANGE } from './constants.js';
|
|
11
|
+
/**
|
|
12
|
+
* Importance range constants (imported from centralized constants).
|
|
13
|
+
*/
|
|
14
|
+
const MIN_IMPORTANCE = IMPORTANCE_RANGE.MIN;
|
|
15
|
+
const MAX_IMPORTANCE = IMPORTANCE_RANGE.MAX;
|
|
16
|
+
/**
|
|
17
|
+
* ISO 8601 date string validation.
|
|
18
|
+
* Accepts standard ISO format: YYYY-MM-DDTHH:mm:ss.sssZ
|
|
19
|
+
*/
|
|
20
|
+
const isoDateSchema = z.string().datetime({ message: 'Must be a valid ISO 8601 date string' });
|
|
21
|
+
/**
|
|
22
|
+
* Entity name validation.
|
|
23
|
+
* Must be a non-empty string with reasonable length constraints.
|
|
24
|
+
*/
|
|
25
|
+
const entityNameSchema = z.string()
|
|
26
|
+
.min(1, 'Entity name cannot be empty')
|
|
27
|
+
.max(500, 'Entity name cannot exceed 500 characters')
|
|
28
|
+
.trim();
|
|
29
|
+
/**
|
|
30
|
+
* Entity type validation.
|
|
31
|
+
* Must be a non-empty string (e.g., "person", "project", "concept").
|
|
32
|
+
*/
|
|
33
|
+
const entityTypeSchema = z.string()
|
|
34
|
+
.min(1, 'Entity type cannot be empty')
|
|
35
|
+
.max(100, 'Entity type cannot exceed 100 characters')
|
|
36
|
+
.trim();
|
|
37
|
+
/**
|
|
38
|
+
* Observation validation.
|
|
39
|
+
* Each observation must be a non-empty string.
|
|
40
|
+
*/
|
|
41
|
+
const observationSchema = z.string()
|
|
42
|
+
.min(1, 'Observation cannot be empty')
|
|
43
|
+
.max(5000, 'Observation cannot exceed 5000 characters');
|
|
44
|
+
/**
|
|
45
|
+
* Tag validation.
|
|
46
|
+
* Tags are normalized to lowercase and must be non-empty.
|
|
47
|
+
*/
|
|
48
|
+
const tagSchema = z.string()
|
|
49
|
+
.min(1, 'Tag cannot be empty')
|
|
50
|
+
.max(100, 'Tag cannot exceed 100 characters')
|
|
51
|
+
.trim()
|
|
52
|
+
.toLowerCase();
|
|
53
|
+
/**
|
|
54
|
+
* Importance validation.
|
|
55
|
+
* Must be a number between MIN_IMPORTANCE and MAX_IMPORTANCE (0-10).
|
|
56
|
+
*/
|
|
57
|
+
const importanceSchema = z.number()
|
|
58
|
+
.int('Importance must be an integer')
|
|
59
|
+
.min(MIN_IMPORTANCE, `Importance must be at least ${MIN_IMPORTANCE}`)
|
|
60
|
+
.max(MAX_IMPORTANCE, `Importance must be at most ${MAX_IMPORTANCE}`);
|
|
61
|
+
/**
|
|
62
|
+
* Relation type validation.
|
|
63
|
+
* Should be in snake_case format (e.g., "works_at", "manages").
|
|
64
|
+
*/
|
|
65
|
+
const relationTypeSchema = z.string()
|
|
66
|
+
.min(1, 'Relation type cannot be empty')
|
|
67
|
+
.max(100, 'Relation type cannot exceed 100 characters')
|
|
68
|
+
.trim();
|
|
69
|
+
/**
|
|
70
|
+
* Complete Entity schema with all fields.
|
|
71
|
+
* Used for validating full entity objects including timestamps.
|
|
72
|
+
*/
|
|
73
|
+
export const EntitySchema = z.object({
|
|
74
|
+
name: entityNameSchema,
|
|
75
|
+
entityType: entityTypeSchema,
|
|
76
|
+
observations: z.array(observationSchema),
|
|
77
|
+
createdAt: isoDateSchema.optional(),
|
|
78
|
+
lastModified: isoDateSchema.optional(),
|
|
79
|
+
tags: z.array(tagSchema).optional(),
|
|
80
|
+
importance: importanceSchema.optional(),
|
|
81
|
+
parentId: entityNameSchema.optional(),
|
|
82
|
+
}).strict();
|
|
83
|
+
/**
|
|
84
|
+
* Entity creation input schema.
|
|
85
|
+
* Used for validating user input when creating new entities.
|
|
86
|
+
* Timestamps are optional and will be auto-generated if not provided.
|
|
87
|
+
*/
|
|
88
|
+
export const CreateEntitySchema = z.object({
|
|
89
|
+
name: entityNameSchema,
|
|
90
|
+
entityType: entityTypeSchema,
|
|
91
|
+
observations: z.array(observationSchema),
|
|
92
|
+
tags: z.array(tagSchema).optional(),
|
|
93
|
+
importance: importanceSchema.optional(),
|
|
94
|
+
parentId: entityNameSchema.optional(),
|
|
95
|
+
createdAt: isoDateSchema.optional(),
|
|
96
|
+
lastModified: isoDateSchema.optional(),
|
|
97
|
+
});
|
|
98
|
+
/**
|
|
99
|
+
* Entity update input schema.
|
|
100
|
+
* All fields are optional for partial updates.
|
|
101
|
+
* Name cannot be updated (it's the unique identifier).
|
|
102
|
+
*/
|
|
103
|
+
export const UpdateEntitySchema = z.object({
|
|
104
|
+
entityType: entityTypeSchema.optional(),
|
|
105
|
+
observations: z.array(observationSchema).optional(),
|
|
106
|
+
tags: z.array(tagSchema).optional(),
|
|
107
|
+
importance: importanceSchema.optional(),
|
|
108
|
+
parentId: entityNameSchema.optional(),
|
|
109
|
+
});
|
|
110
|
+
/**
|
|
111
|
+
* Complete Relation schema with all fields.
|
|
112
|
+
* Used for validating full relation objects including timestamps.
|
|
113
|
+
*/
|
|
114
|
+
export const RelationSchema = z.object({
|
|
115
|
+
from: entityNameSchema,
|
|
116
|
+
to: entityNameSchema,
|
|
117
|
+
relationType: relationTypeSchema,
|
|
118
|
+
createdAt: isoDateSchema.optional(),
|
|
119
|
+
lastModified: isoDateSchema.optional(),
|
|
120
|
+
}).strict();
|
|
121
|
+
/**
|
|
122
|
+
* Relation creation input schema.
|
|
123
|
+
* Used for validating user input when creating new relations.
|
|
124
|
+
* Timestamps are optional and will be auto-generated if not provided.
|
|
125
|
+
*/
|
|
126
|
+
export const CreateRelationSchema = z.object({
|
|
127
|
+
from: entityNameSchema,
|
|
128
|
+
to: entityNameSchema,
|
|
129
|
+
relationType: relationTypeSchema,
|
|
130
|
+
createdAt: isoDateSchema.optional(),
|
|
131
|
+
lastModified: isoDateSchema.optional(),
|
|
132
|
+
});
|
|
133
|
+
/**
|
|
134
|
+
* Search query validation.
|
|
135
|
+
* Validates text search queries with reasonable length constraints.
|
|
136
|
+
*/
|
|
137
|
+
export const SearchQuerySchema = z.string()
|
|
138
|
+
.min(1, 'Search query cannot be empty')
|
|
139
|
+
.max(1000, 'Search query cannot exceed 1000 characters')
|
|
140
|
+
.trim();
|
|
141
|
+
/**
|
|
142
|
+
* Date range validation for search filters.
|
|
143
|
+
*/
|
|
144
|
+
export const DateRangeSchema = z.object({
|
|
145
|
+
start: isoDateSchema,
|
|
146
|
+
end: isoDateSchema,
|
|
147
|
+
}).refine((data) => new Date(data.start) <= new Date(data.end), { message: 'Start date must be before or equal to end date' });
|
|
148
|
+
/**
|
|
149
|
+
* Tag alias validation for TagManager.
|
|
150
|
+
*/
|
|
151
|
+
export const TagAliasSchema = z.object({
|
|
152
|
+
canonical: tagSchema,
|
|
153
|
+
aliases: z.array(tagSchema).min(1, 'Must have at least one alias'),
|
|
154
|
+
});
|
|
155
|
+
/**
|
|
156
|
+
* Export format validation.
|
|
157
|
+
*/
|
|
158
|
+
export const ExportFormatSchema = z.enum(['json', 'graphml', 'csv']);
|
|
159
|
+
/**
|
|
160
|
+
* Batch entity creation validation.
|
|
161
|
+
* Validates array of entities with maximum constraints.
|
|
162
|
+
* Empty arrays are allowed (no-op).
|
|
163
|
+
*/
|
|
164
|
+
export const BatchCreateEntitiesSchema = z.array(CreateEntitySchema)
|
|
165
|
+
.max(1000, 'Cannot create more than 1000 entities in a single batch');
|
|
166
|
+
/**
|
|
167
|
+
* Batch relation creation validation.
|
|
168
|
+
* Validates array of relations with maximum constraints.
|
|
169
|
+
* Empty arrays are allowed (no-op).
|
|
170
|
+
*/
|
|
171
|
+
export const BatchCreateRelationsSchema = z.array(CreateRelationSchema)
|
|
172
|
+
.max(1000, 'Cannot create more than 1000 relations in a single batch');
|
|
173
|
+
/**
|
|
174
|
+
* Entity name array validation for batch deletion.
|
|
175
|
+
*/
|
|
176
|
+
export const EntityNamesSchema = z.array(entityNameSchema)
|
|
177
|
+
.min(1, 'Must specify at least one entity name')
|
|
178
|
+
.max(1000, 'Cannot delete more than 1000 entities in a single batch');
|
|
179
|
+
/**
|
|
180
|
+
* Relation array validation for batch deletion.
|
|
181
|
+
*/
|
|
182
|
+
export const DeleteRelationsSchema = z.array(CreateRelationSchema)
|
|
183
|
+
.min(1, 'Must specify at least one relation')
|
|
184
|
+
.max(1000, 'Cannot delete more than 1000 relations in a single batch');
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Search Result Cache
|
|
3
|
+
*
|
|
4
|
+
* Simple LRU-style cache for search results with TTL support.
|
|
5
|
+
* Improves performance for repeated queries without external dependencies.
|
|
6
|
+
*
|
|
7
|
+
* @module utils/searchCache
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Simple LRU cache implementation for search results.
|
|
11
|
+
*
|
|
12
|
+
* Features:
|
|
13
|
+
* - Maximum size limit (LRU eviction when full)
|
|
14
|
+
* - TTL-based expiration
|
|
15
|
+
* - Cache statistics tracking
|
|
16
|
+
* - Hash-based key generation from query parameters
|
|
17
|
+
*/
|
|
18
|
+
export class SearchCache {
|
|
19
|
+
maxSize;
|
|
20
|
+
ttlMs;
|
|
21
|
+
cache = new Map();
|
|
22
|
+
accessOrder = [];
|
|
23
|
+
hits = 0;
|
|
24
|
+
misses = 0;
|
|
25
|
+
constructor(maxSize = 500, ttlMs = 5 * 60 * 1000 // 5 minutes default
|
|
26
|
+
) {
|
|
27
|
+
this.maxSize = maxSize;
|
|
28
|
+
this.ttlMs = ttlMs;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Generate cache key from query parameters.
|
|
32
|
+
*/
|
|
33
|
+
generateKey(params) {
|
|
34
|
+
// Sort keys for consistent hashing
|
|
35
|
+
const sorted = Object.keys(params)
|
|
36
|
+
.sort()
|
|
37
|
+
.map(key => `${key}:${JSON.stringify(params[key])}`)
|
|
38
|
+
.join('|');
|
|
39
|
+
return sorted;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get value from cache.
|
|
43
|
+
*
|
|
44
|
+
* @param params - Query parameters to generate cache key
|
|
45
|
+
* @returns Cached value or undefined if not found/expired
|
|
46
|
+
*/
|
|
47
|
+
get(params) {
|
|
48
|
+
const key = this.generateKey(params);
|
|
49
|
+
const entry = this.cache.get(key);
|
|
50
|
+
if (!entry) {
|
|
51
|
+
this.misses++;
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
// Check expiration
|
|
55
|
+
if (Date.now() > entry.expiresAt) {
|
|
56
|
+
this.cache.delete(key);
|
|
57
|
+
this.removeFromAccessOrder(key);
|
|
58
|
+
this.misses++;
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
// Update access order (move to end = most recently used)
|
|
62
|
+
this.removeFromAccessOrder(key);
|
|
63
|
+
this.accessOrder.push(key);
|
|
64
|
+
this.hits++;
|
|
65
|
+
return entry.value;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Set value in cache.
|
|
69
|
+
*
|
|
70
|
+
* @param params - Query parameters to generate cache key
|
|
71
|
+
* @param value - Value to cache
|
|
72
|
+
*/
|
|
73
|
+
set(params, value) {
|
|
74
|
+
const key = this.generateKey(params);
|
|
75
|
+
// Remove old entry if exists
|
|
76
|
+
if (this.cache.has(key)) {
|
|
77
|
+
this.removeFromAccessOrder(key);
|
|
78
|
+
}
|
|
79
|
+
// Evict least recently used if at capacity
|
|
80
|
+
if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
|
|
81
|
+
const lruKey = this.accessOrder.shift();
|
|
82
|
+
if (lruKey) {
|
|
83
|
+
this.cache.delete(lruKey);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Add new entry
|
|
87
|
+
this.cache.set(key, {
|
|
88
|
+
value,
|
|
89
|
+
timestamp: Date.now(),
|
|
90
|
+
expiresAt: Date.now() + this.ttlMs,
|
|
91
|
+
});
|
|
92
|
+
this.accessOrder.push(key);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Invalidate all cached entries.
|
|
96
|
+
*/
|
|
97
|
+
clear() {
|
|
98
|
+
this.cache.clear();
|
|
99
|
+
this.accessOrder = [];
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Remove specific entry from access order.
|
|
103
|
+
*/
|
|
104
|
+
removeFromAccessOrder(key) {
|
|
105
|
+
const index = this.accessOrder.indexOf(key);
|
|
106
|
+
if (index > -1) {
|
|
107
|
+
this.accessOrder.splice(index, 1);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get cache statistics.
|
|
112
|
+
*/
|
|
113
|
+
getStats() {
|
|
114
|
+
const total = this.hits + this.misses;
|
|
115
|
+
return {
|
|
116
|
+
hits: this.hits,
|
|
117
|
+
misses: this.misses,
|
|
118
|
+
size: this.cache.size,
|
|
119
|
+
hitRate: total > 0 ? this.hits / total : 0,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Reset cache statistics.
|
|
124
|
+
*/
|
|
125
|
+
resetStats() {
|
|
126
|
+
this.hits = 0;
|
|
127
|
+
this.misses = 0;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Clean up expired entries.
|
|
131
|
+
*
|
|
132
|
+
* Should be called periodically to prevent memory buildup.
|
|
133
|
+
*/
|
|
134
|
+
cleanupExpired() {
|
|
135
|
+
const now = Date.now();
|
|
136
|
+
const keysToDelete = [];
|
|
137
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
138
|
+
if (now > entry.expiresAt) {
|
|
139
|
+
keysToDelete.push(key);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const key of keysToDelete) {
|
|
143
|
+
this.cache.delete(key);
|
|
144
|
+
this.removeFromAccessOrder(key);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Get current cache size.
|
|
149
|
+
*/
|
|
150
|
+
get size() {
|
|
151
|
+
return this.cache.size;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Check if cache has entry for params.
|
|
155
|
+
*/
|
|
156
|
+
has(params) {
|
|
157
|
+
const key = this.generateKey(params);
|
|
158
|
+
const entry = this.cache.get(key);
|
|
159
|
+
if (!entry)
|
|
160
|
+
return false;
|
|
161
|
+
// Check expiration
|
|
162
|
+
if (Date.now() > entry.expiresAt) {
|
|
163
|
+
this.cache.delete(key);
|
|
164
|
+
this.removeFromAccessOrder(key);
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Global search caches for different search types.
|
|
172
|
+
*/
|
|
173
|
+
export const searchCaches = {
|
|
174
|
+
basic: new SearchCache(),
|
|
175
|
+
ranked: new SearchCache(),
|
|
176
|
+
boolean: new SearchCache(),
|
|
177
|
+
fuzzy: new SearchCache(),
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Clear all search caches.
|
|
181
|
+
*
|
|
182
|
+
* Should be called when graph is modified to ensure cache consistency.
|
|
183
|
+
*/
|
|
184
|
+
export function clearAllSearchCaches() {
|
|
185
|
+
searchCaches.basic.clear();
|
|
186
|
+
searchCaches.ranked.clear();
|
|
187
|
+
searchCaches.boolean.clear();
|
|
188
|
+
searchCaches.fuzzy.clear();
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Get combined statistics for all caches.
|
|
192
|
+
*/
|
|
193
|
+
export function getAllCacheStats() {
|
|
194
|
+
return {
|
|
195
|
+
basic: searchCaches.basic.getStats(),
|
|
196
|
+
ranked: searchCaches.ranked.getStats(),
|
|
197
|
+
boolean: searchCaches.boolean.getStats(),
|
|
198
|
+
fuzzy: searchCaches.fuzzy.getStats(),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Clean up expired entries in all caches.
|
|
203
|
+
*/
|
|
204
|
+
export function cleanupAllCaches() {
|
|
205
|
+
searchCaches.basic.cleanupExpired();
|
|
206
|
+
searchCaches.ranked.cleanupExpired();
|
|
207
|
+
searchCaches.boolean.cleanupExpired();
|
|
208
|
+
searchCaches.fuzzy.cleanupExpired();
|
|
209
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TF-IDF (Term Frequency-Inverse Document Frequency) Utilities
|
|
3
|
+
*
|
|
4
|
+
* Algorithms for calculating TF-IDF scores used in ranked search.
|
|
5
|
+
* TF-IDF measures how important a term is to a document in a collection.
|
|
6
|
+
*
|
|
7
|
+
* @module utils/tfidf
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Calculate Term Frequency (TF) for a term in a document.
|
|
11
|
+
*
|
|
12
|
+
* TF = (Number of times term appears in document) / (Total terms in document)
|
|
13
|
+
*
|
|
14
|
+
* @param term - The search term
|
|
15
|
+
* @param document - The document text
|
|
16
|
+
* @returns Term frequency (0.0 to 1.0)
|
|
17
|
+
*/
|
|
18
|
+
export function calculateTF(term, document) {
|
|
19
|
+
const termLower = term.toLowerCase();
|
|
20
|
+
const tokens = tokenize(document);
|
|
21
|
+
if (tokens.length === 0)
|
|
22
|
+
return 0;
|
|
23
|
+
const termCount = tokens.filter(t => t === termLower).length;
|
|
24
|
+
return termCount / tokens.length;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Calculate Inverse Document Frequency (IDF) for a term across documents.
|
|
28
|
+
*
|
|
29
|
+
* IDF = log(Total documents / Documents containing term)
|
|
30
|
+
*
|
|
31
|
+
* @param term - The search term
|
|
32
|
+
* @param documents - Array of document texts
|
|
33
|
+
* @returns Inverse document frequency
|
|
34
|
+
*/
|
|
35
|
+
export function calculateIDF(term, documents) {
|
|
36
|
+
if (documents.length === 0)
|
|
37
|
+
return 0;
|
|
38
|
+
const termLower = term.toLowerCase();
|
|
39
|
+
const docsWithTerm = documents.filter(doc => tokenize(doc).includes(termLower)).length;
|
|
40
|
+
if (docsWithTerm === 0)
|
|
41
|
+
return 0;
|
|
42
|
+
return Math.log(documents.length / docsWithTerm);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Calculate TF-IDF score for a term in a document.
|
|
46
|
+
*
|
|
47
|
+
* TF-IDF = TF * IDF
|
|
48
|
+
*
|
|
49
|
+
* Higher scores indicate more important/relevant terms.
|
|
50
|
+
*
|
|
51
|
+
* @param term - The search term
|
|
52
|
+
* @param document - The document text
|
|
53
|
+
* @param documents - Array of all documents
|
|
54
|
+
* @returns TF-IDF score
|
|
55
|
+
*/
|
|
56
|
+
export function calculateTFIDF(term, document, documents) {
|
|
57
|
+
const tf = calculateTF(term, document);
|
|
58
|
+
const idf = calculateIDF(term, documents);
|
|
59
|
+
return tf * idf;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Tokenize text into lowercase words.
|
|
63
|
+
*
|
|
64
|
+
* Splits on whitespace and removes punctuation.
|
|
65
|
+
*
|
|
66
|
+
* @param text - Text to tokenize
|
|
67
|
+
* @returns Array of lowercase tokens
|
|
68
|
+
*/
|
|
69
|
+
export function tokenize(text) {
|
|
70
|
+
return text
|
|
71
|
+
.toLowerCase()
|
|
72
|
+
.replace(/[^\w\s]/g, ' ')
|
|
73
|
+
.split(/\s+/)
|
|
74
|
+
.filter(token => token.length > 0);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Calculate TF-IDF scores for multiple search terms.
|
|
78
|
+
*
|
|
79
|
+
* @param terms - Array of search terms
|
|
80
|
+
* @param document - The document text
|
|
81
|
+
* @param documents - Array of all documents
|
|
82
|
+
* @returns Map of term to TF-IDF score
|
|
83
|
+
*/
|
|
84
|
+
export function calculateMultiTermTFIDF(terms, document, documents) {
|
|
85
|
+
const scores = new Map();
|
|
86
|
+
for (const term of terms) {
|
|
87
|
+
scores.set(term, calculateTFIDF(term, document, documents));
|
|
88
|
+
}
|
|
89
|
+
return scores;
|
|
90
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helper functions for validating entities, relations, and other data structures.
|
|
5
|
+
*
|
|
6
|
+
* @module utils/validationUtils
|
|
7
|
+
*/
|
|
8
|
+
import { IMPORTANCE_RANGE } from './constants.js';
|
|
9
|
+
/**
|
|
10
|
+
* Type guard to check if value is a non-null object.
|
|
11
|
+
*/
|
|
12
|
+
function isObject(value) {
|
|
13
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Validate an entity object.
|
|
17
|
+
*
|
|
18
|
+
* Checks required fields and data types.
|
|
19
|
+
*
|
|
20
|
+
* @param entity - Entity to validate (unknown type for runtime validation)
|
|
21
|
+
* @returns Validation result
|
|
22
|
+
*/
|
|
23
|
+
export function validateEntity(entity) {
|
|
24
|
+
const errors = [];
|
|
25
|
+
if (!isObject(entity)) {
|
|
26
|
+
return { valid: false, errors: ['Entity must be an object'] };
|
|
27
|
+
}
|
|
28
|
+
if (!entity.name || typeof entity.name !== 'string' || entity.name.trim() === '') {
|
|
29
|
+
errors.push('Entity name is required and must be a non-empty string');
|
|
30
|
+
}
|
|
31
|
+
if (!entity.entityType || typeof entity.entityType !== 'string' || entity.entityType.trim() === '') {
|
|
32
|
+
errors.push('Entity type is required and must be a non-empty string');
|
|
33
|
+
}
|
|
34
|
+
if (!Array.isArray(entity.observations)) {
|
|
35
|
+
errors.push('Observations must be an array');
|
|
36
|
+
}
|
|
37
|
+
else if (!entity.observations.every((o) => typeof o === 'string')) {
|
|
38
|
+
errors.push('All observations must be strings');
|
|
39
|
+
}
|
|
40
|
+
if (entity.tags !== undefined) {
|
|
41
|
+
if (!Array.isArray(entity.tags)) {
|
|
42
|
+
errors.push('Tags must be an array');
|
|
43
|
+
}
|
|
44
|
+
else if (!entity.tags.every((t) => typeof t === 'string')) {
|
|
45
|
+
errors.push('All tags must be strings');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (entity.importance !== undefined) {
|
|
49
|
+
if (typeof entity.importance !== 'number') {
|
|
50
|
+
errors.push('Importance must be a number');
|
|
51
|
+
}
|
|
52
|
+
else if (!validateImportance(entity.importance)) {
|
|
53
|
+
errors.push('Importance must be between 0 and 10');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return { valid: errors.length === 0, errors };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Validate a relation object.
|
|
60
|
+
*
|
|
61
|
+
* Checks required fields and data types.
|
|
62
|
+
*
|
|
63
|
+
* @param relation - Relation to validate (unknown type for runtime validation)
|
|
64
|
+
* @returns Validation result
|
|
65
|
+
*/
|
|
66
|
+
export function validateRelation(relation) {
|
|
67
|
+
const errors = [];
|
|
68
|
+
if (!isObject(relation)) {
|
|
69
|
+
return { valid: false, errors: ['Relation must be an object'] };
|
|
70
|
+
}
|
|
71
|
+
if (!relation.from || typeof relation.from !== 'string' || relation.from.trim() === '') {
|
|
72
|
+
errors.push('Relation "from" is required and must be a non-empty string');
|
|
73
|
+
}
|
|
74
|
+
if (!relation.to || typeof relation.to !== 'string' || relation.to.trim() === '') {
|
|
75
|
+
errors.push('Relation "to" is required and must be a non-empty string');
|
|
76
|
+
}
|
|
77
|
+
if (!relation.relationType || typeof relation.relationType !== 'string' || relation.relationType.trim() === '') {
|
|
78
|
+
errors.push('Relation type is required and must be a non-empty string');
|
|
79
|
+
}
|
|
80
|
+
return { valid: errors.length === 0, errors };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Validate importance level (must be 0-10).
|
|
84
|
+
*
|
|
85
|
+
* @param importance - Importance value to validate
|
|
86
|
+
* @returns True if valid
|
|
87
|
+
*/
|
|
88
|
+
export function validateImportance(importance) {
|
|
89
|
+
return typeof importance === 'number'
|
|
90
|
+
&& !isNaN(importance)
|
|
91
|
+
&& importance >= IMPORTANCE_RANGE.MIN
|
|
92
|
+
&& importance <= IMPORTANCE_RANGE.MAX;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Validate an array of tags.
|
|
96
|
+
*
|
|
97
|
+
* @param tags - Tags array to validate (unknown type for runtime validation)
|
|
98
|
+
* @returns Validation result
|
|
99
|
+
*/
|
|
100
|
+
export function validateTags(tags) {
|
|
101
|
+
const errors = [];
|
|
102
|
+
if (!Array.isArray(tags)) {
|
|
103
|
+
return { valid: false, errors: ['Tags must be an array'] };
|
|
104
|
+
}
|
|
105
|
+
if (!tags.every((t) => typeof t === 'string' && t.trim() !== '')) {
|
|
106
|
+
errors.push('All tags must be non-empty strings');
|
|
107
|
+
}
|
|
108
|
+
return { valid: errors.length === 0, errors };
|
|
109
|
+
}
|
package/package.json
CHANGED
|
@@ -1,48 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@danielsimonjr/memory-mcp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Enhanced MCP memory server with
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"author": "Daniel Simon Jr. (https://github.com/danielsimonjr)",
|
|
7
|
-
"homepage": "https://github.com/danielsimonjr/memory-mcp",
|
|
8
|
-
"bugs": "https://github.com/danielsimonjr/memory-mcp/issues",
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "https://github.com/danielsimonjr/memory-mcp.git"
|
|
12
|
-
},
|
|
13
|
-
"keywords": [
|
|
14
|
-
"mcp",
|
|
15
|
-
"model-context-protocol",
|
|
16
|
-
"memory",
|
|
17
|
-
"knowledge-graph",
|
|
18
|
-
"enhanced",
|
|
19
|
-
"timestamps",
|
|
20
|
-
"tags",
|
|
21
|
-
"export",
|
|
22
|
-
"graphml",
|
|
23
|
-
"analytics"
|
|
24
|
-
],
|
|
25
|
-
"type": "module",
|
|
26
|
-
"bin": {
|
|
27
|
-
"mcp-server-memory": "dist/index.js"
|
|
28
|
-
},
|
|
29
|
-
"files": [
|
|
30
|
-
"dist"
|
|
31
|
-
],
|
|
32
|
-
"scripts": {
|
|
33
|
-
"build": "tsc
|
|
34
|
-
"prepare": "npm run build",
|
|
35
|
-
"watch": "tsc --watch",
|
|
36
|
-
"test": "vitest run --coverage"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@danielsimonjr/memory-mcp",
|
|
3
|
+
"version": "0.41.0",
|
|
4
|
+
"description": "Enhanced MCP memory server with hierarchies, compression, archiving, and 45 advanced tools",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Daniel Simon Jr. (https://github.com/danielsimonjr)",
|
|
7
|
+
"homepage": "https://github.com/danielsimonjr/memory-mcp",
|
|
8
|
+
"bugs": "https://github.com/danielsimonjr/memory-mcp/issues",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/danielsimonjr/memory-mcp.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"mcp",
|
|
15
|
+
"model-context-protocol",
|
|
16
|
+
"memory",
|
|
17
|
+
"knowledge-graph",
|
|
18
|
+
"enhanced",
|
|
19
|
+
"timestamps",
|
|
20
|
+
"tags",
|
|
21
|
+
"export",
|
|
22
|
+
"graphml",
|
|
23
|
+
"analytics"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"bin": {
|
|
27
|
+
"mcp-server-memory": "dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc",
|
|
34
|
+
"prepare": "npm run build",
|
|
35
|
+
"watch": "tsc --watch",
|
|
36
|
+
"test": "vitest run --coverage",
|
|
37
|
+
"typecheck": "tsc --noEmit --noUnusedLocals --noUnusedParameters --strict"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@modelcontextprotocol/sdk": "^1.21.1",
|
|
41
|
+
"zod": "^4.1.13"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^22",
|
|
45
|
+
"@vitest/coverage-v8": "^4.0.13",
|
|
46
|
+
"shx": "^0.4.0",
|
|
47
|
+
"typescript": "^5.6.2",
|
|
48
|
+
"vitest": "^4.0.13"
|
|
49
|
+
}
|
|
50
|
+
}
|