File size: 112,024 Bytes
8e02bdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
import _ from "lodash";
import crypto from "crypto";
import fs from "fs";
import path from "path";

import APIException from "@/lib/exceptions/APIException.ts";
import EX from "@/api/consts/exceptions.ts";
import util from "@/lib/util.ts";
import { getCredit, receiveCredit, request, DEFAULT_ASSISTANT_ID as CORE_ASSISTANT_ID, WEB_ID, acquireToken } from "./core.ts";
import logger from "@/lib/logger.ts";
import browserService from "@/lib/browser-service.ts";
import provider from "@/lib/upstream-provider.ts";

function createRequestSign(uri: string, deviceTime: number) {
  return util.md5(`9e2c|${uri.slice(-7)}|7|8.4.0|${deviceTime}||11ac`);
}

const DEFAULT_ASSISTANT_ID = provider.assistantId;
export const DEFAULT_MODEL = "dreamina_ic_generate_video_model_vgfm_3.0_fast";
const DEFAULT_DRAFT_VERSION = "3.2.8";

const MODEL_DRAFT_VERSIONS: { [key: string]: string } = {
  "dreamina_ic_generate_video_model_vgfm_3.0_fast": "3.3.12",
  "jimeng-video-3.5-pro": "3.3.4",
  "jimeng-video-3.0-pro": "3.2.8",
  "jimeng-video-3.0": "3.2.8",
  "jimeng-video-2.0": "3.2.8",
  "jimeng-video-2.0-pro": "3.2.8",
  // Seedance 模型(与上游 iptag/jimeng-api 保持一致)
  "jimeng-video-seedance-2.0": "3.3.9",
  "seedance-2.0": "3.3.9",
  "seedance-2.0-pro": "3.3.9",
  // Seedance 2.0-fast 模型(v1.9.3 新增)
  "jimeng-video-seedance-2.0-fast": "3.3.9",
  "seedance-2.0-fast": "3.3.9",
};

const MODEL_MAP = {
  "dreamina_ic_generate_video_model_vgfm_3.0_fast": "dreamina_ic_generate_video_model_vgfm_3.0_fast",
  "jimeng-video-3.5-pro": "dreamina_ic_generate_video_model_vgfm_3.5_pro",
  "jimeng-video-3.0-pro": "dreamina_ic_generate_video_model_vgfm_3.0_pro",
  "jimeng-video-3.0": "dreamina_ic_generate_video_model_vgfm_3.0",
  "jimeng-video-2.0": "dreamina_ic_generate_video_model_vgfm_lite",
  "jimeng-video-2.0-pro": "dreamina_ic_generate_video_model_vgfm1.0",
  // Seedance 多图智能视频生成模型(jimeng-video-seedance-2.0 为上游标准名称)
  "jimeng-video-seedance-2.0": "dreamina_seedance_40_pro",
  "seedance-2.0": "dreamina_seedance_40_pro",
  "seedance-2.0-pro": "dreamina_seedance_40_pro",
  // Seedance 2.0-fast 快速生成模型(v1.9.3 新增,内部模型为 dreamina_seedance_40)
  "jimeng-video-seedance-2.0-fast": "dreamina_seedance_40",
  "seedance-2.0-fast": "dreamina_seedance_40",
};

// Seedance 模型的 benefit_type 映射
const SEEDANCE_BENEFIT_TYPE_MAP: { [key: string]: string } = {
  "jimeng-video-seedance-2.0": "dreamina_video_seedance_20_pro",
  "seedance-2.0": "dreamina_video_seedance_20_pro",
  "seedance-2.0-pro": "dreamina_video_seedance_20_pro",
  // Seedance 2.0-fast(v1.9.3 新增,注意:无 "video_" 前缀)
  "jimeng-video-seedance-2.0-fast": "dreamina_seedance_20_fast",
  "seedance-2.0-fast": "dreamina_seedance_20_fast",
};

// 判断是否为 Seedance 模型
export function isSeedanceModel(model: string): boolean {
  return model.startsWith("seedance-") || model.startsWith("jimeng-video-seedance-");
}

// ========== Seedance 多类型素材支持 ==========

// 素材类型
type SeedanceMaterialType = "image" | "video" | "audio";

// 上传结果统一接口
interface UploadedMaterial {
  type: SeedanceMaterialType;
  // 图片
  uri?: string;
  // 视频/音频(VOD)
  vid?: string;
  // 通用
  width?: number;
  height?: number;
  duration?: number;
  fps?: number;
  name?: string;
}

// MIME 类型 → 素材类型映射
const MIME_TO_MATERIAL_TYPE: Record<string, SeedanceMaterialType> = {
  "image/jpeg": "image", "image/png": "image", "image/webp": "image",
  "image/gif": "image", "image/bmp": "image",
  "video/mp4": "video", "video/quicktime": "video", "video/x-m4v": "video",
  "audio/mpeg": "audio", "audio/wav": "audio", "audio/x-wav": "audio",
  "audio/mp3": "audio",
};

// 扩展名 → 素材类型映射(兜底)
const EXT_TO_MATERIAL_TYPE: Record<string, SeedanceMaterialType> = {
  ".jpg": "image", ".jpeg": "image", ".png": "image", ".webp": "image",
  ".gif": "image", ".bmp": "image",
  ".mp4": "video", ".mov": "video", ".m4v": "video",
  ".mp3": "audio", ".wav": "audio",
};

// materialTypes 编码映射
const MATERIAL_TYPE_CODE: Record<SeedanceMaterialType, number> = {
  image: 1, video: 2, audio: 3,
};

/**
 * 检测上传文件的素材类型
 * 优先通过 MIME 类型判断,兜底通过文件扩展名
 */
function detectMaterialType(file: any): SeedanceMaterialType {
  // 优先通过 MIME 类型判断
  const mime = (file.mimetype || file.mimeType || "").toLowerCase();
  if (mime && MIME_TO_MATERIAL_TYPE[mime]) return MIME_TO_MATERIAL_TYPE[mime];
  // 兜底:通过文件扩展名判断
  const filename = (file.originalFilename || file.newFilename || "").toLowerCase();
  const dotIdx = filename.lastIndexOf(".");
  if (dotIdx >= 0) {
    const ext = filename.substring(dotIdx);
    if (EXT_TO_MATERIAL_TYPE[ext]) return EXT_TO_MATERIAL_TYPE[ext];
  }
  // 默认视为图片(向后兼容)
  return "image";
}

/**
 * 从 URL 检测素材类型
 * 通过 URL 路径的扩展名判断
 */
function detectMaterialTypeFromUrl(url: string): SeedanceMaterialType {
  try {
    const pathname = new URL(url).pathname.toLowerCase();
    const dotIdx = pathname.lastIndexOf(".");
    if (dotIdx >= 0) {
      const ext = pathname.substring(dotIdx);
      if (EXT_TO_MATERIAL_TYPE[ext]) return EXT_TO_MATERIAL_TYPE[ext];
    }
  } catch {}
  // 默认视为图片(向后兼容)
  return "image";
}

// 视频支持的分辨率和比例配置
const VIDEO_RESOLUTION_OPTIONS: {
  [resolution: string]: {
    [ratio: string]: { width: number; height: number };
  };
} = {
  "480p": {
    "1:1": { width: 480, height: 480 },
    "4:3": { width: 640, height: 480 },
    "3:4": { width: 480, height: 640 },
    "16:9": { width: 854, height: 480 },
    "9:16": { width: 480, height: 854 },
  },
  "720p": {
    "1:1": { width: 720, height: 720 },
    "4:3": { width: 960, height: 720 },
    "3:4": { width: 720, height: 960 },
    "16:9": { width: 1280, height: 720 },
    "9:16": { width: 720, height: 1280 },
  },
  "1080p": {
    "1:1": { width: 1080, height: 1080 },
    "4:3": { width: 1440, height: 1080 },
    "3:4": { width: 1080, height: 1440 },
    "16:9": { width: 1920, height: 1080 },
    "9:16": { width: 1080, height: 1920 },
  },
};

// 解析视频分辨率参数
function resolveVideoResolution(
  resolution: string = "720p",
  ratio: string = "1:1"
): { width: number; height: number } {
  const resolutionGroup = VIDEO_RESOLUTION_OPTIONS[resolution];
  if (!resolutionGroup) {
    const supportedResolutions = Object.keys(VIDEO_RESOLUTION_OPTIONS).join(", ");
    throw new Error(`不支持的视频分辨率 "${resolution}"。支持的分辨率: ${supportedResolutions}`);
  }

  const ratioConfig = resolutionGroup[ratio];
  if (!ratioConfig) {
    const supportedRatios = Object.keys(resolutionGroup).join(", ");
    throw new Error(`在 "${resolution}" 分辨率下,不支持的比例 "${ratio}"。支持的比例: ${supportedRatios}`);
  }

  return {
    width: ratioConfig.width,
    height: ratioConfig.height,
  };
}

export function getModel(model: string) {
  return MODEL_MAP[model] || MODEL_MAP[DEFAULT_MODEL];
}

