Skip to content

Memory System

Harish Dhanraj Sugandhi edited this page Mar 4, 2026 · 1 revision

Memory System

The memory system provides persistent, typed storage for the AI agent to remember user preferences, constraints, facts, and workflows across conversations.

Architecture

Component File Purpose
Memory_Service inc/Memory/Memory_Service.php Business logic and context retrieval
Memory_Repository inc/Memory/Memory_Repository.php Database CRUD
Memory_Guard inc/Memory/Memory_Guard.php Input validation and limits
Memory_Actions inc/Actions/Memory_Actions.php Action handlers

Memory Types

Type Purpose Example
preference User preferences and settings "Always use British English"
constraint Rules and limitations "Never publish without review"
fact Site-specific knowledge "The blog focuses on AI and tech"
workflow Process definitions "New posts should be drafted, then reviewed, then published"

Storage

Memories are stored in the openwp_memory table with a unique constraint on (memory_type, memory_key):

CREATE TABLE openwp_memory (
    id bigint(20) unsigned AUTO_INCREMENT,
    memory_type varchar(40) NOT NULL,
    memory_key varchar(191) NOT NULL,
    memory_value longtext NOT NULL,
    updated_at datetime NOT NULL,
    UNIQUE KEY memory_unique (memory_type, memory_key)
);

Agent Actions

Action Parameters Description
remember_memory type, key, value, tags[] Store a memory (insert or update)
forget_memory type, key Delete a specific memory
list_memory type?, query?, limit? List/search memories
clear_memory (none) Delete all memories

Memory Policy

The system prompt instructs the LLM to:

  • Only remember when the user explicitly asks to save/remember something
  • Only forget when the user explicitly asks to forget/remove something
  • Only clear when the user explicitly asks to clear/wipe/reset all memory

This prevents the agent from storing memories autonomously.

Context Retrieval

The Memory_Service provides context-aware memory retrieval for the system prompt:

$service = new Memory_Service();
$items = $service->relevant_for_prompt($user_prompt, $limit = 8, $max_chars = 1200);

This retrieves up to 8 memories relevant to the current prompt, limited to 1200 characters total. The retrieved memories are injected into the system prompt as MEMORY_CONTEXT.

Memory Guard

The Memory_Guard (inc/Memory/Memory_Guard.php) validates memory operations:

  • Type must be one of: preference, constraint, fact, workflow
  • Key length validation
  • Value length validation
  • Prevents duplicate keys within the same type

API Endpoints

Method Endpoint Description
GET /openwp/v1/memory List memories with optional type/query filters
POST /openwp/v1/memory Create or update a memory
DELETE /openwp/v1/memory/{id} Delete a specific memory
DELETE /openwp/v1/memory Clear all memories

Frontend

The Memory screen (src/admin/screens/memory/index.jsx) provides:

  • List view of all memories grouped by type
  • Create/edit memory dialog
  • Delete individual memories
  • Clear all memories
  • Search across memory values

Clone this wiki locally