Skip to main content
Triggered before a conversation thread is loaded from history. Use this event to inspect or modify the messages before they are displayed in the chat UI.

Event properties

type
string
required
Always 'pre:threadLoaded'.
messages
array
required
Array of messages to load into the chat.
messages[].id
string
Unique identifier for the message.
messages[].sender
string
Sender type: 'user' or 'response'.
messages[].content
array
Array of content items in the message.
messages[].message_state
object
State information for the message.

Example

instance.on('pre:threadLoaded', (event, instance) => {
    console.log('Loading thread with messages:', event.messages);
    
    // Add feedback controls to all agent responses
    event?.messages.forEach((message) => {
        if (message?.sender === 'response') {
            const [lastItem] = message.content;
            lastItem.message_options = {
                feedback: {
                    is_on: true
                }
            };
            
            // Initialize feedback state
            message.message_state = {
                content: {
                    1: {
                        feedback: {
                            text: "",
                            is_positive: true,
                            selected_categories: []
                        }
                    }
                }
            };
        }
    });
    
    // Modify first message
    if (event.messages.length > 0) {
        event.messages[0].content[0].text = 'Previous conversation loaded';
    }
});

Do you need practical examples?

Learn how to apply the features available for embedded chat into your implementation with guidance and examples.