@aitytech/agentkits-memory 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/README.md +250 -0
  2. package/dist/cache-manager.d.ts +134 -0
  3. package/dist/cache-manager.d.ts.map +1 -0
  4. package/dist/cache-manager.js +407 -0
  5. package/dist/cache-manager.js.map +1 -0
  6. package/dist/cli/save.d.ts +20 -0
  7. package/dist/cli/save.d.ts.map +1 -0
  8. package/dist/cli/save.js +94 -0
  9. package/dist/cli/save.js.map +1 -0
  10. package/dist/cli/setup.d.ts +18 -0
  11. package/dist/cli/setup.d.ts.map +1 -0
  12. package/dist/cli/setup.js +163 -0
  13. package/dist/cli/setup.js.map +1 -0
  14. package/dist/cli/viewer.d.ts +21 -0
  15. package/dist/cli/viewer.d.ts.map +1 -0
  16. package/dist/cli/viewer.js +182 -0
  17. package/dist/cli/viewer.js.map +1 -0
  18. package/dist/hnsw-index.d.ts +111 -0
  19. package/dist/hnsw-index.d.ts.map +1 -0
  20. package/dist/hnsw-index.js +781 -0
  21. package/dist/hnsw-index.js.map +1 -0
  22. package/dist/hooks/cli.d.ts +20 -0
  23. package/dist/hooks/cli.d.ts.map +1 -0
  24. package/dist/hooks/cli.js +102 -0
  25. package/dist/hooks/cli.js.map +1 -0
  26. package/dist/hooks/context.d.ts +31 -0
  27. package/dist/hooks/context.d.ts.map +1 -0
  28. package/dist/hooks/context.js +64 -0
  29. package/dist/hooks/context.js.map +1 -0
  30. package/dist/hooks/index.d.ts +16 -0
  31. package/dist/hooks/index.d.ts.map +1 -0
  32. package/dist/hooks/index.js +20 -0
  33. package/dist/hooks/index.js.map +1 -0
  34. package/dist/hooks/observation.d.ts +30 -0
  35. package/dist/hooks/observation.d.ts.map +1 -0
  36. package/dist/hooks/observation.js +79 -0
  37. package/dist/hooks/observation.js.map +1 -0
  38. package/dist/hooks/service.d.ts +102 -0
  39. package/dist/hooks/service.d.ts.map +1 -0
  40. package/dist/hooks/service.js +454 -0
  41. package/dist/hooks/service.js.map +1 -0
  42. package/dist/hooks/session-init.d.ts +30 -0
  43. package/dist/hooks/session-init.d.ts.map +1 -0
  44. package/dist/hooks/session-init.js +54 -0
  45. package/dist/hooks/session-init.js.map +1 -0
  46. package/dist/hooks/summarize.d.ts +30 -0
  47. package/dist/hooks/summarize.d.ts.map +1 -0
  48. package/dist/hooks/summarize.js +74 -0
  49. package/dist/hooks/summarize.js.map +1 -0
  50. package/dist/hooks/types.d.ts +193 -0
  51. package/dist/hooks/types.d.ts.map +1 -0
  52. package/dist/hooks/types.js +137 -0
  53. package/dist/hooks/types.js.map +1 -0
  54. package/dist/index.d.ts +173 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +564 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/mcp/index.d.ts +9 -0
  59. package/dist/mcp/index.d.ts.map +1 -0
  60. package/dist/mcp/index.js +9 -0
  61. package/dist/mcp/index.js.map +1 -0
  62. package/dist/mcp/server.d.ts +22 -0
  63. package/dist/mcp/server.d.ts.map +1 -0
  64. package/dist/mcp/server.js +368 -0
  65. package/dist/mcp/server.js.map +1 -0
  66. package/dist/mcp/tools.d.ts +14 -0
  67. package/dist/mcp/tools.d.ts.map +1 -0
  68. package/dist/mcp/tools.js +110 -0
  69. package/dist/mcp/tools.js.map +1 -0
  70. package/dist/mcp/types.d.ts +100 -0
  71. package/dist/mcp/types.d.ts.map +1 -0
  72. package/dist/mcp/types.js +9 -0
  73. package/dist/mcp/types.js.map +1 -0
  74. package/dist/migration.d.ts +77 -0
  75. package/dist/migration.d.ts.map +1 -0
  76. package/dist/migration.js +457 -0
  77. package/dist/migration.js.map +1 -0
  78. package/dist/sqljs-backend.d.ts +128 -0
  79. package/dist/sqljs-backend.d.ts.map +1 -0
  80. package/dist/sqljs-backend.js +623 -0
  81. package/dist/sqljs-backend.js.map +1 -0
  82. package/dist/types.d.ts +481 -0
  83. package/dist/types.d.ts.map +1 -0
  84. package/dist/types.js +73 -0
  85. package/dist/types.js.map +1 -0
  86. package/hooks.json +46 -0
  87. package/package.json +67 -0
  88. package/src/__tests__/index.test.ts +407 -0
  89. package/src/__tests__/sqljs-backend.test.ts +410 -0
  90. package/src/cache-manager.ts +515 -0
  91. package/src/cli/save.ts +109 -0
  92. package/src/cli/setup.ts +203 -0
  93. package/src/cli/viewer.ts +218 -0
  94. package/src/hnsw-index.ts +1013 -0
  95. package/src/hooks/__tests__/handlers.test.ts +298 -0
  96. package/src/hooks/__tests__/integration.test.ts +431 -0
  97. package/src/hooks/__tests__/service.test.ts +487 -0
  98. package/src/hooks/__tests__/types.test.ts +341 -0
  99. package/src/hooks/cli.ts +121 -0
  100. package/src/hooks/context.ts +77 -0
  101. package/src/hooks/index.ts +23 -0
  102. package/src/hooks/observation.ts +102 -0
  103. package/src/hooks/service.ts +582 -0
  104. package/src/hooks/session-init.ts +70 -0
  105. package/src/hooks/summarize.ts +89 -0
  106. package/src/hooks/types.ts +365 -0
  107. package/src/index.ts +755 -0
  108. package/src/mcp/__tests__/server.test.ts +181 -0
  109. package/src/mcp/index.ts +9 -0
  110. package/src/mcp/server.ts +441 -0
  111. package/src/mcp/tools.ts +113 -0
  112. package/src/mcp/types.ts +109 -0
  113. package/src/migration.ts +574 -0
  114. package/src/sql.js.d.ts +70 -0
  115. package/src/sqljs-backend.ts +789 -0
  116. package/src/types.ts +715 -0
