-
Notifications
You must be signed in to change notification settings - Fork 43.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor module layout of command classes
- Loading branch information
1 parent
f0a5250
commit 6470e44
Showing
24 changed files
with
117 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import functools | ||
from typing import Any, Callable, Optional | ||
|
||
from autogpt.config import Config | ||
from autogpt.logs import logger | ||
from autogpt.models.command import Command | ||
|
||
# Unique identifier for auto-gpt commands | ||
AUTO_GPT_COMMAND_IDENTIFIER = "auto_gpt_command" | ||
|
||
|
||
def command( | ||
name: str, | ||
description: str, | ||
signature: str, | ||
enabled: bool | Callable[[Config], bool] = True, | ||
disabled_reason: Optional[str] = None, | ||
) -> Callable[..., Any]: | ||
"""The command decorator is used to create Command objects from ordinary functions.""" | ||
|
||
# TODO: Remove this in favor of better command management | ||
CFG = Config() | ||
|
||
if callable(enabled): | ||
enabled = enabled(CFG) | ||
if not enabled: | ||
if disabled_reason is not None: | ||
logger.debug(f"Command '{name}' is disabled: {disabled_reason}") | ||
return lambda func: func | ||
|
||
def decorator(func: Callable[..., Any]) -> Command: | ||
cmd = Command( | ||
name=name, | ||
description=description, | ||
method=func, | ||
signature=signature, | ||
enabled=enabled, | ||
disabled_reason=disabled_reason, | ||
) | ||
|
||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs) -> Any: | ||
return func(*args, **kwargs) | ||
|
||
wrapper.command = cmd | ||
|
||
setattr(wrapper, AUTO_GPT_COMMAND_IDENTIFIER, True) | ||
|
||
return wrapper | ||
|
||
return decorator |
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
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
Empty file.
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,41 @@ | ||
from typing import Any, Callable, Optional | ||
|
||
from autogpt.config import Config | ||
|
||
|
||
class Command: | ||
"""A class representing a command. | ||
Attributes: | ||
name (str): The name of the command. | ||
description (str): A brief description of what the command does. | ||
signature (str): The signature of the function that the command executes. Defaults to None. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
description: str, | ||
method: Callable[..., Any], | ||
signature: str = "", | ||
enabled: bool | Callable[[Config], bool] = True, | ||
disabled_reason: Optional[str] = None, | ||
): | ||
self.name = name | ||
self.description = description | ||
self.method = method | ||
self.signature = signature | ||
self.enabled = enabled | ||
self.disabled_reason = disabled_reason | ||
|
||
def __call__(self, *args, **kwargs) -> Any: | ||
if hasattr(kwargs, "config") and callable(self.enabled): | ||
self.enabled = self.enabled(kwargs["config"]) | ||
if not self.enabled: | ||
if self.disabled_reason: | ||
return f"Command '{self.name}' is disabled: {self.disabled_reason}" | ||
return f"Command '{self.name}' is disabled" | ||
return self.method(*args, **kwargs) | ||
|
||
def __str__(self) -> str: | ||
return f"{self.name}: {self.description}, args: {self.signature}" |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from autogpt.commands.command import command | ||
from autogpt.command_decorator import command | ||
|
||
|
||
@command( | ||
|
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