2026-11-01 08:18:07 +00:00
|
|
|
|
"""系统级接口:健康检查、平台元数据、模型注册表(图1 模块③的配置面)。"""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
|
|
from sqlalchemy import func, or_, select
|
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
|
|
|
|
from app import schemas
|
|
|
|
|
|
from app.api import deps
|
|
|
|
|
|
from app.config import settings
|
|
|
|
|
|
from app.constants import (ASPECT_RATIOS, RESOLUTIONS, AssetKind, CameraAngle, CameraMovement,
|
|
|
|
|
|
ProjectStatus, ShotSize, ShotStatus, TaskKind, TaskStatus, Transition)
|
|
|
|
|
|
from app.api.auth import require_admin
|
|
|
|
|
|
from app.db import get_db, open_sessions, pool_status, session_trace_enabled
|
|
|
|
|
|
from app.models import Asset, GenerationRecord, ModelSpec, PlatformMedia, Task, User
|
|
|
|
|
|
from app.providers import comfy_common, registry as model_registry
|
|
|
|
|
|
from app.services import auth as auth_service
|
|
|
|
|
|
from app.services import capacity, daemon, events, execution, ffmpeg as ff, status
|
|
|
|
|
|
from app.services import secrets
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter(tags=["system"])
|
|
|
|
|
|
|
|
|
|
|
|
MODEL_MEDIA_TYPES = [
|
2026-11-03 04:42:16 +00:00
|
|
|
|
{"value": "llm", "label": "大模型", "description": "文本推理与提示词补全(OpenAI 兼容接口);图像生成请使用独立图像模型"},
|
|
|
|
|
|
{"value": "video", "label": "视频模型", "description": "文生视频、图生视频、首尾帧与参考图生成"},
|
|
|
|
|
|
{"value": "image", "label": "图像模型", "description": "定妆图、三视图、服装、表情与场景图"},
|
|
|
|
|
|
{"value": "tts", "label": "声音模型", "description": "台词配音、音色样本与多语言声线"},
|
2026-11-01 08:18:07 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
MODEL_ADAPTERS = {
|
|
|
|
|
|
"llm": ["mock_llm", "http_llm"],
|
|
|
|
|
|
"video": ["mock", "http", "comfyui"],
|
|
|
|
|
|
"image": ["mock_image", "http_image", "comfyui_image"],
|
2026-11-05 08:53:24 +00:00
|
|
|
|
# 没有 macos_say:部署服务器是 Linux,没有系统 say 命令,
|
|
|
|
|
|
# 开发机上能用的本地嗓门不是产品能力。
|
|
|
|
|
|
"tts": ["mock", "http", "comfyui_tts"],
|
2026-11-01 08:18:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _fault_injected(db: Session) -> list[dict]:
|
|
|
|
|
|
"""列出仍带着 failure_rate 的模型:故障注入是临时态,泄漏出来必须一眼可见。"""
|
|
|
|
|
|
rows = db.scalars(select(ModelSpec)).all()
|
|
|
|
|
|
out = []
|
|
|
|
|
|
for spec in rows:
|
|
|
|
|
|
rate = float((spec.config_json or {}).get("failure_rate") or 0.0)
|
|
|
|
|
|
if rate > 0:
|
|
|
|
|
|
out.append({"id": spec.id, "provider": spec.provider, "model": spec.model,
|
|
|
|
|
|
"media_type": spec.media_type, "enabled": bool(spec.enabled),
|
|
|
|
|
|
"failure_rate": round(rate, 4)})
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/health")
|
|
|
|
|
|
def health(db: Session = Depends(get_db)):
|
|
|
|
|
|
models = db.query(ModelSpec).filter(ModelSpec.enabled.is_(True)).count()
|
|
|
|
|
|
return {"status": "ok", "app": settings.app_name, "executor": execution.backend_name(),
|
|
|
|
|
|
"ffmpeg": ff.available(), "ffmpeg_capabilities": ff.capabilities(),
|
|
|
|
|
|
"database": db.bind.dialect.name if db.bind else "unknown",
|
|
|
|
|
|
# 连接池现状放在健康检查里:曾经出现过「会话把 5+10 条连接占满,
|
|
|
|
|
|
# 任务排队 30 秒超时被误判失败」,光看任务状态完全看不出根因。
|
|
|
|
|
|
"database_pool": pool_status(),
|
|
|
|
|
|
"model_specs": models, "daemon": daemon.snapshot(),
|
|
|
|
|
|
# 部署排障最常用的两个开关,直接进健康检查,省得登录服务器翻 .env:
|
|
|
|
|
|
# auth_enabled=关 意味着任何人裸奔可写;frontend=off 意味着这台只出 API。
|
|
|
|
|
|
"auth_enabled": bool(settings.auth_enabled),
|
|
|
|
|
|
"frontend": (str(dist) if (dist := settings.frontend_dist()) else "api-only"),
|
|
|
|
|
|
"fault_injection": _fault_injected(db)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/system/db-debug")
|
|
|
|
|
|
def db_debug(db: Session = Depends(get_db), _: User = Depends(require_admin)) -> dict:
|
|
|
|
|
|
"""谁借着数据库会话不还:连接池告急时先看这里。
|
|
|
|
|
|
|
|
|
|
|
|
需要以 ``NEX_DB_DEBUG_SESSIONS=1`` 启动才有调用栈;否则只回池子计数。
|
|
|
|
|
|
"""
|
|
|
|
|
|
return {"trace_enabled": session_trace_enabled(), "pool": pool_status(),
|
|
|
|
|
|
"open_sessions": open_sessions(limit=30)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/meta")
|
|
|
|
|
|
def meta(db: Session = Depends(get_db)):
|
|
|
|
|
|
"""前端下拉框/流程条所需的全部枚举与运行时能力。"""
|
|
|
|
|
|
return {
|
|
|
|
|
|
"app": settings.app_name,
|
|
|
|
|
|
"executor": execution.backend_name(),
|
|
|
|
|
|
"ffmpeg_available": ff.available(),
|
|
|
|
|
|
"daemon": daemon.snapshot(),
|
|
|
|
|
|
"presets": list(ff.EXPORT_PRESETS.keys()),
|
|
|
|
|
|
"pipeline_stages": status.PIPELINE_STAGES,
|
|
|
|
|
|
"shot_statuses": ShotStatus.ALL,
|
|
|
|
|
|
"task_kinds": TaskKind.ALL,
|
|
|
|
|
|
"task_statuses": TaskStatus.ALL,
|
|
|
|
|
|
"project_statuses": ProjectStatus.ALL,
|
|
|
|
|
|
"options": {
|
|
|
|
|
|
"shot_size": [{"value": k, "label": v} for k, v in ShotSize.LABELS.items()],
|
|
|
|
|
|
"camera_movement": [{"value": k, "label": v} for k, v in CameraMovement.LABELS.items()],
|
|
|
|
|
|
"camera_angle": [{"value": k, "label": v} for k, v in CameraAngle.LABELS.items()],
|
|
|
|
|
|
"transition": [{"value": k, "label": v} for k, v in Transition.LABELS.items()],
|
|
|
|
|
|
"resolution": [{"value": k, "label": k, "canvas": list(v)} for k, v in RESOLUTIONS.items()],
|
|
|
|
|
|
"aspect_ratio": [{"value": k, "label": k} for k in ASPECT_RATIOS],
|
|
|
|
|
|
"mode": [{"value": "t2v", "label": "文生视频"}, {"value": "i2v", "label": "图生视频(关键帧)"},
|
|
|
|
|
|
{"value": "fl", "label": "首尾帧"}],
|
|
|
|
|
|
"asset_kinds": [{"value": k, "label": v} for k, v in AssetKind.LABELS.items()],
|
|
|
|
|
|
"asset_slots": {k: sorted(v) for k, v in deps.OWNER_KINDS.items()},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _usage_index(db: Session) -> dict[str, dict]:
|
|
|
|
|
|
"""模型调用量(MC-2):模型卡上只有「启用 / 可达」,看不出这条能力有没有真的在跑活。
|
|
|
|
|
|
|
|
|
|
|
|
两个口径合并:
|
|
|
|
|
|
* 生成台账 GenerationRecord —— 正式出片/配音/关键帧调用(次数、耗时、成本);
|
|
|
|
|
|
* 素材表 metadata_json —— 由该模型产出的素材个数(含平台母版,带 provider/model 留痕)。
|
|
|
|
|
|
"""
|
|
|
|
|
|
usage: dict[str, dict] = {}
|
|
|
|
|
|
|
|
|
|
|
|
def bucket(provider: str, model: str) -> dict:
|
|
|
|
|
|
return usage.setdefault(f"{provider}|{model}", {
|
|
|
|
|
|
"calls": 0, "seconds": 0.0, "cost": 0.0, "assets": 0, "last_used": None,
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
provider_of = func.json_extract(Asset.metadata_json, "$.provider")
|
|
|
|
|
|
model_of = func.json_extract(Asset.metadata_json, "$.model")
|
|
|
|
|
|
for provider, model, count in db.execute(
|
|
|
|
|
|
select(provider_of, model_of, func.count(Asset.id))
|
|
|
|
|
|
.where(provider_of.isnot(None), provider_of != "")
|
|
|
|
|
|
.group_by(provider_of, model_of)
|
|
|
|
|
|
).all():
|
|
|
|
|
|
bucket(str(provider or ""), str(model or ""))["assets"] += int(count or 0)
|
|
|
|
|
|
|
|
|
|
|
|
p_of = func.json_extract(PlatformMedia.metadata_json, "$.provider")
|
|
|
|
|
|
m_of = func.json_extract(PlatformMedia.metadata_json, "$.model")
|
|
|
|
|
|
for provider, model, count in db.execute(
|
|
|
|
|
|
select(p_of, m_of, func.count(PlatformMedia.id))
|
|
|
|
|
|
.where(p_of.isnot(None), p_of != "")
|
|
|
|
|
|
.group_by(p_of, m_of)
|
|
|
|
|
|
).all():
|
|
|
|
|
|
bucket(str(provider or ""), str(model or ""))["assets"] += int(count or 0)
|
|
|
|
|
|
|
|
|
|
|
|
for provider, model, calls, seconds, cost, last_used in db.execute(
|
|
|
|
|
|
select(GenerationRecord.provider, GenerationRecord.model,
|
|
|
|
|
|
func.count(GenerationRecord.id),
|
|
|
|
|
|
func.coalesce(func.sum(GenerationRecord.duration_seconds), 0.0),
|
|
|
|
|
|
func.coalesce(func.sum(GenerationRecord.cost), 0.0),
|
|
|
|
|
|
func.max(GenerationRecord.created_at))
|
|
|
|
|
|
.group_by(GenerationRecord.provider, GenerationRecord.model)
|
|
|
|
|
|
).all():
|
|
|
|
|
|
entry = bucket(str(provider or ""), str(model or ""))
|
|
|
|
|
|
entry["calls"] += int(calls or 0)
|
|
|
|
|
|
entry["seconds"] += float(seconds or 0.0)
|
|
|
|
|
|
entry["cost"] += float(cost or 0.0)
|
|
|
|
|
|
if last_used and str(last_used) > str(entry["last_used"] or ""):
|
|
|
|
|
|
entry["last_used"] = str(last_used)
|
|
|
|
|
|
for entry in usage.values():
|
|
|
|
|
|
entry["seconds"] = round(entry["seconds"], 2)
|
|
|
|
|
|
entry["cost"] = round(entry["cost"], 4)
|
|
|
|
|
|
return usage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _upscale_tiers(spec) -> list[str]:
|
|
|
|
|
|
"""放大交付档位(适配器/模型配置声明)。取不到就当没这个能力,不参与下拉。"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
return list(model_registry.capabilities_of(spec).upscale_to or ())
|
|
|
|
|
|
except Exception: # noqa: BLE001 - 列表接口不能被能力探测打挂
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/providers")
|
|
|
|
|
|
def list_specs(db: Session = Depends(get_db)):
|
|
|
|
|
|
rows = db.scalars(select(ModelSpec).order_by(ModelSpec.provider, ModelSpec.model)).all()
|
|
|
|
|
|
load = {f"{row['provider']}|{row['model']}": row
|
|
|
|
|
|
for row in (r.as_dict() for r in capacity.snapshot(db))}
|
|
|
|
|
|
usage = _usage_index(db)
|
|
|
|
|
|
payload = []
|
|
|
|
|
|
for spec in rows:
|
|
|
|
|
|
key = f"{spec.provider}|{spec.model or ''}"
|
|
|
|
|
|
payload.append({
|
|
|
|
|
|
"id": spec.id, "provider": spec.provider, "model": spec.model,
|
|
|
|
|
|
"display_name": spec.display_name or spec.model, "media_type": spec.media_type,
|
|
|
|
|
|
"adapter": spec.adapter, "enabled": spec.enabled, "is_default": spec.is_default,
|
|
|
|
|
|
"status": spec.status, "model_code": spec.model_code or "",
|
|
|
|
|
|
"timeout_seconds": spec.timeout_seconds or 120,
|
|
|
|
|
|
"has_api_key": bool(spec.api_key_encrypted),
|
|
|
|
|
|
"api_key_masked": secrets.mask_value(
|
|
|
|
|
|
secrets.decrypt_value(db, spec.api_key_encrypted or "")
|
|
|
|
|
|
) if spec.api_key_encrypted else "",
|
|
|
|
|
|
"max_concurrency": spec.max_concurrency,
|
|
|
|
|
|
"requests_per_minute": spec.requests_per_minute, "burst": spec.burst,
|
|
|
|
|
|
"modes": spec.modes or [], "resolutions": spec.resolutions or [],
|
|
|
|
|
|
# 「原生能生成」与「能交付」的档位不同:H3 原生 768p,1080p 靠合成端放大交付。
|
|
|
|
|
|
# 前端下拉框要按这个差异加标注,否则用户以为选到 1080p 就多花了卡时。
|
|
|
|
|
|
"upscale_to": _upscale_tiers(spec),
|
|
|
|
|
|
"aspect_ratios": spec.aspect_ratios or [], "min_duration": spec.min_duration,
|
|
|
|
|
|
"max_duration": spec.max_duration, "max_reference_images": spec.max_reference_images,
|
|
|
|
|
|
"price_per_second": spec.price_per_second, "price_per_1k_chars": spec.price_per_1k_chars,
|
|
|
|
|
|
"currency": spec.currency, "default_params": spec.default_params or {},
|
|
|
|
|
|
"config": _redact_config(spec.config_json or {}), "notes": spec.notes,
|
|
|
|
|
|
"uses_mock": spec.provider == "mock" or str(spec.adapter or "").startswith("mock"),
|
|
|
|
|
|
# 故障注入是临时态:列表与页头都要显性提示,避免脚本中断后整条能力被静默打挂
|
|
|
|
|
|
"fault_injection": round(float((spec.config_json or {}).get("failure_rate") or 0.0), 4),
|
|
|
|
|
|
"requires_reference": spec.adapter == "comfyui_tts",
|
|
|
|
|
|
"available": model_registry.is_available(spec, db),
|
|
|
|
|
|
"load": load.get(key, {}),
|
|
|
|
|
|
# 调用量:模型卡要能看出「这条能力真的在跑活」,而不只是「配置看起来没问题」
|
|
|
|
|
|
"usage": usage.get(key, {"calls": 0, "seconds": 0.0, "cost": 0.0, "assets": 0,
|
|
|
|
|
|
"last_used": None}),
|
|
|
|
|
|
})
|
|
|
|
|
|
defaults = {}
|
|
|
|
|
|
for media_type in ("llm", "video", "image", "tts"):
|
|
|
|
|
|
row = next((p for p in payload if p["media_type"] == media_type and p["is_default"]), None)
|
|
|
|
|
|
defaults[f"{media_type}_provider"] = row["provider"] if row else ""
|
|
|
|
|
|
defaults[f"{media_type}_model"] = row["model"] if row else ""
|
2026-11-05 10:17:47 +00:00
|
|
|
|
# 默认位**只由数据库决定**:v0.1.1 起模型配置全部搬进 model_specs 表,
|
|
|
|
|
|
# .env 里不再有任何「默认模型」的说法(理由写在 .env.example 开头)。
|
|
|
|
|
|
# 这里以前会用 VIDEO_PROVIDER / TTS_PROVIDER 覆盖上面的结果,前提是「env 里显式写了值」。
|
|
|
|
|
|
# 现在 env 里根本不写,settings.* 拿到的是代码默认 mock——继续覆盖就等于把服务器上
|
|
|
|
|
|
# 真实模型的默认位悄悄降成 Mock:页面显示「默认模型:Mock 视频」,出片跑出来全是占位片段,
|
|
|
|
|
|
# 而没有任何一处配置过 mock。env 兜底只保留在 resolve_*(空库/无 db 时才走)。
|
2026-11-01 08:18:07 +00:00
|
|
|
|
return {"providers": payload, "defaults": defaults,
|
|
|
|
|
|
"media_types": MODEL_MEDIA_TYPES, "adapters": MODEL_ADAPTERS}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/api/providers")
|
|
|
|
|
|
def create_spec(data: schemas.ModelSpecCreate, db: Session = Depends(get_db)):
|
|
|
|
|
|
payload = data.model_dump()
|
|
|
|
|
|
api_key = payload.pop("api_key", None)
|
|
|
|
|
|
if payload["media_type"] not in MODEL_ADAPTERS:
|
|
|
|
|
|
raise HTTPException(400, f"不支持的模型类型:{payload['media_type']}")
|
|
|
|
|
|
if payload["adapter"] not in MODEL_ADAPTERS[payload["media_type"]]:
|
|
|
|
|
|
raise HTTPException(400, f"{payload['media_type']} 模型不支持适配器 {payload['adapter']};"
|
|
|
|
|
|
f"可用:{', '.join(MODEL_ADAPTERS[payload['media_type']])}")
|
|
|
|
|
|
_validate_model_code(db, payload.get("model_code", ""))
|
|
|
|
|
|
config = payload.pop("config", {}) or {}
|
|
|
|
|
|
_normalize_config(config, payload)
|
|
|
|
|
|
spec = ModelSpec(**payload, config_json=config)
|
|
|
|
|
|
if api_key:
|
|
|
|
|
|
spec.api_key_encrypted = secrets.encrypt_value(db, str(api_key).strip())
|
|
|
|
|
|
if not spec.display_name:
|
|
|
|
|
|
spec.display_name = spec.model or spec.provider
|
|
|
|
|
|
if spec.is_default:
|
|
|
|
|
|
db.query(ModelSpec).filter(ModelSpec.media_type == spec.media_type,
|
|
|
|
|
|
ModelSpec.is_default.is_(True)).update({"is_default": False})
|
|
|
|
|
|
db.add(spec)
|
|
|
|
|
|
db.flush()
|
|
|
|
|
|
events.log_event(db, "model.created", message=f"注册模型 {spec.provider}/{spec.model}",
|
|
|
|
|
|
entity_type="model_spec", entity_id=spec.id,
|
|
|
|
|
|
data={"media_type": spec.media_type, "adapter": spec.adapter,
|
|
|
|
|
|
"model_code": spec.model_code or ""})
|
|
|
|
|
|
db.commit()
|
|
|
|
|
|
return {"ok": True, "id": spec.id}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.patch("/api/providers/{spec_id}")
|
|
|
|
|
|
def update_spec(spec_id: int, data: schemas.ModelSpecUpdate, db: Session = Depends(get_db)):
|
|
|
|
|
|
spec = db.get(ModelSpec, spec_id)
|
|
|
|
|
|
if spec is None:
|
|
|
|
|
|
raise HTTPException(404, "模型不存在")
|
|
|
|
|
|
patch = data.model_dump(exclude_unset=True)
|
|
|
|
|
|
api_key = str(patch.pop("api_key", "") or "").strip()
|
|
|
|
|
|
clear_api_key = bool(patch.pop("clear_api_key", False))
|
|
|
|
|
|
if patch.get("media_type") and patch["media_type"] not in MODEL_ADAPTERS:
|
|
|
|
|
|
raise HTTPException(400, f"不支持的模型类型:{patch['media_type']}")
|
|
|
|
|
|
media_type = patch.get("media_type") or spec.media_type
|
|
|
|
|
|
adapter = patch.get("adapter") or spec.adapter
|
|
|
|
|
|
if adapter not in MODEL_ADAPTERS.get(media_type, []):
|
|
|
|
|
|
raise HTTPException(400, f"{media_type} 模型不支持适配器 {adapter};"
|
|
|
|
|
|
f"可用:{', '.join(MODEL_ADAPTERS.get(media_type, []))}")
|
|
|
|
|
|
config = patch.pop("config", None)
|
|
|
|
|
|
replace_config = bool(patch.pop("replace_config", False))
|
|
|
|
|
|
_validate_model_code(db, patch.get("model_code", ""), exclude_id=spec_id)
|
|
|
|
|
|
if media_type == "llm":
|
|
|
|
|
|
patch["modes"] = [mode for mode in patch.get("modes", spec.modes or [])
|
|
|
|
|
|
if mode in ("chat", "prompt")]
|
|
|
|
|
|
patch["max_reference_images"] = 0
|
|
|
|
|
|
patch["aspect_ratios"] = []
|
|
|
|
|
|
current_config = dict(spec.config_json or {})
|
|
|
|
|
|
if "image" in current_config:
|
|
|
|
|
|
current_config.pop("image", None)
|
|
|
|
|
|
spec.config_json = current_config
|
|
|
|
|
|
if config is not None:
|
|
|
|
|
|
# 只传一个键就把整份 config 覆盖掉,等于悄悄删掉 base_url / workflow / models,
|
|
|
|
|
|
# 模型随即不可用(PATCH 仍返回 200)。默认改成浅合并,全量替换要显式声明。
|
|
|
|
|
|
merged = dict(config) if replace_config else {**(spec.config_json or {}), **config}
|
|
|
|
|
|
config_diff = _diff_config(spec.config_json or {}, merged)
|
|
|
|
|
|
_normalize_config(merged, {**{
|
|
|
|
|
|
"provider": patch.get("provider", spec.provider),
|
|
|
|
|
|
"model": patch.get("model", spec.model),
|
|
|
|
|
|
"media_type": patch.get("media_type", spec.media_type),
|
|
|
|
|
|
}})
|
|
|
|
|
|
spec.config_json = merged
|
|
|
|
|
|
if patch.get("is_default"):
|
|
|
|
|
|
db.query(ModelSpec).filter(ModelSpec.media_type == (patch.get("media_type") or spec.media_type),
|
|
|
|
|
|
ModelSpec.is_default.is_(True)).update({"is_default": False})
|
|
|
|
|
|
for key, value in patch.items():
|
|
|
|
|
|
setattr(spec, key, value)
|
|
|
|
|
|
if clear_api_key:
|
|
|
|
|
|
spec.api_key_encrypted = ""
|
|
|
|
|
|
elif api_key and "*" not in api_key:
|
|
|
|
|
|
spec.api_key_encrypted = secrets.encrypt_value(db, api_key)
|
|
|
|
|
|
db.flush()
|
|
|
|
|
|
log_data = _redact_config(patch)
|
|
|
|
|
|
if config is not None:
|
|
|
|
|
|
# 台账里写清这次是「合并」还是「整份替换」,避免再被误当成全量提交
|
|
|
|
|
|
log_data["config_mode"] = "replace" if replace_config else "merge"
|
|
|
|
|
|
log_data["config_keys"] = sorted(config)
|
|
|
|
|
|
# 模型控制台保存的是整份 config 快照:表单打开后配置若被别处改过(或直接改库),
|
|
|
|
|
|
# 保存就会把旧值静默写回。台账里留一份新旧对照,出问题能追责。
|
|
|
|
|
|
if config_diff:
|
|
|
|
|
|
log_data["config_changes"] = config_diff
|
|
|
|
|
|
events.log_event(db, "model.updated", message=f"更新模型 {spec.provider}/{spec.model}",
|
|
|
|
|
|
entity_type="model_spec", entity_id=spec.id, data=log_data)
|
|
|
|
|
|
db.commit()
|
|
|
|
|
|
return {"ok": True, "id": spec.id}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/api/providers/{spec_id}")
|
|
|
|
|
|
def delete_spec(spec_id: int, db: Session = Depends(get_db)):
|
|
|
|
|
|
spec = db.get(ModelSpec, spec_id)
|
|
|
|
|
|
if spec is None:
|
|
|
|
|
|
raise HTTPException(404, "模型不存在")
|
|
|
|
|
|
running = db.query(Task).filter(Task.provider == spec.provider, Task.model == spec.model,
|
|
|
|
|
|
Task.status.in_(TaskStatus.ACTIVE)).count()
|
|
|
|
|
|
if running:
|
|
|
|
|
|
raise HTTPException(409, f"仍有 {running} 个任务使用该模型,请先停用而非删除")
|
|
|
|
|
|
db.delete(spec)
|
|
|
|
|
|
events.log_event(db, "model.deleted", message=f"删除模型 {spec.provider}/{spec.model}",
|
|
|
|
|
|
entity_type="model_spec", entity_id=spec_id)
|
|
|
|
|
|
db.commit()
|
|
|
|
|
|
return {"ok": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/providers/{spec_id}/api-key")
|
|
|
|
|
|
def reveal_api_key(spec_id: int, db: Session = Depends(get_db),
|
|
|
|
|
|
actor: User = Depends(require_admin)) -> dict:
|
|
|
|
|
|
"""取回某条模型已保存的 API Key 明文(供页面上的「眼睛」图标显示)。
|
|
|
|
|
|
|
|
|
|
|
|
列表接口只回 `sk-****abcd` 掩码,所以用户点「显示」时输入框是空的——看起来像坏了。
|
|
|
|
|
|
明文只在这里按需下发:仅管理员、只读、并在审计流水里留一条痕(谁看了哪条密钥)。
|
|
|
|
|
|
"""
|
|
|
|
|
|
spec = db.get(ModelSpec, spec_id)
|
|
|
|
|
|
if spec is None:
|
|
|
|
|
|
raise HTTPException(404, "模型不存在")
|
|
|
|
|
|
plain = secrets.decrypt_value(db, spec.api_key_encrypted) if spec.api_key_encrypted else ""
|
|
|
|
|
|
auth_service.log_audit(db, "model.api_key_revealed",
|
|
|
|
|
|
message=f"{actor.username} 查看模型密钥 {spec.provider}/{spec.model}",
|
|
|
|
|
|
entity_type="model_spec", entity_id=spec_id, actor=actor)
|
|
|
|
|
|
db.commit()
|
|
|
|
|
|
return {"ok": True, "result": {"api_key": plain, "provider": spec.provider,
|
|
|
|
|
|
"model": spec.model, "set": bool(plain)}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/providers/{spec_id}/probe")
|
|
|
|
|
|
def probe_spec(spec_id: int, db: Session = Depends(get_db)):
|
|
|
|
|
|
"""探测模型是否真的可用;ComfyUI 额外返回连通性、显存、队列与可用模型文件名。"""
|
|
|
|
|
|
spec = db.get(ModelSpec, spec_id)
|
|
|
|
|
|
if spec is None:
|
|
|
|
|
|
raise HTTPException(404, "模型不存在")
|
|
|
|
|
|
adapter = model_registry.adapter_for_spec(spec)
|
|
|
|
|
|
info: dict = {"provider": spec.provider, "model": spec.model, "adapter": spec.adapter,
|
|
|
|
|
|
"available": False, "message": ""}
|
|
|
|
|
|
runtime: dict = {}
|
|
|
|
|
|
try:
|
|
|
|
|
|
runtime = model_registry.runtime_config(db, spec)
|
|
|
|
|
|
# 显式探测要拿真实结果,不能复用列表页那份 30s 缓存
|
|
|
|
|
|
if hasattr(adapter, "invalidate_probes"):
|
|
|
|
|
|
adapter.invalidate_probes(runtime)
|
|
|
|
|
|
# 生成链路上的「开工前探活」是另一份共享缓存(providers/comfy_common.py)。
|
前端四项调整:输入框去掉光晕、账号菜单收口、逐页删开发期说明书、登录页重做成左右分栏
用户这一轮提了四条,其中三条是同一件事的三个面——**界面上残留着写给开发者自己的话**。
逐条落地的口径与取舍:
**1)所有输入框聚焦时的蓝色外框**
量出来其实是三层叠在一起:antd 组件 token 的 `activeShadow`(2px 主色光晕)、
`genFocusStyle` 画的 `outline`、站内 `app.css` 给自绘表面准备的全站 `:focus-visible` 兜底。
第二层没有独立 token,而它依赖的 `lineWidthFocus` 还被 Upload 拖拽区复用,动 token 会连带改上传框,
所以分两刀收:`theme/antd.ts` 关 token(`Input`/`InputNumber` 的 `activeShadow` = `none`、
`Select.activeOutlineColor` = `transparent`),`styles/app.css` 用
`:where(...):focus-visible { outline: none }` 关那圈 outline——用 `:where()` 是为了把特异度归零,
它只是一份选择器清单而不是新加一层优先级,将来谁要恢复焦点环不必先赢过一条 `!important`
(app.css 本身不在 `@layer` 里,压得住被 Tailwind 放进 `@layer antd` 的那份,本来就不需要 `!important`)。
**焦点指示没有被删**:antd 的 `activeBorderColor` + 加粗边框仍在,键盘用户看得见焦点;
自绘表面继续吃全站那条兜底。实测聚焦 `#login-user`:`box-shadow: none`、`outline: none 3px`、
`border-color` 浅 rgb(22,119,255) / 深 rgb(22,104,220)。
**2)账号胶囊:菜单去重、项上不留副标题**
「设置新密码」和「我的账号」是同一件事,且首启时登录页已经直接把人的带到 `/account?must=1`,
重复排队只让菜单变长——删掉,待办语义留着头像上的 Badge 红点。
菜单项不再写「账号信息 · 密码 · 登录设备」这类预演式说明;顺手把胶囊第二行的
`第 N 次登录` 也去了:那是「用户管理」表格里逐人登记的数据,挂在人人都要看的顶栏里等于把统计搬进导航。
现在菜单就是三项:我的账号 / 用户管理 / 退出登录。
**3)逐页去开发时冗余说明**(`Dashboard` / `AssetLibrary` / `ModelCenter` / `AgentConsole` /
`project/{Library,Overview,Storyboard,Timeline,Review,Delivery}` / `ProjectBriefDrawer`)
三类处置,已写进 UI规范 §14 G2:
- 常驻说明书删掉可见文字,信息进 `title`/`desc`:IndexTTS2 那四行论文式介绍(两处重复,
含「下面这条预告会说清楚:」)、总控台「期间页面不会推进,属正常现象」;
- 开发者口径翻成「会怎样」:ffmpeg libass 提示只留降级后果,`FFMPEG_BIN` / `brew install ffmpeg-full` /
当前探测路径收进 `title`;`Delivery` 卡头「媒体由后端 `/media` 直出,下载走 `/api/exports/:id/download`」整条删;
「每一站的汇总状态由它覆盖的那几个节点折算而来」→「点一行直接跳到对应环节」;
- 内部词不许漏到版面:`mock/Mock` → 站内既有的**演练通道**(四处)、「未匹配的列在 `unmatched_images`」
→ 翻成人话;**一个物件只有一个名字**,`/models` 曾同时叫「模型中心 / 模型控制台 / 调度中心 → 模型中心」,
全站统一成侧栏那一项的写法 **模型管理中心**(含 `.env` 注释、`dev.sh`/`server.sh`/`seed.py` 的引导语、
后端四条给用户的报错文案、README、docs、探针页面名),并修掉不存在的入口路径「调度中心 → 模型中心」。
有意保留:单位与取值口径的括号(`(秒)(毫秒)(可选)(0=自动)(dB)`——读数不该依赖悬停)、
条件出现的 `Notice`/`LineHint`/`ErrorText`、`confirmAction` 的 `desc`、`Stat hint`、`nav.hint`、
`PageHead sub`(kit 既定槽位的一句话职责说明)、`Help.tsx` 整页(登记在册的产品手册)。
配套给 `OptionGrid` 单开 `labelTitle`,让组标签的括号补充有地方去而不额外占一行。
**4)登录页重做**(UI规范新增 §13.4)
左右分栏:左栏 `w-[46%] max-w-[620px]` 写平台自述(品牌行 →「把一条长视频,做成一条流水线」+
三条能力点 → 角色可见范围),底色用 `color-mix` 把主色掺进 `color-bg-layout`,**零新 hex**;
右栏卡片保留原生 `<form>` 包在 `<Panel>` 外(Enter 提交与密码自动填充都挂在 form 上)。
窄于 `lg` 左栏整列收起,品牌行回到卡片顶部,任何宽度都认得出登的是哪个系统。
去掉后端下发的种子账号预填(`login_hint` 字段本身保留,`test_auth.py` 与 `e2e_selfcheck.py` 还在用它判首启),
placeholder 回到「用户名」「密码」;新增**记住我**:键 `nex-login`,账号+口令一起记一起清
(只清口令留账号的话勾选框就不算数了),只在登录成功后落盘,取消勾选当场 `writeLocal(key, null)`,
默认不勾(本机明文口令)。
文档同步:UI规范 §8.4(`labelTitle`)、§9.3(焦点轮廓三层口径)、§13.4(登录页)、
§14 G2(三类处置 + 一个物件一个名字)与 §14 例外登记(登录页左栏、`Help.tsx` 整页豁免);
README 鉴权一节补登录页与记住我;命名统一顺带扫了 `.env.example` / 三个脚本 / 后端文案。
验证:`npx tsc --noEmit`、`npm run build` 全绿;`node scripts/ui/classaudit.mjs` 七道闸门 0/0/0/0/0/0/0;
后端 + 前端起在 :8010/:5199 后跑完 §15 全部探针,退出码全 0——visualcheck 1440×明暗 15 路由
(console 0 / HTTP 失败 0 / 断言 0)、tableaudit 0、gridcheck 0、rhythmcheck 0、tagcheck 0/0、
btnrowcheck 0(413 组)、aligncheck 0、symmetrycheck 0、feedbackcheck 通过、footercheck 0(30 处吸底条)、
btnclustercheck 0(159 簇);gateprobe 11/11 反向验证;`pytest -q` 全量 502 passed 1 skipped
(后端改了四条用户可见文案,对应 3 处断言同步更新)。记住我三条路径(勾上存 / 不勾清空 / 取消即清)实测过。
2026-11-05 14:09:16 +00:00
|
|
|
|
# 出片失败时我们让用户「到模型管理中心点探测复测」,那这个按钮就必须真的把那份
|
2026-11-01 08:18:07 +00:00
|
|
|
|
# 缓存打穿并回填——否则会出现「页面刚探测通过、任务还是说连不上」。
|
|
|
|
|
|
base = str(runtime.get("base_url") or runtime.get("url") or "").strip().rstrip("/")
|
|
|
|
|
|
if base and (spec.provider == "comfyui" or str(spec.adapter or "").startswith("comfy")):
|
|
|
|
|
|
comfy_common.invalidate_reachability(base)
|
|
|
|
|
|
reachable, why = comfy_common.probe_reachable(base, timeout=8.0, force=True)
|
|
|
|
|
|
info["reachable"] = reachable
|
|
|
|
|
|
if not reachable:
|
|
|
|
|
|
info["message"] = info.get("message") or f"连不上 {base}:{why}"
|
|
|
|
|
|
if hasattr(adapter, "diagnose"):
|
|
|
|
|
|
info.update(adapter.diagnose(runtime))
|
|
|
|
|
|
else:
|
|
|
|
|
|
info["available"] = bool(adapter.is_available(runtime))
|
|
|
|
|
|
info["message"] = "适配器可用" if info["available"] else "适配器未配置或缺少依赖"
|
|
|
|
|
|
except Exception as exc: # 诊断绝不能把页面打挂
|
|
|
|
|
|
info["message"] = f"探测失败:{type(exc).__name__}: {exc}"
|
|
|
|
|
|
caps = model_registry.capabilities_of(spec)
|
|
|
|
|
|
info["capabilities"] = caps.as_dict()
|
|
|
|
|
|
# 声明档位 ≠ 实际交付像素:把「这个配置真正会出多大画面」摊开给控制台看
|
|
|
|
|
|
try:
|
|
|
|
|
|
info["canvas"] = {ar: list(adapter.effective_canvas(runtime, ar) or ())
|
|
|
|
|
|
for ar in (caps.aspect_ratios or ())}
|
|
|
|
|
|
except Exception: # noqa: BLE001 - 诊断字段不参与主流程
|
|
|
|
|
|
info["canvas"] = {}
|
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _validate_model_code(db: Session, code: str, *, exclude_id: int | None = None) -> None:
|
|
|
|
|
|
code = (code or "").strip()
|
|
|
|
|
|
if not code:
|
|
|
|
|
|
return
|
|
|
|
|
|
stmt = select(ModelSpec).where(ModelSpec.model_code == code)
|
|
|
|
|
|
if exclude_id is not None:
|
|
|
|
|
|
stmt = stmt.where(ModelSpec.id != exclude_id)
|
|
|
|
|
|
if db.scalars(stmt).first():
|
|
|
|
|
|
raise HTTPException(409, f"模型编码 {code} 已被占用,请换一个")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _normalize_config(config: dict, payload: dict) -> None:
|
|
|
|
|
|
"""把控制台级字段同步进适配器配置,避免两处填写不一致。"""
|
|
|
|
|
|
if payload.get("media_type") == "llm":
|
|
|
|
|
|
# LLM 只负责语言推理。图像生成统一走 image 模型,避免同一模型
|
|
|
|
|
|
# 在两套端点/尺寸配置之间产生歧义,也清理旧版本遗留的 image 配置。
|
|
|
|
|
|
for key in ("image", "temperature", "top_p", "max_tokens"):
|
|
|
|
|
|
config.pop(key, None)
|
|
|
|
|
|
if "modes" in payload:
|
|
|
|
|
|
payload["modes"] = [mode for mode in (payload.get("modes") or [])
|
|
|
|
|
|
if mode in ("chat", "prompt")]
|
|
|
|
|
|
payload["max_reference_images"] = 0
|
|
|
|
|
|
payload["aspect_ratios"] = []
|
|
|
|
|
|
if payload.get("model"):
|
|
|
|
|
|
config.setdefault("model", payload["model"])
|
|
|
|
|
|
base = str(config.get("base_url") or "").rstrip("/")
|
|
|
|
|
|
if base:
|
|
|
|
|
|
config["base_url"] = base
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _diff_config(old: dict, new: dict) -> dict:
|
|
|
|
|
|
"""新旧配置的标量差异({"key": {"from":…, "to":…}}),嵌套结构只标记 changed。"""
|
|
|
|
|
|
diff: dict = {}
|
|
|
|
|
|
for key in sorted(set(old) | set(new)):
|
|
|
|
|
|
if key not in new:
|
|
|
|
|
|
diff[key] = {"from": _brief(old.get(key)), "to": None}
|
|
|
|
|
|
continue
|
|
|
|
|
|
if key not in old:
|
|
|
|
|
|
diff[key] = {"from": None, "to": _brief(new.get(key))}
|
|
|
|
|
|
continue
|
|
|
|
|
|
before, after = old.get(key), new.get(key)
|
|
|
|
|
|
if before == after:
|
|
|
|
|
|
continue
|
|
|
|
|
|
if isinstance(before, (int, float, str, bool)) or before is None:
|
|
|
|
|
|
diff[key] = {"from": _brief(before), "to": _brief(after)}
|
|
|
|
|
|
else:
|
|
|
|
|
|
diff[key] = {"changed": True}
|
|
|
|
|
|
return diff
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _brief(value):
|
|
|
|
|
|
if isinstance(value, (int, float, str, bool)) or value is None:
|
|
|
|
|
|
return value
|
|
|
|
|
|
text = json.dumps(value, ensure_ascii=False)
|
|
|
|
|
|
return text if len(text) <= 160 else text[:157] + "..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _redact_config(value):
|
|
|
|
|
|
if isinstance(value, dict):
|
|
|
|
|
|
out = {}
|
|
|
|
|
|
for key, item in value.items():
|
|
|
|
|
|
lowered = str(key).lower()
|
|
|
|
|
|
if (lowered in {"api_key", "apikey", "access_token", "secret", "password"}
|
|
|
|
|
|
or lowered.endswith("_api_key")
|
|
|
|
|
|
or lowered.endswith("_secret")
|
|
|
|
|
|
or lowered.endswith("_password")):
|
|
|
|
|
|
out[key] = "***" if item else ""
|
|
|
|
|
|
else:
|
|
|
|
|
|
out[key] = _redact_config(item)
|
|
|
|
|
|
return out
|
|
|
|
|
|
if isinstance(value, list):
|
|
|
|
|
|
return [_redact_config(item) for item in value]
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/events")
|
|
|
|
|
|
def global_events(limit: int = 80, level: str = "", db: Session = Depends(get_db)):
|
|
|
|
|
|
rows = events.recent_events(db, limit=min(500, max(1, limit)))
|
|
|
|
|
|
if level:
|
|
|
|
|
|
rows = [r for r in rows if r["level"] == level]
|
|
|
|
|
|
return {"events": rows}
|