feat(agent/cli): Speed up loading of saved agents

O(n) to O(1) by removing individual `.exists(dir / "state.json")` calls and using `.list_files()` instead of `.list_folders()`
This commit is contained in:
Reinier van der Leer
2024-04-24 18:52:48 +02:00
parent 1afc8e40df
commit 126aacb2e3

View File

@@ -20,9 +20,9 @@ class AgentManager:
def list_agents(self) -> list[str]: def list_agents(self) -> list[str]:
"""Return all agent directories within storage.""" """Return all agent directories within storage."""
agent_dirs: list[str] = [] agent_dirs: list[str] = []
for dir in self.file_manager.list_folders(): for file_path in self.file_manager.list_files():
if self.file_manager.exists(dir / "state.json"): if len(file_path.parts) == 2 and file_path.name == "state.json":
agent_dirs.append(dir.name) agent_dirs.append(file_path.parent.name)
return agent_dirs return agent_dirs
def get_agent_dir(self, agent_id: str) -> Path: def get_agent_dir(self, agent_id: str) -> Path: