File size: 3,052 Bytes
39f19d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// IMPROVEMENT 3: Typed Memory Classification (ENGRAM-style)
// Based on ENGRAM (arxiv 2511.12960)

import { MemoryType } from './types';

const EPISODIC_PATTERNS: RegExp[] = [
  /\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,
  /\b(i|we)\s+(went|visited|saw|met|attended|watched|played|cooked|bought|finished|started|moved|traveled)\b/i,
  /\b(happened|occurred|took place|showed up|came over)\b/i,
  /\b(meeting|appointment|interview|exam|party|wedding|trip|flight|reservation)\s+(is|at|on|tomorrow|next)\b/i,
  /^i\s+(just|recently|finally)\s+/i,
];

const SEMANTIC_PATTERNS: RegExp[] = [
  /\b(i\s+am|i'm|my\s+name\s+is|i\s+live|i\s+work|my\s+(job|profession|occupation))\b/i,
  /\b(i\s+(like|love|enjoy|prefer|hate|dislike|can't\s+stand|always|never))\b/i,
  /\b(i\s+have|i've\s+got|i\s+own|my\s+(favorite|favourite|fave))\b/i,
  /\b(allergic|allergy|intolerant|vegetarian|vegan|diabetic|gluten.?free)\b/i,
  /\b(my\s+(wife|husband|partner|girlfriend|boyfriend|mom|dad|mother|father|sister|brother|son|daughter|friend|boss|colleague))\b/i,
  /\b(i\s+live\s+in|i'm\s+from|born\s+in|i\s+grew\s+up)\b/i,
  /\b(my\s+\w+\s+is|i\s+am\s+\d+|my\s+birthday)\b/i,
];

const PROCEDURAL_PATTERNS: RegExp[] = [
  /\b(always|never|don't|do\s+not|make\s+sure|remember\s+to|remind\s+me|please\s+(always|never))\b/i,
  /\b(when\s+i|whenever|if\s+i\s+(ask|say|mention|want|need))\b/i,
  /\b(respond|reply|answer|speak|talk)\s+(in|with|like|using)\b/i,
  /\b(format|style|write|use)\s+(it|them|this|that|code|text)\s+(as|in|with|like)\b/i,
  /\b(the\s+rule\s+is|my\s+rule|always\s+check|before\s+you|after\s+you)\b/i,
];

export function routeToMemoryType(text: string): MemoryType[] {
  const types: MemoryType[] = [];
  if (EPISODIC_PATTERNS.some(p => p.test(text))) types.push('episodic');
  if (SEMANTIC_PATTERNS.some(p => p.test(text))) types.push('semantic');
  if (PROCEDURAL_PATTERNS.some(p => p.test(text))) types.push('procedural');
  if (types.length === 0) types.push('semantic');
  return types;
}

export function getPrimaryMemoryType(text: string): MemoryType {
  const types = routeToMemoryType(text);
  if (types.includes('procedural')) return 'procedural';
  if (types.includes('semantic')) return 'semantic';
  return 'episodic';
}

export function formatMemoriesByType(memories: Array<{ text: string; type: MemoryType; source: string }>): string {
  const semantic = memories.filter(m => m.type === 'semantic');
  const episodic = memories.filter(m => m.type === 'episodic');
  const procedural = memories.filter(m => m.type === 'procedural');
  const sections: string[] = [];
  if (semantic.length > 0) sections.push(`About the user:\n${semantic.map(m => `- ${m.text}`).join('\n')}`);
  if (episodic.length > 0) sections.push(`Recent events:\n${episodic.map(m => `- ${m.text}`).join('\n')}`);
  if (procedural.length > 0) sections.push(`Instructions:\n${procedural.map(m => `- ${m.text}`).join('\n')}`);
  return sections.join('\n\n');
}