@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.
- package/LICENSE +21 -0
- package/README.md +89 -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 +55 -4
- package/pyproject.toml +44 -0
- package/requirements/asr-runtime.txt +13 -0
- package/requirements/dev.txt +2 -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 +410 -0
- package/src/clipen/analysis/audio.py +394 -0
- package/src/clipen/analysis/evidence.py +102 -0
- package/src/clipen/analysis/evidence_service.py +929 -0
- package/src/clipen/analysis/image_similarity.py +134 -0
- package/src/clipen/analysis/keyframes.py +159 -0
- package/src/clipen/analysis/models.py +228 -0
- package/src/clipen/analysis/qwen_worker.py +594 -0
- package/src/clipen/analysis/resources.py +22 -0
- package/src/clipen/analysis/runtime.py +62 -0
- package/src/clipen/analysis/service.py +594 -0
- package/src/clipen/analysis/setup.py +921 -0
- package/src/clipen/analysis/setup_cli.py +107 -0
- package/src/clipen/analysis/storage.py +1864 -0
- package/src/clipen/analysis/transcription.py +906 -0
- package/src/clipen/analysis/visual.py +237 -0
- package/src/clipen/annotations/__init__.py +13 -0
- package/src/clipen/annotations/models.py +22 -0
- package/src/clipen/annotations/storage.py +428 -0
- package/src/clipen/annotations/suggestions.py +115 -0
- package/src/clipen/asr_release.py +132 -0
- package/src/clipen/bootstrap.py +978 -0
- package/src/clipen/candidates.py +222 -0
- package/src/clipen/cli.py +84 -0
- package/src/clipen/controller/__init__.py +17 -0
- package/src/clipen/controller/app.py +1657 -0
- package/src/clipen/controller/server.py +533 -0
- package/src/clipen/domain/__init__.py +30 -0
- package/src/clipen/domain/errors.py +5 -0
- package/src/clipen/domain/media_time.py +110 -0
- package/src/clipen/domain/timeline.py +582 -0
- package/src/clipen/domain/validation.py +14 -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 +127 -0
- package/src/clipen/editing/agent_repository.py +978 -0
- package/src/clipen/editing/agent_tools.py +613 -0
- package/src/clipen/editing/alignment.py +150 -0
- package/src/clipen/editing/alignment_setup.py +156 -0
- package/src/clipen/editing/alignment_worker.py +24 -0
- package/src/clipen/editing/change_report.py +128 -0
- package/src/clipen/editing/compiler.py +40 -0
- package/src/clipen/editing/component_looks.py +131 -0
- package/src/clipen/editing/component_review.py +74 -0
- package/src/clipen/editing/coordinator.py +497 -0
- package/src/clipen/editing/editorial_review.py +309 -0
- package/src/clipen/editing/evidence_jobs.py +552 -0
- package/src/clipen/editing/history_changes.py +192 -0
- package/src/clipen/editing/music_choices.py +18 -0
- package/src/clipen/editing/production_catalog.py +629 -0
- package/src/clipen/editing/production_limits.py +85 -0
- package/src/clipen/editing/production_media.py +435 -0
- package/src/clipen/editing/production_preview.py +373 -0
- package/src/clipen/editing/production_progress.py +236 -0
- package/src/clipen/editing/production_requirements.py +117 -0
- package/src/clipen/editing/production_tools.py +1198 -0
- package/src/clipen/editing/program_audio.py +248 -0
- package/src/clipen/editing/program_direction.py +83 -0
- package/src/clipen/editing/program_story.py +91 -0
- package/src/clipen/editing/program_subtitles.py +232 -0
- package/src/clipen/editing/programme_review.py +121 -0
- package/src/clipen/editing/prompt_history.py +84 -0
- package/src/clipen/editing/recut.py +167 -0
- package/src/clipen/editing/refinement_references.py +278 -0
- package/src/clipen/editing/resolve_export.py +847 -0
- package/src/clipen/editing/resolve_titles.py +291 -0
- package/src/clipen/editing/skill_tools.py +951 -0
- package/src/clipen/editing/source_dialogue.py +192 -0
- package/src/clipen/editing/speech_coverage.py +50 -0
- package/src/clipen/editing/speech_quality.py +11 -0
- package/src/clipen/editing/tool_receipts.py +98 -0
- package/src/clipen/editing/versions.py +219 -0
- package/src/clipen/editing/visual_preview.py +189 -0
- package/src/clipen/editing/workbench.py +136 -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 +188 -0
- package/src/clipen/finishing/catalog.py +264 -0
- package/src/clipen/finishing/models.py +14 -0
- package/src/clipen/finishing/renderer.py +45 -0
- package/src/clipen/mcp_editing/__init__.py +5 -0
- package/src/clipen/mcp_editing/__main__.py +42 -0
- package/src/clipen/mcp_editing/server.py +396 -0
- package/src/clipen/media/__init__.py +50 -0
- package/src/clipen/media/discovery.py +253 -0
- package/src/clipen/media/models.py +349 -0
- package/src/clipen/media/preview.py +137 -0
- package/src/clipen/media/probe.py +418 -0
- package/src/clipen/media/service.py +304 -0
- package/src/clipen/media/storage.py +694 -0
- package/src/clipen/music_vocabulary.py +34 -0
- package/src/clipen/power.py +28 -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 +256 -0
- package/src/clipen/projects/evidence_schema.py +52 -0
- package/src/clipen/projects/finishing_schema.py +36 -0
- package/src/clipen/projects/models.py +189 -0
- package/src/clipen/projects/picker.py +232 -0
- package/src/clipen/projects/preview_schema.py +29 -0
- package/src/clipen/projects/service.py +159 -0
- package/src/clipen/projects/storage.py +1458 -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 +77 -0
- package/src/clipen/providers/__init__.py +43 -0
- package/src/clipen/providers/account.py +245 -0
- package/src/clipen/providers/claude.py +409 -0
- package/src/clipen/providers/claude_editing.py +118 -0
- package/src/clipen/providers/codex.py +991 -0
- package/src/clipen/providers/codex_editing.py +91 -0
- package/src/clipen/providers/editing_agent.py +567 -0
- package/src/clipen/providers/errors.py +51 -0
- package/src/clipen/providers/recovery.py +269 -0
- package/src/clipen/py.typed +1 -0
- package/src/clipen/rendering/__init__.py +12 -0
- package/src/clipen/rendering/cover.py +139 -0
- package/src/clipen/rendering/custom_components.py +328 -0
- package/src/clipen/rendering/custom_render.py +218 -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 +96 -0
- package/src/clipen/rendering/program_components.py +254 -0
- package/src/clipen/rendering/program_creator.py +804 -0
- package/src/clipen/rendering/program_identities.py +350 -0
- package/src/clipen/rendering/program_motion.py +186 -0
- package/src/clipen/rendering/program_studio.py +227 -0
- package/src/clipen/rendering/program_styles.py +321 -0
- package/src/clipen/rendering/program_subtitles.py +89 -0
- package/src/clipen/rendering/renderer.py +737 -0
- package/src/clipen/rendering/storage.py +230 -0
- package/src/clipen/rendering/titles.py +467 -0
- package/src/clipen/skills/__init__.py +5 -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 +314 -0
- package/src/clipen/skills/library.py +137 -0
- package/src/clipen/skills/package.py +254 -0
- package/src/clipen/skills/visual_styles.py +55 -0
- package/src/clipen/workspace.py +181 -0
- package/uninstall +25 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""Validated project workspace values shared by storage and the Controller."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from typing import Any, Mapping
|
|
9
|
+
|
|
10
|
+
_PROJECT_ID = re.compile(r"prj_[0-9a-f]{32}\Z")
|
|
11
|
+
_REQUEST_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._:-]{7,127}\Z")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ProjectError(RuntimeError):
|
|
15
|
+
"""A project command failed with a stable product-facing error."""
|
|
16
|
+
|
|
17
|
+
def __init__(self, code: str, message: str, *, status_code: int = 400) -> None:
|
|
18
|
+
super().__init__(message)
|
|
19
|
+
self.code = code
|
|
20
|
+
self.message = message
|
|
21
|
+
self.status_code = status_code
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class DirectoryRole(str, Enum):
|
|
25
|
+
SOURCE = "source"
|
|
26
|
+
REFERENCE = "reference"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class DirectoryAvailability(str, Enum):
|
|
30
|
+
AVAILABLE = "available"
|
|
31
|
+
OFFLINE = "offline"
|
|
32
|
+
CHANGED = "changed"
|
|
33
|
+
PERMISSION_REQUIRED = "permission_required"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def validate_project_id(value: str) -> str:
|
|
37
|
+
if not isinstance(value, str) or _PROJECT_ID.fullmatch(value) is None:
|
|
38
|
+
raise ProjectError("project_not_found", "找不到這個作品。", status_code=404)
|
|
39
|
+
return value
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def validate_request_id(value: object) -> str:
|
|
43
|
+
if not isinstance(value, str) or _REQUEST_ID.fullmatch(value) is None:
|
|
44
|
+
raise ProjectError("request_invalid", "這次操作的識別資訊無效。", status_code=422)
|
|
45
|
+
return value
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def validate_name(value: object) -> str:
|
|
49
|
+
if not isinstance(value, str):
|
|
50
|
+
raise ProjectError("name_invalid", "請輸入作品名稱。", status_code=422)
|
|
51
|
+
name = value.strip()
|
|
52
|
+
if not name or len(name) > 120 or any(ord(character) < 32 for character in name):
|
|
53
|
+
raise ProjectError("name_invalid", "作品名稱需為 1 至 120 個字。", status_code=422)
|
|
54
|
+
return name
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def validate_brief(value: object) -> str:
|
|
58
|
+
if not isinstance(value, str):
|
|
59
|
+
raise ProjectError("brief_invalid", "剪輯需求格式無效。", status_code=422)
|
|
60
|
+
brief = value.strip()
|
|
61
|
+
if len(brief) > 8000 or "\x00" in brief:
|
|
62
|
+
raise ProjectError("brief_invalid", "剪輯需求不能超過 8,000 個字。", status_code=422)
|
|
63
|
+
return brief
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def validate_revision(value: object) -> int:
|
|
67
|
+
if isinstance(value, bool) or not isinstance(value, int) or value < 1:
|
|
68
|
+
raise ProjectError("revision_invalid", "作品版本資訊無效。", status_code=422)
|
|
69
|
+
return value
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass(frozen=True)
|
|
73
|
+
class DirectoryReference:
|
|
74
|
+
role: DirectoryRole
|
|
75
|
+
canonical_path: str
|
|
76
|
+
display_path: str
|
|
77
|
+
display_name: str
|
|
78
|
+
device: int
|
|
79
|
+
inode: int
|
|
80
|
+
saved_at: str
|
|
81
|
+
availability: DirectoryAvailability = DirectoryAvailability.AVAILABLE
|
|
82
|
+
|
|
83
|
+
def __post_init__(self) -> None:
|
|
84
|
+
if not self.canonical_path.startswith("/") or "\x00" in self.canonical_path:
|
|
85
|
+
raise ValueError("directory canonical path must be an absolute POSIX path")
|
|
86
|
+
if not self.display_path or len(self.display_path) > 4096:
|
|
87
|
+
raise ValueError("directory display path must be non-empty and bounded")
|
|
88
|
+
if not self.display_name or len(self.display_name) > 255:
|
|
89
|
+
raise ValueError("directory display name must be non-empty and bounded")
|
|
90
|
+
if self.device < 0 or self.inode < 0:
|
|
91
|
+
raise ValueError("directory identity must be non-negative")
|
|
92
|
+
|
|
93
|
+
def to_storage(self) -> dict[str, object]:
|
|
94
|
+
return {
|
|
95
|
+
"role": self.role.value,
|
|
96
|
+
"canonical_path": self.canonical_path,
|
|
97
|
+
"display_path": self.display_path,
|
|
98
|
+
"display_name": self.display_name,
|
|
99
|
+
"device": self.device,
|
|
100
|
+
"inode": self.inode,
|
|
101
|
+
"saved_at": self.saved_at,
|
|
102
|
+
"availability": self.availability.value,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
def to_public(self) -> dict[str, object]:
|
|
106
|
+
return {
|
|
107
|
+
"role": self.role.value,
|
|
108
|
+
"display_path": self.display_path,
|
|
109
|
+
"display_name": self.display_name,
|
|
110
|
+
"availability": self.availability.value,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@classmethod
|
|
114
|
+
def from_storage(cls, value: Mapping[str, Any]) -> "DirectoryReference":
|
|
115
|
+
return cls(
|
|
116
|
+
role=DirectoryRole(value["role"]),
|
|
117
|
+
canonical_path=str(value["canonical_path"]),
|
|
118
|
+
display_path=str(value["display_path"]),
|
|
119
|
+
display_name=str(value["display_name"]),
|
|
120
|
+
device=int(value["device"]),
|
|
121
|
+
inode=int(value["inode"]),
|
|
122
|
+
saved_at=str(value["saved_at"]),
|
|
123
|
+
availability=DirectoryAvailability(value.get("availability", "available")),
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@dataclass(frozen=True)
|
|
128
|
+
class ProjectSnapshot:
|
|
129
|
+
project_id: str
|
|
130
|
+
name: str
|
|
131
|
+
brief: str
|
|
132
|
+
stage: str
|
|
133
|
+
revision: int
|
|
134
|
+
created_at: str
|
|
135
|
+
updated_at: str
|
|
136
|
+
directories: tuple[DirectoryReference, ...] = ()
|
|
137
|
+
last_activity_at: str | None = field(default=None, compare=False)
|
|
138
|
+
|
|
139
|
+
def __post_init__(self) -> None:
|
|
140
|
+
validate_project_id(self.project_id)
|
|
141
|
+
validate_name(self.name)
|
|
142
|
+
validate_brief(self.brief)
|
|
143
|
+
validate_revision(self.revision)
|
|
144
|
+
if self.stage != "materials":
|
|
145
|
+
raise ValueError("unsupported project stage")
|
|
146
|
+
roles = [reference.role for reference in self.directories]
|
|
147
|
+
if len(roles) != len(set(roles)):
|
|
148
|
+
raise ValueError("project directory roles must be unique")
|
|
149
|
+
|
|
150
|
+
def to_storage(self) -> dict[str, object]:
|
|
151
|
+
return {
|
|
152
|
+
"project_id": self.project_id,
|
|
153
|
+
"name": self.name,
|
|
154
|
+
"brief": self.brief,
|
|
155
|
+
"stage": self.stage,
|
|
156
|
+
"revision": self.revision,
|
|
157
|
+
"created_at": self.created_at,
|
|
158
|
+
"updated_at": self.updated_at,
|
|
159
|
+
"directories": [reference.to_storage() for reference in self.directories],
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
def to_public(self) -> dict[str, object]:
|
|
163
|
+
return {
|
|
164
|
+
"project_id": self.project_id,
|
|
165
|
+
"name": self.name,
|
|
166
|
+
"brief": self.brief,
|
|
167
|
+
"stage": self.stage,
|
|
168
|
+
"revision": self.revision,
|
|
169
|
+
"created_at": self.created_at,
|
|
170
|
+
"updated_at": self.updated_at,
|
|
171
|
+
"last_activity_at": self.last_activity_at or self.updated_at,
|
|
172
|
+
"directories": [reference.to_public() for reference in self.directories],
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
@classmethod
|
|
176
|
+
def from_storage(cls, value: Mapping[str, Any]) -> "ProjectSnapshot":
|
|
177
|
+
raw_directories = value.get("directories", [])
|
|
178
|
+
if not isinstance(raw_directories, list):
|
|
179
|
+
raise ValueError("stored project directories must be a list")
|
|
180
|
+
return cls(
|
|
181
|
+
project_id=str(value["project_id"]),
|
|
182
|
+
name=str(value["name"]),
|
|
183
|
+
brief=str(value["brief"]),
|
|
184
|
+
stage=str(value["stage"]),
|
|
185
|
+
revision=int(value["revision"]),
|
|
186
|
+
created_at=str(value["created_at"]),
|
|
187
|
+
updated_at=str(value["updated_at"]),
|
|
188
|
+
directories=tuple(DirectoryReference.from_storage(item) for item in raw_directories),
|
|
189
|
+
)
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"""Narrow native folder selection without exposing an arbitrary path API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import secrets
|
|
7
|
+
import stat
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
import threading
|
|
11
|
+
import time
|
|
12
|
+
from dataclasses import dataclass, replace
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Callable
|
|
15
|
+
|
|
16
|
+
from .models import (
|
|
17
|
+
DirectoryAvailability,
|
|
18
|
+
DirectoryReference,
|
|
19
|
+
DirectoryRole,
|
|
20
|
+
ProjectError,
|
|
21
|
+
)
|
|
22
|
+
from .storage import utc_now
|
|
23
|
+
|
|
24
|
+
_SELECTION_LIFETIME_SECONDS = 10 * 60
|
|
25
|
+
_MAX_PICKER_OUTPUT_BYTES = 8192
|
|
26
|
+
_PICKER_TIMEOUT_SECONDS = 5 * 60
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class NativeDirectoryPicker:
|
|
30
|
+
"""Select exactly one directory through a user-visible native dialog."""
|
|
31
|
+
|
|
32
|
+
def choose(self, role: DirectoryRole) -> Path | None:
|
|
33
|
+
raise NotImplementedError
|
|
34
|
+
|
|
35
|
+
def choose_folder(self, prompt: str) -> Path | None:
|
|
36
|
+
raise NotImplementedError
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MacOSDirectoryPicker(NativeDirectoryPicker):
|
|
40
|
+
_PROMPTS = {
|
|
41
|
+
DirectoryRole.SOURCE: "選擇毛片資料夾",
|
|
42
|
+
DirectoryRole.REFERENCE: "選擇參考作品資料夾",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
def choose(self, role: DirectoryRole) -> Path | None:
|
|
46
|
+
return self.choose_folder(self._PROMPTS[role])
|
|
47
|
+
|
|
48
|
+
def choose_folder(self, prompt: str) -> Path | None:
|
|
49
|
+
if sys.platform != "darwin":
|
|
50
|
+
raise ProjectError(
|
|
51
|
+
"picker_unsupported",
|
|
52
|
+
"這個版本目前只支援 macOS 資料夾選擇器。",
|
|
53
|
+
status_code=501,
|
|
54
|
+
)
|
|
55
|
+
script = (
|
|
56
|
+
"use scripting additions\n"
|
|
57
|
+
"try\n"
|
|
58
|
+
" tell current application to activate\n"
|
|
59
|
+
f' set selectedFolder to choose folder with prompt "{prompt}"\n'
|
|
60
|
+
" return POSIX path of selectedFolder\n"
|
|
61
|
+
"on error number -128\n"
|
|
62
|
+
' return ""\n'
|
|
63
|
+
"end try"
|
|
64
|
+
)
|
|
65
|
+
try:
|
|
66
|
+
completed = subprocess.run(
|
|
67
|
+
("/usr/bin/osascript", "-e", script),
|
|
68
|
+
stdin=subprocess.DEVNULL,
|
|
69
|
+
stdout=subprocess.PIPE,
|
|
70
|
+
stderr=subprocess.PIPE,
|
|
71
|
+
check=False,
|
|
72
|
+
timeout=_PICKER_TIMEOUT_SECONDS,
|
|
73
|
+
)
|
|
74
|
+
except subprocess.TimeoutExpired as error:
|
|
75
|
+
raise ProjectError("picker_timeout", "資料夾選擇已逾時,請再試一次。", status_code=408) from error
|
|
76
|
+
except OSError as error:
|
|
77
|
+
raise ProjectError("picker_unavailable", "無法開啟 macOS 資料夾選擇器。", status_code=500) from error
|
|
78
|
+
if completed.returncode != 0:
|
|
79
|
+
raise ProjectError("picker_failed", "無法完成資料夾選擇。", status_code=500)
|
|
80
|
+
if len(completed.stdout) > _MAX_PICKER_OUTPUT_BYTES:
|
|
81
|
+
raise ProjectError("picker_failed", "資料夾選擇器回傳的路徑過長。", status_code=500)
|
|
82
|
+
try:
|
|
83
|
+
output = completed.stdout.decode("utf-8").strip()
|
|
84
|
+
except UnicodeDecodeError as error:
|
|
85
|
+
raise ProjectError("picker_failed", "資料夾選擇器回傳無法辨識的路徑。", status_code=500) from error
|
|
86
|
+
if not output:
|
|
87
|
+
return None
|
|
88
|
+
if "\x00" in output or not output.startswith("/"):
|
|
89
|
+
raise ProjectError("picker_failed", "資料夾選擇器回傳無效的路徑。", status_code=500)
|
|
90
|
+
return Path(output)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclass(frozen=True)
|
|
94
|
+
class DirectorySelection:
|
|
95
|
+
token: str
|
|
96
|
+
reference: DirectoryReference
|
|
97
|
+
expires_at: float
|
|
98
|
+
|
|
99
|
+
def to_public(self) -> dict[str, object]:
|
|
100
|
+
return {
|
|
101
|
+
"selection_token": self.token,
|
|
102
|
+
"role": self.reference.role.value,
|
|
103
|
+
"display_path": self.reference.display_path,
|
|
104
|
+
"display_name": self.reference.display_name,
|
|
105
|
+
"expires_in_seconds": _SELECTION_LIFETIME_SECONDS,
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def inspect_directory(
|
|
110
|
+
selected_path: Path,
|
|
111
|
+
role: DirectoryRole,
|
|
112
|
+
managed_root: Path,
|
|
113
|
+
) -> DirectoryReference:
|
|
114
|
+
if not selected_path.is_absolute() or "\x00" in str(selected_path):
|
|
115
|
+
raise ProjectError("directory_invalid", "選取的資料夾路徑無效。", status_code=422)
|
|
116
|
+
try:
|
|
117
|
+
selected_info = selected_path.lstat()
|
|
118
|
+
if stat.S_ISLNK(selected_info.st_mode):
|
|
119
|
+
raise ProjectError(
|
|
120
|
+
"directory_symlink",
|
|
121
|
+
"請選擇實際資料夾,不要選擇 symbolic link。",
|
|
122
|
+
status_code=422,
|
|
123
|
+
)
|
|
124
|
+
canonical = selected_path.resolve(strict=True)
|
|
125
|
+
info = canonical.stat()
|
|
126
|
+
except FileNotFoundError as error:
|
|
127
|
+
raise ProjectError("directory_offline", "找不到選取的資料夾或外接磁碟。", status_code=422) from error
|
|
128
|
+
except PermissionError as error:
|
|
129
|
+
raise ProjectError(
|
|
130
|
+
"directory_permission",
|
|
131
|
+
"Clipen 沒有讀取這個資料夾的權限。",
|
|
132
|
+
status_code=422,
|
|
133
|
+
) from error
|
|
134
|
+
if not stat.S_ISDIR(info.st_mode):
|
|
135
|
+
raise ProjectError("directory_invalid", "請選擇一個資料夾。", status_code=422)
|
|
136
|
+
canonical_managed = managed_root.resolve(strict=True)
|
|
137
|
+
if (
|
|
138
|
+
canonical == canonical_managed
|
|
139
|
+
or canonical.is_relative_to(canonical_managed)
|
|
140
|
+
or canonical_managed.is_relative_to(canonical)
|
|
141
|
+
):
|
|
142
|
+
raise ProjectError(
|
|
143
|
+
"directory_managed_overlap",
|
|
144
|
+
"素材資料夾不能包含 Clipen 的管理資料。",
|
|
145
|
+
status_code=422,
|
|
146
|
+
)
|
|
147
|
+
if not os.access(canonical, os.R_OK | os.X_OK):
|
|
148
|
+
raise ProjectError(
|
|
149
|
+
"directory_permission",
|
|
150
|
+
"Clipen 沒有讀取這個資料夾的權限。",
|
|
151
|
+
status_code=422,
|
|
152
|
+
)
|
|
153
|
+
display_path = str(canonical)
|
|
154
|
+
try:
|
|
155
|
+
relative_home = canonical.relative_to(Path.home())
|
|
156
|
+
display_path = "~" if not relative_home.parts else f"~/{relative_home}"
|
|
157
|
+
except ValueError:
|
|
158
|
+
pass
|
|
159
|
+
return DirectoryReference(
|
|
160
|
+
role=role,
|
|
161
|
+
canonical_path=str(canonical),
|
|
162
|
+
display_path=display_path,
|
|
163
|
+
display_name=canonical.name or str(canonical),
|
|
164
|
+
device=int(info.st_dev),
|
|
165
|
+
inode=int(info.st_ino),
|
|
166
|
+
saved_at=utc_now(),
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def refresh_availability(reference: DirectoryReference) -> DirectoryReference:
|
|
171
|
+
path = Path(reference.canonical_path)
|
|
172
|
+
try:
|
|
173
|
+
info = path.lstat()
|
|
174
|
+
except FileNotFoundError:
|
|
175
|
+
return replace(reference, availability=DirectoryAvailability.OFFLINE)
|
|
176
|
+
except PermissionError:
|
|
177
|
+
return replace(reference, availability=DirectoryAvailability.PERMISSION_REQUIRED)
|
|
178
|
+
if stat.S_ISLNK(info.st_mode) or not stat.S_ISDIR(info.st_mode):
|
|
179
|
+
return replace(reference, availability=DirectoryAvailability.CHANGED)
|
|
180
|
+
if not os.access(path, os.R_OK | os.X_OK):
|
|
181
|
+
return replace(reference, availability=DirectoryAvailability.PERMISSION_REQUIRED)
|
|
182
|
+
# APFS external volumes can receive a different st_dev after a normal
|
|
183
|
+
# unmount/remount. The directory inode remains the stable object identity;
|
|
184
|
+
# every media file is still opened beneath this descriptor and checked
|
|
185
|
+
# against its saved size, mtime and fingerprint before processing.
|
|
186
|
+
if int(info.st_ino) != reference.inode:
|
|
187
|
+
return replace(reference, availability=DirectoryAvailability.CHANGED)
|
|
188
|
+
return replace(reference, availability=DirectoryAvailability.AVAILABLE)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class SelectionAuthority:
|
|
192
|
+
"""Keep selected paths in Controller memory until a bounded save command uses them."""
|
|
193
|
+
|
|
194
|
+
def __init__(
|
|
195
|
+
self,
|
|
196
|
+
managed_root: Path,
|
|
197
|
+
*,
|
|
198
|
+
now: Callable[[], float] = time.monotonic,
|
|
199
|
+
) -> None:
|
|
200
|
+
self._managed_root = managed_root
|
|
201
|
+
self._now = now
|
|
202
|
+
self._selections: dict[str, DirectorySelection] = {}
|
|
203
|
+
self._lock = threading.Lock()
|
|
204
|
+
|
|
205
|
+
def register(self, path: Path, role: DirectoryRole) -> DirectorySelection:
|
|
206
|
+
reference = inspect_directory(path, role, self._managed_root)
|
|
207
|
+
token = secrets.token_urlsafe(32)
|
|
208
|
+
selection = DirectorySelection(
|
|
209
|
+
token=token,
|
|
210
|
+
reference=reference,
|
|
211
|
+
expires_at=self._now() + _SELECTION_LIFETIME_SECONDS,
|
|
212
|
+
)
|
|
213
|
+
with self._lock:
|
|
214
|
+
self._prune()
|
|
215
|
+
self._selections[token] = selection
|
|
216
|
+
return selection
|
|
217
|
+
|
|
218
|
+
def resolve(self, token: object, role: DirectoryRole) -> DirectoryReference:
|
|
219
|
+
if not isinstance(token, str) or len(token) < 32 or len(token) > 256:
|
|
220
|
+
raise ProjectError("selection_invalid", "資料夾選擇已失效,請重新選擇。", status_code=422)
|
|
221
|
+
with self._lock:
|
|
222
|
+
self._prune()
|
|
223
|
+
selection = self._selections.get(token)
|
|
224
|
+
if selection is None or selection.reference.role is not role:
|
|
225
|
+
raise ProjectError("selection_invalid", "資料夾選擇已失效,請重新選擇。", status_code=422)
|
|
226
|
+
return inspect_directory(Path(selection.reference.canonical_path), role, self._managed_root)
|
|
227
|
+
|
|
228
|
+
def _prune(self) -> None:
|
|
229
|
+
current = self._now()
|
|
230
|
+
expired = [token for token, selection in self._selections.items() if selection.expires_at < current]
|
|
231
|
+
for token in expired:
|
|
232
|
+
self._selections.pop(token, None)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""SQLite schema for durable rough-cut preview renders."""
|
|
2
|
+
|
|
3
|
+
PREVIEW_SCHEMA = """
|
|
4
|
+
CREATE TABLE preview_renders (
|
|
5
|
+
render_id TEXT PRIMARY KEY,
|
|
6
|
+
project_id TEXT NOT NULL REFERENCES project(project_id),
|
|
7
|
+
timeline_id TEXT NOT NULL,
|
|
8
|
+
request_id TEXT NOT NULL UNIQUE,
|
|
9
|
+
state TEXT NOT NULL CHECK (
|
|
10
|
+
state IN (
|
|
11
|
+
'queued', 'running', 'cancelling', 'succeeded',
|
|
12
|
+
'failed', 'cancelled', 'interrupted'
|
|
13
|
+
)
|
|
14
|
+
),
|
|
15
|
+
progress REAL NOT NULL DEFAULT 0 CHECK (progress >= 0 AND progress <= 1),
|
|
16
|
+
output_relative_path TEXT,
|
|
17
|
+
output_size_bytes INTEGER,
|
|
18
|
+
error_code TEXT,
|
|
19
|
+
created_at TEXT NOT NULL,
|
|
20
|
+
started_at TEXT,
|
|
21
|
+
completed_at TEXT
|
|
22
|
+
);
|
|
23
|
+
CREATE TABLE current_preview_render (
|
|
24
|
+
slot INTEGER PRIMARY KEY CHECK (slot = 1),
|
|
25
|
+
render_id TEXT NOT NULL REFERENCES preview_renders(render_id)
|
|
26
|
+
);
|
|
27
|
+
CREATE INDEX preview_renders_project_created
|
|
28
|
+
ON preview_renders(project_id, created_at DESC);
|
|
29
|
+
"""
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"""Project use cases coordinating storage with explicitly selected directories."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import threading
|
|
6
|
+
import time
|
|
7
|
+
from dataclasses import replace
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from .models import (
|
|
11
|
+
DirectoryReference,
|
|
12
|
+
DirectoryRole,
|
|
13
|
+
ProjectError,
|
|
14
|
+
ProjectSnapshot,
|
|
15
|
+
validate_request_id,
|
|
16
|
+
validate_revision,
|
|
17
|
+
)
|
|
18
|
+
from .picker import (
|
|
19
|
+
MacOSDirectoryPicker,
|
|
20
|
+
NativeDirectoryPicker,
|
|
21
|
+
SelectionAuthority,
|
|
22
|
+
refresh_availability,
|
|
23
|
+
)
|
|
24
|
+
from .storage import ProjectRepository, command_digest
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ProjectWorkspace:
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
projects_root: Path,
|
|
31
|
+
*,
|
|
32
|
+
picker: NativeDirectoryPicker | None = None,
|
|
33
|
+
selections: SelectionAuthority | None = None,
|
|
34
|
+
) -> None:
|
|
35
|
+
self.repository = ProjectRepository(projects_root)
|
|
36
|
+
self.picker = picker or MacOSDirectoryPicker()
|
|
37
|
+
self.selections = selections or SelectionAuthority(self.repository.root)
|
|
38
|
+
self._picker_lock = threading.Lock()
|
|
39
|
+
self._picker_receipts: dict[str, tuple[DirectoryRole, object]] = {}
|
|
40
|
+
|
|
41
|
+
def list_projects(self) -> tuple[ProjectSnapshot, ...]:
|
|
42
|
+
return tuple(self._refresh(project) for project in self.repository.list_projects())
|
|
43
|
+
|
|
44
|
+
def get_project(self, project_id: str) -> ProjectSnapshot:
|
|
45
|
+
return self._refresh(self.repository.get_project(project_id))
|
|
46
|
+
|
|
47
|
+
def create_project(self, request_id: object, name: object) -> ProjectSnapshot:
|
|
48
|
+
return self._refresh(self.repository.create_project(request_id, name))
|
|
49
|
+
|
|
50
|
+
def update_details(
|
|
51
|
+
self,
|
|
52
|
+
project_id: str,
|
|
53
|
+
request_id: object,
|
|
54
|
+
expected_revision: object,
|
|
55
|
+
name: object,
|
|
56
|
+
brief: object,
|
|
57
|
+
) -> ProjectSnapshot:
|
|
58
|
+
return self._refresh(self.repository.update_details(project_id, request_id, expected_revision, name, brief))
|
|
59
|
+
|
|
60
|
+
def select_directory(self, request_id: object, role: DirectoryRole) -> dict[str, object] | None:
|
|
61
|
+
normalized_request = validate_request_id(request_id)
|
|
62
|
+
with self._picker_lock:
|
|
63
|
+
cached = self._picker_receipts.get(normalized_request)
|
|
64
|
+
if cached is not None:
|
|
65
|
+
cached_role, cached_selection = cached
|
|
66
|
+
if cached_role is not role:
|
|
67
|
+
raise ProjectError(
|
|
68
|
+
"request_reused",
|
|
69
|
+
"這次操作的識別資訊已用於不同內容。",
|
|
70
|
+
status_code=409,
|
|
71
|
+
)
|
|
72
|
+
if cached_selection.expires_at >= time.monotonic():
|
|
73
|
+
return cached_selection.to_public()
|
|
74
|
+
self._picker_receipts.pop(normalized_request, None)
|
|
75
|
+
selected = self.picker.choose(role)
|
|
76
|
+
if selected is None:
|
|
77
|
+
return None
|
|
78
|
+
selection = self.selections.register(selected, role)
|
|
79
|
+
self._picker_receipts[normalized_request] = (role, selection)
|
|
80
|
+
return selection.to_public()
|
|
81
|
+
|
|
82
|
+
def set_directory(
|
|
83
|
+
self,
|
|
84
|
+
project_id: str,
|
|
85
|
+
request_id: object,
|
|
86
|
+
expected_revision: object,
|
|
87
|
+
role: DirectoryRole,
|
|
88
|
+
selection_token: object,
|
|
89
|
+
) -> ProjectSnapshot:
|
|
90
|
+
normalized_request = validate_request_id(request_id)
|
|
91
|
+
revision = validate_revision(expected_revision)
|
|
92
|
+
command_hash = command_digest(
|
|
93
|
+
{
|
|
94
|
+
"expected_revision": revision,
|
|
95
|
+
"role": role.value,
|
|
96
|
+
"selection_token": selection_token,
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
replay = self.repository.replay_receipt(project_id, normalized_request, "set_directory", command_hash)
|
|
100
|
+
if replay is not None:
|
|
101
|
+
return self._refresh(replay)
|
|
102
|
+
reference = self.selections.resolve(selection_token, role)
|
|
103
|
+
current = self.repository.get_project(project_id)
|
|
104
|
+
other = next((item for item in current.directories if item.role is not role), None)
|
|
105
|
+
if other is not None and self._overlaps(reference, other):
|
|
106
|
+
raise ProjectError(
|
|
107
|
+
"directory_overlap",
|
|
108
|
+
"毛片與參考作品請選擇不同且不重疊的資料夾。",
|
|
109
|
+
status_code=422,
|
|
110
|
+
)
|
|
111
|
+
return self._refresh(
|
|
112
|
+
self.repository.set_directory(
|
|
113
|
+
project_id,
|
|
114
|
+
normalized_request,
|
|
115
|
+
command_hash,
|
|
116
|
+
revision,
|
|
117
|
+
reference,
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
def remove_directory(
|
|
122
|
+
self,
|
|
123
|
+
project_id: str,
|
|
124
|
+
request_id: object,
|
|
125
|
+
expected_revision: object,
|
|
126
|
+
role: DirectoryRole,
|
|
127
|
+
) -> ProjectSnapshot:
|
|
128
|
+
return self._refresh(self.repository.remove_directory(project_id, request_id, expected_revision, role))
|
|
129
|
+
|
|
130
|
+
def delete_project(
|
|
131
|
+
self,
|
|
132
|
+
project_id: str,
|
|
133
|
+
request_id: object,
|
|
134
|
+
expected_revision: object,
|
|
135
|
+
confirmation_name: object,
|
|
136
|
+
) -> str:
|
|
137
|
+
return self.repository.delete_project(
|
|
138
|
+
project_id,
|
|
139
|
+
request_id,
|
|
140
|
+
expected_revision,
|
|
141
|
+
confirmation_name,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
@staticmethod
|
|
145
|
+
def _overlaps(first: DirectoryReference, second: DirectoryReference) -> bool:
|
|
146
|
+
first_path = Path(first.canonical_path)
|
|
147
|
+
second_path = Path(second.canonical_path)
|
|
148
|
+
return (
|
|
149
|
+
first_path == second_path
|
|
150
|
+
or first_path.is_relative_to(second_path)
|
|
151
|
+
or second_path.is_relative_to(first_path)
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
@staticmethod
|
|
155
|
+
def _refresh(project: ProjectSnapshot) -> ProjectSnapshot:
|
|
156
|
+
return replace(
|
|
157
|
+
project,
|
|
158
|
+
directories=tuple(refresh_availability(reference) for reference in project.directories),
|
|
159
|
+
)
|