196 lines
7.7 KiB
Python
196 lines
7.7 KiB
Python
"""把作者决定绑定到同一次双人试拍的镜头、视频及质检报告。"""
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.core.errors import fail
|
|
from app.core.maintenance import require_writable
|
|
from app.domain.native_av import TURN_POLICY
|
|
from app.models import Asset, Submission, WorkflowDraft, WorkflowRun, WorkflowStep, utcnow
|
|
from app.services.auth import require_session_user
|
|
from app.services.snapshots import owned
|
|
from app.services.submissions import accept_submission
|
|
from app.services.workflows import emit_run, verified_output, wake
|
|
|
|
|
|
OPERATION = "editor.turn_review"
|
|
POLICY = TURN_POLICY
|
|
|
|
|
|
async def _decisions(session, run):
|
|
receipts = await session.scalars(
|
|
select(Submission.resource_json).where(
|
|
Submission.owner_id == run.owner_id,
|
|
Submission.operation == OPERATION,
|
|
Submission.resource_json["run_id"].as_string() == run.id,
|
|
)
|
|
)
|
|
return {
|
|
(r["shot_id"], r["report_asset_id"], r["report_sha256"]): r
|
|
for r in receipts
|
|
if r.get("shot_id")
|
|
}
|
|
|
|
|
|
async def manually_accepted(session, run, gate_step, report_binding):
|
|
key = (
|
|
gate_step.recipe_json["unit_id"],
|
|
report_binding.get("asset_id"),
|
|
report_binding.get("sha256"),
|
|
)
|
|
return key in await _decisions(session, run)
|
|
|
|
|
|
def reviewable_turns(check, gate, binding, video):
|
|
"""只有验收门检查过的同份原片、两轮台词和声轨可转人工。"""
|
|
request = (gate.result_json or {}).get("review_request")
|
|
if request != {"asset_id": binding["asset_id"], "sha256": binding["sha256"]}:
|
|
return False
|
|
contract = (check.resolved_input_json or {}).get("parameters") or {}
|
|
roles = contract.get("speaker_ids") or []
|
|
intent = contract.get("turns") or []
|
|
rows = binding["report"].get("turns") or []
|
|
if len(roles) != 2 or len(set(roles)) != 2 or len(intent) != 2 or len(rows) != 2:
|
|
return False
|
|
end = 0
|
|
for expected, row in zip(intent, rows, strict=True):
|
|
speaker = expected.get("speaker_id")
|
|
start, stop = row.get("start_sample"), row.get("end_sample")
|
|
if (
|
|
speaker not in roles
|
|
or row.get("expected_speaker_id") != speaker
|
|
or row.get("listener_id") != next(s for s in roles if s != speaker)
|
|
or row.get("utterance_id") != expected.get("utterance_id")
|
|
or row.get("decision") not in {"pass", "indeterminate"}
|
|
or type(start) is not int
|
|
or type(stop) is not int
|
|
or not end <= start < stop <= (video.get("duration_frames") or 0) * 2000
|
|
):
|
|
return False
|
|
end = stop
|
|
return True
|
|
|
|
|
|
async def candidates(session, run, steps):
|
|
by_key = {s.step_key: s for s in steps}
|
|
decisions = await _decisions(session, run)
|
|
rows = []
|
|
for gate in steps:
|
|
recipe = gate.recipe_json or {}
|
|
if gate.kind != "dialogue.accepted" or not recipe.get("turn_report_key"):
|
|
continue
|
|
check = by_key.get(recipe["turn_report_key"])
|
|
video = by_key.get(recipe["video_key"])
|
|
if not check or check.state != "succeeded" or not video:
|
|
continue
|
|
binding = check.result_json or {}
|
|
report = binding.get("report") or {}
|
|
source = video.result_json or {}
|
|
if (
|
|
report.get("policy") != POLICY
|
|
or not binding.get("sha256")
|
|
or not source.get("asset_id")
|
|
or (report.get("source_sha256") or {}).get("video") != source.get("sha256")
|
|
):
|
|
continue
|
|
sid = recipe["unit_id"]
|
|
decision = decisions.get((sid, binding["asset_id"], binding["sha256"]))
|
|
rows.append(
|
|
dict(
|
|
shot_id=sid,
|
|
report_sha256=binding["sha256"],
|
|
report_asset_id=binding["asset_id"],
|
|
report=report,
|
|
video_asset_id=source["asset_id"],
|
|
manual_review=decision,
|
|
can_accept=not decision
|
|
and report.get("decision") == "indeterminate"
|
|
and reviewable_turns(check, gate, binding, source)
|
|
and gate.state == "awaiting_review"
|
|
and run.desired_state != "cancel"
|
|
and run.state not in {"succeeded", "cancelled", "cancelling", "failed"},
|
|
)
|
|
)
|
|
return rows
|
|
|
|
|
|
async def review(runtime, actor, draft_id, run_id, shot_id, body, key):
|
|
from app.services.orchestration.native_dialogue_engine import _read_report
|
|
from app.services.snapshots import snapshot_run
|
|
|
|
require_writable(runtime.settings)
|
|
if not body.note.strip():
|
|
fail(422, "turn_review_note_required", "请填写逐轮复核意见")
|
|
async with runtime.db.session() as session:
|
|
await require_session_user(session, actor)
|
|
await owned(session, WorkflowDraft, draft_id, actor.id)
|
|
run = await owned(session, WorkflowRun, run_id, actor.id)
|
|
if run.draft_id != draft_id:
|
|
fail(404, "resource_not_found", "候选不属于此故事")
|
|
receipt = await accept_submission(
|
|
session,
|
|
actor.id,
|
|
OPERATION,
|
|
key,
|
|
1,
|
|
dict(run_id=run.id, draft_id=draft_id, shot_id=shot_id, **body.model_dump()),
|
|
)
|
|
if receipt.resource_json:
|
|
return await snapshot_run(session, actor.id, run.id)
|
|
if run.version != body.expected_version:
|
|
fail(409, "run_version_conflict", "运行已更新,请刷新后重新查看候选")
|
|
steps = (
|
|
await session.scalars(select(WorkflowStep).where(WorkflowStep.run_id == run.id))
|
|
).all()
|
|
row = next(
|
|
(r for r in await candidates(session, run, steps) if r["shot_id"] == shot_id), None
|
|
)
|
|
if not row or not row["can_accept"] or row["report_sha256"] != body.report_sha256:
|
|
fail(409, "turn_review_unavailable", "该候选已变化或未进入可人工复核状态")
|
|
for asset_id, sha in (
|
|
(row["report_asset_id"], row["report_sha256"]),
|
|
(row["video_asset_id"], row["report"]["source_sha256"]["video"]),
|
|
):
|
|
asset = await owned(session, Asset, asset_id, actor.id)
|
|
if (
|
|
not asset.ready
|
|
or asset.library_scope != "private"
|
|
or asset.sha256 != sha
|
|
or not await verified_output(asset, runtime.settings)
|
|
):
|
|
fail(409, "turn_sources_changed", "候选视频或报告已变化,请重新检查镜头")
|
|
if (
|
|
asset.id == row["report_asset_id"]
|
|
and _read_report(asset, runtime.settings) != row["report"]
|
|
):
|
|
fail(409, "turn_sources_changed", "报告与冻结记录不一致")
|
|
receipt.resource_json = dict(
|
|
run_id=run.id,
|
|
shot_id=shot_id,
|
|
report_asset_id=row["report_asset_id"],
|
|
report_sha256=body.report_sha256,
|
|
video_asset_id=row["video_asset_id"],
|
|
reviewer_id=actor.id,
|
|
reviewer_name=actor.display_name or actor.username,
|
|
reviewed_at=utcnow().isoformat(),
|
|
decision="accept",
|
|
note=body.note,
|
|
)
|
|
gate = next(
|
|
s
|
|
for s in steps
|
|
if s.kind == "dialogue.accepted" and s.recipe_json["unit_id"] == shot_id
|
|
)
|
|
gate.state = "waiting"
|
|
gate.version += 1
|
|
if (
|
|
run.state == "blocked"
|
|
and (run.error_json or {}).get("code") == "native_turn_review_required"
|
|
):
|
|
run.state, run.error_json = "queued", None
|
|
await session.flush()
|
|
await emit_run(session, run)
|
|
result = await snapshot_run(session, actor.id, run.id)
|
|
await session.commit()
|
|
wake(runtime)
|
|
return result
|