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.

7.7 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

"""Images and Fetch go to the provider; Search and code run locally, never on both sides."""

from __future__ import annotations

import asyncio

from types import SimpleNamespace

import pytest

from core.inference.providers import hosted_only_tools, provider_hosted_tools


def _drive(coro):
    return asyncio.new_event_loop().run_until_complete(coro)


class _FakeExternalClient:
    last: dict = {}

    def __init__(self, **kwargs):
        _FakeExternalClient.last = {"ctor": kwargs}
        self.provider_type = kwargs.get("provider_type")

    def stream_chat_completion(self, **kwargs):
        async def gen():
            yield "data: [DONE]\n\n"

        return gen()

    async def served_chat_template(self, model):
        return None

    async def served_model_reads_images(self, model):
        return None

    async def close(self):
        return None


class _LoopEntered(Exception):
    """stream_with_studio_tools was reached; carries the transport it was given."""


def _request():
    async def is_disconnected():
        return False

    return SimpleNamespace(
        # the confirm gate asks over these frames
        headers = {"X-Unsloth-Events": "1"},
        state = SimpleNamespace(skip_api_monitor = True),
        is_disconnected = is_disconnected,
    )


def _install(
    monkeypatch,
    provider_type: str,
    *,
    base_url = None,
    api_type = None,
):
    from core.inference.providers import get_base_url
    from routes import inference as inf

    monkeypatch.setattr(
        inf.providers_db,
        "get_provider",
        lambda _pid: {
            "id": _pid,
            "provider_type": provider_type,
            "base_url": base_url or get_base_url(provider_type) or "http://127.0.0.1:8080/v1",
            "api_type": api_type or "chat_completions",
            "display_name": "Saved connection",
            "is_enabled": True,
        },
    )
    monkeypatch.setattr(inf, "resolve_provider_api_key_or_400", lambda *a, **k: "k")
    monkeypatch.setattr(inf, "ExternalProviderClient", _FakeExternalClient)

    def _loop_raiser(transport, **_kwargs):
        transport._selected_local_tool_names = [
            tool["function"]["name"] for tool in _kwargs["policy"].tools
        ]
        raise _LoopEntered(transport)

    monkeypatch.setattr(inf, "stream_with_studio_tools", _loop_raiser)
    return inf


def _payload(**overrides):
    from models.inference import ChatCompletionRequest

    base = dict(
        messages = [{"role": "user", "content": "draw me a chart of this"}],
        provider_id = "saved-1",
        external_model = "gpt-5.4",
        stream = True,
        enable_tools = True,
    )
    base.update(overrides)
    return ChatCompletionRequest(**base)


def _loop_transport(
    monkeypatch,
    provider_type: str,
    selection: list[str],
    *,
    base_url: str | None = None,
    api_type: str | None = None,
    **overrides,
):
    """Run the route and return the transport the loop was handed."""
    inf = _install(monkeypatch, provider_type, base_url = base_url, api_type = api_type)

    async def go():
        resp = await inf._proxy_to_external_provider(
            _payload(enabled_tools = selection, **overrides), _request(), current_subject = "t"
        )
        return [chunk async for chunk in resp.body_iterator]

    with pytest.raises(_LoopEntered) as excinfo:
        _drive(go())
    return excinfo.value.args[0]


@pytest.fixture(autouse = True)
def _clean_policy():
    from state.tool_policy import reset_tool_policy

    reset_tool_policy()
    yield
    reset_tool_policy()


@pytest.mark.parametrize(
    "selection, expected",
    [
        (["python", "terminal", "image_generation"], ["image_generation"]),
        (["search_knowledge_base", "image_generation"], ["image_generation"]),
        # web_search is Unsloth's own once the loop runs, so it never rides along
        (["web_search", "python", "image_generation"], ["image_generation"]),
        (["python", "terminal"], []),
        (["web_search"], []),
        (
            ["image_generation", "python", "image_generation"],
            ["image_generation"],
        ),
    ],
)
def test_only_the_hosted_tools_studio_cannot_run_ride_along(selection, expected):
    assert hosted_only_tools("openai", selection) == expected


def test_a_provider_without_that_tool_is_not_offered_it():
    """openai has no web_fetch, so asking for one must not invent it."""
    assert "web_fetch" not in provider_hosted_tools("openai")
    assert hosted_only_tools("openai", ["python", "web_fetch"]) == []
    assert hosted_only_tools("anthropic", ["python", "web_fetch"]) == ["web_fetch"]


@pytest.mark.parametrize("provider_type", ["llama_cpp", "vllm", "ollama", "custom"])
def test_a_self_hosted_server_is_sent_no_hosted_names_at_all(provider_type):
    """These servers declare no hosted tools, and some 400 on unknown names, so send none."""
    assert hosted_only_tools(provider_type, ["python", "image_generation"]) == []


def test_an_absent_or_malformed_selection_is_not_a_crash():
    assert hosted_only_tools("openai", None) == []
    assert hosted_only_tools(None, ["image_generation"]) == []
    assert hosted_only_tools("openai", [None, 3, "image_generation"]) == ["image_generation"]


@pytest.mark.parametrize("provider_type", ["openai", "gemini"])
def test_images_plus_a_studio_tool_still_reaches_the_provider(monkeypatch, provider_type):
    """The regression in one line: Images plus Code took the Unsloth loop, and the
    loop used to withhold every hosted name, so image_generation vanished while
    its toggle stayed on."""
    transport = _loop_transport(
        monkeypatch, provider_type, ["python", "terminal", "image_generation"]
    )
    assert transport._request_kwargs["enabled_tools"] == ["image_generation"]


def test_automatic_rag_does_not_cost_the_user_their_image_tool(monkeypatch):
    """Automatic RAG selects the loop silently, which can quietly drop the user's image tool."""
    transport = _loop_transport(
        monkeypatch,
        "openai",
        ["search_knowledge_base", "image_generation"],
        # no scope means no loop at all, so the auto-RAG turn must carry one
        rag_scope = {"kb_id": "kb-1"},
    )
    assert transport._request_kwargs["enabled_tools"] == ["image_generation"]


def test_the_loop_keeps_its_own_search(monkeypatch):
    """Unsloth's web_search runs locally this turn, so the provider must not run its own too."""
    transport = _loop_transport(monkeypatch, "openai", ["web_search", "python"])
    assert transport._request_kwargs["enabled_tools"] is None


def test_a_self_hosted_loop_is_still_sent_no_tool_flags(monkeypatch):
    transport = _loop_transport(monkeypatch, "llama_cpp", ["web_search", "python"])
    assert transport._request_kwargs["enabled_tools"] is None


@pytest.mark.parametrize(
    "base_url,expected_hosted_tools",
    [
        ("https://api.openai.com/v1", ["code_execution", "image_generation"]),
        (
            "https://resource.services.ai.azure.com/openai/v1",
            ["code_execution", "image_generation"],
        ),
        ("https://gateway.example/v1", None),
        ("https://api.openai.com.attacker.example/v1", None),
    ],
)
def test_custom_mixed_tools_keep_local_search_and_scope_hosted_tools(
    monkeypatch, base_url, expected_hosted_tools
):
    transport = _loop_transport(
        monkeypatch,
        "custom",
        ["web_search", "code_execution", "image_generation"],
        base_url = base_url,
        api_type = "responses",
    )
    assert "web_search" in transport._selected_local_tool_names
    assert transport._request_kwargs["enabled_tools"] == expected_hosted_tools