improved
Persistent Listen in Functions
November 4th, 2024 by Zoran Slamkov
What’s New
- Persistent Events: Functions can now define events that persist for the entire conversation session. This means that events associated with components like carousels, choice traces, or buttons remain active even after the conversation moves past the function step.
- Delayed User Interaction: Users can interact with these persistent components at any point during the session. When they do, the agent will refer back to the original function and proceed down the relevant path defined in your function code.
- Flexible Agent Behaviour: You now have control over whether the agent waits for user input at the function step or continues execution immediately, thanks to the new listen parameter settings.
How It Works
Option 1: Agent Waits for User Input (listen: true)
- Behavior:
- The agent pauses execution at the function step.
- It waits for immediate user input before proceeding.
- Use Case:
- Ideal when you require the user to make a choice or provide input before moving on.
- Implementation:
- Set listen: true in your function’s next command.
next: {
listen: true,
to: [
{
on: { 'event.type': 'option_selected', 'event.payload.value': '1' },
dest: 'option_1_path',
},
// Additional event handlers...
],
defaultTo: 'default_path',
},
Option 2: Agent Continues Execution (listen: false)
- Behavior:
- The agent continues execution without waiting at the function step.
- Events defined in the function persist throughout the session and can be triggered later.
- Use Case:
- Perfect for non-blocking interactions where users might choose to interact with components at their convenience.
- Implementation:
- Set listen: false in your function’s next command.
next: {
listen: false, // Must be explicitly set to false
to: [
{
on: { 'event.type': 'item_selected', 'event.payload.label': 'Item A' },
dest: 'item_A_path',
},
// Additional event handlers...
],
defaultTo: 'default_path',
},
Benefits
- Dynamic Conversations: Create agents that can handle delayed interactions, allowing users to make choices at any point during the conversation.
- Persistent Interactivity: Keep buttons and interactive elements active throughout the session.
Example
Function Code with Persistent Events:
export default async function main(args) {
return {
trace: [
{
type: 'carousel',
payload: {
cards: [
{
title: 'Product A',
description: { text: 'Description of Product A' },
imageUrl: 'https://example.com/productA.png',
buttons: [
{
name: 'Buy Now',
request: { type: 'purchase', payload: { productId: 'A' } },
},
],
},
// Additional cards...
],
},
},
],
next: {
listen: false, // Agent continues without waiting
to: [
{
on: { 'event.type': 'purchase', 'event.payload.productId': 'A' },
dest: 'purchase_A_path',
},
// Additional event handlers...
],
defaultTo: 'continue_path',
},
};
}
- Agent Behavior:
- Continues to 'continue_path' immediately.
- The purchase event remains active and can be triggered later.
- When the user clicks “Buy Now” for Product A, the agent navigates to 'purchase_A_path'.
Learn More
• Updated Documentation: Visit our Supporting listen in functions to learn more about using persistent listen functionality.