// AWS4-HMAC-SHA256 签名生成函数(从 images.ts 复制)
function createSignature(
  method: string,
  url: string,
  headers: { [key: string]: string },
  accessKeyId: string,
  secretAccessKey: string,
  sessionToken?: string,
  payload: string = '',
  awsRegion: string = provider.imageAwsRegion,
  serviceName: string = 'imagex'
) {
  const urlObj = new URL(url);
  const pathname = urlObj.pathname || '/';
  const search = urlObj.search;

  // 创建规范请求
  const timestamp = headers['x-amz-date'];
  const date = timestamp.substr(0, 8);
  const region = awsRegion;
  const service = serviceName;
  
  // 规范化查询参数
  const queryParams: Array<[string, string]> = [];
  const searchParams = new URLSearchParams(search);
  searchParams.forEach((value, key) => {
    queryParams.push([key, value]);
  });
  
  // 按键名排序
  queryParams.sort(([a], [b]) => {
    if (a < b) return -1;
    if (a > b) return 1;
    return 0;
  });
  
  const canonicalQueryString = queryParams
    .map(([key, value]) => `${key}=${value}`)
    .join('&');
  
  // 规范化头部
  const headersToSign: { [key: string]: string } = {
    'x-amz-date': timestamp
  };
  
  if (sessionToken) {
    headersToSign['x-amz-security-token'] = sessionToken;
  }
  
  let payloadHash = crypto.createHash('sha256').update('').digest('hex');
  if (method.toUpperCase() === 'POST' && payload) {
    payloadHash = crypto.createHash('sha256').update(payload, 'utf8').digest('hex');
    headersToSign['x-amz-content-sha256'] = payloadHash;
  }
  
  const signedHeaders = Object.keys(headersToSign)
    .map(key => key.toLowerCase())
    .sort()
    .join(';');
  
  const canonicalHeaders = Object.keys(headersToSign)
    .sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
    .map(key => `${key.toLowerCase()}:${headersToSign[key].trim()}\n`)
    .join('');
  
  const canonicalRequest = [
    method.toUpperCase(),
    pathname,
    canonicalQueryString,
    canonicalHeaders,
    signedHeaders,
    payloadHash
  ].join('\n');
  
  // 创建待签名字符串
  const credentialScope = `${date}/${region}/${service}/aws4_request`;
  const stringToSign = [
    'AWS4-HMAC-SHA256',
    timestamp,
    credentialScope,
    crypto.createHash('sha256').update(canonicalRequest, 'utf8').digest('hex')
  ].join('\n');
  
  // 生成签名
  const kDate = crypto.createHmac('sha256', `AWS4${secretAccessKey}`).update(date).digest();
  const kRegion = crypto.createHmac('sha256', kDate).update(region).digest();
  const kService = crypto.createHmac('sha256', kRegion).update(service).digest();
  const kSigning = crypto.createHmac('sha256', kService).update('aws4_request').digest();
  const signature = crypto.createHmac('sha256', kSigning).update(stringToSign, 'utf8').digest('hex');
  
  return `AWS4-HMAC-SHA256 Credential=${accessKeyId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;
}

// 计算文件的CRC32值(从 images.ts 复制)
function calculateCRC32(buffer: ArrayBuffer): string {
  const crcTable = [];
  for (let i = 0; i < 256; i++) {
    let crc = i;
    for (let j = 0; j < 8; j++) {
      crc = (crc & 1) ? (0xEDB88320 ^ (crc >>> 1)) : (crc >>> 1);
    }
    crcTable[i] = crc;
  }
  
  let crc = 0 ^ (-1);
  const bytes = new Uint8Array(buffer);
  for (let i = 0; i < bytes.length; i++) {
    crc = (crc >>> 8) ^ crcTable[(crc ^ bytes[i]) & 0xFF];
  }
  return ((crc ^ (-1)) >>> 0).toString(16).padStart(8, '0');
}

// 视频专用图片上传功能(基于 images.ts 的 uploadImageFromUrl)
async function uploadImageForVideo(imageUrl: string, refreshToken: string): Promise<string> {
  try {
    logger.info(`开始上传视频图片: ${imageUrl}`);
    
    // 第一步:获取上传令牌
    const tokenResult = await request("post", "/mweb/v1/get_upload_token", refreshToken, {
      data: {
        scene: 2, // AIGC 图片上传场景
      },
    });
    
    const { access_key_id, secret_access_key, session_token, service_id } = tokenResult;
    if (!access_key_id || !secret_access_key || !session_token) {
      throw new Error("获取上传令牌失败");
    }
    
    const actualServiceId = service_id || "tb4s082cfz";
    logger.info(`获取上传令牌成功: service_id=${actualServiceId}`);
    
    // 下载图片数据
    const imageResponse = await fetch(imageUrl);
    if (!imageResponse.ok) {
      throw new Error(`下载图片失败: ${imageResponse.status}`);
    }
    
    const imageBuffer = await imageResponse.arrayBuffer();
    const fileSize = imageBuffer.byteLength;
    const crc32 = calculateCRC32(imageBuffer);
    
    logger.info(`图片下载完成: 大小=${fileSize}字节, CRC32=${crc32}`);
    
    // 第二步:申请图片上传权限
    const now = new Date();
    const timestamp = now.toISOString().replace(/[:\-]/g, '').replace(/\.\d{3}Z$/, 'Z');
    
    const randomStr = Math.random().toString(36).substring(2, 12);
    const applyUrl = `https://imagex.bytedanceapi.com/?Action=ApplyImageUpload&Version=2018-08-01&ServiceId=${actualServiceId}&FileSize=${fileSize}&s=${randomStr}`;
    
    const requestHeaders = {
      'x-amz-date': timestamp,
      'x-amz-security-token': session_token
    };
    
    const authorization = createSignature('GET', applyUrl, requestHeaders, access_key_id, secret_access_key, session_token);
    
    logger.info(`申请上传权限: ${applyUrl}`);
    
    const applyResponse = await fetch(applyUrl, {
      method: 'GET',
      headers: {
        'accept': '*/*',
        'accept-language': provider.acceptLanguage,
        'authorization': authorization,
        'origin': provider.origin,
        'referer': provider.generateVideoReferer,
        'sec-ch-ua': '"Not A(Brand";v="8", "Chromium";v="132", "Google Chrome";v="132"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'cross-site',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
        'x-amz-date': timestamp,
        'x-amz-security-token': session_token,
      },
    });
    
    if (!applyResponse.ok) {
      const errorText = await applyResponse.text();
      throw new Error(`申请上传权限失败: ${applyResponse.status} - ${errorText}`);
    }
    
    const applyResult = await applyResponse.json();
    
    if (applyResult?.ResponseMetadata?.Error) {
      throw new Error(`申请上传权限失败: ${JSON.stringify(applyResult.ResponseMetadata.Error)}`);
    }
    
    logger.info(`申请上传权限成功`);
    
    // 解析上传信息
    const uploadAddress = applyResult?.Result?.UploadAddress;
    if (!uploadAddress || !uploadAddress.StoreInfos || !uploadAddress.UploadHosts) {
      throw new Error(`获取上传地址失败: ${JSON.stringify(applyResult)}`);
    }
    
    const storeInfo = uploadAddress.StoreInfos[0];
    const uploadHost = uploadAddress.UploadHosts[0];
    const auth = storeInfo.Auth;
    
    const uploadUrl = `https://${uploadHost}/upload/v1/${storeInfo.StoreUri}`;
    const imageId = storeInfo.StoreUri.split('/').pop();
    
    logger.info(`准备上传图片: imageId=${imageId}, uploadUrl=${uploadUrl}`);
    
    // 第三步:上传图片文件
    const uploadResponse = await fetch(uploadUrl, {
      method: 'POST',
      headers: {
        'Accept': '*/*',
        'Accept-Language': provider.acceptLanguage,
        'Authorization': auth,
        'Connection': 'keep-alive',
        'Content-CRC32': crc32,
        'Content-Disposition': 'attachment; filename="undefined"',
        'Content-Type': 'application/octet-stream',
        'Origin': provider.origin,
        'Referer': provider.generateVideoReferer,
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
        'X-Storage-U': '704135154117550',
      },
      body: imageBuffer,
    });
    
    if (!uploadResponse.ok) {
      const errorText = await uploadResponse.text();
      throw new Error(`图片上传失败: ${uploadResponse.status} - ${errorText}`);
    }
    
    logger.info(`图片文件上传成功`);
    
    // 第四步:提交上传
    const commitUrl = `https://imagex.bytedanceapi.com/?Action=CommitImageUpload&Version=2018-08-01&ServiceId=${actualServiceId}`;
    
    const commitTimestamp = new Date().toISOString().replace(/[:\-]/g, '').replace(/\.\d{3}Z$/, 'Z');
    const commitPayload = JSON.stringify({
      SessionKey: uploadAddress.SessionKey,
      SuccessActionStatus: "200"
    });
    
    const payloadHash = crypto.createHash('sha256').update(commitPayload, 'utf8').digest('hex');
    
    const commitRequestHeaders = {
      'x-amz-date': commitTimestamp,
      'x-amz-security-token': session_token,
      'x-amz-content-sha256': payloadHash
    };
    
    const commitAuthorization = createSignature('POST', commitUrl, commitRequestHeaders, access_key_id, secret_access_key, session_token, commitPayload);
    
    const commitResponse = await fetch(commitUrl, {
      method: 'POST',
      headers: {
        'accept': '*/*',
        'accept-language': provider.acceptLanguage,
        'authorization': commitAuthorization,
        'content-type': 'application/json',
        'origin': provider.origin,
        'referer': provider.generateVideoReferer,
        'sec-ch-ua': '"Not A(Brand";v="8", "Chromium";v="132", "Google Chrome";v="132"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'cross-site',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
        'x-amz-date': commitTimestamp,
        'x-amz-security-token': session_token,
        'x-amz-content-sha256': payloadHash,
      },
      body: commitPayload,
    });
    
    if (!commitResponse.ok) {
      const errorText = await commitResponse.text();
      throw new Error(`提交上传失败: ${commitResponse.status} - ${errorText}`);
    }
    
    const commitResult = await commitResponse.json();
    
    if (commitResult?.ResponseMetadata?.Error) {
      throw new Error(`提交上传失败: ${JSON.stringify(commitResult.ResponseMetadata.Error)}`);
    }
    
    if (!commitResult?.Result?.Results || commitResult.Result.Results.length === 0) {
      throw new Error(`提交上传响应缺少结果: ${JSON.stringify(commitResult)}`);
    }
    
    const uploadResult = commitResult.Result.Results[0];
    if (uploadResult.UriStatus !== 2000) {
      throw new Error(`图片上传状态异常: UriStatus=${uploadResult.UriStatus}`);
    }
    
    const fullImageUri = uploadResult.Uri;
    
    // 验证图片信息
    const pluginResult = commitResult.Result?.PluginResult?.[0];
    if (pluginResult && pluginResult.ImageUri) {
      logger.info(`视频图片上传完成: ${pluginResult.ImageUri}`);
      return pluginResult.ImageUri;
    }

    logger.info(`视频图片上传完成: ${fullImageUri}`);
    return fullImageUri;

  } catch (error) {
    logger.error(`视频图片上传失败: ${error.message}`);
    throw error;
  }
}

// 从Buffer上传视频图片
async function uploadImageBufferForVideo(buffer: Buffer, refreshToken: string): Promise<string> {
  try {
    logger.info(`开始从Buffer上传视频图片,大小: ${buffer.length}字节`);

    // 第一步:获取上传令牌
    const tokenResult = await request("post", "/mweb/v1/get_upload_token", refreshToken, {
      data: {
        scene: 2,
      },
    });

    const { access_key_id, secret_access_key, session_token, service_id } = tokenResult;
    if (!access_key_id || !secret_access_key || !session_token) {
      throw new Error("获取上传令牌失败");
    }

    const actualServiceId = service_id || "tb4s082cfz";
    logger.info(`获取上传令牌成功: service_id=${actualServiceId}`);

    const fileSize = buffer.length;
    const crc32 = calculateCRC32(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));

    logger.info(`Buffer大小: ${fileSize}字节, CRC32=${crc32}`);

    // 第二步:申请图片上传权限
    const now = new Date();
    const timestamp = now.toISOString().replace(/[:\-]/g, '').replace(/\.\d{3}Z$/, 'Z');

    const randomStr = Math.random().toString(36).substring(2, 12);
    const applyUrl = `https://imagex.bytedanceapi.com/?Action=ApplyImageUpload&Version=2018-08-01&ServiceId=${actualServiceId}&FileSize=${fileSize}&s=${randomStr}`;

    const requestHeaders = {
      'x-amz-date': timestamp,
      'x-amz-security-token': session_token
    };

    const authorization = createSignature('GET', applyUrl, requestHeaders, access_key_id, secret_access_key, session_token);

    const applyResponse = await fetch(applyUrl, {
      method: 'GET',
      headers: {
        'accept': '*/*',
        'accept-language': provider.acceptLanguage,
        'authorization': authorization,
        'origin': provider.origin,
        'referer': provider.generateVideoReferer,
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
        'x-amz-date': timestamp,
        'x-amz-security-token': session_token,
      },
    });

    if (!applyResponse.ok) {
      const errorText = await applyResponse.text();
      throw new Error(`申请上传权限失败: ${applyResponse.status} - ${errorText}`);
    }

    const applyResult = await applyResponse.json();

    if (applyResult?.ResponseMetadata?.Error) {
      throw new Error(`申请上传权限失败: ${JSON.stringify(applyResult.ResponseMetadata.Error)}`);
    }

    const uploadAddress = applyResult?.Result?.UploadAddress;
    if (!uploadAddress || !uploadAddress.StoreInfos || !uploadAddress.UploadHosts) {
      throw new Error(`获取上传地址失败: ${JSON.stringify(applyResult)}`);
    }

    const storeInfo = uploadAddress.StoreInfos[0];
    const uploadHost = uploadAddress.UploadHosts[0];
    const auth = storeInfo.Auth;

    const uploadUrl = `https://${uploadHost}/upload/v1/${storeInfo.StoreUri}`;

    // 第三步:上传图片文件
    const uploadResponse = await fetch(uploadUrl, {
      method: 'POST',
      headers: {
        'Accept': '*/*',
        'Authorization': auth,
        'Content-CRC32': crc32,
        'Content-Disposition': 'attachment; filename="undefined"',
        'Content-Type': 'application/octet-stream',
        'Origin': provider.origin,
        'Referer': provider.generateVideoReferer,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
      },
      body: buffer,
    });

    if (!uploadResponse.ok) {
      const errorText = await uploadResponse.text();
      throw new Error(`图片上传失败: ${uploadResponse.status} - ${errorText}`);
    }

    logger.info(`Buffer图片文件上传成功`);

    // 第四步:提交上传
    const commitUrl = `https://imagex.bytedanceapi.com/?Action=CommitImageUpload&Version=2018-08-01&ServiceId=${actualServiceId}`;

    const commitTimestamp = new Date().toISOString().replace(/[:\-]/g, '').replace(/\.\d{3}Z$/, 'Z');
    const commitPayload = JSON.stringify({
      SessionKey: uploadAddress.SessionKey,
      SuccessActionStatus: "200"
    });

    const payloadHash = crypto.createHash('sha256').update(commitPayload, 'utf8').digest('hex');

    const commitRequestHeaders = {
      'x-amz-date': commitTimestamp,
      'x-amz-security-token': session_token,
      'x-amz-content-sha256': payloadHash
    };

    const commitAuthorization = createSignature('POST', commitUrl, commitRequestHeaders, access_key_id, secret_access_key, session_token, commitPayload);

    const commitResponse = await fetch(commitUrl, {
      method: 'POST',
      headers: {
        'accept': '*/*',
        'authorization': commitAuthorization,
        'content-type': 'application/json',
        'origin': provider.origin,
        'referer': provider.generateVideoReferer,
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
        'x-amz-date': commitTimestamp,
        'x-amz-security-token': session_token,
        'x-amz-content-sha256': payloadHash,
      },
      body: commitPayload,
    });

    if (!commitResponse.ok) {
      const errorText = await commitResponse.text();
      throw new Error(`提交上传失败: ${commitResponse.status} - ${errorText}`);
    }

    const commitResult = await commitResponse.json();

    if (commitResult?.ResponseMetadata?.Error) {
      throw new Error(`提交上传失败: ${JSON.stringify(commitResult.ResponseMetadata.Error)}`);
    }

    if (!commitResult?.Result?.Results || commitResult.Result.Results.length === 0) {
      throw new Error(`提交上传响应缺少结果: ${JSON.stringify(commitResult)}`);
    }

    const uploadResult = commitResult.Result.Results[0];
    if (uploadResult.UriStatus !== 2000) {
      throw new Error(`图片上传状态异常: UriStatus=${uploadResult.UriStatus}`);
    }

    const fullImageUri = uploadResult.Uri;

    const pluginResult = commitResult.Result?.PluginResult?.[0];
    if (pluginResult && pluginResult.ImageUri) {
      logger.info(`Buffer视频图片上传完成: ${pluginResult.ImageUri}`);
      return pluginResult.ImageUri;
    }

    logger.info(`Buffer视频图片上传完成: ${fullImageUri}`);
    return fullImageUri;

  } catch (error) {
    logger.error(`Buffer视频图片上传失败: ${error.message}`);
    throw error;
  }
}

/**
 * 解析音频文件时长(毫秒)
 * 支持 WAV 格式精确解析,其他格式按 128kbps 估算
 */
function parseAudioDuration(buffer: Buffer): number {
  try {
    // WAV: RIFF header check
    if (buffer.length >= 44 &&
        buffer[0] === 0x52 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x46 &&
        buffer[8] === 0x57 && buffer[9] === 0x41 && buffer[10] === 0x56 && buffer[11] === 0x45) {
      const byteRate = buffer.readUInt32LE(28);
      if (byteRate > 0) {
        // 查找 data chunk 获取精确大小
        let offset = 12;
        while (offset < buffer.length - 8) {
          const chunkId = buffer.toString('ascii', offset, offset + 4);
          const chunkSize = buffer.readUInt32LE(offset + 4);
          if (chunkId === 'data') {
            return Math.round(chunkSize / byteRate * 1000);
          }
          offset += 8 + chunkSize;
        }
        // 兜底:用文件大小估算
        return Math.round((buffer.length - 44) / byteRate * 1000);
      }
    }
    // 非 WAV:按 128kbps 估算
    return Math.round(buffer.length / (128 * 1000 / 8) * 1000);
  } catch {
    return 0;
  }
}

/**
 * 上传视频/音频文件
 * 通过 ByteDance VOD (视频点播) API 上传
 * 流程: get_upload_token(scene=1) → ApplyUploadInner → Upload → CommitUploadInner
 *
 * @param buffer 文件 Buffer
 * @param mediaType "video" 或 "audio"
 * @param refreshToken 刷新令牌
 * @param filename 原始文件名(可选)
 * @returns { vid, width?, height?, duration?, fps? }
 */
