-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryClient.js
More file actions
363 lines (326 loc) · 12 KB
/
Copy pathMemoryClient.js
File metadata and controls
363 lines (326 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Memory Service Client
* Client for interacting with the Silverpeak Memory Service (RAG system)
*/
const fetch = require('node-fetch');
class MemoryClient {
constructor(serviceUrl = 'http://localhost:5003', campaign = 'test-silverpeak', maxEpisodeAge = 20) {
this.serviceUrl = serviceUrl;
this.campaign = campaign;
this.maxEpisodeAge = maxEpisodeAge;
this.enabled = true; // Can be disabled if service is down
this.actionBuffer = []; // Buffer to collect actions before storing
this.actionBufferSize = 4; // Store memory every 4 actions
this.turnCounter = 0;
this.currentSceneId = null; // For anti-timewarp scene filtering
}
/**
* Set current scene ID for anti-timewarp filtering
* @param {number} sceneId - Current scene ID
*/
setCurrentSceneId(sceneId) {
this.currentSceneId = sceneId;
}
/**
* Check if memory service is available
*/
async checkHealth() {
try {
const response = await fetch(`${this.serviceUrl}/health`, {
timeout: 2000
});
if (response.ok) {
console.log('✓ Memory service is healthy');
this.enabled = true;
return true;
}
} catch (error) {
console.warn('⚠️ Memory service unavailable:', error.message);
this.enabled = false;
}
return false;
}
/**
* Add an action to the buffer and store if buffer is full
*
* @param {string} role - 'player' or 'assistant'
* @param {string} content - The action/response content
*/
async addAction(role, content) {
if (!this.enabled) {
return;
}
this.turnCounter++;
this.actionBuffer.push({
role: role,
content: content,
turn: this.turnCounter
});
console.log(`📝 Action added to memory buffer (${this.actionBuffer.length}/${this.actionBufferSize})`);
// If buffer is full, store memory
if (this.actionBuffer.length >= this.actionBufferSize) {
await this.flushBuffer();
}
}
/**
* Store accumulated actions as a memory
*/
async flushBuffer() {
if (!this.enabled || this.actionBuffer.length === 0) {
return;
}
try {
const response = await fetch(`${this.serviceUrl}/store-memory`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
actions: this.actionBuffer,
campaign: this.campaign,
session: 1, // TODO: Track session number
scene_id: this.currentSceneId,
memory_type: 'episode' // Time-bound narrative events
}),
timeout: 10000
});
if (response.ok) {
const data = await response.json();
console.log(`💾 Memory stored: ${data.memory.id} - "${data.memory.summary.substring(0, 60)}..."`);
console.log(` Entities: ${data.memory.entities.join(', ')}`);
// Clear buffer
this.actionBuffer = [];
} else {
const errorText = await response.text();
console.error('❌ Failed to store memory:', response.status, errorText);
}
} catch (error) {
console.error('❌ Error storing memory:', error.message);
// Keep actions in buffer for retry
}
}
/**
* Retrieve relevant memories for current context
*
* @param {string} query - Current player action or context
* @param {number} nResults - Number of memories to retrieve (default: 5)
* @returns {Promise<Array>} Array of relevant memories
*/
async retrieveMemories(query, nResults = 5) {
if (!this.enabled) {
return [];
}
try {
const response = await fetch(`${this.serviceUrl}/retrieve-memories`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: query,
campaign: this.campaign,
n_results: nResults,
current_scene_id: this.currentSceneId,
exclude_recent_scenes: 5, // Don't retrieve memories from last 5 scenes
max_episode_age: this.maxEpisodeAge
}),
timeout: 10000
});
if (response.ok) {
const data = await response.json();
console.log(`🔍 Retrieved ${data.count} relevant memories`);
return data.memories || [];
} else {
console.error('❌ Failed to retrieve memories:', response.status);
return [];
}
} catch (error) {
console.error('❌ Error retrieving memories:', error.message);
return [];
}
}
/**
* Format memories for inclusion in Claude context
*
* @param {Array} memories - Array of memory objects
* @returns {string} Formatted memory text for context
*/
formatMemoriesForContext(memories) {
if (!memories || memories.length === 0) {
return '';
}
const memoryTexts = memories.map((mem, idx) => {
const relevance = (1 - (mem.distance || 0)).toFixed(2);
return `[Memory ${idx + 1} - Relevance: ${relevance}]\n${mem.text}`;
});
return `\n\n=== BACKGROUND MEMORIES (Reference ONLY) ===
**CRITICAL: These are PAST events. DO NOT replay or return to these scenes.**
Use ONLY for character knowledge, callbacks, and continuity details.
The CURRENT scene is in RECENT HISTORY above. Stay there.
${memoryTexts.join('\n\n')}
=== END BACKGROUND MEMORIES ===\n\n`;
}
/**
* Get all memories for debugging
*
* @returns {Promise<Array>} All stored memories
*/
async getAllMemories() {
if (!this.enabled) {
return [];
}
try {
const response = await fetch(`${this.serviceUrl}/memories?campaign=${this.campaign}`, {
timeout: 5000
});
if (response.ok) {
const data = await response.json();
return data.memories || [];
}
} catch (error) {
console.error('❌ Error getting all memories:', error.message);
}
return [];
}
/**
* Clear all memories (use with caution!)
*/
async clearMemories() {
if (!this.enabled) {
return false;
}
try {
const response = await fetch(`${this.serviceUrl}/clear-memories`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
campaign: this.campaign
}),
timeout: 5000
});
if (response.ok) {
const data = await response.json();
console.log(`🗑️ Cleared ${data.deleted_count} memories`);
return true;
}
} catch (error) {
console.error('❌ Error clearing memories:', error.message);
}
return false;
}
/**
* Store a timeless world fact (NPC bio, faction info, location details, etc.)
* These are NOT filtered by scene_id and are always retrieved when relevant.
*
* @param {string} factType - Type: 'npc_bio', 'faction', 'location', 'item', 'lore'
* @param {string} subject - Name/identifier of the subject
* @param {string} content - The actual fact content
* @param {Array<string>} tags - Optional tags for filtering
* @returns {Promise<object|null>} Stored fact object or null on error
*/
async storeWorldFact(factType, subject, content, tags = []) {
if (!this.enabled) {
return null;
}
try {
const response = await fetch(`${this.serviceUrl}/store-world-fact`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
fact_type: factType,
subject: subject,
content: content,
campaign: this.campaign,
tags: tags
}),
timeout: 10000
});
if (response.ok) {
const data = await response.json();
console.log(`📚 World fact stored: ${data.fact.id} (${factType}) - ${subject}`);
return data.fact;
} else {
const errorText = await response.text();
console.error('❌ Failed to store world fact:', response.status, errorText);
return null;
}
} catch (error) {
console.error('❌ Error storing world fact:', error.message);
return null;
}
}
/**
* Get all world facts for the campaign
*
* @param {string} factType - Optional filter by fact type
* @returns {Promise<Array>} Array of world facts
*/
async getWorldFacts(factType = null) {
if (!this.enabled) {
return [];
}
try {
let url = `${this.serviceUrl}/world-facts?campaign=${this.campaign}`;
if (factType) {
url += `&type=${factType}`;
}
const response = await fetch(url, { timeout: 5000 });
if (response.ok) {
const data = await response.json();
return data.facts || [];
}
} catch (error) {
console.error('❌ Error getting world facts:', error.message);
}
return [];
}
/**
* Store monster stats from 5e API in campaign RAG for quick access
*
* @param {string} monsterName - Name of the monster
* @param {object} monsterData - Full monster data from 5e API
* @returns {Promise<boolean>} Success status
*/
async storeMonsterStats(monsterName, monsterData) {
if (!this.enabled) {
return false;
}
try {
// Format monster data as a searchable memory entry
const memoryText = `Monster: ${monsterName}
HP: ${monsterData.hit_points || 'Unknown'}
AC: ${monsterData.armor_class ?
(Array.isArray(monsterData.armor_class) ? monsterData.armor_class[0].value : monsterData.armor_class)
: 'Unknown'}
CR: ${monsterData.challenge_rating || 'Unknown'}
Size: ${monsterData.size || 'Unknown'}
Type: ${monsterData.type || 'Unknown'}
Actions: ${monsterData.actions ? monsterData.actions.map(a => a.name).join(', ') : 'None'}`;
const response = await fetch(`${this.serviceUrl}/store-memory`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
actions: [{
role: 'system',
content: memoryText,
turn: -1 // System memory, not tied to turn
}],
campaign: this.campaign,
session: 0, // System data
metadata: {
type: 'monster_stats',
monster_name: monsterName,
source: '5e_api'
}
}),
timeout: 10000
});
if (response.ok) {
return true;
} else {
console.warn(`⚠️ Failed to store monster stats: ${response.status}`);
return false;
}
} catch (error) {
console.error('❌ Error storing monster stats:', error.message);
return false;
}
}
}
module.exports = MemoryClient;