update_long_term_memory
Modify existing long-term memory entries by adjusting trigger conditions, updating content, or adding context to improve recall accuracy.
Instructions
Update an existing long-term memory. You can update the trigger condition, prompt content, or add update context.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Name of the memory to update | |
| trigger | No | New trigger condition (JavaScript code) | |
| prompt | No | New memory content | |
| conversation_id | No | Conversation ID that owns this memory | |
| updatedContext | No | Context about this update | |
| recentMessages | No | Optional recent messages to auto-generate updatedContext |
Implementation Reference
- src/tools/long-term-tools.js:83-122 (handler)The main handler function for the 'update_long_term_memory' MCP tool. It handles input arguments, optionally generates updatedContext from recentMessages using createContextSnapshot, prepares the updates object, calls memoryManager.updateMemory, persists changes via storageManager, and returns appropriate success/error responses.handler: async (args) => { try { // Auto-generate context if messages provided let updatedContext = args.updatedContext; if (!updatedContext && args.recentMessages) { updatedContext = createContextSnapshot(args.recentMessages, 4); } const updates = { trigger: args.trigger, prompt: args.prompt, updatedContext }; // Remove undefined values Object.keys(updates).forEach(key => { if (updates[key] === undefined) delete updates[key]; }); const result = await memoryManager.updateMemory(args.name, updates); if (result.success) { await storageManager.saveLongTermMemories(memoryManager.getMemories()); return { success: true, message: `Memory "${args.name}" updated successfully` }; } else { return { success: false, error: result.error }; } } catch (error) { return { success: false, error: error.message }; } }
- src/tools/long-term-tools.js:72-82 (schema)Zod input schema defining parameters for updating a long-term memory: name (required), optional trigger, prompt, conversation_id, updatedContext, and recentMessages for auto-generating context.inputSchema: z.object({ name: z.string().describe('Name of the memory to update'), trigger: z.string().optional().describe('New trigger condition (JavaScript code)'), prompt: z.string().optional().describe('New memory content'), conversation_id: z.string().optional().describe('Conversation ID that owns this memory'), updatedContext: z.string().optional().describe('Context about this update'), recentMessages: z.array(z.object({ role: z.enum(['user', 'assistant', 'system']), content: z.string() })).optional().describe('Optional recent messages to auto-generate updatedContext') }),
- src/tools/long-term-tools.js:69-123 (registration)The complete tool definition object for 'update_long_term_memory' exported via createLongTermTools function, which is registered in src/index.js by calling createLongTermTools and adding to tool registry.{ name: 'update_long_term_memory', description: 'Update an existing long-term memory. You can update the trigger condition, prompt content, or add update context.', inputSchema: z.object({ name: z.string().describe('Name of the memory to update'), trigger: z.string().optional().describe('New trigger condition (JavaScript code)'), prompt: z.string().optional().describe('New memory content'), conversation_id: z.string().optional().describe('Conversation ID that owns this memory'), updatedContext: z.string().optional().describe('Context about this update'), recentMessages: z.array(z.object({ role: z.enum(['user', 'assistant', 'system']), content: z.string() })).optional().describe('Optional recent messages to auto-generate updatedContext') }), handler: async (args) => { try { // Auto-generate context if messages provided let updatedContext = args.updatedContext; if (!updatedContext && args.recentMessages) { updatedContext = createContextSnapshot(args.recentMessages, 4); } const updates = { trigger: args.trigger, prompt: args.prompt, updatedContext }; // Remove undefined values Object.keys(updates).forEach(key => { if (updates[key] === undefined) delete updates[key]; }); const result = await memoryManager.updateMemory(args.name, updates); if (result.success) { await storageManager.saveLongTermMemories(memoryManager.getMemories()); return { success: true, message: `Memory "${args.name}" updated successfully` }; } else { return { success: false, error: result.error }; } } catch (error) { return { success: false, error: error.message }; } } },
- src/memory/long-term.js:205-242 (helper)Core updateMemory method in LongTermMemoryManager class that finds the memory by name, validates new trigger if provided, applies updates to trigger/prompt/updatedContext/modalities, sets updatedAt timestamp, and returns success status. Called by the tool handler.async updateMemory(name, updates) { const memoryIndex = this.memories.findIndex(mem => mem.name === name); if (memoryIndex === -1) { return { success: false, error: `Memory "${name}" not found` }; } // 如果更新触发条件,先验证 if (updates.trigger) { const testResult = await this.testTriggerCondition(updates.trigger); if (!testResult.valid) { return { success: false, error: `Invalid trigger condition: ${testResult.error?.message || 'Unknown error'}` }; } } const memory = this.memories[memoryIndex]; // 应用更新 if (updates.trigger !== undefined) memory.trigger = updates.trigger; if (updates.prompt !== undefined) memory.prompt = updates.prompt; if (updates.updatedContext !== undefined) memory.updatedContext = updates.updatedContext; if (updates.modalities !== undefined || updates.attachments !== undefined) { const modalities = normalizeModalities(updates.modalities ?? updates.attachments ?? []); memory.modalities = modalities; memory.attachments = modalities; } memory.updatedAt = new Date(); return { success: true }; }