Skip to content

Commit 7885686

Browse files
committed
refactor: remove MemoryEventBus and integrate logging for memory operations + prompt changes
1 parent 4391245 commit 7885686

File tree

6 files changed

+32
-162
lines changed

6 files changed

+32
-162
lines changed

src/lib/agent/BrowserAgent.prompt.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,37 @@ ${toolDescriptions}
5454
## 🔌 MCP SERVER INTEGRATION
5555
You have access to MCP (Model Context Protocol) servers that provide direct API access to external services.
5656
57-
### CRITICAL: Three-Step Process (NEVER SKIP STEPS)
58-
When users ask about emails, videos, documents, calendars, repositories, or other external services:
59-
57+
### CRITICAL: MEMORY FIRST, MCP SECOND
58+
**ALWAYS check memory_tool FIRST when user asks about personal/saved data**
59+
**ONLY use MCP when user explicitly mentions these EXACT services:**
60+
61+
### 🎯 MCP ACTIVATION TRIGGERS (Must Be Explicit):
62+
**Use MCP ONLY when user specifically mentions:**
63+
- **Gmail/Email**: "check my Gmail", "my emails", "inbox", "send email"
64+
- **YouTube**: "my YouTube videos", "YouTube channel", "upload to YouTube"
65+
- **GitHub**: "my GitHub repos", "GitHub repository", "commit to GitHub"
66+
- **Slack**: "Slack messages", "Slack channels", "post to Slack"
67+
- **Google Calendar**: "my calendar", "Google Calendar", "schedule meeting"
68+
- **Google Drive**: "my Google Drive", "Drive files", "upload to Drive"
69+
- **Notion**: "my Notion pages", "Notion workspace", "create Notion page"
70+
- **Linear**: "Linear issues", "Linear tickets", "create Linear issue"
71+
72+
### 🚫 DO NOT USE MCP FOR:
73+
- Generic terms: "books", "documents", "files", "videos", "music"
74+
- "My saved [anything]" → This is ALWAYS memory, never MCP
75+
- "Suggest from my [anything]" → This is ALWAYS memory, never MCP
76+
- "My preferences" → This is ALWAYS memory, never MCP
77+
- User mentions platforms but doesn't want live data (e.g., "like the book I saved from Goodreads")
78+
79+
### CRITICAL: Three-Step Process for MCP Integration (ONLY after explicit service mention)
6080
**🔴 STEP 1: MANDATORY - Check Installed MCP Servers**
6181
- Use: mcp_tool with action: 'getUserInstances'
6282
- Returns: List of installed servers with their instance IDs
63-
- Example response: { instances: [{ id: 'a146178c-e0c8-416c-96cd-6fbe809e0cf8', name: 'Gmail', authenticated: true }] }
6483
- SAVE the instance ID for next steps
6584
6685
**🔴 STEP 2: MANDATORY - Get Available Tools (NEVER SKIP THIS)**
6786
- Use: mcp_tool with action: 'listTools', instanceId: [EXACT ID from step 1]
6887
- Returns: List of available tools for that server
69-
- Example response: { tools: [{ name: 'gmail_search', description: 'Search emails' }, { name: 'gmail_send', description: 'Send email' }] }
7088
- DO NOT GUESS TOOL NAMES - you MUST get them from listTools
7189
7290
**🔴 STEP 3: Call the Tool**

src/lib/memory/MemoryEventBus.ts

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/lib/memory/MemoryManager.ts

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MemoryEntry, MemoryMetadata, MemorySearchParams, MemorySearchResult, MemoryOperationResult, MemoryConfig, MemoryStats, MemoryCategory, TaskContext, AgentMemoryContext } from './types';
22
import { Mem0ClientWrapper } from './Mem0ClientWrapper';
3-
import { MemoryEventBus } from './MemoryEventBus';
43
import { v4 as uuidv4 } from 'uuid';
4+
import { Logging } from '@/lib/utils/Logging'
55

