From 41474758a197e068c65d8c179ed9e4a3f91004c4 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:40 +0000 Subject: [PATCH] Update virtual_fs.py --- virtual_fs.py | 65 ++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 42 deletions(-) diff --git a/virtual_fs.py b/virtual_fs.py index a18af7d..5d1e24d 100644 --- a/virtual_fs.py +++ b/virtual_fs.py @@ -1,58 +1,39 @@ -"""Simple in‑memory virtual file system. - -The VFS is just a dictionary mapping file names to string content. -It is shared between all tools and can be exported to the real -filesystem when requested. +""" +Virtual file system used by the deep agent. +It stores files in memory and can be dumped to the real file system. """ -from __future__ import annotations - +import os from pathlib import Path -from typing import Dict, Iterable - +from typing import Dict, Optional class VirtualFileSystem: - """In‑memory file system. + """A simple in‑memory file system. - Attributes - ---------- - files : Dict[str, str] - Mapping of file names to file content. + Files are stored as a mapping from a relative path to its content. """ - def __init__(self) -> None: + def __init__(self, base_dir: str = "virtual_fs"): + self.base_dir = Path(base_dir) self.files: Dict[str, str] = {} - def create_file(self, name: str, content: str) -> None: - """Create or overwrite a virtual file.""" - self.files[name] = content + def write_file(self, path: str, content: str) -> None: + norm_path = Path(path).as_posix() + self.files[norm_path] = content - def list_files(self) -> Iterable[str]: - """Return an iterable of file names.""" - return self.files.keys() + def read_file(self, path: str) -> Optional[str]: + norm_path = Path(path).as_posix() + return self.files.get(norm_path) - def read_file(self, name: str) -> str: - """Return the content of a virtual file. + def list_files(self) -> Dict[str, str]: + return dict(self.files) - Raises ``KeyError`` if the file does not exist. - """ - return self.files[name] - - def export_to_disk(self, root: str | Path = ".") -> None: - """Export all virtual files to the real filesystem. - - Parameters - ---------- - root : str | Path, optional - Root directory where the virtual files will be written. - Defaults to the current working directory. - """ - root_path = Path(root).expanduser().resolve() - root_path.mkdir(parents=True, exist_ok=True) - for name, content in self.files.items(): - file_path = root_path / name + def dump_to_real_fs(self, real_root: str = "real_fs") -> None: + real_root_path = Path(real_root) + for rel_path, content in self.files.items(): + file_path = real_root_path / rel_path file_path.parent.mkdir(parents=True, exist_ok=True) file_path.write_text(content, encoding="utf-8") - def __repr__(self) -> str: # pragma: no cover - return f"VirtualFileSystem({len(self.files)} files)" + def __repr__(self) -> str: + return f"VirtualFileSystem(files={list(self.files.keys())})"