async function uploadMediaForVideo(
  buffer: Buffer,
  mediaType: "video" | "audio",
  refreshToken: string,
  filename?: string
): Promise<{ vid: string; width?: number; height?: number; duration?: number; fps?: number }> {
  const label = mediaType === "audio" ? "音频" : "视频";
  const fileSize = buffer.length;
  logger.info(`开始上传${label}文件,大小: ${fileSize} 字节`);

  // 第一步:获取 VOD 上传令牌(scene=1)
  const tokenResult = await request("post", "/mweb/v1/get_upload_token", refreshToken, {
    data: { scene: 1 },
  });

  const { access_key_id, secret_access_key, session_token, space_name } = tokenResult;
  if (!access_key_id || !secret_access_key || !session_token) {
    throw new Error(`获取${label}上传令牌失败`);
  }

  const spaceName = space_name || "dreamina";
  logger.info(`获取${label}上传令牌成功: spaceName=${spaceName}`);

  // 第二步:申请 VOD 上传权限(ApplyUploadInner)
  const now = new Date();
  const timestamp = now.toISOString().replace(/[:\-]/g, '').replace(/\.\d{3}Z$/, 'Z');
  const randomStr = Math.random().toString(36).substring(2, 12);

  const vodHost = "https://vod.bytedanceapi.com";
  const applyUrl = `${vodHost}/?Action=ApplyUploadInner&Version=2020-11-19&SpaceName=${spaceName}&FileType=video&IsInner=1&FileSize=${fileSize}&s=${randomStr}`;

  const requestHeaders: Record<string, string> = {
    'x-amz-date': timestamp,
    'x-amz-security-token': session_token,
  };

  const authorization = createSignature(
    'GET', applyUrl, requestHeaders,
    access_key_id, secret_access_key, session_token,
    '', provider.vodAwsRegion, 'vod'
  );

  logger.info(`申请${label}上传权限: ${applyUrl}`);

  const applyResponse = await fetch(applyUrl, {
    method: 'GET',
    headers: {
      'accept': '*/*',
      'accept-language': provider.acceptLanguage,
      'authorization': authorization,
      'origin': provider.origin,
      'referer': provider.generateVideoReferer,
      'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
      'x-amz-date': timestamp,
      'x-amz-security-token': session_token,
    },
  });

  if (!applyResponse.ok) {
    const errorText = await applyResponse.text();
    throw new Error(`申请${label}上传权限失败: ${applyResponse.status} - ${errorText}`);
  }

  const applyResult: any = await applyResponse.json();
  if (applyResult?.ResponseMetadata?.Error) {
    throw new Error(`申请${label}上传权限失败: ${JSON.stringify(applyResult.ResponseMetadata.Error)}`);
  }

  const uploadNodes = applyResult?.Result?.InnerUploadAddress?.UploadNodes;
  if (!uploadNodes || uploadNodes.length === 0) {
    throw new Error(`获取${label}上传节点失败: ${JSON.stringify(applyResult)}`);
  }

  const uploadNode = uploadNodes[0];
  const storeInfo = uploadNode.StoreInfos?.[0];
  if (!storeInfo) {
    throw new Error(`获取${label}上传存储信息失败: ${JSON.stringify(uploadNode)}`);
  }

  const uploadHost = uploadNode.UploadHost;
  const storeUri = storeInfo.StoreUri;
  const auth = storeInfo.Auth;
  const sessionKey = uploadNode.SessionKey;
  const vid = uploadNode.Vid;

  logger.info(`获取${label}上传节点成功: host=${uploadHost}, vid=${vid}`);

  // 第三步:上传文件
  const uploadUrl = `https://${uploadHost}/upload/v1/${storeUri}`;
  const crc32 = calculateCRC32(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));

  logger.info(`开始上传${label}文件: ${uploadUrl}, CRC32=${crc32}`);

  const uploadResponse = await fetch(uploadUrl, {
    method: 'POST',
    headers: {
      'Accept': '*/*',
      'Authorization': auth,
      'Content-CRC32': crc32,
      'Content-Type': 'application/octet-stream',
      'Origin': provider.origin,
      'Referer': provider.generateVideoReferer,
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
    },
    body: buffer,
  });

  if (!uploadResponse.ok) {
    const errorText = await uploadResponse.text();
    throw new Error(`${label}文件上传失败: ${uploadResponse.status} - ${errorText}`);
  }

  const uploadData: any = await uploadResponse.json();
  if (uploadData?.code !== 2000) {
    throw new Error(`${label}文件上传失败: code=${uploadData?.code}, message=${uploadData?.message}`);
  }

  logger.info(`${label}文件上传成功: crc32=${uploadData.data?.crc32}`);

  // 第四步:确认上传(CommitUploadInner)
  const commitUrl = `${vodHost}/?Action=CommitUploadInner&Version=2020-11-19&SpaceName=${spaceName}`;
  const commitTimestamp = new Date().toISOString().replace(/[:\-]/g, '').replace(/\.\d{3}Z$/, 'Z');
  const commitPayload = JSON.stringify({
    SessionKey: sessionKey,
    Functions: [],
  });

  const payloadHash = crypto.createHash('sha256').update(commitPayload, 'utf8').digest('hex');

  const commitRequestHeaders: Record<string, string> = {
    'x-amz-date': commitTimestamp,
    'x-amz-security-token': session_token,
    'x-amz-content-sha256': payloadHash,
  };

  const commitAuthorization = createSignature(
    'POST', commitUrl, commitRequestHeaders,
    access_key_id, secret_access_key, session_token,
    commitPayload, provider.vodAwsRegion, 'vod'
  );

  logger.info(`提交${label}上传确认: ${commitUrl}`);

  const commitResponse = await fetch(commitUrl, {
    method: 'POST',
    headers: {
      'accept': '*/*',
      'authorization': commitAuthorization,
      'content-type': 'application/json',
      'origin': provider.origin,
      'referer': provider.generateVideoReferer,
      'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
      'x-amz-date': commitTimestamp,
      'x-amz-security-token': session_token,
      'x-amz-content-sha256': payloadHash,
    },
    body: commitPayload,
  });

  if (!commitResponse.ok) {
    const errorText = await commitResponse.text();
    throw new Error(`提交${label}上传失败: ${commitResponse.status} - ${errorText}`);
  }

  const commitResult: any = await commitResponse.json();
  if (commitResult?.ResponseMetadata?.Error) {
    throw new Error(`提交${label}上传失败: ${JSON.stringify(commitResult.ResponseMetadata.Error)}`);
  }

  if (!commitResult?.Result?.Results || commitResult.Result.Results.length === 0) {
    throw new Error(`提交${label}上传响应缺少结果: ${JSON.stringify(commitResult)}`);
  }

  const result = commitResult.Result.Results[0];
  if (!result.Vid) {
    throw new Error(`提交${label}上传响应缺少 Vid: ${JSON.stringify(result)}`);
  }

  // 从 VOD 返回的元数据中获取信息(音频有 Duration)
  const videoMeta = result.VideoMeta || {};
  let duration = videoMeta.Duration ? Math.round(videoMeta.Duration * 1000) : 0;

  // 如果 VOD 未返回时长,用本地解析兜底
  if (duration <= 0 && mediaType === "audio") {
    duration = parseAudioDuration(buffer);
    logger.info(`VOD 未返回${label}时长,本地解析: ${duration}ms`);
  }

  logger.info(`${label}上传完成: vid=${result.Vid}, duration=${duration}ms`);

  return {
    vid: result.Vid,
    width: videoMeta.Width || 0,
    height: videoMeta.Height || 0,
    duration,
    fps: videoMeta.Fps || 0,
  };
}

/**
 * 通过 get_local_item_list API 获取高质量视频下载URL
 * 浏览器下载视频时使用此API获取高码率版本(~6297 vs 预览版 ~1152)
 *
 * @param itemId 视频项目ID
 * @param refreshToken 刷新令牌
 * @returns 高质量视频URL,失败时返回 null
 */
