File size: 38,134 Bytes
e78cf04
 
9e087a4
e78cf04
 
c270684
e78cf04
 
68042f2
9600ada
 
 
 
 
 
e78cf04
 
 
74d8451
e78cf04
 
 
9600ada
 
 
 
 
 
 
74d8451
9600ada
74d8451
191c490
6712395
9600ada
 
 
 
 
4ca3b83
e78cf04
 
68042f2
191c490
 
 
 
6712395
 
 
68042f2
 
4ca3b83
6712395
 
 
68042f2
 
6712395
 
191c490
9600ada
68042f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191c490
 
 
68042f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e78cf04
 
 
68042f2
191c490
68042f2
e78cf04
 
191c490
9600ada
 
 
 
 
e78cf04
68042f2
 
e78cf04
68042f2
 
 
e78cf04
 
 
 
 
 
 
 
68042f2
e78cf04
9600ada
74d8451
 
9e087a4
74d8451
9600ada
e78cf04
68042f2
e78cf04
9600ada
9e087a4
9600ada
 
e78cf04
 
 
68042f2
e78cf04
 
9600ada
 
4ca3b83
9600ada
 
 
e78cf04
 
68042f2
 
 
 
 
 
 
 
e78cf04
68042f2
 
 
 
 
 
9600ada
68042f2
9e087a4
 
68042f2
9e087a4
 
 
68042f2
74d8451
9600ada
68042f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e78cf04
68042f2
 
9600ada
68042f2
 
 
 
 
 
9600ada
e78cf04
 
3720b6d
68042f2
 
 
3720b6d
68042f2
 
 
 
 
 
 
3720b6d
 
 
 
 
 
 
 
e78cf04
68042f2
 
 
 
 
74d8451
9600ada
 
68042f2
 
 
 
 
 
 
 
 
 
 
9600ada
 
 
 
e78cf04
 
 
68042f2
 
 
9600ada
74d8451
9600ada
74d8451
68042f2
9600ada
68042f2
 
 
 
 
 
9600ada
 
 
68042f2
74d8451
9600ada
 
 
 
 
 
74d8451
9600ada
 
 
e78cf04
 
 
e0993dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6712395
9e087a4
68042f2
6712395
e0993dc
6712395
e0993dc
 
68042f2
6712395
68042f2
 
 
 
 
 
e0993dc
6712395
 
e0993dc
6712395
e0993dc
6712395
 
9e087a4
 
 
 
 
6712395
e0993dc
6712395
e0993dc
 
 
 
cc6eee3
 
6712395
9e087a4
 
cc6eee3
9e087a4
cc6eee3
 
e0993dc
 
 
 
 
6712395
68042f2
e0993dc
 
 
6712395
68042f2
6712395
9e087a4
6712395
 
 
 
 
 
 
e0993dc
 
 
 
 
9e087a4
 
4ca3b83
9e087a4
 
e0993dc
 
6712395
e0993dc
 
 
 
 
e78cf04
 
191c490
9600ada
 
74d8451
 
 
 
 
68042f2
74d8451
 
 
 
 
68042f2
74d8451
 
9600ada
 
74d8451
191c490
68042f2
6712395
9e087a4
6712395
68042f2
6712395
 
 
 
9e087a4
6712395
 
 
 
 
4ca3b83
 
6712395
9600ada
 
 
68042f2
e0993dc
68042f2
191c490
68042f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ca3b83
68042f2
 
 
 
 
 
 
 
 
6712395
4ca3b83
6712395
68042f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191c490
 
9600ada
3720b6d
 
 
 
 
 
 
 
 
 
68042f2
3720b6d
68042f2
3720b6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9600ada
74d8451
9600ada
e78cf04
 
9600ada
 
191c490
e0993dc
 
9600ada
e0993dc
9600ada
 
 
 
 
 
191c490
 
74d8451
 
9600ada
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e78cf04
e0993dc
 
9600ada
 
 
 
 
 
 
4ca3b83
74d8451
9600ada
 
 
 
e78cf04
9600ada
e78cf04
9600ada
 
 
 
 
 
 
 
 
4ca3b83
9600ada
e78cf04
 
9600ada
 
 
 
e78cf04
191c490
 
74d8451
e78cf04
74d8451
9e087a4
9600ada
 
74d8451
9600ada
e78cf04
 
 
9600ada
 
 
 
74d8451
 
9600ada
 
68042f2
74d8451
9600ada
e78cf04
 
74d8451
9e087a4
74d8451
e78cf04
4ca3b83
e78cf04
74d8451
68042f2
74d8451
e78cf04
9600ada
 
e78cf04
 
 
9600ada
68042f2
9600ada
 
 
 
 
 
 
e78cf04
 
 
9600ada
 
 
 
 
 
74d8451
9600ada
 
74d8451
9600ada
 
 
 
e78cf04
 
 
 
 
68042f2
e78cf04
 
9600ada
 
 
4ca3b83
9600ada
 
 
74d8451
e78cf04
 
68042f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e78cf04
68042f2
 
4ca3b83
 
 
9600ada
68042f2
9600ada
 
74d8451
9600ada
68042f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74d8451
9600ada
68042f2
9600ada
 
74d8451
68042f2
9600ada
 
74d8451
9600ada
 
 
 
 
 
74d8451
9600ada
68042f2
74d8451
 
 
 
68042f2
9600ada
 
 
 
 
e78cf04
 
 
9600ada
 
 
 
74d8451
e78cf04
 
 
9600ada
4ca3b83
9600ada
 
68042f2
9600ada
68042f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9600ada
68042f2
 
 
 
 
 
 
 
 
 
 
9600ada
 
 
74d8451
4ca3b83
 
 
 
 
 
 
68042f2
 
 
 
 
 
 
4ca3b83
 
 
 
 
68042f2
4ca3b83
68042f2
4ca3b83
68042f2
4ca3b83
 
9600ada
 
 
 
 
 
 
 
e0993dc
9600ada
e0993dc
4ca3b83
9600ada
e78cf04
 
68042f2
e0993dc
4ca3b83
e0993dc
 
 
 
e78cf04
 
 
e0993dc
c83747a
4ca3b83
 
 
 
 
 
 
 
 
 
 
191c490
e78cf04
 
68042f2
 
 
e78cf04
9600ada
 
 
 
 
74d8451
9600ada
 
 
 
 
 
 
 
 
 
 
68042f2
 
 
 
 
9e087a4
9600ada
4ca3b83
9600ada
 
