| import { MemoryType, MemorySource } from './types'; |
| import { getPrimaryMemoryType } from './typedMemory'; |
|
|
| const ASSISTANT_STORE_PATTERNS: RegExp[] = [ |
| /\b(i\s+(recommend|suggest)|you\s+(should|could|might)\s+(try|consider|check\s+out|look\s+into))\b/i, |
| /\b(i('ll|\s+will)\s+(remind|remember|keep\s+in\s+mind|note\s+that|make\s+sure))\b/i, |
| /\b(you\s+(mentioned|said|told\s+me|like|prefer|are|have))\b/i, |
| /\b(how\s+about|what\s+about|maybe\s+try|consider\s+(going|eating|watching|reading))\b/i, |
| /\b(make\s+sure|be\s+careful|avoid|don't\s+forget|watch\s+out|remember\s+that)\b/i, |
| ]; |
| const ASSISTANT_SKIP_PATTERNS: RegExp[] = [ |
| /^(sure|okay|got\s+it|no\s+problem|you're\s+welcome|happy\s+to\s+help|of\s+course|absolutely)[\s!.]*$/i, |
| /^(what|how|when|where|do\s+you|would\s+you|have\s+you|are\s+you|can\s+you).+\?$/i, |
| /^.{1,20}$/, |
| ]; |
| export function shouldStoreAssistantMessage(text: string): boolean { |
| if (ASSISTANT_SKIP_PATTERNS.some(p => p.test(text.trim()))) return false; |
| if (ASSISTANT_STORE_PATTERNS.some(p => p.test(text))) return true; |
| return false; |
| } |
| export function extractAssistantMemory(text: string): string { |
| const sentences = text.split(/[.!?]+/).map(s => s.trim()).filter(s => s.length > 10); |
| if (!sentences.length) return text.trim(); |
| const scored = sentences.map(s => ({ s, sc: (/\b(try|suggest|recommend)\b/i.test(s)?2:0)+(/\b(avoid|careful|don't)\b/i.test(s)?2:0)+(s.length>30?1:0) })); |
| scored.sort((a,b) => b.sc - a.sc); |
| return `[Assistant] ${scored.slice(0,2).map(x=>x.s).join('. ')}`; |
| } |
| export async function processAssistantForMemory(reply: string, embedFn: (t:string)=>Promise<number[]>) { |
| if (!shouldStoreAssistantMessage(reply)) return null; |
| const text = extractAssistantMemory(reply); |
| return { text, embedding: await embedFn(text), type: getPrimaryMemoryType(text), source: 'assistant' as MemorySource }; |
| } |
|
|