System Prompt System - Manager¶
File: cogs/system_prompt/manager.py
The SystemPromptManager is the core engine of the system prompt feature. It handles the complex logic of inheriting and combining prompts from different levels, manages caching, and ensures content is safe.
SystemPromptManager Class¶
__init__(self, bot)¶
Initializes the manager, creating instances of the SystemPromptCache, PromptValidator, and PermissionValidator. It also sets up the data directory for storing configuration files.
get_effective_prompt(self, channel_id, guild_id, ...)¶
This is the most important method in the manager. It calculates the final system prompt to be used for a given channel by applying the three-tiered inheritance model.
- Process:
- Cache Check: It first checks the
SystemPromptCachefor a valid, non-expired entry for the channel. If found, it returns the cached prompt immediately. - Tier 1 (Base): It retrieves the base prompt from the YAML files using the
gpt.prompting.manager. - Tier 2 (Server): It loads the server's configuration file (
{guild_id}.json) and applies theserver_leveloverrides to the base prompt using_apply_server_overrides. - Tier 3 (Channel): It then applies the channel-specific overrides from the configuration file using
_apply_channel_overrides. - Localization & Variables: It applies language localizations and replaces dynamic variables (like
{current_time}). - Cache Update: The final, combined prompt is stored in the cache for future use.
- Cache Check: It first checks the
- Returns: A dictionary containing the final
promptstring and itssource(e.g., 'cache', 'channel', 'server', 'yaml').
Configuration Management¶
set_channel_prompt(...)/set_server_prompt(...): These methods handle the saving of new or updated prompt configurations. They first validate the input using thePromptValidatorand then write the data to the appropriate JSON configuration file.remove_channel_prompt(...)/remove_server_prompt(...): These methods remove the configuration for a channel or a server, causing them to fall back to the next level of the inheritance chain._load_guild_config(...)/_save_guild_config(...): Private methods for reading from and writing to the per-server JSON files.
Caching (SystemPromptCache)¶
The manager uses an in-memory SystemPromptCache with a Time-To-Live (TTL).
* get(...): Retrieves a prompt from the cache if it's not expired.
* set(...): Stores a newly generated prompt in the cache with a timestamp.
* invalidate(...): Clears the cache for a specific channel or an entire server. This is called automatically whenever a prompt is updated or removed to ensure changes take effect immediately.
Validation (PromptValidator)¶
This internal class ensures the safety and integrity of user-provided prompts.
* validate_prompt_content(...): Checks if the prompt content exceeds the maximum length (MAX_PROMPT_LENGTH) and scans it for potentially malicious code patterns (like <script> tags or javascript: URIs) using a list of regular expressions.