| 12 | |
| 13 | |
| 14 | class TermNetAgent: |
| 15 | def __init__(self, terminal, notification_server_url: str = "http://localhost:5003"): |
| 16 | self.terminal = terminal |
| 17 | self.session_id = hashlib.md5(str(time.time()).encode()).hexdigest()[:8] |
| 18 | self.cache: Dict[str, Tuple[float, str, int, bool]] = {} |
| 19 | self.current_goal = "" |
| 20 | self.last_activity = time.time() |
| 21 | self.notification_check_interval = 300 # 5 minutes |
| 22 | |
| 23 | self.notification_server_url = notification_server_url |
| 24 | self.notifications: List[Dict] = [] # cache of latest notifications |
| 25 | |
| 26 | # 🖥️ Detect operating system |
| 27 | self.os_info = self._detect_os() |
| 28 | |
| 29 | # 🔌 Load tools dynamically |
| 30 | self.tool_loader = ToolLoader() |
| 31 | self.tool_loader.load_tools() |
| 32 | |
| 33 | # Conversation history with initial system message |
| 34 | self.conversation_history: List[Dict[str, str]] = [ |
| 35 | {"role": "system", "content": self._get_system_prompt()} |
| 36 | ] |
| 37 | |
| 38 | # Start background refresh for notifications |
| 39 | self.notification_task = asyncio.create_task(self._auto_refresh_prompt()) |
| 40 | |
| 41 | # ----------------------------- |
| 42 | # SYSTEM DETECTION |
| 43 | # ----------------------------- |
| 44 | def _detect_os(self) -> Dict[str, str]: |
| 45 | """Detect operating system and return relevant info""" |
| 46 | system = platform.system().lower() |
| 47 | |
| 48 | os_info = { |
| 49 | "system": system, |
| 50 | "platform": platform.platform(), |
| 51 | "architecture": platform.architecture()[0], |
| 52 | "python_version": platform.python_version(), |
| 53 | } |
| 54 | |
| 55 | # Add OS-specific info |
| 56 | if system == "darwin": |
| 57 | os_info["os_type"] = "macOS" |
| 58 | os_info["shell_hint"] = "Use bash/zsh commands, brew for packages" |
| 59 | os_info["package_manager"] = "brew" |
| 60 | elif system == "linux": |
| 61 | os_info["os_type"] = "Linux" |
| 62 | os_info["shell_hint"] = "Use bash commands, apt/yum/pacman for packages" |
| 63 | # Try to detect package manager |
| 64 | import shutil |
| 65 | if shutil.which("apt"): |
| 66 | os_info["package_manager"] = "apt" |
| 67 | elif shutil.which("yum"): |
| 68 | os_info["package_manager"] = "yum" |
| 69 | elif shutil.which("pacman"): |
| 70 | os_info["package_manager"] = "pacman" |
| 71 | else: |