alan-ai / alan-sdk-ionic

Conversational AI SDK for Ionic to enable text and voice conversations with actions (React, Angular, Vue)
https://alan.app
1.69k stars 18 forks source link

Callback for callProjectApi() doesn't get executed on native platforms #23

Open ljudbane opened 2 years ago

ljudbane commented 2 years ago

When we call callProjectApi() from ionic application to execute a command from Alan scripts, the callback doesn't get executed when running on Android or iOS device (or simulator). It does work when running in browser (ionic serve).

For example we call Alan's function greetUser from ionic:

this.alanButtonRef.componentOnReady().then(async () => {
    try {
        if (!await this.alanButtonRef.isActive()) {
            console.log('Calling: ' + 'await this.alanButtonRef.activate();');
            await this.alanButtonRef.activate();
        }

        console.log('Calling: ' + 'this.alanButtonRef.callProjectApi(\'greetUser\', {}, ...');
        this.alanButtonRef.callProjectApi('greetUser', {}, (error, result) => {
            console.log(error, 'error');
            console.log(result, 'result');
        });
    } catch (e) {
        console.log('EXCEPTION in sendGreeting()');
        console.log(e);
    }
});

The callback function doesn't get called and those console logs don't ever get printed. Neither is there any caught exception. When testing on Android device we didn't see anything in the logs that would indicate what's the problem. But when running on iOS there is a log entry that could point to the problem:

