/**
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import * as Schema from '../api/schema' ; import { Headers } from '../framework' ; import { Prompt , PromptItem } from './prompt' ; import { JsonObject , clone , isJsonEqual } from '../common' ; import { Handler , Intent , Scene , Session , User , Device , Home , Expected } from './handler' ; import { Context } from './handler/context' ; import { ILogger } from '../logger' ; example public export interface ConversationV3Options { public body ? : Schema . HandlerRequest ; public headers ? : Headers ; see https public clientId ? : string ; public debug ? : boolean ; see debugLogger public logger ? : ILogger ; example see https public verification ? : ConversationVerification | string ; } hidden export interface ConversationOptions { public request ? : Schema . HandlerRequest ; public headers ? : Headers ; } see https public export interface ConversationVerification { public project : string ; public status ? : number ; public error ? : string | ( ( error : string ) => string ) ; } public export class ConversationV3 { public request : Schema . HandlerRequest ; public headers : Headers ; example public handler : Schema . Handler ; public intent : Schema . Intent ; public scene : Schema . Scene ; example see https public session : Schema . Session ; example see https public user : User ; see https public device : Schema . Device ; example see https public home : Home ; public expected : Expected ; public context : Schema . Context ; public prompt : Prompt ; public overwrite = true ; public digested = false ; public body : Schema . HandlerRequest ; hidden _internal : { raw ? : JsonObject ; promptSet : boolean ; orig : { scene : Schema . Scene ; session : Schema . Session ; user : Schema . User ; home : Schema . Home ; } ; } ; param options public constructor ( options = { } ) { const { headers = { } , body = { } } = options ; this . request = body ; this . headers = headers ; this . handler = new Handler ( body . handler ) ; this . intent = new Intent ( body . intent ) ; this . scene = new Scene ( body . scene ) ; this . session = new Session ( body . session as Schema . Session ) ; this . user = new User ( body . user ) ; this . device = new Device ( body . device ) ; this . home = new Home ( body . home ) ; this . expected = new Expected ( ) ; this . context = new Context ( body . context ) ; this . prompt = new Prompt ( ) ; this . _internal = { promptSet : false , orig : { scene : clone ( this . scene ) , session : clone ( this . session ) , user : clone ( this . user ) , home : clone ( this . home ) , } , } ; } public json < T = JsonObject > ( json ) { this . _internal . raw = json ; return this ; } example param promptItems public add ( ... promptItems ) { if ( this . digested ) { throw new Error ( 'Response has already been sent. ' + 'Is this being used in an async call that was not ' + 'returned as a promise to the intent handler?' ) ; } this . prompt . add ( ... promptItems ) ; this . _internal . promptSet = true ; return this ; } example param speech public append ( speech ) { if ( this . digested ) { throw new Error ( 'Response has already been sent. ' + 'Is this being used in an async call that was not ' + 'returned as a promise to the intent handler?' ) ; } this . prompt . append ( speech ) ; this . _internal . promptSet = true ; return this ; } public response ( ) { if ( this . digested ) { throw new Error ( 'Response has already been digested' ) ; } this . digested = true ; const session = new Session ( this . session ) ; if ( session . typeOverrides ! . length ) { for ( const override of session . typeOverrides ! ) { if ( override . mode ) { ( override as , JsonObject ) => ; typeOverrideMode = override . mode ; delete override . mode ; } } } else { delete session . typeOverrides ; } const response = { session , } ; if ( ! this . overwrite ) { this . prompt . override = false ; } response . prompt = new Prompt ( this . prompt ) ; if ( this . scene && ! isJsonEqual ( { ... this . scene } , { ... this . _internal . orig . scene } ) ) { response . scene = new Scene ( this . scene ) ; } if ( this . user && ! isJsonEqual ( { ... this . user } , { ... this . _internal . orig . user } ) ) { response . user = new User ( this . user ) ; } if ( this . home && ! isJsonEqual ( { ... this . home } , { ... this . _internal . orig . home } ) ) { ( response as Schema . HandlerResponse ) . home = new Home ( this . home ) ; } if ( this . expected . languageCode || ( this . expected . speech && this . expected . speech . length ) ) { response . expected = this . expected ; if ( this . expected . speech && ! this . expected . speech . length ) { delete response . expected . speech ; } } return clone ( response ) ; } public serialize ( ) { if ( this . _internal . raw ) { return this . _internal . raw ; } const handlerResponse = this . response ( ) ; return handlerResponse ; } } export interface ExceptionHandler < TConversation extends ConversationV3 > { public ( conv , error ) : Promise < any > | any ; }