diff --git a/ui/litellm-dashboard/src/components/add_model/add_model_tab.tsx b/ui/litellm-dashboard/src/components/add_model/add_model_tab.tsx index 362870b5621..521d84d6dfe 100644 --- a/ui/litellm-dashboard/src/components/add_model/add_model_tab.tsx +++ b/ui/litellm-dashboard/src/components/add_model/add_model_tab.tsx @@ -9,7 +9,8 @@ import AdvancedSettings from "./advanced_settings"; import { Providers, providerLogoMap, getPlaceholder } from "../provider_info_helpers"; import type { Team } from "../key_team_helpers/key_list"; import { CredentialItem } from "../networking"; -import { testModelConnection } from "./handle_add_model_submit"; +import { testModelConnection } from "./test_connection_handler"; +import ConnectionErrorDisplay from "./ConnectionErrorDisplay"; interface AddModelTabProps { form: FormInstance; @@ -55,19 +56,34 @@ const AddModelTab: React.FC = ({ credentials, accessToken, }) => { - // Add state for test mode + // Add state for test mode and connection error const [testMode, setTestMode] = useState("chat"); const [isTestModalVisible, setIsTestModalVisible] = useState(false); + const [connectionError, setConnectionError] = useState(null); // Add a function to handle test connection const handleTestConnection = async () => { - const formValues = form.getFieldsValue(); - await testModelConnection(formValues, accessToken, testMode); - setIsTestModalVisible(false); + // Clear any previous errors + setConnectionError(null); + + try { + const formValues = form.getFieldsValue(); + + // Call the existing testModelConnection function + const result = await testModelConnection(formValues, accessToken, testMode, setConnectionError); + + // Only close the modal on success + if (result && result.status === "success") { + setIsTestModalVisible(false); + } + } catch (error) { + console.error("Test connection failed:", error); + } }; // Show test modal with mode selection const showTestModal = () => { + setConnectionError(null); setIsTestModalVisible(true); }; @@ -233,10 +249,16 @@ const AddModelTab: React.FC = ({ , - ]} + width={connectionError ? 700 : 520} >
Select the mode to test this model with: @@ -252,6 +274,19 @@ const AddModelTab: React.FC = ({ Different models support different modes. Choose the appropriate mode for your model.
+ + {/* Render the ConnectionErrorDisplay when there's an error */} + {connectionError && ( +
+ Connection Test Failed +
+ +
+
+ )} ); diff --git a/ui/litellm-dashboard/src/components/add_model/handle_add_model_submit.tsx b/ui/litellm-dashboard/src/components/add_model/handle_add_model_submit.tsx index e9ea366e87e..dd2219af176 100644 --- a/ui/litellm-dashboard/src/components/add_model/handle_add_model_submit.tsx +++ b/ui/litellm-dashboard/src/components/add_model/handle_add_model_submit.tsx @@ -176,60 +176,4 @@ export const handleAddModelSubmit = async ( } }; -export const testModelConnection = async ( - formValues: Record, - accessToken: string, - testMode: string, - setConnectionError?: (error: Error | string | null) => void -) => { - try { - // Prepare the model data using the existing function - const result = await prepareModelAddRequest(formValues, accessToken, null); - - if (!result) { - throw new Error("Failed to prepare model data"); - } - - const { litellmParamsObj, modelInfoObj } = result; - - // Create the request body for the test connection - const requestBody = { - ...litellmParamsObj, // Unfurl the parameters directly - mode: testMode - }; - - // Call the test connection endpoint - const response = await testConnectionRequest(accessToken, requestBody); - - if (response.status === "success") { - message.success("Connection test successful!"); - // Clear any previous error when successful - if (setConnectionError) { - setConnectionError(null); - } - } else { - // Set the error for ConnectionErrorDisplay instead of showing a message - const errorMessage = response.message || "Unknown error"; - if (setConnectionError) { - setConnectionError(errorMessage); - } else { - message.error("Connection test failed: " + errorMessage); - } - } - - return response; - } catch (error) { - console.error("Test connection error:", error); - - // Set the error for ConnectionErrorDisplay - if (setConnectionError) { - setConnectionError(error); - } else { - message.error("Test connection failed: " + error, 10); - } - - throw error; - } -}; - diff --git a/ui/litellm-dashboard/src/components/add_model/test_connection_handler.tsx b/ui/litellm-dashboard/src/components/add_model/test_connection_handler.tsx new file mode 100644 index 00000000000..7376ee06c45 --- /dev/null +++ b/ui/litellm-dashboard/src/components/add_model/test_connection_handler.tsx @@ -0,0 +1,65 @@ +import { message } from "antd"; +import { testConnectionRequest } from "../networking"; +import { prepareModelAddRequest } from "./handle_add_model_submit"; + +export const testModelConnection = async ( + formValues: Record, + accessToken: string, + testMode: string, + setConnectionError?: (error: Error | string | null) => void +) => { + try { + // Prepare the model data using the existing function + const result = await prepareModelAddRequest(formValues, accessToken, null); + + if (!result) { + throw new Error("Failed to prepare model data"); + } + + const { litellmParamsObj, modelInfoObj } = result; + + // Create the request body for the test connection + const requestBody = { + ...litellmParamsObj, // Unfurl the parameters directly + mode: testMode + }; + + // Call the test connection endpoint + const response = await testConnectionRequest(accessToken, requestBody); + + if (response.status === "success") { + message.success("Connection test successful!"); + // Clear any previous error when successful + if (setConnectionError) { + setConnectionError(null); + } + } else { + // Extract the detailed error message from the response + let errorMessage = response.message || "Unknown error"; + + // Check if there's a more detailed error in the result + if (response.result && response.result.error) { + errorMessage = response.result.error; + } + + if (setConnectionError) { + setConnectionError(errorMessage); + } else { + message.error("Connection test failed: " + errorMessage); + } + } + + return response; + } catch (error) { + console.error("Test connection error:", error); + + // Set the error for ConnectionErrorDisplay + if (setConnectionError) { + setConnectionError(error); + } else { + message.error("Test connection failed: " + error, 10); + } + + return { status: "error", message: error instanceof Error ? error.message : String(error) }; + } +}; \ No newline at end of file