From af69d5b6b0d64a95f6ce52e76f426fa2158582c2 Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Wed, 5 Jun 2024 08:58:35 -0500 Subject: [PATCH] include player name and actions in discord prompt, include room count in generator prompt --- taleweave/bot/discord.py | 26 +++++++++++++++++++++++++- taleweave/context.py | 5 ++--- taleweave/generate.py | 17 +++++++++++++---- taleweave/main.py | 4 +++- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/taleweave/bot/discord.py b/taleweave/bot/discord.py index d11d73b..97d3083 100644 --- a/taleweave/bot/discord.py +++ b/taleweave/bot/discord.py @@ -427,9 +427,33 @@ def embed_from_player(event: PlayerEvent): def embed_from_prompt(event: PromptEvent): - # TODO: ping the player prompt_embed = Embed(title=event.room.name, description=event.character.name) prompt_embed.add_field(name="Prompt", value=truncate(event.prompt)) + + if has_player(event.character.name): + players = list_players() + user = next( + ( + player + for player, character in players.items() + if character == event.character.name + ), + None, + ) + + if user: + # TODO: use Discord user.mention to ping the user + prompt_embed.add_field( + name="Player", + value=user, + ) + + for action in event.actions: + # TODO: use a prompt template to summarize actions + action_name = action["function"]["name"] + action_description = action["function"]["description"] + prompt_embed.add_field(name=action_name, value=action_description) + return prompt_embed diff --git a/taleweave/context.py b/taleweave/context.py index be36142..b3220cc 100644 --- a/taleweave/context.py +++ b/taleweave/context.py @@ -34,6 +34,8 @@ dungeon_master: Agent | None = None # game context +# TODO: wrap this into a class that can be passed around +character_agents: Dict[str, Tuple[Character, Agent]] = {} event_emitter = EventEmitter() game_config: Config = DEFAULT_CONFIG game_systems: List[GameSystem] = [] @@ -41,9 +43,6 @@ system_data: Dict[str, Any] = {} -# TODO: where should this one go? -character_agents: Dict[str, Tuple[Character, Agent]] = {} - STRING_EVENT_TYPE = "message" diff --git a/taleweave/generate.py b/taleweave/generate.py index 63fde29..cc179ec 100644 --- a/taleweave/generate.py +++ b/taleweave/generate.py @@ -38,6 +38,9 @@ logger = getLogger(__name__) +MAX_NAME_LENGTH = 50 +DISALLOWED_PUNCTUATION = ['"', ":"] + def get_world_config(): config = get_game_config() @@ -58,12 +61,12 @@ def name_parser(value: str, **kwargs): format_prompt("world_generate_error_name_json", name=value) ) - if '"' in value or ":" in value: + if any(p in value for p in DISALLOWED_PUNCTUATION): raise ValueError( format_prompt("world_generate_error_name_punctuation", name=value) ) - if len(value) > 50: + if len(value) > MAX_NAME_LENGTH: raise ValueError( format_prompt("world_generate_error_name_length", name=value) ) @@ -99,6 +102,8 @@ def generate_room( agent: Agent, world: World, systems: List[GameSystem], + current_room: int | None = None, + total_rooms: int | None = None, ) -> Room: existing_rooms = [room.name for room in list_rooms(world)] @@ -108,6 +113,8 @@ def generate_room( context={ "world_theme": world.theme, "existing_rooms": existing_rooms, + "current_room": current_room, + "total_rooms": total_rooms, }, result_parser=duplicate_name_parser(existing_rooms), toolbox=None, @@ -575,9 +582,11 @@ def generate_world( set_system_data(system.name, data) # generate the rooms - for _ in range(room_count): + for i in range(room_count): try: - room = generate_room(agent, world, systems) + room = generate_room( + agent, world, systems, current_room=i, total_rooms=room_count + ) generate_system_attributes(agent, world, room, systems) broadcast_generated(entity=room) world.rooms.append(room) diff --git a/taleweave/main.py b/taleweave/main.py index 7f3eb1d..06b64b2 100644 --- a/taleweave/main.py +++ b/taleweave/main.py @@ -299,7 +299,9 @@ def load_or_generate_world( new_rooms = [] for i in range(add_rooms): logger.info(f"generating room {i + 1} of {add_rooms}") - room = generate_room(world_builder, world, systems) + room = generate_room( + world_builder, world, systems, current_room=i, total_rooms=add_rooms + ) new_rooms.append(room) world.rooms.append(room)