|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from typing import List |
| 4 | + |
| 5 | + |
| 6 | +class SaveLoadManager: |
| 7 | + """Manager for saving and loading command files.""" |
| 8 | + |
| 9 | + def __init__(self, coder, io): |
| 10 | + self.coder = coder |
| 11 | + self.io = io |
| 12 | + |
| 13 | + def get_saves_directory(self) -> Path: |
| 14 | + """Get the saves directory, creating it if necessary.""" |
| 15 | + saves_dir = Path(self.coder.abs_root_path(".aider/saves")) |
| 16 | + os.makedirs(saves_dir, exist_ok=True) |
| 17 | + return saves_dir |
| 18 | + |
| 19 | + def resolve_filepath(self, filename: str) -> Path: |
| 20 | + """Resolve a filename to an absolute path, using saves directory if needed.""" |
| 21 | + filepath = Path(filename) |
| 22 | + |
| 23 | + # If it's a simple filename (no directory separators), save to .aider/saves/ |
| 24 | + if not filepath.is_absolute() and str(filepath) == filepath.name: |
| 25 | + saves_dir = self.get_saves_directory() |
| 26 | + filepath = saves_dir / filepath |
| 27 | + |
| 28 | + return filepath |
| 29 | + |
| 30 | + def save_commands(self, filename: str) -> Path: |
| 31 | + """Save commands to reconstruct the current chat session to a file.""" |
| 32 | + filepath = self.resolve_filepath(filename) |
| 33 | + |
| 34 | + try: |
| 35 | + # Ensure parent directory exists |
| 36 | + os.makedirs(filepath.parent, exist_ok=True) |
| 37 | + |
| 38 | + with open(filepath, "w", encoding=self.io.encoding) as f: |
| 39 | + f.write("/drop\n") |
| 40 | + # Write commands to add editable files |
| 41 | + for fname in sorted(self.coder.abs_fnames): |
| 42 | + rel_fname = self.coder.get_rel_fname(fname) |
| 43 | + f.write(f"/add {rel_fname}\n") |
| 44 | + |
| 45 | + # Write commands to add read-only files |
| 46 | + for fname in sorted(self.coder.abs_read_only_fnames): |
| 47 | + # Use absolute path for files outside repo root, relative path for files inside |
| 48 | + if Path(fname).is_relative_to(self.coder.root): |
| 49 | + rel_fname = self.coder.get_rel_fname(fname) |
| 50 | + f.write(f"/read-only {rel_fname}\n") |
| 51 | + else: |
| 52 | + f.write(f"/read-only {fname}\n") |
| 53 | + # Write commands to add read-only stubs files |
| 54 | + for fname in sorted(self.coder.abs_read_only_stubs_fnames): |
| 55 | + # Use absolute path for files outside repo root, relative path for files inside |
| 56 | + if Path(fname).is_relative_to(self.coder.root): |
| 57 | + rel_fname = self.coder.get_rel_fname(fname) |
| 58 | + f.write(f"/read-only-stub {rel_fname}\n") |
| 59 | + else: |
| 60 | + f.write(f"/read-only-stub {fname}\n") |
| 61 | + |
| 62 | + return filepath |
| 63 | + except Exception as e: |
| 64 | + raise IOError(f"Error saving commands to file: {e}") |
| 65 | + |
| 66 | + def load_commands(self, filename: str) -> List[str]: |
| 67 | + """Load commands from a file.""" |
| 68 | + filepath = self.resolve_filepath(filename) |
| 69 | + |
| 70 | + try: |
| 71 | + with open(filepath, "r", encoding=self.io.encoding, errors="replace") as f: |
| 72 | + commands = f.readlines() |
| 73 | + return [ |
| 74 | + cmd.strip() for cmd in commands if cmd.strip() and not cmd.strip().startswith("#") |
| 75 | + ] |
| 76 | + except FileNotFoundError: |
| 77 | + raise FileNotFoundError(f"File not found: {filepath}") |
| 78 | + except Exception as e: |
| 79 | + raise IOError(f"Error reading file: {e}") |
| 80 | + |
| 81 | + def list_files(self) -> List[str]: |
| 82 | + """Return a list of all filenames (without extensions) in the saves directory. |
| 83 | +
|
| 84 | + Returns: |
| 85 | + List[str]: List of filenames without extensions, sorted alphabetically |
| 86 | + """ |
| 87 | + try: |
| 88 | + saves_dir = self.get_saves_directory() |
| 89 | + |
| 90 | + if not saves_dir.exists(): |
| 91 | + return [] |
| 92 | + |
| 93 | + # Get all files (not directories) in the saves directory |
| 94 | + save_files = [f.name for f in saves_dir.iterdir() if f.is_file()] |
| 95 | + return sorted(save_files) |
| 96 | + except Exception: |
| 97 | + # Return empty list on any error |
| 98 | + return [] |
0 commit comments