22
33from __future__ import annotations
44
5+ import importlib
56import importlib .util
67import sys
78from pathlib import Path
1011from pydantic_ai .builtin_tools import AbstractBuiltinTool
1112from pydantic_ai .ui .web import AIModel , BuiltinTool , create_chat_app
1213
13- from .agent_discovery import AgentInfo , find_agents
14-
1514
1615def load_agent_options (
1716 config_path : Path ,
@@ -47,89 +46,62 @@ def load_agent_options(
4746 return None , None , None
4847
4948
50- def select_agent (agents : list [AgentInfo ]) -> AgentInfo | None :
51- """Prompt user to select an agent from the list."""
52- if not agents :
53- print ('No agents found in the current directory.' )
54- return None
55-
56- if len (agents ) == 1 :
57- print (f'Found agent: { agents [0 ].agent_name } in { agents [0 ].file_path } ' )
58- return agents [0 ]
59-
60- print ('Multiple agents found:' )
61- for i , agent_info in enumerate (agents , 1 ):
62- print (f' { i } . { agent_info .agent_name } ({ agent_info .file_path } )' )
63-
64- while True :
65- try :
66- choice = input ('\n Select an agent (enter number): ' ).strip ()
67- index = int (choice ) - 1
68- if 0 <= index < len (agents ):
69- return agents [index ]
70- else :
71- print (f'Please enter a number between 1 and { len (agents )} ' )
72- except (ValueError , KeyboardInterrupt ):
73- print ('\n Selection cancelled.' )
74- return None
49+ def load_agent (agent_path : str ) -> Agent | None :
50+ """Load an agent from module path in uvicorn style.
7551
52+ Args:
53+ agent_path: Path in format 'module:variable', e.g. 'test_agent:my_agent'
7654
77- def load_agent (agent_info : AgentInfo ) -> Agent | None :
78- """Load an agent from the given agent info."""
55+ Returns:
56+ Agent instance or None if loading fails
57+ """
7958 sys .path .insert (0 , str (Path .cwd ()))
8059
8160 try :
82- spec = importlib . util . spec_from_file_location ( agent_info . module_path , agent_info . file_path )
83- if spec is None or spec . loader is None :
84- print (f 'Error: Could not load module from { agent_info . file_path } ' )
85- return None
61+ module_path , variable_name = agent_path . split ( ':' )
62+ except ValueError :
63+ print ('Error: Agent must be specified in "module:variable" format ' )
64+ return None
8665
87- module = importlib . util . module_from_spec ( spec )
88- sys . modules [ agent_info . module_path ] = module
89- spec . loader . exec_module (module )
66+ try :
67+ module = importlib . import_module ( module_path )
68+ agent = getattr (module , variable_name , None )
9069
91- agent = getattr (module , agent_info .agent_name , None )
9270 if agent is None :
93- print (f'Error: Agent { agent_info . agent_name } not found in module' )
71+ print (f'Error: { variable_name } not found in module { module_path } ' )
9472 return None
9573
9674 if not isinstance (agent , Agent ):
97- print (f'Error: { agent_info . agent_name } is not an Agent instance' )
75+ print (f'Error: { variable_name } is not an Agent instance' )
9876 return None
9977
10078 return agent # pyright: ignore[reportUnknownVariableType]
10179
80+ except ImportError as e :
81+ print (f'Error: Could not import module { module_path } : { e } ' )
82+ return None
10283 except Exception as e :
10384 print (f'Error loading agent: { e } ' )
10485 return None
10586
10687
10788def run_chat_command (
108- root_dir : Path | None = None ,
89+ agent_path : str ,
10990 host : str = '127.0.0.1' ,
11091 port : int = 8000 ,
11192 config_path : Path | None = None ,
11293 auto_config : bool = True ,
11394) -> int :
114- """Run the chat command to discover and serve an agent.
95+ """Run the chat command to serve an agent via web UI .
11596
11697 Args:
117- root_dir: Directory to search for agents (defaults to current directory)
98+ agent_path: Agent path in 'module:variable' format, e.g. 'test_agent:my_agent'
11899 host: Host to bind the server to
119100 port: Port to bind the server to
120101 config_path: Path to agent_options.py config file
121102 auto_config: Auto-discover agent_options.py in current directory
122103 """
123- search_dir = root_dir or Path .cwd ()
124-
125- print (f'Searching for agents in { search_dir } ...' )
126- agents = find_agents (search_dir )
127-
128- selected = select_agent (agents )
129- if selected is None :
130- return 1
131-
132- agent = load_agent (selected )
104+ agent = load_agent (agent_path )
133105 if agent is None :
134106 return 1
135107
@@ -145,7 +117,7 @@ def run_chat_command(
145117
146118 app = create_chat_app (agent , models = models , builtin_tools = builtin_tools , builtin_tool_defs = builtin_tool_defs )
147119
148- print (f'\n Starting chat UI for { selected . agent_name } ...' )
120+ print (f'\n Starting chat UI for { agent_path } ...' )
149121 print (f'Open your browser at: http://{ host } :{ port } ' )
150122 print ('Press Ctrl+C to stop the server\n ' )
151123
0 commit comments