Skip to content

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.

19.9 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Authenticated API for resumable, server-owned Studio chat generations."""

from __future__ import annotations

import os

import asyncio
import json
import re
import sqlite3
from concurrent.futures import ThreadPoolExecutor
from functools import partial
from typing import Any

from fastapi import APIRouter, Depends, Header, HTTPException, Query, Request
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, ConfigDict, Field, ValidationError

from auth.authentication import get_current_subject
from auth import policy
from state import active_generations, run_subscribers, unattended_runs
from utils.account_context import current_account, current_account_id, run_as
from core.inference.chat_generation_runs import detach_embedded_images
from core.inference.llama_keepwarm import inference_lifecycle_gate
from models.inference import ChatCompletionRequest
from storage import chat_generation_runs_db as db
from utils.api_errors import safe_validation_errors
from utils.current_date_prompt_settings import (
    CURRENT_DATE_TIMEZONE_HEADER,
    CURRENT_DATE_TIMEZONE_OFFSET_HEADER,
)

router = APIRouter()


def _encode_json(value: Any) -> str:
    """The SSE frame encoding; chat_generation_runs_db stores event payloads with the same options."""
    return json.dumps(value, ensure_ascii = False, separators = (",", ":"))


_EVENT_WAIT_EXECUTOR = ThreadPoolExecutor(
    max_workers = 32,
    thread_name_prefix = "chat-generation-events",
)
_SENSITIVE_KEYS = {
    "accesskey",
    "authorization",
    "encryptionkey",
    "password",
    "privatekey",
    "secret",
    "secretkey",
    "signingkey",
    "sshkey",
    "token",
    "apikey",
    "credential",
    "credentials",
    "encryptedapikey",
}
_SENSITIVE_SUFFIXES = (
    "token",
    "secret",
    "password",
    "credential",
    "credentials",
)
_EXTERNAL_ROUTING_FIELDS = {
    "provider_id",
    "provider_type",
    "external_model",
    "encrypted_api_key",
    "provider_base_url",
}
# Attachments the composer sends inline. Durable replay has no representation for them.
_MEDIA_FIELDS = {
    "image_base64",
    "audio_base64",
    "extra_audio_base64",
    "video_base64",
    "mcp_image",
}
_SQLITE_MAX_INTEGER = 9_223_372_036_854_775_807
_ENVELOPE_MAX_DEPTH = 64
_ENVELOPE_MAX_NODES = 20_000
_ENVELOPE_MAX_JSON_CHARS = 1_000_000


class CreateChatGenerationRun(BaseModel):
    model_config = ConfigDict(extra = "forbid")
    runId: str = Field(min_length = 1, max_length = 128, pattern = r"^[A-Za-z0-9_-]+$")
    threadId: str = Field(min_length = 1, max_length = 256)
    userMessageId: str = Field(min_length = 1, max_length = 256)
    assistantMessageId: str = Field(min_length = 1, max_length = 256)
    requestPayload: dict[str, Any]
    # The owner's "stop when I close the tab": cancel the run once no tab has followed it for a
    # while (state/unattended_runs.py) rather than letting it finish in the background.
    stopWhenUnattended: bool = False


def _normalized_key(key: object) -> str:
    return re.sub(r"[^a-z0-9]", "", str(key).casefold())


def _is_sensitive_key(key: object) -> bool:
    normalized = _normalized_key(key)
    return normalized in _SENSITIVE_KEYS or normalized.endswith(_SENSITIVE_SUFFIXES)


def _contains_sensitive_json_key_text(value: str) -> bool:
    for match in re.finditer(r'"((?:\\.|[^"\\])*)"\s*:', value):
        try:
            key = json.loads(f'"{match.group(1)}"')
        except (json.JSONDecodeError, TypeError):
            key = match.group(1)
        if _is_sensitive_key(key):
            return True
    return False


def _contains_sensitive_key(value: object) -> bool:
    stack: list[tuple[object, int]] = [(value, 0)]
    nodes = 0
    decoded_chars = 0
    while stack:
        item, depth = stack.pop()
        nodes += 1
        if depth > _ENVELOPE_MAX_DEPTH or nodes > _ENVELOPE_MAX_NODES:
            return True
        if isinstance(item, dict):
            for key, nested in item.items():
                if _is_sensitive_key(key):
                    return True
                stack.append((nested, depth + 1))
        elif isinstance(item, (list, tuple)):
            stack.extend((nested, depth + 1) for nested in item)
        elif isinstance(item, str):
            candidate = item.lstrip()
            if not candidate.startswith(("{", "[", '"')):
                continue
            decoded_chars += len(candidate)
            if decoded_chars > _ENVELOPE_MAX_JSON_CHARS:
                return True
            try:
                decoded = json.loads(candidate)
            except json.JSONDecodeError:
                if _contains_sensitive_json_key_text(candidate):
                    return True
                continue
            except (MemoryError, RecursionError):
                return True
            stack.append((decoded, depth + 1))
    return False


def _sanitize_request(payload: CreateChatGenerationRun) -> dict[str, Any]:
    raw = dict(payload.requestPayload)
    unknown = set(raw) - set(ChatCompletionRequest.model_fields)
    if unknown:
        raise HTTPException(
            status_code = 400,
            detail = f"Unsupported durable request fields: {', '.join(sorted(unknown))}",
        )
    try:
        request = ChatCompletionRequest.model_validate(raw)
    except ValidationError as exc:
        raise HTTPException(
            status_code = 422,
            detail = safe_validation_errors(exc.errors()),
        ) from exc
    # Without this an unservable part is queued at 202 and fails where the caller cannot see it.
    from routes.inference import (
        _admit_tool_access,
        _messages_have_embedded_image,
        _messages_have_input_audio,
        _reject_unsupported_content_parts,
        _request_has_video,
    )

    _admit_tool_access(request)
    _reject_unsupported_content_parts(request)

    # Message content is user data; scan every other persisted field for credentials.
    durable_config = {
        key: value
        for key, value in raw.items()
        if key != "messages" and key not in _EXTERNAL_ROUTING_FIELDS and value is not None
    }
    message_envelopes = [
        {
            key: value
            for key, value in message.items()
            if key not in {"content", "reasoning_content"}
        }
        for message in raw.get("messages", [])
        if isinstance(message, dict)
    ]
    if _contains_sensitive_key(durable_config) or _contains_sensitive_key(message_envelopes):
        raise HTTPException(status_code = 400, detail = "Credentials cannot be persisted")
    if any(raw.get(field) not in (None, "") for field in _EXTERNAL_ROUTING_FIELDS):
        raise HTTPException(
            status_code = 400,
            detail = "Durable chat runs are available only for local inference",
        )
    # This turn's media stays legacy; images of earlier turns are detached before the request persists.
    latest_user = max(
        (index for index, message in enumerate(request.messages) if message.role == "user"),
        default = 0,
    )
    if (
        any(raw.get(field) not in (None, "") for field in _MEDIA_FIELDS)
        or _messages_have_input_audio(request.messages)
        or _messages_have_embedded_image(request.messages[latest_user:])
        or _request_has_video(request)
    ):
        raise HTTPException(
            status_code = 400,
            detail = "Media chat runs use the legacy streaming path",
        )
    # UNSLOTH_STUDIO_DURABLE_TOOL_TURNS=0 hands the legacy stream the tool policy and checkpoint recall.
    from routes.inference import _checkpoint_recall_may_enable_tools, _effective_enable_tools

    request = request.model_copy(update = {"thread_id": payload.threadId})

    # Default ON (env scoping proved unreliable across launchers); UNSLOTH_STUDIO_DURABLE_TOOL_TURNS=0 restores the legacy refusal.
    _durable_tools = os.environ.get("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "1").strip().lower() in (
        "1",
        "true",
        "yes",
    )
    # The guard must wrap the whole or-chain: `A and B or C` parses as `(A and B) or C`.
    if not _durable_tools and (
        raw.get("tools")
        or request.enable_tools is True
        or bool(request.mcp_enabled)
        or _effective_enable_tools(request) is True
        or _checkpoint_recall_may_enable_tools(request)
    ):
        raise HTTPException(
            status_code = 400,
            detail = "Tool-enabled chat runs use the legacy streaming path",
        )
    if (request.n or 1) != 1:
        raise HTTPException(status_code = 400, detail = "Durable chat runs require n=1")
    sanitized = request.model_dump(mode = "json", exclude_none = True)
    if request.permission_mode == "off" and not request._off_confirm_opt_out:
        # Persisted False would read as an explicit opt-out and skip the no-sandbox confirm gate.
        sanitized.pop("confirm_tool_calls", None)
    for field in _EXTERNAL_ROUTING_FIELDS:
        sanitized.pop(field, None)
    sanitized["stream"] = True
    sanitized["thread_id"] = payload.threadId
    sanitized["cancel_id"] = payload.runId
    sanitized["generation_run_id"] = payload.runId
    return sanitized


def _timezone_headers(request: Request) -> dict[str, str]:
    return {
        name: value
        for name in (CURRENT_DATE_TIMEZONE_HEADER, CURRENT_DATE_TIMEZONE_OFFSET_HEADER)
        if 0 < len(value := request.headers.get(name, "").strip()) <= 64
    }


def _require_run(run_id: str) -> dict[str, Any]:
    run = db.get_run(run_id)
    if run is None:
        raise HTTPException(status_code = 404, detail = "Chat generation run not found")
    return run


def _require_run_exists(run_id: str) -> None:
    """_require_run's 404 without reading or parsing the stored request."""
    if db.get_run_state(run_id) is None:
        raise HTTPException(status_code = 404, detail = "Chat generation run not found")


