added fallback on features

added fallback on spotify
added subprocess to app launcher so it releases max while the app is open
This commit is contained in:
maglore9900 2024-08-29 10:56:54 -04:00
parent 46e61fc9fe
commit ec07b096dc
3 changed files with 87 additions and 50 deletions

View File

@ -122,28 +122,38 @@ class Agent:
return {"agent_out": agent_out} return {"agent_out": agent_out}
async def spotify_tool(self, state: str): async def spotify_tool(self, state: str):
print("> spotify_tool") try:
print(f"state: {state}") print("> spotify_tool")
tool_action = state['agent_out'][0] print(f"state: {state}")
command = tool_action.tool_input['command'] tool_action = state['agent_out'][0]
print(f"command: {command}")
# print(f"search: {search}") # Inline lambda to get 'command' or 'self' from tool_input
if command == "play": command = (lambda x: x.get('command') or x.get('self'))(tool_action.tool_input)
self.sp.play()
elif command == "pause": if not command:
self.sp.pause() raise ValueError("No valid command found in tool_input")
elif command == "stop":
self.sp.pause() print(f"command: {command}")
elif command == "next":
self.sp.next_track() # Handling the command
elif command == "previous": if command == "play":
self.sp.previous_track() self.sp.play()
elif command == "favorite": elif command == "pause":
self.sp.favorite_current_song() self.sp.pause()
# elif command == "search": elif command == "stop":
# self.sp.search_song_and_play(search) self.sp.pause()
else: elif command == "next":
print("Invalid command") 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): async def app_launcher_tool(self, state: str):
print("> app_launcher_tool") print("> app_launcher_tool")

View File

@ -59,8 +59,9 @@ class AppLauncher:
if match: if match:
app_path = self.index[match[0]]['path'] 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}") print(f"Opening {match[0]} at {app_path}")
else: else:
print(f"No matching application found for '{app_name}'") print(f"No matching application found for '{app_name}'")

View File

@ -1,45 +1,71 @@
import win32gui import win32gui
import win32con import win32con
import win32api
import pywintypes
class WindowFocusManager: class WindowFocusManager:
def __init__(self): def __init__(self):
self.windows = [] self.windows = []
def enum_windows_callback(self, hwnd, window_list): def enum_windows_callback(self, hwnd, window_list):
# Append the window handle and title to the list if it's visible try:
if win32gui.IsWindowVisible(hwnd): if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd):
window_list.append((hwnd, 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): def find_windows(self, partial_window_title):
self.windows = [] try:
win32gui.EnumWindows(self.enum_windows_callback, self.windows) 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()] matching_windows = [hwnd for hwnd, title in self.windows if partial_window_title.lower() in title.lower()]
return matching_windows return matching_windows
except pywintypes.error as e:
print(f"Error finding windows: {e}")
return []
def bring_window_to_front(self, hwnd): def bring_window_to_front(self, hwnd):
# Bring the window to the foreground try:
win32gui.SetForegroundWindow(hwnd) # 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 except pywintypes.error as e:
if win32gui.IsIconic(hwnd): print(f"Failed to bring window to front: {e}")
win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
window_title = win32gui.GetWindowText(hwnd)
print(f"Brought window '{window_title}' to the front.")
def bring_specific_instance_to_front(self, partial_window_title): def bring_specific_instance_to_front(self, partial_window_title):
matching_windows = self.find_windows(partial_window_title) try:
matching_windows = self.find_windows(partial_window_title)
if matching_windows:
# If there are multiple matches, select the first one (or customize as needed) if matching_windows:
hwnd = matching_windows[0] hwnd = matching_windows[0]
self.bring_window_to_front(hwnd) self.bring_window_to_front(hwnd)
else: else:
print(f"No windows found with title containing '{partial_window_title}'.") 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: # Example usage:
# window_manager = WindowFocusManager() # 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("Visual Studio Code") # 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("Chrome") # Bring the first matching instance of Chrome to the front