Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { act, fireEvent, render, screen } from "@testing-library/react";
import type { ReactNode } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import type { ReactElement, ReactNode } from "react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { RouterSettingsFormValue } from "../router_settings/RouterSettingsForm";
import { fetchAvailableModels, fetchAvailableModelsForTeam } from "@/components/llm_calls/fetch_models";
import RouterSettingsAccordion, { RouterSettingsAccordionValue } from "./RouterSettingsAccordion";

vi.mock("../networking", () => ({
getRouterSettingsCall: vi.fn().mockResolvedValue({}),
}));

vi.mock("@/components/llm_calls/fetch_models", () => ({
fetchAvailableModels: vi.fn().mockResolvedValue([]),
fetchAvailableModels: vi.fn().mockResolvedValue([{ model_group: "global-model" }]),
fetchAvailableModelsForTeam: vi.fn().mockResolvedValue([{ model_group: "openai/*" }, { model_group: "gpt-5" }]),
}));

vi.mock("../Settings/RouterSettings/Fallbacks/FallbackSelectionForm", () => ({
FallbackSelectionForm: () => null,
FallbackSelectionForm: ({ availableModels }: { availableModels: string[] }) => (
<div data-testid="available-models">{availableModels.join(",")}</div>
),
}));

vi.mock("@tremor/react", () => ({
Expand All @@ -39,9 +44,19 @@ vi.mock("../router_settings/RouterSettingsForm", () => ({
),
}));

const renderWithQueryClient = (ui: ReactElement) => {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
return render(ui, {
wrapper: ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
),
});
};

describe("RouterSettingsAccordion", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.clearAllMocks();
vi.useFakeTimers({ shouldAdvanceTime: true });
});

