Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: generate images through an external connection (#13756)
Nilay2f412e3484a0
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.pyindex 6d34efb5751..466bc63744e 100644--- a/studio/backend/core/inference/external_provider.py+++ b/studio/backend/core/inference/external_provider.py@@ -7494,6 +7494,22 @@ class ExternalProviderClient:raise asyncio.CancelledErrorreturn audio, media_type or f"audio/{response_format}"+ async def create_images(self, prompt: str, model: str, n: int, size: str) -> list[str]:+ response = await _client().post(+ _append_provider_path(self.base_url, "/images/generations"),+ headers = self._auth_headers(),+ json = {+ "model": model,+ "prompt": prompt,+ "n": n,+ "size": size,+ "response_format": "b64_json",+ },+ timeout = self._timeout,+ )+ response.raise_for_status()+ return [item["b64_json"] for item in response.json()["data"]]+async def create_transcription(self,audio: bytes,diff --git a/studio/backend/core/inference/image_gallery.py b/studio/backend/core/inference/image_gallery.pyindex c2928ca5da7..dfd57d12d6c 100644--- a/studio/backend/core/inference/image_gallery.py+++ b/studio/backend/core/inference/image_gallery.py@@ -51,6 +51,11 @@ def _params_text(meta: dict[str, Any]) -> str:lines.append(f"Negative prompt: {negative}")from core.inference.diffusion_sampler import sampler_label, schedule_label+ if meta.get("model_kind") == "connection":+ lines.append(+ f"Size: {meta.get('width')}x{meta.get('height')}, Model: {meta.get('model', '')}"+ )+ return "\n".join(lines)sampler = sampler_label(meta.get("sampler"))schedule = schedule_label(meta.get("schedule_type"))lines.append(diff --git a/studio/backend/core/inference/llama_keepwarm.py b/studio/backend/core/inference/llama_keepwarm.pyindex 1c3928af8b9..d2dcf2bd972 100644--- a/studio/backend/core/inference/llama_keepwarm.py+++ b/studio/backend/core/inference/llama_keepwarm.py@@ -403,6 +403,24 @@ def mark_current_response_failed() -> None:mark_response_failed(_current_response_scope.get())+_MEDIA_TRACKED_SCOPE_KEY = "_unsloth_keepwarm_media_owner"+_MEDIA_UNTRACKED_SCOPE_KEY = "_unsloth_keepwarm_media_untracked"+++def untrack_current_media_request(scope) -> None:+ """Drop this request from its media tracker once the route knows it won't use the local+ engine, so a call to a saved connection can't hold off a media swap or idle unload."""+ if not isinstance(scope, dict) or scope.get(_MEDIA_UNTRACKED_SCOPE_KEY):+ return+ owner = scope.get(_MEDIA_TRACKED_SCOPE_KEY)+ if owner is None:+ return+ from core.inference import media_keepwarm++ scope[_MEDIA_UNTRACKED_SCOPE_KEY] = True+ media_keepwarm.end_request(owner, counted = False)++def untrack_current_request(scope) -> None:"""Drop this request from the in-flight count once the route knows it won't use thelocal GGUF, so external-provider traffic can't trip the swap busy guard. Idempotent;@@ -711,6 +729,8 @@ class LlamaKeepWarmMiddleware:if chat_tracked:_note_untracked_end(is_preview)raise+ if isinstance(scope, dict):+ scope[_MEDIA_TRACKED_SCOPE_KEY] = media_ownerended = {"done": False}status = {"code": None}# Set once the terminal body frame (more_body False) is sent: only a response that completed cleanly adopts@@ -725,7 +745,9 @@ class LlamaKeepWarmMiddleware:returnended["done"] = Truecode = status["code"]- if media_owner is not None:+ if media_owner is not None and not (+ isinstance(scope, dict) and scope.get(_MEDIA_UNTRACKED_SCOPE_KEY)+ ):media_keepwarm.end_request(media_owner, counted = code not in (401, 403))if not chat_tracked:returndiff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.pyindex e0aaf59818b..65175bd4f2e 100644--- a/studio/backend/models/inference.py+++ b/studio/backend/models/inference.py@@ -4968,6 +4968,14 @@ class DiffusionGenerateRequest(BaseModel):description = "Noise schedule for the sampler. Omitted or 'automatic' uses the sampler's ""usual schedule (Karras for DPM++ 2M, Exponential for DPM++ 2M SDE, else the model's).",)+ provider_id: Optional[str] = Field(+ None,+ description = "[x-unsloth] Saved connection ID. When set, the prompt goes to that "+ "connection's /images/generations as `model` and the results are saved to the gallery.",+ )+ model: Optional[str] = Field(+ None, description = "Model name on the connection named by `provider_id`."+ )@field_validator("loras")@classmethod@@ -5209,6 +5217,11 @@ class DiffusionGenerateProgressResponse(BaseModel):None, description = "Live latent preview of the image being denoised, as a JPEG data URL")preview_seq: int = Field(0, description = "Moves each time a new preview is published")+ target: Optional[Literal["connection"]] = Field(+ None,+ description = "'connection' when the run is on a saved image connection, so a page that "+ "resumed it sends Stop to POST /images/generate/cancel?target=connection",+ )class DiffusionLoadProgressResponse(BaseModel):diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 15ccc8a2d10..2af2bfddec6 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -47613,11 +47613,209 @@ def _generate_failure_detail(message: str) -> str:return _GENERATE_FAILURE_FALLBACK+# Keyed by account id, not account_scope(): the scope moves when the first managed account appears.+_external_image_runs: dict[str, set] = {}+_stopped_external_image_runs: set = set()+++def _external_image_run_active() -> bool:+ return bool(_external_image_runs.get(current_account_id()))+++def _stop_external_image_runs() -> bool:+ runs = [+ task for task in _external_image_runs.get(current_account_id()) or () if not task.done()+ ]+ for task in runs:+ _stopped_external_image_runs.add(task)+ task.cancel()+ return bool(runs)+++async def _external_image_generation(+ body: DiffusionGenerateRequest, request: Request+) -> DiffusionGenerateResponse:+ from PIL import Image++ from core.inference import image_gallery+ from core.inference.llama_keepwarm import (+ untrack_current_media_request,+ untrack_current_request,+ )++ untrack_current_request(request.scope)+ untrack_current_media_request(request.scope)+ provider_id = body.provider_id.strip()+ external_model = (body.model or "").strip()+ if not external_model:+ raise HTTPException(+ status_code = 400,+ detail = "model is required when using an external image connection.",+ )+ if (+ body.init_image+ or body.reference_images+ or body.loras+ or body.controlnet+ or body.prompts+ or body.seeds+ or body.negative_prompt+ or body.mask_image+ or body.strength is not None+ or body.upscale is not None+ or body.workflow+ or body.localized_edit+ or body.sampler+ or body.schedule_type+ ):+ raise HTTPException(+ status_code = 400,+ detail = "An external image connection only supports text to image.",+ )++ config = await asyncio.to_thread(providers_db.get_provider, provider_id)+ if config is None:+ raise HTTPException(status_code = 404, detail = f"Provider config not found: {provider_id}")+ _refuse_decision_connection(config["provider_type"], config.get("api_type"))+ if not config["is_enabled"]:+ raise HTTPException(+ status_code = 400,+ detail = f"Provider '{config['display_name']}' is disabled.",+ )+ base_url = config["base_url"] or get_base_url(config["provider_type"])+ if not base_url:+ raise HTTPException(status_code = 400, detail = "The connection has no base URL.")+ try:+ base_url = validate_provider_base_url(base_url)+ except ValueError as exc:+ raise HTTPException(status_code = 400, detail = str(exc)) from None++ async with provider_config_guard(provider_id):+ current = await asyncio.to_thread(providers_db.get_provider, provider_id)+ routing_fields = ("provider_type", "base_url", "is_enabled")+ if current is None or any(+ current.get(field) != config.get(field) for field in routing_fields+ ):+ raise HTTPException(+ status_code = 409,+ detail = "Provider changed while the request was starting; retry.",+ )+ api_key = await asyncio.to_thread(+ resolve_provider_api_key_or_400,+ provider_id,+ None,+ allow_saved_key = not _request_has_api_key(request),+ )+ latest = await asyncio.to_thread(providers_db.get_provider, provider_id)+ if latest is None or any(+ latest.get(field) != current.get(field) for field in routing_fields+ ):+ raise HTTPException(+ status_code = 409,+ detail = "Provider changed while the request was starting; retry.",+ )++ client = ExternalProviderClient(+ provider_type = config["provider_type"],+ base_url = base_url,+ api_key = api_key,+ timeout = 600.0,+ )+ owner = current_account_id()+ task = asyncio.create_task(+ client.create_images(+ prompt = body.prompt,+ model = external_model,+ n = body.batch_size,+ size = f"{body.width}x{body.height}",+ )+ )+ _external_image_runs.setdefault(owner, set()).add(task)+ try:+ try:+ try:+ encoded = await task+ except asyncio.CancelledError:+ if task not in _stopped_external_image_runs:+ task.cancel()+ raise+ from core.inference.diffusion_families import DIFFUSION_CANCELLED_MSG+ raise HTTPException(status_code = 409, detail = DIFFUSION_CANCELLED_MSG) from None+ images = []+ for item in encoded:+ image = Image.open(io.BytesIO(base64.b64decode(item)))+ image.load()+ images.append(image)+ except HTTPException:+ raise+ except httpx.HTTPStatusError as exc:+ raise HTTPException(+ status_code = 502,+ detail = (+ f"Image endpoint returned HTTP {exc.response.status_code}: {exc.response.text[:300]}"+ ),+ ) from exc+ except httpx.HTTPError as exc:+ raise log_and_http_error(+ exc,+ 502,+ "Could not reach the image endpoint.",+ event = "external_images.transport_error",+ log = logger,+ ) from exc+ except Exception as exc:+ raise HTTPException(+ status_code = 502, detail = "The image endpoint did not return a usable image."+ ) from exc++ created_at = time.time()++ def _persist() -> list[dict]:+ return [+ image_gallery.save(+ image,+ {+ "prompt": body.prompt,+ "width": image.width,+ "height": image.height,+ "steps": body.steps,+ "guidance": body.guidance,+ "seed": body.seed or 0,+ "batch_index": index,+ "batch_size": body.batch_size,+ "model": external_model,+ "model_kind": "connection",+ "created_at": created_at,+ },+ )+ for index, image in enumerate(images)+ ]++ try:+ records = await asyncio.to_thread(_persist)+ except Exception as exc:+ logger.error("diffusion.persist_failed: %s", exc)+ raise HTTPException(status_code = 500, detail = "Failed to save the generated image.")+ return DiffusionGenerateResponse(images = [GalleryImage(**r) for r in records])+ finally:+ runs = _external_image_runs.get(owner)+ if runs is not None:+ runs.discard(task)+ if not runs:+ _external_image_runs.pop(owner, None)+ _stopped_external_image_runs.discard(task)++@studio_router.post("/images/generate", response_model = DiffusionGenerateResponse)@account_access.gpu_busy_routeasync def generate_diffusion_image(- request: DiffusionGenerateRequest, current_subject: str = Depends(get_current_subject)+ request: DiffusionGenerateRequest,+ http_request: Request,+ current_subject: str = Depends(get_current_subject),):+ if request.provider_id:+ return await _external_image_generation(request, http_request)+from core.inference import image_galleryfrom core.inference.diffusion_engine_router import get_active_diffusion_enginefrom core.inference.diffusion_families import (@@ -48587,11 +48785,18 @@ async def diffusion_load_progress(@studio_router.get("/images/generate-progress", response_model = DiffusionGenerateProgressResponse)async def diffusion_generate_progress(current_subject: str = Depends(get_current_subject)):- if account_access.managed_account() and account_access.generation_is_foreign("diffusion"):+ external = _external_image_run_active()++ def _hidden():+ if external:+ return DiffusionGenerateProgressResponse(active = True, target = "connection")return account_access.hidden_generate_progress_response(DiffusionGenerateProgressResponse)++ if account_access.managed_account() and account_access.generation_is_foreign("diffusion"):+ return _hidden()mine = account_access.generation_is_mine("diffusion")if not mine and account_access.resident_hidden("diffusion"):- return account_access.hidden_generate_progress_response(DiffusionGenerateProgressResponse)+ return _hidden()from core.inference.diffusion_engine_router import get_active_diffusion_engineif (@@ -48601,10 +48806,12 @@ async def diffusion_generate_progress(current_subject: str = Depends(get_current"diffusion", get_active_diffusion_engine().status().get("repo_id"))):- return account_access.hidden_generate_progress_response(DiffusionGenerateProgressResponse)+ return _hidden()progress = get_active_diffusion_engine().generate_progress()log_media_generation_progress("image", progress)+ if external and not progress["active"]:+ progress = {**progress, "active": True, "target": "connection"}# A finished generation still persisting its gallery record counts as active, so a reload probe keeps polling.if _diffusion_persist_active > 0 and not progress["active"]:progress = {**progress, "active": True}@@ -48612,7 +48819,9 @@ async def diffusion_generate_progress(current_subject: str = Depends(get_current@studio_router.post("/images/generate/cancel")-async def cancel_diffusion_generation(current_subject: str = Depends(get_current_subject)):+async def cancel_diffusion_generation(+ target: Optional[str] = None, current_subject: str = Depends(get_current_subject)+):"""Stop the in-flight image generation, mirroring POST /video/generate/cancel.Resolved through the engine router, so it stops a diffusers denoise (at its next step@@ -48620,7 +48829,10 @@ async def cancel_diffusion_generation(current_subject: str = Depends(get_currentgeneration's OWN request is what reports the outcome: it unwinds with the cancelledsentinel, which this module already maps to a 409. ``cancelled`` is False when nothing wasrunning, so the page can settle its button back to Generate rather than wait for a- generation that already finished."""+ generation that already finished. ``target=connection`` stops this account's runs on a+ saved image connection instead."""+ if target == "connection":+ return {"cancelled": _stop_external_image_runs()}if account_access.generation_is_foreign("diffusion"):return {"cancelled": False}mine = account_access.generation_is_mine("diffusion")diff --git a/studio/backend/tests/test_diffusion_routes.py b/studio/backend/tests/test_diffusion_routes.pyindex 0da728385ec..baacf0b21c9 100644--- a/studio/backend/tests/test_diffusion_routes.py+++ b/studio/backend/tests/test_diffusion_routes.py@@ -3048,3 +3048,353 @@ def test_a_managed_account_cannot_send_allow_oversized(client, monkeypatch):)assert resp.status_code == 200assert seen == [False]+++def _external_image_connection(monkeypatch, handler):+ import httpx++ import core.inference.external_provider as external_provider++ monkeypatch.setattr(+ external_provider,+ "_http_client",+ httpx.AsyncClient(transport = httpx.MockTransport(handler)),+ )+ monkeypatch.setattr(+ inference_routes.providers_db,+ "get_provider",+ lambda pid: (+ {+ "provider_type": "custom",+ "display_name": "Gufo",+ "base_url": "http://127.0.0.1:8000/v1",+ "is_enabled": True,+ }+ if pid == "conn-1"+ else None+ ),+ )+ monkeypatch.setattr(+ inference_routes, "resolve_provider_api_key_or_400", lambda *a, **k: "sk-test"+ )+++def test_external_connection_generates_without_a_local_model_and_saves_to_gallery(+ client, monkeypatch+):+ import base64+ import io+ import json++ import httpx+ from PIL import Image++ sent = []++ def handler(req):+ sent.append((str(req.url), req.headers.get("authorization"), json.loads(req.content)))+ buf = io.BytesIO()+ Image.new("RGB", (512, 256), (200, 30, 30)).save(buf, format = "PNG")+ png = base64.b64encode(buf.getvalue()).decode()+ return httpx.Response(200, json = {"created": 0, "data": [{"b64_json": png}] * 2})++ _external_image_connection(monkeypatch, handler)+ resp = _post_generate(+ client,+ prompt = "a sloth",+ width = 512,+ height = 256,+ batch_size = 2,+ provider_id = "conn-1",+ model = "qwen-image",+ )+ assert resp.status_code == 200, resp.text+ images = resp.json()["images"]+ assert [(i["prompt"], i["model"], i["width"], i["height"]) for i in images] == [+ ("a sloth", "qwen-image", 512, 256)+ ] * 2+ assert len(client.get("/api/inference/images/gallery").json()["images"]) == 2+ assert sent == [+ (+ "http://127.0.0.1:8000/v1/images/generations",+ "Bearer sk-test",+ {+ "model": "qwen-image",+ "prompt": "a sloth",+ "n": 2,+ "size": "512x256",+ "response_format": "b64_json",+ },+ )+ ]+++def test_external_connection_error_is_a_502_with_the_upstream_reason(client, monkeypatch):+ import httpx++ _external_image_connection(+ monkeypatch, lambda req: httpx.Response(404, text = "model 'nope' not found")+ )+ resp = _post_generate(client, prompt = "a sloth", provider_id = "conn-1", model = "nope")+ assert resp.status_code == 502+ assert "model 'nope' not found" in resp.text+ assert client.get("/api/inference/images/gallery").json()["images"] == []+++def test_external_connection_does_not_hold_local_chat_swaps(client, monkeypatch):+ import base64+ import io++ import httpx+ from PIL import Image++ import core.inference.llama_keepwarm as kw++ client.app.add_middleware(kw.LlamaKeepWarmMiddleware)+ monkeypatch.setattr(kw, "_inflight", 0)+ seen = []++ def handler(req):+ seen.append(+ kw.other_inference_request_count(current_request_counted = False, include_pending = False)+ )+ buf = io.BytesIO()+ Image.new("RGB", (64, 64)).save(buf, format = "PNG")+ return httpx.Response(+ 200, json = {"data": [{"b64_json": base64.b64encode(buf.getvalue()).decode()}]}+ )++ _external_image_connection(monkeypatch, handler)+ resp = client.post(+ "/api/inference/images/generate",+ json = {"prompt": "a sloth", "provider_id": "conn-1", "model": "qwen-image"},+ headers = {"Authorization": "Bearer session"},+ )+ assert resp.status_code == 200, resp.text+ assert seen == [0]+ assert kw._inflight == 0+++def test_external_connection_refuses_a_key_read_after_the_connection_moved(client, monkeypatch):+ import httpx++ sent = []+ _external_image_connection(monkeypatch, lambda req: sent.append(req) or httpx.Response(500))+ rows = iter(["http://127.0.0.1:8000/v1"] * 2 + ["http://10.0.0.9:9000/v1"])+ monkeypatch.setattr(+ inference_routes.providers_db,+ "get_provider",+ lambda pid: {+ "provider_type": "custom",+ "display_name": "Gufo",+ "base_url": next(rows),+ "is_enabled": True,+ },+ )+ resp = _post_generate(client, prompt = "a sloth", provider_id = "conn-1", model = "qwen-image")+ assert resp.status_code == 409+ assert "Provider changed" in resp.json()["detail"]+ assert sent == []+++def test_stop_cancels_a_connection_run_and_progress_shows_it(client, monkeypatch):+ import asyncio++ import httpx++ started = asyncio.Event()+ calls = []++ async def handler(req):+ calls.append(req)+ started.set()+ await asyncio.sleep(30)+ return httpx.Response(500)++ _external_image_connection(monkeypatch, handler)++ async def run():+ transport = httpx.ASGITransport(app = client.app)+ async with httpx.AsyncClient(transport = transport, base_url = "http://t") as ac:+ gen = asyncio.create_task(+ ac.post(+ "/api/inference/images/generate",+ json = {"prompt": "a sloth", "provider_id": "conn-1", "model": "qwen-image"},+ )+ )+ await asyncio.wait_for(started.wait(), 5)+ progress = (await ac.get("/api/inference/images/generate-progress")).json()+ local = (await ac.post("/api/inference/images/generate/cancel")).json()+ stop = (await ac.post("/api/inference/images/generate/cancel?target=connection")).json()+ resp = await asyncio.wait_for(gen, 5)+ after = (await ac.get("/api/inference/images/generate-progress")).json()+ return progress, local, stop, resp, after++ progress, local, stop, resp, after = asyncio.run(run())+ assert progress["active"] is True+ # A page that resumed this run reads its Stop target from progress.+ assert progress["target"] == "connection"+ assert local == {"cancelled": False}+ assert stop == {"cancelled": True}+ assert resp.status_code == 409+ assert after["active"] is False+ assert after["target"] is None+ assert len(calls) == 1+ assert client.get("/api/inference/images/gallery").json()["images"] == []+++def test_stop_reaches_a_connection_run_after_the_account_scope_moves(client, monkeypatch):+ import asyncio++ import httpx+ import hub.services.models.account_access as account_access++ started = asyncio.Event()++ async def handler(req):+ started.set()+ await asyncio.sleep(30)+ return httpx.Response(500)++ _external_image_connection(monkeypatch, handler)++ async def run():+ transport = httpx.ASGITransport(app = client.app)+ async with httpx.AsyncClient(transport = transport, base_url = "http://t") as ac:+ gen = asyncio.create_task(+ ac.post(+ "/api/inference/images/generate",+ json = {"prompt": "a sloth", "provider_id": "conn-1", "model": "qwen-image"},+ )+ )+ await asyncio.wait_for(started.wait(), 5)+ # The first managed account appears mid-run: the owner's scope goes from None to its id.+ monkeypatch.setattr(account_access, "account_scope", lambda: "owner-scope")+ progress = (await ac.get("/api/inference/images/generate-progress")).json()+ stop = (await ac.post("/api/inference/images/generate/cancel?target=connection")).json()+ resp = await asyncio.wait_for(gen, 5)+ return progress, stop, resp++ progress, stop, resp = asyncio.run(run())+ assert progress["active"] is True+ assert stop == {"cancelled": True}+ assert resp.status_code == 409+++def test_external_connection_refuses_options_it_cannot_forward(client, monkeypatch):+ import httpx++ sent = []+ _external_image_connection(monkeypatch, lambda req: sent.append(req) or httpx.Response(500))+ for extra in ({"negative_prompt": "blurry"}, {"sampler": "euler"}, {"strength": 0.5}):+ resp = _post_generate(+ client, prompt = "a sloth", provider_id = "conn-1", model = "qwen-image", **extra+ )+ assert resp.status_code == 400, (extra, resp.text)+ assert sent == []+++def test_connection_images_are_marked_in_the_gallery(client, monkeypatch):+ import base64+ import io++ import httpx+ from PIL import Image++ def handler(req):+ buf = io.BytesIO()+ Image.new("RGB", (64, 64)).save(buf, format = "PNG")+ return httpx.Response(+ 200, json = {"data": [{"b64_json": base64.b64encode(buf.getvalue()).decode()}]}+ )++ _external_image_connection(monkeypatch, handler)+ resp = _post_generate(client, prompt = "a sloth", provider_id = "conn-1", model = "qwen-image")+ assert resp.status_code == 200, resp.text+ assert resp.json()["images"][0]["model_kind"] == "connection"+++def test_a_connection_run_reports_active_even_when_local_progress_is_hidden(client, monkeypatch):+ import hub.services.models.account_access as account_access++ monkeypatch.setattr(account_access, "managed_account", lambda: True)+ monkeypatch.setattr(account_access, "generation_is_foreign", lambda kind: True)+ assert client.get("/api/inference/images/generate-progress").json()["active"] is False+ from utils.account_context import current_account_id++ monkeypatch.setitem(inference_routes._external_image_runs, current_account_id(), {object()})+ progress = client.get("/api/inference/images/generate-progress").json()+ assert progress["active"] is True+ assert progress["target"] == "connection"+++def test_a_connection_call_leaves_the_media_tracker(client, monkeypatch):+ import base64+ import io++ import httpx+ from PIL import Image++ import core.inference.llama_keepwarm as kw+ import core.inference.media_keepwarm as mk++ client.app.add_middleware(kw.LlamaKeepWarmMiddleware)+ seen = []++ def handler(req):+ seen.append(mk._TRACKERS[mk.DIFFUSION].outstanding())+ buf = io.BytesIO()+ Image.new("RGB", (64, 64)).save(buf, format = "PNG")+ return httpx.Response(+ 200, json = {"data": [{"b64_json": base64.b64encode(buf.getvalue()).decode()}]}+ )++ _external_image_connection(monkeypatch, handler)+ before = mk._TRACKERS[mk.DIFFUSION].outstanding()+ resp = client.post(+ "/api/inference/images/generate",+ json = {"prompt": "a sloth", "provider_id": "conn-1", "model": "qwen-image"},+ headers = {"Authorization": "Bearer session"},+ )+ assert resp.status_code == 200, resp.text+ assert seen == [before]+ assert mk._TRACKERS[mk.DIFFUSION].outstanding() == before+++def test_stop_after_the_connection_answered_is_not_acknowledged(client, monkeypatch):+ import asyncio++ from utils.account_context import current_account_id++ async def run():+ done = asyncio.get_running_loop().create_future()+ done.set_result(None)+ task = asyncio.ensure_future(done)+ await task+ inference_routes._external_image_runs[current_account_id()] = {task}+ try:+ return inference_routes._stop_external_image_runs()+ finally:+ inference_routes._external_image_runs.clear()++ assert asyncio.run(run()) is False+++def test_connection_png_parameters_leave_out_unsent_settings():+ from core.inference.image_gallery import _params_text++ text = _params_text(+ {+ "prompt": "a sloth",+ "steps": 9,+ "guidance": 0.0,+ "seed": 123,+ "width": 512,+ "height": 256,+ "model": "qwen-image",+ "model_kind": "connection",+ }+ )+ assert text == "a sloth\nSize: 512x256, Model: qwen-image"+ local = _params_text({"prompt": "a sloth", "steps": 9, "guidance": 0.0, "seed": 123})+ assert "Steps: 9" in local and "Seed: 123" in localdiff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.tsindex e6088b433bc..75ccf8fe9f5 100644--- a/studio/frontend/src/features/chat/index.ts+++ b/studio/frontend/src/features/chat/index.ts@@ -328,6 +328,7 @@ export { ModelLoadDescription } from "./components/model-load-status";export { chatModelLoaded } from "./lib/chat-model-loaded";export type { ChatModelLoadedInput } from "./lib/chat-model-loaded";export {+ buildExternalModelId,customProviderDisplayName,isCustomProviderType,isDecisionConnection,diff --git a/studio/frontend/src/features/images/api.ts b/studio/frontend/src/features/images/api.tsindex bc08b8c2ad2..a2b1fecf9a8 100644--- a/studio/frontend/src/features/images/api.ts+++ b/studio/frontend/src/features/images/api.ts@@ -116,6 +116,8 @@ export interface DiffusionGenerateProgress {// Live latent preview of the image being denoised (small JPEG data URL), and a counter that moves with each one.preview?: string | null;preview_seq?: number;+ // "connection" while the run is on a saved image connection: the Stop target for a resumed run.+ target?: "connection" | null;}export interface DiffusionLoadProgress {@@ -449,10 +451,7 @@ export async function generateDiffusionImage(// A proxy answers with HTML (or nothing); the app answers with JSON. Only the former means the request may still// be running: settling an application error would poll for a generation that never started and hide the reason// the backend gave.- if (- response.status === 503 &&- (response.headers.get("content-type") || "").toLowerCase().includes("application/json")- ) {+ if ((response.headers.get("content-type") || "").toLowerCase().includes("application/json")) {throw new Error(detail);}throw new GenerateResponseLostError(detail);diff --git a/studio/frontend/src/features/images/connection-cancel.ts b/studio/frontend/src/features/images/connection-cancel.tsnew file mode 100644index 00000000000..0112eb4c6a6--- /dev/null+++ b/studio/frontend/src/features/images/connection-cancel.ts@@ -0,0 +1,19 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import { authFetch } from "@/features/auth";+import { readFastApiError } from "@/lib/format-fastapi-error";++/** Stop this account's runs on a saved image connection. Kept out of api.ts, which is in the startup+ * bundle; same no-retry, abortable contract as cancelDiffusionGeneration. */+export async function cancelConnectionImageGeneration(+ signal?: AbortSignal,+): Promise<{ cancelled: boolean }> {+ const response = await authFetch(+ "/api/inference/images/generate/cancel?target=connection",+ { method: "POST", signal },+ { retryNetworkErrors: false },+ );+ if (!response.ok) throw new Error(await readFastApiError(response));+ return (await response.json()) as { cancelled: boolean };+}diff --git a/studio/frontend/src/features/images/images-page.tsx b/studio/frontend/src/features/images/images-page.tsxindex 9f8510539c3..3eace9bb9ec 100644--- a/studio/frontend/src/features/images/images-page.tsx+++ b/studio/frontend/src/features/images/images-page.tsx@@ -96,6 +96,7 @@ import {} from "@/hooks/use-host-class";import { nvfp4SelectionFallback, withNvfp4Option } from "@/lib/nvfp4-options";import type {+ ExternalModelOption,ModelOption,ModelSelectorChangeMeta,} from "@/features/model-picker/components/model-selector/types";@@ -145,8 +146,15 @@ import {recipeWorkflowLabel,type WorkflowId,} from "./workflows";-import { ParamSlider } from "@/features/chat";+import {+ ParamSlider,+ buildExternalModelId,+ isDecisionConnection,+ parseExternalModelId,+ useExternalProvidersStore,+} from "@/features/chat";import { ModelLoadDescription } from "@/features/chat/components/model-load-status";+import { cancelConnectionImageGeneration } from "./connection-cancel";import {type ImageGenerationPresetParams,MediaGenerationPresetControl,@@ -720,6 +728,7 @@ function loadToastArgs(}const NO_SAMPLERS: DiffusionSamplerOption[] = [];+const IMAGE_CONNECTION_TYPES = new Set(["custom", "vllm", "llama_cpp", "lemonade", "ollama"]);const IDLE_PROGRESS: DiffusionLoadProgress = {phase: null,@@ -1194,6 +1203,7 @@ function RecipePopover({useEffect(() => {if (!active) setOpen(false);}, [active]);+ const viaConnection = image.model_kind === "connection";return (<Popover open={active && open} onOpenChange={(o) => setOpen(active && o)}><PopoverTrigger asChild>@@ -1290,15 +1300,15 @@ function RecipePopover({/>) : null}<RecipeRow label="Size" value={`${image.width} × ${image.height}`} />- <RecipeRow label="Steps" value={String(image.steps)} />- <RecipeRow label="Guidance" value={String(image.guidance)} />+ {!viaConnection && <RecipeRow label="Steps" value={String(image.steps)} />}+ {!viaConnection && <RecipeRow label="Guidance" value={String(image.guidance)} />}{samplerRecipeLabel(image.sampler, image.schedule_type) ? (<RecipeRowlabel="Sampler"value={samplerRecipeLabel(image.sampler, image.schedule_type) ?? ""}/>) : null}- <RecipeRow label="Seed" value={String(image.seed)} mono />+ {!viaConnection && <RecipeRow label="Seed" value={String(image.seed)} mono />}</div><div className="shrink-0 border-t border-border/60 px-3 py-2.5"><Button size="sm" className="w-full gap-1.5" onClick={() => onRestore(image)}>@@ -1459,6 +1469,34 @@ export function ImagesPage({const nvfp4Diffusion = useNvfp4Diffusion();const nvfp4DiffusionKnown = useNvfp4DiffusionKnown();const imageModels = useImageModels(hostClass, denseQuantSchemes);+ const connections = useExternalProvidersStore((s) => s.providers);+ const connectionsEnabled = useExternalProvidersStore((s) => s.connectionsEnabled);+ const externalModels = useMemo<ExternalModelOption[]>(+ () =>+ connectionsEnabled+ ? connections+ .filter((c) => IMAGE_CONNECTION_TYPES.has(c.providerType) && !isDecisionConnection(c))+ .flatMap((c) =>+ c.models.map((model) => ({+ id: buildExternalModelId(c.id, model),+ name: model,+ providerId: c.id,+ providerName: c.name,+ providerType: c.providerType,+ })),+ )+ : [],+ [connections, connectionsEnabled],+ );+ const [externalModelId, setExternalModelId] = useState<string | null>(null);+ const runTarget = useRef<"connection" | undefined>(undefined);+ const external = useMemo(+ () =>+ externalModels.some((m) => m.id === externalModelId)+ ? parseExternalModelId(externalModelId)+ : null,+ [externalModels, externalModelId],+ );const { rootStyle: railRootStyle } = useMediaRailWidth("images");const [quant, setQuant] = useState<string | null>(galleryCache.quant);// One prompt per workflow, starting from the last one generated with.@@ -1668,7 +1706,8 @@ export function ImagesPage({const genVisibilityListener = useRef<(() => void) | null>(null);const [status, setStatus] = useState<DiffusionStatus | null>(null);const { familyOverride, setFamilyOverride, familySelect, opaqueKind, selectorModelId } = useFamilyOverride(status, status?.supported_families);- const conditioning = status?.loaded ? (status.conditioning ?? null) : null;+ // A connection is not bound by the resident model's size grid or presets.+ const conditioning = !external && status?.loaded ? (status.conditioning ?? null) : null;const sizeLimits = useMemo(() => sizeLimitsFrom(conditioning), [conditioning]);const unifiedEdit = Boolean(conditioning?.unified_edit);const referenceResolutions = conditioning?.reference_resolutions ?? [];@@ -1896,7 +1935,7 @@ export function ImagesPage({// Refresh the LoRA picker when the loaded family changes, since a LoRA is family-specific.// Not on first load or unload (a restore can precede the load), so track it in a ref.- const loraCapable = Boolean(status?.loaded && status?.supports_lora);+ const loraCapable = Boolean(!external && status?.loaded && status?.supports_lora);const prevLoraFamilyRef = useRef<string | null | undefined>(undefined);// Whether the load in flight baked the LoRA selection into the build (see handleLoad).const bakedLorasOnLoad = useRef(false);@@ -1960,9 +1999,10 @@ export function ImagesPage({}, [loraCapable, residentBuildKey, status?.resolved, loras]);// Refresh the ControlNet options when the loaded family changes, and clear a stale selection.- const controlnetCapable = Boolean(status?.loaded && status?.supports_controlnet);- const negativeCapable = status?.supports_negative_prompt !== false;- const samplerOptions = (status?.loaded ? status.samplers : null) ?? NO_SAMPLERS;+ const controlnetSupported = Boolean(status?.loaded && status?.supports_controlnet);+ const controlnetCapable = !external && controlnetSupported;+ const negativeCapable = !external && status?.supports_negative_prompt !== false;+ const samplerOptions = (!external && status?.loaded ? status.samplers : null) ?? NO_SAMPLERS;// The checkpoint, not residentDefaults: community SDXL files all share the SDXL base repo.const samplerModelKey = status?.loaded ? `${status.repo_id ?? ""}|${status.gguf_filename ?? ""}` : "";const samplerChoice = normalizeSamplerChoice(@@ -1983,7 +2023,7 @@ export function ImagesPage({[samplerChoice, samplerModelKey, samplerOptions],);useEffect(() => {- if (!controlnetCapable) {+ if (!controlnetSupported) {setAvailableControlNets([]);setControlnetId("");setControlImage(null);@@ -2002,7 +2042,7 @@ export function ImagesPage({return () => {cancelled = true;};- }, [controlnetCapable, status?.family]);+ }, [controlnetSupported, status?.family]);// The control types offered for the selected ControlNet: a union model advertises several.const controlTypeOptions = useMemo(() => {@@ -2493,12 +2533,15 @@ export function ImagesPage({);const restoreSettings = useCallback((image: GalleryImage) => {+ const viaConnection = image.model_kind === "connection";// Negative prompt only applies when guidance>0; do not restore a hidden value.const restoredNegative = image.guidance > 0 ? (image.negative_prompt ?? "") : "";setNegativePrompt(restoredNegative);if (restoredNegative) setNegativeOpen(true);- setSteps(image.steps);- setGuidance(image.guidance);+ if (!viaConnection) {+ setSteps(image.steps);+ setGuidance(image.guidance);+ }const restoredChoice = {sampler: image.sampler ?? DEFAULT_SAMPLER,scheduleType: image.schedule_type ?? AUTOMATIC_SCHEDULE,@@ -2507,7 +2550,7 @@ export function ImagesPage({else setRestoredSampler(restoredChoice);// Restore from the BASE batch seed, not this image's derived seed, or a replay with batch_size// advances it again.- setSeed(String(image.batch_seed ?? image.seed));+ if (!viaConnection) setSeed(String(image.batch_seed ?? image.seed));const restored = restorableSize(image.width, image.height, image.workflow, sizeLimits);setWidth(restored.width);setHeight(restored.height);@@ -2807,6 +2850,7 @@ export function ImagesPage({pollInFlight = true;try {const p = await getGenerateProgress();+ if (p.active) runTarget.current = p.target ?? undefined;if (!p.active) {if (genPollTimer.current) clearInterval(genPollTimer.current);genPollTimer.current = null;@@ -2859,6 +2903,7 @@ export function ImagesPage({try {const g = await getGenerateProgress();if (g.active) {+ runTarget.current = g.target ?? undefined;setBusy("generating");setGenStep(g);resumeGeneratePoll();@@ -3039,6 +3084,7 @@ export function ImagesPage({// Reuse the pick toast when loading starts.pickToastId?: string,): Promise<boolean> => {+ setExternalModelId(null);// Cancel any prior poll loop so two cannot run at once.if (pollTimer.current) clearTimeout(pollTimer.current);// Read BEFORE the start request goes out: a Cancel pressed while it is in flight sends an@@ -3582,7 +3628,10 @@ export function ImagesPage({// already staging with its claim, or resumePendingLoad drops that load once its files arrive.const downloadOnlyPick = modelSelectionAction === "download";const token = downloadOnlyPick ? undefined : pickGuard.claim();- if (!downloadOnlyPick) setFamilyOverride("auto");+ if (!downloadOnlyPick) {+ setFamilyOverride("auto");+ setExternalModelId(null);+ }void navigateSelf({ to: "/images", search: {}, replace: true });// A label means a GGUF repo whatever the catalog says, and is not loadable, so resolve it// rather than routing it as a filename.@@ -3696,6 +3745,19 @@ export function ImagesPage({// with a 409. A download-only pick submits no load, so that cannot happen, and the selector// stays interactive during a generation: refusing it there was a silent dead click.if (busy !== null && !downloadOnlyPick) return;+ if (meta.source === "external") {+ if (busy !== null) return;+ if (quantRevert.current && quantRevert.current === stagedQuantRevert.current) {+ revertPick(quantRevert.current);+ quantRevert.current = null;+ }+ pickGuard.cancel();+ pickToast.dismissAll();+ beginPick();+ setExternalModelId(id);+ return;+ }+ if (!downloadOnlyPick) setExternalModelId(null);if (!downloadOnlyPick) beginPick();// This pick owns the page now, so one still awaiting a listing or a plan drops out. Before any// branch, since staging never sets `busy`.@@ -3883,6 +3945,7 @@ export function ImagesPage({loadGgufRepoPick,loadOrStage,pickGuard,+ pickToast,quant,revertPick,],@@ -3906,6 +3969,7 @@ export function ImagesPage({// The deploy owns the page now: a resolving pick or a staged download would load over the base it is about to.pickGuard.cancel();pickToast.dismissAll();+ setExternalModelId(null);pendingDeploy.current = { loraId: stem, family: args.family };if (args.trigger.trim()) setPrompt(args.trigger.trim());setPageMode("create");@@ -4375,7 +4439,7 @@ export function ImagesPage({}// Resolve a base seed up front, so each sequential image gets a distinct, reproducible seed (base + i).let baseSeed: number;- if (seed.trim()) {+ if (seed.trim() && !external) {const n = Number(seed);if (!Number.isInteger(n) || n < 0 || n > Number.MAX_SAFE_INTEGER) {toast.error("Seed must be a non-negative integer");@@ -4422,6 +4486,7 @@ export function ImagesPage({cancelAbort.current?.abort();cancelAbort.current = null;runToken.current += 1;+ runTarget.current = external ? "connection" : undefined;// Poll the backend's per-step progress across the whole run. A named poll body also serves the// visibilitychange listener, so a throttled tab catches up when visible.let pollInFlight = false;@@ -4515,6 +4580,7 @@ export function ImagesPage({strength: controlStrength,}: undefined,+ ...(external && { provider_id: external.providerId, model: external.modelId }),});} catch (err) {// The POST response was lost while the backend kept generating (the secure-mode tunnel caps@@ -4581,7 +4647,7 @@ export function ImagesPage({setGenStep(null);setStopping(false);}- }, [allowOversized, livePreview, prompt, negativePrompt, negativeCapable, restoredSampler, samplerModelKey, samplerOptions, samplerChoice, width, height, steps, guidance, seed, batchSize, count, workflow, initImage, maskImage, strength, extendPct, extendSides, upscaleFactor, upscaleStrength, referenceImages, loras, loraCapable, controlnetCapable, controlnetId, controlImage, controlType, controlStrength, ensureSrc, loadGallery, refreshStatus, unifiedEdit, localizedMode, localizedLayer, maxExtras, referenceResolution, conditioning, editSize, editSizing, sizeLimits]);+ }, [external, allowOversized, livePreview, prompt, negativePrompt, negativeCapable, restoredSampler, samplerModelKey, samplerOptions, samplerChoice, width, height, steps, guidance, seed, batchSize, count, workflow, initImage, maskImage, strength, extendPct, extendSides, upscaleFactor, upscaleStrength, referenceImages, loras, loraCapable, controlnetCapable, controlnetId, controlImage, controlType, controlStrength, ensureSrc, loadGallery, refreshStatus, unifiedEdit, localizedMode, localizedLayer, maxExtras, referenceResolution, conditioning, editSize, editSizing, sizeLimits]);// Stop the in-flight generation. Latch FIRST, so a multi-run request stops even if the POST// races the run that is already finishing.@@ -4596,7 +4662,9 @@ export function ImagesPage({const abort = new AbortController();cancelAbort.current = abort;try {- const { cancelled } = await cancelDiffusionGeneration(abort.signal);+ const { cancelled } = runTarget.current+ ? await cancelConnectionImageGeneration(abort.signal)+ : await cancelDiffusionGeneration(abort.signal);cancelAcked.current = Boolean(cancelled);if (!cancelled) setStopping(false);} catch {@@ -4616,6 +4684,10 @@ export function ImagesPage({const handleGenerateWithRecall = useCallback(async () => {if (busy !== null || !imagePresets.hydrated) return;+ if (external) {+ await handleGenerate();+ return;+ }if (status?.loaded) {const kind = status.model_kind;if (@@ -4672,6 +4744,7 @@ export function ImagesPage({}, [busy,currentLoadAdvanced,+ external,handleGenerate,handleLoad,imagePresets.hydrated,@@ -4721,6 +4794,10 @@ export function ImagesPage({// Publish what the loaded model can do, so the sidebar submenu dims the rest. null while// nothing is loaded, which leaves every workflow open to set up first.useEffect(() => {+ if (external) {+ setSupported(["create"]);+ return;+ }if (!status?.loaded) {setSupported(null);return;@@ -4731,7 +4808,7 @@ export function ImagesPage({wf.includes(t.requires === null ? "txt2img" : t.requires),).map((t) => t.id),);- }, [status?.loaded, status?.workflows, setSupported]);+ }, [external, status?.loaded, status?.workflows, setSupported]);// Keep the active workflow valid for the loaded model: snap to the first supported one when capabilities change.useEffect(() => {@@ -4977,7 +5054,8 @@ export function ImagesPage({<ModelSelectortriggerDataTour="images-model"models={imageModels}- value={selectorModelId}+ externalModels={externalModels}+ value={(external && externalModelId) || selectorModelId}loadedModelIdOverride={selectorModelId}activeGgufVariant={quant}onValueChange={handleModelSelect}@@ -5691,26 +5769,30 @@ export function ImagesPage({{!unifiedEditActive && sizeControls}{/* First of the one-line sliders, so it takes a bigger break than the gap gives. */}- <div className="pt-2">- <SliderField- label="Steps"- hint="Number of denoising steps. Start with the selected model's default; more steps take longer and may not improve quality."- value={steps}- min={1}- max={50}- step={1}- onChange={setSteps}- />- </div>- <SliderField- label="Guidance"- hint="Controls how strongly the model follows the prompt. Start with the selected model's default; distilled models may require low or zero guidance."- value={guidance}- min={0}- max={15}- step={0.5}- onChange={setGuidance}- />+ {!external && (+ <>+ <div className="pt-2">+ <SliderField+ label="Steps"+ hint="Number of denoising steps. Start with the selected model's default; more steps take longer and may not improve quality."+ value={steps}+ min={1}+ max={50}+ step={1}+ onChange={setSteps}+ />+ </div>+ <SliderField+ label="Guidance"+ hint="Controls how strongly the model follows the prompt. Start with the selected model's default; distilled models may require low or zero guidance."+ value={guidance}+ min={0}+ max={15}+ step={0.5}+ onChange={setGuidance}+ />+ </>+ )}<SliderFieldlabel="Batch size"hint="How many images to make at once. Faster than running them one by one, but uses more VRAM. They share a seed but each one is different."@@ -5773,21 +5855,25 @@ export function ImagesPage({</div></Field>)}- <Field- label="Seed"- hint="Leave empty for a fresh random seed each run."- className="pt-2"- >- <Input- placeholder="Random if empty"- value={seed}- onChange={(e) => setSeed(e.target.value)}- />- </Field>+ {!external && (+ <>+ <Field+ label="Seed"+ hint="Leave empty for a fresh random seed each run."+ className="pt-2"+ >+ <Input+ placeholder="Random if empty"+ value={seed}+ onChange={(e) => setSeed(e.target.value)}+ />+ </Field>- <AdvancedDisclosure open={advancedOpen} onOpenChange={setAdvancedOpen}>- {advancedControls}- </AdvancedDisclosure>+ <AdvancedDisclosure open={advancedOpen} onOpenChange={setAdvancedOpen}>+ {advancedControls}+ </AdvancedDisclosure>+ </>+ )}</div>{/* The scroll mask provides the fade; leave the footer unpainted to avoid dark-mode banding. */}@@ -5808,11 +5894,11 @@ export function ImagesPage({<ButtonclassName="relative z-10 h-11 px-8 disabled:bg-muted disabled:text-muted-foreground disabled:opacity-100"onClick={handleGenerateWithRecall}- disabled={busy !== null || !imagePresets.hydrated || (!status?.loaded && !rememberedModel)}+ disabled={busy !== null || !imagePresets.hydrated || (!status?.loaded && !rememberedModel && !external)}>Generate</Button>- {status?.loaded && (canReapply || status?.model_kind === "pipeline") && (+ {!external && status?.loaded && (canReapply || status?.model_kind === "pipeline") && (<Tooltip><TooltipTrigger asChild={true}><Button@@ -6007,7 +6093,7 @@ export function ImagesPage({{/* Same icon as the Images nav item. */}<HugeiconsIcon icon={Image03Icon} className="size-12" strokeWidth={1.5} /><p className="text-sm">- {status?.loaded+ {status?.loaded || external? "Enter a prompt and hit Generate.": "Select a diffusion model to load"}</p>diff --git a/studio/frontend/tests/image-generate-upstream-error.test.ts b/studio/frontend/tests/image-generate-upstream-error.test.tsnew file mode 100644index 00000000000..52a569fb020--- /dev/null+++ b/studio/frontend/tests/image-generate-upstream-error.test.ts@@ -0,0 +1,96 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import { register } from "node:module";+import test, { afterEach } from "node:test";++register("./helpers/settings-api-resolver.mjs", import.meta.url);++const originalFetch = globalThis.fetch;+afterEach(() => {+ globalThis.fetch = originalFetch;+});++const { cancelDiffusionGeneration, generateDiffusionImage, GenerateResponseLostError } = await import(+ "../src/features/images/api.ts"+);++function respond(status: number, body: string, contentType: string) {+ globalThis.fetch = (async () =>+ new Response(body, { status, headers: { "content-type": contentType } })) as typeof fetch;+}++test("a JSON 502 from the app shows the upstream reason instead of waiting for a lost run", async () => {+ respond(+ 502,+ JSON.stringify({ detail: "Image endpoint returned HTTP 401: invalid api key" }),+ "application/json",+ );+ await assert.rejects(generateDiffusionImage({ prompt: "a sloth" }), (err: unknown) => {+ assert.ok(!(err instanceof GenerateResponseLostError));+ assert.match((err as Error).message, /invalid api key/);+ return true;+ });+});++test("a proxy's HTML 502 is still a lost response", async () => {+ respond(502, "<html>Bad gateway</html>", "text/html");+ await assert.rejects(+ generateDiffusionImage({ prompt: "a sloth" }),+ (err: unknown) => err instanceof GenerateResponseLostError,+ );+});++test("Stop on a connection run targets the connection, a local Stop does not", async () => {+ const urls: string[] = [];+ globalThis.fetch = (async (input: RequestInfo | URL) => {+ urls.push(String(input));+ return new Response(JSON.stringify({ cancelled: true }), {+ status: 200,+ headers: { "content-type": "application/json" },+ });+ }) as typeof fetch;+ const { cancelConnectionImageGeneration } = await import(+ "../src/features/images/connection-cancel.ts"+ );+ await cancelConnectionImageGeneration();+ await cancelDiffusionGeneration();+ assert.deepEqual(urls, [+ "/api/inference/images/generate/cancel?target=connection",+ "/api/inference/images/generate/cancel",+ ]);+});++test("a connected pick retires a staged local pick and skips the hidden seed", async () => {+ const { readSrc } = await import("./helpers/kit.ts");+ const page = readSrc("features/images/images-page.tsx");+ const branch = page.slice(page.indexOf('if (meta.source === "external") {'));+ const body = branch.slice(0, branch.indexOf("setExternalModelId(id);"));+ assert.doesNotMatch(body, /downloadOnlyPick/);+ assert.match(body, /pickGuard\.cancel\(\);/);+ assert.match(body, /beginPick\(\);/);+ assert.match(page, /if \(seed\.trim\(\) && !external\) \{/);+});++test("any local load takes the page back from a connection", async () => {+ const { readSrc } = await import("./helpers/kit.ts");+ const page = readSrc("features/images/images-page.tsx");+ const load = page.slice(page.indexOf("const handleLoad = useCallback("));+ const head = load.slice(0, load.indexOf("if (pollTimer.current) clearTimeout(pollTimer.current);"));+ assert.match(head, /setExternalModelId\(null\);/);+});++test("the connection Stop stays out of the startup bundle's api module", async () => {+ const { readSrc } = await import("./helpers/kit.ts");+ assert.doesNotMatch(readSrc("features/images/api.ts"), /target=connection/);+ assert.match(readSrc("features/images/images-page.tsx"), /from "\.\/connection-cancel"/);+});++test("a connected pick hides ControlNet without clearing the local setup", async () => {+ const { readSrc } = await import("./helpers/kit.ts");+ const page = readSrc("features/images/images-page.tsx");+ const effect = page.slice(page.indexOf("setAvailableControlNets([]);") - 120);+ assert.match(effect.slice(0, 160), /if \(!controlnetSupported\) \{/);+ assert.match(page, /\}, \[controlnetSupported, status\?\.family\]\);/);+});diff --git a/studio/frontend/tests/image-model-recall.test.ts b/studio/frontend/tests/image-model-recall.test.tsindex 44e6ecf9bb5..39fd6ef8438 100644--- a/studio/frontend/tests/image-model-recall.test.ts+++ b/studio/frontend/tests/image-model-recall.test.ts@@ -65,6 +65,7 @@ test("recalling a quantized model carries the selected adapters into its load",vaeFile: "",splitComponentFileList,busy: null,+ external: null,imagePresets: { hydrated: true },prompt: "a teapot",pendingRecalledGeneration: { current: null },