e78cf04
9600ada
4ca3b83
9600ada
4ca3b83
9600ada
9e087a4
 
 
 
4ca3b83
 
9600ada
74d8451
4ca3b83
9600ada
 
 
e78cf04
 
9600ada
 
4ca3b83
e78cf04
4ca3b83
 
 
9600ada
 
191c490
9600ada
 
 
 
 
 
 
 
 
 
68042f2
9600ada
 
 
68042f2
9600ada
 
9e087a4
9600ada
 
 
 
 
 
 
 
 
 
 
e78cf04
 
9600ada
 
 
4ca3b83
9600ada
 
 
 
 
 
9e087a4
 
 
 
 
 
 
 
9600ada
 
9e087a4
e78cf04
9600ada
 
 
 
 
 
 
 
 
 
 
9e087a4
 
4ca3b83
9e087a4
e78cf04
9600ada
 
 
 
 
 
e78cf04
9600ada
e78cf04
9e087a4
9600ada
 
4ca3b83
9600ada
e78cf04
9600ada
 
 
 
 
 
 
e78cf04
 
 
74d8451
9600ada
 
 
4ca3b83
e78cf04
9600ada
 
e78cf04
 
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
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
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
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
642
643
644
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
'use strict';
require('dotenv').config();

const crypto = require('node:crypto');
const express = require('express');
const path = require('path');
const WebSocket = require('ws');
const axios = require('axios');

const {
  S3Client,
  PutObjectCommand,
  GetObjectCommand,
  ListObjectsV2Command,
  DeleteObjectCommand
} = require('@aws-sdk/client-s3');

// ============================================================
// 0) Config
// ============================================================
const PORT = process.env.PORT || 7860;
const {
  OPENWEBUI_BASE_URL,
  OPENWEBUI_API_KEY,
  OPENCLAW_WS_URL,
  OPENCLAW_TOKEN,
  BOT_USERNAME = 'openman',
  POLL_INTERVAL = 5000,
  DEBUG_LOGS = 'true',
  CONNECT_PASSWORD = '',
  FRONT_STREAM_INTERVAL = '1500',
  MAX_UPLOAD_MB = '30',
  DEFAULT_MESSAGE_LIMIT = '120',
  PUBLIC_URL = '',
  STORAGE_EXTERNAL_ENDPOINT,
  STORAGE_PUBLIC_BUCKET,
  STORAGE_REGION,
  STORAGE_ACCESS_KEY_ID,
  STORAGE_SECRET_ACCESS_KEY,
  STORAGE_S3_FORCE_PATH_STYLE
} = process.env;

// 运行时配置(可被 /api/runtime-settings 更新并立即生效)
const runtimeConfig = {
  botUsername: BOT_USERNAME,
  debugLogs: String(DEBUG_LOGS).toLowerCase() !== 'false',
  streamInterval: Math.max(parseInt(FRONT_STREAM_INTERVAL, 10) || 1500, 800),
  defaultMessageLimit: Math.max(parseInt(DEFAULT_MESSAGE_LIMIT, 10) || 120, 20),
  uiProfile: {
    OPENCLAW_WS_URL: OPENCLAW_WS_URL || '',
    // 注意:token / secret 不会通过 config 返回给前端,但会在运行时保存并用于连接
    OPENCLAW_TOKEN: OPENCLAW_TOKEN || '',
    PUBLIC_URL: PUBLIC_URL || '',
    STORAGE_EXTERNAL_ENDPOINT: STORAGE_EXTERNAL_ENDPOINT || '',
    STORAGE_PUBLIC_BUCKET: STORAGE_PUBLIC_BUCKET || '',
    STORAGE_REGION: STORAGE_REGION || '',
    STORAGE_ACCESS_KEY_ID: STORAGE_ACCESS_KEY_ID || '',
    STORAGE_SECRET_ACCESS_KEY: STORAGE_SECRET_ACCESS_KEY || '',
    STORAGE_S3_FORCE_PATH_STYLE: STORAGE_S3_FORCE_PATH_STYLE || 'false'
  }
};

function safeRuntimeConfig() {
  const ui = runtimeConfig.uiProfile || {};
  const safeUi = {
    OPENCLAW_WS_URL: ui.OPENCLAW_WS_URL || '',
    PUBLIC_URL: ui.PUBLIC_URL || '',
    STORAGE_EXTERNAL_ENDPOINT: ui.STORAGE_EXTERNAL_ENDPOINT || '',
    STORAGE_PUBLIC_BUCKET: ui.STORAGE_PUBLIC_BUCKET || '',
    STORAGE_REGION: ui.STORAGE_REGION || '',
    STORAGE_S3_FORCE_PATH_STYLE: ui.STORAGE_S3_FORCE_PATH_STYLE || 'false',
    // 不泄露敏感字段,仅提示是否已配置
    OPENCLAW_TOKEN_CONFIGURED: !!(ui.OPENCLAW_TOKEN || OPENCLAW_TOKEN),
    STORAGE_ACCESS_KEY_CONFIGURED: !!(ui.STORAGE_ACCESS_KEY_ID || STORAGE_ACCESS_KEY_ID),
    STORAGE_SECRET_KEY_CONFIGURED: !!(ui.STORAGE_SECRET_ACCESS_KEY || STORAGE_SECRET_ACCESS_KEY)
  };
  return {
    botUsername: runtimeConfig.botUsername,
    debugLogs: runtimeConfig.debugLogs,
    streamInterval: runtimeConfig.streamInterval,
    defaultMessageLimit: runtimeConfig.defaultMessageLimit,
    uiProfile: safeUi
  };
}

const uploadMaxMB = Math.max(parseInt(MAX_UPLOAD_MB, 10) || 30, 5);
const uploadMaxBytes = uploadMaxMB * 1024 * 1024;

// ============================================================
// S3 dynamic client (runtime apply)
// ============================================================
let currentS3Config = {
  region: runtimeConfig.uiProfile.STORAGE_REGION || 'us-east-1',
  endpoint: runtimeConfig.uiProfile.STORAGE_EXTERNAL_ENDPOINT || '',
  forcePathStyle: runtimeConfig.uiProfile.STORAGE_S3_FORCE_PATH_STYLE === 'true',
  accessKeyId: runtimeConfig.uiProfile.STORAGE_ACCESS_KEY_ID || '',
  secretAccessKey: runtimeConfig.uiProfile.STORAGE_SECRET_ACCESS_KEY || ''
};

