Lifecycle callbacks

To create a robust AI assistant, you must understand in which state a dialog with the user can be and know how and when the dialog transitions between the states.

AI assistant lifecycle

Every project you create with Alan AI goes through the following workflow:

  1. Project is created: The project lifecycle begins when you create and save scripts for the AI assistant in Alan AI Studio. At this step, Alan AI loads the scripts in the Alan AI Cloud and builds the project model for the AI assistant.

    Alan AI reloads scripts and rebuilds the project model every time you update and save scripts in Alan AI Studio.

  2. User is connected: When a new user opens an app with the AI assistant and starts a conversation, Alan AI connects to the project in the Alan AI Cloud, creates a new dialog for this user and assigns a unique ID to this dialog. For every user interacting with Alan AI, a separate dialog is created. All the communication with Alan AI is performed within this dialog, even if the user stops interaction for a while by clicking or tapping the AI assistant button.

  3. User is disconnected: When the user stops communicating with the app, he or she gets disconnected, and Alan AI closes the dialog created for the user. The dialog can also be closed in the following cases:

    • The user is inactive for a 30-minute period.

    • The dialog scripts for the project are reloaded.

Callbacks

As the dialog transitions from state to state, you may need to adjust the AI assistant behavior. To let you do it, Alan AI offers a set of predefined callbacks. You can use these callbacks to perform necessary tasks and handle significant events in your dialog script:

onCreateProject()

This callback is invoked when the project is created and the project model is built. You can perform any initialization activities that may be required for your assistant.

Dialog script
onCreateProject(() => {
    project.drinks = "green tea, black tea, oolong";
});

intent(`Get me $(DRINKS: ${project.drinks})`, p => {
    p.play(`Adding ${p.DRINKS.value} to your order...`)
});;

onCreateUser()

This callback is invoked when a new user connects to Alan AI and starts the dialog session. Here, for example, you can set user-specific data.

Dialog script
onCreateUser(p => {
    p.userData.name = "John Smith";
});

onUserEvent((p, e) => {
    if (e.event == 'firstClick') {
        p.play(`Hi, ${p.userData.name}, how can I help you today?`);
    }
});

onCleanupUser()

This callback is invoked when the user disconnects from Alan AI or the dialog is closed. Here you can perform any cleanup activities or save the user data.

Dialog script
onCleanupUser(p => {
    p.userData.name = "";
});

onVisualState()

This callback is invoked when the visual state is sent from the client app.

Setting the visual state in the app:

Client app
<script>
  function myFunction() {
    alanBtnInstance.setVisualState({"page": "admittance"});
  }
</script>

Playing a greeting in the dialog script:

Dialog script
onVisualState((p, s) => {
    if (p.visual.page === "admittance") {
        p.play("Hello there! I'm your AI assistant, here to guide you through your journey towards academic success.")
    }
});

onUserEvent()

This callback invoked when Alan AI emits events driven by users’ interactions with the AI assistant.

Dialog script
onCreateUser(p => {
    p.userData.name = "John Smith";
});

onUserEvent((p, e) => {
    if (e.event == 'firstClick') {
        p.play(`Hi, ${p.userData.name}, how can I help you today?`);
    }
});

onEnter()

This callback is invoked when a context added to the script is activated.

Dialog script
let countContext = context(() => {
    onEnter(p => {
        p.state.result = 0;
    });

    intent('Yes', p => {
        p.state.result += 1;
        p.play(p.state.result.toString());
    });
});

intent("Count the number of times I've said yes", p =>{
    p.play("Sure, let's go");
    p.then(countContext);
});