From 696f296b994b0ab18981d3faffaf177c588a3515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Thu, 4 Jun 2026 19:19:34 +0000 Subject: [PATCH] Add vfs.py --- vfs.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 vfs.py diff --git a/vfs.py b/vfs.py new file mode 100644 index 0000000..18b3004 --- /dev/null +++ b/vfs.py @@ -0,0 +1,61 @@ +""" +Virtual File System implementation. + +This module defines a simple in-memory virtual file system that can be used by the agent +to create and store files during its execution. At the end of the run the contents can +be exported to the real file system. +""" + +import os +from typing import Dict, List + +class VirtualFileSystem: + """A minimal virtual file system. + + Files are stored in an internal dictionary mapping file paths to their content. + """ + + def __init__(self) -> None: + self._files: Dict[str, str] = {} + + def write_file(self, file_path: str, content: str) -> None: + """Write content to a virtual file. + + Parameters + ---------- + file_path: str + Path of the file relative to the virtual root. + content: str + Text content to write. + """ + self._files[file_path] = content + + def read_file(self, file_path: str) -> str: + """Read content from a virtual file. + + Returns an empty string if the file does not exist. + """ + return self._files.get(file_path, "") + + def list_files(self) -> List[str]: + """Return a list of all virtual file paths.""" + return list(self._files.keys()) + + def export_to_real_fs(self, base_path: str) -> None: + """Export all virtual files to the real file system. + + Parameters + ---------- + base_path: str + Directory on the real file system where the virtual files will be written. + """ + os.makedirs(base_path, exist_ok=True) + for path, content in self._files.items(): + full_path = os.path.join(base_path, path) + dir_name = os.path.dirname(full_path) + os.makedirs(dir_name, exist_ok=True) + with open(full_path, "w", encoding="utf-8") as f: + f.write(content) + +# Global instance that will be used by the agent tools +vfs = VirtualFileSystem() \ No newline at end of file