@clipen/clipen-cli 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -2
  3. package/apps/web/dist/assets/index-D5D2HiB1.js +14 -0
  4. package/apps/web/dist/assets/index-DIMuOXSu.css +1 -0
  5. package/apps/web/dist/favicon.svg +1 -0
  6. package/apps/web/dist/index.html +16 -0
  7. package/bin/clipen.js +157 -0
  8. package/clipen +18 -0
  9. package/install +25 -0
  10. package/package.json +55 -4
  11. package/pyproject.toml +44 -0
  12. package/requirements/asr-runtime.txt +13 -0
  13. package/requirements/dev.txt +2 -0
  14. package/requirements/runtime.txt +20 -0
  15. package/src/clipen/__init__.py +3 -0
  16. package/src/clipen/analysis/__init__.py +89 -0
  17. package/src/clipen/analysis/artifacts.py +410 -0
  18. package/src/clipen/analysis/audio.py +394 -0
  19. package/src/clipen/analysis/evidence.py +102 -0
  20. package/src/clipen/analysis/evidence_service.py +929 -0
  21. package/src/clipen/analysis/image_similarity.py +134 -0
  22. package/src/clipen/analysis/keyframes.py +159 -0
  23. package/src/clipen/analysis/models.py +228 -0
  24. package/src/clipen/analysis/qwen_worker.py +594 -0
  25. package/src/clipen/analysis/resources.py +22 -0
  26. package/src/clipen/analysis/runtime.py +62 -0
  27. package/src/clipen/analysis/service.py +594 -0
  28. package/src/clipen/analysis/setup.py +921 -0
  29. package/src/clipen/analysis/setup_cli.py +107 -0
  30. package/src/clipen/analysis/storage.py +1864 -0
  31. package/src/clipen/analysis/transcription.py +906 -0
  32. package/src/clipen/analysis/visual.py +237 -0
  33. package/src/clipen/annotations/__init__.py +13 -0
  34. package/src/clipen/annotations/models.py +22 -0
  35. package/src/clipen/annotations/storage.py +428 -0
  36. package/src/clipen/annotations/suggestions.py +115 -0
  37. package/src/clipen/asr_release.py +132 -0
  38. package/src/clipen/bootstrap.py +978 -0
  39. package/src/clipen/candidates.py +222 -0
  40. package/src/clipen/cli.py +84 -0
  41. package/src/clipen/controller/__init__.py +17 -0
  42. package/src/clipen/controller/app.py +1657 -0
  43. package/src/clipen/controller/server.py +533 -0
  44. package/src/clipen/domain/__init__.py +30 -0
  45. package/src/clipen/domain/errors.py +5 -0
  46. package/src/clipen/domain/media_time.py +110 -0
  47. package/src/clipen/domain/timeline.py +582 -0
  48. package/src/clipen/domain/validation.py +14 -0
  49. package/src/clipen/editing/EDITORIAL_REGRESSIONS.md +38 -0
  50. package/src/clipen/editing/PROGRAM_FORMAT.md +102 -0
  51. package/src/clipen/editing/README.md +115 -0
  52. package/src/clipen/editing/REFACTOR_DESIGN.md +241 -0
  53. package/src/clipen/editing/REFACTOR_HANDOFF.md +73 -0
  54. package/src/clipen/editing/__init__.py +15 -0
  55. package/src/clipen/editing/agent.py +13 -0
  56. package/src/clipen/editing/agent_models.py +127 -0
  57. package/src/clipen/editing/agent_repository.py +978 -0
  58. package/src/clipen/editing/agent_tools.py +613 -0
  59. package/src/clipen/editing/alignment.py +150 -0
  60. package/src/clipen/editing/alignment_setup.py +156 -0
  61. package/src/clipen/editing/alignment_worker.py +24 -0
  62. package/src/clipen/editing/change_report.py +128 -0
  63. package/src/clipen/editing/compiler.py +40 -0
  64. package/src/clipen/editing/component_looks.py +131 -0
  65. package/src/clipen/editing/component_review.py +74 -0
  66. package/src/clipen/editing/coordinator.py +497 -0
  67. package/src/clipen/editing/editorial_review.py +309 -0
  68. package/src/clipen/editing/evidence_jobs.py +552 -0
  69. package/src/clipen/editing/history_changes.py +192 -0
  70. package/src/clipen/editing/music_choices.py +18 -0
  71. package/src/clipen/editing/production_catalog.py +629 -0
  72. package/src/clipen/editing/production_limits.py +85 -0
  73. package/src/clipen/editing/production_media.py +435 -0
  74. package/src/clipen/editing/production_preview.py +373 -0
  75. package/src/clipen/editing/production_progress.py +236 -0
  76. package/src/clipen/editing/production_requirements.py +117 -0
  77. package/src/clipen/editing/production_tools.py +1198 -0
  78. package/src/clipen/editing/program_audio.py +248 -0
  79. package/src/clipen/editing/program_direction.py +83 -0
  80. package/src/clipen/editing/program_story.py +91 -0
  81. package/src/clipen/editing/program_subtitles.py +232 -0
  82. package/src/clipen/editing/programme_review.py +121 -0
  83. package/src/clipen/editing/prompt_history.py +84 -0
  84. package/src/clipen/editing/recut.py +167 -0
  85. package/src/clipen/editing/refinement_references.py +278 -0
  86. package/src/clipen/editing/resolve_export.py +847 -0
  87. package/src/clipen/editing/resolve_titles.py +291 -0
  88. package/src/clipen/editing/skill_tools.py +951 -0
  89. package/src/clipen/editing/source_dialogue.py +192 -0
  90. package/src/clipen/editing/speech_coverage.py +50 -0
  91. package/src/clipen/editing/speech_quality.py +11 -0
  92. package/src/clipen/editing/tool_receipts.py +98 -0
  93. package/src/clipen/editing/versions.py +219 -0
  94. package/src/clipen/editing/visual_preview.py +189 -0
  95. package/src/clipen/editing/workbench.py +136 -0
  96. package/src/clipen/filesystem.py +25 -0
  97. package/src/clipen/finishing/README.md +103 -0
  98. package/src/clipen/finishing/__init__.py +1 -0
  99. package/src/clipen/finishing/audio.py +188 -0
  100. package/src/clipen/finishing/catalog.py +264 -0
  101. package/src/clipen/finishing/models.py +14 -0
  102. package/src/clipen/finishing/renderer.py +45 -0
  103. package/src/clipen/mcp_editing/__init__.py +5 -0
  104. package/src/clipen/mcp_editing/__main__.py +42 -0
  105. package/src/clipen/mcp_editing/server.py +396 -0
  106. package/src/clipen/media/__init__.py +50 -0
  107. package/src/clipen/media/discovery.py +253 -0
  108. package/src/clipen/media/models.py +349 -0
  109. package/src/clipen/media/preview.py +137 -0
  110. package/src/clipen/media/probe.py +418 -0
  111. package/src/clipen/media/service.py +304 -0
  112. package/src/clipen/media/storage.py +694 -0
  113. package/src/clipen/music_vocabulary.py +34 -0
  114. package/src/clipen/power.py +28 -0
  115. package/src/clipen/projects/__init__.py +26 -0
  116. package/src/clipen/projects/agent_schema.py +94 -0
  117. package/src/clipen/projects/annotation_schema.py +40 -0
  118. package/src/clipen/projects/deletion.py +256 -0
  119. package/src/clipen/projects/evidence_schema.py +52 -0
  120. package/src/clipen/projects/finishing_schema.py +36 -0
  121. package/src/clipen/projects/models.py +189 -0
  122. package/src/clipen/projects/picker.py +232 -0
  123. package/src/clipen/projects/preview_schema.py +29 -0
  124. package/src/clipen/projects/service.py +159 -0
  125. package/src/clipen/projects/storage.py +1458 -0
  126. package/src/clipen/projects/timeline_schema.py +32 -0
  127. package/src/clipen/projects/validation_schema.py +28 -0
  128. package/src/clipen/provider_registry.py +77 -0
  129. package/src/clipen/providers/__init__.py +43 -0
  130. package/src/clipen/providers/account.py +245 -0
  131. package/src/clipen/providers/claude.py +409 -0
  132. package/src/clipen/providers/claude_editing.py +118 -0
  133. package/src/clipen/providers/codex.py +991 -0
  134. package/src/clipen/providers/codex_editing.py +91 -0
  135. package/src/clipen/providers/editing_agent.py +567 -0
  136. package/src/clipen/providers/errors.py +51 -0
  137. package/src/clipen/providers/recovery.py +269 -0
  138. package/src/clipen/py.typed +1 -0
  139. package/src/clipen/rendering/__init__.py +12 -0
  140. package/src/clipen/rendering/cover.py +139 -0
  141. package/src/clipen/rendering/custom_components.py +328 -0
  142. package/src/clipen/rendering/custom_render.py +218 -0
  143. package/src/clipen/rendering/fonts/anton-OFL.txt +93 -0
  144. package/src/clipen/rendering/fonts/bebasneue-OFL.txt +93 -0
  145. package/src/clipen/rendering/fonts/caveat-OFL.txt +93 -0
  146. package/src/clipen/rendering/fonts/chenyu-OFL.txt +93 -0
  147. package/src/clipen/rendering/fonts/delagothicone-OFL.txt +93 -0
  148. package/src/clipen/rendering/fonts/hachimarupop-OFL.txt +93 -0
  149. package/src/clipen/rendering/fonts/iansui-OFL.txt +93 -0
  150. package/src/clipen/rendering/fonts/manifest.json +145 -0
  151. package/src/clipen/rendering/fonts/playfairdisplay-OFL.txt +93 -0
  152. package/src/clipen/rendering/fonts/rocknrollone-OFL.txt +93 -0
  153. package/src/clipen/rendering/fonts/yomogi-OFL.txt +94 -0
  154. package/src/clipen/rendering/fonts/yuseimagic-OFL.txt +93 -0
  155. package/src/clipen/rendering/fonts/zenkurenaido-OFL.txt +93 -0
  156. package/src/clipen/rendering/fonts/zenmarugothic-OFL.txt +93 -0
  157. package/src/clipen/rendering/production_effects.py +96 -0
  158. package/src/clipen/rendering/program_components.py +254 -0
  159. package/src/clipen/rendering/program_creator.py +804 -0
  160. package/src/clipen/rendering/program_identities.py +350 -0
  161. package/src/clipen/rendering/program_motion.py +186 -0
  162. package/src/clipen/rendering/program_studio.py +227 -0
  163. package/src/clipen/rendering/program_styles.py +321 -0
  164. package/src/clipen/rendering/program_subtitles.py +89 -0
  165. package/src/clipen/rendering/renderer.py +737 -0
  166. package/src/clipen/rendering/storage.py +230 -0
  167. package/src/clipen/rendering/titles.py +467 -0
  168. package/src/clipen/skills/__init__.py +5 -0
  169. package/src/clipen/skills/builtin/SKILL.md +72 -0
  170. package/src/clipen/skills/builtin/assets/sample-daily-vlog.mp4 +0 -0
  171. package/src/clipen/skills/builtin/assets/sample-event-recap.mp4 +0 -0
  172. package/src/clipen/skills/builtin/assets/sample-food-review.mp4 +0 -0
  173. package/src/clipen/skills/builtin/assets/sample-product-demo.mp4 +0 -0
  174. package/src/clipen/skills/builtin/assets/sample-product-review.mp4 +0 -0
  175. package/src/clipen/skills/builtin/assets/sample-product-unboxing.mp4 +0 -0
  176. package/src/clipen/skills/builtin/assets/sample-shorts-highlight.mp4 +0 -0
  177. package/src/clipen/skills/builtin/assets/sample-travel-highlight.mp4 +0 -0
  178. package/src/clipen/skills/builtin/assets/sample-travel-vlog.mp4 +0 -0
  179. package/src/clipen/skills/builtin/assets/style-cinema.png +0 -0
  180. package/src/clipen/skills/builtin/assets/style-editorial.png +0 -0
  181. package/src/clipen/skills/builtin/assets/style-tpl-daily-vlog.png +0 -0
  182. package/src/clipen/skills/builtin/assets/style-tpl-event-recap.png +0 -0
  183. package/src/clipen/skills/builtin/assets/style-tpl-food-review.png +0 -0
  184. package/src/clipen/skills/builtin/assets/style-tpl-product-demo.png +0 -0
  185. package/src/clipen/skills/builtin/assets/style-tpl-product-review.png +0 -0
  186. package/src/clipen/skills/builtin/assets/style-tpl-product-unboxing.png +0 -0
  187. package/src/clipen/skills/builtin/assets/style-tpl-shorts-highlight.png +0 -0
  188. package/src/clipen/skills/builtin/assets/style-tpl-travel-highlight.png +0 -0
  189. package/src/clipen/skills/builtin/assets/style-tpl-travel-vlog.png +0 -0
  190. package/src/clipen/skills/builtin/assets/style-variety.png +0 -0
  191. package/src/clipen/skills/builtin/components.json +133 -0
  192. package/src/clipen/skills/builtin/font_sources.json +20 -0
  193. package/src/clipen/skills/builtin/font_styles.json +329 -0
  194. package/src/clipen/skills/builtin/manifest.json +32 -0
  195. package/src/clipen/skills/builtin/presets.json +8 -0
  196. package/src/clipen/skills/builtin/references/captions.md +14 -0
  197. package/src/clipen/skills/builtin/references/channel-design.md +37 -0
  198. package/src/clipen/skills/builtin/references/editing.md +30 -0
  199. package/src/clipen/skills/builtin/references/font-chenyu-OFL.txt +93 -0
  200. package/src/clipen/skills/builtin/references/font-iansui-OFL.txt +93 -0
  201. package/src/clipen/skills/builtin/references/graphics.md +59 -0
  202. package/src/clipen/skills/builtin/references/review.md +16 -0
  203. package/src/clipen/skills/builtin/references/sound.md +8 -0
  204. package/src/clipen/skills/builtin/references/story.md +7 -0
  205. package/src/clipen/skills/builtin/skills/travel-program/SKILL.md +27 -0
  206. package/src/clipen/skills/builtin/skills/travel-program/manifest.json +6 -0
  207. package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/SKILL.md +33 -0
  208. package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/manifest.json +8 -0
  209. package/src/clipen/skills/builtin/template-skills/clipen-event-recap/SKILL.md +34 -0
  210. package/src/clipen/skills/builtin/template-skills/clipen-event-recap/manifest.json +8 -0
  211. package/src/clipen/skills/builtin/template-skills/clipen-food-review/SKILL.md +33 -0
  212. package/src/clipen/skills/builtin/template-skills/clipen-food-review/manifest.json +8 -0
  213. package/src/clipen/skills/builtin/template-skills/clipen-product-demo/SKILL.md +35 -0
  214. package/src/clipen/skills/builtin/template-skills/clipen-product-demo/manifest.json +8 -0
  215. package/src/clipen/skills/builtin/template-skills/clipen-product-review/SKILL.md +36 -0
  216. package/src/clipen/skills/builtin/template-skills/clipen-product-review/manifest.json +8 -0
  217. package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/SKILL.md +39 -0
  218. package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/manifest.json +8 -0
  219. package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/SKILL.md +38 -0
  220. package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/manifest.json +8 -0
  221. package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/SKILL.md +34 -0
  222. package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/manifest.json +8 -0
  223. package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/SKILL.md +37 -0
  224. package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/manifest.json +8 -0
  225. package/src/clipen/skills/builtin/templates.json +284 -0
  226. package/src/clipen/skills/builtin/visual_styles.json +1074 -0
  227. package/src/clipen/skills/catalog.py +314 -0
  228. package/src/clipen/skills/library.py +137 -0
  229. package/src/clipen/skills/package.py +254 -0
  230. package/src/clipen/skills/visual_styles.py +55 -0
  231. package/src/clipen/workspace.py +181 -0
  232. package/uninstall +25 -0
