From 909825bf1b02b7027beb715f2a6a306a03a57ca3 Mon Sep 17 00:00:00 2001 From: AI Christianson Date: Mon, 10 Mar 2025 19:37:39 -0400 Subject: [PATCH] refactor: extract get_most_recent_id --- ra_aid/agents/key_facts_gc_agent.py | 8 ++------ ra_aid/agents/key_snippets_gc_agent.py | 8 ++------ ra_aid/agents/research_agent.py | 14 ++++++++------ ra_aid/agents/research_notes_gc_agent.py | 8 ++------ .../repositories/human_input_repository.py | 19 +++++++++++++++++++ ra_aid/tools/memory.py | 12 +++--------- 6 files changed, 36 insertions(+), 33 deletions(-) diff --git a/ra_aid/agents/key_facts_gc_agent.py b/ra_aid/agents/key_facts_gc_agent.py index d158c64..e8fbd38 100644 --- a/ra_aid/agents/key_facts_gc_agent.py +++ b/ra_aid/agents/key_facts_gc_agent.py @@ -48,9 +48,7 @@ def delete_key_facts(fact_ids: List[int]) -> str: # Try to get the current human input to protect its facts current_human_input_id = None try: - recent_inputs = get_human_input_repository().get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - current_human_input_id = recent_inputs[0].id + current_human_input_id = get_human_input_repository().get_most_recent_id() except Exception as e: console.print(f"Warning: Could not retrieve current human input: {str(e)}") @@ -133,9 +131,7 @@ def run_key_facts_gc_agent() -> None: # Try to get the current human input ID to exclude its facts current_human_input_id = None try: - recent_inputs = get_human_input_repository().get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - current_human_input_id = recent_inputs[0].id + current_human_input_id = get_human_input_repository().get_most_recent_id() except Exception as e: console.print(f"Warning: Could not retrieve current human input: {str(e)}") diff --git a/ra_aid/agents/key_snippets_gc_agent.py b/ra_aid/agents/key_snippets_gc_agent.py index b9d55ef..6fb86fa 100644 --- a/ra_aid/agents/key_snippets_gc_agent.py +++ b/ra_aid/agents/key_snippets_gc_agent.py @@ -46,9 +46,7 @@ def delete_key_snippets(snippet_ids: List[int]) -> str: # Try to get the current human input to protect its snippets current_human_input_id = None try: - recent_inputs = get_human_input_repository().get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - current_human_input_id = recent_inputs[0].id + current_human_input_id = get_human_input_repository().get_most_recent_id() except Exception as e: console.print(f"Warning: Could not retrieve current human input: {str(e)}") @@ -125,9 +123,7 @@ def run_key_snippets_gc_agent() -> None: # Try to get the current human input ID to exclude its snippets current_human_input_id = None try: - recent_inputs = get_human_input_repository().get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - current_human_input_id = recent_inputs[0].id + current_human_input_id = get_human_input_repository().get_most_recent_id() except Exception as e: console.print(f"Warning: Could not retrieve current human input: {str(e)}") diff --git a/ra_aid/agents/research_agent.py b/ra_aid/agents/research_agent.py index 4acde08..966321f 100644 --- a/ra_aid/agents/research_agent.py +++ b/ra_aid/agents/research_agent.py @@ -109,12 +109,14 @@ def run_research_agent( base_task = base_task_or_query try: human_input_repository = get_human_input_repository() - recent_inputs = human_input_repository.get_recent(1) - if recent_inputs and len(recent_inputs) > 0 and recent_inputs[0].content != base_task_or_query: - last_human_input = recent_inputs[0].content - base_task = ( - f"{last_human_input}\n{base_task}" - ) + most_recent_id = human_input_repository.get_most_recent_id() + if most_recent_id is not None: + recent_input = human_input_repository.get(most_recent_id) + if recent_input and recent_input.content != base_task_or_query: + last_human_input = recent_input.content + base_task = ( + f"{last_human_input}\n{base_task}" + ) except RuntimeError as e: logger.error(f"Failed to access human input repository: {str(e)}") # Continue without appending last human input diff --git a/ra_aid/agents/research_notes_gc_agent.py b/ra_aid/agents/research_notes_gc_agent.py index 70bb91f..fbc0d60 100644 --- a/ra_aid/agents/research_notes_gc_agent.py +++ b/ra_aid/agents/research_notes_gc_agent.py @@ -48,9 +48,7 @@ def delete_research_notes(note_ids: List[int]) -> str: # Try to get the current human input to protect its notes current_human_input_id = None try: - recent_inputs = get_human_input_repository().get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - current_human_input_id = recent_inputs[0].id + current_human_input_id = get_human_input_repository().get_most_recent_id() except Exception as e: console.print(f"Warning: Could not retrieve current human input: {str(e)}") @@ -138,9 +136,7 @@ def run_research_notes_gc_agent(threshold: int = 30) -> None: # Try to get the current human input ID to exclude its notes current_human_input_id = None try: - recent_inputs = get_human_input_repository().get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - current_human_input_id = recent_inputs[0].id + current_human_input_id = get_human_input_repository().get_most_recent_id() except Exception as e: console.print(f"Warning: Could not retrieve current human input: {str(e)}") diff --git a/ra_aid/database/repositories/human_input_repository.py b/ra_aid/database/repositories/human_input_repository.py index f8f89ff..f20853d 100644 --- a/ra_aid/database/repositories/human_input_repository.py +++ b/ra_aid/database/repositories/human_input_repository.py @@ -257,6 +257,25 @@ class HumanInputRepository: except peewee.DatabaseError as e: logger.error(f"Failed to fetch recent human inputs: {str(e)}") raise + + def get_most_recent_id(self) -> Optional[int]: + """ + Get the ID of the most recent human input record. + + Returns: + Optional[int]: The ID of the most recent human input, or None if no records exist + + Raises: + peewee.DatabaseError: If there's an error accessing the database + """ + try: + recent_inputs = self.get_recent(1) + if recent_inputs and len(recent_inputs) > 0: + return recent_inputs[0].id + return None + except peewee.DatabaseError as e: + logger.error(f"Failed to fetch most recent human input ID: {str(e)}") + raise def get_by_source(self, source: str) -> List[HumanInput]: """ diff --git a/ra_aid/tools/memory.py b/ra_aid/tools/memory.py index c768638..96b402c 100644 --- a/ra_aid/tools/memory.py +++ b/ra_aid/tools/memory.py @@ -54,9 +54,7 @@ def emit_research_notes(notes: str) -> str: human_input_id = None try: human_input_repo = get_human_input_repository() - recent_inputs = human_input_repo.get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - human_input_id = recent_inputs[0].id + human_input_id = human_input_repo.get_most_recent_id() except RuntimeError as e: logger.warning(f"No HumanInputRepository available: {str(e)}") except Exception as e: @@ -109,9 +107,7 @@ def emit_key_facts(facts: List[str]) -> str: human_input_id = None try: human_input_repo = get_human_input_repository() - recent_inputs = human_input_repo.get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - human_input_id = recent_inputs[0].id + human_input_id = human_input_repo.get_most_recent_id() except RuntimeError as e: logger.warning(f"No HumanInputRepository available: {str(e)}") except Exception as e: @@ -186,9 +182,7 @@ def emit_key_snippet(snippet_info: SnippetInfo) -> str: human_input_id = None try: human_input_repo = get_human_input_repository() - recent_inputs = human_input_repo.get_recent(1) - if recent_inputs and len(recent_inputs) > 0: - human_input_id = recent_inputs[0].id + human_input_id = human_input_repo.get_most_recent_id() except RuntimeError as e: logger.warning(f"No HumanInputRepository available: {str(e)}") except Exception as e: