Skip to content

Commit

Permalink
include player name and actions in discord prompt, include room count…
Browse files Browse the repository at this point in the history
… in generator prompt
  • Loading branch information
ssube committed Jun 5, 2024
1 parent 5a32bd9 commit af69d5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
26 changes: 25 additions & 1 deletion taleweave/bot/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
5 changes: 2 additions & 3 deletions taleweave/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@
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] = []
prompt_library: PromptLibrary = PromptLibrary(prompts={})
system_data: Dict[str, Any] = {}


# TODO: where should this one go?
character_agents: Dict[str, Tuple[Character, Agent]] = {}

STRING_EVENT_TYPE = "message"


Expand Down
17 changes: 13 additions & 4 deletions taleweave/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

logger = getLogger(__name__)

MAX_NAME_LENGTH = 50
DISALLOWED_PUNCTUATION = ['"', ":"]


def get_world_config():
config = get_game_config()
Expand All @@ -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)
)
Expand Down Expand Up @@ -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)]

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion taleweave/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit af69d5b

Please sign in to comment.