1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const CLDebug_1 = require("../CLDebug");
|
5 | const models_1 = require("@conversationlearner/models");
|
6 | const NEGATIVE_PREFIX = '~';
|
7 | class 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 |
|
66 | memoryManager.Expire();
|
67 | this.filledEntityMap.map = memoryManager.curMemories.map;
|
68 | yield this.Set();
|
69 | });
|
70 | }
|
71 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
118 | PositiveName(negativeName) {
|
119 | if (negativeName.startsWith(NEGATIVE_PREFIX)) {
|
120 | return negativeName.slice(1);
|
121 | }
|
122 | return null;
|
123 | }
|
124 |
|
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 |
|
134 | Forget(entityName, entityValue = null, isBucket = false) {
|
135 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
136 | try {
|
137 |
|
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 | }
|
178 | BotMemory.MEMKEY = 'BOTMEMORY';
|
179 | exports.BotMemory = BotMemory;
|
180 |
|
\ | No newline at end of file |