"""状态与能力的业务读取;不持有 HTTP Request。""" import asyncio import time from sqlalchemy import text from sqlalchemy.exc import SQLAlchemyError from app.adapters.provider.client import capabilities as provider_capabilities from app.core.maintenance import maintenance_active from app.core.runtime_policy import read_runtime_policy from app.services.orchestration.resource_policy import execution_policy from app.domain.timeline import ( DEFAULT_SOURCE_FRAMES, MAX_SOURCE_FRAMES, DEFAULT_CLIP_FRAMES, MAX_CLIP_FRAMES, ) def workflow_limits(policy): return { "max_shots": policy["workflow_max_shots"], "source_frames": DEFAULT_SOURCE_FRAMES, "default_source_frames": DEFAULT_SOURCE_FRAMES, "max_source_frames": MAX_SOURCE_FRAMES, "default_clip_frames": DEFAULT_CLIP_FRAMES, "max_clip_frames": MAX_CLIP_FRAMES, "fps": 24, } async def health(runtime): async with runtime.db.session() as session: await session.execute(text("SELECT 1")) return {"status": "ok", "database": runtime.settings.sqlalchemy_url.get_backend_name()} async def readiness(runtime): checks = { "database": False, "coordinator": True, "upstream_protocol": False, "accepting": not runtime.draining and (not maintenance_active(runtime.settings)), } try: async with runtime.db.session() as session: await session.execute(text("SELECT 1")) checks["database"] = True except SQLAlchemyError: pass coordinator = runtime.coordinator if runtime.settings.run_coordinator: heartbeat = getattr(coordinator, "last_tick_success", None) checks["coordinator"] = bool( coordinator._task and (not coordinator._task.done()) and (heartbeat is not None) and (time.monotonic() - heartbeat < 90) ) try: caps = await asyncio.wait_for(runtime.provider.upstream_capabilities(), 3) checks["upstream_protocol"] = caps.get("protocol_version", 0) >= 2 and bool( caps.get("admission_policy") ) except (Exception, asyncio.TimeoutError): pass return checks async def task_capabilities(runtime): items = provider_capabilities(runtime.settings) video = [row for row in items if row["task_type"] in {"video.people.analyze", "video.replace"}] if any(row["available"] for row in video): try: upstream = (await runtime.provider.upstream_capabilities()).get("video_edit", {}) except Exception: upstream = {} for row in video: if ( upstream.get("available") is not True or upstream.get("profile") != row["defaults"]["profile"] or upstream.get("fps") != 24 ): row["available"] = False row["reason"] = row["description"] = "上游视频编辑未完成认证或模型版本不匹配。" else: for key in ("max_seconds", "max_people", "max_pixels"): value = upstream.get(key) if type(value) is not int or value < 1: row["available"] = False row["reason"] = row["description"] = "上游视频编辑能力合同无效。" else: row["constraints"][key] = min(row["constraints"][key], value) for row in video: row.setdefault("reason", None if row["available"] else row["description"]) return items async def capabilities(runtime): policy = read_runtime_policy(runtime.settings) return { "editable_production": {"schema_version": 1}, "items": await task_capabilities(runtime), "scheduler": await runtime.provider.scheduler_status(), "execution_policy": execution_policy(settings=runtime.settings), "workflow_limits": workflow_limits(policy), } async def status(runtime): policy = read_runtime_policy(runtime.settings) online, scheduler = await asyncio.gather( runtime.provider.health(), runtime.provider.scheduler_status() ) return { "inference_online": online, "scheduler": scheduler, "models": await task_capabilities(runtime), "environment": runtime.settings.mode, "database_backend": runtime.settings.sqlalchemy_url.get_backend_name(), "execution_policy": execution_policy(settings=runtime.settings), "workflow_limits": workflow_limits(policy), "public_access": getattr(runtime.settings, "public_access", False), }