Manager
Module to control the history plugins and provide an abstract interface to execute them.
- HISTORY_PLUGIN_ERROR_MESSAGE = 'No history plugin set. Set plugin before operations.'
Default history plugin error message.
- class HistoryManager(config, plugin=None)[source]
Manages history operations by delegating to a specific history implementation.
Example
>>> user_id = "a658710c-de6d-11ef-ae5b-52b437312584" >>> chat_id = "af83c6d2-de6d-11ef-ac4d-52b437312584" >>> manager = HistoryManager(config, plugin=LocalHistory) >>> entries = manager.read(user_id) >>> manager.write(chat_id, user_id, "How do I check disk space?", "Use df -h command...") >>> manager.clear()
Initialize the history manager.
- Parameters:
config (Config) – Instance of configuration class
user_id (int) – The effective user id who asked for the history.
plugin (Optional[Type[BaseHistory]], optional) – Optional history implementation class
- clear(user_id)[source]
Clear all history entries.
- Return type:
- Parameters:
user_id (str) – The user’s identifier
- Raises:
RuntimeError – If no plugin is set
- clear_from_chat(user_id, from_chat)[source]
Clear all history entries.
- Return type:
- Parameters:
user_id (str) – The user’s identifier
- Raises:
RuntimeError – If no plugin is set
- property is_history_enabled: bool
Check if the history is enabled in the configuration file.
- Returns:
Trueif history is enabled,Falseotherwise.- Return type:
- property plugin: Type[BaseHistoryPlugin] | None
Property for the internal plugin attribute
- Returns:
Instance of the provided plugin (if any)
- Return type:
Optional[Type[BaseHistory]]
- read(user_id)[source]
Read history entries using the current plugin.
- Return type:
list[HistoryModel]- Parameters:
user_id (str) – The user’s identifier
- Raises:
RuntimeError – If no plugin is set
- Returns:
List of history entries
- Return type:
Union[list, Sequence[Any]]
- read_from_chat(user_id, from_chat)[source]
Read history entries using the current plugin.
- Return type:
Optional[HistoryModel]- Parameters:
user_id (str) – The user’s identifier
- Raises:
RuntimeError – If no plugin is set
- Returns:
An optional single history entry
- Return type:
Optional[HistoryModel]
- write(chat_id, user_id, query, response)[source]
Write a new history entry using the current plugin.