-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.js
More file actions
109 lines (91 loc) · 2.82 KB
/
memory.js
File metadata and controls
109 lines (91 loc) · 2.82 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
const Database = require('better-sqlite3');
const path = require('path');
const fs = require('fs');
const DB_DIR = path.join(__dirname, 'db');
fs.mkdirSync(DB_DIR, { recursive: true });
const db = new Database(path.join(DB_DIR, 'clud.db'));
// WAL mode for better concurrent reads
db.pragma('journal_mode = WAL');
db.exec(`
CREATE TABLE IF NOT EXISTS users (
user_id TEXT PRIMARY KEY,
username TEXT,
display_name TEXT,
first_seen TEXT DEFAULT (datetime('now')),
last_seen TEXT DEFAULT (datetime('now')),
interaction_count INTEGER DEFAULT 0,
topics TEXT DEFAULT '[]',
mood TEXT DEFAULT 'neutral',
notes TEXT DEFAULT ''
);
CREATE TABLE IF NOT EXISTS interactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT,
tweet_id TEXT UNIQUE,
user_text TEXT,
clud_reply TEXT,
created_at TEXT DEFAULT (datetime('now')),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
CREATE TABLE IF NOT EXISTS thoughts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
thought TEXT,
source TEXT,
created_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS stats (
key TEXT PRIMARY KEY,
value TEXT
);
`);
// Prepared statements
const upsertUser = db.prepare(`
INSERT INTO users (user_id, username, display_name, last_seen, interaction_count)
VALUES (?, ?, ?, datetime('now'), 1)
ON CONFLICT(user_id) DO UPDATE SET
username = excluded.username,
display_name = excluded.display_name,
last_seen = datetime('now'),
interaction_count = interaction_count + 1
`);
const getUser = db.prepare('SELECT * FROM users WHERE user_id = ?');
const getUserByName = db.prepare('SELECT * FROM users WHERE username = ? COLLATE NOCASE');
const addInteraction = db.prepare(`
INSERT OR IGNORE INTO interactions (user_id, tweet_id, user_text, clud_reply)
VALUES (?, ?, ?, ?)
`);
const getRecentInteractions = db.prepare(`
SELECT * FROM interactions WHERE user_id = ?
ORDER BY created_at DESC LIMIT ?
`);
const addThought = db.prepare(`
INSERT INTO thoughts (thought, source) VALUES (?, ?)
`);
const getRecentThoughts = db.prepare(`
SELECT * FROM thoughts ORDER BY created_at DESC LIMIT ?
`);
const getTotalUsers = db.prepare('SELECT COUNT(*) as count FROM users');
const getTotalInteractions = db.prepare('SELECT COUNT(*) as count FROM interactions');
const updateUserTopics = db.prepare(`
UPDATE users SET topics = ? WHERE user_id = ?
`);
const setStat = db.prepare(`
INSERT INTO stats (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value
`);
const getStat = db.prepare('SELECT value FROM stats WHERE key = ?');
module.exports = {
db,
upsertUser,
getUser,
getUserByName,
addInteraction,
getRecentInteractions,
addThought,
getRecentThoughts,
getTotalUsers,
getTotalInteractions,
updateUserTopics,
setStat,
getStat,
};