cmvr-visual/backend/app/services/dialogue.py

148 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""对白模式命令。模型工作只能通过持久化任务,不在HTTP命令内执行。"""
from app.core.maintenance import require_writable
from app.models import WorkflowDraft
from app.services.auth import require_session_user
from app.services.submissions import accept_submission, request_payload
from app.services.snapshots import owned
async def enable_dialogue(runtime, actor, draft_id, body, key):
from app.core.errors import fail
from app.domain.dialogue import upgrade_dialogue_manifest
from app.services.editors import check_version, dto, load_manifest, write_manifest
from app.services.editor_modes import assert_idle
require_writable(runtime.settings)
async with runtime.db.session() as session:
await require_session_user(session, actor)
receipt = await accept_submission(
session,
actor.id,
"editor.dialogue.enable",
key,
1,
{"draft_id": draft_id, **request_payload(body)},
)
draft = await owned(session, WorkflowDraft, draft_id, actor.id)
if receipt.resource_json:
return await dto(session, draft)
check_version(draft, body.expected_version)
if draft.kind != "story_v3":
fail(422, "dialogue_story_required", "对白模式仅适用于故事")
await assert_idle(session, draft)
manifest = await load_manifest(session, draft)
if manifest.get("schema_version") != 3 or not draft.editor_manifest:
manifest = upgrade_dialogue_manifest(manifest)
manifest["editing_mode"] = "auto"
await write_manifest(session, draft, manifest, runtime.settings)
elif manifest.get("spec", {}).get("dialogue", {}).get("generation") != "native_av":
# 历史已冻结运行保持原图;只有显式启用命令升级作者草稿。
manifest["spec"]["dialogue"].update(generation="native_av", language="Chinese")
await write_manifest(session, draft, manifest, runtime.settings)
receipt.resource_json = {"draft_id": draft.id}
result = await dto(session, draft)
await session.commit()
return result
async def apply_pending_result(db, run_id, settings):
"""推进事务提交后独立应用;与编辑命令使用相同锁顺序。"""
from sqlalchemy import select
from app.models import User, WorkflowRun
from app.services.editors import apply_run_result
async with db.session() as session:
identity = (
await session.execute(
select(WorkflowRun.owner_id, WorkflowRun.draft_id).where(
WorkflowRun.id == run_id,
WorkflowRun.deleted_at.is_(None),
WorkflowRun.state == "succeeded",
WorkflowRun.apply_state == "pending",
)
)
).first()
if not identity:
return False
owner_id, draft_id = identity
await session.scalar(select(User).where(User.id == owner_id).with_for_update())
await session.scalar(
select(WorkflowDraft)
.where(WorkflowDraft.id == draft_id, WorkflowDraft.owner_id == owner_id)
.with_for_update()
)
run = await session.scalar(
select(WorkflowRun)
.where(WorkflowRun.id == run_id)
.with_for_update()
.execution_options(populate_existing=True)
)
if (
not run
or run.deleted_at is not None
or run.apply_state != "pending"
or run.state != "succeeded"
or run.desired_state == "cancel"
or run.spec_snapshot.get("schema_version") != 5
):
return False
result = await apply_run_result(session, run, settings)
await session.commit()
return result
async def fork_prepared(runtime, actor, draft_id, body, key):
from copy import deepcopy
from app.core.errors import fail
from app.models import WorkflowRun
from app.services.editors import check_version, dto, write_manifest
require_writable(runtime.settings)
async with runtime.db.session() as session:
await require_session_user(session, actor)
receipt = await accept_submission(
session,
actor.id,
"editor.dialogue.fork_prepared",
key,
1,
{"draft_id": draft_id, **request_payload(body)},
)
if receipt.resource_json:
target = await owned(
session, WorkflowDraft, receipt.resource_json["draft_id"], actor.id
)
return {**await dto(session, target), "origin": receipt.resource_json["origin"]}
source = await owned(session, WorkflowDraft, draft_id, actor.id)
check_version(source, body.expected_version)
run = await owned(session, WorkflowRun, body.run_id, actor.id)
if run.draft_id != draft_id or run.spec_snapshot.get("schema_version") != 5:
fail(404, "resource_not_found", "准备结果不属于此故事")
if (
not run.prepared_plan
or not run.prepared_plan.get("production_materialized")
or not run.prepared_plan.get("timing", {}).get("shots")
):
fail(409, "preparation_incomplete", "尚无可采用的准备结构")
name = body.name.strip()
if not name:
fail(422, "name_required", "请输入副本名称")
manifest = deepcopy(run.result_manifest or run.prepared_plan["effective_manifest"])
manifest["base_run_id"] = None
target = WorkflowDraft(
owner_id=actor.id,
name=name,
kind="story_v3",
folder_id=source.folder_id,
spec=deepcopy(manifest["spec"]),
)
session.add(target)
await session.flush()
await write_manifest(session, target, manifest, runtime.settings)
origin = {"source_draft_id": source.id, "source_run_id": run.id}
receipt.resource_json = {"draft_id": target.id, "origin": origin}
result = {**await dto(session, target), "origin": origin}
await session.commit()
return result