heaven_base.utils.name_utils
1import re 2import os 3from pathlib import Path 4from typing import Optional 5 6 7def get_agent_base_dir() -> str: 8 """Get the agent base directory (lazy loading to avoid import-time env var requirements).""" 9 from .get_env_value import EnvConfigUtil 10 return os.path.join(EnvConfigUtil.get_heaven_data_dir(), "agents") 11 12 13def pascal_to_snake(pascal: str) -> str: 14 """Convert PascalCase to snake_case.""" 15 # Add underscore before any capital letter and convert to lowercase 16 snake = re.sub('(?!^)([A-Z])', r'_\1', pascal).lower() 17 return snake 18 19 20def to_pascal_case(text: str) -> str: 21 # Assuming words are separated by spaces, underscores, or hyphens 22 return ''.join(word.capitalize() for word in re.split(r'[\s_-]+', text)) 23 24 25def camel_to_snake(name: str) -> str: 26 return ''.join(['_'+c.lower() if c.isupper() else c for c in name]).lstrip('_') 27 28def normalize_agent_name(name: str) -> str: 29 """Normalize agent names for filesystem consistency. 30 31 Converts: 32 - "TestAgent" -> "test_agent" 33 - "AgentmakerTestAgent" -> "agentmaker_test_agent" 34 - "ResearcherAgent" -> "researcher_agent" 35 """ 36 # Handle already snake_case names 37 if '_' in name: 38 return name.lower() 39 40 # Convert camelCase/PascalCase to snake_case 41 words = re.findall('[A-Z][^A-Z]*', name) 42 if not words: # If no capital letters found 43 return name.lower() 44 return '_'.join(word.lower() for word in words) 45 46 47 48def resolve_agent_name(agent_name_input: str) -> Optional[str]: 49 """ 50 Checks if an agent directory exists based on standard naming conventions 51 and returns the normalized snake_case name if found, otherwise None. 52 53 Args: 54 agent_name_input: The potential agent name (PascalCase or snake_case). 55 56 Returns: 57 The normalized snake_case agent name if its directory exists, otherwise None. 58 """ 59 if not agent_name_input: 60 return None 61 62 # Normalize the input name to snake_case for directory lookup 63 agent_name_snake = normalize_agent_name(agent_name_input) 64 print(f"[resolve_agent_name] Checking for '{agent_name_input}' (normalized dir: '{agent_name_snake}')") 65 66 # Construct the expected directory path 67 agent_dir_path = Path(get_agent_base_dir()) / agent_name_snake 68 69 # Check if the directory exists 70 if agent_dir_path.is_dir(): 71 print(f"[resolve_agent_name] Found directory: {agent_dir_path}") 72 # Return the normalized snake_case name used for the directory, 73 # as this is what hermes/agent loading likely expects. 74 return agent_name_snake 75 else: 76 print(f"[resolve_agent_name] Directory not found: {agent_dir_path}") 77 return None
def
get_agent_base_dir() -> str:
8def get_agent_base_dir() -> str: 9 """Get the agent base directory (lazy loading to avoid import-time env var requirements).""" 10 from .get_env_value import EnvConfigUtil 11 return os.path.join(EnvConfigUtil.get_heaven_data_dir(), "agents")
Get the agent base directory (lazy loading to avoid import-time env var requirements).
def
pascal_to_snake(pascal: str) -> str:
14def pascal_to_snake(pascal: str) -> str: 15 """Convert PascalCase to snake_case.""" 16 # Add underscore before any capital letter and convert to lowercase 17 snake = re.sub('(?!^)([A-Z])', r'_\1', pascal).lower() 18 return snake
Convert PascalCase to snake_case.
def
to_pascal_case(text: str) -> str:
def
camel_to_snake(name: str) -> str:
def
normalize_agent_name(name: str) -> str:
29def normalize_agent_name(name: str) -> str: 30 """Normalize agent names for filesystem consistency. 31 32 Converts: 33 - "TestAgent" -> "test_agent" 34 - "AgentmakerTestAgent" -> "agentmaker_test_agent" 35 - "ResearcherAgent" -> "researcher_agent" 36 """ 37 # Handle already snake_case names 38 if '_' in name: 39 return name.lower() 40 41 # Convert camelCase/PascalCase to snake_case 42 words = re.findall('[A-Z][^A-Z]*', name) 43 if not words: # If no capital letters found 44 return name.lower() 45 return '_'.join(word.lower() for word in words)
Normalize agent names for filesystem consistency.
Converts:
- "TestAgent" -> "test_agent"
- "AgentmakerTestAgent" -> "agentmaker_test_agent"
- "ResearcherAgent" -> "researcher_agent"
def
resolve_agent_name(agent_name_input: str) -> Optional[str]:
49def resolve_agent_name(agent_name_input: str) -> Optional[str]: 50 """ 51 Checks if an agent directory exists based on standard naming conventions 52 and returns the normalized snake_case name if found, otherwise None. 53 54 Args: 55 agent_name_input: The potential agent name (PascalCase or snake_case). 56 57 Returns: 58 The normalized snake_case agent name if its directory exists, otherwise None. 59 """ 60 if not agent_name_input: 61 return None 62 63 # Normalize the input name to snake_case for directory lookup 64 agent_name_snake = normalize_agent_name(agent_name_input) 65 print(f"[resolve_agent_name] Checking for '{agent_name_input}' (normalized dir: '{agent_name_snake}')") 66 67 # Construct the expected directory path 68 agent_dir_path = Path(get_agent_base_dir()) / agent_name_snake 69 70 # Check if the directory exists 71 if agent_dir_path.is_dir(): 72 print(f"[resolve_agent_name] Found directory: {agent_dir_path}") 73 # Return the normalized snake_case name used for the directory, 74 # as this is what hermes/agent loading likely expects. 75 return agent_name_snake 76 else: 77 print(f"[resolve_agent_name] Directory not found: {agent_dir_path}") 78 return None
Checks if an agent directory exists based on standard naming conventions and returns the normalized snake_case name if found, otherwise None.
Args: agent_name_input: The potential agent name (PascalCase or snake_case).
Returns: The normalized snake_case agent name if its directory exists, otherwise None.