Enhance import handling for Hecate in terminal_tool.py to manage local folder shadowing and improve error reporting for import failures.

This commit is contained in:
teknium 2025-10-03 09:46:44 +00:00
parent c42d9055ed
commit 6fac6fecde

View file

@ -116,15 +116,29 @@ def terminal_tool(
# even when hecate is not installed. If unavailable, gracefully # even when hecate is not installed. If unavailable, gracefully
# indicate that the terminal tool is disabled. # indicate that the terminal tool is disabled.
try: try:
from hecate import run_tool_with_lifecycle_management # Primary import path when the hecate package is properly installed
try:
from hecate import run_tool_with_lifecycle_management # type: ignore
except ImportError as primary_import_error:
# Fallback for when a local folder named "hecate" shadows the installed package
# (common when the repo is cloned inside the project root). In that case,
# the actual implementation lives under hecate.hecate.cli.
try:
from hecate.hecate.cli import run_tool_with_lifecycle_management # type: ignore
except Exception as fallback_import_error:
raise ImportError(
f"Failed to import 'run_tool_with_lifecycle_management' from hecate: "
f"{primary_import_error}; fallback failed: {fallback_import_error}"
)
from morphcloud._llm import ToolCall from morphcloud._llm import ToolCall
except ImportError: except ImportError as import_error:
return json.dumps({ return json.dumps({
"output": "", "output": "",
"screen": "", "screen": "",
"session_id": None, "session_id": None,
"exit_code": -1, "exit_code": -1,
"error": "Terminal tool is disabled: 'hecate' is not installed. Install with: pip install hecate", "error": f"Terminal tool is disabled due to import error: {import_error}",
"status": "disabled" "status": "disabled"
}) })
@ -197,12 +211,16 @@ def check_hecate_requirements() -> bool:
print(f"Warning: Missing optional environment variables: {', '.join(missing_optional)}") print(f"Warning: Missing optional environment variables: {', '.join(missing_optional)}")
print(" (Some Hecate features may be limited)") print(" (Some Hecate features may be limited)")
# Check if Hecate is importable # Check if Hecate entrypoint is importable (handle local-folder shadowing)
try: try:
import hecate try:
return True from hecate import run_tool_with_lifecycle_management # type: ignore
except ImportError: except ImportError:
print("Hecate is not installed. Please install it with: pip install hecate") from hecate.hecate.cli import run_tool_with_lifecycle_management # type: ignore
return True
except Exception as e:
print(f"Hecate not available: {e}\nIf you cloned the hecate repo into this project, it may shadow the installed package. "
f"Either install it (pip install -e hecate) and/or move/rename the local 'hecate' folder.")
return False return False
# Module-level initialization check # Module-level initialization check