-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoryStore.js
More file actions
305 lines (266 loc) · 7.9 KB
/
Copy pathmemoryStore.js
File metadata and controls
305 lines (266 loc) · 7.9 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
// memoryStore.js
const { pool } = require('./db');
/** @typedef {import('mysql2/promise').RowDataPacket} RowDataPacket */
/** @typedef {import('mysql2/promise').ResultSetHeader} ResultSetHeader */
/**
* Ensure a channel row exists.
* @param {string} channelId
*/
async function ensureChannel(channelId) {
await pool.execute(
`INSERT INTO channels (id)
VALUES (?)
ON DUPLICATE KEY UPDATE id = id`,
[channelId]
);
}
/**
* Append a message to memory (user or assistant)
* @param {string} channelId
* @param {{ role: 'user' | 'assistant', userId?: string | null, content: string, timestamp?: string | Date }} messageData
* @param {string} [guildId]
*/
async function addMessage(channelId, { role, userId, content, timestamp }, guildId) {
await ensureChannel(channelId);
const createdAt = timestamp ? new Date(timestamp) : new Date();
await pool.execute(
`INSERT INTO messages (channel_id, user_id, role, content, created_at, guild_id)
VALUES (?, ?, ?, ?, ?, ?)`,
[channelId, userId || null, role, content, createdAt, guildId]
);
}
/**
* Get stored memory for a channel:
* - summary (from channels.summary)
* - last N messages (ordered oldest → newest)
* @param {string} channelId
* @param {number} [limit=50]
* @returns {Promise<{ summary: string, messages: Array<any> }>}
*/
async function getChannelMemory(channelId, limit = 50) {
await ensureChannel(channelId);
const [channelRows] = await pool.execute(
`SELECT summary
FROM channels
WHERE id = ?`,
[channelId]
);
// Cast to RowDataPacket[] to fix TS errors
const rows = /** @type {RowDataPacket[]} */ (channelRows);
const summary = rows[0]?.summary || '';
const [messageRows] = await pool.execute(
`SELECT role,
user_id AS userId,
content,
created_at AS timestamp
FROM messages
WHERE channel_id = ?
ORDER BY created_at DESC
LIMIT ?`,
[channelId, limit]
);
// Cast and reverse
const msgs = /** @type {RowDataPacket[]} */ (messageRows);
const messages = msgs.reverse();
return { summary, messages };
}
/**
* Ensure a guild row exists.
* @param {string} guildId
*/
async function ensureGuild(guildId) {
await pool.execute(
`INSERT INTO guilds (id)
VALUES (?)
ON DUPLICATE KEY UPDATE id = id`,
[guildId]
);
}
/**
* Get stored memory for a guild:
* - summary
* - users (JSON string)
* @param {string} guildId
* @returns {Promise<{ summary: string, users: string }>}
*/
async function getGuildMemory(guildId) {
await ensureGuild(guildId);
const [rows] = await pool.execute(
`SELECT summary, users_json AS users
FROM guilds
WHERE id = ?`,
[guildId]
);
const r = /** @type {RowDataPacket[]} */ (rows);
const row = r[0] || {};
return { summary: row.summary || '', users: row.users || '[]' };
}
/**
* Update or set the summary and users JSON for a guild.
* @param {string} guildId
* @param {string} summary
* @param {string} usersJson
*/
async function updateGuildMemory(guildId, summary, usersJson) {
await ensureGuild(guildId);
await pool.execute(
`UPDATE guilds
SET summary = ?,
users_json = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?`,
[summary, usersJson, guildId]
);
}
/**
* Set a note for a user in the guild's users JSON.
* Creates or updates a user entry with a `note` field.
* @param {string} guildId
* @param {string} userId
* @param {string} note
*/
async function setUserNote(guildId, userId, note) {
const guild = await getGuildMemory(guildId);
/** @type {Array<{id: string, note?: string}>} */
const users = JSON.parse(guild.users || '[]');
let user = users.find(u => String(u.id) === String(userId));
if (!user) {
user = { id: String(userId) };
users.push(user);
}
user.note = note;
await updateGuildMemory(guildId, guild.summary || '', JSON.stringify(users));
}
/**
* Get the note for a user in a guild, or null if none.
* @param {string} guildId
* @param {string} userId
* @returns {Promise<string|null>}
*/
async function getUserNote(guildId, userId) {
const guild = await getGuildMemory(guildId);
/** @type {Array<{id: string, note?: string}>} */
const users = JSON.parse(guild.users || '[]');
const user = users.find(u => String(u.id) === String(userId));
return user?.note ?? null;
}
/**
* Update or set the summary for a channel.
* @param {string} channelId
* @param {string} summary
*/
async function updateChannelSummary(channelId, summary) {
await ensureChannel(channelId);
await pool.execute(
`UPDATE channels
SET summary = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?`,
[summary, channelId]
);
}
/**
* Keep only the latest N messages in a channel, delete older ones.
* @param {string} channelId
* @param {number} [keepLatest=50]
*/
async function truncateOldMessages(channelId, keepLatest = 50) {
// MySQL: delete any rows not in the set of latest `keepLatest` ids
await pool.execute(
`DELETE FROM messages
WHERE channel_id = ?
AND id NOT IN (
SELECT id FROM (
SELECT id
FROM messages
WHERE channel_id = ?
ORDER BY created_at DESC
LIMIT ?
) AS t
)`,
[channelId, channelId, keepLatest]
);
}
/**
* Get guild-wide context for AI (cross-channel awareness)
* Returns summaries from top N active channels + trending topics
*
* @param {string} guildId - Discord guild ID
* @param {{ maxChannels?: number, timeWindowHours?: number }} [options] - Configuration options
* @returns {Promise<{ summaries: Array<any>, topics: Array<string>, contextString: string }>} - { summaries, topics, contextString }
*/
async function getGuildWideContext(guildId, options = {}) {
const { maxChannels = 5, timeWindowHours = 24 } = options;
const conn = await pool.getConnection();
try {
// Get top N active channels by message count in the last 24h
const [activeChannels] = await conn.query(`
SELECT
m.channel_id,
(SELECT channel_name FROM messages WHERE channel_id = m.channel_id ORDER BY created_at DESC LIMIT 1) AS name,
c.summary,
COUNT(*) as msg_count
FROM messages m
LEFT JOIN channels c ON m.channel_id = c.id
WHERE m.guild_id = ?
AND m.created_at > NOW() - INTERVAL ? HOUR
GROUP BY m.channel_id, c.summary
ORDER BY msg_count DESC
LIMIT ?
`, [guildId, timeWindowHours, maxChannels]);
// Cast result to RowDataPacket[]
const channels = /** @type {RowDataPacket[]} */ (activeChannels);
if (channels.length === 0) {
return {
summaries: [],
topics: [],
contextString: 'No recent activity in this server.'
};
}
// Build summaries array
const summaries = channels.map(ch => ({
channelId: ch.channel_id,
name: ch.name || 'unknown',
summary: ch.summary || 'No summary yet',
messageCount: ch.msg_count
}));
// Get trending topics from emerging_topics
const [topicsRows] = await conn.query(`
SELECT topic, score
FROM emerging_topics
WHERE guild_id = ?
ORDER BY score DESC
LIMIT 5
`, [guildId]);
const tRows = /** @type {RowDataPacket[]} */ (topicsRows);
const topics = tRows.map(t => t.topic);
// Build context string for AI consumption
const channelSummaries = summaries
.map(s => `#${s.name}: ${s.summary}`)
.join('\n');
const topicsStr = topics.length > 0
? `Trending: ${topics.join(', ')}`
: '';
const contextString = [channelSummaries, topicsStr]
.filter(Boolean)
.join('\n\n');
return {
summaries,
topics,
contextString
};
} finally {
conn.release();
}
}
module.exports = {
addMessage,
getChannelMemory,
updateChannelSummary,
truncateOldMessages,
getGuildMemory,
updateGuildMemory,
setUserNote,
getUserNote,
getGuildWideContext,
};