@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,396 @@
1
+ """Minimal MCP stdio adapter over Clipen's allowlisted editing tools."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import json
7
+ import sys
8
+ from copy import deepcopy
9
+ from io import BytesIO
10
+ from pathlib import Path
11
+ from typing import BinaryIO, Mapping
12
+
13
+ from clipen.editing import EditAgentTools, ToolResult
14
+ from clipen.editing.production_catalog import AGENT_TOOLS
15
+ from clipen.editing.tool_receipts import TIMELINE_MUTATIONS
16
+ from clipen.projects import ProjectError
17
+
18
+ _MAX_MESSAGE_BYTES = 1024 * 1024
19
+ _MAX_IMAGE_BYTES = 96 * 1024
20
+ _MAX_IMAGE_SIZE = (640, 360)
21
+ _PROTOCOL_VERSION = "2025-06-18"
22
+
23
+
24
+ def _tool(
25
+ name: str,
26
+ description: str,
27
+ properties: Mapping[str, object],
28
+ required: tuple[str, ...] = (),
29
+ ) -> dict[str, object]:
30
+ schema: dict[str, object] = {
31
+ "type": "object",
32
+ "properties": dict(properties),
33
+ "additionalProperties": False,
34
+ }
35
+ if required:
36
+ schema["required"] = list(required)
37
+ return {"name": name, "description": description, "inputSchema": schema}
38
+
39
+
40
+ _TOOLS = (
41
+ _tool(
42
+ "get_edit_status",
43
+ "Read the run state, bounded budget, and authoritative user-confirmed edit direction.",
44
+ {},
45
+ ),
46
+ _tool(
47
+ "search_media",
48
+ "Search path-free candidate clips from the current approved material snapshot.",
49
+ {
50
+ "query": {"type": "string", "maxLength": 200},
51
+ "limit": {"type": "integer", "minimum": 1, "maximum": 50},
52
+ "offset": {"type": "integer", "minimum": 0, "maximum": 100000},
53
+ },
54
+ ),
55
+ _tool(
56
+ "inspect_clip",
57
+ "Inspect one candidate's source-bound evidence and up to four local sample images.",
58
+ {"candidate_id": {"type": "string"}},
59
+ ("candidate_id",),
60
+ ),
61
+ _tool(
62
+ "inspect_sequence",
63
+ "Read the current immutable timeline revision. Long films return an outline; then read parts with "
64
+ "track (video, title or audio) and/or from_seconds/to_seconds.",
65
+ {
66
+ "track": {"type": "string", "enum": ["video", "title", "audio"]},
67
+ "from_seconds": {"type": "number", "minimum": 0},
68
+ "to_seconds": {"type": "number", "minimum": 0},
69
+ },
70
+ ),
71
+ _tool(
72
+ "replace_sequence",
73
+ "Build the short style proof from precise in/out ranges inside approved candidates.",
74
+ {
75
+ "clips": {
76
+ "type": "array",
77
+ "items": {
78
+ "type": "object",
79
+ "properties": {
80
+ "candidate_id": {"type": "string"},
81
+ "start_offset_seconds": {"type": "number", "minimum": 0},
82
+ "duration_seconds": {
83
+ "type": "number",
84
+ "minimum": 0.75,
85
+ "maximum": 30,
86
+ },
87
+ },
88
+ "required": [
89
+ "candidate_id",
90
+ "start_offset_seconds",
91
+ "duration_seconds",
92
+ ],
93
+ "additionalProperties": False,
94
+ },
95
+ "minItems": 1,
96
+ "maxItems": 180,
97
+ },
98
+ "summary": {"type": "string", "maxLength": 500},
99
+ "reprise_reason": {
100
+ "type": "string",
101
+ "minLength": 1,
102
+ "maxLength": 500,
103
+ "description": "Explicit editorial reason for intentionally repeating a source range as a cold open/callback. Without this, duplicate source ranges are rejected.",
104
+ },
105
+ },
106
+ ("clips", "summary"),
107
+ ),
108
+ _tool(
109
+ "set_clip_effect",
110
+ "Set one semantic effect on an existing media clip; exporters map it safely.",
111
+ {
112
+ "item_id": {"type": "string"},
113
+ "effect_id": {
114
+ "type": "string",
115
+ "enum": [
116
+ "video.punch_in",
117
+ "video.slow_push",
118
+ "video.reframe",
119
+ "video.speed_ramp",
120
+ ],
121
+ },
122
+ "parameters": {
123
+ "type": "object",
124
+ "description": (
125
+ "Use {} for safe defaults, or exact numeric keys: "
126
+ "punch_in {scale,center_x,center_y}; slow_push "
127
+ "{scale_from,scale_to}; reframe {x,y}; speed_ramp {rate}."
128
+ ),
129
+ "properties": {
130
+ "scale": {"type": "number", "minimum": 1.01, "maximum": 2},
131
+ "center_x": {"type": "number", "minimum": 0, "maximum": 1},
132
+ "center_y": {"type": "number", "minimum": 0, "maximum": 1},
133
+ "scale_from": {"type": "number", "minimum": 1, "maximum": 2},
134
+ "scale_to": {"type": "number", "minimum": 1, "maximum": 2},
135
+ "x": {"type": "number", "minimum": 0, "maximum": 1},
136
+ "y": {"type": "number", "minimum": 0, "maximum": 1},
137
+ "rate": {"type": "number", "minimum": 0.25, "maximum": 4},
138
+ },
139
+ "additionalProperties": False,
140
+ },
141
+ "summary": {"type": "string", "maxLength": 500},
142
+ },
143
+ ("item_id", "effect_id", "parameters", "summary"),
144
+ ),
145
+ _tool("undo_revision", "Select the parent of the current immutable revision.", {}),
146
+ _tool(
147
+ "finish_edit",
148
+ "Finish this bounded run and freeze its selected final revision, with the change report the person reads: "
149
+ "summary (what this version is now, in plain words), changes (what you changed and why, as a viewer would put it), "
150
+ "notes (one answer per review note the person sent: reference_id, outcome done/partly/not_done/kept, explanation) "
151
+ "and not_done. Anything your tools can do is done before finishing, never left in not_done. not_done and a "
152
+ "partly/not_done note need reason needs_you (detail: what the person must decide or supply) or cannot "
153
+ "(detail: which tool limit stops it). Do not list limits that hold every time, such as not hearing audio.",
154
+ {
155
+ "summary": {"type": "string", "maxLength": 1000},
156
+ "changes": {
157
+ "type": "array",
158
+ "minItems": 1,
159
+ "maxItems": 20,
160
+ "items": {
161
+ "type": "object",
162
+ "properties": {
163
+ "what": {"type": "string", "maxLength": 200},
164
+ "why": {"type": "string", "maxLength": 300},
165
+ },
166
+ "required": ["what", "why"],
167
+ "additionalProperties": False,
168
+ },
169
+ },
170
+ "notes": {
171
+ "type": "array",
172
+ "maxItems": 40,
173
+ "items": {
174
+ "type": "object",
175
+ "properties": {
176
+ "reference_id": {"type": "string", "maxLength": 160},
177
+ "outcome": {"enum": ["done", "partly", "not_done", "kept"]},
178
+ "explanation": {"type": "string", "maxLength": 300},
179
+ "reason": {"enum": ["needs_you", "cannot"]},
180
+ },
181
+ "required": ["reference_id", "outcome", "explanation"],
182
+ "additionalProperties": False,
183
+ },
184
+ },
185
+ "not_done": {
186
+ "type": "array",
187
+ "maxItems": 10,
188
+ "items": {
189
+ "type": "object",
190
+ "properties": {
191
+ "what": {"type": "string", "maxLength": 200},
192
+ "reason": {"enum": ["needs_you", "cannot"]},
193
+ "detail": {"type": "string", "maxLength": 300},
194
+ },
195
+ "required": ["what", "reason", "detail"],
196
+ "additionalProperties": False,
197
+ },
198
+ },
199
+ },
200
+ ("summary", "changes"),
201
+ ),
202
+ ) + AGENT_TOOLS
203
+ _TOOL_NAMES = frozenset(entry["name"] for entry in _TOOLS)
204
+
205
+ # The same optional compare-and-swap guard applies to every independent tool.
206
+ # Tools strip it before domain argument validation; actual revision writes also
207
+ # check inside the SQLite transaction.
208
+ for _entry in _TOOLS:
209
+ if _entry["name"] in TIMELINE_MUTATIONS:
210
+ _entry["inputSchema"]["properties"]["expected_revision_id"] = {
211
+ "type": "string",
212
+ "pattern": "^editrev_[0-9a-f]{32}$",
213
+ "description": "Current revision guard. A stale value fails without overwriting newer edits.",
214
+ }
215
+ _entry["inputSchema"]["properties"]["mutation_id"] = {
216
+ "type": "string",
217
+ "minLength": 1,
218
+ "maxLength": 160,
219
+ "description": "Unique operation key; reuse with identical arguments only when retrying the same operation.",
220
+ }
221
+
222
+
223
+ class EditingMcpServer:
224
+ """Handle MCP JSON-RPC without exposing a shell, resources, or arbitrary files."""
225
+
226
+ def __init__(self, tools: EditAgentTools) -> None:
227
+ self.tools = tools
228
+
229
+ def start(self) -> None:
230
+ self.tools.activate()
231
+
232
+ def handle(self, message: object) -> dict[str, object] | None:
233
+ if not isinstance(message, dict) or message.get("jsonrpc") != "2.0":
234
+ return self._error(None, -32600, "Invalid Request")
235
+ identifier = message.get("id")
236
+ method = message.get("method")
237
+ if not isinstance(method, str):
238
+ return self._error(identifier, -32600, "Invalid Request")
239
+ if identifier is None:
240
+ return None
241
+ params = message.get("params", {})
242
+ if method == "initialize":
243
+ return self._result(
244
+ identifier,
245
+ {
246
+ "protocolVersion": _PROTOCOL_VERSION,
247
+ "capabilities": {"tools": {"listChanged": False}},
248
+ "serverInfo": {"name": "clipen-editing", "version": "0.1.0"},
249
+ "instructions": (
250
+ "Start with get_edit_status. For skill_production read the frozen "
251
+ "SKILL.md through read_production_skill and explore authorized sources. "
252
+ "You decide the workflow; keep source media immutable and inspect results."
253
+ ),
254
+ },
255
+ )
256
+ if method == "ping":
257
+ return self._result(identifier, {})
258
+ if method == "tools/list":
259
+ entries = deepcopy(list(_TOOLS))
260
+ for entry in entries:
261
+ if entry["name"] in TIMELINE_MUTATIONS:
262
+ # Rebind instead of extending: tools may share one required
263
+ # list, and duplicates make the schema invalid for strict providers.
264
+ required = entry["inputSchema"].get("required", [])
265
+ entry["inputSchema"]["required"] = required + [
266
+ k for k in ("expected_revision_id", "mutation_id") if k not in required
267
+ ]
268
+ return self._result(identifier, {"tools": entries})
269
+ if method == "tools/call":
270
+ return self._call_tool(identifier, params)
271
+ return self._error(identifier, -32601, "Method not found")
272
+
273
+ def _call_tool(self, identifier: object, params: object) -> dict[str, object]:
274
+ if not isinstance(params, dict) or not isinstance(params.get("name"), str):
275
+ return self._error(identifier, -32602, "Invalid params")
276
+ if params["name"] not in _TOOL_NAMES:
277
+ # Only listed tools reach Clipen, even if the model guesses another name.
278
+ return self._error(identifier, -32602, "Unknown tool")
279
+ arguments = params.get("arguments", {})
280
+ try:
281
+ result = self.tools.call(params["name"], arguments)
282
+ except ProjectError as error:
283
+ return self._result(
284
+ identifier,
285
+ {
286
+ "content": [
287
+ {
288
+ "type": "text",
289
+ "text": json.dumps(
290
+ {"error": error.code, "message": error.message},
291
+ ensure_ascii=False,
292
+ ),
293
+ }
294
+ ],
295
+ "isError": True,
296
+ },
297
+ )
298
+ except (TypeError, ValueError) as error:
299
+ return self._result(
300
+ identifier,
301
+ {
302
+ "content": [
303
+ {
304
+ "type": "text",
305
+ "text": json.dumps(
306
+ {"error": "agent_input_invalid", "message": str(error)},
307
+ ensure_ascii=False,
308
+ ),
309
+ }
310
+ ],
311
+ "isError": True,
312
+ },
313
+ )
314
+ return self._result(identifier, self._content(result))
315
+
316
+ @staticmethod
317
+ def _content(result: ToolResult) -> dict[str, object]:
318
+ content: list[dict[str, object]] = [{"type": "text", "text": json.dumps(result.payload, ensure_ascii=False)}]
319
+ for path in result.images:
320
+ encoded = _bounded_image(path)
321
+ if encoded is None:
322
+ continue
323
+ data, mime = encoded
324
+ content.append(
325
+ {
326
+ "type": "image",
327
+ "data": base64.b64encode(data).decode("ascii"),
328
+ "mimeType": mime,
329
+ }
330
+ )
331
+ return {"content": content, "isError": False}
332
+
333
+ @staticmethod
334
+ def _result(identifier: object, value: object) -> dict[str, object]:
335
+ return {"jsonrpc": "2.0", "id": identifier, "result": value}
336
+
337
+ @staticmethod
338
+ def _error(identifier: object, code: int, message: str) -> dict[str, object]:
339
+ return {
340
+ "jsonrpc": "2.0",
341
+ "id": identifier,
342
+ "error": {"code": code, "message": message},
343
+ }
344
+
345
+
346
+ def _bounded_image(path: Path) -> tuple[bytes, str] | None:
347
+ """Encode a model-readable thumbnail without modifying the evidence artifact."""
348
+
349
+ try:
350
+ from PIL import Image
351
+
352
+ with Image.open(path) as source:
353
+ image = source.convert("RGB")
354
+ for size in (_MAX_IMAGE_SIZE, (480, 270), (320, 180)):
355
+ image.thumbnail(size, Image.Resampling.LANCZOS)
356
+ for quality in (76, 60, 44):
357
+ output = BytesIO()
358
+ image.save(output, "JPEG", quality=quality, optimize=True)
359
+ data = output.getvalue()
360
+ if len(data) <= _MAX_IMAGE_BYTES:
361
+ return data, "image/jpeg"
362
+ return None
363
+ except (ImportError, OSError, ValueError):
364
+ try:
365
+ if path.stat().st_size > _MAX_IMAGE_BYTES:
366
+ return None
367
+ data = path.read_bytes()
368
+ except OSError:
369
+ return None
370
+ suffix = path.suffix.lower()
371
+ mime = "image/jpeg" if suffix in {".jpg", ".jpeg"} else "image/png"
372
+ return data, mime
373
+
374
+
375
+ def run_stdio(
376
+ server: EditingMcpServer,
377
+ source: BinaryIO | None = None,
378
+ destination: BinaryIO | None = None,
379
+ ) -> int:
380
+ """Serve newline-delimited MCP messages with a strict input bound."""
381
+
382
+ input_stream = source or sys.stdin.buffer
383
+ output_stream = destination or sys.stdout.buffer
384
+ server.start()
385
+ for raw in input_stream:
386
+ if len(raw) > _MAX_MESSAGE_BYTES:
387
+ response = server._error(None, -32600, "Message too large")
388
+ else:
389
+ try:
390
+ response = server.handle(json.loads(raw))
391
+ except (UnicodeDecodeError, json.JSONDecodeError):
392
+ response = server._error(None, -32700, "Parse error")
393
+ if response is not None:
394
+ output_stream.write(json.dumps(response, ensure_ascii=False, separators=(",", ":")).encode("utf-8") + b"\n")
395
+ output_stream.flush()
396
+ return 0
@@ -0,0 +1,50 @@
1
+ """Local media inventory domain and persistence."""
2
+
3
+ from .discovery import DiscoveredAsset, DiscoveryError, MediaDiscovery
4
+ from .models import (
5
+ AssetCandidate,
6
+ AssetKind,
7
+ InventoryCounts,
8
+ InventoryItem,
9
+ InventoryPage,
10
+ InventoryRoot,
11
+ InventoryRun,
12
+ InventoryState,
13
+ ItemStatus,
14
+ MediaStream,
15
+ ProbeStatus,
16
+ )
17
+ from .preview import MediaPreview, MediaPreviewRepository
18
+ from .probe import (
19
+ FFprobeAdapter,
20
+ MediaProbeError,
21
+ MediaProbeResult,
22
+ bounded_file_fingerprint,
23
+ )
24
+ from .service import InventoryCoordinator
25
+ from .storage import InventoryRepository
26
+
27
+ __all__ = [
28
+ "AssetCandidate",
29
+ "AssetKind",
30
+ "DiscoveredAsset",
31
+ "DiscoveryError",
32
+ "FFprobeAdapter",
33
+ "InventoryCounts",
34
+ "InventoryCoordinator",
35
+ "MediaPreview",
36
+ "MediaPreviewRepository",
37
+ "InventoryItem",
38
+ "InventoryPage",
39
+ "InventoryRepository",
40
+ "InventoryRoot",
41
+ "InventoryRun",
42
+ "InventoryState",
43
+ "ItemStatus",
44
+ "MediaStream",
45
+ "MediaDiscovery",
46
+ "MediaProbeError",
47
+ "MediaProbeResult",
48
+ "ProbeStatus",
49
+ "bounded_file_fingerprint",
50
+ ]