|
| 1 | +import importlib |
| 2 | +import os |
| 3 | +import webbrowser |
| 4 | +from urllib.parse import quote_plus, urlparse |
| 5 | + |
| 6 | +from .ai import ask_model, classify_action |
| 7 | +from .apps import find_application |
| 8 | +from .config import CMD_OPEN_PHRASES, KNOWN_SITES |
| 9 | +from .memory import remember_note |
| 10 | +from .safety import BLOCKED_APPS, is_dangerous_request |
| 11 | +from .text_utils import normalize_text |
| 12 | + |
| 13 | +try: |
| 14 | + win32con = importlib.import_module("win32con") |
| 15 | + win32gui = importlib.import_module("win32gui") |
| 16 | +except ImportError: |
| 17 | + win32con = None |
| 18 | + win32gui = None |
| 19 | + |
| 20 | + |
| 21 | +def open_application(name): |
| 22 | + app = find_application(name) |
| 23 | + if not app: |
| 24 | + return f"I could not find a safe installed app named {name}." |
| 25 | + path = app["path"] |
| 26 | + os.startfile(path) |
| 27 | + return f"Opening {app['name']}." |
| 28 | + |
| 29 | + |
| 30 | +def close_visible_window(name): |
| 31 | + if win32gui is None or win32con is None: |
| 32 | + return "Close is not available because pywin32 is missing." |
| 33 | + wanted = normalize_text(name) |
| 34 | + if not wanted or wanted in BLOCKED_APPS: |
| 35 | + return "That close request is blocked for safety." |
| 36 | + matches = [] |
| 37 | + |
| 38 | + def callback(hwnd, _): |
| 39 | + if not win32gui.IsWindowVisible(hwnd): |
| 40 | + return |
| 41 | + title = win32gui.GetWindowText(hwnd) |
| 42 | + if wanted in normalize_text(title): |
| 43 | + matches.append((hwnd, title)) |
| 44 | + |
| 45 | + win32gui.EnumWindows(callback, None) |
| 46 | + if not matches: |
| 47 | + return f"I could not find an open window matching {name}." |
| 48 | + hwnd, title = matches[0] |
| 49 | + win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) |
| 50 | + return f"Closing {title or name}." |
| 51 | + |
| 52 | + |
| 53 | +def is_safe_url(url): |
| 54 | + parsed = urlparse(url) |
| 55 | + return parsed.scheme in {"http", "https"} and bool(parsed.netloc) |
| 56 | + |
| 57 | + |
| 58 | +def open_web_target(target): |
| 59 | + cleaned = normalize_text(target) |
| 60 | + url = KNOWN_SITES.get(cleaned) |
| 61 | + if not url and "." in cleaned: |
| 62 | + url = target if target.startswith(("http://", "https://")) else f"https://{target}" |
| 63 | + if not url or not is_safe_url(url): |
| 64 | + return "I can only open safe web addresses." |
| 65 | + webbrowser.open(url) |
| 66 | + return f"Opening {url}." |
| 67 | + |
| 68 | + |
| 69 | +def run_action(action): |
| 70 | + if not action: |
| 71 | + return "" |
| 72 | + action = action.strip() |
| 73 | + lowered = action.lower() |
| 74 | + if lowered == "open_cmd": |
| 75 | + os.startfile("cmd.exe") |
| 76 | + return "Opening Command Prompt." |
| 77 | + if lowered == "blocked" or is_dangerous_request(action): |
| 78 | + return "I cannot do that for safety." |
| 79 | + if lowered.startswith("open_web:"): |
| 80 | + return open_web_target(action.split(":", 1)[1].strip()) |
| 81 | + if lowered.startswith("search_google:"): |
| 82 | + query = action.split(":", 1)[1].strip().strip("<> ") |
| 83 | + if not query or is_dangerous_request(query): |
| 84 | + return "I cannot search that for safety." |
| 85 | + webbrowser.open(f"https://www.google.com/search?q={quote_plus(query)}") |
| 86 | + return f"Searching Google for {query}." |
| 87 | + if lowered.startswith("open_app:"): |
| 88 | + return open_application(action.split(":", 1)[1].strip()) |
| 89 | + if lowered.startswith("close_app:"): |
| 90 | + return close_visible_window(action.split(":", 1)[1].strip()) |
| 91 | + if lowered == "chat": |
| 92 | + return "" |
| 93 | + return "" |
| 94 | + |
| 95 | + |
| 96 | +def rule_based_action(text): |
| 97 | + cleaned = normalize_text(text) |
| 98 | + if not cleaned: |
| 99 | + return "" |
| 100 | + if cleaned in CMD_OPEN_PHRASES: |
| 101 | + return "open_cmd" |
| 102 | + if is_dangerous_request(cleaned): |
| 103 | + return "blocked" |
| 104 | + |
| 105 | + search_prefixes = ["search for ", "google search ", "look up ", "find ", "ara ", "google da ara "] |
| 106 | + for prefix in search_prefixes: |
| 107 | + if cleaned.startswith(prefix): |
| 108 | + query = cleaned.removeprefix(prefix).strip() |
| 109 | + return f"search_google:{query}" if query else "" |
| 110 | + if cleaned.endswith(" ara"): |
| 111 | + query = cleaned[: -len(" ara")].strip() |
| 112 | + return f"search_google:{query}" if query else "" |
| 113 | + |
| 114 | + close_prefixes = ["close ", "kapat ", "close the ", "can you close "] |
| 115 | + for prefix in close_prefixes: |
| 116 | + if cleaned.startswith(prefix): |
| 117 | + app = cleaned.removeprefix(prefix).strip() |
| 118 | + return f"close_app:{app}" if app else "" |
| 119 | + if cleaned.endswith(" kapat"): |
| 120 | + app = cleaned[: -len(" kapat")].strip() |
| 121 | + return f"close_app:{app}" if app else "" |
| 122 | + |
| 123 | + open_prefixes = ["open ", "launch ", "start ", "can you open ", "please open ", "ac ", "aç "] |
| 124 | + suffix_open_words = [" ac", " aç", " i ac", " i aç", " u ac", " u aç"] |
| 125 | + for site, url in KNOWN_SITES.items(): |
| 126 | + if cleaned in {site, f"open {site}", f"{site} ac", f"{site} aç"}: |
| 127 | + return f"open_web:{url}" |
| 128 | + for prefix in open_prefixes: |
| 129 | + if cleaned.startswith(prefix): |
| 130 | + target = cleaned.removeprefix(prefix).strip() |
| 131 | + if target in KNOWN_SITES: |
| 132 | + return f"open_web:{target}" |
| 133 | + return f"open_app:{target}" if target else "" |
| 134 | + for suffix in suffix_open_words: |
| 135 | + if cleaned.endswith(suffix): |
| 136 | + target = cleaned[: -len(suffix)].strip() |
| 137 | + if target in KNOWN_SITES: |
| 138 | + return f"open_web:{target}" |
| 139 | + return f"open_app:{target}" if target else "" |
| 140 | + return "" |
| 141 | + |
| 142 | + |
| 143 | +def handle_user_text(text): |
| 144 | + cleaned = normalize_text(text) |
| 145 | + if cleaned.startswith("remember that "): |
| 146 | + return remember_note(text.split("remember that", 1)[1].strip()) |
| 147 | + action = rule_based_action(text) or classify_action(text) |
| 148 | + if action.strip().lower() == "blocked" and not is_dangerous_request(text): |
| 149 | + action = "chat" |
| 150 | + answer = run_action(action) |
| 151 | + if answer: |
| 152 | + return answer |
| 153 | + return ask_model(text) |
0 commit comments