From ec07b096dcdae241adfaf4063850d384cdd87972 Mon Sep 17 00:00:00 2001 From: maglore9900 Date: Thu, 29 Aug 2024 10:56:54 -0400 Subject: [PATCH] added fallback on features added fallback on spotify added subprocess to app launcher so it releases max while the app is open --- modules/agent.py | 54 ++++++++++++++++----------- modules/app_launcher.py | 3 +- modules/windows_focus.py | 80 ++++++++++++++++++++++++++-------------- 3 files changed, 87 insertions(+), 50 deletions(-) diff --git a/modules/agent.py b/modules/agent.py index 0a71b8b..26fdcf4 100644 --- a/modules/agent.py +++ b/modules/agent.py @@ -122,28 +122,38 @@ class Agent: return {"agent_out": agent_out} async def spotify_tool(self, state: str): - print("> spotify_tool") - print(f"state: {state}") - tool_action = state['agent_out'][0] - command = tool_action.tool_input['command'] - print(f"command: {command}") - # print(f"search: {search}") - if command == "play": - self.sp.play() - elif command == "pause": - self.sp.pause() - elif command == "stop": - self.sp.pause() - elif command == "next": - self.sp.next_track() - elif command == "previous": - self.sp.previous_track() - elif command == "favorite": - self.sp.favorite_current_song() - # elif command == "search": - # self.sp.search_song_and_play(search) - else: - print("Invalid command") + try: + print("> spotify_tool") + print(f"state: {state}") + tool_action = state['agent_out'][0] + + # Inline lambda to get 'command' or 'self' from tool_input + command = (lambda x: x.get('command') or x.get('self'))(tool_action.tool_input) + + if not command: + raise ValueError("No valid command found in tool_input") + + print(f"command: {command}") + + # Handling the command + if command == "play": + self.sp.play() + elif command == "pause": + self.sp.pause() + elif command == "stop": + self.sp.pause() + elif command == "next": + self.sp.next_track() + elif command == "previous": + self.sp.previous_track() + elif command == "favorite": + self.sp.favorite_current_song() + else: + print("Invalid command") + + except Exception as e: + print(f"An error occurred: {e}") + async def app_launcher_tool(self, state: str): print("> app_launcher_tool") diff --git a/modules/app_launcher.py b/modules/app_launcher.py index 264ebcc..52c6798 100644 --- a/modules/app_launcher.py +++ b/modules/app_launcher.py @@ -59,8 +59,9 @@ class AppLauncher: if match: app_path = self.index[match[0]]['path'] - subprocess.run(app_path) + subprocess.Popen(app_path) # Use Popen instead of run print(f"Opening {match[0]} at {app_path}") else: print(f"No matching application found for '{app_name}'") + diff --git a/modules/windows_focus.py b/modules/windows_focus.py index e19f162..ea1c9c1 100644 --- a/modules/windows_focus.py +++ b/modules/windows_focus.py @@ -1,45 +1,71 @@ import win32gui import win32con +import win32api +import pywintypes class WindowFocusManager: def __init__(self): self.windows = [] def enum_windows_callback(self, hwnd, window_list): - # Append the window handle and title to the list if it's visible - if win32gui.IsWindowVisible(hwnd): - window_list.append((hwnd, win32gui.GetWindowText(hwnd))) + try: + if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd): + window_list.append((hwnd, win32gui.GetWindowText(hwnd))) + except pywintypes.error as e: + print(f"Error enumerating window: {e}") def find_windows(self, partial_window_title): - self.windows = [] - win32gui.EnumWindows(self.enum_windows_callback, self.windows) - - # Filter windows that match the partial title - matching_windows = [hwnd for hwnd, title in self.windows if partial_window_title.lower() in title.lower()] - return matching_windows + try: + self.windows = [] + win32gui.EnumWindows(self.enum_windows_callback, self.windows) + + matching_windows = [hwnd for hwnd, title in self.windows if partial_window_title.lower() in title.lower()] + return matching_windows + except pywintypes.error as e: + print(f"Error finding windows: {e}") + return [] def bring_window_to_front(self, hwnd): - # Bring the window to the foreground - win32gui.SetForegroundWindow(hwnd) + try: + # Ensure the window is not minimized + if win32gui.IsIconic(hwnd): + try: + win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) + except pywintypes.error as e: + print(f"Error restoring window: {e}") + + # Bring the window to the foreground + try: + win32gui.SetForegroundWindow(hwnd) + except pywintypes.error as e: + print(f"Error setting foreground window: {e}") + + # Optionally, send a series of ALT key presses to help with focus + try: + win32api.keybd_event(win32con.VK_MENU, 0, win32con.KEYEVENTF_KEYUP, 0) + win32gui.SetForegroundWindow(hwnd) + except pywintypes.error as e: + print(f"Error sending ALT key event: {e}") + + window_title = win32gui.GetWindowText(hwnd) + print(f"Brought window '{window_title}' to the front.") - # If the window is minimized, restore it - if win32gui.IsIconic(hwnd): - win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) - - window_title = win32gui.GetWindowText(hwnd) - print(f"Brought window '{window_title}' to the front.") + except pywintypes.error as e: + print(f"Failed to bring window to front: {e}") def bring_specific_instance_to_front(self, partial_window_title): - matching_windows = self.find_windows(partial_window_title) - - if matching_windows: - # If there are multiple matches, select the first one (or customize as needed) - hwnd = matching_windows[0] - self.bring_window_to_front(hwnd) - else: - print(f"No windows found with title containing '{partial_window_title}'.") + try: + matching_windows = self.find_windows(partial_window_title) + + if matching_windows: + hwnd = matching_windows[0] + self.bring_window_to_front(hwnd) + else: + print(f"No windows found with title containing '{partial_window_title}'.") + except pywintypes.error as e: + print(f"Error bringing specific instance to front: {e}") # Example usage: # window_manager = WindowFocusManager() -# window_manager.bring_specific_instance_to_front("outlook") # Bring the first matching instance of Visual Studio Code to the front -# window_manager.bring_specific_instance_to_front("Notepad") # Bring the first matching instance of Notepad to the front +# window_manager.bring_specific_instance_to_front("Visual Studio Code") # Bring the first matching instance of Visual Studio Code to the front +# window_manager.bring_specific_instance_to_front("Chrome") # Bring the first matching instance of Chrome to the front