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: serve the Jev API locally with Laya (#11603)
Nilaya838acf95ffb
diff --git a/studio/backend/core/systemone/__init__.py b/studio/backend/core/systemone/__init__.pynew file mode 100644index 00000000000..32014236c6a--- /dev/null+++ b/studio/backend/core/systemone/__init__.py@@ -0,0 +1,2 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0diff --git a/studio/backend/core/systemone/catalog.py b/studio/backend/core/systemone/catalog.pynew file mode 100644index 00000000000..85cd1c96510--- /dev/null+++ b/studio/backend/core/systemone/catalog.py@@ -0,0 +1,77 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""System One decision models Studio can serve on ``POST /v1/systemone``."""++from __future__ import annotations++import os+from dataclasses import dataclass+from pathlib import Path++LAYA_REPO = "convaiinnovations/laya"+++@dataclass(frozen = True)+class Checkpoint:+ name: str+ source: str+ subfolder: str | None+ description: str+ download_bytes: int = 0++ @property+ def is_local(self) -> bool:+ return Path(self.source).expanduser().is_dir()+++CHECKPOINTS = {+ c.name: c+ for c in (+ Checkpoint(+ "laya-multilingual",+ LAYA_REPO,+ "multilingual",+ "Laya on mmBERT-base: 100+ languages, 1024-token context.",+ 678_201_636,+ ),+ Checkpoint(+ "laya-english",+ LAYA_REPO,+ None,+ "Laya on ModernBERT-large: English, 512-token context.",+ 846_195_574,+ ),+ Checkpoint(+ "laya-typed-decisions",+ LAYA_REPO,+ "typed-decisions",+ "Laya English fine-tuned on four typed-decision workflows, 1024-token context.",+ 846_195_716,+ ),+ )+}++# Names TypeSafe's and OpenJev's SDKs send by default, so an unmodified client reaches the configured model.+DEFAULT_ALIASES = frozenset({"default", "laya", "jev-latest", "jev-preview", "openjev-latest"})+LOCAL_NAME = "laya-local"+++def default_checkpoint() -> Checkpoint:+ from utils.systemone_settings import get_model++ configured = get_model()+ if configured in CHECKPOINTS:+ return CHECKPOINTS[configured]+ subfolder = os.environ.get("UNSLOTH_SYSTEMONE_SUBFOLDER", "").strip() or None+ return Checkpoint(LOCAL_NAME, configured, subfolder, "Local Laya checkpoint.")+++def resolve(model: str) -> Checkpoint | None:+ name = (model or "").strip()+ if name in DEFAULT_ALIASES:+ return default_checkpoint()+ if name == LOCAL_NAME:+ checkpoint = default_checkpoint()+ return checkpoint if checkpoint.name == LOCAL_NAME else None+ return CHECKPOINTS.get(name)diff --git a/studio/backend/core/systemone/laya_runtime.py b/studio/backend/core/systemone/laya_runtime.pynew file mode 100644index 00000000000..f0fa855c50c--- /dev/null+++ b/studio/backend/core/systemone/laya_runtime.py@@ -0,0 +1,673 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""In-process Laya runtime behind ``POST /v1/systemone``: one checkpoint resident at a time."""++from __future__ import annotations++import gc+import logging+import sys+import threading+import time+from pathlib import Path+from typing import Any++from .catalog import LOCAL_NAME, Checkpoint++logger = logging.getLogger(__name__)++LOAD_WAIT_S = 20.0+RUN_WAIT_S = 30.0+# Bounded: each waiter holds a shared threadpool worker, so an unbounded queue stalls every sync route.+MAX_PENDING = 8+FAILURE_BACKOFF_S = 60.0+_REQUIRED_DIRS = ("encoder", "tokenizer")+LAYA_REQUIREMENT = "laya==0.3.5"+INSTALL_TIMEOUT_S = 300++_state_lock = threading.Lock()+_run_lock = threading.Lock()+_admission = threading.BoundedSemaphore(MAX_PENDING)+_agent = None+_loaded: Checkpoint | None = None+_device_name: str | None = None+_loader: threading.Thread | None = None+_loading: Checkpoint | None = None+_failure: tuple[Checkpoint, str, float] | None = None+_install_lock = threading.Lock()+_installer: threading.Thread | None = None+_install_failure: tuple[str, float] | None = None+++class Unavailable(Exception):+ def __init__(+ self,+ status: int,+ error_type: str,+ message: str,+ retry_after: float | None = None,+ ):+ super().__init__(message)+ self.status = status+ self.error_type = error_type+ self.message = message+ self.retry_after = retry_after+++def _device() -> str:+ from utils.systemone_settings import get_device as preferred_device++ # CPU unless asked: a CUDA context opened in the backend process is never returned (see core.rag.embeddings._device).+ if preferred_device() != "gpu":+ return "cpu"+ from utils.hardware.hardware import DeviceType, get_device++ device = get_device()+ if device == DeviceType.MLX:+ if _mlx_available():+ return "mlx"+ import torch+ return "mps" if torch.backends.mps.is_available() else "cpu"+ candidate = {DeviceType.CUDA: "cuda", DeviceType.XPU: "xpu"}.get(device, "cpu")+ if candidate == "cpu":+ return candidate+ from utils.torch_device_probe import device_can_allocate++ return candidate if device_can_allocate(candidate) else "cpu"+++def _mlx_available() -> bool:+ try:+ import unsloth_zoo.mlx.decision # noqa: F401+ except ImportError:+ return False+ return True+++_WEIGHT_FILES = ("rl_agent_config.json", "model.safetensors")+++def _wanted(path: str, subfolder: str | None) -> bool:+ prefix = f"{subfolder}/" if subfolder else ""+ if not path.startswith(prefix):+ return False+ rest = path[len(prefix) :]+ return rest in _WEIGHT_FILES or rest.startswith(tuple(f"{name}/" for name in _REQUIRED_DIRS))+++def _checkpoint_dir(checkpoint: Checkpoint, *, local_only: bool = False) -> Path:+ if checkpoint.name == LOCAL_NAME and not checkpoint.is_local:+ raise FileNotFoundError(+ f"UNSLOTH_SYSTEMONE_MODEL={checkpoint.source} is neither a known model nor a directory"+ )+ if checkpoint.is_local:+ root = Path(checkpoint.source).expanduser()+ else:+ from huggingface_hub import snapshot_download++ from utils.hf_cache_settings import active_hf_hub_cache+ from utils.utils import hf_env_offline++ prefix = f"{checkpoint.subfolder}/" if checkpoint.subfolder else ""+ root = Path(+ snapshot_download(+ checkpoint.source,+ cache_dir = active_hf_hub_cache(),+ local_files_only = local_only or hf_env_offline(),+ allow_patterns = [prefix + name for name in _WEIGHT_FILES]+ + [f"{prefix}{name}/*" for name in _REQUIRED_DIRS],+ )+ )+ folder = root / checkpoint.subfolder if checkpoint.subfolder else root+ # Without these, laya falls back to downloading the base encoder from the Hub on every load.+ missing = [name for name in _REQUIRED_DIRS if not (folder / name).is_dir()]+ if missing:+ raise FileNotFoundError(+ f"Laya checkpoint at {folder} is missing {', '.join(n + '/' for n in missing)}"+ )+ return root+++def package_available() -> bool:+ import importlib.util+ return importlib.util.find_spec("laya") is not None+++def _install_command() -> list[str]:+ import sys++ from utils.mlx_repair import _uv_executable++ # No deps: laya's deps are all Studio pins, so the install can never move them.+ uv = _uv_executable()+ if uv:+ return [uv, "pip", "install", "--python", sys.executable, "--no-deps", LAYA_REQUIREMENT]+ return [sys.executable, "-m", "pip", "install", "--no-deps", LAYA_REQUIREMENT]+++def ensure_package() -> None:+ global _install_failure+ if package_available():+ _install_failure = None+ return+ with _install_lock:+ if package_available():+ return+ if _install_failure and time.monotonic() < _install_failure[1]:+ raise RuntimeError(_install_failure[0])+ import importlib+ import os+ import subprocess++ from utils.mlx_repair import _MLX_ENV_ALLOWLIST, _venv_root++ # Same allowlisted env as the MLX self-heal: secrets/package-source vars cannot steer the install.+ env = {key: os.environ[key] for key in _MLX_ENV_ALLOWLIST if key in os.environ}+ if (venv_root := _venv_root()) is not None:+ env["VIRTUAL_ENV"] = venv_root+ logger.info("Installing %s for the Decision API", LAYA_REQUIREMENT)+ try:+ result = subprocess.run(+ _install_command(),+ env = env,+ capture_output = True,+ text = True,+ encoding = "utf-8",+ errors = "replace",+ timeout = INSTALL_TIMEOUT_S,+ )+ detail = ((result.stderr or result.stdout).strip().splitlines() or [""])[-1]+ ok = result.returncode == 0+ except (OSError, subprocess.TimeoutExpired) as exc:+ detail, ok = type(exc).__name__, False+ importlib.invalidate_caches()+ if ok and package_available():+ _install_failure = None+ return+ message = (+ f"Could not install {LAYA_REQUIREMENT}. Check the internet connection. {detail}".strip()+ )+ logger.warning("Decision API install failed: %s", message)+ _install_failure = (message, time.monotonic() + FAILURE_BACKOFF_S)+ raise RuntimeError(message)+++def install_in_background() -> None:+ global _installer, _install_failure+ if package_available():+ _install_failure = None+ return+ with _state_lock:+ if _installer is not None and _installer.is_alive():+ return+ if _install_failure and time.monotonic() < _install_failure[1]:+ return+ _installer = threading.Thread(+ target = _install_quietly, name = "systemone-install", daemon = True+ )+ _installer.start()+++def _install_quietly() -> None:+ try:+ ensure_package()+ except RuntimeError:+ pass+++def installing() -> bool:+ return _install_lock.locked() or (_installer is not None and _installer.is_alive())+++def is_cached(checkpoint: Checkpoint) -> bool:+ try:+ root = _checkpoint_dir(checkpoint, local_only = True)+ except Exception:+ return False+ folder = root / checkpoint.subfolder if checkpoint.subfolder else root+ return all((folder / name).is_file() for name in _WEIGHT_FILES)+++def download_plan(checkpoint: Checkpoint) -> dict[str, Any]:+ cached = is_cached(checkpoint)+ plan = {"repo": None, "files": [], "size_bytes": 0, "cached": cached, "error": None}+ if checkpoint.name == LOCAL_NAME:+ if not cached:+ plan["error"] = f"No complete Laya checkpoint at {checkpoint.source}"+ return plan+ plan["repo"] = checkpoint.source+ plan["size_bytes"] = checkpoint.download_bytes+ if cached:+ return plan+ try:+ from huggingface_hub import HfApi+ entries = HfApi().list_repo_tree(checkpoint.source, recursive = True)+ files = [+ (entry.path, getattr(entry, "size", 0) or 0)+ for entry in entries+ if hasattr(entry, "size") and _wanted(entry.path, checkpoint.subfolder)+ ]+ except Exception as exc:+ plan["error"] = f"Could not list {checkpoint.source}: {type(exc).__name__}"+ return plan+ plan["files"] = sorted(path for path, _ in files)+ plan["size_bytes"] = sum(size for _, size in files) or checkpoint.download_bytes+ return plan+++def _release_memory() -> None:+ gc.collect()+ # MLX keeps freed buffers in its allocator cache until told otherwise; mlx below 0.24.1 names it mx.metal.clear_cache.+ if (mx := sys.modules.get("mlx.core")) is not None:+ clear = getattr(mx, "clear_cache", None) or getattr(+ getattr(mx, "metal", None), "clear_cache", None+ )+ if clear is not None:+ clear()+ # Torch's allocator too; only already-initialised backends, so a CPU-only Studio never opens a device context.+ if (torch := sys.modules.get("torch")) is not None:+ try:+ if torch.cuda.is_initialized():+ torch.cuda.empty_cache()+ if hasattr(torch, "xpu") and torch.xpu.is_initialized():+ torch.xpu.empty_cache()+ if torch.backends.mps.is_available():+ torch.mps.empty_cache()+ except Exception:+ logger.debug("Could not clear the Decision API device cache", exc_info = True)+++def _evict() -> None:+ global _agent, _loaded, _device_name+ with _run_lock:+ _agent = _loaded = _device_name = None+ _release_memory()+++class _MLXAgent:+ """The part of ``laya.Agent`` this module drives, over the unsloth-zoo MLX decision model."""++ device = "mlx"++ def __init__(self, folder: Path):+ import json++ from laya.agent import Agent+ from laya.common import clamp_temperature+ from transformers import AutoTokenizer+ from unsloth_zoo.mlx.decision import load_decision_model++ self.folder = folder+ self.cfg = json.loads((folder / "rl_agent_config.json").read_text(encoding = "utf-8"))+ self.tok = AutoTokenizer.from_pretrained(str(folder / "tokenizer"))+ self.temperature = [+ clamp_temperature(t) for t in self.cfg.get("temperature", [1.0, 1.0, 1.0])+ ]+ self.temperature_by_options = {+ k: clamp_temperature(v) for k, v in self.cfg.get("temperature_by_options", {}).items()+ }+ self._to_internal = Agent._to_internal+ self.model = load_decision_model(folder)+++def _load_checkpoint(checkpoint: Checkpoint):+ root = _checkpoint_dir(checkpoint)+ import laya++ # Evict only once the new checkpoint is on disk, so a long or failed download leaves the resident model serving.+ _evict()+ device = _device()+ if device == "mlx":+ folder = root / checkpoint.subfolder if checkpoint.subfolder else root+ return _MLXAgent(folder), device+ return laya.load(str(root), subfolder = checkpoint.subfolder, device = device), device+++def _hub_download_active(checkpoint: Checkpoint) -> bool:+ # A Hub download job writing the same repo would share blobs with our snapshot_download.+ if checkpoint.is_local:+ return False+ try:+ from hub.utils.download_registry import get_models_registry+ if not get_models_registry().active_job_refs(checkpoint.source):+ return False+ except Exception:+ return False+ return not is_cached(checkpoint)+++def loading_repo_ids() -> tuple[str, ...]:+ with _state_lock:+ if _loading is not None and not _loading.is_local:+ return (_loading.source,)+ return ()+++def _load(checkpoint: Checkpoint) -> None:+ global _agent, _loaded, _device_name, _loading, _failure+ started = time.monotonic()+ try:+ ensure_package()+ agent, device = _load_checkpoint(checkpoint)+ except Exception as exc:+ message = (+ str(exc)+ if isinstance(exc, RuntimeError) and _install_failure+ else f"Could not load {checkpoint.name}: {type(exc).__name__}: {exc}"+ )+ logger.warning("System One load failed: %s", message)+ with _state_lock:+ _failure = (checkpoint, message, time.monotonic() + FAILURE_BACKOFF_S)+ _loading = None+ return+ with _state_lock:+ _agent, _loaded, _device_name = agent, checkpoint, str(getattr(agent, "device", device))+ _failure = _loading = None+ logger.info(+ "System One loaded %s on %s in %.1fs",+ checkpoint.name,+ _device_name,+ time.monotonic() - started,+ )+++def _ensure_loading(checkpoint: Checkpoint) -> threading.Thread | None:+ global _loader, _loading+ with _state_lock:+ if _loaded == checkpoint and _agent is not None:+ return None+ if _failure and _failure[0] == checkpoint and time.monotonic() < _failure[2]:+ raise Unavailable(+ 503,+ "model_unavailable",+ _failure[1],+ retry_after = max(1.0, _failure[2] - time.monotonic()),+ )+ if _loading is not None:+ if _loading != checkpoint:+ raise Unavailable(+ 503, "model_loading", f"{_loading.name} is loading", retry_after = 5+ )+ if _loader is not None and _loader.is_alive():+ return _loader+ raise Unavailable(503, "model_loading", f"{checkpoint.name} is loading", retry_after = 5)+ # Claimed before the registry probe, so a Hub download admitted from now on sees this load.+ _loading = checkpoint+ # Outside _state_lock: the registry calls loading_repo_ids() while holding its own lock.+ if _hub_download_active(checkpoint):+ with _state_lock:+ _loading = None+ raise Unavailable(503, "model_loading", f"{checkpoint.name} is downloading", retry_after = 5)+ with _state_lock:+ _loader = threading.Thread(+ target = _load, args = (checkpoint,), name = "systemone-load", daemon = True+ )+ _loader.start()+ return _loader+++def _agent_for(checkpoint: Checkpoint, wait: float):+ loader = _ensure_loading(checkpoint)+ if loader is not None:+ loader.join(wait)+ if loader.is_alive() or _ensure_loading(checkpoint) is not None:+ raise Unavailable(+ 503, "model_loading", f"{checkpoint.name} is still loading", retry_after = 5+ )+ return _agent+++def _to_laya(question: dict[str, Any]) -> dict[str, Any]:+ out = {"type": question["type"], "instructions": question.get("instructions") or ""}+ if question.get("criteria") is not None:+ out["criteria"] = question["criteria"]+ return out+++# Long states tokenized in growing prefixes; margin keeps kept tokens clear of the cut (prefix tokenization differs).+_PREFIX_MARGIN = 64+_HEAD_CACHE_SIZE = 1024+++def _state_ids(tok, state, room: int) -> tuple[list[int], bool]:+ from laya.common import serialize_state++ text = serialize_state(state).replace(tok.mask_token, " ")+ chars = max(4096, room * 16)+ while chars < len(text):+ ids = tok(text[:chars], add_special_tokens = False)["input_ids"]+ if len(ids) > room + _PREFIX_MARGIN:+ return ids[:room], True+ chars *= 4+ ids = tok(text, add_special_tokens = False)["input_ids"]+ return ids[:room], len(ids) > room+++def _head(agent, question: dict[str, Any], max_len: int, head_max_len: int):+ import json++ from laya.common import build_sequence, render_options++ cache = agent.__dict__.setdefault("_unsloth_heads", {})+ key = json.dumps(question, ensure_ascii = False)+ if key not in cache:+ internal = agent._to_internal(question)+ ids, markers = build_sequence(agent.tok, "", internal, max_len, head_max_len)+ if len(markers) != len(render_options(internal)):+ raise ValueError("question options exceed head_max_len=%d" % head_max_len)+ if len(cache) >= _HEAD_CACHE_SIZE:+ cache.clear()+ cache[key] = (ids, markers, internal)+ return cache[key]+++def _predict(agent, state, questions: dict[str, dict[str, Any]]) -> tuple[dict[str, Any], bool]:+ import numpy as np+ from laya.common import QTYPES, confidence_from_probs++ max_len = int(agent.cfg.get("max_len", 512))+ head_max_len = int(agent.cfg.get("head_max_len", 192))+ names = list(questions)+ heads = []+ for name in names:+ try:+ heads.append(_head(agent, questions[name], max_len, head_max_len))+ except ValueError:+ raise ValueError(+ "question %r options exceed head_max_len=%d" % (name, head_max_len)+ ) from None+ room = max(0, max_len - min(len(ids) for ids, _, _ in heads))+ state_ids, state_cut = _state_ids(agent.tok, state, room)+ items, truncated = [], False+ for ids, markers, internal in heads:+ keep = max(0, max_len - len(ids))+ truncated = truncated or state_cut or len(state_ids) > keep+ items.append(+ {+ "ids": (ids[:-1] + state_ids[:keep] + ids[-1:])[:max_len],+ "markers": markers,+ "qtype": QTYPES[internal["t"]],+ }+ )+ logits, usage = _forward(agent, items)+ answers = {}+ for row, (name, (_, markers, internal)) in enumerate(zip(names, heads)):+ k = len(markers)+ p = _probabilities(agent, logits[row], k, QTYPES[internal["t"]])+ confidence = round(confidence_from_probs(p, k), 4)+ if internal["t"] == "choice":+ keys = list(internal["crit"])+ answers[name] = {+ "type": "choice",+ "choice": keys[int(p.argmax())],+ "probabilities": {key: round(float(v), 4) for key, v in zip(keys, p)},+ "confidence": confidence,+ }+ elif internal["t"] == "score":+ answers[name] = {+ "type": "score",+ "score": round(float((np.arange(k) * p).sum()), 4),+ "legend": {str(i): c for i, c in enumerate(internal["crit"])},+ "probabilities": {str(i): round(float(v), 4) for i, v in enumerate(p)},+ "confidence": confidence,+ }+ else:+ answers[name] = {+ "type": "noul",+ "noul": round(float(p[1]), 4),+ "confidence": round(max(float(p[1]), 1.0 - float(p[1])), 4),+ }+ return {"answers": answers, "usage": {"input_tokens": usage, "output_tokens": 0}}, truncated+++def _forward(agent, items: list[dict[str, Any]]):+ global _agent, _loaded, _device_name+ import torch+ from laya.common import collate_items++ batch = collate_items([items], agent.tok.pad_token_id)+ if agent.device == "mlx":+ try:+ return agent.model.logits(batch), int(batch["attention_mask"].sum())+ except RuntimeError as exc:+ # MLX reports exhausted memory as "[malloc] Unable to allocate ..." or "Insufficient Memory".+ reason = str(exc).lower()+ if "memory" not in reason and "allocate" not in reason:+ raise+ # Past the handler, so the traceback no longer keeps the MLX arrays alive while the CPU copy loads.+ import laya++ logger.warning("Laya ran out of GPU memory; moving it to CPU")+ agent.model = None+ _release_memory()+ try:+ cpu = laya.load(str(agent.folder), device = "cpu")+ except Exception:+ # Callers hold _run_lock, so drop the half-moved agent here; the next request loads it again.+ _agent = _loaded = _device_name = None+ raise+ agent.model, agent.device, agent.dtype = cpu.model, cpu.device, cpu.dtype+ _device_name = "cpu"+ try:+ logits = _run_model(agent, batch)+ except (RuntimeError, torch.cuda.OutOfMemoryError) as exc:+ # Same fallback as laya's Agent.predict: a GPU that runs out of memory moves the model to CPU.+ reason = str(exc).lower()+ if agent.device.type == "cpu" or ("memory" not in reason and "cuda" not in reason):+ raise+ logger.warning("Laya ran out of GPU memory; moving it to CPU")+ agent.device, agent.dtype = torch.device("cpu"), torch.float32+ agent.model.to(agent.device)+ _device_name = "cpu"+ _release_memory()+ logits = _run_model(agent, batch)+ return logits.float().cpu().numpy(), int(batch["attention_mask"].sum())+++def _run_model(agent, batch):+ import torch++ device = agent.device+ with (+ torch.inference_mode(),+ torch.autocast(device_type = device.type, dtype = agent.dtype, enabled = device.type == "cuda"),+ ):+ logits, _ = agent.model(+ batch["input_ids"].to(device),+ batch["attention_mask"].to(device),+ batch["marker_pos"].to(device),+ batch["marker_mask"].to(device),+ batch["qtype"].to(device),+ )+ return logits+++def _probabilities(agent, row, k: int, qtype: int):+ import numpy as np+ from laya.common import temp_bucket++ scale = agent.temperature_by_options.get(temp_bucket(qtype, k), agent.temperature[qtype])+ z = row[:k] / scale+ p = np.exp(z - z.max())+ return p / p.sum()+++def _wire_answer(answer: dict[str, Any]) -> dict[str, Any]:+ kind = answer["type"]+ if kind == "noul":+ return {"type": "noul", "noul": float(answer["noul"])}+ if kind == "choice":+ return {+ "type": "choice",+ "choice": answer["choice"],+ "confidence": float(answer["confidence"]),+ "probabilities": {k: float(v) for k, v in answer["probabilities"].items()},+ }+ return {+ "type": "score",+ "score": float(answer["score"]),+ "confidence": float(answer["confidence"]),+ "legend": answer["legend"],+ "probabilities": {k: float(v) for k, v in answer["probabilities"].items()},+ }+++def decide(checkpoint: Checkpoint, state, questions: dict[str, dict[str, Any]]) -> dict[str, Any]:+ if not _admission.acquire(blocking = False):+ raise Unavailable(529, "overloaded", "System One is busy; retry shortly", retry_after = 1)+ try:+ return _decide(checkpoint, state, questions)+ finally:+ _admission.release()+++def _decide(checkpoint: Checkpoint, state, questions: dict[str, dict[str, Any]]) -> dict[str, Any]:+ agent = _agent_for(checkpoint, LOAD_WAIT_S)+ if not _run_lock.acquire(timeout = RUN_WAIT_S):+ raise Unavailable(529, "overloaded", "System One is busy; retry shortly", retry_after = 1)+ try:+ if agent is None or _agent is not agent or _loaded != checkpoint:+ raise Unavailable(+ 503, "model_loading", f"{checkpoint.name} is reloading", retry_after = 5+ )+ laya_questions = {name: _to_laya(q) for name, q in questions.items()}+ try:+ result, truncated = _predict(agent, state, laya_questions)+ except ValueError as exc:+ raise Unavailable(400, "invalid_request_error", str(exc)) from None+ finally:+ _run_lock.release()+ return {+ "model": checkpoint.name,+ "answers": {name: _wire_answer(result["answers"][name]) for name in questions},+ "usage": {"input_tokens": int(result["usage"]["input_tokens"]), "output_tokens": 0},+ "truncated": truncated,+ }+++def status() -> dict[str, Any]:+ with _state_lock:+ failure = _failure if _failure and time.monotonic() < _failure[2] else None+ return {+ "loaded_model": _loaded.name if _loaded else None,+ "device": _device_name,+ "loading_model": _loading.name if _loading else None,+ "installing": installing(),+ "error": failure[1] if failure else (_install_failure[0] if _install_failure else None),+ "error_model": failure[0].name if failure else None,+ }+++def unload() -> bool:+ global _agent, _loaded, _device_name, _failure, _install_failure+ with _state_lock:+ # _loading: a load claimed but not yet started would otherwise land after this unload.+ if _loading is not None or (_loader is not None and _loader.is_alive()):+ raise Unavailable(409, "model_loading", "Wait for the load to finish before unloading")+ with _run_lock:+ was_loaded = _agent is not None+ _agent = _loaded = _device_name = None+ _failure = _install_failure = None+ _release_memory()+ return was_loadeddiff --git a/studio/backend/hub/services/models/downloads.py b/studio/backend/hub/services/models/downloads.pyindex 997e0a113c6..f86cbdddd45 100644--- a/studio/backend/hub/services/models/downloads.py+++ b/studio/backend/hub/services/models/downloads.py@@ -119,6 +119,13 @@ def _load_in_flight(repo_id: str) -> bool:return Trueexcept Exception:pass+ try:+ from core.systemone.laya_runtime import loading_repo_ids+ key = download_registry.normalize_repo_key(repo_id)+ if any(download_registry.normalize_repo_key(r) == key for r in loading_repo_ids()):+ return True+ except Exception:+ passreturn _diffusion_load_in_flight(repo_id)diff --git a/studio/backend/main.py b/studio/backend/main.pyindex 36f1184ead8..91083fe32c0 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -366,6 +366,7 @@ from hub.utils.download_registry import (terminate_active_downloads as terminate_hub_downloads,)from routes.settings import router as settings_router+from routes.systemone import router as systemone_routerfrom routes.prompts import router as prompts_routerfrom routes.profile_stats import router as profile_stats_routerfrom auth import policy as auth_policy, storage@@ -1614,6 +1615,7 @@ app.include_router(video_openai_router, prefix = "/api/inference", tags = ["infeapp.include_router(video_openai_router, prefix = "/v1", tags = ["openai-compat"])app.include_router(inference_router, prefix = "/v1", tags = ["openai-compat"])+app.include_router(systemone_router, prefix = "/v1", tags = ["systemone"])# llama-server / Ollama discovery probes. Declares its own full paths (/props, /version, /api/tags, ...) so it# needs no prefix, and must be registered ahead of the SPA catch-all in serve_frontend() or /props and /version# go on resolving to index.html with a 200.diff --git a/studio/backend/requirements/extras-no-deps.txt b/studio/backend/requirements/extras-no-deps.txtindex 1b61721f43e..ad2c62fc866 100644--- a/studio/backend/requirements/extras-no-deps.txt+++ b/studio/backend/requirements/extras-no-deps.txt@@ -10,6 +10,10 @@ snac==1.2.1# installing with --no-deps to avoid pulling in torch>=0.11.0peft==0.18.1+# Laya System One runtime for /v1/systemone. Pure Python over torch, transformers,+# safetensors, huggingface_hub and numpy, all already pinned elsewhere.+laya==0.3.5; python_version >= "3.10"+# TRL and related packagestrl==0.23.1# executorch>=1.0.1 # 41.5 MB - no imports in unsloth/zoo/studiodiff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.pyindex d2d551289e8..bd1a56e670c 100644--- a/studio/backend/routes/settings.py+++ b/studio/backend/routes/settings.py@@ -46,7 +46,7 @@ from core.rag.config import (effective_gguf_repo_for_embedding_model,)from loggers import get_logger-from utils.utils import safe_error_detail, log_and_http_error+from utils.utils import safe_curated_detail, safe_error_detail, log_and_http_errorfrom utils.personalization_settings import (MAX_AVATAR_DATA_URL_BYTES,PERSONALIZATION_VERSION,@@ -75,6 +75,7 @@ from utils.helper_precache_settings import (helper_model_disabled_by_env,set_helper_precache_enabled,)+from utils import systemone_settingsfrom utils.download_transport_settings import (get_download_transport_mode,set_download_transport_mode,@@ -616,6 +617,42 @@ def clear_hugging_face_token(return HuggingFaceTokenResponse(token = None, has_token = False)+class SystemOneModelOption(BaseModel):+ name: str+ description: str+ download_bytes: int+++class SystemOneSettingsResponse(BaseModel):+ enabled: bool+ enabled_locked: bool+ model: str+ model_locked: bool+ device: str+ device_locked: bool+ gpu_available: bool+ models: list[SystemOneModelOption]+ loaded_model: Optional[str] = None+ loaded_device: Optional[str] = None+ loading_model: Optional[str] = None+ installing: bool = False+ error: Optional[str] = None+++class SystemOneSettingsPayload(BaseModel):+ enabled: Optional[bool] = None+ model: Optional[str] = None+ device: Optional[str] = None+++class SystemOneDownloadPlan(BaseModel):+ repo: Optional[str] = None+ files: list[str]+ size_bytes: int+ cached: bool+ error: Optional[str] = None++class HelperPrecachePayload(BaseModel):enabled: bool@@ -1294,6 +1331,92 @@ def update_helper_precache(return _helper_precache_response(enabled)+def _systemone_response() -> SystemOneSettingsResponse:+ from core.systemone import catalog, laya_runtime++ enabled = systemone_settings.get_enabled()+ if enabled:+ laya_runtime.install_in_background()+ runtime = laya_runtime.status()+ model = catalog.default_checkpoint().name+ error = runtime["error"]+ if runtime["error_model"] not in (None, model):+ error = None+ return SystemOneSettingsResponse(+ enabled = enabled,+ enabled_locked = systemone_settings.enabled_locked(),+ model = model,+ model_locked = systemone_settings.model_locked(),+ device = systemone_settings.get_device(),+ device_locked = systemone_settings.device_locked(),+ gpu_available = systemone_settings.gpu_available(),+ models = [+ SystemOneModelOption(+ name = c.name, description = c.description, download_bytes = c.download_bytes+ )+ for c in catalog.CHECKPOINTS.values()+ ],+ loaded_model = runtime["loaded_model"],+ loaded_device = runtime["device"],+ loading_model = runtime["loading_model"],+ installing = runtime["installing"],+ error = error,+ )+++@_shared_settings_router.get("/systemone", response_model = SystemOneSettingsResponse)+def get_systemone_settings(+ current_subject: str = Depends(get_current_subject),+) -> SystemOneSettingsResponse:+ return _systemone_response()+++@_owner_settings_router.put("/systemone", response_model = SystemOneSettingsResponse)+def update_systemone_settings(+ payload: SystemOneSettingsPayload, current_subject: str = Depends(get_current_subject)+) -> SystemOneSettingsResponse:+ from core.systemone import laya_runtime++ try:+ values = systemone_settings.validate(**payload.model_dump(exclude_none = True))+ except ValueError as exc:+ raise log_and_http_error(+ exc,+ 400,+ safe_curated_detail(exc, fallback = "Invalid Decision API setting."),+ event = "settings.update_systemone_failed",+ log = logger,+ ) from exc+ if values:+ # The resident model was built from the old settings; drop it so the next request uses the new ones.+ try:+ laya_runtime.unload()+ except laya_runtime.Unavailable as exc:+ raise HTTPException(status_code = 409, detail = exc.message) from None+ systemone_settings.save(values)+ return _systemone_response()+++@_owner_settings_router.get("/systemone/resolve", response_model = SystemOneDownloadPlan)+def resolve_systemone_download(+ current_subject: str = Depends(get_current_subject),+) -> SystemOneDownloadPlan:+ from core.systemone import catalog, laya_runtime+ return SystemOneDownloadPlan(**laya_runtime.download_plan(catalog.default_checkpoint()))+++@_owner_settings_router.post("/systemone/unload", response_model = SystemOneSettingsResponse)+def unload_systemone_model(+ current_subject: str = Depends(get_current_subject),+) -> SystemOneSettingsResponse:+ from core.systemone import laya_runtime+ try:+ laya_runtime.unload()+ except laya_runtime.Unavailable as exc:+ raise HTTPException(status_code = 409, detail = exc.message) from None+ return _systemone_response()++@_shared_settings_router.get("/download-transport", response_model = DownloadTransportResponse)def get_download_transport(current_subject: str = Depends(get_current_subject),diff --git a/studio/backend/routes/systemone.py b/studio/backend/routes/systemone.pynew file mode 100644index 00000000000..4dbbc7698c6--- /dev/null+++ b/studio/backend/routes/systemone.py@@ -0,0 +1,153 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Jev-compatible System One API: typed decisions (noul / choice / score) read off a Laya checkpoint."""++from __future__ import annotations++import json+import math+from typing import Any, Optional, Union++from fastapi import APIRouter, Depends, HTTPException, Request+from fastapi.responses import JSONResponse+from pydantic import BaseModel, ConfigDict, Field++from auth.authentication import get_current_subject+from core.systemone import catalog, laya_runtime+from utils import systemone_settings++MAX_QUESTIONS = 64+MAX_CHOICES = 255+MAX_SCORE_LEVELS = 10+MAX_STATE_CHARS = 200_000+MAX_QUESTION_CHARS = 20_000+_TYPES = ("noul", "choice", "score")++router = APIRouter()++# typing.Union, not `|`: this alias is evaluated at import, and Studio still starts on Python 3.9.+JSONContent = Union[str, dict[str, Any], list[Any]]+++class QuestionIn(BaseModel):+ model_config = ConfigDict(extra = "forbid")++ type: str+ instructions: Optional[JSONContent] = None+ criteria: Optional[JSONContent] = None+++class SystemOneRequest(BaseModel):+ # Unknown fields refused, not dropped: an ignored OpenJev extension (`images`) answers a different question.+ model_config = ConfigDict(extra = "allow")++ state: JSONContent+ model: str+ questions: dict[str, QuestionIn] = Field(min_length = 1)+++def _error(+ status: int,+ error_type: str,+ message: str,+ retry_after: float | None = None,+) -> HTTPException:+ headers = {"Retry-After": str(max(1, math.ceil(retry_after)))} if retry_after else None+ return HTTPException(+ status_code = status,+ detail = {"error_type": error_type, "message": message},+ headers = headers,+ )+++def _validate(name: str, question: QuestionIn) -> None:+ if len(name) + len(json.dumps(question.model_dump(), ensure_ascii = False)) > MAX_QUESTION_CHARS:+ raise _error(+ 400,+ "invalid_request_error",+ f"Question is longer than {MAX_QUESTION_CHARS} characters",+ )+ if question.type not in _TYPES:+ raise _error(+ 400, "api_usage_error", f'Question "{name}" has unknown type "{question.type}"'+ )+ criteria = question.criteria+ if question.type == "noul":+ if criteria is not None and (+ not isinstance(criteria, dict) or set(criteria) - {"true", "false"}+ ):+ raise _error(+ 400,+ "invalid_request_error",+ f'Noul "{name}" criteria may only have "true" and "false"',+ )+ elif question.type == "choice":+ if not isinstance(criteria, dict) or not criteria:+ raise _error(+ 400, "invalid_request_error", f'Choice "{name}" needs criteria naming its options'+ )+ if len(criteria) > MAX_CHOICES:+ raise _error(+ 400, "invalid_request_error", f'Choice "{name}" has more than {MAX_CHOICES} options'+ )+ elif not isinstance(criteria, list) or not 1 <= len(criteria) <= MAX_SCORE_LEVELS:+ raise _error(+ 400,+ "invalid_request_error",+ f'Score "{name}" needs 1 to {MAX_SCORE_LEVELS} criteria levels',+ )++[email protected]("/systemone")+def system_one(+ payload: SystemOneRequest,+ request: Request,+ current_subject: str = Depends(get_current_subject),+):+ if not systemone_settings.get_enabled():+ raise _error(+ 404,+ "api_usage_error",+ "The Decision API is off. The Studio owner can turn it on in Settings > API.",+ )+ if payload.model_extra:+ raise _error(+ 400,+ "api_usage_error",+ f"Unsupported field(s): {', '.join(sorted(payload.model_extra))}",+ )+ checkpoint = catalog.resolve(payload.model)+ if checkpoint is None:+ raise _error(400, "api_usage_error", f"Unknown model: {payload.model}")+ from auth.authentication import request_admitted_without_credential++ # Same rule as the OpenAI routes: a keyless caller never downloads or swaps in another model.+ if checkpoint != catalog.default_checkpoint() and request_admitted_without_credential(request):+ raise _error(+ 403,+ "permission_error",+ "Keyless requests can only use the configured Decision API model; send an API key to pick another.",+ )+ state_chars = (+ len(payload.state)+ if isinstance(payload.state, str)+ else len(json.dumps(payload.state, ensure_ascii = False))+ )+ if state_chars > MAX_STATE_CHARS:+ raise _error(+ 400, "invalid_request_error", f"State is longer than {MAX_STATE_CHARS} characters"+ )+ if len(payload.questions) > MAX_QUESTIONS:+ raise _error(400, "invalid_request_error", f"At most {MAX_QUESTIONS} questions per request")+ for name, question in payload.questions.items():+ _validate(name, question)++ questions = {name: q.model_dump() for name, q in payload.questions.items()}+ try:+ result = laya_runtime.decide(checkpoint, payload.state, questions)+ except laya_runtime.Unavailable as exc:+ raise _error(exc.status, exc.error_type, exc.message, exc.retry_after) from None+ if result.pop("truncated"):+ raise _error(422, "invalid_request_error", "State exceeds the Laya context window")+ return JSONResponse(result)diff --git a/studio/backend/tests/test_desktop_auth.py b/studio/backend/tests/test_desktop_auth.pyindex 2c0a03604ab..89346f37a2b 100644--- a/studio/backend/tests/test_desktop_auth.py+++ b/studio/backend/tests/test_desktop_auth.py@@ -825,6 +825,8 @@ def test_health_response_reports_desktop_capability_fields(monkeypatch):profile_stats_module.router = APIRouter()accounts_module = ModuleType("routes.accounts")accounts_module.router = APIRouter()+ systemone_module = ModuleType("routes.systemone")+ systemone_module.router = APIRouter()# Derived from main.py's import block, not hand-listed: the old hardcoded dict went stale# twice (#8511's openai_codex_auth_router, #8648's youtube_router), each time killing every@@ -849,6 +851,7 @@ def test_health_response_reports_desktop_capability_fields(monkeypatch):monkeypatch.setitem(sys.modules, "routes.npu", npu_module)monkeypatch.setitem(sys.modules, "routes.profile_stats", profile_stats_module)monkeypatch.setitem(sys.modules, "routes.accounts", accounts_module)+ monkeypatch.setitem(sys.modules, "routes.systemone", systemone_module)import studio.backend.main as backend_maindiff --git a/studio/backend/tests/test_systemone.py b/studio/backend/tests/test_systemone.pynew file mode 100644index 00000000000..16e222479ba--- /dev/null+++ b/studio/backend/tests/test_systemone.py@@ -0,0 +1,1111 @@+# 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 os+import sys+import threading+import weakref+from types import SimpleNamespace++import pytest+from fastapi import FastAPI+from fastapi.testclient import TestClient++from auth.authentication import get_current_subject+from core.systemone import catalog, laya_runtime+from routes import systemone+from utils import systemone_settings++_REAL_LOAD = laya_runtime._load_checkpoint+_REAL_PREDICT = laya_runtime._predict+_REAL_OWNER_SETTING = systemone_settings._owner_setting+_REAL_UNAVAILABLE_REASON = systemone_settings.runtime_unavailable_reason+QUESTIONS = {+ "urgent": {"type": "noul", "instructions": "Does the customer need a reply within the hour?"},+ "team": {+ "type": "choice",+ "instructions": "Which team should handle it?",+ "criteria": {"outage": "service down", "billing": "charges, refunds"},+ },+ "tone": {+ "type": "score",+ "instructions": "How upset is the customer?",+ "criteria": ["calm", "annoyed", "furious"],+ },+}+++def _laya_answers(questions):+ answers = {}+ for name, q in questions.items():+ if q["type"] == "noul":+ answers[name] = {+ "type": "noul",+ "noul": 0.9,+ "confidence": 0.9,+ "action": {"act_probability": 0.1},+ }+ elif q["type"] == "choice":+ keys = list(q["criteria"])+ answers[name] = {+ "type": "choice",+ "choice": keys[0],+ "probabilities": {k: 1.0 if i == 0 else 0.0 for i, k in enumerate(keys)},+ "confidence": 1.0,+ "action": {"act_probability": 0.0},+ }+ else:+ answers[name] = {+ "type": "score",+ "score": 1.5,+ "legend": {str(i): c for i, c in enumerate(q["criteria"])},+ "probabilities": {+ str(i): 1 / len(q["criteria"]) for i in range(len(q["criteria"]))+ },+ "confidence": 0.2,+ "action": {"act_probability": 0.3},+ }+ return {+ "model": "laya-rl-agent",+ "answers": answers,+ "usage": {"input_tokens": 42, "output_tokens": 0},+ }+++class FakeAgent:+ def __init__(self, name = "agent"):+ self.name = name+ self.calls = []++ def predict(self, state, questions):+ self.calls.append((state, questions))+ return _laya_answers(questions)++[email protected](autouse = True)+def runtime(monkeypatch):+ for name, value in (+ ("_agent", None),+ ("_loaded", None),+ ("_device_name", None),+ ("_loader", None),+ ("_loading", None),+ ("_failure", None),+ ("_installer", None),+ ("_install_failure", None),+ ):+ monkeypatch.setattr(laya_runtime, name, value)+ for name in (+ "UNSLOTH_SYSTEMONE_MODEL",+ "UNSLOTH_SYSTEMONE_DISABLE",+ "UNSLOTH_SYSTEMONE_DEVICE",+ ):+ monkeypatch.delenv(name, raising = False)+ import storage.studio_db as studio_db++ settings = {systemone_settings.ENABLED_KEY: True}+ monkeypatch.setattr(systemone_settings, "_owner_setting", settings.get)+ monkeypatch.setattr(studio_db, "upsert_app_settings", settings.update)+ loads = []++ def load(checkpoint):+ loads.append(checkpoint.name)+ return FakeAgent(checkpoint.name), "cpu"++ monkeypatch.setattr(laya_runtime, "_load_checkpoint", load)+ monkeypatch.setattr(+ laya_runtime, "_predict", lambda agent, state, qs: (agent.predict(state, qs), False)+ )+ monkeypatch.setattr(systemone_settings, "runtime_unavailable_reason", lambda: None)+ monkeypatch.setattr(laya_runtime, "package_available", lambda: True)+ yield loads+ if laya_runtime._loader is not None:+ laya_runtime._loader.join(5)+++def client():+ app = FastAPI()+ from routes.settings import router as settings_router++ app.include_router(systemone.router, prefix = "/v1")+ app.include_router(settings_router, prefix = "/api/settings")+ app.dependency_overrides[get_current_subject] = lambda: "tester"+ return TestClient(app)+++def _post(client, **body):+ body = {+ "state": "Everything is down and we have a demo at noon.",+ "model": "jev-latest",+ "questions": QUESTIONS,+ **body,+ }+ return client.post("/v1/systemone", json = body)+++def test_answers_in_jev_wire_format(client, runtime):+ response = _post(client)+ assert response.status_code == 200, response.text+ body = response.json()+ assert body["model"] == "laya-multilingual"+ assert body["usage"] == {"input_tokens": 42, "output_tokens": 0}+ assert body["answers"]["urgent"] == {"type": "noul", "noul": 0.9}+ assert body["answers"]["team"] == {+ "type": "choice",+ "choice": "outage",+ "confidence": 1.0,+ "probabilities": {"outage": 1.0, "billing": 0.0},+ }+ tone = body["answers"]["tone"]+ assert set(tone) == {"type", "score", "confidence", "legend", "probabilities"}+ assert tone["legend"] == {"0": "calm", "1": "annoyed", "2": "furious"}+ assert "X-Unsloth-State-Truncated" not in response.headers+ assert runtime == ["laya-multilingual"]+++def test_model_loaded_once_and_questions_forwarded(client, runtime):+ _post(client)+ _post(client, state = {"subject": "Charged twice"}, questions = {"q": {"type": "noul"}})+ assert runtime == ["laya-multilingual"]+ state, questions = laya_runtime._agent.calls[-1]+ assert state == {"subject": "Charged twice"}+ assert questions == {"q": {"type": "noul", "instructions": ""}}+++def test_named_checkpoint_swaps_the_resident_model(client, runtime):+ _post(client)+ response = _post(client, model = "laya-english")+ assert response.json()["model"] == "laya-english"+ assert runtime == ["laya-multilingual", "laya-english"]+ assert laya_runtime.status()["loaded_model"] == "laya-english"+++ "body, status, error_type",+ [+ ({"model": "gpt-4"}, 400, "api_usage_error"),+ ({"questions": {"q": {"type": "maybe"}}}, 400, "api_usage_error"),+ ({"questions": {"q": {"type": "choice"}}}, 400, "invalid_request_error"),+ ({"questions": {"q": {"type": "choice", "criteria": {}}}}, 400, "invalid_request_error"),+ (+ {"questions": {"q": {"type": "score", "criteria": [str(i) for i in range(11)]}}},+ 400,+ "invalid_request_error",+ ),+ (+ {"questions": {"q": {"type": "noul", "criteria": {"yes": "x"}}}},+ 400,+ "invalid_request_error",+ ),+ ({"images": ["data:image/png;base64,AAAA"]}, 400, "api_usage_error"),+ (+ {"questions": {f"q{i}": {"type": "noul"} for i in range(65)}},+ 400,+ "invalid_request_error",+ ),+ ],+)+def test_requests_the_model_cannot_answer_are_refused(client, runtime, body, status, error_type):+ response = _post(client, **body)+ assert response.status_code == status, response.text+ assert response.json()["detail"]["error_type"] == error_type+ assert runtime == []+++ "body", [{"state": 3}, {"questions": {}}, {"questions": {"q": {"type": "noul", "extra": 1}}}]+)+def test_malformed_requests_are_validation_errors(client, body):+ assert _post(client, **body).status_code == 422+++def test_laya_refusal_becomes_a_bad_request(client, monkeypatch):+ def refuse(state, questions):+ raise ValueError("question 'team' options exceed head_max_len=256")++ monkeypatch.setattr(+ laya_runtime, "_load_checkpoint", lambda c: (SimpleNamespace(predict = refuse), "cpu")+ )+ response = _post(client)+ assert response.status_code == 400+ assert "head_max_len" in response.json()["detail"]["message"]+++def test_truncated_state_is_rejected(client, monkeypatch):+ monkeypatch.setattr(+ laya_runtime, "_predict", lambda agent, state, qs: (agent.predict(state, qs), True)+ )+ response = _post(client)+ assert response.status_code == 422+ assert "context window" in response.json()["detail"]["message"]+++def test_failed_load_backs_off_and_reports_why(client, monkeypatch):+ attempts = []++ def fail(checkpoint):+ attempts.append(checkpoint.name)+ raise FileNotFoundError("Laya checkpoint at /x is missing encoder/")++ monkeypatch.setattr(laya_runtime, "_load_checkpoint", fail)+ first = _post(client)+ assert first.status_code == 503+ assert "missing encoder" in first.json()["detail"]["message"]+ assert int(first.headers["Retry-After"]) >= 1+ assert _post(client).status_code == 503+ assert attempts == ["laya-multilingual"]+++class FakePip:+ def __init__(+ self,+ monkeypatch,+ returncode = 0,+ stderr = "",+ release = None,+ ):+ import subprocess++ self.calls = []+ self.installed = False+ self.returncode, self.stderr, self.release = returncode, stderr, release+ monkeypatch.setattr(laya_runtime, "package_available", lambda: self.installed)+ monkeypatch.setattr(subprocess, "run", self.run)++ def run(self, cmd, **kwargs):+ self.calls.append((cmd, kwargs))+ if self.release is not None:+ self.release.wait(5)+ self.installed = self.returncode == 0+ return SimpleNamespace(returncode = self.returncode, stdout = "", stderr = self.stderr)+++def test_missing_package_is_installed_automatically(client, monkeypatch, runtime):+ monkeypatch.setenv("UNSLOTH_TEST_SECRET", "do-not-forward")+ pip = FakePip(monkeypatch)+ response = _post(client)+ assert response.status_code == 200, response.text+ [(cmd, kwargs)] = pip.calls+ assert cmd[-2:] == ["--no-deps", laya_runtime.LAYA_REQUIREMENT]+ assert "UNSLOTH_TEST_SECRET" not in kwargs["env"]+ assert runtime == ["laya-multilingual"]+++def test_failed_install_says_why_and_backs_off(client, monkeypatch, runtime):+ pip = FakePip(monkeypatch, returncode = 1, stderr = "error: Failed to fetch laya\n")+ first = _post(client)+ assert first.status_code == 503+ message = first.json()["detail"]["message"]+ assert "Could not install laya==0.3.5" in message and "Failed to fetch laya" in message+ assert _post(client).status_code == 503+ assert len(pip.calls) == 1+ assert runtime == []+ assert client.get("/api/settings/systemone").json()["error"] == message+++def test_a_package_installed_by_hand_clears_the_old_install_error(client, monkeypatch):+ monkeypatch.setattr(laya_runtime, "FAILURE_BACKOFF_S", 0.0)+ pip = FakePip(monkeypatch, returncode = 1, stderr = "offline")+ assert _post(client).status_code == 503+ laya_runtime._loader.join(5)+ pip.installed = True+ assert _post(client).status_code == 200+ body = client.get("/api/settings/systemone").json()+ assert body["error"] is None and body["loaded_model"] == "laya-multilingual"+++def test_turning_it_back_on_retries_a_failed_install(client, monkeypatch):+ pip = FakePip(monkeypatch, returncode = 1, stderr = "offline")+ assert _post(client).status_code == 503+ client.put("/api/settings/systemone", json = {"enabled": False})+ pip.returncode = 0+ client.put("/api/settings/systemone", json = {"enabled": True})+ laya_runtime._installer.join(5)+ assert pip.installed is True+ assert _post(client).status_code == 200+++def test_settings_install_in_the_background_and_say_so(client, monkeypatch):+ release = threading.Event()+ pip = FakePip(monkeypatch, release = release)+ assert client.get("/api/settings/systemone").json()["installing"] is True+ release.set()+ laya_runtime._installer.join(5)+ body = client.get("/api/settings/systemone").json()+ assert body["installing"] is False and body["error"] is None+ assert len(pip.calls) == 1+++def test_nothing_installs_while_it_is_off(client, monkeypatch):+ monkeypatch.setattr(systemone_settings, "_owner_setting", {}.get)+ pip = FakePip(monkeypatch)+ assert client.get("/api/settings/systemone").json()["installing"] is False+ assert pip.calls == []+++def test_slow_load_answers_retry_after_instead_of_hanging(client, monkeypatch):+ release = threading.Event()++ def slow(checkpoint):+ release.wait(5)+ return FakeAgent(), "cpu"++ monkeypatch.setattr(laya_runtime, "_load_checkpoint", slow)+ monkeypatch.setattr(laya_runtime, "LOAD_WAIT_S", 0.05)+ response = _post(client)+ assert response.status_code == 503+ assert response.json()["detail"]["error_type"] == "model_loading"+ assert response.headers["Retry-After"] == "5"+ release.set()+ laya_runtime._loader.join(5)+ assert _post(client).status_code == 200+++def test_busy_model_answers_overloaded(client, monkeypatch):+ _post(client)+ monkeypatch.setattr(laya_runtime, "RUN_WAIT_S", 0.05)+ with laya_runtime._run_lock:+ response = _post(client)+ assert response.status_code == 529+ assert response.json()["detail"]["error_type"] == "overloaded"+++def test_off_by_default_and_says_where_to_turn_it_on(client, monkeypatch, runtime):+ monkeypatch.setattr(systemone_settings, "_owner_setting", {}.get)+ response = _post(client)+ assert response.status_code == 404+ assert "Settings > API" in response.json()["detail"]["message"]+ assert runtime == []+++def test_env_kill_switch_beats_the_setting(client, monkeypatch, runtime):+ monkeypatch.setenv("UNSLOTH_SYSTEMONE_DISABLE", "1")+ assert _post(client).status_code == 404+ settings = client.get("/api/settings/systemone").json()+ assert settings["enabled"] is False and settings["enabled_locked"] is True+ assert client.put("/api/settings/systemone", json = {"enabled": True}).status_code == 400+ assert runtime == []+++def test_settings_report_choices_and_residency(client):+ settings = client.get("/api/settings/systemone").json()+ assert settings["enabled"] is True+ assert settings["model"] == "laya-multilingual"+ assert settings["device"] == "cpu"+ assert settings["loaded_model"] is None+ assert [m["name"] for m in settings["models"]] == list(catalog.CHECKPOINTS)+ assert all(m["download_bytes"] > 0 for m in settings["models"])+ _post(client)+ settings = client.get("/api/settings/systemone").json()+ assert settings["loaded_model"] == "laya-multilingual"+ assert settings["loaded_device"] == "cpu"+ unloaded = client.post("/api/settings/systemone/unload").json()+ assert unloaded["loaded_model"] is None+++def test_changing_settings_drops_the_resident_model(client, runtime):+ _post(client)+ response = client.put(+ "/api/settings/systemone", json = {"model": "laya-english", "device": "gpu"}+ )+ assert response.status_code == 200, response.text+ body = response.json()+ assert (body["model"], body["device"], body["loaded_model"]) == ("laya-english", "gpu", None)+ assert _post(client).json()["model"] == "laya-english"+ assert runtime == ["laya-multilingual", "laya-english"]+++def test_turning_it_off_unloads_and_refuses(client, runtime):+ _post(client)+ body = client.put("/api/settings/systemone", json = {"enabled": False}).json()+ assert body["enabled"] is False and body["loaded_model"] is None+ assert _post(client).status_code == 404++[email protected]("payload", [{"model": "gpt-4"}, {"device": "tpu"}])+def test_invalid_settings_are_refused_without_evicting_the_model(client, payload):+ _post(client)+ assert client.put("/api/settings/systemone", json = payload).status_code == 400+ assert client.get("/api/settings/systemone").json()["loaded_model"] == "laya-multilingual"+++def test_env_model_is_locked(client, monkeypatch, tmp_path):+ monkeypatch.setenv("UNSLOTH_SYSTEMONE_MODEL", "laya-english")+ settings = client.get("/api/settings/systemone").json()+ assert settings["model"] == "laya-english" and settings["model_locked"] is True+ assert (+ client.put("/api/settings/systemone", json = {"model": "laya-multilingual"}).status_code+ == 400+ )+++def test_settings_change_waits_for_a_running_load(client, monkeypatch):+ release = threading.Event()++ def slow(checkpoint):+ release.wait(5)+ return FakeAgent(), "cpu"++ monkeypatch.setattr(laya_runtime, "_load_checkpoint", slow)+ monkeypatch.setattr(laya_runtime, "LOAD_WAIT_S", 0.05)+ assert _post(client).status_code == 503+ assert client.put("/api/settings/systemone", json = {"model": "laya-english"}).status_code == 409+ assert client.get("/api/settings/systemone").json()["model"] == "laya-multilingual"+ release.set()+ laya_runtime._loader.join(5)+++def test_turning_it_off_waits_for_a_claimed_load(client, monkeypatch):+ probing, release = threading.Event(), threading.Event()++ def probe(checkpoint):+ probing.set()+ release.wait(5)+ return False++ monkeypatch.setattr(laya_runtime, "_hub_download_active", probe)+ claim = threading.Thread(+ target = laya_runtime._ensure_loading, args = (catalog.default_checkpoint(),)+ )+ claim.start()+ assert probing.wait(5)+ assert client.put("/api/settings/systemone", json = {"enabled": False}).status_code == 409+ release.set()+ claim.join(5)+ laya_runtime._loader.join(5)+ assert client.get("/api/settings/systemone").json()["enabled"] is True+++def test_keyless_callers_only_reach_the_configured_model(client, monkeypatch, runtime):+ import auth.authentication++ monkeypatch.setattr(auth.authentication, "request_admitted_without_credential", lambda r: True)+ refused = _post(client, model = "laya-english")+ assert refused.status_code == 403+ assert refused.json()["detail"]["error_type"] == "permission_error"+ assert _post(client, model = "laya-multilingual").status_code == 200+ assert _post(client).status_code == 200+++def test_unload_returns_cached_device_memory(monkeypatch):+ freed = []+ fake_torch = SimpleNamespace(+ cuda = SimpleNamespace(is_initialized = lambda: True, empty_cache = lambda: freed.append("cuda")),+ backends = SimpleNamespace(mps = SimpleNamespace(is_available = lambda: False)),+ )+ monkeypatch.setitem(sys.modules, "torch", fake_torch)+ laya_runtime.unload()+ assert freed == ["cuda"]+++def test_download_plan_lists_exact_subfolder_files(client, monkeypatch):+ import huggingface_hub++ class Entry:+ def __init__(self, path, size):+ self.path, self.size = path, size++ tree = [+ Entry("model.safetensors", 9),+ Entry("multilingual/model.safetensors", 600),+ Entry("multilingual/rl_agent_config.json", 1),+ Entry("multilingual/tokenizer/tokenizer.json", 30),+ Entry("multilingual/encoder/config.json", 2),+ Entry("multilingual/README.md", 5),+ Entry("typed-decisions/model.safetensors", 800),+ ]+ monkeypatch.setattr(laya_runtime, "is_cached", lambda checkpoint: False)+ monkeypatch.setattr(huggingface_hub.HfApi, "list_repo_tree", lambda self, repo, **kw: tree)+ plan = client.get("/api/settings/systemone/resolve").json()+ assert plan == {+ "repo": catalog.LAYA_REPO,+ "files": [+ "multilingual/encoder/config.json",+ "multilingual/model.safetensors",+ "multilingual/rl_agent_config.json",+ "multilingual/tokenizer/tokenizer.json",+ ],+ "size_bytes": 633,+ "cached": False,+ "error": None,+ }+++def test_download_plan_for_a_cached_model_skips_the_hub(client, monkeypatch):+ import huggingface_hub++ monkeypatch.setattr(laya_runtime, "is_cached", lambda checkpoint: True)+ monkeypatch.setattr(+ huggingface_hub.HfApi, "list_repo_tree", lambda *a, **k: pytest.fail("hub listing")+ )+ plan = client.get("/api/settings/systemone/resolve").json()+ assert plan["cached"] is True and plan["files"] == []+++def test_is_cached_needs_weights_and_folders(monkeypatch, tmp_path):+ checkpoint = catalog.Checkpoint("laya-local", str(tmp_path), None, "")+ assert laya_runtime.is_cached(checkpoint) is False+ for name in ("encoder", "tokenizer"):+ (tmp_path / name).mkdir()+ assert laya_runtime.is_cached(checkpoint) is False+ for name in ("model.safetensors", "rl_agent_config.json"):+ (tmp_path / name).write_text("x")+ assert laya_runtime.is_cached(checkpoint) is True+++def test_local_checkpoint_is_served_under_its_own_name(client, monkeypatch, runtime, tmp_path):+ monkeypatch.setenv("UNSLOTH_SYSTEMONE_MODEL", str(tmp_path))+ assert _post(client).json()["model"] == catalog.LOCAL_NAME+ assert _post(client, model = catalog.LOCAL_NAME).status_code == 200+ assert runtime == [catalog.LOCAL_NAME]+++def test_misspelled_model_setting_is_not_fetched_from_the_hub(client, monkeypatch):+ import huggingface_hub++ monkeypatch.setattr(laya_runtime, "_load_checkpoint", _REAL_LOAD)+ monkeypatch.setattr(+ huggingface_hub, "snapshot_download", lambda *a, **k: pytest.fail("hub download")+ )+ monkeypatch.setenv("UNSLOTH_SYSTEMONE_MODEL", "laya-multilingul")+ response = _post(client)+ assert response.status_code == 503+ assert "neither a known model nor a directory" in response.json()["detail"]["message"]+++def test_oversized_state_is_refused(client, runtime):+ response = _post(client, state = "x" * (systemone.MAX_STATE_CHARS + 1))+ assert response.status_code == 400+ assert runtime == []+++def test_waiting_requests_are_bounded(client, monkeypatch):+ monkeypatch.setattr(laya_runtime, "_admission", threading.BoundedSemaphore(1))+ with laya_runtime._admission:+ response = _post(client)+ assert response.status_code == 529+ assert response.headers["Retry-After"] == "1"+ assert _post(client).status_code == 200+++def test_local_checkpoint_needs_encoder_and_tokenizer(tmp_path):+ checkpoint = catalog.Checkpoint("laya-local", str(tmp_path), None, "")+ with pytest.raises(FileNotFoundError, match = "encoder/, tokenizer/"):+ laya_runtime._checkpoint_dir(checkpoint)+ for name in ("encoder", "tokenizer"):+ (tmp_path / name).mkdir()+ assert laya_runtime._checkpoint_dir(checkpoint) == tmp_path+++def test_hub_checkpoint_downloads_only_its_subfolder_into_studio_cache(monkeypatch, tmp_path):+ import huggingface_hub++ from utils import hf_cache_settings++ for name in ("encoder", "tokenizer"):+ (tmp_path / "multilingual" / name).mkdir(parents = True)+ seen = {}++ def snapshot(repo, **kwargs):+ seen.update(kwargs, repo = repo)+ return str(tmp_path)++ monkeypatch.setattr(huggingface_hub, "snapshot_download", snapshot)+ monkeypatch.setattr(hf_cache_settings, "active_hf_hub_cache", lambda: "/cache/hub")+ monkeypatch.delenv("HF_HUB_OFFLINE", raising = False)+ assert laya_runtime._checkpoint_dir(catalog.CHECKPOINTS["laya-multilingual"]) == tmp_path+ assert seen["repo"] == catalog.LAYA_REPO+ assert seen["cache_dir"] == "/cache/hub"+ assert seen["local_files_only"] is False+ assert all(p.startswith("multilingual/") for p in seen["allow_patterns"])+++def test_device_defaults_to_cpu():+ assert laya_runtime._device() == "cpu"+++def test_gpu_on_apple_silicon_runs_mlx_and_falls_back_to_mps(monkeypatch):+ torch = pytest.importorskip("torch")+ from utils.hardware import hardware++ monkeypatch.setenv("UNSLOTH_SYSTEMONE_DEVICE", "gpu")+ monkeypatch.setattr(hardware, "get_device", lambda: hardware.DeviceType.MLX)+ monkeypatch.setattr(torch.backends.mps, "is_available", lambda: True)+ real = laya_runtime._mlx_available+ monkeypatch.setattr(laya_runtime, "_mlx_available", lambda: True)+ assert laya_runtime._device() == "mlx"+ monkeypatch.setattr(laya_runtime, "_mlx_available", real)+ # An unsloth-zoo release without the module.+ monkeypatch.setitem(sys.modules, "unsloth_zoo.mlx.decision", None)+ assert laya_runtime._device() == "mps"+++def test_mlx_answers_match_laya_on_cpu(monkeypatch):+ path = os.environ.get("SYSTEMONE_TEST_LAYA")+ if not path:+ pytest.skip("set SYSTEMONE_TEST_LAYA to a downloaded convaiinnovations/laya snapshot")+ laya = pytest.importorskip("laya")+ if not laya_runtime._mlx_available():+ pytest.skip("needs unsloth_zoo.mlx.decision on Apple Silicon")+ from pathlib import Path++ reference = laya.load(path, subfolder = "multilingual", device = "cpu")+ monkeypatch.setattr(laya_runtime, "_checkpoint_dir", lambda checkpoint: Path(path))+ monkeypatch.setattr(laya_runtime, "_device", lambda: "mlx")+ agent, device = _REAL_LOAD(catalog.CHECKPOINTS["laya-multilingual"])+ assert isinstance(agent, laya_runtime._MLXAgent) and device == "mlx"+ for state in (+ "Everything is down and we have a demo at noon.",+ "Hola, ¿me pueden devolver el dinero? " * 80,+ ):+ expected, _ = _REAL_PREDICT(reference, state, QUESTIONS)+ got, _ = _REAL_PREDICT(agent, state, QUESTIONS)+ assert got["usage"] == expected["usage"]+ for name, answer in expected["answers"].items():+ if answer["type"] == "noul":+ assert got["answers"][name]["noul"] == pytest.approx(answer["noul"], abs = 5e-3)+ else:+ assert got["answers"][name]["probabilities"] == pytest.approx(+ answer["probabilities"], abs = 5e-3+ )+++def test_managed_account_reads_the_owner_switch(monkeypatch):+ import storage.studio_db as studio_db+ from utils import account_context++ readers = []++ def read(key, fallback = None):+ readers.append(account_context.current_account().account_id)+ return True++ monkeypatch.setattr(systemone_settings, "_owner_setting", _REAL_OWNER_SETTING)+ monkeypatch.setattr(studio_db, "get_app_setting", read)+ token = account_context.bind_account(account_context.AccountContext("alice", "alice"))+ try:+ assert systemone_settings.get_enabled() is True+ assert account_context.current_account().account_id == "alice"+ finally:+ account_context.reset_account(token)+ assert readers == [account_context.OWNER_ACCOUNT_ID]+++def test_studio_app_serves_systemone_routes():+ from main import app++ client = TestClient(app)+ body = {"state": "x", "model": "jev-latest", "questions": {"q": {"type": "noul"}}}+ assert client.post("/v1/systemone", json = body).status_code == 401+ assert client.get("/api/settings/systemone").status_code == 401+ assert client.put("/api/settings/systemone", json = {"enabled": True}).status_code == 401+ assert client.post("/api/settings/systemone/unload").status_code == 401+++def test_keyless_inference_scope_covers_systemone():+ from utils.keyless_api_access import scope_covers+ assert scope_covers("inference", "POST", "/v1/systemone")+ assert not scope_covers("inference", "PUT", "/api/settings/systemone")+++def test_switching_models_keeps_serving_until_the_new_checkpoint_is_on_disk(client, monkeypatch):+ assert _post(client).status_code == 200+ fetching = threading.Event()+ release = threading.Event()++ def download(checkpoint, **kwargs):+ fetching.set()+ release.wait(5)+ raise OSError("connection reset")++ monkeypatch.setattr(laya_runtime, "_load_checkpoint", _REAL_LOAD)+ monkeypatch.setattr(laya_runtime, "_checkpoint_dir", download)+ monkeypatch.setattr(laya_runtime, "LOAD_WAIT_S", 0.05)+ assert _post(client, model = "laya-english").status_code == 503+ assert fetching.wait(5)+ assert _post(client).status_code == 200+ release.set()+ laya_runtime._loader.join(5)+ assert _post(client).status_code == 200+ assert laya_runtime.status()["loaded_model"] == "laya-multilingual"+++def test_load_waits_for_a_hub_download_of_the_same_repo(client, monkeypatch, runtime):+ from hub.utils import download_registry++ registry = download_registry.get_models_registry()+ monkeypatch.setattr(+ registry, "active_job_refs", lambda repo = None: ["job"] if repo == catalog.LAYA_REPO else []+ )+ monkeypatch.setattr(laya_runtime, "is_cached", lambda checkpoint: False)+ response = _post(client)+ assert response.status_code == 503+ assert response.json()["detail"]["message"] == "laya-multilingual is downloading"+ assert runtime == []+ monkeypatch.setattr(laya_runtime, "is_cached", lambda checkpoint: True)+ assert _post(client).status_code == 200+++def test_hub_download_is_refused_while_laya_loads_the_repo(client, monkeypatch):+ from hub.services.models import downloads++ release = threading.Event()++ def slow(checkpoint):+ release.wait(5)+ return FakeAgent(), "cpu"++ monkeypatch.setattr(laya_runtime, "_load_checkpoint", slow)+ monkeypatch.setattr(laya_runtime, "LOAD_WAIT_S", 0.05)+ assert not downloads._load_in_flight(catalog.LAYA_REPO)+ assert _post(client).status_code == 503+ assert downloads._load_in_flight("ConvAIInnovations/Laya")+ release.set()+ laya_runtime._loader.join(5)+ assert not downloads._load_in_flight(catalog.LAYA_REPO)+++def test_errors_keep_the_jev_shape_behind_studio_error_handlers(monkeypatch):+ from utils.api_errors import install_api_error_handlers++ app = FastAPI()+ install_api_error_handlers(app)+ app.include_router(systemone.router, prefix = "/v1")+ app.dependency_overrides[get_current_subject] = lambda: "tester"+ client = TestClient(app)+ response = _post(client, model = "gpt-4")+ assert response.status_code == 400+ assert response.json()["detail"]["error_type"] == "api_usage_error"+ malformed = _post(client, state = 3)+ assert malformed.status_code == 422+ assert malformed.json()["detail"][0]["loc"][:2] == ["body", "state"]+++def test_state_limit_counts_characters_not_json_escapes(client):+ text = "\u4f60" * (systemone.MAX_STATE_CHARS // 2)+ assert _post(client, state = text).status_code == 200+ assert _post(client, state = {"message": text}).status_code == 200+++def test_load_probes_the_download_registry_without_holding_runtime_state(client, monkeypatch):+ from hub.utils import download_registry++ registry = download_registry.get_models_registry()+ seen, blocked = [], []++ def active_job_refs(repo = None):+ probe = threading.Thread(target = lambda: seen.append(laya_runtime.loading_repo_ids()))+ probe.start()+ probe.join(2)+ blocked.append(probe.is_alive())+ return []++ monkeypatch.setattr(registry, "active_job_refs", active_job_refs)+ assert _post(client).status_code == 200+ assert blocked == [False]+ assert seen == [(catalog.LAYA_REPO,)]+++def test_decision_api_cannot_be_enabled_where_laya_is_not_installed(client, monkeypatch):+ settings = {}+ monkeypatch.setattr(systemone_settings, "_owner_setting", settings.get)+ import storage.studio_db as studio_db++ monkeypatch.setattr(studio_db, "upsert_app_settings", settings.update)+ reason = "The Decision API needs PyTorch, which this Studio install does not include."+ monkeypatch.setattr(systemone_settings, "runtime_unavailable_reason", lambda: reason)+ response = client.put("/api/settings/systemone", json = {"enabled": True})+ assert response.status_code == 400+ assert response.json()["detail"] == reason+ assert client.get("/api/settings/systemone").json()["enabled"] is False+ assert client.put("/api/settings/systemone", json = {"enabled": False}).status_code == 200+++def test_runtime_reason_names_what_the_install_lacks(monkeypatch):+ import importlib.util++ real = importlib.util.find_spec+ monkeypatch.setattr(+ importlib.util, "find_spec", lambda name, *a: None if name == "torch" else real(name, *a)+ )+ expected = "Python 3.10" if sys.version_info < (3, 10) else "PyTorch"+ assert expected in _REAL_UNAVAILABLE_REASON()+++def test_settings_drop_a_load_failure_once_its_backoff_expires(client, monkeypatch):+ def fail(checkpoint):+ raise OSError("connection reset")++ monkeypatch.setattr(laya_runtime, "_load_checkpoint", fail)+ assert _post(client).status_code == 503+ assert "connection reset" in client.get("/api/settings/systemone").json()["error"]+ checkpoint, message, _deadline = laya_runtime._failure+ monkeypatch.setattr(laya_runtime, "_failure", (checkpoint, message, 0.0))+ assert client.get("/api/settings/systemone").json()["error"] is None+++def test_settings_only_report_a_failure_of_the_selected_model(client, monkeypatch):+ def fail(checkpoint):+ raise OSError("connection reset")++ assert _post(client).status_code == 200+ monkeypatch.setattr(laya_runtime, "_load_checkpoint", fail)+ assert _post(client, model = "laya-english").status_code == 503+ body = client.get("/api/settings/systemone").json()+ assert body["error"] is None and body["loaded_model"] == "laya-multilingual"+ laya_runtime.unload()+ assert _post(client).status_code == 503+ assert "connection reset" in client.get("/api/settings/systemone").json()["error"]+++def test_route_module_imports_without_pep604_aliases():+ import ast+ import inspect++ tree = ast.parse(inspect.getsource(systemone))+ runtime_unions = [+ node+ for stmt in tree.body+ if isinstance(stmt, ast.Assign)+ for node in ast.walk(stmt.value)+ if isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr)+ ]+ assert runtime_unions == []+++def test_oversized_question_text_is_refused_before_the_model(client, runtime):+ questions = {"q": {"type": "noul", "instructions": "x" * systemone.MAX_QUESTION_CHARS}}+ response = _post(client, questions = questions)+ assert response.status_code == 400+ assert "longer than" in response.json()["detail"]["message"]+ criteria = {f"option {i}": "y" * 200 for i in range(200)}+ assert (+ _post(client, questions = {"q": {"type": "choice", "criteria": criteria}}).status_code == 400+ )+ assert runtime == []+ assert _post(client).status_code == 200+++def test_real_laya_answers_through_the_route(client, monkeypatch):+ path = os.environ.get("SYSTEMONE_TEST_LAYA")+ if not path:+ pytest.skip("set SYSTEMONE_TEST_LAYA to a downloaded convaiinnovations/laya snapshot")+ pytest.importorskip("laya")+ monkeypatch.setattr(laya_runtime, "_load_checkpoint", _REAL_LOAD)+ monkeypatch.setattr(laya_runtime, "_predict", _REAL_PREDICT)+ monkeypatch.setenv("UNSLOTH_SYSTEMONE_MODEL", path)+ monkeypatch.setenv("UNSLOTH_SYSTEMONE_SUBFOLDER", "multilingual")+ monkeypatch.setattr(laya_runtime, "LOAD_WAIT_S", 600.0)+ response = _post(client)+ assert response.status_code == 200, response.text+ body = response.json()+ assert body["model"] == catalog.LOCAL_NAME+ assert 0.0 <= body["answers"]["urgent"]["noul"] <= 1.0+ assert body["answers"]["team"]["choice"] in {"outage", "billing"}+ assert 0.0 <= body["answers"]["tone"]["score"] <= 2.0+ assert "X-Unsloth-State-Truncated" not in response.headers+ long_response = _post(client, state = "word " * 3000)+ assert long_response.status_code == 422+ assert "context window" in long_response.json()["detail"]["message"]+++class WordTokenizer:+ mask_token = "[MASK]"++ def __init__(self):+ self.calls = []++ def __call__(+ self,+ text,+ add_special_tokens = False,+ ):+ self.calls.append(len(text))+ return {"input_ids": [len(word) for word in text.split()]}+++def test_long_state_is_tokenized_only_as_far_as_the_model_reads():+ pytest.importorskip("laya")+ tok = WordTokenizer()+ ids, cut = laya_runtime._state_ids(tok, "word " * 100_000, 900)+ assert (ids, cut) == ([4] * 900, True)+ assert max(tok.calls) < 100_000++[email protected]("words, cut", [(900, False), (901, True), (5000, True)])+def test_state_cut_matches_the_full_tokenization(words, cut):+ pytest.importorskip("laya")+ ids, was_cut = laya_runtime._state_ids(WordTokenizer(), "word " * words, 900)+ assert (len(ids), was_cut) == (min(words, 900), cut)+++class MovableModel:+ def __init__(self):+ self.moved_to = []++ def to(self, device):+ self.moved_to.append(device.type)+ return self+++def gpu_agent(monkeypatch):+ torch = pytest.importorskip("torch")+ collate = lambda groups, pad: {"attention_mask": torch.ones(1, len(groups[0][0]["ids"]))}+ monkeypatch.setitem(sys.modules, "laya.common", SimpleNamespace(collate_items = collate))+ return SimpleNamespace(+ device = torch.device("cuda"),+ dtype = torch.float16,+ model = MovableModel(),+ tok = SimpleNamespace(pad_token_id = 0),+ )+++def _items():+ return [{"ids": [1, 5, 2], "markers": [1], "qtype": 0}]+++def test_gpu_out_of_memory_falls_back_to_cpu_like_laya(monkeypatch, gpu_agent):+ import torch++ agent = gpu_agent+ ran_on = []++ def run(agent, batch):+ ran_on.append(agent.device.type)+ if agent.device.type != "cpu":+ raise torch.cuda.OutOfMemoryError("CUDA out of memory. Tried to allocate 2.00 GiB")+ return torch.tensor([[0.5, 1.5]])++ monkeypatch.setattr(laya_runtime, "_run_model", run)+ monkeypatch.setattr(laya_runtime, "_device_name", "cuda:0")+ released = []+ monkeypatch.setattr(laya_runtime, "_release_memory", lambda: released.append(ran_on[:]))+ logits, tokens = laya_runtime._forward(agent, _items())+ assert logits.tolist() == [[0.5, 1.5]] and tokens == 3+ assert ran_on == ["cuda", "cpu"]+ assert released == [["cuda"]]+ assert (agent.device.type, agent.dtype, agent.model.moved_to) == ("cpu", torch.float32, ["cpu"])+ assert laya_runtime._device_name == "cpu"+ laya_runtime._forward(agent, _items())+ assert ran_on == ["cuda", "cpu", "cpu"]+++def test_unrelated_gpu_errors_are_not_retried_on_cpu(monkeypatch, gpu_agent):+ agent = gpu_agent++ def run(agent, batch):+ raise RuntimeError("shape mismatch")++ monkeypatch.setattr(laya_runtime, "_run_model", run)+ with pytest.raises(RuntimeError, match = "shape mismatch"):+ laya_runtime._forward(agent, _items())+ assert agent.device.type == "cuda" and agent.model.moved_to == []++[email protected]("where", ["mx", "mx.metal", None])+def test_release_memory_clears_the_mlx_cache_under_either_name(monkeypatch, where):+ cleared = []+ clear = SimpleNamespace(clear_cache = lambda: cleared.append(where))+ mx = {"mx": clear, "mx.metal": SimpleNamespace(metal = clear), None: SimpleNamespace()}[where]+ monkeypatch.setitem(sys.modules, "mlx.core", mx)+ laya_runtime._release_memory()+ assert cleared == ([where] if where else [])+++def test_mlx_out_of_memory_falls_back_to_cpu(monkeypatch, gpu_agent):+ import torch++ class Model:+ def __init__(self, message):+ self.message = message++ def logits(self, batch):+ raise RuntimeError(self.message)++ cpu = SimpleNamespace(model = MovableModel(), device = torch.device("cpu"), dtype = torch.float32)+ loads, mlx_models, cleared = [], [], []++ def load(path, device):+ # The MLX model and MLX's buffer cache are both released before the CPU copy loads.+ assert mlx_models[-1]() is None and cleared+ loads.append((path, device))+ if len(loads) == 1:+ raise MemoryError+ return cpu++ def run_out_of_memory(message):+ agent.model = Model(message)+ mlx_models.append(weakref.ref(agent.model))+ cleared.clear()+ monkeypatch.setattr(laya_runtime, "_agent", agent)+ monkeypatch.setattr(laya_runtime, "_loaded", "checkpoint")+ return laya_runtime._forward(agent, _items())++ monkeypatch.setitem(sys.modules, "laya", SimpleNamespace(load = load))+ monkeypatch.setitem(+ sys.modules, "mlx.core", SimpleNamespace(clear_cache = lambda: cleared.append(True))+ )+ ran_on = []+ monkeypatch.setattr(+ laya_runtime,+ "_run_model",+ lambda agent, batch: ran_on.append(agent.device.type) or torch.tensor([[0.5, 1.5]]),+ )+ monkeypatch.setattr(laya_runtime, "_device_name", "mlx")+ agent = laya_runtime._MLXAgent.__new__(laya_runtime._MLXAgent)+ agent.folder, agent.tok, agent.model = "ckpt", gpu_agent.tok, Model("shape mismatch")+ with pytest.raises(RuntimeError, match = "shape mismatch"):+ laya_runtime._forward(agent, _items())+ assert loads == [] and agent.device == "mlx"++ with pytest.raises(MemoryError):+ run_out_of_memory("[malloc] Unable to allocate 2147483648 bytes.")+ assert (laya_runtime._agent, laya_runtime._loaded, laya_runtime._device_name) == (+ None,+ None,+ None,+ )++ logits, tokens = run_out_of_memory(+ "[METAL] Command buffer execution failed: Insufficient Memory."+ )+ assert logits.tolist() == [[0.5, 1.5]] and tokens == 3+ assert loads == [("ckpt", "cpu")] * 2+ assert (agent.model, agent.device.type, agent.dtype, laya_runtime._device_name) == (+ cpu.model,+ "cpu",+ torch.float32,+ "cpu",+ )+ laya_runtime._forward(agent, _items())+ assert ran_on == ["cpu", "cpu"] and len(loads) == 2+++def test_fast_path_matches_laya_predict():+ path = os.environ.get("SYSTEMONE_TEST_LAYA")+ if not path:+ pytest.skip("set SYSTEMONE_TEST_LAYA to a downloaded convaiinnovations/laya snapshot")+ laya = pytest.importorskip("laya")+ agent = laya.load(path, subfolder = "multilingual", device = "cpu")+ questions = {name: laya_runtime._to_laya(q) for name, q in QUESTIONS.items()}+ questions["zeta"] = {+ "type": "choice",+ "instructions": "Pick one",+ "criteria": {"zeta": None, "alpha": "first", "mid": ""},+ }+ for state in (+ "I was charged twice.",+ {"turns": ["hi", "refund please"]},+ "Über 请 word " * 20_000,+ ):+ expected = agent.predict(state, questions)+ result, truncated = _REAL_PREDICT(agent, state, questions)+ assert result["usage"] == expected["usage"]+ for name, answer in result["answers"].items():+ want = {k: v for k, v in expected["answers"][name].items() if k != "action"}+ assert answer == want+ if "probabilities" in answer:+ assert list(answer["probabilities"]) == list(want["probabilities"])+ assert truncated == (len(state) > 200 if isinstance(state, str) else False)diff --git a/studio/backend/utils/api_errors.py b/studio/backend/utils/api_errors.pyindex 37185561fe5..4941df1e8af 100644--- a/studio/backend/utils/api_errors.py+++ b/studio/backend/utils/api_errors.py@@ -90,9 +90,15 @@ def is_anthropic_path(path: str) -> bool:return path.startswith("/v1/messages")+# Jev-compatible, not OpenAI: its clients expect FastAPI's own 422 and {"detail": ...} bodies.+_NATIVE_ERROR_PATHS = frozenset({"/v1/systemone"})++def wants_api_error_envelope(path: str) -> bool:"""True for the OpenAI/Anthropic-compatible surfaces: the ``/v1/*`` mount andthe preview ``/p/<run>[/<ckpt>]/v1/*`` mount."""+ if path in _NATIVE_ERROR_PATHS:+ return Falsereturn path.startswith("/v1/") or (path.startswith("/p/") and "/v1/" in path)diff --git a/studio/backend/utils/keyless_api_access.py b/studio/backend/utils/keyless_api_access.pyindex cba0b9f6d4b..0ab663c59f8 100644--- a/studio/backend/utils/keyless_api_access.py+++ b/studio/backend/utils/keyless_api_access.py@@ -39,6 +39,7 @@ _INFERENCE_ROUTES = frozenset(("GET", "/v1/models"),("GET", "/api/inference/loaded-models"),("POST", "/v1/responses"),+ ("POST", "/v1/systemone"),# Discovery probes, read-only. A keyless client that may list models and chat but gets 401 on /props reads that as an auth wall in front of the whole surface and stops, which is the opposite of what the scope grants.("GET", "/props"),("GET", "/v1/props"),diff --git a/studio/backend/utils/systemone_settings.py b/studio/backend/utils/systemone_settings.pynew file mode 100644index 00000000000..429488cd157--- /dev/null+++ b/studio/backend/utils/systemone_settings.py@@ -0,0 +1,129 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Installation-wide settings for the Decision API (/v1/systemone). Environment variables win."""++from __future__ import annotations++import os+import sys+from typing import Any++ENABLED_KEY = "systemone_enabled"+MODEL_KEY = "systemone_model"+DEVICE_KEY = "systemone_device"+DEFAULT_MODEL = "laya-multilingual"+DEVICES = ("cpu", "gpu")++ENV_DISABLE = "UNSLOTH_SYSTEMONE_DISABLE"+ENV_MODEL = "UNSLOTH_SYSTEMONE_MODEL"+ENV_DEVICE = "UNSLOTH_SYSTEMONE_DEVICE"+++def _owner_setting(key: str) -> Any:+ # Installation-wide: a managed account's API key must see the owner's switch, not its own database.+ from storage.studio_db import get_app_setting+ from utils.account_context import OWNER, bind_account, reset_account++ token = bind_account(OWNER)+ try:+ return get_app_setting(key, None)+ except Exception:+ return None+ finally:+ reset_account(token)+++def _env(name: str) -> str:+ return os.environ.get(name, "").strip()+++def enabled_locked() -> bool:+ return _env(ENV_DISABLE) == "1"+++def model_locked() -> bool:+ return bool(_env(ENV_MODEL))+++def device_locked() -> bool:+ return bool(_env(ENV_DEVICE))+++def runtime_unavailable_reason() -> str | None:+ # laya is installed only on Python 3.10+ (extras-no-deps.txt) and needs torch (absent on --no-torch).+ if sys.version_info < (3, 10):+ return "The Decision API needs Python 3.10 or newer."+ try:+ from importlib.util import find_spec+ if find_spec("torch") is None:+ return "The Decision API needs PyTorch, which this Studio install does not include."+ except Exception:+ pass+ return None+++def get_enabled() -> bool:+ if enabled_locked():+ return False+ return _owner_setting(ENABLED_KEY) is True+++def get_model() -> str:+ if model_locked():+ return _env(ENV_MODEL)+ from core.systemone.catalog import CHECKPOINTS++ stored = _owner_setting(MODEL_KEY)+ return stored if stored in CHECKPOINTS else DEFAULT_MODEL+++def get_device() -> str:+ if device_locked():+ return "gpu" if _env(ENV_DEVICE).lower() in ("gpu", "auto") else "cpu"+ stored = _owner_setting(DEVICE_KEY)+ return stored if stored in DEVICES else "cpu"+++def validate(+ *,+ enabled: bool | None = None,+ model: str | None = None,+ device: str | None = None,+) -> dict[str, Any]:+ from core.systemone.catalog import CHECKPOINTS++ values: dict[str, Any] = {}+ if enabled is not None:+ if enabled_locked():+ raise ValueError(f"The Decision API is turned off by {ENV_DISABLE}.")+ if enabled and (reason := runtime_unavailable_reason()):+ raise ValueError(reason)+ values[ENABLED_KEY] = bool(enabled)+ if model is not None:+ if model_locked():+ raise ValueError(f"The Decision API model is set by {ENV_MODEL}.")+ if model not in CHECKPOINTS:+ raise ValueError(f"Unknown Decision API model: {model}")+ values[MODEL_KEY] = model+ if device is not None:+ if device_locked():+ raise ValueError(f"The Decision API device is set by {ENV_DEVICE}.")+ if device not in DEVICES:+ raise ValueError("Device must be cpu or gpu.")+ values[DEVICE_KEY] = device+ return values+++def save(values: dict[str, Any]) -> None:+ from storage.studio_db import upsert_app_settings+ if values:+ upsert_app_settings(values)+++def gpu_available() -> bool:+ try:+ from utils.hardware.hardware import DeviceType, get_device as detected_device+ return detected_device() in (DeviceType.CUDA, DeviceType.XPU, DeviceType.MLX)+ except Exception:+ return Falsediff --git a/studio/frontend/src/features/hub/index.ts b/studio/frontend/src/features/hub/index.tsindex b9991f96d1d..4a6830dfcff 100644--- a/studio/frontend/src/features/hub/index.ts+++ b/studio/frontend/src/features/hub/index.ts@@ -2,11 +2,13 @@// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0export {+ DOWNLOAD_KIND,DownloadProgressBar,downloadManager,finishExternalJob,jobKeyOf,pendingDrafterPresentation,+ scopedVariant,type StagedDownloadProgress,startExternalJob,subscribeJobListeners,diff --git a/studio/frontend/src/features/settings/api/systemone.ts b/studio/frontend/src/features/settings/api/systemone.tsnew file mode 100644index 00000000000..ba22639c640--- /dev/null+++ b/studio/frontend/src/features/settings/api/systemone.ts@@ -0,0 +1,147 @@+// 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";++export type SystemOneDevice = "cpu" | "gpu";++export type SystemOneModel = {+ name: string;+ description: string;+ downloadBytes: number;+};++export type SystemOneSettings = {+ enabled: boolean;+ enabledLocked: boolean;+ model: string;+ modelLocked: boolean;+ device: SystemOneDevice;+ deviceLocked: boolean;+ gpuAvailable: boolean;+ models: SystemOneModel[];+ loadedModel: string | null;+ loadedDevice: string | null;+ loadingModel: string | null;+ installing: boolean;+ error: string | null;+};++export type SystemOneDownloadPlan = {+ repo: string | null;+ files: string[];+ sizeBytes: number;+ cached: boolean;+ error: string | null;+};++type ApiSystemOneSettings = {+ enabled: boolean;+ // biome-ignore lint/style/useNamingConvention: API schema+ enabled_locked: boolean;+ model: string;+ // biome-ignore lint/style/useNamingConvention: API schema+ model_locked: boolean;+ device: SystemOneDevice;+ // biome-ignore lint/style/useNamingConvention: API schema+ device_locked: boolean;+ // biome-ignore lint/style/useNamingConvention: API schema+ gpu_available: boolean;+ // biome-ignore lint/style/useNamingConvention: API schema+ models: { name: string; description: string; download_bytes: number }[];+ // biome-ignore lint/style/useNamingConvention: API schema+ loaded_model: string | null;+ // biome-ignore lint/style/useNamingConvention: API schema+ loaded_device: string | null;+ // biome-ignore lint/style/useNamingConvention: API schema+ loading_model: string | null;+ installing: boolean;+ error: string | null;+};++type ApiSystemOneDownloadPlan = {+ repo: string | null;+ files: string[];+ // biome-ignore lint/style/useNamingConvention: API schema+ size_bytes: number;+ cached: boolean;+ error: string | null;+};++const SETTINGS_PATH = "/api/settings/systemone";++function fromApi(settings: ApiSystemOneSettings): SystemOneSettings {+ return {+ enabled: settings.enabled,+ enabledLocked: settings.enabled_locked,+ model: settings.model,+ modelLocked: settings.model_locked,+ device: settings.device,+ deviceLocked: settings.device_locked,+ gpuAvailable: settings.gpu_available,+ models: settings.models.map((m) => ({+ name: m.name,+ description: m.description,+ downloadBytes: m.download_bytes,+ })),+ loadedModel: settings.loaded_model,+ loadedDevice: settings.loaded_device,+ loadingModel: settings.loading_model,+ installing: settings.installing,+ error: settings.error,+ };+}++async function readSettings(res: Response, fallback: string) {+ if (!res.ok) {+ throw new Error(await readFastApiError(res, fallback));+ }+ return fromApi((await res.json()) as ApiSystemOneSettings);+}++export async function loadSystemOneSettings(): Promise<SystemOneSettings> {+ return readSettings(+ await authFetch(SETTINGS_PATH),+ "Failed to load Decision API settings",+ );+}++export async function updateSystemOneSettings(patch: {+ enabled?: boolean;+ model?: string;+ device?: SystemOneDevice;+}): Promise<SystemOneSettings> {+ return readSettings(+ await authFetch(SETTINGS_PATH, {+ method: "PUT",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(patch),+ }),+ "Failed to save Decision API settings",+ );+}++export async function unloadSystemOneModel(): Promise<SystemOneSettings> {+ return readSettings(+ await authFetch(`${SETTINGS_PATH}/unload`, { method: "POST" }),+ "Failed to unload the Decision API model",+ );+}++export async function resolveSystemOneDownload(): Promise<SystemOneDownloadPlan> {+ const res = await authFetch(`${SETTINGS_PATH}/resolve`);+ if (!res.ok) {+ throw new Error(+ await readFastApiError(res, "Failed to check the Decision API model"),+ );+ }+ const plan = (await res.json()) as ApiSystemOneDownloadPlan;+ return {+ repo: plan.repo,+ files: plan.files,+ sizeBytes: plan.size_bytes,+ cached: plan.cached,+ error: plan.error,+ };+}diff --git a/studio/frontend/src/features/settings/components/decision-api-section.tsx b/studio/frontend/src/features/settings/components/decision-api-section.tsxnew file mode 100644index 00000000000..c741b446b5e--- /dev/null+++ b/studio/frontend/src/features/settings/components/decision-api-section.tsx@@ -0,0 +1,424 @@+// 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 { Button } from "@/components/ui/button";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";+import { Spinner } from "@/components/ui/spinner";+import { Switch } from "@/components/ui/switch";+import {+ DOWNLOAD_KIND,+ downloadManager,+ formatBytes,+ jobKeyOf,+ scopedVariant,+ useDownloadManagerStore,+} from "@/features/hub";+import { type TranslationKey, translate, useT } from "@/i18n";+import { toast } from "@/lib/toast";+import { cn } from "@/lib/utils";+import { TaskDone01Icon } from "@hugeicons/core-free-icons";+import { HugeiconsIcon } from "@hugeicons/react";+import { type ReactElement, useEffect, useState } from "react";+import {+ type SystemOneDevice,+ type SystemOneDownloadPlan,+ type SystemOneSettings,+ loadSystemOneSettings,+ resolveSystemOneDownload,+ unloadSystemOneModel,+ updateSystemOneSettings,+} from "../api/systemone";+import { SettingsRow } from "./settings-row";++const DOWNLOAD_SCOPE = "systemone";+const POLL_MS = 5000;+const RECOMMENDED_MODEL = "laya-multilingual";+const MODEL_LABELS: Record<string, TranslationKey> = {+ "laya-multilingual": "settings.apiKeys.decisionApi.modelMultilingual",+ "laya-english": "settings.apiKeys.decisionApi.modelEnglish",+ "laya-typed-decisions": "settings.apiKeys.decisionApi.modelTypedDecisions",+};+const ENV_DISABLE = "UNSLOTH_SYSTEMONE_DISABLE";+const ENV_MODEL = "UNSLOTH_SYSTEMONE_MODEL";+const ENV_DEVICE = "UNSLOTH_SYSTEMONE_DEVICE";++function deviceLabel(device: string | null): string {+ return device && device !== "cpu" ? "GPU" : "CPU";+}++function errorMessage(error: unknown): string | null {+ return error instanceof Error ? error.message : null;+}++export function DecisionApiSection(): ReactElement | null {+ const t = useT();+ const [settings, setSettings] = useState<SystemOneSettings | null>(null);+ const [planState, setPlanState] = useState<{+ model: string;+ plan: SystemOneDownloadPlan;+ } | null>(null);+ const [busy, setBusy] = useState(false);+ const [error, setError] = useState<string | null>(null);++ const enabled = settings?.enabled ?? false;+ const model = settings?.model ?? null;+ const plan = planState && planState.model === model ? planState.plan : null;++ useEffect(() => {+ let live = true;+ loadSystemOneSettings().then(+ (next) => live && setSettings(next),+ (err) =>+ live &&+ setError(+ errorMessage(err) ??+ translate("settings.apiKeys.decisionApi.loadError"),+ ),+ );+ return () => {+ live = false;+ };+ }, []);++ useEffect(() => {+ if (!enabled) return;+ const timer = window.setInterval(() => {+ if (document.visibilityState !== "visible") return;+ loadSystemOneSettings().then(setSettings, () => undefined);+ }, POLL_MS);+ return () => window.clearInterval(timer);+ }, [enabled]);++ const jobKey =+ plan?.repo && !plan.cached && plan.files.length > 0+ ? jobKeyOf(DOWNLOAD_KIND.MODEL, plan.repo, scopedVariant(DOWNLOAD_SCOPE))+ : null;+ const jobState = useDownloadManagerStore((s) =>+ jobKey ? (s.jobs[jobKey]?.state ?? null) : null,+ );+ const downloading = jobState === "running" || jobState === "cancelling";+ const downloadDone = jobState === "complete";++ useEffect(() => {+ if (!enabled || !model) return;+ let live = true;+ resolveSystemOneDownload().then(+ (next) => live && setPlanState({ model, plan: next }),+ (err) => live && setError(errorMessage(err)),+ );+ return () => {+ live = false;+ };+ }, [enabled, model, downloadDone]);++ const modelLabel = (name: string) =>+ MODEL_LABELS[name] ? t(MODEL_LABELS[name]) : name;++ const startDownload = async (next: SystemOneDownloadPlan) => {+ if (!next.repo || next.cached || next.files.length === 0) return;+ try {+ const outcome = await downloadManager.requestStart({+ kind: DOWNLOAD_KIND.MODEL,+ repoId: next.repo,+ variant: scopedVariant(DOWNLOAD_SCOPE),+ scopeId: DOWNLOAD_SCOPE,+ files: next.files,+ inventoryKind: "model",+ expectedBytes: next.sizeBytes,+ });+ if (outcome === "started") return;+ if (outcome === "conflict" || outcome === "busy") {+ toast.info(t("settings.apiKeys.decisionApi.downloadBusy"));+ } else {+ toast.error(t("settings.apiKeys.decisionApi.downloadFailed"));+ }+ } catch (err) {+ toast.error(t("settings.apiKeys.decisionApi.downloadFailed"), {+ description: errorMessage(err) ?? undefined,+ });+ }+ };++ const apply = async (+ patch: Parameters<typeof updateSystemOneSettings>[0],+ downloadAfter: boolean,+ ) => {+ setBusy(true);+ setError(null);+ try {+ const next = await updateSystemOneSettings(patch);+ setSettings(next);+ // Download on the switch, not on the first request: a first API call should not sit behind a 700 MB transfer.+ if (next.enabled && downloadAfter) {+ const nextPlan = await resolveSystemOneDownload();+ setPlanState({ model: next.model, plan: nextPlan });+ await startDownload(nextPlan);+ }+ } catch (err) {+ setError(+ errorMessage(err) ?? t("settings.apiKeys.decisionApi.saveFailed"),+ );+ } finally {+ setBusy(false);+ }+ };++ const unload = async () => {+ setBusy(true);+ setError(null);+ try {+ setSettings(await unloadSystemOneModel());+ } catch (err) {+ setError(errorMessage(err));+ } finally {+ setBusy(false);+ }+ };++ const header = (+ <>+ <div className="flex items-start gap-3 bg-muted/30 p-4">+ <div className="flex size-8 shrink-0 items-center justify-center rounded-md border border-border/70 bg-muted/40">+ <HugeiconsIcon+ icon={TaskDone01Icon}+ className="size-4 text-foreground"+ />+ </div>+ <div className="flex min-w-0 flex-col gap-0.5">+ <h2 className="text-base font-semibold font-heading text-foreground">+ {t("settings.apiKeys.decisionApi.title")}+ </h2>+ <p className="text-xs text-muted-foreground leading-relaxed">+ {t("settings.apiKeys.decisionApi.description")}+ </p>+ </div>+ </div>++ {error ? (+ <p className="border-t border-border/60 px-4 py-2.5 text-xs leading-snug text-destructive">+ {error}+ </p>+ ) : null}+ </>+ );++ if (!settings) {+ return error ? (+ <section+ data-settings-label={t("settings.apiKeys.decisionApi.title")}+ className="overflow-hidden rounded-lg border border-border/70"+ >+ {header}+ </section>+ ) : null;+ }++ const current = settings.models.find((m) => m.name === settings.model);+ const knownModel = current !== undefined;+ const sizeBytes = plan?.sizeBytes || current?.downloadBytes || 0;++ let tone: "pending" | "ready" | "error" | null = null;+ let status = "";+ let action: "download" | "unload" | null = null;+ if (settings.error) {+ tone = "error";+ status = settings.error;+ } else if (settings.installing) {+ tone = "pending";+ status = t("settings.apiKeys.decisionApi.installing");+ } else if (downloading) {+ tone = "pending";+ status = t("settings.apiKeys.decisionApi.downloading");+ } else if (settings.loadingModel === settings.model) {+ tone = "pending";+ status = t("settings.apiKeys.decisionApi.loading");+ } else if (settings.loadedModel === settings.model) {+ tone = "ready";+ status = t("settings.apiKeys.decisionApi.loadedOn", {+ device: deviceLabel(settings.loadedDevice),+ });+ action = "unload";+ } else if (plan === null) {+ tone = "pending";+ status = t("settings.apiKeys.decisionApi.checking");+ } else if (!plan.cached && plan.error) {+ tone = "error";+ status = plan.error;+ } else if (!plan.cached) {+ status = t("settings.apiKeys.decisionApi.notDownloaded", {+ size: formatBytes(sizeBytes),+ });+ action = "download";+ } else {+ tone = "ready";+ status = t("settings.apiKeys.decisionApi.downloaded");+ }++ return (+ <section+ data-settings-label={t("settings.apiKeys.decisionApi.title")}+ className="overflow-hidden rounded-lg border border-border/70"+ >+ {header}++ <div className="border-t border-border/60 px-4 py-1">+ <SettingsRow+ label={t("settings.apiKeys.decisionApi.enable")}+ description={+ settings.enabledLocked+ ? t("settings.apiKeys.decisionApi.lockedByEnv", {+ name: ENV_DISABLE,+ })+ : t("settings.apiKeys.decisionApi.enableDescription")+ }+ alignTop={true}+ >+ <Switch+ checked={enabled}+ disabled={busy || settings.enabledLocked}+ onCheckedChange={(on) => void apply({ enabled: on }, on)}+ aria-label={t("settings.apiKeys.decisionApi.enable")}+ />+ </SettingsRow>++ <SettingsRow+ label={t("settings.apiKeys.decisionApi.model")}+ hint={+ settings.modelLocked+ ? t("settings.apiKeys.decisionApi.lockedByEnv", {+ name: ENV_MODEL,+ })+ : undefined+ }+ description={+ enabled && status ? (+ <span+ className={cn(+ "flex min-w-0 items-center gap-2",+ tone === "error" && "text-destructive",+ )}+ >+ {tone ? (+ <span+ className={cn(+ "size-1.5 shrink-0 rounded-full",+ tone === "pending"+ ? "animate-pulse bg-current"+ : tone === "ready"+ ? "bg-emerald-500"+ : "bg-destructive",+ )}+ />+ ) : null}+ <span className="truncate" title={status}>+ {status}+ </span>+ </span>+ ) : undefined+ }+ className="max-[420px]:flex-col max-[420px]:items-stretch max-[420px]:gap-3"+ >+ <div className="flex items-center gap-2 max-[420px]:w-full">+ {action === "download" ? (+ <Button+ variant="outline"+ size="sm"+ className="h-7 shrink-0 px-2.5 text-xs"+ disabled={busy || downloading}+ onClick={() => plan && void startDownload(plan)}+ >+ {downloading ? <Spinner className="mr-1.5" /> : null}+ {t("settings.apiKeys.decisionApi.download")}+ </Button>+ ) : action === "unload" ? (+ <Button+ variant="outline"+ size="sm"+ className="h-7 shrink-0 px-2.5 text-xs"+ disabled={busy}+ onClick={() => void unload()}+ >+ {t("settings.apiKeys.decisionApi.unload")}+ </Button>+ ) : null}+ {knownModel ? (+ <Select+ value={settings.model}+ disabled={busy || settings.modelLocked}+ onValueChange={(name) => void apply({ model: name }, true)}+ >+ <SelectTrigger+ className="w-48 max-[420px]:flex-1"+ aria-label={t("settings.apiKeys.decisionApi.model")}+ >+ <SelectValue>{modelLabel(settings.model)}</SelectValue>+ </SelectTrigger>+ <SelectContent>+ {settings.models.map((option) => (+ <SelectItem key={option.name} value={option.name}>+ <span className="flex items-center gap-2">+ {modelLabel(option.name)}+ <span className="text-ui-10 tabular-nums text-muted-foreground">+ {formatBytes(option.downloadBytes)}+ </span>+ {option.name === RECOMMENDED_MODEL ? (+ <span className="rounded-full bg-emerald-500/12 px-1.5 py-px text-ui-9 font-medium text-emerald-600 dark:text-emerald-400">+ {t("settings.apiKeys.decisionApi.recommended")}+ </span>+ ) : null}+ </span>+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ ) : (+ <span className="font-mono text-xs text-foreground">+ {settings.model}+ </span>+ )}+ </div>+ </SettingsRow>++ <SettingsRow+ label={t("settings.apiKeys.decisionApi.device")}+ description={+ settings.deviceLocked+ ? t("settings.apiKeys.decisionApi.lockedByEnv", {+ name: ENV_DEVICE,+ })+ : t("settings.apiKeys.decisionApi.deviceDescription")+ }+ >+ <Select+ value={settings.device}+ disabled={busy || settings.deviceLocked}+ onValueChange={(device) =>+ void apply({ device: device as SystemOneDevice }, false)+ }+ >+ <SelectTrigger+ className="w-36"+ aria-label={t("settings.apiKeys.decisionApi.device")}+ >+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="cpu">+ {t("settings.apiKeys.decisionApi.deviceCpu")}+ </SelectItem>+ <SelectItem value="gpu" disabled={!settings.gpuAvailable}>+ {t("settings.apiKeys.decisionApi.deviceGpu")}+ </SelectItem>+ </SelectContent>+ </Select>+ </SettingsRow>+ </div>+ </section>+ );+}diff --git a/studio/frontend/src/features/settings/settings-search.ts b/studio/frontend/src/features/settings/settings-search.tsindex 62baed9a124..89e2db11101 100644--- a/studio/frontend/src/features/settings/settings-search.ts+++ b/studio/frontend/src/features/settings/settings-search.ts@@ -150,6 +150,7 @@ export const SETTINGS_SEARCH_INDEX: Record<SettingsTab, TranslationKey[]> = {"settings.apiKeys.title","settings.apiKeys.description","settings.apiKeys.accessTokens",+ "settings.apiKeys.decisionApi.title",],// The two cards label themselves in English in every locale, so keys naming them// would never match their own anchor. The header carries both entries instead.diff --git a/studio/frontend/src/features/settings/tabs/api-keys-tab.tsx b/studio/frontend/src/features/settings/tabs/api-keys-tab.tsxindex a2ee4aad922..01c2cfd6d3c 100644--- a/studio/frontend/src/features/settings/tabs/api-keys-tab.tsx+++ b/studio/frontend/src/features/settings/tabs/api-keys-tab.tsx@@ -21,6 +21,7 @@ import type {} from "../api/keyless-api-access";import { ApiKeyRow } from "../components/api-key-row";import { CreateKeyForm } from "../components/create-key-form";+import { DecisionApiSection } from "../components/decision-api-section";import { KeyRevealCard } from "../components/key-reveal-card";import { KeylessApiAccessSection } from "../components/keyless-api-access-section";import { LanAccessSection } from "../components/lan-access-section";@@ -204,6 +205,8 @@ export function ApiKeysTab() {keylessExposure={keyless.exposure}/>+ {isOwner ? <DecisionApiSection /> : null}+<Dialogopen={revokeTarget !== null}onOpenChange={(o) => !o && setRevokeTarget(null)}diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.tsindex c34ff9ccdb0..caead82dbb9 100644--- a/studio/frontend/src/i18n/locales/ar.ts+++ b/studio/frontend/src/i18n/locales/ar.ts@@ -2014,6 +2014,35 @@ export const ar = {"ستفقد التطبيقات التي تستخدم هذا التوكن الوصول فورًا. لا يمكن التراجع عن هذا الإجراء.",revokeAction: 'إلغاء "{name}"',revoking: "جارٍ الإلغاء...",+ decisionApi: {+ title: "واجهة القرارات البرمجية",+ description: "أجب عن أسئلة نعم/لا والاختيار من متعدد والتقييم حول النص باستخدام نموذج Laya محلي. يعمل مع حزمة TypeSafe.",+ enable: "خدمة الطلبات",+ enableDescription: "يقدّم /v1/systemone. عند التشغيل يُنزَّل النموذج.",+ lockedByEnv: "مضبوط بواسطة {name}.",+ model: "النموذج",+ modelMultilingual: "متعدد اللغات",+ modelEnglish: "إنجليزي",+ modelTypedDecisions: "قرارات مُصنَّفة",+ recommended: "موصى به",+ device: "التشغيل على",+ deviceDescription: "وحدة GPU أسرع في الإجابة، لكنها تحتفظ بذاكرتها محجوزة حتى إعادة التشغيل.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "جارٍ التحقق…",+ notDownloaded: "غير مُنزَّل · {size}",+ downloading: "جارٍ التنزيل…",+ downloaded: "مُنزَّل · يُحمَّل عند أول طلب",+ installing: "جارٍ التثبيت…",+ loading: "جارٍ التحميل…",+ loadedOn: "مُحمَّل على {device}",+ download: "تنزيل",+ unload: "إلغاء التحميل",+ downloadBusy: "يجري بالفعل تنزيل نموذج لواجهة القرارات.",+ downloadFailed: "تعذّر بدء التنزيل.",+ saveFailed: "تعذّر حفظ إعداد واجهة القرارات.",+ loadError: "تعذّر تحميل إعدادات واجهة القرارات.",+ },usageNoModel:"حمّل نموذجًا أو نزّله لعرض أمثلة قابلة للتشغيل. لا يوجد في هذا الخادم أي نموذج يمكن استخدامه في الأمثلة بعد.",},diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.tsindex 65e36c5c77b..3c245b803df 100644--- a/studio/frontend/src/i18n/locales/de.ts+++ b/studio/frontend/src/i18n/locales/de.ts@@ -2058,6 +2058,35 @@ export const de = {"Apps, die dieses Token verwenden, verlieren sofort den Zugriff. Dies kann nicht rückgängig gemacht werden.",revokeAction: "„{name}“ widerrufen",revoking: "Wird widerrufen...",+ decisionApi: {+ title: "Entscheidungs-API",+ description: "Beantworte Ja/Nein-, Auswahl- und Bewertungsfragen zu Text mit einem lokalen Laya-Modell. Funktioniert mit dem TypeSafe-SDK.",+ enable: "Anfragen beantworten",+ enableDescription: "Stellt /v1/systemone bereit. Beim Einschalten wird das Modell heruntergeladen.",+ lockedByEnv: "Festgelegt durch {name}.",+ model: "Modell",+ modelMultilingual: "Multilingual",+ modelEnglish: "Englisch",+ modelTypedDecisions: "Typisierte Entscheidungen",+ recommended: "Empfohlen",+ device: "Ausführen auf",+ deviceDescription: "Die GPU antwortet schneller, hält ihren Speicher aber bis zum Neustart reserviert.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "Wird geprüft…",+ notDownloaded: "Nicht heruntergeladen · {size}",+ downloading: "Wird heruntergeladen…",+ downloaded: "Heruntergeladen · wird bei der ersten Anfrage geladen",+ installing: "Wird installiert…",+ loading: "Wird geladen…",+ loadedOn: "Geladen auf {device}",+ download: "Herunterladen",+ unload: "Entladen",+ downloadBusy: "Ein Modell der Entscheidungs-API wird bereits heruntergeladen.",+ downloadFailed: "Der Download konnte nicht gestartet werden.",+ saveFailed: "Die Einstellung der Entscheidungs-API konnte nicht gespeichert werden.",+ loadError: "Die Einstellungen der Entscheidungs-API konnten nicht geladen werden.",+ },usageNoModel:"Laden Sie ein Modell oder laden Sie eines herunter, um ausführbare Beispiele zu sehen. Dieser Server kennt noch kein Modell, das in den Beispielen verwendet werden könnte.",},diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.tsindex 245d21de060..239d6f38934 100644--- a/studio/frontend/src/i18n/locales/en.ts+++ b/studio/frontend/src/i18n/locales/en.ts@@ -2008,6 +2008,35 @@ export const en = {"Apps using this token immediately lose access. This cannot be undone.",revokeAction: 'Revoke "{name}"',revoking: "Revoking...",+ decisionApi: {+ title: "Decision API",+ description: "Answer yes/no, multiple choice and score questions about text with a local Laya model. Works with the TypeSafe SDK.",+ enable: "Serve requests",+ enableDescription: "Serves /v1/systemone. Turning it on downloads the model.",+ lockedByEnv: "Set by {name}.",+ model: "Model",+ modelMultilingual: "Multilingual",+ modelEnglish: "English",+ modelTypedDecisions: "Typed decisions",+ recommended: "Recommended",+ device: "Run on",+ deviceDescription: "GPU answers faster, but keeps its memory reserved until restart.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "Checking…",+ notDownloaded: "Not downloaded · {size}",+ downloading: "Downloading…",+ downloaded: "Downloaded · loads on first request",+ installing: "Installing…",+ loading: "Loading…",+ loadedOn: "Loaded on {device}",+ download: "Download",+ unload: "Unload",+ downloadBusy: "A Decision API model is already downloading.",+ downloadFailed: "Couldn't start the download.",+ saveFailed: "Couldn't save the Decision API setting.",+ loadError: "Couldn't load Decision API settings.",+ },},about: {title: "About",diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.tsindex a2057669052..412dcf7242b 100644--- a/studio/frontend/src/i18n/locales/es.ts+++ b/studio/frontend/src/i18n/locales/es.ts@@ -2049,6 +2049,35 @@ export const es = {"Las apps que usan este token pierden el acceso de inmediato. Esto no se puede deshacer.",revokeAction: 'Revocar "{name}"',revoking: "Revocando...",+ decisionApi: {+ title: "API de decisiones",+ description: "Responde preguntas de sí/no, de opción múltiple y de puntuación sobre texto con un modelo Laya local. Funciona con el SDK de TypeSafe.",+ enable: "Atender solicitudes",+ enableDescription: "Atiende /v1/systemone. Al activarlo se descarga el modelo.",+ lockedByEnv: "Definido por {name}.",+ model: "Modelo",+ modelMultilingual: "Multilingüe",+ modelEnglish: "Inglés",+ modelTypedDecisions: "Decisiones tipadas",+ recommended: "Recomendado",+ device: "Ejecutar en",+ deviceDescription: "La GPU responde más rápido, pero mantiene su memoria reservada hasta reiniciar.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "Comprobando…",+ notDownloaded: "No descargado · {size}",+ downloading: "Descargando…",+ downloaded: "Descargado · se carga con la primera solicitud",+ installing: "Instalando…",+ loading: "Cargando…",+ loadedOn: "Cargado en {device}",+ download: "Descargar",+ unload: "Descargar de memoria",+ downloadBusy: "Ya se está descargando un modelo de la API de decisiones.",+ downloadFailed: "No se pudo iniciar la descarga.",+ saveFailed: "No se pudo guardar el ajuste de la API de decisiones.",+ loadError: "No se pudieron cargar los ajustes de la API de decisiones.",+ },usageNoModel:"Carga o descarga un modelo para ver ejemplos ejecutables. Este servidor todavía no tiene ningún modelo que indicar.",},diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.tsindex f84af6f9a67..929ab782ae1 100644--- a/studio/frontend/src/i18n/locales/fr.ts+++ b/studio/frontend/src/i18n/locales/fr.ts@@ -2059,6 +2059,35 @@ export const fr = {"Les applications utilisant ce jeton perdent immédiatement l’accès. Cette action est irréversible.",revokeAction: 'Révoquer "{name}"',revoking: "Révocation...",+ decisionApi: {+ title: "API de décision",+ description: "Répondez à des questions oui/non, à choix multiples et à score sur du texte avec un modèle Laya local. Compatible avec le SDK TypeSafe.",+ enable: "Répondre aux requêtes",+ enableDescription: "Sert /v1/systemone. L'activer télécharge le modèle.",+ lockedByEnv: "Défini par {name}.",+ model: "Modèle",+ modelMultilingual: "Multilingue",+ modelEnglish: "Anglais",+ modelTypedDecisions: "Décisions typées",+ recommended: "Recommandé",+ device: "Exécuter sur",+ deviceDescription: "Le GPU répond plus vite, mais garde sa mémoire réservée jusqu'au redémarrage.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "Vérification…",+ notDownloaded: "Non téléchargé · {size}",+ downloading: "Téléchargement…",+ downloaded: "Téléchargé · chargé à la première requête",+ installing: "Installation…",+ loading: "Chargement…",+ loadedOn: "Chargé sur {device}",+ download: "Télécharger",+ unload: "Décharger",+ downloadBusy: "Un modèle de l'API de décision est déjà en cours de téléchargement.",+ downloadFailed: "Impossible de lancer le téléchargement.",+ saveFailed: "Impossible d'enregistrer le réglage de l'API de décision.",+ loadError: "Impossible de charger les réglages de l'API de décision.",+ },usageNoModel:"Chargez ou téléchargez un modèle pour voir des exemples exécutables. Aucun modèle n'est encore disponible sur ce serveur pour figurer dans les exemples.",},diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.tsindex e23272c892e..8571978a11c 100644--- a/studio/frontend/src/i18n/locales/hi.ts+++ b/studio/frontend/src/i18n/locales/hi.ts@@ -2020,6 +2020,35 @@ export const hi = {"इस टोकन का उपयोग करने वाले ऐप तुरंत एक्सेस खो देंगे। इसे पूर्ववत नहीं किया जा सकता।",revokeAction: '"{name}" रद्द करें',revoking: "रद्द किया जा रहा है...",+ decisionApi: {+ title: "निर्णय API",+ description: "लोकल Laya मॉडल से टेक्स्ट पर हाँ/नहीं, बहुविकल्पी और स्कोर वाले सवालों के जवाब दें। TypeSafe SDK के साथ काम करता है।",+ enable: "अनुरोधों का जवाब दें",+ enableDescription: "/v1/systemone चलाता है। चालू करने पर मॉडल डाउनलोड होता है।",+ lockedByEnv: "{name} द्वारा सेट।",+ model: "मॉडल",+ modelMultilingual: "बहुभाषी",+ modelEnglish: "अंग्रेज़ी",+ modelTypedDecisions: "टाइप्ड निर्णय",+ recommended: "सुझाया गया",+ device: "यहाँ चलाएँ",+ deviceDescription: "GPU तेज़ जवाब देता है, लेकिन रीस्टार्ट होने तक अपनी मेमोरी आरक्षित रखता है।",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "जाँच हो रही है…",+ notDownloaded: "डाउनलोड नहीं हुआ · {size}",+ downloading: "डाउनलोड हो रहा है…",+ downloaded: "डाउनलोड हो गया · पहले अनुरोध पर लोड होगा",+ installing: "इंस्टॉल हो रहा है…",+ loading: "लोड हो रहा है…",+ loadedOn: "{device} पर लोड हुआ",+ download: "डाउनलोड करें",+ unload: "अनलोड करें",+ downloadBusy: "निर्णय API का एक मॉडल पहले से डाउनलोड हो रहा है।",+ downloadFailed: "डाउनलोड शुरू नहीं हो सका।",+ saveFailed: "निर्णय API की सेटिंग सेव नहीं हो सकी।",+ loadError: "निर्णय API की सेटिंग्स लोड नहीं हो सकीं।",+ },usageNoModel:"चलाने योग्य उदाहरण देखने के लिए कोई मॉडल लोड या डाउनलोड करें। इस सर्वर के पास अभी बताने को कोई मॉडल नहीं है।",},diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.tsindex b196087ec19..1e406cdead7 100644--- a/studio/frontend/src/i18n/locales/it.ts+++ b/studio/frontend/src/i18n/locales/it.ts@@ -2024,6 +2024,35 @@ export const it = {"Le app che usano questo token perdono immediatamente l'accesso. L'operazione è irreversibile.",revokeAction: "Revoca «{name}»",revoking: "Revoca in corso...",+ decisionApi: {+ title: "API decisionale",+ description: "Rispondi a domande sì/no, a scelta multipla e a punteggio sul testo con un modello Laya locale. Funziona con l'SDK di TypeSafe.",+ enable: "Rispondi alle richieste",+ enableDescription: "Gestisce /v1/systemone. Attivandolo si scarica il modello.",+ lockedByEnv: "Impostato da {name}.",+ model: "Modello",+ modelMultilingual: "Multilingue",+ modelEnglish: "Inglese",+ modelTypedDecisions: "Decisioni tipizzate",+ recommended: "Consigliato",+ device: "Esegui su",+ deviceDescription: "La GPU risponde più velocemente, ma tiene la memoria riservata fino al riavvio.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "Verifica in corso…",+ notDownloaded: "Non scaricato · {size}",+ downloading: "Download in corso…",+ downloaded: "Scaricato · si carica alla prima richiesta",+ installing: "Installazione…",+ loading: "Caricamento…",+ loadedOn: "Caricato su {device}",+ download: "Scarica",+ unload: "Rimuovi dalla memoria",+ downloadBusy: "Un modello dell'API decisionale è già in download.",+ downloadFailed: "Impossibile avviare il download.",+ saveFailed: "Impossibile salvare l'impostazione dell'API decisionale.",+ loadError: "Impossibile caricare le impostazioni dell'API decisionale.",+ },},about: {title: "Informazioni",diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.tsindex 312ea7d6cdc..0a0a7dc1a20 100644--- a/studio/frontend/src/i18n/locales/ja.ts+++ b/studio/frontend/src/i18n/locales/ja.ts@@ -1984,6 +1984,35 @@ export const ja = {revokeDescription: "このトークンを使用しているアプリはすぐにアクセスできなくなります。この操作は取り消せません。",revokeAction: '"{name}" を失効',revoking: "失効中...",+ decisionApi: {+ title: "判定 API",+ description: "ローカルの Laya モデルで、テキストに関するはい/いいえ・選択式・スコアの質問に答えます。TypeSafe SDK で使えます。",+ enable: "リクエストに応答",+ enableDescription: "/v1/systemone を提供します。オンにするとモデルをダウンロードします。",+ lockedByEnv: "{name} で設定されています。",+ model: "モデル",+ modelMultilingual: "多言語",+ modelEnglish: "英語",+ modelTypedDecisions: "型付き判定",+ recommended: "推奨",+ device: "実行先",+ deviceDescription: "GPU の方が高速ですが、再起動するまでメモリを確保し続けます。",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "確認中…",+ notDownloaded: "未ダウンロード · {size}",+ downloading: "ダウンロード中…",+ downloaded: "ダウンロード済み · 最初のリクエストで読み込み",+ installing: "インストール中…",+ loading: "読み込み中…",+ loadedOn: "{device} に読み込み済み",+ download: "ダウンロード",+ unload: "アンロード",+ downloadBusy: "判定 API のモデルはすでにダウンロード中です。",+ downloadFailed: "ダウンロードを開始できませんでした。",+ saveFailed: "判定 API の設定を保存できませんでした。",+ loadError: "判定 API の設定を読み込めませんでした。",+ },usageNoModel:"モデルを読み込むかダウンロードすると、実行できる例が表示されます。このサーバーにはまだ指定できるモデルがありません。",},diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.tsindex d344615c1d9..be348d1976b 100644--- a/studio/frontend/src/i18n/locales/ko.ts+++ b/studio/frontend/src/i18n/locales/ko.ts@@ -2007,6 +2007,35 @@ export const ko = {"이 토큰을 사용하는 앱은 즉시 접근 권한을 잃습니다. 이 작업은 되돌릴 수 없습니다.",revokeAction: '"{name}" 폐기',revoking: "폐기 중...",+ decisionApi: {+ title: "판단 API",+ description: "로컬 Laya 모델로 텍스트에 대한 예/아니오, 객관식, 점수 질문에 답합니다. TypeSafe SDK와 함께 사용할 수 있습니다.",+ enable: "요청 처리",+ enableDescription: "/v1/systemone을 제공합니다. 켜면 모델을 다운로드합니다.",+ lockedByEnv: "{name}(으)로 설정됨.",+ model: "모델",+ modelMultilingual: "다국어",+ modelEnglish: "영어",+ modelTypedDecisions: "유형별 판단",+ recommended: "추천",+ device: "실행 위치",+ deviceDescription: "GPU가 더 빠르지만 다시 시작할 때까지 메모리를 계속 차지합니다.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "확인 중…",+ notDownloaded: "다운로드 안 됨 · {size}",+ downloading: "다운로드 중…",+ downloaded: "다운로드됨 · 첫 요청 때 로드",+ installing: "설치 중…",+ loading: "로드 중…",+ loadedOn: "{device}에 로드됨",+ download: "다운로드",+ unload: "언로드",+ downloadBusy: "판단 API 모델이 이미 다운로드 중입니다.",+ downloadFailed: "다운로드를 시작하지 못했습니다.",+ saveFailed: "판단 API 설정을 저장하지 못했습니다.",+ loadError: "판단 API 설정을 불러오지 못했습니다.",+ },usageNoModel:"모델을 로드하거나 다운로드하면 실행 가능한 예제가 표시됩니다. 이 서버에는 아직 지정할 모델이 없습니다.",},diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.tsindex 4dff21112d5..ca401d84521 100644--- a/studio/frontend/src/i18n/locales/pt-br.ts+++ b/studio/frontend/src/i18n/locales/pt-br.ts@@ -2030,6 +2030,35 @@ export const ptBR = {"Aplicativos que usam este token perderão o acesso imediatamente. Esta ação não pode ser desfeita.",revokeAction: 'Revogar "{name}"',revoking: "Revogando...",+ decisionApi: {+ title: "API de decisões",+ description: "Responda a perguntas de sim/não, múltipla escolha e pontuação sobre texto com um modelo Laya local. Funciona com o SDK da TypeSafe.",+ enable: "Atender solicitações",+ enableDescription: "Atende /v1/systemone. Ao ativar, o modelo é baixado.",+ lockedByEnv: "Definido por {name}.",+ model: "Modelo",+ modelMultilingual: "Multilíngue",+ modelEnglish: "Inglês",+ modelTypedDecisions: "Decisões tipadas",+ recommended: "Recomendado",+ device: "Executar em",+ deviceDescription: "A GPU responde mais rápido, mas mantém a memória reservada até reiniciar.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "Verificando…",+ notDownloaded: "Não baixado · {size}",+ downloading: "Baixando…",+ downloaded: "Baixado · carrega na primeira solicitação",+ installing: "Instalando…",+ loading: "Carregando…",+ loadedOn: "Carregado em {device}",+ download: "Baixar",+ unload: "Descarregar",+ downloadBusy: "Um modelo da API de decisões já está sendo baixado.",+ downloadFailed: "Não foi possível iniciar o download.",+ saveFailed: "Não foi possível salvar a configuração da API de decisões.",+ loadError: "Não foi possível carregar as configurações da API de decisões.",+ },usageNoModel:"Carregue ou baixe um modelo para ver exemplos executáveis. Este servidor ainda não tem nenhum modelo para indicar.",},diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.tsindex 12ddbe049d1..3084e6dbb1f 100644--- a/studio/frontend/src/i18n/locales/ru.ts+++ b/studio/frontend/src/i18n/locales/ru.ts@@ -2026,6 +2026,35 @@ export const ru = {"Приложения, использующие этот токен, немедленно потеряют доступ. Это действие нельзя отменить.",revokeAction: 'Отозвать «{name}»',revoking: "Отзыв...",+ decisionApi: {+ title: "API решений",+ description: "Отвечайте на вопросы «да/нет», с выбором и с оценкой о тексте с помощью локальной модели Laya. Работает с SDK TypeSafe.",+ enable: "Обрабатывать запросы",+ enableDescription: "Обслуживает /v1/systemone. При включении модель скачивается.",+ lockedByEnv: "Задано переменной {name}.",+ model: "Модель",+ modelMultilingual: "Многоязычная",+ modelEnglish: "Английская",+ modelTypedDecisions: "Типизированные решения",+ recommended: "Рекомендуется",+ device: "Запускать на",+ deviceDescription: "GPU отвечает быстрее, но держит память зарезервированной до перезапуска.",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "Проверка…",+ notDownloaded: "Не скачана · {size}",+ downloading: "Скачивание…",+ downloaded: "Скачана · загрузится при первом запросе",+ installing: "Установка…",+ loading: "Загрузка…",+ loadedOn: "Загружена на {device}",+ download: "Скачать",+ unload: "Выгрузить",+ downloadBusy: "Модель API решений уже скачивается.",+ downloadFailed: "Не удалось начать скачивание.",+ saveFailed: "Не удалось сохранить настройку API решений.",+ loadError: "Не удалось загрузить настройки API решений.",+ },usageNoModel:"Загрузите или скачайте модель, чтобы увидеть готовые к запуску примеры. На этом сервере пока нет модели, которую можно подставить в примеры.",},diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.tsindex 94f0aee6202..44a2fc64160 100644--- a/studio/frontend/src/i18n/locales/zh-CN.ts+++ b/studio/frontend/src/i18n/locales/zh-CN.ts@@ -1963,6 +1963,35 @@ export const zhCN = {"使用此 token 的应用会立即失去访问权限。此操作无法撤销。",revokeAction: "撤销“{name}”",revoking: "撤销中...",+ decisionApi: {+ title: "决策 API",+ description: "使用本地 Laya 模型回答关于文本的是/否、选择和评分问题。可配合 TypeSafe SDK 使用。",+ enable: "处理请求",+ enableDescription: "提供 /v1/systemone。开启后会下载模型。",+ lockedByEnv: "由 {name} 设置。",+ model: "模型",+ modelMultilingual: "多语言",+ modelEnglish: "英语",+ modelTypedDecisions: "类型化决策",+ recommended: "推荐",+ device: "运行于",+ deviceDescription: "GPU 响应更快,但在重启前会一直占用显存。",+ deviceCpu: "CPU",+ deviceGpu: "GPU",+ checking: "正在检查…",+ notDownloaded: "未下载 · {size}",+ downloading: "正在下载…",+ downloaded: "已下载 · 首次请求时加载",+ installing: "正在安装…",+ loading: "正在加载…",+ loadedOn: "已加载到 {device}",+ download: "下载",+ unload: "卸载",+ downloadBusy: "已有一个决策 API 模型正在下载。",+ downloadFailed: "无法开始下载。",+ saveFailed: "无法保存决策 API 设置。",+ loadError: "无法加载决策 API 设置。",+ },usageNoModel:"加载或下载一个模型后即可看到可运行的示例。此服务器目前还没有可指定的模型。",},diff --git a/studio/frontend/tests/decision-api-section.test.ts b/studio/frontend/tests/decision-api-section.test.tsnew file mode 100644index 00000000000..039560e0a58--- /dev/null+++ b/studio/frontend/tests/decision-api-section.test.ts@@ -0,0 +1,117 @@+// 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 { readFileSync } from "node:fs";+import test from "node:test";+import { fileURLToPath } from "node:url";++import { en } from "../src/i18n/locales/en.ts";+import { SETTINGS_SEARCH_INDEX } from "../src/features/settings/settings-search.ts";++function read(path: string): string {+ return readFileSync(fileURLToPath(new URL(path, import.meta.url)), "utf-8");+}++// The section reaches the hub and auth barrels, which cannot be imported here, so this asserts on source.+const SECTION = read(+ "../src/features/settings/components/decision-api-section.tsx",+);+const API = read("../src/features/settings/api/systemone.ts");+const API_TAB = read("../src/features/settings/tabs/api-keys-tab.tsx");++test("the API tab renders the section and search finds it", () => {+ assert.match(API_TAB, /<DecisionApiSection \/>/);+ assert.ok(+ SETTINGS_SEARCH_INDEX["api-keys"].includes(+ "settings.apiKeys.decisionApi.title",+ ),+ );+ assert.match(+ SECTION,+ /data-settings-label=\{t\("settings.apiKeys.decisionApi.title"\)\}/,+ );+});++test("only the owner sees it, below the chat usage examples", () => {+ assert.match(+ API_TAB,+ /<UsageExamples[\s\S]*?\/>\s*(?:\{\/\*[^*]*\*\/\}\s*)?\{isOwner \? <DecisionApiSection \/> : null\}/,+ );+ assert.doesNotMatch(SECTION, /useIsAccountOwner/);+});++test("turning it on or picking a model downloads, the device does not", () => {+ assert.match(SECTION, /apply\(\{ enabled: on \}, on\)/);+ assert.match(SECTION, /apply\(\{ model: name \}, true\)/);+ assert.match(+ SECTION,+ /apply\(\{ device: device as SystemOneDevice \}, false\)/,+ );+});++test("the download goes through the manager with the exact files and one scope", () => {+ assert.match(SECTION, /const DOWNLOAD_SCOPE = "systemone";/);+ assert.match(SECTION, /scopeId: DOWNLOAD_SCOPE,/);+ assert.match(SECTION, /files: next\.files,/);+ assert.match(SECTION, /expectedBytes: next\.sizeBytes,/);+});++test("environment overrides lock their control and say why", () => {+ for (const [flag, env] of [+ ["enabledLocked", "ENV_DISABLE"],+ ["modelLocked", "ENV_MODEL"],+ ["deviceLocked", "ENV_DEVICE"],+ ]) {+ assert.match(+ SECTION,+ new RegExp(`disabled=\\{busy \\|\\| settings\\.${flag}\\}`),+ );+ assert.match(SECTION, new RegExp(`name: ${env}`));+ }+ assert.equal(en.settings.apiKeys.decisionApi.lockedByEnv, "Set by {name}.");+});++test("GPU is offered only where the backend found one", () => {+ assert.match(+ SECTION,+ /<SelectItem value="gpu" disabled=\{!settings\.gpuAvailable\}>/,+ );+});++test("the copy stays plain", () => {+ const copy = en.settings.apiKeys.decisionApi;+ assert.equal("experimental" in copy, false);+ assert.equal("tryIt" in copy, false);+ for (const value of Object.values(copy)) {+ assert.doesNotMatch(value, /[;\u2014]/);+ }+});++test("the client talks to the settings routes and maps the schema", () => {+ assert.match(API, /const SETTINGS_PATH = "\/api\/settings\/systemone";/);+ assert.match(API, /`\$\{SETTINGS_PATH\}\/unload`/);+ assert.match(API, /`\$\{SETTINGS_PATH\}\/resolve`/);+ for (const [camel, snake] of [+ ["enabledLocked", "enabled_locked"],+ ["gpuAvailable", "gpu_available"],+ ["loadedModel", "loaded_model"],+ ["downloadBytes", "download_bytes"],+ ["sizeBytes", "size_bytes"],+ ]) {+ assert.match(API, new RegExp(`${camel}: \\w+\\.${snake}`));+ }+});++test("reports a resident model as loaded only when it is the selected one", () => {+ assert.match(SECTION, /settings\.loadedModel === settings\.model/);+ assert.match(SECTION, /settings\.loadingModel === settings\.model/);+});++test("a failed first load still renders the section with its error", () => {+ assert.match(SECTION, /if \(!settings\) \{\s*return error \?/);+ assert.match(+ SECTION,+ /translate\("settings.apiKeys.decisionApi.loadError"\)/,+ );+});