To Native Cordova ->  alanVoice isActive alanVoice1282791712 ["options": []]
⚡️  [log] - Calling: await this.alanButtonRef.activate();
To Native Cordova ->  alanVoice activate alanVoice1282791713 ["options": []]
⚡️  [log] - Calling: this.alanButtonRef.callProjectApi('greetUser', {}, ...
To Native Cordova ->  alanVoice callProjectApi alanVoice1282791714 ["options": [greetUser, {
}]]
⚡️  [log] - Error in Success callbackId: alanVoice1282791714 : TypeError: second argument to Function.prototype.apply must be an Array-like object (evaluating 'callback.apply(this, ...arguments)')
⚡️  [error] - {}

⚡️  ------ STARTUP JS ERROR ------

⚡️  TypeError: second argument to Function.prototype.apply must be an Array-like object (evaluating 'callback.apply(this, ...arguments)')
⚡️  URL: capacitor://localhost/polyfills-es2015.js
⚡️  polyfills-es2015.js:3737:36

⚡️  See above for help with debugging blank-screen issues
snyuryev commented 2 years ago

Hey @ljudbane We will try to reproduce your issue.

snyuryev commented 2 years ago

Hey @ljudbane We found how to reproduce the issue which is related to your scenario. I will let you know when the fix for your case is ready.

snyuryev commented 2 years ago

@ljudbane We have a new release v 2.4.0 Please take a look - that should resolve this issue.

ljudbane commented 2 years ago

@snyuryev Thank you, i will check it out and report back if the issue is fixed for me.

ljudbane commented 2 years ago

I did some testing on iOS simulator.

First i updated libs to most recent versions:

    "@alan-ai/alan-button": "^1.9.22",
    "@alan-ai/cordova-plugin-alan-voice": "^2.4.0",

First call in our app to project API is:

this.alanButtonRef.componentOnReady().then(async () => {
    try {
        if (!await this.alanButtonRef.isActive()) {
            console.log('Calling: ' + 'await this.alanButtonRef.activate();');
            await this.alanButtonRef.activate();
        }

        console.log('Calling: ' + 'this.alanButtonRef.callProjectApi(\'greetUser\', {}, ...');
        await this.alanButtonRef.callProjectApi('greetUser', {}, (error, result) => {
            console.log('THIS IS CALLBACK!!');
            console.log(error, 'error');
            console.log(result, 'result');
        });
    } catch (e) {
        console.log('EXCEPTION in sendGreeting()', e);
    }
});

And in the log of simulator there are not any console.log outputs from callback function, nor from the catch block:

To Native Cordova ->  alanVoice addButton alanVoice1003651242 ["options": [xxxx/stage, <null>, <null>, 60px, <null>, {
}]]
2022-01-19 16:37:36.414939+0100 App[26337:364739] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x60000288bb20> F8BB1C28-BAE8-11D6-9C31-00039315CD46
AlanBase main thread started
reconnecting: wss://studio.alan.app/ws_project/xxxx/stage
name resolved: studio.alan.app:443 - 35.232.244.12:443
⚡️  [log] - Alan: buttonState event: CONNECTING
authorized with dialogId: bae664f0-793d-11ec-a504-e74e8c746d30
⚡️  [log] - Alan: buttonState event: ONLINE
To Native Cordova ->  alanVoice setVisualState alanVoice1003651243 ["options": [{
    initialStatsAverageCartValue = "1230.365";
    initialStatsCurrency = USD;
    initialStatsLoaded = 1;
    initialStatsNewCustomers = 1;
    initialStatsOrders = 2;
    initialStatsRevenue = "2460.73";
    initialStatsVisits = 70;
    pushNotificationsEnabled = 1;
}]]
To Native Cordova ->  alanVoice isActive alanVoice1003651244 ["options": []]
To Native Cordova ->  alanVoice activate alanVoice1003651245 ["options": []]
⚡️  [log] - Calling: await this.alanButtonRef.activate();
⚡️  [log] - Calling: this.alanButtonRef.callProjectApi('greetUser', {}, ...
To Native Cordova ->  alanVoice callProjectApi alanVoice1003651246 ["options": [greetUser, {
}]]

Then i click on a button in our app that launches another callProjectAPI:

this.alanButtonRef.componentOnReady().then(async () => {
        try {
            if (!await this.alanButtonRef.isActive()) {
                await this.alanButtonRef.activate();
            }

            await this.alanButtonRef.callProjectApi('pushCampaignIntro', {}, (error, result) => {
                console.log('THIS IS CALLBACK!!');
                console.log(error, 'error');
                console.log(result, 'result');
            });
        } catch (e) {
            console.log('EXCEPTION IN pushCampaignIntro');
            console.log(e);
        }
});

And in the log i see the same error that was present before at the time of original issue:

To Native Cordova ->  alanVoice isActive alanVoice1003651248 ["options": []]
To Native Cordova ->  alanVoice activate alanVoice1003651249 ["options": []]
To Native Cordova ->  alanVoice callProjectApi alanVoice1003651250 ["options": [pushCampaignIntro, {
}]]
⚡️  [log] - Alan: buttonState event listener: LISTEN
⚡️  [log] - Error in Success callbackId: alanVoice1003651250 : TypeError: second argument to Function.prototype.apply must be an Array-like object (evaluating 'callback.apply(this, ...arguments)')
⚡️  [error] - {}

⚡️  ------ STARTUP JS ERROR ------

⚡️  TypeError: second argument to Function.prototype.apply must be an Array-like object (evaluating 'callback.apply(this, ...arguments)')
⚡️  URL: capacitor://localhost/polyfills-es2015.js
⚡️  polyfills-es2015.js:3737:36

⚡️  See above for help with debugging blank-screen issues
⚡️  [log] - Alan: buttonState event listener: REPLY
⚡️  [log] - Alan: buttonState event listener: ONLINE

Every subsequent callProjectApi will trigger the error. Only the first one won't. We plan to test on Android next, so i will report those findings later.

ljudbane commented 2 years ago

I've also tested on Android and the code in callback function doesn't get executed. I don't see any error in logcat.

snyuryev commented 2 years ago

@ljudbane Did you run ionic capacitor copy android or ionic capacitor copy ios after plugin update and before run the app?

ljudbane commented 2 years ago

@snyuryev Yes i did, multiple times. Sorry for the late reply.

I will try to replicate the issue in one of your example apps.