UNPKG

3.46 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.ActionsSdkConversation = void 0;
19const conversation_1 = require("./conversation");
20const serializeData = (data) => JSON.stringify({ data });
21const deserializeData = (body, defaultData) => {
22 const { conversation = {} } = body;
23 const { conversationToken } = conversation;
24 const data = conversationToken
25 ? JSON.parse(conversationToken).data
26 : Object.assign({}, defaultData);
27 return data;
28};
29/** @public */
30class ActionsSdkConversation extends conversation_1.Conversation {
31 /** @public */
32 constructor(options = {}) {
33 const { body = {} } = options;
34 super({
35 request: body,
36 headers: options.headers,
37 init: options.init,
38 ordersv3: options.ordersv3,
39 });
40 this.body = body;
41 const { inputs = [] } = body;
42 const [firstInput = {}] = inputs;
43 const { intent = '' } = firstInput;
44 this.intent = intent;
45 this.data = deserializeData(this.body, this._init.data);
46 }
47 /** @public */
48 serialize() {
49 if (this._raw) {
50 return this._raw;
51 }
52 const { richResponse, expectUserResponse, userStorage, expectedIntent, noInputPrompts, speechBiasingHints, } = this.response();
53 const inputPrompt = {
54 noInputPrompts,
55 };
56 if (richResponse.items.length) {
57 inputPrompt.richInitialPrompt = richResponse;
58 }
59 const possibleIntents = [
60 expectedIntent || {
61 intent: 'actions.intent.TEXT',
62 },
63 ];
64 const expectedInput = {
65 possibleIntents,
66 speechBiasingHints,
67 };
68 if (inputPrompt.richInitialPrompt || inputPrompt.noInputPrompts) {
69 expectedInput.inputPrompt = inputPrompt;
70 }
71 const response = {
72 expectUserResponse,
73 };
74 if (expectUserResponse) {
75 response.expectedInputs = [expectedInput];
76 }
77 else {
78 response.finalResponse = { richResponse };
79 }
80 const convDataDefault = deserializeData({}, this._init.data);
81 const convDataDefaultSerialized = serializeData(convDataDefault);
82 const convDataDefaulted = deserializeData(this.body, this._init.data);
83 const convDataIn = serializeData(convDataDefaulted);
84 const convDataOut = serializeData(this.data);
85 if (convDataOut !== convDataDefaultSerialized ||
86 convDataOut !== convDataIn) {
87 response.conversationToken = convDataOut;
88 }
89 if (userStorage) {
90 response.userStorage = userStorage;
91 }
92 return response;
93 }
94}
95exports.ActionsSdkConversation = ActionsSdkConversation;
96//# sourceMappingURL=conv.js.map
\No newline at end of file