fix: Create parent directories for history files and improve error handling

This commit is contained in:
Paul Gauthier (aider) 2025-06-25 11:12:24 -07:00
parent 5b317e5ec0
commit 14af218ea2

View file

@ -308,6 +308,12 @@ class InputOutput:
self.yes = yes
self.input_history_file = input_history_file
if self.input_history_file:
try:
Path(self.input_history_file).parent.mkdir(parents=True, exist_ok=True)
except (PermissionError, OSError) as e:
self.tool_warning(f"Could not create directory for input history: {e}")
self.input_history_file = None
self.llm_history_file = llm_history_file
if chat_history_file is not None:
self.chat_history_file = Path(chat_history_file)
@ -731,7 +737,6 @@ class InputOutput:
if not self.input_history_file:
return
try:
Path(self.input_history_file).parent.mkdir(parents=True, exist_ok=True)
FileHistory(self.input_history_file).append_string(inp)
# Also add to the in-memory history if it exists
if self.prompt_session and self.prompt_session.history:
@ -750,9 +755,14 @@ class InputOutput:
if not self.llm_history_file:
return
timestamp = datetime.now().isoformat(timespec="seconds")
try:
Path(self.llm_history_file).parent.mkdir(parents=True, exist_ok=True)
with open(self.llm_history_file, "a", encoding="utf-8") as log_file:
log_file.write(f"{role.upper()} {timestamp}\n")
log_file.write(content + "\n")
except (PermissionError, OSError) as err:
self.tool_warning(f"Unable to write to llm history file {self.llm_history_file}: {err}")
self.llm_history_file = None
def display_user_input(self, inp):
if self.pretty and self.user_input_color: