-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigManager.js
More file actions
299 lines (259 loc) · 6.57 KB
/
Copy pathConfigManager.js
File metadata and controls
299 lines (259 loc) · 6.57 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
const fs = require('fs');
const path = require('path');
/**
* ConfigManager - In-memory config management with persistence
*
* Solves the performance issue of reading config from disk on every operation.
* Loads config once into memory and only writes to disk when changes occur.
*/
class ConfigManager {
constructor(configFilePath) {
this.configFilePath = configFilePath;
this.config = {};
this.isLoaded = false;
// Load initial config
this.loadConfig();
}
/**
* Load config from disk into memory (called once on startup)
*/
loadConfig() {
try {
if (fs.existsSync(this.configFilePath)) {
const configData = fs.readFileSync(this.configFilePath, 'utf8');
this.config = JSON.parse(configData);
console.log(`[ConfigManager] Config loaded from ${this.configFilePath}`);
} else {
this.config = {};
console.log('[ConfigManager] Config file not found, starting with empty config');
}
this.isLoaded = true;
} catch (error) {
console.error('[ConfigManager] Error loading config:', error);
this.config = {};
this.isLoaded = true;
}
}
/**
* Get entire config object (read from memory)
*/
getConfig() {
if (!this.isLoaded) {
this.loadConfig();
}
return { ...this.config }; // Return copy to prevent direct mutations
}
/**
* Get specific config value (read from memory)
*/
get(key, defaultValue = undefined) {
if (!this.isLoaded) {
this.loadConfig();
}
return this.config[key] !== undefined ? this.config[key] : defaultValue;
}
/**
* Set specific config value (update memory and persist to disk)
*/
set(key, value) {
if (!this.isLoaded) {
this.loadConfig();
}
// Check if value actually changed
if (this.config[key] === value) {
return; // No change, skip disk write
}
this.config[key] = value;
this.persistToDisk();
}
/**
* Update multiple config values at once (batch operation)
*/
update(updates) {
if (!this.isLoaded) {
this.loadConfig();
}
let hasChanges = false;
// Check if any values actually changed
for (const [key, value] of Object.entries(updates)) {
if (this.config[key] !== value) {
this.config[key] = value;
hasChanges = true;
}
}
// Only write to disk if there were actual changes
if (hasChanges) {
this.persistToDisk();
}
}
/**
* Delete a config key (update memory and persist to disk)
*/
delete(key) {
if (!this.isLoaded) {
this.loadConfig();
}
if (this.config[key] !== undefined) {
delete this.config[key];
this.persistToDisk();
}
}
/**
* Check if a config key exists (read from memory)
*/
has(key) {
if (!this.isLoaded) {
this.loadConfig();
}
return this.config[key] !== undefined;
}
/**
* Get current thinking mode (frequently accessed)
*/
getThinkingMode() {
return this.get('thinkingMode', null);
}
/**
* Set thinking mode (frequently updated)
*/
setThinkingMode(mode) {
this.set('thinkingMode', mode);
}
/**
* Get current project (frequently accessed)
*/
getCurrentProject() {
return this.get('currentProject', null);
}
/**
* Set current project (frequently updated)
*/
setCurrentProject(project) {
this.set('currentProject', project);
}
/**
* Get QTunnel token (occasionally accessed)
*/
getQTunnelToken() {
return this.get('qTunnelToken', null);
}
/**
* Set QTunnel token
*/
setQTunnelToken(token) {
this.set('qTunnelToken', token);
}
/**
* Get admin user ID
*/
getAdminUserId() {
return this.get('adminUserId', null);
}
/**
* Set admin user ID
*/
setAdminUserId(userId) {
this.set('adminUserId', userId);
}
/**
* Get project sessions (complex nested data)
*/
getProjectSessions() {
return this.get('projectSessions', {});
}
/**
* Update project sessions (helper for complex nested updates)
*/
updateProjectSessions(sessionUpdates) {
const currentSessions = this.getProjectSessions();
const updatedSessions = { ...currentSessions, ...sessionUpdates };
this.set('projectSessions', updatedSessions);
}
/**
* Get ActivityWatch enabled state
*/
getActivityWatchEnabled() {
return this.get('activityWatchEnabled', true);
}
/**
* Set ActivityWatch enabled state
*/
setActivityWatchEnabled(enabled) {
this.set('activityWatchEnabled', enabled);
}
/**
* Get ActivityWatch time multiplier
*/
getActivityWatchTimeMultiplier() {
return this.get('activityWatchTimeMultiplier', 1.0);
}
/**
* Set ActivityWatch time multiplier
*/
setActivityWatchTimeMultiplier(multiplier) {
this.set('activityWatchTimeMultiplier', multiplier);
}
/**
* Get concat always-on mode state
*/
getConcatAlwaysOn() {
return this.get('concatAlwaysOn', false);
}
/**
* Set concat always-on mode state
*/
setConcatAlwaysOn(enabled) {
this.set('concatAlwaysOn', enabled);
}
/**
* Get always new session mode state
*/
getAlwaysNewSession() {
return this.get('alwaysNewSession', false);
}
/**
* Set always new session mode state
*/
setAlwaysNewSession(enabled) {
this.set('alwaysNewSession', enabled);
}
/**
* Persist current in-memory config to disk
* Only called when config actually changes
*/
persistToDisk() {
try {
// Ensure directory exists
const configDir = path.dirname(this.configFilePath);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
// Write config to disk with pretty formatting
const configData = JSON.stringify(this.config, null, 2);
fs.writeFileSync(this.configFilePath, configData, 'utf8');
console.log(`[ConfigManager] Config persisted to ${this.configFilePath}`);
} catch (error) {
console.error('[ConfigManager] Error persisting config:', error);
}
}
/**
* Force reload config from disk (useful for external changes)
*/
reloadFromDisk() {
console.log('[ConfigManager] Reloading config from disk...');
this.loadConfig();
}
/**
* Get config file path
*/
getConfigFilePath() {
return this.configFilePath;
}
/**
* Check if config is loaded
*/
isConfigLoaded() {
return this.isLoaded;
}
}
module.exports = ConfigManager;