package/src/types.ts ADDED
@@ -0,0 +1,715 @@
1
+ /**
2
+ * Project-Scoped Memory Types
3
+ *
4
+ * Type definitions for the AgentKits memory system.
5
+ * Designed for project-level memory stored in .claude/memory/
6
+ *
7
+ * @module @agentkits/memory/types
8
+ */
9
+
10
+ // ===== Core Memory Entry Types =====
11
+
12
+ /**
13
+ * Memory entry type classification (matches existing .claude/memory/*.md structure)
14
+ */
15
+ export type MemoryType =
16
+ | 'episodic' // Time-based: active-context, session-state, progress
17
+ | 'semantic' // Facts: project-context, patterns
18
+ | 'procedural' // How-to: decisions, errors
19
+ | 'working' // Short-term operational memory
20
+ | 'cache'; // Temporary cached data
21
+
22
+ /**
23
+ * Access level for memory entries
24
+ */
25
+ export type AccessLevel =
26
+ | 'private' // Only this session
27
+ | 'project' // All sessions in this project
28
+ | 'shared'; // Shared across projects (future)
29
+
30
+ /**
31
+ * Distance metrics for vector similarity search
32
+ */
33
+ export type DistanceMetric =
34
+ | 'cosine' // Cosine similarity (default)
35
+ | 'euclidean' // Euclidean distance (L2)
36
+ | 'dot' // Dot product
37
+ | 'manhattan'; // Manhattan distance (L1)
38
+
39
+ // ===== Memory Entry =====
40
+
41
+ /**
42
+ * Core memory entry structure with optional vector embedding
43
+ */
44
+ export interface MemoryEntry {
45
+ /** Unique identifier */
46
+ id: string;
47
+
48
+ /** Human-readable key for retrieval (e.g., 'active-context', 'auth-pattern') */
49
+ key: string;
50
+
51
+ /** Actual content of the memory */
52
+ content: string;
53
+
54
+ /** Vector embedding for semantic search (Float32Array for efficiency) */
55
+ embedding?: Float32Array;
56
+
57
+ /** Type of memory */
58
+ type: MemoryType;
59
+
60
+ /** Namespace for organization (maps to old .md file categories) */
61
+ namespace: string;
62
+
63
+ /** Tags for categorization and filtering */
64
+ tags: string[];
65
+
66
+ /** Additional metadata */
67
+ metadata: Record<string, unknown>;
68
+
69
+ /** Session ID that created this entry */
70
+ sessionId?: string;
71
+
72
+ /** Owner ID for multi-user scenarios */
73
+ ownerId?: string;
74
+
75
+ /** Access level */
76
+ accessLevel: AccessLevel;
77
+
78
+ /** Creation timestamp */
79
+ createdAt: number;
80
+
81
+ /** Last update timestamp */
82
+ updatedAt: number;
83
+
84
+ /** Expiration timestamp (optional) */
85
+ expiresAt?: number;
86
+
87
+ /** Version number for optimistic locking */
88
+ version: number;
89
+
90
+ /** References to other memory entries */
91
+ references: string[];
92
+
93
+ /** Access count for usage tracking */
94
+ accessCount: number;
95
+
96
+ /** Last access timestamp */
97
+ lastAccessedAt: number;
98
+ }
99
+
100
+ /**
101
+ * Input for creating a new memory entry
102
+ */
103
+ export interface MemoryEntryInput {
104
+ key: string;
105
+ content: string;
106
+ type?: MemoryType;
107
+ namespace?: string;
108
+ tags?: string[];
109
+ metadata?: Record<string, unknown>;
110
+ sessionId?: string;
111
+ accessLevel?: AccessLevel;
112
+ expiresAt?: number;
113
+ references?: string[];
114
+ }
115
+
116
+ /**
117
+ * Partial update for a memory entry
118
+ */
119
+ export interface MemoryEntryUpdate {
120
+ content?: string;
121
+ tags?: string[];
122
+ metadata?: Record<string, unknown>;
123
+ accessLevel?: AccessLevel;
124
+ expiresAt?: number;
125
+ references?: string[];
126
+ }
127
+
128
+ // ===== Query Types =====
129
+
130
+ /**
131
+ * Query type for memory retrieval
132
+ */
133
+ export type QueryType =
134
+ | 'semantic' // Vector similarity search
135
+ | 'exact' // Exact key match
136
+ | 'prefix' // Key prefix match
137
+ | 'tag' // Tag-based search
138
+ | 'hybrid'; // Combined semantic + filters
139
+
140
+ /**
141
+ * Memory query specification
142
+ */
143
+ export interface MemoryQuery {
144
+ /** Type of query to perform */
145
+ type: QueryType;
146
+
147
+ /** Content for semantic search (will be embedded) */
148
+ content?: string;
149
+
150
+ /** Pre-computed embedding for semantic search */
151
+ embedding?: Float32Array;
152
+
153
+ /** Exact key to match */
154
+ key?: string;
155
+
156
+ /** Key prefix to match */
157
+ keyPrefix?: string;
158
+
159
+ /** Namespace filter */
160
+ namespace?: string;
161
+
162
+ /** Tag filters (entries must have all specified tags) */
163
+ tags?: string[];
164
+
165
+ /** Memory type filter */
166
+ memoryType?: MemoryType;
167
+
168
+ /** Session ID filter */
169
+ sessionId?: string;
170
+
171
+ /** Owner ID filter */
172
+ ownerId?: string;
173
+
174
+ /** Access level filter */
175
+ accessLevel?: AccessLevel;
176
+
177
+ /** Metadata filters */
178
+ metadata?: Record<string, unknown>;
179
+
180
+ /** Time range filters */
181
+ createdAfter?: number;
182
+ createdBefore?: number;
183
+ updatedAfter?: number;
184
+ updatedBefore?: number;
185
+
186
+ /** Maximum number of results */
187
+ limit: number;
188
+
189
+ /** Offset for pagination */
190
+ offset?: number;
191
+
192
+ /** Minimum similarity threshold (0-1) for semantic search */
193
+ threshold?: number;
194
+
195
+ /** Include expired entries */
196
+ includeExpired?: boolean;
197
+
198
+ /** Distance metric for semantic search */
199
+ distanceMetric?: DistanceMetric;
200
+ }
201
+
202
+ /**
203
+ * Search result with similarity score
204
+ */
205
+ export interface SearchResult {
206
+ /** The memory entry */
207
+ entry: MemoryEntry;
208
+
209
+ /** Similarity score (0-1, higher is better) */
210
+ score: number;
211
+
212
+ /** Distance from query vector */
213
+ distance: number;
214
+ }
215
+
216
+ /**
217
+ * Search options for vector search
218
+ */
219
+ export interface SearchOptions {
220
+ /** Number of results to return */
221
+ k: number;
222
+
223
+ /** Minimum similarity threshold (0-1) */
224
+ threshold?: number;
225
+
226
+ /** Distance metric */
227
+ metric?: DistanceMetric;
228
+
229
+ /** Additional filters to apply post-search */
230
+ filters?: Partial<MemoryQuery>;
231
+ }
232
+
233
+ // ===== HNSW Index Types =====
234
+
235
+ /**
236
+ * Quantization type
237
+ */
238
+ export type QuantizationType = 'scalar' | 'product' | 'binary' | 'none';
239
+
240
+ /**
241
+ * Quantization configuration for vector compression
242
+ */
243
+ export interface QuantizationConfig {
244
+ /** Enable quantization */
245
+ enabled: boolean;
246
+
247
+ /** Number of bits per dimension (default: 8) */
248
+ bits: number;
249
+
250
+ /** Quantization method */
251
+ method: 'scalar' | 'product';
252
+
253
+ /** Quantization type (alternative to method) */
254
+ type?: QuantizationType;
255
+
256
+ /** Number of subquantizers for product quantization */
257
+ subquantizers?: number;
258
+ }
259
+
260
+ /**
261
+ * HNSW index configuration
262
+ */
263
+ export interface HNSWConfig {
264
+ /** Vector dimensions (e.g., 384 for local models, 1536 for OpenAI) */
265
+ dimensions: number;
266
+
267
+ /** Maximum number of connections per layer (default: 16) */
268
+ M: number;
269
+
270
+ /** Size of the dynamic candidate list during construction (default: 200) */
271
+ efConstruction: number;
272
+
273
+ /** Maximum elements the index can hold */
274
+ maxElements: number;
275
+
276
+ /** Distance metric */
277
+ metric: DistanceMetric;
278
+
279
+ /** Quantization configuration */
280
+ quantization?: QuantizationConfig;
281
+ }
282
+
283
+ /**
284
+ * HNSW index statistics
285
+ */
286
+ export interface HNSWStats {
287
+ /** Total number of vectors in the index */
288
+ vectorCount: number;
289
+
290
+ /** Memory usage in bytes */
291
+ memoryUsage: number;
292
+
293
+ /** Average search time in milliseconds */
294
+ avgSearchTime: number;
295
+
296
+ /** Index build time in milliseconds */
297
+ buildTime: number;
298
+
299
+ /** Compression ratio if quantization is enabled */
300
+ compressionRatio?: number;
301
+ }
302
+
303
+ // ===== Backend Interface =====
304
+
305
+ /**
306
+ * Memory backend interface for storage and retrieval
307
+ */
308
+ export interface IMemoryBackend {
309
+ /** Initialize the backend */
310
+ initialize(): Promise<void>;
311
+
312
+ /** Shutdown the backend */
313
+ shutdown(): Promise<void>;
314
+
315
+ /** Store a memory entry */
316
+ store(entry: MemoryEntry): Promise<void>;
317
+
318
+ /** Retrieve a memory entry by ID */
319
+ get(id: string): Promise<MemoryEntry | null>;
320
+
321
+ /** Retrieve a memory entry by key within a namespace */
322
+ getByKey(namespace: string, key: string): Promise<MemoryEntry | null>;
323
+
324
+ /** Update a memory entry */
325
+ update(id: string, update: MemoryEntryUpdate): Promise<MemoryEntry | null>;
326
+
327
+ /** Delete a memory entry */
328
+ delete(id: string): Promise<boolean>;
329
+
330
+ /** Query memory entries */
331
+ query(query: MemoryQuery): Promise<MemoryEntry[]>;
332
+
333
+ /** Semantic vector search */
334
+ search(embedding: Float32Array, options: SearchOptions): Promise<SearchResult[]>;
335
+
336
+ /** Bulk insert entries */
337
+ bulkInsert(entries: MemoryEntry[]): Promise<void>;
338
+
339
+ /** Bulk delete entries */
340
+ bulkDelete(ids: string[]): Promise<number>;
341
+
342
+ /** Get entry count */
343
+ count(namespace?: string): Promise<number>;
344
+
345
+ /** List all namespaces */
346
+ listNamespaces(): Promise<string[]>;
347
+
348
+ /** Clear all entries in a namespace */
349
+ clearNamespace(namespace: string): Promise<number>;
350
+
351
+ /** Get backend statistics */
352
+ getStats(): Promise<BackendStats>;
353
+
354
+ /** Perform health check */
355
+ healthCheck(): Promise<HealthCheckResult>;
356
+ }
357
+
358
+ /**
359
+ * Backend statistics
360
+ */
361
+ export interface BackendStats {
362
+ /** Total number of entries */
363
+ totalEntries: number;
364
+
365
+ /** Entries by namespace */
366
+ entriesByNamespace: Record<string, number>;
367
+
368
+ /** Entries by type */
369
+ entriesByType: Record<MemoryType, number>;
370
+
371
+ /** Total memory usage in bytes */
372
+ memoryUsage: number;
373
+
374
+ /** HNSW index statistics */
375
+ hnswStats?: HNSWStats;
376
+
377
+ /** Cache statistics */
378
+ cacheStats?: CacheStats;
379
+
380
+ /** Average query time in milliseconds */
381
+ avgQueryTime: number;
382
+
383
+ /** Average search time in milliseconds */
384
+ avgSearchTime: number;
385
+ }
386
+
387
+ /**
388
+ * Health check result
389
+ */
390
+ export interface HealthCheckResult {
391
+ /** Overall health status */
392
+ status: 'healthy' | 'degraded' | 'unhealthy';
393
+
394
+ /** Individual component health */
395
+ components: {
396
+ storage: ComponentHealth;
397
+ index: ComponentHealth;
398
+ cache: ComponentHealth;
399
+ };
400
+
401
+ /** Health check timestamp */
402
+ timestamp: number;
403
+
404
+ /** Any issues detected */
405
+ issues: string[];
406
+
407
+ /** Recommendations for improvement */
408
+ recommendations: string[];
409
+ }
410
+
411
+ /**
412
+ * Individual component health status
413
+ */
414
+ export interface ComponentHealth {
415
+ status: 'healthy' | 'degraded' | 'unhealthy';
416
+ latency: number;
417
+ message?: string;
418
+ }
419
+
420
+ // ===== Cache Types =====
421
+
422
+ /**
423
+ * Cache configuration
424
+ */
425
+ export interface CacheConfig {
426
+ /** Maximum number of entries in the cache */
427
+ maxSize: number;
428
+
429
+ /** Default TTL in milliseconds */
430
+ ttl: number;
431
+
432
+ /** Enable LRU eviction */
433
+ lruEnabled: boolean;
434
+
435
+ /** Maximum memory usage in bytes */
436
+ maxMemory?: number;
437
+
438
+ /** Enable write-through caching (write to backend immediately) */
439
+ writeThrough?: boolean;
440
+ }
441
+
442
+ /**
443
+ * Cache statistics
444
+ */
445
+ export interface CacheStats {
446
+ /** Number of entries in cache */
447
+ size: number;
448
+
449
+ /** Cache hit rate (0-1) */
450
+ hitRate: number;
451
+
452
+ /** Total cache hits */
453
+ hits: number;
454
+
455
+ /** Total cache misses */
456
+ misses: number;
457
+
458
+ /** Total evictions */
459
+ evictions: number;
460
+
461
+ /** Memory usage in bytes */
462
+ memoryUsage: number;
463
+ }
464
+
465
+ /**
466
+ * Cached entry wrapper
467
+ */
468
+ export interface CachedEntry<T> {
469
+ /** The cached data */
470
+ data: T;
471
+
472
+ /** When the entry was cached */
473
+ cachedAt: number;
474
+
475
+ /** When the entry expires */
476
+ expiresAt: number;
477
+
478
+ /** Last access timestamp */
479
+ lastAccessedAt: number;
480
+
481
+ /** Access count */
482
+ accessCount: number;
483
+ }
484
+
485
+ // ===== Migration Types =====
486
+
487
+ /**
488
+ * Migration source type
489
+ */
490
+ export type MigrationSource =
491
+ | 'markdown' // .claude/memory/*.md files
492
+ | 'json' // JSON exports
493
+ | 'sqlite'; // Existing SQLite databases
494
+
495
+ /**
496
+ * Migration configuration
497
+ */
498
+ export interface MigrationConfig {
499
+ /** Source backend type */
500
+ source: MigrationSource;
501
+
502
+ /** Source path or connection string */
503
+ sourcePath: string;
504
+
505
+ /** Batch size for migration */
506
+ batchSize: number;
507
+
508
+ /** Generate embeddings during migration */
509
+ generateEmbeddings: boolean;
510
+
511
+ /** Validate data during migration */
512
+ validateData: boolean;
513
+
514
+ /** Continue on error */
515
+ continueOnError: boolean;
516
+
517
+ /** Namespace mapping */
518
+ namespaceMapping?: Record<string, string>;
519
+
520
+ /** Type mapping */
521
+ typeMapping?: Record<string, MemoryType>;
522
+ }
523
+
524
+ /**
525
+ * Migration progress
526
+ */
527
+ export interface MigrationProgress {
528
+ /** Total entries to migrate */
529
+ total: number;
530
+
531
+ /** Entries migrated so far */
532
+ migrated: number;
533
+
534
+ /** Entries failed */
535
+ failed: number;
536
+
537
+ /** Entries skipped */
538
+ skipped: number;
539
+
540
+ /** Progress percentage (0-100) */
541
+ percentage: number;
542
+
543
+ /** Errors encountered */
544
+ errors: MigrationError[];
545
+ }
546
+
547
+ /**
548
+ * Migration error
549
+ */
550
+ export interface MigrationError {
551
+ /** Entry ID or key that failed */
552
+ entryId: string;
553
+
554
+ /** Error message */
555
+ message: string;
556
+
557
+ /** Error code */
558
+ code: string;
559
+
560
+ /** Whether the error is recoverable */
561
+ recoverable: boolean;
562
+ }
563
+
564
+ /**
565
+ * Migration result
566
+ */
567
+ export interface MigrationResult {
568
+ /** Whether migration completed successfully */
569
+ success: boolean;
570
+
571
+ /** Final progress state */
572
+ progress: MigrationProgress;
573
+
574
+ /** Total time taken in milliseconds */
575
+ duration: number;
576
+
577
+ /** Summary message */
578
+ summary: string;
579
+ }
580
+
581
+ // ===== Session Types =====
582
+
583
+ /**
584
+ * Session information for tracking Claude Code sessions
585
+ */
586
+ export interface SessionInfo {
587
+ /** Session ID */
588
+ id: string;
589
+
590
+ /** Session start time */
591
+ startedAt: number;
592
+
593
+ /** Session end time (if ended) */
594
+ endedAt?: number;
595
+
596
+ /** Summary of work done */
597
+ summary?: string;
598
+
599
+ /** Status */
600
+ status: 'active' | 'completed' | 'abandoned';
601
+
602
+ /** Last checkpoint */
603
+ lastCheckpoint?: string;
604
+ }
605
+
606
+ // ===== Event Types =====
607
+
608
+ /**
609
+ * Memory event types for index operations
610
+ */
611
+ export type MemoryEventType =
612
+ | 'insert'
613
+ | 'update'
614
+ | 'delete'
615
+ | 'search'
616
+ | 'rebuild'
617
+ | 'resize';
618
+
619
+ /**
620
+ * Memory event for tracking index operations
621
+ */
622
+ export interface MemoryEvent {
623
+ /** Event type */
624
+ type: MemoryEventType;
625
+
626
+ /** Entry ID (if applicable) */
627
+ entryId?: string;
628
+
629
+ /** Timestamp */
630
+ timestamp: number;
631
+
632
+ /** Additional event data */
633
+ data?: Record<string, unknown>;
634
+ }
635
+
636
+ /**
637
+ * Handler function for memory events
638
+ */
639
+ export type MemoryEventHandler = (event: MemoryEvent) => void | Promise<void>;
640
+
641
+ // ===== Utility Types =====
642
+
643
+ /**
644
+ * Embedding generator function type
645
+ */
646
+ export type EmbeddingGenerator = (content: string) => Promise<Float32Array>;
647
+
648
+ /**
649
+ * Generates a unique memory ID
650
+ */
651
+ export function generateMemoryId(): string {
652
+ const timestamp = Date.now().toString(36);
653
+ const random = Math.random().toString(36).substring(2, 10);
654
+ return `mem_${timestamp}_${random}`;
655
+ }
656
+
657
+ /**
658
+ * Generates a unique session ID
659
+ */
660
+ export function generateSessionId(): string {
661
+ const timestamp = Date.now().toString(36);
662
+ const random = Math.random().toString(36).substring(2, 6);
663
+ return `ses_${timestamp}_${random}`;
664
+ }
665
+
666
+ /**
667
+ * Creates a default memory entry
668
+ */
669
+ export function createDefaultEntry(input: MemoryEntryInput): MemoryEntry {
670
+ const now = Date.now();
671
+ return {
672
+ id: generateMemoryId(),
673
+ key: input.key,
674
+ content: input.content,
675
+ type: input.type || 'semantic',
676
+ namespace: input.namespace || 'default',
677
+ tags: input.tags || [],
678
+ metadata: input.metadata || {},
679
+ sessionId: input.sessionId,
680
+ accessLevel: input.accessLevel || 'project',
681
+ createdAt: now,
682
+ updatedAt: now,
683
+ expiresAt: input.expiresAt,
684
+ version: 1,
685
+ references: input.references || [],
686
+ accessCount: 0,
687
+ lastAccessedAt: now,
688
+ };
689
+ }
690
+
691
+ /**
692
+ * Default namespaces matching existing .claude/memory/*.md structure
693
+ */
694
+ export const DEFAULT_NAMESPACES = {
695
+ CONTEXT: 'context', // project-context.md
696
+ ACTIVE: 'active-context', // active-context.md
697
+ SESSION: 'session-state', // session-state.md
698
+ PROGRESS: 'progress', // progress.md
699
+ PATTERNS: 'patterns', // patterns.md
700
+ DECISIONS: 'decisions', // decisions.md
701
+ ERRORS: 'errors', // errors.md
702
+ } as const;
703
+
704
+ /**
705
+ * Maps namespace to memory type
706
+ */
707
+ export const NAMESPACE_TYPE_MAP: Record<string, MemoryType> = {
708
+ [DEFAULT_NAMESPACES.CONTEXT]: 'semantic',
709
+ [DEFAULT_NAMESPACES.ACTIVE]: 'episodic',
710
+ [DEFAULT_NAMESPACES.SESSION]: 'episodic',
711
+ [DEFAULT_NAMESPACES.PROGRESS]: 'episodic',
712
+ [DEFAULT_NAMESPACES.PATTERNS]: 'semantic',
713
+ [DEFAULT_NAMESPACES.DECISIONS]: 'procedural',
714
+ [DEFAULT_NAMESPACES.ERRORS]: 'procedural',
715
+ };