Files
2026-06-04 19:19:34 +00:00

61 lines
1.9 KiB
Python

"""
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()