mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2026-01-04 23:04:28 +01:00
Introduce method to ignore unexpected command params (#3570)
Co-authored-by: Nicholas Tindle <nick@ntindle.com> Co-authored-by: Reinier van der Leer <github@pwuts.nl> Co-authored-by: Luke K <2609441+lc0rp@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import functools
|
||||
import importlib
|
||||
import inspect
|
||||
from inspect import Parameter
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
from autogpt.config import Config
|
||||
@@ -175,3 +176,32 @@ def command(
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def ignore_unexpected_kwargs(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||
def filter_kwargs(kwargs: dict) -> dict:
|
||||
sig = inspect.signature(func)
|
||||
# Parameter.VAR_KEYWORD - a dict of keyword arguments that aren't bound to any other
|
||||
if any(map(lambda p: p.kind == Parameter.VAR_KEYWORD, sig.parameters.values())):
|
||||
# if **kwargs exist, return directly
|
||||
return kwargs
|
||||
|
||||
_params = list(
|
||||
filter(
|
||||
lambda p: p.kind
|
||||
in {Parameter.KEYWORD_ONLY, Parameter.POSITIONAL_OR_KEYWORD},
|
||||
sig.parameters.values(),
|
||||
)
|
||||
)
|
||||
|
||||
res_kwargs = {
|
||||
param.name: kwargs[param.name] for param in _params if param.name in kwargs
|
||||
}
|
||||
return res_kwargs
|
||||
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs) -> Any:
|
||||
kwargs = filter_kwargs(kwargs)
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
@@ -13,8 +13,9 @@ from confection import Config
|
||||
from requests.adapters import HTTPAdapter, Retry
|
||||
|
||||
from autogpt.agent.agent import Agent
|
||||
from autogpt.commands.command import command
|
||||
from autogpt.commands.command import command, ignore_unexpected_kwargs
|
||||
from autogpt.commands.file_operations_utils import read_textual_file
|
||||
from autogpt.config import Config
|
||||
from autogpt.logs import logger
|
||||
from autogpt.memory.vector import MemoryItem, VectorMemory
|
||||
from autogpt.spinner import Spinner
|
||||
@@ -308,6 +309,7 @@ def delete_file(filename: str, agent: Agent) -> str:
|
||||
|
||||
|
||||
@command("list_files", "List Files in Directory", '"directory": "<directory>"')
|
||||
@ignore_unexpected_kwargs
|
||||
def list_files(directory: str, agent: Agent) -> list[str]:
|
||||
"""lists files in a directory recursively
|
||||
|
||||
|
||||
Reference in New Issue
Block a user