async function fetchHighQualityVideoUrl(itemId: string, refreshToken: string): Promise<string | null> {
  try {
    logger.info(`尝试获取高质量视频下载URL,item_id: ${itemId}`);

    const result = await request("post", "/mweb/v1/get_local_item_list", refreshToken, {
      data: {
        item_id_list: [itemId],
        pack_item_opt: {
          scene: 1,
          need_data_integrity: true,
        },
        is_for_video_download: true,
      },
    });

    const responseStr = JSON.stringify(result);
    logger.info(`get_local_item_list 响应大小: ${responseStr.length} 字符`);

    // 策略1: 从结构化字段中提取视频URL
    const itemList = result.item_list || result.local_item_list || [];
    if (itemList.length > 0) {
      const item = itemList[0];
      const videoUrl =
        item?.video?.transcoded_video?.origin?.video_url ||
        item?.video?.download_url ||
        item?.video?.play_url ||
        item?.video?.url;

      if (videoUrl) {
        logger.info(`从get_local_item_list结构化字段获取到高清视频URL: ${videoUrl}`);
        return videoUrl;
      }
    }

    // 策略2: 正则匹配 dreamnia.jimeng.com 高质量URL
    const hqUrlMatch = responseStr.match(/https:\/\/v[0-9]+-dreamnia\.jimeng\.com\/[^"\s\\]+/);
    if (hqUrlMatch && hqUrlMatch[0]) {
      logger.info(`正则提取到高质量视频URL (dreamnia): ${hqUrlMatch[0]}`);
      return hqUrlMatch[0];
    }

    // 策略3: 匹配任何 jimeng.com 域名的视频URL
    const jimengUrlMatch = responseStr.match(/https:\/\/v[0-9]+-[^"\\]*\.jimeng\.com\/[^"\s\\]+/);
    if (jimengUrlMatch && jimengUrlMatch[0]) {
      logger.info(`正则提取到jimeng视频URL: ${jimengUrlMatch[0]}`);
      return jimengUrlMatch[0];
    }

    // 策略4: 匹配任何视频URL(兜底)
    const anyVideoUrlMatch = responseStr.match(/https:\/\/v[0-9]+-[^"\\]*\.(vlabvod|jimeng)\.com\/[^"\s\\]+/);
    if (anyVideoUrlMatch && anyVideoUrlMatch[0]) {
      logger.info(`从get_local_item_list提取到视频URL: ${anyVideoUrlMatch[0]}`);
      return anyVideoUrlMatch[0];
    }

    logger.warn(`未能从get_local_item_list响应中提取到视频URL`);
    return null;
  } catch (error) {
    logger.warn(`获取高质量视频下载URL失败: ${error.message}`);
    return null;
  }
}

/**
 * 生成视频
 *
 * @param _model 模型名称
 * @param prompt 提示词
 * @param options 选项
 * @param refreshToken 刷新令牌
 * @returns 视频URL
 */
export async function generateVideo(
  _model: string,
  prompt: string,
  {
    ratio = "1:1",
    resolution = "720p",
    duration = 5,
    filePaths = [],
    files = [],
  }: {
    ratio?: string;
    resolution?: string;
    duration?: number;
    filePaths?: string[];
    files?: any[];
  },
  refreshToken: string
) {
  const model = getModel(_model);

  // 解析分辨率参数获取实际的宽高
  const { width, height } = resolveVideoResolution(resolution, ratio);

  logger.info(`使用模型: ${_model} 映射模型: ${model} ${width}x${height} (${ratio}@${resolution}) 时长: ${duration}秒`);

  // 检查积分
  const { totalCredit } = await getCredit(refreshToken);
  if (totalCredit <= 0)
    await receiveCredit(refreshToken);

  // 处理首帧和尾帧图片
  let first_frame_image = undefined;
  let end_frame_image = undefined;

  // 处理上传的文件(multipart/form-data)
  if (files && files.length > 0) {
    let uploadIDs: string[] = [];
    logger.info(`开始处理 ${files.length} 个上传文件用于视频生成`);

    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      if (!file || !file.filepath) {
        logger.warn(`第 ${i + 1} 个文件无效,跳过`);
        continue;
      }

      try {
        logger.info(`开始上传第 ${i + 1} 个文件: ${file.originalFilename || file.filepath}`);

        // 读取文件内容并上传
        const buffer = fs.readFileSync(file.filepath);
        const imageUri = await uploadImageBufferForVideo(buffer, refreshToken);

        if (imageUri) {
          uploadIDs.push(imageUri);
          logger.info(`第 ${i + 1} 个文件上传成功: ${imageUri}`);
        } else {
          logger.error(`第 ${i + 1} 个文件上传失败: 未获取到 image_uri`);
        }
      } catch (error) {
        logger.error(`第 ${i + 1} 个文件上传失败: ${error.message}`);

        if (i === 0) {
          logger.error(`首帧文件上传失败,停止视频生成以避免浪费积分`);
          throw new APIException(EX.API_REQUEST_FAILED, `首帧文件上传失败: ${error.message}`);
        } else {
          logger.warn(`第 ${i + 1} 个文件上传失败,将跳过此文件继续处理`);
        }
      }
    }

    logger.info(`文件上传完成,成功上传 ${uploadIDs.length} 个文件`);

    if (uploadIDs.length === 0) {
      logger.error(`所有文件上传失败,停止视频生成以避免浪费积分`);
      throw new APIException(EX.API_REQUEST_FAILED, '所有文件上传失败,请检查文件是否有效');
    }

    // 构建首帧图片对象
    if (uploadIDs[0]) {
      first_frame_image = {
        format: "",
        height: height,
        id: util.uuid(),
        image_uri: uploadIDs[0],
        name: "",
        platform_type: 1,
        source_from: "upload",
        type: "image",
        uri: uploadIDs[0],
        width: width,
      };
      logger.info(`设置首帧图片: ${uploadIDs[0]}`);
    }

    // 构建尾帧图片对象
    if (uploadIDs[1]) {
      end_frame_image = {
        format: "",
        height: height,
        id: util.uuid(),
        image_uri: uploadIDs[1],
        name: "",
        platform_type: 1,
        source_from: "upload",
        type: "image",
        uri: uploadIDs[1],
        width: width,
      };
      logger.info(`设置尾帧图片: ${uploadIDs[1]}`);
    }
  } else if (filePaths && filePaths.length > 0) {
    let uploadIDs: string[] = [];
    logger.info(`开始上传 ${filePaths.length} 张图片用于视频生成`);
    
    for (let i = 0; i < filePaths.length; i++) {
      const filePath = filePaths[i];
      if (!filePath) {
        logger.warn(`第 ${i + 1} 张图片路径为空,跳过`);
        continue;
      }
      
      try {
        logger.info(`开始上传第 ${i + 1} 张图片: ${filePath}`);
        
        // 使用Amazon S3上传方式
        const imageUri = await uploadImageForVideo(filePath, refreshToken);
        
        if (imageUri) {
          uploadIDs.push(imageUri);
          logger.info(`第 ${i + 1} 张图片上传成功: ${imageUri}`);
        } else {
          logger.error(`第 ${i + 1} 张图片上传失败: 未获取到 image_uri`);
        }
      } catch (error) {
        logger.error(`第 ${i + 1} 张图片上传失败: ${error.message}`);
        
        // 图片上传失败时,停止视频生成避免浪费积分
        if (i === 0) {
          logger.error(`首帧图片上传失败,停止视频生成以避免浪费积分`);
          throw new APIException(EX.API_REQUEST_FAILED, `首帧图片上传失败: ${error.message}`);
        } else {
          logger.warn(`第 ${i + 1} 张图片上传失败,将跳过此图片继续处理`);
        }
      }
    }
    
    logger.info(`图片上传完成,成功上传 ${uploadIDs.length} 张图片`);
    
    // 如果没有成功上传任何图片,停止视频生成
    if (uploadIDs.length === 0) {
      logger.error(`所有图片上传失败,停止视频生成以避免浪费积分`);
      throw new APIException(EX.API_REQUEST_FAILED, '所有图片上传失败,请检查图片URL是否有效');
    }
    
    // 构建首帧图片对象
    if (uploadIDs[0]) {
      first_frame_image = {
        format: "",
        height: height,
        id: util.uuid(),
        image_uri: uploadIDs[0],
        name: "",
        platform_type: 1,
        source_from: "upload",
        type: "image",
        uri: uploadIDs[0],
        width: width,
      };
      logger.info(`设置首帧图片: ${uploadIDs[0]}`);
    }
    
    // 构建尾帧图片对象
    if (uploadIDs[1]) {
      end_frame_image = {
        format: "",
        height: height,
        id: util.uuid(),
        image_uri: uploadIDs[1],
        name: "",
        platform_type: 1,
        source_from: "upload",
        type: "image",
        uri: uploadIDs[1],
        width: width,
      };
      logger.info(`设置尾帧图片: ${uploadIDs[1]}`);
    } else if (filePaths.length > 1) {
      logger.warn(`第二张图片上传失败或未提供,将仅使用首帧图片`);
    }
  } else {
    logger.info(`未提供图片文件,将进行纯文本视频生成`);
  }

  const componentId = util.uuid();
  const submitId = util.uuid();
  const metricsExtra = provider.name === "dreamina-intl"
    ? JSON.stringify({
        promptSource: "custom",
        isDefaultSeed: 1,
        originSubmitId: submitId,
        isRegenerate: false,
        enterFrom: "click",
        position: "page_bottom_box",
        functionMode: end_frame_image ? "first_last_frames" : "text_to_video",
        sceneOptions: JSON.stringify([
          {
            type: "video",
            scene: "BasicVideoGenerateButton",
            resolution,
            modelReqKey: model,
            videoDuration: duration,
            reportParams: {
              enterSource: "generate",
              vipSource: "generate",
              extraVipFunctionKey: `${model}-${resolution}`,
              useVipFunctionDetailsReporterHoc: true,
            },
            materialTypes: [],
          },
        ]),
      })
    : JSON.stringify({
        enterFrom: "click",
        isDefaultSeed: 1,
        promptSource: "custom",
        isRegenerate: false,
        originSubmitId: util.uuid(),
      });
  
  // 获取当前模型的 draft 版本
  const draftVersion = MODEL_DRAFT_VERSIONS[_model] || DEFAULT_DRAFT_VERSION;
  
  // 计算视频宽高比
  const gcd = (a: number, b: number): number => b === 0 ? a : gcd(b, a % b);
  const divisor = gcd(width, height);
  const aspectRatio = `${width / divisor}:${height / divisor}`;
  
  const requestBody = {
        "extend": {
          "root_model": end_frame_image ? MODEL_MAP['jimeng-video-3.0'] : model,
          ...(provider.name === "dreamina-intl"
            ? {
                m_video_commerce_info: {
                  benefit_type: "basic_video_operation_vgfm_v_three",
                  resource_id: "generate_video",
                  resource_id_type: "str",
                  resource_sub_type: "aigc"
                },
                workspace_id: 0,
                m_video_commerce_info_list: [{
                  benefit_type: "basic_video_operation_vgfm_v_three",
                  resource_id: "generate_video",
                  resource_id_type: "str",
                  resource_sub_type: "aigc"
                }]
              }
            : {
                m_video_commerce_info: {
                  benefit_type: "basic_video_operation_vgfm_v_three",
                  resource_id: "generate_video",
                  resource_id_type: "str",
                  resource_sub_type: "aigc"
                },
                m_video_commerce_info_list: [{
                  benefit_type: "basic_video_operation_vgfm_v_three",
                  resource_id: "generate_video",
                  resource_id_type: "str",
                  resource_sub_type: "aigc"
                }]
              }),
        },
        "submit_id": submitId,
        "metrics_extra": metricsExtra,
        "draft_content": JSON.stringify({
          "type": "draft",
          "id": util.uuid(),
          "min_version": "3.0.5",
          "is_from_tsn": true,
          "version": draftVersion,
          "main_component_id": componentId,
          "component_list": [{
            "type": "video_base_component",
            "id": componentId,
            "min_version": "1.0.0",
            "metadata": {
              "type": "",
              "id": util.uuid(),
              "created_platform": 3,
              "created_platform_version": "",
              "created_time_in_ms": Date.now(),
              "created_did": ""
            },
            "generate_type": "gen_video",
            "aigc_mode": "workbench",
            "abilities": {
              "type": "",
              "id": util.uuid(),
              "gen_video": {
                "id": util.uuid(),
                "type": "",
                "text_to_video_params": {
                  "type": "",
                  "id": util.uuid(),
                  "model_req_key": model,
                  "priority": 0,
                  "seed": Math.floor(Math.random() * 100000000) + 2500000000,
                  "video_aspect_ratio": aspectRatio,
                  "video_gen_inputs": [{
                    duration_ms: duration * 1000,
                    first_frame_image: first_frame_image,
                    end_frame_image: end_frame_image,
                    fps: 24,
                    id: util.uuid(),
                    min_version: "3.0.5",
                    prompt: prompt,
                    resolution: resolution,
                    type: "",
                    video_mode: 2
                  }]
                },
                "video_task_extra": metricsExtra,
              }
            }
          }],
        }),
        http_common_info: {
          aid: DEFAULT_ASSISTANT_ID,
        },
      };

  const deviceTime = util.unixTimestamp();
  const generateResult = provider.name === "dreamina-intl"
    ? await browserService.fetch(
        refreshToken,
        `${provider.mwebApiBaseUrl}${provider.videoGeneratePath}?${new URLSearchParams({
          aid: String(provider.assistantId),
          device_platform: "web",
          region: provider.region,
          da_version: "3.3.12",
          os: "windows",
          web_component_open_flag: "1",
          commerce_with_input_video: "1",
          web_version: "7.5.0",
          aigc_features: "app_lip_sync",
          msToken: provider.extraCookies?.msToken || "",
        }).toString()}`,
        {
          method: "POST",
          headers: {
            appid: String(provider.assistantId),
            "app-sdk-version": "48.0.0",
            appvr: "8.4.0",
            "device-time": String(deviceTime),
            sign: createRequestSign(provider.videoGeneratePath, deviceTime),
            "sign-ver": "1",
            loc: provider.loc,
            lan: provider.lan,
            pf: "7",
            tdid: "",
            did: "7623012616730936850",
            "store-country-code": provider.storeRegionValue,
            "store-country-code-src": provider.storeRegionSrcValue,
            Accept: "application/json, text/plain, */*",
            Referer: `${provider.pageBaseUrl}/`,
            "accept-language": provider.acceptLanguage,
          },
          body: JSON.stringify(requestBody),
        }
      )
    : await request(
        "post",
        provider.videoGeneratePath,
        refreshToken,
        {
          params: {
            aigc_features: "app_lip_sync",
            web_version: "6.6.0",
            da_version: draftVersion,
          },
          data: requestBody,
        }
      );
  const aigc_data = generateResult?.aigc_data || generateResult?.data?.aigc_data || generateResult?.data || generateResult;
  logger.info(`国际版视频生成返回: ${JSON.stringify(generateResult).slice(0, 1500)}`);

  const historyId = aigc_data.history_record_id;
  const pollId = submitId;
  if (!historyId)
    throw new APIException(EX.API_IMAGE_GENERATION_FAILED, `记录ID不存在,国际版返回: ${JSON.stringify(generateResult).slice(0, 800)}`);

  // 轮询获取结果
  let status = 20, failCode, item_list = [];
  let retryCount = 0;
  const maxRetries = 120; // 支持较长的视频生成时间(约20分钟以上)
  
  // 首次查询前等待更长时间,让服务器有时间处理请求
  await new Promise((resolve) => setTimeout(resolve, 5000));
  
  logger.info(`开始轮询视频生成结果,历史ID: ${historyId},最大重试次数: ${maxRetries}`);
  logger.info(`上游API地址: ${provider.baseUrl}/mweb/v1/get_history_by_ids`);
  logger.info(`视频生成请求已发送,请同时在上游站点查看: ${provider.generateVideoReferer}`);
  
  while (status === 20 && retryCount < maxRetries) {
    try {
      // 构建请求URL和参数
      const requestUrl = "/mweb/v1/get_history_by_ids";
      const requestData = provider.name === "dreamina-intl"
        ? { submit_ids: [pollId] }
        : { history_ids: [historyId] };
      
      // 尝试两种不同的API请求方式
      let result;
      let useAlternativeApi = retryCount > 10 && retryCount % 2 === 0; // 在重试10次后,每隔一次尝试备用API
      
      if (useAlternativeApi) {
        // 备用API请求方式
        logger.info(`尝试备用API请求方式,URL: ${requestUrl}, 历史ID: ${historyId}, 重试次数: ${retryCount + 1}/${maxRetries}`);
        const alternativeRequestData = {
          history_record_ids: [historyId],
        };
        result = await request("post", "/mweb/v1/get_history_records", refreshToken, {
          data: alternativeRequestData,
        });
        logger.info(`备用API响应摘要: ${JSON.stringify(result).substring(0, 500)}...`);
      } else {
        // 标准API请求方式
        logger.info(`发送请求获取视频生成结果,URL: ${requestUrl}, 历史ID: ${historyId}, 重试次数: ${retryCount + 1}/${maxRetries}`);
        result = await request("post", requestUrl, refreshToken, {
          data: requestData,
        });
        const responseStr = JSON.stringify(result);
        logger.info(`标准API响应摘要: ${responseStr.substring(0, 300)}...`);
      }
      

      // 检查结果是否有效
      let historyData;
      
      if (useAlternativeApi && result.history_records && result.history_records.length > 0) {
        // 处理备用API返回的数据格式
        historyData = result.history_records[0];
        logger.info(`从备用API获取到历史记录`);
      } else if (result.history_list && result.history_list.length > 0) {
        // 处理标准API返回的数据格式
        historyData = result.history_list[0];
        logger.info(`从标准API获取到历史记录`);
      } else if (result[provider.name === "dreamina-intl" ? pollId : historyId]) {
        // get_history_by_ids 返回数据以 historyId 为键(如 result["8918159809292"])
        historyData = result[provider.name === "dreamina-intl" ? pollId : historyId];
        logger.info(`从historyId键获取到历史记录`);
      } else {
        // 所有API都没有返回有效数据
        logger.warn(`历史记录不存在,重试中 (${retryCount + 1}/${maxRetries})... 历史ID: ${historyId}`);
        logger.info(`请同时在上游站点检查视频是否已生成: ${provider.generateVideoReferer}`);

        retryCount++;
        // 增加重试间隔时间,但设置上限为30秒
        const waitTime = Math.min(2000 * (retryCount + 1), 30000);
        logger.info(`等待 ${waitTime}ms 后进行第 ${retryCount + 1} 次重试`);
        await new Promise((resolve) => setTimeout(resolve, waitTime));
        continue;
      }
      
      // 记录获取到的结果详情
      logger.info(`获取到历史记录结果: ${JSON.stringify(historyData)}`);
      

      // 从历史数据中提取状态和结果
      status = historyData.status;
      failCode = historyData.fail_code;
      item_list = historyData.item_list || [];
      
      logger.info(`视频生成状态: ${status}, 失败代码: ${failCode || '无'}, 项目列表长度: ${item_list.length}`);
      
      // 如果有视频URL,提前记录
      let tempVideoUrl = item_list?.[0]?.video?.transcoded_video?.origin?.video_url;
      if (!tempVideoUrl) {
        // 尝试从其他可能的路径获取
        tempVideoUrl = item_list?.[0]?.video?.play_url || 
                      item_list?.[0]?.video?.download_url || 
                      item_list?.[0]?.video?.url;
      }
      
      if (tempVideoUrl) {
        logger.info(`检测到视频URL: ${tempVideoUrl}`);
      }

      if (status === 30) {
        const error = failCode === 2038 
          ? new APIException(EX.API_CONTENT_FILTERED, "内容被过滤")
          : new APIException(EX.API_IMAGE_GENERATION_FAILED, `生成失败,错误码: ${failCode}`);
        // 添加历史ID到错误对象,以便在chat.ts中显示
        error.historyId = historyId;
        throw error;
      }
      
      // 如果状态仍在处理中,等待后继续
      if (status === 20) {
        const waitTime = 2000 * (Math.min(retryCount + 1, 5)); // 随着重试次数增加等待时间,但最多10秒
        logger.info(`视频生成中,状态码: ${status},等待 ${waitTime}ms 后继续查询`);
        await new Promise((resolve) => setTimeout(resolve, waitTime));
      }
    } catch (error) {
      logger.error(`轮询视频生成结果出错: ${error.message}`);
      retryCount++;
      await new Promise((resolve) => setTimeout(resolve, 2000 * (retryCount + 1)));
    }
  }
  
  // 如果达到最大重试次数仍未成功
  if (retryCount >= maxRetries && status === 20) {
    logger.error(`视频生成超时,已尝试 ${retryCount} 次,总耗时约 ${Math.floor(retryCount * 2000 / 1000 / 60)} 分钟`);
    const error = new APIException(EX.API_IMAGE_GENERATION_FAILED, "获取视频生成结果超时,请稍后在即梦官网查看您的视频");
    // 添加历史ID到错误对象,以便在chat.ts中显示
    error.historyId = historyId;
    throw error;
  }

  // 尝试通过 get_local_item_list 获取高质量视频下载URL
  const itemId = item_list?.[0]?.item_id
    || item_list?.[0]?.id
    || item_list?.[0]?.local_item_id
    || item_list?.[0]?.common_attr?.id;

  if (itemId) {
    try {
      const hqVideoUrl = await fetchHighQualityVideoUrl(String(itemId), refreshToken);
      if (hqVideoUrl) {
        logger.info(`视频生成成功(高质量),URL: ${hqVideoUrl}`);
        return hqVideoUrl;
      }
    } catch (error) {
      logger.warn(`获取高质量视频URL失败,将使用预览URL作为回退: ${error.message}`);
    }
  } else {
    logger.warn(`未能从item_list中提取item_id,将使用预览URL。item_list[0]键: ${item_list?.[0] ? Object.keys(item_list[0]).join(', ') : '无'}`);
  }

  // 回退:提取预览视频URL
  let videoUrl = item_list?.[0]?.video?.transcoded_video?.origin?.video_url;
  
  // 如果通过常规路径无法获取视频URL,尝试其他可能的路径
  if (!videoUrl) {
    // 尝试从item_list中的其他可能位置获取
    if (item_list?.[0]?.video?.play_url) {
      videoUrl = item_list[0].video.play_url;
      logger.info(`从play_url获取到视频URL: ${videoUrl}`);
    } else if (item_list?.[0]?.video?.download_url) {
      videoUrl = item_list[0].video.download_url;
      logger.info(`从download_url获取到视频URL: ${videoUrl}`);
    } else if (item_list?.[0]?.video?.url) {
      videoUrl = item_list[0].video.url;
      logger.info(`从url获取到视频URL: ${videoUrl}`);
    } else {
      // 如果仍然找不到,记录错误并抛出异常
      logger.error(`未能获取视频URL,item_list: ${JSON.stringify(item_list)}`);
      const error = new APIException(EX.API_IMAGE_GENERATION_FAILED, "未能获取视频URL,请稍后在即梦官网查看");
      // 添加历史ID到错误对象,以便在chat.ts中显示
      error.historyId = historyId;
      throw error;
    }
  }

  logger.info(`视频生成成功,URL: ${videoUrl}`);
  return videoUrl;
}

/**
 * Seedance 2.0 多图智能视频生成
 * 支持多张图片与文本混合生成视频
 *
 * @param _model 模型名称
 * @param prompt 提示词(支持 @1 @2 等引用图片占位符)
 * @param options 选项
 * @param refreshToken 刷新令牌
 * @returns 视频URL
 */
export async function generateSeedanceVideo(
  _model: string,
  prompt: string,
  {
    ratio = "4:3",
    resolution = "720p",
    duration = 4,
    filePaths = [],
    files = [],
  }: {
    ratio?: string;
    resolution?: string;
    duration?: number;
    filePaths?: string[];
    files?: any[];
  },
  refreshToken: string
) {
  const model = getModel(_model);
  const benefitType = SEEDANCE_BENEFIT_TYPE_MAP[_model] || "dreamina_video_seedance_20_pro";

  // Seedance 2.0 默认时长为4秒
  const actualDuration = duration || 4;

  // 解析分辨率参数获取实际的宽高
  const { width, height } = resolveVideoResolution(resolution, ratio);

  logger.info(`Seedance 2.0 生成: 模型=${_model} 映射=${model} ${width}x${height} (${ratio}@${resolution}) 时长=${actualDuration}秒`);

  // 检查积分
  const { totalCredit } = await getCredit(refreshToken);
  if (totalCredit <= 0)
    await receiveCredit(refreshToken);

  // 上传所有文件(支持图片/视频/音频)
  let uploadedMaterials: UploadedMaterial[] = [];

  // 处理上传的文件(multipart/form-data)
  if (files && files.length > 0) {
    logger.info(`Seedance: 开始处理 ${files.length} 个上传文件`);

    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      if (!file || !file.filepath) {
        logger.warn(`Seedance: 第 ${i + 1} 个文件无效,跳过`);
        continue;
      }

      const materialType = detectMaterialType(file);
      try {
        logger.info(`Seedance: 开始上传第 ${i + 1} 个文件 (${materialType}): ${file.originalFilename || file.filepath}`);
        const buffer = fs.readFileSync(file.filepath);

        if (materialType === "image") {
          const imageUri = await uploadImageBufferForVideo(buffer, refreshToken);
          if (imageUri) {
            uploadedMaterials.push({ type: "image", uri: imageUri, width, height });
            logger.info(`Seedance: 第 ${i + 1} 个图片上传成功: ${imageUri}`);
          }
        } else {
          // 视频或音频 → VOD 上传
          const vodResult = await uploadMediaForVideo(buffer, materialType, refreshToken, file.originalFilename);
          uploadedMaterials.push({
            type: materialType,
            vid: vodResult.vid,
            width: vodResult.width,
            height: vodResult.height,
            duration: vodResult.duration,
            fps: vodResult.fps,
            name: file.originalFilename || "",
          });
          logger.info(`Seedance: 第 ${i + 1}${materialType === "video" ? "视频" : "音频"}上传成功: ${vodResult.vid}`);
        }
      } catch (error) {
        logger.error(`Seedance: 第 ${i + 1} 个文件上传失败: ${error.message}`);
        if (i === 0) {
          throw new APIException(EX.API_REQUEST_FAILED, `首个文件上传失败: ${error.message}`);
        }
      }
    }
  } else if (filePaths && filePaths.length > 0) {
    logger.info(`Seedance: 开始上传 ${filePaths.length} 个文件`);

    for (let i = 0; i < filePaths.length; i++) {
      const filePath = filePaths[i];
      if (!filePath) continue;

      const materialType = detectMaterialTypeFromUrl(filePath);
      try {
        logger.info(`Seedance: 开始上传第 ${i + 1} 个文件 (${materialType}): ${filePath}`);

        if (materialType === "image") {
          const imageUri = await uploadImageForVideo(filePath, refreshToken);
          if (imageUri) {
            uploadedMaterials.push({ type: "image", uri: imageUri, width, height });
            logger.info(`Seedance: 第 ${i + 1} 个图片上传成功: ${imageUri}`);
          }
        } else {
          // 视频或音频 URL → 下载后 VOD 上传
          const response = await fetch(filePath);
          if (!response.ok) throw new Error(`下载文件失败: ${response.status}`);
          const arrayBuffer = await response.arrayBuffer();
          const buffer = Buffer.from(arrayBuffer);
          const vodResult = await uploadMediaForVideo(buffer, materialType, refreshToken);
          uploadedMaterials.push({
            type: materialType,
            vid: vodResult.vid,
            width: vodResult.width,
            height: vodResult.height,
            duration: vodResult.duration,
            fps: vodResult.fps,
          });
          logger.info(`Seedance: 第 ${i + 1}${materialType === "video" ? "视频" : "音频"}上传成功: ${vodResult.vid}`);
        }
      } catch (error) {
        logger.error(`Seedance: 第 ${i + 1} 个文件上传失败: ${error.message}`);
        if (i === 0) {
          throw new APIException(EX.API_REQUEST_FAILED, `首个文件上传失败: ${error.message}`);
        }
      }
    }
  }

  if (uploadedMaterials.length === 0) {
    throw new APIException(EX.API_REQUEST_FAILED, 'Seedance 2.0 需要至少一个文件(图片/视频/音频)');
  }

  logger.info(`Seedance: 成功上传 ${uploadedMaterials.length} 个文件`);

  // 动态 benefit_type:包含视频素材时追加 _with_video 后缀
  const hasVideoMaterial = uploadedMaterials.some(m => m.type === "video");
  const finalBenefitType = hasVideoMaterial ? `${benefitType}_with_video` : benefitType;

  // 构建 material_list(支持图片/视频/音频)
  const materialList = uploadedMaterials.map((mat) => {
    const base = { type: "", id: util.uuid() };
    if (mat.type === "image") {
      return {
        ...base,
        material_type: "image",
        image_info: {
          type: "image",
          id: util.uuid(),
          source_from: "upload",
          platform_type: 1,
          name: "",
          image_uri: mat.uri,
          aigc_image: { type: "", id: util.uuid() },
          width: mat.width,
          height: mat.height,
          format: "",
          uri: mat.uri,
        }
      };
    } else if (mat.type === "video") {
      return {
        ...base,
        material_type: "video",
        video_info: {
          type: "video",
          id: util.uuid(),
          source_from: "upload",
          name: mat.name || "",
          vid: mat.vid,
          fps: mat.fps || 0,
          width: mat.width || 0,
          height: mat.height || 0,
          duration: mat.duration || 0,
        }
      };
    } else {
      // audio
      return {
        ...base,
        material_type: "audio",
        audio_info: {
          type: "audio",
          id: util.uuid(),
          source_from: "upload",
          vid: mat.vid,
          duration: mat.duration || 0,
          name: mat.name || "",
        }
      };
    }
  });

  // 解析 prompt 中的素材占位符(@1, @2 等)并构建 meta_list
  const metaList = buildMetaListFromPrompt(prompt, uploadedMaterials);

  const componentId = util.uuid();
  const submitId = util.uuid();
  const draftVersion = MODEL_DRAFT_VERSIONS[_model] || "3.3.9";

  // 计算视频宽高比
  const gcd = (a: number, b: number): number => b === 0 ? a : gcd(b, a % b);
  const divisor = gcd(width, height);
  const aspectRatio = `${width / divisor}:${height / divisor}`;

  const metricsExtra = JSON.stringify({
    isDefaultSeed: 1,
    originSubmitId: submitId,
    isRegenerate: false,
    enterFrom: "click",
    position: "page_bottom_box",
    functionMode: "omni_reference",
    sceneOptions: JSON.stringify([{
      type: "video",
      scene: "BasicVideoGenerateButton",
      modelReqKey: model,
      videoDuration: actualDuration,
      reportParams: {
        enterSource: "generate",
        vipSource: "generate",
        extraVipFunctionKey: model,
        useVipFunctionDetailsReporterHoc: true
      },
      materialTypes: [...new Set(uploadedMaterials.map(m => MATERIAL_TYPE_CODE[m.type]))]
    }])
  });

  // 构建 Seedance 2.0 专用请求(通过浏览器代理,绕过 shark a_bogus 检测)
  const token = await acquireToken(refreshToken);
  const generateQueryParams = new URLSearchParams({
    aid: String(CORE_ASSISTANT_ID),
    device_platform: "web",
    region: provider.region,
    webId: String(WEB_ID),
    da_version: draftVersion,
    web_component_open_flag: "1",
    web_version: "7.5.0",
    aigc_features: "app_lip_sync",
  });
  const generateUrl = `${provider.webApiBaseUrl}${provider.videoGeneratePath}?${generateQueryParams.toString()}`;
  const generateBody = {
    extend: {
      root_model: model,
      m_video_commerce_info: {
        benefit_type: finalBenefitType,
        resource_id: "generate_video",
        resource_id_type: "str",
        resource_sub_type: "aigc"
      },
      m_video_commerce_info_list: [{
        benefit_type: finalBenefitType,
        resource_id: "generate_video",
        resource_id_type: "str",
        resource_sub_type: "aigc"
      }]
    },
    submit_id: submitId,
    metrics_extra: metricsExtra,
    draft_content: JSON.stringify({
      type: "draft",
      id: util.uuid(),
      min_version: draftVersion,
      min_features: ["AIGC_Video_UnifiedEdit"],
      is_from_tsn: true,
      version: draftVersion,
      main_component_id: componentId,
      component_list: [{
        type: "video_base_component",
        id: componentId,
        min_version: "1.0.0",
        aigc_mode: "workbench",
        metadata: {
          type: "",
          id: util.uuid(),
          created_platform: 3,
          created_platform_version: "",
          created_time_in_ms: String(Date.now()),
          created_did: ""
        },
        generate_type: "gen_video",
        abilities: {
          type: "",
          id: util.uuid(),
          gen_video: {
            type: "",
            id: util.uuid(),
            text_to_video_params: {
              type: "",
              id: util.uuid(),
              video_gen_inputs: [{
                type: "",
                id: util.uuid(),
                min_version: draftVersion,
                prompt: "",  // Seedance 2.0 prompt 在 meta_list 中
                video_mode: 2,
                fps: 24,
                duration_ms: actualDuration * 1000,
                idip_meta_list: [],
                unified_edit_input: {
                  type: "",
                  id: util.uuid(),
                  material_list: materialList,
                  meta_list: metaList
                }
              }],
              video_aspect_ratio: aspectRatio,
              seed: Math.floor(Math.random() * 1000000000),
              model_req_key: model,
              priority: 0
            },
            video_task_extra: metricsExtra
          }
        },
        process_type: 1
      }]
    }),
    http_common_info: {
      aid: CORE_ASSISTANT_ID,
    },
  };

  logger.info(`Seedance: 通过浏览器代理发送 generate 请求...`);
  const generateResult = await browserService.fetch(
    token,
    generateUrl,
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(generateBody),
    }
  );

  // 检查浏览器代理返回的结果
  const { ret, errmsg, data: generateData } = generateResult;
  if (ret !== undefined && Number(ret) !== 0) {
    if (Number(ret) === 5000) {
      throw new APIException(EX.API_IMAGE_GENERATION_INSUFFICIENT_POINTS, `[无法生成视频]: 即梦积分可能不足,${errmsg}`);
    }
    throw new APIException(EX.API_REQUEST_FAILED, `[请求jimeng失败]: ${errmsg}`);
  }
  const aigc_data = generateData?.aigc_data || generateResult.aigc_data;

  const historyId = aigc_data.history_record_id;
  if (!historyId)
    throw new APIException(EX.API_IMAGE_GENERATION_FAILED, "记录ID不存在");

  // 轮询获取结果(与普通视频相同的逻辑)
  let status = 20, failCode, item_list = [];
  let retryCount = 0;
  const maxRetries = 120;

  await new Promise((resolve) => setTimeout(resolve, 5000));

  logger.info(`Seedance: 开始轮询视频生成结果,历史ID: ${historyId}`);

  while (status === 20 && retryCount < maxRetries) {
    try {
      const result = await request("post", "/mweb/v1/get_history_by_ids", refreshToken, {
        data: { history_ids: [historyId] },
      });

      const responseStr = JSON.stringify(result);
      logger.info(`Seedance: 轮询响应摘要: ${responseStr.substring(0, 300)}...`);

      // get_history_by_ids 返回的数据可能以 historyId 为键(如 result["8918159809292"]),
      // 也可能在 result.history_list 数组中
      let historyData = result.history_list?.[0] || result[historyId];

      if (!historyData) {
        retryCount++;
        const waitTime = Math.min(2000 * (retryCount + 1), 30000);
        await new Promise((resolve) => setTimeout(resolve, waitTime));
        continue;
      }
      status = historyData.status;
      failCode = historyData.fail_code;
      item_list = historyData.item_list || [];

      logger.info(`Seedance: 状态=${status}, 失败码=${failCode || '无'}`);

      if (status === 30) {
        const error = failCode === 2038
          ? new APIException(EX.API_CONTENT_FILTERED, "内容被过滤")
          : new APIException(EX.API_IMAGE_GENERATION_FAILED, `生成失败,错误码: ${failCode}`);
        error.historyId = historyId;
        throw error;
      }

      if (status === 20) {
        const waitTime = 2000 * Math.min(retryCount + 1, 5);
        await new Promise((resolve) => setTimeout(resolve, waitTime));
      }

      retryCount++;
    } catch (error) {
      if (error instanceof APIException) throw error;
      logger.error(`Seedance: 轮询出错: ${error.message}`);
      retryCount++;
      await new Promise((resolve) => setTimeout(resolve, 2000 * (retryCount + 1)));
    }
  }

  if (retryCount >= maxRetries && status === 20) {
    const error = new APIException(EX.API_IMAGE_GENERATION_FAILED, "视频生成超时");
    error.historyId = historyId;
    throw error;
  }

  // 尝试通过 get_local_item_list 获取高质量视频下载URL
  const seedanceItemId = item_list?.[0]?.item_id
    || item_list?.[0]?.id
    || item_list?.[0]?.local_item_id
    || item_list?.[0]?.common_attr?.id;

  if (seedanceItemId) {
    try {
      const hqVideoUrl = await fetchHighQualityVideoUrl(String(seedanceItemId), refreshToken);
      if (hqVideoUrl) {
        logger.info(`Seedance: 视频生成成功(高质量),URL: ${hqVideoUrl}`);
        return hqVideoUrl;
      }
    } catch (error) {
      logger.warn(`Seedance: 获取高质量视频URL失败,将使用预览URL作为回退: ${error.message}`);
    }
  } else {
    logger.warn(`Seedance: 未能从item_list中提取item_id,将使用预览URL。item_list[0]键: ${item_list?.[0] ? Object.keys(item_list[0]).join(', ') : '无'}`);
  }

  // 回退:提取预览视频URL
  let videoUrl = item_list?.[0]?.video?.transcoded_video?.origin?.video_url
    || item_list?.[0]?.video?.play_url
    || item_list?.[0]?.video?.download_url
    || item_list?.[0]?.video?.url;

  if (!videoUrl) {
    const error = new APIException(EX.API_IMAGE_GENERATION_FAILED, "未能获取视频URL");
    error.historyId = historyId;
    throw error;
  }

  logger.info(`Seedance: 视频生成成功,URL: ${videoUrl}`);
  return videoUrl;
}

