|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from litellm import ChatCompletionRequest |
| 4 | + |
| 5 | +from codegate.db.connection import DbReader |
| 6 | +from codegate.pipeline.base import ( |
| 7 | + PipelineContext, |
| 8 | + PipelineResponse, |
| 9 | + PipelineResult, |
| 10 | + PipelineStep, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class WorkspaceCommands: |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + self._db_recorder = DbReader() |
| 18 | + self.commands = { |
| 19 | + "list": self._list_workspaces, |
| 20 | + } |
| 21 | + |
| 22 | + async def _list_workspaces(self, *args): |
| 23 | + """ |
| 24 | + List all workspaces |
| 25 | + """ |
| 26 | + workspaces = await self._db_recorder.get_workspaces() |
| 27 | + print(workspaces) |
| 28 | + respond_str = "" |
| 29 | + for workspace in workspaces: |
| 30 | + respond_str += f"{workspace.id} - {workspace.name}" |
| 31 | + if workspace.is_active: |
| 32 | + respond_str += " (active)" |
| 33 | + respond_str += "\n" |
| 34 | + return respond_str |
| 35 | + |
| 36 | + async def execute(self, command: str, *args) -> str: |
| 37 | + """ |
| 38 | + Execute the given command |
| 39 | +
|
| 40 | + Args: |
| 41 | + command (str): The command to execute |
| 42 | + """ |
| 43 | + command_to_execute = self.commands.get(command) |
| 44 | + if command_to_execute is not None: |
| 45 | + return await command_to_execute(*args) |
| 46 | + else: |
| 47 | + return "Command not found" |
| 48 | + |
| 49 | + async def parse_execute_cmd(self, last_user_message: str) -> str: |
| 50 | + """ |
| 51 | + Parse the last user message and execute the command |
| 52 | +
|
| 53 | + Args: |
| 54 | + last_user_message (str): The last user message |
| 55 | + """ |
| 56 | + command_and_args = last_user_message.split("codegate-workspace ")[1] |
| 57 | + command, *args = command_and_args.split(" ") |
| 58 | + return await self.execute(command, *args) |
| 59 | + |
| 60 | + |
| 61 | +class CodegateWorkspace(PipelineStep): |
| 62 | + """Pipeline step that handles workspace information requests.""" |
| 63 | + |
| 64 | + @property |
| 65 | + def name(self) -> str: |
| 66 | + """ |
| 67 | + Returns the name of this pipeline step. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + str: The identifier 'codegate-workspace' |
| 71 | + """ |
| 72 | + return "codegate-workspace" |
| 73 | + |
| 74 | + async def process( |
| 75 | + self, request: ChatCompletionRequest, context: PipelineContext |
| 76 | + ) -> PipelineResult: |
| 77 | + """ |
| 78 | + Checks if the last user message contains "codegate-workspace" and |
| 79 | + responds with command specified. |
| 80 | + This short-circuits the pipeline if the message is found. |
| 81 | +
|
| 82 | + Args: |
| 83 | + request (ChatCompletionRequest): The chat completion request to process |
| 84 | + context (PipelineContext): The current pipeline context |
| 85 | +
|
| 86 | + Returns: |
| 87 | + PipelineResult: Contains workspace response if triggered, otherwise continues |
| 88 | + pipeline |
| 89 | + """ |
| 90 | + last_user_message = self.get_last_user_message(request) |
| 91 | + |
| 92 | + if last_user_message is not None: |
| 93 | + last_user_message_str, _ = last_user_message |
| 94 | + if "codegate-workspace" in last_user_message_str.lower(): |
| 95 | + context.shortcut_response = True |
| 96 | + command_output = await WorkspaceCommands().parse_execute_cmd(last_user_message_str) |
| 97 | + return PipelineResult( |
| 98 | + response=PipelineResponse( |
| 99 | + step_name=self.name, |
| 100 | + content=command_output, |
| 101 | + model=request["model"], |
| 102 | + ), |
| 103 | + context=context, |
| 104 | + ) |
| 105 | + |
| 106 | + # Fall through |
| 107 | + return PipelineResult(request=request, context=context) |
0 commit comments