1 | ;
|
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 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.ContextValues = void 0;
|
19 | const common_1 = require("../../common");
|
20 | const isV1 = (context) => typeof context.lifespan === 'number';
|
21 | class ContextValues {
|
22 | /** @hidden */
|
23 | constructor(outputContexts = [], _session) {
|
24 | this._session = _session;
|
25 | this.input = {};
|
26 | for (const context of outputContexts) {
|
27 | const name = context.name;
|
28 | const parameters = context.parameters;
|
29 | if (isV1(context)) {
|
30 | const lifespan = context.lifespan;
|
31 | Object.assign(this.input, {
|
32 | [name]: {
|
33 | name,
|
34 | lifespan,
|
35 | parameters,
|
36 | },
|
37 | });
|
38 | continue;
|
39 | }
|
40 | const lifespanCount = context.lifespanCount;
|
41 | const find = /([^/]+)?$/.exec(name);
|
42 | Object.assign(this.input, {
|
43 | [find ? find[0] : name]: {
|
44 | name,
|
45 | lifespan: lifespanCount,
|
46 | parameters,
|
47 | },
|
48 | });
|
49 | }
|
50 | this.output = {};
|
51 | }
|
52 | /** @hidden */
|
53 | _serialize() {
|
54 | return Object.keys(this.output).map((name) => {
|
55 | const { lifespan, parameters } = this.output[name];
|
56 | return {
|
57 | name: `${this._session}/contexts/${name}`,
|
58 | lifespanCount: lifespan,
|
59 | parameters,
|
60 | };
|
61 | });
|
62 | }
|
63 | /** @hidden */
|
64 | _serializeV1() {
|
65 | return Object.keys(this.output).map((name) => {
|
66 | const { lifespan, parameters } = this.output[name];
|
67 | return {
|
68 | name,
|
69 | lifespan,
|
70 | parameters,
|
71 | };
|
72 | });
|
73 | }
|
74 | /**
|
75 | * Returns the incoming context by name for this intent.
|
76 | *
|
77 | * @example
|
78 | * ```javascript
|
79 | *
|
80 | * const AppContexts = {
|
81 | * NUMBER: 'number',
|
82 | * }
|
83 | *
|
84 | * const app = dialogflow()
|
85 | *
|
86 | * app.intent('Default Welcome Intent', conv => {
|
87 | * conv.contexts.set(AppContexts.NUMBER, 1)
|
88 | * conv.ask('Welcome to action snippets! Say a number.')
|
89 | * })
|
90 | *
|
91 | * // Create intent with 'number' context as requirement
|
92 | * app.intent('Number Input', conv => {
|
93 | * const context = conv.contexts.get(AppContexts.NUMBER)
|
94 | * })
|
95 | * ```
|
96 | *
|
97 | * @param name The name of the Context to retrieve.
|
98 | * @return Context value matching name or undefined if no matching context.
|
99 | * @public
|
100 | */
|
101 | get(name) {
|
102 | return this.input[name];
|
103 | }
|
104 | /**
|
105 | * Set a new context for the current intent.
|
106 | *
|
107 | * @example
|
108 | * ```javascript
|
109 | *
|
110 | * const AppContexts = {
|
111 | * NUMBER: 'number',
|
112 | * }
|
113 | *
|
114 | * const app = dialogflow()
|
115 | *
|
116 | * app.intent('Default Welcome Intent', conv => {
|
117 | * conv.contexts.set(AppContexts.NUMBER, 1)
|
118 | * conv.ask('Welcome to action snippets! Say a number.')
|
119 | * })
|
120 | *
|
121 | * // Create intent with 'number' context as requirement
|
122 | * app.intent('Number Input', conv => {
|
123 | * const context = conv.contexts.get(AppContexts.NUMBER)
|
124 | * })
|
125 | * ```
|
126 | *
|
127 | * @param name Name of the context. Dialogflow converts to lowercase.
|
128 | * @param lifespan Context lifespan.
|
129 | * @param parameters Context parameters.
|
130 | * @public
|
131 | */
|
132 | set(name, lifespan, parameters) {
|
133 | this.output[name] = {
|
134 | lifespan,
|
135 | parameters,
|
136 | };
|
137 | }
|
138 | /** @public */
|
139 | delete(name) {
|
140 | this.set(name, 0);
|
141 | }
|
142 | /**
|
143 | * Returns the incoming contexts for this intent as an iterator.
|
144 | *
|
145 | * @example
|
146 | * ```javascript
|
147 | *
|
148 | * const AppContexts = {
|
149 | * NUMBER: 'number',
|
150 | * }
|
151 | *
|
152 | * const app = dialogflow()
|
153 | *
|
154 | * app.intent('Default Welcome Intent', conv => {
|
155 | * conv.contexts.set(AppContexts.NUMBER, 1)
|
156 | * conv.ask('Welcome to action snippets! Say a number.')
|
157 | * })
|
158 | *
|
159 | * // Create intent with 'number' context as requirement
|
160 | * app.intent('Number Input', conv => {
|
161 | * for (const context of conv.contexts) {
|
162 | * // do something with the contexts
|
163 | * }
|
164 | * })
|
165 | * ```
|
166 | *
|
167 | * @public
|
168 | */
|
169 | [Symbol.iterator]() {
|
170 | const contexts = common_1.values(this.input);
|
171 | return contexts[Symbol.iterator]();
|
172 | // suppose to be Array.prototype.values(), but can't use because of bug:
|
173 | // https://bugs.chromium.org/p/chromium/issues/detail?id=615873
|
174 | }
|
175 | }
|
176 | exports.ContextValues = ContextValues;
|
177 | //# sourceMappingURL=context.js.map |
\ | No newline at end of file |