function createS3Client(conf) {
  const region = conf.region || 'us-east-1';
  const endpoint = conf.endpoint || undefined;
  const forcePathStyle = !!conf.forcePathStyle;
  const accessKeyId = conf.accessKeyId || '';
  const secretAccessKey = conf.secretAccessKey || '';

  return new S3Client({
    region,
    endpoint,
    forcePathStyle,
    credentials:
      accessKeyId && secretAccessKey
        ? { accessKeyId, secretAccessKey }
        : undefined
  });
}

let s3 = createS3Client(currentS3Config);

function rebuildS3ClientIfNeeded() {
  const ui = runtimeConfig.uiProfile || {};
  const next = {
    region: ui.STORAGE_REGION || STORAGE_REGION || 'us-east-1',
    endpoint: ui.STORAGE_EXTERNAL_ENDPOINT || STORAGE_EXTERNAL_ENDPOINT || '',
    forcePathStyle: (ui.STORAGE_S3_FORCE_PATH_STYLE || STORAGE_S3_FORCE_PATH_STYLE || 'false') === 'true',
    accessKeyId: ui.STORAGE_ACCESS_KEY_ID || STORAGE_ACCESS_KEY_ID || '',
    secretAccessKey: ui.STORAGE_SECRET_ACCESS_KEY || STORAGE_SECRET_ACCESS_KEY || ''
  };

  const changed =
    next.region !== currentS3Config.region ||
    next.endpoint !== currentS3Config.endpoint ||
    next.forcePathStyle !== currentS3Config.forcePathStyle ||
    next.accessKeyId !== currentS3Config.accessKeyId ||
    next.secretAccessKey !== currentS3Config.secretAccessKey;

  if (changed) {
    log('🔄 S3 配置变更,重建 S3Client');
    currentS3Config = next;
    s3 = createS3Client(currentS3Config);
  }
}

const S3_PREFIX_CONFIG = 'openclaw/channels.json';
const S3_PREFIX_UPLOADS = 'openclaw/uploads/';

// ============================================================
// logs
// ============================================================
const logBuffer = [];
function addLog(type, msg) {
  if (type === 'info' && !runtimeConfig.debugLogs) return;
  const time = new Date().toLocaleTimeString('zh-CN', { hour12: false });
  const line = `[${time}] ${type === 'err' ? '❌ ' : ''}${msg}`;
  console[type === 'err' ? 'error' : 'log'](line);
  logBuffer.unshift(line);
  if (logBuffer.length > 300) logBuffer.pop();
}
function log(msg) { addLog('info', msg); }
function err(msg) { addLog('err', msg); }

// ============================================================
// runtime state
// ============================================================
const channelRuntime = new Map();
let ws = null;
let isClawReady = false;
let isReconnecting = false;
let pingInterval = null;
let reconnectTimer = null;
const pendingRequests = new Map();

