games.single_player.base¶
Single-player game framework for LLM-powered games.
This module provides a core framework for building single-player games where an LLM can act as the player, the assistant, or the game engine. The framework is designed to be flexible, extensible, and independent of any multiplayer game concepts.
Example
>>> from haive.agents.single_player import SinglePlayerGameAgent
>>> class WordleAgent(SinglePlayerGameAgent):
... def __init__(self, config):
... super().__init__(config)
... self.state_manager = WordleStateManager
- Typical usage:
Inherit from SinglePlayerGameState for game-specific state
Inherit from SinglePlayerStateManager for game logic
Inherit from SinglePlayerGameConfig for configuration
Inherit from SinglePlayerGameAgent for the agent implementation
Classes¶
Difficulty level for a game. |
|
Mode of operation for the game. |
|
Source of the game content. |
|
Type of player in a single-player game. |
|
Base agent for single-player games. |
|
Configuration for single-player games. |
|
Base state for single-player games. |
|
Base state manager for single-player games. |
Module Contents¶
- class games.single_player.base.GameDifficulty¶
-
Difficulty level for a game.
Initialize self. See help(type(self)) for accurate signature.
- class games.single_player.base.GameMode¶
-
Mode of operation for the game.
Initialize self. See help(type(self)) for accurate signature.
- class games.single_player.base.GameSourceType¶
-
Source of the game content.
Initialize self. See help(type(self)) for accurate signature.
- class games.single_player.base.PlayerType¶
-
Type of player in a single-player game.
Initialize self. See help(type(self)) for accurate signature.
- class games.single_player.base.SinglePlayerGameAgent(config)¶
Base agent for single-player games.
This class provides the core functionality for single-player game agents, including state initialization, move handling, analysis, and visualization.
- config¶
Configuration for the game agent
- state_manager¶
Manager for game state transitions
- engines¶
Dictionary of LLM engines for move generation and analysis
- graph¶
State graph for game flow
- app¶
Compiled graph application
Initialize the game agent.
- Parameters:
config – Configuration for the game agent
- analyze_position(state)¶
Analyze the current game state.
- Parameters:
state (T) – Current game state
- Returns:
Command with the updated game state including analysis
- Return type:
langgraph.types.Command
- abstractmethod extract_move(response)¶
Extract move from engine response.
- Parameters:
response (Any) – Response from the engine
- Returns:
Extracted move
- Return type:
Any
- get_hint(state)¶
Get a hint for the current game state.
- Parameters:
state (T) – Current game state
- Returns:
Command with the updated game state including a hint
- Return type:
langgraph.types.Command
- initialize_game(state)¶
Initialize a new game.
- interactive_command(state, command)¶
Process an interactive command.
- Parameters:
state (T) – Current game state
command (str) – Command string
- Returns:
Command with the updated game state
- Return type:
langgraph.types.Command
- make_player_move(state)¶
Make a move for the player.
In auto mode, this uses the LLM to generate a move. In interactive mode, this just returns the state unchanged.
- Parameters:
state (T) – Current game state
- Returns:
Command with the updated game state
- Return type:
langgraph.types.Command
- abstractmethod prepare_analysis_context(state)¶
Prepare context for analysis.
- abstractmethod prepare_move_context(state)¶
Prepare context for move generation.
- save_state_history()¶
Save the current agent state to a JSON file.
- Return type:
None
- setup_workflow()¶
Setup the workflow for the game.
The workflow depends on the game mode: - Auto: Initialize -> Analyze -> Move -> Check -> Repeat - Interactive: Initialize -> Listen for commands - Assist: Initialize -> Analyze -> Listen for commands
- should_continue_game(state)¶
Check if the game should continue.
- Parameters:
state (T) – Current game state
- Returns:
True if the game should continue, False otherwise
- Return type:
- class games.single_player.base.SinglePlayerGameConfig(/, **data)¶
Bases:
pydantic.BaseModel
Configuration for single-player games.
This class defines the core configuration parameters that all single-player games need, including player type, game mode, and difficulty.
- Parameters:
data (Any)
- state_schema¶
The state schema class for the game
- player_type¶
Type of player (human, LLM, hybrid)
- game_mode¶
Mode of operation (interactive, auto, assist)
- difficulty¶
Difficulty level of the game
- max_hints¶
Maximum number of hints allowed
- auto_analyze¶
Whether to automatically analyze after each move
- engines¶
Configurations for game LLMs
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- class games.single_player.base.SinglePlayerGameState(/, **data)¶
Bases:
pydantic.BaseModel
Base state for single-player games.
This class defines the core state attributes that all single-player games need to track, including game status, move history, and analysis.
- Parameters:
data (Any)
- player_type¶
Type of player (human, LLM, hybrid)
- Type:
- difficulty¶
Difficulty level of the game
- Type:
- move_history¶
History of moves made
- Type:
List[Dict]
- analysis_history¶
History of analyses made
- Type:
List[Dict]
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- increment_move_count()¶
Increment the move count.
- Return type:
None
- use_hint()¶
Use a hint.
- Return type:
None
- class games.single_player.base.SinglePlayerStateManager¶
Bases:
Generic
[T
]Base state manager for single-player games.
This class provides the interface for managing game state transitions and operations. Each game should extend this with game-specific logic by implementing the required methods.
- Type Parameters:
T: The type of the game state, must be a Pydantic BaseModel.
- initialize()¶
Initialize a new game state
- Parameters:
difficulty (GameDifficulty)
player_type (PlayerType)
- Return type:
T
- apply_move()¶
Apply a move to the game state
- Parameters:
state (T)
move (Any)
- Return type:
T
- generate_hint()¶
Generate a hint for the current game state
- check_game_status()¶
Check and update the game status
- Parameters:
state (T)
- Return type:
T
- interactive_input()¶
Process interactive input from the player
- Parameters:
state (T)
user_input (str)
- Return type:
T
- classmethod apply_move(state, move)¶
- Abstractmethod:
- Parameters:
state (T)
move (Any)
- Return type:
T
Apply a move to the game state.
- Parameters:
state (T) – Current game state
move (Any) – Move to apply
- Returns:
Updated game state
- Return type:
T
- classmethod check_game_status(state)¶
- Abstractmethod:
- Parameters:
state (T)
- Return type:
T
Check and update the game status.
- Parameters:
state (T) – Current game state
- Returns:
Updated game state with status checked
- Return type:
T
- classmethod generate_hint(state)¶
Generate a hint for the current game state.
- classmethod get_legal_moves(state)¶
- Abstractmethod:
- Parameters:
state (T)
- Return type:
list[Any]
Get all legal moves for the current state.
- Parameters:
state (T) – Current game state
- Returns:
List of legal moves
- Return type:
list[Any]
- classmethod initialize(difficulty=GameDifficulty.MEDIUM, player_type=PlayerType.LLM, **kwargs)¶
- Abstractmethod:
- Parameters:
difficulty (GameDifficulty)
player_type (PlayerType)
- Return type:
T
Initialize a new game state.
- Parameters:
difficulty (GameDifficulty) – Difficulty level of the game
player_type (PlayerType) – Type of player
**kwargs – Additional game-specific initialization parameters
- Returns:
A new game state
- Return type:
T
- classmethod interactive_input(state, user_input)¶
Process interactive input from the player.
This method handles general commands like ‘hint’, ‘quit’, etc. Game-specific commands should be handled by overriding this method.
- Parameters:
state (T) – Current game state
user_input (str) – User input string
- Returns:
Updated game state
- Return type:
T