|
| 1 | +import datetime |
| 2 | +from typing import Optional, Tuple |
| 3 | + |
| 4 | +from codegate.db.connection import DbReader, DbRecorder |
| 5 | +from codegate.db.models import Session, Workspace |
| 6 | + |
| 7 | + |
| 8 | +class WorkspaceCrud: |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + self._db_reader = DbReader() |
| 12 | + |
| 13 | + async def add_workspace(self, new_workspace_name: str) -> bool: |
| 14 | + """ |
| 15 | + Add a workspace |
| 16 | +
|
| 17 | + Args: |
| 18 | + name (str): The name of the workspace |
| 19 | + """ |
| 20 | + db_recorder = DbRecorder() |
| 21 | + workspace_created = await db_recorder.add_workspace(new_workspace_name) |
| 22 | + return bool(workspace_created) |
| 23 | + |
| 24 | + async def get_workspaces(self): |
| 25 | + """ |
| 26 | + Get all workspaces |
| 27 | + """ |
| 28 | + return await self._db_reader.get_workspaces() |
| 29 | + |
| 30 | + async def _is_workspace_active_or_not_exist( |
| 31 | + self, workspace_name: str |
| 32 | + ) -> Tuple[bool, Optional[Session], Optional[Workspace]]: |
| 33 | + """ |
| 34 | + Check if the workspace is active |
| 35 | +
|
| 36 | + Will return: |
| 37 | + - True if the workspace was activated |
| 38 | + - False if the workspace is already active or does not exist |
| 39 | + """ |
| 40 | + selected_workspace = await self._db_reader.get_workspace_by_name(workspace_name) |
| 41 | + if not selected_workspace: |
| 42 | + return True, None, None |
| 43 | + |
| 44 | + sessions = await self._db_reader.get_sessions() |
| 45 | + # The current implementation expects only one active session |
| 46 | + if len(sessions) != 1: |
| 47 | + raise RuntimeError("Something went wrong. No active session found.") |
| 48 | + |
| 49 | + session = sessions[0] |
| 50 | + if session.active_workspace_id == selected_workspace.id: |
| 51 | + return True, None, None |
| 52 | + return False, session, selected_workspace |
| 53 | + |
| 54 | + async def activate_workspace(self, workspace_name: str) -> bool: |
| 55 | + """ |
| 56 | + Activate a workspace |
| 57 | +
|
| 58 | + Will return: |
| 59 | + - True if the workspace was activated |
| 60 | + - False if the workspace is already active or does not exist |
| 61 | + """ |
| 62 | + is_active, session, workspace = await self._is_workspace_active_or_not_exist(workspace_name) |
| 63 | + if is_active: |
| 64 | + return False |
| 65 | + |
| 66 | + session.active_workspace_id = workspace.id |
| 67 | + session.last_update = datetime.datetime.now(datetime.timezone.utc) |
| 68 | + db_recorder = DbRecorder() |
| 69 | + await db_recorder.update_session(session) |
| 70 | + return True |
| 71 | + |
| 72 | + |
| 73 | +class WorkspaceCommands: |
| 74 | + |
| 75 | + def __init__(self): |
| 76 | + self.workspace_crud = WorkspaceCrud() |
| 77 | + self.commands = { |
| 78 | + "list": self._list_workspaces, |
| 79 | + "add": self._add_workspace, |
| 80 | + "activate": self._activate_workspace, |
| 81 | + } |
| 82 | + |
| 83 | + async def _list_workspaces(self, *args) -> str: |
| 84 | + """ |
| 85 | + List all workspaces |
| 86 | + """ |
| 87 | + workspaces = await self.workspace_crud.get_workspaces() |
| 88 | + respond_str = "" |
| 89 | + for workspace in workspaces: |
| 90 | + respond_str += f"- {workspace.name}" |
| 91 | + if workspace.active_workspace_id: |
| 92 | + respond_str += " **(active)**" |
| 93 | + respond_str += "\n" |
| 94 | + return respond_str |
| 95 | + |
| 96 | + async def _add_workspace(self, *args) -> str: |
| 97 | + """ |
| 98 | + Add a workspace |
| 99 | + """ |
| 100 | + if args is None or len(args) == 0: |
| 101 | + return "Please provide a name. Use `codegate-workspace add your_workspace_name`" |
| 102 | + |
| 103 | + new_workspace_name = args[0] |
| 104 | + if not new_workspace_name: |
| 105 | + return "Please provide a name. Use `codegate-workspace add your_workspace_name`" |
| 106 | + |
| 107 | + workspace_created = await self.workspace_crud.add_workspace(new_workspace_name) |
| 108 | + if not workspace_created: |
| 109 | + return ( |
| 110 | + "Something went wrong. Workspace could not be added.\n" |
| 111 | + "1. Check if the name is alphanumeric and only contains dashes, and underscores.\n" |
| 112 | + "2. Check if the workspace already exists." |
| 113 | + ) |
| 114 | + return f"Workspace **{new_workspace_name}** has been added" |
| 115 | + |
| 116 | + async def _activate_workspace(self, *args) -> str: |
| 117 | + """ |
| 118 | + Activate a workspace |
| 119 | + """ |
| 120 | + if args is None or len(args) == 0: |
| 121 | + return "Please provide a name. Use `codegate-workspace activate workspace_name`" |
| 122 | + |
| 123 | + workspace_name = args[0] |
| 124 | + if not workspace_name: |
| 125 | + return "Please provide a name. Use `codegate-workspace activate workspace_name`" |
| 126 | + |
| 127 | + was_activated = await self.workspace_crud.activate_workspace(workspace_name) |
| 128 | + if not was_activated: |
| 129 | + return ( |
| 130 | + f"Workspace **{workspace_name}** does not exist or was already active. " |
| 131 | + f"Use `codegate-workspace add {workspace_name}` to add it" |
| 132 | + ) |
| 133 | + return f"Workspace **{workspace_name}** has been activated" |
| 134 | + |
| 135 | + async def execute(self, command: str, *args) -> str: |
| 136 | + """ |
| 137 | + Execute the given command |
| 138 | +
|
| 139 | + Args: |
| 140 | + command (str): The command to execute |
| 141 | + """ |
| 142 | + command_to_execute = self.commands.get(command) |
| 143 | + if command_to_execute is not None: |
| 144 | + return await command_to_execute(*args) |
| 145 | + else: |
| 146 | + return "Command not found" |
| 147 | + |
| 148 | + async def parse_execute_cmd(self, last_user_message: str) -> str: |
| 149 | + """ |
| 150 | + Parse the last user message and execute the command |
| 151 | +
|
| 152 | + Args: |
| 153 | + last_user_message (str): The last user message |
| 154 | + """ |
| 155 | + command_and_args = last_user_message.lower().split("codegate-workspace ")[1] |
| 156 | + command, *args = command_and_args.split(" ") |
| 157 | + return await self.execute(command, *args) |
0 commit comments