// OpenWebUI client (固定使用 env;不做运行时改动)
const webuiApi = axios.create({
  baseURL: OPENWEBUI_BASE_URL,
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${OPENWEBUI_API_KEY}`
  },
  timeout: 10000
});

const fileDownloader = axios.create({
  baseURL: OPENWEBUI_BASE_URL,
  headers: { Authorization: `Bearer ${OPENWEBUI_API_KEY}` },
  responseType: 'arraybuffer',
  timeout: 30000
});

// ============================================================
// 1) S3 helpers
// ============================================================
async function streamToString(stream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    stream.on('data', c => chunks.push(c));
    stream.on('error', reject);
    stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
  });
}

function getS3Bucket() {
  return runtimeConfig.uiProfile.STORAGE_PUBLIC_BUCKET || STORAGE_PUBLIC_BUCKET || '';
}
function getPublicBaseUrl() {
  const ui = runtimeConfig.uiProfile || {};
  return ui.PUBLIC_URL || PUBLIC_URL || ui.STORAGE_EXTERNAL_ENDPOINT || STORAGE_EXTERNAL_ENDPOINT || '';
}

async function loadChannelsFromS3() {
  const bucket = getS3Bucket();
  if (!bucket) {
    log('ℹ️ [S3] STORAGE_PUBLIC_BUCKET 未配置,跳过读取频道配置');
    return;
  }

  try {
    log(`☁️ [S3] 读取频道配置... bucket=${bucket}`);
    const rsp = await s3.send(
      new GetObjectCommand({
        Bucket: bucket,
        Key: S3_PREFIX_CONFIG
      })
    );

    const str = await streamToString(rsp.Body);
    const data = JSON.parse(str);
    const channels = Array.isArray(data?.channels) ? data.channels : [];

    // 规范化 + 去重
    const normalized = [];
    const seen = new Set();
    for (const ch of channels) {
      const id = String(ch?.id || '').trim();
      if (!id || seen.has(id)) continue;
      seen.add(id);
      normalized.push({ id, name: String(ch?.name || id).trim() || id });
    }

    // 以 S3 为准“替换”本地 channelRuntime
    const nextIds = new Set(normalized.map(x => x.id));

    // 1) 删除本地存在但 S3 不存在的频道
    for (const id of Array.from(channelRuntime.keys())) {
      if (!nextIds.has(id)) channelRuntime.delete(id);
    }

    // 2) 新增/更新 S3 中的频道
    for (const ch of normalized) {
      if (!channelRuntime.has(ch.id)) {
        channelRuntime.set(ch.id, { id: ch.id, name: ch.name, lastMsgId: null, syncing: false });
        initChannelBaseline(ch.id);
      } else {
        const old = channelRuntime.get(ch.id);
        old.name = ch.name || old.name || ch.id;
      }
    }

    log(`✅ [S3] 频道配置已应用:${normalized.length} 个`);
  } catch (e) {
    if (e.name === 'NoSuchKey') {
      log('ℹ️ [S3] 无 channels.json(将清空本地频道列表)');
      channelRuntime.clear();
    } else {
      err(`S3读取错误: ${e.message}`);
    }
  }
}

async function saveChannelsToS3() {
  const bucket = getS3Bucket();
  if (!bucket) throw new Error('STORAGE_PUBLIC_BUCKET 未配置');

  try {
    const channels = Array.from(channelRuntime.values()).map(c => ({ id: c.id, name: c.name || c.id }));
    const body = JSON.stringify({ version: '2.0', savedAt: new Date().toISOString(), channels }, null, 2);
    await s3.send(new PutObjectCommand({
      Bucket: bucket,
      Key: S3_PREFIX_CONFIG,
      Body: body,
      ContentType: 'application/json'
    }));
    log(`☁️ [S3] 已保存频道配置 (${channels.length} 个)`);
  } catch (e) {
    err(`S3保存错误: ${e.message}`);
    throw e;
  }
}

async function uploadToS3(filename, buffer, mimeType) {
  const bucket = getS3Bucket();
  if (!bucket) {
    err('S3 Bucket 未配置,无法上传');
    return null;
  }
  const safeName = `${Date.now()}_${String(filename).replace(/[^a-zA-Z0-9._-]/g, '_')}`;
  const key = S3_PREFIX_UPLOADS + safeName;
  try {
    await s3.send(new PutObjectCommand({
      Bucket: bucket,
      Key: key,
      Body: buffer,
      ContentType: mimeType || 'application/octet-stream',
      ACL: 'public-read'
    }));

    let base = getPublicBaseUrl();
    if (base && !base.endsWith('/')) base += '/';
    return `${base}${bucket}/${key}`;
  } catch (e) {
    err(`S3上传失败 [${filename}]: ${e.message}`);
    return null;
  }
}

async function cleanOldS3Files() {
  const bucket = getS3Bucket();
  if (!bucket) return;

  try {
    const threeDaysAgo = Date.now() - 3 * 24 * 60 * 60 * 1000;
    let continuationToken = null;
    let deleted = 0;

    do {
      const result = await s3.send(new ListObjectsV2Command({
        Bucket: bucket,
        Prefix: S3_PREFIX_UPLOADS,
        ContinuationToken: continuationToken
      }));

      if (result.Contents) {
        for (const item of result.Contents) {
          if (item.LastModified && item.LastModified.getTime() < threeDaysAgo) {
            await s3.send(new DeleteObjectCommand({ Bucket: bucket, Key: item.Key }));
            deleted++;
          }
        }
      }
      continuationToken = result.NextContinuationToken;
    } while (continuationToken);

    if (deleted > 0) log(`🧹 [S3] 清理旧文件 ${deleted} 个`);
  } catch (e) {
    err(`清理失败: ${e.message}`);
  }
}

// ============================================================
// 2) OpenClaw request helpers
// ============================================================
function clawRequest(method, params = {}, timeout = 5000) {
  return new Promise((resolve, reject) => {
    if (!isClawReady || !ws || ws.readyState !== WebSocket.OPEN) {
      return reject(new Error('OpenClaw未连接'));
    }
    const id = crypto.randomUUID();
    const timer = setTimeout(() => {
      pendingRequests.delete(id);
      reject(new Error(`OpenClaw请求超时: ${method}`));
    }, timeout);

    pendingRequests.set(id, { resolve, reject, timer, method });
    try {
      ws.send(JSON.stringify({ type: 'req', id, method, params }));
    } catch (e) {
      clearTimeout(timer);
      pendingRequests.delete(id);
      reject(e);
    }
  });
}

function parseClawContent(content) {
  const result = { text: '', toolCalls: [], toolResults: [], files: [] };
  if (typeof content === 'string') { result.text = content; return result; }

  if (Array.isArray(content)) {
    const lines = [];
    for (const item of content) {
      if (!item) continue;
      if (typeof item === 'string') { lines.push(item); continue; }
      const type = item.type || '';
      if (type === 'text' && item.text) lines.push(item.text);
      else if (type === 'image' && item.url) { lines.push(`![图片](${item.url})`); result.files.push({ url: item.url, type: 'image' }); }
      else if (type === 'audio' && item.url) { lines.push(`[语音](${item.url}#audio)`); result.files.push({ url: item.url, type: 'audio' }); }
      else if (type === 'video' && item.url) { lines.push(`[视频](${item.url})`); result.files.push({ url: item.url, type: 'video' }); }
      else if (type === 'toolCall') result.toolCalls.push(item);
      else if (type === 'toolResult') result.toolResults.push(item);
    }
    result.text = lines.join('\n').trim();
    return result;
  }

  if (content && typeof content === 'object') {
    if (typeof content.text === 'string') result.text = content.text;
    if (Array.isArray(content.parts)) {
      const p = parseClawContent(content.parts);
      result.text = p.text;
      result.toolCalls = p.toolCalls;
      result.toolResults = p.toolResults;
      result.files = p.files;
    }
  }
  return result;
}

function stableMsgId(m, idx = 0) {
  if (m.id) return String(m.id);
  const tsRaw = m.timestamp ?? m.created_at ?? 0;
  const ts = Number(tsRaw) || 0;
  const p = parseClawContent(m.content);
  const hash = crypto
    .createHash('sha1')
    .update(`${ts}|${m.role || ''}|${p.text || ''}|${JSON.stringify(p.toolCalls || [])}|${JSON.stringify(p.toolResults || [])}`)
    .digest('hex')
    .slice(0, 12);
  return `claw_${ts}_${idx}_${hash}`;
}

function normalizeClawMessages(rawList = []) {
  return rawList.map((m, idx) => {
    const role = m.role || 'assistant';
    const parsed = parseClawContent(m.content);
    const ts = Number(m.timestamp ?? m.created_at ?? Date.now()) || Date.now();
    return {
      id: stableMsgId(m, idx),
      role,
      content: parsed.text || '',
      timestamp: ts,
      user: {
        name: role === 'assistant' ? runtimeConfig.botUsername : role === 'toolResult' ? 'Tool' : 'User'
      },
      data: {
        files: parsed.files || [],
        toolCalls: parsed.toolCalls || [],
        toolResults: parsed.toolResults || [],
        rawRole: role
      }
    };
  });
}

async function getClawMessagesByChannel(cid, limit = 120) {
  const payload = await clawRequest(
    'chat.history',
    { sessionKey: `agent:main:${cid}`, limit: Math.max(10, limit) },
    7000
  );
  const raw = Array.isArray(payload?.messages) ? payload.messages : [];
  const normalized = normalizeClawMessages(raw);
  normalized.sort((a, b) => Number(b.timestamp || 0) - Number(a.timestamp || 0)); // new->old
  return normalized;
}

