Skip to main content
Triggered before chat receives a response from the agent. Use this event to inspect or modify the agent’s response before it is rendered in the UI.

Event properties

type
string
required
Always 'pre:receive'.
message
object
required
The agent’s response payload.
message.content
array
required
Array of content items in the response.
message.content[].text
string
Text content of the response item. If you need to modify the displayed text only, update this property.
message.content[].response_type
string
Type of the response item, for example, 'text', 'user_defined'.
message.message_state
object
State information for the message. You can create a new object with the spread operator before adding nested custom properties.
message.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:
saveUpdate
boolean
Set to true to persist client-side modifications to the backend database.When you modify message content in pre:receive, those changes only affect what displays in the UI. This triggers an automatic PATCH API call that persists 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 enabling saveUpdate.

Examples

The following example demonstrates how to modify agent response text and add custom feedback controls before displaying the message to the user:
The following example demonstrates how to translate agent responses from English to the user’s preferred language before displaying them in the UI:
The following example demonstrates how to translate with streaming suppression. When using translation with streaming responses, suppress delta rendering in pre:stream:delta:

Considerations

Persistence across sessions

Modifications made in pre:receive might not run in all scenarios, for example:
  • Users might close browser before response arrives.
  • Users navigate away during response.
  • Long-running flows return responses hours later.
To address persistence across sessions, you can implement the same logic in both events:
  1. Use pre:receive for new messages.
  2. Use pre:threadLoaded for historical messages.
  3. Add a tracking flag in message_state.user_defined to avoid duplicate processing.
Example tracking flag: message_state.user_defined.already_processed = true

Do you need practical examples?

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