diff --git a/swarms/cli/main.py b/swarms/cli/main.py index ef4151eb..19d2d4cf 100644 --- a/swarms/cli/main.py +++ b/swarms/cli/main.py @@ -15,12 +15,16 @@ from swarms.agents.auto_generate_swarm_config import ( from swarms.agents.create_agents_from_yaml import ( create_agents_from_yaml, ) +from swarms.structs.agent_registry import AgentRegistry from swarms.cli.onboarding_process import OnboardingProcess from swarms.utils.formatter import formatter # Initialize console with custom styling console = Console() +# Global registry for storing agents loaded via the CLI +registry = AgentRegistry() + class SwarmCLIError(Exception): """Custom exception for Swarm CLI errors""" @@ -91,6 +95,7 @@ def create_command_table() -> Table: ("get-api-key", "Retrieve your API key from the platform"), ("check-login", "Verify login status and initialize cache"), ("run-agents", "Execute agents from your YAML configuration"), + ("list-agents", "Display names of registered agents"), ("auto-upgrade", "Update Swarms to the latest version"), ("book-call", "Schedule a strategy session with our team"), ("autoswarm", "Generate and execute an autonomous swarm"), @@ -173,6 +178,38 @@ def check_login(): return True +def list_registered_agents(yaml_file: str) -> None: + """Load agents from YAML and display their names.""" + if not os.path.exists(yaml_file): + raise FileNotFoundError( + f"YAML file not found: {yaml_file}" + ) + + agents = create_agents_from_yaml( + yaml_file=yaml_file, + return_type="agents", + ) + + # Ensure agents is a list + if not isinstance(agents, list): + agents = [agents] + + registry.add_many(agents) + + table = Table( + show_header=True, + header_style=f"bold {COLORS['primary']}", + border_style=COLORS["secondary"], + title="Registered Agents", + ) + table.add_column("Agent Name", style="bold white") + + for name in registry.list_agents(): + table.add_row(name) + + console.print(table) + + def run_autoswarm(task: str, model: str): """Run autoswarm with enhanced error handling""" try: @@ -242,6 +279,7 @@ def main(): "get-api-key", "check-login", "run-agents", + "list-agents", "auto-upgrade", "book-call", "autoswarm", @@ -390,6 +428,13 @@ def main(): "2. Verify your API keys are set\n" "3. Check network connectivity", ) + elif args.command == "list-agents": + try: + list_registered_agents(args.yaml_file) + except FileNotFoundError as e: + show_error("File Error", str(e)) + except Exception as e: + show_error("Execution Error", str(e)) elif args.command == "book-call": webbrowser.open( "https://cal.com/swarms/swarms-strategy-session" diff --git a/tests/cli/test_list_agents.py b/tests/cli/test_list_agents.py new file mode 100644 index 00000000..70e93cae --- /dev/null +++ b/tests/cli/test_list_agents.py @@ -0,0 +1,74 @@ +import sys +import types +import importlib.util +from pathlib import Path +from unittest.mock import MagicMock, patch + +# Create minimal stub modules to satisfy imports in cli.main +swarms_pkg = types.ModuleType("swarms") +sys.modules["swarms"] = swarms_pkg + +agents_pkg = types.ModuleType("swarms.agents") +sys.modules["swarms.agents"] = agents_pkg + +auto_module = types.ModuleType("swarms.agents.auto_generate_swarm_config") +auto_module.generate_swarm_config = lambda *a, **k: None +sys.modules["swarms.agents.auto_generate_swarm_config"] = auto_module + +create_module = types.ModuleType("swarms.agents.create_agents_from_yaml") +create_module.create_agents_from_yaml = lambda *a, **k: [] +sys.modules["swarms.agents.create_agents_from_yaml"] = create_module + +structs_pkg = types.ModuleType("swarms.structs") +sys.modules["swarms.structs"] = structs_pkg + +agent_registry_module = types.ModuleType("swarms.structs.agent_registry") +class DummyRegistry: + def __init__(self, *_, **__): + self.agents = [] + def add_many(self, agents): + self.agents.extend(agents) + def list_agents(self): + return [a.agent_name for a in self.agents] +agent_registry_module.AgentRegistry = DummyRegistry +sys.modules["swarms.structs.agent_registry"] = agent_registry_module + +cli_pkg = types.ModuleType("swarms.cli") +sys.modules["swarms.cli"] = cli_pkg + +onboarding_module = types.ModuleType("swarms.cli.onboarding_process") +class OnboardingProcess: + def run(self): + pass +onboarding_module.OnboardingProcess = OnboardingProcess +sys.modules["swarms.cli.onboarding_process"] = onboarding_module + +utils_pkg = types.ModuleType("swarms.utils") +sys.modules["swarms.utils"] = utils_pkg + +formatter_module = types.ModuleType("swarms.utils.formatter") +class DummyFormatter: + def print_panel(self, *args, **kwargs): + pass +formatter_module.formatter = DummyFormatter() +sys.modules["swarms.utils.formatter"] = formatter_module + +# Load CLI module +spec = importlib.util.spec_from_file_location("cli_main", Path("swarms/cli/main.py")) +cli_main = importlib.util.module_from_spec(spec) +spec.loader.exec_module(cli_main) + + +def test_list_agents_command(capsys): + mock_agent1 = MagicMock(agent_name="Agent1") + mock_agent2 = MagicMock(agent_name="Agent2") + + with patch.object(cli_main, "create_agents_from_yaml", return_value=[mock_agent1, mock_agent2]): + testargs = ["swarms", "list-agents", "--yaml-file", "dummy.yaml"] + with patch.object(sys, "argv", testargs), patch.object(cli_main.os.path, "exists", return_value=True): + cli_main.main() + + captured = capsys.readouterr() + assert "Agent1" in captured.out + assert "Agent2" in captured.out +