// ============================================================
// 3) API
// ============================================================
const app = express();
app.use(express.json({ limit: `${uploadMaxMB}mb` }));
app.use(express.static(path.join(__dirname, 'public')));

function passOK(req) {
  if (!CONNECT_PASSWORD) return true;
  const p = req.headers['x-connect-password'] || req.query.p || '';
  return String(p) === String(CONNECT_PASSWORD);
}

function apiGuard(req, res, next) {
  if (req.path === '/config') return next();
  if (passOK(req)) return next();
  return res.status(401).json({ error: '连接密码错误或未提供' });
}

app.use('/api', apiGuard);

app.get('/api/config', (req, res) => {
  res.json({
    needPassword: !!CONNECT_PASSWORD,
    maxUploadMB: uploadMaxMB,
    runtimeConfig: safeRuntimeConfig(),
    envKeys: [
      'CONNECT_PASSWORD',
      'OPENCLAW_WS_URL',
      'OPENCLAW_TOKEN',
      'BOT_USERNAME',
      'FRONT_STREAM_INTERVAL',
      'MAX_UPLOAD_MB',
      'DEFAULT_MESSAGE_LIMIT',
      'DEBUG_LOGS',
      'STORAGE_EXTERNAL_ENDPOINT',
      'STORAGE_PUBLIC_BUCKET',
      'STORAGE_REGION',
      'STORAGE_ACCESS_KEY_ID',
      'STORAGE_SECRET_ACCESS_KEY',
      'STORAGE_S3_FORCE_PATH_STYLE',
      'PUBLIC_URL'
    ]
  });
});

app.get('/api/runtime-settings', (req, res) => res.json(safeRuntimeConfig()));

app.post('/api/runtime-settings', async (req, res) => {
  const body = req.body || {};

  // 记录旧值用于判断是否需要“立即应用”
  const oldWs = runtimeConfig.uiProfile.OPENCLAW_WS_URL || '';
  const oldToken = runtimeConfig.uiProfile.OPENCLAW_TOKEN || '';
  const oldBot = runtimeConfig.botUsername;
  const oldDebug = runtimeConfig.debugLogs;

  const oldS3Fingerprint = JSON.stringify({
    bucket: runtimeConfig.uiProfile.STORAGE_PUBLIC_BUCKET || STORAGE_PUBLIC_BUCKET || '',
    endpoint: runtimeConfig.uiProfile.STORAGE_EXTERNAL_ENDPOINT || STORAGE_EXTERNAL_ENDPOINT || '',
    region: runtimeConfig.uiProfile.STORAGE_REGION || STORAGE_REGION || '',
    forcePathStyle: runtimeConfig.uiProfile.STORAGE_S3_FORCE_PATH_STYLE || STORAGE_S3_FORCE_PATH_STYLE || 'false',
    accessKeyId: runtimeConfig.uiProfile.STORAGE_ACCESS_KEY_ID || STORAGE_ACCESS_KEY_ID || '',
    secretAccessKey: runtimeConfig.uiProfile.STORAGE_SECRET_ACCESS_KEY || STORAGE_SECRET_ACCESS_KEY || ''
  });

  // ---- 普通运行时项 ----
  if (typeof body.botUsername === 'string' && body.botUsername.trim()) {
    runtimeConfig.botUsername = body.botUsername.trim();
  }
  if (typeof body.debugLogs === 'boolean') runtimeConfig.debugLogs = body.debugLogs;

  if (Number.isFinite(Number(body.streamInterval))) {
    runtimeConfig.streamInterval = Math.max(800, parseInt(body.streamInterval, 10));
  }
  if (Number.isFinite(Number(body.defaultMessageLimit))) {
    runtimeConfig.defaultMessageLimit = Math.max(20, parseInt(body.defaultMessageLimit, 10));
  }

  // ---- uiProfile 合并(OpenClaw/S3 都在这里)----
  if (body.uiProfile && typeof body.uiProfile === 'object') {
    runtimeConfig.uiProfile = { ...runtimeConfig.uiProfile, ...body.uiProfile };
  }

  const newS3Fingerprint = JSON.stringify({
    bucket: runtimeConfig.uiProfile.STORAGE_PUBLIC_BUCKET || STORAGE_PUBLIC_BUCKET || '',
    endpoint: runtimeConfig.uiProfile.STORAGE_EXTERNAL_ENDPOINT || STORAGE_EXTERNAL_ENDPOINT || '',
    region: runtimeConfig.uiProfile.STORAGE_REGION || STORAGE_REGION || '',
    forcePathStyle: runtimeConfig.uiProfile.STORAGE_S3_FORCE_PATH_STYLE || STORAGE_S3_FORCE_PATH_STYLE || 'false',
    accessKeyId: runtimeConfig.uiProfile.STORAGE_ACCESS_KEY_ID || STORAGE_ACCESS_KEY_ID || '',
    secretAccessKey: runtimeConfig.uiProfile.STORAGE_SECRET_ACCESS_KEY || STORAGE_SECRET_ACCESS_KEY || ''
  });

  // 立即应用:S3 client
  const s3Changed = oldS3Fingerprint !== newS3Fingerprint;
  if (s3Changed) {
    rebuildS3ClientIfNeeded();
    // 关键:S3 配置变了就重新拉 channels.json,刷新频道列表
    await loadChannelsFromS3();
  }

  // botUsername / debugLogs 打日志(可选)
  if (runtimeConfig.botUsername !== oldBot) log(`👤 BOT_USERNAME 更新: ${oldBot} -> ${runtimeConfig.botUsername}`);
  if (runtimeConfig.debugLogs !== oldDebug) log(`🐞 DEBUG_LOGS 更新: ${oldDebug} -> ${runtimeConfig.debugLogs}`);

  // 立即应用:OpenClaw 连接变更 → 重连
  const newWs = runtimeConfig.uiProfile.OPENCLAW_WS_URL || '';
  const newToken = runtimeConfig.uiProfile.OPENCLAW_TOKEN || '';
  if (newWs !== oldWs || newToken !== oldToken) {
    log('🔄 OpenClaw 配置变更,触发重连');
    forceReconnectOpenClaw();
  }

  log('⚙️ 运行设置更新');
  res.json({ success: true, runtimeConfig: safeRuntimeConfig() });
});

app.get('/api/channels', (req, res) => {
  res.json(Array.from(channelRuntime.values()));
});

