mirror of https://github.com/aliasrobotics/cai.git
fix: gracefully handle missing OpenAI API key during agent import
This commit is contained in:
parent
e22a1220f7
commit
fb8a81f87d
|
|
@ -98,8 +98,20 @@ def get_available_agents() -> Dict[str, Agent]: # pylint: disable=R0912 # noqa
|
|||
agent_name = attr_name
|
||||
if agent_name not in agents_to_display:
|
||||
agents_to_display[agent_name] = attr
|
||||
except (ImportError, AttributeError):
|
||||
pass
|
||||
except Exception as e:
|
||||
# Handle missing API key errors gracefully
|
||||
error_str = str(e)
|
||||
module_short_name = name.split('.')[-1]
|
||||
|
||||
if "api_key" in error_str and "client option" in error_str:
|
||||
print(f"\n[!] Warning: Agent '{module_short_name}' skipped: Missing OpenAI API Key.")
|
||||
print(" Set the OPENAI_API_KEY environment variable to enable it.")
|
||||
elif isinstance(e, (ImportError, AttributeError)):
|
||||
# Ignore standard import errors
|
||||
pass
|
||||
else:
|
||||
# Log other unexpected errors
|
||||
print(f"Error importing {module_short_name}: {e}")
|
||||
|
||||
# Also check the patterns subdirectory
|
||||
patterns_path = os.path.join(os.path.dirname(__file__), "patterns")
|
||||
|
|
@ -119,10 +131,19 @@ def get_available_agents() -> Dict[str, Agent]: # pylint: disable=R0912 # noqa
|
|||
agent_name = attr_name
|
||||
if agent_name not in agents_to_display:
|
||||
agents_to_display[agent_name] = attr
|
||||
except (ImportError, AttributeError) as e:
|
||||
except Exception as e:
|
||||
# Extract module name from the full import path
|
||||
module_short_name = name.split('.')[-1]
|
||||
print(f"Error importing {module_short_name}: {e}")
|
||||
|
||||
error_str = str(e)
|
||||
if "api_key" in error_str and "client option" in error_str:
|
||||
print(f"\n[!] Warning: Pattern '{module_short_name}' skipped: Missing OpenAI API Key.")
|
||||
print(" Set the OPENAI_API_KEY environment variable to enable it.")
|
||||
elif isinstance(e, (ImportError, AttributeError)):
|
||||
# Ignore standard import errors, or print if critical
|
||||
pass
|
||||
else:
|
||||
print(f"Error importing {module_short_name}: {e}")
|
||||
|
||||
# Add all patterns (parallel, swarm, etc.) as pseudo-agents
|
||||
from cai.agents.patterns import PATTERNS
|
||||
|
|
@ -172,7 +193,8 @@ def get_agent_module(agent_name: str) -> str:
|
|||
# Try both with and without _agent suffix
|
||||
if (attr_name == agent_name) and isinstance(getattr(module, attr_name), Agent):
|
||||
return name
|
||||
except (ImportError, AttributeError):
|
||||
except (ImportError, AttributeError, Exception):
|
||||
# If a module fails to load (e.g. API key missing), just skip it
|
||||
pass
|
||||
|
||||
# Also check the patterns subdirectory
|
||||
|
|
@ -186,7 +208,7 @@ def get_agent_module(agent_name: str) -> str:
|
|||
# Try both with and without _agent suffix
|
||||
if (attr_name == agent_name) and isinstance(getattr(module, attr_name), Agent):
|
||||
return name
|
||||
except (ImportError, AttributeError):
|
||||
except (ImportError, AttributeError, Exception):
|
||||
pass
|
||||
|
||||
return "unknown"
|
||||
|
|
|
|||
Loading…
Reference in New Issue