UNPKG

6.49 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const CLDebug_1 = require("../CLDebug");
5const models_1 = require("@conversationlearner/models");
6const NEGATIVE_PREFIX = '~';
7class BotMemory {
8 constructor(init) {
9 this.filledEntityMap = new models_1.FilledEntityMap();
10 Object.assign(this, init);
11 }
12 static Get(clMemory) {
13 if (!BotMemory._instance) {
14 BotMemory._instance = new BotMemory();
15 }
16 BotMemory._instance.clMemory = clMemory;
17 return BotMemory._instance;
18 }
19 FilledEntityMap() {
20 return tslib_1.__awaiter(this, void 0, void 0, function* () {
21 yield this.Init();
22 return this.filledEntityMap;
23 });
24 }
25 Init() {
26 return tslib_1.__awaiter(this, void 0, void 0, function* () {
27 if (!this.clMemory) {
28 throw new Error('BotMemory called without initializing memory');
29 }
30 let data = yield this.clMemory.GetAsync(BotMemory.MEMKEY);
31 if (data) {
32 this.Deserialize(data);
33 }
34 else {
35 this.ClearAsync();
36 }
37 });
38 }
39 Serialize() {
40 return JSON.stringify(this.filledEntityMap.map);
41 }
42 Deserialize(text) {
43 if (!text) {
44 return;
45 }
46 let json = JSON.parse(text);
47 this.filledEntityMap.map = json ? json : {};
48 }
49 Set() {
50 return tslib_1.__awaiter(this, void 0, void 0, function* () {
51 if (!this.clMemory) {
52 throw new Error('BotMemory called without initializing memory');
53 }
54 yield this.clMemory.SetAsync(BotMemory.MEMKEY, this.Serialize());
55 });
56 }
57 RestoreFromMapAsync(filledEntityMap) {
58 return tslib_1.__awaiter(this, void 0, void 0, function* () {
59 this.filledEntityMap.map = filledEntityMap.map;
60 yield this.Set();
61 });
62 }
63 RestoreFromMemoryManagerAsync(memoryManager) {
64 return tslib_1.__awaiter(this, void 0, void 0, function* () {
65 // Disable memory manager. Use has been completed
66 memoryManager.Expire();
67 this.filledEntityMap.map = memoryManager.curMemories.map;
68 yield this.Set();
69 });
70 }
71 // Clear memory values not in saveList
72 ClearAsync(saveList) {
73 return tslib_1.__awaiter(this, void 0, void 0, function* () {
74 if (!saveList) {
75 this.filledEntityMap = new models_1.FilledEntityMap();
76 }
77 else {
78 for (let key of Object.keys(this.filledEntityMap.map)) {
79 if (saveList.indexOf(key) < 0) {
80 delete this.filledEntityMap.map[key];
81 }
82 }
83 }
84 yield this.Set();
85 });
86 }
87 // Remember value for an entity
88 RememberEntity(entityName, entityId, entityValue, isBucket = false, builtinType = null, resolution = null) {
89 return tslib_1.__awaiter(this, void 0, void 0, function* () {
90 yield this.Init();
91 this.filledEntityMap.Remember(entityName, entityId, entityValue, isBucket, builtinType, resolution);
92 yield this.Set();
93 });
94 }
95 // Remember multiple values for an entity
96 RememberMany(entityName, entityId, entityValues, isBucket = false, builtinType = null, resolution = null) {
97 return tslib_1.__awaiter(this, void 0, void 0, function* () {
98 yield this.Init();
99 this.filledEntityMap.RememberMany(entityName, entityId, entityValues, isBucket, builtinType, resolution);
100 yield this.Set();
101 });
102 }
103 /** Return array of entity names for which I've remembered something */
104 RememberedNames() {
105 return tslib_1.__awaiter(this, void 0, void 0, function* () {
106 yield this.Init();
107 return Object.keys(this.filledEntityMap);
108 });
109 }
110 /** Return array of entity Ids for which I've remembered something */
111 FilledEntitiesAsync() {
112 return tslib_1.__awaiter(this, void 0, void 0, function* () {
113 yield this.Init();
114 return this.filledEntityMap.FilledEntities();
115 });
116 }
117 /** Given negative entity name, return positive version */
118 PositiveName(negativeName) {
119 if (negativeName.startsWith(NEGATIVE_PREFIX)) {
120 return negativeName.slice(1);
121 }
122 return null;
123 }
124 /** Forget a predicted Entity */
125 ForgetEntity(entityName, entityValue, isMultiValue) {
126 return tslib_1.__awaiter(this, void 0, void 0, function* () {
127 let posName = this.PositiveName(entityName);
128 if (posName) {
129 yield this.Forget(posName, entityValue, isMultiValue);
130 }
131 });
132 }
133 /** Forget an entity value */
134 Forget(entityName, entityValue = null, isBucket = false) {
135 return tslib_1.__awaiter(this, void 0, void 0, function* () {
136 try {
137 // Check if entity buckets values
138 yield this.Init();
139 this.filledEntityMap.Forget(entityName, entityValue, isBucket);
140 yield this.Set();
141 }
142 catch (error) {
143 CLDebug_1.CLDebug.Error(error);
144 }
145 });
146 }
147 DumpMemory() {
148 return tslib_1.__awaiter(this, void 0, void 0, function* () {
149 yield this.Init();
150 return this.filledEntityMap.ToMemory();
151 });
152 }
153 Value(entityName) {
154 return tslib_1.__awaiter(this, void 0, void 0, function* () {
155 yield this.Init();
156 return this.filledEntityMap.ValueAsString(entityName);
157 });
158 }
159 ValueAsList(entityName) {
160 return tslib_1.__awaiter(this, void 0, void 0, function* () {
161 yield this.Init();
162 return this.filledEntityMap.ValueAsList(entityName);
163 });
164 }
165 ValueAsPrebuilt(entityName) {
166 return tslib_1.__awaiter(this, void 0, void 0, function* () {
167 yield this.Init();
168 return this.MemoryValues(entityName);
169 });
170 }
171 MemoryValues(entityName) {
172 if (!this.filledEntityMap.map[entityName]) {
173 return [];
174 }
175 return this.filledEntityMap.map[entityName].values;
176 }
177}
178BotMemory.MEMKEY = 'BOTMEMORY';
179exports.BotMemory = BotMemory;
180//# sourceMappingURL=BotMemory.js.map
\No newline at end of file