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.
AutoGPT: Fix artifact creation & downloading
Reinier van der Leer62df429ffa71
diff --git a/autogpts/autogpt/autogpt/app/agent_protocol_server.py b/autogpts/autogpt/autogpt/app/agent_protocol_server.pyindex 279c2044cf..00c41640ae 100644--- a/autogpts/autogpt/autogpt/app/agent_protocol_server.py+++ b/autogpts/autogpt/autogpt/app/agent_protocol_server.py@@ -6,7 +6,7 @@ from uuid import uuid4from fastapi import APIRouter, FastAPI, UploadFilefrom fastapi.middleware.cors import CORSMiddleware-from fastapi.responses import FileResponse, RedirectResponse, StreamingResponse+from fastapi.responses import RedirectResponse, StreamingResponsefrom fastapi.staticfiles import StaticFilesfrom forge.sdk.db import AgentDBfrom forge.sdk.errors import NotFoundError@@ -317,7 +317,7 @@ class AgentProtocolServer:file_path = os.path.join(relative_path, file_name)workspace = get_task_agent_file_workspace(task_id, self.agent_manager)- workspace.write_file(file_path, data)+ await workspace.write_file(file_path, data)artifact = await self.db.create_artifact(task_id=task_id,@@ -365,7 +365,8 @@ def get_task_agent_file_workspace(root=agent_manager.get_agent_dir(agent_id=task_agent_id(task_id),must_exist=True,- ),+ )+ / "workspace",restrict_to_root=True,)diff --git a/autogpts/autogpt/autogpt/commands/file_operations.py b/autogpts/autogpt/autogpt/commands/file_operations.pyindex 8e3b54ab22..29935dc873 100644--- a/autogpts/autogpt/autogpt/commands/file_operations.py+++ b/autogpts/autogpt/autogpt/commands/file_operations.py@@ -209,7 +209,7 @@ def ingest_file(aliases=["create_file"],)@sanitize_path_arg("filename")-def write_to_file(filename: Path, contents: str, agent: Agent) -> str:+async def write_to_file(filename: Path, contents: str, agent: Agent) -> str:"""Write contents to a fileArgs:@@ -225,7 +225,7 @@ def write_to_file(filename: Path, contents: str, agent: Agent) -> str:directory = os.path.dirname(filename)os.makedirs(directory, exist_ok=True)- agent.workspace.write_file(filename, contents)+ await agent.workspace.write_file(filename, contents)log_operation("write", filename, agent, checksum)return f"File {filename.name} has been written successfully."diff --git a/autogpts/autogpt/autogpt/file_workspace/file_workspace.py b/autogpts/autogpt/autogpt/file_workspace/file_workspace.pyindex 37da359e1a..61b3c42f15 100644--- a/autogpts/autogpt/autogpt/file_workspace/file_workspace.py+++ b/autogpts/autogpt/autogpt/file_workspace/file_workspace.py@@ -3,6 +3,7 @@ The FileWorkspace class provides an interface for interacting with a file worksp"""from __future__ import annotations+import inspectimport loggingfrom pathlib import Pathfrom typing import Any, Callable, Optional@@ -65,7 +66,7 @@ class FileWorkspace:with self.open_file(path, "rb" if binary else "r") as file:return file.read()- def write_file(self, path: str | Path, content: str | bytes):+ async def write_file(self, path: str | Path, content: str | bytes):"""Write to a file in the workspace."""with self.open_file(path, "wb" if type(content) is bytes else "w") as file:file.write(content)@@ -74,7 +75,9 @@ class FileWorkspace:path = Path(path)if path.is_absolute():path = path.relative_to(self.root)- self.on_write_file(path)+ res = self.on_write_file(path)+ if inspect.isawaitable(res):+ await resdef list_files(self, path: str | Path = "."):"""List all files in a directory in the workspace."""diff --git a/autogpts/autogpt/tests/unit/test_file_operations.py b/autogpts/autogpt/tests/unit/test_file_operations.pyindex 3636ce8011..6d4c69ace1 100644--- a/autogpts/autogpt/tests/unit/test_file_operations.py+++ b/autogpts/autogpt/tests/unit/test_file_operations.py@@ -204,53 +204,59 @@ def test_read_file_not_found(agent: Agent):file_ops.read_file(filename, agent=agent)-def test_write_to_file_relative_path(test_file_name: Path, agent: Agent):+async def test_write_to_file_relative_path(test_file_name: Path, agent: Agent):new_content = "This is new content.\n"- file_ops.write_to_file(str(test_file_name), new_content, agent=agent)+ await file_ops.write_to_file(test_file_name, new_content, agent=agent)with open(agent.workspace.get_path(test_file_name), "r", encoding="utf-8") as f:content = f.read()assert content == new_content-def test_write_to_file_absolute_path(test_file_path: Path, agent: Agent):+async def test_write_to_file_absolute_path(test_file_path: Path, agent: Agent):new_content = "This is new content.\n"- file_ops.write_to_file(str(test_file_path), new_content, agent=agent)+ await file_ops.write_to_file(test_file_path, new_content, agent=agent)with open(test_file_path, "r", encoding="utf-8") as f:content = f.read()assert content == new_content-def test_write_file_logs_checksum(test_file_name: Path, agent: Agent):+async def test_write_file_logs_checksum(test_file_name: Path, agent: Agent):new_content = "This is new content.\n"new_checksum = file_ops.text_checksum(new_content)- file_ops.write_to_file(str(test_file_name), new_content, agent=agent)+ await file_ops.write_to_file(test_file_name, new_content, agent=agent)with open(agent.file_manager.file_ops_log_path, "r", encoding="utf-8") as f:log_entry = f.read()assert log_entry == f"write: {test_file_name} #{new_checksum}\n"-def test_write_file_fails_if_content_exists(test_file_name: Path, agent: Agent):+async def test_write_file_fails_if_content_exists(test_file_name: Path, agent: Agent):new_content = "This is new content.\n"file_ops.log_operation("write",- str(test_file_name),+ test_file_name,agent=agent,checksum=file_ops.text_checksum(new_content),)with pytest.raises(DuplicateOperationError):- file_ops.write_to_file(str(test_file_name), new_content, agent=agent)+ await file_ops.write_to_file(test_file_name, new_content, agent=agent)-def test_write_file_succeeds_if_content_different(+async def test_write_file_succeeds_if_content_different(test_file_with_content_path: Path, agent: Agent):new_content = "This is different content.\n"- file_ops.write_to_file(str(test_file_with_content_path), new_content, agent=agent)+ await file_ops.write_to_file(test_file_with_content_path, new_content, agent=agent)-def test_append_to_file(test_nested_file: Path, agent: Agent):+async def test_append_to_file(test_nested_file: Path, agent: Agent):append_text = "This is appended text.\n"- file_ops.write_to_file(test_nested_file, append_text, agent=agent)+ await file_ops.write_to_file(test_nested_file, append_text, agent=agent)file_ops.append_to_file(test_nested_file, append_text, agent=agent)