v 0.1.2
parent
01606b2949
commit
ce91173a6c
|
|
@ -10,7 +10,7 @@ from app import schemas
|
|||
from app.api import deps
|
||||
from app.constants import ShotStatus, TaskKind, TaskStatus
|
||||
from app.db import get_db
|
||||
from app.models import ReviewRecord, Shot, Task, now_utc
|
||||
from app.models import Project, ReviewRecord, Shot, Task, now_utc
|
||||
from app.services import daemon, dispatch, events, quality, status, storage
|
||||
|
||||
router = APIRouter(tags=["pipeline"])
|
||||
|
|
@ -88,6 +88,20 @@ def list_tasks(project_id: int | None = Query(None), status_filter: str = Query(
|
|||
return {"tasks": [status.task_dict(t) for t in rows], "count": len(rows), "counts": counts}
|
||||
|
||||
|
||||
# 注意顺序:这条必须排在 /api/tasks/{task_id} 前面,否则 activity 会被当成 task_id 解析
|
||||
@router.get("/api/tasks/activity")
|
||||
def get_task_activity(active_only: bool = Query(False), owner_type: str = Query(""),
|
||||
limit: int = Query(status.ACTIVITY_LIMIT), db: Session = Depends(get_db)):
|
||||
"""平台动态:此刻在跑什么、刚刚跑完了什么(项目侧 + 平台侧同源一条接口)。
|
||||
|
||||
顶栏每 3 秒问一次,所以它只扫任务表里进行中的那几行 + 最近 6 小时的落定记录,
|
||||
不碰项目统计。前端据此决定要不要重新拉列表——用户不必再手动刷新页面去猜
|
||||
「图到底出没了」。
|
||||
"""
|
||||
return {"ok": True, "result": status.activity(db, limit=limit, owner_type=owner_type,
|
||||
active_only=active_only)}
|
||||
|
||||
|
||||
@router.get("/api/tasks/{task_id}")
|
||||
def get_task(task_id: int, db: Session = Depends(get_db)):
|
||||
return {"task": status.task_dict(deps.get_task(db, task_id))}
|
||||
|
|
@ -96,7 +110,8 @@ def get_task(task_id: int, db: Session = Depends(get_db)):
|
|||
@router.post("/api/tasks/{task_id}/retry")
|
||||
def retry_task(task_id: int, data: schemas.RetryOptions, db: Session = Depends(get_db)):
|
||||
task = deps.get_task(db, task_id)
|
||||
project = deps.get_project(db, task.project_id)
|
||||
# 平台资产生成不属于任何项目:按项目取父记录会凭空 404,界面就永远没法重试
|
||||
project = db.get(Project, task.project_id) if task.project_id else None
|
||||
if task.status not in (TaskStatus.FAILED, TaskStatus.CANCELLED):
|
||||
raise HTTPException(409, f"当前状态 {task.status} 不能重试")
|
||||
task.status = TaskStatus.QUEUED
|
||||
|
|
@ -113,18 +128,27 @@ def retry_task(task_id: int, data: schemas.RetryOptions, db: Session = Depends(g
|
|||
shot.status = ShotStatus.QUEUED
|
||||
shot.stage = "queued"
|
||||
db.flush()
|
||||
events.log_event(db, "task.requeued", project_id=project.id, task_id=task.id, shot_id=task.shot_id,
|
||||
events.log_event(db, "task.requeued", project_id=project.id if project else None,
|
||||
task_id=task.id, shot_id=task.shot_id,
|
||||
message=f"任务 #{task.id} 人工重试入队", entity_type="task", entity_id=task.id)
|
||||
if project is None:
|
||||
db.commit()
|
||||
# 用户是点了「重试」才来的,不该再等一轮调度扫描
|
||||
dispatch.dispatch_task(db, db.get(Task, task.id))
|
||||
return {"ok": True, "result": status.task_dict(db.get(Task, task.id))}
|
||||
return deps.done(db, project, {"task_id": task.id, "status": task.status})
|
||||
|
||||
|
||||
@router.post("/api/tasks/{task_id}/cancel")
|
||||
def cancel_task(task_id: int, data: schemas.CancelOptions, db: Session = Depends(get_db)):
|
||||
task = deps.get_task(db, task_id)
|
||||
project = deps.get_project(db, task.project_id)
|
||||
project = db.get(Project, task.project_id) if task.project_id else None
|
||||
if task.status in TaskStatus.TERMINAL:
|
||||
raise HTTPException(409, f"任务已结束({task.status}),无需取消")
|
||||
dispatch.cancel_task(db, task, reason=data.reason)
|
||||
if project is None:
|
||||
db.commit()
|
||||
return {"ok": True, "result": status.task_dict(db.get(Task, task.id))}
|
||||
return deps.done(db, project, {"task_id": task.id, "status": task.status})
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
"""平台级数字资产 API:跨项目人物、场景、声线的管理、生成与引入。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import re
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
||||
from sqlalchemy import select
|
||||
|
|
@ -17,8 +15,14 @@ from app.db import get_db
|
|||
from app.models import (Asset, Character, PlatformCharacter, PlatformMedia, PlatformMediaVersion,
|
||||
PlatformScene, PlatformVoice, Project, Scene, VoiceAsset, now_utc)
|
||||
from app.providers import registry as model_registry
|
||||
from app.providers.base import AssetGenRequest, ProviderError, VoiceGenRequest
|
||||
from app.services import audio, events, storage, timbre, versions
|
||||
from app.providers.base import AssetGenRequest
|
||||
from app.services import (audio, events, platform_generation, status as status_service, storage,
|
||||
timbre, versions)
|
||||
from app.services.platform_generation import (build_owner_prompt as _owner_prompt,
|
||||
get_owner as _owner,
|
||||
normalize_type as _normalize_type,
|
||||
resolve_reference_media as _reference_media,
|
||||
type_label as _type_label)
|
||||
|
||||
router = APIRouter(tags=["platform-assets"])
|
||||
|
||||
|
|
@ -30,28 +34,6 @@ PLATFORM_KINDS: dict[str, set[str]] = {
|
|||
}
|
||||
|
||||
|
||||
def _normalize_type(owner_type: str) -> str:
|
||||
return {
|
||||
"characters": "human", "character": "human", "human": "human", "humans": "human",
|
||||
"scene": "scene", "scenes": "scene", "voice": "voice", "voices": "voice",
|
||||
}.get(owner_type, owner_type)
|
||||
|
||||
|
||||
def _owner(db: Session, owner_type: str, owner_id: int):
|
||||
owner_type = _normalize_type(owner_type)
|
||||
if owner_type == "human":
|
||||
row = db.get(PlatformCharacter, owner_id)
|
||||
elif owner_type == "scene":
|
||||
row = db.get(PlatformScene, owner_id)
|
||||
elif owner_type == "voice":
|
||||
row = db.get(PlatformVoice, owner_id)
|
||||
else:
|
||||
raise HTTPException(400, f"不支持的平台资产类型:{owner_type}")
|
||||
if row is None:
|
||||
raise HTTPException(404, "平台资产不存在")
|
||||
return row
|
||||
|
||||
|
||||
def _media(db: Session, owner_type: str, owner_id: int) -> list[dict]:
|
||||
rows = db.query(PlatformMedia).filter(
|
||||
PlatformMedia.owner_type == owner_type, PlatformMedia.owner_id == owner_id,
|
||||
|
|
@ -59,31 +41,6 @@ def _media(db: Session, owner_type: str, owner_id: int) -> list[dict]:
|
|||
return [storage.platform_media_dict(row) for row in rows]
|
||||
|
||||
|
||||
def _reference_media(db: Session, owner_type: str, owner_id: int, ids: list[int],
|
||||
*, kind: str) -> list[PlatformMedia]:
|
||||
"""校验平台生成参考素材只能来自当前资产,避免跨资产误读本地路径。"""
|
||||
rows: list[PlatformMedia] = []
|
||||
seen: set[int] = set()
|
||||
image_suffixes = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff"}
|
||||
audio_suffixes = {".wav", ".mp3", ".m4a", ".aac", ".flac", ".ogg", ".opus"}
|
||||
for media_id in ids:
|
||||
if media_id in seen:
|
||||
continue
|
||||
seen.add(media_id)
|
||||
row = db.get(PlatformMedia, media_id)
|
||||
if (row is None or row.owner_type != owner_type or row.owner_id != owner_id):
|
||||
raise HTTPException(400, "参考素材不属于当前平台资产")
|
||||
if not storage.exists(row.path):
|
||||
raise HTTPException(400, "参考素材文件不存在,请重新上传")
|
||||
suffix = Path(row.path).suffix.lower()
|
||||
if kind == "image" and suffix not in image_suffixes:
|
||||
raise HTTPException(400, "图像参考素材必须是图片文件")
|
||||
if kind == "audio" and suffix not in audio_suffixes:
|
||||
raise HTTPException(400, "音色参考素材必须是音频文件")
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
|
||||
def _human_dict(db: Session, row: PlatformCharacter, media: list[dict] | None = None) -> dict:
|
||||
project = db.get(Project, row.project_id) if row.project_id else None
|
||||
return {
|
||||
|
|
@ -920,39 +877,6 @@ def publish_project_asset(data: schemas.PlatformPublish, db: Session = Depends(g
|
|||
"media_version": max(versions) if versions else 0}}
|
||||
|
||||
|
||||
def produced_media_metadata(media, reference: dict, check: dict, text: str) -> None:
|
||||
"""把「这次到底用了谁的嗓子、念了什么、音色对不对」写进素材,界面才说得清楚。"""
|
||||
metadata = dict(media.metadata_json or {})
|
||||
metadata.update({
|
||||
"text": text,
|
||||
"reference_audio": reference.get("path") or reference.get("attached") or "",
|
||||
"reference_source": reference.get("source") or "",
|
||||
"reference_note": reference.get("note") or "使用本声线已绑定的参考音频",
|
||||
"timbre_check": check,
|
||||
})
|
||||
media.metadata_json = metadata
|
||||
note = str(media.note or "")
|
||||
tail = check.get("note") or ""
|
||||
if tail and "音色自检" not in note:
|
||||
media.note = f"{note}|音色自检:{tail}".strip("|")
|
||||
|
||||
|
||||
def _owner_prompt(project: Project | None, owner_type: str, owner, kind: str, custom: str) -> str:
|
||||
if custom.strip():
|
||||
return custom.strip()
|
||||
bits = []
|
||||
if owner_type == "human":
|
||||
bits = [f"{owner.name},电影级角色设定图", owner.appearance, owner.persona_prompt,
|
||||
"五官、发型、体型、服装与配饰保持一致,正面清晰,材质细节可信"]
|
||||
elif owner_type == "scene":
|
||||
bits = [f"{owner.name},电影级场景概念图", owner.prompt or owner.description,
|
||||
owner.lighting, owner.time_of_day, owner.palette,
|
||||
"空间结构清晰,透视准确,建筑与陈设保持一致"]
|
||||
if project:
|
||||
bits.append(project.style_guide)
|
||||
return "。".join(str(part).strip() for part in bits if part)
|
||||
|
||||
|
||||
@router.get("/api/platform/{owner_type}/{asset_id}/reference-status")
|
||||
def platform_reference_status(owner_type: str, asset_id: int, db: Session = Depends(get_db)):
|
||||
"""开「生成试听」抽屉时先问一句:现在这点生成会拿到谁的嗓子。
|
||||
|
|
@ -969,109 +893,24 @@ def platform_reference_status(owner_type: str, asset_id: int, db: Session = Depe
|
|||
db, owner, clone=timbre.clone_model(spec), instruct=timbre.instruct_model(spec))}
|
||||
|
||||
|
||||
@router.post("/api/platform/{owner_type}/{asset_id}/generate")
|
||||
@router.post("/api/platform/{owner_type}/{asset_id}/generate", status_code=202)
|
||||
def generate_platform_asset(owner_type: str, asset_id: int, data: schemas.PlatformGenerate,
|
||||
db: Session = Depends(get_db)):
|
||||
"""发起一次生成:当场回 202 与任务号,出图/出试听在后台跑。
|
||||
|
||||
以前这里是同步等出图的:实测 Qwen-Image 出图 46~49s,反代默认 60s 读超时,
|
||||
余量只有十几秒,同卡一排队就直接 504(前端表现为「生成失败」,图却照样落盘)。
|
||||
现在它登记成统一任务表里的一条 platform_asset:便宜的校验同步做完才返 202
|
||||
(表单该红的框立刻红),进度与产物由 ``GET /api/tasks/activity`` 供
|
||||
顶栏「平台动态」轮询,刷新页面、换台设备都还看得见同一条进度。
|
||||
"""
|
||||
owner_type = _normalize_type(owner_type)
|
||||
owner = _owner(db, owner_type, asset_id)
|
||||
if owner_type == "voice":
|
||||
spec, _adapter = model_registry.resolve_voice(db, data.provider or None, data.model or None)
|
||||
if not spec.enabled:
|
||||
raise HTTPException(409, f"模型 {spec.provider}/{spec.model} 已停用")
|
||||
reference_ids = list(data.reference_media_ids)
|
||||
if data.reference_media_id:
|
||||
reference_ids.append(data.reference_media_id)
|
||||
references = _reference_media(db, "voice", owner.id, reference_ids, kind="audio")
|
||||
reference_media = references[0] if references else None
|
||||
# 试听文本是「模型要念出来的台词」,不是声线设定。旧实现拿 owner.description 兜底,
|
||||
# 于是用户填的「温和、语速中等,沉稳」被当成台词念了出来,试听样本一听就是设定说明文。
|
||||
text = (data.sample_text or "").strip()
|
||||
if not text:
|
||||
raise HTTPException(422, "请先填写试听文本:它是模型要朗读的台词,"
|
||||
"系统不会用「音色描述」代替")
|
||||
out = storage.platform_dir("voice", owner.id, "generated") / f"sample-{now_utc().timestamp():.0f}.wav"
|
||||
# 参考音频决定音色,而表单里的「性别」对 IndexTTS2 只是元数据。
|
||||
# 旧实现缺参考时直接回退到模型默认参考(女声),男声声线就这样被生成成了女声。
|
||||
reference = timbre.resolve_reference(
|
||||
db, owner,
|
||||
explicit=str(storage.to_absolute(reference_media.path)) if reference_media else "",
|
||||
clone=timbre.clone_model(spec), instruct=timbre.instruct_model(spec))
|
||||
if not reference["path"] and reference.get("source") == "none":
|
||||
raise HTTPException(409, reference["note"])
|
||||
# 交给模型的是绝对路径(供应商要直接读文件);声线列里存 media_root 相对路径,
|
||||
# 换机器 / 进容器时不会因为写死绝对路径而失效。与任务链路 handlers 保持一致。
|
||||
reference_path = reference["path"] or owner.provider_voice_id
|
||||
request = VoiceGenRequest(
|
||||
text=text, voice_id=owner.name, provider_voice_id=reference_path,
|
||||
language=owner.language, speed=float((owner.tuning or {}).get("speed", 1.0)),
|
||||
seed=data.seed, params=(owner.tuning or {}).get("params") or {},
|
||||
# 没有参考音频时靠它出音,有参考音频时用它微调音色(见 comfy_assets.graph)
|
||||
instruct_text=timbre.instruct_text_of(owner) if timbre.instruct_model(spec) else "",
|
||||
idempotency_key=f"platform-voice:{owner.id}:{asset_id}",
|
||||
label=f"PLATFORM-VOICE-{owner.id}", output_path=out,
|
||||
)
|
||||
try:
|
||||
result = model_registry.generate_voice(db, request, spec.provider, spec.model)
|
||||
except ProviderError as exc:
|
||||
raise HTTPException(502, f"{exc.message}({exc.code})") from exc
|
||||
kind = "voice_sample"
|
||||
owner.provider = spec.provider
|
||||
if reference["path"] and timbre.clone_model(spec):
|
||||
# 把真正参与克隆的那段参考音频绑回声线:否则试听样本是 A 的嗓子,
|
||||
# 之后项目配音又落回模型默认参考,变成另一种嗓子。
|
||||
owner.provider_voice_id = str(storage.to_relative(reference["path"]))
|
||||
else:
|
||||
spec, _adapter = model_registry.resolve_asset(db, "image", data.provider or None, data.model or None)
|
||||
if not spec.enabled:
|
||||
raise HTTPException(409, f"模型 {spec.provider}/{spec.model} 已停用")
|
||||
references = _reference_media(db, owner_type, owner.id, data.reference_media_ids, kind="image")
|
||||
prompt = _owner_prompt(None, owner_type, owner, data.kind, data.prompt)
|
||||
out = storage.platform_dir(owner_type, owner.id, "generated") / f"{data.kind}.png"
|
||||
request = AssetGenRequest(
|
||||
kind=data.kind, prompt=prompt, negative_prompt=data.negative_prompt,
|
||||
width=data.width, height=data.height, seed=data.seed,
|
||||
reference_images=[str(storage.to_absolute(row.path)) for row in references],
|
||||
params={"model": spec.model},
|
||||
idempotency_key=(
|
||||
f"platform-image:{owner.id}:{data.kind}:"
|
||||
f"{now_utc().isoformat()}:{data.prompt}:{','.join(map(str, data.reference_media_ids))}"
|
||||
),
|
||||
label=f"PLATFORM-{owner_type.upper()}-{owner.id}", output_path=out,
|
||||
)
|
||||
try:
|
||||
result = model_registry.generate_asset(db, request, "image", spec.provider, spec.model)
|
||||
except ProviderError as exc:
|
||||
raise HTTPException(502, f"{exc.message}({exc.code})") from exc
|
||||
kind = data.kind
|
||||
produced = Path(result.file_path)
|
||||
if not produced.exists() or produced.stat().st_size < 128:
|
||||
raise HTTPException(502, "模型未返回有效文件")
|
||||
info = storage.save_platform_local_file(
|
||||
produced, owner_type=owner_type, owner_id=owner.id, kind=kind,
|
||||
stem=f"{owner_type}-{owner.id}-{kind}-ai",
|
||||
)
|
||||
if getattr(result, "duration_ms", 0):
|
||||
info["duration_ms"] = int(result.duration_ms)
|
||||
media = storage.register_platform_media(
|
||||
db, owner_type=owner_type, owner_id=owner.id, kind=kind, path=info["path"],
|
||||
name=f"{owner.name} · AI {AssetKind.LABELS.get(kind, kind)}",
|
||||
note=f"平台资产生成:{spec.provider}/{spec.model}", source="provider", info=info,
|
||||
metadata={"generated": True, "provider": spec.provider, "model": spec.model,
|
||||
"prompt": getattr(request, "prompt", None) or getattr(request, "text", ""),
|
||||
"reference_media_ids": ([reference_media.id] if owner_type == "voice" and reference_media
|
||||
else [row.id for row in references]),
|
||||
"task_id": None},
|
||||
)
|
||||
if owner_type == "voice":
|
||||
# 音色自检:量一遍实际基频,与设定性别比对,结果进素材元数据供界面标注。
|
||||
check = timbre.check_voice(produced, owner.gender)
|
||||
produced_media_metadata(media, reference, check, text)
|
||||
db.commit()
|
||||
events.log_event(db, "platform.asset.generated",
|
||||
message=f"平台{_type_label(owner_type)}「{owner.name}」已生成"
|
||||
f"{AssetKind.LABELS.get(kind, kind)}({spec.provider}/{spec.model})",
|
||||
entity_type=f"platform_{owner_type}", entity_id=owner.id)
|
||||
return {"ok": True, "result": storage.platform_media_dict(media)}
|
||||
spec = platform_generation.preflight(db, owner_type, owner, data)
|
||||
kind = "voice_sample" if owner_type == "voice" else data.kind
|
||||
task = platform_generation.enqueue(db, owner_type=owner_type, owner=owner, spec=spec,
|
||||
data=data, asset_kind=kind)
|
||||
return {"ok": True, "result": status_service.task_dict(task)}
|
||||
|
||||
|
||||
@router.post("/api/platform/{owner_type}/{asset_id}/text-assist")
|
||||
|
|
@ -1107,7 +946,3 @@ def platform_text_assist(owner_type: str, asset_id: int, data: schemas.PlatformT
|
|||
raise HTTPException(502, "大模型未返回有效内容")
|
||||
return {"ok": True, "result": {"text": result.text.strip(), "provider": spec.provider,
|
||||
"model": spec.model}}
|
||||
|
||||
|
||||
def _type_label(owner_type: str) -> str:
|
||||
return {"human": "人物", "scene": "场景", "voice": "声线"}.get(owner_type, owner_type)
|
||||
|
|
|
|||
|
|
@ -24,8 +24,12 @@ class TaskKind:
|
|||
COMPOSE = "compose" # 按时间线合成 + 混音 + 导出
|
||||
IMPORT = "import" # 分镜/脚本导入
|
||||
ASSET = "asset" # 资产衍生(占位,供扩展)
|
||||
# 平台资产库的 AI 生成:出定妆图 / 场景图 / 试听样本。
|
||||
# 它与 ASSET 的区别只在「不属于任何项目」——正因如此才必须单独一种 kind,
|
||||
# 否则按 kind 过滤的队列视图会把平台侧的生成混进某个项目的资产任务里。
|
||||
PLATFORM_ASSET = "platform_asset"
|
||||
|
||||
ALL = [GENERATE_SHOT, VOICEOVER, QC_SHOT, COMPOSE, IMPORT, ASSET]
|
||||
ALL = [GENERATE_SHOT, VOICEOVER, QC_SHOT, COMPOSE, IMPORT, ASSET, PLATFORM_ASSET]
|
||||
|
||||
|
||||
class TaskStatus:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
"""数据库会话与基类。支持 PostgreSQL(生产)与 SQLite(本地自检)。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
|
|
@ -15,6 +16,8 @@ from sqlalchemy.pool import NullPool, StaticPool
|
|||
|
||||
from app.config import settings
|
||||
|
||||
log = logging.getLogger("nex.db")
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
|
@ -241,13 +244,18 @@ def init_db() -> None:
|
|||
创建新表,再对比 ORM 元数据与真实列;只补缺失列,不删改已有数据。
|
||||
新补列一律按 nullable 创建,便于旧数据行平滑升级。
|
||||
|
||||
建表/补列/回填三步都走 ``wait_for_lock``:抢不到文件锁时等上一个进程退出,
|
||||
建表之后的补列/放开非空/回填三步都走 ``wait_for_lock``:抢不到文件锁时等上一个进程退出,
|
||||
而不是让整个后端起不来(起不来时端口还占着,表现是「全站无响应」)。
|
||||
|
||||
``_relax_nullability`` 是这里唯一会**改已有列**的一步,因为只有它避不开:
|
||||
平台资产接入统一任务表之后 ``tasks.project_id`` 必须可空,
|
||||
而 SQLite 的 ALTER TABLE 改不了列约束——只能整表重建(数据逐列搬过去)。
|
||||
"""
|
||||
from app import models # noqa: F401 确保模型全部注册
|
||||
|
||||
wait_for_lock("建表 create_all", lambda: Base.metadata.create_all(bind=engine))
|
||||
_maintenance(_add_missing_columns, "补齐新增列")
|
||||
_maintenance(_relax_nullability, "放开新增可空列")
|
||||
_maintenance(_backfill_defaults, "回填新增列默认值")
|
||||
|
||||
|
||||
|
|
@ -269,6 +277,108 @@ def _add_missing_columns() -> None:
|
|||
conn.execute(text(f'ALTER TABLE "{table.name}" ADD COLUMN {ddl}'))
|
||||
|
||||
|
||||
def _relax_nullability() -> None:
|
||||
"""把「ORM 已放开、老库里却还是 NOT NULL」的列改成可空。
|
||||
|
||||
为什么只此一列要命:``tasks.project_id`` 建库时是必填,因为当时所有异步工作
|
||||
都挂在某个项目下;平台资产库的 AI 生成接入同一张任务表之后,它没有项目可挂
|
||||
(平台母版本就不属于任何项目)。硬塞 0/-1 会让每一条按项目统计的 SQL
|
||||
(容量、成本、流程进度)多背一个特例,所以老老实实放开约束。
|
||||
|
||||
SQLite 没有 ``ALTER COLUMN ... DROP NOT NULL``,只能整表重建:
|
||||
按 ORM 元数据建新表 → 逐列搬数据(只搬两边都有的列)→ 删旧表 → 改名 → 重建索引。
|
||||
搬完再核对行数,对不上就抛错:宁可后端起不来,也不能悄悄少几十条任务履历。
|
||||
"""
|
||||
inspector = inspect(engine)
|
||||
existing_tables = set(inspector.get_table_names())
|
||||
dialect = engine.dialect.name
|
||||
for table in Base.metadata.sorted_tables:
|
||||
if table.name not in existing_tables:
|
||||
continue
|
||||
db_columns = {column["name"]: column for column in inspector.get_columns(table.name)}
|
||||
stale = [column.name for column in table.columns
|
||||
if column.nullable and column.name in db_columns
|
||||
and not db_columns[column.name]["nullable"] and not column.primary_key]
|
||||
if not stale:
|
||||
continue
|
||||
if dialect == "sqlite":
|
||||
with engine.begin() as conn:
|
||||
before = conn.execute(text(f'SELECT COUNT(*) FROM "{table.name}"')).scalar() or 0
|
||||
_rebuild_sqlite_table(conn, inspector, table, set(db_columns))
|
||||
after = conn.execute(text(f'SELECT COUNT(*) FROM "{table.name}"')).scalar() or 0
|
||||
if before != after:
|
||||
raise RuntimeError(f"重建 {table.name} 后行数不一致({before} → {after}),已中止启动")
|
||||
log.info("已放开 %s.%s 的 NOT NULL(整表重建,%d 行数据已搬运)",
|
||||
table.name, "、".join(stale), after)
|
||||
elif dialect in ("postgresql", "mysql", "mariadb"):
|
||||
with engine.begin() as conn:
|
||||
for name in stale:
|
||||
conn.execute(text(f'ALTER TABLE "{table.name}" ALTER COLUMN "{name}" DROP NOT NULL'))
|
||||
log.info("已放开 %s.%s 的 NOT NULL", table.name, "、".join(stale))
|
||||
else:
|
||||
log.warning("方言 %s 未实现放开非空,%s.%s 仍是 NOT NULL",
|
||||
dialect, table.name, "、".join(stale))
|
||||
|
||||
|
||||
def _literal(column) -> str:
|
||||
"""按列类型给一个「空值当没有」的兜底字面量(与 Python 侧 default 同语义)。"""
|
||||
from sqlalchemy import Boolean, Date, DateTime, Float, Integer, Interval, Numeric, Time
|
||||
|
||||
if isinstance(column.type, Interval):
|
||||
return "'00:00:00'"
|
||||
if isinstance(column.type, (DateTime, Date, Time)):
|
||||
return "'1970-01-01 00:00:00'"
|
||||
if isinstance(column.type, Boolean):
|
||||
return "0"
|
||||
if isinstance(column.type, (Integer, Float, Numeric)):
|
||||
return "0"
|
||||
if column.type.__class__.__name__ in ("JSON",):
|
||||
return "'{}'"
|
||||
return "''"
|
||||
|
||||
|
||||
def _rebuild_sqlite_table(conn, inspector, table, db_columns: set[str]) -> None:
|
||||
"""按 ORM 元数据重建一张 SQLite 表(列定义以模型为准,数据逐列搬运)。"""
|
||||
from sqlalchemy.schema import CreateIndex, CreateTable
|
||||
|
||||
temp = f"_nex_migrate_{table.name}"
|
||||
# 镜像表借 Base.metadata 建:新建的 MetaData 里没有 projects/shots,
|
||||
# 编译外键时会报 NoReferencedTableError。用完即摘,别把临时表留在元数据里。
|
||||
mirror = table.to_metadata(Base.metadata, name=temp)
|
||||
# 只搬两边都有的列:库里多出来的历史列(早已从模型里删掉)不值得再留一份空壳
|
||||
db_null = {column["name"] for column in inspector.get_columns(table.name) if column["nullable"]}
|
||||
shared = [column.name for column in table.columns if column.name in db_columns]
|
||||
quote = lambda name: '"' + name.replace('"', '""') + '"'
|
||||
|
||||
def source(column) -> str:
|
||||
"""搬运表达式:刚补进来的列在旧行里全是 NULL,而新表按模型是 NOT NULL。
|
||||
|
||||
``_add_missing_columns`` 只能把新列加成可空(SQLite 的限制),
|
||||
所以这里必须给个兜底值,否则「补列 → 重建」这两步自己打架:
|
||||
一条 NOT NULL 的老库记录搬进新表就会当场抛 IntegrityError。
|
||||
"""
|
||||
name = quote(column.name)
|
||||
if column.nullable or column.name not in db_null:
|
||||
return name
|
||||
return f"COALESCE({name}, {_literal(column)})"
|
||||
|
||||
try:
|
||||
conn.execute(text(str(CreateTable(mirror).compile(dialect=engine.dialect))))
|
||||
finally:
|
||||
Base.metadata.remove(mirror)
|
||||
columns = ", ".join(quote(column.name) for column in table.columns
|
||||
if column.name in db_columns)
|
||||
values = ", ".join(source(column) for column in table.columns if column.name in db_columns)
|
||||
conn.execute(text(f'INSERT INTO {quote(temp)} ({columns}) '
|
||||
f'SELECT {values} FROM {quote(table.name)}'))
|
||||
conn.execute(text(f'DROP TABLE {quote(table.name)}'))
|
||||
# 改名放在建索引之前:索引挂在表对象上,DDL 里写的是正式表名,
|
||||
# 先建就会指向还不存在的表。
|
||||
conn.execute(text(f'ALTER TABLE {quote(temp)} RENAME TO {quote(table.name)}'))
|
||||
for index in table.indexes:
|
||||
conn.execute(text(str(CreateIndex(index).compile(dialect=engine.dialect))))
|
||||
|
||||
|
||||
def _backfill_defaults() -> None:
|
||||
"""只回填代码依赖的新增标量列;JSON 列由读取侧继续兼容 null。"""
|
||||
statements = [
|
||||
|
|
@ -278,6 +388,9 @@ def _backfill_defaults() -> None:
|
|||
"UPDATE characters SET platform_source_type='' WHERE platform_source_type IS NULL",
|
||||
"UPDATE scenes SET platform_source_type='' WHERE platform_source_type IS NULL",
|
||||
"UPDATE voice_assets SET platform_source_type='' WHERE platform_source_type IS NULL",
|
||||
# 统一任务表新列:旧行没有归属与状态说明,补齐空串,读取侧不必再判 null
|
||||
"UPDATE tasks SET owner_type='' WHERE owner_type IS NULL",
|
||||
"UPDATE tasks SET message='' WHERE message IS NULL",
|
||||
]
|
||||
with engine.begin() as conn:
|
||||
for sql in statements:
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ from app.models import Asset, Character, Project, Scene, Shot, Task, Timeline, V
|
|||
from app.providers import registry as model_registry
|
||||
from app.providers.base import ProviderError
|
||||
from app.services import audio, compose, costing, dispatch, events, ffmpeg as ff
|
||||
from app.services import progress
|
||||
from app.services import prompt as prompt_service
|
||||
from app.services import quality, status as status_service, storage, timbre
|
||||
|
||||
|
|
@ -169,8 +170,10 @@ def handle_generate_shot(task_id: int) -> dict:
|
|||
进度条,也不该把它倒回去——倒回去看起来像任务被打回起点,用户会以为卡住了。
|
||||
"""
|
||||
value = float(ratio)
|
||||
progress = None if value < 0 else 0.1 + 0.6 * max(0.0, min(1.0, value))
|
||||
dispatch.heartbeat(db, task, progress=progress, note=note)
|
||||
# 别名叫 _pct:模块里另有一个 services.progress(run 里绑任务上下文),
|
||||
# 局部变量压住同名导入,将来在闭包里调 progress.report() 会撞成 AttributeError
|
||||
_pct = None if value < 0 else 0.1 + 0.6 * max(0.0, min(1.0, value))
|
||||
dispatch.heartbeat(db, task, progress=_pct, note=note)
|
||||
db.commit()
|
||||
|
||||
started = time.monotonic()
|
||||
|
|
@ -692,6 +695,38 @@ def handle_asset(task_id: int) -> dict:
|
|||
db.close()
|
||||
|
||||
|
||||
def handle_platform_asset(task_id: int) -> dict:
|
||||
"""平台资产库的 AI 生成(定妆图 / 场景图 / 试听样本)。
|
||||
|
||||
执行体与项目侧任务完全同构:认领 → 跑模型 → 落素材 → 回写任务行。
|
||||
真正的业务在 services/platform_generation(那里同时被接口层复用做参数校验),
|
||||
这一层只负责把异常收口成「重试或失败」,绝不向上抛给执行后端。
|
||||
"""
|
||||
from app.services import platform_generation
|
||||
|
||||
db, task = _load(task_id)
|
||||
if db is None or task is None:
|
||||
return {"skipped": "任务不存在"}
|
||||
if not _begin(db, task):
|
||||
return {"skipped": "任务已处于终态"}
|
||||
try:
|
||||
result = platform_generation.generate(db, task)
|
||||
if _was_cancelled(db, task.id):
|
||||
# 出图已经落盘,但用户明确取消过:任务行保持「已取消」,别再写成成功
|
||||
return {"cancelled": True, "media": result.get("media")}
|
||||
media = result.get("media") or {}
|
||||
dispatch.mark_succeeded(db, task, result=result, output_path=media.get("path") or None)
|
||||
# 落定之后把「此刻在做什么」换成结果一句话:执行体最后写的那条进度是
|
||||
# 「算力已就绪:模型出图中」,留在已完成那一行上就成了自相矛盾的回显。
|
||||
task.message = platform_generation.landed_message(media)
|
||||
db.commit()
|
||||
return result
|
||||
except Exception as exc:
|
||||
return _finish_failure(db, task, exc, shot=None)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
HANDLERS = {
|
||||
TaskKind.GENERATE_SHOT: handle_generate_shot,
|
||||
TaskKind.VOICEOVER: handle_voiceover,
|
||||
|
|
@ -699,6 +734,7 @@ HANDLERS = {
|
|||
TaskKind.COMPOSE: handle_compose,
|
||||
TaskKind.IMPORT: handle_import,
|
||||
TaskKind.ASSET: handle_asset,
|
||||
TaskKind.PLATFORM_ASSET: handle_platform_asset,
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -739,6 +775,10 @@ def run(kind: str, task_id: int) -> dict:
|
|||
_reconcile_project(task_id)
|
||||
return {"failed": True}
|
||||
try:
|
||||
# progress.bind 让埋在适配器深处的代码(providers/comfy_common.gpu_slot)
|
||||
# 能把「排在别人后面」和「模型正在出图」写回任务行——没有这一句,
|
||||
# 动辄几分钟的任务在界面上就只有一个转圈的图标。
|
||||
progress.bind(task_id)
|
||||
with _Heartbeat(task_id):
|
||||
return handler(task_id)
|
||||
except Exception as exc: # handler 内部已收口,这里只做最后兜底
|
||||
|
|
@ -749,4 +789,5 @@ def run(kind: str, task_id: int) -> dict:
|
|||
db.close()
|
||||
return {"failed": True, "error": str(exc)}
|
||||
finally:
|
||||
progress.unbind()
|
||||
_reconcile_project(task_id) # 队列动过,项目阶段就得跟上
|
||||
|
|
|
|||
|
|
@ -51,6 +51,13 @@ def asset(self, task_id: int) -> dict:
|
|||
return handlers.run("asset", task_id)
|
||||
|
||||
|
||||
@celery_app.task(name="app.jobs.platform_asset", bind=True, acks_late=True)
|
||||
def platform_asset(self, task_id: int) -> dict:
|
||||
from app.jobs import handlers
|
||||
|
||||
return handlers.run("platform_asset", task_id)
|
||||
|
||||
|
||||
@celery_app.task(name="app.jobs.dispatch_due")
|
||||
def dispatch_due() -> dict:
|
||||
"""周期调度器:扫描到期任务并按模型限流额度派发。"""
|
||||
|
|
|
|||
|
|
@ -12,21 +12,38 @@ from app.db import Base
|
|||
|
||||
|
||||
class Task(TimestampMixin, Base):
|
||||
"""统一任务表:所有异步工作(生成/配音/质检/合成/导入)都走同一编排。"""
|
||||
"""统一任务表:**凡是「要跑模型、要等一会儿」的活都在这一张表里**。
|
||||
|
||||
项目侧的生成/配音/质检/合成/导入走它,平台资产库里「点一下 AI 生成定妆图」
|
||||
也走它(``kind=platform_asset``、``project_id`` 为空、``owner_type/owner_id``
|
||||
指向平台母版)。这样「平台此刻在忙什么」只需要问一处,
|
||||
顶栏的活动面板、限流派发、失败重试、断点续跑、成本台账全都自动对平台任务生效,
|
||||
不必为平台侧另立一套并行体系(那套体系当年就是为了让用户看得见进度才建的)。
|
||||
"""
|
||||
|
||||
__tablename__ = "tasks"
|
||||
__table_args__ = (Index("ix_tasks_status_priority", "status", "priority", "id"),
|
||||
Index("ix_tasks_provider_status", "provider", "status"))
|
||||
Index("ix_tasks_provider_status", "provider", "status"),
|
||||
Index("ix_tasks_owner_status", "owner_type", "owner_id", "status"))
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
||||
# 可空:平台资产母版不属于任何项目(老库里这一列是 NOT NULL,
|
||||
# 由 db._relax_nullability 在启动时改成可空,不需要删库重建)
|
||||
project_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("projects.id", ondelete="CASCADE"), nullable=True, index=True)
|
||||
shot_id: Mapped[int | None] = mapped_column(ForeignKey("shots.id", ondelete="CASCADE"), nullable=True, index=True)
|
||||
timeline_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
||||
# 非项目归属(平台母版):卡片要按「这条资产正在生成」亮状态,重复点击要按它去重
|
||||
owner_type: Mapped[str] = mapped_column(String(20), default="", index=True)
|
||||
owner_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
kind: Mapped[str] = mapped_column(String(30), default=TaskKind.GENERATE_SHOT, index=True)
|
||||
name: Mapped[str] = mapped_column(String(200), default="")
|
||||
stage: Mapped[str] = mapped_column(String(40), default="dispatch")
|
||||
|
||||
status: Mapped[str] = mapped_column(String(30), default=TaskStatus.QUEUED, index=True)
|
||||
# 此刻在做什么的一句话说明(「排队中:等待算力空闲」「模型出图中」)。
|
||||
# 只有进度条时用户分不清「模型在算」和「排在别人后面」,只能一遍遍刷新页面。
|
||||
message: Mapped[str] = mapped_column(String(400), default="")
|
||||
priority: Mapped[int] = mapped_column(Integer, default=5)
|
||||
progress: Mapped[float] = mapped_column(Float, default=0.0)
|
||||
|
||||
|
|
|
|||
|
|
@ -68,13 +68,33 @@ def lock_keys(config: dict | None, base_url: str) -> list[str]:
|
|||
return keys
|
||||
|
||||
|
||||
def slot_note(message: str) -> None:
|
||||
"""把算力锁上的等待写成任务行上的一行状态;不在任务上下文里时静默返回。
|
||||
|
||||
延迟导入:本模块被各适配器**顶层**导入,而 providers 不该在导入期依赖 services
|
||||
(services 反过来要读注册表)。这一句拿不到也不影响出图,吞掉即可。
|
||||
"""
|
||||
try:
|
||||
from app.services import progress
|
||||
|
||||
progress.report(message)
|
||||
except Exception: # noqa: BLE001 - 状态回写不该弄挂生成
|
||||
pass
|
||||
|
||||
|
||||
@contextmanager
|
||||
def gpu_slot(config: dict, base_url: str) -> Iterator[None]:
|
||||
# 锁键先排序再逐个获取,保证全局一致的加锁顺序,避免死锁。
|
||||
locks = [resource_lock(key) for key in sorted(lock_keys(config, base_url))]
|
||||
with ExitStack() as stack:
|
||||
for lock in locks:
|
||||
# 排队这件事必须说给用户听:一次出图动辄十几分钟,而 video / image / tts
|
||||
# 打的是同一台 ComfyUI,在这把锁上无声串行(见 lock_keys)。以前界面上
|
||||
# 只有「进行中」三个字在转,用户分不清「模型正在算」还是「排在别人后面」,
|
||||
# 只能一遍遍刷新页面。
|
||||
slot_note("排队中:等待算力空闲")
|
||||
stack.enter_context(lock)
|
||||
slot_note("算力已就绪:模型出图中")
|
||||
yield
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ from app.models import ModelSpec, Task
|
|||
# 找不到模型规格时,用任务类型推断媒体类型。资产任务默认按图像处理;
|
||||
# 若使用 TTS 生成声线样本,任务行仍会带上已解析的具体模型,因此会优先命中规格表。
|
||||
KIND_MEDIA_TYPE = {TaskKind.GENERATE_SHOT: "video", TaskKind.VOICEOVER: "tts",
|
||||
TaskKind.ASSET: "image"}
|
||||
TaskKind.ASSET: "image", TaskKind.PLATFORM_ASSET: "image"}
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
|
|||
|
|
@ -343,52 +343,104 @@ def dispatch_due(db: Session, *, limit: int | None = None, project_id: int | Non
|
|||
blocked[key] = reason
|
||||
if slots[key] <= 0:
|
||||
continue
|
||||
# 先「原子认领」再提交执行:条件更新只有一个会话能成功,
|
||||
# 因此守护线程、drain、人工 dispatch 并发时同一任务绝不会被跑两次。
|
||||
claimed = db.execute(update(Task)
|
||||
.where(Task.id == task.id,
|
||||
Task.status.in_([TaskStatus.QUEUED, TaskStatus.RETRYING]))
|
||||
# worker_id 先清空:认领到执行体真正起跑之间有个短窗口,
|
||||
# 期间它还属于「没有 runner」而不是「上一代进程的僵尸」,
|
||||
# 否则断点续跑可能把刚认领的任务再捞回去跑两遍。
|
||||
.values(status=TaskStatus.RUNNING, started_at=now, heartbeat_at=now,
|
||||
attempts=Task.attempts + 1, next_retry_at=None, error_message="",
|
||||
worker_id="", execution_backend=execution.backend_name())
|
||||
.execution_options(synchronize_session=False)).rowcount
|
||||
db.commit()
|
||||
if not claimed:
|
||||
if not claim_task(db, task, now=now):
|
||||
continue # 已被其它派发者认领,跳过
|
||||
try:
|
||||
handle = execution.submit(task)
|
||||
except Exception as exc: # 后端不可用 → 归还队列并稍后重试
|
||||
log.exception("任务派发失败 task=%s", task.id)
|
||||
task = db.get(Task, task.id)
|
||||
task.status = TaskStatus.QUEUED
|
||||
task.attempts = max(0, (task.attempts or 1) - 1)
|
||||
task.started_at = None
|
||||
task.heartbeat_at = None
|
||||
task.error_message = f"任务派发失败:{exc}"[:4000]
|
||||
task.next_retry_at = now + timedelta(seconds=30)
|
||||
events.log_event(db, "task.dispatch_failed", project_id=task.project_id, task_id=task.id,
|
||||
level="error", message=task.error_message, entity_type="task", entity_id=task.id)
|
||||
db.commit()
|
||||
continue
|
||||
handle = submit_claimed(db, task, now=now)
|
||||
if not handle:
|
||||
continue # 派发失败,已归还队列
|
||||
slots[key] -= 1
|
||||
task = db.get(Task, task.id)
|
||||
task.celery_task_id = handle
|
||||
task.execution_backend = execution.backend_name()
|
||||
task.queued_at = task.queued_at or now
|
||||
mark_dispatched(db, task, handle, now=now)
|
||||
dispatched.append({"task_id": task.id, "kind": task.kind, "shot_id": task.shot_id,
|
||||
"provider": task.provider, "handle": handle})
|
||||
events.log_event(db, "task.dispatched", project_id=task.project_id, task_id=task.id,
|
||||
shot_id=task.shot_id, message=f"任务已派发到 {task.execution_backend} 执行后端",
|
||||
entity_type="task", entity_id=task.id,
|
||||
data={"provider": task.provider, "model": task.model, "handle": handle})
|
||||
db.commit()
|
||||
return {"dispatched": dispatched, "count": len(dispatched), "blocked": [
|
||||
{"provider_model": k, "reason": v} for k, v in blocked.items()]}
|
||||
|
||||
|
||||
def claim_task(db: Session, task: Task, *, now: datetime | None = None) -> bool:
|
||||
"""原子认领一条排队中的任务:条件更新只有一个会话能成功。
|
||||
|
||||
守护线程、drain、人工 dispatch、平台侧「点一下生成」四条路都会走到这里,
|
||||
因此同一任务绝不会被跑两次——这是「不重复烧 GPU」的唯一保证,别换成先读后写。
|
||||
"""
|
||||
now = now or now_utc()
|
||||
claimed = db.execute(update(Task)
|
||||
.where(Task.id == task.id,
|
||||
Task.status.in_([TaskStatus.QUEUED, TaskStatus.RETRYING]))
|
||||
# worker_id 先清空:认领到执行体真正起跑之间有个短窗口,
|
||||
# 期间它还属于「没有 runner」而不是「上一代进程的僵尸」,
|
||||
# 否则断点续跑可能把刚认领的任务再捞回去跑两遍。
|
||||
.values(status=TaskStatus.RUNNING, started_at=now, heartbeat_at=now,
|
||||
attempts=Task.attempts + 1, next_retry_at=None, error_message="",
|
||||
worker_id="", execution_backend=execution.backend_name())
|
||||
.execution_options(synchronize_session=False)).rowcount
|
||||
db.commit()
|
||||
return bool(claimed)
|
||||
|
||||
|
||||
def submit_claimed(db: Session, task: Task, *, now: datetime | None = None) -> str:
|
||||
"""把已认领的任务交给执行后端,返回句柄;派发失败时归还队列并稍后重试,返回空串。"""
|
||||
now = now or now_utc()
|
||||
try:
|
||||
return execution.submit(task)
|
||||
except Exception as exc: # 后端不可用 → 归还队列并稍后重试
|
||||
log.exception("任务派发失败 task=%s", task.id)
|
||||
task = db.get(Task, task.id)
|
||||
task.status = TaskStatus.QUEUED
|
||||
task.attempts = max(0, (task.attempts or 1) - 1)
|
||||
task.started_at = None
|
||||
task.heartbeat_at = None
|
||||
task.error_message = f"任务派发失败:{exc}"[:4000]
|
||||
task.next_retry_at = now + timedelta(seconds=30)
|
||||
events.log_event(db, "task.dispatch_failed", project_id=task.project_id, task_id=task.id,
|
||||
level="error", message=task.error_message, entity_type="task", entity_id=task.id)
|
||||
db.commit()
|
||||
return ""
|
||||
|
||||
|
||||
def mark_dispatched(db: Session, task: Task, handle: str, *, now: datetime | None = None) -> Task:
|
||||
"""记下执行句柄并留一条事件(不提交:批量派发时由调用方统一收口)。"""
|
||||
now = now or now_utc()
|
||||
task = db.get(Task, task.id)
|
||||
task.celery_task_id = handle
|
||||
task.execution_backend = execution.backend_name()
|
||||
task.queued_at = task.queued_at or now
|
||||
events.log_event(db, "task.dispatched", project_id=task.project_id, task_id=task.id,
|
||||
shot_id=task.shot_id, message=f"任务已派发到 {task.execution_backend} 执行后端",
|
||||
entity_type="task", entity_id=task.id,
|
||||
data={"provider": task.provider, "model": task.model, "handle": handle})
|
||||
return task
|
||||
|
||||
|
||||
def dispatch_task(db: Session, task: Task) -> dict:
|
||||
"""立刻派发**这一条**任务:有额度当场起跑,没额度就留在队列里等守护线程。
|
||||
|
||||
平台资产生成走这里。用户点完按钮不该干等 dispatch_interval_seconds(默认 5s)
|
||||
才知道排上了没有;但也不能绕过限流插队——出图、出片、配音打的是同一台
|
||||
ComfyUI,插队的代价由正在跑的项目买单。所以这里查同样的 headroom、
|
||||
走同样的原子认领,只是不排队等下一轮扫描。
|
||||
返回值只用于日志与接口提示,真正的进度一律以任务行为准。
|
||||
"""
|
||||
now = now_utc()
|
||||
available, reason = capacity.headroom(db, task.provider or "", task.model or None)
|
||||
if available <= 0:
|
||||
return {"dispatched": False, "queued": True, "task_id": task.id,
|
||||
"status": task.status, "reason": reason or "算力已满,已进入队列"}
|
||||
if not claim_task(db, task, now=now):
|
||||
fresh = db.get(Task, task.id)
|
||||
return {"dispatched": False, "queued": True, "task_id": task.id,
|
||||
"status": fresh.status, "reason": "任务已被其它派发者认领"}
|
||||
handle = submit_claimed(db, task, now=now)
|
||||
if not handle:
|
||||
fresh = db.get(Task, task.id)
|
||||
return {"dispatched": False, "queued": True, "task_id": task.id,
|
||||
"status": fresh.status, "reason": fresh.error_message or "任务派发失败"}
|
||||
fresh = mark_dispatched(db, task, handle, now=now)
|
||||
db.commit()
|
||||
return {"dispatched": True, "queued": True, "task_id": fresh.id,
|
||||
"status": fresh.status, "handle": handle, "reason": ""}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 状态回写
|
||||
def _write_through(db: Session, *, commit: bool = False, attempts: int = 4) -> None:
|
||||
"""把状态回写落库,遇到 SQLite 抢锁先自己退避重试。
|
||||
|
|
|
|||
|
|
@ -23,10 +23,12 @@ CELERY_TASK_NAMES = {
|
|||
"compose": "app.jobs.compose",
|
||||
"import": "app.jobs.import_package",
|
||||
"asset": "app.jobs.asset",
|
||||
"platform_asset": "app.jobs.platform_asset",
|
||||
}
|
||||
|
||||
# 平台资产生成(出图/出试听)与导入同属 io:都不占大 GPU 时长,但都会被算力锁挡住
|
||||
QUEUES = {"generate_shot": "generate", "voiceover": "audio", "qc_shot": "qc", "compose": "compose",
|
||||
"import": "io", "asset": "io"}
|
||||
"import": "io", "asset": "io", "platform_asset": "io"}
|
||||
|
||||
|
||||
def celery_app():
|
||||
|
|
|
|||
|
|
@ -0,0 +1,405 @@
|
|||
r"""平台资产库的 AI 生成:登记成任务 → 统一调度执行 → 进度写回任务行。
|
||||
|
||||
为什么要走任务表(2026-11 · 504 那一串):
|
||||
出图实测 46~49s(docs/MiniMaxH3_Spark_性能诊断与优化方案.md:681),反代
|
||||
``proxy_read_timeout`` 出厂 60s,余量只有十几秒;同卡一排队就直接 504。
|
||||
而 504 只是**看起来**失败了——出图线程并没有被取消,图照样落盘,
|
||||
用户看不见结果,只能手动刷新页面去猜它到底成没成。
|
||||
现在接口只做「校验 + 登记 + 派发」,当场 202 返回任务号,
|
||||
进度、产物、失败原因全都在 ``tasks`` 那一行里,换设备、刷新页面都接着看得到。
|
||||
|
||||
为什么并入 ``tasks`` 而不是另立一张操作表:
|
||||
「所有要跑模型、要等一会儿的活」本该只有一个地方能问。并表之后
|
||||
限流派发(capacity.headroom)、失败重试、断点续跑(resume_interrupted)、
|
||||
心跳与 stale、成本与事件台账,平台侧生成全部自动继承,
|
||||
不必再维护第二套进行中转圈。代价只是 ``project_id`` 变成可空,
|
||||
由 ``db._relax_nullability`` 在启动时原地放开。
|
||||
|
||||
执行体在 ``jobs/handlers.handle_platform_asset`` 里,与项目侧任务同一条通道;
|
||||
深处的算力排队说明由 ``services/progress`` 写回(见 providers/comfy_common.gpu_slot)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app import schemas
|
||||
from app.constants import AssetKind, TaskKind, TaskStatus
|
||||
from app.models import (PlatformCharacter, PlatformMedia, PlatformScene, PlatformVoice,
|
||||
Project, Task, now_utc)
|
||||
from app.providers import registry as model_registry
|
||||
from app.providers.base import AssetGenRequest, ProviderError, VoiceGenRequest
|
||||
from app.services import costing, dispatch, events, progress, storage, timbre
|
||||
|
||||
log = logging.getLogger("nex.platform_generation")
|
||||
|
||||
#: 一条平台生成任务最多能被写成多少次状态说明(message 列宽 400)
|
||||
MAX_LABEL_CHARS = 200
|
||||
|
||||
|
||||
def landed_message(media: dict) -> str:
|
||||
"""任务成功落定时要写进 ``tasks.message`` 的那句话。
|
||||
|
||||
``progress.report`` 写的是「此刻在做什么」,成功之后没人覆盖它,
|
||||
于是顶栏「平台动态 · 最近落定」会出现「已完成 + 算力已就绪:模型出图中」
|
||||
这种自相矛盾的回显(状态胶囊说完了,正文还在说进行中)。
|
||||
"""
|
||||
bits = [f"已生成{media.get('kind_label') or '新素材'}"]
|
||||
if media.get("version"):
|
||||
bits.append(f"第 {media['version']} 版")
|
||||
if media.get("duration_ms"):
|
||||
bits.append(f"{round(media['duration_ms'] / 1000, 1)}s")
|
||||
elif media.get("size_bytes"):
|
||||
bits.append(f"{round(media['size_bytes'] / 1024 / 1024, 2)} MB")
|
||||
return " · ".join(bits)[:MAX_LABEL_CHARS]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 母版与素材的公共读法
|
||||
def normalize_type(owner_type: str) -> str:
|
||||
return {
|
||||
"characters": "human", "character": "human", "human": "human", "humans": "human",
|
||||
"scene": "scene", "scenes": "scene", "voice": "voice", "voices": "voice",
|
||||
}.get(owner_type, owner_type)
|
||||
|
||||
|
||||
def get_owner(db: Session, owner_type: str, owner_id: int):
|
||||
owner_type = normalize_type(owner_type)
|
||||
if owner_type == "human":
|
||||
row = db.get(PlatformCharacter, owner_id)
|
||||
elif owner_type == "scene":
|
||||
row = db.get(PlatformScene, owner_id)
|
||||
elif owner_type == "voice":
|
||||
row = db.get(PlatformVoice, owner_id)
|
||||
else:
|
||||
raise HTTPException(400, f"不支持的平台资产类型:{owner_type}")
|
||||
if row is None:
|
||||
raise HTTPException(404, "平台资产不存在")
|
||||
return row
|
||||
|
||||
|
||||
def type_label(owner_type: str) -> str:
|
||||
return {"human": "人物", "scene": "场景", "voice": "声线"}.get(owner_type, owner_type)
|
||||
|
||||
|
||||
def resolve_reference_media(db: Session, owner_type: str, owner_id: int, ids: list[int],
|
||||
*, kind: str) -> list[PlatformMedia]:
|
||||
"""校验平台生成参考素材只能来自当前资产,避免跨资产误读本地路径。"""
|
||||
rows: list[PlatformMedia] = []
|
||||
seen: set[int] = set()
|
||||
image_suffixes = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff"}
|
||||
audio_suffixes = {".wav", ".mp3", ".m4a", ".aac", ".flac", ".ogg", ".opus"}
|
||||
for media_id in ids:
|
||||
if media_id in seen:
|
||||
continue
|
||||
seen.add(media_id)
|
||||
row = db.get(PlatformMedia, media_id)
|
||||
if (row is None or row.owner_type != owner_type or row.owner_id != owner_id):
|
||||
raise HTTPException(400, "参考素材不属于当前平台资产")
|
||||
if not storage.exists(row.path):
|
||||
raise HTTPException(400, "参考素材文件不存在,请重新上传")
|
||||
suffix = Path(row.path).suffix.lower()
|
||||
if kind == "image" and suffix not in image_suffixes:
|
||||
raise HTTPException(400, "图像参考素材必须是图片文件")
|
||||
if kind == "audio" and suffix not in audio_suffixes:
|
||||
raise HTTPException(400, "音色参考素材必须是音频文件")
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
|
||||
def build_owner_prompt(project: Project | None, owner_type: str, owner, kind: str, custom: str) -> str:
|
||||
if custom.strip():
|
||||
return custom.strip()
|
||||
bits = []
|
||||
if owner_type == "human":
|
||||
bits = [f"{owner.name},电影级角色设定图", owner.appearance, owner.persona_prompt,
|
||||
"五官、发型、体型、服装与配饰保持一致,正面清晰,材质细节可信"]
|
||||
elif owner_type == "scene":
|
||||
bits = [f"{owner.name},电影级场景概念图", owner.prompt or owner.description,
|
||||
owner.lighting, owner.time_of_day, owner.palette,
|
||||
"空间结构清晰,透视准确,建筑与陈设保持一致"]
|
||||
if project:
|
||||
bits.append(project.style_guide)
|
||||
return "。".join(str(part).strip() for part in bits if part)
|
||||
|
||||
|
||||
def resolve_spec(db: Session, owner_type: str, data: schemas.PlatformGenerate):
|
||||
"""按资产类型解析这次生成实际会派给哪个模型(留空即跟随平台默认)。"""
|
||||
if owner_type == "voice":
|
||||
spec, _adapter = model_registry.resolve_voice(db, data.provider or None, data.model or None)
|
||||
else:
|
||||
spec, _adapter = model_registry.resolve_asset(db, "image",
|
||||
data.provider or None, data.model or None)
|
||||
return spec
|
||||
|
||||
|
||||
def resolve_voice_reference(db: Session, owner, spec,
|
||||
data: schemas.PlatformGenerate) -> tuple[PlatformMedia | None, dict]:
|
||||
"""把「这次生成会拿到谁的嗓子」算出来:参考音频决定音色,表单里的性别只是元数据。"""
|
||||
reference_ids = list(data.reference_media_ids)
|
||||
if data.reference_media_id:
|
||||
reference_ids.append(data.reference_media_id)
|
||||
references = resolve_reference_media(db, "voice", owner.id, reference_ids, kind="audio")
|
||||
reference_media = references[0] if references else None
|
||||
reference = timbre.resolve_reference(
|
||||
db, owner,
|
||||
explicit=str(storage.to_absolute(reference_media.path)) if reference_media else "",
|
||||
clone=timbre.clone_model(spec), instruct=timbre.instruct_model(spec))
|
||||
return reference_media, reference
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 同步校验
|
||||
def preflight(db: Session, owner_type: str, owner, data: schemas.PlatformGenerate):
|
||||
"""开工前能当场说清的错误,绝不留到五分钟以后。
|
||||
|
||||
异步化之后接口立刻返回,如果连「没填试听文本」都要等轮询才发现,
|
||||
用户面对的是一个跑完了才知道白跑的进度条。所以这些**便宜且确定**的校验
|
||||
留在请求里同步做(422/409 原样吐给表单),耗时部分才交给后台。
|
||||
后台执行时会把同样的校验再跑一遍:排到 GPU 时模型可能已被停用、
|
||||
参考音频可能已被删掉,以及时报错的是供应商而不是表单。
|
||||
"""
|
||||
spec = resolve_spec(db, owner_type, data)
|
||||
if not spec.enabled:
|
||||
raise HTTPException(409, f"模型 {spec.provider}/{spec.model} 已停用")
|
||||
if owner_type != "voice":
|
||||
return spec
|
||||
if not (data.sample_text or "").strip():
|
||||
raise HTTPException(422, "请先填写试听文本:它是模型要朗读的台词,"
|
||||
"系统不会用「音色描述」代替")
|
||||
# 嗓子来源必须当场说清:克隆模型缺参考音频时会静默回退到模型默认参考(女声),
|
||||
# 男声声线就这样被生成成了女声——等四十秒后试听才发现,等于白跑一轮。
|
||||
_media, reference = resolve_voice_reference(db, owner, spec, data)
|
||||
if not reference["path"] and reference.get("source") == "none":
|
||||
raise HTTPException(409, reference["note"])
|
||||
return spec
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 登记与执行
|
||||
def active_task(db: Session, owner_type: str, owner_id: int, asset_kind: str) -> Task | None:
|
||||
"""这条母版的这一类素材是否已经有生成任务在跑(挡连点:三次点击只该出一张图)。"""
|
||||
rows = db.scalars(select(Task).where(
|
||||
Task.owner_type == owner_type, Task.owner_id == owner_id,
|
||||
Task.kind == TaskKind.PLATFORM_ASSET, Task.status.in_(TaskStatus.ACTIVE)
|
||||
).order_by(Task.id)).all()
|
||||
for row in rows:
|
||||
if str((row.payload_json or {}).get("asset_kind") or "") == asset_kind:
|
||||
return row
|
||||
return None
|
||||
|
||||
|
||||
def enqueue(db: Session, *, owner_type: str, owner, spec, data: schemas.PlatformGenerate,
|
||||
asset_kind: str) -> Task:
|
||||
"""登记一条平台生成任务并立刻尝试派发;返回任务行(202 的响应体就是它)。"""
|
||||
running = active_task(db, owner_type, owner.id, asset_kind)
|
||||
if running is not None:
|
||||
raise HTTPException(409, f"「{owner.name}」的{AssetKind.LABELS.get(asset_kind, asset_kind)}"
|
||||
f"正在生成中(任务 #{running.id}),完成前不能重复发起")
|
||||
label = f"生成「{owner.name}」的{AssetKind.LABELS.get(asset_kind, asset_kind)}"[:MAX_LABEL_CHARS]
|
||||
cost = (costing.estimate_voice_cost(spec, len((data.sample_text or "").strip()))
|
||||
if owner_type == "voice" else 0.0)
|
||||
task = Task(
|
||||
project_id=None, shot_id=None, kind=TaskKind.PLATFORM_ASSET,
|
||||
name=label, stage="platform_generate", status=TaskStatus.QUEUED,
|
||||
priority=5, provider=spec.provider, model=spec.model,
|
||||
mode="voice" if owner_type == "voice" else data.kind,
|
||||
owner_type=owner_type, owner_id=owner.id,
|
||||
# 同一母版同一素材类型的重复点击靠这条键挡,不给它配项目侧那套镜头幂等语义
|
||||
idempotency_key=f"platform:{owner_type}:{owner.id}:{asset_kind}:{int(now_utc().timestamp())}",
|
||||
# 只留一次自动重试:连不上算力这类瞬时故障值得再试一把(生成失败没有产物,
|
||||
# 不存在重复扣费);参数与配置错误在 generate 里标了 retryable=False,当场判死。
|
||||
max_attempts=2, payload_json=_payload(owner_type, owner, asset_kind, data),
|
||||
cost=cost, queued_at=now_utc(), message="排队中:等待调度")
|
||||
db.add(task)
|
||||
db.flush()
|
||||
events.log_event(db, "task.queued", project_id=None, task_id=task.id,
|
||||
message=f"平台{type_label(owner_type)}「{owner.name}」{label}",
|
||||
entity_type="task", entity_id=task.id,
|
||||
data={"owner_type": owner_type, "owner_id": owner.id, "asset_kind": asset_kind,
|
||||
"provider": spec.provider, "model": spec.model})
|
||||
db.commit()
|
||||
result = dispatch.dispatch_task(db, task)
|
||||
log.info("平台生成任务 #%s 已登记:%s(派发 %s)", task.id, label,
|
||||
"立即开始" if result["dispatched"] else f"排队等待,{result['reason']}")
|
||||
return db.get(Task, task.id)
|
||||
|
||||
|
||||
def _payload(owner_type: str, owner, asset_kind: str,
|
||||
data: schemas.PlatformGenerate) -> dict:
|
||||
"""任务参数:请求体原样收在 ``request`` 里,外层放调度与展示要用的字段。
|
||||
|
||||
``owner_name`` 只是给顶栏「平台动态」的「查看」用的:那条深链按名字搜资产库,
|
||||
而 ``task.name`` 是「生成「俊杰」的定妆图」这样的一句话,拿它当搜索词永远 0 条。
|
||||
存名字而不是回表查,是为了不给 ``task_dict`` 加一次查询——它全站每条任务都要过。
|
||||
"""
|
||||
return {"operation": "generate", "owner_type": owner_type, "owner_id": owner.id,
|
||||
"owner_name": getattr(owner, "name", ""),
|
||||
"asset_kind": asset_kind, "request": data.model_dump()}
|
||||
|
||||
|
||||
def request_from(task: Task) -> tuple[str, int, str, schemas.PlatformGenerate]:
|
||||
"""从任务行还原「当时点的是什么」(执行体只拿得到 task_id)。"""
|
||||
payload = task.payload_json or {}
|
||||
raw = dict(payload.get("request") or {})
|
||||
# 老任务行可能带着外层多写的字段,Strict 模型会当场拒绝——只留请求体认识的键
|
||||
fields = set(schemas.PlatformGenerate.model_fields)
|
||||
data = schemas.PlatformGenerate(**{k: v for k, v in raw.items() if k in fields})
|
||||
return (str(payload.get("owner_type") or ""), int(payload.get("owner_id") or 0),
|
||||
str(payload.get("asset_kind") or data.kind), data)
|
||||
|
||||
|
||||
def produced_media_metadata(media, reference: dict, check: dict, text: str) -> None:
|
||||
"""把「这次到底用了谁的嗓子、念了什么、音色对不对」写进素材,界面才说得清楚。"""
|
||||
metadata = dict(media.metadata_json or {})
|
||||
metadata.update({
|
||||
"text": text,
|
||||
"reference_audio": reference.get("path") or reference.get("attached") or "",
|
||||
"reference_source": reference.get("source") or "",
|
||||
"reference_note": reference.get("note") or "使用本声线已绑定的参考音频",
|
||||
"timbre_check": check,
|
||||
})
|
||||
media.metadata_json = metadata
|
||||
note = str(media.note or "")
|
||||
tail = check.get("note") or ""
|
||||
if tail and "音色自检" not in note:
|
||||
media.note = f"{note}|音色自检:{tail}".strip("|")
|
||||
|
||||
|
||||
def generate(db: Session, task: Task) -> dict:
|
||||
"""真正跑一次平台资产生成并落库;失败一律抛 ``ProviderError`` 交给任务层归类。"""
|
||||
owner_type, owner_id, asset_kind, data = request_from(task)
|
||||
owner = get_owner(db, owner_type, owner_id)
|
||||
progress.report("准备生成参数")
|
||||
if owner_type == "voice":
|
||||
result = _generate_voice(db, owner, data, task)
|
||||
else:
|
||||
result = _generate_image(db, owner_type, owner, data, task)
|
||||
db.commit()
|
||||
events.log_event(db, "platform.asset.generated", project_id=None, task_id=task.id,
|
||||
message=f"平台{type_label(owner_type)}「{owner.name}」已生成"
|
||||
f"{AssetKind.LABELS.get(asset_kind, asset_kind)}"
|
||||
f"({result['provider']}/{result['model']})",
|
||||
entity_type=f"platform_{owner_type}", entity_id=owner.id,
|
||||
data={"media_id": result["media"].get("id"), "task_id": task.id})
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
def _config_error(message: str, code: str) -> ProviderError:
|
||||
"""参数/配置类错误:重试一百次也是同一个结果,直接判死并把原因说清。"""
|
||||
return ProviderError(message, code=code, retryable=False)
|
||||
|
||||
|
||||
def _generate_image(db: Session, owner_type: str, owner,
|
||||
data: schemas.PlatformGenerate, task: Task) -> dict:
|
||||
spec = resolve_spec(db, owner_type, data)
|
||||
if not spec.enabled:
|
||||
raise _config_error(f"模型 {spec.provider}/{spec.model} 已停用", "model_disabled")
|
||||
references = resolve_reference_media(db, owner_type, owner.id, data.reference_media_ids,
|
||||
kind="image")
|
||||
prompt = build_owner_prompt(None, owner_type, owner, data.kind, data.prompt)
|
||||
out = storage.platform_dir(owner_type, owner.id, "generated") / f"{data.kind}.png"
|
||||
request = AssetGenRequest(
|
||||
kind=data.kind, prompt=prompt, negative_prompt=data.negative_prompt,
|
||||
width=data.width, height=data.height, seed=data.seed,
|
||||
reference_images=[str(storage.to_absolute(row.path)) for row in references],
|
||||
params={"model": spec.model},
|
||||
idempotency_key=(
|
||||
f"platform-image:{owner.id}:{data.kind}:"
|
||||
f"{now_utc().isoformat()}:{data.prompt}:{','.join(map(str, data.reference_media_ids))}"
|
||||
),
|
||||
label=f"PLATFORM-{owner_type.upper()}-{owner.id}", output_path=out,
|
||||
)
|
||||
progress.report(f"调用 {spec.provider}/{spec.model} 出图")
|
||||
result = model_registry.generate_asset(db, request, "image", spec.provider, spec.model)
|
||||
produced = _require_file(result, "图像")
|
||||
kind = data.kind
|
||||
info = _store(produced, owner_type=owner_type, owner_id=owner.id, kind=kind,
|
||||
stem=f"{owner_type}-{owner.id}-{kind}-ai", result=result)
|
||||
media = storage.register_platform_media(
|
||||
db, owner_type=owner_type, owner_id=owner.id, kind=kind, path=info["path"],
|
||||
name=f"{owner.name} · AI {AssetKind.LABELS.get(kind, kind)}",
|
||||
note=f"平台资产生成:{spec.provider}/{spec.model}", source="provider", info=info,
|
||||
metadata={"generated": True, "provider": spec.provider, "model": spec.model,
|
||||
"prompt": request.prompt,
|
||||
"reference_media_ids": [row.id for row in references],
|
||||
"task_id": task.id},
|
||||
)
|
||||
return {"media": storage.platform_media_dict(media), "kind": kind,
|
||||
"provider": spec.provider, "model": spec.model}
|
||||
|
||||
|
||||
def _generate_voice(db: Session, owner, data: schemas.PlatformGenerate, task: Task) -> dict:
|
||||
spec = resolve_spec(db, "voice", data)
|
||||
if not spec.enabled:
|
||||
raise _config_error(f"模型 {spec.provider}/{spec.model} 已停用", "model_disabled")
|
||||
# 试听文本是「模型要念出来的台词」,不是声线设定。旧实现拿 owner.description 兜底,
|
||||
# 于是用户填的「温和、语速中等,沉稳」被当成台词念了出来,试听样本一听就是设定说明文。
|
||||
text = (data.sample_text or "").strip()
|
||||
if not text:
|
||||
raise _config_error("请先填写试听文本:它是模型要朗读的台词,系统不会用「音色描述」代替",
|
||||
"missing_sample_text")
|
||||
reference_media, reference = resolve_voice_reference(db, owner, spec, data)
|
||||
# 旧实现缺参考时直接回退到模型默认参考(女声),男声声线就这样被生成成了女声。
|
||||
if not reference["path"] and reference.get("source") == "none":
|
||||
raise _config_error(reference["note"], "missing_reference_audio")
|
||||
out = storage.platform_dir("voice", owner.id, "generated") / f"sample-{now_utc().timestamp():.0f}.wav"
|
||||
# 交给模型的是绝对路径(供应商要直接读文件);声线列里存 media_root 相对路径,
|
||||
# 换机器 / 进容器时不会因为写死绝对路径而失效。与任务链路 handlers 保持一致。
|
||||
reference_path = reference["path"] or owner.provider_voice_id
|
||||
request = VoiceGenRequest(
|
||||
text=text, voice_id=owner.name, provider_voice_id=reference_path,
|
||||
language=owner.language, speed=float((owner.tuning or {}).get("speed", 1.0)),
|
||||
seed=data.seed, params=(owner.tuning or {}).get("params") or {},
|
||||
# 没有参考音频时靠它出音,有参考音频时用它微调音色(见 comfy_assets.graph)
|
||||
instruct_text=timbre.instruct_text_of(owner) if timbre.instruct_model(spec) else "",
|
||||
idempotency_key=f"platform-voice:{owner.id}:{task.id}",
|
||||
label=f"PLATFORM-VOICE-{owner.id}", output_path=out,
|
||||
)
|
||||
progress.report(f"调用 {spec.provider}/{spec.model} 出试听")
|
||||
result = model_registry.generate_voice(db, request, spec.provider, spec.model)
|
||||
produced = _require_file(result, "音频")
|
||||
info = _store(produced, owner_type="voice", owner_id=owner.id, kind="voice_sample",
|
||||
stem=f"voice-{owner.id}-ai", result=result)
|
||||
media = storage.register_platform_media(
|
||||
db, owner_type="voice", owner_id=owner.id, kind="voice_sample", path=info["path"],
|
||||
name=f"{owner.name} · AI {AssetKind.LABELS.get('voice_sample', 'voice_sample')}",
|
||||
note=f"平台资产生成:{spec.provider}/{spec.model}", source="provider", info=info,
|
||||
metadata={"generated": True, "provider": spec.provider, "model": spec.model,
|
||||
"prompt": request.text,
|
||||
"reference_media_ids": [reference_media.id] if reference_media else [],
|
||||
"task_id": task.id},
|
||||
)
|
||||
owner.provider = spec.provider
|
||||
if reference["path"] and timbre.clone_model(spec):
|
||||
# 把真正参与克隆的那段参考音频绑回声线:否则试听样本是 A 的嗓子,
|
||||
# 之后项目配音又落回模型默认参考,变成另一种嗓子。
|
||||
owner.provider_voice_id = str(storage.to_relative(reference["path"]))
|
||||
# 音色自检:量一遍实际基频,与设定性别比对,结果进素材元数据供界面标注。
|
||||
check = timbre.check_voice(produced, owner.gender)
|
||||
produced_media_metadata(media, reference, check, text)
|
||||
return {"media": storage.platform_media_dict(media), "kind": "voice_sample",
|
||||
"provider": spec.provider, "model": spec.model}
|
||||
|
||||
|
||||
def _require_file(result, what: str) -> Path:
|
||||
produced = Path(result.file_path)
|
||||
if not produced.exists() or produced.stat().st_size < 128:
|
||||
raise ProviderError(f"模型未返回有效{what}文件", code="missing_output")
|
||||
return produced
|
||||
|
||||
|
||||
def _store(produced: Path, *, owner_type: str, owner_id: int, kind: str, stem: str,
|
||||
result) -> dict:
|
||||
info = storage.save_platform_local_file(
|
||||
produced, owner_type=owner_type, owner_id=owner_id, kind=kind, stem=stem)
|
||||
if getattr(result, "duration_ms", 0):
|
||||
info["duration_ms"] = int(result.duration_ms)
|
||||
return info
|
||||
|
||||
|
||||
__all__ = ["active_task", "build_owner_prompt", "enqueue", "generate", "get_owner",
|
||||
"normalize_type", "preflight", "produced_media_metadata", "request_from",
|
||||
"resolve_reference_media", "resolve_spec", "resolve_voice_reference", "type_label"]
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
"""进行中的任务如何把自己「此刻在做什么」说出来。
|
||||
|
||||
为什么需要这一层:出图动辄四十几秒、出片动辄十几分钟,而调用模型的那段代码埋在
|
||||
适配器深处(``providers/comfy_common.py``),手里既没有会话也不知道 task_id。
|
||||
以前它什么都说不出来,界面上就只剩一个转圈的图标——用户分不清「模型正在算」
|
||||
和「排在别人的任务后面」,只能一遍遍刷新页面去猜。
|
||||
|
||||
做法:执行线程起跑时把 task_id 绑到 thread-local(见 ``jobs/handlers.run``),
|
||||
深处任何一层调 ``report("排队中:等待算力空闲")`` 就能写回任务行;
|
||||
不在任务上下文里(普通请求线程)静默返回,调用点因此不需要判空。
|
||||
|
||||
写入走**另一条会话**并立刻提交:执行线程自己那条会话正握着业务事务,
|
||||
在深处顺手 commit 会把事务提前收掉,半途失败的素材状态就会对不上。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import threading
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import update
|
||||
|
||||
from app.db import SessionLocal
|
||||
from app.models import Task, now_utc
|
||||
|
||||
log = logging.getLogger("nex.progress")
|
||||
|
||||
_local = threading.local()
|
||||
|
||||
#: message 列宽 400,超了按字符砍掉——宁可截断也不要当场报错
|
||||
MAX_MESSAGE_CHARS = 400
|
||||
|
||||
|
||||
def bind(task_id: int) -> None:
|
||||
"""声明「当前线程正在执行这条任务」;由 handlers.run 统一调用。"""
|
||||
_local.task_id = int(task_id)
|
||||
|
||||
|
||||
def unbind() -> None:
|
||||
_local.task_id = None
|
||||
|
||||
|
||||
def current_task_id() -> int | None:
|
||||
return getattr(_local, "task_id", None)
|
||||
|
||||
|
||||
def report(message: str, *, progress: float | None = None) -> None:
|
||||
"""给进行中的任务补一句解释(可顺带推进度);不在任务上下文里时什么都不做。
|
||||
|
||||
这里**只写不读**、且永远不改 status:状态归执行体自己收口,
|
||||
深处这一层只负责把过程说清楚,失败了也不该影响正在跑的模型调用。
|
||||
"""
|
||||
task_id = current_task_id()
|
||||
if not task_id:
|
||||
return
|
||||
patch: dict[str, Any] = {"message": message[:MAX_MESSAGE_CHARS], "heartbeat_at": now_utc()}
|
||||
if progress is not None:
|
||||
patch["progress"] = max(0.0, min(1.0, float(progress)))
|
||||
session = SessionLocal()
|
||||
try:
|
||||
session.execute(update(Task).where(Task.id == task_id).values(**patch)
|
||||
.execution_options(synchronize_session=False))
|
||||
session.commit()
|
||||
except Exception as exc: # noqa: BLE001 - 说不出来不等于做错了
|
||||
log.debug("任务 #%s 状态说明回写失败:%s", task_id, exc)
|
||||
session.rollback()
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
__all__ = ["MAX_MESSAGE_CHARS", "bind", "current_task_id", "report", "unbind"]
|
||||
|
|
@ -5,7 +5,7 @@
|
|||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session, selectinload
|
||||
|
|
@ -80,6 +80,10 @@ def task_dict(task: Task) -> dict:
|
|||
return {
|
||||
"id": task.id, "project_id": task.project_id, "shot_id": task.shot_id,
|
||||
"timeline_id": task.timeline_id, "kind": task.kind, "name": task.name, "stage": task.stage,
|
||||
# 非项目归属(平台资产母版):卡片按它亮「正在生成」,活动面板按它跳回资产
|
||||
"owner_type": task.owner_type or "", "owner_id": task.owner_id,
|
||||
# 「此刻在做什么」优先取执行体自己说的,退回深处 progress.report 写的那句
|
||||
"message": _task_message(task),
|
||||
"status": task.status, "priority": task.priority, "progress": round(task.progress or 0, 4),
|
||||
"provider": task.provider, "model": task.model, "mode": task.mode,
|
||||
"execution_backend": task.execution_backend, "celery_task_id": task.celery_task_id,
|
||||
|
|
@ -100,6 +104,60 @@ def task_dict(task: Task) -> dict:
|
|||
}
|
||||
|
||||
|
||||
def _task_message(task: Task) -> str:
|
||||
"""任务此刻在做什么的一句话:优先 ``task.message``(执行体与算力锁写的),
|
||||
|
||||
退回 ``checkpoint_json.last_note``(项目侧 handler 一直在往这里写阶段说明),
|
||||
两边都有时取更新的那一条——两侧写入者不同,谁最后落地谁就是真相。
|
||||
"""
|
||||
note = str((task.checkpoint_json or {}).get("last_note") or "")
|
||||
mine = str(task.message or "")
|
||||
if not note:
|
||||
return mine
|
||||
if not mine:
|
||||
return note
|
||||
at = (task.checkpoint_json or {}).get("at") or ""
|
||||
heartbeat = task.heartbeat_at.isoformat() if task.heartbeat_at else ""
|
||||
return note if str(at) >= str(heartbeat or "") else mine
|
||||
|
||||
|
||||
#: 活动面板默认回吐多少条历史:够翻完一个下午的生成,又不至于让 3 秒一次的轮询变重
|
||||
ACTIVITY_LIMIT = 40
|
||||
#: 已落定的任务在活动面板里保留多久(与 ACTIVITY_LIMIT 一起构成「刚刚完成」那一段)
|
||||
ACTIVITY_WINDOW = timedelta(hours=6)
|
||||
|
||||
|
||||
def activity(db: Session, *, limit: int = ACTIVITY_LIMIT, owner_type: str = "",
|
||||
active_only: bool = False) -> dict:
|
||||
"""顶栏「平台动态」要的那一份:进行中的全部 + 最近落定的若干条。
|
||||
|
||||
数据源就是统一任务表,所以项目侧的出片与平台侧的生成天然同屏——
|
||||
用户问的是「平台现在在忙什么」,不关心这条活算不算某个项目的。
|
||||
``limit`` 只截历史:进行中的任务一条都不能少,否则用户在面板里
|
||||
看不到自己刚点的那次,比没有面板更糟(他会以为排队排丢了)。
|
||||
"""
|
||||
stmt = select(Task).order_by(Task.id.desc())
|
||||
if owner_type:
|
||||
stmt = stmt.where(Task.owner_type == owner_type)
|
||||
if active_only:
|
||||
rows = db.scalars(stmt.where(Task.status.in_(TaskStatus.ACTIVE))).all()
|
||||
else:
|
||||
active = db.scalars(stmt.where(Task.status.in_(TaskStatus.ACTIVE))).all()
|
||||
settled = db.scalars(
|
||||
stmt.where(Task.status.notin_(TaskStatus.ACTIVE),
|
||||
Task.updated_at >= now_utc() - ACTIVITY_WINDOW)
|
||||
.limit(min(200, max(1, limit)))
|
||||
).all()
|
||||
rows = list(active) + list(settled)
|
||||
items = [task_dict(t) for t in rows]
|
||||
counts: dict[str, int] = {}
|
||||
for row in rows:
|
||||
counts[row.status] = counts.get(row.status, 0) + 1
|
||||
return {"tasks": items,
|
||||
"active": sum(counts.get(s, 0) for s in TaskStatus.ACTIVE),
|
||||
"counts": counts, "window_hours": ACTIVITY_WINDOW.total_seconds() / 3600}
|
||||
|
||||
|
||||
def shot_dict(db: Session, shot: Shot, *, assets: bool = True) -> dict:
|
||||
from app.services import preview as preview_service
|
||||
from app.services import prompt as prompt_service
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ from app.config import settings
|
|||
|
||||
# 队列划分:不同工种的资源画像不同(GPU/网络受限的生成 vs CPU 密集的合成)
|
||||
QUEUES = {"generate_shot": "generate", "voiceover": "audio", "qc_shot": "qc",
|
||||
"compose": "compose", "import": "io", "asset": "io"}
|
||||
"compose": "compose", "import": "io", "asset": "io", "platform_asset": "io"}
|
||||
|
||||
TASK_ROUTES = {
|
||||
"app.jobs.generate_shot": {"queue": "generate"},
|
||||
|
|
@ -22,6 +22,7 @@ TASK_ROUTES = {
|
|||
"app.jobs.compose": {"queue": "compose"},
|
||||
"app.jobs.import_package": {"queue": "io"},
|
||||
"app.jobs.asset": {"queue": "io"},
|
||||
"app.jobs.platform_asset": {"queue": "io"},
|
||||
}
|
||||
|
||||
celery_app = Celery("nex_animation", broker=settings.broker_url, backend=settings.result_backend)
|
||||
|
|
|
|||
|
|
@ -153,6 +153,25 @@ def api(client):
|
|||
body = self.post("/api/projects", json=payload)
|
||||
return body["result"]["id"]
|
||||
|
||||
def generate(self, url, json, *, timeout=120, expect=202, task_only=False):
|
||||
"""发起一次平台资产生成,并**等它真的跑完**,返回生成出来的素材字典。
|
||||
|
||||
接口只登记任务并立刻返 202(同步等出图那一版会被反代 60s 读超时打断),
|
||||
所以自检不能再直接读 POST 响应体:必须等 inline 线程池跑空、
|
||||
再回读任务行,否则断言读到的是「排队中」的空结果。
|
||||
``expect`` 非 202 时(表单校验、去重拦截)原样返回响应体,不做等待。
|
||||
"""
|
||||
body = self.call("POST", url, expect=expect, json=json)
|
||||
if expect != 202:
|
||||
return body
|
||||
task_id = body["result"]["id"]
|
||||
assert execution.wait_idle(timeout), f"平台生成任务 #{task_id} 在 {timeout}s 内没有跑完"
|
||||
task = self.get(f"/api/tasks/{task_id}")["task"]
|
||||
assert task["status"] == "succeeded", (
|
||||
f"平台生成任务 #{task_id} 未成功:{task['status']} / "
|
||||
f"{task['error_code']} {task['error_message']}")
|
||||
return task if task_only else task["result"]["media"]
|
||||
|
||||
def drain(self, project_id, timeout=600):
|
||||
return self.post(f"/api/projects/{project_id}/dispatch", json={"drain": True,
|
||||
"timeout": timeout})
|
||||
|
|
|
|||
|
|
@ -144,7 +144,10 @@ def test_dead_endpoint_is_reported_as_refused_not_as_outage(shell_proxy):
|
|||
comfy_common.require_reachable(dead, stage="开工前")
|
||||
message = str(caught.value)
|
||||
assert caught.value.code == "provider_unreachable"
|
||||
assert "502" not in message
|
||||
# 断言前先把地址从文本里摘掉:``_free_base()`` 拿的是随机空闲端口,
|
||||
# 50227 这种端口号自带「502」三个字,会让这条用例每跑几千次红一次。
|
||||
haystack = message.replace(dead, "").replace(dead.rsplit(":", 1)[-1], "")
|
||||
assert "502" not in haystack
|
||||
assert ("Connection refused" in message or "ConnectError" in message), message
|
||||
assert "模型管理中心" in message
|
||||
|
||||
|
|
|
|||
|
|
@ -31,11 +31,11 @@ def test_platform_assets_upload_import_publish_and_generate(api):
|
|||
)["result"]
|
||||
assert uploaded["url"].startswith("/media/platform/")
|
||||
|
||||
generated = api.post(
|
||||
generated = api.generate(
|
||||
f"/api/platform/{'scene'}/{scene['id']}/generate",
|
||||
json={"kind": "scene_image", "provider": "mock", "model": "mock-image-1",
|
||||
"prompt": "冷蓝全息舰桥", "width": 320, "height": 180},
|
||||
)["result"]
|
||||
{"kind": "scene_image", "provider": "mock", "model": "mock-image-1",
|
||||
"prompt": "冷蓝全息舰桥", "width": 320, "height": 180},
|
||||
)
|
||||
assert generated["metadata"]["generated"] is True
|
||||
|
||||
imported = api.post("/api/platform/import", json={
|
||||
|
|
@ -126,12 +126,12 @@ def test_platform_image_generation_accepts_owned_reference_media(api):
|
|||
files={"file": ("reference.png", _make_image_png(320, 320, "0x335577"), "image/png")},
|
||||
data={"kind": "reference", "name": "参考定妆"},
|
||||
)["result"]
|
||||
generated = api.post(
|
||||
generated = api.generate(
|
||||
f"/api/platform/human/{character['id']}/generate",
|
||||
json={"kind": "portrait", "provider": "mock", "model": "mock-image-1",
|
||||
"prompt": "保持参考图中的五官与服装", "width": 320, "height": 320,
|
||||
"reference_media_ids": [reference["id"]]},
|
||||
)["result"]
|
||||
{"kind": "portrait", "provider": "mock", "model": "mock-image-1",
|
||||
"prompt": "保持参考图中的五官与服装", "width": 320, "height": 320,
|
||||
"reference_media_ids": [reference["id"]]},
|
||||
)
|
||||
|
||||
assert generated["metadata"]["reference_media_ids"] == [reference["id"]]
|
||||
|
||||
|
|
@ -166,11 +166,11 @@ def test_platform_voice_generation_uses_uploaded_reference(api, db, monkeypatch)
|
|||
|
||||
monkeypatch.setattr(platform_api.model_registry, "is_available", lambda *_args, **_kwargs: True)
|
||||
monkeypatch.setattr(platform_api.model_registry, "generate_voice", fake_generate)
|
||||
generated = api.post(
|
||||
generated = api.generate(
|
||||
f"/api/platform/voice/{voice['id']}/generate",
|
||||
json={"kind": "voice_sample", "provider": "comfyui", "model": "indextts-test",
|
||||
"sample_text": "参考音色已经接通。", "reference_media_id": reference["id"]},
|
||||
)["result"]
|
||||
{"kind": "voice_sample", "provider": "comfyui", "model": "indextts-test",
|
||||
"sample_text": "参考音色已经接通。", "reference_media_id": reference["id"]},
|
||||
)
|
||||
assert generated["kind"] == "voice_sample"
|
||||
assert captured["request"].provider_voice_id.endswith(reference["path"])
|
||||
assert Path(captured["request"].provider_voice_id).is_absolute()
|
||||
|
|
|
|||
|
|
@ -146,13 +146,16 @@ def test_upload_after_master_deletion_reuses_id_without_integrity_error(api):
|
|||
|
||||
|
||||
def test_generate_after_reused_id_does_not_500(api):
|
||||
"""端到端复现报错接口:主键复用 + 遗留孤儿行时 generate 必须 200 且版本号正确递增。"""
|
||||
"""端到端复现报错接口:主键复用 + 遗留孤儿行时 generate 必须成功且版本号正确递增。
|
||||
|
||||
三次生成串行等待(api.generate),因为同一条母版同一类素材在生成中会被去重挡住。
|
||||
"""
|
||||
human = api.post("/api/platform/characters", json={
|
||||
"name": "生成复用", "appearance": "银色短发,深蓝机甲",
|
||||
"persona_prompt": "冷静果决的领航员"})["result"]
|
||||
payload = {"kind": "portrait", "provider": "mock", "model": "mock-image-1",
|
||||
"prompt": "冷蓝定妆", "width": 240, "height": 240}
|
||||
generated = api.post(f"/api/platform/human/{human['id']}/generate", json=payload)["result"]
|
||||
generated = api.generate(f"/api/platform/human/{human['id']}/generate", payload)
|
||||
media_id = generated["id"]
|
||||
assert generated["version"] == 1 and _version_nos("platform_media_versions", "media_id", media_id) == [1]
|
||||
|
||||
|
|
@ -164,13 +167,13 @@ def test_generate_after_reused_id_does_not_500(api):
|
|||
"name": "生成复用", "appearance": "银色短发,深蓝机甲",
|
||||
"persona_prompt": "冷静果决的领航员"})["result"]
|
||||
assert reuse["id"] != human["id"]
|
||||
second = api.post(f"/api/platform/human/{reuse['id']}/generate", json=payload)["result"]
|
||||
second = api.generate(f"/api/platform/human/{reuse['id']}/generate", payload)
|
||||
assert second["id"] == media_id, "新母版复用同一 media_id,正是当年 500 的触发条件"
|
||||
assert second["version"] == 2, "首版号按子表现状推导,跳过孤儿行占用的 version_no=1"
|
||||
assert _version_nos("platform_media_versions", "media_id", media_id) == [1, 2]
|
||||
assert Path(storage.to_absolute(second["path"])).exists()
|
||||
|
||||
third = api.post(f"/api/platform/human/{reuse['id']}/generate", json=payload)["result"]
|
||||
third = api.generate(f"/api/platform/human/{reuse['id']}/generate", payload)
|
||||
assert third["version"] == 3 and _version_nos("platform_media_versions", "media_id", media_id) == [1, 2, 3]
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -106,8 +106,8 @@ def test_real_sqlite_write_lock_is_waited_for(tmp_path: Path):
|
|||
|
||||
|
||||
def test_init_db_survives_a_busy_database(monkeypatch):
|
||||
"""把 init_db 的三步全换成「先抛两次锁」,确认启动路径整体是等锁而不是崩掉。"""
|
||||
counters = {"create_all": 0, "columns": 0, "backfill": 0}
|
||||
"""把 init_db 的四步全换成「先抛两次锁」,确认启动路径整体是等锁而不是崩掉。"""
|
||||
counters = {"create_all": 0, "columns": 0, "relax": 0, "backfill": 0}
|
||||
|
||||
def flaky(key: str):
|
||||
def run(*_args, **_kwargs) -> None:
|
||||
|
|
@ -119,8 +119,10 @@ def test_init_db_survives_a_busy_database(monkeypatch):
|
|||
metadata = type("M", (), {"create_all": staticmethod(flaky("create_all"))})()
|
||||
monkeypatch.setattr(dbmod, "Base", type("B", (), {"metadata": metadata})())
|
||||
monkeypatch.setattr(dbmod, "_add_missing_columns", flaky("columns"))
|
||||
# tasks.project_id 放开非空要整表重建 tasks,这一步同样在启动路径上,必须一起等锁
|
||||
monkeypatch.setattr(dbmod, "_relax_nullability", flaky("relax"))
|
||||
monkeypatch.setattr(dbmod, "_backfill_defaults", flaky("backfill"))
|
||||
monkeypatch.setattr(dbmod, "STARTUP_LOCK_BUDGET_SECONDS", 10.0)
|
||||
|
||||
dbmod.init_db()
|
||||
assert counters == {"create_all": 3, "columns": 3, "backfill": 3}
|
||||
assert counters == {"create_all": 3, "columns": 3, "relax": 3, "backfill": 3}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,261 @@
|
|||
"""统一任务表与「平台动态」:所有跑模型的活都必须能被问出进度。
|
||||
|
||||
用户的原话(2026-11):「平台应该有个进度,所有跑模型的任务都应该写到一个任务表中。
|
||||
异步,可以请求进度。然后界面上可以整体展示。」
|
||||
|
||||
在那之前平台资产库的 AI 生成是一个同步 HTTP 请求:出图实测四十几秒,反代 60s 读
|
||||
超时就把连接掐了(504),而**出图线程并没有被取消**——图照样落盘,用户看不见,
|
||||
只能手动刷新页面去猜它到底成没成。现在接口只做「校验 + 登记 + 派发」,
|
||||
当场返回任务号,进度与产物全在 ``tasks`` 那一行里,换设备、刷新页面都接着看得到。
|
||||
|
||||
这一组用例锁的是三件容易被改回去的事:
|
||||
1. 平台生成必须落到同一张 ``tasks`` 表,并带上母版归属(``owner_type/owner_id``)
|
||||
—— 顶栏要按它点亮卡片、活动面板要按它跳回资产;
|
||||
2. ``GET /api/tasks/activity`` 必须看得见它,且进行中的任务一条都不能被条数截掉;
|
||||
3. 同一条母版同一类素材在跑的时候再点一次必须挡下来(409),
|
||||
否则三次点击就是三次 GPU 排队、三份产物、三倍扣费。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
|
||||
from app.constants import TaskKind, TaskStatus
|
||||
from app.providers import registry as model_registry
|
||||
from app.providers.base import AssetGenResult, ProviderError
|
||||
from tests.conftest import _make_image_png
|
||||
|
||||
|
||||
def _scene(api, name: str = "星舰舰桥") -> dict:
|
||||
return api.post("/api/platform/scenes",
|
||||
json={"name": name, "prompt": "环形舰桥,全息星图", "lighting": "冷蓝"})["result"]
|
||||
|
||||
|
||||
def _png(width: int = 320, height: int = 180) -> bytes:
|
||||
"""一张能过「产物校验」的图:模型跑完后平台会拒绝小于 128 字节的文件。
|
||||
|
||||
本机没有 ffmpeg 时 ``_make_image_png`` 会退回 70 字节的 1x1 图,
|
||||
那种图在这里会被判成「模型未返回有效图像文件」,用例就变成测 ffmpeg 装没装。
|
||||
"""
|
||||
payload = _make_image_png(width, height, "0x2f6d4f")
|
||||
return payload + b"\x00" * max(0, 256 - len(payload))
|
||||
|
||||
|
||||
def _body(prompt: str = "冷蓝全息舰桥") -> dict:
|
||||
return {"kind": "scene_image", "provider": "mock", "model": "mock-image-1",
|
||||
"prompt": prompt, "width": 320, "height": 180}
|
||||
|
||||
|
||||
def _poll(api, task_id: int, done, what: str, timeout: float = 20.0) -> dict:
|
||||
"""轮询任务行直到 ``done(task)`` 成立——用于观察「正在跑」这一中间态。"""
|
||||
import time
|
||||
|
||||
deadline = time.monotonic() + timeout
|
||||
task: dict = {}
|
||||
while time.monotonic() < deadline:
|
||||
task = api.get(f"/api/tasks/{task_id}")["task"]
|
||||
if done(task):
|
||||
return task
|
||||
time.sleep(0.05)
|
||||
raise AssertionError(f"任务 #{task_id} 未在 {timeout}s 内{what},当前 "
|
||||
f"{task.get('status')} / {task.get('message')}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 落进统一任务表
|
||||
def test_platform_generation_lands_in_the_unified_task_table(api):
|
||||
"""生成不再是一个「请求-等待-响应」,而是一条可查询、可重试、可取消的任务。"""
|
||||
scene = _scene(api)
|
||||
task = api.generate(f"/api/platform/scene/{scene['id']}/generate", _body(), task_only=True)
|
||||
|
||||
assert task["kind"] == TaskKind.PLATFORM_ASSET
|
||||
# 母版本就不属于任何项目:挂 0/-1 会让每一条按项目统计的 SQL 多背一个特例
|
||||
assert task["project_id"] is None and task["shot_id"] is None
|
||||
assert task["owner_type"] == "scene" and task["owner_id"] == scene["id"]
|
||||
assert task["status"] == TaskStatus.SUCCEEDED
|
||||
assert scene["name"] in task["name"]
|
||||
assert task["output_url"].startswith("/media/platform/")
|
||||
# 产物反向指回任务号:素材出问题时能从文件一路追回是哪一次生成
|
||||
assert task["result"]["media"]["metadata"]["task_id"] == task["id"]
|
||||
assert task["elapsed_ms"] >= 0 and task["payload"]["asset_kind"] == "scene_image"
|
||||
|
||||
|
||||
def test_activity_shows_finished_platform_work(api):
|
||||
"""顶栏那一份「平台动态」就是统一任务表的切片,不需要第二个数据源。"""
|
||||
scene = _scene(api)
|
||||
task = api.generate(f"/api/platform/scene/{scene['id']}/generate", _body(), task_only=True)
|
||||
|
||||
panel = api.get("/api/tasks/activity")["result"]
|
||||
assert panel["active"] == 0
|
||||
assert panel["counts"].get(TaskStatus.SUCCEEDED) == 1
|
||||
assert panel["window_hours"] > 0
|
||||
|
||||
row = next(item for item in panel["tasks"] if item["id"] == task["id"])
|
||||
assert row["owner_type"] == "scene" and row["owner_id"] == scene["id"]
|
||||
assert row["project_id"] is None
|
||||
# 面板上必须能说出「这是哪条母版的什么」,只剩一个转圈图标等于没说
|
||||
assert scene["name"] in row["name"]
|
||||
assert row["message"] and row["result"]["media"]["id"]
|
||||
|
||||
|
||||
def test_activity_ignores_settled_work_outside_the_window(api):
|
||||
"""历史只留最近一段时间:三天前跑成功的那条不该一直挂在顶栏角标旁边。"""
|
||||
from datetime import timedelta
|
||||
|
||||
from app.db import SessionLocal
|
||||
from app.models import Task, now_utc
|
||||
from app.services import status as status_service
|
||||
|
||||
scene = _scene(api)
|
||||
task = api.generate(f"/api/platform/scene/{scene['id']}/generate", _body(), task_only=True)
|
||||
with SessionLocal() as session:
|
||||
row = session.get(Task, task["id"])
|
||||
# 落定时间推到窗口之外:它已经从「刚刚完成」那一格里掉出去了
|
||||
row.updated_at = now_utc() - timedelta(hours=status_service.ACTIVITY_WINDOW.total_seconds() / 3600 + 1)
|
||||
session.commit()
|
||||
|
||||
panel = api.get("/api/tasks/activity")["result"]
|
||||
assert [item["id"] for item in panel["tasks"]] == []
|
||||
assert panel["counts"] == {}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 进行中的可见性
|
||||
def _blocking_adapter(release: threading.Event, notes: list[str]):
|
||||
"""替身模型:卡在一个事件上,并在深处报一句「算力排队」——真实链路正是这个样子。
|
||||
|
||||
``services/progress`` 靠 thread-local 里的 task_id 写回任务行,
|
||||
跑在 inline 线程池里的执行体如果没绑定 task_id,这里写进去的就是空气,
|
||||
界面上也永远看不到「排在别人后面」和「模型正在算」的区别。
|
||||
"""
|
||||
from app.services import progress
|
||||
|
||||
class Blocking:
|
||||
name = "blocking"
|
||||
|
||||
def __call__(self, db, request, media_type, provider, model):
|
||||
notes.append(progress.current_task_id() and "bound" or "unbound")
|
||||
progress.report("排队中:等待算力空闲")
|
||||
assert release.wait(20), "替身模型没有被放行,用例会挂在这里"
|
||||
with open(request.output_path, "wb") as handle:
|
||||
handle.write(_png())
|
||||
return AssetGenResult(file_path=str(request.output_path), metadata={"model": model})
|
||||
|
||||
return Blocking()
|
||||
|
||||
|
||||
def test_a_running_generation_is_visible_and_blocks_the_second_click(api, monkeypatch):
|
||||
"""跑模型期间:界面看得见它、卡片点不动它,一次点击只该有一份产物。"""
|
||||
release = threading.Event()
|
||||
notes: list[str] = []
|
||||
monkeypatch.setattr(model_registry, "generate_asset", _blocking_adapter(release, notes))
|
||||
scene = _scene(api)
|
||||
|
||||
# 202 而不是 200:接口只做「校验 + 登记 + 派发」,出图这件事交给后台
|
||||
first = api.post(f"/api/platform/scene/{scene['id']}/generate", json=_body(), expect=202)
|
||||
assert first["result"]["status"] in (TaskStatus.QUEUED, TaskStatus.RUNNING)
|
||||
task_id = first["result"]["id"]
|
||||
# 深处那句排队说明必须能到达任务行:出图四十多秒里,用户看的就只有这一行字
|
||||
running = _poll(api, task_id, lambda t: t["message"] == "排队中:等待算力空闲",
|
||||
"报出算力排队说明")
|
||||
assert running["status"] == TaskStatus.RUNNING
|
||||
assert notes == ["bound"], "执行体必须把 task_id 绑进线程,否则 progress.report 写不进来"
|
||||
|
||||
panel = api.get("/api/tasks/activity")["result"]
|
||||
assert panel["active"] == 1
|
||||
assert panel["tasks"][0]["id"] == task_id
|
||||
|
||||
# 用户没等完又点了一次:挡下来,而不是悄悄排第二份
|
||||
again = api.post(f"/api/platform/scene/{scene['id']}/generate",
|
||||
json=_body("换个提示词再试"), expect=409)
|
||||
assert "正在生成中" in again["detail"] and scene["name"] in again["detail"]
|
||||
|
||||
release.set()
|
||||
from app.services import execution
|
||||
|
||||
assert execution.wait_idle(30)
|
||||
done = api.get(f"/api/tasks/{task_id}")["task"]
|
||||
assert done["status"] == TaskStatus.SUCCEEDED
|
||||
assert api.get("/api/tasks/activity")["result"]["active"] == 0
|
||||
|
||||
|
||||
def test_a_running_platform_task_can_be_cancelled_without_a_project(api, monkeypatch):
|
||||
"""取消按钮过去靠「按项目取父记录」拿权限,平台任务没有项目,会凭空 404。"""
|
||||
release = threading.Event()
|
||||
monkeypatch.setattr(model_registry, "generate_asset", _blocking_adapter(release, []))
|
||||
scene = _scene(api)
|
||||
task_id = api.post(f"/api/platform/scene/{scene['id']}/generate", json=_body(),
|
||||
expect=202)["result"]["id"]
|
||||
_poll(api, task_id, lambda t: t["status"] == TaskStatus.RUNNING, "开始执行")
|
||||
|
||||
cancelled = api.post(f"/api/tasks/{task_id}/cancel", json={"reason": "提示词写错了"})["result"]
|
||||
assert cancelled["status"] == TaskStatus.CANCELLED
|
||||
|
||||
release.set()
|
||||
from app.services import execution
|
||||
|
||||
assert execution.wait_idle(30)
|
||||
# 出图线程跑完了也不许把「已取消」改写成成功:用户明确说过不要
|
||||
final = api.get(f"/api/tasks/{task_id}")["task"]
|
||||
assert final["status"] == TaskStatus.CANCELLED
|
||||
|
||||
|
||||
def test_a_transient_failure_stays_on_the_panel_while_it_waits_to_retry(api, monkeypatch):
|
||||
"""连不上算力是瞬时故障:平台自己会再试一次,这期间它在面板上仍然是「进行中」。
|
||||
|
||||
挡在这里的是「重试中的任务从角标上消失了」这一类回归——用户看到的是
|
||||
进度条归零、什么都没发生,于是以为失败了,其实排队等下一次派发。
|
||||
"""
|
||||
def busy(db, request, media_type, provider, model):
|
||||
raise ProviderError("算力服务无响应:http://comfyui:9200", code="gpu_unreachable")
|
||||
|
||||
monkeypatch.setattr(model_registry, "generate_asset", busy)
|
||||
scene = _scene(api)
|
||||
created = api.post(f"/api/platform/scene/{scene['id']}/generate", json=_body(),
|
||||
expect=202)["result"]
|
||||
|
||||
from app.services import execution
|
||||
|
||||
assert execution.wait_idle(30)
|
||||
task = api.get(f"/api/tasks/{created['id']}")["task"]
|
||||
assert task["status"] == TaskStatus.RETRYING
|
||||
assert task["error_code"] == "gpu_unreachable"
|
||||
assert task["max_attempts"] == 2 and task["next_retry_at"] is not None
|
||||
|
||||
panel = api.get("/api/tasks/activity")["result"]
|
||||
assert panel["active"] == 1
|
||||
row = next(item for item in panel["tasks"] if item["id"] == created["id"])
|
||||
# 面板上直接把供应商那句原话摊开,用户才知道去模型管理中心查地址,而不是重传素材
|
||||
assert "算力服务无响应" in row["error_message"]
|
||||
|
||||
|
||||
def test_a_failed_platform_task_can_be_retried_from_the_panel(api, monkeypatch):
|
||||
"""面板上的「重试」要能立刻再排一次,而不是等下一轮调度扫描(本机 5s)。
|
||||
|
||||
这里用 retryable=False 的错误(提示词超限这一类):平台自己不会反复撞墙,
|
||||
改好之后由人点重试——那正是活动面板上那颗按钮的用途。
|
||||
"""
|
||||
fixed = {"ok": False}
|
||||
|
||||
def adapter(db, request, media_type, provider, model):
|
||||
if not fixed["ok"]:
|
||||
raise ProviderError("提示词超出模型长度上限", code="prompt_too_long", retryable=False)
|
||||
with open(request.output_path, "wb") as handle:
|
||||
handle.write(_png())
|
||||
return AssetGenResult(file_path=str(request.output_path), metadata={"model": model})
|
||||
|
||||
monkeypatch.setattr(model_registry, "generate_asset", adapter)
|
||||
scene = _scene(api)
|
||||
task_id = api.post(f"/api/platform/scene/{scene['id']}/generate", json=_body(),
|
||||
expect=202)["result"]["id"]
|
||||
|
||||
from app.services import execution
|
||||
|
||||
assert execution.wait_idle(30)
|
||||
failed = api.get(f"/api/tasks/{task_id}")["task"]
|
||||
assert failed["status"] == TaskStatus.FAILED and failed["attempts"] == 1
|
||||
|
||||
fixed["ok"] = True
|
||||
retried = api.post(f"/api/tasks/{task_id}/retry", json={})["result"]
|
||||
assert retried["status"] in (TaskStatus.QUEUED, TaskStatus.RUNNING, TaskStatus.SUCCEEDED)
|
||||
assert execution.wait_idle(30)
|
||||
done = api.get(f"/api/tasks/{task_id}")["task"]
|
||||
assert done["status"] == TaskStatus.SUCCEEDED
|
||||
assert done["result"]["media"]["metadata"]["task_id"] == task_id
|
||||
|
|
@ -190,9 +190,8 @@ def test_male_voice_without_reference_borrows_male_reference(api, db):
|
|||
assert resolved["path"].endswith(".wav")
|
||||
assert male_master
|
||||
|
||||
created = api.post(f"/api/platform/voice/{newcomer}/generate",
|
||||
json={"kind": "voice_sample", "sample_text": "我是俊杰,公司的产品专家。"})
|
||||
media = created["result"]
|
||||
media = api.generate(f"/api/platform/voice/{newcomer}/generate",
|
||||
{"kind": "voice_sample", "sample_text": "我是俊杰,公司的产品专家。"})
|
||||
assert media["metadata"]["reference_source"] == "gender_sibling"
|
||||
assert media["metadata"]["text"] == "我是俊杰,公司的产品专家。"
|
||||
assert "timbre_check" in media["metadata"]
|
||||
|
|
@ -265,8 +264,8 @@ def test_sample_text_is_required_and_never_the_description(api, db):
|
|||
json={"kind": "voice_sample", "sample_text": " \n "}, expect=422)
|
||||
assert "试听文本" in blank["detail"]
|
||||
|
||||
produced = api.post(f"/api/platform/voice/{voice_id}/generate",
|
||||
json={"kind": "voice_sample", "sample_text": "你好,我是紫小慧。"})["result"]
|
||||
produced = api.generate(f"/api/platform/voice/{voice_id}/generate",
|
||||
{"kind": "voice_sample", "sample_text": "你好,我是紫小慧。"})
|
||||
assert produced["metadata"]["text"] == "你好,我是紫小慧。"
|
||||
assert "音色温暖柔和" not in produced["metadata"]["text"]
|
||||
|
||||
|
|
@ -308,8 +307,8 @@ def test_ai_sample_never_becomes_this_voice_s_own_reference(api, db):
|
|||
_clone_tts(db)
|
||||
_platform_voice(api, name="洪七公 · 声线", gender="male", reference_hz=105)
|
||||
newcomer = _platform_voice(api, name="李俊杰", gender="male", description="低沉浑厚的男声")
|
||||
produced = api.post(f"/api/platform/voice/{newcomer}/generate",
|
||||
json={"kind": "voice_sample", "sample_text": "我是李俊杰。"})["result"]
|
||||
produced = api.generate(f"/api/platform/voice/{newcomer}/generate",
|
||||
{"kind": "voice_sample", "sample_text": "我是李俊杰。"})
|
||||
assert produced["metadata"]["generated"] is True
|
||||
|
||||
# 模拟旧链路的泄漏现场:声线绑定的其实就是那个 AI 样本
|
||||
|
|
@ -364,8 +363,8 @@ def test_importing_a_voice_keeps_the_ai_provenance(api, db, studio):
|
|||
|
||||
_clone_tts(db)
|
||||
master = _platform_voice(api, name="周伯通 · 声线", gender="male", reference_hz=118)
|
||||
api.post(f"/api/platform/voice/{master}/generate",
|
||||
json={"kind": "voice_sample", "sample_text": "老顽童来也。"})
|
||||
api.generate(f"/api/platform/voice/{master}/generate",
|
||||
{"kind": "voice_sample", "sample_text": "老顽童来也。"})
|
||||
|
||||
api.post("/api/platform/import",
|
||||
json={"project_id": studio["project_id"], "asset_type": "voice", "asset_id": master})
|
||||
|
|
|
|||
83
docs/UI规范.md
83
docs/UI规范.md
|
|
@ -1337,6 +1337,89 @@ chromeless 页面(不套顶栏 / 侧栏,也不拉需要鉴权的 `/api/meta`
|
|||
只剩 `extra` 的卡头少一个样本。它量的是渲染结果,不要求每页样本数相同——
|
||||
看的是 `ALIGN_PROBED` 总有没有掉到地板(本轮实测 181 / 底线 40),不是数标题。
|
||||
|
||||
### 13.7 模型任务统一进任务表:顶栏「平台动态」是唯一进度源(2026-11-06 · 用户第 2、3 条 + 架构指令)
|
||||
|
||||
**症状**:资产库点「AI 生成」是一个同步 HTTP 请求,出图 40 秒、出片 6 分钟,
|
||||
请求挂着的时候界面上什么都没有;生成完了页面也不知道,用户只能自己按 F5 猜
|
||||
(原话「很多资产的生成需要手动刷新页面」)。更根本的是**平台在跑什么看不见**——
|
||||
三条生成同时在进行,界面上没有任何一处说得出「现在有几条活、跑到哪了、谁在跑」。
|
||||
|
||||
用户给的口径是一句架构指令,不是一处样式:
|
||||
|
||||
> 平台应该有个进度,所有跑模型的任务都应该写到一个任务表中。异步,可以请求进度。然后界面上可以整体展示。
|
||||
|
||||
落到实现上是三条规矩:
|
||||
|
||||
**① 任务表是唯一进度源,不新建第二张表。**
|
||||
既有 `tasks` 表(调度台在用的那张)加三列就够用:`owner_type` / `owner_id` 把任务挂到
|
||||
「哪条母版」,`message` 放人类可读的进度文案(「正在出图 3/4 · 分镜 2」)。
|
||||
生成接口从「同步跑完再返回」改成 **202 + 排队**:
|
||||
`POST /api/platform/{owner_type}/{asset_id}/generate` 立刻返回整条 task dict,
|
||||
真正的执行走 `jobs/handlers.py`,与调度台共用 `dispatch_task` 和 `io` 队列。
|
||||
**同步接口留在 `services/platform_generation.py::generate_*` 里**给测试与内部调用用,
|
||||
接口层只是不再有人等着它返回。
|
||||
|
||||
**② 全站只有一个地方展示进度:顶栏的活动中心。**
|
||||
`GET /api/tasks/activity` 返回 `tasks / active / counts`(只回 6 小时内的活,上限 40 条),
|
||||
`app/layout/ActivityCenter.tsx` 订它:**有活在跑**时按钮变成 `<Spinner />` + 「生成中」+ 数量 `Badge`,
|
||||
**空闲时**是 `IconClock` + 「平台动态」,永远占同一个位置(按钮不随状态出现或消失,
|
||||
顶栏宽度就不抖)。抽屉分「进行中 / 最近落定」两段,每行给:名字、`<Status>`、
|
||||
`message`、`ProgressBar`、`provider/model · 已用多久`,动作是取消 / 重试 / 查看。
|
||||
`stale`(心跳超时)单独用 warn 档,**卡在 90% 不动**和**失败**是两件事,别混成一个红。
|
||||
|
||||
「查看」跳 `/assets?type=…&q=…`,资产库是唯一认这份深链的人:读完立刻
|
||||
`clearDeepLink({},{replace:true})` 把 query 擦掉——否则同一颗胶囊连点两次,
|
||||
URL 没变,effect 不再触发。
|
||||
|
||||
**③ 「自动刷新」开关只管页面重拉数据,不管进度轮询。**
|
||||
开关(`Check`,存 `nex.autoRefresh`,默认开)落在活动中心的吸底条上,
|
||||
一句话说明白了它管什么:**关掉只停「页面自己重拉列表」,顶栏进度照旧在跑**。
|
||||
- 受它管的:`useAutoRefresh` 的兜底周期、`usePolling`(`intervalMs` 那个定时器)。
|
||||
- 不受它管的:活动中心的 3 秒轮询。关掉自动刷新不等于让平台停止干活——
|
||||
任务还在跑,进度当然还得看得见,否则关掉之后连「跑到哪了」都查不到。
|
||||
|
||||
那颗「立即刷新」按钮**绕开这个开关**(`stores/activity.ts::refreshNow` → `manualTick` +1):
|
||||
开关管的是「页面自己偷偷重拉」,人已经按下按钮了还要先看开关脸色,等于这颗按钮白点——
|
||||
开关关掉期间落定的那几笔,正是靠它补上的。`useRefreshOnActivity` 因此订两个水位:
|
||||
`revision`(落定,受开关管)与 `manualTick`(手动,不受开关管)。
|
||||
双向判据(`/api/platform/assets` 的请求计数 + 卡上定妆图的 `version`)实测:
|
||||
开关 ON 时任务落定后计数 1→2、卡图 v10→v11(没人按 F5);
|
||||
开关 OFF 时落定后 14 秒(覆盖 3s 快轮询 + 20s 兜底周期)计数恒为 3、数据停在旧快照,
|
||||
点「立即刷新」立刻 3→4 并补回 v12。**两头都要量**:只验 A 侧,
|
||||
「开关是个摆设」这种实现照样全绿。
|
||||
|
||||
连线是 `stores/activity.ts` 的 `revision`:某条任务从进行中变成落定,`revision += 1`,
|
||||
挂了的页面各自重拉一次(`hooks/use-refresh-on-activity.ts`)。**落定那一刻刷**比任何
|
||||
固定周期都准,也只在真要刷的时候刷。两条实现细节:
|
||||
- 首帧不刷:`seen` 初值 `-1`,挂载时只把当前 `revision` 记成基线。
|
||||
页面自己刚拉过一次,`revision` 里还带着历史落定数,不记基线就会一进页面就刷两下。
|
||||
- 开关关掉时**照样推进水位**:否则重新打开的瞬间会补放积压的那几笔,
|
||||
页面会在人没反应的时候自己连刷好几下。
|
||||
|
||||
**卡底动作的配套改动**(用户第 1 条:「类型和上传、引入是配对的,但 AI 生成是单独的逻辑」):
|
||||
母版卡底部从两轨改**三轨** `grid-cols-[auto_minmax(0,1fr)_auto]`——
|
||||
|
||||
| 轨 | 内容 | 为什么 |
|
||||
|---|---|---|
|
||||
| 左 | 只有「AI 生成」 | 它是「让模型跑一张」,走排队任务,结果什么时候回来不由这张卡决定 |
|
||||
| 中 | `ActionGroup[类型 Select + 上传 (+ 录制)]` + `ActionGroup[引入项目 + 删除]` | 前两颗是同一个动作的两半(传成哪一类 → 传),后两颗是母版级的 |
|
||||
| 右 | 版本 / 更新时间 | 元数据不是动作,永远不换到动作下面(§7.1) |
|
||||
|
||||
「有文件」那一侧左缘那条 `border-s border-fill-secondary` 是**分界线不是装饰**:画在中轨
|
||||
而不是左轨右缘,是为了让它的长度天然等于被分开的那组动作的高度,不必再凑 `items-stretch`
|
||||
+ `self-center`(那两个工具类还会在 `classaudit` 里新引入 `self` 这个工具类根,
|
||||
把 `UserAdmin.tsx` 里那个同名变量 `self` 误判成死类名)。
|
||||
`max-lg` 单列时连同 `ps-3` 一起撤掉(`max-lg:border-s-0 max-lg:ps-0`)——竖线在只有一列的时候没有分界对象。
|
||||
生成中这张卡:按钮 `loading`、卡头多一枚 `Tag tone="accent" icon={<Spinner />}`
|
||||
写「生成中 · 已用 Xs」。卡片不必自己记「我刚点了哪一张」——`byOwner` 是顶栏那份任务表
|
||||
按 owner 建的索引,切换筛选、翻页、甚至整页刷新之后,正在生成那张卡照样亮着。
|
||||
列表视图的动作列同一套分轨(竖排三段,中间一条 `border-t` 就是那条分界线的竖排写法)。
|
||||
|
||||
**探针侧新增的盲区**:三轨栅格让 `btnclustercheck` 的「父元素是 grid → 不是它的对象」
|
||||
这条豁免正好接管卡底(三条轨各是 grid item),于是它量不到轨内。轨内的按钮各自
|
||||
关在 `.nex-action-group` 里,归 `btnrowcheck` 管。**别为了过闸把 ActionGroup 拆成裸按钮**——
|
||||
拆了以后它们成了 grid item 的兄弟,两道闸都扫不到,反而把盲区放大。
|
||||
|
||||
---
|
||||
|
||||
## 14. 说明文字与表单增删(G1/G2/G3/G4/G5)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import { Navigate, Outlet, useLocation, useMatches } from 'react-router-dom'
|
|||
import { Button, Flex, Layout, Spin } from 'antd'
|
||||
import { Notice } from '../components/ui'
|
||||
import { IconReload } from '../components/icons'
|
||||
import { startActivityFeed, stopActivityFeed } from '../stores/activity'
|
||||
import { authState, refreshState, useAuthStore, useIsAuthenticated } from '../stores/auth'
|
||||
import { loadSystem, useSystemStore } from '../stores/system'
|
||||
import { toggleTheme } from '../stores/theme'
|
||||
|
|
@ -108,6 +109,15 @@ export default function App() {
|
|||
}
|
||||
}, [chromeless, isAuthenticated])
|
||||
|
||||
/* 顶栏「平台动态」的订阅:整站只有这一条任务表轮询(有活 3s / 空闲 15s),
|
||||
各页面的自动刷新都挂在它落定的那一刻,不再各拉各的。
|
||||
登录页不订阅:没鉴权时 /api/tasks/activity 会 401,那条 401 还会把人
|
||||
从登录页踢去 next= 一圈(AuthGate 的 401 回调),像登录页自己抽风。 */
|
||||
useEffect(() => {
|
||||
if (chromeless || !isAuthenticated) stopActivityFeed()
|
||||
else startActivityFeed()
|
||||
}, [chromeless, isAuthenticated])
|
||||
|
||||
/* 全局快捷键:⌘/Ctrl+Shift+L 切明暗;输入框里打字时不抢键 */
|
||||
useEffect(() => {
|
||||
function onKey(event: KeyboardEvent) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,203 @@
|
|||
/**
|
||||
* 顶栏「平台动态」:全站所有跑模型的任务在同一处看得见、点得动。
|
||||
*
|
||||
* 症状(用户原话三条,2026-11):
|
||||
* · 「当有 AI 正在生成资产时,没有地方可以看到平台正在做的操作」——
|
||||
* 出图 40 秒、出片 6 分钟,界面却只有一个转圈的按钮,甚至常常连它都没有;
|
||||
* · 「很多资产的生成需要手动刷新页面」——生成早落定了,列表还停在点按钮之前;
|
||||
* · 「是否应该有个统一的自动刷新开关」——刷新这件事此前散在 12 个页面里,
|
||||
* 谁也关不掉。
|
||||
* 做法:后端把每一次跑模型都登记进统一任务表(``tasks``,平台生成与项目出片同源),
|
||||
* 这一颗按钮是它的订阅端:数字 = 此刻进行中的条数,点开是全部进行中 +
|
||||
* 最近 6 小时落定的流水,每条能取消 / 重试 / 跳回它所属的资产或项目;
|
||||
* 全站自动刷新开关也落在这块面板上——它是「刷新策略」的唯一入口。
|
||||
*
|
||||
* 为什么不放进侧栏或者做成一张页:这不是一个功能模块,是常驻的运行状态,
|
||||
* 和顶栏已有的「在线/离线」是同一族信息(AppHeader 右侧那段)。
|
||||
* 数据由 stores/activity 单例轮询供给(有活 3 秒、空闲 15 秒),
|
||||
* 这里只读 store,不再发第二份请求。
|
||||
*/
|
||||
import { useMemo, useState } from 'react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { Flex, Tooltip, Typography } from 'antd'
|
||||
import { Badge, Button, Check, Drawer, Empty, ProgressBar, Spinner, Status, Tag } from '../../components/ui'
|
||||
import { IconCaution, IconClock, IconClose, IconExternal, IconRetry } from '../../components/icons'
|
||||
import { fmtAgo, fmtMs } from '../../lib/format'
|
||||
import { cancel, refreshNow, retry, setAutoRefresh, useActivityStore, type ActivityTask } from '../../stores/activity'
|
||||
import { notifyBad } from '../../stores/notice'
|
||||
|
||||
const ACTIVE = ['queued', 'running', 'retrying']
|
||||
|
||||
/** 任务类型 → 一句话:面板上一眼分得清这是出图、出片还是配音 */
|
||||
const KIND_LABEL: Record<string, string> = {
|
||||
platform_asset: '平台资产生成', generate_shot: '镜头视频', voiceover: '配音',
|
||||
qc_shot: '质量检查', compose: '时间线合成', asset: '素材处理', import: '导入'
|
||||
}
|
||||
|
||||
/** 母版生成跳回资产库并带上它的分类与名字;项目任务跳回调度中心 */
|
||||
function targetOf(task: ActivityTask) {
|
||||
if (task.owner_type && task.owner_id) {
|
||||
// 搜索词用母版名(登记时顺手存在 payload 里),不是 task.name:
|
||||
// 「生成「俊杰」的定妆图」拿去搜资产库永远 0 条。
|
||||
// 改名之后点「查看」会搜不到——那时落在正确的分类里也比停在原页面有用。
|
||||
const q = new URLSearchParams({ type: task.owner_type })
|
||||
const name = String(task.payload?.owner_name || '').trim()
|
||||
if (name) q.set('q', name)
|
||||
return `/assets?${q.toString()}`
|
||||
}
|
||||
if (task.project_id) return `/projects/${task.project_id}/dispatch`
|
||||
return ''
|
||||
}
|
||||
|
||||
function Row({ task }: { task: ActivityTask }) {
|
||||
const navigate = useNavigate()
|
||||
const [busy, setBusy] = useState('')
|
||||
const active = ACTIVE.includes(task.status)
|
||||
const href = targetOf(task)
|
||||
const run = async (what: string, fn: () => Promise<unknown>) => {
|
||||
setBusy(what)
|
||||
try { await fn() } catch (exc: any) { notifyBad({ title: what + '失败', desc: exc?.message || String(exc) }) } finally { setBusy('') }
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-w-0 rounded-lg border border-fill-secondary p-3">
|
||||
{/* 第一行只回答「是哪件事、到哪一步了」:标题 + 状态,图标负责跑动 */}
|
||||
<Flex align="center" gap={8} className="min-w-0">
|
||||
{active && <Spinner className="flex-none" />}
|
||||
<Typography.Text strong ellipsis={{ tooltip: task.name }} className="min-w-0 flex-1">
|
||||
{task.name}
|
||||
</Typography.Text>
|
||||
<Status value={task.status} size="sm" className="flex-none" />
|
||||
</Flex>
|
||||
|
||||
{/* 第二行是执行体自己写的那句「此刻在做什么」——排队、传参考图、
|
||||
等算力锁、下载回传都在这一行,用户不必猜那 40 秒里发生了什么 */}
|
||||
<Typography.Text type="secondary" ellipsis={{ tooltip: task.message || undefined }}
|
||||
className="mt-1 block min-w-0 text-xs">
|
||||
{task.message || KIND_LABEL[task.kind] || task.kind}
|
||||
</Typography.Text>
|
||||
|
||||
{active && task.progress > 0 && <ProgressBar value={Math.round(task.progress * 100)} className="!mb-0" />}
|
||||
|
||||
{/* 第三行:元数据 + 动作。元数据是「谁在跑、跑了多久」,
|
||||
动作只有取消 / 重试 / 跳过去看,全部是 text 档,不跟标题抢注意力 */}
|
||||
<Flex align="center" gap={8} wrap className="mt-2 min-w-0 text-xs text-text-quaternary">
|
||||
<span className="min-w-0 truncate">
|
||||
{(task.provider || task.model) ? `${task.provider}/${task.model}` : KIND_LABEL[task.kind] || task.kind}
|
||||
{/* 进行中说「已用多久」,落定说「什么时候的事」——同一句「已用 刚刚」两头不靠 */}
|
||||
{active ? ` · 已用 ${fmtMs(task.elapsed_ms)}` : ` · ${fmtAgo(task.finished_at || task.updated_at)}`}
|
||||
{task.attempts > 1 ? ` · 第 ${task.attempts} 次` : ''}
|
||||
</span>
|
||||
{task.stale && active && (
|
||||
<Tag size="sm" tone="warn" icon={<IconCaution />}
|
||||
title={`执行体心跳已停止 ${Math.round((task.heartbeat_age_seconds ?? 0) / 60)} 分钟,平台会自动接管续跑`}>
|
||||
心跳中断
|
||||
</Tag>
|
||||
)}
|
||||
<span className="ml-auto inline-flex min-w-0 items-center gap-1">
|
||||
{active && (
|
||||
<Button type="text" danger icon={<IconClose />} title="取消这次生成(已花费的算力不退还)"
|
||||
loading={busy === '取消'} onClick={() => void run('取消', () => cancel(task))}>
|
||||
取消
|
||||
</Button>
|
||||
)}
|
||||
{!active && (task.status === 'failed' || task.status === 'cancelled') && (
|
||||
<Button type="text" icon={<IconRetry />} title="按原参数重新排队"
|
||||
loading={busy === '重试'} onClick={() => void run('重试', () => retry(task))}>
|
||||
重试
|
||||
</Button>
|
||||
)}
|
||||
{href && (
|
||||
<Button type="text" icon={<IconExternal />} title="跳到它所在的页面"
|
||||
onClick={() => { navigate(href) }}>
|
||||
查看
|
||||
</Button>
|
||||
)}
|
||||
</span>
|
||||
</Flex>
|
||||
|
||||
{task.status === 'failed' && task.error_message && (
|
||||
<Typography.Text type="danger" className="mt-1 block min-w-0 text-xs wrap-anywhere">
|
||||
{task.error_code ? `${task.error_code}:` : ''}{task.error_message}
|
||||
</Typography.Text>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function ActivityCenter() {
|
||||
const [open, setOpen] = useState(false)
|
||||
const tasks = useActivityStore((s) => s.tasks)
|
||||
const active = useActivityStore((s) => s.active)
|
||||
const autoRefresh = useActivityStore((s) => s.autoRefresh)
|
||||
|
||||
const { running, settled } = useMemo(() => ({
|
||||
running: tasks.filter((row) => ACTIVE.includes(row.status)),
|
||||
settled: tasks.filter((row) => !ACTIVE.includes(row.status)).slice(0, 12)
|
||||
}), [tasks])
|
||||
|
||||
const title = active > 0 ? `平台正在执行 ${active} 项任务` : '平台当前空闲'
|
||||
|
||||
return (
|
||||
<>
|
||||
<Tooltip title={active > 0 ? `${title} · 点击查看进度` : '平台动态 · 所有跑模型的任务都在这里'}>
|
||||
{/* 空的时候它只是一颗安静的入口;有活时换转圈 + 计数,
|
||||
计数用 kit 的 Badge(顶栏流程控制台把 Tag 放按钮里是同一族写法) */}
|
||||
<Button type="text" onClick={() => setOpen(true)}
|
||||
aria-haspopup="dialog" aria-expanded={open}
|
||||
icon={active > 0 ? <Spinner /> : <IconClock />}
|
||||
aria-label={title}>
|
||||
<span className="whitespace-nowrap max-xl:hidden">{active > 0 ? '生成中' : '平台动态'}</span>
|
||||
{active > 0 && <Badge tone="accent" size="sm" className="ms-1">{active}</Badge>}
|
||||
</Button>
|
||||
</Tooltip>
|
||||
|
||||
<Drawer open={open} onClose={() => setOpen(false)} width={520} label="平台动态"
|
||||
title="平台动态"
|
||||
sub="所有跑模型的任务都在同一张任务表里排队:这里看得到它正在做什么、跑到哪、能不能停。"
|
||||
actions={
|
||||
<>
|
||||
<Check checked={autoRefresh} className="mr-auto !mb-0"
|
||||
title="关掉后页面不再自己重拉列表;任务进度照常更新"
|
||||
onChange={setAutoRefresh}>
|
||||
自动刷新页面
|
||||
</Check>
|
||||
<Button icon={<IconRetry />} onClick={() => void refreshNow()}>立即刷新</Button>
|
||||
</>
|
||||
}>
|
||||
<div className="flex min-w-0 flex-col gap-3">
|
||||
<div className="min-w-0">
|
||||
<Flex align="center" gap={8} className="min-w-0 text-xs font-bold tracking-wide text-text-quaternary">
|
||||
<span>进行中</span>
|
||||
<Badge tone={running.length ? 'accent' : 'default'} size="sm">{running.length}</Badge>
|
||||
</Flex>
|
||||
<div className="mt-2 flex min-w-0 flex-col gap-2">
|
||||
{running.map((task) => <Row key={task.id} task={task} />)}
|
||||
{!running.length && (
|
||||
<div className="min-w-0 rounded-lg border border-fill-secondary px-3 py-2 text-xs text-text-quaternary">
|
||||
此刻没有任务在跑。发起一次生成,它会立刻出现在这里。
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{!!settled.length && (
|
||||
<div className="min-w-0">
|
||||
<Flex align="center" gap={8} className="min-w-0 text-xs font-bold tracking-wide text-text-quaternary">
|
||||
<span>最近落定</span>
|
||||
<Badge size="sm">{settled.length}</Badge>
|
||||
</Flex>
|
||||
<div className="mt-2 flex min-w-0 flex-col gap-2">
|
||||
{settled.map((task) => <Row key={task.id} task={task} />)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!tasks.length && <Empty>还没有生成记录</Empty>}
|
||||
</div>
|
||||
</Drawer>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default ActivityCenter
|
||||
|
|
@ -19,6 +19,7 @@ import { IconBook, IconMoon, IconSiderCollapse, IconSiderExpand, IconSun } from
|
|||
import { cn } from '../../lib/cn'
|
||||
import { toggleTheme, useAppliedTheme } from '../../stores/theme'
|
||||
import { useSystemStore } from '../../stores/system'
|
||||
import ActivityCenter from './ActivityCenter'
|
||||
import PipelineConsole from './PipelineConsole'
|
||||
import UserMenu from './UserMenu'
|
||||
|
||||
|
|
@ -91,6 +92,10 @@ export function AppHeader({ projectId, collapsed, onCollapse, onOpenBrief, brief
|
|||
<Badge status="error" text={<span className="whitespace-nowrap max-xl:hidden">后端离线</span>} className="max-lg:hidden" />
|
||||
</Tooltip>
|
||||
)}
|
||||
{/* 平台动态(跑模型的任务在做什么)不跟着 md 让位:
|
||||
窄屏它自己收成「转圈 + 计数」一颗图标,而它报的是「机器正占着活」——
|
||||
这恰恰是手机上看板最需要知道的事,隐掉等于让人白等一次生成。 */}
|
||||
<ActivityCenter />
|
||||
{md && (
|
||||
<Button type="text" icon={<IconBook />} href="/help" target="_blank" rel="noreferrer"
|
||||
title="使用手册 · 全流程操作截图(新窗口打开)">
|
||||
|
|
|
|||
|
|
@ -2,3 +2,4 @@ export * from './use-async'
|
|||
export * from './use-polling'
|
||||
export * from './use-local-storage'
|
||||
export * from './use-auto-refresh'
|
||||
export * from './use-refresh-on-activity'
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { useEffect, useRef } from 'react'
|
||||
import { useActivityStore } from '../stores/activity'
|
||||
|
||||
/**
|
||||
* 自动刷新轮询 —— 旧站它是渲染不出任何界面的 <AutoRefresh/> 组件,
|
||||
|
|
@ -7,11 +8,17 @@ import { useEffect, useRef } from 'react'
|
|||
* · `every` —— 轮询周期(毫秒);由别处(如项目 store 每 6 秒)负责刷新的页面不挂它;
|
||||
* · `busy` —— 正在请求 / 正在生成时跳过本轮,不抢占;
|
||||
* · `paused` —— 表单抽屉、确认弹窗里有没保存的内容时跳过本轮;
|
||||
* · `autoRefresh` —— 顶栏「平台动态」里那个全站开关:关掉就不排定时器;
|
||||
* · 标签页切到后台不发请求;切回来发现已经过期一个周期,立刻补一次。
|
||||
*
|
||||
* 数据到点自己刷新是平台的责任,页面上不需要留一行时钟让用户确认数据新不新。
|
||||
*
|
||||
* 为什么开关要下沉到钩子里:逐个页面传 `enabled` 的话,第 13 个页面一定会忘,
|
||||
* 而「我已经关了自动刷新,这个页面还在偷偷重渲染」比没有开关更糟
|
||||
* ——抽屉刚填一半就被列表重建拔掉,正是旧站最常被抱怨的那件事。
|
||||
*/
|
||||
export function useAutoRefresh(onRefresh: () => void, every = 15000, busy = false, paused = false) {
|
||||
const autoRefresh = useActivityStore((s) => s.autoRefresh)
|
||||
const lastAttempt = useRef(Date.now())
|
||||
const cb = useRef(onRefresh)
|
||||
cb.current = onRefresh
|
||||
|
|
@ -20,6 +27,7 @@ export function useAutoRefresh(onRefresh: () => void, every = 15000, busy = fals
|
|||
|
||||
useEffect(() => {
|
||||
let timer = 0
|
||||
if (!autoRefresh) return
|
||||
const tick = () => {
|
||||
const g = gate.current
|
||||
if (g.busy || g.paused || !g.every || document.hidden) return
|
||||
|
|
@ -39,5 +47,5 @@ export function useAutoRefresh(onRefresh: () => void, every = 15000, busy = fals
|
|||
if (timer) window.clearInterval(timer)
|
||||
document.removeEventListener('visibilitychange', onVisible)
|
||||
}
|
||||
}, [every])
|
||||
}, [every, autoRefresh])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,16 +5,19 @@
|
|||
* 否则刷新一重建列表,抽屉里刚填一半的表单会被连根拔掉。
|
||||
*/
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { useActivityStore } from '../stores/activity'
|
||||
|
||||
export function usePolling(tick: () => void, intervalMs = 6000, paused = false, enabled = true) {
|
||||
const latest = useRef(tick)
|
||||
latest.current = tick
|
||||
/* 与 useAutoRefresh 共用顶栏那个全站开关:语义都是「页面自己重拉数据」 */
|
||||
const autoRefresh = useActivityStore((s) => s.autoRefresh)
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || intervalMs <= 0) return
|
||||
if (!enabled || !autoRefresh || intervalMs <= 0) return
|
||||
const timer = window.setInterval(() => {
|
||||
if (!paused && document.visibilityState === 'visible') latest.current()
|
||||
}, intervalMs)
|
||||
return () => window.clearInterval(timer)
|
||||
}, [intervalMs, paused, enabled])
|
||||
}, [intervalMs, paused, enabled, autoRefresh])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* 「有活落定了就重拉一次」——页面列表与统一任务表之间的唯一连线。
|
||||
*
|
||||
* 为什么不是把轮询间隔调短(用户原话「很多资产的生成需要手动刷新页面」):
|
||||
* 出图 40 秒、出片 6 分钟,把周期调到 3 秒意味着 95% 的请求都在问同一份
|
||||
* 没人改过的数据;不调整的话用户就只能自己按 F5 猜它好了没有。
|
||||
* 现在改由活动面板通知:顶栏那条订阅发现某条任务从「进行中」变成
|
||||
* 「成功 / 失败」,把 revision +1,所有挂了这根钩子的页面各自重拉一次。
|
||||
* 落定那一刻刷,比任何固定周期都准,也只在真要刷的时候刷。
|
||||
*
|
||||
* 与 useAutoRefresh 的分工:兜底周期照旧留着(活动面板挂了、任务在别的
|
||||
* 进程里落定,都还得靠它收敛),这里加的是「立刻」那一半。
|
||||
* 首帧不触发:挂载时页面自己已经拉过一次,revision 也带着历史落定数。
|
||||
*/
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { useActivityStore } from '../stores/activity'
|
||||
|
||||
export function useRefreshOnActivity(onRefresh: () => void) {
|
||||
const revision = useActivityStore((s) => s.revision)
|
||||
const manualTick = useActivityStore((s) => s.manualTick)
|
||||
const autoRefresh = useActivityStore((s) => s.autoRefresh)
|
||||
const cb = useRef(onRefresh)
|
||||
cb.current = onRefresh
|
||||
/** 已经处理过的水位:挂载时页面自己已经拉过一次,历史落定不算新事件 */
|
||||
const seen = useRef({ revision: -1, manualTick: -1 })
|
||||
|
||||
useEffect(() => {
|
||||
const was = seen.current
|
||||
if (was.revision < 0) { seen.current = { revision, manualTick }; return }
|
||||
const landed = revision !== was.revision
|
||||
const manual = manualTick !== was.manualTick
|
||||
if (!landed && !manual) return
|
||||
seen.current = { revision, manualTick }
|
||||
// 开关关掉时仍要把落定水位推进去:否则重新打开的瞬间会补放积压的那几笔,
|
||||
// 页面会在人没反应的时候自己刷新好几下。手动那一笔不受开关管辖。
|
||||
if (!autoRefresh && !manual) return
|
||||
cb.current()
|
||||
}, [revision, manualTick, autoRefresh])
|
||||
}
|
||||
|
|
@ -24,9 +24,9 @@
|
|||
* 旧站那张「角标被删除 × 压住」的补丁(把角标让到 right:25px)可以退役了。
|
||||
*/
|
||||
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { Link, useSearchParams } from 'react-router-dom'
|
||||
import * as api from '../api/client'
|
||||
import { fmtAgo, fmtBytes, opt, statusLabel } from '../lib/format'
|
||||
import { fmtAgo, fmtBytes, fmtMs, opt, statusLabel } from '../lib/format'
|
||||
import { fromAsset, isAudio as audioOf, isImage as imageOf, type MediaItem } from '../lib/media'
|
||||
import { defaultAssetName, existingNames, keepOrRestoreCurrent } from '../lib/versions'
|
||||
import { cn } from '../lib/cn'
|
||||
|
|
@ -39,9 +39,11 @@ import { MultiSelect, Select } from '../components/forms'
|
|||
import { AudioPlay } from '../components/media/AudioPlay'
|
||||
import { MediaPick, MediaRemove, MediaThumb, type MediaRadius } from '../components/media/MediaThumb'
|
||||
import { RecordAudio, type RecordPickMeta } from '../components/media/RecordAudio'
|
||||
import { useAutoRefresh } from '../hooks'
|
||||
import { useAutoRefresh, useRefreshOnActivity } from '../hooks'
|
||||
import { useRecordingBusy } from '../stores/recording'
|
||||
import { confirmAction } from '../stores/confirm'
|
||||
import { notify } from '../stores/notice'
|
||||
import { ownerKey, track, useActivityStore } from '../stores/activity'
|
||||
import { ActionGroup, BarRight, Between, Block, Button, CardActions, Check, Checkbox, Chip, ChipBar, Ctlbar, Drawer, Empty, ErrorText, Field, FieldBlock, FilePick, FormGrid, FormRow, Grid, Input, LINK_CLS, MONO, MUTED, MUTED_MONO, Modal, Notice, NumberInput, PageHead, Pager, PageStack, Panel, PanelHead, Row, SearchInput, SectionTitle, Spinner, Stat, StatGrid, Tabs, Tag, TagGroup, TextArea, Tile, Value, ViewSwitch } from '../components/ui'
|
||||
import { IconAdd, IconBlocks, IconBolt, IconCaution, IconCheck, IconClose, IconCopy, IconDelete, IconEdit, IconGrid, IconImage, IconLibrary, IconList, IconRobot, IconSound, IconTag, IconUpload, IconUser, IconVoice } from '../components/icons'
|
||||
|
||||
|
|
@ -273,6 +275,30 @@ export default function AssetLibrary() {
|
|||
/** 有弹窗 / 抽屉 / 批量确认挡在页面上时暂停自动刷新,避免列表在用户手下换人 */
|
||||
const modalOpen = drawer || genDrawer || !!confirmDelete || importTargets.length > 0 || !!batchDialog
|
||||
|
||||
/**
|
||||
* 「这条母版有一次生成正在跑」——顶栏平台动态那份任务表按 owner 建的索引。
|
||||
*
|
||||
* 有了它,卡片不必再自己记「我刚点了哪一张」:切换筛选、翻页、
|
||||
* 甚至刷新整页之后,正在生成的那张卡照样亮着、照样显示已经跑了多久,
|
||||
* 而按钮自己变灰(后端对同一条母版的重复生成会回 409,界面上先挡住)。
|
||||
*/
|
||||
const byOwner = useActivityStore((s) => s.byOwner)
|
||||
const generatingOf = (row: any) => byOwner[ownerKey(type, row?.id)] || null
|
||||
|
||||
/* 顶栏「平台动态」的「查看」跳回来时带上分类与名字(/assets?type=voice&q=…):
|
||||
这里是唯一认这份参数的人,读完立刻把 query 擦掉,
|
||||
否则同一颗胶囊连点两次会因为 URL 没变而不再响应。 */
|
||||
const [deepLink, clearDeepLink] = useSearchParams()
|
||||
useEffect(() => {
|
||||
const nextType = String(deepLink.get('type') || '') as AssetType
|
||||
const needle = String(deepLink.get('q') || '').trim()
|
||||
if (!TYPE_KEYS.includes(nextType) && !needle) return
|
||||
if (TYPE_KEYS.includes(nextType)) setType(nextType)
|
||||
if (needle) { setSearch(needle); setKeyword(needle) }
|
||||
clearDeepLink({}, { replace: true })
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [deepLink])
|
||||
|
||||
/* ── 派生:统计、分页口径、筛选项 ─────────────────────────────── */
|
||||
const totals = useMemo(() => {
|
||||
const byOwner = stats.by_owner || {}
|
||||
|
|
@ -421,7 +447,10 @@ export default function AssetLibrary() {
|
|||
return () => window.removeEventListener('nex:media-changed', onMediaChanged)
|
||||
}, [])
|
||||
|
||||
/* 兜底周期 + 事件即时两段:任务落定的那一刻由活动面板通知(生成完不用人再刷新),
|
||||
20 秒那一条负责收住「面板没看到的那笔」(比如别人在另一台机器上跑的)。 */
|
||||
useAutoRefresh(() => void reloadRef.current(), 20000, !!busy, modalOpen || recordingBusy)
|
||||
useRefreshOnActivity(() => void reloadRef.current())
|
||||
|
||||
/**
|
||||
* 关键词防抖:输入框每敲一个字就发一次请求的话,中文输入法连发半截拼音时
|
||||
|
|
@ -974,11 +1003,15 @@ export default function AssetLibrary() {
|
|||
setBusy(`生成${TYPE_META[type].label}资产`)
|
||||
setError('')
|
||||
try {
|
||||
const media: any = await api.post(`/api/platform/${type}/${row.id}/generate`, body)
|
||||
setNotice(`${row.name} 的${type === 'voice' ? '试听样本' : opt('asset_kinds', draft.kind)}`
|
||||
+ `已生成(${spec.provider}/${spec.model}${media?.id ? ` · v${media.version}` : ''})`)
|
||||
await loadList()
|
||||
if (view === 'media') await loadWall()
|
||||
// 202 + 任务号:出图在线程里跑,页面不等它(以前在这儿 await 四十秒,
|
||||
// 反代 60 秒一到就是一屏 504,而图其实已经在磁盘上了)
|
||||
const task: any = await api.post(`/api/platform/${type}/${row.id}/generate`, body)
|
||||
track(task)
|
||||
notify({
|
||||
title: `${row.name} 已排进队列`,
|
||||
desc: `${spec.provider}/${spec.model} · 顶栏「平台动态」看进度,跑完列表自己刷新`
|
||||
})
|
||||
setGenDrawer(false)
|
||||
} catch (exc: any) {
|
||||
setError(exc?.message || String(exc))
|
||||
} finally {
|
||||
|
|
@ -1238,6 +1271,10 @@ export default function AssetLibrary() {
|
|||
</Tag>
|
||||
)}
|
||||
{!row.media_count && <Tag tone="warn">待补素材</Tag>}
|
||||
{!!generatingOf(row) && (<Tag tone="accent" icon={<Spinner />}
|
||||
title={generatingOf(row).message || '正在生成,进度见顶栏「平台动态」'}>
|
||||
生成中 · {fmtMs(generatingOf(row).elapsed_ms)}
|
||||
</Tag>)}
|
||||
</div>
|
||||
<p className="mt-1 mb-1 line-clamp-1 text-xs leading-normal text-text-tertiary">
|
||||
{row.persona_prompt || row.appearance || row.description || row.prompt || row.voice_profile || '尚未填写设定。'}
|
||||
|
|
@ -1268,17 +1305,22 @@ export default function AssetLibrary() {
|
|||
))}
|
||||
{!row.assets?.length && <span className={cn(MUTED, 'text-xs')}>暂无素材</span>}
|
||||
</div>
|
||||
<div className="flex flex-col gap-1 w-[132px] flex-none items-stretch ">
|
||||
<Button type="primary" block onClick={() => openGenerate(row)} icon={<IconBolt />}>
|
||||
{/* 动作列和网格卡底共用同一套分轨:AI 生成自己一段(下面一条横线
|
||||
就是分界线),「我自己有文件」的上传一段,母版级的
|
||||
引入 / 编辑 / 删除一段。以前上传和引入缝在同一行,
|
||||
一个是对这张卡加版本、一个是把这张卡搬进项目,毫无关系。 */}
|
||||
<div className="flex w-[168px] flex-none flex-col items-stretch gap-1.5">
|
||||
<Button type="primary" block loading={!!generatingOf(row)}
|
||||
onClick={() => openGenerate(row)} icon={<IconBolt />}
|
||||
title={generatingOf(row) ? generatingOf(row).message || '这条母版有一次生成正在排队' : undefined}>
|
||||
{isVoice ? '生成试听' : 'AI 生成'}
|
||||
</Button>
|
||||
<div className="flex gap-1 [&>*]:flex-1">
|
||||
<FilePick icon=<IconUpload /> label="上传" title="选完即作为新版本登记到这个母版下"
|
||||
onFiles={(files) => files.forEach((file) => void uploadFile(row, file, uploadKindFor(row)))} />
|
||||
<FilePick className="w-full" icon=<IconUpload /> label="上传"
|
||||
title="选完即作为新版本登记到这个母版下"
|
||||
onFiles={(files) => files.forEach((file) => void uploadFile(row, file, uploadKindFor(row)))} />
|
||||
<div className="flex gap-1 border-t border-fill-secondary pt-1.5 [&>*]:flex-1">
|
||||
<Button icon={<IconLibrary />} onClick={() => openImportSelection(row)}>引入</Button>
|
||||
</div>
|
||||
<div className="flex gap-1 [&>*]:flex-1">
|
||||
<Button icon={<IconEdit />} onClick={() => openEdit(row)}>编辑</Button>
|
||||
<Button icon={<IconEdit />} title="编辑这个母版" onClick={() => openEdit(row)} aria-label="编辑这个母版" />
|
||||
<Button icon={<IconDelete />} type="text" danger title="删除这个资源" onClick={() => setConfirmDelete(row)} aria-label="删除这个资源" />
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -1293,6 +1335,7 @@ export default function AssetLibrary() {
|
|||
<Grid cols={2} gap={10} oneAt="xl" className="[&>.ant-col>*]:h-full">
|
||||
{rows.map((row: any, idx: number) => {
|
||||
const audios = (row.assets || []).filter(isAudio)
|
||||
const generating = generatingOf(row)
|
||||
return (
|
||||
<Tile key={row.id} as="div" variant="pick" active={isSelected(row)}
|
||||
className={cn('relative flex-col items-stretch gap-3 p-4',
|
||||
|
|
@ -1315,6 +1358,10 @@ export default function AssetLibrary() {
|
|||
</Tag>
|
||||
)}
|
||||
{!row.media_count && <Tag tone="warn">待补素材</Tag>}
|
||||
{!!generating && (<Tag tone="accent" icon={<Spinner />}
|
||||
title={generating.message || '正在生成,进度见顶栏「平台动态」'}>
|
||||
生成中 · {fmtMs(generating.elapsed_ms)}
|
||||
</Tag>)}
|
||||
</div>
|
||||
<div className={MUTED_MONO}>{row.code || `${type}-${row.id}`}</div>
|
||||
</div>
|
||||
|
|
@ -1388,20 +1435,30 @@ export default function AssetLibrary() {
|
|||
)
|
||||
})}
|
||||
{!row.assets?.length && (
|
||||
<div className="flex min-h-[46px] items-center gap-2 rounded-md bg-fill-secondary px-3 py-2 text-xs leading-normal text-text-quaternary">
|
||||
<div className="flex min-h-[46px] items-center gap-2 rounded-md bg-fill-secondary px-3 py-2 text-xs leading-normal text-text-quaternary">
|
||||
<IconImage className="flex-none" />
|
||||
<span className="min-w-0">暂无素材:用下方「AI 生成」出图 / 出试听,或直接上传现成文件。</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 动作在左、版本元数据在右:钉成两列栅格。
|
||||
以前靠 BarRight 的 ml-auto 自然排列,一超宽那段元数据就独自
|
||||
折到第二行的最右端(实测左边缘比同排按钮低 310px),像掉出去的孤儿。 */}
|
||||
<div className="mt-auto grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 gap-y-2 border-t border-fill-secondary pt-3 max-lg:grid-cols-1">
|
||||
<CardActions className="!mt-0">
|
||||
<Button type="primary" onClick={() => openGenerate(row)} icon={<IconBolt />}>
|
||||
{/* 三轨栅格:左轨只有「AI 生成」,中轨是配对的「类型 + 上传」与
|
||||
母版级的「引入 / 删除」,右轨永远是版本元数据。
|
||||
为什么要把 AI 生成单独隔出来(用户原话「类型和上传、引入是配对的,
|
||||
但 AI 生成是单独的逻辑」):另外两组都是「我手上已经有文件」,
|
||||
选类型 → 上传是同一个动作的两半;AI 生成则是「让模型跑一张」,
|
||||
走的是排队任务,结果什么时候回来不由这张卡决定。
|
||||
六颗按钮缝在一条 Flex 里,主谓关系就得靠读者自己现场解析。
|
||||
中轨左缘那条竖线是分界线,不是装饰:它画在「有文件」这一侧,
|
||||
所以永远贴着被分开的那组动作,长度也等于这组动作的高度。
|
||||
元数据继续钉在最右:以前靠 BarRight 的 ml-auto 自然排列,
|
||||
一超宽它就独自折到第二行最右端(实测比同排按钮低 310px),像掉出去的孤儿。 */}
|
||||
<div className="mt-auto grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-x-3 gap-y-2 border-t border-fill-secondary pt-3 max-lg:grid-cols-1">
|
||||
<Button type="primary" className="justify-self-start" loading={!!generating}
|
||||
onClick={() => openGenerate(row)} icon={<IconBolt />}
|
||||
title={generating ? generating.message || '这条母版有一次生成正在排队' : undefined}>
|
||||
{isVoice ? '生成试听' : 'AI 生成'}
|
||||
</Button>
|
||||
<CardActions className="!mt-0 border-s border-fill-secondary ps-3 max-lg:border-s-0 max-lg:ps-0">
|
||||
{/* 「传成哪一类」和「上传」是同一个动作的两半,拆到两行就读不出主谓 */}
|
||||
<ActionGroup>
|
||||
<Select className="w-[105px]" ariaLabel="选择要上传的资产类型"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,230 @@
|
|||
/**
|
||||
* 「平台此刻在忙什么」——顶栏活动中心的唯一数据源。
|
||||
*
|
||||
* 为什么要单独一层(2026-11 · 手动刷新那一串):
|
||||
* 跑模型的活动辄四十几秒到十几分钟,而它过去只是一次 HTTP 请求。
|
||||
* 反代一超时页面就报 504,出图线程却还在跑;用户既看不见进度,
|
||||
* 也不知道什么时候该刷新页面,只能反复按 F5 去猜。
|
||||
* 后端现在把所有生成登记进统一任务表(``tasks``),
|
||||
* 这一层就是它的订阅端:定时问一次 `/api/tasks/activity`,
|
||||
* 把「进行中的」「刚刚落定的」分开记账,落定那一刻负责
|
||||
* ① 弹一次吐司、② 把 revision +1 让页面自己重新拉列表。
|
||||
*
|
||||
* 自动刷新开关(用户原话「是否应该有个统一的自动刷新开关」):
|
||||
* 开关放在这里而不是某个页面里,因为要管的是全站——
|
||||
* `useAutoRefresh` 与 `useRefreshOnActivity` 都先到这儿来问一句。
|
||||
* 关掉的只是「页面自己重拉数据」,进度本身一直在轮询:
|
||||
* 看不到进度才是这次问题的根,把它一起关掉等于退回手动刷新。
|
||||
*/
|
||||
import { create } from 'zustand'
|
||||
import * as api from '../api/client'
|
||||
import { readLocal, writeLocal } from '../hooks/use-local-storage'
|
||||
import { notify, notifyBad } from './notice'
|
||||
|
||||
/** 与后端 TaskStatus.ACTIVE 对齐:这三态都算「平台还占着活」 */
|
||||
export const ACTIVE_STATUSES = ['queued', 'running', 'retrying']
|
||||
const TERMINAL = ['succeeded', 'failed', 'cancelled', 'skipped']
|
||||
|
||||
/** 顶栏轮询周期:有活时快问,闲下来退到 15s,不给后端添常驻负载 */
|
||||
const FAST_MS = 3000
|
||||
const IDLE_MS = 15000
|
||||
|
||||
export type ActivityTask = {
|
||||
id: number
|
||||
project_id: number | null
|
||||
shot_id: number | null
|
||||
kind: string
|
||||
name: string
|
||||
stage: string
|
||||
owner_type: string
|
||||
owner_id: number | null
|
||||
message: string
|
||||
status: string
|
||||
progress: number
|
||||
provider: string
|
||||
model: string
|
||||
attempts: number
|
||||
error_code: string
|
||||
error_message: string
|
||||
elapsed_ms: number
|
||||
stale?: boolean
|
||||
heartbeat_age_seconds?: number | null
|
||||
finished_at?: string | null
|
||||
updated_at?: string | null
|
||||
result?: any
|
||||
payload?: any
|
||||
}
|
||||
|
||||
type ActivityState = {
|
||||
/** 面板上的全部条目:进行中的按 id 升序,历史按落定时间倒序 */
|
||||
tasks: ActivityTask[]
|
||||
active: number
|
||||
counts: Record<string, number>
|
||||
/** 「这条母版正在生成」的索引:资产卡片点亮按钮、显示「生成中 · 12s」靠它 */
|
||||
byOwner: Record<string, ActivityTask>
|
||||
loaded: boolean
|
||||
error: string
|
||||
/** 每当有任务落定就 +1:页面订阅它,不需要自己算 diff */
|
||||
revision: number
|
||||
/** 全站自动刷新开关(localStorage 记忆,默认开) */
|
||||
autoRefresh: boolean
|
||||
/** 每次点「立即刷新」+1:它绕开开关,手动刷新永远该生效 */
|
||||
manualTick: number
|
||||
set: (patch: Partial<ActivityState>) => void
|
||||
}
|
||||
|
||||
export const ownerKey = (ownerType: string, ownerId: number | string | null | undefined) =>
|
||||
`${ownerType}:${ownerId ?? ''}`
|
||||
|
||||
export const isBusy = (task?: ActivityTask | null) => !!task && ACTIVE_STATUSES.includes(task.status)
|
||||
|
||||
const useActivityStore = create<ActivityState>((set) => ({
|
||||
tasks: [],
|
||||
active: 0,
|
||||
counts: {},
|
||||
byOwner: {},
|
||||
loaded: false,
|
||||
error: '',
|
||||
revision: 0,
|
||||
autoRefresh: readLocal('nex.autoRefresh', true),
|
||||
manualTick: 0,
|
||||
set: (patch) => set(patch)
|
||||
}))
|
||||
|
||||
const index = (tasks: ActivityTask[]) => {
|
||||
const map: Record<string, ActivityTask> = {}
|
||||
tasks.forEach((task) => {
|
||||
if (!task.owner_type || task.owner_id === null || !isBusy(task)) return
|
||||
map[ownerKey(task.owner_type, task.owner_id)] = task
|
||||
})
|
||||
return map
|
||||
}
|
||||
|
||||
/** 已经弹过吐司的落定任务:轮询会反复看见同一条历史记录,不能重复打扰 */
|
||||
const announced = new Set<number>()
|
||||
let primed = false
|
||||
|
||||
/**
|
||||
* 首轮只记账不弹吐司:登录后第一次拉到的往往是「昨天跑完的」那批,
|
||||
* 全弹一遍等于把用户的历史记录甩在他脸上。
|
||||
*/
|
||||
function settle(prev: Record<number, ActivityTask>, next: ActivityTask[]) {
|
||||
let done = 0
|
||||
next.forEach((task) => {
|
||||
const before = prev[task.id]
|
||||
if (!before || !ACTIVE_STATUSES.includes(before.status)) return
|
||||
if (!TERMINAL.includes(task.status)) return
|
||||
done += 1
|
||||
if (announced.has(task.id)) return
|
||||
announced.add(task.id)
|
||||
if (task.status === 'succeeded') {
|
||||
notify({ title: `${task.name}已完成`, desc: `${task.provider}/${task.model}` })
|
||||
} else if (task.status === 'failed') {
|
||||
notifyBad({ title: `${task.name}失败`, desc: task.error_message || task.error_code || '未知原因' })
|
||||
}
|
||||
})
|
||||
return done
|
||||
}
|
||||
|
||||
let timer = 0
|
||||
let inFlight = false
|
||||
|
||||
function schedule() {
|
||||
if (!timer) return
|
||||
window.clearTimeout(timer)
|
||||
const wait = useActivityStore.getState().active > 0 ? FAST_MS : IDLE_MS
|
||||
timer = window.setTimeout(() => { void pollActivity() }, wait)
|
||||
}
|
||||
|
||||
/** 拉一次活动面板;同一时刻只允许有一条在途请求 */
|
||||
async function pollActivity() {
|
||||
if (inFlight) return
|
||||
inFlight = true
|
||||
try {
|
||||
const body: any = await api.get('/api/tasks/activity')
|
||||
const tasks: ActivityTask[] = body?.tasks || []
|
||||
const prev: Record<number, ActivityTask> = {}
|
||||
useActivityStore.getState().tasks.forEach((row) => { prev[row.id] = row })
|
||||
const landed = primed ? settle(prev, tasks) : 0
|
||||
primed = true
|
||||
useActivityStore.getState().set({
|
||||
tasks,
|
||||
active: Number(body?.active ?? tasks.filter((row) => ACTIVE_STATUSES.includes(row.status)).length),
|
||||
counts: body?.counts || {},
|
||||
byOwner: index(tasks),
|
||||
loaded: true,
|
||||
error: '',
|
||||
revision: useActivityStore.getState().revision + landed
|
||||
})
|
||||
} catch (exc: any) {
|
||||
// 顶栏不该因为一次网络抖动就报错给用户:离线横幅那边已经说了同一件事
|
||||
useActivityStore.getState().set({ error: exc?.message || String(exc) })
|
||||
} finally {
|
||||
inFlight = false
|
||||
schedule()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 乐观登记:POST 202 之后接口线程才知道任务长什么样,
|
||||
* 但卡片必须立刻变成「生成中」——用户点下去的那一瞬间就该有反馈,
|
||||
* 而不是等下一次轮询(最长 3 秒)才亮起来。
|
||||
*/
|
||||
function track(task: ActivityTask | null | undefined) {
|
||||
if (!task || !task.id) return
|
||||
const state = useActivityStore.getState()
|
||||
const tasks = [task, ...state.tasks.filter((row) => row.id !== task.id)]
|
||||
state.set({ tasks, active: tasks.filter((row) => ACTIVE_STATUSES.includes(row.status)).length, byOwner: index(tasks) })
|
||||
announced.delete(task.id)
|
||||
// 刚点下去就轮到它,别让用户盯着 15 秒的空闲周期等进度
|
||||
if (timer) window.clearTimeout(timer)
|
||||
timer = window.setTimeout(() => { void pollActivity() }, FAST_MS)
|
||||
}
|
||||
|
||||
/** 取消一条正在跑的任务(平台任务没有项目,走的也是这条通用任务接口) */
|
||||
async function cancel(task: ActivityTask, reason = '人工取消') {
|
||||
await api.post(`/api/tasks/${task.id}/cancel`, { reason })
|
||||
const state = useActivityStore.getState()
|
||||
state.set({ tasks: state.tasks.map((row) => (row.id === task.id ? { ...row, status: 'cancelled' } : row)) })
|
||||
await pollActivity()
|
||||
}
|
||||
|
||||
/** 重试一条失败的任务:接口会顺手派发,不等下一轮调度扫描 */
|
||||
async function retry(task: ActivityTask) {
|
||||
const body: any = await api.post(`/api/tasks/${task.id}/retry`, {})
|
||||
track(body)
|
||||
await pollActivity()
|
||||
return body
|
||||
}
|
||||
|
||||
/** 由 app/App.tsx 在登录后启动;重复调用只会留一个定时器 */
|
||||
function startActivityFeed() {
|
||||
if (timer) return
|
||||
timer = window.setTimeout(() => { void pollActivity() }, 0)
|
||||
}
|
||||
|
||||
function stopActivityFeed() {
|
||||
if (timer) window.clearTimeout(timer)
|
||||
timer = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 「立即刷新」:先让挂了的页面各重拉一次,再顺手问一遍任务表。
|
||||
*
|
||||
* 它和开关无关 —— 开关管的是「页面自己偷偷重拉」,
|
||||
* 人已经按下按钮了还要先看开关脸色,就等于这颗按钮白点
|
||||
* (开关关掉期间落定的那几笔,正是靠它补上的)。
|
||||
*/
|
||||
function refreshNow() {
|
||||
const state = useActivityStore.getState()
|
||||
state.set({ manualTick: state.manualTick + 1 })
|
||||
return pollActivity()
|
||||
}
|
||||
|
||||
/** 自动刷新开关:关掉之后页面不再自己重拉列表,进度轮询照常 */
|
||||
function setAutoRefresh(next: boolean) {
|
||||
writeLocal('nex.autoRefresh', next)
|
||||
useActivityStore.getState().set({ autoRefresh: next })
|
||||
}
|
||||
|
||||
export { useActivityStore, startActivityFeed, stopActivityFeed, pollActivity, track, cancel, retry, setAutoRefresh, refreshNow }
|
||||
Loading…
Reference in New Issue