app.post('/api/channels', async (req, res) => {
  try {
    const id = String(req.body?.id || '').trim();
    const name = String(req.body?.name || id).trim();
    if (!id) return res.status(400).json({ error: 'id不能为空' });
    if (channelRuntime.has(id)) return res.status(409).json({ error: '频道已存在' });

    channelRuntime.set(id, { id, name: name || id, lastMsgId: null, syncing: false });
    await saveChannelsToS3();
    initChannelBaseline(id);
    log(`➕ 新增频道: ${name} (${id})`);
    res.json({ success: true, channel: { id, name: name || id } });
  } catch (e) {
    err(`新增频道失败: ${e.message}`);
    res.status(500).json({ error: '新增频道失败' });
  }
});

app.delete('/api/channels/:id', async (req, res) => {
  try {
    const id = String(req.params.id || '').trim();
    if (!id) return res.status(400).json({ error: 'id不能为空' });
    if (!channelRuntime.has(id)) return res.status(404).json({ error: '频道不存在' });

    channelRuntime.delete(id);
    await saveChannelsToS3();
    log(`➖ 删除频道: ${id}`);
    res.json({ success: true });
  } catch (e) {
    err(`删除频道失败: ${e.message}`);
    res.status(500).json({ error: '删除频道失败' });
  }
});

app.get('/api/logs', (req, res) => res.json(logBuffer));

app.get('/api/messages/:cid', async (req, res) => {
  const cid = req.params.cid;
  try {
    const limit = Math.max(parseInt(req.query.limit, 10) || runtimeConfig.defaultMessageLimit, 20);
    const list = await getClawMessagesByChannel(cid, limit);
    res.json(list);
  } catch (e) {
    err(`[前端] Claw消息获取失败 ${cid}: ${e.message}`);
    res.json([]);
  }
});

app.get('/api/messages/:cid/stream', async (req, res) => {
  const cid = req.params.cid;
  const limit = Math.max(parseInt(req.query.limit, 10) || runtimeConfig.defaultMessageLimit, 20);
  const interval = Math.max(parseInt(req.query.interval, 10) || runtimeConfig.streamInterval, 800);
  if (!passOK(req)) return res.status(401).end('unauthorized');

  res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
  res.setHeader('Cache-Control', 'no-cache, no-transform');
  res.setHeader('Connection', 'keep-alive');
  res.flushHeaders();

  let closed = false;
  let lastSeen = req.query.last_id || null;

  const sendEvent = (event, payload) => {
    if (closed) return;
    res.write(`event: ${event}\n`);
    res.write(`data: ${JSON.stringify(payload)}\n\n`);
  };

  const heartbeat = setInterval(() => {
    if (!closed) res.write(': ping\n\n');
  }, 15000);

  const tick = async () => {
    if (closed) return;
    try {
      const latest = await getClawMessagesByChannel(cid, limit); // new->old
      const sorted = [...latest].sort((a, b) => Number(a.timestamp || 0) - Number(b.timestamp || 0)); // old->new

      if (!lastSeen) {
        if (sorted.length > 0) lastSeen = sorted[sorted.length - 1].id;
        sendEvent('ready', { ok: true, count: sorted.length, lastSeen });
        return;
      }

      const idx = sorted.findIndex(m => m.id === lastSeen);
      const fresh = idx === -1 ? sorted : sorted.slice(idx + 1);
      if (fresh.length > 0) {
        lastSeen = fresh[fresh.length - 1].id;
        sendEvent('messages', { items: fresh, lastSeen });
      }
    } catch (e) {
      sendEvent('warn', { message: e.message || 'stream tick error' });
    }
  };

  const timer = setInterval(tick, interval);
  tick();

  req.on('close', () => {
    closed = true;
    clearInterval(timer);
    clearInterval(heartbeat);
    try { res.end(); } catch {}
  });
});

app.post('/api/upload', async (req, res) => {
  const { filename, base64, mimeType } = req.body || {};
  if (!base64 || !filename) return res.status(400).json({ error: '参数缺失' });
  try {
    const buffer = Buffer.from(base64, 'base64');
    if (buffer.length > uploadMaxBytes) {
      return res.status(413).json({ error: `文件过大,超过 ${uploadMaxMB}MB` });
    }
    const url = await uploadToS3(filename, buffer, mimeType);
    if (!url) return res.status(500).json({ error: 'S3上传失败' });
    res.json({ url, mimeType: mimeType || '' });
  } catch (e) {
    err(`/api/upload失败: ${e.message}`);
    res.status(500).json({ error: '上传异常' });
  }
});

app.post('/api/send/:cid', async (req, res) => {
  const cid = req.params.cid;
  const { text = '' } = req.body || {};
  if (!channelRuntime.has(cid)) return res.status(404).json({ error: '频道不存在' });

  const clean = String(text).trim();
  if (!clean) return res.status(400).json({ error: 'text不能为空' });

  const isCommand = clean.startsWith('/');
  triggerAgent(cid, null, 'admin', clean, isCommand);
  log(`[Web发送] ${cid}: ${clean.slice(0, 120)}...`);
  res.json({ success: true });
});

app.get('/health', (req, res) => {
  res.json({ status: 'ok', wsConnected: isClawReady, channels: channelRuntime.size });
});

app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'public', 'index.html')));

// ============================================================
// 4) OpenClaw WS + sync logic
// ============================================================
app.listen(PORT, async () => {
  log(`🚀 服务启动: ${PORT}`);
  await init();
});

async function init() {
  try {
    rebuildS3ClientIfNeeded();
    await loadChannelsFromS3();
    connectOpenClaw();
    setInterval(cleanOldS3Files, 3600 * 1000);
    cleanOldS3Files();
  } catch (e) {
    err(e.message);
  }
}

async function initChannelBaseline(id) {
  const state = channelRuntime.get(id);
  if (!state) return;
  try {
    const res = await webuiApi.get(`/api/v1/channels/${id}/messages?limit=1`);
    if (res.data && res.data.length > 0) {
      state.lastMsgId = res.data[0].id;
      log(`✅ [${state.name}] 锚点初始化: ${state.lastMsgId}`);
    } else {
      state.lastMsgId = 'EMPTY_START';
      log(`✅ [${state.name}] 空频道,等待第一条消息`);
    }
  } catch {
    setTimeout(() => initChannelBaseline(id), 5000);
  }
}