@@ -0,0 +1,309 @@
1
+ """Shot coverage and revision-bound, explicitly model-authored editorial findings.
2
+
3
+ Coverage proves which stills were delivered, never that motion or sound was
4
+ understood. Findings expose the model's decisions rather than inventing a score.
5
+ """
6
+
7
+ import hashlib
8
+ import json
9
+ from fractions import Fraction
10
+
11
+ from clipen.domain import EditTimeline, TrackKind
12
+ from clipen.media import MediaPreviewRepository
13
+ from clipen.projects import ProjectError
14
+
15
+ from .agent_models import ToolResult
16
+ from .production_media import sample_frames
17
+ from .production_preview import _event, inspect_preview
18
+
19
+
20
+ def shot_offsets(item, fps):
21
+ # First/last real frames, not the following shot at the outgoing boundary.
22
+ return [
23
+ item.start_frame / fps,
24
+ (item.start_frame + (item.duration_frames - 1) / 2) / fps,
25
+ (item.start_frame + item.duration_frames - 1) / fps,
26
+ ]
27
+
28
+
29
+ def _effects(item):
30
+ return [(e.effect_id, e.version, e._parameters_json) for e in item.effects]
31
+
32
+
33
+ def _fingerprint(value):
34
+ return hashlib.sha256(json.dumps(value, ensure_ascii=False, sort_keys=True).encode()).hexdigest()
35
+
36
+
37
+ def shot_fingerprints(timeline):
38
+ """What each shot looks like on screen, independent of where it sits in the film.
39
+
40
+ Covers the source range, length, the shot's own effects, the next shot's
41
+ incoming transition and every text or component drawn over it (relative to
42
+ the shot). A shot with the same fingerprint renders the same stills.
43
+ """
44
+ video = [t for t in timeline.tracks if t.kind is TrackKind.VIDEO]
45
+ clips = [item for track in video for item in track.items]
46
+ texts = [item for t in timeline.tracks if t.kind is TrackKind.TITLE for item in t.items]
47
+ result = {}
48
+ for index, clip in enumerate(clips):
49
+ end = clip.start_frame + clip.duration_frames
50
+ following = clips[index + 1] if index + 1 < len(clips) else None
51
+ over = sorted(
52
+ (t.text, t.style_id, t.start_frame - clip.start_frame, t.duration_frames)
53
+ for t in texts
54
+ if t.start_frame < end and clip.start_frame < t.start_frame + t.duration_frames
55
+ )
56
+ result[clip.item_id] = _fingerprint(
57
+ {
58
+ "source": clip.source.span.to_storage(),
59
+ "duration": clip.duration_frames,
60
+ "effects": _effects(clip),
61
+ "next_transition": [e for e in _effects(following) if e[0] == "video.transition"]
62
+ if following
63
+ else None,
64
+ "texts": over,
65
+ "canvas": [
66
+ timeline.width,
67
+ timeline.height,
68
+ timeline.frame_rate.numerator,
69
+ timeline.frame_rate.denominator,
70
+ ],
71
+ }
72
+ )
73
+ return result
74
+
75
+
76
+ def text_fingerprints(timeline):
77
+ """A text matches an earlier review when it and the shot under its midpoint are unchanged."""
78
+ shots = shot_fingerprints(timeline)
79
+ clips = [item for t in timeline.tracks if t.kind is TrackKind.VIDEO for item in t.items]
80
+ result = {}
81
+ for t in timeline.tracks:
82
+ if t.kind is not TrackKind.TITLE:
83
+ continue
84
+ for item in t.items:
85
+ middle = item.start_frame + (item.duration_frames - 1) / 2
86
+ under = next((c for c in clips if c.start_frame <= middle < c.start_frame + c.duration_frames), None)
87
+ result[item.item_id] = _fingerprint(
88
+ {
89
+ "text": item.text,
90
+ "style": item.style_id,
91
+ "duration": item.duration_frames,
92
+ "offset": None if under is None else middle - under.start_frame,
93
+ "shot": None if under is None else shots[under.item_id],
94
+ }
95
+ )
96
+ return result
97
+
98
+
99
+ def carried_reviews(tools, revision, recipe_id):
100
+ """Shots and texts already reviewed and accepted in an earlier revision of this run, unchanged since.
101
+
102
+ Only revisions with a recorded editorial review count: that review required
103
+ complete coverage, so an accepted, unchanged shot need not be looked at again.
104
+ """
105
+ with tools.repository.projects.connect(tools.project_id) as db:
106
+ reports = {}
107
+ for row in db.execute(
108
+ "SELECT payload_json FROM edit_agent_events WHERE run_id=? AND event_type='editorial_review' ORDER BY sequence",
109
+ (tools.run_id,),
110
+ ):
111
+ report = json.loads(row["payload_json"])
112
+ if report.get("recipe_id") == recipe_id and report["revision_id"] != revision.revision_id:
113
+ reports[report["revision_id"]] = report
114
+ payloads = (
115
+ {
116
+ row["revision_id"]: row["payload_json"]
117
+ for row in db.execute(
118
+ f"SELECT revision_id,payload_json FROM edit_agent_revisions WHERE run_id=? AND revision_id IN ({','.join('?' for _ in reports)})",
119
+ (tools.run_id, *reports),
120
+ )
121
+ }
122
+ if reports
123
+ else {}
124
+ )
125
+ accepted_shots, accepted_texts = set(), set()
126
+ for revision_id, report in reports.items():
127
+ if revision_id not in payloads:
128
+ continue
129
+ earlier = EditTimeline.from_payload(json.loads(payloads[revision_id]))
130
+ shots, texts = shot_fingerprints(earlier), text_fingerprints(earlier)
131
+ accepted_shots |= {
132
+ shots[s["item_id"]]
133
+ for s in report.get("shots", [])
134
+ if s.get("decision") != "revise" and s["item_id"] in shots
135
+ }
136
+ accepted_texts |= {
137
+ texts[c["item_id"]] for c in report.get("text_checks", []) if c.get("matches") and c["item_id"] in texts
138
+ }
139
+ accepted_shots |= set(report.get("carried_shots", []))
140
+ accepted_texts |= set(report.get("carried_texts", []))
141
+ current_shots, current_texts = shot_fingerprints(revision.timeline), text_fingerprints(revision.timeline)
142
+ return (
143
+ {item: print_ for item, print_ in current_shots.items() if print_ in accepted_shots},
144
+ {item: print_ for item, print_ in current_texts.items() if print_ in accepted_texts},
145
+ )
146
+
147
+
148
+ def coverage_targets(timeline):
149
+ fps = timeline.frame_rate.numerator / timeline.frame_rate.denominator
150
+ return {
151
+ item.item_id: shot_offsets(item, fps) for t in timeline.tracks if t.kind is TrackKind.VIDEO for item in t.items
152
+ }
153
+
154
+
155
+ def text_targets(timeline):
156
+ fps = timeline.frame_rate.numerator / timeline.frame_rate.denominator
157
+ return {
158
+ item.item_id: (item.start_frame + (item.duration_frames - 1) / 2) / fps
159
+ for t in timeline.tracks
160
+ if t.kind is TrackKind.TITLE
161
+ for item in t.items
162
+ }
163
+
164
+
165
+ def inspect_cut(tools, args):
166
+ revision = tools.repository.current_revision(tools.project_id, tools.run_id)
167
+ timeline = revision.timeline
168
+ targets = coverage_targets(timeline)
169
+ clips = {i.item_id: i for i in tools._video().items}
170
+ if any(i not in clips for i in args["item_ids"]):
171
+ raise ProjectError("timeline_item_not_found", "指定鏡頭不在目前剪輯。", status_code=404)
172
+ fps = Fraction(timeline.frame_rate.numerator, timeline.frame_rate.denominator)
173
+ speech = tools._production_list_transcripts({"include_drafts": True}).payload
174
+ cues, limitations = speech["cues"], speech["limitations"]
175
+ images = []
176
+ shots = []
177
+ for identifier in args["item_ids"]:
178
+ item = clips[identifier]
179
+ texts = [
180
+ i
181
+ for t in timeline.tracks
182
+ if t.kind is TrackKind.TITLE
183
+ for i in t.items
184
+ if i.start_frame < item.start_frame + item.duration_frames
185
+ and item.start_frame < i.start_frame + i.duration_frames
186
+ ]
187
+ if args.get("render_id"):
188
+ offsets = targets[identifier] + [text_targets(timeline)[i.item_id] for i in texts[:1]]
189
+ # Text can span shots; its midpoint must still be inspected, once.
190
+ offsets = sorted(set(offsets))
191
+ for start in range(0, len(offsets), 8):
192
+ result = inspect_preview(
193
+ tools, {"render_id": args["render_id"], "offsets_seconds": offsets[start : start + 8]}
194
+ )
195
+ if result.payload["render"]["state"] != "succeeded" or result.payload["stale"]:
196
+ return ToolResult(result.payload)
197
+ images.extend(result.images)
198
+ samples = [{"timeline_seconds": o} for o in offsets]
199
+ else:
200
+ span = item.source.span
201
+ unit = Fraction(span.time_base.numerator, span.time_base.denominator)
202
+ duration = span.duration_pts * unit
203
+ last = min(Fraction(item.duration_frames - 1, 1) / fps, duration - Fraction(1, 1) / fps)
204
+ offsets = [0, float(max(0, last) / 2), float(max(0, last))]
205
+ media = getattr(tools, "media", None) or MediaPreviewRepository(tools.repository.projects)
206
+ paths, samples = sample_frames(
207
+ media, tools.project_id, span, offsets, tools._workdir("samples"), tools._cancel_event()
208
+ )
209
+ images.extend(paths)
210
+ shots.append(
211
+ {
212
+ "item_id": identifier,
213
+ "source": item.source.span.to_storage(),
214
+ "samples": samples,
215
+ "image_count": len(samples),
216
+ "texts": [
217
+ {
218
+ "item_id": i.item_id,
219
+ "text": i.text,
220
+ "start_frame": i.start_frame,
221
+ "duration_frames": i.duration_frames,
222
+ }
223
+ for i in texts
224
+ ],
225
+ "dialogue": [c for c in cues if c["item_id"] == identifier],
226
+ "dialogue_problems": [c for c in limitations if c["item_id"] == identifier],
227
+ }
228
+ )
229
+ from .programme_review import pacing
230
+
231
+ return ToolResult(
232
+ {
233
+ "revision_id": revision.revision_id,
234
+ "shots": shots,
235
+ "pacing": pacing(timeline),
236
+ "image_order": "shots in requested order; frames in sample order",
237
+ "coverage": "start/middle/end stills; continuous motion and audio remain unreviewed",
238
+ "text_coverage": "At most one additional text midpoint per shot; validate_sequence lists remaining text samples for inspect_preview.",
239
+ "editorial_questions": [
240
+ "Does this show the event or only preparation/empty wall?",
241
+ "Does the scene establish a place and then deliver action/reaction?",
242
+ "Is each location/day assertion supported by this exact shot?",
243
+ "Does retained speech finish naturally? Repair source spans before captions.",
244
+ ],
245
+ },
246
+ tuple(images),
247
+ )
248
+
249
+
250
+ def review_cut(tools, args):
251
+ from .production_preview import finish_review_status
252
+
253
+ revision = tools.repository.current_revision(tools.project_id, tools.run_id)
254
+ status = finish_review_status(tools)
255
+ if not status["render_coverage_complete"]:
256
+ raise ProjectError(
257
+ "agent_preview_review_required", "請先檢視每個鏡頭的頭、中、尾及所有文字顯示畫面。", status_code=409
258
+ )
259
+ every_shot = set(coverage_targets(revision.timeline))
260
+ every_text = set(text_targets(revision.timeline))
261
+ carried_shots, carried_texts = set(status.get("carried_shots", ())), set(status.get("carried_texts", ()))
262
+ shots = args["shots"]
263
+ checks = args["text_checks"]
264
+ listed_shots = [s["item_id"] for s in shots]
265
+ listed_texts = [c["item_id"] for c in checks]
266
+ # Every changed shot and text must be judged; unchanged, already accepted ones may be listed or left out.
267
+ if (
268
+ len(listed_shots) != len(set(listed_shots))
269
+ or not every_shot - carried_shots <= set(listed_shots) <= every_shot
270
+ or len(listed_texts) != len(set(listed_texts))
271
+ or not every_text - carried_texts <= set(listed_texts) <= every_text
272
+ ):
273
+ raise ProjectError(
274
+ "agent_review_incomplete",
275
+ "審片紀錄必須逐一列出所有改過的鏡頭與文字,不可重複或遺漏;沒改過且已通過的可以省略。",
276
+ status_code=422,
277
+ )
278
+ if status["unresolved"]:
279
+ still_unresolved = {s["item_id"] for s in shots if s["decision"] == "revise"} | {
280
+ c["item_id"] for c in checks if not c["matches"]
281
+ }
282
+ if not set(status["unresolved"]) <= still_unresolved:
283
+ raise ProjectError(
284
+ "agent_revision_required",
285
+ "這個版本已記錄待修問題;請先實際修改時間軸,再輸出並檢視新版,不能只改評語清除問題。",
286
+ status_code=409,
287
+ )
288
+ from .production_preview import render_recipe_id
289
+
290
+ shot_prints, text_prints = shot_fingerprints(revision.timeline), text_fingerprints(revision.timeline)
291
+ report = {
292
+ "revision_id": revision.revision_id,
293
+ "recipe_id": render_recipe_id(),
294
+ "authority": "model_authored",
295
+ **args,
296
+ # Carried items keep their earlier acceptance for later revisions.
297
+ "carried_shots": sorted(shot_prints[i] for i in carried_shots - set(listed_shots)),
298
+ "carried_texts": sorted(text_prints[i] for i in carried_texts - set(listed_texts)),
299
+ }
300
+ _event(tools, "editorial_review", report)
301
+ return ToolResult(
302
+ {
303
+ "assessment": report,
304
+ "unresolved": [s["item_id"] for s in shots if s["decision"] == "revise"]
305
+ + [c["item_id"] for c in checks if not c["matches"]],
306
+ "human_review": "pending",
307
+ "quality_claim": "This records model findings, not independent approval.",
308
+ }
309
+ )