Restructure backend and frontend modules; add story, library, and editor schema migrations. Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews. Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
116 lines
3.8 KiB
Python
116 lines
3.8 KiB
Python
import hashlib
|
||
import json
|
||
from sqlalchemy import select
|
||
from app.core.security import Actor
|
||
from app.core.errors import fail
|
||
from app.models import Submission
|
||
|
||
"Durable intent records; callers commit the record and resource in one transaction."
|
||
|
||
|
||
def folder_payload(body):
|
||
result = dict(body)
|
||
if result.get("folder_id") is None:
|
||
result.pop("folder_id", None)
|
||
return result
|
||
|
||
|
||
def request_payload(body):
|
||
# 新增归属字段省略时维持旧指纹;显式 output null 必须保留,表示取消继承目标。
|
||
result = body.model_dump(mode="json")
|
||
for key in ("folder_id", "output_folder_id"):
|
||
if key not in body.model_fields_set:
|
||
result.pop(key, None)
|
||
if "items" in result and hasattr(body, "items"):
|
||
result["items"] = [request_payload(item) for item in body.items]
|
||
return folder_payload(result)
|
||
|
||
|
||
def body_hash(body, schema=2):
|
||
try:
|
||
return hashlib.sha256(
|
||
json.dumps(
|
||
{"schema": schema, "body": body},
|
||
sort_keys=True,
|
||
separators=(",", ":"),
|
||
ensure_ascii=False,
|
||
allow_nan=False,
|
||
).encode()
|
||
).hexdigest()
|
||
except (ValueError, TypeError):
|
||
fail(422, "invalid_json", "请求必须为合法JSON")
|
||
|
||
|
||
async def accept_submission(session, owner_id, operation, key, payload_schema, body):
|
||
if not key or not 1 <= len(key) <= 128:
|
||
fail(422, "invalid_idempotency_key", "需要1–128字符的幂等键")
|
||
digest = body_hash(body, payload_schema)
|
||
row = await session.scalar(
|
||
select(Submission).where(
|
||
Submission.owner_id == owner_id,
|
||
Submission.operation == operation,
|
||
Submission.key == key,
|
||
)
|
||
)
|
||
if row:
|
||
if row.body_hash != digest:
|
||
fail(409, "idempotency_conflict", "此幂等键已用于不同参数")
|
||
if row.status == "purged":
|
||
fail(410, "resource_purged", "提交结果已清理")
|
||
return row
|
||
row = Submission(
|
||
owner_id=owner_id,
|
||
operation=operation,
|
||
key=key,
|
||
payload_schema=payload_schema,
|
||
body_hash=digest,
|
||
request_json=body,
|
||
resource_json={},
|
||
)
|
||
session.add(row)
|
||
await session.flush()
|
||
return row
|
||
|
||
|
||
async def recover(key: str, runtime, operation: str, actor: Actor = None):
|
||
async with runtime.db.session() as session:
|
||
row = await session.scalar(
|
||
select(Submission).where(
|
||
Submission.owner_id == actor.id,
|
||
Submission.operation == operation,
|
||
Submission.key == key,
|
||
)
|
||
)
|
||
if not row and operation == "jobs.create":
|
||
from app.models import Job
|
||
|
||
legacy = (
|
||
await session.scalars(
|
||
select(Job)
|
||
.where(Job.owner_id == actor.id, Job.request_key == key)
|
||
.order_by(Job.batch_index)
|
||
)
|
||
).all()
|
||
if legacy:
|
||
if any((job.deleted_at is not None for job in legacy)):
|
||
fail(410, "resource_purged", "提交结果已删除")
|
||
resources = {"job_ids": [job.id for job in legacy]}
|
||
return {
|
||
"key": key,
|
||
"operation": operation,
|
||
"status": "accepted",
|
||
"resource_json": resources,
|
||
**resources,
|
||
}
|
||
if not row:
|
||
fail(404, "submission_not_found", "尚未找到提交记录;重试仍须使用原幂等键")
|
||
if row.status == "purged":
|
||
fail(410, "resource_purged", "提交结果已清理")
|
||
return {
|
||
"key": row.key,
|
||
"operation": row.operation,
|
||
"status": row.status,
|
||
"resource_json": row.resource_json,
|
||
**row.resource_json,
|
||
}
|