@yamo/memory-mesh 2.3.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/bin/memory_mesh.js +1 -1
  2. package/lib/llm/client.d.ts +111 -0
  3. package/lib/llm/client.js +299 -357
  4. package/lib/llm/client.ts +413 -0
  5. package/lib/llm/index.d.ts +17 -0
  6. package/lib/llm/index.js +15 -8
  7. package/lib/llm/index.ts +19 -0
  8. package/lib/memory/adapters/client.d.ts +183 -0
  9. package/lib/memory/adapters/client.js +518 -0
  10. package/lib/memory/adapters/client.ts +678 -0
  11. package/lib/memory/adapters/config.d.ts +137 -0
  12. package/lib/memory/adapters/config.js +189 -0
  13. package/lib/memory/adapters/config.ts +259 -0
  14. package/lib/memory/adapters/errors.d.ts +76 -0
  15. package/lib/memory/adapters/errors.js +128 -0
  16. package/lib/memory/adapters/errors.ts +166 -0
  17. package/lib/memory/context-manager.d.ts +44 -0
  18. package/lib/memory/context-manager.js +344 -0
  19. package/lib/memory/context-manager.ts +432 -0
  20. package/lib/memory/embeddings/factory.d.ts +59 -0
  21. package/lib/memory/embeddings/factory.js +148 -0
  22. package/lib/{embeddings/factory.js → memory/embeddings/factory.ts} +69 -28
  23. package/lib/memory/embeddings/index.d.ts +2 -0
  24. package/lib/memory/embeddings/index.js +2 -0
  25. package/lib/memory/embeddings/index.ts +2 -0
  26. package/lib/memory/embeddings/service.d.ts +164 -0
  27. package/lib/memory/embeddings/service.js +515 -0
  28. package/lib/{embeddings/service.js → memory/embeddings/service.ts} +223 -156
  29. package/lib/memory/index.d.ts +9 -0
  30. package/lib/memory/index.js +9 -1
  31. package/lib/memory/index.ts +20 -0
  32. package/lib/memory/memory-mesh.d.ts +274 -0
  33. package/lib/memory/memory-mesh.js +1469 -678
  34. package/lib/memory/memory-mesh.ts +1803 -0
  35. package/lib/memory/memory-translator.d.ts +19 -0
  36. package/lib/memory/memory-translator.js +125 -0
  37. package/lib/memory/memory-translator.ts +158 -0
  38. package/lib/memory/schema.d.ts +111 -0
  39. package/lib/memory/schema.js +183 -0
  40. package/lib/memory/schema.ts +267 -0
  41. package/lib/memory/scorer.d.ts +26 -0
  42. package/lib/memory/scorer.js +77 -0
  43. package/lib/memory/scorer.ts +95 -0
  44. package/lib/memory/search/index.d.ts +1 -0
  45. package/lib/memory/search/index.js +1 -0
  46. package/lib/memory/search/index.ts +1 -0
  47. package/lib/memory/search/keyword-search.d.ts +62 -0
  48. package/lib/memory/search/keyword-search.js +135 -0
  49. package/lib/{search/keyword-search.js → memory/search/keyword-search.ts} +66 -36
  50. package/lib/scrubber/config/defaults.d.ts +53 -0
  51. package/lib/scrubber/config/defaults.js +49 -57
  52. package/lib/scrubber/config/defaults.ts +117 -0
  53. package/lib/scrubber/index.d.ts +6 -0
  54. package/lib/scrubber/index.js +3 -23
  55. package/lib/scrubber/index.ts +7 -0
  56. package/lib/scrubber/scrubber.d.ts +61 -0
  57. package/lib/scrubber/scrubber.js +99 -121
  58. package/lib/scrubber/scrubber.ts +168 -0
  59. package/lib/scrubber/stages/chunker.d.ts +13 -0
  60. package/lib/scrubber/stages/metadata-annotator.d.ts +18 -0
  61. package/lib/scrubber/stages/normalizer.d.ts +13 -0
  62. package/lib/scrubber/stages/semantic-filter.d.ts +13 -0
  63. package/lib/scrubber/stages/structural-cleaner.d.ts +13 -0
  64. package/lib/scrubber/stages/validator.d.ts +18 -0
  65. package/lib/scrubber/telemetry.d.ts +36 -0
  66. package/lib/scrubber/telemetry.js +53 -58
  67. package/lib/scrubber/telemetry.ts +99 -0
  68. package/lib/utils/logger.d.ts +29 -0
  69. package/lib/utils/logger.js +64 -0
  70. package/lib/utils/logger.ts +85 -0
  71. package/lib/utils/skill-metadata.d.ts +32 -0
  72. package/lib/utils/skill-metadata.js +132 -0
  73. package/lib/utils/skill-metadata.ts +147 -0
  74. package/lib/yamo/emitter.d.ts +73 -0
  75. package/lib/yamo/emitter.js +78 -143
  76. package/lib/yamo/emitter.ts +249 -0
  77. package/lib/yamo/schema.d.ts +58 -0
  78. package/lib/yamo/schema.js +81 -108
  79. package/lib/yamo/schema.ts +165 -0
  80. package/package.json +11 -8
  81. package/index.d.ts +0 -111
  82. package/lib/embeddings/index.js +0 -2
  83. package/lib/index.js +0 -6
  84. package/lib/lancedb/client.js +0 -633
  85. package/lib/lancedb/config.js +0 -215
  86. package/lib/lancedb/errors.js +0 -144
  87. package/lib/lancedb/index.js +0 -4
  88. package/lib/lancedb/schema.js +0 -217
  89. package/lib/scrubber/errors/scrubber-error.js +0 -43
  90. package/lib/scrubber/stages/chunker.js +0 -103
  91. package/lib/scrubber/stages/metadata-annotator.js +0 -74
  92. package/lib/scrubber/stages/normalizer.js +0 -59
  93. package/lib/scrubber/stages/semantic-filter.js +0 -61
  94. package/lib/scrubber/stages/structural-cleaner.js +0 -82
  95. package/lib/scrubber/stages/validator.js +0 -66
  96. package/lib/scrubber/utils/hash.js +0 -39
  97. package/lib/scrubber/utils/html-parser.js +0 -45
  98. package/lib/scrubber/utils/pattern-matcher.js +0 -63
  99. package/lib/scrubber/utils/token-counter.js +0 -31
  100. package/lib/search/index.js +0 -1
  101. package/lib/utils/index.js +0 -1
  102. package/lib/yamo/index.js +0 -15
