<commit_before>import {Plugin, HandleRequest, EnumRequestType} from "jovo-core";
import _set = require('lodash.set');
import _get = require('lodash.get');

import {GoogleAssistant} from "../GoogleAssistant";
import {GoogleAction} from "../core/GoogleAction";
import {GoogleActionResponse} from "../core/GoogleActionResponse";
import {GoogleActionSpeechBuilder} from "../core/GoogleActionSpeechBuilder";

import uuidv4 = require('uuid/v4');
import {EnumGoogleAssistantRequestType} from "../core/google-assistant-enums";



export class GoogleAssistantCore implements Plugin {

    install(googleAssistant: GoogleAssistant) {
        googleAssistant.middleware('$init')!.use(this.init.bind(this));
        googleAssistant.middleware('$type')!.use(this.type.bind(this));

        googleAssistant.middleware('after.$type')!.use(this.userStorageGet.bind(this));

        googleAssistant.middleware('$output')!.use(this.output.bind(this));
        googleAssistant.middleware('after.$output')!.use(this.userStorageStore.bind(this));

        GoogleAction.prototype.displayText = function(displayText: string) {
            _set(this.$output, 'GoogleAssistant.displayText',
                displayText
            );
            return this;
        };

    }

    async init(handleRequest: HandleRequest) {
        const requestObject = handleRequest.host.$request;
        if (requestObject.user &&
            requestObject.conversation &&
            requestObject.surface &&
            requestObject.availableSurfaces) {
            handleRequest.jovo = new GoogleAction(handleRequest.app, handleRequest.host, handleRequest);
        }
    }

    type(googleAction: GoogleAction) {
        if (_get(googleAction.$originalRequest || googleAction.$request, 'inputs[0].intent') === 'actions.intent.CANCEL') {
            _set(googleAction.$type, 'type', EnumRequestType.END);
        }
        if (_get(googleAction.$originalRequest || googleAction.$request,
            'inputs[0].arguments[0].name') === 'is_health_check' &&
            _get(googleAction.$originalRequest || googleAction.$request,
                'inputs[0].arguments[0].boolValue') === true &&
            googleAction.$app.config.handlers.ON_HEALTH_CHECK) {
            _set(googleAction.$type, 'type', EnumGoogleAssistantRequestType.ON_HEALTH_CHECK);
            _set(googleAction.$type, 'optional', true);
        }
    }

    async output(googleAction: GoogleAction) {
        const output = googleAction.$output;
        if (!googleAction.$originalResponse) {
            googleAction.$originalResponse = new GoogleActionResponse();
        }

        const tell = _get(output, 'GoogleAssistant.tell') || _get(output, 'tell');
        if (tell) {<commit_msg>:bulb: adds comment to mark placeholder code for GoogleAction object creation<commit_after>import {Plugin, HandleRequest, EnumRequestType} from "jovo-core";
import _set = require('lodash.set');
import _get = require('lodash.get');

import {GoogleAssistant} from "../GoogleAssistant";
import {GoogleAction} from "../core/GoogleAction";
import {GoogleActionResponse} from "../core/GoogleActionResponse";
import {GoogleActionSpeechBuilder} from "../core/GoogleActionSpeechBuilder";

import uuidv4 = require('uuid/v4');
import {EnumGoogleAssistantRequestType} from "../core/google-assistant-enums";



export class GoogleAssistantCore implements Plugin {

    install(googleAssistant: GoogleAssistant) {
        googleAssistant.middleware('$init')!.use(this.init.bind(this));
        googleAssistant.middleware('$type')!.use(this.type.bind(this));

        googleAssistant.middleware('after.$type')!.use(this.userStorageGet.bind(this));

        googleAssistant.middleware('$output')!.use(this.output.bind(this));
        googleAssistant.middleware('after.$output')!.use(this.userStorageStore.bind(this));

        GoogleAction.prototype.displayText = function(displayText: string) {
            _set(this.$output, 'GoogleAssistant.displayText',
                displayText
            );
            return this;
        };

    }

    async init(handleRequest: HandleRequest) {
        const requestObject = handleRequest.host.$request;
        /**
         * placeholder, since GoogleAction can't be run without dialogflow currently.
         * Platform object creation is therefor handled by dialogflow integration.
         */
        if (requestObject.user &&
            requestObject.conversation &&
            requestObject.surface &&
            requestObject.availableSurfaces) {
            handleRequest.jovo = new GoogleAction(handleRequest.app, handleRequest.host, handleRequest);
        }
    }

    type(googleAction: GoogleAction) {
        if (_get(googleAction.$originalRequest || googleAction.$request, 'inputs[0].intent') === 'actions.intent.CANCEL') {
            _set(googleAction.$type, 'type', EnumRequestType.END);
        }
        if (_get(googleAction.$originalRequest || googleAction.$request,
            'inputs[0].arguments[0].name') === 'is_health_check' &&
            _get(googleAction.$originalRequest || googleAction.$request,
                'inputs[0].arguments[0].boolValue') === true &&
            googleAction.$app.config.handlers.ON_HEALTH_CHECK) {
            _set(googleAction.$type, 'type', EnumGoogleAssistantRequestType.ON_HEALTH_CHECK);
            _set(googleAction.$type, 'optional', true);
        }
    }

    async output(googleAction: GoogleAction) {
        const output = googleAction.$output;
        if (!googleAction.$originalResponse) {
            googleAction.$originalResponse = new GoogleActionResponse();
        }

        const tell = _get(output, 'GoogleAssistant.tell') || _get(output, 'tell');
        if (tell) {
{'fraction_non_alphanumeric': 0.1307544197444425, 'fraction_numerical': 0.0024505513740591634, 'mean_word_length': 3.2514880952380953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2018693', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1503, 'n_words': 260}