afterEach(() => {
Expand All @@ -58,7 +73,7 @@ describe("RouterSettingsAccordion", () => {

it("debounces propagation and calls onChange once with the last value", async () => {
const onChange = vi.fn<(value: RouterSettingsAccordionValue) => void>();
render(<RouterSettingsAccordion accessToken="test-token" onChange={onChange} />);
renderWithQueryClient(<RouterSettingsAccordion accessToken="test-token" onChange={onChange} />);
await flushInitialPropagation(onChange);

fireEvent.click(screen.getByText("set-least-busy"));
Expand All @@ -81,9 +96,51 @@ describe("RouterSettingsAccordion", () => {
expect(onChange.mock.calls[0][0].router_settings.routing_strategy).toBe("usage-based-routing");
});

it("offers the team's own models, including team-scoped BYOK ones, when a teamId is given", async () => {
renderWithQueryClient(<RouterSettingsAccordion accessToken="test-token" teamId="team-123" />);

await waitFor(() => {
expect(screen.getByTestId("available-models")).toHaveTextContent("gpt-5,openai/*");
});
expect(fetchAvailableModelsForTeam).toHaveBeenCalledWith("test-token", "team-123");
expect(fetchAvailableModels).not.toHaveBeenCalled();
});

it("falls back to the proxy-wide model listing when no teamId is given", async () => {
renderWithQueryClient(<RouterSettingsAccordion accessToken="test-token" />);

await waitFor(() => {
expect(screen.getByTestId("available-models")).toHaveTextContent("global-model");
});
expect(fetchAvailableModelsForTeam).not.toHaveBeenCalled();
});

it("ignores a stale team's model response that resolves after a newer team was selected", async () => {
const resolvers: ((models: { model_group: string }[]) => void)[] = [];
vi.mocked(fetchAvailableModelsForTeam).mockImplementation(
() => new Promise((resolve) => resolvers.push(resolve)) as Promise<{ model_group: string }[]>,
);

const { rerender } = renderWithQueryClient(<RouterSettingsAccordion accessToken="test-token" teamId="team-slow" />);
await waitFor(() => expect(resolvers).toHaveLength(1));

rerender(<RouterSettingsAccordion accessToken="test-token" teamId="team-fast" />);
await waitFor(() => expect(resolvers).toHaveLength(2));

await act(async () => {
resolvers[1]([{ model_group: "fast-team-model" }]);
resolvers[0]([{ model_group: "slow-team-model" }]);
});

await waitFor(() => {
expect(screen.getByTestId("available-models")).toHaveTextContent("fast-team-model");
});
expect(screen.getByTestId("available-models")).not.toHaveTextContent("slow-team-model");
});

it("does not call onChange when unmounted mid-wait", async () => {
const onChange = vi.fn<(value: RouterSettingsAccordionValue) => void>();
const { unmount } = render(<RouterSettingsAccordion accessToken="test-token" onChange={onChange} />);
const { unmount } = renderWithQueryClient(<RouterSettingsAccordion accessToken="test-token" onChange={onChange} />);
await flushInitialPropagation(onChange);

fireEvent.click(screen.getByText("set-least-busy"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { useEffect, useState, useImperativeHandle, forwardRef, useRef } from "react";
import { TabPanel, TabPanels, TabGroup, TabList, Tab } from "@tremor/react";
import { useQuery } from "@tanstack/react-query";
import { useDebouncedCallback } from "@tanstack/react-pacer/debouncer";
import { getRouterSettingsCall } from "../networking";
import RouterSettingsForm, { RouterSettingsFormValue } from "../router_settings/RouterSettingsForm";
import { Fallbacks } from "../Settings/RouterSettings/Fallbacks/AddFallbacks";
import { FallbackSelectionForm } from "../Settings/RouterSettings/Fallbacks/FallbackSelectionForm";
import { FallbackGroup } from "../Settings/RouterSettings/Fallbacks/FallbackGroupConfig";
import { fetchAvailableModels, ModelGroup } from "@/components/llm_calls/fetch_models";
import { fetchAvailableModels, fetchAvailableModelsForTeam, ModelGroup } from "@/components/llm_calls/fetch_models";

export interface RouterSettingsAccordionValue {
router_settings: {
Expand All @@ -17,11 +18,11 @@
timeout?: number | null;
retry_after?: number | null;
fallbacks?: Fallbacks | null;
context_window_fallbacks?: any | null;

Check warning on line 21 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
retry_policy?: any | null;

Check warning on line 22 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
model_group_alias?: { [key: string]: any } | null;

Check warning on line 23 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
enable_tag_filtering?: boolean;
routing_strategy_args?: { [key: string]: any } | null;

Check warning on line 25 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
};
}

Expand All @@ -29,7 +30,8 @@
accessToken: string;
value?: RouterSettingsAccordionValue;
onChange?: (value: RouterSettingsAccordionValue) => void;
modelData?: any;

Check warning on line 33 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
teamId?: string | null;
}

export interface RouterSettingsAccordionRef {
Expand All @@ -39,17 +41,16 @@
const PROPAGATE_WAIT_MS = 100;

const RouterSettingsAccordion = forwardRef<RouterSettingsAccordionRef, RouterSettingsAccordionProps>(
({ accessToken, value, onChange, modelData }, ref) => {
({ accessToken, value, onChange, modelData, teamId }, ref) => {
const [formValue, setFormValue] = useState<RouterSettingsFormValue>({
routerSettings: {},
selectedStrategy: null,
enableTagFiltering: false,
});
const [fallbacks, setFallbacks] = useState<Fallbacks>([]);
const [fallbackGroups, setFallbackGroups] = useState<FallbackGroup[]>([]);
const [modelInfo, setModelInfo] = useState<ModelGroup[]>([]);
const [availableRoutingStrategies, setAvailableRoutingStrategies] = useState<string[]>([]);
const [routerFieldsMetadata, setRouterFieldsMetadata] = useState<{ [key: string]: any }>({});

Check warning on line 53 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
const [routingStrategyDescriptions, setRoutingStrategyDescriptions] = useState<{ [key: string]: string }>({});
const isInternalUpdateRef = useRef(false);
const lastInitializedValueRef = useRef<string | null>(null);
Expand Down Expand Up @@ -117,7 +118,7 @@
const rs = value.router_settings;
const { fallbacks: _, ...routerSettingsWithoutFallbacks } = rs;
setFormValue({
routerSettings: routerSettingsWithoutFallbacks as { [key: string]: any },

Check warning on line 121 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
selectedStrategy: rs.routing_strategy || null,
enableTagFiltering: rs.enable_tag_filtering ?? false,
});
Expand Down Expand Up @@ -150,8 +151,8 @@
getRouterSettingsCall(accessToken).then((data) => {
if (data.fields) {
// Build metadata map for easy lookup
const fieldsMap: { [key: string]: any } = {};

Check warning on line 154 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
data.fields.forEach((field: any) => {

Check warning on line 155 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
fieldsMap[field.field_name] = {
ui_field_name: field.ui_field_name,
field_description: field.field_description,
Expand All @@ -162,7 +163,7 @@
setRouterFieldsMetadata(fieldsMap);

// Extract routing strategies from the routing_strategy field's options
const routingStrategyField = data.fields.find((field: any) => field.field_name === "routing_strategy");

Check warning on line 166 in ui/litellm-dashboard/src/components/common_components/RouterSettingsAccordion.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
if (routingStrategyField?.options) {
setAvailableRoutingStrategies(routingStrategyField.options);
}
Expand All @@ -175,21 +176,11 @@
});
}, [accessToken]);

// Fetch available models for fallbacks
useEffect(() => {
if (!accessToken) {
return;
}
const loadModels = async () => {
try {
const uniqueModels = await fetchAvailableModels(accessToken);
setModelInfo(uniqueModels);
} catch (error) {
console.error("Error fetching model info for fallbacks:", error);
}
};
loadModels();
}, [accessToken]);
const { data: modelInfo = [] } = useQuery<ModelGroup[]>({
queryKey: ["fallbackAvailableModels", accessToken, teamId ?? null],
queryFn: () => (teamId ? fetchAvailableModelsForTeam(accessToken, teamId) : fetchAvailableModels(accessToken)),
enabled: Boolean(accessToken),
});

// Helper function to build router_settings from current state
const buildRouterSettings = (): RouterSettingsAccordionValue["router_settings"] => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { modelAvailableCall } from "@/components/networking";
import { fetchAvailableModelsForTeam } from "./fetch_models";

vi.mock("@/components/networking", () => ({
modelAvailableCall: vi.fn(),
modelHubCall: vi.fn(),
}));

const modelAvailableCallMock = vi.mocked(modelAvailableCall);

describe("fetchAvailableModelsForTeam", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("requests the models scoped to the team so team-only BYOK models are included", async () => {
modelAvailableCallMock.mockResolvedValue({
data: [{ id: "all-proxy-models" }, { id: "openai/*" }, { id: "gpt-5-mini" }, { id: "openai/*" }],
});

const models = await fetchAvailableModelsForTeam("token", "team-123");

expect(modelAvailableCallMock).toHaveBeenCalledWith("token", "", "", false, "team-123");
expect(models).toEqual([{ model_group: "gpt-5-mini" }, { model_group: "openai/*" }]);
});

it("returns an empty list when the team has no models", async () => {
modelAvailableCallMock.mockResolvedValue({ data: [] });

expect(await fetchAvailableModelsForTeam("token", "team-123")).toEqual([]);
});
});
12 changes: 11 additions & 1 deletion ui/litellm-dashboard/src/components/llm_calls/fetch_models.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// fetch_models.ts

import { modelHubCall } from "@/components/networking";
import { excludeProxyWideSentinel } from "@/components/key_team_helpers/fetch_available_models_team_key";
import { modelAvailableCall, modelHubCall } from "@/components/networking";

export interface ModelGroup {
model_group: string;
mode?: string;
}

export const fetchAvailableModelsForTeam = async (accessToken: string, teamId: string): Promise<ModelGroup[]> => {
const response = await modelAvailableCall(accessToken, "", "", false, teamId);
const modelNames: string[] = (response?.data ?? []).map((model: { id: string }) => model.id);

return excludeProxyWideSentinel(Array.from(new Set(modelNames)))
.sort((a, b) => a.localeCompare(b))
.map((model) => ({ model_group: model }));
};

/**
* Fetches available models using modelHubCall and formats them for the selection dropdown.
*/
Expand Down
1 change: 1 addition & 0 deletions ui/litellm-dashboard/src/components/team/TeamInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ const TeamInfoView: React.FC<TeamInfoProps> = ({
<RouterSettingsAccordion
ref={routerSettingsRef}
accessToken={accessToken || ""}
teamId={teamId}
value={info.router_settings ? { router_settings: info.router_settings } : undefined}
/>
</Form.Item>
Expand Down
Loading