UNPKG

14.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const ConversationLearner_1 = require("../ConversationLearner");
5const CLRunner_1 = require("../CLRunner");
6var UIMode;
7(function (UIMode) {
8 UIMode["TEACH"] = "TEACH";
9 UIMode["EDIT"] = "EDIT";
10 UIMode["NONE"] = "NONE";
11})(UIMode = exports.UIMode || (exports.UIMode = {}));
12var BotStateType;
13(function (BotStateType) {
14 // Currently running application
15 BotStateType["APP"] = "APP";
16 // Conversation Id associated with this session
17 BotStateType["CONVERSATION_ID"] = "CONVERSATION_ID";
18 // BotBuilder conversation reference
19 BotStateType["CONVERSATION_REFERENCE"] = "CONVERSATION_REFERENCE";
20 // Which packages are active for editing
21 BotStateType["EDITING_PACKAGE"] = "EDITING_PACKAGE";
22 // Is UI in Teach or Edit mode?
23 BotStateType["UI_MODE"] = "UI_MODE";
24 // Last time active session was used (in ticks)
25 BotStateType["LAST_ACTIVE"] = "LAST_ACTIVE";
26 // If session is a chat session what is logDialogId
27 BotStateType["LOG_DIALOG_ID"] = "LOG_DIALOG_ID";
28 // Current message being processed
29 BotStateType["MESSAGE_MUTEX"] = "MESSAGE_MUTEX";
30 // True if onEndSession needs to be called
31 BotStateType["NEED_SESSIONEND_CALL"] = "ON_ENDSESSION_CALLED";
32 // Currently active session
33 BotStateType["SESSION_ID"] = "SESSION_ID";
34})(BotStateType = exports.BotStateType || (exports.BotStateType = {}));
35class BotState {
36 constructor(init) {
37 Object.assign(this, init);
38 }
39 static Get(clMemory) {
40 if (!BotState._instance) {
41 BotState._instance = new BotState();
42 }
43 BotState._instance.memory = clMemory;
44 return BotState._instance;
45 }
46 GetStateAsync(botStateType) {
47 return tslib_1.__awaiter(this, void 0, void 0, function* () {
48 if (!this.memory) {
49 throw new Error('BotState called without initializing memory');
50 }
51 try {
52 let data = yield this.memory.GetAsync(botStateType);
53 return JSON.parse(data);
54 }
55 catch (_a) {
56 // If brand new use, need to initialize
57 yield this._SetAppAsync(null);
58 let data = yield this.memory.GetAsync(botStateType);
59 return JSON.parse(data);
60 }
61 });
62 }
63 SetStateAsync(botStateType, value) {
64 return tslib_1.__awaiter(this, void 0, void 0, function* () {
65 if (!this.memory) {
66 throw new Error('BotState called without initializing memory');
67 }
68 const json = JSON.stringify(value);
69 yield this.memory.SetAsync(botStateType, json);
70 });
71 }
72 // NOTE: CLMemory should be the only one to call this
73 _SetAppAsync(app) {
74 return tslib_1.__awaiter(this, void 0, void 0, function* () {
75 yield this.SetApp(app);
76 yield this.SetConversationId(null);
77 yield this.SetConversationReference(null);
78 yield this.SetLastActive(0);
79 yield this.SetMessageProcessing(null);
80 yield this.SetNeedSessionEndCall(false);
81 yield this.SetUIMode(UIMode.NONE);
82 yield this.SetSessionId(null);
83 yield this.SetLogDialogId(null);
84 yield this.ClearEditingPackageAsync();
85 });
86 }
87 // ------------------------------------------------
88 // APP
89 // ------------------------------------------------
90 GetApp() {
91 return tslib_1.__awaiter(this, void 0, void 0, function* () {
92 try {
93 return yield this.GetStateAsync(BotStateType.APP);
94 }
95 catch (err) {
96 return null;
97 }
98 });
99 }
100 SetApp(app) {
101 return tslib_1.__awaiter(this, void 0, void 0, function* () {
102 if (!app) {
103 yield this.SetStateAsync(BotStateType.APP, null);
104 }
105 else {
106 // Store only needed data
107 let smallApp = {
108 appId: app.appId,
109 appName: app.appName,
110 livePackageId: app.livePackageId,
111 devPackageId: app.devPackageId,
112 metadata: {
113 isLoggingOn: app.metadata.isLoggingOn
114 }
115 };
116 yield this.SetStateAsync(BotStateType.APP, smallApp);
117 }
118 });
119 }
120 // ------------------------------------------------
121 // CONVERSATION_ID
122 // ------------------------------------------------
123 GetConversationId() {
124 return tslib_1.__awaiter(this, void 0, void 0, function* () {
125 return yield this.GetStateAsync(BotStateType.CONVERSATION_ID);
126 });
127 }
128 SetConversationId(conversationId) {
129 return tslib_1.__awaiter(this, void 0, void 0, function* () {
130 yield this.SetStateAsync(BotStateType.CONVERSATION_ID, conversationId);
131 });
132 }
133 // ------------------------------------------------
134 // EDITING_PACKAGE
135 // ------------------------------------------------
136 GetEditingPackages() {
137 return tslib_1.__awaiter(this, void 0, void 0, function* () {
138 return (yield this.GetStateAsync(BotStateType.EDITING_PACKAGE)) || {};
139 });
140 }
141 SetEditingPackage(appId, packageId) {
142 return tslib_1.__awaiter(this, void 0, void 0, function* () {
143 let activeApps = (yield this.GetStateAsync(BotStateType.EDITING_PACKAGE)) || {};
144 activeApps[appId] = packageId;
145 yield this.SetStateAsync(BotStateType.EDITING_PACKAGE, activeApps);
146 return activeApps;
147 });
148 }
149 GetEditingPackageForApp(appId) {
150 return tslib_1.__awaiter(this, void 0, void 0, function* () {
151 let activeApps = (yield this.GetStateAsync(BotStateType.EDITING_PACKAGE)) || {};
152 return activeApps[appId];
153 });
154 }
155 ClearEditingPackageAsync() {
156 return tslib_1.__awaiter(this, void 0, void 0, function* () {
157 yield this.SetStateAsync(BotStateType.EDITING_PACKAGE, {});
158 });
159 }
160 // ------------------------------------------------
161 // NEED_SESSIONEND_CALL
162 // ------------------------------------------------
163 GetNeedSessionEndCall() {
164 return tslib_1.__awaiter(this, void 0, void 0, function* () {
165 const needed = yield this.GetStateAsync(BotStateType.NEED_SESSIONEND_CALL);
166 return (needed ? needed : false);
167 });
168 }
169 SetNeedSessionEndCall(needed) {
170 return tslib_1.__awaiter(this, void 0, void 0, function* () {
171 needed = needed ? needed : false;
172 yield this.SetStateAsync(BotStateType.NEED_SESSIONEND_CALL, needed);
173 });
174 }
175 // ------------------------------------------------
176 // LAST_ACTIVE
177 // ------------------------------------------------
178 GetLastActive() {
179 return tslib_1.__awaiter(this, void 0, void 0, function* () {
180 return yield this.GetStateAsync(BotStateType.LAST_ACTIVE);
181 });
182 }
183 SetLastActive(lastActive) {
184 return tslib_1.__awaiter(this, void 0, void 0, function* () {
185 yield this.SetStateAsync(BotStateType.LAST_ACTIVE, lastActive);
186 });
187 }
188 // ------------------------------------------------
189 // SESSION_ID
190 // ------------------------------------------------
191 GetSessionIdAndSetConversationId(conversationId) {
192 return tslib_1.__awaiter(this, void 0, void 0, function* () {
193 // If conversationId not set yet, use the session and set it
194 let existingConversationId = yield this.GetConversationId();
195 if (!existingConversationId) {
196 yield this.SetConversationId(conversationId);
197 return yield this.GetStateAsync(BotStateType.SESSION_ID);
198 }
199 // If conversation Id matches return the sessionId
200 else if (existingConversationId === conversationId) {
201 return yield this.GetStateAsync(BotStateType.SESSION_ID);
202 }
203 // If existingConversationId Id is a object - TEAMs Channel
204 else if (typeof existingConversationId === 'object') {
205 if (existingConversationId.conversation.id === conversationId) {
206 return yield this.GetStateAsync(BotStateType.SESSION_ID);
207 }
208 }
209 // Otherwise session is for another conversation
210 return null;
211 });
212 }
213 GetSessionIdAsync() {
214 return tslib_1.__awaiter(this, void 0, void 0, function* () {
215 return yield this.GetStateAsync(BotStateType.SESSION_ID);
216 });
217 }
218 SetSessionId(sessionId) {
219 return tslib_1.__awaiter(this, void 0, void 0, function* () {
220 yield this.SetStateAsync(BotStateType.SESSION_ID, sessionId);
221 });
222 }
223 InitSessionAsync(sessionId, logDialogId, conversationId, sessionStartFlags) {
224 return tslib_1.__awaiter(this, void 0, void 0, function* () {
225 yield this.SetSessionId(sessionId);
226 yield this.SetLogDialogId(logDialogId);
227 yield this.SetNeedSessionEndCall(true);
228 yield this.SetConversationId(conversationId);
229 yield this.SetLastActive(new Date().getTime());
230 yield this.SetUIMode((sessionStartFlags & CLRunner_1.SessionStartFlags.IN_TEACH) > 0 ? UIMode.TEACH : UIMode.NONE);
231 yield this.SetMessageProcessing(null);
232 });
233 }
234 // End a session.
235 EndSessionAsync() {
236 return tslib_1.__awaiter(this, void 0, void 0, function* () {
237 yield this.SetSessionId(null);
238 yield this.SetLogDialogId(null);
239 yield this.SetConversationId(null);
240 yield this.SetLastActive(0);
241 yield this.SetUIMode(UIMode.NONE);
242 yield this.SetMessageProcessing(null);
243 });
244 }
245 // ------------------------------------------------
246 // UI_MODE
247 // ------------------------------------------------
248 getUIMode() {
249 return tslib_1.__awaiter(this, void 0, void 0, function* () {
250 const uiMode = yield this.GetStateAsync(BotStateType.UI_MODE);
251 return uiMode;
252 });
253 }
254 SetUIMode(uiMode) {
255 return tslib_1.__awaiter(this, void 0, void 0, function* () {
256 yield this.SetStateAsync(BotStateType.UI_MODE, uiMode);
257 });
258 }
259 // ------------------------------------------------
260 // CONVERSATION_REFERENCE
261 // ------------------------------------------------
262 SetConversationReference(conversationReference) {
263 return tslib_1.__awaiter(this, void 0, void 0, function* () {
264 yield this.SetStateAsync(BotStateType.CONVERSATION_REFERENCE, conversationReference);
265 });
266 }
267 GetConversationReverence() {
268 return tslib_1.__awaiter(this, void 0, void 0, function* () {
269 return yield this.GetStateAsync(BotStateType.CONVERSATION_REFERENCE);
270 });
271 }
272 // For initial pro-active message need to build conversation reference from scratch
273 CreateConversationReference(userName, userId, conversationId) {
274 return tslib_1.__awaiter(this, void 0, void 0, function* () {
275 let conversationReference = {
276 user: { name: userName, id: userId },
277 conversation: { id: conversationId },
278 channelId: 'emulator',
279 // TODO: Refactor away from static coupling. BotState needs to have access to options object through constructor
280 // tslint:disable-next-line:no-http-string
281 serviceUrl: `http://127.0.0.1:${ConversationLearner_1.ConversationLearner.options.botPort}`
282 };
283 this.SetConversationReference(conversationReference);
284 });
285 }
286 // ------------------------------------------------
287 // LOG_DIALOG_ID
288 // ------------------------------------------------
289 GetLogDialogId() {
290 return tslib_1.__awaiter(this, void 0, void 0, function* () {
291 return yield this.GetStateAsync(BotStateType.LOG_DIALOG_ID);
292 });
293 }
294 SetLogDialogId(logDialogId) {
295 return tslib_1.__awaiter(this, void 0, void 0, function* () {
296 yield this.SetStateAsync(BotStateType.LOG_DIALOG_ID, logDialogId);
297 });
298 }
299 // ------------------------------------------------
300 // MESSAGE_MUTEX
301 // ------------------------------------------------
302 GetMessageProcessing() {
303 return tslib_1.__awaiter(this, void 0, void 0, function* () {
304 return yield this.GetStateAsync(BotStateType.MESSAGE_MUTEX);
305 });
306 }
307 MessageProcessingPopAsync() {
308 return tslib_1.__awaiter(this, void 0, void 0, function* () {
309 let popVal = yield this.GetStateAsync(BotStateType.MESSAGE_MUTEX);
310 yield this.SetStateAsync(BotStateType.MESSAGE_MUTEX, null);
311 return popVal;
312 });
313 }
314 SetMessageProcessing(queuedInput) {
315 return tslib_1.__awaiter(this, void 0, void 0, function* () {
316 yield this.SetStateAsync(BotStateType.MESSAGE_MUTEX, queuedInput);
317 });
318 }
319 // -------------------------------------------------------------------
320 SessionInfoAsync() {
321 return tslib_1.__awaiter(this, void 0, void 0, function* () {
322 const conversationReference = yield this.GetConversationReverence();
323 if (conversationReference && conversationReference.conversation) {
324 return {
325 userName: conversationReference.user && conversationReference.user.name,
326 userId: conversationReference.user && conversationReference.user.id,
327 logDialogId: yield this.GetLogDialogId()
328 };
329 }
330 return {
331 userName: '',
332 userId: '',
333 logDialogId: ''
334 };
335 });
336 }
337}
338exports.BotState = BotState;
339//# sourceMappingURL=BotState.js.map
\No newline at end of file