koishi-plugin-chat-patch 2.1.2 → 2.3.0
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/client/icons/activity.vue +9 -9
- package/client/icons/index.ts +4 -4
- package/client/index.scss +90 -90
- package/client/index.ts +25 -25
- package/client/vue/chat-logic.ts +683 -678
- package/client/vue/composables/useChatActions.ts +1 -1
- package/client/vue/composables/useChatData.ts +1 -1
- package/client/vue/composables/useImageCache.ts +1 -1
- package/client/vue/composables/useVideoCache.ts +130 -0
- package/client/vue/index.vue +658 -612
- package/client/vue/types.ts +1 -1
- package/dist/index.js +2 -2
- package/lib/api-handlers.d.ts +1 -0
- package/lib/file-manager.d.ts +25 -0
- package/lib/index.js +362 -200
- package/lib/message-handler.d.ts +7 -5
- package/lib/utils.d.ts +13 -0
- package/package.json +52 -52
- package/readme.md +25 -25
- package/src/api-handlers.ts +704 -704
- package/src/config.ts +45 -46
- package/src/file-manager.ts +215 -169
- package/src/index.ts +175 -125
- package/src/message-handler.ts +440 -407
- package/src/types.ts +62 -62
- package/src/utils.ts +151 -144
package/src/api-handlers.ts
CHANGED
|
@@ -1,704 +1,704 @@
|
|
|
1
|
-
import { FileManager } from './file-manager'
|
|
2
|
-
import { MessageHandler } from './message-handler'
|
|
3
|
-
import { Context, h, Logger, Universal } from 'koishi'
|
|
4
|
-
import { Config } from './config'
|
|
5
|
-
import { } from '@koishijs/plugin-console'
|
|
6
|
-
import { URL, pathToFileURL, fileURLToPath } from 'node:url'
|
|
7
|
-
import { readFileSync } from 'node:fs'
|
|
8
|
-
import * as mime from 'mime-types'
|
|
9
|
-
|
|
10
|
-
export class ApiHandlers {
|
|
11
|
-
private logger: Logger
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
private
|
|
17
|
-
private
|
|
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
|
-
this.ctx.console.addListener('get-history-messages' as any, async (requestData: {
|
|
61
|
-
selfId: string
|
|
62
|
-
channelId: string
|
|
63
|
-
limit?: number
|
|
64
|
-
offset?: number
|
|
65
|
-
}) => {
|
|
66
|
-
try {
|
|
67
|
-
const data = this.fileManager.readChatDataFromFile()
|
|
68
|
-
const channelKey = `${requestData.selfId}:${requestData.channelId}`
|
|
69
|
-
let messages = data.messages[channelKey] || []
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
messages =
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
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
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
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
|
-
|
|
306
|
-
this.
|
|
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
|
-
deletedMessages
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
this.logInfo(
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
deletedMessages
|
|
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
|
-
this.
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
this.
|
|
404
|
-
return { success:
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const
|
|
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
|
-
} catch (error: any) {
|
|
468
|
-
this.logger.error('
|
|
469
|
-
return { success: false, error: error?.message || String(error) }
|
|
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
|
-
|
|
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
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
const
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
const
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
private async cleanupMediaCache() {
|
|
673
|
-
const baseDir = this.ctx.baseDir + '/data/chat-patch/persist-media'
|
|
674
|
-
if (!require('node:fs').existsSync(baseDir)) return
|
|
675
|
-
|
|
676
|
-
const cleanupDir = (dirName: string, limit: number) => {
|
|
677
|
-
const dirPath = require('node:path').join(baseDir, dirName)
|
|
678
|
-
if (!require('node:fs').existsSync(dirPath)) return
|
|
679
|
-
|
|
680
|
-
const files = require('node:fs').readdirSync(dirPath)
|
|
681
|
-
.map(file => {
|
|
682
|
-
const filePath = require('node:path').join(dirPath, file)
|
|
683
|
-
const stats = require('node:fs').statSync(filePath)
|
|
684
|
-
return { name: file, path: filePath, mtime: stats.mtimeMs }
|
|
685
|
-
})
|
|
686
|
-
.sort((a, b) => b.mtime - a.mtime)
|
|
687
|
-
|
|
688
|
-
if (files.length > limit) {
|
|
689
|
-
files.slice(limit).forEach(f => {
|
|
690
|
-
try { require('node:fs').unlinkSync(f.path) } catch { }
|
|
691
|
-
})
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
cleanupDir('images', 100)
|
|
696
|
-
cleanupDir('media', 20)
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
private logInfo(...args: any[]) {
|
|
700
|
-
if (this.config.loggerinfo) {
|
|
701
|
-
(this.logger.info as (...args: any[]) => void)(...args)
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
}
|
|
1
|
+
import { FileManager } from './file-manager'
|
|
2
|
+
import { MessageHandler } from './message-handler'
|
|
3
|
+
import { Context, h, Logger, Universal } from 'koishi'
|
|
4
|
+
import { Config } from './config'
|
|
5
|
+
import { } from '@koishijs/plugin-console'
|
|
6
|
+
import { URL, pathToFileURL, fileURLToPath } from 'node:url'
|
|
7
|
+
import { readFileSync } from 'node:fs'
|
|
8
|
+
import * as mime from 'mime-types'
|
|
9
|
+
|
|
10
|
+
export class ApiHandlers {
|
|
11
|
+
private logger: Logger
|
|
12
|
+
|
|
13
|
+
private currentTempVideo: string | null = null
|
|
14
|
+
|
|
15
|
+
constructor(
|
|
16
|
+
private ctx: Context,
|
|
17
|
+
private config: Config,
|
|
18
|
+
private fileManager: FileManager,
|
|
19
|
+
private messageHandler: MessageHandler
|
|
20
|
+
) {
|
|
21
|
+
this.logger = ctx.logger('chat-patch')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
registerApiHandlers() {
|
|
25
|
+
this.ctx.console.addListener('clear-all-indexeddb-data' as any, async () => {
|
|
26
|
+
try {
|
|
27
|
+
this.logInfo('收到清空 IndexedDB 数据请求')
|
|
28
|
+
|
|
29
|
+
return { success: true, message: '可以清空 IndexedDB' }
|
|
30
|
+
} catch (error: any) {
|
|
31
|
+
this.logger.error('清空 IndexedDB 数据失败:', error)
|
|
32
|
+
return { success: false, error: error?.message || String(error) }
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
this.ctx.console.addListener('get-chat-data' as any, async () => {
|
|
37
|
+
try {
|
|
38
|
+
|
|
39
|
+
const data = this.fileManager.readChatDataFromFile()
|
|
40
|
+
|
|
41
|
+
this.logInfo('获取基础聊天数据')
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
success: true,
|
|
45
|
+
data: {
|
|
46
|
+
bots: data.bots || {},
|
|
47
|
+
channels: data.channels || {},
|
|
48
|
+
pinnedBots: data.pinnedBots || [],
|
|
49
|
+
pinnedChannels: data.pinnedChannels || [],
|
|
50
|
+
|
|
51
|
+
messages: {}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
} catch (error: any) {
|
|
55
|
+
this.logger.error('获取聊天数据失败:', error)
|
|
56
|
+
return { success: false, error: error?.message || String(error) }
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
this.ctx.console.addListener('get-history-messages' as any, async (requestData: {
|
|
61
|
+
selfId: string
|
|
62
|
+
channelId: string
|
|
63
|
+
limit?: number
|
|
64
|
+
offset?: number
|
|
65
|
+
}) => {
|
|
66
|
+
try {
|
|
67
|
+
const data = this.fileManager.readChatDataFromFile()
|
|
68
|
+
const channelKey = `${requestData.selfId}:${requestData.channelId}`
|
|
69
|
+
let messages = data.messages[channelKey] || []
|
|
70
|
+
|
|
71
|
+
const sortedMessages = messages.sort((a, b) => b.timestamp - a.timestamp)
|
|
72
|
+
|
|
73
|
+
if (requestData.limit !== undefined) {
|
|
74
|
+
const limit = requestData.limit
|
|
75
|
+
const offset = requestData.offset || 0
|
|
76
|
+
messages = sortedMessages.slice(offset, offset + limit)
|
|
77
|
+
|
|
78
|
+
messages = messages.sort((a, b) => a.timestamp - b.timestamp)
|
|
79
|
+
} else {
|
|
80
|
+
|
|
81
|
+
messages = sortedMessages.sort((a, b) => a.timestamp - b.timestamp)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this.logInfo('获取历史消息:', channelKey, '共', messages.length, '条消息')
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
success: true,
|
|
88
|
+
messages: messages,
|
|
89
|
+
total: sortedMessages.length
|
|
90
|
+
}
|
|
91
|
+
} catch (error: any) {
|
|
92
|
+
this.logger.error('获取历史消息失败:', error)
|
|
93
|
+
return { success: false, error: error?.message || String(error), messages: [], total: 0 }
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
this.ctx.console.addListener('get-all-channel-message-counts' as any, async () => {
|
|
98
|
+
try {
|
|
99
|
+
const data = this.fileManager.readChatDataFromFile()
|
|
100
|
+
const counts: Record<string, number> = {}
|
|
101
|
+
|
|
102
|
+
for (const [channelKey, messages] of Object.entries(data.messages)) {
|
|
103
|
+
counts[channelKey] = messages.length
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.logInfo('获取所有频道消息数量:', {
|
|
107
|
+
频道数: Object.keys(counts).length,
|
|
108
|
+
总消息数: Object.values(counts).reduce((total, count) => total + count, 0)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
success: true,
|
|
113
|
+
counts: counts
|
|
114
|
+
}
|
|
115
|
+
} catch (error: any) {
|
|
116
|
+
this.logger.error('获取频道消息数量失败:', error)
|
|
117
|
+
return { success: false, error: error?.message || String(error), counts: {} }
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
this.ctx.console.addListener('fetch-image' as any, async (data: { url: string }) => {
|
|
122
|
+
try {
|
|
123
|
+
|
|
124
|
+
if (this.isFileUrl(data.url)) {
|
|
125
|
+
this.logInfo('处理本地文件请求:', data.url)
|
|
126
|
+
return await this.handleLocalFileRequest(data.url)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const response = await fetch(data.url, {
|
|
130
|
+
headers: {
|
|
131
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
|
132
|
+
'Referer': ''
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
if (!response.ok) {
|
|
137
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const buffer = await response.arrayBuffer()
|
|
141
|
+
const base64 = Buffer.from(buffer).toString('base64')
|
|
142
|
+
const contentType = response.headers.get('content-type') || 'image/jpeg'
|
|
143
|
+
return {
|
|
144
|
+
success: true,
|
|
145
|
+
base64: base64,
|
|
146
|
+
contentType: contentType,
|
|
147
|
+
dataUrl: `data:${contentType};base64,${base64}`
|
|
148
|
+
}
|
|
149
|
+
} catch (error: any) {
|
|
150
|
+
return { success: false, error: error?.message || String(error) }
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
this.ctx.console.addListener('clear-channel-history' as any, async (data: {
|
|
155
|
+
selfId: string
|
|
156
|
+
channelId: string
|
|
157
|
+
keepCount?: number
|
|
158
|
+
}) => {
|
|
159
|
+
try {
|
|
160
|
+
this.logInfo('收到清理历史记录请求:', data)
|
|
161
|
+
|
|
162
|
+
const chatData = this.fileManager.readChatDataFromFile()
|
|
163
|
+
const channelKey = `${data.selfId}:${data.channelId}`
|
|
164
|
+
|
|
165
|
+
if (!chatData.messages[channelKey]) {
|
|
166
|
+
return { success: true, message: '频道没有历史消息' }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const messages = chatData.messages[channelKey]
|
|
170
|
+
const originalCount = messages.length
|
|
171
|
+
|
|
172
|
+
const keepCount = data.keepCount || this.config.keepMessagesOnClear
|
|
173
|
+
|
|
174
|
+
if (keepCount > 0 && originalCount <= keepCount) {
|
|
175
|
+
return { success: true, message: `消息数量(${originalCount})未超过保留数量(${keepCount}),无需清理` }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const sortedMessages = [...messages].sort((a, b) => a.timestamp - b.timestamp)
|
|
179
|
+
const keptMessages = keepCount > 0 ? sortedMessages.slice(-keepCount) : []
|
|
180
|
+
const clearedCount = originalCount - keptMessages.length
|
|
181
|
+
|
|
182
|
+
chatData.messages[channelKey] = keptMessages
|
|
183
|
+
this.fileManager.writeChatDataToFile(chatData)
|
|
184
|
+
|
|
185
|
+
this.logInfo(`频道 ${channelKey} 历史记录清理完成:`, {
|
|
186
|
+
原始消息数: originalCount,
|
|
187
|
+
保留消息数: keptMessages.length,
|
|
188
|
+
清理消息数: clearedCount
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
success: true,
|
|
193
|
+
message: `成功清理 ${clearedCount} 条历史消息,保留最新 ${keptMessages.length} 条`,
|
|
194
|
+
clearedCount: clearedCount,
|
|
195
|
+
keptCount: keptMessages.length
|
|
196
|
+
}
|
|
197
|
+
} catch (error: any) {
|
|
198
|
+
this.logger.error('清理频道历史记录失败:', error)
|
|
199
|
+
return { success: false, error: error?.message || String(error) }
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
this.ctx.console.addListener('send-message' as any, async (data: {
|
|
204
|
+
selfId: string
|
|
205
|
+
channelId: string
|
|
206
|
+
content: string
|
|
207
|
+
images?: Array<{
|
|
208
|
+
tempId: string
|
|
209
|
+
filename: string
|
|
210
|
+
}>
|
|
211
|
+
}) => {
|
|
212
|
+
try {
|
|
213
|
+
this.logInfo('收到发送消息请求:', data)
|
|
214
|
+
|
|
215
|
+
const bot = this.ctx.bots.find((bot: any) => bot.selfId === data.selfId || bot.user?.id === data.selfId)
|
|
216
|
+
if (!bot) {
|
|
217
|
+
this.logger.error('未找到机器人:', data.selfId, '当前可用机器人:', this.ctx.bots.map((b: any) => b.selfId))
|
|
218
|
+
return { success: false, error: `未找到机器人 ${data.selfId},请检查机器人是否在线` }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (bot.status !== Universal.Status.ONLINE) {
|
|
222
|
+
this.logger.error('机器人离线:', data.selfId, '状态:', bot.status)
|
|
223
|
+
return { success: false, error: `机器人 ${data.selfId} 当前离线` }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let messageContent = data.content
|
|
227
|
+
|
|
228
|
+
if (data.images && data.images.length > 0) {
|
|
229
|
+
const tempDir = this.ctx.baseDir + '/data/chat-patch/temp'
|
|
230
|
+
|
|
231
|
+
for (const image of data.images) {
|
|
232
|
+
const files = require('fs').readdirSync(tempDir).filter((file: string) =>
|
|
233
|
+
file.includes(`temp_${image.tempId}`)
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
if (files.length > 0) {
|
|
237
|
+
const imagePath = `${tempDir}/${files[0]}`
|
|
238
|
+
|
|
239
|
+
const fileUrl = this.createFileUrl(imagePath)
|
|
240
|
+
messageContent += h.image(fileUrl).toString()
|
|
241
|
+
this.logInfo('添加图片到消息:', { imagePath, fileUrl })
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const parsedContent = h.parse(messageContent)
|
|
247
|
+
|
|
248
|
+
this.messageHandler.setCorrectChannelId(data.selfId, data.channelId)
|
|
249
|
+
|
|
250
|
+
const result = await bot.sendMessage(data.channelId, parsedContent)
|
|
251
|
+
this.logInfo('消息发送成功:', result)
|
|
252
|
+
|
|
253
|
+
const messageId = Array.isArray(result) ? result[0] : result;
|
|
254
|
+
|
|
255
|
+
if (messageId) {
|
|
256
|
+
const chatData = this.fileManager.readChatDataFromFile()
|
|
257
|
+
const channelKey = `${data.selfId}:${data.channelId}`
|
|
258
|
+
const messages = chatData.messages[channelKey] || []
|
|
259
|
+
|
|
260
|
+
const msg = [...messages].reverse().find(m => m.type === 'bot' && m.sending)
|
|
261
|
+
if (msg) {
|
|
262
|
+
msg.realId = messageId;
|
|
263
|
+
msg.sending = false;
|
|
264
|
+
this.fileManager.writeChatDataToFile(chatData)
|
|
265
|
+
|
|
266
|
+
this.ctx.console.broadcast('bot-message-updated', {
|
|
267
|
+
channelKey,
|
|
268
|
+
tempId: msg.id,
|
|
269
|
+
realId: messageId
|
|
270
|
+
})
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
success: !!messageId,
|
|
276
|
+
messageId: messageId,
|
|
277
|
+
tempImageIds: data.images?.map(img => img.tempId) || []
|
|
278
|
+
}
|
|
279
|
+
} catch (error: any) {
|
|
280
|
+
this.logger.error('发送消息失败:', error)
|
|
281
|
+
return { success: false, error: error?.message || String(error) }
|
|
282
|
+
}
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
this.ctx.console.addListener('cleanup-temp-images' as any, async (data: {
|
|
286
|
+
tempImageIds: string[]
|
|
287
|
+
}) => {
|
|
288
|
+
try {
|
|
289
|
+
this.logInfo('收到清理临时图片请求:', data.tempImageIds)
|
|
290
|
+
|
|
291
|
+
this.logInfo('临时图片将由定时任务清理,保持文件可用性')
|
|
292
|
+
|
|
293
|
+
return { success: true, cleanedCount: 0 }
|
|
294
|
+
} catch (error: any) {
|
|
295
|
+
this.logger.error('清理临时图片失败:', error)
|
|
296
|
+
return { success: false, error: error?.message || String(error) }
|
|
297
|
+
}
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
this.ctx.console.addListener('delete-bot-data' as any, async (data: {
|
|
301
|
+
selfId: string
|
|
302
|
+
}) => {
|
|
303
|
+
try {
|
|
304
|
+
this.logInfo('收到删除机器人数据请求:', data)
|
|
305
|
+
|
|
306
|
+
const chatData = this.fileManager.readChatDataFromFile()
|
|
307
|
+
let deletedChannels = 0
|
|
308
|
+
let deletedMessages = 0
|
|
309
|
+
|
|
310
|
+
if (chatData.channels[data.selfId]) {
|
|
311
|
+
deletedChannels = Object.keys(chatData.channels[data.selfId]).length
|
|
312
|
+
delete chatData.channels[data.selfId]
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const channelsToDelete = Object.keys(chatData.messages).filter(key => key.startsWith(`${data.selfId}:`))
|
|
316
|
+
for (const channelKey of channelsToDelete) {
|
|
317
|
+
deletedMessages += chatData.messages[channelKey].length
|
|
318
|
+
delete chatData.messages[channelKey]
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
delete chatData.bots[data.selfId]
|
|
322
|
+
this.fileManager.writeChatDataToFile(chatData)
|
|
323
|
+
|
|
324
|
+
this.logInfo(`机器人 ${data.selfId} 数据删除完成:`, {
|
|
325
|
+
删除频道数: deletedChannels,
|
|
326
|
+
删除消息数: deletedMessages
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
success: true,
|
|
331
|
+
message: `成功删除机器人数据:${deletedChannels} 个频道,${deletedMessages} 条消息`,
|
|
332
|
+
deletedChannels,
|
|
333
|
+
deletedMessages
|
|
334
|
+
}
|
|
335
|
+
} catch (error: any) {
|
|
336
|
+
this.logger.error('删除机器人数据失败:', error)
|
|
337
|
+
return { success: false, error: error?.message || String(error) }
|
|
338
|
+
}
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
this.ctx.console.addListener('delete-channel-data' as any, async (data: {
|
|
342
|
+
selfId: string
|
|
343
|
+
channelId: string
|
|
344
|
+
}) => {
|
|
345
|
+
try {
|
|
346
|
+
this.logInfo('收到删除频道数据请求:', data)
|
|
347
|
+
|
|
348
|
+
const chatData = this.fileManager.readChatDataFromFile()
|
|
349
|
+
const channelKey = `${data.selfId}:${data.channelId}`
|
|
350
|
+
let deletedMessages = 0
|
|
351
|
+
|
|
352
|
+
if (chatData.messages[channelKey]) {
|
|
353
|
+
deletedMessages = chatData.messages[channelKey].length
|
|
354
|
+
delete chatData.messages[channelKey]
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (chatData.channels[data.selfId] && chatData.channels[data.selfId][data.channelId]) {
|
|
358
|
+
delete chatData.channels[data.selfId][data.channelId]
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
this.fileManager.writeChatDataToFile(chatData)
|
|
362
|
+
|
|
363
|
+
this.logInfo(`频道 ${channelKey} 数据删除完成:`, {
|
|
364
|
+
删除消息数: deletedMessages
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
success: true,
|
|
369
|
+
message: `成功删除频道数据:${deletedMessages} 条消息`,
|
|
370
|
+
deletedMessages
|
|
371
|
+
}
|
|
372
|
+
} catch (error: any) {
|
|
373
|
+
this.logger.error('删除频道数据失败:', error)
|
|
374
|
+
return { success: false, error: error?.message || String(error) }
|
|
375
|
+
}
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
this.ctx.console.addListener('set-pinned-bots' as any, async (data: {
|
|
379
|
+
pinnedBots: string[]
|
|
380
|
+
}) => {
|
|
381
|
+
try {
|
|
382
|
+
this.logInfo('收到设置置顶机器人请求:', data.pinnedBots)
|
|
383
|
+
const chatData = this.fileManager.readChatDataFromFile()
|
|
384
|
+
chatData.pinnedBots = data.pinnedBots
|
|
385
|
+
this.fileManager.writeChatDataToFile(chatData)
|
|
386
|
+
return { success: true }
|
|
387
|
+
} catch (error: any) {
|
|
388
|
+
this.logger.error('设置置顶机器人失败:', error)
|
|
389
|
+
return { success: false, error: error?.message || String(error) }
|
|
390
|
+
}
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
this.ctx.console.addListener('set-pinned-channels' as any, async (data: {
|
|
394
|
+
pinnedChannels: string[]
|
|
395
|
+
}) => {
|
|
396
|
+
try {
|
|
397
|
+
this.logInfo('收到设置置顶频道请求:', data.pinnedChannels)
|
|
398
|
+
const chatData = this.fileManager.readChatDataFromFile()
|
|
399
|
+
chatData.pinnedChannels = data.pinnedChannels
|
|
400
|
+
this.fileManager.writeChatDataToFile(chatData)
|
|
401
|
+
return { success: true }
|
|
402
|
+
} catch (error: any) {
|
|
403
|
+
this.logger.error('设置置顶频道失败:', error)
|
|
404
|
+
return { success: false, error: error?.message || String(error) }
|
|
405
|
+
}
|
|
406
|
+
})
|
|
407
|
+
|
|
408
|
+
this.ctx.console.addListener('upload-image' as any, async (data: {
|
|
409
|
+
file: string
|
|
410
|
+
filename: string
|
|
411
|
+
mimeType: string
|
|
412
|
+
isGif?: boolean
|
|
413
|
+
}) => {
|
|
414
|
+
try {
|
|
415
|
+
this.logInfo('收到图片上传请求:', { filename: data.filename, mimeType: data.mimeType, isGif: data.isGif })
|
|
416
|
+
|
|
417
|
+
const base64Data = data.file.replace(/^data:image\/\w+;base64,/, '')
|
|
418
|
+
const buffer = Buffer.from(base64Data, 'base64')
|
|
419
|
+
|
|
420
|
+
const tempId = Date.now() + '_' + Math.random().toString(36).substring(2, 11)
|
|
421
|
+
const extension = data.filename.split('.').pop()?.toLowerCase() || (data.isGif ? 'gif' : 'jpg')
|
|
422
|
+
const tempFilename = `temp_${tempId}.${extension}`
|
|
423
|
+
|
|
424
|
+
const tempDir = this.ctx.baseDir + '/data/chat-patch/temp'
|
|
425
|
+
if (!require('fs').existsSync(tempDir)) {
|
|
426
|
+
require('fs').mkdirSync(tempDir, { recursive: true })
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const tempPath = `${tempDir}/${tempFilename}`
|
|
430
|
+
|
|
431
|
+
require('fs').writeFileSync(tempPath, buffer)
|
|
432
|
+
|
|
433
|
+
this.logInfo('图片上传成功:', { tempPath, size: buffer.length, isGif: data.isGif })
|
|
434
|
+
|
|
435
|
+
return {
|
|
436
|
+
success: true,
|
|
437
|
+
tempId: tempId,
|
|
438
|
+
tempPath: tempPath,
|
|
439
|
+
filename: data.filename,
|
|
440
|
+
size: buffer.length,
|
|
441
|
+
isGif: data.isGif
|
|
442
|
+
}
|
|
443
|
+
} catch (error: any) {
|
|
444
|
+
this.logger.error('图片上传失败:', error)
|
|
445
|
+
return { success: false, error: error?.message || String(error) }
|
|
446
|
+
}
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
this.ctx.console.addListener('delete-temp-image' as any, async (data: {
|
|
450
|
+
tempId: string
|
|
451
|
+
}) => {
|
|
452
|
+
try {
|
|
453
|
+
const tempDir = this.ctx.baseDir + '/data/chat-patch/temp'
|
|
454
|
+
const files = require('fs').readdirSync(tempDir).filter((file: string) =>
|
|
455
|
+
file.includes(`temp_${data.tempId}`)
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
for (const file of files) {
|
|
459
|
+
const filePath = `${tempDir}/${file}`
|
|
460
|
+
if (require('fs').existsSync(filePath)) {
|
|
461
|
+
require('fs').unlinkSync(filePath)
|
|
462
|
+
this.logInfo('删除临时图片:', filePath)
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
return { success: true }
|
|
467
|
+
} catch (error: any) {
|
|
468
|
+
this.logger.error('删除临时图片失败:', error)
|
|
469
|
+
return { success: false, error: error?.message || String(error) }
|
|
470
|
+
}
|
|
471
|
+
})
|
|
472
|
+
|
|
473
|
+
this.setupTempFileCleanup()
|
|
474
|
+
|
|
475
|
+
this.ctx.console.addListener('get-plugin-config' as any, async () => {
|
|
476
|
+
try {
|
|
477
|
+
return {
|
|
478
|
+
success: true,
|
|
479
|
+
config: {
|
|
480
|
+
maxMessagesPerChannel: this.config.maxMessagesPerChannel,
|
|
481
|
+
keepMessagesOnClear: this.config.keepMessagesOnClear,
|
|
482
|
+
maxPersistImages: this.config.maxPersistImages,
|
|
483
|
+
loggerinfo: this.config.loggerinfo,
|
|
484
|
+
blockedPlatforms: this.config.blockedPlatforms || [],
|
|
485
|
+
clearIndexedDBOnStart: this.config.clearIndexedDBOnStart
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
} catch (error: any) {
|
|
489
|
+
this.logger.error('获取插件配置失败:', error)
|
|
490
|
+
return { success: false, error: error?.message || String(error) }
|
|
491
|
+
}
|
|
492
|
+
})
|
|
493
|
+
|
|
494
|
+
this.ctx.console.addListener('get-user-info' as any, async (data: { selfId: string, userId: string, guildId?: string }) => {
|
|
495
|
+
try {
|
|
496
|
+
const bot = this.ctx.bots.find(b => b.selfId === data.selfId)
|
|
497
|
+
if (!bot) return { success: false, error: '机器人不存在' }
|
|
498
|
+
|
|
499
|
+
if (!bot.getUser || typeof bot.getUser !== 'function') {
|
|
500
|
+
return { success: false, error: '此平台不支持查看用户信息' }
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const user = await bot.getUser(data.userId, data.guildId)
|
|
504
|
+
|
|
505
|
+
if (user) {
|
|
506
|
+
|
|
507
|
+
if (user.avatar) {
|
|
508
|
+
user.avatar = await this.messageHandler.downloadAndCacheMedia(user.avatar, 'avatar')
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const chatData = this.fileManager.readChatDataFromFile()
|
|
512
|
+
let changed = false
|
|
513
|
+
|
|
514
|
+
const botChannels = chatData.channels[data.selfId] || {}
|
|
515
|
+
|
|
516
|
+
const possibleChannelIds = [
|
|
517
|
+
data.userId,
|
|
518
|
+
`private:${data.userId}`,
|
|
519
|
+
`direct:${data.userId}`
|
|
520
|
+
]
|
|
521
|
+
|
|
522
|
+
for (const channelId of possibleChannelIds) {
|
|
523
|
+
const channel = botChannels[channelId]
|
|
524
|
+
if (channel && channel.isDirect) {
|
|
525
|
+
const newName = `私聊(${user.name})`
|
|
526
|
+
if (channel.name !== newName) {
|
|
527
|
+
channel.name = newName
|
|
528
|
+
changed = true
|
|
529
|
+
this.logInfo('更新私聊频道名称:', { channelId, oldName: channel.name, newName })
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
const channelKeyPrefix = `${data.selfId}:`
|
|
535
|
+
for (const [key, messages] of Object.entries(chatData.messages)) {
|
|
536
|
+
if (key.startsWith(channelKeyPrefix)) {
|
|
537
|
+
messages.forEach(msg => {
|
|
538
|
+
if (msg.userId === data.userId) {
|
|
539
|
+
if (user.name && msg.username !== user.name) {
|
|
540
|
+
msg.username = user.name
|
|
541
|
+
changed = true
|
|
542
|
+
}
|
|
543
|
+
if (user.avatar && msg.avatar !== user.avatar) {
|
|
544
|
+
msg.avatar = user.avatar
|
|
545
|
+
changed = true
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if (changed) {
|
|
553
|
+
this.fileManager.writeChatDataToFile(chatData)
|
|
554
|
+
|
|
555
|
+
this.ctx.console.broadcast('chat-data-updated', {})
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
return { success: true, data: user }
|
|
560
|
+
} catch (error: any) {
|
|
561
|
+
return { success: false, error: error?.message || '获取用户信息失败' }
|
|
562
|
+
}
|
|
563
|
+
})
|
|
564
|
+
|
|
565
|
+
this.ctx.console.addListener('debug-get-raw-data' as any, async () => {
|
|
566
|
+
try {
|
|
567
|
+
const data = this.fileManager.readChatDataFromFile()
|
|
568
|
+
return {
|
|
569
|
+
success: true,
|
|
570
|
+
data: data
|
|
571
|
+
}
|
|
572
|
+
} catch (error: any) {
|
|
573
|
+
this.logger.error('获取原始数据失败:', error)
|
|
574
|
+
return { success: false, error: error?.message || String(error) }
|
|
575
|
+
}
|
|
576
|
+
})
|
|
577
|
+
|
|
578
|
+
this.ctx.console.addListener('fetch-video-temp' as any, async (data: { url: string }) => {
|
|
579
|
+
try {
|
|
580
|
+
this.logInfo('收到视频临时加载请求:', data.url)
|
|
581
|
+
|
|
582
|
+
if (this.isFileUrl(data.url)) {
|
|
583
|
+
const result = await this.handleLocalFileRequest(data.url)
|
|
584
|
+
return result
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const response = await fetch(data.url, {
|
|
588
|
+
headers: {
|
|
589
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
|
590
|
+
'Referer': ''
|
|
591
|
+
}
|
|
592
|
+
})
|
|
593
|
+
|
|
594
|
+
if (!response.ok) {
|
|
595
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const buffer = await response.arrayBuffer()
|
|
599
|
+
const base64 = Buffer.from(buffer).toString('base64')
|
|
600
|
+
|
|
601
|
+
const contentType = response.headers.get('content-type') || 'video/mp4'
|
|
602
|
+
|
|
603
|
+
this.logInfo('视频下载成功:', { size: buffer.byteLength, contentType })
|
|
604
|
+
|
|
605
|
+
return {
|
|
606
|
+
success: true,
|
|
607
|
+
base64: base64,
|
|
608
|
+
contentType: contentType,
|
|
609
|
+
dataUrl: `data:${contentType};base64,${base64}`,
|
|
610
|
+
size: buffer.byteLength
|
|
611
|
+
}
|
|
612
|
+
} catch (error: any) {
|
|
613
|
+
this.logger.error('视频临时加载失败:', error)
|
|
614
|
+
return { success: false, error: error?.message || String(error) }
|
|
615
|
+
}
|
|
616
|
+
})
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
private isFileUrl(url: string): boolean {
|
|
620
|
+
try {
|
|
621
|
+
const parsedUrl = new URL(url)
|
|
622
|
+
return parsedUrl.protocol === 'file:'
|
|
623
|
+
} catch {
|
|
624
|
+
return false
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
private createFileUrl(filePath: string): string {
|
|
629
|
+
try {
|
|
630
|
+
return pathToFileURL(filePath).href
|
|
631
|
+
} catch (error) {
|
|
632
|
+
this.logger.error('创建文件URL失败:', { filePath, error })
|
|
633
|
+
|
|
634
|
+
return `file://${filePath}`
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
private async handleLocalFileRequest(fileUrl: string) {
|
|
639
|
+
try {
|
|
640
|
+
|
|
641
|
+
const filePath = fileURLToPath(fileUrl)
|
|
642
|
+
|
|
643
|
+
const buffer = readFileSync(filePath)
|
|
644
|
+
const base64 = buffer.toString('base64')
|
|
645
|
+
|
|
646
|
+
const contentType = mime.lookup(filePath) || 'application/octet-stream'
|
|
647
|
+
|
|
648
|
+
this.logInfo('成功读取本地文件:', { fileUrl, filePath, contentType })
|
|
649
|
+
|
|
650
|
+
return {
|
|
651
|
+
success: true,
|
|
652
|
+
base64: base64,
|
|
653
|
+
contentType: contentType,
|
|
654
|
+
dataUrl: `data:${contentType};base64,${base64}`
|
|
655
|
+
}
|
|
656
|
+
} catch (error: any) {
|
|
657
|
+
this.logger.error('读取本地文件失败:', { fileUrl, error: error.message })
|
|
658
|
+
return {
|
|
659
|
+
success: false,
|
|
660
|
+
error: `读取本地文件失败: ${error.message}`
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
private setupTempFileCleanup() {
|
|
666
|
+
|
|
667
|
+
this.ctx.setInterval(() => {
|
|
668
|
+
this.cleanupMediaCache()
|
|
669
|
+
}, 5 * 60 * 1000)
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
private async cleanupMediaCache() {
|
|
673
|
+
const baseDir = this.ctx.baseDir + '/data/chat-patch/persist-media'
|
|
674
|
+
if (!require('node:fs').existsSync(baseDir)) return
|
|
675
|
+
|
|
676
|
+
const cleanupDir = (dirName: string, limit: number) => {
|
|
677
|
+
const dirPath = require('node:path').join(baseDir, dirName)
|
|
678
|
+
if (!require('node:fs').existsSync(dirPath)) return
|
|
679
|
+
|
|
680
|
+
const files = require('node:fs').readdirSync(dirPath)
|
|
681
|
+
.map(file => {
|
|
682
|
+
const filePath = require('node:path').join(dirPath, file)
|
|
683
|
+
const stats = require('node:fs').statSync(filePath)
|
|
684
|
+
return { name: file, path: filePath, mtime: stats.mtimeMs }
|
|
685
|
+
})
|
|
686
|
+
.sort((a, b) => b.mtime - a.mtime)
|
|
687
|
+
|
|
688
|
+
if (files.length > limit) {
|
|
689
|
+
files.slice(limit).forEach(f => {
|
|
690
|
+
try { require('node:fs').unlinkSync(f.path) } catch { }
|
|
691
|
+
})
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
cleanupDir('images', 100)
|
|
696
|
+
cleanupDir('media', 20)
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
private logInfo(...args: any[]) {
|
|
700
|
+
if (this.config.loggerinfo) {
|
|
701
|
+
(this.logger.info as (...args: any[]) => void)(...args)
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|