log
2026-06-25 7c4c956be0c3c4c2f76f181e356779003bc8f998
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
# -*- coding: utf-8 -*-
"""
阶段3: 分镜智能体
基于剧本JSON,逐场景拆分为带时长标签的分镜(shots),按幕分组输出。
支持 Segment -> Shots 嵌套结构。
"""
 
import os
import re
import json
import asyncio
import logging
from datetime import datetime
from typing import Any, Optional, Callable, Dict, List, Tuple
 
from .base_agent import AgentInterface
 
logger = logging.getLogger(__name__)
 
def _get_storyboard_prompt(name: str, lang: str = "zh") -> str:
    from prompts.loader import load_prompt_with_fallback
    return load_prompt_with_fallback("storyboard", name, lang, "zh")
 
class StoryboardAgent(AgentInterface):
    def __init__(self):
        super().__init__(name="Storyboard")
 
    MIN_SHOT_DURATION = 2
    MIN_SEGMENT_DURATION = 5
    MAX_SEGMENT_DURATION = 15
    OPENING_SHOT_TYPES = {"中景", "全景"}
 
    @staticmethod
    def _extract_json_array(text: str) -> Optional[List[dict]]:
        text = text.strip()
        text = re.sub(r"^```(?:json)?\s*", "", text)
        text = re.sub(r"\s*```$", "", text)
        try:
            result = json.loads(text)
            if isinstance(result, list): return result
        except json.JSONDecodeError: pass
        m = re.search(r"\[.*\]", text, re.DOTALL)
        if m:
            try:
                result = json.loads(m.group())
                if isinstance(result, list): return result
            except json.JSONDecodeError: pass
        return None
 
    @staticmethod
    def _extract_json_object(text: str) -> Optional[dict]:
        text = text.strip()
        text = re.sub(r"^```(?:json)?\s*", "", text)
        text = re.sub(r"\s*```$", "", text)
        try:
            result = json.loads(text)
            if isinstance(result, dict):
                return result
        except json.JSONDecodeError:
            pass
        start = text.find("{")
        end = text.rfind("}")
        if start >= 0 and end > start:
            try:
                result = json.loads(text[start:end + 1])
                if isinstance(result, dict):
                    return result
            except json.JSONDecodeError:
                pass
        return None
 
    @staticmethod
    def _clean_script_line(line: str) -> str:
        line = line.strip()
        line = re.sub(r"^[-*•]\s*", "", line)
        line = re.sub(r"^<action>\s*", "", line, flags=re.I)
        line = re.sub(r"\s*</action>$", "", line, flags=re.I)
        line = re.sub(r"\s+", " ", line)
        return line.strip()
 
    @staticmethod
    def _strip_markup(text: str) -> str:
        text = re.sub(r"^[#>\s]+", "", text.strip())
        text = text.strip("*_` \t")
        return text.strip()
 
    @staticmethod
    def _character_names(characters: List[dict]) -> List[str]:
        names = []
        for item in characters:
            name = str(item.get("name", "")).strip()
            if name:
                names.append(name)
        return sorted(set(names), key=len, reverse=True)
 
    @staticmethod
    def _setting_names(settings: List[dict]) -> List[str]:
        names = []
        for item in settings:
            name = str(item.get("name", "")).strip()
            if name:
                names.append(name)
        return sorted(set(names), key=len, reverse=True)
 
    @classmethod
    def _match_characters(cls, text: str, character_names: List[str]) -> List[str]:
        """Only use the existing character list as keywords; never invent names."""
        return [name for name in character_names if name and name in text]
 
    @staticmethod
    def _setting_matches_text(setting_name: str, text: str) -> bool:
        if setting_name in text:
            return True
        normalized = re.sub(r"[()()【】\[\]\s]", "", setting_name)
        text_normalized = re.sub(r"\s", "", text)
        if normalized and normalized in text_normalized:
            return True
        base_name = re.sub(r"[((].*?[))]", "", setting_name).strip()
        return bool(base_name and base_name in text)
 
    @classmethod
    def _resolve_location(
        cls,
        text: str,
        current_location: str,
        setting_names: List[str],
    ) -> str:
        for name in setting_names:
            if cls._setting_matches_text(name, text):
                return name
        if current_location:
            return current_location
        return setting_names[0] if setting_names else ""
 
    @classmethod
    def _parse_scene_header_parts(cls, line: str, setting_names: List[str]) -> Optional[dict]:
        clean = cls._strip_markup(line)
        if not clean:
            return None
 
        # Examples:
        # **第1集-第1场 日 内 高二三班教室**
        # **1-1 夜 内 锐创科技办公室**
        header_patterns = [
            r"^第\d+集\s*[-—]\s*第\d+场\s+(日|夜|晨|傍晚|深夜)\s+(内|外)\s+(.+)$",
            r"^\d+\s*[-—_]\s*\d+\s+(日|夜|晨|傍晚|深夜)\s+(内|外)\s+(.+)$",
        ]
        for pattern in header_patterns:
            match = re.match(pattern, clean, flags=re.I)
            if not match:
                continue
            time_of_day = match.group(1).strip()
            scene_space = match.group(2).strip()
            candidate = match.group(3).strip()
            for name in setting_names:
                if cls._setting_matches_text(name, candidate):
                    return {
                        "location": name,
                        "scene_time": time_of_day,
                        "scene_space": scene_space,
                        "scene_context": clean,
                    }
            return {
                "location": candidate[:40] or clean[:40],
                "scene_time": time_of_day,
                "scene_space": scene_space,
                "scene_context": clean,
            }
        return None
 
    @classmethod
    def _parse_scene_header(cls, line: str, setting_names: List[str]) -> Optional[str]:
        parts = cls._parse_scene_header_parts(line, setting_names)
        if not parts:
            return None
        return parts.get("location")
 
    @staticmethod
    def _is_metadata_line(line: str) -> bool:
        return bool(re.match(r"^(?:剧本名称|时长|风格|类型|标题)[::]", line))
 
    @staticmethod
    def _is_end_marker(line: str) -> bool:
        return bool(re.match(r"^[((]?(?:第.+集\s*)?完[))]?$|^\(?THE END\)?$", line.strip(), flags=re.I))
 
    @staticmethod
    def _dialogue_parts(line: str) -> Optional[Tuple[str, str, str]]:
        """Return speaker, tone/action, dialogue when a line looks like dialogue."""
        match = re.match(r"^([^::]{1,24}?)(?:[((]([^))]{0,40})[))])?[::]\s*(.+)$", line)
        if not match:
            return None
        speaker = match.group(1).strip()
        tone = (match.group(2) or "").strip()
        dialogue = match.group(3).strip()
        if not dialogue:
            return None
        # Avoid treating section labels as dialogue.
        if speaker in {"人物", "场景", "画面", "镜头", "地点", "时间"}:
            return None
        return speaker, tone, dialogue
 
    @classmethod
    def _duration_for_script_unit(cls, text: str, is_dialogue: bool) -> int:
        if not is_dialogue:
            return 2
        dialogue = cls._dialogue_parts(text)
        dialogue_text = dialogue[2] if dialogue else text
        visible_text = re.sub(r"\s+", "", dialogue_text)
        duration = (len(visible_text) + 4) // 5
        return max(2, min(15, duration))
 
    @classmethod
    def _is_entry_action(cls, text: str, character_names: List[str]) -> bool:
        if cls._dialogue_parts(text):
            return False
        if character_names and not cls._match_characters(text, character_names):
            return False
        return bool(re.search(
            r"(走进|走入|进入|进来|推门而入|推门进|冲进|闯进|踏进|来到|回到|走向(?:教室|办公室|会议室|厕所|房间|门口))",
            text,
        ))
 
    @classmethod
    def _annotate_episode_script(
        cls,
        script_text: str,
        characters: List[dict],
        settings: List[dict],
    ) -> Tuple[str, List[dict]]:
        character_names = cls._character_names(characters)
        setting_names = cls._setting_names(settings)
        raw_lines = script_text.replace("\r\n", "\n").split("\n")
 
        annotated_lines: List[str] = []
        units: List[dict] = []
        current_location = setting_names[0] if setting_names else ""
        current_scene_time = ""
        current_scene_space = ""
        current_scene_context = ""
        scene_characters: List[str] = []
        scene_key = 0
 
        for line_number, raw_line in enumerate(raw_lines, 1):
            stripped = raw_line.strip()
            line = cls._clean_script_line(cls._strip_markup(stripped))
            if not line:
                continue
            if cls._is_metadata_line(line) or cls._is_end_marker(line):
                annotated_lines.append(line)
                continue
 
            header_parts = cls._parse_scene_header_parts(stripped, setting_names)
            if header_parts:
                scene_key += 1
                current_location = header_parts.get("location", current_location)
                current_scene_time = header_parts.get("scene_time", "")
                current_scene_space = header_parts.get("scene_space", "")
                current_scene_context = header_parts.get("scene_context", cls._strip_markup(stripped))
                scene_characters = cls._match_characters(line, character_names)
                annotated_lines.append(cls._strip_markup(stripped))
                continue
 
            if re.match(r"^人物[::]", line):
                scene_characters = cls._match_characters(line, character_names)
                annotated_lines.append(line)
                continue
 
            dialogue = cls._dialogue_parts(line)
            is_action = bool(re.match(r"^<action>", stripped, flags=re.I)) or bool(re.search(r"</action>$", stripped, flags=re.I))
            if not dialogue and not is_action:
                annotated_lines.append(line)
                continue
 
            unit_id = f"U{len(units) + 1:03d}"
            is_dialogue = dialogue is not None
            duration = cls._duration_for_script_unit(line, is_dialogue)
            matched_chars = cls._match_characters(line, character_names)
            unit_chars = matched_chars[:]
            speaker = ""
            tone = ""
            if dialogue:
                speaker, tone, _ = dialogue
                speaker_matches = cls._match_characters(speaker, character_names)
                if speaker_matches:
                    unit_chars = list(dict.fromkeys(speaker_matches + unit_chars))
 
            units.append({
                "unit_id": unit_id,
                "line_number": line_number,
                "text": line,
                "duration": duration,
                "is_dialogue": is_dialogue,
                "speaker": speaker,
                "tone": tone,
                "characters": unit_chars,
                "scene_characters": scene_characters[:],
                "scene_key": scene_key,
                "location": cls._resolve_location(line, current_location, setting_names),
                "scene_time": current_scene_time,
                "scene_space": current_scene_space,
                "scene_context": current_scene_context,
                "is_entry": cls._is_entry_action(line, character_names),
            })
            annotated_lines.append(f"[{duration}秒][{unit_id}] {line}")
 
        return "\n".join(annotated_lines), units
 
    @classmethod
    def _segment_plan_from_units(cls, ep_n: int, segment_number: int, items: List[dict]) -> dict:
        characters: List[str] = []
        for item in items:
            for name in item.get("characters") or item.get("scene_characters") or []:
                if name not in characters:
                    characters.append(name)
        return {
            "episode_number": ep_n,
            "segment_number": segment_number,
            "location": items[0].get("location", "") if items else "",
            "scene_time": items[0].get("scene_time", "") if items else "",
            "scene_space": items[0].get("scene_space", "") if items else "",
            "scene_context": items[0].get("scene_context", "") if items else "",
            "characters": characters,
            "total_duration": sum(int(item.get("duration") or 0) for item in items),
            "items": items,
        }
 
    @classmethod
    def _extract_plan_unit_ids(cls, seg: dict) -> List[str]:
        ids: List[str] = []
        for value in seg.get("unit_ids") or []:
            if isinstance(value, str) and re.match(r"^U\d{3,}$", value):
                ids.append(value)
        for item in seg.get("items") or []:
            if isinstance(item, dict):
                unit_id = item.get("unit_id")
                if isinstance(unit_id, str) and re.match(r"^U\d{3,}$", unit_id):
                    ids.append(unit_id)
        if not ids:
            raw = json.dumps(seg, ensure_ascii=False)
            ids = re.findall(r"\bU\d{3,}\b", raw)
        return list(dict.fromkeys(ids))
 
    @classmethod
    def _validate_segment_plan(cls, ep_n: int, raw_plan: List[dict], units: List[dict]) -> List[dict]:
        unit_by_id = {item["unit_id"]: item for item in units}
        source_ids = [item["unit_id"] for item in units]
        flat_ids: List[str] = []
        plans: List[dict] = []
 
        for seg in raw_plan:
            if not isinstance(seg, dict):
                raise ValueError("片段规划包含非对象元素")
            ids = cls._extract_plan_unit_ids(seg)
            if not ids:
                raise ValueError("片段缺少 unit_ids")
            for unit_id in ids:
                if unit_id not in unit_by_id:
                    raise ValueError(f"片段包含未知 unit_id: {unit_id}")
                if unit_id in flat_ids:
                    raise ValueError(f"片段重复引用 unit_id: {unit_id}")
 
            items = [unit_by_id[unit_id] for unit_id in ids]
            scene_keys = {item.get("scene_key") for item in items}
            if len(scene_keys) > 1:
                raise ValueError("片段跨场景,场景切换必须分片段")
            for idx, item in enumerate(items):
                if item.get("is_entry") and idx != 0:
                    raise ValueError(f"{item['unit_id']} 是入场动作,必须作为片段开头")
 
            duration = sum(int(item.get("duration") or 0) for item in items)
            if duration > cls.MAX_SEGMENT_DURATION:
                raise ValueError(f"片段时长 {duration}s 超出上限 {cls.MAX_SEGMENT_DURATION}s")
 
            flat_ids.extend(ids)
            plans.append(cls._segment_plan_from_units(ep_n, len(plans) + 1, items))
 
        if flat_ids != source_ids:
            missing = [unit_id for unit_id in source_ids if unit_id not in flat_ids]
            extra = [unit_id for unit_id in flat_ids if unit_id not in source_ids]
            raise ValueError(f"片段规划动作/台词缺漏或乱序,missing={missing}, extra={extra}")
        return plans
 
    @classmethod
    def _fallback_segment_plan(cls, ep_n: int, units: List[dict]) -> List[dict]:
        plans: List[dict] = []
        current: List[dict] = []
 
        def flush_current():
            nonlocal current
            if not current:
                return
            plans.append(cls._segment_plan_from_units(ep_n, len(plans) + 1, current))
            current = []
 
        for unit in units:
            current_duration = sum(int(item.get("duration") or 0) for item in current)
            scene_changed = current and unit.get("scene_key") != current[-1].get("scene_key")
            entry_boundary = current and unit.get("is_entry")
            would_overflow = current and current_duration + int(unit.get("duration") or 0) > cls.MAX_SEGMENT_DURATION
 
            if scene_changed or entry_boundary or would_overflow:
                flush_current()
            current.append(unit)
 
        flush_current()
        return plans
 
    @staticmethod
    def _scene_item_payload(items: List[dict]) -> List[dict]:
        return [
            {
                "unit_id": item["unit_id"],
                "duration": item["duration"],
                "text": item["text"],
                "characters": item.get("characters", []),
                "is_entry": item.get("is_entry", False),
                "scene_context": item.get("scene_context", ""),
            }
            for item in items
        ]
 
    @classmethod
    def _build_segmentation_prompt(
        cls,
        ep_n: int,
        ep_t: str,
        annotated_script: str,
        characters: List[dict],
        settings: List[dict],
        retry_error: str = "",
    ) -> str:
        template = _get_storyboard_prompt("segment_plan", "zh")
        retry_feedback = ""
        if retry_error:
            retry_feedback = f"上一次输出未通过校验,错误原因:{retry_error}\n请根据这个错误修正输出。"
        from prompts.loader import format_prompt
        return format_prompt(
            template,
            episode_number=ep_n,
            episode_title=ep_t,
            annotated_script=annotated_script,
            asset_characters=json.dumps(characters, ensure_ascii=False),
            asset_settings=json.dumps(settings, ensure_ascii=False),
            retry_feedback=retry_feedback,
        )
 
    @classmethod
    def _build_segment_design_prompt(
        cls,
        ep_n: int,
        ep_t: str,
        plan: dict,
        style: str,
        retry_error: str = "",
    ) -> str:
        items_payload = cls._scene_item_payload(plan.get("items", []))
        segment_total = sum(int(item.get("duration") or 0) for item in plan.get("items", []))
        output_total = max(cls.MIN_SEGMENT_DURATION, segment_total)
        template = _get_storyboard_prompt("segment_design", "zh")
        retry_feedback = ""
        if retry_error:
            retry_feedback = f"上一次输出未通过校验,错误原因:{retry_error}\n请根据这个错误修正输出。"
        from prompts.loader import format_prompt
        return format_prompt(
            template,
            episode_number=ep_n,
            episode_title=ep_t,
            segment_number=plan.get("segment_number"),
            location=plan.get("location", ""),
            scene_time=plan.get("scene_time", ""),
            scene_space=plan.get("scene_space", ""),
            scene_context=plan.get("scene_context", ""),
            characters=json.dumps(plan.get("characters", []), ensure_ascii=False),
            total_duration=output_total,
            items=json.dumps(items_payload, ensure_ascii=False),
            style=style,
            retry_feedback=retry_feedback,
        )
 
    async def _query_json_array_with_retries(
        self,
        prompt: str,
        llm_model: str,
        sid: str,
        *,
        label: str,
        max_retries: int = 3,
    ) -> List[dict]:
        from models.llm_client import LLM
 
        loop = asyncio.get_running_loop()
        last_error: Optional[Exception] = None
        raw = ""
        for attempt in range(max_retries):
            try:
                llm = LLM()
                raw = await loop.run_in_executor(
                    None,
                    self._cancellable_query,
                    llm,
                    prompt,
                    [],
                    llm_model,
                    False,
                    sid,
                    False,
                )
                extracted = self._extract_json_array(raw)
                if extracted is not None:
                    return extracted
                raise ValueError("模型输出不是 JSON 数组")
            except Exception as exc:
                last_error = exc
                logger.warning("[Storyboard] %s attempt %d failed: %s", label, attempt + 1, exc)
        logger.error("[Storyboard] %s failed after retries. Last raw: %s", label, raw[:2000])
        raise last_error or ValueError(f"{label} 失败")
 
    async def _query_json_object_with_retries(
        self,
        prompt: str,
        llm_model: str,
        sid: str,
        *,
        label: str,
        max_retries: int = 3,
    ) -> dict:
        from models.llm_client import LLM
 
        loop = asyncio.get_running_loop()
        last_error: Optional[Exception] = None
        raw = ""
        for attempt in range(max_retries):
            try:
                llm = LLM()
                raw = await loop.run_in_executor(
                    None,
                    self._cancellable_query,
                    llm,
                    prompt,
                    [],
                    llm_model,
                    False,
                    sid,
                    False,
                )
                extracted = self._extract_json_object(raw)
                if extracted is not None:
                    return extracted
                raise ValueError("模型输出不是 JSON 对象")
            except Exception as exc:
                last_error = exc
                logger.warning("[Storyboard] %s attempt %d failed: %s", label, attempt + 1, exc)
        logger.error("[Storyboard] %s failed after retries. Last raw: %s", label, raw[:2000])
        raise last_error or ValueError(f"{label} 失败")
 
    @classmethod
    def _normalize_shot_type(cls, value: Any, *, first: bool = False) -> str:
        text = str(value or "")
        if "全景" in text:
            shot_type = "全景"
        elif "中景" in text:
            shot_type = "中景"
        elif "近景" in text or "特写" in text:
            shot_type = "近景"
        else:
            shot_type = "中景"
        if first and shot_type not in cls.OPENING_SHOT_TYPES:
            return "中景"
        return shot_type
 
    @classmethod
    def _shot_durations_for_plan(cls, plan: dict) -> List[int]:
        durations = [int(item.get("duration") or cls.MIN_SHOT_DURATION) for item in plan.get("items", [])]
        total = sum(durations)
        if durations and total < cls.MIN_SEGMENT_DURATION:
            durations[-1] += cls.MIN_SEGMENT_DURATION - total
        return durations
 
    @classmethod
    def _opening_camera_prefix(cls, shot_type: str, characters: List[str]) -> str:
        subject = "、".join(characters) if characters else "片段中的所有人物"
        return f"{shot_type},平视机位,镜头同时拍到{subject},站位:主要人物面向镜头或彼此成自然对话关系分布。"
 
    @classmethod
    def _normalize_segment_design(cls, ep_n: int, plan: dict, raw_design: dict) -> dict:
        items = plan.get("items", [])
        raw_shots = raw_design.get("shots") if isinstance(raw_design, dict) else None
        if not isinstance(raw_shots, list) or not raw_shots:
            raise ValueError("片段设计缺少 shots")
 
        by_id = {
            shot.get("unit_id"): shot
            for shot in raw_shots
            if isinstance(shot, dict) and isinstance(shot.get("unit_id"), str)
        }
        if by_id:
            missing = [item["unit_id"] for item in items if item["unit_id"] not in by_id]
            extra = [unit_id for unit_id in by_id if unit_id not in {item["unit_id"] for item in items}]
            if missing or extra:
                raise ValueError(f"片段设计 unit_id 不匹配,missing={missing}, extra={extra}")
            ordered_raw = [by_id[item["unit_id"]] for item in items]
        else:
            if len(raw_shots) != len(items):
                raise ValueError("片段设计 shots 数量与 items 不一致")
            ordered_raw = raw_shots
 
        durations = cls._shot_durations_for_plan(plan)
        shots: List[dict] = []
        characters = plan.get("characters", [])
        for idx, (item, raw_shot) in enumerate(zip(items, ordered_raw)):
            shot_type = cls._normalize_shot_type(raw_shot.get("shot_type"), first=(idx == 0))
            content = str(raw_shot.get("content") or "").strip()
            shots.append({
                "shot_number": idx + 1,
                "shot_type": shot_type,
                "duration": durations[idx],
                "content": content,
            })
 
        return cls._segment_from_shots(
            ep_n,
            int(plan.get("segment_number") or 1),
            plan.get("location", ""),
            shots,
            characters,
            plan.get("scene_time", ""),
            plan.get("scene_space", ""),
            plan.get("scene_context", ""),
        )
 
    @classmethod
    def _fallback_design_segment(cls, ep_n: int, plan: dict) -> dict:
        characters = plan.get("characters", [])
        durations = cls._shot_durations_for_plan(plan)
        shots: List[dict] = []
        for idx, item in enumerate(plan.get("items", [])):
            is_dialogue = bool(item.get("is_dialogue"))
            speaker = item.get("speaker") or "角色"
            shot_type = "中景" if idx == 0 or is_dialogue else "全景"
            if idx == 0:
                prefix = cls._opening_camera_prefix(shot_type, characters)
                content = f"{prefix}人物朝向彼此或镜头侧前方。{item['text']}"
            elif is_dialogue:
                content = f"{shot_type},平视略侧机位拍摄{speaker},人物面向对话对象。{item['text']}"
            else:
                subject = "、".join(item.get("characters") or characters) or "场景主体"
                content = f"{shot_type},平视跟拍{subject},人物沿动作方向移动。{item['text']}"
            shots.append({
                "shot_number": idx + 1,
                "shot_type": shot_type,
                "duration": durations[idx],
                "content": content,
            })
        return cls._segment_from_shots(
            ep_n,
            int(plan.get("segment_number") or 1),
            plan.get("location", ""),
            shots,
            characters,
            plan.get("scene_time", ""),
            plan.get("scene_space", ""),
            plan.get("scene_context", ""),
        )
 
    @staticmethod
    def _staging_continuity_payload(segments: List[dict]) -> List[dict]:
        payload: List[dict] = []
        for seg in segments:
            payload.append({
                "segment_number": seg.get("segment_number"),
                "location": seg.get("location", ""),
                "scene_context": seg.get("scene_context", ""),
                "scene_space": seg.get("scene_space", ""),
                "characters": seg.get("characters", []),
                "shots": [
                    {
                        "shot_number": shot.get("shot_number"),
                        "shot_type": shot.get("shot_type", ""),
                        "content": shot.get("content", ""),
                    }
                    for shot in seg.get("shots", [])
                    if isinstance(shot, dict)
                ],
            })
        return payload
 
    @classmethod
    def _build_staging_continuity_prompt(
        cls,
        ep_n: int,
        ep_t: str,
        segments: List[dict],
        retry_error: str = "",
    ) -> str:
        template = _get_storyboard_prompt("staging_continuity", "zh")
        retry_feedback = ""
        if retry_error:
            retry_feedback = f"上一次输出未通过校验,错误原因:{retry_error}\n请根据这个错误修正输出。"
        from prompts.loader import format_prompt
        return format_prompt(
            template,
            episode_number=ep_n,
            episode_title=ep_t,
            segments=json.dumps(cls._staging_continuity_payload(segments), ensure_ascii=False, indent=2),
            retry_feedback=retry_feedback,
        )
 
    @staticmethod
    def _apply_staging_continuity_patches(segments: List[dict], review: dict) -> int:
        patches = review.get("patches") if isinstance(review, dict) else None
        if not isinstance(patches, list):
            raise ValueError("站位连续性检查输出缺少 patches 数组")
 
        segment_by_number = {
            int(seg.get("segment_number") or 0): seg
            for seg in segments
            if isinstance(seg, dict)
        }
        updates: List[Tuple[dict, str]] = []
        for patch in patches:
            if not isinstance(patch, dict):
                raise ValueError("站位连续性补丁包含非对象元素")
            try:
                segment_number = int(patch.get("segment_number"))
                shot_number = int(patch.get("shot_number"))
            except (TypeError, ValueError):
                raise ValueError(f"站位连续性补丁编号无效: {patch}")
            content = str(patch.get("content") or "").strip()
            if not content:
                raise ValueError(f"站位连续性补丁 content 为空: {patch}")
 
            seg = segment_by_number.get(segment_number)
            if not seg:
                raise ValueError(f"站位连续性补丁引用未知片段: {segment_number}")
            shot = next(
                (item for item in seg.get("shots", []) if int(item.get("shot_number") or 0) == shot_number),
                None,
            )
            if not shot:
                raise ValueError(f"站位连续性补丁引用未知分镜: segment={segment_number}, shot={shot_number}")
            updates.append((shot, content))
 
        for shot, content in updates:
            shot["content"] = content
        return len(updates)
 
    async def _fix_episode_staging_continuity(
        self,
        ep_n: int,
        ep_t: str,
        segments: List[dict],
        llm_model: str,
        sid: str,
    ) -> List[dict]:
        last_error: Optional[Exception] = None
        for attempt in range(2):
            try:
                prompt = self._build_staging_continuity_prompt(
                    ep_n,
                    ep_t,
                    segments,
                    retry_error=str(last_error) if last_error else "",
                )
                review = await self._query_json_object_with_retries(
                    prompt,
                    llm_model,
                    sid,
                    label=f"第 {ep_n} 集人物站位连续性检查",
                    max_retries=1,
                )
                applied = self._apply_staging_continuity_patches(segments, review)
                issues = review.get("issues") if isinstance(review, dict) else []
                if applied:
                    logger.info("[Storyboard] Episode %s staging continuity fixed %d shots. issues=%s", ep_n, applied, issues)
                else:
                    logger.info("[Storyboard] Episode %s staging continuity passed.", ep_n)
                return segments
            except Exception as exc:
                last_error = exc
                logger.warning("[Storyboard] Episode %s staging continuity attempt %d failed: %s", ep_n, attempt + 1, exc)
        logger.warning("[Storyboard] Episode %s staging continuity check skipped after retries: %s", ep_n, last_error)
        return segments
 
    async def _plan_episode_segments(
        self,
        ep_n: int,
        ep_t: str,
        annotated_script: str,
        units: List[dict],
        characters: List[dict],
        settings: List[dict],
        llm_model: str,
        sid: str,
    ) -> List[dict]:
        last_error: Optional[Exception] = None
        for attempt in range(3):
            try:
                prompt = self._build_segmentation_prompt(
                    ep_n,
                    ep_t,
                    annotated_script,
                    characters,
                    settings,
                    retry_error=str(last_error) if last_error else "",
                )
                raw_plan = await self._query_json_array_with_retries(
                    prompt,
                    llm_model,
                    sid,
                    label=f"第 {ep_n} 集片段规划",
                    max_retries=1,
                )
                return self._validate_segment_plan(ep_n, raw_plan, units)
            except Exception as exc:
                last_error = exc
                logger.warning("[Storyboard] Episode %s segment plan validation attempt %d failed: %s", ep_n, attempt + 1, exc)
 
        logger.warning("[Storyboard] Episode %s falling back to deterministic segment plan: %s", ep_n, last_error)
        return self._fallback_segment_plan(ep_n, units)
 
    async def _design_one_segment(
        self,
        ep_n: int,
        ep_t: str,
        plan: dict,
        style: str,
        llm_model: str,
        sid: str,
    ) -> dict:
        last_error: Optional[Exception] = None
        for attempt in range(3):
            try:
                prompt = self._build_segment_design_prompt(
                    ep_n,
                    ep_t,
                    plan,
                    style,
                    retry_error=str(last_error) if last_error else "",
                )
                raw_design = await self._query_json_object_with_retries(
                    prompt,
                    llm_model,
                    sid,
                    label=f"第 {ep_n} 集片段 {plan.get('segment_number')} 分镜设计",
                    max_retries=1,
                )
                return self._normalize_segment_design(ep_n, plan, raw_design)
            except Exception as exc:
                last_error = exc
                logger.warning(
                    "[Storyboard] Episode %s segment %s design attempt %d failed: %s",
                    ep_n,
                    plan.get("segment_number"),
                    attempt + 1,
                    exc,
                )
 
        logger.warning(
            "[Storyboard] Episode %s segment %s falling back to deterministic design: %s",
            ep_n,
            plan.get("segment_number"),
            last_error,
        )
        return self._fallback_design_segment(ep_n, plan)
 
    async def _design_episode_storyboard(
        self,
        ep_n: int,
        ep_t: str,
        ep_c: str,
        characters: List[dict],
        settings: List[dict],
        style: str,
        llm_model: str,
        sid: str,
        progress_note: Optional[Callable[[str], None]] = None,
    ) -> List[dict]:
        annotated_script, units = self._annotate_episode_script(ep_c, characters, settings)
        if not units:
            raise Exception(f"第 {ep_n} 集未能识别出动作或台词")
 
        logger.info("[Storyboard] Episode %s annotated %d script units", ep_n, len(units))
        if progress_note:
            progress_note(f"第 {ep_n} 集已完成时长标注,正在划分片段")
        plans = await self._plan_episode_segments(ep_n, ep_t, annotated_script, units, characters, settings, llm_model, sid)
        if not plans:
            raise Exception(f"第 {ep_n} 集片段规划失败")
 
        logger.info("[Storyboard] Episode %s planned %d segments; designing in parallel", ep_n, len(plans))
        if progress_note:
            progress_note(f"第 {ep_n} 集已规划 {len(plans)} 个片段,正在设计分镜")
        tasks = [
            self._design_one_segment(ep_n, ep_t, plan, style, llm_model, sid)
            for plan in plans
        ]
        segments = await asyncio.gather(*tasks)
        segments.sort(key=lambda item: int(item.get("segment_number") or 0))
        logger.info("[Storyboard] Episode %s checking staging continuity", ep_n)
        if progress_note:
            progress_note(f"第 {ep_n} 集正在检查人物站位连续性")
        segments = await self._fix_episode_staging_continuity(ep_n, ep_t, segments, llm_model, sid)
        return segments
 
    @classmethod
    def _estimate_duration(cls, text: str, is_dialogue: bool) -> int:
        """Legacy heuristic duration in seconds."""
        visible_text = re.sub(r"[“”\"',。!?、,.!?;;::\s]", "", text)
        if is_dialogue:
            duration = 3 + len(visible_text) // 18
        else:
            duration = 3 + len(visible_text) // 34
        return max(cls.MIN_SHOT_DURATION, min(duration, cls.MAX_SEGMENT_DURATION))
 
    @staticmethod
    def _infer_shot_type(text: str, is_dialogue: bool) -> str:
        if re.search(r"城市|办公室|窗外|全景|拉远|场景|夜景|两台电脑|屏幕并排", text):
            return "全景"
        if re.search(r"特写|屏幕|手机|键盘|终端|报表|手指|代码|PASS|眼睛|嘴角|表情", text):
            return "近景"
        if is_dialogue:
            return "中景"
        return "中景"
 
    @classmethod
    def _normalize_first_shot_type(cls, shots: List[dict]) -> None:
        if not shots:
            return
        if shots[0].get("shot_type") not in cls.OPENING_SHOT_TYPES:
            shots[0]["shot_type"] = "中景"
            content = shots[0].get("content", "")
            shots[0]["content"] = re.sub(r"^(近景|过肩近景|特写|大特写)", "中景", content, count=1)
 
    @classmethod
    def _ensure_segment_duration(cls, shots: List[dict]) -> int:
        total = sum(int(shot.get("duration") or cls.MIN_SHOT_DURATION) for shot in shots)
        if shots and total < cls.MIN_SEGMENT_DURATION:
            shots[-1]["duration"] += cls.MIN_SEGMENT_DURATION - total
            total = cls.MIN_SEGMENT_DURATION
        return min(total, cls.MAX_SEGMENT_DURATION)
 
    @classmethod
    def _make_shot_content(
        cls,
        *,
        shot_type: str,
        line: str,
        characters: List[str],
        is_dialogue: bool,
        speaker: str = "",
        tone: str = "",
    ) -> str:
        if is_dialogue:
            tone_text = tone or "自然、贴合当下情绪"
            return f"{shot_type},镜头对准{speaker or '角色'},呈现动作与表情变化。{speaker}说:“{line}”。音色:{tone_text}。"
        subject = "、".join(characters) if characters else "场景主体"
        return f"{shot_type},镜头呈现{subject}。{line}"
 
    @classmethod
    def _segment_from_shots(
        cls,
        ep_n: int,
        segment_number: int,
        location: str,
        shots: List[dict],
        characters: List[str],
        scene_time: str = "",
        scene_space: str = "",
        scene_context: str = "",
    ) -> dict:
        for idx, shot in enumerate(shots, 1):
            shot["shot_number"] = idx
            shot["duration"] = min(
                cls.MAX_SEGMENT_DURATION,
                max(cls.MIN_SHOT_DURATION, int(shot.get("duration") or cls.MIN_SHOT_DURATION)),
            )
        cls._normalize_first_shot_type(shots)
        total_duration = cls._ensure_segment_duration(shots)
        segment = {
            "segment_id": f"seg_{ep_n:02d}_{segment_number:02d}",
            "segment_number": segment_number,
            "total_duration": total_duration,
            "location": location,
            "characters": characters,
            "shots": shots,
            "episode_number": ep_n,
        }
        if scene_time:
            segment["scene_time"] = scene_time
        if scene_space:
            segment["scene_space"] = scene_space
        if scene_context:
            segment["scene_context"] = scene_context
        return segment
 
    @classmethod
    def _build_segments_by_regex(
        cls,
        ep_n: int,
        script_text: str,
        characters: List[dict],
        settings: List[dict],
    ) -> List[dict]:
        """Deterministically parse script text into model-ready segments.
 
        This path intentionally mirrors the LLM prompt output:
        segment_number, total_duration, location, characters, shots[].
        If it cannot extract useful shots, the caller falls back to LLM.
        """
        character_names = cls._character_names(characters)
        setting_names = cls._setting_names(settings)
        raw_lines = [cls._strip_markup(line) for line in script_text.replace("\r\n", "\n").split("\n")]
 
        atomic_shots: List[dict] = []
        current_location = setting_names[0] if setting_names else ""
        current_scene_time = ""
        current_scene_space = ""
        current_scene_context = ""
        scene_characters: List[str] = []
        scene_key = 0
        found_scene_header = False
 
        for raw_line in raw_lines:
            line = cls._clean_script_line(raw_line)
            if not line or cls._is_metadata_line(line) or cls._is_end_marker(line):
                continue
            if re.match(r"^第?\d+集$", line):
                continue
 
            header_parts = cls._parse_scene_header_parts(line, setting_names)
            if header_parts:
                found_scene_header = True
                scene_key += 1
                current_location = header_parts.get("location", current_location)
                current_scene_time = header_parts.get("scene_time", "")
                current_scene_space = header_parts.get("scene_space", "")
                current_scene_context = header_parts.get("scene_context", line)
                matched = cls._match_characters(line, character_names)
                if matched:
                    scene_characters = matched
                continue
 
            if re.match(r"^人物[::]", line):
                scene_characters = cls._match_characters(line, character_names)
                continue
 
            matched_chars = cls._match_characters(line, character_names)
            shot_chars = matched_chars or scene_characters
            dialogue = cls._dialogue_parts(line)
            is_dialogue = dialogue is not None
            speaker = ""
            tone = ""
            shot_text = line
            if dialogue:
                speaker, tone, shot_text = dialogue
                speaker_matches = cls._match_characters(speaker, character_names)
                if speaker_matches:
                    shot_chars = list(dict.fromkeys(speaker_matches + shot_chars))
                elif speaker in {"旁白", "独白", "画外音"}:
                    shot_chars = shot_chars or scene_characters
 
            shot_type = cls._infer_shot_type(line, is_dialogue)
            duration = cls._estimate_duration(shot_text, is_dialogue)
            location = cls._resolve_location(line, current_location, setting_names)
 
            atomic_shots.append({
                "scene_key": scene_key,
                "location": location,
                "scene_time": current_scene_time,
                "scene_space": current_scene_space,
                "scene_context": current_scene_context,
                "characters": shot_chars,
                "shot": {
                    "shot_number": 0,
                    "shot_type": shot_type,
                    "duration": duration,
                    "content": cls._make_shot_content(
                        shot_type=shot_type,
                        line=shot_text,
                        characters=shot_chars,
                        is_dialogue=is_dialogue,
                        speaker=speaker,
                        tone=tone,
                    ),
                },
            })
 
        if not found_scene_header or not atomic_shots:
            return []
 
        segments: List[dict] = []
        current_items: List[dict] = []
        current_scene_key: Optional[int] = None
        current_location = ""
        current_scene_time = ""
        current_scene_space = ""
        current_scene_context = ""
        current_chars: List[str] = []
 
        def flush_current():
            nonlocal current_items, current_scene_key, current_location
            nonlocal current_scene_time, current_scene_space, current_scene_context, current_chars
            if not current_items:
                return
            shots = [item["shot"] for item in current_items]
            chars: List[str] = []
            for item in current_items:
                for name in item["characters"]:
                    if name not in chars:
                        chars.append(name)
            if not chars:
                chars = current_chars
            segments.append(cls._segment_from_shots(
                ep_n,
                len(segments) + 1,
                current_location,
                shots,
                chars,
                current_scene_time,
                current_scene_space,
                current_scene_context,
            ))
            current_items = []
            current_scene_key = None
            current_location = ""
            current_scene_time = ""
            current_scene_space = ""
            current_scene_context = ""
            current_chars = []
 
        for item in atomic_shots:
            item_duration = int(item["shot"]["duration"])
            current_duration = sum(int(existing["shot"]["duration"]) for existing in current_items)
            scene_changed = current_items and (
                item["scene_key"] != current_scene_key or item["location"] != current_location
            )
            would_overflow = current_items and current_duration + item_duration > cls.MAX_SEGMENT_DURATION
 
            # A scene switch always starts a new video-model call. Otherwise greedily
            # pack shots until the next one would exceed the 15s upper bound.
            if scene_changed or would_overflow:
                flush_current()
 
            current_items.append(item)
            current_scene_key = item["scene_key"]
            current_location = item["location"]
            current_scene_time = item.get("scene_time", "")
            current_scene_space = item.get("scene_space", "")
            current_scene_context = item.get("scene_context", "")
            for name in item["characters"]:
                if name not in current_chars:
                    current_chars.append(name)
 
        flush_current()
        return segments
 
    @classmethod
    def _normalize_llm_segments(cls, ep_n: int, extracted: List[dict]) -> List[dict]:
        valid_segments = []
        for seg in extracted:
            if not isinstance(seg, dict):
                continue
 
            shots = seg.get("shots", [])
            pending_shots = []
            for s in shots:
                if not isinstance(s, dict):
                    continue
                dur = s.get("duration", cls.MIN_SHOT_DURATION)
                try:
                    dur = int(dur)
                except (TypeError, ValueError):
                    dur = cls.MIN_SHOT_DURATION
                pending_shots.append({
                    "shot_number": 0,
                    "shot_type": s.get("shot_type", "中景"),
                    "duration": max(cls.MIN_SHOT_DURATION, dur),
                    "content": s.get("content", "")
                })
 
            if not pending_shots:
                continue
 
            chunk: List[dict] = []
 
            def flush_chunk():
                nonlocal chunk
                if not chunk:
                    return
                valid_segments.append(cls._segment_from_shots(
                    ep_n,
                    len(valid_segments) + 1,
                    seg.get("location", ""),
                    chunk,
                    seg.get("characters", []),
                ))
                chunk = []
 
            for shot in pending_shots:
                chunk_duration = sum(int(item.get("duration") or cls.MIN_SHOT_DURATION) for item in chunk)
                if chunk and chunk_duration + int(shot["duration"]) > cls.MAX_SEGMENT_DURATION:
                    flush_chunk()
                chunk.append(shot)
            flush_chunk()
        return valid_segments
 
    @staticmethod
    def _validate_episodes(episodes: List[dict]) -> List[dict]:
        """验证嵌套的 Episode -> Segment -> Shots 结构"""
        valid_episodes = []
        for ep in episodes:
            if not isinstance(ep, dict): continue
            
            segments = ep.get("segments", [])
            valid_segments = []
            for idx, seg in enumerate(segments, 1):
                if not isinstance(seg, dict): continue
                
                shots = seg.get("shots", [])
                valid_shots = []
                calc_total_duration = 0
                
                for s in shots:
                    if not isinstance(s, dict): continue
                    dur = s.get("duration", 5)
                    calc_total_duration += dur
                    valid_shots.append({
                        "shot_number": s.get("shot_number", len(valid_shots) + 1),
                        "shot_type": s.get("shot_type", "中景"),
                        "duration": dur,
                        "content": s.get("content", "")
                    })
                
                valid_segments.append({
                    "segment_id": seg.get("segment_id", f"seg_{str(idx).zfill(8)}"),
                    "segment_number": seg.get("segment_number", len(valid_segments) + 1),
                    "total_duration": seg.get("total_duration", calc_total_duration),
                    "location": seg.get("location", ""),
                    "scene_time": seg.get("scene_time", ""),
                    "scene_space": seg.get("scene_space", ""),
                    "scene_context": seg.get("scene_context", ""),
                    "characters": seg.get("characters", []),
                    "shots": valid_shots
                })
            
            valid_episodes.append({
                "episode_number": ep.get("episode_number", len(valid_episodes) + 1),
                "episode_title": ep.get("episode_title", ""),
                "segments": valid_segments
            })
        return valid_episodes
 
    async def process(self, input_data: Any, intervention: Optional[Dict] = None) -> Dict:
        input_data = self._merge_session_params(input_data)
        sid = input_data.get("session_id")
        if not sid: raise Exception("Missing session_id")
        artifacts = self._session_artifacts(input_data)
        session_meta = self._session_meta(input_data)
            
        llm_model = input_data.get("llm_model") or session_meta.get("llm_model")
        if not llm_model:
            raise ValueError("Missing required model configuration: llm_model")
        style = input_data.get("style") or session_meta.get("style") or "anime"
        
        # 处理人工干预/修改
        if intervention and "modified_storyboard" in intervention:
            modified_episodes = intervention["modified_storyboard"]
            if isinstance(modified_episodes, str): modified_episodes = json.loads(modified_episodes)
            return {
                "payload": {
                    "session_id": sid,
                    "episodes": modified_episodes,
                    "user_modified": True,
                    "updated_at": datetime.now().isoformat(),
                },
                "stage_completed": True,
            }
        
        script_data = artifacts.get("script_generation", {})
        if not script_data: raise Exception("未找到剧本数据")
        
        episodes = script_data.get("episodes", [])
        if not episodes:
            raise Exception("剧本数据中不包含有效集数列表(episodes)")
 
        # 检查是否有已存在的分镜数据,识别需要生成的集数
        existing_storyboard = artifacts.get("storyboard", {})
        existing_story_eps = existing_storyboard.get("episodes", [])
        
        # 建立已生成的 segments 索引
        ready_eps = {e["episode_number"] for e in existing_story_eps if e.get("segments")}
        
        # 确定需要处理的集数:如果该集还没有 segments,则需要生成
        episodes_to_proc = [ep for ep in episodes if ep.get("episode_number") not in ready_eps]
        
        if not episodes_to_proc:
            logger.info("[Storyboard] All episodes already have storyboard segments. Skipping generation.")
            return {"payload": {"session_id": sid, "episodes": existing_story_eps}, "stage_completed": True}
 
        chars = script_data.get("characters", [])
        sets = script_data.get("settings", [])
        
        self._report_progress("分镜", f"开始设计 {len(episodes_to_proc)} 集的分镜...", 5)
        total_to_process = len(episodes_to_proc)
        completed_count = 0
        progress_state = {"percent": 10}
 
        def report_storyboard_note(message: str, percent: Optional[int] = None, data: Optional[dict] = None):
            if percent is not None:
                progress_state["percent"] = max(progress_state["percent"], percent)
            self._report_progress("分镜设计", message, progress_state["percent"], data)
        
        async def proc_ep(ep):
            ep_n = ep.get("episode_number", 1)
            ep_t = ep.get("act_title", f"第{ep_n}集")
            ep_c = ep.get("content", "")
            report_storyboard_note(f"正在处理第 {ep_n} 集分镜")
            segments = await self._design_episode_storyboard(
                ep_n,
                ep_t,
                ep_c,
                chars,
                sets,
                style,
                llm_model,
                sid,
                progress_note=report_storyboard_note,
            )
 
            return {
                "episode_number": ep_n,
                "episode_title": ep_t,
                "segments": segments
            }
 
        # 核心:支持流式推送增量产物,让前端能看到实时进度
        updated_ep_map = {e["episode_number"]: e for e in existing_story_eps}
        
        # 报告一次进度,带上 assets_preview 让编排器更新内存中的初步数据
        report_storyboard_note("准备生成分镜...", 10, {
            "assets_preview": {
                "session_id": sid,
                "episodes": sorted(updated_ep_map.values(), key=lambda x: x["episode_number"]),
                "created_at": datetime.now().isoformat(),
            },
            "persist": True,
        })
 
        results_queue = [asyncio.create_task(proc_ep(ep)) for ep in episodes_to_proc]
 
        for coro in asyncio.as_completed(results_queue):
            res = await coro
            completed_count += 1
            updated_ep_map[res["episode_number"]] = res
            
            # 每完成一集分镜,通过编排器更新内存并受控持久化
            temp_eps = sorted(updated_ep_map.values(), key=lambda x: x["episode_number"])
            
            # 并发多集时只按已完成集数推进全局进度,避免各集内部进度互相覆盖。
            pct = min(95, 10 + int(85 * completed_count / max(total_to_process, 1)))
            report_storyboard_note(
                f"已完成 {completed_count}/{total_to_process} 集分镜:第 {res['episode_number']} 集",
                pct,
                {
                    "assets_preview": {
                        "session_id": sid,
                        "episodes": temp_eps,
                        "created_at": datetime.now().isoformat(),
                    },
                    "persist": True,
                },
            )
 
        final_all_episodes = sorted(updated_ep_map.values(), key=lambda x: x["episode_number"])
        
        self._report_progress("分镜", "完成", 100)
        return {"payload": {"session_id": sid, "episodes": final_all_episodes}, "stage_completed": True}