diff --git a/litellm/proxy/agent_endpoints/agent_registry.py b/litellm/proxy/agent_endpoints/agent_registry.py index 7929f67720d..edebfcd683c 100644 --- a/litellm/proxy/agent_endpoints/agent_registry.py +++ b/litellm/proxy/agent_endpoints/agent_registry.py @@ -456,8 +456,9 @@ class AgentRegistry: return hashlib.sha256(json.dumps(agent_config, sort_keys=True).encode()).hexdigest() def ids_for_agent(self, agent_id: str) -> frozenset[str]: + stable_id: Final = self.stable_agent_id(agent_id) return frozenset( - {agent_id, *(legacy for legacy, stable in self.config_agent_legacy_ids.items() if stable == agent_id)} + {stable_id, *(legacy for legacy, stable in self.config_agent_legacy_ids.items() if stable == stable_id)} ) def stable_agent_id(self, agent_id: str) -> str: diff --git a/litellm/proxy/agent_endpoints/endpoints.py b/litellm/proxy/agent_endpoints/endpoints.py index 093116f2772..3867528b34f 100644 --- a/litellm/proxy/agent_endpoints/endpoints.py +++ b/litellm/proxy/agent_endpoints/endpoints.py @@ -1022,7 +1022,7 @@ async def delete_agent( """ await check_feature_access_for_user(user_api_key_dict, "agents") - from litellm.proxy.proxy_server import prisma_client + from litellm.proxy.proxy_server import prisma_client, proxy_config _check_agent_management_permission(user_api_key_dict) @@ -1040,6 +1040,13 @@ async def delete_agent( if existing_agent is None: raise HTTPException(status_code=404, detail=f"Agent with ID {agent_id} not found in DB.") + await proxy_config.remove_from_list_setting( + section_name="litellm_settings", + key="public_agent_groups", + item=agent_id, + on_written=_set_public_agent_groups, + aliases=AGENT_REGISTRY.ids_for_agent, + ) await AGENT_REGISTRY.delete_agent_from_db(agent_id=agent_id, prisma_client=prisma_client) AGENT_REGISTRY.deregister_agent(agent_name=existing_agent.get("agent_name")) diff --git a/litellm/proxy/management_endpoints/mcp_management_endpoints.py b/litellm/proxy/management_endpoints/mcp_management_endpoints.py index 40769df8ffd..968591a5f10 100644 --- a/litellm/proxy/management_endpoints/mcp_management_endpoints.py +++ b/litellm/proxy/management_endpoints/mcp_management_endpoints.py @@ -2628,6 +2628,14 @@ if MCP_AVAILABLE: }, ) + from litellm.proxy.proxy_server import proxy_config + + await proxy_config.remove_from_list_setting( + section_name="litellm_settings", + key="public_mcp_servers", + item=server_id, + on_written=_set_public_mcp_servers, + ) # try to delete the mcp server mcp_server_record_deleted: Final = await delete_mcp_server(prisma_client, server_id) diff --git a/litellm/proxy/management_endpoints/model_management_endpoints.py b/litellm/proxy/management_endpoints/model_management_endpoints.py index 77664ae5d7f..fc2066f871a 100644 --- a/litellm/proxy/management_endpoints/model_management_endpoints.py +++ b/litellm/proxy/management_endpoints/model_management_endpoints.py @@ -24,7 +24,7 @@ from types import MappingProxyType from typing import TYPE_CHECKING, Annotated, Final, Literal, Protocol, TypeAlias, TypeVar, cast, runtime_checkable from fastapi import APIRouter, Depends, Header, HTTPException, Request, status -from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError, field_validator +from pydantic import BaseModel, ConfigDict, Field, JsonValue, TypeAdapter, ValidationError, field_validator import litellm from litellm._logging import verbose_proxy_logger @@ -3597,6 +3597,14 @@ async def update_public_model_groups( ) +_USEFUL_LINKS: Final = TypeAdapter(Mapping[str, JsonValue]) +_PUBLIC_MODEL_GROUPS_LINKS: Final = TypeAdapter(dict[str, str | dict[str, object]]) + + +def _set_public_model_groups_links(public_model_groups_links: Mapping[str, JsonValue]) -> None: + litellm.public_model_groups_links = _PUBLIC_MODEL_GROUPS_LINKS.validate_python(public_model_groups_links) + + @router.post( "/model_hub/update_useful_links", description="Update useful links", @@ -3623,6 +3631,20 @@ async def update_useful_links( }, ) + if request.previous_useful_links is not None: + stored_useful_links: Final = await proxy_config.apply_mapping_setting_delta( + section_name="litellm_settings", + key="public_model_groups_links", + requested=_USEFUL_LINKS.validate_python(request.useful_links), + previous=_USEFUL_LINKS.validate_python(request.previous_useful_links), + on_written=_set_public_model_groups_links, + ) + return { + "message": "Successfully updated useful links", + "useful_links": dict(stored_useful_links), + "updated_by": user_api_key_dict.user_id, + } + # Load existing config first (this may overwrite in-memory litellm settings # from DB values via _update_config_from_db), so set the in-memory value AFTER config: Final = await proxy_config.get_config() diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index bae76709962..78a15dcac21 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -5516,16 +5516,52 @@ def _with_list_delta( aliases: Callable[[str], frozenset[str]], ) -> Mapping[str, JsonValue]: current: Final = _listed_items(section, key) - removed: Final = frozenset(chain.from_iterable(aliases(item) for item in previous.difference(requested))) + requested_ids: Final = frozenset(chain.from_iterable(aliases(item) for item in requested)) + previous_ids: Final = frozenset(chain.from_iterable(aliases(item) for item in previous)) + removed: Final = previous_ids.difference(requested_ids) kept: Final = tuple(item for item in current if item not in removed) added: Final = tuple( - item for item in dict.fromkeys(requested) if item not in previous and aliases(item).isdisjoint(kept) + item + for item in _first_of_each_alias_set(requested, aliases) + if aliases(item).isdisjoint(previous_ids) and aliases(item).isdisjoint(kept) ) if (*kept, *added) == current: return section return MappingProxyType({**section, key: [*kept, *added]}) +def _first_of_each_alias_set(items: tuple[str, ...], aliases: Callable[[str], frozenset[str]]) -> tuple[str, ...]: + return tuple( + item + for index, item in enumerate(items) + if all(aliases(item).isdisjoint(aliases(earlier)) for earlier in items[:index]) + ) + + +def _with_mapping_delta( + section: Mapping[str, JsonValue], + *, + key: str, + requested: Mapping[str, JsonValue], + previous: Mapping[str, JsonValue], +) -> Mapping[str, JsonValue]: + current: Final = _CONFIG_SECTION_VALUES.validate_python(section.get(key) or {}) + changed: Final = { + name: value for name, value in requested.items() if name not in previous or previous[name] != value + } + removed: Final = frozenset(previous).difference(requested) + kept: Final = {name: value for name, value in current.items() if name not in removed and name not in changed} + merged: Final = {**kept, **changed} + if merged == dict(current): + return section + return MappingProxyType({**section, key: merged}) + + +def _settings_db_client() -> PrismaClient | None: + stores_settings_in_db: Final = general_settings_view().get("store_model_in_db", False) is True or store_model_in_db + return prisma_client if stores_settings_in_db else None + + @dataclass(frozen=True, slots=True) class _SectionRewrite: before: Mapping[str, JsonValue] @@ -6121,11 +6157,11 @@ class ProxyConfig: listed_as: frozenset[str], on_written: Callable[[tuple[str, ...]], None], ) -> tuple[str, ...] | None: - section_rewrite: Final = await self._rewrite_list_setting( + section_rewrite: Final = await self._rewrite_setting( section_name=section_name, key=key, rewrite=lambda section: _with_list_item(section, key=key, item=item, listed_as=listed_as), - on_written=on_written, + on_written=lambda section: on_written(_listed_items(section, key)), ) return _listed_items(section_rewrite.after, key) if section_rewrite.changed else None @@ -6139,29 +6175,60 @@ class ProxyConfig: on_written: Callable[[tuple[str, ...]], None], aliases: Callable[[str], frozenset[str]] = _listed_only_as_itself, ) -> tuple[str, ...]: - section_rewrite: Final = await self._rewrite_list_setting( + section_rewrite: Final = await self._rewrite_setting( section_name=section_name, key=key, rewrite=lambda section: _with_list_delta( section, key=key, requested=tuple(requested), previous=frozenset(previous), aliases=aliases ), - on_written=on_written, + on_written=lambda section: on_written(_listed_items(section, key)), ) return _listed_items(section_rewrite.after, key) - async def _rewrite_list_setting( + async def remove_from_list_setting( + self, + *, + section_name: Section, + key: str, + item: str, + on_written: Callable[[tuple[str, ...]], None], + aliases: Callable[[str], frozenset[str]] = _listed_only_as_itself, + ) -> None: + if _settings_db_client() is not None and self._settings_stores[section_name].owned_by_config(key): + return + await self.apply_list_setting_delta( + section_name=section_name, key=key, requested=(), previous=(item,), on_written=on_written, aliases=aliases + ) + + async def apply_mapping_setting_delta( + self, + *, + section_name: Section, + key: str, + requested: Mapping[str, JsonValue], + previous: Mapping[str, JsonValue], + on_written: Callable[[Mapping[str, JsonValue]], None], + ) -> Mapping[str, JsonValue]: + section_rewrite: Final = await self._rewrite_setting( + section_name=section_name, + key=key, + rewrite=lambda section: _with_mapping_delta(section, key=key, requested=requested, previous=previous), + on_written=lambda section: on_written(_CONFIG_SECTION_VALUES.validate_python(section.get(key) or {})), + ) + return _CONFIG_SECTION_VALUES.validate_python(section_rewrite.after.get(key) or {}) + + async def _rewrite_setting( self, *, section_name: Section, key: str, rewrite: Callable[[Mapping[str, JsonValue]], Mapping[str, JsonValue]], - on_written: Callable[[tuple[str, ...]], None], + on_written: Callable[[Mapping[str, JsonValue]], None], ) -> _SectionRewrite: - if prisma_client is None or not ( - general_settings_view().get("store_model_in_db", False) is True or store_model_in_db - ): + db_client: Final = _settings_db_client() + if db_client is None: file_rewrite: Final = await self._rewrite_file_section(section_name=section_name, rewrite=rewrite) - on_written(_listed_items(file_rewrite.after, key)) + on_written(file_rewrite.after) return file_rewrite store: Final = self._settings_stores[section_name] if store.owned_by_config(key): @@ -6169,11 +6236,9 @@ class ProxyConfig: if rewrite(config_section) != config_section: self._raise_config_owned(section_name=section_name, rejected=(key,), store=store) return _SectionRewrite(before=config_section, after=config_section) - section_rewrite: Final = await _rewrite_config_section( - prisma_client, section_name=section_name, rewrite=rewrite - ) + section_rewrite: Final = await _rewrite_config_section(db_client, section_name=section_name, rewrite=rewrite) store.apply_db_row(section_name, section_rewrite.after) - on_written(_listed_items(section_rewrite.after, key)) + on_written(section_rewrite.after) if section_rewrite.changed: await invalidate_config_param(section_name) return section_rewrite diff --git a/litellm/types/proxy/management_endpoints/model_management_endpoints.py b/litellm/types/proxy/management_endpoints/model_management_endpoints.py index f032bb20c2a..d4cffb4a21d 100644 --- a/litellm/types/proxy/management_endpoints/model_management_endpoints.py +++ b/litellm/types/proxy/management_endpoints/model_management_endpoints.py @@ -48,6 +48,14 @@ class UpdateUsefulLinksRequest(LiteLLMBaseModel): # New format: { "displayName": { "url": "...", "index": 0 } } # Old format: { "displayName": "url" } (for backward compatibility) useful_links: dict[str, str | dict[str, object]] + previous_useful_links: dict[str, str | dict[str, object]] | None = Field( + default=None, + description=( + "The useful links the caller started from. When set, only the links added, changed or removed relative " + "to it change, so links another admin edited meanwhile keep that edit. When omitted, useful_links " + "replaces the stored links." + ), + ) class AutoRouterClassifierDefaultPromptResponse(LiteLLMBaseModel): diff --git a/tests/integration/management/test_ai_hub_publish.py b/tests/integration/management/test_ai_hub_publish.py new file mode 100644 index 00000000000..caeb02fb71e --- /dev/null +++ b/tests/integration/management/test_ai_hub_publish.py @@ -0,0 +1,69 @@ +import uuid +from collections.abc import Callable +from concurrent.futures import ThreadPoolExecutor +from types import MappingProxyType +from typing import Final + +import httpx +import pytest +from integration._support.client import Gateway, Scenario, list_value, object_value + +AGENTS_PER_RACE: Final = 8 +PUBLISHES: Final[MappingProxyType[str, Callable[[Gateway, str], httpx.Response]]] = MappingProxyType({ + "single-agent-route": lambda proxy, agent_id: proxy.request("POST", f"/v1/agents/{agent_id}/make_public"), + "dashboard-modal": lambda proxy, agent_id: proxy.request( + "POST", "/v1/agents/make_public", {"agent_ids": [agent_id], "previous_agent_ids": []} + ), +}) + + +def _agent(gateway: Gateway, scenario: Scenario) -> str: + name: Final = f"hub-publish-{uuid.uuid4().hex}" + card: Final = { + "protocolVersion": "0.3", + "name": name, + "description": "Synthetic hub agent", + "version": "1.0.0", + "url": "http://127.0.0.1:9/", + "capabilities": {"streaming": False}, + "defaultInputModes": ["text"], + "defaultOutputModes": ["text"], + "skills": [], + } + created: Final = gateway.request("POST", "/v1/agents", {"agent_name": name, "agent_card_params": card}) + assert created.status_code == 200, created.text + agent_id: Final = str(created.json()["agent_id"]) + scenario.cleanups.callback(lambda: gateway.request("DELETE", f"/v1/agents/{agent_id}")) + return agent_id + + +def _stored_public_agents(gateway: Gateway) -> frozenset[str]: + rows: Final = gateway.rows( + 'SELECT param_value FROM "LiteLLM_Config" WHERE param_name=%s', ("litellm_settings",) + ) + return frozenset(str(item) for item in list_value(object_value(rows[0]["param_value"])["public_agent_groups"])) + + +@pytest.mark.parametrize("publish", tuple(PUBLISHES), ids=tuple(PUBLISHES)) +def test_concurrent_publishes_through_two_proxies_keep_every_agent_and_a_delete_unpublishes( + gateway: Gateway, peer: Gateway, scenario: Scenario, publish: str +) -> None: + agent_ids: Final = tuple(_agent(gateway, scenario) for _ in range(AGENTS_PER_RACE)) + proxies: Final = (gateway, peer) + + with ThreadPoolExecutor(max_workers=AGENTS_PER_RACE) as pool: + replies: Final = tuple( + pool.map( + lambda indexed: PUBLISHES[publish](proxies[indexed[0] % 2], indexed[1]), + enumerate(agent_ids), + ) + ) + + assert [reply.status_code for reply in replies] == [200] * AGENTS_PER_RACE, [reply.text for reply in replies] + assert frozenset(agent_ids) <= _stored_public_agents(gateway) + + deleted: Final = peer.request("DELETE", f"/v1/agents/{agent_ids[0]}") + + assert deleted.status_code == 200, deleted.text + assert agent_ids[0] not in _stored_public_agents(gateway) + assert frozenset(agent_ids[1:]) <= _stored_public_agents(gateway) diff --git a/tests/unit/proxy/agent_endpoints/test_endpoints.py b/tests/unit/proxy/agent_endpoints/test_endpoints.py index f2fdb3426c8..3bb85b12633 100644 --- a/tests/unit/proxy/agent_endpoints/test_endpoints.py +++ b/tests/unit/proxy/agent_endpoints/test_endpoints.py @@ -1385,6 +1385,66 @@ async def test_make_agents_public_treats_a_config_agents_legacy_id_as_the_same_a assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1"] +@pytest.mark.asyncio +async def test_make_agents_public_matches_a_legacy_id_in_previous_agent_ids_to_the_stored_agent( + monkeypatch: pytest.MonkeyPatch, +) -> None: + rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[]) + config_agent: Final = {"agent_name": "Config Agent", "agent_card_params": _sample_agent_card_params()} + registry.load_agents_from_config([config_agent]) + stable_id: Final = registry.stable_agent_id(next(iter(registry.config_agent_legacy_ids))) + legacy_id: Final = next(iter(registry.ids_for_agent(stable_id) - {stable_id})) + rows.serialized_rows["litellm_settings"] = json.dumps({"public_agent_groups": [stable_id, "agent-1"]}) + + unchanged: Final = await _make_agents_public([stable_id, "agent-1"], previous_agent_ids=[legacy_id, "agent-1"]) + unpublished: Final = await _make_agents_public(["agent-1"], previous_agent_ids=[legacy_id, "agent-1"]) + + assert unchanged["public_agent_groups"] == [stable_id, "agent-1"] + assert unpublished["public_agent_groups"] == ["agent-1"] + assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1"] + + +@pytest.mark.asyncio +async def test_make_agents_public_stores_one_id_when_both_ids_of_a_config_agent_are_ticked( + monkeypatch: pytest.MonkeyPatch, +) -> None: + rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[]) + config_agent: Final = {"agent_name": "Config Agent", "agent_card_params": _sample_agent_card_params()} + registry.load_agents_from_config([config_agent]) + legacy_id: Final = next(iter(registry.config_agent_legacy_ids)) + stable_id: Final = registry.stable_agent_id(legacy_id) + + response: Final = await _make_agents_public([legacy_id, stable_id, "agent-1"], previous_agent_ids=[]) + + assert response["public_agent_groups"] == [legacy_id, "agent-1"] + assert rows.stored("litellm_settings")["public_agent_groups"] == [legacy_id, "agent-1"] + + +@pytest.mark.asyncio +async def test_delete_agent_unpublishes_it_so_a_new_agent_reusing_its_id_starts_private( + monkeypatch: pytest.MonkeyPatch, +) -> None: + import litellm + + rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=["agent-1", "agent-2"]) + agents_table: Final = SimpleNamespace( + find_unique=AsyncMock(return_value={"agent_id": "agent-1", "agent_name": "Agent One"}) + ) + monkeypatch.setattr( + "litellm.proxy.proxy_server.prisma_client", + ReplicaPrisma(rows, tables={"litellm_agentstable": agents_table}), + ) + monkeypatch.setattr( + agent_endpoints, "AGENT_REGISTRY", MagicMock(wraps=registry, delete_agent_from_db=AsyncMock()) + ) + + response: Final = await agent_endpoints.delete_agent(agent_id="agent-1", user_api_key_dict=_PROXY_ADMIN) + + assert response == {"message": "Agent agent-1 deleted successfully"} + assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-2"] + assert litellm.public_agent_groups == ["agent-2"] + + @pytest.mark.asyncio async def test_make_agents_public_without_previous_agent_ids_replaces_the_stored_list( monkeypatch: pytest.MonkeyPatch, diff --git a/tests/unit/proxy/fake_config_table.py b/tests/unit/proxy/fake_config_table.py index 65d13c31dab..8356f7cf310 100644 --- a/tests/unit/proxy/fake_config_table.py +++ b/tests/unit/proxy/fake_config_table.py @@ -57,11 +57,16 @@ def section_locked_transactions(litellm_config: object) -> Callable[[], SectionL class ReplicaPrisma: - def __init__(self, rows: ConfigRows, cached_rows: Mapping[str, Mapping[str, object]] | None = None) -> None: + def __init__( + self, + rows: ConfigRows, + cached_rows: Mapping[str, Mapping[str, object]] | None = None, + tables: Mapping[str, object] | None = None, + ) -> None: self.rows: Final = rows self._cached_rows: Final = cached_rows self.tx: Final = section_locked_transactions(rows) - self.db: Final = SimpleNamespace(litellm_config=rows) + self.db: Final = SimpleNamespace(litellm_config=rows, **(tables or {})) self.writer_db: Final = self.db async def get_generic_data(self, key: str, value: str, table_name: str) -> SimpleNamespace | None: diff --git a/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py index c2ded683ebb..3acc3268696 100644 --- a/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py @@ -344,6 +344,65 @@ async def test_mcp_publication_from_the_modal_keeps_a_server_another_admin_publi assert litellm.public_mcp_servers == ["server-1", "server-3"] +def _replica_publishing_servers( + monkeypatch: pytest.MonkeyPatch, *, config_file_public_mcp_servers: list[str] | None = None +) -> ConfigRows: + from litellm.proxy.proxy_server import ProxyConfig + + rows: Final = ConfigRows({"litellm_settings": {"public_mcp_servers": ["server-1", "server-2"]}}) + proxy_config: Final = ProxyConfig() + if config_file_public_mcp_servers is not None: + proxy_config._load_yaml_settings_stores( + {"litellm_settings": {"public_mcp_servers": config_file_public_mcp_servers}} + ) + monkeypatch.setattr(litellm, "public_mcp_servers", ["server-1", "server-2"]) + monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows)) + monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True) + monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", proxy_config) + monkeypatch.setattr( + mgmt_endpoints, + "delete_mcp_server", + AsyncMock(return_value=generate_mock_mcp_server_db_record(server_id="server-1")), + ) + monkeypatch.setattr( + mgmt_endpoints, "global_mcp_server_manager", MagicMock(reload_servers_from_database=AsyncMock()) + ) + return rows + + +@pytest.mark.asyncio +async def test_remove_mcp_server_unpublishes_it_so_a_new_server_reusing_its_id_starts_private( + monkeypatch: pytest.MonkeyPatch, +) -> None: + rows: Final = _replica_publishing_servers(monkeypatch) + + response: Final = await mgmt_endpoints.remove_mcp_server( + server_id="server-1", + user_api_key_dict=generate_mock_user_api_key_auth(user_role=LitellmUserRoles.PROXY_ADMIN), + litellm_changed_by=None, + ) + + assert response.status_code == 202 + assert rows.stored("litellm_settings")["public_mcp_servers"] == ["server-2"] + assert litellm.public_mcp_servers == ["server-2"] + + +@pytest.mark.asyncio +async def test_remove_mcp_server_still_deletes_when_the_config_file_owns_the_public_list( + monkeypatch: pytest.MonkeyPatch, +) -> None: + rows: Final = _replica_publishing_servers(monkeypatch, config_file_public_mcp_servers=["server-1"]) + + response: Final = await mgmt_endpoints.remove_mcp_server( + server_id="server-1", + user_api_key_dict=generate_mock_user_api_key_auth(user_role=LitellmUserRoles.PROXY_ADMIN), + litellm_changed_by=None, + ) + + assert response.status_code == 202 + assert rows.stored("litellm_settings")["public_mcp_servers"] == ["server-1", "server-2"] + + class TestMCPCredentialsTokenExchangeProfile: """token_exchange_profile must be a declared MCPCredentials field so the management API can persist the entra_obo profile. An undeclared key is silently stripped by pydantic when the diff --git a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py index d9f7862bb10..800db944e17 100644 --- a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py @@ -2120,6 +2120,71 @@ class TestUpdateModel: assert persisted["litellm_credential_name"] == "shared-credential" +@pytest.mark.asyncio +async def test_update_useful_links_from_the_dashboard_keeps_a_link_another_admin_edited( + monkeypatch: pytest.MonkeyPatch, +) -> None: + import litellm + from litellm.proxy.management_endpoints.model_management_endpoints import update_useful_links + from litellm.proxy.proxy_server import ProxyConfig + from litellm.types.proxy.management_endpoints.model_management_endpoints import UpdateUsefulLinksRequest + + docs: Final = {"url": "https://docs.example.com", "index": 0} + status_before: Final = {"url": "https://status.example.com", "index": 1} + status_edited_by_other_admin: Final = {"url": "https://status.example.com/v2", "index": 1} + retired: Final = {"url": "https://retired.example.com", "index": 2} + docs_edited: Final = {"url": "https://docs.example.com/v2", "index": 0} + blog: Final = {"url": "https://blog.example.com", "index": 2} + rows: Final = ConfigRows( + { + "litellm_settings": { + "public_model_groups_links": {"Docs": docs, "Status": status_edited_by_other_admin, "Retired": retired} + } + } + ) + monkeypatch.setattr(litellm, "public_model_groups_links", {}) + monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows)) + monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True) + monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", ProxyConfig()) + + response: Final = await update_useful_links( + request=UpdateUsefulLinksRequest( + useful_links={"Docs": docs_edited, "Status": status_before, "Blog": blog}, + previous_useful_links={"Docs": docs, "Status": status_before, "Retired": retired}, + ), + user_api_key_dict=UserAPIKeyAuth(user_id="admin", user_role=LitellmUserRoles.PROXY_ADMIN), + ) + + expected: Final = {"Docs": docs_edited, "Status": status_edited_by_other_admin, "Blog": blog} + assert response["useful_links"] == expected + assert rows.stored("litellm_settings")["public_model_groups_links"] == expected + assert litellm.public_model_groups_links == expected + + +@pytest.mark.asyncio +async def test_update_useful_links_that_change_nothing_create_no_settings_row( + monkeypatch: pytest.MonkeyPatch, +) -> None: + import litellm + from litellm.proxy.management_endpoints.model_management_endpoints import update_useful_links + from litellm.proxy.proxy_server import ProxyConfig + from litellm.types.proxy.management_endpoints.model_management_endpoints import UpdateUsefulLinksRequest + + rows: Final = ConfigRows({}) + monkeypatch.setattr(litellm, "public_model_groups_links", {}) + monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows)) + monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True) + monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", ProxyConfig()) + + response: Final = await update_useful_links( + request=UpdateUsefulLinksRequest(useful_links={}, previous_useful_links={}), + user_api_key_dict=UserAPIKeyAuth(user_id="admin", user_role=LitellmUserRoles.PROXY_ADMIN), + ) + + assert response["useful_links"] == {} + assert "litellm_settings" not in rows.serialized_rows + + @pytest.mark.asyncio async def test_update_public_model_groups_from_the_modal_keeps_a_group_another_admin_published( monkeypatch: pytest.MonkeyPatch, diff --git a/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.test.tsx b/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.test.tsx index 310201ab243..dc0fabdcb45 100644 --- a/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.test.tsx +++ b/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.test.tsx @@ -52,9 +52,11 @@ describe("UsefulLinksManagement", () => { await user.click(screen.getByRole("button", { name: /add link/i })); await waitFor(() => - expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith("token", { - Docs: { url: "https://docs.example.com", index: 0 }, - }), + expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith( + "token", + { Docs: { url: "https://docs.example.com", index: 0 } }, + {}, + ), ); expect(await screen.findByText("Docs")).toBeInTheDocument(); @@ -62,6 +64,33 @@ describe("UsefulLinksManagement", () => { expect(mockedNotifications.success).toHaveBeenCalledWith("Link added successfully"); }); + it("should send the links it last saved as the starting point of the next save", async () => { + const user = userEvent.setup(); + render(); + + const displayNameInput = await screen.findByPlaceholderText("Friendly name"); + const urlInput = screen.getByPlaceholderText("https://example.com"); + fireEvent.change(displayNameInput, { target: { value: "Docs" } }); + fireEvent.change(urlInput, { target: { value: "https://docs.example.com" } }); + await user.click(screen.getByRole("button", { name: /add link/i })); + expect(await screen.findByText("Docs")).toBeInTheDocument(); + + fireEvent.change(displayNameInput, { target: { value: "Status" } }); + fireEvent.change(urlInput, { target: { value: "https://status.example.com" } }); + await user.click(screen.getByRole("button", { name: /add link/i })); + + await waitFor(() => + expect(mockedUpdateUsefulLinksCall).toHaveBeenLastCalledWith( + "token", + { + Docs: { url: "https://docs.example.com", index: 0 }, + Status: { url: "https://status.example.com", index: 1 }, + }, + { Docs: { url: "https://docs.example.com", index: 0 } }, + ), + ); + }); + it("should rearrange links and save the new order", async () => { const user = userEvent.setup(); mockedGetPublicModelHubInfo.mockResolvedValue({ @@ -87,11 +116,19 @@ describe("UsefulLinksManagement", () => { await user.click(screen.getByRole("button", { name: /save order/i })); await waitFor(() => - expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith("token", { - "Second Link": { url: "https://second.example.com", index: 0 }, - "First Link": { url: "https://first.example.com", index: 1 }, - "Third Link": { url: "https://third.example.com", index: 2 }, - }), + expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith( + "token", + { + "Second Link": { url: "https://second.example.com", index: 0 }, + "First Link": { url: "https://first.example.com", index: 1 }, + "Third Link": { url: "https://third.example.com", index: 2 }, + }, + { + "First Link": "https://first.example.com", + "Second Link": "https://second.example.com", + "Third Link": "https://third.example.com", + }, + ), ); expect(mockedNotifications.success).toHaveBeenCalledWith("Link order saved successfully"); @@ -155,9 +192,11 @@ describe("UsefulLinksManagement", () => { await user.click(screen.getByRole("button", { name: /save/i })); await waitFor(() => - expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith("token", { - "Updated Link": { url: "https://test.example.com", index: 0 }, - }), + expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith( + "token", + { "Updated Link": { url: "https://test.example.com", index: 0 } }, + { "Test Link": "https://test.example.com" }, + ), ); expect(mockedNotifications.success).toHaveBeenCalledWith("Link updated successfully"); diff --git a/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.tsx b/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.tsx index 60e97935522..dd9647a826f 100644 --- a/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.tsx +++ b/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.tsx @@ -27,6 +27,7 @@ const UsefulLinksManagement: React.FC = ({ accessTok const [isExpanded, setIsExpanded] = useState(true); const [isRearranging, setIsRearranging] = useState(false); const [originalLinksOrder, setOriginalLinksOrder] = useState([]); + const [savedLinks, setSavedLinks] = useState>({}); const fetchUsefulLinks = async () => { if (!accessToken) return; @@ -36,6 +37,7 @@ const UsefulLinksManagement: React.FC = ({ accessTok if (response && response.useful_links) { const usefulLinks = response.useful_links || {}; + setSavedLinks(usefulLinks); // Convert object to array of links with ids // Handle both old format (Dict[str, str]) and new format (Dict[str, {url, index}]) @@ -98,7 +100,8 @@ const UsefulLinksManagement: React.FC = ({ accessTok }; }); - await updateUsefulLinksCall(accessToken, linksObject); + await updateUsefulLinksCall(accessToken, linksObject, savedLinks); + setSavedLinks(linksObject); return true; } catch (error) { diff --git a/ui/litellm-dashboard/src/components/networking.tsx b/ui/litellm-dashboard/src/components/networking.tsx index d662568e1b2..d3f8e4cbc3e 100644 --- a/ui/litellm-dashboard/src/components/networking.tsx +++ b/ui/litellm-dashboard/src/components/networking.tsx @@ -1869,11 +1869,12 @@ export const deleteAllowedIP = async (accessToken: string, ip: string) => { export const updateUsefulLinksCall = async ( accessToken: string, useful_links: Record, + previous_useful_links: Record, ) => { try { return await apiClient.post(`/model_hub/update_useful_links`, { accessToken, - body: { useful_links: useful_links }, + body: { useful_links, previous_useful_links }, }); } catch (error) { console.error("Failed to create key:", error); diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index 9133da6bbdc..bb842a22b6f 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -49293,6 +49293,15 @@ export interface components { }; /** UpdateUsefulLinksRequest */ UpdateUsefulLinksRequest: { + /** + * Previous Useful Links + * @description The useful links the caller started from. When set, only the links added, changed or removed relative to it change, so links another admin edited meanwhile keep that edit. When omitted, useful_links replaces the stored links. + */ + previous_useful_links?: { + [key: string]: string | { + [key: string]: unknown; + }; + } | null; /** Useful Links */ useful_links: { [key: string]: string | {