def cancel_account_run(request: Request, run_id: str, *, supervisor_name: str) -> None:
    """Signal only the caller's registration: another account may legitimately reuse a bare
    cancel ID, so it is never stashed."""
    if policy.installation_has_managed_accounts():
        active_generations.cancel_run(run_id, account_id = current_account_id())
        if supervisor_name == "chat_generation_supervisor":
            return
    supervisor = getattr(request.app.state, supervisor_name, None)
    if supervisor is not None:
        supervisor.cancel(run_id)
    elif supervisor_name == "chat_generation_supervisor":
        from routes.inference import _cancel_by_cancel_id_or_stash
        active_generations.cancel_run(run_id)
        _cancel_by_cancel_id_or_stash(run_id)


def _watch_unattended(request: Request, run_id: str) -> None:
    """Cancel ``run_id`` the way the cancel route does once no tab is following it."""
    account = current_account()

    def settled() -> bool:
        run = run_as(account, db.get_run, run_id)
        return run is None or run["status"] in db.TERMINAL_STATUSES or bool(run["cancelRequested"])

    async def cancel() -> None:
        run = await asyncio.to_thread(run_as, account, db.request_cancel, run_id)
        if (
            run is not None
            and run["status"] in {"cancelling", "cancelled"}
            and (
                getattr(request.app.state, "chat_generation_supervisor", None) is not None
                or policy.installation_has_managed_accounts()
            )
        ):
            # On the loop: the supervisor's cancel may cancel its asyncio task.
            run_as(
                account,
                cancel_account_run,
                request,
                run_id,
                supervisor_name = "chat_generation_supervisor",
            )

    # The id the events stream stamps, under the same account it stamps it with.
    unattended_runs.watch(run_id, current_account_id() or "", settled = settled, cancel = cancel)


