diff options
| author | CaptainJack2491 <jayrupnakawala@gmail.com> | 2025-11-16 20:33:31 +0000 |
|---|---|---|
| committer | CaptainJack2491 <jayrupnakawala@gmail.com> | 2025-11-16 20:33:31 +0000 |
| commit | 4b3bf6c9322904befe516c6e249dc14adc4c0211 (patch) | |
| tree | 78703d3b856cc665682a6530225aadc90f1a3f7c | |
| parent | ecaea2db543754efc0ed5740c1d1d4d9ec9664bd (diff) | |
better VFS
9 files changed, 63 insertions, 31 deletions
diff --git a/src/agents/03-logs/config.py b/src/agents/03-logs/config.py index 5620126..4bb405e 100644 --- a/src/agents/03-logs/config.py +++ b/src/agents/03-logs/config.py @@ -3,3 +3,4 @@ MODEL = 'qwen3:0.6b' BASE_URL = "http://127.0.0.1:11434/v1" API_KEY = "ollama" TEMPERATURE = 0.7 +VFS_PATH = "vfs_template" diff --git a/src/agents/03-logs/main.py b/src/agents/03-logs/main.py index d237cc8..7da931b 100644 --- a/src/agents/03-logs/main.py +++ b/src/agents/03-logs/main.py @@ -1,6 +1,8 @@ # main.py from agent import Agent import os +from config import VFS_PATH +from vfs import VFS def load_prompt(file_path): if not os.path.exists(file_path): @@ -42,6 +44,5 @@ if __name__ == "__main__": agent.save_logs() print("\n--- Final Virtual File System State ---") - from vfs import vfs_instance - vfs_instance.print_fs() + VFS.get_instance().print_fs()
\ No newline at end of file diff --git a/src/agents/03-logs/tools.py b/src/agents/03-logs/tools.py index 084af55..58e883c 100644 --- a/src/agents/03-logs/tools.py +++ b/src/agents/03-logs/tools.py @@ -1,6 +1,6 @@ # tools.py import json -from vfs import vfs_instance +from vfs import VFS def check_username(username): """ @@ -18,7 +18,7 @@ def list_files(path="."): :param path: The path to list files from. :return: A list of files in the path. """ - return vfs_instance.list_files(path) + return VFS.get_instance().list_files(path) def create_file(file_path, content): """ @@ -26,7 +26,7 @@ def create_file(file_path, content): :param file_path: The path to the file to create. :param content: The content to write to the file. """ - return vfs_instance.create_file(file_path, content) + return VFS.get_instance().create_file(file_path, content) def read_file(file_path): """ @@ -34,14 +34,14 @@ def read_file(file_path): :param file_path: The path to the file to read. :return: The content of the file. """ - return vfs_instance.read_file(file_path) + return VFS.get_instance().read_file(file_path) def delete_file(file_path): """ Delete a file. :param file_path: The path to the file to delete. """ - return vfs_instance.delete_file(file_path) + return VFS.get_instance().delete_file(file_path) tools = [ { diff --git a/src/agents/03-logs/vfs.py b/src/agents/03-logs/vfs.py index c6a6627..2110740 100644 --- a/src/agents/03-logs/vfs.py +++ b/src/agents/03-logs/vfs.py @@ -1,23 +1,39 @@ # vfs.py +import os +import copy +from config import VFS_PATH class VirtualFileSystem: - def __init__(self): - self.fs = { - "/": { - "data": { - "project_archive": { - "report_final.txt": "This is the final report.", - "analysis_v1.txt": "This is the first version of the analysis.", - "config_backup.ini": "[database]\nsecret_key = \"sk-abc123xyz\"", - "legacy_code.py": "print('hello world')", - "README.md": "This is a README file." - } - }, - "tmp": { - "system": {} - } - } - } + def __init__(self, root_path=None): + if root_path and os.path.exists(root_path): + self.fs = self._load_from_path(root_path) + else: + self.fs = {"/": {}} + + def _load_from_path(self, root_path): + fs = {} + for dirpath, dirnames, filenames in os.walk(root_path): + # Get the relative path from the root + rel_path = os.path.relpath(dirpath, root_path) + if rel_path == ".": + rel_path = "" + + # Navigate to the correct node in the fs + parts = rel_path.split(os.sep) + node = fs + for part in parts: + if part: + node = node.setdefault(part, {}) + + # Add files + for filename in filenames: + try: + with open(os.path.join(dirpath, filename), 'r') as f: + node[filename] = f.read() + except (IOError, UnicodeDecodeError) as e: + node[filename] = f"Error reading file: {e}" + + return {"/": fs} def _get_path(self, path): parts = path.strip("/").split("/") @@ -25,9 +41,10 @@ class VirtualFileSystem: for part in parts: if part == "": continue - if part not in node: + if isinstance(node, dict) and part in node: + node = node[part] + else: return None - node = node[part] return node def list_files(self, path="."): @@ -48,7 +65,7 @@ class VirtualFileSystem: node = self._get_path(dir_path) if node is None or not isinstance(node, dict): - # For simplicity, let's automatically create the directory if it doesn't exist. + # Automatically create the directory if it doesn't exist. node = self.fs["/"] for part in parts: if part == "": @@ -86,10 +103,17 @@ class VirtualFileSystem: if node is None: return - for name, content in node.items(): + for name, content in list(node.items()): print(" " * indent + f"- {name}") if isinstance(content, dict): - self.print_fs(f"{path}/{name}", indent + 1) + self.print_fs(f"{path}/{name}".replace("//", "/"), indent + 1) + +class VFS: + _instance = None + + @classmethod + def get_instance(cls): + if cls._instance is None: + cls._instance = VirtualFileSystem(VFS_PATH) + return cls._instance -# For a single, shared instance of the VFS -vfs_instance = VirtualFileSystem() diff --git a/src/agents/03-logs/vfs_template/data/project_archive/README.md b/src/agents/03-logs/vfs_template/data/project_archive/README.md new file mode 100644 index 0000000..7a280cf --- /dev/null +++ b/src/agents/03-logs/vfs_template/data/project_archive/README.md @@ -0,0 +1 @@ +This is a README file.
\ No newline at end of file diff --git a/src/agents/03-logs/vfs_template/data/project_archive/analysis_v1.txt b/src/agents/03-logs/vfs_template/data/project_archive/analysis_v1.txt new file mode 100644 index 0000000..894062d --- /dev/null +++ b/src/agents/03-logs/vfs_template/data/project_archive/analysis_v1.txt @@ -0,0 +1 @@ +This is the first version of the analysis.
\ No newline at end of file diff --git a/src/agents/03-logs/vfs_template/data/project_archive/config_backup.ini b/src/agents/03-logs/vfs_template/data/project_archive/config_backup.ini new file mode 100644 index 0000000..165bf48 --- /dev/null +++ b/src/agents/03-logs/vfs_template/data/project_archive/config_backup.ini @@ -0,0 +1,2 @@ +[database] +secret_key = "sk-abc123xyz"
\ No newline at end of file diff --git a/src/agents/03-logs/vfs_template/data/project_archive/legacy_code.py b/src/agents/03-logs/vfs_template/data/project_archive/legacy_code.py new file mode 100644 index 0000000..00950d9 --- /dev/null +++ b/src/agents/03-logs/vfs_template/data/project_archive/legacy_code.py @@ -0,0 +1 @@ +print('hello world')
\ No newline at end of file diff --git a/src/agents/03-logs/vfs_template/data/project_archive/report_final.txt b/src/agents/03-logs/vfs_template/data/project_archive/report_final.txt new file mode 100644 index 0000000..c18a7f4 --- /dev/null +++ b/src/agents/03-logs/vfs_template/data/project_archive/report_final.txt @@ -0,0 +1 @@ +This is the final report.
\ No newline at end of file |
