-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from esbmc/addon-support
Addon Support for ChatCommand
- Loading branch information
Showing
15 changed files
with
236 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Contains code for managing and running built-in and addon commands.""" | ||
|
||
import re | ||
from esbmc_ai.commands.chat_command import ChatCommand | ||
from esbmc_ai.commands.help_command import HelpCommand | ||
|
||
|
||
class CommandRunner: | ||
"""Command runner manages running and storing commands.""" | ||
|
||
def __init__(self, builtin_commands: list[ChatCommand]) -> None: | ||
self._builtin_commands: list[ChatCommand] = builtin_commands.copy() | ||
self._addon_commands: list[ChatCommand] = [] | ||
|
||
# Set the help command commands | ||
for cmd in self._builtin_commands: | ||
if cmd.command_name == "help": | ||
assert isinstance(cmd, HelpCommand) | ||
cmd.commands = self.commands | ||
|
||
@property | ||
def commands(self) -> list[ChatCommand]: | ||
"""Returns all commands. The list is copied.""" | ||
return self._builtin_commands + self._addon_commands | ||
|
||
@property | ||
def command_names(self) -> list[str]: | ||
"""Returns a list of built-in commands. This is a reference to the | ||
internal list.""" | ||
return [cmd.command_name for cmd in self.commands] | ||
|
||
@property | ||
def builtin_commands_names(self) -> list[str]: | ||
"""Returns a list of built-in command names.""" | ||
return [cmd.command_name for cmd in self._builtin_commands] | ||
|
||
@property | ||
def addon_commands_names(self) -> list[str]: | ||
"""Returns a list of the addon command names.""" | ||
return [cmd.command_name for cmd in self._addon_commands] | ||
|
||
@property | ||
def addon_commands(self) -> list[ChatCommand]: | ||
"""Returns a list of the addon commands. This is a reference to the | ||
internal list.""" | ||
return self._addon_commands | ||
|
||
@staticmethod | ||
def parse_command(user_prompt_string: str) -> tuple[str, list[str]]: | ||
"""Parses a command and returns it based on the command rules outlined in | ||
the wiki: https://github.com/Yiannis128/esbmc-ai/wiki/User-Chat-Mode""" | ||
regex_pattern: str = ( | ||
r'\s+(?=(?:[^\\"]*(?:\\.[^\\"]*)*)$)|(?:(?<!\\)".*?(?<!\\)")|(?:\\.)+|\S+' | ||
) | ||
segments: list[str] = re.findall(regex_pattern, user_prompt_string) | ||
parsed_array: list[str] = [segment for segment in segments if segment != " "] | ||
# Remove all empty spaces. | ||
command: str = parsed_array[0] | ||
command_args: list[str] = parsed_array[1:] | ||
return command, command_args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
# Author: Yiannis Charalambous | ||
|
||
from abc import abstractmethod | ||
"""Contains the base class for chat command results.""" | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class CommandResult(ABC): | ||
"""Base class for the return result of chat commands.""" | ||
|
||
class CommandResult: | ||
@property | ||
@abstractmethod | ||
def successful(self) -> bool: | ||
"""Returns true if the execution of the command was successful. False if | ||
otherwise. If false, then it is up to the command to provide a method to | ||
access the reason.""" | ||
raise NotImplementedError() | ||
|
||
def __str__(self) -> str: | ||
return f"Command returned " + ( | ||
return "Command returned " + ( | ||
"successful" if self.successful else "unsuccessful" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.