Predefined script objects

Alan AI provides several predefined objects that you can use to store and share data:

authData

The authData object can be used to store static device- or user-specific data that need to be passed from the client app. The data is sent when the Alan AI SDK connects to the dialog script and can be accessed through the p.authData object.

For details, see authData.

client

The client object provides general information about users interacting with the AI assistant, such as approximate user’s location and interaction statistics.

For details, see Client object.

project

The project object can be used in the global project scope. The data stored in project is accessible from any script added to the AI assistant project. For example, if you want a variable to be available in all project scripts, you can save its value to project.

project can be useful if you want to split a large script and separate configuration and data structures from functions and project logic. This will make your project more transparent and easier to support.

Assume you have two scripts: script1 and script2. In script1, you can set project.config = {initValue: 1}. Then, you can use project.config in script2 as an existing and defined variable:

Dialog script
//script1
project.config = {initValue: 1};

//script2
console.log("Init value is " + project.config.initValue);

To store data in project, you can use onCreateProject callback. For details, see Predefined callbacks.

state

If you work with contexts in the dialog script, you can use the state object. Every context contains a preconfigured state object. You can think of state as a dictionary or knowledge base where you can keep any information you need.

The data stored in state is available in the current conversational context and can be accessed through the p.state object in the script. When a new context is activated, you can send data to it or overwrite it with the then function. For details, see Sharing information between contexts.

Dialog script
let startGame = context(() => {
    intent("What is my current score?", p => {
        p.play(`Your score is ${p.state.result.toString()}`);
    });
});

intent("Let's start the game", p =>{
    p.play("Sure, let's go");
    p.then(startGame, {state: {result:0}});
});

userData

The userData object can be used during a dialog session with a specific user. The session starts when the user launches your app and interacts with the AI assistant within it. When the user closes your app, the session ends. The session can also be terminated after 30 minutes of inactivity.

In userData, you can store any data that needs to be available during the current dialog session. This data is accessible through the p.userData object in the dialog script, from any script or context inside the project.

Dialog script
onCreateUser(p => {
    p.userData.favorites = ("action");
});

intent('What movies would you recommend?', p => {
    p.play(`Here are some popular movies in the ${p.userData.favorites} genre...`);
});

To store and reset data in userData, you can use onCreateUser and onCleanupUser callbacks. For details, see Predefined callbacks.

visual

The visual object can be used to store the visual state obtained from the client app. You can use this object to differentiate and filter commands in the dialog script based on the current visual context of your app.

The visual state object is different and independent for every user of your app. This object is session-specific: it does not persist between dialog sessions. For details, see Visual state.

Dialog script
intent(
    'What screen- is that?',
    'Where am I?',
    p => {
        if (!p.visual.screen) {
            p.play('This is an e-commerce app with an AI assistant');
            return;
        }
        switch (p.visual.screen) {
            case 'all':
                p.play('This is the main screen showing all available products.');
                break;
            case 'clothing':
                p.play('These are all of the available clothing.');
                break;
            case 'accessories':
                p.play('These are all of the available accessories.');
                break;
        }
    },
);