From 0bb89fd5590c67e8eb4cc0bd5e38ab13b6282245 Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Wed, 16 Sep 2026 10:27:59 -0700 Subject: [PATCH] feat(ui): persist guardrails state in the URL The Guardrails page now keeps its tab, table sort and page, detail tab, garden search, card and filters, test playground selection and search, and submission search, status and open submission in query params. Non-admins land on Submitted Guardrails instead of an empty tab strip. The Guardrails Monitor keeps its date range, overview sort, detail tab and log viewer filter, sample size and open request in the URL. The logs query key now includes the date range so changing it refetches logs. LogViewer takes optional view state and keeps its own state when none is passed, so ToolDetail is unchanged. --- .../_components/GuardrailDetail.test.tsx | 135 ++++++++++++++++-- .../_components/GuardrailDetail.tsx | 24 ++-- .../GuardrailsMonitorView.test.tsx | 72 +++++++++- .../_components/GuardrailsMonitorView.tsx | 26 +--- .../_components/GuardrailsOverview.test.tsx | 54 ++++++- .../_components/GuardrailsOverview.tsx | 53 +++---- .../_components/useGuardrailDetailUrlState.ts | 37 +++++ .../_components/useMonitorDateRange.ts | 54 +++++++ .../GuardrailTestPlayground.test.tsx | 126 ++++++++++++---- .../_components/GuardrailTestPlayground.tsx | 32 +++-- .../_components/GuardrailsPanel.test.tsx | 91 +++++++++++- .../_components/GuardrailsPanel.tsx | 15 +- .../_components/TeamGuardrailsTab.test.tsx | 108 +++++++++++++- .../_components/TeamGuardrailsTab.tsx | 16 +-- .../_components/guardrail_garden.test.tsx | 124 +++++++++++++++- .../_components/guardrail_garden.tsx | 38 +++-- .../guardrail_garden_detail.test.tsx | 73 +++++++++- .../_components/guardrail_garden_detail.tsx | 8 +- .../guardrail_info.characterization.test.tsx | 7 +- .../_components/guardrail_info.test.tsx | 98 +++++++++++-- .../guardrails/_components/guardrail_info.tsx | 4 +- .../_components/guardrail_table.test.tsx | 89 +++++++++++- .../_components/guardrail_table.tsx | 18 ++- .../_components/useGardenDetailTab.ts | 10 ++ .../_components/useGuardrailDetailTab.ts | 10 ++ .../_components/useTeamGuardrailsUrlState.ts | 24 ++++ .../GuardrailsMonitor/LogViewer.test.tsx | 83 +++++++++++ .../GuardrailsMonitor/LogViewer.tsx | 49 ++++--- .../GuardrailsMonitor/useLogViewerState.ts | 22 +++ 29 files changed, 1295 insertions(+), 205 deletions(-) create mode 100644 ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/useGuardrailDetailUrlState.ts create mode 100644 ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/useMonitorDateRange.ts create mode 100644 ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/useGardenDetailTab.ts create mode 100644 ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/useGuardrailDetailTab.ts create mode 100644 ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/useTeamGuardrailsUrlState.ts create mode 100644 ui/litellm-dashboard/src/components/GuardrailsMonitor/useLogViewerState.ts diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailDetail.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailDetail.test.tsx index 9f2a4c42228..5e97206d92f 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailDetail.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailDetail.test.tsx @@ -1,8 +1,9 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import userEvent from "@testing-library/user-event"; -import { render, screen, waitFor } from "@testing-library/react"; -import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import type { UrlUpdateEvent } from "nuqs/adapters/testing"; +import { renderWithProviders, screen, testQueryClient, waitFor } from "@/../tests/test-utils"; import type { GuardrailUsageDetail } from "@/app/(dashboard)/hooks/guardrails/useGuardrailsUsage"; +import type { LogViewerState } from "@/components/GuardrailsMonitor/useLogViewerState"; import { GuardrailDetail } from "./GuardrailDetail"; const mockUseGuardrailsUsageDetail = vi.fn(); @@ -16,7 +17,20 @@ vi.mock("@/components/networking", () => ({ })); vi.mock("@/components/GuardrailsMonitor/LogViewer", () => ({ - LogViewer: ({ guardrailName }: { guardrailName: string }) =>
{guardrailName}
, + LogViewer: ({ guardrailName, viewState }: { guardrailName: string; viewState?: LogViewerState }) => ( +
+ {guardrailName} + + + + +
+ ), })); vi.mock("./EvaluationSettingsModal", () => ({ @@ -59,15 +73,19 @@ const defaultProps = { endDate: "2026-07-24", }; -function renderDetail(props: Partial = {}) { - const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); - return render(, { - wrapper: ({ children }) => {children}, - }); +function renderDetail( + props: Partial = {}, + urlOptions: { searchParams?: string; onUrlUpdate?: (event: UrlUpdateEvent) => void } = {}, +) { + return renderWithProviders(, urlOptions); } +const lastUrlUpdate = (onUrlUpdate: ReturnType void>>) => + onUrlUpdate.mock.calls.at(-1)?.[0]; + describe("GuardrailDetail", () => { beforeEach(() => { + testQueryClient.clear(); vi.clearAllMocks(); mockUseGuardrailsUsageDetail.mockReturnValue(loaded(detail)); mockGetGuardrailsUsageLogs.mockResolvedValue({ logs: [], total: 0 }); @@ -152,6 +170,19 @@ describe("GuardrailDetail", () => { expect(screen.getByTestId("log-viewer")).toHaveTextContent("pii-detector"); }); + it("should request the logs again when the date range changes", async () => { + const { rerender } = renderDetail(); + await waitFor(() => expect(mockGetGuardrailsUsageLogs).toHaveBeenCalledTimes(1)); + + rerender(); + + await waitFor(() => expect(mockGetGuardrailsUsageLogs).toHaveBeenCalledTimes(2)); + expect(mockGetGuardrailsUsageLogs).toHaveBeenLastCalledWith( + "test-token", + expect.objectContaining({ guardrailId: "pii-detector", startDate: "2026-06-01", endDate: "2026-06-15" }), + ); + }); + it("should keep the evaluation settings modal closed until its button is clicked", async () => { const user = userEvent.setup(); renderDetail(); @@ -171,4 +202,92 @@ describe("GuardrailDetail", () => { ); expect(mockGetGuardrailsUsageLogs).not.toHaveBeenCalled(); }); + + describe("URL state", () => { + it("should select the Logs tab when ?tab=logs", async () => { + renderDetail({}, { searchParams: "?tab=logs" }); + + expect(await screen.findByRole("tab", { name: "Logs" })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tab", { name: "Overview" })).toHaveAttribute("aria-selected", "false"); + }); + + it("should write the clicked tab to ?tab= and drop it for the overview", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); + renderDetail({}, { onUrlUpdate }); + + await user.click(await screen.findByRole("tab", { name: "Logs" })); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("tab")).toBe("logs")); + + await user.click(screen.getByRole("tab", { name: "Overview" })); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("tab")).toBe(false)); + expect(screen.getByRole("tab", { name: "Overview" })).toHaveAttribute("aria-selected", "true"); + }); + + it("should hand the log filter, sample size and open request from the URL to the log viewer", async () => { + renderDetail({}, { searchParams: "?tab=logs&log_filter=blocked&log_sample=50&request_id=req-3" }); + + const viewer = await screen.findByTestId("log-viewer"); + expect(viewer).toHaveAttribute("data-filter", "blocked"); + expect(viewer).toHaveAttribute("data-sample", "50"); + expect(viewer).toHaveAttribute("data-request", "req-3"); + }); + + it("should fall back to all logs and a sample of 10 for unknown values", async () => { + renderDetail({}, { searchParams: "?log_filter=everything&log_sample=7" }); + + const viewer = await screen.findByTestId("log-viewer"); + expect(viewer).toHaveAttribute("data-filter", "all"); + expect(viewer).toHaveAttribute("data-sample", "10"); + expect(viewer).toHaveAttribute("data-request", ""); + }); + + it("should write the log viewer's filter, sample size and open request to the URL", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); + renderDetail({}, { onUrlUpdate }); + + await user.click(await screen.findByRole("button", { name: "show blocked" })); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("log_filter")).toBe("blocked")); + expect(screen.getByTestId("log-viewer")).toHaveAttribute("data-filter", "blocked"); + + await user.click(screen.getByRole("button", { name: "sample 50" })); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("log_sample")).toBe("50")); + + await user.click(screen.getByRole("button", { name: "open req-9" })); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("request_id")).toBe("req-9")); + expect(lastUrlUpdate(onUrlUpdate)?.options.history).toBe("push"); + + await user.click(screen.getByRole("button", { name: "close request" })); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("request_id")).toBe(false)); + expect(lastUrlUpdate(onUrlUpdate)?.options.history).toBe("replace"); + expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("log_filter")).toBe("blocked"); + }); + + it("should drop the detail tab and log viewer state when going back", async () => { + const user = userEvent.setup(); + const onBack = vi.fn(); + const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); + renderDetail( + { onBack }, + { + searchParams: + "?guardrail=pii-detector&start_date=2026-07-01&tab=logs&log_filter=blocked&log_sample=100&request_id=req-3", + onUrlUpdate, + }, + ); + + await user.click(await screen.findByRole("button", { name: /back to overview/i })); + + expect(onBack).toHaveBeenCalledOnce(); + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + const update = lastUrlUpdate(onUrlUpdate); + for (const key of ["tab", "log_filter", "log_sample", "request_id"]) { + expect(update?.searchParams.has(key)).toBe(false); + } + expect(update?.searchParams.get("guardrail")).toBe("pii-detector"); + expect(update?.searchParams.get("start_date")).toBe("2026-07-01"); + expect(update?.options.history).toBe("replace"); + }); + }); }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailDetail.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailDetail.tsx index 86b596d4bcd..e30e8b6a8f5 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailDetail.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailDetail.tsx @@ -13,6 +13,7 @@ import { GuardrailUsageBreakdown } from "./GuardrailUsageBreakdown"; import { LogViewer } from "@/components/GuardrailsMonitor/LogViewer"; import { MetricCard } from "@/components/GuardrailsMonitor/MetricCard"; import type { LogEntry } from "@/components/GuardrailsMonitor/mockData"; +import { useGuardrailDetailUrlState } from "./useGuardrailDetailUrlState"; interface GuardrailDetailProps { guardrailId: string; @@ -29,7 +30,7 @@ const STATUS_TONE: Record = { }; export function GuardrailDetail({ guardrailId, onBack, accessToken = null, startDate, endDate }: GuardrailDetailProps) { - const [activeTab, setActiveTab] = useState("overview"); + const { tab: activeTab, setTab: setActiveTab, logViewerState, clear: clearUrlState } = useGuardrailDetailUrlState(); const [evaluationModalOpen, setEvaluationModalOpen] = useState(false); const [logsPage] = useState(1); const logsPageSize = 50; @@ -40,7 +41,7 @@ export function GuardrailDetail({ guardrailId, onBack, accessToken = null, start error: detailError, } = useGuardrailsUsageDetail(guardrailId, { accessToken, startDate, endDate }); const { data: logsData, isLoading: logsLoading } = useQuery({ - queryKey: ["guardrails-usage-logs", guardrailId, logsPage, logsPageSize], + queryKey: ["guardrails-usage-logs", guardrailId, startDate, endDate, logsPage, logsPageSize], queryFn: () => getGuardrailsUsageLogs(accessToken!, { guardrailId, @@ -90,6 +91,11 @@ export function GuardrailDetail({ guardrailId, onBack, accessToken = null, start avgLatency: undefined as number | undefined, }; + const handleBack = () => { + clearUrlState(); + onBack(); + }; + if (detailLoading && !detailData) { return (
@@ -100,7 +106,7 @@ export function GuardrailDetail({ guardrailId, onBack, accessToken = null, start if (detailError && !detailData) { return (
- @@ -109,23 +115,23 @@ export function GuardrailDetail({ guardrailId, onBack, accessToken = null, start ); } - const logViewer = (filterAction?: "all") => ( + const renderLogViewer = () => ( ); return (
- @@ -156,7 +162,7 @@ export function GuardrailDetail({ guardrailId, onBack, accessToken = null, start
- setActiveTab(value as string)}> + Overview @@ -194,11 +200,11 @@ export function GuardrailDetail({ guardrailId, onBack, accessToken = null, start {detailData && } - {logViewer("all")} + {renderLogViewer()} - {logViewer()} + {renderLogViewer()} diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsMonitorView.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsMonitorView.test.tsx index df106fc38c3..7ddc0cb7709 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsMonitorView.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsMonitorView.test.tsx @@ -1,9 +1,10 @@ +import moment from "moment"; import { type UrlUpdateEvent } from "nuqs/adapters/testing"; -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import userEvent from "@testing-library/user-event"; import GuardrailsMonitorView from "./GuardrailsMonitorView"; import * as networking from "@/components/networking"; -import { renderWithProviders, screen, testQueryClient, waitFor } from "@/../tests/test-utils"; +import { fireEvent, renderWithProviders, screen, testQueryClient, waitFor } from "@/../tests/test-utils"; vi.mock("@/components/networking", () => ({ getGuardrailsUsageLogs: vi.fn(), @@ -149,4 +150,71 @@ describe("GuardrailsMonitorView", () => { expect(await screen.findByRole("heading", { name: /Guardrails Monitor/i })).toBeInTheDocument(); }); }); + + describe("date range in the URL (?start_date= and ?end_date=)", () => { + const lastOverviewRange = () => { + const [options] = mockUseGuardrailsUsageOverview.mock.calls.at(-1) as [{ startDate: string; endDate: string }]; + return { startDate: options.startDate, endDate: options.endDate }; + }; + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it("should request usage for the range in the URL and show it in the picker", () => { + renderWithProviders(, { + searchParams: "?start_date=2026-01-05&end_date=2026-01-20", + }); + + expect(lastOverviewRange()).toEqual({ startDate: "2026-01-05", endDate: "2026-01-20" }); + expect(screen.getByRole("button", { name: "5 Jan, 00:00 - 20 Jan, 23:59" })).toBeInTheDocument(); + }); + + it("should pass the URL range to the detail view as well", () => { + renderWithProviders(, { + searchParams: "?guardrail=gr-pii&start_date=2026-01-05&end_date=2026-01-20", + }); + + expect(mockUseGuardrailsUsageDetail).toHaveBeenLastCalledWith( + "gr-pii", + expect.objectContaining({ startDate: "2026-01-05", endDate: "2026-01-20" }), + ); + }); + + it("should default to a seven day range ending today when the URL has none", () => { + renderWithProviders(); + + const { startDate, endDate } = lastOverviewRange(); + expect(endDate).toBe(moment().format("YYYY-MM-DD")); + expect(moment(endDate).diff(moment(startDate), "days")).toBe(7); + }); + + it("should ignore a malformed date and keep the other one", () => { + renderWithProviders(, { + searchParams: "?start_date=2026-02-30&end_date=2026-01-20", + }); + + const { startDate, endDate } = lastOverviewRange(); + expect(endDate).toBe("2026-01-20"); + expect(startDate).toBe(moment().subtract(7, "days").format("YYYY-MM-DD")); + }); + + it("should write the applied range to the URL and request usage for it", async () => { + vi.stubGlobal("requestIdleCallback", (callback: () => void) => setTimeout(callback, 0)); + const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); + renderWithProviders(, { + searchParams: "?start_date=2026-01-05&end_date=2026-01-20", + onUrlUpdate, + }); + + fireEvent.click(screen.getByRole("button", { name: "5 Jan, 00:00 - 20 Jan, 23:59" })); + fireEvent.change(screen.getByDisplayValue("2026-01-05"), { target: { value: "2026-03-01" } }); + fireEvent.change(screen.getByDisplayValue("2026-01-20"), { target: { value: "2026-03-15" } }); + fireEvent.click(screen.getByRole("button", { name: "Apply" })); + + await waitFor(() => expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("start_date")).toBe("2026-03-01")); + expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get("end_date")).toBe("2026-03-15"); + expect(lastOverviewRange()).toEqual({ startDate: "2026-03-01", endDate: "2026-03-15" }); + expect(screen.getByRole("button", { name: "1 Mar, 00:00 - 15 Mar, 23:59" })).toBeInTheDocument(); + }); + }); }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsMonitorView.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsMonitorView.tsx index f90a46e19e4..0042c7a99cd 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsMonitorView.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsMonitorView.tsx @@ -1,39 +1,19 @@ -import type { DateRangePickerValue } from "@/components/shared/date_picker_types"; import { parseAsString, useQueryState } from "nuqs"; -import React, { useCallback, useMemo, useState } from "react"; -import { formatDate } from "@/components/networking"; import AdvancedDatePicker from "@/components/shared/advanced_date_picker"; import { GuardrailDetail } from "./GuardrailDetail"; import { GuardrailsOverview } from "./GuardrailsOverview"; +import { useMonitorDateRange } from "./useMonitorDateRange"; interface GuardrailsMonitorViewProps { accessToken?: string | null; } -const defaultEnd = new Date(); -const defaultStart = new Date(); -defaultStart.setDate(defaultStart.getDate() - 7); - export default function GuardrailsMonitorView({ accessToken = null }: GuardrailsMonitorViewProps) { const [selectedGuardrailId, setSelectedGuardrailId] = useQueryState( "guardrail", parseAsString.withOptions({ history: "push" }), ); - - const initialFrom = useMemo(() => new Date(defaultStart), []); - const initialTo = useMemo(() => new Date(defaultEnd), []); - - const [dateValue, setDateValue] = useState({ - from: initialFrom, - to: initialTo, - }); - - const startDate = dateValue.from ? formatDate(dateValue.from) : ""; - const endDate = dateValue.to ? formatDate(dateValue.to) : ""; - - const handleDateChange = useCallback((newValue: DateRangePickerValue) => { - setDateValue(newValue); - }, []); + const { startDate, endDate, pickerValue, setPickerValue } = useMonitorDateRange(); const handleSelectGuardrail = (id: string) => { void setSelectedGuardrailId(id); @@ -44,7 +24,7 @@ export default function GuardrailsMonitorView({ accessToken = null }: Guardrails }; const dateRangeControl = ( - + ); return ( diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsOverview.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsOverview.test.tsx index ead5fc9f845..645fe4faf39 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsOverview.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsOverview.test.tsx @@ -1,5 +1,6 @@ -import { render, screen, waitFor, within } from "@testing-library/react"; +import { renderWithProviders, screen, waitFor, within } from "@/../tests/test-utils"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; import type { GuardrailUsageOverview, @@ -80,14 +81,18 @@ const overview: GuardrailUsageOverview = { totalUntrackedUsageUnits: { sensitiveInformationPolicyUnits: 250 }, }; -function renderOverview(onSelectGuardrail = vi.fn()) { - return render( +function renderOverview( + onSelectGuardrail = vi.fn(), + urlOptions: { searchParams?: string; onUrlUpdate?: OnUrlUpdateFunction } = {}, +) { + return renderWithProviders( , + urlOptions, ); } @@ -288,4 +293,47 @@ describe("GuardrailsOverview", () => { expect(await screen.findByText("Failed to load data. Try again.")).toBeInTheDocument(); }); + + describe("sort in the URL (?sort_by= and ?sort_order=)", () => { + const rowOrder = () => + screen + .getAllByRole("row") + .slice(1) + .map((r) => within(r).getAllByRole("button")[0].textContent); + + it("orders rows by the column and direction in the URL", () => { + renderOverview(vi.fn(), { searchParams: "?sort_by=requestsEvaluated&sort_order=asc" }); + + expect(rowOrder()).toEqual(["Free Bedrock Guardrail", "High Failure Guardrail", "Low Failure Guardrail"]); + }); + + it("keeps guardrails with no known cost last when the URL sorts by cost ascending", () => { + renderOverview(vi.fn(), { searchParams: "?sort_by=cost&sort_order=asc" }); + + expect(rowOrder()).toEqual(["Free Bedrock Guardrail", "High Failure Guardrail", "Low Failure Guardrail"]); + }); + + it("falls back to the highest fail rate first for a column that cannot be sorted", () => { + renderOverview(vi.fn(), { searchParams: "?sort_by=name" }); + + expect(rowOrder()).toEqual(["High Failure Guardrail", "Low Failure Guardrail", "Free Bedrock Guardrail"]); + }); + + it("writes the clicked column and direction to the URL", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderOverview(vi.fn(), { onUrlUpdate }); + const lastParams = () => onUrlUpdate.mock.calls.at(-1)?.[0].searchParams; + + await user.click(screen.getByRole("button", { name: /Requests/ })); + await waitFor(() => expect(lastParams()?.get("sort_by")).toBe("requestsEvaluated")); + expect(lastParams()?.get("sort_order")).toBe("asc"); + expect(rowOrder()).toEqual(["Free Bedrock Guardrail", "High Failure Guardrail", "Low Failure Guardrail"]); + + await user.click(screen.getByRole("button", { name: /Requests/ })); + await waitFor(() => expect(lastParams()?.has("sort_order")).toBe(false)); + expect(lastParams()?.get("sort_by")).toBe("requestsEvaluated"); + expect(rowOrder()).toEqual(["Low Failure Guardrail", "High Failure Guardrail", "Free Bedrock Guardrail"]); + }); + }); }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsOverview.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsOverview.tsx index 468e6967d81..d3337a416df 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsOverview.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/GuardrailsOverview.tsx @@ -1,7 +1,12 @@ -import type { ColumnDef, OnChangeFn, SortingState } from "@tanstack/react-table"; +import type { ColumnDef } from "@tanstack/react-table"; import { CircleDollarSign, Download, HeartPulse, Settings, TrendingUp, TriangleAlert } from "lucide-react"; import React, { useMemo, useState } from "react"; -import { DataTable, DataTableSortHeader } from "@/components/shared/DataTable"; +import { + DataTable, + DataTableSortHeader, + useUrlTableState, + type UrlTableStateOptions, +} from "@/components/shared/DataTable"; import { MoneyCell } from "@/components/shared/table_cells/money_cell"; import { CellTooltip } from "@/components/shared/table_cells/cell_tooltip"; import { @@ -32,7 +37,12 @@ interface GuardrailsOverviewProps { dateRangeControl?: React.ReactNode; } -type SortKey = "failRate" | "requestsEvaluated" | "avgLatency" | "cost"; +const TABLE_STATE_OPTIONS: UrlTableStateOptions = { + sortFields: ["failRate", "requestsEvaluated", "avgLatency", "cost"], + defaultSort: { id: "failRate", desc: true }, + defaultPageSize: 25, + filterColumns: [], +}; const providerColors: Record = { Bedrock: "bg-warning/15 text-warning border-warning/20", @@ -118,8 +128,7 @@ export function GuardrailsOverview({ onSelectGuardrail, dateRangeControl, }: GuardrailsOverviewProps) { - const [sortBy, setSortBy] = useState("failRate"); - const [sortDir, setSortDir] = useState<"asc" | "desc">("desc"); + const { sorting, onSortingChange } = useUrlTableState(TABLE_STATE_OPTIONS); const [evaluationModalOpen, setEvaluationModalOpen] = useState(false); const { @@ -144,15 +153,6 @@ export function GuardrailsOverview({ }; }, [guardrailsData, activeData]); const chartData = guardrailsData?.chart; - const sorted = useMemo(() => { - const mult = sortDir === "desc" ? -1 : 1; - return [...activeData].sort((a, b) => { - const aVal = a[sortBy]; - const bVal = b[sortBy]; - if (aVal == null || bVal == null) return Number(aVal == null) - Number(bVal == null); - return (aVal - bVal) * mult; - }); - }, [activeData, sortBy, sortDir]); const isLoading = guardrailsLoading; const error = guardrailsError; @@ -234,7 +234,9 @@ export function GuardrailsOverview({ }, { header: ({ column }) => , - accessorKey: "avgLatency", + id: "avgLatency", + accessorFn: (row) => row.avgLatency ?? undefined, + sortUndefined: "last", meta: { numeric: true }, sortDescFirst: false, cell: ({ row }) => ( @@ -262,24 +264,15 @@ export function GuardrailsOverview({ }, { header: ({ column }) => , - accessorKey: "cost", + id: "cost", + accessorFn: (row) => row.cost ?? undefined, + sortUndefined: "last", meta: { numeric: true }, sortDescFirst: false, cell: ({ row }) => , }, ]; - const sortableKeys: SortKey[] = ["failRate", "requestsEvaluated", "avgLatency", "cost"]; - const sorting = useMemo(() => [{ id: sortBy, desc: sortDir === "desc" }], [sortBy, sortDir]); - const handleSortingChange: OnChangeFn = (updater) => { - const nextSorting = typeof updater === "function" ? updater(sorting) : updater; - const primarySort = nextSorting[0]; - if (primarySort && sortableKeys.includes(primarySort.id as SortKey)) { - setSortBy(primarySort.id as SortKey); - setSortDir(primarySort.desc ? "desc" : "asc"); - } - }; - return (
row.id} isLoading={isLoading} noDataMessage="No data for this period" onRowClick={(row) => onSelectGuardrail(row.id)} rowClassName={() => "cursor-pointer"} - sortingMode="server" + sortingMode="client" sorting={sorting} - onSortingChange={handleSortingChange} + onSortingChange={onSortingChange} enableSortingRemoval={false} size="compact" toolbar={() => ( diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/useGuardrailDetailUrlState.ts b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/useGuardrailDetailUrlState.ts new file mode 100644 index 00000000000..bbf6ffd6d0e --- /dev/null +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/useGuardrailDetailUrlState.ts @@ -0,0 +1,37 @@ +import { parseAsNumberLiteral, parseAsString, parseAsStringLiteral, useQueryStates } from "nuqs"; +import { useCallback, useMemo } from "react"; +import { LOG_FILTERS, LOG_SAMPLE_SIZES, type LogViewerState } from "@/components/GuardrailsMonitor/useLogViewerState"; +import { useUrlTab } from "@/hooks/useUrlTab"; + +const DETAIL_TABS = ["overview", "logs"] as const; + +const logParsers = { + log_filter: parseAsStringLiteral(LOG_FILTERS).withDefault("all"), + log_sample: parseAsNumberLiteral(LOG_SAMPLE_SIZES).withDefault(10), + request_id: parseAsString.withOptions({ history: "push" }), +}; + +export function useGuardrailDetailUrlState() { + const [tab, setTab] = useUrlTab(DETAIL_TABS, "overview"); + const [{ log_filter, log_sample, request_id }, setLogParams] = useQueryStates(logParsers); + + const logViewerState = useMemo( + () => ({ + filter: log_filter, + setFilter: (filter) => void setLogParams({ log_filter: filter }), + sampleSize: log_sample, + setSampleSize: (size) => void setLogParams({ log_sample: size }), + requestId: request_id, + setRequestId: (requestId) => + void setLogParams({ request_id: requestId }, requestId === null ? { history: "replace" } : {}), + }), + [log_filter, log_sample, request_id, setLogParams], + ); + + const clear = useCallback(() => { + setTab("overview"); + void setLogParams(null, { history: "replace" }); + }, [setTab, setLogParams]); + + return { tab, setTab, logViewerState, clear }; +} diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/useMonitorDateRange.ts b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/useMonitorDateRange.ts new file mode 100644 index 00000000000..3fcf4397d73 --- /dev/null +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails-monitor/_components/useMonitorDateRange.ts @@ -0,0 +1,54 @@ +import moment from "moment"; +import { createParser, useQueryStates } from "nuqs"; +import { useCallback, useMemo } from "react"; +import type { DateRangePickerValue } from "@/components/shared/date_picker_types"; + +const DAY_FORMAT = "YYYY-MM-DD"; +const DEFAULT_RANGE_DAYS = 7; + +const formatDay = (date: Date) => moment(date).format(DAY_FORMAT); + +const parseAsDay = createParser({ + parse: (value: string) => { + const day = moment(value, DAY_FORMAT, true); + return day.isValid() ? day.format(DAY_FORMAT) : null; + }, + serialize: (value: string) => value, +}); + +const buildParsers = (today: Date) => ({ + start_date: parseAsDay.withDefault(formatDay(moment(today).subtract(DEFAULT_RANGE_DAYS, "days").toDate())), + end_date: parseAsDay.withDefault(formatDay(today)), +}); + +export interface MonitorDateRange { + startDate: string; + endDate: string; + pickerValue: DateRangePickerValue; + setPickerValue: (value: DateRangePickerValue) => void; +} + +export function useMonitorDateRange(): MonitorDateRange { + const parsers = useMemo(() => buildParsers(new Date()), []); + const [{ start_date: startDate, end_date: endDate }, setRange] = useQueryStates(parsers); + + const pickerValue = useMemo( + () => ({ + from: moment(startDate, DAY_FORMAT).startOf("day").toDate(), + to: moment(endDate, DAY_FORMAT).endOf("day").toDate(), + }), + [startDate, endDate], + ); + + const setPickerValue = useCallback( + ({ from, to }: DateRangePickerValue) => { + void setRange({ + start_date: from ? formatDay(from) : null, + end_date: to ? formatDay(to) : null, + }); + }, + [setRange], + ); + + return { startDate, endDate, pickerValue, setPickerValue }; +} diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailTestPlayground.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailTestPlayground.test.tsx index 252b01dbc4b..720675023e2 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailTestPlayground.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailTestPlayground.test.tsx @@ -1,5 +1,6 @@ -import { render, screen, waitFor } from "@testing-library/react"; +import { fireEvent, renderWithProviders, screen, waitFor, within } from "@/../tests/test-utils"; import userEvent from "@testing-library/user-event"; +import type { OnUrlUpdateFunction } from "nuqs/adapters/testing"; import { beforeEach, describe, expect, it, vi } from "vitest"; import GuardrailTestPlayground from "./GuardrailTestPlayground"; @@ -19,55 +20,118 @@ Object.defineProperty(window, "matchMedia", { })), }); +const makeGuardrail = (id: string, name: string) => ({ + guardrail_id: id, + guardrail_name: name, + litellm_params: { + guardrail: "presidio", + mode: "pre_call" as const, + default_on: false, + }, + guardrail_info: {}, +}); + +const sidebarItem = (name: string) => within(screen.getAllByRole("list")[0]).getByText(name); + +const lastUrlUpdate = (onUrlUpdate: ReturnType>) => + onUrlUpdate.mock.calls.at(-1)?.[0]; + describe("GuardrailTestPlayground", () => { const mockAccessToken = "test-token"; - const mockGuardrails = [ - { - guardrail_id: "guard-1", - guardrail_name: "test-guardrail", - litellm_params: { - guardrail: "presidio", - mode: "pre_call", - default_on: false, - }, - guardrail_info: {}, - }, - ]; + const mockGuardrails = [makeGuardrail("guard-1", "test-guardrail")]; + const twoGuardrails = [makeGuardrail("guard-1", "pii, strict"), makeGuardrail("guard-2", "toxicity")]; + + const renderPlayground = ( + guardrailsList = mockGuardrails, + searchParams?: string, + onUrlUpdate?: OnUrlUpdateFunction, + ) => + renderWithProviders( + , + { searchParams, onUrlUpdate }, + ); beforeEach(() => { vi.clearAllMocks(); }); it("should allow selecting a guardrail and show test panel", async () => { - /** - * Tests that clicking on a guardrail selects it and displays the test panel. - * This validates the core workflow of selecting and testing guardrails. - */ const user = userEvent.setup(); - render( - , - ); + renderPlayground(); - // Initially, the empty state should be shown expect(screen.getByText("Select Guardrails to Test")).toBeInTheDocument(); - // Click on the guardrail to select it - const guardrailItem = screen.getByText("test-guardrail"); - await user.click(guardrailItem); + await user.click(screen.getByText("test-guardrail")); - // Verify the test panel is now shown await waitFor(() => { expect(screen.getByText("Test Guardrails:")).toBeInTheDocument(); expect(screen.getByPlaceholderText("Enter text to test with guardrails...")).toBeInTheDocument(); }); - // Verify the selected count expect(screen.getByText("1 of 1 selected")).toBeInTheDocument(); }); + + describe("URL state", () => { + it("should select the guardrails named in ?test_guardrails=, including names with commas", () => { + renderPlayground(twoGuardrails, `?test_guardrails=${encodeURIComponent("pii%2C strict")},toxicity`); + + expect(screen.getByText("2 of 2 selected")).toBeInTheDocument(); + expect(screen.getByText("Test 2 guardrails")).toBeInTheDocument(); + }); + + it("should ignore names in ?test_guardrails= that are not in the guardrail list", () => { + renderPlayground(twoGuardrails, "?test_guardrails=deleted-guardrail"); + + expect(screen.getByText("0 of 2 selected")).toBeInTheDocument(); + expect(screen.getByText("Select Guardrails to Test")).toBeInTheDocument(); + }); + + it("should write each selection to ?test_guardrails= and remove the key once nothing is selected", async () => { + const user = userEvent.setup(); + const onUrlUpdate = vi.fn(); + renderPlayground(twoGuardrails, "?test_guardrails=deleted-guardrail", onUrlUpdate); + + await user.click(sidebarItem("pii, strict")); + await waitFor(() => + expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("test_guardrails")).toBe("pii%2C strict"), + ); + + await user.click(sidebarItem("toxicity")); + await waitFor(() => + expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("test_guardrails")).toBe("pii%2C strict,toxicity"), + ); + expect(screen.getByText("2 of 2 selected")).toBeInTheDocument(); + + await user.click(sidebarItem("pii, strict")); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("test_guardrails")).toBe("toxicity")); + + await user.click(sidebarItem("toxicity")); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("test_guardrails")).toBe(false)); + expect(screen.getByText("Select Guardrails to Test")).toBeInTheDocument(); + }); + + it("should filter the list and prefill the search box from ?test_q=", () => { + renderPlayground(twoGuardrails, "?test_q=TOX"); + + expect(screen.getByPlaceholderText("Search guardrails...")).toHaveValue("TOX"); + expect(screen.getByText("toxicity")).toBeInTheDocument(); + expect(screen.queryByText("pii, strict")).not.toBeInTheDocument(); + }); + + it("should write the search box to ?test_q=", async () => { + const onUrlUpdate = vi.fn(); + renderPlayground(twoGuardrails, undefined, onUrlUpdate); + + fireEvent.change(screen.getByPlaceholderText("Search guardrails..."), { target: { value: "nothing" } }); + + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("test_q")).toBe("nothing")); + expect(screen.getByText("No guardrails match your search")).toBeInTheDocument(); + }); + }); }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailTestPlayground.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailTestPlayground.tsx index c64b5d7cb5c..d32ca1b9586 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailTestPlayground.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailTestPlayground.tsx @@ -1,5 +1,6 @@ -import React, { useState } from "react"; +import React, { useMemo, useState } from "react"; import { FlaskConical, Search } from "lucide-react"; +import { parseAsArrayOf, parseAsString, useQueryState } from "nuqs"; import GuardrailTestPanel from "./GuardrailTestPanel"; import { applyGuardrail } from "@/components/networking"; import { toast } from "@/lib/toast"; @@ -29,6 +30,9 @@ interface GuardrailTestPlaygroundProps { onClose: () => void; } +const selectedNamesParser = parseAsArrayOf(parseAsString).withDefault([]); +const searchParser = parseAsString.withDefault(""); + interface TestResult { guardrailName: string; response_text: string; @@ -47,24 +51,28 @@ const GuardrailTestPlayground: React.FC = ({ accessToken, onClose, }) => { - const [selectedGuardrails, setSelectedGuardrails] = useState>(new Set()); - const [searchQuery, setSearchQuery] = useState(""); + const [selectedNames, setSelectedNames] = useQueryState("test_guardrails", selectedNamesParser); + const [searchQuery, setSearchQuery] = useQueryState("test_q", searchParser); const [testResults, setTestResults] = useState([]); const [testErrors, setTestErrors] = useState([]); const [isTesting, setIsTesting] = useState(false); + const selectedGuardrails = useMemo(() => { + const knownNames = new Set(guardrailsList.map((guardrail) => guardrail.guardrail_name)); + return new Set(selectedNames.filter((name) => knownNames.has(name))); + }, [guardrailsList, selectedNames]); + const filteredGuardrails = guardrailsList.filter((guardrail) => guardrail.guardrail_name?.toLowerCase().includes(searchQuery.toLowerCase()), ); const toggleGuardrailSelection = (guardrailName: string) => { - const newSelection = new Set(selectedGuardrails); - if (newSelection.has(guardrailName)) { - newSelection.delete(guardrailName); - } else { - newSelection.add(guardrailName); - } - setSelectedGuardrails(newSelection); + const selected = Array.from(selectedGuardrails); + void setSelectedNames( + selectedGuardrails.has(guardrailName) + ? selected.filter((name) => name !== guardrailName) + : [...selected, guardrailName], + ); }; const handleTestGuardrails = async (text: string, metadata?: Record | null) => { @@ -131,7 +139,7 @@ const GuardrailTestPlayground: React.FC = ({ setSearchQuery(e.target.value)} + onChange={(e) => void setSearchQuery(e.target.value)} />
@@ -214,7 +222,7 @@ const GuardrailTestPlayground: React.FC = ({ results={testResults.length > 0 ? testResults : null} errors={testErrors.length > 0 ? testErrors : null} isLoading={isTesting} - onClose={() => setSelectedGuardrails(new Set())} + onClose={() => void setSelectedNames(null)} />
)} diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailsPanel.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailsPanel.test.tsx index 7be120a4cb7..165d676b47f 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailsPanel.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailsPanel.test.tsx @@ -1,8 +1,8 @@ -import { type UrlUpdateEvent } from "nuqs/adapters/testing"; +import { NuqsTestingAdapter, type UrlUpdateEvent } from "nuqs/adapters/testing"; import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import GuardrailsPanel from "./GuardrailsPanel"; import { getGuardrailsList, deleteGuardrailCall } from "@/components/networking"; -import { fireEvent, renderWithProviders, screen, waitFor, within } from "@/../tests/test-utils"; +import { fireEvent, render, renderWithProviders, screen, waitFor, within } from "@/../tests/test-utils"; vi.mock("@/components/networking", () => ({ getGuardrailsList: vi.fn(), @@ -204,6 +204,23 @@ describe("GuardrailsPanel", () => { expect(await screen.findByTestId("guardrail-info-view")).toHaveTextContent("test-guardrail-1"); }); + it("should drop the detail tab together with ?guardrail= when the info view is closed", async () => { + const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); + renderWithProviders(, { + searchParams: "?tab=guardrails&guardrail=test-guardrail-1&detail_tab=settings", + onUrlUpdate, + }); + + fireEvent.click(await screen.findByRole("button", { name: "Close Guardrail Info" })); + + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + const lastUpdate = onUrlUpdate.mock.calls.at(-1)![0]; + expect(lastUpdate.searchParams.has("guardrail")).toBe(false); + expect(lastUpdate.searchParams.has("detail_tab")).toBe(false); + expect(lastUpdate.searchParams.get("tab")).toBe("guardrails"); + expect(lastUpdate.options.history).toBe("replace"); + }); + it("should clear ?guardrail= by replacing history when the info view is closed", async () => { const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); renderWithProviders(, { @@ -221,3 +238,73 @@ describe("GuardrailsPanel", () => { }); }); }); + +describe("GuardrailsPanel tab in the URL", () => { + const lastTabUpdate = (onUrlUpdate: ReturnType void>>) => + onUrlUpdate.mock.calls.at(-1)?.[0].searchParams; + + beforeEach(() => { + vi.clearAllMocks(); + vi.mocked(getGuardrailsList).mockResolvedValue({ guardrails: [] }); + }); + + it("should select the Guardrails tab for an admin when the URL names no tab", () => { + renderWithProviders(); + + expect(screen.getByRole("tab", { name: "Guardrails" })).toHaveAttribute("aria-selected", "true"); + }); + + it("should select the tab named by ?tab=", () => { + renderWithProviders(, { + searchParams: "?tab=garden", + }); + + expect(screen.getByRole("tab", { name: "Guardrail Garden" })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tab", { name: "Guardrails" })).toHaveAttribute("aria-selected", "false"); + }); + + it("should write the clicked tab to ?tab= and drop it for the default tab", async () => { + const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); + renderWithProviders(, { onUrlUpdate }); + + fireEvent.click(screen.getByRole("tab", { name: "Test Playground" })); + await waitFor(() => expect(lastTabUpdate(onUrlUpdate)?.get("tab")).toBe("test")); + expect(screen.getByRole("tab", { name: "Test Playground" })).toHaveAttribute("aria-selected", "true"); + + fireEvent.click(screen.getByRole("tab", { name: "Submitted Guardrails" })); + await waitFor(() => expect(lastTabUpdate(onUrlUpdate)?.get("tab")).toBe("submitted")); + + fireEvent.click(screen.getByRole("tab", { name: "Guardrails" })); + await waitFor(() => expect(lastTabUpdate(onUrlUpdate)?.has("tab")).toBe(false)); + expect(screen.getByRole("tab", { name: "Guardrails" })).toHaveAttribute("aria-selected", "true"); + }); + + it("should land a non-admin on Submitted Guardrails when the URL names no tab", () => { + renderWithProviders(); + + expect(screen.getByRole("tab", { name: "Submitted Guardrails" })).toHaveAttribute("aria-selected", "true"); + expect(screen.queryByRole("tab", { name: "Guardrails" })).not.toBeInTheDocument(); + expect(screen.getByText("Mock Team Guardrails Tab")).toBeInTheDocument(); + }); + + it("should send a non-admin with an admin-only ?tab= to Submitted Guardrails and drop the param", async () => { + const onUrlUpdate = vi.fn<(event: UrlUpdateEvent) => void>(); + render(, { + wrapper: ({ children }) => ( + + {children} + + ), + }); + + expect(screen.getByRole("tab", { name: "Submitted Guardrails" })).toHaveAttribute("aria-selected", "true"); + await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled()); + expect(lastTabUpdate(onUrlUpdate)?.has("tab")).toBe(false); + expect(lastTabUpdate(onUrlUpdate)?.get("other")).toBe("1"); + }); +}); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailsPanel.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailsPanel.tsx index 901e39004f3..85060ac644b 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailsPanel.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/GuardrailsPanel.tsx @@ -14,6 +14,7 @@ import { cn } from "@/lib/cva.config"; import AddGuardrailForm from "./add_guardrail_form"; import GuardrailTable from "./guardrail_table"; import { isAdminRole } from "@/utils/roles"; +import { useUrlTab } from "@/hooks/useUrlTab"; import GuardrailInfoView from "./guardrail_info"; import GuardrailTestPlayground from "./GuardrailTestPlayground"; import { toast } from "@/lib/toast"; @@ -23,6 +24,11 @@ import { formatGuardrailMode, getGuardrailLogoAndName } from "./guardrail_info_h import { CustomCodeModal } from "./custom_code"; import GuardrailGarden from "./guardrail_garden"; import { TeamGuardrailsTab } from "./TeamGuardrailsTab"; +import { GUARDRAIL_DETAIL_TAB_KEY } from "./useGuardrailDetailTab"; + +const ADMIN_TABS = ["garden", "guardrails", "test", "submitted"] as const; +type GuardrailsTab = (typeof ADMIN_TABS)[number]; +const MEMBER_TABS: readonly GuardrailsTab[] = ["submitted"]; interface GuardrailsPanelProps { accessToken: string | null; @@ -45,7 +51,9 @@ const GuardrailsPanel: React.FC = ({ accessToken, userRole "guardrail", parseAsString.withOptions({ history: "push" }), ); + const [, setDetailTab] = useQueryState(GUARDRAIL_DETAIL_TAB_KEY); const isAdmin = userRole ? isAdminRole(userRole) : false; + const [activeTab, setActiveTab] = useUrlTab(isAdmin ? ADMIN_TABS : MEMBER_TABS, isAdmin ? "guardrails" : "submitted"); const fetchGuardrails = async () => { if (!accessToken) { @@ -69,6 +77,7 @@ const GuardrailsPanel: React.FC = ({ accessToken, userRole const closeGuardrailDetail = () => { void setSelectedGuardrailId(null, { history: "replace" }); + void setDetailTab(null); }; const handleAddGuardrail = () => { @@ -133,7 +142,7 @@ const GuardrailsPanel: React.FC = ({ accessToken, userRole return (
- + {isAdmin && ( <> @@ -143,7 +152,7 @@ const GuardrailsPanel: React.FC = ({ accessToken, userRole Guardrails - + Test Playground @@ -231,7 +240,7 @@ const GuardrailsPanel: React.FC = ({ accessToken, userRole /> - + , "userRole"> = { + isLoading: false, + isAuthorized: true, token: "test-token", accessToken: "test-token", userId: "user-1", userEmail: "user@example.com", + userRoleLabel: "", + isViewOnly: false, premiumUser: false, disabledPersonalKeyCreation: null, showSSOBanner: false, @@ -96,8 +101,8 @@ describe("TeamGuardrailsTab — approve/reject role gate", () => { expect(screen.getByRole("button", { name: /reject/i })).toBeInTheDocument(); }); - it("hides Approve and Reject buttons when userRole is undefined (defaults to non-admin)", async () => { - mockUseAuthorized.mockReturnValue({ ...baseAuth, userRole: undefined }); + it("hides Approve and Reject buttons when the session has no role (defaults to non-admin)", async () => { + mockUseAuthorized.mockReturnValue({ ...baseAuth, userRole: "Undefined Role" }); renderWithProviders(); await screen.findByText("test-pending-guardrail"); @@ -140,3 +145,98 @@ describe("TeamGuardrailsTab — approve/reject role gate", () => { expect(screen.getByLabelText("Remove x-request-id")).toBeInTheDocument(); }); }); + +describe("TeamGuardrailsTab URL state", () => { + const mockUseAuthorized = vi.mocked(useAuthorized); + const lastUrlUpdate = (onUrlUpdate: ReturnType>) => + onUrlUpdate.mock.calls.at(-1)?.[0]; + + beforeEach(() => { + vi.clearAllMocks(); + mockUseAuthorized.mockReturnValue({ ...baseAuth, userRole: "Admin" }); + vi.mocked(listGuardrailSubmissions).mockResolvedValue({ + submissions: [pendingSubmission], + summary: { total: 1, pending_review: 1, active: 0, rejected: 0 }, + }); + }); + + it("loads submissions with the search and status from ?sub_q= and ?sub_status=", async () => { + renderWithProviders(, { + searchParams: "?sub_q=pii&sub_status=pending", + }); + + expect(screen.getByPlaceholderText("Search guardrails...")).toHaveValue("pii"); + expect(screen.getByLabelText("Filter by status")).toHaveValue("pending"); + await waitFor(() => + expect(listGuardrailSubmissions).toHaveBeenCalledWith("test-token", { status: "pending_review", search: "pii" }), + ); + expect(listGuardrailSubmissions).toHaveBeenCalledTimes(1); + }); + + it("treats an unknown ?sub_status= as all statuses", async () => { + renderWithProviders(, { searchParams: "?sub_status=archived" }); + + expect(screen.getByLabelText("Filter by status")).toHaveValue("all"); + await waitFor(() => + expect(listGuardrailSubmissions).toHaveBeenCalledWith("test-token", { status: undefined, search: undefined }), + ); + }); + + it("writes the search box to ?sub_q=", async () => { + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + await screen.findByText("test-pending-guardrail"); + + fireEvent.change(screen.getByPlaceholderText("Search guardrails..."), { target: { value: "toxic" } }); + + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("sub_q")).toBe("toxic")); + await waitFor(() => + expect(listGuardrailSubmissions).toHaveBeenLastCalledWith("test-token", { status: undefined, search: "toxic" }), + ); + }); + + it("writes the status filter to ?sub_status= and removes it for all statuses", async () => { + const onUrlUpdate = vi.fn(); + renderWithProviders(, { onUrlUpdate }); + await screen.findByText("test-pending-guardrail"); + const statusSelect = screen.getByLabelText("Filter by status"); + + fireEvent.change(statusSelect, { target: { value: "rejected" } }); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("sub_status")).toBe("rejected")); + await waitFor(() => + expect(listGuardrailSubmissions).toHaveBeenLastCalledWith("test-token", { + status: "rejected", + search: undefined, + }), + ); + + fireEvent.change(statusSelect, { target: { value: "all" } }); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("sub_status")).toBe(false)); + }); + + it("opens the submission named by ?submission=", async () => { + renderWithProviders(, { searchParams: "?submission=guard-1" }); + + expect(await screen.findByText("Forward LiteLLM API Key")).toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Close detail panel" })).toBeInTheDocument(); + }); + + it("pushes ?submission= when a submission is opened and replaces it away when the panel is closed", async () => { + const onUrlUpdate = vi.fn(); + renderWithProviders(, { + searchParams: "?sub_status=pending", + onUrlUpdate, + }); + + fireEvent.click(await screen.findByRole("button", { name: "Review" })); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("submission")).toBe("guard-1")); + expect(lastUrlUpdate(onUrlUpdate)?.options.history).toBe("push"); + expect(await screen.findByText("Forward LiteLLM API Key")).toBeInTheDocument(); + + fireEvent.click(screen.getByRole("button", { name: "Close detail panel" })); + await waitFor(() => expect(lastUrlUpdate(onUrlUpdate)?.searchParams.has("submission")).toBe(false)); + expect(lastUrlUpdate(onUrlUpdate)?.searchParams.get("sub_status")).toBe("pending"); + expect(lastUrlUpdate(onUrlUpdate)?.options.history).toBe("replace"); + expect(screen.queryByText("Forward LiteLLM API Key")).not.toBeInTheDocument(); + }); +}); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/TeamGuardrailsTab.tsx b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/TeamGuardrailsTab.tsx index d45cfc3fe7d..c67099ee35f 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/TeamGuardrailsTab.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/guardrails/_components/TeamGuardrailsTab.tsx @@ -40,6 +40,7 @@ import { isValidUrl } from "@/lib/forms/urlValidation"; import { useZodForm } from "@/lib/forms/useZodForm"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; +import { useTeamGuardrailsUrlState } from "./useTeamGuardrailsUrlState"; const GUARDRAIL_MODES = [ { value: "pre_call", label: "Pre Call" }, @@ -823,10 +824,9 @@ export function TeamGuardrailsTab({ accessToken }: TeamGuardrailsTabProps) { active: 0, rejected: 0, }); - const [search, setSearch] = useState(""); + const { search, setSearch, statusFilter, setStatusFilter, selectedId, openSubmission, closeSubmission } = + useTeamGuardrailsUrlState(); const [searchDebounced] = useDebouncedValue(search, { wait: DEBOUNCE_WAIT_MS }); - const [statusFilter, setStatusFilter] = useState<"all" | GuardrailStatus>("all"); - const [selectedId, setSelectedId] = useState(null); const [expandedHeaders, setExpandedHeaders] = useState>(new Set()); const [confirmAction, setConfirmAction] = useState<{ id: string; @@ -956,7 +956,7 @@ export function TeamGuardrailsTab({ accessToken }: TeamGuardrailsTabProps) { try { await approveGuardrailSubmission(accessToken, id); setConfirmAction(null); - if (selectedId === id) setSelectedId(null); + if (selectedId === id) closeSubmission(); await fetchSubmissions(); toast.success("Guardrail approved"); } catch { @@ -969,7 +969,7 @@ export function TeamGuardrailsTab({ accessToken }: TeamGuardrailsTabProps) { try { await rejectGuardrailSubmission(accessToken, id); setConfirmAction(null); - if (selectedId === id) setSelectedId(null); + if (selectedId === id) closeSubmission(); await fetchSubmissions(); toast.success("Guardrail rejected"); } catch { @@ -1009,7 +1009,7 @@ export function TeamGuardrailsTab({ accessToken }: TeamGuardrailsTabProps) {