kiku-agent 1.0.0 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +293 -122
- package/package.json +60 -61
- package/server/agent/agentCore.js +892 -782
- package/server/agent/appPaths.js +21 -0
- package/server/agent/audioPlayer.js +739 -673
- package/server/agent/cli.js +1414 -1111
- package/server/agent/commandRegistry.js +74 -58
- package/server/agent/commands.js +1694 -788
- package/server/agent/config.js +264 -90
- package/server/agent/contextCompactor.js +79 -0
- package/server/agent/doctor.js +69 -0
- package/server/agent/eventBus.js +107 -103
- package/server/agent/gpuMetrics.js +189 -0
- package/server/agent/llmClient.js +369 -200
- package/server/agent/mcpClient.js +364 -0
- package/server/agent/memory.js +670 -566
- package/server/agent/modelManager.js +77 -0
- package/server/agent/research.js +202 -0
- package/server/agent/session.js +244 -239
- package/server/agent/subagent.js +116 -0
- package/server/agent/theme.js +125 -124
- package/server/agent/toolRouter.js +201 -0
- package/server/agent/tools.js +1679 -1528
- package/server/agent/ui.js +760 -469
- package/server/agent/youtubeAuth.js +144 -65
- package/server/mcp/claude-desktop-config.example.json +1 -1
- package/server/mcp/data/discography.js +36 -36
- package/server/mcp/dataAdapter.js +102 -0
- package/server/mcp/examples/claude-code.mcp.example.json +19 -0
- package/server/mcp/examples/claude-desktop.example.json +11 -0
- package/server/mcp/examples/cline.example.json +13 -0
- package/server/mcp/examples/cursor.example.json +14 -0
- package/server/mcp/examples/gemini-cli.example.json +17 -0
- package/server/mcp/examples/opencode.example.json +21 -0
- package/server/mcp/examples/vscode.example.json +19 -0
- package/server/mcp/examples/windsurf.example.json +14 -0
- package/server/mcp/handler.js +85 -159
- package/server/mcp/security.js +140 -138
- package/server/mcp/server.js +129 -100
- package/server/mcp/stdio.js +4 -2
package/server/agent/memory.js
CHANGED
|
@@ -1,566 +1,670 @@
|
|
|
1
|
-
import { promises as fs } from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.
|
|
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
|
-
this.
|
|
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
|
-
const
|
|
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
|
-
const
|
|
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
|
-
const
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
.
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
async
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
}
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { EXPORT_DIR, KIKU_DATA_DIR, SESSION_DIR } from './appPaths.js';
|
|
4
|
+
|
|
5
|
+
const MEMORY_DIR = KIKU_DATA_DIR;
|
|
6
|
+
const MEMORY_FILE = path.join(MEMORY_DIR, 'long_term_memory.json');
|
|
7
|
+
const SESSIONS_DIR = SESSION_DIR;
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export class AgentMemory {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.facts = [];
|
|
13
|
+
this.preferences = {
|
|
14
|
+
language: null,
|
|
15
|
+
topicsOfInterest: [],
|
|
16
|
+
responseStyle: 'default',
|
|
17
|
+
userSkills: [],
|
|
18
|
+
skillsEnabled: true,
|
|
19
|
+
};
|
|
20
|
+
this.sessions = [];
|
|
21
|
+
this.summaries = [];
|
|
22
|
+
this.stats = {
|
|
23
|
+
totalFacts: 0,
|
|
24
|
+
totalSessions: 0,
|
|
25
|
+
totalConsolidations: 0,
|
|
26
|
+
lastConsolidationAt: null,
|
|
27
|
+
createdAt: new Date()
|
|
28
|
+
};
|
|
29
|
+
this._loaded = false;
|
|
30
|
+
this._storageWarningShown = false;
|
|
31
|
+
this._storageError = null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
async init() {
|
|
36
|
+
try {
|
|
37
|
+
await fs.mkdir(MEMORY_DIR, { recursive: true, mode: 0o700 });
|
|
38
|
+
await fs.mkdir(SESSIONS_DIR, { recursive: true, mode: 0o700 });
|
|
39
|
+
this._storageError = null;
|
|
40
|
+
return true;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
this._storageError = error;
|
|
43
|
+
this._warnStorage(error);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
_warnStorage(error) {
|
|
49
|
+
if (this._storageWarningShown) return;
|
|
50
|
+
this._storageWarningShown = true;
|
|
51
|
+
console.error(`Kiku memory is running without persistence (${error?.code || 'unknown'}) in ${MEMORY_DIR}. Set KIKU_DATA_DIR to a writable user folder; Administrator privileges are not required.`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
async load() {
|
|
56
|
+
try {
|
|
57
|
+
await this.init();
|
|
58
|
+
const data = await fs.readFile(MEMORY_FILE, 'utf-8');
|
|
59
|
+
const parsed = JSON.parse(data);
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
this.facts = (parsed.facts || []).map(f => ({
|
|
63
|
+
...f,
|
|
64
|
+
createdAt: f.createdAt ? new Date(f.createdAt) : new Date(),
|
|
65
|
+
lastAccessedAt: f.lastAccessedAt ? new Date(f.lastAccessedAt) : new Date()
|
|
66
|
+
}));
|
|
67
|
+
|
|
68
|
+
const loadedPrefs = parsed.preferences || {};
|
|
69
|
+
this.preferences = {
|
|
70
|
+
...this.preferences,
|
|
71
|
+
...loadedPrefs,
|
|
72
|
+
userSkills: Array.isArray(loadedPrefs.userSkills) ? loadedPrefs.userSkills : [],
|
|
73
|
+
topicsOfInterest: (loadedPrefs.topicsOfInterest || []).map(t => ({
|
|
74
|
+
...t,
|
|
75
|
+
lastSeen: t.lastSeen ? new Date(t.lastSeen) : new Date()
|
|
76
|
+
}))
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
this.sessions = parsed.sessions || [];
|
|
80
|
+
this.summaries = parsed.summaries || [];
|
|
81
|
+
this.stats = { ...this.stats, ...(parsed.stats || {}) };
|
|
82
|
+
this._loaded = true;
|
|
83
|
+
return true;
|
|
84
|
+
} catch {
|
|
85
|
+
this._loaded = true;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
async save() {
|
|
92
|
+
try {
|
|
93
|
+
if (!await this.init()) return false;
|
|
94
|
+
|
|
95
|
+
const serializablePrefs = {
|
|
96
|
+
...this.preferences,
|
|
97
|
+
topicsOfInterest: (this.preferences.topicsOfInterest || []).map(t => ({
|
|
98
|
+
...t,
|
|
99
|
+
lastSeen: t.lastSeen ? t.lastSeen.toISOString() : null
|
|
100
|
+
}))
|
|
101
|
+
};
|
|
102
|
+
const data = {
|
|
103
|
+
facts: this.facts.map(f => ({
|
|
104
|
+
...f,
|
|
105
|
+
createdAt: f.createdAt ? f.createdAt.toISOString() : f.createdAt,
|
|
106
|
+
lastAccessedAt: f.lastAccessedAt ? f.lastAccessedAt.toISOString() : f.lastAccessedAt
|
|
107
|
+
})),
|
|
108
|
+
preferences: serializablePrefs,
|
|
109
|
+
sessions: this.sessions,
|
|
110
|
+
summaries: this.summaries,
|
|
111
|
+
stats: this.stats,
|
|
112
|
+
lastSavedAt: new Date().toISOString()
|
|
113
|
+
};
|
|
114
|
+
await fs.writeFile(MEMORY_FILE, JSON.stringify(data, null, 2), 'utf-8');
|
|
115
|
+
return true;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
this._warnStorage(error);
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
addFact(fact, meta = {}) {
|
|
123
|
+
const entry = {
|
|
124
|
+
id: `fact_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`,
|
|
125
|
+
content: fact,
|
|
126
|
+
category: meta.category || 'general',
|
|
127
|
+
source: meta.source || 'conversation',
|
|
128
|
+
confidence: meta.confidence || 'high',
|
|
129
|
+
createdAt: new Date(),
|
|
130
|
+
lastAccessedAt: new Date(),
|
|
131
|
+
accessCount: 0,
|
|
132
|
+
tags: meta.tags || []
|
|
133
|
+
};
|
|
134
|
+
this.facts.push(entry);
|
|
135
|
+
this.stats.totalFacts = this.facts.length;
|
|
136
|
+
this.save().catch(() => {});
|
|
137
|
+
return entry;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
scoreFactRelevance(fact, queryTerms = [], now = Date.now()) {
|
|
142
|
+
if (!fact) return 0;
|
|
143
|
+
const content = (fact.content || '').toLowerCase();
|
|
144
|
+
const tags = (fact.tags || []).map(t => String(t).toLowerCase());
|
|
145
|
+
|
|
146
|
+
let matchScore = 0;
|
|
147
|
+
if (queryTerms.length > 0) {
|
|
148
|
+
for (const term of queryTerms) {
|
|
149
|
+
if (!term || term.length < 2) continue;
|
|
150
|
+
if (tags.some(t => t.includes(term))) matchScore += 3.0;
|
|
151
|
+
if (content.includes(term)) matchScore += 2.0;
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
matchScore = 1.0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const createdTime = fact.createdAt ? new Date(fact.createdAt).getTime() : now;
|
|
158
|
+
const ageInDays = Math.max(0, (now - createdTime) / (1000 * 60 * 60 * 24));
|
|
159
|
+
const recencyDecay = Math.exp(-ageInDays / 45);
|
|
160
|
+
|
|
161
|
+
const accessBoost = Math.log10(1 + (fact.accessCount || 0) * 2);
|
|
162
|
+
|
|
163
|
+
return (matchScore * (0.6 + 0.4 * recencyDecay)) + accessBoost;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
getRelevantFacts(query = '', limit = 8) {
|
|
167
|
+
if (!this.facts || this.facts.length === 0) return [];
|
|
168
|
+
const q = String(query || '').toLowerCase().trim();
|
|
169
|
+
const terms = q ? q.split(/\s+/).filter(t => t.length >= 2) : [];
|
|
170
|
+
const now = Date.now();
|
|
171
|
+
|
|
172
|
+
const scored = this.facts.map(fact => ({
|
|
173
|
+
fact,
|
|
174
|
+
score: this.scoreFactRelevance(fact, terms, now)
|
|
175
|
+
}));
|
|
176
|
+
|
|
177
|
+
scored.sort((a, b) => b.score - a.score);
|
|
178
|
+
const top = scored.slice(0, limit).map(s => s.fact);
|
|
179
|
+
|
|
180
|
+
for (const f of top) {
|
|
181
|
+
f.lastAccessedAt = new Date();
|
|
182
|
+
f.accessCount = (f.accessCount || 0) + 1;
|
|
183
|
+
}
|
|
184
|
+
return top;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
searchFacts(query, options = {}) {
|
|
188
|
+
const { category, limit = 10 } = options;
|
|
189
|
+
const q = (query || '').toLowerCase().trim();
|
|
190
|
+
|
|
191
|
+
let results = this.facts;
|
|
192
|
+
|
|
193
|
+
if (category) {
|
|
194
|
+
results = results.filter(f => f.category === category);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (q) {
|
|
198
|
+
results = results.filter(f =>
|
|
199
|
+
f.content.toLowerCase().includes(q) ||
|
|
200
|
+
f.tags.some(t => t.toLowerCase().includes(q))
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const terms = q ? q.split(/\s+/).filter(t => t.length >= 2) : [];
|
|
205
|
+
const now = Date.now();
|
|
206
|
+
|
|
207
|
+
results.sort((a, b) => {
|
|
208
|
+
const scoreA = this.scoreFactRelevance(a, terms, now);
|
|
209
|
+
const scoreB = this.scoreFactRelevance(b, terms, now);
|
|
210
|
+
return scoreB - scoreA;
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
for (const fact of results.slice(0, limit)) {
|
|
214
|
+
fact.lastAccessedAt = new Date();
|
|
215
|
+
fact.accessCount = (fact.accessCount || 0) + 1;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return results.slice(0, limit);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
removeFact(factId) {
|
|
222
|
+
const idx = this.facts.findIndex(f => f.id === factId);
|
|
223
|
+
if (idx === -1) return false;
|
|
224
|
+
this.facts.splice(idx, 1);
|
|
225
|
+
this.stats.totalFacts = this.facts.length;
|
|
226
|
+
this.save().catch(() => {});
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
getFactsByCategory() {
|
|
232
|
+
const grouped = {};
|
|
233
|
+
for (const fact of this.facts) {
|
|
234
|
+
const cat = fact.category || 'general';
|
|
235
|
+
if (!grouped[cat]) grouped[cat] = [];
|
|
236
|
+
grouped[cat].push(fact);
|
|
237
|
+
}
|
|
238
|
+
return grouped;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
setPreference(key, value) {
|
|
242
|
+
this.preferences[key] = value;
|
|
243
|
+
this.save().catch(() => {});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
addUserSkill(instruction) {
|
|
247
|
+
const value = String(instruction || '').replace(/\s+/g, ' ').trim();
|
|
248
|
+
if (!value) return null;
|
|
249
|
+
if (value.length > 500) throw new Error('Skill instruction must be 500 characters or fewer.');
|
|
250
|
+
const duplicate = this.preferences.userSkills.find(skill => skill.instruction.toLowerCase() === value.toLowerCase());
|
|
251
|
+
if (duplicate) return duplicate;
|
|
252
|
+
const skill = { id: `skill_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`, instruction: value };
|
|
253
|
+
this.preferences.userSkills.push(skill);
|
|
254
|
+
this.preferences.userSkills = this.preferences.userSkills.slice(-20);
|
|
255
|
+
this.save().catch(() => {});
|
|
256
|
+
return skill;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
removeUserSkill(id) {
|
|
260
|
+
const before = this.preferences.userSkills.length;
|
|
261
|
+
this.preferences.userSkills = this.preferences.userSkills.filter(skill => skill.id !== id);
|
|
262
|
+
if (this.preferences.userSkills.length === before) return false;
|
|
263
|
+
this.save().catch(() => {});
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
clearUserSkills() {
|
|
268
|
+
this.preferences.userSkills = [];
|
|
269
|
+
this.save().catch(() => {});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
buildUserSkillContext() {
|
|
273
|
+
if (!this.preferences.skillsEnabled || this.preferences.userSkills.length === 0) return '';
|
|
274
|
+
return this.preferences.userSkills.map((skill, index) => `${index + 1}. ${skill.instruction}`).join('\n');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
async savePreferences() {
|
|
279
|
+
await this.save();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
trackTopicInterest(topic) {
|
|
284
|
+
const existing = this.preferences.topicsOfInterest.find(
|
|
285
|
+
t => t.topic.toLowerCase() === topic.toLowerCase()
|
|
286
|
+
);
|
|
287
|
+
if (existing) {
|
|
288
|
+
existing.count++;
|
|
289
|
+
existing.lastSeen = new Date();
|
|
290
|
+
} else {
|
|
291
|
+
this.preferences.topicsOfInterest.push({
|
|
292
|
+
topic,
|
|
293
|
+
count: 1,
|
|
294
|
+
lastSeen: new Date()
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
this.preferences.topicsOfInterest.sort((a, b) => b.count - a.count);
|
|
299
|
+
this.preferences.topicsOfInterest = this.preferences.topicsOfInterest.slice(0, 20);
|
|
300
|
+
this.save().catch(() => {});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
getTopTopics(limit = 5) {
|
|
305
|
+
return this.preferences.topicsOfInterest.slice(0, limit);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
async archiveSession(sessionData) {
|
|
310
|
+
const archive = {
|
|
311
|
+
id: sessionData.id,
|
|
312
|
+
createdAt: sessionData.createdAt,
|
|
313
|
+
endedAt: new Date(),
|
|
314
|
+
messageCount: sessionData.messages?.length || 0,
|
|
315
|
+
stats: { ...sessionData.stats },
|
|
316
|
+
thinkingMode: sessionData.thinkingMode,
|
|
317
|
+
summary: null
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
let archivedToDisk = false;
|
|
322
|
+
try {
|
|
323
|
+
if (!await this.init()) return null;
|
|
324
|
+
const sessionFile = path.join(SESSIONS_DIR, `${sessionData.id}.json`);
|
|
325
|
+
await fs.writeFile(sessionFile, JSON.stringify(sessionData, null, 2), 'utf-8');
|
|
326
|
+
archivedToDisk = true;
|
|
327
|
+
} catch (err) {
|
|
328
|
+
this._warnStorage(err);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (!archivedToDisk) return null;
|
|
332
|
+
|
|
333
|
+
this.sessions.push(archive);
|
|
334
|
+
this.stats.totalSessions = this.sessions.length;
|
|
335
|
+
await this.save();
|
|
336
|
+
return archive;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
getArchivedSessions(limit = 20) {
|
|
340
|
+
return this.sessions
|
|
341
|
+
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
|
|
342
|
+
.slice(0, limit);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async loadArchivedSession(sessionId) {
|
|
346
|
+
try {
|
|
347
|
+
const sessionFile = path.join(SESSIONS_DIR, `${sessionId}.json`);
|
|
348
|
+
const data = await fs.readFile(sessionFile, 'utf-8');
|
|
349
|
+
return JSON.parse(data);
|
|
350
|
+
} catch {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
async consolidateSession(sessionData) {
|
|
357
|
+
if (!sessionData?.messages?.length) return null;
|
|
358
|
+
|
|
359
|
+
const userMessages = sessionData.messages
|
|
360
|
+
.filter(m => m.role === 'user')
|
|
361
|
+
.map(m => {
|
|
362
|
+
const textPart = m.parts?.find(p => p.type === 'text');
|
|
363
|
+
return textPart?.content || '';
|
|
364
|
+
})
|
|
365
|
+
.filter(Boolean);
|
|
366
|
+
|
|
367
|
+
const assistantMessages = sessionData.messages
|
|
368
|
+
.filter(m => m.role === 'assistant')
|
|
369
|
+
.map(m => {
|
|
370
|
+
const textPart = m.parts?.find(p => p.type === 'text');
|
|
371
|
+
return textPart?.content || '';
|
|
372
|
+
})
|
|
373
|
+
.filter(Boolean);
|
|
374
|
+
|
|
375
|
+
const topics = this._extractTopics(userMessages);
|
|
376
|
+
|
|
377
|
+
for (const topic of topics) {
|
|
378
|
+
this.trackTopicInterest(topic);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const summary = {
|
|
382
|
+
id: `sum_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`,
|
|
383
|
+
sessionId: sessionData.id,
|
|
384
|
+
createdAt: new Date(),
|
|
385
|
+
topics,
|
|
386
|
+
questionCount: userMessages.length,
|
|
387
|
+
keyExchanges: userMessages.slice(0, 5).map((q, i) => ({
|
|
388
|
+
question: q.slice(0, 200),
|
|
389
|
+
answerPreview: (assistantMessages[i] || '').slice(0, 200)
|
|
390
|
+
})),
|
|
391
|
+
durationMs: sessionData.stats?.totalDurationMs || 0,
|
|
392
|
+
toolCalls: sessionData.stats?.totalToolCalls || 0
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
this.summaries.push(summary);
|
|
396
|
+
this.stats.totalConsolidations = this.summaries.length;
|
|
397
|
+
this.stats.lastConsolidationAt = new Date();
|
|
398
|
+
|
|
399
|
+
const archived = this.sessions.find(s => s.id === sessionData.id);
|
|
400
|
+
if (archived) {
|
|
401
|
+
archived.summary = summary;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
await this.save();
|
|
405
|
+
return summary;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
_extractTopics(messages) {
|
|
409
|
+
const topicKeywords = {
|
|
410
|
+
'drama': ['drama', 'series', 'ละคร', '剧', '电视', 'tv series', 'film', 'movie', 'work'],
|
|
411
|
+
'music': ['song', 'music', 'mv', 'single', 'ost', 'เพลง', '歌', 'album', 'discography'],
|
|
412
|
+
'brand': ['brand', 'ambassador', 'endorsement', 'แบรนด์', '品牌', 'commercial'],
|
|
413
|
+
'awards': ['award', 'honor', 'ceremony', 'รางวัล', '奖', 'winner'],
|
|
414
|
+
'profile': ['profile', 'bio', 'birthday', 'age', 'ประวัติ', '资料', '个人信息'],
|
|
415
|
+
'timeline': ['career', 'timeline', 'history', 'era', 'เส้นทาง', '经历', 'ปี'],
|
|
416
|
+
'news': ['news', 'latest', 'recent', 'update', 'ข่าว', '最新', 'ล่าสุด'],
|
|
417
|
+
'video': ['video', 'stage', 'performance', 'live', 'คลิป', '视频', '舞台'],
|
|
418
|
+
'social': ['weibo', 'douyin', 'instagram', 'social media', 'โซเชียล', '微博'],
|
|
419
|
+
'gallery': ['photo', 'picture', 'gallery', 'fashion', 'photoshoot', 'ภาพ', '照片']
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
const topicCounts = {};
|
|
423
|
+
const allText = messages.join(' ').toLowerCase();
|
|
424
|
+
|
|
425
|
+
for (const [topic, keywords] of Object.entries(topicKeywords)) {
|
|
426
|
+
const count = keywords.reduce((acc, kw) => {
|
|
427
|
+
const regex = new RegExp(`\\b${kw}\\b`, 'gi');
|
|
428
|
+
return acc + (allText.match(regex) || []).length;
|
|
429
|
+
}, 0);
|
|
430
|
+
if (count > 0) topicCounts[topic] = count;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
return Object.entries(topicCounts)
|
|
434
|
+
.sort(([, a], [, b]) => b - a)
|
|
435
|
+
.slice(0, 5)
|
|
436
|
+
.map(([topic]) => topic);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
buildMemoryContext(currentQuery = '') {
|
|
441
|
+
const parts = [];
|
|
442
|
+
|
|
443
|
+
if (this.preferences.language) {
|
|
444
|
+
parts.push(`User's preferred language: ${this.preferences.language}`);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const userSkillContext = this.buildUserSkillContext();
|
|
448
|
+
if (userSkillContext) {
|
|
449
|
+
parts.push(`User-taught response skills (follow these preferences unless they conflict with safety or verified facts):\n${userSkillContext}`);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const topTopics = this.getTopTopics(5);
|
|
453
|
+
if (topTopics.length > 0) {
|
|
454
|
+
const topicList = topTopics.map(t => `${t.topic} (${t.count}x)`).join(', ');
|
|
455
|
+
parts.push(`User frequently asks about: ${topicList}`);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const relevantFacts = currentQuery ? this.getRelevantFacts(currentQuery, 8) : this.facts.slice(-8);
|
|
459
|
+
if (relevantFacts.length > 0) {
|
|
460
|
+
const factList = relevantFacts.map(f => `- ${f.content}`).join('\n');
|
|
461
|
+
parts.push(`Known facts from previous conversations:\n${factList}`);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const recentSummaries = this.summaries
|
|
465
|
+
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
|
|
466
|
+
.slice(0, 3);
|
|
467
|
+
if (recentSummaries.length > 0) {
|
|
468
|
+
const sumList = recentSummaries.map(s => {
|
|
469
|
+
const date = new Date(s.createdAt).toLocaleDateString();
|
|
470
|
+
const topics = s.topics.join(', ');
|
|
471
|
+
return `- [${date}] Topics: ${topics} (${s.questionCount} questions)`;
|
|
472
|
+
}).join('\n');
|
|
473
|
+
parts.push(`Recent conversation themes:\n${sumList}`);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (parts.length === 0) return '';
|
|
477
|
+
|
|
478
|
+
return `\n\n=== LONG-TERM MEMORY CONTEXT ===\n${parts.join('\n\n')}\n=== END MEMORY CONTEXT ===`;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
getStatus() {
|
|
482
|
+
return {
|
|
483
|
+
factsCount: this.facts.length,
|
|
484
|
+
factsByCategory: this.getFactsByCategory(),
|
|
485
|
+
preferences: this.preferences,
|
|
486
|
+
sessionsCount: this.sessions.length,
|
|
487
|
+
summariesCount: this.summaries.length,
|
|
488
|
+
topTopics: this.getTopTopics(10),
|
|
489
|
+
stats: this.stats,
|
|
490
|
+
lastConsolidationAt: this.stats.lastConsolidationAt,
|
|
491
|
+
memoryFileSize: null
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async importFromJSON(filePath) {
|
|
496
|
+
try {
|
|
497
|
+
const raw = await fs.readFile(filePath, 'utf-8');
|
|
498
|
+
const data = JSON.parse(raw);
|
|
499
|
+
|
|
500
|
+
if (!data.facts && !data.preferences && !data.sessions) {
|
|
501
|
+
return { success: false, message: 'Invalid memory file format — missing facts, preferences, or sessions.' };
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
let factsImported = 0;
|
|
505
|
+
let factsSkipped = 0;
|
|
506
|
+
let topicsImported = 0;
|
|
507
|
+
|
|
508
|
+
if (Array.isArray(data.facts)) {
|
|
509
|
+
for (const fact of data.facts) {
|
|
510
|
+
const isDuplicate = this.facts.some(
|
|
511
|
+
existing => existing.content === fact.content
|
|
512
|
+
);
|
|
513
|
+
if (!isDuplicate && fact.content) {
|
|
514
|
+
this.facts.push({
|
|
515
|
+
id: fact.id || `fact_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`,
|
|
516
|
+
content: fact.content,
|
|
517
|
+
category: fact.category || 'imported',
|
|
518
|
+
source: fact.source || 'import',
|
|
519
|
+
confidence: fact.confidence || 'high',
|
|
520
|
+
createdAt: fact.createdAt ? new Date(fact.createdAt) : new Date(),
|
|
521
|
+
lastAccessedAt: new Date(),
|
|
522
|
+
accessCount: 0,
|
|
523
|
+
tags: fact.tags || []
|
|
524
|
+
});
|
|
525
|
+
factsImported++;
|
|
526
|
+
} else {
|
|
527
|
+
factsSkipped++;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (data.preferences) {
|
|
533
|
+
if (data.preferences.language) {
|
|
534
|
+
this.preferences.language = data.preferences.language;
|
|
535
|
+
}
|
|
536
|
+
if (data.preferences.responseStyle) {
|
|
537
|
+
this.preferences.responseStyle = data.preferences.responseStyle;
|
|
538
|
+
}
|
|
539
|
+
if (Array.isArray(data.preferences.topicsOfInterest)) {
|
|
540
|
+
for (const topic of data.preferences.topicsOfInterest) {
|
|
541
|
+
const existing = this.preferences.topicsOfInterest.find(
|
|
542
|
+
t => t.topic.toLowerCase() === topic.topic.toLowerCase()
|
|
543
|
+
);
|
|
544
|
+
if (existing) {
|
|
545
|
+
existing.count += topic.count || 1;
|
|
546
|
+
existing.lastSeen = new Date();
|
|
547
|
+
} else {
|
|
548
|
+
this.preferences.topicsOfInterest.push({
|
|
549
|
+
topic: topic.topic,
|
|
550
|
+
count: topic.count || 1,
|
|
551
|
+
lastSeen: topic.lastSeen ? new Date(topic.lastSeen) : new Date()
|
|
552
|
+
});
|
|
553
|
+
topicsImported++;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
this.preferences.topicsOfInterest.sort((a, b) => b.count - a.count);
|
|
557
|
+
this.preferences.topicsOfInterest = this.preferences.topicsOfInterest.slice(0, 20);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (Array.isArray(data.sessions)) {
|
|
562
|
+
for (const session of data.sessions) {
|
|
563
|
+
const isDuplicate = this.sessions.some(s => s.id === session.id);
|
|
564
|
+
if (!isDuplicate) {
|
|
565
|
+
this.sessions.push(session);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (Array.isArray(data.summaries)) {
|
|
571
|
+
for (const summary of data.summaries) {
|
|
572
|
+
const isDuplicate = this.summaries.some(s => s.id === summary.id);
|
|
573
|
+
if (!isDuplicate) {
|
|
574
|
+
this.summaries.push(summary);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
this.stats.totalFacts = this.facts.length;
|
|
580
|
+
this.stats.totalSessions = this.sessions.length;
|
|
581
|
+
this.stats.totalConsolidations = this.summaries.length;
|
|
582
|
+
|
|
583
|
+
await this.save();
|
|
584
|
+
|
|
585
|
+
return {
|
|
586
|
+
success: true,
|
|
587
|
+
factsImported,
|
|
588
|
+
factsSkipped,
|
|
589
|
+
topicsImported,
|
|
590
|
+
totalFacts: this.facts.length,
|
|
591
|
+
totalSessions: this.sessions.length,
|
|
592
|
+
totalSummaries: this.summaries.length
|
|
593
|
+
};
|
|
594
|
+
} catch (error) {
|
|
595
|
+
return { success: false, message: error.message };
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
async exportToJSON() {
|
|
600
|
+
const exportDir = EXPORT_DIR;
|
|
601
|
+
try {
|
|
602
|
+
await fs.mkdir(exportDir, { recursive: true });
|
|
603
|
+
} catch {}
|
|
604
|
+
|
|
605
|
+
const now = new Date();
|
|
606
|
+
const timestamp = now.toISOString().replace(/[:.]/g, '-').slice(0, 19);
|
|
607
|
+
const filename = `kiku-memory-${timestamp}.json`;
|
|
608
|
+
const filePath = path.join(exportDir, filename);
|
|
609
|
+
|
|
610
|
+
const exportData = {
|
|
611
|
+
exportedAt: now.toISOString(),
|
|
612
|
+
stats: this.stats,
|
|
613
|
+
preferences: {
|
|
614
|
+
...this.preferences,
|
|
615
|
+
topicsOfInterest: (this.preferences.topicsOfInterest || []).map(t => ({
|
|
616
|
+
...t,
|
|
617
|
+
lastSeen: t.lastSeen ? t.lastSeen.toISOString() : null
|
|
618
|
+
}))
|
|
619
|
+
},
|
|
620
|
+
facts: this.facts.map(f => ({
|
|
621
|
+
...f,
|
|
622
|
+
createdAt: f.createdAt ? f.createdAt.toISOString() : f.createdAt,
|
|
623
|
+
lastAccessedAt: f.lastAccessedAt ? f.lastAccessedAt.toISOString() : f.lastAccessedAt
|
|
624
|
+
})),
|
|
625
|
+
factsByCategory: this.getFactsByCategory(),
|
|
626
|
+
sessions: this.sessions,
|
|
627
|
+
summaries: this.summaries
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
await fs.writeFile(filePath, JSON.stringify(exportData, null, 2), 'utf-8');
|
|
631
|
+
return { filePath, filename, size: (await fs.stat(filePath)).size };
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
async clearAll() {
|
|
635
|
+
this.facts = [];
|
|
636
|
+
this.preferences = {
|
|
637
|
+
language: null,
|
|
638
|
+
topicsOfInterest: [],
|
|
639
|
+
responseStyle: 'default'
|
|
640
|
+
};
|
|
641
|
+
this.sessions = [];
|
|
642
|
+
this.summaries = [];
|
|
643
|
+
this.stats = {
|
|
644
|
+
totalFacts: 0,
|
|
645
|
+
totalSessions: 0,
|
|
646
|
+
totalConsolidations: 0,
|
|
647
|
+
lastConsolidationAt: null,
|
|
648
|
+
createdAt: new Date()
|
|
649
|
+
};
|
|
650
|
+
await this.save();
|
|
651
|
+
return true;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
async getStorageSize() {
|
|
655
|
+
try {
|
|
656
|
+
const stat = await fs.stat(MEMORY_FILE);
|
|
657
|
+
return stat.size;
|
|
658
|
+
} catch {
|
|
659
|
+
return 0;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
static formatBytes(bytes) {
|
|
664
|
+
if (bytes === 0) return '0 B';
|
|
665
|
+
const k = 1024;
|
|
666
|
+
const sizes = ['B', 'KB', 'MB', 'GB'];
|
|
667
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
668
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
|
|
669
|
+
}
|
|
670
|
+
}
|