loudiman commited on
Commit
39f19d4
·
verified ·
1 Parent(s): 0460020

Add typedMemory

Browse files
Files changed (1) hide show
  1. src/typedMemory.ts +57 -0
src/typedMemory.ts ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // IMPROVEMENT 3: Typed Memory Classification (ENGRAM-style)
2
+ // Based on ENGRAM (arxiv 2511.12960)
3
+
4
+ import { MemoryType } from './types';
5
+
6
+ const EPISODIC_PATTERNS: RegExp[] = [
7
+ /\b(yesterday|today|tomorrow|last\s+(week|month|year|night|time)|this\s+(morning|afternoon|evening|week)|just\s+(now|did|got|went|had|came|finished))\b/i,
8
+ /\b(i|we)\s+(went|visited|saw|met|attended|watched|played|cooked|bought|finished|started|moved|traveled)\b/i,
9
+ /\b(happened|occurred|took place|showed up|came over)\b/i,
10
+ /\b(meeting|appointment|interview|exam|party|wedding|trip|flight|reservation)\s+(is|at|on|tomorrow|next)\b/i,
11
+ /^i\s+(just|recently|finally)\s+/i,
12
+ ];
13
+
14
+ const SEMANTIC_PATTERNS: RegExp[] = [
15
+ /\b(i\s+am|i'm|my\s+name\s+is|i\s+live|i\s+work|my\s+(job|profession|occupation))\b/i,
16
+ /\b(i\s+(like|love|enjoy|prefer|hate|dislike|can't\s+stand|always|never))\b/i,
17
+ /\b(i\s+have|i've\s+got|i\s+own|my\s+(favorite|favourite|fave))\b/i,
18
+ /\b(allergic|allergy|intolerant|vegetarian|vegan|diabetic|gluten.?free)\b/i,
19
+ /\b(my\s+(wife|husband|partner|girlfriend|boyfriend|mom|dad|mother|father|sister|brother|son|daughter|friend|boss|colleague))\b/i,
20
+ /\b(i\s+live\s+in|i'm\s+from|born\s+in|i\s+grew\s+up)\b/i,
21
+ /\b(my\s+\w+\s+is|i\s+am\s+\d+|my\s+birthday)\b/i,
22
+ ];
23
+
24
+ const PROCEDURAL_PATTERNS: RegExp[] = [
25
+ /\b(always|never|don't|do\s+not|make\s+sure|remember\s+to|remind\s+me|please\s+(always|never))\b/i,
26
+ /\b(when\s+i|whenever|if\s+i\s+(ask|say|mention|want|need))\b/i,
27
+ /\b(respond|reply|answer|speak|talk)\s+(in|with|like|using)\b/i,
28
+ /\b(format|style|write|use)\s+(it|them|this|that|code|text)\s+(as|in|with|like)\b/i,
29
+ /\b(the\s+rule\s+is|my\s+rule|always\s+check|before\s+you|after\s+you)\b/i,
30
+ ];
31
+
32
+ export function routeToMemoryType(text: string): MemoryType[] {
33
+ const types: MemoryType[] = [];
34
+ if (EPISODIC_PATTERNS.some(p => p.test(text))) types.push('episodic');
35
+ if (SEMANTIC_PATTERNS.some(p => p.test(text))) types.push('semantic');
36
+ if (PROCEDURAL_PATTERNS.some(p => p.test(text))) types.push('procedural');
37
+ if (types.length === 0) types.push('semantic');
38
+ return types;
39
+ }
40
+
41
+ export function getPrimaryMemoryType(text: string): MemoryType {
42
+ const types = routeToMemoryType(text);
43
+ if (types.includes('procedural')) return 'procedural';
44
+ if (types.includes('semantic')) return 'semantic';
45
+ return 'episodic';
46
+ }
47
+
48
+ export function formatMemoriesByType(memories: Array<{ text: string; type: MemoryType; source: string }>): string {
49
+ const semantic = memories.filter(m => m.type === 'semantic');
50
+ const episodic = memories.filter(m => m.type === 'episodic');
51
+ const procedural = memories.filter(m => m.type === 'procedural');
52
+ const sections: string[] = [];
53
+ if (semantic.length > 0) sections.push(`About the user:\n${semantic.map(m => `- ${m.text}`).join('\n')}`);
54
+ if (episodic.length > 0) sections.push(`Recent events:\n${episodic.map(m => `- ${m.text}`).join('\n')}`);
55
+ if (procedural.length > 0) sections.push(`Instructions:\n${procedural.map(m => `- ${m.text}`).join('\n')}`);
56
+ return sections.join('\n\n');
57
+ }