import os import subprocess from datetime import datetime from config import OS_COMMANDS, TEMP_DIR def execute_command(command_type: str, command_text: str) -> str: try: if command_type == "open_app": for app, cmd in OS_COMMANDS.get("open", {}).items(): if app in command_text.lower(): subprocess.Popen(cmd.split()) return f"✅ Opening {app.title()}..." elif command_type == "close_app": for app, cmd in OS_COMMANDS.get("close", {}).items(): if app in command_text.lower(): os.system(cmd) return f"🛑 Closing {app.title()}..." elif command_type == "screenshot": screenshot_path = os.path.join(TEMP_DIR, f"screenshot_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png") cmd = OS_COMMANDS["screenshot"]["default"] cmd = cmd.replace("screenshot.png", screenshot_path) os.system(cmd) return f"📸 Screenshot saved to: {screenshot_path}" elif command_type == "create_file": filename = "new_file.txt" with open(filename, "w") as f: f.write("Hello from your AI assistant.") return f"✅ File '{filename}' created." elif command_type == "shutdown": os.system(OS_COMMANDS["shutdown"]) return "🔌 Shutting down..." elif command_type == "restart": os.system(OS_COMMANDS["restart"]) return "🔄 Restarting system..." elif command_type == "lock": os.system(OS_COMMANDS["lock"]) return "🔒 System locked." except Exception as e: return f"❌ Error while executing command: {e}" return "⚠️ Command not recognized or not supported yet."