@@ -0,0 +1,9 @@
1
+ /**
2
+ * YAMO Brain Module
3
+ * Semantic memory mesh with vector search capabilities
4
+ */
5
+ export { MemoryMesh, run, type MemoryMeshOptions, type MemoryEntry, type SearchResult, type CacheEntry, } from "./memory-mesh.js";
6
+ export { MemoryContextManager } from "./context-manager.js";
7
+ export { LanceDBClient, type LanceDBDriver } from "./adapters/client.js";
8
+ export * from "./embeddings/index.js";
9
+ export * from "./search/index.js";
@@ -1 +1,9 @@
1
- export { default as MemoryMesh, run } from './memory-mesh.js';
1
+ /**
2
+ * YAMO Brain Module
3
+ * Semantic memory mesh with vector search capabilities
4
+ */
5
+ export { MemoryMesh, run, } from "./memory-mesh.js";
6
+ export { MemoryContextManager } from "./context-manager.js";
7
+ export { LanceDBClient } from "./adapters/client.js";
8
+ export * from "./embeddings/index.js";
9
+ export * from "./search/index.js";
@@ -0,0 +1,20 @@
1
+ /**
2
+ * YAMO Brain Module
3
+ * Semantic memory mesh with vector search capabilities
4
+ */
5
+
6
+ export {
7
+ MemoryMesh,
8
+ run,
9
+ type MemoryMeshOptions,
10
+ type MemoryEntry,
11
+ type SearchResult,
12
+ type CacheEntry,
13
+ } from "./memory-mesh.js";
14
+
15
+ export { MemoryContextManager } from "./context-manager.js";
16
+
17
+ export { LanceDBClient, type LanceDBDriver } from "./adapters/client.js";
18
+
19
+ export * from "./embeddings/index.js";
20
+ export * from "./search/index.js";
@@ -0,0 +1,274 @@
1
+ /**
2
+ * Memory Mesh - Vector Memory Storage with LanceDB
3
+ * Provides persistent semantic memory for YAMO OS using LanceDB backend
4
+ *
5
+ * CLI Interface:
6
+ * node tools/memory_mesh.js ingest '{"content": "...", "metadata": {...}}'
7
+ * node tools/memory_mesh.js search '{"query": "...", "limit": 10}'
8
+ * node tools/memory_mesh.js get '{"id": "..."}'
9
+ * node tools/memory_mesh.js delete '{"id": "..."}'
10
+ * node tools/memory_mesh.js stats '{}'
11
+ *
12
+ * Also supports STDIN input for YAMO skill compatibility:
13
+ * echo '{"action": "ingest", "content": "..."}' | node tools/memory_mesh.js
14
+ */
15
+ import { LanceDBClient } from "./adapters/client.js";
16
+ import { Config } from "./adapters/config.js";
17
+ import EmbeddingFactory from "./embeddings/factory.js";
18
+ import { Scrubber } from "../scrubber/scrubber.js";
19
+ import { KeywordSearch } from "./search/keyword-search.js";
20
+ import { LLMClient } from "../llm/client.js";
21
+ import * as lancedb from "@lancedb/lancedb";
22
+ export interface MemoryMeshOptions {
23
+ enableYamo?: boolean;
24
+ enableLLM?: boolean;
25
+ enableMemory?: boolean;
26
+ agentId?: string;
27
+ llmProvider?: string;
28
+ llmApiKey?: string;
29
+ llmModel?: string;
30
+ llmMaxTokens?: number;
31
+ skill_directories?: string | string[];
32
+ dbDir?: string;
33
+ }
34
+ export interface MemoryEntry {
35
+ id: string;
36
+ content: string;
37
+ vector: number[];
38
+ metadata: string;
39
+ }
40
+ export interface SearchResult extends MemoryEntry {
41
+ score: number;
42
+ [key: string]: any;
43
+ }
44
+ export interface CacheEntry {
45
+ result: SearchResult[];
46
+ timestamp: number;
47
+ }
48
+ /**
49
+ * MemoryMesh class for managing vector memory storage
50
+ */
51
+ export declare class MemoryMesh {
52
+ client: LanceDBClient | null;
53
+ config: Config | null;
54
+ embeddingFactory: EmbeddingFactory;
55
+ keywordSearch: KeywordSearch;
56
+ isInitialized: boolean;
57
+ vectorDimension: number;
58
+ enableYamo: boolean;
59
+ enableLLM: boolean;
60
+ enableMemory: boolean;
61
+ agentId: string;
62
+ yamoTable: lancedb.Table | null;
63
+ skillTable: lancedb.Table | null;
64
+ llmClient: LLMClient | null;
65
+ scrubber: Scrubber;
66
+ queryCache: Map<string, CacheEntry>;
67
+ cacheConfig: {
68
+ maxSize: number;
69
+ ttlMs: number;
70
+ };
71
+ skillDirectories: string[];
72
+ dbDir?: string;
73
+ /**
74
+ * Create a new MemoryMesh instance
75
+ * @param {Object} [options={}]
76
+ */
77
+ constructor(options?: MemoryMeshOptions);
78
+ /**
79
+ * Generate a cache key from query and options
80
+ * @private
81
+ */
82
+ _generateCacheKey(query: string, options?: any): string;
83
+ /**
84
+ * Get cached result if valid
85
+ * @private
86
+ *
87
+ * Race condition fix: The delete-then-set pattern for LRU tracking creates a window
88
+ * where another operation could observe the key as missing. We use a try-finally
89
+ * pattern to ensure atomicity at the application level.
90
+ */
91
+ _getCachedResult(key: string): SearchResult[] | null;
92
+ /**
93
+ * Cache a search result
94
+ * @private
95
+ */
96
+ _cacheResult(key: string, result: SearchResult[]): void;
97
+ /**
98
+ * Clear all cached results
99
+ */
100
+ clearCache(): void;
101
+ /**
102
+ * Get cache statistics
103
+ */
104
+ getCacheStats(): any;
105
+ /**
106
+ * Validate and sanitize metadata to prevent prototype pollution
107
+ * @private
108
+ */
109
+ _validateMetadata(metadata: any): Record<string, any>;
110
+ /**
111
+ * Sanitize and validate content before storage
112
+ * @private
113
+ */
114
+ _sanitizeContent(content: string): string;
115
+ /**
116
+ * Initialize the LanceDB client
117
+ */
118
+ init(): Promise<void>;
119
+ /**
120
+ * Add content to memory with auto-generated embedding and scrubbing.
121
+ *
122
+ * This is the primary method for storing information in the memory mesh.
123
+ * The content goes through several processing steps:
124
+ *
125
+ * 1. **Scrubbing**: PII and sensitive data are sanitized (if enabled)
126
+ * 2. **Validation**: Content length and metadata are validated
127
+ * 3. **Embedding**: Content is converted to a vector representation
128
+ * 4. **Storage**: Record is stored in LanceDB with metadata
129
+ * 5. **Emission**: Optional YAMO block emitted for provenance tracking
130
+ *
131
+ * @param content - The text content to store in memory
132
+ * @param metadata - Optional metadata (type, source, tags, etc.)
133
+ * @returns Promise with memory record containing id, content, metadata, created_at
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const memory = await mesh.add("User likes TypeScript", {
138
+ * type: "preference",
139
+ * source: "chat",
140
+ * tags: ["programming", "languages"]
141
+ * });
142
+ * ```
143
+ *
144
+ * @throws {Error} If content exceeds max length (100KB)
145
+ * @throws {Error} If embedding generation fails
146
+ * @throws {Error} If database client is not initialized
147
+ */
148
+ add(content: string, metadata?: any): Promise<any>;
149
+ /**
150
+ * Reflect on recent memories
151
+ */
152
+ reflect(options?: any): Promise<any>;
153
+ /**
154
+ * Ingest synthesized skill
155
+ * @param sourceFilePath - If provided, skip file write (file already exists)
156
+ */
157
+ ingestSkill(yamoText: string, metadata?: any, sourceFilePath?: string): Promise<any>;
158
+ /**
159
+ * Recursive Skill Synthesis
160
+ */
161
+ synthesize(options?: any): Promise<any>;
162
+ /**
163
+ * Update reliability
164
+ */
165
+ updateSkillReliability(id: string, success: boolean): Promise<any>;
166
+ /**
167
+ * Prune skills
168
+ */
169
+ pruneSkills(threshold?: number): Promise<any>;
170
+ /**
171
+ * List all synthesized skills
172
+ * @param {Object} [options={}] - Search options
173
+ * @returns {Promise<Array>} Normalized skill results
174
+ */
175
+ listSkills(options?: any): Promise<any[]>;
176
+ /**
177
+ * Search for synthesized skills by semantic intent
178
+ * @param {string} query - Search query (intent description)
179
+ * @param {Object} [options={}] - Search options
180
+ * @returns {Promise<Array>} Normalized skill results
181
+ */
182
+ searchSkills(query: string, options?: any): Promise<any[]>;
183
+ /**
184
+ * Get recent YAMO logs for the heartbeat
185
+ * @param {Object} options
186
+ */
187
+ getYamoLog(options?: any): Promise<any[]>;
188
+ /**
189
+ * Emit a YAMO block to the YAMO blocks table
190
+ * @private
191
+ *
192
+ * Note: YAMO emission is non-critical - failures are logged but don't throw
193
+ * to prevent disrupting the main operation.
194
+ */
195
+ _emitYamoBlock(operationType: string, memoryId: string | undefined, yamoText: string): Promise<void>;
196
+ /**
197
+ * Search memory using hybrid vector + keyword search with Reciprocal Rank Fusion (RRF).
198
+ *
199
+ * This method performs semantic search by combining:
200
+ * 1. **Vector Search**: Uses embeddings to find semantically similar content
201
+ * 2. **Keyword Search**: Uses BM25-style keyword matching
202
+ * 3. **RRF Fusion**: Combines both result sets using Reciprocal Rank Fusion
203
+ *
204
+ * The RRF algorithm scores each document as: `sum(1 / (k + rank))` where k=60.
205
+ * This gives higher scores to documents that rank well in BOTH searches.
206
+ *
207
+ * **Performance**: Uses adaptive sorting strategy
208
+ * - Small datasets (≤ 2× limit): Full sort O(n log n)
209
+ * - Large datasets: Partial selection sort O(n×k) where k=limit
210
+ *
211
+ * **Caching**: Results are cached for 5 minutes by default (configurable via options)
212
+ *
213
+ * @param query - The search query text
214
+ * @param options - Search options
215
+ * @param options.limit - Maximum results to return (default: 10)
216
+ * @param options.filter - LanceDB filter expression (e.g., "type == 'preference'")
217
+ * @param options.useCache - Enable/disable result caching (default: true)
218
+ * @returns Promise with array of search results, sorted by relevance score
219
+ *
220
+ * @example
221
+ * ```typescript
222
+ * // Simple search
223
+ * const results = await mesh.search("TypeScript preferences");
224
+ *
225
+ * // Search with filter
226
+ * const code = await mesh.search("bug fix", { filter: "type == 'error'" });
227
+ *
228
+ * // Search with limit
229
+ * const top3 = await mesh.search("security issues", { limit: 3 });
230
+ * ```
231
+ *
232
+ * @throws {Error} If embedding generation fails
233
+ * @throws {Error} If database client is not initialized
234
+ */
235
+ search(query: string, options?: any): Promise<SearchResult[]>;
236
+ _normalizeScores(results: SearchResult[]): SearchResult[];
237
+ /**
238
+ * Tokenize query for keyword matching (private helper for searchSkills)
239
+ * Converts text to lowercase tokens, filtering out short tokens and punctuation.
240
+ * Handles camelCase/PascalCase by splitting on uppercase letters.
241
+ */
242
+ private _tokenizeQuery;
243
+ formatResults(results: SearchResult[]): string;
244
+ get(id: string): Promise<any>;
245
+ getAll(options?: any): Promise<any>;
246
+ stats(): Promise<any>;
247
+ _parseEmbeddingConfig(): any[];
248
+ /**
249
+ * Close database connections and release resources
250
+ *
251
+ * This should be called when done with the MemoryMesh to properly:
252
+ * - Close LanceDB connections
253
+ * - Release file handles
254
+ * - Clean up resources
255
+ *
256
+ * Important for tests and cleanup to prevent connection leaks.
257
+ *
258
+ * @returns {Promise<void>}
259
+ *
260
+ * @example
261
+ * ```typescript
262
+ * const mesh = new MemoryMesh();
263
+ * await mesh.init();
264
+ * // ... use mesh ...
265
+ * await mesh.close(); // Clean up
266
+ * ```
267
+ */
268
+ close(): Promise<void>;
269
+ }
270
+ /**
271
+ * Main CLI handler
272
+ */
273
+ export declare function run(): Promise<void>;
274
+ export default MemoryMesh;