← back to Designer Wallcoverings

DW-Agents/dw-agents/MEMORY-SYSTEM-USAGE.md

191 lines

# Agent Memory System - Usage Guide

## Overview

Every agent can now have **persistent memory** that survives restarts. You can add notes, reminders, preferences, and learnings through chat.

## How to Use (As User)

### Adding Memories via Chat

Just type in the agent's chat:

```
Remember: Always check inventory on Mondays
```

```
Note: Customer prefers email over phone
```

```
Preference: Use purple for all primary buttons
```

```
Learning: Image uploads work best under 5MB
```

### Viewing Memories

Type:
```
Show my memories
```

Or:
```
What do you remember?
```

### Searching Memories

```
Search memories for "button"
```

### Deleting Memories

```
Forget about inventory checks
```

## Automatic Features

- ✅ Memories auto-load when agent starts
- ✅ Memories auto-save to disk immediately
- ✅ AI includes memories in every response
- ✅ Memories persist across restarts

## Memory Types

1. **note** - General information
2. **reminder** - Things to do regularly
3. **preference** - How things should be done
4. **learning** - Lessons from experience
5. **conversation** - Important chat history

## File Storage

Each agent stores memories in:
```
/root/DW-Agents/agent-[name]/memory.json
```

Example for marketing agent:
```
/root/DW-Agents/agent-marketing/memory.json
```

## Integration in Agents

To add to an existing agent:

### 1. Import the system

```typescript
import { AgentMemory } from '../shared-memory-system';
```

### 2. Initialize on startup

```typescript
const memory = new AgentMemory('marketing'); // Use agent name
```

### 3. Add to chat endpoint

```typescript
app.post('/api/chat', async (req, res) => {
  const { message } = req.body;

  // Auto-detect memory commands
  if (message.toLowerCase().match(/^(remember|note|preference|learning):/i)) {
    const type = message.split(':')[0].toLowerCase() as any;
    const content = message.split(':')[1].trim();
    memory.add(content, type === 'remember' ? 'note' : type);
    return res.json({
      response: `✅ Memory saved: "${content}"`,
      success: true
    });
  }

  if (message.toLowerCase().includes('show') && message.toLowerCase().includes('memor')) {
    const summary = memory.getContextSummary();
    return res.json({ response: summary, success: true });
  }

  if (message.toLowerCase().includes('search memor')) {
    const query = message.split('for')[1]?.trim() || '';
    const results = memory.search(query);
    return res.json({
      response: results.length > 0
        ? results.map(m => `- ${m.content}`).join('\\n')
        : 'No matching memories found.',
      success: true
    });
  }

  // Include memories in AI context
  const systemPrompt = `You are the Marketing Agent.

${memory.getContextSummary()}

Use this knowledge to provide better responses.`;

  // ... rest of AI call with systemPrompt
});
```

### 4. Display memories in UI (optional)

```typescript
app.get('/api/memories', (req, res) => {
  res.json({
    memories: memory.getAll(),
    recent: memory.getRecent(10),
    byType: {
      notes: memory.getByType('note'),
      reminders: memory.getByType('reminder'),
      preferences: memory.getByType('preference'),
      learnings: memory.getByType('learning')
    }
  });
});
```

## Benefits

### For You
- **Tell agents once**, they remember forever
- **No configuration files** to edit manually
- **Natural language** - just chat normally
- **Search & retrieve** past information easily

### For Agents
- **Context-aware** responses using past learnings
- **Consistent behavior** based on preferences
- **Track important** customer/business info
- **Improve over time** by learning from interactions

## Example Workflow

```
You: Remember: We prefer shipments on Tuesdays
Agent: ✅ Memory saved: "We prefer shipments on Tuesdays"

[Agent restarts]

You: When should I schedule the next shipment?
Agent: Based on your preference for Tuesday shipments,
       I recommend scheduling for next Tuesday, November 12th.
```

## Next Steps

Would you like me to:
1. **Add memory system to all agents** automatically?
2. **Add memory viewer UI** to the dashboard?
3. **Add voice commands** for memories?
4. **Export/import memories** for backup?