const { publicKey, privateKey } = crypto.generateKeyPairSync('ed25519');
const rawPublicKey = publicKey.export({ type: 'spki', format: 'der' }).subarray(12);
const deviceId = crypto.createHash('sha256').update(rawPublicKey).digest('hex');
const toB64Url = (buf) => buf.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');

function cleanupConnection() {
  if (pingInterval) clearInterval(pingInterval);
  if (ws) {
    ws.removeAllListeners();
    try { ws.terminate(); } catch {}
    ws = null;
  }
  isClawReady = false;
  for (const s of channelRuntime.values()) s.syncing = false;
}

function getEffectiveWsUrl() {
  return (runtimeConfig.uiProfile.OPENCLAW_WS_URL || OPENCLAW_WS_URL || '').trim();
}
function getEffectiveToken() {
  return (runtimeConfig.uiProfile.OPENCLAW_TOKEN || OPENCLAW_TOKEN || '').trim();
}

function forceReconnectOpenClaw() {
  cleanupConnection();
  isReconnecting = false;
  if (reconnectTimer) {
    clearTimeout(reconnectTimer);
    reconnectTimer = null;
  }
  connectOpenClaw(0);
}

function connectOpenClaw(retryCount = 0) {
  const wsUrl = getEffectiveWsUrl();
  if (!wsUrl) {
    err('OPENCLAW_WS_URL 未配置,无法连接 OpenClaw');
    return;
  }
  if (isReconnecting || ws) return;

  isReconnecting = true;
  log(`🔌 连接 OpenClaw (${retryCount + 1})...`);

  try {
    // ws endpoint
    const url = new URL(wsUrl.replace(/\/$/, '') + '/ws');

    // 关键:Origin header(通常需要用 https/http,而不是 wss/ws)
    // wss://xxx -> https://xxx
    // ws://xxx  -> http://xxx
    const origin = url.origin.replace(/^wss:/i, 'https:').replace(/^ws:/i, 'http:');
    
    ws = new WebSocket(url, {
      headers: {
        Origin: origin
      }
      // 也可以用 ws 的 origin 选项(等价):
      // origin
    });

    ws.on('open', () => {
      log(`✅ WS 握手成功 (Origin=${origin})`);
      isReconnecting = false;
      ws.isAlive = true;

      ws.on('pong', () => { ws.isAlive = true; });
      pingInterval = setInterval(() => {
        if (ws.isAlive === false) {
          err('心跳超时,终止连接');
          return ws.terminate();
        }
        ws.isAlive = false;
        ws.ping();
      }, 10000);
    });

    ws.on('message', handleClawMessage);

    ws.on('close', (code) => {
      log(`🔌 连接断开 (${code})`);
      handleDisconnect(retryCount);
    });

    ws.on('error', (e) => err(`WS错误: ${e.message}`));
  } catch (e) {
    err(e.message);
    handleDisconnect(retryCount);
  }
}

function handleDisconnect(retryCount) {
  cleanupConnection();
  isReconnecting = false;
  const delay = Math.min(5000 * Math.pow(1.5, retryCount), 60000);
  if (reconnectTimer) clearTimeout(reconnectTimer);
  reconnectTimer = setTimeout(() => connectOpenClaw(retryCount + 1), delay);
}

function handleClawMessage(data) {
  let msg;
  try { msg = JSON.parse(data.toString()); } catch { return; }

  if (msg.event === 'connect.challenge') {
    const { nonce } = msg.payload || {};
    const signedAt = Date.now();
    const token = getEffectiveToken();

    // scopes:排序,保证签名一致性
    const scopesArr = ['operator.admin', 'operator.approvals', 'operator.pairing'].slice().sort();
    const scopesStr = scopesArr.join(',');

    // v3 新增字段:platform + deviceFamily(必须与 connect.params 里保持一致)
    const platform =
      process.platform === 'win32' ? 'Win32' :
      process.platform === 'darwin' ? 'Darwin' :
      process.platform === 'linux' ? 'Linux' :
      String(process.platform || 'unknown');

    const deviceFamily = 'desktop';

    // v3 payload
    const payload = [
      'v3',
      deviceId,
      'openclaw-control-ui', // clientId
      'webchat',             // mode
      'operator',            // role
      scopesStr,             // scopes (string)
      String(signedAt),
      token,
      nonce,
      platform,              // NEW
      deviceFamily           // NEW
    ].join('|');

    const signature = crypto.sign(null, Buffer.from(payload), privateKey);

    ws.send(JSON.stringify({
      type: 'req',
      id: crypto.randomUUID(),
      method: 'connect',
      params: {
        minProtocol: 3,
        maxProtocol: 3,
        client: {
          id: 'openclaw-control-ui',
          version: 'dev',
          platform: platform,
          deviceFamily: deviceFamily, // NEW: 让网关有字段可取,避免“签名绑定字段缺失”
          mode: 'webchat'
        },
        device: {
          id: deviceId,
          publicKey: toB64Url(rawPublicKey),
          signature: toB64Url(signature),
          signedAt,
          nonce // 保留:避免 DEVICE_AUTH_NONCE_REQUIRED / MISMATCH
        },
        auth: { token },
        role: 'operator',
        scopes: scopesArr
      }
    }));
  }

  if (msg.payload?.type === 'hello-ok') {
    log('🎉 OpenClaw 认证成功');
    isClawReady = true;
  }

  if (msg.type === 'res' && msg.id && pendingRequests.has(msg.id)) {
    const req = pendingRequests.get(msg.id);
    pendingRequests.delete(msg.id);
    if (req?.timer) clearTimeout(req.timer);
    try { req?.resolve?.(msg.payload); } catch {}
  }
}

async function fetchClawHistory(cid, uid) {
  try {
    const payload = await clawRequest('chat.history', { sessionKey: `agent:main:${cid}`, limit: 5 }, 5000);
    return payload?.messages || [];
  } catch {
    return [];
  }
}

function triggerAgent(cid, uid, name, text, isCommand) {
  if (!isClawReady || !ws || ws.readyState !== WebSocket.OPEN) return;
  const finalMsg = isCommand ? text : `${name} 说: ${text}`;
  ws.send(JSON.stringify({
    type: 'req',
    id: crypto.randomUUID(),
    method: isCommand ? 'chat.send' : 'agent',
    params: {
      sessionKey: `agent:main:${cid}`,
      message: finalMsg,
      idempotencyKey: crypto.randomUUID(),
      deliver: isCommand ? false : undefined
    }
  }));
  log(`🚀 [发送] ${name}: ${text.slice(0, 30)}...`);
}

