Spaces:
Build error
Build error
| import { MemoryModel } from '../memory/db.js'; | |
| export const saveMemoryTool = { | |
| type: 'function', | |
| function: { | |
| name: 'save_memory', | |
| description: 'Save important facts into long-term memory. ONLY use this when the user explicitly states a personal fact (e.g. "I am 30 years old", "my dog is Rex", "I live in Berlin") that must be remembered forever. DO NOT use this for conversational filler, temporary intentions, or greetings.', | |
| parameters: { | |
| type: 'object', | |
| properties: { | |
| content: { | |
| type: 'string', | |
| description: 'The precise fact to remember. Write it from your perspective, e.g. "The user has a dog named Rex"' | |
| } | |
| }, | |
| required: ['content'] | |
| } | |
| } | |
| }; | |
| export const searchMemoryTool = { | |
| type: 'function', | |
| function: { | |
| name: 'search_memory', | |
| description: 'Search the users long-term memory using specific keywords to retrieve past facts, preferences, and context.', | |
| parameters: { | |
| type: 'object', | |
| properties: { | |
| query: { | |
| type: 'string', | |
| description: 'The keyword or phrase to search for. E.g "dog" or "Rex"' | |
| } | |
| }, | |
| required: ['query'] | |
| } | |
| } | |
| }; | |
| export async function executeSaveMemory(content: string): Promise<string> { | |
| try { | |
| const now = new Date().toISOString(); | |
| await MemoryModel.create({ content, timestamp: now }); | |
| return `Successfully saved memory: ${content}`; | |
| } catch (e: any) { | |
| return `Failed to save memory: ${e.message}`; | |
| } | |
| } | |
| export async function executeSearchMemory(query: string): Promise<string> { | |
| try { | |
| // Use MongoDB text search | |
| const safeQuery = query.replace(/"/g, ''); | |
| const rows = await MemoryModel.find( | |
| { $text: { $search: safeQuery } }, | |
| { score: { $meta: "textScore" } } | |
| ) | |
| .sort({ score: { $meta: "textScore" } }) | |
| .limit(10) | |
| .lean(); | |
| if (rows.length === 0) { | |
| return `No memories found matching "${query}"`; | |
| } | |
| const results = rows.map(r => `[${r.timestamp}] ${r.content}`).join('\\n'); | |
| return `Found memories: \\n${results}`; | |
| } catch (e: any) { | |
| return `Error searching memory: ${e.message}`; | |
| } | |
| } | |