@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,64 @@
|
|
|
1
|
+
"""Lazy lifecycle binding between managed ASR setup and media preparation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import threading
|
|
6
|
+
|
|
7
|
+
from .artifacts import ArtifactStore
|
|
8
|
+
from .service import PreparationCoordinator
|
|
9
|
+
from .setup import ASRSetupManager, ASRSetupStatus
|
|
10
|
+
from .storage import PreparationRepository
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ManagedAnalysisRuntime:
|
|
14
|
+
"""Creates the heavy coordinator only after the verified runtime is ready."""
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
repository: PreparationRepository,
|
|
19
|
+
artifacts: ArtifactStore,
|
|
20
|
+
setup: ASRSetupManager,
|
|
21
|
+
) -> None:
|
|
22
|
+
self.repository = repository
|
|
23
|
+
self._artifacts = artifacts
|
|
24
|
+
self._setup = setup
|
|
25
|
+
self._lock = threading.Lock()
|
|
26
|
+
self._coordinator: PreparationCoordinator | None = None
|
|
27
|
+
|
|
28
|
+
def public_status(self) -> dict[str, object]:
|
|
29
|
+
return self._setup.public_status()
|
|
30
|
+
|
|
31
|
+
def coordinator(self) -> PreparationCoordinator | None:
|
|
32
|
+
with self._lock:
|
|
33
|
+
if self._coordinator is not None:
|
|
34
|
+
return self._coordinator
|
|
35
|
+
binding = self._setup.runtime_binding()
|
|
36
|
+
if binding is None:
|
|
37
|
+
return None
|
|
38
|
+
factory, model = binding
|
|
39
|
+
self._coordinator = PreparationCoordinator(
|
|
40
|
+
self.repository,
|
|
41
|
+
self._artifacts,
|
|
42
|
+
transcriber_factory=factory,
|
|
43
|
+
model=model,
|
|
44
|
+
)
|
|
45
|
+
return self._coordinator
|
|
46
|
+
|
|
47
|
+
def remove(self) -> ASRSetupStatus:
|
|
48
|
+
with self._lock:
|
|
49
|
+
coordinator = self._coordinator
|
|
50
|
+
self._coordinator = None
|
|
51
|
+
return self._setup.remove(
|
|
52
|
+
None if coordinator is None else lambda: coordinator.close(timeout=15)
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def close(self) -> None:
|
|
56
|
+
self._setup.close()
|
|
57
|
+
with self._lock:
|
|
58
|
+
coordinator = self._coordinator
|
|
59
|
+
self._coordinator = None
|
|
60
|
+
if coordinator is not None:
|
|
61
|
+
coordinator.close(timeout=15)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
__all__ = ["ManagedAnalysisRuntime"]
|
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
"""Single-worker orchestration for local, recoverable media preparation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import json
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import queue
|
|
9
|
+
import threading
|
|
10
|
+
from typing import Callable, Iterable, Mapping, Protocol
|
|
11
|
+
|
|
12
|
+
from clipen.projects.models import ProjectError
|
|
13
|
+
|
|
14
|
+
from .artifacts import ArtifactStore
|
|
15
|
+
from .audio import (
|
|
16
|
+
AudioPreparationError,
|
|
17
|
+
FFmpegAudioAdapter,
|
|
18
|
+
PCMChunker,
|
|
19
|
+
PreparedAudio,
|
|
20
|
+
inspect_normalized_audio,
|
|
21
|
+
project_samples_to_source,
|
|
22
|
+
)
|
|
23
|
+
from .models import (
|
|
24
|
+
ArtifactKind,
|
|
25
|
+
PreparationChunkStatus,
|
|
26
|
+
PreparationItemStatus,
|
|
27
|
+
PreparationRun,
|
|
28
|
+
PreparationState,
|
|
29
|
+
TimelineSegment,
|
|
30
|
+
)
|
|
31
|
+
from .storage import PreparationRepository, PreparationScope, PreparationWorkItem
|
|
32
|
+
from .transcription import TranscriptionError, TranscriptionResult
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class _AudioAdapter(Protocol):
|
|
36
|
+
def version(self) -> str: ...
|
|
37
|
+
|
|
38
|
+
def extract(
|
|
39
|
+
self, source, output: Path, cancel: threading.Event | None = None
|
|
40
|
+
) -> PreparedAudio: ...
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class _Transcriber(Protocol):
|
|
44
|
+
def probe(self) -> str: ...
|
|
45
|
+
|
|
46
|
+
def transcribe(
|
|
47
|
+
self,
|
|
48
|
+
audio: PreparedAudio,
|
|
49
|
+
output: Path,
|
|
50
|
+
*,
|
|
51
|
+
language: str = "zh",
|
|
52
|
+
initial_prompt: str | None = None,
|
|
53
|
+
cancel: threading.Event | None = None,
|
|
54
|
+
) -> TranscriptionResult: ...
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(frozen=True)
|
|
58
|
+
class PreparationRecipe:
|
|
59
|
+
audio_recipe_id: str = "audio.pcm16-16khz-mono.v1"
|
|
60
|
+
transcript_recipe_id: str = "asr.qwen3-1.7b.adaptive-camera-audio.v2"
|
|
61
|
+
chunk_seconds: int = 5 * 60
|
|
62
|
+
language: str = "zh"
|
|
63
|
+
vocabulary: tuple[str, ...] = ()
|
|
64
|
+
|
|
65
|
+
def __post_init__(self) -> None:
|
|
66
|
+
PCMChunker(chunk_seconds=self.chunk_seconds, overlap_seconds=0)
|
|
67
|
+
if not self.audio_recipe_id or not self.transcript_recipe_id:
|
|
68
|
+
raise ValueError("preparation recipe IDs are required")
|
|
69
|
+
if self.language not in {"zh", "ja", "en"}:
|
|
70
|
+
raise ValueError("preparation language is unsupported")
|
|
71
|
+
if (
|
|
72
|
+
not isinstance(self.vocabulary, tuple)
|
|
73
|
+
or len(self.vocabulary) > 40
|
|
74
|
+
or any(
|
|
75
|
+
not isinstance(term, str)
|
|
76
|
+
or not term.strip()
|
|
77
|
+
or term != term.strip()
|
|
78
|
+
or len(term) > 40
|
|
79
|
+
for term in self.vocabulary
|
|
80
|
+
)
|
|
81
|
+
or len(set(self.vocabulary)) != len(self.vocabulary)
|
|
82
|
+
or len("、".join(self.vocabulary).encode("utf-8")) > 2_000
|
|
83
|
+
):
|
|
84
|
+
raise ValueError("preparation vocabulary is invalid")
|
|
85
|
+
|
|
86
|
+
def to_storage(self) -> dict[str, object]:
|
|
87
|
+
return {
|
|
88
|
+
"audio_recipe_id": self.audio_recipe_id,
|
|
89
|
+
"transcript_recipe_id": self.transcript_recipe_id,
|
|
90
|
+
"chunk_seconds": self.chunk_seconds,
|
|
91
|
+
"overlap_seconds": 0,
|
|
92
|
+
"language": self.language,
|
|
93
|
+
"vocabulary": list(self.vocabulary),
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def initial_prompt(self) -> str | None:
|
|
98
|
+
# Dedicated glossary fields are intentionally absent from the UX.
|
|
99
|
+
# Traditional Chinese is a deterministic transform after recognition.
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
@classmethod
|
|
103
|
+
def from_storage(cls, value: Mapping[str, object]) -> "PreparationRecipe":
|
|
104
|
+
vocabulary = value.get("vocabulary", [])
|
|
105
|
+
if not isinstance(vocabulary, list) or any(
|
|
106
|
+
not isinstance(term, str) for term in vocabulary
|
|
107
|
+
):
|
|
108
|
+
raise ValueError("stored preparation vocabulary is invalid")
|
|
109
|
+
return cls(
|
|
110
|
+
audio_recipe_id=str(value.get("audio_recipe_id", "")),
|
|
111
|
+
transcript_recipe_id=str(value.get("transcript_recipe_id", "")),
|
|
112
|
+
chunk_seconds=value.get("chunk_seconds", 0), # type: ignore[arg-type]
|
|
113
|
+
language=str(value.get("language", "")),
|
|
114
|
+
vocabulary=tuple(vocabulary),
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass(frozen=True)
|
|
119
|
+
class _Job:
|
|
120
|
+
project_id: str
|
|
121
|
+
run_id: str
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class PreparationCoordinator:
|
|
125
|
+
"""Executes one heavy preparation at a time and checkpoints every boundary."""
|
|
126
|
+
|
|
127
|
+
def __init__(
|
|
128
|
+
self,
|
|
129
|
+
repository: PreparationRepository,
|
|
130
|
+
artifacts: ArtifactStore,
|
|
131
|
+
*,
|
|
132
|
+
transcriber_factory: Callable[[], _Transcriber],
|
|
133
|
+
model: Mapping[str, object],
|
|
134
|
+
audio_factory: Callable[[], _AudioAdapter] = FFmpegAudioAdapter,
|
|
135
|
+
recipe: PreparationRecipe = PreparationRecipe(),
|
|
136
|
+
) -> None:
|
|
137
|
+
self.repository = repository
|
|
138
|
+
self.artifacts = artifacts
|
|
139
|
+
self._transcriber_factory = transcriber_factory
|
|
140
|
+
self._model = dict(model)
|
|
141
|
+
self._audio_factory = audio_factory
|
|
142
|
+
self.recipe = recipe
|
|
143
|
+
self._queue: queue.Queue[_Job | None] = queue.Queue()
|
|
144
|
+
self._lock = threading.Lock()
|
|
145
|
+
self._cancellations: dict[str, threading.Event] = {}
|
|
146
|
+
self._projects: set[str] = set()
|
|
147
|
+
self._worker: threading.Thread | None = None
|
|
148
|
+
self._shutdown = threading.Event()
|
|
149
|
+
|
|
150
|
+
def preflight(self, project_id: str) -> PreparationScope:
|
|
151
|
+
return self.repository.preflight(project_id)
|
|
152
|
+
|
|
153
|
+
def recover_incomplete(self, project_ids: Iterable[str]) -> int:
|
|
154
|
+
recovered = 0
|
|
155
|
+
for project_id in project_ids:
|
|
156
|
+
recovered += self.repository.interrupt_incomplete(project_id)
|
|
157
|
+
return recovered
|
|
158
|
+
|
|
159
|
+
def start(
|
|
160
|
+
self,
|
|
161
|
+
project_id: str,
|
|
162
|
+
request_id: object,
|
|
163
|
+
expected_revision: object,
|
|
164
|
+
scope_id: object,
|
|
165
|
+
language: object = "zh",
|
|
166
|
+
vocabulary: object = (),
|
|
167
|
+
) -> PreparationRun:
|
|
168
|
+
try:
|
|
169
|
+
if not isinstance(language, str):
|
|
170
|
+
raise ValueError("language must be a string")
|
|
171
|
+
if not isinstance(vocabulary, (list, tuple)):
|
|
172
|
+
raise ValueError("vocabulary must be a list")
|
|
173
|
+
normalized_vocabulary = tuple(
|
|
174
|
+
term.strip()
|
|
175
|
+
for term in vocabulary
|
|
176
|
+
if isinstance(term, str) and term.strip()
|
|
177
|
+
)
|
|
178
|
+
if len(normalized_vocabulary) != len(vocabulary):
|
|
179
|
+
raise ValueError("vocabulary entries are invalid")
|
|
180
|
+
recipe = PreparationRecipe(
|
|
181
|
+
self.recipe.audio_recipe_id,
|
|
182
|
+
self.recipe.transcript_recipe_id,
|
|
183
|
+
self.recipe.chunk_seconds,
|
|
184
|
+
language,
|
|
185
|
+
normalized_vocabulary,
|
|
186
|
+
)
|
|
187
|
+
except ValueError as error:
|
|
188
|
+
raise ProjectError(
|
|
189
|
+
"analysis_settings_invalid",
|
|
190
|
+
"請確認主要語言與專有詞設定。",
|
|
191
|
+
status_code=422,
|
|
192
|
+
) from error
|
|
193
|
+
run, created, retired_artifact_paths = self.repository.create_run_with_status(
|
|
194
|
+
project_id,
|
|
195
|
+
request_id,
|
|
196
|
+
expected_revision,
|
|
197
|
+
scope_id,
|
|
198
|
+
recipe=recipe.to_storage(),
|
|
199
|
+
model=self._model,
|
|
200
|
+
)
|
|
201
|
+
if not created:
|
|
202
|
+
return run
|
|
203
|
+
try:
|
|
204
|
+
self.artifacts.prune_unreferenced(project_id, retired_artifact_paths)
|
|
205
|
+
except ProjectError:
|
|
206
|
+
# The new run is already durable. Do not strand it outside the worker
|
|
207
|
+
# queue merely because retired blob cleanup needs a later clean pass.
|
|
208
|
+
pass
|
|
209
|
+
with self._lock:
|
|
210
|
+
if self._shutdown.is_set():
|
|
211
|
+
return self.repository.transition(
|
|
212
|
+
project_id,
|
|
213
|
+
run.run_id,
|
|
214
|
+
PreparationState.INTERRUPTED,
|
|
215
|
+
phase="interrupted",
|
|
216
|
+
error_code="controller_stopped",
|
|
217
|
+
)
|
|
218
|
+
self._projects.add(project_id)
|
|
219
|
+
self._cancellations[run.run_id] = threading.Event()
|
|
220
|
+
self._ensure_worker_locked()
|
|
221
|
+
self._queue.put(_Job(project_id, run.run_id))
|
|
222
|
+
return run
|
|
223
|
+
|
|
224
|
+
def cancel(self, project_id: str, run_id: str) -> PreparationRun:
|
|
225
|
+
run = self.repository.request_cancel(project_id, run_id)
|
|
226
|
+
if run.state in {PreparationState.CANCELLING, PreparationState.CANCELLED}:
|
|
227
|
+
with self._lock:
|
|
228
|
+
event = self._cancellations.get(run.run_id)
|
|
229
|
+
if event is not None:
|
|
230
|
+
event.set()
|
|
231
|
+
return run
|
|
232
|
+
|
|
233
|
+
def resume(self, project_id: str, run_id: str, request_id: object) -> PreparationRun:
|
|
234
|
+
run, resumed = self.repository.resume_interrupted(project_id, run_id, request_id)
|
|
235
|
+
if not resumed:
|
|
236
|
+
return run
|
|
237
|
+
with self._lock:
|
|
238
|
+
if self._shutdown.is_set():
|
|
239
|
+
return self.repository.transition(
|
|
240
|
+
project_id,
|
|
241
|
+
run.run_id,
|
|
242
|
+
PreparationState.INTERRUPTED,
|
|
243
|
+
phase="interrupted",
|
|
244
|
+
error_code="controller_stopped",
|
|
245
|
+
)
|
|
246
|
+
self._projects.add(project_id)
|
|
247
|
+
self._cancellations[run.run_id] = threading.Event()
|
|
248
|
+
self._ensure_worker_locked()
|
|
249
|
+
self._queue.put(_Job(project_id, run.run_id))
|
|
250
|
+
return run
|
|
251
|
+
|
|
252
|
+
def close(self, timeout: float = 3) -> None:
|
|
253
|
+
with self._lock:
|
|
254
|
+
if self._shutdown.is_set():
|
|
255
|
+
return
|
|
256
|
+
self._shutdown.set()
|
|
257
|
+
for event in self._cancellations.values():
|
|
258
|
+
event.set()
|
|
259
|
+
worker = self._worker
|
|
260
|
+
self._queue.put(None)
|
|
261
|
+
if worker is not None:
|
|
262
|
+
worker.join(max(0, timeout))
|
|
263
|
+
for project_id in tuple(self._projects):
|
|
264
|
+
self.repository.interrupt_incomplete(project_id)
|
|
265
|
+
|
|
266
|
+
def _ensure_worker_locked(self) -> None:
|
|
267
|
+
if self._worker is None:
|
|
268
|
+
self._worker = threading.Thread(
|
|
269
|
+
target=self._work_loop,
|
|
270
|
+
name="clipen-media-preparation",
|
|
271
|
+
daemon=True,
|
|
272
|
+
)
|
|
273
|
+
self._worker.start()
|
|
274
|
+
|
|
275
|
+
def _work_loop(self) -> None:
|
|
276
|
+
while True:
|
|
277
|
+
job = self._queue.get()
|
|
278
|
+
try:
|
|
279
|
+
if job is None:
|
|
280
|
+
return
|
|
281
|
+
self._execute(job)
|
|
282
|
+
finally:
|
|
283
|
+
self._queue.task_done()
|
|
284
|
+
|
|
285
|
+
def _execute(self, job: _Job) -> None:
|
|
286
|
+
with self._lock:
|
|
287
|
+
cancel = self._cancellations.get(job.run_id)
|
|
288
|
+
if cancel is None:
|
|
289
|
+
return
|
|
290
|
+
transcriber = None
|
|
291
|
+
try:
|
|
292
|
+
queued = self.repository.get_run(job.project_id, job.run_id)
|
|
293
|
+
if queued.state is not PreparationState.QUEUED:
|
|
294
|
+
return
|
|
295
|
+
if self._shutdown.is_set():
|
|
296
|
+
self._interrupt(job, "controller_stopped")
|
|
297
|
+
return
|
|
298
|
+
recipe = PreparationRecipe.from_storage(
|
|
299
|
+
self.repository.recipe(job.project_id, job.run_id)
|
|
300
|
+
)
|
|
301
|
+
run = self.repository.transition(
|
|
302
|
+
job.project_id,
|
|
303
|
+
job.run_id,
|
|
304
|
+
PreparationState.RUNNING,
|
|
305
|
+
phase="extracting_audio",
|
|
306
|
+
)
|
|
307
|
+
audio_adapter = self._audio_factory()
|
|
308
|
+
transcriber = self._transcriber_factory()
|
|
309
|
+
audio_adapter.version()
|
|
310
|
+
transcriber.probe()
|
|
311
|
+
for item in self.repository.list_work_items(job.project_id, job.run_id):
|
|
312
|
+
if cancel.is_set():
|
|
313
|
+
self._finish_cancel_or_interrupt(job)
|
|
314
|
+
return
|
|
315
|
+
if item.status in {
|
|
316
|
+
PreparationItemStatus.NO_AUDIO,
|
|
317
|
+
PreparationItemStatus.FAILED,
|
|
318
|
+
PreparationItemStatus.TRANSCRIBED,
|
|
319
|
+
PreparationItemStatus.NO_SPEECH,
|
|
320
|
+
}:
|
|
321
|
+
continue
|
|
322
|
+
self._process_item(
|
|
323
|
+
job, run, item, audio_adapter, transcriber, cancel, recipe
|
|
324
|
+
)
|
|
325
|
+
if cancel.is_set():
|
|
326
|
+
self._finish_cancel_or_interrupt(job)
|
|
327
|
+
return
|
|
328
|
+
self.repository.publish(job.project_id, job.run_id)
|
|
329
|
+
except (AudioPreparationError, TranscriptionError) as error:
|
|
330
|
+
if error.code == "cancelled":
|
|
331
|
+
self._finish_cancel_or_interrupt(job)
|
|
332
|
+
else:
|
|
333
|
+
self._fail_run(job, error.code)
|
|
334
|
+
except ProjectError as error:
|
|
335
|
+
self._settle_conflict(job, error.code)
|
|
336
|
+
except Exception:
|
|
337
|
+
self._fail_run(job, "preparation_internal_error")
|
|
338
|
+
finally:
|
|
339
|
+
if transcriber is not None:
|
|
340
|
+
close = getattr(transcriber, "close", None)
|
|
341
|
+
if callable(close):
|
|
342
|
+
close()
|
|
343
|
+
with self._lock:
|
|
344
|
+
self._cancellations.pop(job.run_id, None)
|
|
345
|
+
|
|
346
|
+
def _process_item(
|
|
347
|
+
self,
|
|
348
|
+
job: _Job,
|
|
349
|
+
run: PreparationRun,
|
|
350
|
+
item: PreparationWorkItem,
|
|
351
|
+
audio_adapter: _AudioAdapter,
|
|
352
|
+
transcriber: _Transcriber,
|
|
353
|
+
cancel: threading.Event,
|
|
354
|
+
recipe: PreparationRecipe,
|
|
355
|
+
) -> None:
|
|
356
|
+
target = item.target
|
|
357
|
+
if target.audio is None:
|
|
358
|
+
return
|
|
359
|
+
try:
|
|
360
|
+
artifact_id = item.audio_artifact_id
|
|
361
|
+
if item.status is PreparationItemStatus.PENDING:
|
|
362
|
+
staging = self.artifacts.create_staging_file(job.project_id, suffix=".wav")
|
|
363
|
+
try:
|
|
364
|
+
audio_adapter.extract(target.audio, staging, cancel)
|
|
365
|
+
artifact = self.artifacts.publish(
|
|
366
|
+
job.project_id,
|
|
367
|
+
staging,
|
|
368
|
+
kind=ArtifactKind.ANALYSIS_AUDIO,
|
|
369
|
+
producer_operation_id=run.operation_id,
|
|
370
|
+
producer_attempt_id=run.attempt_id,
|
|
371
|
+
recipe_id=recipe.audio_recipe_id,
|
|
372
|
+
lineage={
|
|
373
|
+
"inventory_run_id": run.inventory_run_id,
|
|
374
|
+
"version_id": target.version_id,
|
|
375
|
+
"stream_index": target.stream_index,
|
|
376
|
+
},
|
|
377
|
+
source_span=target.audio.source_span,
|
|
378
|
+
)
|
|
379
|
+
finally:
|
|
380
|
+
staging.unlink(missing_ok=True)
|
|
381
|
+
artifact_id = artifact.artifact_id
|
|
382
|
+
self.repository.settle_item(
|
|
383
|
+
job.project_id,
|
|
384
|
+
job.run_id,
|
|
385
|
+
target.version_id,
|
|
386
|
+
target.stream_index,
|
|
387
|
+
PreparationItemStatus.PREPARED,
|
|
388
|
+
audio_artifact_id=artifact_id,
|
|
389
|
+
checkpoint={"audio_ready": True},
|
|
390
|
+
)
|
|
391
|
+
if artifact_id is None:
|
|
392
|
+
raise ProjectError(
|
|
393
|
+
"storage_corrupt", "Clipen 找不到已準備的音訊。", status_code=500
|
|
394
|
+
)
|
|
395
|
+
audio_record = self.artifacts.get(job.project_id, artifact_id)
|
|
396
|
+
prepared = inspect_normalized_audio(self.artifacts.path(audio_record))
|
|
397
|
+
chunker = PCMChunker(
|
|
398
|
+
chunk_seconds=recipe.chunk_seconds, overlap_seconds=0
|
|
399
|
+
)
|
|
400
|
+
chunk_paths: list[Path] = []
|
|
401
|
+
|
|
402
|
+
def make_chunk() -> Path:
|
|
403
|
+
path = self.artifacts.create_staging_file(job.project_id, suffix=".wav")
|
|
404
|
+
chunk_paths.append(path)
|
|
405
|
+
return path
|
|
406
|
+
|
|
407
|
+
chunks = chunker.split(prepared, target.audio.source_span, make_chunk)
|
|
408
|
+
try:
|
|
409
|
+
stored = self.repository.register_chunks(
|
|
410
|
+
job.project_id,
|
|
411
|
+
job.run_id,
|
|
412
|
+
target.version_id,
|
|
413
|
+
target.stream_index,
|
|
414
|
+
chunks,
|
|
415
|
+
)
|
|
416
|
+
completed = []
|
|
417
|
+
for chunk_file, checkpoint in zip(chunks, stored, strict=True):
|
|
418
|
+
if cancel.is_set():
|
|
419
|
+
raise TranscriptionError("cancelled")
|
|
420
|
+
if checkpoint.status is PreparationChunkStatus.PENDING:
|
|
421
|
+
chunk_audio = inspect_normalized_audio(chunk_file.path)
|
|
422
|
+
output = self.artifacts.create_staging_file(
|
|
423
|
+
job.project_id, suffix=".json"
|
|
424
|
+
)
|
|
425
|
+
try:
|
|
426
|
+
result = transcriber.transcribe(
|
|
427
|
+
chunk_audio,
|
|
428
|
+
output,
|
|
429
|
+
language=recipe.language,
|
|
430
|
+
initial_prompt=recipe.initial_prompt,
|
|
431
|
+
cancel=cancel,
|
|
432
|
+
)
|
|
433
|
+
transcript = self.artifacts.publish(
|
|
434
|
+
job.project_id,
|
|
435
|
+
output,
|
|
436
|
+
kind=ArtifactKind.TRANSCRIPT,
|
|
437
|
+
producer_operation_id=run.operation_id,
|
|
438
|
+
producer_attempt_id=run.attempt_id,
|
|
439
|
+
recipe_id=recipe.transcript_recipe_id,
|
|
440
|
+
lineage={
|
|
441
|
+
"audio_artifact_id": artifact_id,
|
|
442
|
+
"chunk_ordinal": checkpoint.ordinal,
|
|
443
|
+
},
|
|
444
|
+
source_span=checkpoint.source_span,
|
|
445
|
+
)
|
|
446
|
+
finally:
|
|
447
|
+
output.unlink(missing_ok=True)
|
|
448
|
+
checkpoint = self.repository.settle_chunk(
|
|
449
|
+
job.project_id,
|
|
450
|
+
job.run_id,
|
|
451
|
+
target.version_id,
|
|
452
|
+
target.stream_index,
|
|
453
|
+
checkpoint.ordinal,
|
|
454
|
+
transcript.artifact_id,
|
|
455
|
+
result,
|
|
456
|
+
)
|
|
457
|
+
completed.append(checkpoint)
|
|
458
|
+
self._publish_transcript(
|
|
459
|
+
job, run, item, artifact_id, tuple(completed), recipe
|
|
460
|
+
)
|
|
461
|
+
finally:
|
|
462
|
+
for path in chunk_paths:
|
|
463
|
+
path.unlink(missing_ok=True)
|
|
464
|
+
except (AudioPreparationError, TranscriptionError) as error:
|
|
465
|
+
if error.code == "cancelled":
|
|
466
|
+
raise
|
|
467
|
+
self._fail_item(job, target, error.code)
|
|
468
|
+
except ProjectError:
|
|
469
|
+
raise
|
|
470
|
+
except Exception:
|
|
471
|
+
self._fail_item(job, target, "preparation_item_internal_error")
|
|
472
|
+
|
|
473
|
+
def _publish_transcript(
|
|
474
|
+
self, job, run, item, audio_artifact_id, chunks, recipe: PreparationRecipe
|
|
475
|
+
) -> None:
|
|
476
|
+
target = item.target
|
|
477
|
+
timeline: list[TimelineSegment] = []
|
|
478
|
+
payload_segments: list[dict[str, object]] = []
|
|
479
|
+
texts: list[str] = []
|
|
480
|
+
chunk_artifacts: list[str] = []
|
|
481
|
+
for chunk in chunks:
|
|
482
|
+
if chunk.result is None or chunk.transcript_artifact_id is None:
|
|
483
|
+
raise ProjectError(
|
|
484
|
+
"storage_corrupt", "Clipen 找不到逐字稿切塊。", status_code=500
|
|
485
|
+
)
|
|
486
|
+
chunk_artifacts.append(chunk.transcript_artifact_id)
|
|
487
|
+
if chunk.result.text.strip():
|
|
488
|
+
texts.append(chunk.result.text.strip())
|
|
489
|
+
for segment in chunk.result.segments:
|
|
490
|
+
span = project_samples_to_source(
|
|
491
|
+
target.audio.source_span,
|
|
492
|
+
chunk.start_sample + segment.start_sample,
|
|
493
|
+
chunk.start_sample + segment.end_sample,
|
|
494
|
+
16_000,
|
|
495
|
+
)
|
|
496
|
+
timeline.append(
|
|
497
|
+
TimelineSegment(span, segment.text, segment.word_confidence)
|
|
498
|
+
)
|
|
499
|
+
words = []
|
|
500
|
+
for word in segment.words:
|
|
501
|
+
word_span = project_samples_to_source(
|
|
502
|
+
target.audio.source_span,
|
|
503
|
+
chunk.start_sample + word.start_sample,
|
|
504
|
+
chunk.start_sample + word.end_sample,
|
|
505
|
+
16_000,
|
|
506
|
+
)
|
|
507
|
+
words.append(
|
|
508
|
+
{
|
|
509
|
+
"source_span": word_span.to_storage(),
|
|
510
|
+
"text": word.text,
|
|
511
|
+
"probability": word.probability,
|
|
512
|
+
}
|
|
513
|
+
)
|
|
514
|
+
payload_segments.append(
|
|
515
|
+
{
|
|
516
|
+
"source_span": span.to_storage(),
|
|
517
|
+
"text": segment.text,
|
|
518
|
+
"no_speech_probability": segment.no_speech_probability,
|
|
519
|
+
"words": words,
|
|
520
|
+
}
|
|
521
|
+
)
|
|
522
|
+
output = self.artifacts.create_staging_file(job.project_id, suffix=".json")
|
|
523
|
+
try:
|
|
524
|
+
output.write_text(
|
|
525
|
+
json.dumps(
|
|
526
|
+
{
|
|
527
|
+
"schema_version": 1,
|
|
528
|
+
"language": recipe.language,
|
|
529
|
+
"text": "\n".join(texts),
|
|
530
|
+
"segments": payload_segments,
|
|
531
|
+
},
|
|
532
|
+
ensure_ascii=False,
|
|
533
|
+
sort_keys=True,
|
|
534
|
+
separators=(",", ":"),
|
|
535
|
+
),
|
|
536
|
+
encoding="utf-8",
|
|
537
|
+
)
|
|
538
|
+
artifact = self.artifacts.publish(
|
|
539
|
+
job.project_id,
|
|
540
|
+
output,
|
|
541
|
+
kind=ArtifactKind.TRANSCRIPT,
|
|
542
|
+
producer_operation_id=run.operation_id,
|
|
543
|
+
producer_attempt_id=run.attempt_id,
|
|
544
|
+
recipe_id=recipe.transcript_recipe_id,
|
|
545
|
+
lineage={
|
|
546
|
+
"audio_artifact_id": audio_artifact_id,
|
|
547
|
+
"chunk_artifact_ids": chunk_artifacts,
|
|
548
|
+
},
|
|
549
|
+
source_span=target.audio.source_span,
|
|
550
|
+
)
|
|
551
|
+
finally:
|
|
552
|
+
output.unlink(missing_ok=True)
|
|
553
|
+
self.repository.publish_transcript_revision(
|
|
554
|
+
job.project_id,
|
|
555
|
+
job.run_id,
|
|
556
|
+
target.version_id,
|
|
557
|
+
target.stream_index,
|
|
558
|
+
artifact_id=artifact.artifact_id,
|
|
559
|
+
language=recipe.language,
|
|
560
|
+
model=self._model,
|
|
561
|
+
recipe_id=recipe.transcript_recipe_id,
|
|
562
|
+
source_span=target.audio.source_span,
|
|
563
|
+
segments=tuple(timeline),
|
|
564
|
+
needs_review=any(
|
|
565
|
+
chunk.result is not None and chunk.result.needs_review
|
|
566
|
+
for chunk in chunks
|
|
567
|
+
),
|
|
568
|
+
)
|
|
569
|
+
|
|
570
|
+
def _fail_item(self, job, target, code: str) -> None:
|
|
571
|
+
current = self.repository.get_run(job.project_id, job.run_id)
|
|
572
|
+
if current.state is not PreparationState.RUNNING:
|
|
573
|
+
return
|
|
574
|
+
self.repository.settle_item(
|
|
575
|
+
job.project_id,
|
|
576
|
+
job.run_id,
|
|
577
|
+
target.version_id,
|
|
578
|
+
target.stream_index,
|
|
579
|
+
PreparationItemStatus.FAILED,
|
|
580
|
+
error_code=code,
|
|
581
|
+
)
|
|
582
|
+
|
|
583
|
+
def _finish_cancel_or_interrupt(self, job: _Job) -> None:
|
|
584
|
+
current = self.repository.get_run(job.project_id, job.run_id)
|
|
585
|
+
if current.state is PreparationState.CANCELLING:
|
|
586
|
+
self.repository.transition(
|
|
587
|
+
job.project_id,
|
|
588
|
+
job.run_id,
|
|
589
|
+
PreparationState.CANCELLED,
|
|
590
|
+
phase="cancelled",
|
|
591
|
+
)
|
|
592
|
+
elif current.state in {PreparationState.QUEUED, PreparationState.RUNNING}:
|
|
593
|
+
self._interrupt(job, "controller_stopped")
|
|
594
|
+
|
|
595
|
+
def _interrupt(self, job: _Job, code: str) -> None:
|
|
596
|
+
try:
|
|
597
|
+
self.repository.transition(
|
|
598
|
+
job.project_id,
|
|
599
|
+
job.run_id,
|
|
600
|
+
PreparationState.INTERRUPTED,
|
|
601
|
+
phase="interrupted",
|
|
602
|
+
error_code=code,
|
|
603
|
+
)
|
|
604
|
+
except ProjectError:
|
|
605
|
+
pass
|
|
606
|
+
|
|
607
|
+
def _fail_run(self, job: _Job, code: str) -> None:
|
|
608
|
+
try:
|
|
609
|
+
current = self.repository.get_run(job.project_id, job.run_id)
|
|
610
|
+
if current.state in {PreparationState.RUNNING, PreparationState.CANCELLING}:
|
|
611
|
+
self.repository.transition(
|
|
612
|
+
job.project_id,
|
|
613
|
+
job.run_id,
|
|
614
|
+
PreparationState.FAILED,
|
|
615
|
+
phase="failed",
|
|
616
|
+
error_code=code,
|
|
617
|
+
)
|
|
618
|
+
except ProjectError:
|
|
619
|
+
pass
|
|
620
|
+
|
|
621
|
+
def _settle_conflict(self, job: _Job, code: str) -> None:
|
|
622
|
+
current = self.repository.get_run(job.project_id, job.run_id)
|
|
623
|
+
if current.state is PreparationState.CANCELLING:
|
|
624
|
+
self._finish_cancel_or_interrupt(job)
|
|
625
|
+
elif current.state is PreparationState.RUNNING:
|
|
626
|
+
self._fail_run(job, code)
|