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[].messageId
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[].content[].text
string
Text content of the message item. If you need to modify the displayed text only, update this property.
messages[].content[].response_type
string
Type of the content item, for example, 'text'.
messages[].message_state
object
State information for the message. You can create a new object with the spread operator before adding nested custom properties.
messages[].message_state.user_defined
object
A dedicated namespace for your custom metadata. Use this namespace to:
  • Prevent conflicts with system properties.
  • Keep your custom data organized.
  • Ensure future platform updates do not break your code.
To use this namespace, create a new message_state object and a new user_defined object inside it. Add your custom properties inside user_defined.
If message_state or user_defined was previously sealed or made non-extensible, directly assigning a new nested property can throw an Object is not extensible error in strict mode. To make your code more robust and preserve existing properties, use the spread operator to create a fresh object reference before adding custom fields:
messageIdsForUpdate
array
Array of message IDs to persist to the backend database.When you modify historical messages in pre:threadLoaded, those changes only affect what displays in the UI. To save your modifications to the backend, add the messageId of each modified message to this array. This triggers automatic PATCH API calls that persist your changes.
The system does not automatically set tracking properties. If you need to track whether modifications were applied, create a new message_state object and set the flag inside message_state.user_defined before adding the message ID to the array.

Examples

The following example demonstrates how to modify historical messages when loading a conversation thread, including adding feedback controls to agent responses and updating message text:
The following example demonstrates how to translate all historical messages when loading conversation history, with intelligent skipping of already-translated content:
The following example demonstrates how to optimize translation performance by tracking the current language state of each message and only translating when necessary:

Considerations

Complement to pre:receive

Use both events together for consistent modifications and ensure that all messages are processed: To avoid duplicate processing, you can:
  1. Set a tracking flag in message_state.user_defined during pre:receive.
  2. Check for this flag in pre:threadLoaded.
  3. Process only messages without the flag.
Example tracking flag: message_state.user_defined.already_translated = true

Do you need practical examples?

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