/**
 * 解析 prompt 中的素材占位符并构建 meta_list
 * 支持格式: "使用 @1 图片,@2 图片做动画" -> [text, material(0), text, material(1), text]
 * meta_type 根据素材实际类型动态匹配(image/video/audio)
 */
function buildMetaListFromPrompt(prompt: string, materials: Array<{ type: SeedanceMaterialType }>): Array<{meta_type: string, text?: string, material_ref?: {material_idx: number}}> {
  const metaList: Array<{meta_type: string, text?: string, material_ref?: {material_idx: number}}> = [];
  const materialCount = materials.length;

  // 匹配 @1, @2, @图1, @图2, @image1 等格式
  const placeholderRegex = /@(?:图|image)?(\d+)/gi;

  let lastIndex = 0;
  let match;

  while ((match = placeholderRegex.exec(prompt)) !== null) {
    // 添加占位符前的文本
    if (match.index > lastIndex) {
      const textBefore = prompt.substring(lastIndex, match.index);
      if (textBefore.trim()) {
        metaList.push({ meta_type: "text", text: textBefore });
      }
    }

    // 添加素材引用(使用对应素材的类型作为 meta_type)
    const materialIndex = parseInt(match[1]) - 1; // @1 对应 index 0
    if (materialIndex >= 0 && materialIndex < materialCount) {
      metaList.push({
        meta_type: materials[materialIndex].type,
        text: "",
        material_ref: { material_idx: materialIndex }
      });
    }

    lastIndex = match.index + match[0].length;
  }

  // 添加剩余的文本
  if (lastIndex < prompt.length) {
    const remainingText = prompt.substring(lastIndex);
    if (remainingText.trim()) {
      metaList.push({ meta_type: "text", text: remainingText });
    }
  }

  // 如果没有找到任何占位符,默认引用所有素材并附加整个prompt作为文本
  if (metaList.length === 0) {
    // 先添加所有素材引用
    for (let i = 0; i < materialCount; i++) {
      if (i === 0) {
        metaList.push({ meta_type: "text", text: "使用" });
      }
      metaList.push({
        meta_type: materials[i].type,
        text: "",
        material_ref: { material_idx: i }
      });
      if (i < materialCount - 1) {
        metaList.push({ meta_type: "text", text: "和" });
      }
    }
    // 添加描述文本
    if (prompt && prompt.trim()) {
      metaList.push({ meta_type: "text", text: `素材,${prompt}` });
    } else {
      metaList.push({ meta_type: "text", text: "素材生成视频" });
    }
  }

  return metaList;
}

/**
 * 独立的视频结果轮询函数
 * 用于继续轮询已有的 historyId,适用于任务恢复场景
 *
 * @param historyId 即梦平台的 history_record_id
 * @param refreshToken 刷新令牌
 * @param maxRetries 最大重试次数(默认120次)
 * @returns 视频URL
 */
async function pollVideoResult(
  historyId: string,
  refreshToken: string,
  maxRetries: number = 120
): Promise<string> {
  let status = 20, failCode, item_list = [];
  let retryCount = 0;

  logger.info(`轮询视频结果: historyId=${historyId}, maxRetries=${maxRetries}`);

  while (status === 20 && retryCount < maxRetries) {
    try {
      const result = await request("post", "/mweb/v1/get_history_by_ids", refreshToken, {
        data: { history_ids: [historyId] },
      });

      const responseStr = JSON.stringify(result);
      logger.info(`轮询响应摘要: ${responseStr.substring(0, 300)}...`);

      let historyData = result.history_list?.[0] || result[historyId];

      if (!historyData) {
        retryCount++;
        const waitTime = Math.min(2000 * (retryCount + 1), 30000);
        logger.info(`历史记录未找到,等待 ${waitTime}ms 后重试 (${retryCount}/${maxRetries})`);
        await new Promise((resolve) => setTimeout(resolve, waitTime));
        continue;
      }

      status = historyData.status;
      failCode = historyData.fail_code;
      item_list = historyData.item_list || [];

      logger.info(`轮询状态: status=${status}, failCode=${failCode || '无'}, items=${item_list.length}`);

      if (status === 30) {
        const error = failCode === 2038
          ? new APIException(EX.API_CONTENT_FILTERED, "内容被过滤")
          : new APIException(EX.API_IMAGE_GENERATION_FAILED, `生成失败,错误码: ${failCode}`);
        error.historyId = historyId;
        throw error;
      }

      if (status === 20) {
        const waitTime = 2000 * Math.min(retryCount + 1, 5);
        logger.info(`视频生成中,等待 ${waitTime}ms 后继续查询 (${retryCount + 1}/${maxRetries})`);
        await new Promise((resolve) => setTimeout(resolve, waitTime));
      }

      retryCount++;
    } catch (error) {
      if (error instanceof APIException) throw error;
      logger.error(`轮询出错: ${error.message}`);
      retryCount++;
      await new Promise((resolve) => setTimeout(resolve, 2000 * (retryCount + 1)));
    }
  }

  if (retryCount >= maxRetries && status === 20) {
    const error = new APIException(EX.API_IMAGE_GENERATION_FAILED, "视频生成超时");
    error.historyId = historyId;
    throw error;
  }

  // 尝试获取高质量视频URL
  const itemId = item_list?.[0]?.item_id
    || item_list?.[0]?.id
    || item_list?.[0]?.local_item_id
    || item_list?.[0]?.common_attr?.id;

  if (itemId) {
    try {
      const hqVideoUrl = await fetchHighQualityVideoUrl(String(itemId), refreshToken);
      if (hqVideoUrl) {
        logger.info(`视频生成成功(高质量),URL: ${hqVideoUrl}`);
        return hqVideoUrl;
      }
    } catch (error) {
      logger.warn(`获取高质量视频URL失败: ${error.message}`);
    }
  }

  // 回退:提取预览视频URL
  let videoUrl = item_list?.[0]?.video?.transcoded_video?.origin?.video_url
    || item_list?.[0]?.video?.play_url
    || item_list?.[0]?.video?.download_url
    || item_list?.[0]?.video?.url;

  if (!videoUrl) {
    const error = new APIException(EX.API_IMAGE_GENERATION_FAILED, "未能获取视频URL");
    error.historyId = historyId;
    throw error;
  }

  logger.info(`视频生成成功,URL: ${videoUrl}`);
  return videoUrl;
}

/**
 * 即时单次查询视频状态(不轮询)
 * 用于 on-demand 查询:当用户查询一个 processing 任务时,去即梦平台查一次
 * 如果视频已生成好,直接返回视频URL;否则返回 null
 *
 * @param historyId 即梦平台的 history_record_id
 * @param refreshToken 刷新令牌
 * @returns 视频URL(已完成)或 null(仍在处理中或失败)
 */
async function checkVideoStatusByHistoryId(
  historyId: string,
  refreshToken: string
): Promise<string | null> {
  try {
    const result = await request("post", "/mweb/v1/get_history_by_ids", refreshToken, {
      data: { history_ids: [historyId] },
    });

    let historyData = result.history_list?.[0] || result[historyId];
    if (!historyData) {
      logger.info(`即时查询: 未找到历史记录 historyId=${historyId}`);
      return null;
    }

    const status = historyData.status;
    const failCode = historyData.fail_code;
    const item_list = historyData.item_list || [];

    logger.info(`即时查询: historyId=${historyId}, status=${status}, failCode=${failCode || '无'}, items=${item_list.length}`);

    // status=20 表示还在处理中
    if (status === 20) {
      return null;
    }

    // status=30 表示生成失败
    if (status === 30) {
      logger.warn(`即时查询: 视频生成失败, historyId=${historyId}, failCode=${failCode}`);
      return null;
    }

    // status=10 或其他非20值,尝试提取视频URL
    // 尝试获取高质量视频URL
    const itemId = item_list?.[0]?.item_id
      || item_list?.[0]?.id
      || item_list?.[0]?.local_item_id
      || item_list?.[0]?.common_attr?.id;

    if (itemId) {
      try {
        const hqVideoUrl = await fetchHighQualityVideoUrl(String(itemId), refreshToken);
        if (hqVideoUrl) {
          logger.info(`即时查询: 获取高质量视频URL成功, historyId=${historyId}`);
          return hqVideoUrl;
        }
      } catch (error) {
        logger.warn(`即时查询: 获取高质量视频URL失败: ${error.message}`);
      }
    }

    // 回退:提取预览视频URL
    const videoUrl = item_list?.[0]?.video?.transcoded_video?.origin?.video_url
      || item_list?.[0]?.video?.play_url
      || item_list?.[0]?.video?.download_url
      || item_list?.[0]?.video?.url;

    if (videoUrl) {
      logger.info(`即时查询: 获取预览视频URL成功, historyId=${historyId}`);
      return videoUrl;
    }

    // item_list 非空但无法提取URL,可能还在处理中
    if (item_list.length === 0) {
      logger.info(`即时查询: item_list 为空,可能仍在处理, historyId=${historyId}`);
      return null;
    }

    logger.warn(`即时查询: item_list 非空但无法提取视频URL, historyId=${historyId}`);
    return null;
  } catch (error) {
    logger.error(`即时查询出错: historyId=${historyId}, ${error.message}`);
    return null;
  }
}

// ========== 异步视频生成任务管理 ==========

// 异步任务状态类型
type AsyncTaskStatus = "processing" | "succeeded" | "failed";

// 异步任务持久化接口(仅可序列化字段)
interface AsyncTaskData {
  taskId: string;
  status: AsyncTaskStatus;
  model: string;
  prompt: string;
  refreshToken: string;
  createdAt: number;
  updatedAt: number;
  historyId?: string;  // 即梦平台的 history_record_id,用于重启后继续轮询
  result?: {
    url?: string;
    b64_json?: string;
    revised_prompt?: string;
  };
  error?: string;
}

// 运行时任务接口(含内存中的 Promise 控制器)
interface AsyncTask extends AsyncTaskData {
  _resolve?: (value: void) => void;
  _promise?: Promise<void>;
}

// 任务存储目录
const ASYNC_TASK_DIR = path.join(process.cwd(), "tmp", "async-tasks");

// 内存任务映射(从文件加载后使用)
const asyncTaskStore = new Map<string, AsyncTask>();

// 当前活跃异步任务数
let activeAsyncCount = 0;

// 最大并发数
const MAX_ASYNC_CONCURRENCY = 10;

// 任务过期时间(24小时,单位毫秒)
const TASK_EXPIRY_MS = 24 * 60 * 60 * 1000;

/**
 * 获取任务文件路径
 */
function taskFilePath(taskId: string): string {
  return path.join(ASYNC_TASK_DIR, `${taskId}.json`);
}

/**
 * 将任务数据持久化到文件
 */
function saveTaskToFile(task: AsyncTask): void {
  try {
    const data: AsyncTaskData = {
      taskId: task.taskId,
      status: task.status,
      model: task.model,
      prompt: task.prompt,
      refreshToken: task.refreshToken,
      createdAt: task.createdAt,
      updatedAt: task.updatedAt,
      historyId: task.historyId,
      result: task.result,
      error: task.error,
    };
    fs.writeFileSync(taskFilePath(task.taskId), JSON.stringify(data, null, 2), "utf-8");
  } catch (err) {
    logger.error(`保存任务文件失败: ${task.taskId}, ${err.message}`);
  }
}

/**
 * 从文件加载单个任务
 */
function loadTaskFromFile(filePath: string): AsyncTaskData | null {
  try {
    const raw = fs.readFileSync(filePath, "utf-8");
    return JSON.parse(raw) as AsyncTaskData;
  } catch (err) {
    logger.error(`加载任务文件失败: ${filePath}, ${err.message}`);
    return null;
  }
}

/**
 * 删除任务文件
 */
function deleteTaskFile(taskId: string): void {
  try {
    const fp = taskFilePath(taskId);
    if (fs.existsSync(fp)) {
      fs.unlinkSync(fp);
    }
  } catch (err) {
    logger.error(`删除任务文件失败: ${taskId}, ${err.message}`);
  }
}

/**
 * 启动时从文件恢复所有未完成任务
 * 恢复 processing 状态的任务并重新执行轮询
 */
function restoreTasksFromFiles(): void {
  try {
    if (!fs.existsSync(ASYNC_TASK_DIR)) {
      fs.mkdirSync(ASYNC_TASK_DIR, { recursive: true });
      return;
    }

    const files = fs.readdirSync(ASYNC_TASK_DIR).filter(f => f.endsWith(".json"));
    if (files.length === 0) return;

    logger.info(`发现 ${files.length} 个异步任务文件,开始恢复...`);

    for (const file of files) {
      const data = loadTaskFromFile(path.join(ASYNC_TASK_DIR, file));
      if (!data) continue;

      // 跳过已过期的任务
      if (Date.now() - data.updatedAt > TASK_EXPIRY_MS) {
        deleteTaskFile(data.taskId);
        logger.info(`恢复时清理过期任务: ${data.taskId}`);
        continue;
      }

      // 已成功/失败的任务直接加载到内存(不占用并发槽位)
      if (data.status !== "processing") {
        const task = data as AsyncTask;
        asyncTaskStore.set(data.taskId, task);
        logger.info(`恢复已完成任务: ${data.taskId}, 状态: ${data.status}`);
        continue;
      }

      // processing 状态的任务:恢复并重启轮询
      if (activeAsyncCount >= MAX_ASYNC_CONCURRENCY) {
        logger.warn(`恢复任务 ${data.taskId} 跳过:并发已满 ${activeAsyncCount}/${MAX_ASYNC_CONCURRENCY}`);
        // 仍加载到内存但不重启轮询,等有槽位时手动查询会触发
        const task = data as AsyncTask;
        asyncTaskStore.set(data.taskId, task);
        continue;
      }

      const task: AsyncTask = {
        ...data,
        _promise: undefined,
        _resolve: undefined,
      };
      // 创建 Promise 并将 resolve 绑定到 task._resolve
      task._promise = new Promise<void>((resolve) => {
        task._resolve = resolve;
      });
      asyncTaskStore.set(data.taskId, task);
      activeAsyncCount++;
      logger.info(`恢复并重启 processing 任务: ${data.taskId}, 当前并发: ${activeAsyncCount}/${MAX_ASYNC_CONCURRENCY}`);

      // 后台重新执行轮询
      restartPollingForTask(task);
    }

    logger.info(`任务恢复完成,当前活跃并发: ${activeAsyncCount}/${MAX_ASYNC_CONCURRENCY}`);
  } catch (err) {
    logger.error(`恢复任务文件出错: ${err.message}`);
  }
}

/**
 * 为恢复的 processing 任务重启轮询
 * 使用保存的 historyId 继续轮询,而不是重新提交生成请求
 * 超时后保持 processing 状态,等用户查询时做 on-demand 查询
 */
function restartPollingForTask(task: AsyncTask): void {
  (async () => {
    try {
      if (!task.historyId) {
        // 没有 historyId,无法恢复轮询,标记为失败
        task.status = "failed";
        task.error = "任务缺少 historyId,无法恢复轮询";
        task.updatedAt = Date.now();
        saveTaskToFile(task);
        logger.error(`恢复任务失败: ${task.taskId}, 缺少 historyId`);
        return;
      }

      logger.info(`恢复任务轮询: ${task.taskId}, historyId=${task.historyId}`);

      // 使用保存的 historyId 继续轮询
      const videoUrl = await pollVideoResult(task.historyId, task.refreshToken);

      task.status = "succeeded";
      task.result = { url: videoUrl, revised_prompt: task.prompt };
      task.updatedAt = Date.now();
      saveTaskToFile(task);
      logger.info(`恢复任务轮询成功: ${task.taskId}`);
    } catch (error: any) {
      // 超时错误:保持 processing 状态,不标记为 failed
      // 用户查询时会通过 on-demand 查询检查即梦平台的最新状态
      const errorMsg = error?.message || "";
      if (errorMsg.includes("超时")) {
        task.updatedAt = Date.now();
        saveTaskToFile(task); // 保存 historyId,保持 processing
        logger.warn(`恢复任务轮询超时,保持 processing 状态: ${task.taskId}, historyId=${task.historyId},等待用户查询时 on-demand 检查`);
      } else {
        task.status = "failed";
        task.error = error instanceof APIException
          ? `[${error.code}] ${error.message}`
          : errorMsg || "未知错误";
        task.updatedAt = Date.now();
        saveTaskToFile(task);
        logger.error(`恢复任务轮询失败: ${task.taskId}, ${task.error}`);
      }
    } finally {
      activeAsyncCount--;
      if (task._resolve) task._resolve();
    }
  })();
}

// 定期清理过期任务文件(每30分钟)
setInterval(() => {
  const now = Date.now();
  for (const [taskId, task] of asyncTaskStore) {
    if (now - task.updatedAt > TASK_EXPIRY_MS) {
      asyncTaskStore.delete(taskId);
      deleteTaskFile(taskId);
      logger.info(`异步任务已过期清理: ${taskId}`);
    }
  }
}, 30 * 60 * 1000);

// 启动时恢复任务
restoreTasksFromFiles();

/**
 * 普通视频生成(内部版,返回 historyId)
 * 将生成请求和轮询拆分,在获取到 historyId 后立即保存,以便重启恢复
 */
async function _generateVideoWithHistoryId(
  _model: string,
  prompt: string,
  options: {
    ratio?: string;
    resolution?: string;
    duration?: number;
    filePaths?: string[];
    files?: any[];
  },
  refreshToken: string,
  onHistoryId?: (historyId: string) => void
): Promise<{ url: string; historyId: string }> {
  const model = getModel(_model);
  const { ratio = "1:1", resolution = "720p", duration = 5, filePaths = [], files = [] } = options;
  const { width, height } = resolveVideoResolution(resolution, ratio);

  logger.info(`异步任务-普通视频: 模型=${_model} 映射=${model} ${width}x${height} (${ratio}@${resolution}) 时长=${duration}秒`);

  // 检查积分
  const { totalCredit } = await getCredit(refreshToken);
  if (totalCredit <= 0) await receiveCredit(refreshToken);

  // 处理首帧和尾帧图片
  let first_frame_image = undefined;
  let end_frame_image = undefined;

  if (files && files.length > 0) {
    let uploadIDs: string[] = [];
    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      if (!file || !file.filepath) continue;
      try {
        const buffer = fs.readFileSync(file.filepath);
        const imageUri = await uploadImageBufferForVideo(buffer, refreshToken);
        if (imageUri) uploadIDs.push(imageUri);
      } catch (error) {
        if (i === 0) throw new APIException(EX.API_REQUEST_FAILED, `首帧文件上传失败: ${error.message}`);
      }
    }
    if (uploadIDs.length === 0) throw new APIException(EX.API_REQUEST_FAILED, '所有文件上传失败');
    if (uploadIDs[0]) {
      first_frame_image = { format: "", height, id: util.uuid(), image_uri: uploadIDs[0], name: "", platform_type: 1, source_from: "upload", type: "image", uri: uploadIDs[0], width };
    }
    if (uploadIDs[1]) {
      end_frame_image = { format: "", height, id: util.uuid(), image_uri: uploadIDs[1], name: "", platform_type: 1, source_from: "upload", type: "image", uri: uploadIDs[1], width };
    }
  } else if (filePaths && filePaths.length > 0) {
    let uploadIDs: string[] = [];
    for (let i = 0; i < filePaths.length; i++) {
      if (!filePaths[i]) continue;
      try {
        const imageUri = await uploadImageForVideo(filePaths[i], refreshToken);
        if (imageUri) uploadIDs.push(imageUri);
      } catch (error) {
        if (i === 0) throw new APIException(EX.API_REQUEST_FAILED, `首帧图片上传失败: ${error.message}`);
      }
    }
    if (uploadIDs.length === 0) throw new APIException(EX.API_REQUEST_FAILED, '所有图片上传失败');
    if (uploadIDs[0]) {
      first_frame_image = { format: "", height, id: util.uuid(), image_uri: uploadIDs[0], name: "", platform_type: 1, source_from: "upload", type: "image", uri: uploadIDs[0], width };
    }
    if (uploadIDs[1]) {
      end_frame_image = { format: "", height, id: util.uuid(), image_uri: uploadIDs[1], name: "", platform_type: 1, source_from: "upload", type: "image", uri: uploadIDs[1], width };
    }
  }

  const componentId = util.uuid();
  const metricsExtra = JSON.stringify({
    "enterFrom": "click", "isDefaultSeed": 1, "promptSource": "custom",
    "isRegenerate": false, "originSubmitId": util.uuid(),
  });
  const draftVersion = MODEL_DRAFT_VERSIONS[_model] || DEFAULT_DRAFT_VERSION;
  const gcd = (a: number, b: number): number => b === 0 ? a : gcd(b, a % b);
  const divisor = gcd(width, height);
  const aspectRatio = `${width / divisor}:${height / divisor}`;

  // 提交生成请求
  const { aigc_data } = await request("post", provider.videoGeneratePath, refreshToken, {
    params: {
      aigc_features: "app_lip_sync", web_version: "6.6.0", da_version: draftVersion,
    },
    data: {
      "extend": {
        "root_model": end_frame_image ? MODEL_MAP['jimeng-video-3.0'] : model,
        "m_video_commerce_info": { benefit_type: "basic_video_operation_vgfm_v_three", resource_id: "generate_video", resource_id_type: "str", resource_sub_type: "aigc" },
        "m_video_commerce_info_list": [{ benefit_type: "basic_video_operation_vgfm_v_three", resource_id: "generate_video", resource_id_type: "str", resource_sub_type: "aigc" }]
      },
      "submit_id": util.uuid(),
      "metrics_extra": metricsExtra,
      "draft_content": JSON.stringify({
        "type": "draft", "id": util.uuid(), "min_version": "3.0.5", "is_from_tsn": true,
        "version": draftVersion, "main_component_id": componentId,
        "component_list": [{
          "type": "video_base_component", "id": componentId, "min_version": "1.0.0",
          "metadata": { "type": "", "id": util.uuid(), "created_platform": 3, "created_platform_version": "", "created_time_in_ms": Date.now(), "created_did": "" },
          "generate_type": "gen_video", "aigc_mode": "workbench",
          "abilities": {
            "type": "", "id": util.uuid(),
            "gen_video": {
              "id": util.uuid(), "type": "",
              "text_to_video_params": {
                "type": "", "id": util.uuid(), "model_req_key": model, "priority": 0,
                "seed": Math.floor(Math.random() * 100000000) + 2500000000,
                "video_aspect_ratio": aspectRatio,
                "video_gen_inputs": [{
                  duration_ms: duration * 1000, first_frame_image, end_frame_image,
                  fps: 24, id: util.uuid(), min_version: "3.0.5", prompt, resolution, type: "", video_mode: 2
                }]
              },
              "video_task_extra": metricsExtra,
            }
          }
        }],
      }),
      http_common_info: { aid: DEFAULT_ASSISTANT_ID },
    }
  });

  const historyId = aigc_data.history_record_id;
  if (!historyId) throw new APIException(EX.API_IMAGE_GENERATION_FAILED, "记录ID不存在");

  logger.info(`异步任务-普通视频: 生成请求已提交, historyId=${historyId}`);

  // 立即通知外部 historyId,确保即使后续轮询超时也能保存
  if (onHistoryId) onHistoryId(historyId);

  // 轮询获取结果(使用独立的轮询函数)
  const videoUrl = await pollVideoResult(historyId, refreshToken);
  return { url: videoUrl, historyId };
}

/**
 * Seedance 2.0 视频生成(内部版,返回 historyId)
 * 将生成请求和轮询拆分,在获取到 historyId 后立即保存,以便重启恢复
 */