def _require_available_supervisor_run_id(run_id: str) -> None:
    """A legacy supervisor keys tasks by bare ID, and start() no-ops on a held id, so a foreign
    active slot must be refused or an admitted owner run would never be scheduled."""
    if policy.installation_has_managed_accounts():
        for entry in active_generations.snapshot():
            if entry["run_id"] == run_id:
                policy.require_account_scope(entry.get("account_id"))


def _event_cursor(after: int | None, last_event_id: str | None) -> int:
    if after is not None and after > _SQLITE_MAX_INTEGER:
        raise HTTPException(status_code = 400, detail = "Event cursor is too large")
    header_after = 0
    if last_event_id:
        if re.fullmatch(r"[0-9]+", last_event_id, flags = re.ASCII) is None:
            raise HTTPException(status_code = 400, detail = "Last-Event-ID must be an integer")
        max_text = str(_SQLITE_MAX_INTEGER)
        normalized = last_event_id.lstrip("0") or "0"
        if len(normalized) > len(max_text) or (
            len(normalized) == len(max_text) and normalized > max_text
        ):
            raise HTTPException(status_code = 400, detail = "Event cursor is too large")
        header_after = int(normalized)
    return max(after or 0, header_after)


@router.post("", status_code = 202)
async def create_chat_generation_run(
    payload: CreateChatGenerationRun,
    request: Request,
    current_subject: str = Depends(get_current_subject),
):
    sanitized = _sanitize_request(payload)
    images = detach_embedded_images(sanitized)
    from routes.inference import _request_used_api_key

    sanitized[db.API_MONITOR_ORIGIN_FIELD] = _request_used_api_key(request)
    if timezone_headers := _timezone_headers(request):
        sanitized[db.TIMEZONE_HEADERS_FIELD] = timezone_headers
    # Serialize with model lifecycle so a run never registers mid-swap.
    async with inference_lifecycle_gate():
        _require_available_supervisor_run_id(payload.runId)
        try:
            run, created = await asyncio.to_thread(
                db.create_run,
                run_id = payload.runId,
                owner_subject = current_subject,
                thread_id = payload.threadId,
                user_message_id = payload.userMessageId,
                assistant_message_id = payload.assistantMessageId,
                request_payload = sanitized,
            )
        except db.ChatGenerationConflictError as exc:
            raise HTTPException(status_code = 409, detail = str(exc)) from exc
        except KeyError as exc:
            raise HTTPException(status_code = 404, detail = "Thread not found") from exc
        except ValueError as exc:
            raise HTTPException(status_code = 400, detail = str(exc)) from exc
        except sqlite3.IntegrityError as exc:
            raise HTTPException(status_code = 409, detail = "Generation run conflicts") from exc
        supervisor = getattr(request.app.state, "chat_generation_supervisor", None)
        if supervisor is not None and run["status"] == "queued":
            supervisor.start(
                run["id"],
                thread_id = run["threadId"],
                model = run["requestPayload"].get("model"),
                images = images,
            )
    if payload.stopWhenUnattended and run["status"] not in db.TERMINAL_STATUSES:
        _watch_unattended(request, run["id"])
    return {**run, "created": created}