// ============================================================
// sync logic(保留你的同步,但把 BOT_USERNAME 改为 runtimeConfig.botUsername 生效)
// ============================================================
async function syncChannel(cid) {
  const state = channelRuntime.get(cid);
  if (!state || !state.lastMsgId || !isClawReady) return;

  if (state.syncing) {
    if (Date.now() - (state.syncStartTime || 0) > 60000) {
      log(`⚠️ [${state.name}] 同步超时复位`);
      state.syncing = false;
    } else return;
  }

  state.syncing = true;
  state.syncStartTime = Date.now();

  try {
    const { data: msgs } = await webuiApi.get(`/api/v1/channels/${cid}/messages?skip=0&limit=10`);
    if (!msgs?.length) throw new Error('No messages');

    const botName = (runtimeConfig.botUsername || 'openman').toLowerCase();

    const webuiFingerprints = new Set(
      msgs.map(m => (m.content || m.data?.content || '').trim()).filter(Boolean)
    );

    const activeUsers = new Map();
    msgs.slice(0, 5).forEach(m => {
      if (m.user_id) activeUsers.set(m.user_id, { id: m.user_id, name: m.user?.name || 'User' });
    });

    for (const u of activeUsers.values()) {
      await new Promise(r => setTimeout(r, 200));
      const history = await fetchClawHistory(cid, u.id);
      const replies = history.filter(h => h.role === 'assistant').slice(-2);
      for (const r of replies) {
        const txt =
          typeof r.content === 'string'
            ? r.content.trim()
            : Array.isArray(r.content)
              ? (r.content[0]?.text || '').trim()
              : '';
        if (txt && !webuiFingerprints.has(txt)) {
          log(`📥 [回填回复] -> ${u.name}`);
          await webuiApi.post(`/api/v1/channels/${cid}/messages/post`, { content: txt, data: { files: [] }, reply_to_id: null });
          webuiFingerprints.add(txt);
        }
      }
    }

    const sorted = msgs.reverse();
    let newMsgs = [];
    const startIdx = sorted.findIndex(m => m.id === state.lastMsgId);

    if (state.lastMsgId === 'EMPTY_START') newMsgs = sorted;
    else if (startIdx !== -1) newMsgs = sorted.slice(startIdx + 1);
    else {
      if (sorted.length > 0) {
        const newestId = sorted[sorted.length - 1].id;
        log(`⚠️ [${state.name}] 锚点丢失,重置 -> ${newestId}`);
        state.lastMsgId = newestId;
      }
      newMsgs = [];
    }

    if (newMsgs.length > 0) {
      state.lastMsgId = sorted[sorted.length - 1].id;

      for (const m of newMsgs) {
        const sender = m.user?.name || '';
        if (sender.toLowerCase() === botName) continue;

        let txt = (m.content || m.data?.content || '')
          .replace(/<@U:[^>]+>/g, '')
          .replace(new RegExp(`@${runtimeConfig.botUsername}`, 'gi'), '')
          .trim();

        const atts = [];
        let rawFiles = [];

        if (m.data === true) {
          try {
            const { data: detailData } = await webuiApi.get(`/api/v1/channels/${cid}/messages/${m.id}/data`);
            if (detailData && detailData.files) rawFiles = detailData.files;
          } catch (e) {
            err(`详情获取失败: ${e.message}`);
          }
        } else if (typeof m.data === 'object' && m.data?.files) {
          rawFiles = m.data.files;
        }

        if (rawFiles.length > 0) {
          for (const fItem of rawFiles) {
            try {
              let buffer, filename, mimeType;
              const f = fItem.file || fItem;

              if (fItem.url && fItem.url.startsWith('data:')) {
                const matches = fItem.url.match(/^data:([a-zA-Z0-9/+-]+);base64,(.+)$/);
                if (matches) {
                  mimeType = matches[1];
                  const extMap = {
                    'image/jpeg': 'jpg',
                    'image/png': 'png',
                    'image/gif': 'gif',
                    'image/webp': 'webp',
                    'audio/ogg': 'ogg',
                    'audio/webm': 'webm'
                  };
                  const ext = extMap[mimeType] || 'bin';
                  buffer = Buffer.from(matches[2], 'base64');
                  filename = `upload_${Date.now()}_${Math.random().toString(36).slice(2)}.${ext}`;
                }
              } else if (f.id) {
                const { data } = await fileDownloader.get(`/api/v1/files/${f.id}/content`);
                buffer = data;
                filename = `${Date.now()}_${(f.filename || 'file').replace(/[^a-zA-Z0-9.-]/g, '_')}`;
                mimeType = f.content_type || fItem.content_type || 'application/octet-stream';
              }

              if (buffer && filename) {
                const s3Url = await uploadToS3(filename, buffer, mimeType);
                if (s3Url) {
                  const isImage = mimeType?.startsWith('image/');
                  const isAudio = mimeType?.startsWith('audio/');
                  atts.push(
                    `[${isImage ? '图片' : isAudio ? '语音' : '文件'}: ${filename}](${s3Url}${isAudio ? '#audio' : ''}) (必须下载附件,了解内容后才能回复)`
                  );
                }
              }
            } catch (e) {
              err(`附件处理失败: ${e.message}`);
            }
          }
        }

        if (atts.length) txt += (txt ? '\n\n' : '') + atts.join('\n');

        const isCommand = txt.trim().startsWith('/');
        if (txt || atts.length > 0) {
          if (!txt && atts.length > 0) txt = '发送了附件。';
          await new Promise(r => setTimeout(r, 500));
          triggerAgent(cid, m.user_id, sender, txt, isCommand);
        }
      }
    }
  } catch (e) {
    if (e.message !== 'No messages') err(`[${state.name}] 跳过: ${e.message}`);
  } finally {
    state.syncing = false;
  }
}

async function loop() {
  const delay = Math.max(parseInt(POLL_INTERVAL || 5000, 10), 2000);
  if (isClawReady) {
    for (const cid of channelRuntime.keys()) {
      await syncChannel(cid);
      await new Promise(r => setTimeout(r, 200));
    }
  }
  setTimeout(loop, delay);
}
loop();