@clipen/clipen-cli 0.0.0-stage → 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +79 -2
- package/apps/web/dist/assets/index-D5D2HiB1.js +14 -0
- package/apps/web/dist/assets/index-DIMuOXSu.css +1 -0
- package/apps/web/dist/favicon.svg +1 -0
- package/apps/web/dist/index.html +16 -0
- package/bin/clipen.js +157 -0
- package/clipen +18 -0
- package/install +25 -0
- package/package.json +52 -4
- package/pyproject.toml +28 -0
- package/requirements/asr-runtime.txt +13 -0
- package/requirements/runtime.txt +20 -0
- package/src/clipen/__init__.py +3 -0
- package/src/clipen/analysis/__init__.py +89 -0
- package/src/clipen/analysis/artifacts.py +416 -0
- package/src/clipen/analysis/audio.py +392 -0
- package/src/clipen/analysis/evidence.py +102 -0
- package/src/clipen/analysis/evidence_service.py +751 -0
- package/src/clipen/analysis/image_similarity.py +143 -0
- package/src/clipen/analysis/keyframes.py +171 -0
- package/src/clipen/analysis/models.py +230 -0
- package/src/clipen/analysis/qwen_worker.py +629 -0
- package/src/clipen/analysis/resources.py +21 -0
- package/src/clipen/analysis/runtime.py +64 -0
- package/src/clipen/analysis/service.py +626 -0
- package/src/clipen/analysis/setup.py +935 -0
- package/src/clipen/analysis/setup_cli.py +117 -0
- package/src/clipen/analysis/storage.py +2046 -0
- package/src/clipen/analysis/transcription.py +948 -0
- package/src/clipen/analysis/visual.py +176 -0
- package/src/clipen/annotations/__init__.py +10 -0
- package/src/clipen/annotations/models.py +22 -0
- package/src/clipen/annotations/storage.py +380 -0
- package/src/clipen/annotations/suggestions.py +120 -0
- package/src/clipen/asr_release.py +139 -0
- package/src/clipen/bootstrap.py +967 -0
- package/src/clipen/cli.py +84 -0
- package/src/clipen/controller/__init__.py +17 -0
- package/src/clipen/controller/app.py +1470 -0
- package/src/clipen/controller/server.py +543 -0
- package/src/clipen/domain/__init__.py +30 -0
- package/src/clipen/domain/errors.py +5 -0
- package/src/clipen/domain/media_time.py +117 -0
- package/src/clipen/domain/timeline.py +584 -0
- package/src/clipen/domain/validation.py +15 -0
- package/src/clipen/editing/EDITORIAL_REGRESSIONS.md +38 -0
- package/src/clipen/editing/PROGRAM_FORMAT.md +102 -0
- package/src/clipen/editing/README.md +115 -0
- package/src/clipen/editing/REFACTOR_DESIGN.md +241 -0
- package/src/clipen/editing/REFACTOR_HANDOFF.md +73 -0
- package/src/clipen/editing/__init__.py +15 -0
- package/src/clipen/editing/agent.py +13 -0
- package/src/clipen/editing/agent_models.py +134 -0
- package/src/clipen/editing/agent_repository.py +757 -0
- package/src/clipen/editing/agent_tools.py +532 -0
- package/src/clipen/editing/alignment.py +110 -0
- package/src/clipen/editing/alignment_setup.py +108 -0
- package/src/clipen/editing/alignment_worker.py +22 -0
- package/src/clipen/editing/change_report.py +71 -0
- package/src/clipen/editing/compiler.py +41 -0
- package/src/clipen/editing/component_looks.py +107 -0
- package/src/clipen/editing/component_review.py +78 -0
- package/src/clipen/editing/coordinator.py +374 -0
- package/src/clipen/editing/editorial_review.py +187 -0
- package/src/clipen/editing/evidence_jobs.py +295 -0
- package/src/clipen/editing/history_changes.py +131 -0
- package/src/clipen/editing/music_choices.py +15 -0
- package/src/clipen/editing/production_catalog.py +192 -0
- package/src/clipen/editing/production_limits.py +59 -0
- package/src/clipen/editing/production_media.py +226 -0
- package/src/clipen/editing/production_preview.py +180 -0
- package/src/clipen/editing/production_progress.py +141 -0
- package/src/clipen/editing/production_requirements.py +50 -0
- package/src/clipen/editing/production_tools.py +596 -0
- package/src/clipen/editing/program_audio.py +118 -0
- package/src/clipen/editing/program_direction.py +49 -0
- package/src/clipen/editing/program_story.py +53 -0
- package/src/clipen/editing/program_subtitles.py +134 -0
- package/src/clipen/editing/programme_review.py +73 -0
- package/src/clipen/editing/prompt_history.py +47 -0
- package/src/clipen/editing/recut.py +127 -0
- package/src/clipen/editing/refinement_references.py +188 -0
- package/src/clipen/editing/resolve_export.py +616 -0
- package/src/clipen/editing/resolve_titles.py +234 -0
- package/src/clipen/editing/skill_tools.py +575 -0
- package/src/clipen/editing/source_dialogue.py +82 -0
- package/src/clipen/editing/speech_coverage.py +30 -0
- package/src/clipen/editing/speech_quality.py +6 -0
- package/src/clipen/editing/tool_receipts.py +52 -0
- package/src/clipen/editing/versions.py +161 -0
- package/src/clipen/editing/visual_preview.py +93 -0
- package/src/clipen/editing/workbench.py +74 -0
- package/src/clipen/filesystem.py +25 -0
- package/src/clipen/finishing/README.md +103 -0
- package/src/clipen/finishing/__init__.py +1 -0
- package/src/clipen/finishing/audio.py +149 -0
- package/src/clipen/finishing/catalog.py +181 -0
- package/src/clipen/finishing/models.py +14 -0
- package/src/clipen/finishing/renderer.py +26 -0
- package/src/clipen/mcp_editing/__init__.py +5 -0
- package/src/clipen/mcp_editing/__main__.py +40 -0
- package/src/clipen/mcp_editing/server.py +353 -0
- package/src/clipen/media/__init__.py +50 -0
- package/src/clipen/media/discovery.py +266 -0
- package/src/clipen/media/models.py +360 -0
- package/src/clipen/media/preview.py +147 -0
- package/src/clipen/media/probe.py +446 -0
- package/src/clipen/media/service.py +317 -0
- package/src/clipen/media/storage.py +742 -0
- package/src/clipen/music_vocabulary.py +5 -0
- package/src/clipen/planning/__init__.py +9 -0
- package/src/clipen/planning/models.py +240 -0
- package/src/clipen/power.py +23 -0
- package/src/clipen/projects/__init__.py +26 -0
- package/src/clipen/projects/agent_schema.py +94 -0
- package/src/clipen/projects/annotation_schema.py +40 -0
- package/src/clipen/projects/deletion.py +269 -0
- package/src/clipen/projects/evidence_schema.py +52 -0
- package/src/clipen/projects/finishing_schema.py +34 -0
- package/src/clipen/projects/models.py +192 -0
- package/src/clipen/projects/picker.py +256 -0
- package/src/clipen/projects/preview_schema.py +29 -0
- package/src/clipen/projects/service.py +175 -0
- package/src/clipen/projects/storage.py +1478 -0
- package/src/clipen/projects/timeline_schema.py +32 -0
- package/src/clipen/projects/validation_schema.py +28 -0
- package/src/clipen/provider_registry.py +75 -0
- package/src/clipen/providers/__init__.py +43 -0
- package/src/clipen/providers/account.py +194 -0
- package/src/clipen/providers/claude.py +375 -0
- package/src/clipen/providers/claude_editing.py +111 -0
- package/src/clipen/providers/codex.py +1036 -0
- package/src/clipen/providers/codex_editing.py +91 -0
- package/src/clipen/providers/editing_agent.py +565 -0
- package/src/clipen/providers/errors.py +51 -0
- package/src/clipen/providers/recovery.py +270 -0
- package/src/clipen/py.typed +1 -0
- package/src/clipen/rendering/__init__.py +12 -0
- package/src/clipen/rendering/cover.py +126 -0
- package/src/clipen/rendering/custom_components.py +240 -0
- package/src/clipen/rendering/custom_render.py +197 -0
- package/src/clipen/rendering/fonts/anton-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/bebasneue-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/caveat-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/chenyu-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/delagothicone-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/hachimarupop-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/iansui-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/manifest.json +145 -0
- package/src/clipen/rendering/fonts/playfairdisplay-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/rocknrollone-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/yomogi-OFL.txt +94 -0
- package/src/clipen/rendering/fonts/yuseimagic-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/zenkurenaido-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/zenmarugothic-OFL.txt +93 -0
- package/src/clipen/rendering/production_effects.py +64 -0
- package/src/clipen/rendering/program_components.py +193 -0
- package/src/clipen/rendering/program_creator.py +649 -0
- package/src/clipen/rendering/program_identities.py +255 -0
- package/src/clipen/rendering/program_motion.py +141 -0
- package/src/clipen/rendering/program_studio.py +168 -0
- package/src/clipen/rendering/program_styles.py +235 -0
- package/src/clipen/rendering/program_subtitles.py +52 -0
- package/src/clipen/rendering/renderer.py +708 -0
- package/src/clipen/rendering/storage.py +243 -0
- package/src/clipen/rendering/titles.py +360 -0
- package/src/clipen/skills/__init__.py +4 -0
- package/src/clipen/skills/builtin/SKILL.md +72 -0
- package/src/clipen/skills/builtin/assets/sample-daily-vlog.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-event-recap.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-food-review.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-demo.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-review.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-unboxing.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-shorts-highlight.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-travel-highlight.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-travel-vlog.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/style-cinema.png +0 -0
- package/src/clipen/skills/builtin/assets/style-editorial.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-daily-vlog.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-event-recap.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-food-review.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-demo.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-review.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-unboxing.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-shorts-highlight.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-travel-highlight.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-travel-vlog.png +0 -0
- package/src/clipen/skills/builtin/assets/style-variety.png +0 -0
- package/src/clipen/skills/builtin/components.json +133 -0
- package/src/clipen/skills/builtin/font_sources.json +20 -0
- package/src/clipen/skills/builtin/font_styles.json +329 -0
- package/src/clipen/skills/builtin/manifest.json +32 -0
- package/src/clipen/skills/builtin/presets.json +8 -0
- package/src/clipen/skills/builtin/references/captions.md +14 -0
- package/src/clipen/skills/builtin/references/channel-design.md +37 -0
- package/src/clipen/skills/builtin/references/editing.md +30 -0
- package/src/clipen/skills/builtin/references/font-chenyu-OFL.txt +93 -0
- package/src/clipen/skills/builtin/references/font-iansui-OFL.txt +93 -0
- package/src/clipen/skills/builtin/references/graphics.md +59 -0
- package/src/clipen/skills/builtin/references/review.md +16 -0
- package/src/clipen/skills/builtin/references/sound.md +8 -0
- package/src/clipen/skills/builtin/references/story.md +7 -0
- package/src/clipen/skills/builtin/skills/travel-program/SKILL.md +27 -0
- package/src/clipen/skills/builtin/skills/travel-program/manifest.json +6 -0
- package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/SKILL.md +33 -0
- package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-event-recap/SKILL.md +34 -0
- package/src/clipen/skills/builtin/template-skills/clipen-event-recap/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-food-review/SKILL.md +33 -0
- package/src/clipen/skills/builtin/template-skills/clipen-food-review/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-demo/SKILL.md +35 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-demo/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-review/SKILL.md +36 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-review/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/SKILL.md +39 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/SKILL.md +38 -0
- package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/SKILL.md +34 -0
- package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/SKILL.md +37 -0
- package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/manifest.json +8 -0
- package/src/clipen/skills/builtin/templates.json +284 -0
- package/src/clipen/skills/builtin/visual_styles.json +1074 -0
- package/src/clipen/skills/catalog.py +218 -0
- package/src/clipen/skills/library.py +131 -0
- package/src/clipen/skills/package.py +231 -0
- package/src/clipen/skills/visual_styles.py +44 -0
- package/src/clipen/workspace.py +210 -0
- package/uninstall +25 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Provider-neutral failures shared by every AI adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Protocol
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ProviderUnavailable(RuntimeError):
|
|
9
|
+
"""The selected provider cannot be launched on this machine."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ProviderProtocolError(RuntimeError):
|
|
13
|
+
"""The provider violated the expected one-shot result contract."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ProviderStoppedEarly(ProviderProtocolError):
|
|
17
|
+
"""The provider ended its turn before finishing the Clipen edit."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ProviderDeliveryBlocked(ProviderStoppedEarly):
|
|
21
|
+
"""The provider ended its turn while Clipen's delivery checks still fail."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ProviderTimedOut(RuntimeError):
|
|
25
|
+
"""The provider exceeded the bounded attempt duration."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ProviderCancelled(RuntimeError):
|
|
29
|
+
"""The caller cancelled the provider attempt."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ProviderAuthenticationError(ProviderUnavailable):
|
|
33
|
+
"""The provider login is missing or no longer valid."""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ProviderUsageLimitError(RuntimeError):
|
|
37
|
+
"""The provider rejected the attempt because of an account limit."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ProviderNetworkError(RuntimeError):
|
|
41
|
+
"""The provider could not reach its service."""
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ProviderPermissionError(RuntimeError):
|
|
45
|
+
"""The provider sandbox or permission contract was rejected."""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class CancellationToken(Protocol):
|
|
49
|
+
"""Minimal cancellation contract accepted by the adapter."""
|
|
50
|
+
|
|
51
|
+
def is_set(self) -> bool: ...
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""Durable, bounded receipts for reconciling one-shot provider attempts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
from enum import Enum
|
|
8
|
+
import hashlib
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
import uuid
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
_RECEIPT_SCHEMA = 1
|
|
16
|
+
_MAX_RECEIPT_BYTES = 64 * 1024
|
|
17
|
+
_SHA256_HEX_LENGTH = 64
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class AttemptReceiptError(RuntimeError):
|
|
21
|
+
"""A persisted provider receipt is missing, unsafe, or malformed."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ProviderAttemptState(str, Enum):
|
|
25
|
+
PREPARED = "prepared"
|
|
26
|
+
RUNNING = "running"
|
|
27
|
+
SUCCEEDED = "succeeded"
|
|
28
|
+
FAILED = "failed"
|
|
29
|
+
CANCELLED = "cancelled"
|
|
30
|
+
TIMED_OUT = "timed_out"
|
|
31
|
+
INTERRUPTED = "interrupted"
|
|
32
|
+
UNKNOWN = "unknown"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class ProviderUsage:
|
|
37
|
+
input_tokens: int
|
|
38
|
+
cached_input_tokens: int
|
|
39
|
+
output_tokens: int
|
|
40
|
+
|
|
41
|
+
def __post_init__(self) -> None:
|
|
42
|
+
for value in (
|
|
43
|
+
self.input_tokens,
|
|
44
|
+
self.cached_input_tokens,
|
|
45
|
+
self.output_tokens,
|
|
46
|
+
):
|
|
47
|
+
if not isinstance(value, int) or isinstance(value, bool) or value < 0:
|
|
48
|
+
raise ValueError("provider token usage must contain non-negative integers")
|
|
49
|
+
if self.cached_input_tokens > self.input_tokens:
|
|
50
|
+
raise ValueError("cached input tokens cannot exceed input tokens")
|
|
51
|
+
|
|
52
|
+
def to_dict(self) -> dict[str, int]:
|
|
53
|
+
return {
|
|
54
|
+
"input_tokens": self.input_tokens,
|
|
55
|
+
"cached_input_tokens": self.cached_input_tokens,
|
|
56
|
+
"output_tokens": self.output_tokens,
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@classmethod
|
|
60
|
+
def from_dict(cls, value: object) -> "ProviderUsage":
|
|
61
|
+
if not isinstance(value, dict) or set(value) != {
|
|
62
|
+
"input_tokens",
|
|
63
|
+
"cached_input_tokens",
|
|
64
|
+
"output_tokens",
|
|
65
|
+
}:
|
|
66
|
+
raise ValueError("provider token usage is malformed")
|
|
67
|
+
return cls(
|
|
68
|
+
input_tokens=value["input_tokens"],
|
|
69
|
+
cached_input_tokens=value["cached_input_tokens"],
|
|
70
|
+
output_tokens=value["output_tokens"],
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def utc_now() -> str:
|
|
75
|
+
return datetime.now(timezone.utc).isoformat(timespec="microseconds")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def sha256_bytes(value: bytes) -> str:
|
|
79
|
+
return hashlib.sha256(value).hexdigest()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def sha256_file(path: Path, limit: int) -> str:
|
|
83
|
+
if path.is_symlink() or not path.is_file():
|
|
84
|
+
raise AttemptReceiptError("provider artifact is missing or unsafe")
|
|
85
|
+
size = path.stat().st_size
|
|
86
|
+
if size > limit:
|
|
87
|
+
raise AttemptReceiptError("provider artifact exceeds its size limit")
|
|
88
|
+
digest = hashlib.sha256()
|
|
89
|
+
with path.open("rb") as stream:
|
|
90
|
+
remaining = size
|
|
91
|
+
while remaining:
|
|
92
|
+
block = stream.read(min(64 * 1024, remaining))
|
|
93
|
+
if not block:
|
|
94
|
+
raise AttemptReceiptError("provider artifact changed while hashing")
|
|
95
|
+
digest.update(block)
|
|
96
|
+
remaining -= len(block)
|
|
97
|
+
return digest.hexdigest()
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _optional_hash(value: object, field: str) -> str | None:
|
|
101
|
+
if value is None:
|
|
102
|
+
return None
|
|
103
|
+
if (
|
|
104
|
+
not isinstance(value, str)
|
|
105
|
+
or len(value) != _SHA256_HEX_LENGTH
|
|
106
|
+
or any(character not in "0123456789abcdef" for character in value)
|
|
107
|
+
):
|
|
108
|
+
raise ValueError(f"{field} must be a SHA-256 hex digest")
|
|
109
|
+
return value
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _optional_string(value: object, field: str, limit: int = 128) -> str | None:
|
|
113
|
+
if value is None:
|
|
114
|
+
return None
|
|
115
|
+
if not isinstance(value, str) or not value or len(value) > limit:
|
|
116
|
+
raise ValueError(f"{field} must be a bounded string")
|
|
117
|
+
return value
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _optional_integer(value: object, field: str) -> int | None:
|
|
121
|
+
if value is None:
|
|
122
|
+
return None
|
|
123
|
+
if not isinstance(value, int) or isinstance(value, bool) or value < 0:
|
|
124
|
+
raise ValueError(f"{field} must be a non-negative integer")
|
|
125
|
+
return value
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@dataclass(frozen=True)
|
|
129
|
+
class ProviderAttemptReceipt:
|
|
130
|
+
attempt_id: str
|
|
131
|
+
state: ProviderAttemptState
|
|
132
|
+
prompt_sha256: str
|
|
133
|
+
schema_sha256: str
|
|
134
|
+
started_at: str | None = None
|
|
135
|
+
finished_at: str | None = None
|
|
136
|
+
pid: int | None = None
|
|
137
|
+
return_code: int | None = None
|
|
138
|
+
output_sha256: str | None = None
|
|
139
|
+
events_sha256: str | None = None
|
|
140
|
+
diagnostics_sha256: str | None = None
|
|
141
|
+
usage: ProviderUsage | None = None
|
|
142
|
+
error_code: str | None = None
|
|
143
|
+
|
|
144
|
+
def __post_init__(self) -> None:
|
|
145
|
+
if not isinstance(self.attempt_id, str) or not self.attempt_id or len(self.attempt_id) > 128:
|
|
146
|
+
raise ValueError("attempt_id must be a bounded string")
|
|
147
|
+
_optional_hash(self.prompt_sha256, "prompt_sha256")
|
|
148
|
+
_optional_hash(self.schema_sha256, "schema_sha256")
|
|
149
|
+
_optional_hash(self.output_sha256, "output_sha256")
|
|
150
|
+
_optional_hash(self.events_sha256, "events_sha256")
|
|
151
|
+
_optional_hash(self.diagnostics_sha256, "diagnostics_sha256")
|
|
152
|
+
_optional_string(self.started_at, "started_at", 64)
|
|
153
|
+
_optional_string(self.finished_at, "finished_at", 64)
|
|
154
|
+
_optional_integer(self.pid, "pid")
|
|
155
|
+
if self.return_code is not None and (
|
|
156
|
+
not isinstance(self.return_code, int) or isinstance(self.return_code, bool)
|
|
157
|
+
):
|
|
158
|
+
raise ValueError("return_code must be an integer")
|
|
159
|
+
_optional_string(self.error_code, "error_code", 64)
|
|
160
|
+
|
|
161
|
+
def to_dict(self) -> dict[str, object]:
|
|
162
|
+
return {
|
|
163
|
+
"schema": _RECEIPT_SCHEMA,
|
|
164
|
+
"provider": "codex",
|
|
165
|
+
"attempt_id": self.attempt_id,
|
|
166
|
+
"state": self.state.value,
|
|
167
|
+
"prompt_sha256": self.prompt_sha256,
|
|
168
|
+
"schema_sha256": self.schema_sha256,
|
|
169
|
+
"started_at": self.started_at,
|
|
170
|
+
"finished_at": self.finished_at,
|
|
171
|
+
"pid": self.pid,
|
|
172
|
+
"return_code": self.return_code,
|
|
173
|
+
"output_sha256": self.output_sha256,
|
|
174
|
+
"events_sha256": self.events_sha256,
|
|
175
|
+
"diagnostics_sha256": self.diagnostics_sha256,
|
|
176
|
+
"usage": None if self.usage is None else self.usage.to_dict(),
|
|
177
|
+
"error_code": self.error_code,
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@classmethod
|
|
181
|
+
def from_dict(cls, value: object) -> "ProviderAttemptReceipt":
|
|
182
|
+
expected = {
|
|
183
|
+
"schema",
|
|
184
|
+
"provider",
|
|
185
|
+
"attempt_id",
|
|
186
|
+
"state",
|
|
187
|
+
"prompt_sha256",
|
|
188
|
+
"schema_sha256",
|
|
189
|
+
"started_at",
|
|
190
|
+
"finished_at",
|
|
191
|
+
"pid",
|
|
192
|
+
"return_code",
|
|
193
|
+
"output_sha256",
|
|
194
|
+
"events_sha256",
|
|
195
|
+
"diagnostics_sha256",
|
|
196
|
+
"usage",
|
|
197
|
+
"error_code",
|
|
198
|
+
}
|
|
199
|
+
if not isinstance(value, dict) or set(value) != expected:
|
|
200
|
+
raise ValueError("provider attempt receipt has unexpected fields")
|
|
201
|
+
if value["schema"] != _RECEIPT_SCHEMA or value["provider"] != "codex":
|
|
202
|
+
raise ValueError("provider attempt receipt version is unsupported")
|
|
203
|
+
usage = None if value["usage"] is None else ProviderUsage.from_dict(value["usage"])
|
|
204
|
+
return cls(
|
|
205
|
+
attempt_id=value["attempt_id"],
|
|
206
|
+
state=ProviderAttemptState(value["state"]),
|
|
207
|
+
prompt_sha256=value["prompt_sha256"],
|
|
208
|
+
schema_sha256=value["schema_sha256"],
|
|
209
|
+
started_at=value["started_at"],
|
|
210
|
+
finished_at=value["finished_at"],
|
|
211
|
+
pid=value["pid"],
|
|
212
|
+
return_code=value["return_code"],
|
|
213
|
+
output_sha256=value["output_sha256"],
|
|
214
|
+
events_sha256=value["events_sha256"],
|
|
215
|
+
diagnostics_sha256=value["diagnostics_sha256"],
|
|
216
|
+
usage=usage,
|
|
217
|
+
error_code=value["error_code"],
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class AttemptReceiptStore:
|
|
222
|
+
"""Atomically replace one private receipt inside an attempt workspace."""
|
|
223
|
+
|
|
224
|
+
def __init__(self, workspace: Path, attempt_id: str) -> None:
|
|
225
|
+
if workspace.is_symlink():
|
|
226
|
+
raise AttemptReceiptError("attempt workspace cannot be a symbolic link")
|
|
227
|
+
self.workspace = workspace.resolve(strict=True)
|
|
228
|
+
if not self.workspace.is_dir():
|
|
229
|
+
raise AttemptReceiptError("attempt workspace must be a directory")
|
|
230
|
+
self.path = self.workspace / f"provider-attempt-{attempt_id}.json"
|
|
231
|
+
|
|
232
|
+
def write(self, receipt: ProviderAttemptReceipt) -> None:
|
|
233
|
+
if self.path.is_symlink():
|
|
234
|
+
raise AttemptReceiptError("provider attempt receipt cannot be a symbolic link")
|
|
235
|
+
encoded = json.dumps(
|
|
236
|
+
receipt.to_dict(), ensure_ascii=False, sort_keys=True, separators=(",", ":")
|
|
237
|
+
).encode("utf-8")
|
|
238
|
+
if len(encoded) > _MAX_RECEIPT_BYTES:
|
|
239
|
+
raise AttemptReceiptError("provider attempt receipt exceeds its size limit")
|
|
240
|
+
temporary = self.workspace / f".receipt-{receipt.attempt_id}-{uuid.uuid4().hex}"
|
|
241
|
+
descriptor = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
242
|
+
try:
|
|
243
|
+
with os.fdopen(descriptor, "wb") as stream:
|
|
244
|
+
stream.write(encoded)
|
|
245
|
+
stream.flush()
|
|
246
|
+
os.fsync(stream.fileno())
|
|
247
|
+
os.replace(temporary, self.path)
|
|
248
|
+
os.chmod(self.path, 0o600)
|
|
249
|
+
directory = os.open(self.workspace, os.O_RDONLY)
|
|
250
|
+
try:
|
|
251
|
+
os.fsync(directory)
|
|
252
|
+
finally:
|
|
253
|
+
os.close(directory)
|
|
254
|
+
finally:
|
|
255
|
+
if temporary.exists() and not temporary.is_symlink():
|
|
256
|
+
temporary.unlink()
|
|
257
|
+
|
|
258
|
+
def read(self) -> ProviderAttemptReceipt:
|
|
259
|
+
if self.path.is_symlink() or not self.path.is_file():
|
|
260
|
+
raise AttemptReceiptError("provider attempt receipt is missing or unsafe")
|
|
261
|
+
if self.path.stat().st_size > _MAX_RECEIPT_BYTES:
|
|
262
|
+
raise AttemptReceiptError("provider attempt receipt exceeds its size limit")
|
|
263
|
+
try:
|
|
264
|
+
value = json.loads(self.path.read_text(encoding="utf-8"))
|
|
265
|
+
receipt = ProviderAttemptReceipt.from_dict(value)
|
|
266
|
+
except (OSError, UnicodeError, json.JSONDecodeError, ValueError) as error:
|
|
267
|
+
raise AttemptReceiptError("provider attempt receipt is malformed") from error
|
|
268
|
+
if self.path.stat().st_mode & 0o077:
|
|
269
|
+
raise AttemptReceiptError("provider attempt receipt permissions are unsafe")
|
|
270
|
+
return receipt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Local rough-cut preview rendering."""
|
|
2
|
+
|
|
3
|
+
from .cover import ProjectCoverRepository
|
|
4
|
+
from .renderer import FfmpegPreviewRenderer, PreviewRenderError
|
|
5
|
+
from .storage import PreviewRenderRepository
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"FfmpegPreviewRenderer",
|
|
9
|
+
"PreviewRenderError",
|
|
10
|
+
"PreviewRenderRepository",
|
|
11
|
+
"ProjectCoverRepository",
|
|
12
|
+
]
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""A still from a project's own footage, shown as its cover on the home page."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import secrets
|
|
8
|
+
import shutil
|
|
9
|
+
import subprocess
|
|
10
|
+
|
|
11
|
+
from clipen.media import MediaPreviewRepository
|
|
12
|
+
from clipen.projects import ProjectError, ProjectRepository
|
|
13
|
+
from clipen.projects.models import validate_project_id
|
|
14
|
+
|
|
15
|
+
from .storage import PreviewRenderRepository
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Far enough in to skip a black fade-in, then the very first frame for clips shorter than that.
|
|
19
|
+
_STILL_OFFSETS = (3.0, 0.0)
|
|
20
|
+
_SOURCE_CANDIDATES = 20
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ProjectCoverRepository:
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
projects: ProjectRepository,
|
|
27
|
+
executable: str | None = None,
|
|
28
|
+
*,
|
|
29
|
+
timeout_seconds: float = 20,
|
|
30
|
+
) -> None:
|
|
31
|
+
self.projects = projects
|
|
32
|
+
self.executable = executable or shutil.which("ffmpeg")
|
|
33
|
+
self.timeout_seconds = timeout_seconds
|
|
34
|
+
|
|
35
|
+
def cover(self, project_id: str) -> Path:
|
|
36
|
+
"""The cover still, cut once from the latest finished film or else the first source video."""
|
|
37
|
+
validate_project_id(project_id)
|
|
38
|
+
source = self._source(project_id)
|
|
39
|
+
if source is None:
|
|
40
|
+
raise _unavailable()
|
|
41
|
+
key, media = source
|
|
42
|
+
directory = self._directory(project_id)
|
|
43
|
+
cached = directory / f"{key}.jpg"
|
|
44
|
+
if cached.is_file() and not cached.is_symlink():
|
|
45
|
+
return cached
|
|
46
|
+
if not self.executable or not os.path.isabs(self.executable):
|
|
47
|
+
raise _unavailable()
|
|
48
|
+
staging = directory / f".{secrets.token_hex(8)}.partial.jpg"
|
|
49
|
+
try:
|
|
50
|
+
for offset in _STILL_OFFSETS:
|
|
51
|
+
if self._cut(media, offset, staging):
|
|
52
|
+
break
|
|
53
|
+
else:
|
|
54
|
+
raise _unavailable()
|
|
55
|
+
# A newer film or source replaces the old still rather than piling up beside it.
|
|
56
|
+
for stale in directory.glob("*.jpg"):
|
|
57
|
+
if not stale.name.startswith("."):
|
|
58
|
+
stale.unlink(missing_ok=True)
|
|
59
|
+
os.replace(staging, cached)
|
|
60
|
+
finally:
|
|
61
|
+
staging.unlink(missing_ok=True)
|
|
62
|
+
return cached
|
|
63
|
+
|
|
64
|
+
def _source(self, project_id: str) -> tuple[str, Path] | None:
|
|
65
|
+
renders = PreviewRenderRepository(self.projects)
|
|
66
|
+
with self.projects.connect(project_id) as database:
|
|
67
|
+
finished = database.execute(
|
|
68
|
+
"SELECT render_id FROM preview_renders WHERE project_id=? AND state='succeeded' "
|
|
69
|
+
"ORDER BY completed_at DESC LIMIT 1",
|
|
70
|
+
(project_id,),
|
|
71
|
+
).fetchone()
|
|
72
|
+
videos = database.execute(
|
|
73
|
+
"SELECT versions.version_id FROM asset_versions AS versions "
|
|
74
|
+
"JOIN assets ON assets.asset_id=versions.asset_id "
|
|
75
|
+
"WHERE assets.project_id=? AND assets.role='source' AND versions.probe_status='ready' "
|
|
76
|
+
"AND EXISTS(SELECT 1 FROM media_streams AS streams "
|
|
77
|
+
"WHERE streams.version_id=versions.version_id AND streams.kind='video') "
|
|
78
|
+
"ORDER BY assets.relative_path, versions.created_at DESC LIMIT ?",
|
|
79
|
+
(project_id, _SOURCE_CANDIDATES),
|
|
80
|
+
).fetchall()
|
|
81
|
+
if finished is not None:
|
|
82
|
+
try:
|
|
83
|
+
return finished["render_id"], renders.media_path(project_id, finished["render_id"])
|
|
84
|
+
except ProjectError:
|
|
85
|
+
pass
|
|
86
|
+
previews = MediaPreviewRepository(self.projects)
|
|
87
|
+
for row in videos:
|
|
88
|
+
try:
|
|
89
|
+
path, _ = previews.locate(project_id, row["version_id"])
|
|
90
|
+
except ProjectError:
|
|
91
|
+
continue
|
|
92
|
+
return row["version_id"], path
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
def _directory(self, project_id: str) -> Path:
|
|
96
|
+
root = self.projects.root / project_id
|
|
97
|
+
if root.is_symlink() or not root.is_dir():
|
|
98
|
+
raise ProjectError("project_not_found", "找不到這個作品。", status_code=404)
|
|
99
|
+
directory = root / "covers"
|
|
100
|
+
directory.mkdir(mode=0o700, exist_ok=True)
|
|
101
|
+
if directory.is_symlink() or not directory.is_dir():
|
|
102
|
+
raise ProjectError("storage_corrupt", "Clipen 無法安全建立封面資料夾。", status_code=500)
|
|
103
|
+
return directory
|
|
104
|
+
|
|
105
|
+
def _cut(self, media: Path, offset: float, output: Path) -> bool:
|
|
106
|
+
command = (
|
|
107
|
+
self.executable, "-nostdin", "-hide_banner", "-loglevel", "error",
|
|
108
|
+
"-ss", f"{offset:.3f}", "-i", str(media),
|
|
109
|
+
"-frames:v", "1", "-vf", "scale=640:-2", "-q:v", "4", "-y", str(output),
|
|
110
|
+
)
|
|
111
|
+
try:
|
|
112
|
+
completed = subprocess.run(
|
|
113
|
+
command,
|
|
114
|
+
stdin=subprocess.DEVNULL,
|
|
115
|
+
stdout=subprocess.DEVNULL,
|
|
116
|
+
stderr=subprocess.DEVNULL,
|
|
117
|
+
timeout=self.timeout_seconds,
|
|
118
|
+
check=False,
|
|
119
|
+
)
|
|
120
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
121
|
+
return False
|
|
122
|
+
return completed.returncode == 0 and output.is_file() and output.stat().st_size > 0
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _unavailable() -> ProjectError:
|
|
126
|
+
return ProjectError("cover_unavailable", "這個作品還沒有可用的封面。", status_code=404)
|