forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongTermMemory.js
More file actions
150 lines (142 loc) 路 3.77 KB
/
Copy pathlongTermMemory.js
File metadata and controls
150 lines (142 loc) 路 3.77 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
/**
* Long-Term Memory System for AI Agents
*
* Uses PostgreSQL for persistent storage
* Supports: plants, pets, transactions, verifications
*/
class LongTermMemory {
constructor(options = {}) {
this.db = options.db || null;
this.namespace = options.namespace || 'myzubster';
this.collections = {
plants: 'plant_history',
pets: 'pet_history',
transactions: 'tx_history',
verifications: 'verification_history',
userPreferences: 'user_preferences'
};
this.cache = new Map();
this.cacheTTL = options.cacheTTL || 300000;
}
async store(type, data) {
const collection = this.collections[type] || 'default';
if (this.db) {
try {
const result = await this.db.collection(collection).insertOne({
...data,
type,
storedAt: new Date()
});
this.cache.set(`${type}:${data.id || data.timestamp}`, {
data,
storedAt: new Date()
});
return result;
} catch (error) {
console.error('Memory store failed:', error);
throw error;
}
}
const key = `${type}:${data.id || data.timestamp}`;
this.cache.set(key, {
data,
storedAt: new Date()
});
return { inserted: true, key };
}
async retrieve(type, id, options = {}) {
const collection = this.collections[type] || 'default';
const cacheKey = `${type}:${id}`;
if (this.cache.has(cacheKey)) {
const cached = this.cache.get(cacheKey);
if (Date.now() - cached.storedAt.getTime() < this.cacheTTL) {
return cached.data;
}
this.cache.delete(cacheKey);
}
if (this.db) {
try {
const result = await this.db.collection(collection).findOne({
$or: [
{ id: id },
{ plantId: id },
{ petId: id },
{ txId: id },
{ itemId: id }
],
...options.query || {}
});
if (result) {
this.cache.set(cacheKey, {
data: result,
storedAt: new Date()
});
return result;
}
return null;
} catch (error) {
console.error('Memory retrieve failed:', error);
throw error;
}
}
return this.cache.get(cacheKey)?.data || null;
}
async query(type, filter = {}, options = {}) {
const collection = this.collections[type] || 'default';
if (this.db) {
try {
const results = await this.db.collection(collection)
.find(filter)
.sort(options.sort || { storedAt: -1 })
.limit(options.limit || 100)
.toArray();
return results;
} catch (error) {
console.error('Memory query failed:', error);
throw error;
}
}
const results = [];
for (const [key, value] of this.cache) {
if (key.startsWith(`${type}:`)) {
results.push(value.data);
}
}
return results;
}
async delete(type, id) {
const collection = this.collections[type] || 'default';
const cacheKey = `${type}:${id}`;
this.cache.delete(cacheKey);
if (this.db) {
try {
const result = await this.db.collection(collection).deleteOne({
$or: [
{ id: id },
{ plantId: id },
{ petId: id },
{ txId: id },
{ itemId: id }
]
});
return result;
} catch (error) {
console.error('Memory delete failed:', error);
throw error;
}
}
return { deleted: true };
}
async clearCache() {
this.cache.clear();
return { cleared: true };
}
getStats() {
return {
cacheSize: this.cache.size,
collections: Object.keys(this.collections),
namespace: this.namespace
};
}
}
module.exports = LongTermMemory;