diff options
Diffstat (limited to 'autogpts/forge/forge/actions/file_system/files.py')
-rw-r--r-- | autogpts/forge/forge/actions/file_system/files.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/autogpts/forge/forge/actions/file_system/files.py b/autogpts/forge/forge/actions/file_system/files.py new file mode 100644 index 000000000..ca7abde29 --- /dev/null +++ b/autogpts/forge/forge/actions/file_system/files.py @@ -0,0 +1,78 @@ +from typing import List + +from ..registry import action + + +@action( + name="list_files", + description="List files in a directory", + parameters=[ + { + "name": "path", + "description": "Path to the directory", + "type": "string", + "required": True, + } + ], + output_type="list[str]", +) +async def list_files(agent, task_id: str, path: str) -> List[str]: + """ + List files in a workspace directory + """ + return agent.workspace.list(task_id=task_id, path=str(path)) + + +@action( + name="write_file", + description="Write data to a file", + parameters=[ + { + "name": "file_path", + "description": "Path to the file", + "type": "string", + "required": True, + }, + { + "name": "data", + "description": "Data to write to the file", + "type": "bytes", + "required": True, + }, + ], + output_type="None", +) +async def write_file(agent, task_id: str, file_path: str, data: bytes): + """ + Write data to a file + """ + if isinstance(data, str): + data = data.encode() + + agent.workspace.write(task_id=task_id, path=file_path, data=data) + return await agent.db.create_artifact( + task_id=task_id, + file_name=file_path.split("/")[-1], + relative_path=file_path, + agent_created=True, + ) + + +@action( + name="read_file", + description="Read data from a file", + parameters=[ + { + "name": "file_path", + "description": "Path to the file", + "type": "string", + "required": True, + }, + ], + output_type="bytes", +) +async def read_file(agent, task_id: str, file_path: str) -> bytes: + """ + Read data from a file + """ + return agent.workspace.read(task_id=task_id, path=file_path) |