Pass Configs to Commands and remove CFG = Config() in the commands/ folder (#4328)

* feat: pass config to call_ai_functions in coimmands

* feat: config for read_audio_from_file

* feat: file operations cfg

NOTE: we replaced the CFG in the command enable with TRUE b/c not sure how to handle this yet

* feat: git command conversion

* feat: google search

* feat: image generation

* feat: extract cfg from browser commands

* feat: remove cfg from execute code commands

* fix: file operation related tests

* fix: linting

* fix: tests for read_audio

* fix: test error

* feat: update cassettes

* fix: linting

* fix: test typechecking

* fix: google_search errors if unexpected kw arg is passed

* fix: pass config param to google search test

* fix: agent commands were broken + cassettes

* fix: agent test

* feat: cassettes

* feat: enable/disable logic for commands

* fix: some commands threw errors

* feat: fix tests

* Add new cassettes

* Add new cassettes

* ci: trigger ci

* Update autogpt/commands/execute_code.py

Co-authored-by: Reinier van der Leer <github@pwuts.nl>

* fix prompt

* fix prompt + rebase

* add config remove useless imports

* put back CFG just for download file

* lint

* The signature should be mandatory in the decorator

* black isort

* fix: remove the CFG

* fix: non typed arg

* lint: type some args

* lint: add types for libraries

* Add new cassettes

* fix: windows compatibility

* fix: add config access to decorator

* fix: remove twitter mention

* DDGS search works at 3.0.2 version

* ci: linting

---------

Co-authored-by: Auto-GPT-Bot <github-bot@agpt.co>
Co-authored-by: merwanehamadi <merwanehamadi@gmail.com>
Co-authored-by: Reinier van der Leer <github@pwuts.nl>
Co-authored-by: kinance <kinance@gmail.com>
This commit is contained in:
Nicholas Tindle
2023-05-26 10:39:25 -05:00
committed by GitHub
parent f07fcdf0a7
commit acfd966aa4
46 changed files with 1851 additions and 396 deletions

View File

@@ -55,11 +55,11 @@ def test_file(test_file_path: Path):
@pytest.fixture()
def test_file_with_content_path(test_file: TextIOWrapper, file_content):
def test_file_with_content_path(test_file: TextIOWrapper, file_content, config):
test_file.write(file_content)
test_file.close()
file_ops.log_operation(
"write", test_file.name, file_ops.text_checksum(file_content)
"write", test_file.name, config, file_ops.text_checksum(file_content)
)
return Path(test_file.name)
@@ -117,7 +117,7 @@ def test_file_operations_state(test_file: TextIOWrapper):
assert file_ops.file_operations_state(test_file.name) == expected_state
def test_is_duplicate_operation(config, mocker: MockerFixture):
def test_is_duplicate_operation(config: Config, mocker: MockerFixture):
# Prepare a fake state dictionary for the function to use
state = {
"path/to/file1.txt": "checksum1",
@@ -127,30 +127,42 @@ def test_is_duplicate_operation(config, mocker: MockerFixture):
# Test cases with write operations
assert (
file_ops.is_duplicate_operation("write", "path/to/file1.txt", "checksum1")
file_ops.is_duplicate_operation(
"write", "path/to/file1.txt", config, "checksum1"
)
is True
)
assert (
file_ops.is_duplicate_operation("write", "path/to/file1.txt", "checksum2")
file_ops.is_duplicate_operation(
"write", "path/to/file1.txt", config, "checksum2"
)
is False
)
assert (
file_ops.is_duplicate_operation("write", "path/to/file3.txt", "checksum3")
file_ops.is_duplicate_operation(
"write", "path/to/file3.txt", config, "checksum3"
)
is False
)
# Test cases with append operations
assert (
file_ops.is_duplicate_operation("append", "path/to/file1.txt", "checksum1")
file_ops.is_duplicate_operation(
"append", "path/to/file1.txt", config, "checksum1"
)
is False
)
# Test cases with delete operations
assert file_ops.is_duplicate_operation("delete", "path/to/file1.txt") is False
assert file_ops.is_duplicate_operation("delete", "path/to/file3.txt") is True
assert (
file_ops.is_duplicate_operation("delete", "path/to/file1.txt", config) is False
)
assert (
file_ops.is_duplicate_operation("delete", "path/to/file3.txt", config) is True
)
# Test logging a file operation
def test_log_operation(config: Config):
file_ops.log_operation("log_test", "path/to/test")
file_ops.log_operation("log_test", "path/to/test", config)
with open(config.file_logger_path, "r", encoding="utf-8") as f:
content = f.read()
assert f"log_test: path/to/test\n" in content
@@ -164,7 +176,7 @@ def test_text_checksum(file_content: str):
def test_log_operation_with_checksum(config: Config):
file_ops.log_operation("log_test", "path/to/test", checksum="ABCDEF")
file_ops.log_operation("log_test", "path/to/test", config, checksum="ABCDEF")
with open(config.file_logger_path, "r", encoding="utf-8") as f:
content = f.read()
assert f"log_test: path/to/test #ABCDEF\n" in content
@@ -211,50 +223,56 @@ def test_read_file(
mock_MemoryItem_from_text,
test_file_with_content_path: Path,
file_content,
config: Config,
):
content = file_ops.read_file(test_file_with_content_path)
assert content == file_content
content = file_ops.read_file(test_file_with_content_path, config)
assert content.replace("\r", "") == file_content
def test_write_to_file(test_file_path: Path):
def test_write_to_file(test_file_path: Path, config):
new_content = "This is new content.\n"
file_ops.write_to_file(str(test_file_path), new_content)
file_ops.write_to_file(str(test_file_path), new_content, config)
with open(test_file_path, "r", encoding="utf-8") as f:
content = f.read()
assert content == new_content
def test_write_file_logs_checksum(config: Config, test_file_path: Path):
def test_write_file_logs_checksum(test_file_path: Path, config):
new_content = "This is new content.\n"
new_checksum = file_ops.text_checksum(new_content)
file_ops.write_to_file(str(test_file_path), new_content)
file_ops.write_to_file(str(test_file_path), new_content, config)
with open(config.file_logger_path, "r", encoding="utf-8") as f:
log_entry = f.read()
assert log_entry == f"write: {test_file_path} #{new_checksum}\n"
def test_write_file_fails_if_content_exists(test_file_path: Path):
def test_write_file_fails_if_content_exists(test_file_path: Path, config):
new_content = "This is new content.\n"
file_ops.log_operation(
"write",
str(test_file_path),
config,
checksum=file_ops.text_checksum(new_content),
)
result = file_ops.write_to_file(str(test_file_path), new_content)
result = file_ops.write_to_file(str(test_file_path), new_content, config)
assert result == "Error: File has already been updated."
def test_write_file_succeeds_if_content_different(test_file_with_content_path: Path):
def test_write_file_succeeds_if_content_different(
test_file_with_content_path: Path, config
):
new_content = "This is different content.\n"
result = file_ops.write_to_file(str(test_file_with_content_path), new_content)
result = file_ops.write_to_file(
str(test_file_with_content_path), new_content, config
)
assert result == "File written to successfully."
def test_append_to_file(test_nested_file: Path):
def test_append_to_file(test_nested_file: Path, config):
append_text = "This is appended text.\n"
file_ops.write_to_file(test_nested_file, append_text)
file_ops.write_to_file(test_nested_file, append_text, config)
file_ops.append_to_file(test_nested_file, append_text)
file_ops.append_to_file(test_nested_file, append_text, config)
with open(test_nested_file, "r") as f:
content_after = f.read()
@@ -262,12 +280,10 @@ def test_append_to_file(test_nested_file: Path):
assert content_after == append_text + append_text
def test_append_to_file_uses_checksum_from_appended_file(
config: Config, test_file_path: Path
):
def test_append_to_file_uses_checksum_from_appended_file(test_file_path: Path, config):
append_text = "This is appended text.\n"
file_ops.append_to_file(test_file_path, append_text)
file_ops.append_to_file(test_file_path, append_text)
file_ops.append_to_file(test_file_path, append_text, config)
file_ops.append_to_file(test_file_path, append_text, config)
with open(config.file_logger_path, "r", encoding="utf-8") as f:
log_contents = f.read()
@@ -282,8 +298,8 @@ def test_append_to_file_uses_checksum_from_appended_file(
)
def test_delete_file(test_file_with_content_path: Path):
result = file_ops.delete_file(str(test_file_with_content_path))
def test_delete_file(test_file_with_content_path: Path, config):
result = file_ops.delete_file(str(test_file_with_content_path), config)
assert result == "File deleted successfully."
assert os.path.exists(test_file_with_content_path) is False
@@ -291,16 +307,16 @@ def test_delete_file(test_file_with_content_path: Path):
def test_delete_missing_file(config):
filename = "path/to/file/which/does/not/exist"
# confuse the log
file_ops.log_operation("write", filename, checksum="fake")
file_ops.log_operation("write", filename, config, checksum="fake")
try:
os.remove(filename)
except FileNotFoundError as err:
assert str(err) in file_ops.delete_file(filename)
assert str(err) in file_ops.delete_file(filename, config)
return
assert False, f"Failed to test delete_file; {filename} not expected to exist"
def test_list_files(workspace: Workspace, test_directory: Path):
def test_list_files(workspace: Workspace, test_directory: Path, config):
# Case 1: Create files A and B, search for A, and ensure we don't return A and B
file_a = workspace.get_path("file_a.txt")
file_b = workspace.get_path("file_b.txt")
@@ -318,7 +334,7 @@ def test_list_files(workspace: Workspace, test_directory: Path):
with open(os.path.join(test_directory, file_a.name), "w") as f:
f.write("This is file A in the subdirectory.")
files = file_ops.list_files(str(workspace.root))
files = file_ops.list_files(str(workspace.root), config)
assert file_a.name in files
assert file_b.name in files
assert os.path.join(Path(test_directory).name, file_a.name) in files
@@ -331,17 +347,17 @@ def test_list_files(workspace: Workspace, test_directory: Path):
# Case 2: Search for a file that does not exist and make sure we don't throw
non_existent_file = "non_existent_file.txt"
files = file_ops.list_files("")
files = file_ops.list_files("", config)
assert non_existent_file not in files
def test_download_file(config, workspace: Workspace):
def test_download_file(workspace: Workspace, config):
url = "https://github.com/Significant-Gravitas/Auto-GPT/archive/refs/tags/v0.2.2.tar.gz"
local_name = workspace.get_path("auto-gpt.tar.gz")
size = 365023
readable_size = readable_file_size(size)
assert (
file_ops.download_file(url, local_name)
file_ops.download_file(url, local_name, config)
== f'Successfully downloaded and locally stored file: "{local_name}"! (Size: {readable_size})'
)
assert os.path.isfile(local_name) is True
@@ -349,10 +365,10 @@ def test_download_file(config, workspace: Workspace):
url = "https://github.com/Significant-Gravitas/Auto-GPT/archive/refs/tags/v0.0.0.tar.gz"
assert "Got an HTTP Error whilst trying to download file" in file_ops.download_file(
url, local_name
url, local_name, config
)
url = "https://thiswebsiteiswrong.hmm/v0.0.0.tar.gz"
assert "Failed to establish a new connection:" in file_ops.download_file(
url, local_name
url, local_name, config
)