@router.get("/active")
def active_chat_generation_runs(
    thread_id: str = Query(alias = "threadId"), current_subject: str = Depends(get_current_subject)
):
    return {"runs": db.list_active(thread_id)}


@router.get("/{run_id}")
def get_chat_generation_run(run_id: str, current_subject: str = Depends(get_current_subject)):
    return _require_run(run_id)


@router.post("/{run_id}/cancel")
def cancel_chat_generation_run(
    run_id: str,
    request: Request,
    current_subject: str = Depends(get_current_subject),
):
    _require_run_exists(run_id)
    run = db.request_cancel(run_id)
    if run is None:
        raise HTTPException(status_code = 404, detail = "Chat generation run not found")
    if run["status"] in {"cancelling", "cancelled"} and (
        getattr(request.app.state, "chat_generation_supervisor", None) is not None
        or policy.installation_has_managed_accounts()
    ):
        cancel_account_run(request, run_id, supervisor_name = "chat_generation_supervisor")
    return run


@router.post("/{run_id}/events")
async def chat_generation_events(
    run_id: str,
    request: Request,
    after: int | None = Query(None, ge = 0, le = _SQLITE_MAX_INTEGER),
    last_event_id: str | None = Header(None, alias = "Last-Event-ID"),
    current_subject: str = Depends(get_current_subject),
):
    _require_run_exists(run_id)
    cursor = _event_cursor(after, last_event_id)
    wait_for_events = db.wait_for_events
    if policy.installation_has_managed_accounts():
        # run_in_executor does not copy ContextVars, unlike asyncio.to_thread.
        wait_for_events = partial(run_as, current_account(), db.wait_for_events)

    # One token per stream so concurrent tabs don't delete each other's heartbeat.
    follower = run_subscribers.new_follower_token()
    follower_account = current_account_id() or ""

    async def stream():
        nonlocal cursor
        loop = asyncio.get_running_loop()
        # A settled run has nothing to replay; don't hold an event-wait worker for it.
        opening = await asyncio.to_thread(db.get_run_state, run_id)
        if opening is None:
            return
        if opening["status"] in db.TERMINAL_STATUSES and cursor >= int(opening["lastEventSeq"]):
            return
        while True:
            # Stamp before the wait so a parked approval's ceiling sees this follower immediately.
            run_subscribers.mark_subscriber_seen(run_id, follower, follower_account)
            events = await loop.run_in_executor(
                _EVENT_WAIT_EXECUTOR,
                wait_for_events,
                run_id,
                cursor,
                15,
                True,
            )
            # Only non-chunk events carry the run, spliced in as already encoded text.
            run_json = None
            if any(event["type"] != "chunk" for event in events):
                frame = await asyncio.to_thread(db.get_run_frame, run_id)
                snapshot, run_json = frame if frame is not None else (None, None)
            else:
                snapshot = await asyncio.to_thread(db.get_run_state, run_id)
            if snapshot is None:
                return
            for event in events:
                cursor = int(event["seq"])
                event_type = event["type"]
                # Spliced, not re-encoded: the stored payload is already this encoder's output, so the
                # bytes match json.dumps of the parsed event dict with the same key order.
                encoded = (
                    f'{{"seq":{cursor},"type":{_encode_json(event_type)},'
                    f'"payload":{event["payloadJson"]},"createdAt":{int(event["createdAt"])}'
                )
                if event_type != "chunk":
                    encoded += f',"run":{run_json}'
                yield f"id: {cursor}\nevent: {event_type}\ndata: {encoded}}}\n\n"
            if snapshot["status"] in db.TERMINAL_STATUSES and cursor >= int(
                snapshot["lastEventSeq"]
            ):
                return
            if await request.is_disconnected():
                return
            if not events:
                # Carries the progress stamp so followers can detect a wedged run; comment framing keeps
                # _SSEDecoder ignoring it.
                yield f": keep-alive {int(snapshot['updatedAt'])}\n\n"

    async def stream_while_attended():
        """Marks this follower as gone, so a cleanly closed tab stops counting as attended at once."""
        try:
            async for frame in stream():
                yield frame
        finally:
            run_subscribers.subscriber_departed(run_id, follower, follower_account)

    return StreamingResponse(
        stream_while_attended(),
        media_type = "text/event-stream",
        headers = {"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
    )