## Initialization1. The script starts by importing various Python modules, including `argparse`, `toml`, `subprocess`, and `curses`, and various LLM binding libraries.
2. It defines a `GlobalState` class, which stores various global variables and settings, such as the game configuration, LLM configuration, and debug logging settings.
3. The script parses command-line arguments using `argparse`, which includes options for specifying the configuration file, transcript output file, and other settings.
4. It loads the configuration file (in TOML format) and sets up the game and LLM configurations using the `toml` module.
5. It creates an instance of the `GlobalState` class and sets up the global variables and settings.
## Game Loop
1. The script launches a ZIL interpreter (using the `bocfel` command) and sets up a subprocess to interact with it using the `subprocess` module.
2. It sends initialization commands to the game and ignores the output.
3. The script enters a loop where it:
* Reads user input (either from the terminal or from a splitscreen interface) using the `sys.stdin.readline()` function.
* Sends the input to the ZIL interpreter using the `process.stdin.write()` function.
* Gets the response from the ZIL interpreter using the `non_blocking_read()` function, which reads from the subprocess's stdout pipe.
* Checks if the response indicates a parser error; if so, it attempts to fix the error by trying alternative commands using the `try_to_fix_parser_error()` function.
* If the response is valid, it uses the LLM to rewrite the response in a more descriptive and engaging style using the `rewrite_response()` function.
* Outputs the rewritten response to the user, along with any relevant debug information, using the `show_output_to_user_etc()` function.
## LLM Interaction
1. The script uses the `get_llm_response()` function to interact with the LLM, which takes a prompt and returns a response.
2. The prompt is constructed by combining various elements, such as the game's current state, the user's input, and any relevant context, using the `add_to_llm_prompt()` function.
3. The script uses the LLM's response to generate a rewritten version of the game's response using the `process_rewritten_response_of_room()` function.
4. The rewritten response is then output to the user, along with any relevant debug information.
## Splitscreen Mode
1. If the `--splitscreen` option is specified, the script uses the `curses` library to create a splitscreen interface, where the game's output is displayed on one side and the user's input is displayed on the other.
2. The script uses the `get_command()` method to read user input from the splitscreen interface.
## Debugging and Logging
1. The script uses various logging mechanisms, including debug logs, transcripts, and error messages, to provide information about its operation.
2. It uses environment variables and command-line options to control the level of debugging and logging.
## Functions
Here's a brief explanation of some of the key functions used in the script:
* `add_to_llm_prompt()`: Adds a prompt to the LLM's input.
* `get_llm_response()`: Gets a response from the LLM.
* `rewrite_response()`: Rewrites the game's response using the LLM.
* `try_to_fix_parser_error()`: Tries to fix parser errors by attempting alternative commands.
* `show_output_to_user_etc()`: Outputs the rewritten response to the user, along with any relevant debug information.
* `non_blocking_read()`: Reads from the subprocess's stdout pipe without blocking.
* `configure_non_blocking_reads()`: Configures the subprocess's stdout pipe to be non-blocking.
## LLM Configuration
The script uses a TOML configuration file to specify the LLM configuration, which includes settings such as the LLM model, API key, and temperature. The script uses this configuration to interact with the LLM and generate rewritten responses.
## Game Configuration
The script uses a TOML configuration file to specify the game configuration, which includes settings such as the game path, syntax, and background information. The script uses this configuration to interact with the ZIL interpreter and generate rewritten responses.
Говно в общем.