The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(ui): drop stale user search answers so Enter commits the current match
mateo-berrif614f039c56c
diff --git a/ui/litellm-dashboard/src/components/common_components/user_search_modal.test.tsx b/ui/litellm-dashboard/src/components/common_components/user_search_modal.test.tsxindex cbaf00b7131..98b85c738b4 100644--- a/ui/litellm-dashboard/src/components/common_components/user_search_modal.test.tsx+++ b/ui/litellm-dashboard/src/components/common_components/user_search_modal.test.tsx@@ -198,3 +198,58 @@ describe("UserSearchModal submit payload", () => {await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));});});++describe("UserSearchModal out-of-order search results", () => {+ const answers = new Map<string, (users: { user_id: string; user_email: string }[]) => void>();++ beforeEach(() => {+ answers.clear();+ vi.mocked(userFilterUICall).mockReset();+ vi.mocked(userFilterUICall).mockImplementation(+ (_accessToken, params) =>+ new Promise((resolve) => {+ answers.set(params.get("user_email") ?? "", resolve);+ }) as never,+ );+ });++ const answerFor = async (search: string, users: { user_id: string; user_email: string }[]) => {+ const resolve = answers.get(search);+ if (resolve === undefined) throw new Error(`no pending search for "${search}"`);+ await act(async () => {+ resolve(users);+ });+ };++ it("commits the current search's match when an abandoned search answers last", async () => {+ const user = userEvent.setup();+ const onSubmit = vi.fn();+ render(<UserSearchModal isVisible onCancel={vi.fn()} onSubmit={onSubmit} accessToken="sk-test" />);++ const input = getEmailSearchInput();+ await user.click(input);+ await user.type(input, "ali");+ await waitFor(() => expect(answers.has("ali")).toBe(true), { timeout: 3000 });++ await user.type(input, "[email protected]");+ await waitFor(() => expect(answers.has("[email protected]")).toBe(true), { timeout: 3000 });++ await answerFor("[email protected]", [{ user_id: "u-smith", user_email: "[email protected]" }]);+ await screen.findByRole("option", { name: "[email protected]" });++ await answerFor("ali", [+ { user_id: "u-jones", user_email: "[email protected]" },+ { user_id: "u-smith", user_email: "[email protected]" },+ ]);++ await user.keyboard("{Enter}");+ await user.click(screen.getByRole("button", { name: /add member/i }));++ await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));+ expect(onSubmit.mock.calls[0][0]).toStrictEqual({+ user_email: "[email protected]",+ user_id: "u-smith",+ role: "user",+ });+ });+});diff --git a/ui/litellm-dashboard/src/components/common_components/user_search_modal.tsx b/ui/litellm-dashboard/src/components/common_components/user_search_modal.tsxindex aeead9d82e6..71d38c6db25 100644--- a/ui/litellm-dashboard/src/components/common_components/user_search_modal.tsx+++ b/ui/litellm-dashboard/src/components/common_components/user_search_modal.tsx@@ -1,4 +1,4 @@-import { useState } from "react";+import { useRef, useState } from "react";import { Modal, Alert } from "antd";import { UserAddOutlined } from "@ant-design/icons";import { useDebouncedCallback } from "@tanstack/react-pacer/debouncer";@@ -78,8 +78,13 @@ const UserSearchModal: React.FC<UserSearchModalProps> = ({const [loading, setLoading] = useState<boolean>(false);const [selectedField, setSelectedField] = useState<"user_email" | "user_id">("user_email");const [isSubmitting, setIsSubmitting] = useState(false);+ const latestSearchRef = useRef(0);const fetchUsers = async (searchText: string, fieldName: "user_email" | "user_id"): Promise<void> => {+ const searchId = latestSearchRef.current + 1;+ latestSearchRef.current = searchId;+ const isLatestSearch = (): boolean => searchId === latestSearchRef.current;+if (!searchText) {setUserOptions([]);return;@@ -96,6 +101,7 @@ const UserSearchModal: React.FC<UserSearchModalProps> = ({return;}const response = await userFilterUICall(accessToken, params);+ if (!isLatestSearch()) return;const data: User[] = response;const options: UserOption[] = data.map((user) => ({@@ -107,7 +113,7 @@ const UserSearchModal: React.FC<UserSearchModalProps> = ({} catch (error) {console.error("Error fetching users:", error);} finally {- setLoading(false);+ if (isLatestSearch()) setLoading(false);}};diff --git a/ui/litellm-dashboard/src/components/organisms/create_key_button.integration.test.tsx b/ui/litellm-dashboard/src/components/organisms/create_key_button.integration.test.tsxindex 9788d365fd0..c413bbeebfc 100644--- a/ui/litellm-dashboard/src/components/organisms/create_key_button.integration.test.tsx+++ b/ui/litellm-dashboard/src/components/organisms/create_key_button.integration.test.tsx@@ -1,6 +1,6 @@import userEvent from "@testing-library/user-event";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-import { renderWithProviders, screen, testQueryClient, waitFor } from "../../../tests/test-utils";+import { act, renderWithProviders, screen, testQueryClient, waitFor } from "../../../tests/test-utils";import type { Team } from "../key_team_helpers/key_list";import { keyCreateCall, keyCreateServiceAccountCall, modelAvailableCall, userFilterUICall } from "../networking";import CreateKey from "./create_key_button";@@ -803,6 +803,38 @@ describe("CreateKey", () => {vi.useRealTimers();}});++ it("keeps the current search's users when an abandoned search answers last", async () => {+ const answers = new Map<string, (users: { user_id: string; user_email: string }[]) => void>();+ vi.mocked(userFilterUICall).mockImplementation(+ (_accessToken, params) =>+ new Promise((resolve) => {+ answers.set(params.get("user_email") ?? "", resolve);+ }) as never,+ );++ const user = userEvent.setup();+ renderCreateKey({ autoOpenCreate: true, prefillData: { owned_by: "another_user" } });+ const search = antdSearchInput(await screen.findByText("Type email to search for users"));++ await user.type(search, "ali");+ await waitFor(() => expect(answers.has("ali")).toBe(true), { timeout: 3000 });++ await user.type(search, "[email protected]");+ await waitFor(() => expect(answers.has("[email protected]")).toBe(true), { timeout: 3000 });++ await act(async () => {+ answers.get("[email protected]")?.([{ user_id: "u-smith", user_email: "[email protected]" }]);+ });+ await screen.findByTitle("[email protected] (u-smith)");++ await act(async () => {+ answers.get("ali")?.([{ user_id: "u-jones", user_email: "[email protected]" }]);+ });++ expect(screen.queryByTitle("[email protected] (u-jones)")).not.toBeInTheDocument();+ expect(screen.getByTitle("[email protected] (u-smith)")).toBeInTheDocument();+ });});describe("created key display", () => {diff --git a/ui/litellm-dashboard/src/components/organisms/create_key_button.tsx b/ui/litellm-dashboard/src/components/organisms/create_key_button.tsxindex 77ef31e0ede..1e7fff687f1 100644--- a/ui/litellm-dashboard/src/components/organisms/create_key_button.tsx+++ b/ui/litellm-dashboard/src/components/organisms/create_key_button.tsx@@ -27,7 +27,7 @@ import {import { ChevronDown } from "lucide-react";import { useDebouncedCallback } from "@tanstack/react-pacer/debouncer";import { DEBOUNCE_WAIT_MS } from "@/utils/debounceConstants";-import React, { useEffect, useState } from "react";+import React, { useEffect, useRef, useState } from "react";import { rolesWithWriteAccess } from "../../utils/roles";import AgentSelector from "../agent_management/AgentSelector";import AccessGroupSelector from "../common_components/AccessGroupSelector";@@ -195,6 +195,7 @@ const CreateKey: React.FC<CreateKeyProps> = ({ team, teams, data, addKey, autoOpconst [possibleUIRoles, setPossibleUIRoles] = useState<Record<string, Record<string, string>>>({});const [userOptions, setUserOptions] = useState<UserOption[]>([]);const [userSearchLoading, setUserSearchLoading] = useState<boolean>(false);+ const latestUserSearchRef = useRef(0);const [disabledCallbacks, setDisabledCallbacks] = useState<string[]>([]);const [keyType, setKeyType] = useState<string>("llm_api");const [modelAliases, setModelAliases] = useState<{ [key: string]: string }>({});@@ -503,6 +504,10 @@ const CreateKey: React.FC<CreateKeyProps> = ({ team, teams, data, addKey, autoOp};const fetchUsers = async (searchText: string): Promise<void> => {+ const searchId = latestUserSearchRef.current + 1;+ latestUserSearchRef.current = searchId;+ const isLatestSearch = (): boolean => searchId === latestUserSearchRef.current;+if (!searchText) {setUserOptions([]);return;@@ -516,6 +521,7 @@ const CreateKey: React.FC<CreateKeyProps> = ({ team, teams, data, addKey, autoOpreturn;}const response = await userFilterUICall(accessToken, params);+ if (!isLatestSearch()) return;const data: User[] = response;const options: UserOption[] = data.map((user) => ({@@ -529,7 +535,7 @@ const CreateKey: React.FC<CreateKeyProps> = ({ team, teams, data, addKey, autoOpconsole.error("Error fetching users:", error);toast.fromError("Failed to search for users");} finally {- setUserSearchLoading(false);+ if (isLatestSearch()) setUserSearchLoading(false);}};