@@ -267,27 +267,65 @@ def fetch_models(
267267 try :
268268 toolbox_dir = Path (__file__ ).parent .parent
269269 # Security: Use shlex.split() instead of shell=True to prevent injection
270+ import contextlib
271+ import importlib
272+ import io
270273 import shlex
274+ import shutil
271275
272- result = subprocess .run (
273- shlex .split (list_cmd ),
274- shell = False ,
275- capture_output = True ,
276- text = True ,
277- cwd = toolbox_dir ,
278- env = env ,
279- timeout = 60 ,
280- )
276+ tokens = shlex .split (list_cmd )
277+
278+ output = ""
279+
280+ # Support running specific python -m modules by calling them directly
281+ if (
282+ len (tokens ) >= 3
283+ and tokens [1 ] == "-m"
284+ and tokens [2 ]
285+ in (
286+ "code_assistant_manager.litellm_models" ,
287+ "code_assistant_manager.copilot_models" ,
288+ )
289+ ):
290+ module_name = tokens [2 ]
291+ try :
292+ mod = importlib .import_module (module_name )
293+ buf = io .StringIO ()
294+ with contextlib .redirect_stdout (buf ):
295+ # Call list_models() which prints model IDs
296+ mod .list_models ()
297+ output = buf .getvalue ().strip ()
298+ except Exception as e :
299+ print (f"Warning: Failed to load module { module_name } : { e } " )
300+ return True , models
301+
302+ # If first token is not an executable on PATH, treat the value as literal model list
303+ elif tokens and shutil .which (tokens [0 ]) is None :
304+ output = " " .join (tokens ).strip ()
305+
306+ else :
307+ # Fallback: run as subprocess
308+ result = subprocess .run (
309+ tokens ,
310+ shell = False ,
311+ capture_output = True ,
312+ text = True ,
313+ cwd = toolbox_dir ,
314+ env = env ,
315+ timeout = 60 ,
316+ )
281317
282- output = result .stdout .strip ()
283- if result .returncode != 0 :
284- print (f"Warning: Command failed with return code { result .returncode } " )
285- if result .stderr :
286- print (f"Command stderr: { result .stderr } " )
287- return True , models
288- if not output :
289- print ("Warning: Command returned no output" )
290- return True , models
318+ output = result .stdout .strip ()
319+ if result .returncode != 0 :
320+ print (
321+ f"Warning: Command failed with return code { result .returncode } "
322+ )
323+ if result .stderr :
324+ print (f"Command stderr: { result .stderr } " )
325+ return True , models
326+ if not output :
327+ print ("Warning: Command returned no output" )
328+ return True , models
291329
292330 # Parse output
293331 models = self ._parse_models_output (output )
0 commit comments