Message History¶
- class streamlit_rich_message_history.history.MessageHistory[source]¶
Bases:
object
Class to store and manage a history of messages in a Streamlit application.
The MessageHistory class provides a convenient way to maintain a conversation-like interface in Streamlit apps, with support for different types of messages (user, assistant, error) and rich content components.
It manages the addition, storage, and rendering of messages, as well as the registration of custom component types, detectors, and renderers.
- messages¶
A list of Message objects that comprise the conversation history
- add_assistant_message(message)[source]¶
Add a pre-created assistant message to the history.
- Parameters:
message (
AssistantMessage
) – The AssistantMessage object to add to the history- Return type:
None
- add_assistant_message_create(avatar)[source]¶
Create and add a new empty assistant message.
This creates an assistant message that can be populated with components after creation.
- Parameters:
avatar (
str
) – Avatar image URL or emoji for the assistant- Returns:
The created assistant message
- Return type:
- add_error_message(avatar, error_text)[source]¶
Create and add an error message to the history.
- Parameters:
avatar (
str
) – Avatar image URL or emoji for the error messageerror_text (
str
) – The error text to display
- Returns:
The created error message
- Return type:
- add_user_message(message)[source]¶
Add a pre-created user message to the history.
- Parameters:
message (
UserMessage
) – The UserMessage object to add to the history- Return type:
None
- add_user_message_create(avatar, text)[source]¶
Create and add a new user message with text.
- Parameters:
avatar (
str
) – Avatar image URL or emoji for the usertext (
str
) – The text content of the user message
- Returns:
The created user message
- Return type:
- static register_component_detector(component_type, detector)[source]¶
Register a detector function for a component type.
The detector function determines whether a given content should be treated as the specified component type.
- Parameters:
component_type (
ComponentType
) – The component type to register a detector fordetector (
Callable
[[Any
,dict
],bool
]) – A function that takes (content, kwargs) and returns True if the content should be treated as this component type
- Return type:
None
- static register_component_method(method_name, component_type, method_func=None)[source]¶
Register a new component method to the Message class.
This allows adding custom methods to Message objects for adding specific types of components with a convenient API.
- Parameters:
method_name (
str
) – The name of the method to add (e.g., ‘add_chart’)component_type (
ComponentType
) – The component type this method will createmethod_func (
Optional
[Callable
]) – Optional custom function to use for the method (if None, a default implementation will be used)
- Return type:
None
- static register_component_renderer(component_type, renderer)[source]¶
Register a renderer function for a component type.
The renderer function handles displaying the component in the Streamlit UI.
- Parameters:
component_type (
ComponentType
) – The component type to register a renderer forrenderer (
Callable
[[Any
,dict
],None
]) – A function that takes (content, kwargs) and renders the component in the Streamlit app
- Return type:
None
- static register_component_type(name)[source]¶
Register a new component type for use in messages.
This allows extending the library with custom component types that can be detected and rendered appropriately.
- Parameters:
name (
str
) – The name of the new component type- Returns:
The created component type enum value
- Return type: