pull/876/merge
Pavan Kumar 1 month ago committed by GitHub
commit 3573d4ccc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,11 @@
# Test configuration for listing agents via CLI
# This file demonstrates a simple multi-agent setup for testing purposes
agents:
- agent_name: "Test-Agent-1"
model_name: "gpt-4o-mini"
system_prompt: "You are Test-Agent-1, designed to handle Task 1 efficiently."
task: "Task 1: Analyze provided text for sentiment and key themes."
- agent_name: "Test-Agent-2"
model_name: "gpt-4o-mini"
system_prompt: "You are Test-Agent-2, specialized in processing and responding to Task 2."
task: "Task 2: Generate a concise summary of the provided information."

@ -15,12 +15,16 @@ from swarms.agents.auto_generate_swarm_config import (
from swarms.agents.create_agents_from_yaml import ( from swarms.agents.create_agents_from_yaml import (
create_agents_from_yaml, create_agents_from_yaml,
) )
from swarms.structs.agent_registry import AgentRegistry
from swarms.cli.onboarding_process import OnboardingProcess from swarms.cli.onboarding_process import OnboardingProcess
from swarms.utils.formatter import formatter from swarms.utils.formatter import formatter
# Initialize console with custom styling # Initialize console with custom styling
console = Console() console = Console()
# Global registry for storing agents loaded via the CLI
registry = AgentRegistry()
class SwarmCLIError(Exception): class SwarmCLIError(Exception):
"""Custom exception for Swarm CLI errors""" """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"), ("get-api-key", "Retrieve your API key from the platform"),
("check-login", "Verify login status and initialize cache"), ("check-login", "Verify login status and initialize cache"),
("run-agents", "Execute agents from your YAML configuration"), ("run-agents", "Execute agents from your YAML configuration"),
("list-agents", "Display names of registered agents"),
("auto-upgrade", "Update Swarms to the latest version"), ("auto-upgrade", "Update Swarms to the latest version"),
("book-call", "Schedule a strategy session with our team"), ("book-call", "Schedule a strategy session with our team"),
("autoswarm", "Generate and execute an autonomous swarm"), ("autoswarm", "Generate and execute an autonomous swarm"),
@ -173,6 +178,38 @@ def check_login():
return True 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): def run_autoswarm(task: str, model: str):
"""Run autoswarm with enhanced error handling""" """Run autoswarm with enhanced error handling"""
try: try:
@ -242,6 +279,7 @@ def main():
"get-api-key", "get-api-key",
"check-login", "check-login",
"run-agents", "run-agents",
"list-agents",
"auto-upgrade", "auto-upgrade",
"book-call", "book-call",
"autoswarm", "autoswarm",
@ -390,6 +428,13 @@ def main():
"2. Verify your API keys are set\n" "2. Verify your API keys are set\n"
"3. Check network connectivity", "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": elif args.command == "book-call":
webbrowser.open( webbrowser.open(
"https://cal.com/swarms/swarms-strategy-session" "https://cal.com/swarms/swarms-strategy-session"

@ -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
Loading…
Cancel
Save