@clipen/clipen-cli 1.0.0 → 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/README.md +60 -50
- package/package.json +7 -4
- package/pyproject.toml +18 -2
- package/requirements/dev.txt +2 -0
- package/src/clipen/__init__.py +1 -1
- package/src/clipen/analysis/__init__.py +7 -7
- package/src/clipen/analysis/artifacts.py +4 -10
- package/src/clipen/analysis/audio.py +10 -8
- package/src/clipen/analysis/evidence_service.py +345 -167
- package/src/clipen/analysis/image_similarity.py +5 -14
- package/src/clipen/analysis/keyframes.py +5 -17
- package/src/clipen/analysis/models.py +5 -7
- package/src/clipen/analysis/qwen_worker.py +19 -54
- package/src/clipen/analysis/resources.py +2 -1
- package/src/clipen/analysis/runtime.py +1 -3
- package/src/clipen/analysis/service.py +16 -48
- package/src/clipen/analysis/setup.py +11 -25
- package/src/clipen/analysis/setup_cli.py +5 -15
- package/src/clipen/analysis/storage.py +96 -278
- package/src/clipen/analysis/transcription.py +40 -82
- package/src/clipen/analysis/visual.py +109 -48
- package/src/clipen/annotations/__init__.py +5 -2
- package/src/clipen/annotations/models.py +1 -1
- package/src/clipen/annotations/storage.py +132 -84
- package/src/clipen/annotations/suggestions.py +3 -8
- package/src/clipen/asr_release.py +4 -11
- package/src/clipen/bootstrap.py +65 -54
- package/src/clipen/{planning/models.py → candidates.py} +29 -47
- package/src/clipen/cli.py +2 -2
- package/src/clipen/controller/app.py +401 -214
- package/src/clipen/controller/server.py +28 -38
- package/src/clipen/domain/__init__.py +1 -1
- package/src/clipen/domain/media_time.py +3 -10
- package/src/clipen/domain/timeline.py +45 -47
- package/src/clipen/domain/validation.py +0 -1
- package/src/clipen/editing/__init__.py +1 -1
- package/src/clipen/editing/agent_models.py +4 -11
- package/src/clipen/editing/agent_repository.py +391 -170
- package/src/clipen/editing/agent_tools.py +180 -99
- package/src/clipen/editing/alignment.py +92 -52
- package/src/clipen/editing/alignment_setup.py +118 -70
- package/src/clipen/editing/alignment_worker.py +11 -9
- package/src/clipen/editing/change_report.py +95 -38
- package/src/clipen/editing/compiler.py +0 -1
- package/src/clipen/editing/component_looks.py +58 -34
- package/src/clipen/editing/component_review.py +34 -38
- package/src/clipen/editing/coordinator.py +213 -90
- package/src/clipen/editing/editorial_review.py +217 -95
- package/src/clipen/editing/evidence_jobs.py +457 -200
- package/src/clipen/editing/history_changes.py +129 -68
- package/src/clipen/editing/music_choices.py +8 -5
- package/src/clipen/editing/production_catalog.py +573 -136
- package/src/clipen/editing/production_limits.py +43 -17
- package/src/clipen/editing/production_media.py +376 -167
- package/src/clipen/editing/production_preview.py +322 -129
- package/src/clipen/editing/production_progress.py +164 -69
- package/src/clipen/editing/production_requirements.py +106 -39
- package/src/clipen/editing/production_tools.py +1064 -462
- package/src/clipen/editing/program_audio.py +211 -81
- package/src/clipen/editing/program_direction.py +59 -25
- package/src/clipen/editing/program_story.py +83 -45
- package/src/clipen/editing/program_subtitles.py +194 -96
- package/src/clipen/editing/programme_review.py +98 -50
- package/src/clipen/editing/prompt_history.py +64 -27
- package/src/clipen/editing/recut.py +65 -25
- package/src/clipen/editing/refinement_references.py +176 -86
- package/src/clipen/editing/resolve_export.py +449 -218
- package/src/clipen/editing/resolve_titles.py +162 -105
- package/src/clipen/editing/skill_tools.py +530 -154
- package/src/clipen/editing/source_dialogue.py +177 -67
- package/src/clipen/editing/speech_coverage.py +42 -22
- package/src/clipen/editing/speech_quality.py +6 -1
- package/src/clipen/editing/tool_receipts.py +78 -32
- package/src/clipen/editing/versions.py +122 -64
- package/src/clipen/editing/visual_preview.py +157 -61
- package/src/clipen/editing/workbench.py +104 -42
- package/src/clipen/finishing/audio.py +69 -30
- package/src/clipen/finishing/catalog.py +176 -93
- package/src/clipen/finishing/models.py +1 -1
- package/src/clipen/finishing/renderer.py +33 -14
- package/src/clipen/mcp_editing/__main__.py +3 -1
- package/src/clipen/mcp_editing/server.py +77 -34
- package/src/clipen/media/__init__.py +3 -3
- package/src/clipen/media/discovery.py +7 -20
- package/src/clipen/media/models.py +7 -18
- package/src/clipen/media/preview.py +14 -24
- package/src/clipen/media/probe.py +16 -44
- package/src/clipen/media/service.py +6 -19
- package/src/clipen/media/storage.py +33 -81
- package/src/clipen/music_vocabulary.py +33 -4
- package/src/clipen/power.py +8 -3
- package/src/clipen/projects/__init__.py +1 -1
- package/src/clipen/projects/deletion.py +19 -32
- package/src/clipen/projects/finishing_schema.py +3 -1
- package/src/clipen/projects/models.py +2 -5
- package/src/clipen/projects/picker.py +13 -37
- package/src/clipen/projects/service.py +8 -24
- package/src/clipen/projects/storage.py +226 -246
- package/src/clipen/provider_registry.py +4 -2
- package/src/clipen/providers/account.py +86 -35
- package/src/clipen/providers/claude.py +59 -25
- package/src/clipen/providers/claude_editing.py +14 -7
- package/src/clipen/providers/codex.py +73 -118
- package/src/clipen/providers/codex_editing.py +2 -2
- package/src/clipen/providers/editing_agent.py +19 -17
- package/src/clipen/providers/recovery.py +7 -8
- package/src/clipen/rendering/cover.py +18 -5
- package/src/clipen/rendering/custom_components.py +167 -79
- package/src/clipen/rendering/custom_render.py +62 -41
- package/src/clipen/rendering/production_effects.py +77 -45
- package/src/clipen/rendering/program_components.py +134 -73
- package/src/clipen/rendering/program_creator.py +432 -277
- package/src/clipen/rendering/program_identities.py +277 -182
- package/src/clipen/rendering/program_motion.py +140 -95
- package/src/clipen/rendering/program_studio.py +175 -116
- package/src/clipen/rendering/program_styles.py +231 -145
- package/src/clipen/rendering/program_subtitles.py +71 -34
- package/src/clipen/rendering/renderer.py +150 -121
- package/src/clipen/rendering/storage.py +9 -22
- package/src/clipen/rendering/titles.py +193 -86
- package/src/clipen/skills/__init__.py +1 -0
- package/src/clipen/skills/catalog.py +151 -55
- package/src/clipen/skills/library.py +13 -7
- package/src/clipen/skills/package.py +46 -23
- package/src/clipen/skills/visual_styles.py +29 -18
- package/src/clipen/workspace.py +15 -44
- package/src/clipen/planning/__init__.py +0 -9
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from dataclasses import dataclass
|
|
6
|
-
from enum import Enum
|
|
7
5
|
import asyncio
|
|
8
6
|
import hashlib
|
|
9
|
-
from http.cookies import SimpleCookie
|
|
10
7
|
import json
|
|
11
|
-
from pathlib import Path
|
|
12
8
|
import secrets
|
|
13
9
|
import threading
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from enum import Enum
|
|
12
|
+
from http.cookies import SimpleCookie
|
|
13
|
+
from pathlib import Path
|
|
14
14
|
|
|
15
15
|
from starlette.applications import Starlette
|
|
16
16
|
from starlette.concurrency import run_in_threadpool
|
|
@@ -38,7 +38,6 @@ from clipen.projects import (
|
|
|
38
38
|
)
|
|
39
39
|
from clipen.projects.models import validate_request_id
|
|
40
40
|
|
|
41
|
-
|
|
42
41
|
_SESSION_COOKIE = "clipen_session"
|
|
43
42
|
_CSRF_HEADER = "x-clipen-csrf"
|
|
44
43
|
_INSTANCE_HEADER = "x-clipen-instance"
|
|
@@ -64,6 +63,7 @@ class ProviderStatus:
|
|
|
64
63
|
|
|
65
64
|
def __post_init__(self) -> None:
|
|
66
65
|
from clipen.provider_registry import get_provider
|
|
66
|
+
|
|
67
67
|
get_provider(self.provider_id)
|
|
68
68
|
if not self.name.strip() or len(self.name) > 80:
|
|
69
69
|
raise ValueError("provider name must be non-empty and bounded")
|
|
@@ -208,9 +208,7 @@ class LocalRequestGuard:
|
|
|
208
208
|
"/api/session/connect",
|
|
209
209
|
"/api/instance/reopen",
|
|
210
210
|
}:
|
|
211
|
-
credentials = self.authority.authenticate(
|
|
212
|
-
_cookie_value(headers, _SESSION_COOKIE)
|
|
213
|
-
)
|
|
211
|
+
credentials = self.authority.authenticate(_cookie_value(headers, _SESSION_COOKIE))
|
|
214
212
|
if credentials is None:
|
|
215
213
|
await self._reject(scope, receive, send, 401, "session_required")
|
|
216
214
|
return
|
|
@@ -220,9 +218,7 @@ class LocalRequestGuard:
|
|
|
220
218
|
return
|
|
221
219
|
if path != "/api/session/connect":
|
|
222
220
|
csrf = headers.get(_CSRF_HEADER)
|
|
223
|
-
if credentials is None or csrf is None or not secrets.compare_digest(
|
|
224
|
-
csrf, credentials.csrf_token
|
|
225
|
-
):
|
|
221
|
+
if credentials is None or csrf is None or not secrets.compare_digest(csrf, credentials.csrf_token):
|
|
226
222
|
await self._reject(scope, receive, send, 403, "csrf_failed")
|
|
227
223
|
return
|
|
228
224
|
|
|
@@ -243,9 +239,7 @@ class LocalRequestGuard:
|
|
|
243
239
|
headers["referrer-policy"] = "no-referrer"
|
|
244
240
|
headers["cross-origin-opener-policy"] = "same-origin"
|
|
245
241
|
headers["cross-origin-resource-policy"] = "same-origin"
|
|
246
|
-
headers["permissions-policy"] = (
|
|
247
|
-
"camera=(), microphone=(), geolocation=(), payment=(), usb=()"
|
|
248
|
-
)
|
|
242
|
+
headers["permissions-policy"] = "camera=(), microphone=(), geolocation=(), payment=(), usb=()"
|
|
249
243
|
path = str(scope.get("path", ""))
|
|
250
244
|
if path.startswith("/api/") or path in {"", "/", "/index.html"}:
|
|
251
245
|
headers["cache-control"] = "no-store"
|
|
@@ -335,27 +329,32 @@ def create_app(
|
|
|
335
329
|
async def account(request: Request) -> Response:
|
|
336
330
|
# Which model runs and how much quota is left; read-only, never calls a model.
|
|
337
331
|
if authority.authenticate(_cookie_value(request.headers, _SESSION_COOKIE)) is None:
|
|
338
|
-
return JSONResponse(
|
|
339
|
-
|
|
340
|
-
|
|
332
|
+
return JSONResponse(
|
|
333
|
+
{"error": {"code": "session_required", "message": "請從 Clipen Terminal 重新開啟工作台。"}},
|
|
334
|
+
status_code=401,
|
|
335
|
+
)
|
|
336
|
+
details = (
|
|
337
|
+
await run_in_threadpool(provider_account)
|
|
338
|
+
if provider_account is not None
|
|
339
|
+
else {"model": None, "quota": None}
|
|
340
|
+
)
|
|
341
341
|
return JSONResponse({"provider": {"id": provider.provider_id, "name": provider.name}, **details})
|
|
342
342
|
|
|
343
343
|
async def power(request: Request) -> Response:
|
|
344
344
|
# Lets the UI warn before production: on battery a closed lid still sleeps the Mac.
|
|
345
345
|
if authority.authenticate(_cookie_value(request.headers, _SESSION_COOKIE)) is None:
|
|
346
|
-
return JSONResponse(
|
|
347
|
-
|
|
346
|
+
return JSONResponse(
|
|
347
|
+
{"error": {"code": "session_required", "message": "請從 Clipen Terminal 重新開啟工作台。"}},
|
|
348
|
+
status_code=401,
|
|
349
|
+
)
|
|
348
350
|
from clipen.power import power_status
|
|
351
|
+
|
|
349
352
|
return JSONResponse(await run_in_threadpool(power_status))
|
|
350
353
|
|
|
351
354
|
async def reopen_instance(request: Request) -> Response:
|
|
352
355
|
supplied = request.headers.get(_INSTANCE_HEADER)
|
|
353
356
|
expected = config.instance_secret
|
|
354
|
-
if (
|
|
355
|
-
expected is None
|
|
356
|
-
or supplied is None
|
|
357
|
-
or not secrets.compare_digest(supplied, expected)
|
|
358
|
-
):
|
|
357
|
+
if expected is None or supplied is None or not secrets.compare_digest(supplied, expected):
|
|
359
358
|
return JSONResponse(
|
|
360
359
|
{"error": {"code": "instance_invalid", "message": "找不到 Clipen instance。"}},
|
|
361
360
|
status_code=404,
|
|
@@ -374,19 +373,13 @@ def create_app(
|
|
|
374
373
|
async for chunk in request.stream():
|
|
375
374
|
body.extend(chunk)
|
|
376
375
|
if len(body) > _MAX_JSON_BODY_BYTES:
|
|
377
|
-
raise ProjectError(
|
|
378
|
-
"body_too_large", "這次操作的資料過大。", status_code=413
|
|
379
|
-
)
|
|
376
|
+
raise ProjectError("body_too_large", "這次操作的資料過大。", status_code=413)
|
|
380
377
|
try:
|
|
381
378
|
payload = json.loads(body.decode("utf-8"))
|
|
382
379
|
except (UnicodeDecodeError, json.JSONDecodeError) as error:
|
|
383
|
-
raise ProjectError(
|
|
384
|
-
"json_invalid", "無法讀取這次操作的資料。", status_code=400
|
|
385
|
-
) from error
|
|
380
|
+
raise ProjectError("json_invalid", "無法讀取這次操作的資料。", status_code=400) from error
|
|
386
381
|
if not isinstance(payload, dict):
|
|
387
|
-
raise ProjectError(
|
|
388
|
-
"json_invalid", "操作資料必須是 JSON object。", status_code=400
|
|
389
|
-
)
|
|
382
|
+
raise ProjectError("json_invalid", "操作資料必須是 JSON object。", status_code=400)
|
|
390
383
|
return payload
|
|
391
384
|
|
|
392
385
|
def require_workspace() -> ProjectWorkspace:
|
|
@@ -424,11 +417,7 @@ def create_app(
|
|
|
424
417
|
return repository
|
|
425
418
|
|
|
426
419
|
def require_preparations() -> PreparationCoordinator:
|
|
427
|
-
coordinator = (
|
|
428
|
-
analysis_runtime.coordinator()
|
|
429
|
-
if analysis_runtime is not None
|
|
430
|
-
else preparations
|
|
431
|
-
)
|
|
420
|
+
coordinator = analysis_runtime.coordinator() if analysis_runtime is not None else preparations
|
|
432
421
|
if coordinator is None:
|
|
433
422
|
raise ProjectError(
|
|
434
423
|
"asr_setup_required",
|
|
@@ -473,22 +462,16 @@ def create_app(
|
|
|
473
462
|
)
|
|
474
463
|
return edit_agents
|
|
475
464
|
|
|
476
|
-
def require_fields(
|
|
477
|
-
payload: dict[str, object], required: set[str], optional: set[str] | None = None
|
|
478
|
-
) -> None:
|
|
465
|
+
def require_fields(payload: dict[str, object], required: set[str], optional: set[str] | None = None) -> None:
|
|
479
466
|
allowed = required | (optional or set())
|
|
480
467
|
if not required.issubset(payload) or not set(payload).issubset(allowed):
|
|
481
|
-
raise ProjectError(
|
|
482
|
-
"command_invalid", "這次操作缺少必要資料。", status_code=422
|
|
483
|
-
)
|
|
468
|
+
raise ProjectError("command_invalid", "這次操作缺少必要資料。", status_code=422)
|
|
484
469
|
|
|
485
470
|
def parse_role(value: object) -> DirectoryRole:
|
|
486
471
|
try:
|
|
487
472
|
return DirectoryRole(value)
|
|
488
473
|
except (TypeError, ValueError) as error:
|
|
489
|
-
raise ProjectError(
|
|
490
|
-
"role_invalid", "資料夾用途無效。", status_code=422
|
|
491
|
-
) from error
|
|
474
|
+
raise ProjectError("role_invalid", "資料夾用途無效。", status_code=422) from error
|
|
492
475
|
|
|
493
476
|
async def project_error_response(error: ProjectError) -> JSONResponse:
|
|
494
477
|
return JSONResponse(
|
|
@@ -516,17 +499,13 @@ def create_app(
|
|
|
516
499
|
async def list_projects(_: Request) -> Response:
|
|
517
500
|
try:
|
|
518
501
|
projects = await run_in_threadpool(require_workspace().list_projects)
|
|
519
|
-
return JSONResponse(
|
|
520
|
-
{"projects": [project.to_public() for project in projects]}
|
|
521
|
-
)
|
|
502
|
+
return JSONResponse({"projects": [project.to_public() for project in projects]})
|
|
522
503
|
except ProjectError as error:
|
|
523
504
|
return await project_error_response(error)
|
|
524
505
|
|
|
525
506
|
async def get_project(request: Request) -> Response:
|
|
526
507
|
try:
|
|
527
|
-
project = await run_in_threadpool(
|
|
528
|
-
require_workspace().get_project, request.path_params["project_id"]
|
|
529
|
-
)
|
|
508
|
+
project = await run_in_threadpool(require_workspace().get_project, request.path_params["project_id"])
|
|
530
509
|
return JSONResponse({"project": project.to_public()})
|
|
531
510
|
except ProjectError as error:
|
|
532
511
|
return await project_error_response(error)
|
|
@@ -547,9 +526,7 @@ def create_app(
|
|
|
547
526
|
async def update_details(request: Request) -> Response:
|
|
548
527
|
try:
|
|
549
528
|
payload = await read_json(request)
|
|
550
|
-
require_fields(
|
|
551
|
-
payload, {"request_id", "expected_revision", "name", "brief"}
|
|
552
|
-
)
|
|
529
|
+
require_fields(payload, {"request_id", "expected_revision", "name", "brief"})
|
|
553
530
|
project = await run_in_threadpool(
|
|
554
531
|
require_workspace().update_details,
|
|
555
532
|
request.path_params["project_id"],
|
|
@@ -643,9 +620,7 @@ def create_app(
|
|
|
643
620
|
try:
|
|
644
621
|
stream_index = int(request.path_params["stream_index"])
|
|
645
622
|
except (TypeError, ValueError) as error:
|
|
646
|
-
raise ProjectError(
|
|
647
|
-
"annotation_not_found", "找不到這個素材。", status_code=404
|
|
648
|
-
) from error
|
|
623
|
+
raise ProjectError("annotation_not_found", "找不到這個素材。", status_code=404) from error
|
|
649
624
|
result = await run_in_threadpool(
|
|
650
625
|
require_annotations().get,
|
|
651
626
|
request.path_params["project_id"],
|
|
@@ -662,9 +637,13 @@ def create_app(
|
|
|
662
637
|
require_fields(
|
|
663
638
|
payload,
|
|
664
639
|
{
|
|
665
|
-
"request_id",
|
|
666
|
-
"
|
|
667
|
-
"
|
|
640
|
+
"request_id",
|
|
641
|
+
"version_id",
|
|
642
|
+
"stream_index",
|
|
643
|
+
"expected_material_revision",
|
|
644
|
+
"expected_transcript_revision",
|
|
645
|
+
"material",
|
|
646
|
+
"transcript",
|
|
668
647
|
},
|
|
669
648
|
)
|
|
670
649
|
result = await run_in_threadpool(
|
|
@@ -726,6 +705,7 @@ def create_app(
|
|
|
726
705
|
# Finished films are rendered by AI production and played or downloaded from here.
|
|
727
706
|
try:
|
|
728
707
|
from clipen.rendering import PreviewRenderRepository
|
|
708
|
+
|
|
729
709
|
path = await run_in_threadpool(
|
|
730
710
|
PreviewRenderRepository(require_workspace().repository).media_path,
|
|
731
711
|
request.path_params["project_id"],
|
|
@@ -739,6 +719,7 @@ def create_app(
|
|
|
739
719
|
# A still cut from the project's own footage for its card on the home page.
|
|
740
720
|
try:
|
|
741
721
|
from clipen.rendering import ProjectCoverRepository
|
|
722
|
+
|
|
742
723
|
path = await run_in_threadpool(
|
|
743
724
|
ProjectCoverRepository(require_workspace().repository).cover,
|
|
744
725
|
request.path_params["project_id"],
|
|
@@ -759,210 +740,382 @@ def create_app(
|
|
|
759
740
|
|
|
760
741
|
async def edit_agent_export(request: Request) -> Response:
|
|
761
742
|
try:
|
|
762
|
-
path=await run_in_threadpool(
|
|
763
|
-
|
|
743
|
+
path = await run_in_threadpool(
|
|
744
|
+
require_edit_agents().export_path,
|
|
745
|
+
request.path_params["project_id"],
|
|
746
|
+
request.path_params["run_id"],
|
|
747
|
+
request.path_params["revision_id"],
|
|
748
|
+
)
|
|
749
|
+
return FileResponse(path, media_type="application/json", filename=path.name)
|
|
764
750
|
except ProjectError as error:
|
|
765
751
|
return await project_error_response(error)
|
|
766
752
|
|
|
767
753
|
async def production_skills(request: Request) -> Response:
|
|
768
754
|
from clipen.skills import list_skills
|
|
769
|
-
|
|
755
|
+
|
|
756
|
+
return JSONResponse({"skills": list_skills()})
|
|
770
757
|
|
|
771
758
|
async def production_style_preview(request: Request) -> Response:
|
|
772
759
|
import base64
|
|
760
|
+
|
|
773
761
|
from clipen.skills import get_skill
|
|
774
762
|
from clipen.skills.visual_styles import public_styles
|
|
763
|
+
|
|
775
764
|
try:
|
|
776
|
-
skill=get_skill(request.path_params[
|
|
777
|
-
identifier=request.path_params[
|
|
778
|
-
style=next(
|
|
765
|
+
skill = get_skill(request.path_params["skill_id"])
|
|
766
|
+
identifier = request.path_params["style_id"]
|
|
767
|
+
style = next(
|
|
768
|
+
(s for s in public_styles(skill) if s["visual_style_id"] == identifier and "preview_resource" in s),
|
|
769
|
+
None,
|
|
770
|
+
)
|
|
779
771
|
if style is None:
|
|
780
|
-
raise ProjectError(
|
|
781
|
-
asset=skill[
|
|
782
|
-
return Response(
|
|
772
|
+
raise ProjectError("visual_style_unavailable", "找不到這套畫面風格。", status_code=404)
|
|
773
|
+
asset = skill["assets"][style["preview_resource"]]
|
|
774
|
+
return Response(
|
|
775
|
+
base64.b64decode(asset["data"], validate=True),
|
|
776
|
+
media_type="image/png",
|
|
777
|
+
headers={"Cache-Control": "no-cache"},
|
|
778
|
+
)
|
|
783
779
|
except ProjectError as error:
|
|
784
780
|
return await project_error_response(error)
|
|
785
781
|
|
|
786
782
|
async def production_template_sample(request: Request) -> Response:
|
|
787
|
-
from clipen.skills import get_skill
|
|
788
|
-
from importlib.resources import files
|
|
789
783
|
import hashlib
|
|
784
|
+
from importlib.resources import files
|
|
785
|
+
|
|
786
|
+
from clipen.skills import get_skill
|
|
787
|
+
|
|
790
788
|
try:
|
|
791
|
-
skill=get_skill(request.path_params[
|
|
792
|
-
template=next(
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
if
|
|
796
|
-
raise ProjectError(
|
|
797
|
-
|
|
798
|
-
|
|
789
|
+
skill = get_skill(request.path_params["skill_id"])
|
|
790
|
+
template = next(
|
|
791
|
+
(t for t in skill["templates"] if t["template_id"] == request.path_params["template_id"]), None
|
|
792
|
+
)
|
|
793
|
+
if template is None:
|
|
794
|
+
raise ProjectError("template_unavailable", "找不到模板示例。", status_code=404)
|
|
795
|
+
path = files("clipen.skills").joinpath("builtin", template["sample_resource"])
|
|
796
|
+
if hashlib.sha256(path.read_bytes()).hexdigest() != skill["samples"][template["sample_resource"]]["sha256"]:
|
|
797
|
+
raise ProjectError("skill_asset_invalid", "模板示例未通過校驗。", status_code=409)
|
|
798
|
+
return FileResponse(str(path), media_type="video/mp4", headers={"Cache-Control": "no-cache"})
|
|
799
|
+
except ProjectError as error:
|
|
800
|
+
return await project_error_response(error)
|
|
799
801
|
|
|
800
802
|
async def start_production(request: Request) -> Response:
|
|
801
803
|
try:
|
|
802
804
|
payload = await read_json(request)
|
|
803
|
-
require_fields(
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
805
|
+
require_fields(
|
|
806
|
+
payload,
|
|
807
|
+
{"request_id", "consent", "skill_id", "intent"},
|
|
808
|
+
{
|
|
809
|
+
"max_tool_calls",
|
|
810
|
+
"max_preview_renders",
|
|
811
|
+
"visual_style_id",
|
|
812
|
+
"font_style_id",
|
|
813
|
+
"target_duration_seconds",
|
|
814
|
+
"music_asset_id",
|
|
815
|
+
"template_id",
|
|
816
|
+
},
|
|
817
|
+
)
|
|
818
|
+
run = await run_in_threadpool(
|
|
819
|
+
require_edit_agents().start,
|
|
820
|
+
request.path_params["project_id"],
|
|
821
|
+
payload["request_id"],
|
|
822
|
+
payload["consent"],
|
|
823
|
+
production={
|
|
824
|
+
"skill_id": payload["skill_id"],
|
|
825
|
+
"intent": payload["intent"],
|
|
826
|
+
**(
|
|
827
|
+
{
|
|
828
|
+
key: payload[key]
|
|
829
|
+
for key in (
|
|
830
|
+
"visual_style_id",
|
|
831
|
+
"font_style_id",
|
|
832
|
+
"target_duration_seconds",
|
|
833
|
+
"music_asset_id",
|
|
834
|
+
"template_id",
|
|
835
|
+
)
|
|
836
|
+
if key in payload
|
|
837
|
+
}
|
|
838
|
+
),
|
|
839
|
+
},
|
|
840
|
+
max_tool_calls=payload.get(
|
|
841
|
+
"max_tool_calls", 500 if payload.get("target_duration_seconds", 60) > 75 else 120
|
|
842
|
+
),
|
|
843
|
+
max_preview_renders=payload.get(
|
|
844
|
+
"max_preview_renders", 6 if payload.get("target_duration_seconds", 60) > 75 else 4
|
|
845
|
+
),
|
|
846
|
+
)
|
|
847
|
+
return JSONResponse({"run": run.to_public()}, status_code=202)
|
|
809
848
|
except (TypeError, ValueError) as error:
|
|
810
|
-
return await project_error_response(ProjectError(
|
|
849
|
+
return await project_error_response(ProjectError("command_invalid", str(error), status_code=422))
|
|
811
850
|
except ProjectError as error:
|
|
812
851
|
return await project_error_response(error)
|
|
813
852
|
|
|
814
853
|
async def skill_document(request: Request) -> Response:
|
|
815
854
|
"""Read-only text of a skill and the skills it extends, for review in the UI."""
|
|
816
855
|
from clipen.skills import get_skill
|
|
856
|
+
|
|
817
857
|
try:
|
|
818
|
-
skill = await run_in_threadpool(get_skill, request.path_params[
|
|
819
|
-
chain = [
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
858
|
+
skill = await run_in_threadpool(get_skill, request.path_params["skill_id"])
|
|
859
|
+
chain = [
|
|
860
|
+
{
|
|
861
|
+
**{k: entry[k] for k in ("skill_id", "version", "source")},
|
|
862
|
+
"content": skill["resources"][entry["resource"]],
|
|
863
|
+
"references": sorted(
|
|
864
|
+
k
|
|
865
|
+
for k in skill["resources"]
|
|
866
|
+
if k.startswith(entry["resource"].rsplit("/", 1)[0] + "/") and k != entry["resource"]
|
|
867
|
+
),
|
|
868
|
+
}
|
|
869
|
+
for entry in skill["skill_chain"]
|
|
870
|
+
]
|
|
871
|
+
return JSONResponse({"skill_id": skill["skill_id"], "name": skill["name"], "chain": chain})
|
|
825
872
|
except ProjectError as error:
|
|
826
873
|
return await project_error_response(error)
|
|
827
874
|
|
|
828
875
|
async def skill_library_status(_: Request) -> Response:
|
|
829
876
|
from clipen.skills import skill_library
|
|
877
|
+
|
|
830
878
|
try:
|
|
831
|
-
return JSONResponse({
|
|
879
|
+
return JSONResponse({"skills": await run_in_threadpool(skill_library().list)})
|
|
832
880
|
except ProjectError as error:
|
|
833
881
|
return await project_error_response(error)
|
|
834
882
|
|
|
835
883
|
async def import_skill(request: Request) -> Response:
|
|
836
884
|
"""Import a folder the user picks in the native dialog; the browser never sends a path."""
|
|
837
885
|
from clipen.skills import skill_library
|
|
886
|
+
|
|
838
887
|
try:
|
|
839
888
|
payload = await read_json(request)
|
|
840
|
-
require_fields(payload, {
|
|
841
|
-
validate_request_id(payload[
|
|
842
|
-
folder = await run_in_threadpool(require_workspace().picker.choose_folder,
|
|
889
|
+
require_fields(payload, {"request_id"})
|
|
890
|
+
validate_request_id(payload["request_id"])
|
|
891
|
+
folder = await run_in_threadpool(require_workspace().picker.choose_folder, "選擇要匯入的 Skill 資料夾")
|
|
843
892
|
if folder is None:
|
|
844
893
|
return Response(status_code=204)
|
|
845
894
|
package = await run_in_threadpool(skill_library().import_folder, folder)
|
|
846
|
-
return JSONResponse({
|
|
895
|
+
return JSONResponse({"skill": package.summary()}, status_code=201)
|
|
847
896
|
except ProjectError as error:
|
|
848
897
|
return await project_error_response(error)
|
|
849
898
|
|
|
850
899
|
async def remove_skill(request: Request) -> Response:
|
|
851
900
|
from clipen.skills import skill_library
|
|
901
|
+
|
|
852
902
|
try:
|
|
853
903
|
require_fields(await read_json(request), set())
|
|
854
|
-
await run_in_threadpool(skill_library().remove, request.path_params[
|
|
855
|
-
return JSONResponse({
|
|
904
|
+
await run_in_threadpool(skill_library().remove, request.path_params["skill_id"])
|
|
905
|
+
return JSONResponse({"removed": request.path_params["skill_id"]})
|
|
856
906
|
except ProjectError as error:
|
|
857
907
|
return await project_error_response(error)
|
|
858
908
|
|
|
859
909
|
async def production_prompt_history(request: Request) -> Response:
|
|
860
910
|
try:
|
|
861
911
|
from clipen.editing.prompt_history import history, save
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
912
|
+
|
|
913
|
+
repository = require_edit_agents().repository
|
|
914
|
+
if request.method == "GET":
|
|
915
|
+
offset = int(request.query_params.get("offset", "0"))
|
|
916
|
+
limit = int(request.query_params.get("limit", "30"))
|
|
917
|
+
if not 0 <= offset <= 100000 or not 1 <= limit <= 100:
|
|
918
|
+
raise ValueError("分頁參數無效。")
|
|
919
|
+
return JSONResponse(
|
|
920
|
+
await run_in_threadpool(history, repository, request.query_params.get("project_id"), offset, limit)
|
|
921
|
+
)
|
|
922
|
+
payload = await read_json(request)
|
|
923
|
+
require_fields(payload, {"request_id", "kind", "text"})
|
|
924
|
+
return JSONResponse(
|
|
925
|
+
await run_in_threadpool(
|
|
926
|
+
save,
|
|
927
|
+
repository,
|
|
928
|
+
request.path_params["project_id"],
|
|
929
|
+
payload["request_id"],
|
|
930
|
+
payload["kind"],
|
|
931
|
+
payload["text"],
|
|
932
|
+
),
|
|
933
|
+
status_code=201,
|
|
934
|
+
)
|
|
935
|
+
except ProjectError as error:
|
|
936
|
+
return await project_error_response(error)
|
|
937
|
+
except (TypeError, ValueError) as error:
|
|
938
|
+
return await project_error_response(ProjectError("prompt_invalid", str(error), status_code=422))
|
|
871
939
|
|
|
872
940
|
async def production_workbench(request: Request) -> Response:
|
|
873
941
|
try:
|
|
874
942
|
from clipen.editing.workbench import state, tools_for
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
943
|
+
|
|
944
|
+
coordinator = require_edit_agents()
|
|
945
|
+
project = request.path_params["project_id"]
|
|
946
|
+
run = request.path_params["run_id"]
|
|
947
|
+
if request.method == "GET":
|
|
948
|
+
return JSONResponse(await run_in_threadpool(state, coordinator.repository, project, run))
|
|
949
|
+
payload = await read_json(request)
|
|
950
|
+
operation = request.path_params["operation"]
|
|
951
|
+
if operation == "edit":
|
|
952
|
+
require_fields(payload, {"tool", "arguments", "expected_revision_id", "mutation_id"})
|
|
953
|
+
if not isinstance(payload["arguments"], dict):
|
|
954
|
+
raise ProjectError("command_invalid", "編輯參數必須是物件。", status_code=422)
|
|
955
|
+
result = await run_in_threadpool(
|
|
956
|
+
coordinator.workbench_edit,
|
|
957
|
+
project,
|
|
958
|
+
run,
|
|
959
|
+
payload["tool"],
|
|
960
|
+
payload["arguments"],
|
|
961
|
+
payload["expected_revision_id"],
|
|
962
|
+
payload["mutation_id"],
|
|
963
|
+
)
|
|
964
|
+
elif operation == "refine":
|
|
965
|
+
require_fields(
|
|
966
|
+
payload,
|
|
967
|
+
{"request_id", "expected_revision_id", "instruction", "consent"},
|
|
968
|
+
{"music_asset_id", "expected_references_version"},
|
|
969
|
+
)
|
|
970
|
+
result = {
|
|
971
|
+
"run": (
|
|
972
|
+
await run_in_threadpool(
|
|
973
|
+
coordinator.refine,
|
|
974
|
+
project,
|
|
975
|
+
run,
|
|
976
|
+
payload["request_id"],
|
|
977
|
+
payload["expected_revision_id"],
|
|
978
|
+
payload["instruction"],
|
|
979
|
+
payload["consent"],
|
|
980
|
+
payload.get("music_asset_id"),
|
|
981
|
+
payload.get("expected_references_version"),
|
|
982
|
+
)
|
|
983
|
+
).to_public()
|
|
984
|
+
}
|
|
985
|
+
elif operation == "reference":
|
|
986
|
+
require_fields(
|
|
987
|
+
payload, {"expected_revision_id", "expected_version", "mutation_id"}, {"reference", "delete_id"}
|
|
988
|
+
)
|
|
887
989
|
from clipen.editing.refinement_references import update
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
990
|
+
|
|
991
|
+
result = await run_in_threadpool(
|
|
992
|
+
update,
|
|
993
|
+
coordinator.repository,
|
|
994
|
+
project,
|
|
995
|
+
run,
|
|
996
|
+
payload["expected_revision_id"],
|
|
997
|
+
payload["expected_version"],
|
|
998
|
+
payload["mutation_id"],
|
|
999
|
+
payload.get("reference"),
|
|
1000
|
+
payload.get("delete_id"),
|
|
1001
|
+
)
|
|
1002
|
+
elif operation == "frame":
|
|
1003
|
+
require_fields(payload, {"expected_revision_id", "frame"}, {"component"})
|
|
891
1004
|
from clipen.editing.visual_preview import frame
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
1005
|
+
|
|
1006
|
+
result = await run_in_threadpool(
|
|
1007
|
+
frame,
|
|
1008
|
+
coordinator.repository,
|
|
1009
|
+
project,
|
|
1010
|
+
run,
|
|
1011
|
+
payload["expected_revision_id"],
|
|
1012
|
+
payload["frame"],
|
|
1013
|
+
payload.get("component"),
|
|
1014
|
+
)
|
|
1015
|
+
elif operation == "render":
|
|
1016
|
+
require_fields(payload, {"expected_revision_id"})
|
|
1017
|
+
result = await run_in_threadpool(
|
|
1018
|
+
coordinator.workbench_render, project, run, payload["expected_revision_id"]
|
|
1019
|
+
)
|
|
1020
|
+
elif operation == "export":
|
|
1021
|
+
require_fields(payload, set())
|
|
1022
|
+
result = await run_in_threadpool(
|
|
1023
|
+
lambda: tools_for(coordinator.repository, project, run).production_call("export_edit", {}).payload
|
|
1024
|
+
)
|
|
1025
|
+
elif operation == "resolve":
|
|
1026
|
+
require_fields(payload, set())
|
|
901
1027
|
from clipen.editing.resolve_export import export_resolve, reveal, start_watching
|
|
902
1028
|
from clipen.editing.workbench import idle
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
await run_in_threadpool(
|
|
1029
|
+
|
|
1030
|
+
await run_in_threadpool(idle, coordinator.repository, project, run)
|
|
1031
|
+
result = await run_in_threadpool(export_resolve, coordinator.repository, project, run)
|
|
1032
|
+
await run_in_threadpool(reveal, Path(result["fcpxml"]))
|
|
906
1033
|
# Saving the timeline as .drt in Resolve is enough; Clipen makes the editable copy on its own.
|
|
907
|
-
if result.get(
|
|
908
|
-
|
|
909
|
-
|
|
1034
|
+
if result.get("editable_overlays"):
|
|
1035
|
+
start_watching(Path(result["folder"]))
|
|
1036
|
+
elif operation in {"resolve_titles", "resolve_status"}:
|
|
1037
|
+
require_fields(payload, {"folder"})
|
|
910
1038
|
from clipen.editing.resolve_export import convert_timeline, export_folder, reveal, timeline_status
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
if
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
1039
|
+
|
|
1040
|
+
exports = (await run_in_threadpool(export_folder, coordinator.repository, project)).resolve()
|
|
1041
|
+
package = Path(str(payload["folder"])).resolve()
|
|
1042
|
+
if package.parent != exports:
|
|
1043
|
+
raise ProjectError(
|
|
1044
|
+
"resolve_package_invalid", "只能轉換這個專案匯出到「Clipen 匯出」的資料夾。", status_code=422
|
|
1045
|
+
)
|
|
1046
|
+
if operation == "resolve_status":
|
|
1047
|
+
return JSONResponse(await run_in_threadpool(timeline_status, package))
|
|
1048
|
+
target, titles = await run_in_threadpool(convert_timeline, package)
|
|
1049
|
+
await run_in_threadpool(reveal, target)
|
|
1050
|
+
result = {"drt": str(target), "titles": titles}
|
|
1051
|
+
else:
|
|
1052
|
+
raise ProjectError("command_invalid", "不支援這個編輯操作。", status_code=404)
|
|
919
1053
|
return JSONResponse(result)
|
|
920
|
-
except ProjectError as error:
|
|
921
|
-
|
|
1054
|
+
except ProjectError as error:
|
|
1055
|
+
return await project_error_response(error)
|
|
1056
|
+
except (TypeError, ValueError) as error:
|
|
1057
|
+
return await project_error_response(ProjectError("command_invalid", str(error), status_code=422))
|
|
922
1058
|
|
|
923
1059
|
async def production_music(request: Request) -> Response:
|
|
924
|
-
from clipen.editing.production_media import AgentAudioStore
|
|
925
1060
|
from clipen.editing.music_choices import music_choices
|
|
1061
|
+
from clipen.editing.production_media import AgentAudioStore
|
|
926
1062
|
from clipen.editing.workbench import import_music
|
|
927
|
-
|
|
928
|
-
|
|
1063
|
+
|
|
1064
|
+
coordinator = require_edit_agents()
|
|
1065
|
+
project = request.path_params["project_id"]
|
|
1066
|
+
directory = None
|
|
929
1067
|
try:
|
|
930
1068
|
# Resolve and authorize the project before creating any upload data.
|
|
931
1069
|
coordinator.repository.projects.get_project(project)
|
|
932
|
-
store=AgentAudioStore(coordinator.repository.projects,project)
|
|
933
|
-
if request.method==
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
1070
|
+
store = AgentAudioStore(coordinator.repository.projects, project)
|
|
1071
|
+
if request.method == "GET":
|
|
1072
|
+
return JSONResponse({"assets": music_choices(store)})
|
|
1073
|
+
latest = coordinator.repository.latest(project)
|
|
1074
|
+
if latest and latest.state in {"queued", "running"}:
|
|
1075
|
+
raise ProjectError("edit_agent_busy", "請先停止 AI 製作,再上傳配樂。", status_code=409)
|
|
1076
|
+
name = request.query_params.get("filename", "")
|
|
1077
|
+
if not name or len(name) > 160 or "/" in name or "\\" in name or "\x00" in name:
|
|
1078
|
+
raise ProjectError("music_invalid", "音樂檔名無效。", status_code=422)
|
|
1079
|
+
suffix = Path(name).suffix.lower()
|
|
1080
|
+
if suffix not in {".mp3", ".wav", ".m4a", ".aac", ".flac", ".ogg", ".aiff", ".aif"}:
|
|
1081
|
+
raise ProjectError(
|
|
1082
|
+
"music_invalid", "請選擇 MP3、WAV、M4A、AAC、FLAC、OGG 或 AIFF 音樂檔。", status_code=422
|
|
1083
|
+
)
|
|
1084
|
+
directory = store.directory / ("upload-" + secrets.token_hex(16))
|
|
1085
|
+
directory.mkdir(mode=0o700)
|
|
1086
|
+
path = directory / ("music" + suffix)
|
|
1087
|
+
size = 0
|
|
1088
|
+
with path.open("xb") as output:
|
|
944
1089
|
path.chmod(0o600)
|
|
945
1090
|
async for chunk in request.stream():
|
|
946
|
-
size+=len(chunk)
|
|
947
|
-
if size>128*1024*1024:
|
|
1091
|
+
size += len(chunk)
|
|
1092
|
+
if size > 128 * 1024 * 1024:
|
|
1093
|
+
raise ProjectError("body_too_large", "音樂檔上限為 128 MB。", status_code=413)
|
|
948
1094
|
output.write(chunk)
|
|
949
|
-
if not size:
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
1095
|
+
if not size:
|
|
1096
|
+
raise ProjectError("music_invalid", "音樂檔是空的。", status_code=422)
|
|
1097
|
+
asset = await run_in_threadpool(import_music, coordinator.repository, project, path, name)
|
|
1098
|
+
directory = None
|
|
1099
|
+
return JSONResponse({"asset": asset}, status_code=201)
|
|
1100
|
+
except ProjectError as error:
|
|
1101
|
+
return await project_error_response(error)
|
|
954
1102
|
finally:
|
|
955
1103
|
if directory is not None:
|
|
956
1104
|
import shutil
|
|
1105
|
+
|
|
957
1106
|
shutil.rmtree(directory)
|
|
958
1107
|
|
|
959
1108
|
async def resume_production(request: Request) -> Response:
|
|
960
1109
|
try:
|
|
961
1110
|
payload = await read_json(request)
|
|
962
|
-
require_fields(payload, {
|
|
963
|
-
run = await run_in_threadpool(
|
|
964
|
-
|
|
965
|
-
|
|
1111
|
+
require_fields(payload, {"consent"})
|
|
1112
|
+
run = await run_in_threadpool(
|
|
1113
|
+
require_edit_agents().resume,
|
|
1114
|
+
request.path_params["project_id"],
|
|
1115
|
+
request.path_params["run_id"],
|
|
1116
|
+
payload["consent"],
|
|
1117
|
+
)
|
|
1118
|
+
return JSONResponse({"run": run.to_public()}, status_code=202)
|
|
966
1119
|
except ProjectError as error:
|
|
967
1120
|
return await project_error_response(error)
|
|
968
1121
|
|
|
@@ -1008,9 +1161,7 @@ def create_app(
|
|
|
1008
1161
|
run, page = await run_in_threadpool(
|
|
1009
1162
|
lambda: (
|
|
1010
1163
|
coordinator.repository.get_run(project_id, run_id),
|
|
1011
|
-
coordinator.repository.list_items(
|
|
1012
|
-
project_id, run_id, offset=offset, limit=limit
|
|
1013
|
-
),
|
|
1164
|
+
coordinator.repository.list_items(project_id, run_id, offset=offset, limit=limit),
|
|
1014
1165
|
)
|
|
1015
1166
|
)
|
|
1016
1167
|
return JSONResponse({"inventory": run.to_public(), **page.to_public()})
|
|
@@ -1054,7 +1205,9 @@ def create_app(
|
|
|
1054
1205
|
raw_offset = request.query_params.get("offset", "0")
|
|
1055
1206
|
if not raw_offset.isascii() or not raw_offset.isdigit() or len(raw_offset) > 4:
|
|
1056
1207
|
raise ProjectError("invalid_page", "頁碼不正確。", status_code=400)
|
|
1057
|
-
return JSONResponse(
|
|
1208
|
+
return JSONResponse(
|
|
1209
|
+
await run_in_threadpool(require_evidence().status, request.path_params["project_id"], int(raw_offset))
|
|
1210
|
+
)
|
|
1058
1211
|
except ProjectError as error:
|
|
1059
1212
|
return await project_error_response(error)
|
|
1060
1213
|
|
|
@@ -1066,13 +1219,22 @@ def create_app(
|
|
|
1066
1219
|
payload = await read_json(request)
|
|
1067
1220
|
if action == "start":
|
|
1068
1221
|
require_fields(payload, {"request_id", "expected_revision", "scope_id"})
|
|
1069
|
-
result = await run_in_threadpool(
|
|
1222
|
+
result = await run_in_threadpool(
|
|
1223
|
+
service.start, project_id, payload["request_id"], payload["expected_revision"], payload["scope_id"]
|
|
1224
|
+
)
|
|
1070
1225
|
elif action in {"cancel", "resume"}:
|
|
1071
1226
|
require_fields(payload, {"run_id"})
|
|
1072
1227
|
result = await run_in_threadpool(getattr(service, action), project_id, payload["run_id"])
|
|
1073
1228
|
elif action == "observe":
|
|
1074
1229
|
require_fields(payload, {"run_id", "request_id", "item_key", "consent"})
|
|
1075
|
-
result = await run_in_threadpool(
|
|
1230
|
+
result = await run_in_threadpool(
|
|
1231
|
+
service.observe,
|
|
1232
|
+
project_id,
|
|
1233
|
+
payload["run_id"],
|
|
1234
|
+
payload["request_id"],
|
|
1235
|
+
payload["item_key"],
|
|
1236
|
+
payload["consent"],
|
|
1237
|
+
)
|
|
1076
1238
|
else:
|
|
1077
1239
|
raise ProjectError("command_unknown", "找不到這個操作。", status_code=404)
|
|
1078
1240
|
return JSONResponse(result, status_code=202)
|
|
@@ -1081,9 +1243,23 @@ def create_app(
|
|
|
1081
1243
|
|
|
1082
1244
|
async def evidence_image(request: Request) -> Response:
|
|
1083
1245
|
try:
|
|
1246
|
+
|
|
1084
1247
|
def read_image():
|
|
1085
|
-
return
|
|
1086
|
-
|
|
1248
|
+
return (
|
|
1249
|
+
require_evidence()
|
|
1250
|
+
.image(
|
|
1251
|
+
request.path_params["project_id"],
|
|
1252
|
+
request.path_params["run_id"],
|
|
1253
|
+
request.path_params["artifact_id"],
|
|
1254
|
+
)
|
|
1255
|
+
.read_bytes()
|
|
1256
|
+
)
|
|
1257
|
+
|
|
1258
|
+
return Response(
|
|
1259
|
+
await run_in_threadpool(read_image),
|
|
1260
|
+
media_type="image/png",
|
|
1261
|
+
headers={"Cache-Control": "private, no-store", "X-Content-Type-Options": "nosniff"},
|
|
1262
|
+
)
|
|
1087
1263
|
except ProjectError as error:
|
|
1088
1264
|
return await project_error_response(error)
|
|
1089
1265
|
|
|
@@ -1150,9 +1326,7 @@ def create_app(
|
|
|
1150
1326
|
require_preparation_repository().result_items,
|
|
1151
1327
|
request.path_params["project_id"],
|
|
1152
1328
|
request.path_params["run_id"],
|
|
1153
|
-
offset=_query_integer(
|
|
1154
|
-
request, "offset", 0, minimum=0, maximum=1_000_000
|
|
1155
|
-
),
|
|
1329
|
+
offset=_query_integer(request, "offset", 0, minimum=0, maximum=1_000_000),
|
|
1156
1330
|
limit=_query_integer(request, "limit", 25, minimum=1, maximum=100),
|
|
1157
1331
|
trip_day=trip_day,
|
|
1158
1332
|
review_state=request.query_params.get("review_state", "all"),
|
|
@@ -1169,9 +1343,7 @@ def create_app(
|
|
|
1169
1343
|
request.path_params["run_id"],
|
|
1170
1344
|
request.path_params["version_id"],
|
|
1171
1345
|
request.path_params["stream_index"],
|
|
1172
|
-
offset=_query_integer(
|
|
1173
|
-
request, "offset", 0, minimum=0, maximum=1_000_000
|
|
1174
|
-
),
|
|
1346
|
+
offset=_query_integer(request, "offset", 0, minimum=0, maximum=1_000_000),
|
|
1175
1347
|
limit=_query_integer(request, "limit", 100, minimum=1, maximum=100),
|
|
1176
1348
|
)
|
|
1177
1349
|
return JSONResponse({"transcript": transcript})
|
|
@@ -1245,9 +1417,7 @@ def create_app(
|
|
|
1245
1417
|
run_id = request.path_params["run_id"]
|
|
1246
1418
|
raw_last = request.headers.get("last-event-id", "0")
|
|
1247
1419
|
if not raw_last.isascii() or not raw_last.isdigit() or len(raw_last) > 20:
|
|
1248
|
-
raise ProjectError(
|
|
1249
|
-
"event_cursor_invalid", "分析進度位置無效。", status_code=422
|
|
1250
|
-
)
|
|
1420
|
+
raise ProjectError("event_cursor_invalid", "分析進度位置無效。", status_code=422)
|
|
1251
1421
|
cursor = int(raw_last)
|
|
1252
1422
|
await run_in_threadpool(repository.get_run, project_id, run_id)
|
|
1253
1423
|
|
|
@@ -1256,9 +1426,7 @@ def create_app(
|
|
|
1256
1426
|
idle = 0
|
|
1257
1427
|
while idle < 50 and not await request.is_disconnected():
|
|
1258
1428
|
events = await run_in_threadpool(
|
|
1259
|
-
lambda: repository.list_events(
|
|
1260
|
-
project_id, run_id, after_sequence=cursor
|
|
1261
|
-
)
|
|
1429
|
+
lambda: repository.list_events(project_id, run_id, after_sequence=cursor)
|
|
1262
1430
|
)
|
|
1263
1431
|
if events:
|
|
1264
1432
|
idle = 0
|
|
@@ -1276,12 +1444,16 @@ def create_app(
|
|
|
1276
1444
|
if idle % 20 == 0:
|
|
1277
1445
|
yield b": keepalive\n\n"
|
|
1278
1446
|
run = await run_in_threadpool(repository.get_run, project_id, run_id)
|
|
1279
|
-
if
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1447
|
+
if (
|
|
1448
|
+
run.state.value
|
|
1449
|
+
in {
|
|
1450
|
+
"succeeded",
|
|
1451
|
+
"failed",
|
|
1452
|
+
"cancelled",
|
|
1453
|
+
"interrupted",
|
|
1454
|
+
}
|
|
1455
|
+
and not events
|
|
1456
|
+
):
|
|
1285
1457
|
return
|
|
1286
1458
|
await asyncio.sleep(0.5)
|
|
1287
1459
|
|
|
@@ -1297,20 +1469,36 @@ def create_app(
|
|
|
1297
1469
|
debug=False,
|
|
1298
1470
|
routes=[
|
|
1299
1471
|
Route("/api/health", health, methods=["GET"]),
|
|
1300
|
-
Route(
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
Route(
|
|
1306
|
-
|
|
1472
|
+
Route(
|
|
1473
|
+
"/api/projects/{project_id}/edit-agent/{run_id}/exports/{revision_id}",
|
|
1474
|
+
edit_agent_export,
|
|
1475
|
+
methods=["GET"],
|
|
1476
|
+
),
|
|
1477
|
+
Route(
|
|
1478
|
+
"/api/projects/{project_id}/production/commands/resume/{run_id}", resume_production, methods=["POST"]
|
|
1479
|
+
),
|
|
1480
|
+
Route("/api/projects/{project_id}/production/music", production_music, methods=["GET", "POST"]),
|
|
1481
|
+
Route("/api/projects/{project_id}/production/{run_id}/workbench", production_workbench, methods=["GET"]),
|
|
1482
|
+
Route(
|
|
1483
|
+
"/api/projects/{project_id}/production/{run_id}/workbench/{operation}",
|
|
1484
|
+
production_workbench,
|
|
1485
|
+
methods=["POST"],
|
|
1486
|
+
),
|
|
1487
|
+
Route("/api/production/prompts", production_prompt_history, methods=["GET"]),
|
|
1488
|
+
Route("/api/projects/{project_id}/production/prompts", production_prompt_history, methods=["POST"]),
|
|
1307
1489
|
Route("/api/production/skills", production_skills, methods=["GET"]),
|
|
1308
1490
|
Route("/api/production/skill-library", skill_library_status, methods=["GET"]),
|
|
1309
1491
|
Route("/api/production/skills/{skill_id}/document", skill_document, methods=["GET"]),
|
|
1310
1492
|
Route("/api/production/skill-library/commands/import", import_skill, methods=["POST"]),
|
|
1311
1493
|
Route("/api/production/skill-library/{skill_id}/commands/remove", remove_skill, methods=["POST"]),
|
|
1312
|
-
Route(
|
|
1313
|
-
|
|
1494
|
+
Route(
|
|
1495
|
+
"/api/production/skills/{skill_id}/styles/{style_id}/preview", production_style_preview, methods=["GET"]
|
|
1496
|
+
),
|
|
1497
|
+
Route(
|
|
1498
|
+
"/api/production/skills/{skill_id}/templates/{template_id}/sample",
|
|
1499
|
+
production_template_sample,
|
|
1500
|
+
methods=["GET"],
|
|
1501
|
+
),
|
|
1314
1502
|
Route("/api/projects/{project_id}/production/commands/start", start_production, methods=["POST"]),
|
|
1315
1503
|
Route("/api/session/connect", connect_local_browser, methods=["POST"]),
|
|
1316
1504
|
Route("/api/instance/reopen", reopen_instance, methods=["POST"]),
|
|
@@ -1415,8 +1603,7 @@ def create_app(
|
|
|
1415
1603
|
methods=["GET"],
|
|
1416
1604
|
),
|
|
1417
1605
|
Route(
|
|
1418
|
-
"/api/projects/{project_id}/analyses/{run_id}/items/"
|
|
1419
|
-
"{version_id}/{stream_index:int}/transcript",
|
|
1606
|
+
"/api/projects/{project_id}/analyses/{run_id}/items/{version_id}/{stream_index:int}/transcript",
|
|
1420
1607
|
transcript_segments,
|
|
1421
1608
|
methods=["GET"],
|
|
1422
1609
|
),
|