1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.ActionsSdkConversation = void 0;
|
19 | const conversation_1 = require("./conversation");
|
20 | const serializeData = (data) => JSON.stringify({ data });
|
21 | const 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 |
|
30 | class ActionsSdkConversation extends conversation_1.Conversation {
|
31 |
|
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 |
|
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 | }
|
95 | exports.ActionsSdkConversation = ActionsSdkConversation;
|
96 |
|
\ | No newline at end of file |