UNPKG

9.82 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const CLStrings_1 = require("../CLStrings");
4const CLM = require("@conversationlearner/models");
5class ReadOnlyClientMemoryManager {
6 constructor(prevMemories, curMemories, allEntities, sessionInfo) {
7 this.allEntities = [];
8 this.allEntities = allEntities;
9 this.sessionInfo = sessionInfo;
10 this.prevMemories = prevMemories;
11 this.curMemories = curMemories;
12 this.__expired = false;
13 }
14 Expire() {
15 this.__expired = true;
16 }
17 __FindEntity(entityName) {
18 return this.allEntities.find(e => e.entityName == entityName);
19 }
20 __ToString(value) {
21 if (typeof value == 'object') {
22 return JSON.stringify(value);
23 }
24 return value.toString();
25 }
26 /**
27 * Get current value of entity
28 */
29 Get(entityName, converter) {
30 return this.GetValues(entityName, this.curMemories, converter);
31 }
32 /**
33 * Get value of entity before most recent input
34 */
35 GetPrevious(entityName, converter) {
36 return this.GetValues(entityName, this.prevMemories, converter);
37 }
38 /**
39 * Get information about the current running session
40 */
41 SessionInfo() {
42 return this.sessionInfo;
43 }
44 static AS_VALUE(memoryValues) {
45 if (memoryValues.length > 1) {
46 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_VALUE_LIST_EXCEPTION);
47 }
48 return memoryValues.length == 0 ? null : memoryValues[0];
49 }
50 static AS_VALUE_LIST(memoryValues) {
51 return memoryValues;
52 }
53 static AS_STRING(memoryValues) {
54 return memoryValues.length == 0 ? null : CLM.memoryValuesAsString(memoryValues);
55 }
56 static AS_STRING_LIST(memoryValues) {
57 return memoryValues.map(mv => {
58 if (typeof mv.userText !== 'string') {
59 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_NOT_A_STRING_EXCEPTION);
60 }
61 return mv.userText;
62 });
63 }
64 static AS_NUMBER(memoryValues) {
65 if (memoryValues.length == 0) {
66 return null;
67 }
68 if (memoryValues.length > 1) {
69 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_NUMBER_LIST_EXCEPTION);
70 }
71 const number = Number(memoryValues[0].userText);
72 if (isNaN(number)) {
73 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_NOT_A_NUMBER_EXCEPTION);
74 }
75 return number;
76 }
77 static AS_NUMBER_LIST(memoryValues) {
78 return memoryValues.map(mv => {
79 let number = Number(mv.userText);
80 if (isNaN(number)) {
81 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_NOT_A_NUMBER_EXCEPTION);
82 }
83 return number;
84 });
85 }
86 static AS_BOOLEAN(memoryValues) {
87 if (memoryValues.length == 0) {
88 return null;
89 }
90 if (memoryValues.length > 1) {
91 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_BOOLEAN_LIST_EXCEPTION);
92 }
93 let text = CLM.memoryValuesAsString(memoryValues);
94 if (text.toLowerCase() === 'true') {
95 return true;
96 }
97 if (text.toLowerCase() === 'false') {
98 return false;
99 }
100 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_NOT_A_BOOLEAN_EXCEPTION);
101 }
102 static AS_BOOLEAN_LIST(memoryValues) {
103 return memoryValues.map(mv => {
104 if (!mv.userText) {
105 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_NOT_A_BOOLEAN_EXCEPTION);
106 }
107 if (mv.userText.toLowerCase() === 'true') {
108 return true;
109 }
110 if (mv.userText.toLowerCase() === 'false') {
111 return false;
112 }
113 throw new Error(CLStrings_1.CLStrings.MEMORY_MANAGER_NOT_A_BOOLEAN_EXCEPTION);
114 });
115 }
116 GetValues(entityName, entityMap, converter) {
117 let values = entityMap.Values(entityName);
118 const entity = this.__FindEntity(entityName);
119 if (entity && entity.entityType === CLM.EntityType.LUIS && entity.enumValues) {
120 for (let value of values) {
121 const enumValue = entity.enumValues.find(ev => ev.enumValue === value.enumValueId);
122 value.displayText = enumValue ? enumValue.enumValue : null;
123 }
124 }
125 // cast to conditional type is necessary
126 // see here for description https://github.com/Microsoft/TypeScript/issues/22735#issuecomment-376960435
127 if (!converter) {
128 const entityValues = entityMap.Values(entityName);
129 const foundEntity = this.__FindEntity(entityName);
130 if (foundEntity && !foundEntity.isMultivalue) {
131 return ClientMemoryManager.AS_VALUE(entityValues);
132 }
133 return entityValues;
134 }
135 return converter(entityMap.Values(entityName));
136 }
137}
138exports.ReadOnlyClientMemoryManager = ReadOnlyClientMemoryManager;
139class ClientMemoryManager extends ReadOnlyClientMemoryManager {
140 constructor(prevMemories, curMemories, allEntities, sessionInfo) {
141 super(prevMemories, curMemories, allEntities, sessionInfo);
142 this.DeleteAll = () => this.DeleteAllExcept();
143 }
144 AsReadOnly() {
145 return this;
146 }
147 Set(entityName, value) {
148 if (this.__expired) {
149 throw new Error(`ClientMemoryManager: RememberEntity "${entityName}" ${CLStrings_1.CLStrings.MEMORY_MANAGER_EXPIRED_EXCEPTION}`);
150 }
151 let entity = this.__FindEntity(entityName);
152 if (!entity) {
153 throw new Error(`${CLStrings_1.CLStrings.API_MISSING_ENTITY} ${entityName}`);
154 }
155 if (CLM.isPrebuilt(entity)) {
156 throw new Error(`${CLStrings_1.CLStrings.MEMORY_MANAGER_PRETRAINED_EXCEPTION} ${entityName}`);
157 }
158 // ENUM entity type
159 if (entity.entityType === CLM.EntityType.ENUM && entity.enumValues) {
160 const stringVal = this.__ToString(value).toUpperCase();
161 const enumValue = entity.enumValues.find(ev => ev.enumValue === stringVal);
162 if (!enumValue) {
163 const enumValues = entity.enumValues.map(ev => ev.enumValue).join(", ");
164 // Throw w/o a stack trace
165 throw `"${entityName}"${CLStrings_1.CLStrings.MEMORY_MANAGER_INVALID_ENUM_EXCEPTION1}"${stringVal}"${CLStrings_1.CLStrings.MEMORY_MANAGER_INVALID_ENUM_EXCEPTION2}(${enumValues})`;
166 }
167 else {
168 // Store ENUM ID not the string value
169 this.curMemories.Remember(entity.entityName, entity.entityId, enumValue.enumValue, entity.isMultivalue, null, null, enumValue.enumValueId);
170 }
171 }
172 else if (Array.isArray(value)) {
173 if (!entity.isMultivalue) {
174 throw new Error(`Array passed to Set for entity (${entityName}) that isn't Multi-Value.`);
175 }
176 let stringValues = value.map((v) => {
177 return this.__ToString(v);
178 });
179 this.curMemories.RememberMany(entity.entityName, entity.entityId, stringValues, entity.isMultivalue);
180 }
181 else {
182 let stringValue = this.__ToString(value);
183 this.curMemories.Remember(entity.entityName, entity.entityId, stringValue, entity.isMultivalue);
184 }
185 }
186 Delete(entityName, value = null) {
187 if (this.__expired) {
188 throw new Error(`ClientMemoryManager: ForgetEntity "${entityName}" ${CLStrings_1.CLStrings.MEMORY_MANAGER_EXPIRED_EXCEPTION}`);
189 }
190 let entity = this.__FindEntity(entityName);
191 if (!entity) {
192 throw new Error(`${CLStrings_1.CLStrings.API_MISSING_ENTITY} ${entityName}`);
193 }
194 this.curMemories.Forget(entity.entityName, value, entity.isMultivalue);
195 }
196 /** Delete all entity values apart from any included in the list of saveEntityNames
197 * @param saveEntityNames Array of entity names not to forget
198 */
199 DeleteAllExcept(...saveEntityNames) {
200 if (this.__expired) {
201 throw new Error(`ClientMemoryManager: DeleteAll ${CLStrings_1.CLStrings.MEMORY_MANAGER_EXPIRED_EXCEPTION}`);
202 }
203 for (let entity of this.allEntities) {
204 if (saveEntityNames.indexOf(entity.entityName) < 0) {
205 this.curMemories.Forget(entity.entityName, null, entity.isMultivalue);
206 }
207 }
208 }
209 /**
210 * Copy values from one entity to another
211 * @param entityNameFrom Source Entity
212 * @param entityNameTo Destination Entity
213 */
214 Copy(entityNameFrom, entityNameTo) {
215 if (this.__expired) {
216 throw new Error(`ClientMemoryManager: Copy ${CLStrings_1.CLStrings.MEMORY_MANAGER_EXPIRED_EXCEPTION}`);
217 }
218 let entityFrom = this.__FindEntity(entityNameFrom);
219 let entityTo = this.__FindEntity(entityNameTo);
220 if (!entityFrom) {
221 throw new Error(`${CLStrings_1.CLStrings.API_MISSING_ENTITY} ${entityNameFrom}`);
222 }
223 if (!entityTo) {
224 throw new Error(`${CLStrings_1.CLStrings.API_MISSING_ENTITY} ${entityNameTo}`);
225 }
226 if (entityFrom.isMultivalue != entityTo.isMultivalue) {
227 throw new Error(`Can't copy between multivalue and non-multivalue Entity (${entityNameFrom} -> ${entityNameTo})`);
228 }
229 // Clear "To" entity
230 this.curMemories.Forget(entityNameTo);
231 // Get value of "From" entity
232 let values = this.curMemories.ValueAsList(entityNameFrom);
233 // Copy values from "From"
234 for (let value of values) {
235 this.Set(entityNameTo, value);
236 }
237 }
238}
239exports.ClientMemoryManager = ClientMemoryManager;
240//# sourceMappingURL=ClientMemoryManager.js.map
\No newline at end of file