2020 get_players_in_room )
2121from among_them .utils .end_utils import get_end_game_reason
2222from among_them .utils .history_utils import initialize_history , create_system_message
23- from among_them .utils .prompt_utils import reconstruct_environment_prompt_from_history
2423from among_them .game_config import GameConfig
2524
2625class GameEngine :
@@ -44,7 +43,7 @@ def __init__(self, game_config: GameConfig = GameConfig(), file_path: Optional[s
4443 if file_path is not None :
4544 self .file_path = file_path
4645
47- def get_turn_context (self , player_name : Optional [str ] = None ) -> Optional [tuple [History , List [Action ], str , str , List [dict ]]]:
46+ def get_turn_context (self , player_name : Optional [str ] = None ) -> Optional [tuple [History , List [Action ], List [ dict ] , List [dict ]]]:
4847 """Gathers all necessary context for the current player's turn.
4948
5049 This method calculates everything needed for a turn up-front to avoid
@@ -56,8 +55,8 @@ def get_turn_context(self, player_name: Optional[str] = None) -> Optional[tuple[
5655 Returns:
5756 A tuple containing:
5857 - An incomplete History object representing the current turn's context.
59- - The system prompt for the LLM .
60- - The user prompt for the LLM .
58+ - List of actions the player can take .
59+ - The conversation history with the new user message appended (List[Dict]) .
6160 - A list of pre-discussion vote prompts (empty if not in DISCUSS phase).
6261 Or None if the game is in a state that doesn't require player input.
6362 """
@@ -69,7 +68,7 @@ def get_turn_context(self, player_name: Optional[str] = None) -> Optional[tuple[
6968 # If game ended no further player input is required.
7069 if get_end_game_reason (self .history , self .players ) is not None :
7170 print ("Game over! {}" .format (get_end_game_reason (self .history , self .players )))
72- return None , None , None , None , None
71+ return None , None , None , None
7372
7473 current_player , next_player_names = get_next_random_player (self .history , self .players )
7574
@@ -104,7 +103,7 @@ def get_turn_context(self, player_name: Optional[str] = None) -> Optional[tuple[
104103 actions_player_can_take = get_vote_actions (self .history , self .players , current_player )
105104 location = Location .CAFETERIA
106105 else :
107- return None , None , None , None , None
106+ return None , None , None , None
108107
109108 # Create a placeholder action for the current player
110109 placeholder_action = Action (player_name = current_player .name , type = ActionType .SPEAK , target_message = "placeholder" )
@@ -127,12 +126,33 @@ def get_turn_context(self, player_name: Optional[str] = None) -> Optional[tuple[
127126 votes_before_this_discussion_message = {},
128127 )
129128
130- # Build new environment-like prompt
131- environment_prompt = reconstruct_environment_prompt_from_history (
129+ # Build conversation history from existing turns + new user message
130+ from among_them .utils .prompt_utils import build_conversation_for_player , get_initial_turn_prompt , get_incremental_observations
131+
132+ # Get past conversation turns for this player
133+ conversation = build_conversation_for_player (
132134 current_player , self .history , self .players , self .game_config
133135 )
134- system_prompt = "" # System context is included in environment_prompt
135- user_prompt = environment_prompt
136+
137+ # Find last turn index for this player to determine if this is first turn
138+ player_turn_indices = [i for i , event in enumerate (self .history ) if event .action_taken .player_name == current_player .name ]
139+
140+ # Generate new user message for current turn
141+ if not player_turn_indices :
142+ # First turn: include system prompt
143+ new_user_msg = get_initial_turn_prompt (
144+ current_player , self .history , self .players , self .game_config , len (self .history )
145+ )
146+ else :
147+ # Subsequent turn: only incremental observations
148+ last_turn_index = player_turn_indices [- 1 ]
149+ new_user_msg = get_incremental_observations (
150+ current_player , self .history , self .players , self .game_config ,
151+ last_turn_index , len (self .history )
152+ )
153+
154+ # Append new user message to conversation
155+ conversation .append ({"role" : "user" , "content" : new_user_msg })
136156
137157 pre_discussion_vote_prompts = []
138158 if phase == GamePhase .DISCUSS :
@@ -149,17 +169,38 @@ def get_turn_context(self, player_name: Optional[str] = None) -> Optional[tuple[
149169 alive_player_names = alive_player_names ,
150170 )
151171 )
152- voting_prompt = reconstruct_environment_prompt_from_history (
172+
173+ # Build conversation for this player up to voting phase
174+ voting_conversation = build_conversation_for_player (
153175 player , fake_history , self .players , self .game_config
154176 )
177+
178+ # Find last turn for this player in fake_history
179+ player_turn_indices = [i for i , event in enumerate (fake_history ) if event .action_taken .player_name == player .name ]
180+
181+ # Generate voting prompt message
182+ if not player_turn_indices :
183+ # First turn scenario (shouldn't happen in voting but handle it)
184+ voting_user_msg = get_initial_turn_prompt (
185+ player , fake_history , self .players , self .game_config , len (fake_history )
186+ )
187+ else :
188+ last_turn_index = player_turn_indices [- 1 ]
189+ voting_user_msg = get_incremental_observations (
190+ player , fake_history , self .players , self .game_config ,
191+ last_turn_index , len (fake_history )
192+ )
193+
194+ # Append the new voting message
195+ voting_conversation .append ({"role" : "user" , "content" : voting_user_msg })
196+
155197 pre_discussion_vote_prompts .append ({
156198 "player" : player ,
157- "system_prompt" : "" ,
158- "user_prompt" : voting_prompt ,
199+ "conversation" : voting_conversation , # Now a conversation!
159200 "actions" : fake_voting_actions
160201 })
161202
162- return turn_history , actions_player_can_take , system_prompt , user_prompt , pre_discussion_vote_prompts
203+ return turn_history , actions_player_can_take , conversation , pre_discussion_vote_prompts
163204
164205 def step (self , turn_context : History , action_taken : Action , llm_response : str = "" , llm_cot : str = "" , token_usage : dict = {}, pre_discussion_votes : Optional [dict ] = None ) -> tuple [bool , Optional [EndGameReason ]]:
165206 """Executes a single player action and updates the game state using pre-calculated context.
0 commit comments