async function _generateSeedanceVideoWithHistoryId(
  _model: string,
  prompt: string,
  options: {
    ratio?: string;
    resolution?: string;
    duration?: number;
    filePaths?: string[];
    files?: any[];
  },
  refreshToken: string,
  onHistoryId?: (historyId: string) => void
): Promise<{ url: string; historyId: string }> {
  const model = getModel(_model);
  const benefitType = SEEDANCE_BENEFIT_TYPE_MAP[_model] || "dreamina_video_seedance_20_pro";
  const { ratio = "4:3", resolution = "720p", duration = 4, filePaths = [], files = [] } = options;
  const actualDuration = duration || 4;
  const { width, height } = resolveVideoResolution(resolution, ratio);

  logger.info(`异步任务-Seedance: 模型=${_model} 映射=${model} ${width}x${height} (${ratio}@${resolution}) 时长=${actualDuration}秒`);

  // 检查积分
  const { totalCredit } = await getCredit(refreshToken);
  if (totalCredit <= 0) await receiveCredit(refreshToken);

  // 上传素材(复用 generateSeedanceVideo 中的逻辑)
  let uploadedMaterials: UploadedMaterial[] = [];

  if (files && files.length > 0) {
    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      if (!file || !file.filepath) continue;
      const materialType = detectMaterialType(file);
      try {
        const buffer = fs.readFileSync(file.filepath);
        if (materialType === "image") {
          const imageUri = await uploadImageBufferForVideo(buffer, refreshToken);
          if (imageUri) uploadedMaterials.push({ type: "image", uri: imageUri, width, height });
        } else {
          const vodResult = await uploadMediaForVideo(buffer, materialType, refreshToken, file.originalFilename);
          uploadedMaterials.push({ type: materialType, vid: vodResult.vid, width: vodResult.width, height: vodResult.height, duration: vodResult.duration, fps: vodResult.fps, name: file.originalFilename || "" });
        }
      } catch (error) {
        if (i === 0) throw new APIException(EX.API_REQUEST_FAILED, `首个文件上传失败: ${error.message}`);
      }
    }
  } else if (filePaths && filePaths.length > 0) {
    for (let i = 0; i < filePaths.length; i++) {
      if (!filePaths[i]) continue;
      const materialType = detectMaterialTypeFromUrl(filePaths[i]);
      try {
        if (materialType === "image") {
          const imageUri = await uploadImageForVideo(filePaths[i], refreshToken);
          if (imageUri) uploadedMaterials.push({ type: "image", uri: imageUri, width, height });
        } else {
          const response = await fetch(filePaths[i]);
          if (!response.ok) throw new Error(`下载文件失败: ${response.status}`);
          const buffer = Buffer.from(await response.arrayBuffer());
          const vodResult = await uploadMediaForVideo(buffer, materialType, refreshToken);
          uploadedMaterials.push({ type: materialType, vid: vodResult.vid, width: vodResult.width, height: vodResult.height, duration: vodResult.duration, fps: vodResult.fps });
        }
      } catch (error) {
        if (i === 0) throw new APIException(EX.API_REQUEST_FAILED, `首个文件上传失败: ${error.message}`);
      }
    }
  }

  if (uploadedMaterials.length === 0) {
    throw new APIException(EX.API_REQUEST_FAILED, 'Seedance 2.0 需要至少一个文件');
  }

  // 构建请求参数(与 generateSeedanceVideo 相同)
  const hasVideoMaterial = uploadedMaterials.some(m => m.type === "video");
  const finalBenefitType = hasVideoMaterial ? `${benefitType}_with_video` : benefitType;

  const materialList = uploadedMaterials.map((mat) => {
    const base = { type: "", id: util.uuid() };
    if (mat.type === "image") {
      return { ...base, material_type: "image", image_info: { type: "image", id: util.uuid(), source_from: "upload", platform_type: 1, name: "", image_uri: mat.uri, aigc_image: { type: "", id: util.uuid() }, width: mat.width, height: mat.height, format: "", uri: mat.uri } };
    } else if (mat.type === "video") {
      return { ...base, material_type: "video", video_info: { type: "video", id: util.uuid(), source_from: "upload", name: mat.name || "", vid: mat.vid, fps: mat.fps || 0, width: mat.width || 0, height: mat.height || 0, duration: mat.duration || 0 } };
    } else {
      return { ...base, material_type: "audio", audio_info: { type: "audio", id: util.uuid(), source_from: "upload", vid: mat.vid, duration: mat.duration || 0, name: mat.name || "" } };
    }
  });

  const metaList = buildMetaListFromPrompt(prompt, uploadedMaterials);
  const componentId = util.uuid();
  const submitId = util.uuid();
  const draftVersion = MODEL_DRAFT_VERSIONS[_model] || "3.3.9";
  const gcd = (a: number, b: number): number => b === 0 ? a : gcd(b, a % b);
  const divisor = gcd(width, height);
  const aspectRatio = `${width / divisor}:${height / divisor}`;

  const metricsExtra = JSON.stringify({
    isDefaultSeed: 1, originSubmitId: submitId, isRegenerate: false, enterFrom: "click",
    position: "page_bottom_box", functionMode: "omni_reference",
    sceneOptions: JSON.stringify([{ type: "video", scene: "BasicVideoGenerateButton", modelReqKey: model, videoDuration: actualDuration, reportParams: { enterSource: "generate", vipSource: "generate", extraVipFunctionKey: model, useVipFunctionDetailsReporterHoc: true }, materialTypes: [...new Set(uploadedMaterials.map(m => MATERIAL_TYPE_CODE[m.type]))] }])
  });

  const token = await acquireToken(refreshToken);
  const generateQueryParams = new URLSearchParams({
    aid: String(CORE_ASSISTANT_ID), device_platform: "web", region: provider.region,
    webId: String(WEB_ID), da_version: draftVersion, web_component_open_flag: "1",
    web_version: "7.5.0", aigc_features: "app_lip_sync",
  });
  const generateUrl = `${provider.webApiBaseUrl}${provider.videoGeneratePath}?${generateQueryParams.toString()}`;
  const generateBody = {
    extend: {
      root_model: model,
      m_video_commerce_info: { benefit_type: finalBenefitType, resource_id: "generate_video", resource_id_type: "str", resource_sub_type: "aigc" },
      m_video_commerce_info_list: [{ benefit_type: finalBenefitType, resource_id: "generate_video", resource_id_type: "str", resource_sub_type: "aigc" }]
    },
    submit_id: submitId, metrics_extra: metricsExtra,
    draft_content: JSON.stringify({
      type: "draft", id: util.uuid(), min_version: draftVersion, min_features: ["AIGC_Video_UnifiedEdit"],
      is_from_tsn: true, version: draftVersion, main_component_id: componentId,
      component_list: [{
        type: "video_base_component", id: componentId, min_version: "1.0.0", aigc_mode: "workbench",
        metadata: { type: "", id: util.uuid(), created_platform: 3, created_platform_version: "", created_time_in_ms: String(Date.now()), created_did: "" },
        generate_type: "gen_video",
        abilities: {
          type: "", id: util.uuid(),
          gen_video: {
            type: "", id: util.uuid(),
            text_to_video_params: {
              type: "", id: util.uuid(),
              video_gen_inputs: [{
                type: "", id: util.uuid(), min_version: draftVersion, prompt: "", video_mode: 2, fps: 24,
                duration_ms: actualDuration * 1000, idip_meta_list: [],
                unified_edit_input: { type: "", id: util.uuid(), material_list: materialList, meta_list: metaList }
              }],
              video_aspect_ratio: aspectRatio, seed: Math.floor(Math.random() * 1000000000), model_req_key: model, priority: 0
            },
            video_task_extra: metricsExtra
          }
        },
        process_type: 1
      }]
    }),
    http_common_info: { aid: CORE_ASSISTANT_ID },
  };

  logger.info(`异步任务-Seedance: 通过浏览器代理发送 generate 请求...`);
  const generateResult = await browserService.fetch(token, generateUrl, {
    method: "POST", headers: { "Content-Type": "application/json" },
    body: JSON.stringify(generateBody),
  });

  const { ret, errmsg, data: generateData } = generateResult;
  if (ret !== undefined && Number(ret) !== 0) {
    if (Number(ret) === 5000) {
      throw new APIException(EX.API_IMAGE_GENERATION_INSUFFICIENT_POINTS, `[无法生成视频]: 即梦积分可能不足,${errmsg}`);
    }
    throw new APIException(EX.API_REQUEST_FAILED, `[请求jimeng失败]: ${errmsg}`);
  }
  const aigc_data = generateData?.aigc_data || generateResult.aigc_data;
  const historyId = aigc_data.history_record_id;
  if (!historyId) throw new APIException(EX.API_IMAGE_GENERATION_FAILED, "记录ID不存在");

  logger.info(`异步任务-Seedance: 生成请求已提交, historyId=${historyId}`);

  // 立即通知外部 historyId,确保即使后续轮询超时也能保存
  if (onHistoryId) onHistoryId(historyId);

  // 轮询获取结果(使用独立的轮询函数)
  const videoUrl = await pollVideoResult(historyId, refreshToken);
  return { url: videoUrl, historyId };
}

/**
 * 提交异步视频生成任务
 * 调用生成接口后立即返回 taskId,后台执行轮询等待视频生成完成
 */
export function submitAsyncVideoTask(
  model: string,
  prompt: string,
  options: {
    ratio?: string;
    resolution?: string;
    duration?: number;
    filePaths?: string[];
    files?: any[];
  },
  refreshToken: string
): string {
  if (activeAsyncCount >= MAX_ASYNC_CONCURRENCY) {
    throw new APIException(
      EX.API_REQUEST_FAILED,
      `当前异步任务并发数已达上限 (${MAX_ASYNC_CONCURRENCY}),请稍后重试`
    );
  }

  // 确保任务目录存在
  if (!fs.existsSync(ASYNC_TASK_DIR)) {
    fs.mkdirSync(ASYNC_TASK_DIR, { recursive: true });
  }

  const taskId = util.uuid();
  const task: AsyncTask = {
    taskId,
    status: "processing",
    model,
    prompt,
    refreshToken,
    createdAt: Date.now(),
    updatedAt: Date.now(),
  };
  // 创建用于查询接口阻塞等待的 Promise
  task._promise = new Promise<void>((resolve) => {
    task._resolve = resolve;
  });

  asyncTaskStore.set(taskId, task);
  saveTaskToFile(task);
  activeAsyncCount++;
  logger.info(
    `异步任务已创建: ${taskId}, 模型: ${model}, 当前并发: ${activeAsyncCount}/${MAX_ASYNC_CONCURRENCY}`
  );

  // 后台执行视频生成(含轮询)
  (async () => {
    try {
      let videoUrl: string;
      if (isSeedanceModel(model)) {
        const seedanceDuration =
          options.duration === 5 ? 4 : options.duration;
        const seedanceRatio =
          options.ratio === "1:1" ? "4:3" : options.ratio;

        // 使用 Seedance 生成:先生成获取 historyId,通过回调立即保存,然后再轮询
        const { url } = await _generateSeedanceVideoWithHistoryId(
          model, prompt, {
            ratio: seedanceRatio,
            resolution: options.resolution,
            duration: seedanceDuration,
            filePaths: options.filePaths,
            files: options.files,
          }, refreshToken,
          // onHistoryId 回调:在获取到 historyId 后立即保存到 task 文件
          (historyId) => {
            task.historyId = historyId;
            saveTaskToFile(task);
            logger.info(`异步任务-Seedance: historyId 已保存, ${taskId} -> ${historyId}`);
          }
        );

        videoUrl = url;
      } else {
        // 普通视频生成:先生成获取 historyId,通过回调立即保存,然后再轮询
        const { url } = await _generateVideoWithHistoryId(
          model, prompt, {
            ratio: options.ratio,
            resolution: options.resolution,
            duration: options.duration,
            filePaths: options.filePaths,
            files: options.files,
          }, refreshToken,
          // onHistoryId 回调:在获取到 historyId 后立即保存到 task 文件
          (historyId) => {
            task.historyId = historyId;
            saveTaskToFile(task);
            logger.info(`异步任务-普通视频: historyId 已保存, ${taskId} -> ${historyId}`);
          }
        );

        videoUrl = url;
      }

      task.status = "succeeded";
      task.result = {
        url: videoUrl,
        revised_prompt: prompt,
      };
      task.updatedAt = Date.now();
      saveTaskToFile(task);
      logger.info(`异步任务成功: ${taskId}, 视频URL: ${videoUrl}`);
    } catch (error: any) {
      const errorMsg = error?.message || "";

      // 超时错误:保持 processing 状态,保存 historyId
      // 用户查询时会通过 on-demand 查询检查即梦平台最新状态
      if (errorMsg.includes("超时")) {
        task.updatedAt = Date.now();
        saveTaskToFile(task); // 保存 historyId,保持 processing
        logger.warn(`异步任务后台轮询超时,保持 processing 状态: ${taskId}, historyId=${task.historyId},等待用户查询时 on-demand 检查`);
      } else {
        task.status = "failed";
        task.error = error instanceof APIException
          ? `[${error.code}] ${error.message}`
          : errorMsg || "未知错误";
        task.updatedAt = Date.now();
        saveTaskToFile(task);
        logger.error(`异步任务失败: ${taskId}, 错误: ${task.error}`);
      }
    } finally {
      activeAsyncCount--;
      // 通知查询接口任务已完成(succeeded/failed),或后台轮询已停止(超时保持processing)
      if (task._resolve) {
        task._resolve();
      }
    }
  })();

  return taskId;
}

/**
 * 查询异步视频生成任务结果
 * - 如果后台轮询仍在进行中(有 _promise),阻塞等待完成
 * - 如果后台轮询已停止但任务仍为 processing(超时场景),做 on-demand 即时查询
 */
export async function queryAsyncVideoTask(
  taskId: string
): Promise<AsyncTask> {
  // 先从内存查找
  let task = asyncTaskStore.get(taskId);

  // 内存中没有,尝试从文件加载
  if (!task) {
    const fp = taskFilePath(taskId);
    if (!fs.existsSync(fp)) {
      throw new APIException(
        EX.API_REQUEST_PARAMS_INVALID,
        `任务ID不存在或已过期: ${taskId}`
      );
    }
    const data = loadTaskFromFile(fp);
    if (!data) {
      throw new APIException(
        EX.API_REQUEST_PARAMS_INVALID,
        `任务数据损坏: ${taskId}`
      );
    }
    task = data as AsyncTask;
    asyncTaskStore.set(taskId, task);
    logger.info(`从文件加载任务: ${taskId}, 状态: ${task.status}`);
  }

  // 已终态的任务直接返回
  if (task.status === "succeeded" || task.status === "failed") {
    return task;
  }

  // processing 状态的任务
  if (task.status === "processing") {
    // 如果后台轮询仍在进行中(有活跃的 Promise),阻塞等待
    if (task._promise) {
      logger.info(`查询接口等待后台轮询完成: ${taskId}`);
      await task._promise;
      return task;
    }

    // 后台轮询已停止(超时或重启后的 processing 任务),做 on-demand 即时查询
    if (task.historyId) {
      logger.info(`on-demand 即时查询: ${taskId}, historyId=${task.historyId}`);
      const videoUrl = await checkVideoStatusByHistoryId(task.historyId, task.refreshToken);

      if (videoUrl) {
        // 视频已生成好!更新任务状态
        task.status = "succeeded";
        task.result = { url: videoUrl, revised_prompt: task.prompt };
        task.updatedAt = Date.now();
        saveTaskToFile(task);
        logger.info(`on-demand 查询发现视频已完成: ${taskId}, URL: ${videoUrl}`);
      } else {
        // 仍在处理中,保持 processing
        logger.info(`on-demand 查询: 视频仍在处理中, ${taskId}`);
      }
    } else {
      logger.warn(`processing 任务缺少 historyId,无法 on-demand 查询: ${taskId}`);
    }
  }

  return task;
}