1- import { useCallback , useState } from "react"
1+ import { useCallback , useState , useEffect , useRef } from "react"
22import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
33import { useEvent } from "react-use"
44
@@ -24,6 +24,7 @@ export const LiteLLM = ({ apiConfiguration, setApiConfigurationField, routerMode
2424 const { t } = useAppTranslation ( )
2525 const [ refreshStatus , setRefreshStatus ] = useState < "idle" | "loading" | "success" | "error" > ( "idle" )
2626 const [ refreshError , setRefreshError ] = useState < string | undefined > ( )
27+ const initialRefreshPerformedRef = useRef ( false )
2728
2829 const handleInputChange = useCallback (
2930 < K extends keyof ProviderSettings , E > (
@@ -36,14 +37,18 @@ export const LiteLLM = ({ apiConfiguration, setApiConfigurationField, routerMode
3637 [ setApiConfigurationField ] ,
3738 )
3839
39- const handleRefreshModels = ( ) => {
40+ const handleRefreshModels = useCallback ( ( ) => {
4041 setRefreshStatus ( "loading" )
4142 setRefreshError ( undefined )
4243
43- // Due to the button's disabled state logic, litellmApiKey and litellmBaseUrl are guaranteed to be non-empty strings here.
44- // We use non-null assertions (!) to reflect this guarantee for type safety.
45- const key = apiConfiguration . litellmApiKey !
46- const url = apiConfiguration . litellmBaseUrl !
44+ const key = apiConfiguration . litellmApiKey
45+ const url = apiConfiguration . litellmBaseUrl
46+
47+ if ( ! key || ! url ) {
48+ setRefreshStatus ( "error" )
49+ setRefreshError ( t ( "settings:providers.refreshModels.missingConfig" ) )
50+ return
51+ }
4752
4853 const message : WebviewMessage = {
4954 type : "requestProviderModels" ,
@@ -54,9 +59,34 @@ export const LiteLLM = ({ apiConfiguration, setApiConfigurationField, routerMode
5459 } ,
5560 }
5661 vscode . postMessage ( message )
57- }
62+ } , [ apiConfiguration . litellmApiKey , apiConfiguration . litellmBaseUrl , setRefreshStatus , setRefreshError , t ] )
63+
64+ // Effect to trigger initial model refresh, once per component instance when conditions are met
65+ useEffect ( ( ) => {
66+ // Only proceed if the initial refresh for this component instance hasn't been done
67+ if ( initialRefreshPerformedRef . current ) {
68+ return
69+ }
70+
71+ // Check if the necessary configuration is available
72+ if ( apiConfiguration . litellmApiKey && apiConfiguration . litellmBaseUrl ) {
73+ // Mark that we are performing the refresh for this instance
74+ initialRefreshPerformedRef . current = true
75+ // Directly execute refresh logic
76+ setRefreshStatus ( "loading" )
77+ setRefreshError ( undefined )
78+ const message : WebviewMessage = {
79+ type : "requestProviderModels" ,
80+ payload : {
81+ provider : "litellm" ,
82+ apiKey : apiConfiguration . litellmApiKey ,
83+ baseUrl : apiConfiguration . litellmBaseUrl ,
84+ } ,
85+ }
86+ vscode . postMessage ( message )
87+ }
88+ } , [ apiConfiguration . litellmApiKey , apiConfiguration . litellmBaseUrl ] )
5889
59- // Listen for model refresh responses using useEvent
6090 useEvent ( "message" , ( event : MessageEvent < ExtensionMessage > ) => {
6191 const message = event . data
6292 if ( message . type === "providerModelsResponse" ) {
@@ -67,7 +97,6 @@ export const LiteLLM = ({ apiConfiguration, setApiConfigurationField, routerMode
6797 setRefreshError ( message . payload . error )
6898 } else {
6999 setRefreshStatus ( "success" )
70- // Parent (ApiOptions.tsx) will handle updating the routerModels prop for ModelPicker
71100 }
72101 } else {
73102 console . log (
@@ -77,7 +106,6 @@ export const LiteLLM = ({ apiConfiguration, setApiConfigurationField, routerMode
77106 }
78107 }
79108 } )
80- console . log ( "apiconfig1212" , apiConfiguration )
81109
82110 return (
83111 < >
0 commit comments