add_long_term_memory
Store persistent memories that automatically activate when specific conversation conditions are met, using JavaScript triggers to determine relevance.
Instructions
Add a new long-term memory with a trigger condition. The trigger is JavaScript code that determines when this memory should be activated. Available context: context.messages (array), context.conversation_id (string), context.participants (object). Available functions: match_keys(messages, keywords, scope, depth), match_keys_all(messages, keywords, scope, depth).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Unique name for the memory | |
| prompt | Yes | The memory content to be recalled when triggered | |
| trigger | Yes | JavaScript code that returns true/false to determine if memory should activate. Example: "match_keys(context.messages, ['birthday'], 'any') || new Date().getMonth() === 6" | |
| conversation_id | No | Conversation ID that owns this memory (defaults to "default") | |
| createdContext | No | Optional context about when/why this memory was created | |
| recentMessages | No | Optional recent messages to auto-generate createdContext |
Implementation Reference
- src/tools/long-term-tools.js:32-66 (handler)The main handler function for the 'add_long_term_memory' tool. It processes input arguments, auto-generates context if recentMessages provided, adds the memory via memoryManager.addMemory, saves to storage, and returns success/error response.handler: async (args) => { try { // Auto-generate context if messages provided let createdContext = args.createdContext || ''; if (!createdContext && args.recentMessages) { createdContext = createContextSnapshot(args.recentMessages, 4); } const result = await memoryManager.addMemory({ name: args.name, prompt: args.prompt, trigger: args.trigger, createdContext }); if (result.success) { await storageManager.saveLongTermMemories(memoryManager.getMemories()); return { success: true, message: `Memory "${args.name}" added successfully`, totalMemories: memoryManager.getMemories().length }; } else { return { success: false, error: result.error }; } } catch (error) { return { success: false, error: error.message }; } }
- src/tools/long-term-tools.js:21-31 (schema)Zod inputSchema defining parameters for the tool: name, prompt, trigger, conversation_id (opt), createdContext (opt), recentMessages (opt).inputSchema: z.object({ name: z.string().describe('Unique name for the memory'), prompt: z.string().describe('The memory content to be recalled when triggered'), trigger: z.string().describe('JavaScript code that returns true/false to determine if memory should activate. Example: "match_keys(context.messages, [\'birthday\'], \'any\') || new Date().getMonth() === 6"'), conversation_id: z.string().optional().describe('Conversation ID that owns this memory (defaults to "default")'), createdContext: z.string().optional().describe('Optional context about when/why this memory was created'), recentMessages: z.array(z.object({ role: z.enum(['user', 'assistant', 'system']), content: z.string() })).optional().describe('Optional recent messages to auto-generate createdContext') }),
- src/index.js:156-158 (registration)Static registration of default long-term tools (including add_long_term_memory) to the toolRegistry for list_tools response.// 注册所有长期记忆工具 const longTermTools = createLongTermTools(defaultLongTermManager, defaultStorageManager); longTermTools.forEach(tool => registerTool(tool, 'long-term'));
- src/index.js:291-295 (registration)Dynamic registration/execution: recreates long-term tools with conversation-specific managers and calls the handler for 'add_long_term_memory' during tool invocation.manager = await getLongTermManager(conversationId); storage = getStorageManager(conversationId); const tools = createLongTermTools(manager, storage); const tool = tools.find(t => t.name === toolName); result = await withTimeout(tool.handler(validatedArgs), timeout, `Tool ${toolName} timeout`);