AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
Introduce method to ignore unexpected command params (#3570)
Richard Bealesc76c67a69c89
diff --git a/autogpt/commands/command.py b/autogpt/commands/command.pyindex 742cc8df64..ed93589fe2 100644--- a/autogpt/commands/command.py+++ b/autogpt/commands/command.py@@ -1,6 +1,7 @@import functoolsimport importlibimport inspect+from inspect import Parameterfrom typing import Any, Callable, Optionalfrom autogpt.config import Config@@ -175,3 +176,32 @@ def command(return wrapperreturn 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 wrapperdiff --git a/autogpt/commands/file_operations.py b/autogpt/commands/file_operations.pyindex a0a6153905..b851d66255 100644--- a/autogpt/commands/file_operations.py+++ b/autogpt/commands/file_operations.py@@ -13,8 +13,9 @@ from confection import Configfrom requests.adapters import HTTPAdapter, Retryfrom autogpt.agent.agent import Agent-from autogpt.commands.command import command+from autogpt.commands.command import command, ignore_unexpected_kwargsfrom autogpt.commands.file_operations_utils import read_textual_file+from autogpt.config import Configfrom autogpt.logs import loggerfrom autogpt.memory.vector import MemoryItem, VectorMemoryfrom 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_kwargsdef list_files(directory: str, agent: Agent) -> list[str]:"""lists files in a directory recursively