feat(settings): support gateway source switching

This commit is contained in:
chengma
2026-07-20 16:29:28 +08:00
parent ed1d8ee764
commit 6a0cd1c763
18 changed files with 746 additions and 77 deletions
+41 -1
View File
@@ -36,6 +36,18 @@ def _runtime(config, cmhub_config_path):
return ai._cmhub_runtime(config, "image", cmhub_config_path)
def _is_default_gateway_job(job):
return (
str(getattr(job, "generation_source", "") or "").strip().lower() == "cmhub"
and str(getattr(job, "provider", "") or "").strip().lower() == "cmhub"
)
def _ensure_new_submission_allowed(config):
if appconfig.ai_backend(config) != "cmhub":
raise ImageStudioGenerationError("商品套图仅支持默认网关,请到⑤设置切换后再生成")
def _generated_kind(job_type):
return "generated_detail" if str(job_type) == "detail" else "generated_main"
@@ -85,8 +97,10 @@ def create_generation_jobs(
count,
*,
job_type="main",
config=None,
path=None,
):
_ensure_new_submission_allowed(config)
total = max(0, int(count or 0))
if total <= 0:
raise ImageStudioGenerationError("生成数量必须大于0")
@@ -126,6 +140,7 @@ def generate_image_jobs(
prompt,
count,
job_type=job_type,
config=config,
path=path,
)
return run_jobs(
@@ -179,13 +194,35 @@ def run_jobs(
job_list = list(jobs or [])
if not job_list:
return {"total": 0, "success": 0, "failed": 0, "cancelled": 0, "jobs": []}
rejected = [
job
for job in job_list
if getattr(job, "task_id", None) and not _is_default_gateway_job(job)
]
job_list = [job for job in job_list if job not in rejected]
summary = {
"total": len(job_list) + len(rejected),
"success": 0,
"failed": len(rejected),
"cancelled": 0,
"jobs": [
{
"job": job,
"status": "failed",
"error": "该已提交任务不属于默认网关,不能继续查询",
}
for job in rejected
],
}
if not job_list:
return summary
runtime = _runtime(cfg, cmhub_config_path)
ai_cfg = appconfig.ai_config(cfg)
image_root = appconfig.image_dir(cfg)
max_workers = min(MAX_CMHUB_IMAGE_STUDIO_WORKERS, max(1, int(ai_cfg.get("image_concurrency", 1) or 1)), len(job_list))
should_stop = should_stop or (lambda: False)
lock = threading.Lock()
summary = {"total": len(job_list), "success": 0, "failed": 0, "cancelled": 0, "jobs": []}
def record(result):
with lock:
@@ -373,8 +410,11 @@ def _submit_or_resume_job(
reference_assets=(),
):
if job.task_id:
if not _is_default_gateway_job(job):
raise ImageStudioGenerationError("该已提交任务不属于默认网关,不能继续查询")
_notify(on_event, {"job_id": job.id, "step": "cover_request", "result": "resume", "task_id": job.task_id})
return _request_result(job.task_id, runtime, config)
_ensure_new_submission_allowed(config)
_raise_if_stopped(should_stop)
ai_cfg = appconfig.ai_config(config)
resolution = str(ai_cfg.get("resolution", "1k") or "1k")