40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
"""
|
||
Virtual file system used by the deep agent.
|
||
It stores files in memory and can be dumped to the real file system.
|
||
"""
|
||
|
||
import os
|
||
from pathlib import Path
|
||
from typing import Dict, Optional
|
||
|
||
class VirtualFileSystem:
|
||
"""A simple in‑memory file system.
|
||
|
||
Files are stored as a mapping from a relative path to its content.
|
||
"""
|
||
|
||
def __init__(self, base_dir: str = "virtual_fs"):
|
||
self.base_dir = Path(base_dir)
|
||
self.files: Dict[str, str] = {}
|
||
|
||
def write_file(self, path: str, content: str) -> None:
|
||
norm_path = Path(path).as_posix()
|
||
self.files[norm_path] = content
|
||
|
||
def read_file(self, path: str) -> Optional[str]:
|
||
norm_path = Path(path).as_posix()
|
||
return self.files.get(norm_path)
|
||
|
||
def list_files(self) -> Dict[str, str]:
|
||
return dict(self.files)
|
||
|
||
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:
|
||
return f"VirtualFileSystem(files={list(self.files.keys())})"
|