Overview
Theengine/state.py module manages global state variables for the active session, world, character, model, and rules. It also handles state persistence to disk via persist.json.
Global State Variables
Session State
Currently selected world ID (e.g.,
"cyberpunk", "fantasy")Currently selected character ID (e.g.,
"elara", "mercenary")Currently active session ID (12-character hex string, e.g.,
"a1b2c3d4e5f6")/world select- SetsACTIVE_WORLD_ID/character select- SetsACTIVE_CHARACTER_ID/session new- SetsACTIVE_SESSION_ID/session continue- Sets all three based on session data/session delete- ClearsACTIVE_SESSION_IDif current session deleted
Model State
Currently selected LLM model (e.g.,
"google/gemini-2.0-pro-exp-02-05:free")Whether the model has been explicitly selected by the user (vs default)
/modelcommand - Sets both values and confirms selection- Session continuation - Restores model from session metadata
Rules State
List of active rule IDs (e.g.,
["gritty", "nsfw"])/rules add- Appends rule ID if not already present/rules clear- Empties the list/nsfw- Toggles"nsfw"in the list
Available Assets
All world assets loaded from
assets/worlds/*.yaml - Format: [{"id": "...", "name": "..."}]All character assets loaded from
assets/characters/*.yamlAll rule assets loaded from
assets/rules/*.yamlAll LLM models fetched from providers (OpenRouter, Groq, Gemini, DeepSeek) during startup
Functions
save_state
Persists current state topersist.json for recovery on restart.
- Collects current values of active session variables
- Writes JSON file with world, character, session, model, and rules
- File is created in the working directory as
persist.json
engine/state.py:9-19
- Every command that modifies state (
/world select,/character select,/model,/rules add,/session new, etc.)
persist.json:
load_persisted_state
Loads state frompersist.json on engine startup to restore the previous session.
- Checks if
persist.jsonexists - Reads and parses JSON
- Updates global state variables with persisted values
- Falls back to defaults if file doesn’t exist or is invalid
engine/state.py:22-39
engine/state.py:64)
Note: session_id is not restored from persist.json. Sessions must be explicitly continued with /session continue.
State Lifecycle
Engine Startup
- Module loads, initializes variables to defaults
available_worlds/characters/rulespopulated from YAML filesload_persisted_state()called to restore previous stateengine/main.pyfetches models and populatesAVAILABLE_MODELS
User Commands
Commands modify state variables and call
save_state():/world select fantasy→ACTIVE_WORLD_ID = "fantasy"→save_state()/model gpt-4o-mini→CURRENT_MODEL = "gpt-4o-mini"→save_state()
Usage Examples
Reading State
Modifying State
Using Available Assets
Persistence File Location
- Filename:
persist.json - Location: Working directory where the engine is launched
- Format: JSON
- Encoding: UTF-8
Thread Safety
Related
Handlers
broadcast_sync_state sends state to TUI
Commands
Commands that modify state
Session Management
How sessions use state
Configuration
Initial state setup