From fc07ccd9df23adb197510b90a329a00277b772b4 Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Sun, 26 May 2024 20:07:03 -0500 Subject: [PATCH] add limit to the number of notes an actor can have, add action to summarize notes --- adventure/actions/planning.py | 50 +++++++++++++++++++++++++++++++++-- adventure/models/config.py | 4 +++ adventure/simulate.py | 8 +++--- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/adventure/actions/planning.py b/adventure/actions/planning.py index 9f94b78..30a7b3c 100644 --- a/adventure/actions/planning.py +++ b/adventure/actions/planning.py @@ -1,7 +1,11 @@ -from adventure.context import action_context, get_current_step +from adventure.context import action_context, get_agent_for_actor, get_current_step +from adventure.errors import ActionError +from adventure.models.config import DEFAULT_CONFIG from adventure.models.planning import CalendarEvent from adventure.utils.planning import get_recent_notes +actor_config = DEFAULT_CONFIG.world.actor + def take_note(fact: str): """ @@ -14,9 +18,15 @@ def take_note(fact: str): with action_context() as (_, action_actor): if fact in action_actor.planner.notes: - return "You already have a note about that fact." + raise ActionError("You already have a note about that fact.") + + if len(action_actor.planner.notes) >= actor_config.note_limit: + raise ActionError( + "You have reached the limit of notes you can take. Please erase, replace, or summarize some notes." + ) action_actor.planner.notes.append(fact) + return "You make a note of that fact." @@ -73,6 +83,42 @@ def replace_note(old: str, new: str) -> str: return "Note replaced." +def summarize_notes(limit: int) -> str: + """ + Summarize your notes by combining similar notes and removing duplicates. + + Args: + limit: The maximum number of notes to keep. + """ + + with action_context() as (_, action_actor): + notes = action_actor.planner.notes + action_agent = get_agent_for_actor(action_actor) + + if not action_agent: + raise ActionError("Agent missing for actor {action_actor.name}") + + summary = action_agent( + "Please summarize your notes. Remove any duplicates and combine similar notes. " + "If a newer note contradicts an older note, keep the newer note. " + "Clean up your notes so you can focus on the most important facts. " + "Respond with one note per line. You can have up to {limit} notes, " + "so make sure you reply with less than {limit} lines. " + "Your notes are:\n{notes}", + limit=limit, + notes=notes, + ) + + new_notes = [note.strip() for note in summary.split("\n") if note.strip()] + if len(new_notes) > actor_config.note_limit: + raise ActionError( + f"Too many notes. You can only have up to {actor_config.note_limit} notes." + ) + + action_actor.planner.notes[:] = new_notes + return "Notes were summarized successfully." + + def schedule_event(name: str, turns: int): """ Schedule an event to happen at a specific turn. Events are important occurrences that can affect the world in diff --git a/adventure/models/config.py b/adventure/models/config.py index 7e70364..c586a9c 100644 --- a/adventure/models/config.py +++ b/adventure/models/config.py @@ -43,6 +43,8 @@ class ServerConfig: @dataclass class WorldActorConfig: conversation_limit: int + event_limit: int + note_limit: int @dataclass @@ -88,6 +90,8 @@ DEFAULT_CONFIG = Config( world=WorldConfig( actor=WorldActorConfig( conversation_limit=2, + event_limit=5, + note_limit=10, ), size=WorldSizeConfig( actor_items=IntRange(min=0, max=2), diff --git a/adventure/simulate.py b/adventure/simulate.py index e4c59dd..cb6a7e1 100644 --- a/adventure/simulate.py +++ b/adventure/simulate.py @@ -27,6 +27,7 @@ from adventure.actions.planning import ( read_notes, replace_note, schedule_event, + summarize_notes, take_note, ) from adventure.context import ( @@ -248,12 +249,13 @@ def simulate_world( # build a toolbox for the planners planner_toolbox = Toolbox( [ - take_note, + check_calendar, + erase_notes, read_notes, replace_note, - erase_notes, schedule_event, - check_calendar, + summarize_notes, + take_note, ] )