mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-25 09:54:23 +01:00
* Command name supports multiple names * Separate CommandRegistry.commands and .command_aliases * Update test_commands.py * Add __contains__ operator to CommandRegistry * Update error message for unknown commands --------- Co-authored-by: Reinier van der Leer <github@pwuts.nl>
58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import functools
|
|
from typing import Any, Callable, Optional, TypedDict
|
|
|
|
from autogpt.config import Config
|
|
from autogpt.models.command import Command, CommandParameter
|
|
|
|
# Unique identifier for auto-gpt commands
|
|
AUTO_GPT_COMMAND_IDENTIFIER = "auto_gpt_command"
|
|
|
|
|
|
class CommandParameterSpec(TypedDict):
|
|
type: str
|
|
description: str
|
|
required: bool
|
|
|
|
|
|
def command(
|
|
name: str,
|
|
description: str,
|
|
parameters: dict[str, CommandParameterSpec],
|
|
enabled: bool | Callable[[Config], bool] = True,
|
|
disabled_reason: Optional[str] = None,
|
|
aliases: list[str] = [],
|
|
) -> Callable[..., Any]:
|
|
"""The command decorator is used to create Command objects from ordinary functions."""
|
|
|
|
def decorator(func: Callable[..., Any]) -> Command:
|
|
typed_parameters = [
|
|
CommandParameter(
|
|
name=param_name,
|
|
description=parameter.get("description"),
|
|
type=parameter.get("type", "string"),
|
|
required=parameter.get("required", False),
|
|
)
|
|
for param_name, parameter in parameters.items()
|
|
]
|
|
cmd = Command(
|
|
name=name,
|
|
description=description,
|
|
method=func,
|
|
parameters=typed_parameters,
|
|
enabled=enabled,
|
|
disabled_reason=disabled_reason,
|
|
aliases=aliases,
|
|
)
|
|
|
|
@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
|