66
/**
77
* MemoryManager - Central orchestrator for the memory system
@@ -11,27 +11,15 @@ import { v4 as uuidv4 } from 'uuid';
1111
*/
1212
export class MemoryManager {
1313
private mem0Client: Mem0ClientWrapper;
14-
private eventBus: MemoryEventBus;
1514
private config: MemoryConfig;
1615
private agentId: string;
1716
private sessionId: string;
1817

1918
constructor(apiKey?: string, config: Partial<MemoryConfig> = {}, agentId: string = 'default') {
2019
this.mem0Client = new Mem0ClientWrapper(apiKey);
21-
this.eventBus = new MemoryEventBus();
2220
this.agentId = agentId;
2321
this.sessionId = uuidv4();
2422

25-
this.eventBus.subscribe((event) => {
26-
console.log('🧠 Memory Event:', {
27-
type: event.type,
28-
agentId: event.data.agentId,
29-
category: event.data.category,
30-
tabId: event.data.tabId,
31-
timestamp: event.data.timestamp.toISOString()
32-
});
33-
});
34-
3523
// Merge with default config
3624
this.config = {
3725
enabled: true,
@@ -89,13 +77,7 @@ export class MemoryManager {
8977
updatedAt: new Date()
9078
};
9179

92-
this.eventBus.emit('memory_added', {
93-
entryId: memoryEntry.id,
94-
category: fullMetadata.category,
95-
agentId: this.agentId,
96-
tabId: fullMetadata.tabId,
97-
timestamp: new Date()
98-
});
80+
Logging.log('MemoryManager', `Memory added successfully: ${memoryEntry.id}`);
9981

10082
return {
10183
success: true,
@@ -131,16 +113,11 @@ export class MemoryManager {
131113

132114
const result = await this.mem0Client.searchMemories(searchParams);
133115

134-
this.eventBus.emit('memory_searched', {
135-
category: searchParams.category,
136-
agentId: this.agentId,
137-
tabId: searchParams.tabId,
138-
timestamp: new Date()
139-
});
116+
Logging.log('MemoryManager', `Memory search completed: ${searchParams.query}`);
140117

141118
return result;
142119
} catch (error) {
143-
console.error('Memory search failed:', error);
120+
Logging.log('MemoryManager', `Memory search failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
144121
return { entries: [], total: 0, hasMore: false };
145122
}
146123
}
@@ -320,18 +297,12 @@ export class MemoryManager {
320297
return this.agentId;
321298
}
322299

323-
/**
324-
* Subscribe to memory events
325-
*/
326-
onMemoryEvent(callback: (event: any) => void): void {
327-
this.eventBus.subscribe(callback);
328-
}
329300

330301
/**
331302
* Cleanup old memories based on retention policy
332303
*/
333304
async cleanup(): Promise<void> {
334-
if (!this.config.autoCleanup) {
305+
if (!this.config.autoCleanup) {
335306
return;
336307
}
337308

src/lib/memory/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
// Core components
99
export { MemoryManager } from './MemoryManager';
1010
export { Mem0ClientWrapper } from './Mem0ClientWrapper';
11-
export { MemoryEventBus } from './MemoryEventBus';
11+
import { Logging } from '@/lib/utils/Logging'
1212

1313
// Import for factory functions
1414
import { MemoryManager } from './MemoryManager';
1515

1616
// Types and schemas
17-
export type { MemoryEntry, MemoryMetadata, MemorySearchParams, MemorySearchResult, MemoryOperationResult, MemoryConfig, MemoryStats, TaskContext, AgentMemoryContext, MemoryEvent } from './types';
17+
export type { MemoryEntry, MemoryMetadata, MemorySearchParams, MemorySearchResult, MemoryOperationResult, MemoryConfig, MemoryStats, TaskContext, AgentMemoryContext } from './types';
1818

1919
export { MemoryCategory, MemoryEntrySchema, MemoryMetadataSchema, MemorySearchParamsSchema, MemoryStatsSchema, MemoryConfigSchema } from './types';
2020

@@ -61,7 +61,7 @@ export async function initializeMemorySystem(apiKey?: string, agentId?: string):
6161
await memoryManager.initialize();
6262
return memoryManager;
6363
} catch (error) {
64-
console.warn('Failed to initialize memory system:', error);
64+
Logging.log('MemorySystem', `Failed to initialize memory system: ${error instanceof Error ? error.message : 'Unknown error'}`);
6565
return null;
6666
}
6767
}

src/lib/memory/types.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,3 @@ export interface AgentMemoryContext {
132132
}>;
133133
}
134134

135-
// Memory events for coordination
136-
export interface MemoryEvent {
137-
type:
138-
| "memory_added"
139-
| "memory_updated"
140-
| "memory_deleted"
141-
| "memory_searched";
142-
data: {
143-
entryId?: string;
144-
category?: MemoryCategory;
145-
agentId: string;
146-
tabId?: number;
147-
timestamp: Date;
148-
};
149-
}

src/lib/tools/memory/MemoryTool.prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ You have access to a persistent memory system for task continuity and learning a
102102
### 🎯 MEMORY ACTIVATION TRIGGERS:
103103
**AUTOMATIC TRIGGERS** - Use memory tool when you encounter these patterns:
104104
- User says: "save", "store", "remember", "recall", "what did I", "my preferences", "last time", "before", "previously"
105-
- User asks: "continue where I left off", "use my usual settings", "what was that thing I searched for"
105+
- User asks: "continue where I left off", "use my usual settings", "what was that thing I searched for", "show me my saved items", "what you know about me", etc.
106106
- User mentions: "bookmark this", "keep this for later", "I need this information again"
107107
- Tasks involving: user preferences, repeated patterns, multi-step workflows, cross-tab context
108108
- When you find: important information that could be useful later, user-specific details, successful patterns

0 commit comments

Comments
 (0)