@indra.ai/deva 1.1.35 → 1.1.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -11,15 +11,15 @@ class Deva {
11
11
  this._config = opts.config || {}; // local Config Object
12
12
  this._agent = opts.agent || false; // Agent profile object
13
13
  this._client = {}; // this will be set on init.
14
- this._state = 'OFFLINE'; // current state of agent.
14
+ this._message = 'offline'; // current state of agent.
15
15
  this._active = false; // the active/birth date.
16
16
  this._security = false; // inherited Security features.
17
17
  this._support = false; // inherited Support features.
18
18
  this._services = false; // inherited Service features.
19
- this._assistant = false; // inherited @Assistant features.
20
- this._business = false; // inherited @Business features.
21
- this._development = false; // inherited @Business features.
22
- this._legal = false; // inherited @Legal features.
19
+ this._assistant = false; // inherited Assistant features.
20
+ this._business = false; // inherited Business features.
21
+ this._development = false; // inherited Business features.
22
+ this._legal = false; // inherited Legal features.
23
23
  this.events = opts.events || new EventEmitter({}); // Event Bus
24
24
  this.lib = opts.lib || {}; // used for loading library functions
25
25
  this.devas = opts.devas || {}; // Devas which are loaded
@@ -46,202 +46,223 @@ class Deva {
46
46
  "_agent"
47
47
  ];
48
48
 
49
+ this._state = 'offline'; // current state of agent.
50
+ this._states = {
51
+ ask: 'ask',
52
+ question: 'question',
53
+
54
+ offline: 'offline',
55
+ online: 'online',
56
+
57
+ init: 'init',
58
+ start: 'start',
59
+ enter: 'enter',
60
+ stop: 'stop',
61
+ exit: 'exit',
62
+ error: 'error',
63
+ load: 'load',
64
+ unload: 'unload',
65
+ uid: 'uid',
66
+ hash: 'hash',
67
+ cipher: 'cipher',
68
+ decipher: 'deciper',
69
+
70
+ invalid: 'invalid',
71
+ done: 'done',
72
+ error: 'error',
73
+ }; // states object
74
+
75
+ this._zone = false; // current state of agent.
76
+ this._zones = {
77
+ deva: 'deva',
78
+ config: 'config',
79
+ features: 'features',
80
+ idle: 'idle',
81
+ train: 'training',
82
+ work: 'working',
83
+ invalid: 'invalid',
84
+ done: 'done',
85
+ error: 'error',
86
+ }; // states object
87
+
88
+ this._action = false;
89
+ this._actions = {
90
+ wait: 'wait',
91
+ question: 'question',
92
+ question_ask: 'question:ask',
93
+ question_ask_answer: 'question:ask:answer',
94
+ question_cmd: 'question:cmd',
95
+ question_method: 'question:method',
96
+ question_talk: 'quesiton:talk',
97
+ question_hash: 'question:hash',
98
+ question_answer: 'question:answer',
99
+ question_done: 'quesiton:done',
100
+ answer: 'answer',
101
+ answer_talk: 'answer:talk',
102
+ ask: 'ask',
103
+ ask_answer: 'ask:answer',
104
+ security: '#security',
105
+ Security: '@SECURITY',
106
+ support: '#support',
107
+ Support: '@SUPPORT',
108
+ systems: '#systems',
109
+ Systems: '@SYSTEMS',
110
+ services: '#services',
111
+ Services: '@SERVICES',
112
+ solutions: '#solutions',
113
+ Solutions: '@SOLUTIONS',
114
+ development: '#development',
115
+ Development: '@DEVELOPMENT',
116
+ business: '#business',
117
+ Business: '@Business',
118
+ legal: '#legal',
119
+ Legal: '@LEGAL',
120
+ assistant: '#assistant',
121
+ Assistant: '@ASSISTANT',
122
+ story: '#story',
123
+ Story: '@STORY',
124
+ mind: '#mind',
125
+ Mind: '@MIND',
126
+ client_data: 'client:data',
127
+ invalid: 'invalid',
128
+ error: 'error',
129
+ done: 'done',
130
+ }
131
+
132
+ this._feature = false;
49
133
  this._features = {
50
- assistant: {
51
- label: '👤ASSISTANT',
52
- name: '@ASSISTANT',
53
- tag: '#ASSISTANT',
54
- loc: '$ASSISTANT',
55
- },
56
- business: {
57
- label: '💼BUSINESS',
58
- name: '@BUSINESS',
59
- tag: '#BUSINESS',
60
- loc: '$BUSINESS',
61
- },
62
- legal: {
63
- label: '👨‍⚖️LEGAL',
64
- name: '@LEGAL',
65
- tag: '#LEGAL',
66
- loc:' $LEGAL',
67
- },
68
- development: {
69
- label: '👨‍💻DEVELOPMENT',
70
- name: '@DEVELOPMENT',
71
- tag: '#DEVELOPMENT',
72
- loc: '$DEVELOPMENT',
73
- },
74
- security: {
75
- label: '🚨SECURITY',
76
- name: '@SECURITY',
77
- tag: '#SECURITY',
78
- loc: '$SECURITY',
134
+ security: 'security',
135
+ Security: '@SECURITY',
136
+ support: 'support',
137
+ Support: '@SUPPORT',
138
+ services: 'services',
139
+ Services: '@SERVICES',
140
+ solutions: 'solutions',
141
+ Solutions: '@SOLUTIONS',
142
+ systems: 'systems',
143
+ Systems: '@SYSTEMS',
144
+ development: 'development',
145
+ Development: '@DEVELOPMENT',
146
+ business: 'business',
147
+ Business: '@BUSINESS',
148
+ legal:'legal',
149
+ Legal:'@LEGAL',
150
+ assistant: 'assistant',
151
+ Assistant: '@ASSISTANT',
152
+ story: 'story',
153
+ Story: '@STORY',
154
+ mind: 'mind',
155
+ Mind: '@MIND',
156
+ error: 'error',
157
+ done: 'done',
158
+ };
159
+
160
+ this._messages = {
161
+ states: {
162
+ offline: `${this._agent.profile.name} ofline`,
163
+ online: `${this._agent.profile.name} online`,
164
+ ask: `:${this._state.ask}`,
165
+ question: `${this._states.quesiton}`,
166
+ offline: `${this._states.offline}`,
167
+ online: `${this._states.online}`,
168
+ init: `${this._agent.profile.name} initialize`,
169
+ start: `${this._agent.profile.name} start`,
170
+ enter: `${this._agent.profile.name} enter`,
171
+ stop: `${this._agent.profile.name} stop`,
172
+ exit: `${this._agent.profile.name} exit`,
173
+ load: `${this._agent.profile.name} load`,
174
+ unload: `${this._agent.profile.name} unload`,
175
+ uid: `${this._states.uid}`,
176
+ hash: `${this._states.hash}`,
177
+ cipher: `${this._states.cipher}`,
178
+ decipher: `${this._states.decipher}`,
179
+ invalid: `${this._states.invalid}`,
180
+ done: `${this._agent.profile.name} is Done`,
181
+ error: `${this._states.error}`,
79
182
  },
80
- support: {
81
- label: '🆘SUPPORT',
82
- name: '@SUPPORT',
83
- tag: '#SUPPORT',
84
- loc: '$SUPPORT',
183
+ zones: {
184
+ deva: 'Deva',
185
+ config: 'Configuration',
186
+ features: 'Features',
187
+ idle: 'Idle',
188
+ train: `${this._zones.train}`,
189
+ work: `${this._zones.work}`,
190
+ invalid: `${this._zones.invalid}`,
191
+ done: `${this._zones.done}`,
192
+ error: `${this._zones.error}`,
85
193
  },
86
- services: {
87
- label: '📞SERVICES',
88
- name: '@SERVICES',
89
- tag: '#SERVICES',
90
- loc: '$SERVICES',
194
+ actions: {
195
+ wait: `${this._agent.profile.name} ${this._actions.wait}`,
196
+ question: `${this._agent.profile.name} ${this._actions.question}`,
197
+ question_ask: `${this._agent.profile.name} ${this._actions.question_ask}`,
198
+ question_ask_answer: `${this._agent.profile.name} ${this._actions.questoin_ask_answer}`,
199
+ question_cmd: `${this._agent.profile.name} ${this._actions.question_cmd}`,
200
+ question_method: `${this._agent.profile.name} ${this._actions.question_method}`,
201
+ question_talk: `${this._agent.profile.name} ${this._actions.question_talk}`,
202
+ question_hash: `${this._agent.profile.name} ${this._actions.question_hash}`,
203
+ question_answer: `${this._agent.profile.name} ${this._actions.question_answer}`,
204
+ question_done: `${this._agent.profile.name} ${this._actions.question_done}`,
205
+ answer: `${this._agent.profile.name} ${this._actions.answer}`,
206
+ answer_talk: `${this._agent.profile.name} ${this._actions.answer_talk}`,
207
+ ask: `${this._agent.profile.name} ${this._actions.ask}`,
208
+ ask_answer: `${this._agent.profile.name} ${this._actions.ask_answer}`,
209
+ security: `${this._actions.security} is responding`,
210
+ Security: `${this._actions.Security} is guarding`,
211
+ support: `${this._actions.support} is responding`,
212
+ Support: `${this._actions.Support} is caring`,
213
+ services: `${this._actions.Services} is responding`,
214
+ Services: `${this._actions.Services} is servicing`,
215
+ solutions: `${this._actions.solutions} is responding`,
216
+ Solutions: `${this._actions.Solutions} is providing`,
217
+ solutions: `${this._actions.Systems} is responding`,
218
+ Systems: `${this._actions.Systems} is delivering`,
219
+ development: `${this._actions.Development} is responding`,
220
+ Development: `${this._actions.Development} is building`,
221
+ business: `${this._actions.Business} is responding`,
222
+ Business: `${this._actions.Business} is successful`,
223
+ legal: `${this._actions.Legal} is responding`,
224
+ Legal: `${this._actions.Legal} is doing their job`,
225
+ assistant: `${this._actions.Assistant} is responding`,
226
+ Assistant: `${this._actions.Assistant} is helping`,
227
+ story: `${this._actions.Story} is responding`,
228
+ Story: `${this._actions.Story} is inspiring`,
229
+ mind: `${this._actions.Mind} is responding`,
230
+ Mind: `${this._actions.Mind} is thinking and pondering`,
231
+ client_data: `Client Data`,
232
+ invalid: `${this._actions.invalid}`,
233
+ done: `${this._actions.done}`,
234
+ error: `${this._action.error}`,
91
235
  },
92
- solutions: {
93
- label: '💡SOLUTIONS',
94
- name: '@SOLUTIONS',
95
- tag: '#SOLUTIONS',
96
- loc: '$SOLUTIONS',
236
+ features: {
237
+ security: `${this._features.security} is protecting`,
238
+ Security: `${this._features.Security} is configuring`,
239
+ support: `${this._features.support} is caring`,
240
+ Support: `${this._features.Support} is configuring`,
241
+ services: `${this._features.services} is servicing`,
242
+ Services: `${this._features.Services} is configuring`,
243
+ solutions: `${this._features.solutions} is solving`,
244
+ Solutions: `${this._features.Solutions} is configuring`,
245
+ systems: `${this._features.systems} is mantaining`,
246
+ Systems: `${this._features.Systems} is configuring`,
247
+ development: `${this._features.development} is developing`,
248
+ Development: `${this._features.Development} is working on ideas`,
249
+ business: `${this._features.business} is successful`,
250
+ Business: `${this._features.Business} is configuring`,
251
+ legal: `${this._features.legal} is upholding the law`,
252
+ Legal: `${this._features.Legal} is configuring`,
253
+ assistant: `${this._features.assistant} is assisting`,
254
+ Assistant: `${this._features.Assistant} is configuring`,
255
+ story: `${this._features.story} is creating`,
256
+ Story: `${this._features.Story} is configuring`,
257
+ mind: `${this._features.story} is thinking and pondering`,
258
+ Mind: `${this._features.Mind} is configuring`,
259
+ invalid: this._features.invalid,
260
+ done: this._features.done,
261
+ error: this._features.error,
97
262
  },
98
- systems: {
99
- label: '🔧SYSTEMS',
100
- name: '@SYSTEMS',
101
- tag: '#SYSTEMS',
102
- loc: '$SYSTEMS',
103
- }
104
- };
105
- this._states = {}; // states object
106
- this._messages = {}; // messages object
263
+ }; // messages object
107
264
  }
108
265
 
109
- /**************
110
- func: States
111
- params: none
112
- describe:
113
- The States function builds the list of states with the associated client and
114
- agent variables in the place holders. This function runs after the Client
115
- function to ensure that hte messages are personalized to the Client and AGent.
116
- ***************/
117
- States() {
118
- const {
119
- development, security, services, support,
120
- systems, solutions, assistant, business, legal
121
- } = this._features;
122
-
123
- const cp = this._client.profile;
124
- const ckey = this._client.key
125
- const ap = this._agent.profile;
126
- const akey = this._agent.key;
127
-
128
- const _states = {
129
- uid: `${security.label}:UID ${cp.name} made a #uid with ${ap.name}`,
130
- hash: `${security.label}:HASH ${cp.name} made a #hash with ${ap.name}`,
131
- cipher: `${security.label}:CIPHER ${cp.name} locked a #cipher with ${ap.name}`,
132
- decipher: `${security.label}:DECIPHER ${cp.name} unlocked a #cipher with ${ap.name}`,
133
- offline: `👻 ${ap.name} is offline`,
134
- online: `📡 ${ap.name} is online`,
135
- config: `‍📀 ${ap.name} is checking the config`,
136
- client: `👨‍💻 ${ap.name} opened the ${ckey} profile`,
137
- agent: `👨‍💻 ${ap.name} is looking at ${akey} profile`,
138
- init: `🚀 ${ap.name} is initializing for ${cp.name}`,
139
- start: `🎬 ${ap.name} has started the process for ${cp.name}`,
140
- enter: `🎪 ${ap.name} is entering the deva.world with ${cp.name}`,
141
- stop: `🛑 ${ap.name} has stopped for ${cp.name}`,
142
- exit: `🚪 ${ap.name} found the exit with ${cp.name}`,
143
- done: `🤝 ${ap.name} is all done time for #offerings 🍫🍌`,
144
- wait: `😵‍💫 ${ap.name} waiting for #stuff from ${cp.name}`,
145
- data: `📀 ${ap.name} is receiving #data for ${cp.name}`,
146
- ask: `🙋‍♀️ ${ap.name} is asking a #question from ${cp.name}`,
147
- cmd: `📟 ${ap.name} entered a #command from ${cp.name}`,
148
- question: `🐵 ${ap.name} is in #question mode ${cp.name}`,
149
- ask: `🐵 ${ap.name} is in #ask mode ${cp.name}`,
150
- talk: `🎙️ ${ap.name} is in #talk mode with ${cp.name}`,
151
- listen: `🎧 ${ap.name} is in #listening mode with ${cp.name}`,
152
- error: `❌ ${ap.name} had an error. Let's have @Systems look into that.`,
153
- story: `📓STORY: ${cp.name} is creating an amazing #story ${cp.name}`,
154
- development: `${development.label}: ${cp.name} and ${ap.name} need ${development.name} assistance`,
155
- security: `${security.label}: ${cp.name} and ${ap.name} need ${security.name} assistance`,
156
- support: `${support.label}: ${cp.name} and ${ap.name} need ${support.name} assistance`,
157
- services: `${services.label}: ${cp.name} and ${ap.name} need ${services.name} assistance`,
158
- systems: `${systems.label}: ${cp.name} and ${ap.name} need ${systems.name} assistance`,
159
- solutions: `${development.label}: ${cp.name} and ${ap.name} need ${development.name} assistance`,
160
- legal: `${legal.label}: ${cp.name} and ${ap.name} need ${legal.name} assistance`,
161
- business: `${business.label}: ${cp.name} and ${ap.name} need ${business.name} assistance`,
162
- devas_start: `🧞‍♂️DEVAS: Starting all the #Devas with ${cp.name}`,
163
- devas_ready: `🧞‍♂️DEVA:READY The #Devas are #ready and #waiitng for ${cp.name} using @${ckey} #${ckey} $${ckey}`,
164
- devas_stop: `🧞‍♂️DEVA:STOPPING The #Devas are #stopping with ${cp.name}`,
165
- devas_stopped: `🧞‍♂️DEVA:🛑STOP #Devas and ${cp.name} have #stopped, and that means time for #offerings 🍎🍑🍍🧋`,
166
- deva_load: `🧞‍♂️DEVA:LOADING ${ap.name} loading for ${cp.name}`,
167
- deva_loaded: `🧞‍♂️DEVA:LOADED ${ap.name} loaded for ${cp.name}`,
168
- deva_unloaded: `🧞‍♂️DEVAS:UNLOADED ${ap.name} unloaded for ${cp.name}`,
169
- question_me: `❓QUESTION:START ${cp.name} started with a great #question to ${ap.name}`,
170
- question_default: `🧞‍♂️QUESTION:ME ${cp.name} asked a great #question to ${ap.name}`,
171
- question_ask: `🧞QUESTION:ASK ${ap.name} is pondering what ${cp.name} asked`,
172
- question_asking: `🧞QUESTION:ASK:ANSWER ${ap.name} is asking another #Deva for ${cp.name}`,
173
- question_aswering: `🧞QUESTION:ASK:ANSWERING ${ap.name} is answering the #question ${cp.name} asked`,
174
- question_answer: `🔮QUESTION:ANSWER ${cp.name} received an #ansewr from ${ap.name}`,
175
- question_command: `🧞‍♀️QUESTION:CMD ${cp.name} issued a #command to ${ap.name}`,
176
- hash_question: `${security.label}:HASH:QUESTION ${ap.name} created the #question #hash for ${cp.name}`,
177
- hash_ask: `${security.label}:HASH:ASK ${ap.name} created the #ask #hash for ${cp.name}`,
178
- hash_answer: `${security.label}:HASH:ANSWER ${cp.name} #answer #hash with ${ap.name}`,
179
- hash_command: `${security.label}:HASH:COMMAND ${cp.name} used a #command with ${ap.name}`,
180
- hash_packet: `${security.label}:HASH:PACKET ${ap.name} created the #packet #hash for ${cp.name}`,
181
- ask_question: `❓QUESTION: ${cp.name} asked ${ap.name} a great #question`,
182
- ask_answer: `💡ANSWER: ${cp.name} received a great #answer from ${ap.name}`,
183
- method_not_found: `${security.label}:ERROR:COMMAND ${cp.name} used a #command while working with ${ap.name}, and may need from ${security.name}`,
184
-
185
- ready_security: `${security.label}:READY - ${security.name} is ready on ${security.tag} in ${security.loc}`,
186
- ready_support: `${support.label}:READY - ${support.name} is ready on ${support.tag} in ${support.loc}`,
187
- ready_services: `${services.label}:READY - ${services.name} is ready on ${services.tag} in ${services.loc}`,
188
- ready_systems: `${systems.label}:READY - ${systems.name} is ready on ${systems.tag} in ${systems.loc}`,
189
- ready_solutions: `${solutions.label}:READY - ${solutions.name} is ready on ${solutions.tag} in ${solutions.loc}`,
190
- ready_development: `${development.label}:READY - ${development.name} is ready ${development.tag} in ${development.loc}`,
191
- ready_legal: `${legal.label}:READY - ${legal.name} is ready on ${legal.tag} in ${legal.loc}`,
192
- ready_business: `${business.label}:READY - ${business.name} is ready ${business.tag} in ${business.loc}`,
193
- ready_assistant: `${assistant.label}:READY - ${assistant.name} is ready ${assistant.tag} in ${business.loc}`,
194
-
195
- alert_security: `${security.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
196
- alert_support: `${support.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
197
- alert_services: `${services.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
198
- alert_solutions: `${solutions.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
199
- alert_systems: `${systems.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
200
- alert_development: `${development.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
201
- alert_assistant: `${assistant.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
202
- alert_legal: `${legal.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
203
- alert_business: `${business.label}:ALERT There is an #issue with ${cp.name} and ${ap.name}.`,
204
-
205
- setting_client: `👨CLIENT: ${ap.name} is setting #${ckey} for ${cp.name} `,
206
- setting_security: `${security.label}: ${cp.name} and ${ap.name} are receiving ${security.name} on ${security.tag} in ${security.loc}`,
207
- setting_development: `${development.label}: ${cp.name} and ${ap.name} are receiving ${development.name} on ${development.tag} in ${development.loc}`,
208
- setting_support: `${support.label}: ${cp.name} and ${ap.name} are receiving ${support.name} on ${development.tag} in ${development.loc}`,
209
- setting_services: `${services.label}: ${cp.name} and ${ap.name} are receiving ${services.name} on ${services.tag} in ${services.loc}`,
210
- setting_systems: `${systems.label}: ${cp.name} and ${ap.name} are receiving ${systems.name} on ${systems.tag} in ${systems.loc}`,
211
- setting_solutions: `${solutions.label}: ${cp.name} and ${ap.name} are receiving ${solutions.name} on ${solutions.tag} in ${solutions.loc}`,
212
- setting_assistant: `${assistant.label}: ${cp.name} and ${ap.name} are receiving ${assistant.name} on ${assistant.tag} in ${assistant.loc}`,
213
- setting_legal: `${legal.label}: ${cp.name} and ${ap.name} are receiving ${legal.name} on ${legal.tag} in ${legal.loc}`,
214
- setting_business: `${business.label}: ${cp.name} and ${ap.name} are receiving ${business.name} on ${business.tag} in ${business.loc}`,
215
- setting_done: `✅DONE: FEATURE SETTINGS COMPLETE`,
216
- }
217
- this._states = _states; // The available states to work with.
218
- }
219
-
220
- /**************
221
- func: Messages
222
- params: none
223
- describe:
224
- The Messages function builds the system messages, and allows for the
225
- reloading of system messages when client or agent data changes.
226
- ***************/
227
- Messages() {
228
- // Default system messages
229
- const _messages = {
230
- offline: `🙅‍♂️ ${this._agent.profile.name} offline`,
231
- init: `⚠️ ${this._agent.profile.name} init`,
232
- start: `✅ ${this._agent.profile.name} start`,
233
- stop: `💥 ${this._agent.profile.name} stop.`,
234
- enter: `🖖 ${this._agent.profile.name} enter.`,
235
- exit: `🚪 ${this._agent.profile.name} exit.`,
236
- done: `👍 ${this._agent.profile.name} done.`,
237
- devas_started: '🍪DEVAS: #online and ready for #offerings 🍫🥛🍚🍯🧂',
238
- devas_stopped: '🛑DEVAS: have stopped',
239
- notext: `❌ ${this._client.profile.name}, Invalid input.`,
240
- deval_already_loaded: `That #DEVA is already loaded.`,
241
- method_not_found: `❌ ${this._client.profile.name} you sure messed that up!`,
242
- }
243
- this._messages = this.copy(_messages); // set a copy of _messages as this._messages;
244
- }
245
266
 
246
267
  /**************
247
268
  func: Client
@@ -253,21 +274,11 @@ class Deva {
253
274
  usage:
254
275
  this.Client = {data}
255
276
  ***************/
256
- set Client(client) {
277
+ Client(client) {
278
+ this.action('client_data');
257
279
  const _client = this.copy(client); // copy the client parameter
258
280
  this._client = _client; // set local _client to this scope
259
-
260
- this.States() // set the States after the Client
261
- this.Messages() // set the Messages after the Client
262
- // this.Support();
263
- // this.Services();
264
- // this.Systems();
265
- // this.Solutions();
266
- // this.Development();
267
- // this.Assistant();
268
- // this.Business();
269
- // this.Legal();
270
- // if (_client.featuers) delete _client.features; // delete features key for client security
281
+ return Promise.resolve();
271
282
  }
272
283
 
273
284
  /**************
@@ -278,10 +289,11 @@ class Deva {
278
289
  client presented data.
279
290
  ***************/
280
291
  Security() {
292
+ this.feature('Security');
281
293
  try {
282
294
  if (!this._client.features.security) return this.Support();
283
295
  else {
284
- this.state('setting_security');
296
+ this.action('Security');
285
297
  const {id, profile, features} = this._client; // make a copy the clinet data.
286
298
  const {security} = features; // make a copy the clinet data.
287
299
  this._security = { // set this_security with data
@@ -298,6 +310,8 @@ class Deva {
298
310
  return this.Support();
299
311
  }
300
312
  } catch (e) {
313
+ this.action('error');
314
+ this.feature('error');
301
315
  return this.error(e) // run error handling if an error is caught
302
316
  }
303
317
  }
@@ -310,10 +324,11 @@ class Deva {
310
324
  client presented data.
311
325
  ***************/
312
326
  Support() {
327
+ this.feature('Support'); // set state to support setting
313
328
  try {
314
329
  if (!this._client.features.support) return this.Services()
315
330
  else {
316
- this.state('setting_support'); // set state to support setting
331
+ this.action('Support');
317
332
  const {id, features, profile} = this._client; // set the local consts from client copy
318
333
  const {support} = features; // set support from features const
319
334
  this._support = { // set this_support with data
@@ -328,6 +343,8 @@ class Deva {
328
343
  return this.Services();
329
344
  }
330
345
  } catch (e) {
346
+ this.action('error');
347
+ this.feature('error');
331
348
  return this.error(e) // run error handling if an error is caught
332
349
  }
333
350
  }
@@ -340,10 +357,11 @@ class Deva {
340
357
  client presented data.
341
358
  ***************/
342
359
  Services() {
360
+ this.feature('Services'); // set state to security setting
343
361
  try {
344
362
  if (!this._client.features.services) return this.Systems();
345
363
  else {
346
- this.state('setting_services'); // set state to security setting
364
+ this.action('Services')
347
365
  const {id, features, profile} = this._client; // set the local consts from client copy
348
366
  const {services} = features; // set services from features const
349
367
  this._services = { // set this_services with data
@@ -358,6 +376,8 @@ class Deva {
358
376
  return this.Systems()
359
377
  }
360
378
  } catch (e) {
379
+ this.action('error');
380
+ this.feature('error');
361
381
  return this.error(e) // run error handling if an error is caught
362
382
  }
363
383
  }
@@ -370,10 +390,11 @@ class Deva {
370
390
  client presented data.
371
391
  ***************/
372
392
  Systems() {
393
+ this.feature('Systems'); // set state to systems setting
373
394
  try {
374
395
  if (!this._client.features.systems) return this.Solutions();
375
396
  else {
376
- this.state('setting_systems'); // set state to systems setting
397
+ this.action('Systems');
377
398
  const {id, features, profile} = this._client; // set the local consts from client copy
378
399
  const {systems} = features; // set systems from features const
379
400
  this._systems = { // set this_systems with data
@@ -389,6 +410,8 @@ class Deva {
389
410
  }
390
411
  } catch (e) {
391
412
  // run error handling if an error is caught
413
+ this.action('error');
414
+ this.feature('error');
392
415
  return this.error(e)
393
416
  }
394
417
  }
@@ -401,10 +424,11 @@ class Deva {
401
424
  client presented data.
402
425
  ***************/
403
426
  Solutions() {
427
+ this.feature('Solutions'); // set state to solutions setting
404
428
  try {
405
429
  if (!this._client.features.solutions) return this.Development();
406
430
  else {
407
- this.state('setting_solutions'); // set state to solutions setting
431
+ this.action('Solutions');
408
432
  const {id, features, profile} = this._client; // set the local consts from client copy
409
433
  const {solutions} = features; // set solutions from features const
410
434
  this._solutions = { // set this_solutions with data
@@ -419,6 +443,8 @@ class Deva {
419
443
  return this.Development()
420
444
  }
421
445
  } catch (e) {
446
+ this.action('error');
447
+ this.feature('error');
422
448
  return this.error(e) // run error handling if an error is caught
423
449
  }
424
450
  }
@@ -431,18 +457,17 @@ class Deva {
431
457
  client presented data.
432
458
  ***************/
433
459
  Development() {
460
+ this.feature('Development'); // set state to development setting
434
461
  try {
435
462
  if (!this._client.features.development) return this.Business();
436
463
  else {
437
- this.state('setting_development'); // set state to development setting
464
+ this.action('Development');
438
465
  const {id, features, profile} = this._client; // set the local consts from client copy
439
466
  const {development} = features; // set development from features const
440
467
  this._development = { // set this_development with data
441
468
  id: this.uid(true), // uuid of the development feature
442
469
  client_id: id, // client id for reference
443
470
  client_name: profile.name, // client name for personalization
444
- hash: development.hash, // client preferred hash algorithm
445
- cipher: development.cipher, // client preferred cipher settings
446
471
  concerns: development.concerns, // any concerns for client
447
472
  global: development.global, // the global policies for client
448
473
  personal: development.devas[this._agent.key] // Client personal features and rules.
@@ -451,6 +476,8 @@ class Deva {
451
476
  return this.Business()
452
477
  }
453
478
  } catch (e) {
479
+ this.action('error');
480
+ this.feature('error');
454
481
  return this.error(e) // run error handling if an error is caught
455
482
  }
456
483
  }
@@ -463,18 +490,17 @@ class Deva {
463
490
  client presented data.
464
491
  ***************/
465
492
  Business(client=false) {
493
+ this.feature('Business'); // set state to business setting
466
494
  try {
467
495
  if (!this._client.features.business) return this.Legal();
468
496
  else {
469
- this.state('setting_business'); // set state to business setting
497
+ this.action('Business');
470
498
  const {id, features, profile} = this._client; // set the local consts from client copy
471
499
  const {business} = features; // set business from features const
472
500
  this._business = { // set this_business with data
473
501
  id: this.uid(true), // uuid of the business feature
474
502
  client_id: id, // client id for reference
475
503
  client_name: profile.name, // client name for personalization
476
- hash: business.hash, // client preferred hash algorithm
477
- cipher: business.cipher, // client preferred cipher settings
478
504
  concerns: business.concerns, // any concerns for client
479
505
  global: business.global, // the global policies for client
480
506
  personal: business.devas[this._agent.key] // Client personal features and rules.
@@ -483,6 +509,8 @@ class Deva {
483
509
  return this.Legal();
484
510
  }
485
511
  } catch (e) {
512
+ this.action('error');
513
+ this.feature('error');
486
514
  return this.error(e) // run error handling if an error is caught
487
515
  }
488
516
  }
@@ -495,18 +523,17 @@ class Deva {
495
523
  client presented data.
496
524
  ***************/
497
525
  Legal() {
526
+ this.feature('Legal'); // set state to legal setting
498
527
  try {
499
528
  if (!this._client.features.legal) this.Assistant();
500
529
  else {
501
- this.state('setting_legal'); // set state to legal setting
530
+ this.action('Legal');
502
531
  const {id, features, profile} = this._client; // set the local consts from client copy
503
532
  const {legal} = features; // set legal from features const
504
533
  this._legal = { // set this_legal with data
505
534
  id: this.uid(true), // uuid of the legal feature
506
535
  client_id: id, // client id for reference
507
536
  client_name: profile.name, // client name for personalization
508
- hash: legal.hash, // client preferred hash algorithm
509
- cipher: legal.cipher, // client preferred cipher settings
510
537
  concerns: legal.concerns, // any concerns for client
511
538
  global: legal.global, // the global policies for client
512
539
  personal: legal.devas[this._agent.key] // Client personal features and rules.
@@ -527,26 +554,91 @@ class Deva {
527
554
  client presented data.
528
555
  ***************/
529
556
  Assistant(client=false) {
557
+ this.feature('Assistant'); // set state to assistant setting
530
558
  try {
531
559
  if (!this._client.features.assistant) return this.Done();
532
560
  else {
533
- this.state('setting_assistant'); // set state to assistant setting
561
+ this.action('Assistant');
534
562
  const {id, features, profile} = this._client; // set the local consts from client copy
535
563
  const {assistant} = features; // set assistant from features const
536
564
  this._assistant = { // set this_assistant with data
537
565
  id: this.uid(true), // uuid of the assistant feature
538
566
  client_id: id, // client id for reference
539
567
  client_name: profile.name, // client name for personalization
540
- hash: assistant.hash, // client preferred hash algorithm
541
- cipher: assistant.cipher, // client preferred cipher settings
542
568
  concerns: assistant.concerns, // any concerns for client
543
569
  global: assistant.global, // the global policies for client
544
570
  personal: assistant.devas[this._agent.key] // Client personal features and rules.
545
571
  };
546
572
  delete this._client.features.assistant;
573
+ return this.Story();
574
+ }
575
+ } catch (e) {
576
+ return this.error(e) // run error handling if an error is caught
577
+ }
578
+ }
579
+
580
+ /**************
581
+ func: Story
582
+ params: client: false
583
+ describe:
584
+ The Story feature sets the correct variables and necessary rules for the
585
+ client presented data.
586
+ ***************/
587
+ Story(client=false) {
588
+ this.feature('Story'); // set state to story setting
589
+ try {
590
+ if (!this._client.features.story) return this.Mind();
591
+ else {
592
+ this.action('Story');
593
+ const {id, features, profile} = this._client; // set the local consts from client copy
594
+ const {story} = features; // set story from features const
595
+ this._story = { // set this_story with data
596
+ id: this.uid(true), // uuid of the story feature
597
+ client_id: id, // client id for reference
598
+ client_name: profile.name, // client name for personalization
599
+ concerns: story.concerns, // any concerns for client
600
+ global: story.global, // the global policies for client
601
+ personal: story.devas[this._agent.key] // Client personal features and rules.
602
+ };
603
+ delete this._client.features.story;
604
+ return this.Mind();
605
+ }
606
+ } catch (e) {
607
+ this.action('error');
608
+ this.feature('error');
609
+ return this.error(e) // run error handling if an error is caught
610
+ }
611
+ }
612
+
613
+ /**************
614
+ func: Mind
615
+ params: client: false
616
+ describe:
617
+ The Mind feature sets the correct variables and necessary rules for the
618
+ client presented data.
619
+ ***************/
620
+ Mind(client=false) {
621
+ this.feature('Mind'); // set state to story setting
622
+ try {
623
+ if (!this._client.features.mind) return this.Done();
624
+ else {
625
+ this.action('Mind');
626
+ const {id, features, profile} = this._client; // set the local consts from client copy
627
+ const {mind} = features; // set mind from features const
628
+ this._mind = { // set this_mind with data
629
+ id: this.uid(true), // uuid of the mind feature
630
+ client_id: id, // client id for reference
631
+ client_name: profile.name, // client name for personalization
632
+ concerns: mind.concerns, // any concerns for client
633
+ global: mind.global, // the global policies for client
634
+ personal: mind.devas[this._agent.key] // Client personal features and rules.
635
+ };
636
+ delete this._client.features.mind;
547
637
  return this.Done();
548
638
  }
549
639
  } catch (e) {
640
+ this.action('error');
641
+ this.feature('error');
550
642
  return this.error(e) // run error handling if an error is caught
551
643
  }
552
644
  }
@@ -557,13 +649,16 @@ class Deva {
557
649
  describe: The end of the workflow Client Feature Workflow
558
650
  ***************/
559
651
  Done() {
560
- try {
561
- delete this._client.features; // delete the features key when done.
562
- this.state('setting_done'); // set state to assistant setting
563
- return;
564
- } catch (e) {
565
- return this.error(e);
566
- }
652
+ return new Promise((resolve, reject) => {
653
+ try {
654
+ delete this._client.features; // delete the features key when done.
655
+ this.feature('done'); // set state to assistant setting
656
+ return resolve();
657
+ } catch (e) {
658
+ this.feature('error')
659
+ return this.error(e, false, reject);
660
+ }
661
+ });
567
662
  }
568
663
  /**************
569
664
  func: _assignBind
@@ -609,6 +704,7 @@ class Deva {
609
704
  return new Promise((resolve, reject) => {
610
705
  try {
611
706
  // set the default listeners for the states of the agent.
707
+
612
708
  for (let state in this._states) {
613
709
  if (typeof this[state] === 'function') {
614
710
  this.events.on(`${this._agent.key}:${state}`, packet => {
@@ -623,13 +719,11 @@ class Deva {
623
719
  return this.listeners[listener](packet);
624
720
  })
625
721
  }
722
+ return resolve();
626
723
  }
627
724
  catch (e) {
628
725
  return this.error(e, false, reject);
629
726
  }
630
- finally {
631
- return resolve();
632
- }
633
727
  });
634
728
  }
635
729
 
@@ -650,13 +744,11 @@ class Deva {
650
744
  this.devas[d][inherit] = this[inherit];
651
745
  });
652
746
  }
747
+ return resolve();
653
748
  }
654
749
  catch (e) {
655
750
  return this.error(e, false, reject);
656
751
  }
657
- finally {
658
- return resolve();
659
- }
660
752
  });
661
753
  }
662
754
 
@@ -699,149 +791,6 @@ class Deva {
699
791
  return this._states;
700
792
  }
701
793
 
702
- /**************
703
- func: state
704
- params:
705
- - st: The state flag to set for the Deva that matches to this._states
706
- describe
707
- ***************/
708
- state(st, data=false) {
709
- if (!Object.keys(this._states).includes(st)) return;
710
- this._state = `${this._states[st]} | ${this.formatDate(Date.now(), 'short_month', true)}`;
711
- const _data = {
712
- id: this.uid(true),
713
- client: this._client.id,
714
- agent: this._agent.id,
715
- st: st,
716
- state: this._state,
717
- data,
718
- created: Date.now(),
719
- };
720
- this.prompt(this._state);
721
- this.talk(`${this._agent.key}:state`, _data);
722
- return this._state;
723
- }
724
-
725
- /**************
726
- func: uid
727
- params:
728
- - guid: This is a true false flag for generating a guid.
729
- describe:
730
- The uid function can create two types of id for you.
731
- 1. random GUID - this is good for when you need a uinique record id returned
732
- 2. transport id - The transport id is a number generated to provide a
733
- numerical number used for transporting records to places
734
- like social networks, email, other networks where informaton
735
- is shared.
736
- ***************/
737
- uid(guid=false) {
738
- let id;
739
- if (guid) {
740
- id = randomUUID()
741
- }
742
- else {
743
- const min = Math.floor(Date.now() - (Date.now() / Math.PI));
744
- const max = Math.floor(Date.now() + (Date.now() * Math.PI));
745
- id = Math.floor(Math.random() * (max - min)) + min;
746
- }
747
- return id;
748
- }
749
-
750
- /**************
751
- func: hash
752
- params:
753
- - texts: The text string to create a hash value for.
754
- - algo: The hashing algorithm to use for hashing. md5, sha256, or sha512
755
-
756
- describe:
757
- The hash algorithm will take a string of text and produce a hash.
758
- ***************/
759
- hash(str, algo=false) {
760
- algo = algo || this._security.hash || 'md5';
761
- const the_hash = createHash(algo);
762
- the_hash.update(str);
763
- const _digest = the_hash.digest('base64');
764
- this.state('hash', {
765
- id: this.uid(true),
766
- value: _digest,
767
- client_id: this._client.id,
768
- agent_id: this._agent.id,
769
- created: Date.now(),
770
- });
771
- return _digest;
772
- }
773
-
774
- /**************
775
- func: cipher
776
- params: str - string to encrypt
777
- describe:
778
- The encrypt function allows for the internal encryption of data based on the
779
- defined client security settings.
780
- ***************/
781
- cipher(str) {
782
- const security = this._security;
783
- const {password, algorithm} = security.cipher;
784
- const key = createHash('sha256').update(String(password)).digest('base64');
785
- const key_in_bytes = Buffer.from(key, 'base64')
786
- const iv = randomBytes(16);
787
-
788
- // create a new cipher
789
- const _cipher = createCipheriv(algorithm, key_in_bytes, iv);
790
- const encrypted = _cipher.update(String(str), 'utf8', 'hex') + _cipher.final('hex');
791
-
792
- this.state('cipher', {
793
- id: this.uid(true),
794
- iv,
795
- key,
796
- agent_id: this._agent.id,
797
- client_id: this._client.id,
798
- created: Date.now()
799
- });
800
-
801
- return {
802
- iv: iv.toString('base64'),
803
- key,
804
- encrypted,
805
- }
806
- }
807
- decipher(opt) {
808
- const iv = Buffer.from(opt.iv, 'base64');
809
- const encrypted = Buffer.from(opt.encrypted, 'hex');
810
- const key_in_bytes = Buffer.from(opt.key, 'base64')
811
- const security = this._security;
812
- const {algorithm} = security.cipher;
813
- const decipher = createDecipheriv( algorithm, key_in_bytes, iv);
814
- const decrypted = decipher.update(encrypted);
815
- const final = Buffer.concat([decrypted, decipher.final()]);
816
- this.state('decipher', {
817
- id: this.uid(true),
818
- iv: opt.iv,
819
- key: opt.key,
820
- agent_id: this._agent.id,
821
- client_id: this._client.id,
822
- created: Date.now()
823
- });
824
- return final.toString();
825
- }
826
-
827
- /**************
828
- func: copy
829
- params: obj
830
- describe:
831
- a simple copy object to create a memory clean copy of data to
832
- prevent collisions when needed. Handles clean text, array, object copy.
833
- it makes the assumption tha the user is submitting either an array or object
834
- for copying.
835
- ***************/
836
- copy(obj) {
837
- let v, key;
838
- const output = Array.isArray(obj) ? [] : {};
839
- for (key in obj) {
840
- v = obj[key];
841
- output[key] = (typeof v === "object") ? this.copy(v) : v;
842
- }
843
- return output;
844
- }
845
794
  /**************
846
795
  func: talk
847
796
  params:
@@ -864,7 +813,10 @@ class Deva {
864
813
  describe:
865
814
  ***************/
866
815
  listen(evt, callback) {
867
- return this.events.on(evt, callback);
816
+ this.listeners[evt] = callback;
817
+ return this.events.on(evt, packet => {
818
+ return this.listeners[evt](packet);
819
+ });
868
820
  }
869
821
 
870
822
  /**************
@@ -907,8 +859,8 @@ class Deva {
907
859
  so the event is specific to the talk.
908
860
  ***************/
909
861
  ask(packet) {
910
- if (!this._active) return Promise.resolve(this._messages.offline);
911
- this.state('ask_question', packet);
862
+ if (!this._active) return Promise.resolve(this._messages.states.offline);
863
+ this.action('question', packet);
912
864
 
913
865
  packet.a = {
914
866
  agent: this._agent || false,
@@ -927,6 +879,7 @@ class Deva {
927
879
  try {
928
880
  if (this.methods[packet.q.meta.method] !== 'function') {
929
881
  return setImmediate(() => {
882
+ this.action('ask_invalid')
930
883
  packet.a.text = `INVALID METHOD (${packet.q.meta.method})`;
931
884
  this.talk(`${this._agent.key}:ask:${packet.id}`, packet);
932
885
  });
@@ -943,21 +896,74 @@ class Deva {
943
896
  else {
944
897
  packet.a.text = result;
945
898
  }
946
-
947
- this.state('ask_answer', packet);
899
+ this.action('ask_answer', packet);
948
900
  this.talk(`${this._agent.key}:ask:${packet.id}`, packet);
949
901
  }).catch(err => {
902
+ this.action('error', err);
950
903
  this.talk(`${this._agent.key}:ask:${packet.id}`, {error:err.toString()});
951
904
  return this.error(err, packet);
952
905
  })
953
906
  }
954
907
  catch (e) {
908
+ this.action('error', err);
955
909
  this.talk(`${this._agent.key}:ask:${packet.id}`, {error:e.toString()});
956
910
  return this.error(e, packet)
957
911
  }
958
912
  // now when we ask the meta params[0] should be the method
959
913
  }
960
914
 
915
+ answer(packet, resolve, reject) {
916
+ this.action('answer', packet); // set the question answer state
917
+ // check if method exists and is of type function
918
+ const {method,params} = packet.q.meta;
919
+ const isMethod = this.methods[method] && typeof this.methods[method] == 'function';
920
+ if (!isMethod) {
921
+ this.action('invalid')
922
+ return resolve(this._methodNotFound(packet)); // resolve method not found if check if check fails
923
+ }
924
+ // Call the local method to process the question based the extracted parameters
925
+ return this.methods[method](packet).then(result => {
926
+ // check the result for the text, html, and data object.
927
+ // this is for when answers are returned from nested Devas.
928
+ const text = typeof result === 'object' ? result.text : result;
929
+ const html = typeof result === 'object' ? result.html : result;
930
+ // if the data passed is NOT an object it will FALSE
931
+ const data = typeof result === 'object' ? result.data : false;
932
+
933
+
934
+ packet.a = { // setup the packet.a container
935
+ agent: this._agent || false, // set the agent who answered the question
936
+ client: this._client || false, // set the client asking the question
937
+ meta: { // setup the answer meta container
938
+ key: this._agent.key, // set the agent key inot the meta
939
+ method, // set the method into the meta
940
+ params, // set the params into the meta
941
+ },
942
+ text, // set answer text
943
+ html,
944
+ data,
945
+ created: Date.now(),
946
+ };
947
+
948
+ // create a hash for the answer and insert into answer meta.
949
+ this.action('hash_answer');
950
+ packet.a.meta.hash = this.hash(JSON.stringify(packet.a));
951
+ // create a hash for entire packet and insert into packet
952
+ // hash the entire packet.
953
+ this.action('hash_packet');
954
+ packet.hash = this.hash(JSON.stringify(packet));
955
+
956
+ this.action('answer_talk');
957
+ this.talk(`${this._agent.key}:answer`, packet); // set the question answer state
958
+
959
+ this.action('done');
960
+ return resolve(packet); // resolve the packet to the caller.
961
+ }).catch(err => { // catch any errors in the method
962
+ this.action('error', err);
963
+ return this.error(err, packet, reject); // return this.error with err, packet, reject
964
+ });
965
+ }
966
+
961
967
  /**************
962
968
  func: question
963
969
  example: this.question('#*agent.key *method* *text*')
@@ -969,10 +975,10 @@ class Deva {
969
975
  ***************/
970
976
  question(TEXT=false, DATA=false) {
971
977
  // check the active status
972
- if (!this._active) return Promise.resolve(this._messages.offline);
973
- this.state('question_me', {text:TEXT, data:DATA}); // set the question me state text TEXT & DATA passed in.
978
+ if (!this._active) return Promise.resolve(this._messages.states.offline);
974
979
  const id = this.uid(); // generate a unique id for transport.
975
980
  const t_split = TEXT.split(' '); // split the text on spaces to get words.
981
+ this.action('question', id);
976
982
 
977
983
  // check to see if the string is an #ask string to talk to the other Deva.
978
984
  const isAsk = t_split[0].startsWith(this.askChr);
@@ -999,14 +1005,10 @@ class Deva {
999
1005
  // resolve with the no text message if the client says nothing.
1000
1006
  if (!TEXT) return resolve(this._messages.notext);
1001
1007
  // reject question if Deva offline
1002
- if (!this._active) return resolve(this._messages.offline);
1003
-
1008
+ if (!this._active) return resolve(this._messages.states.offline);
1009
+ let _action = 'question_method'
1004
1010
  try { // try to answer the question
1005
- let _state = 'question_default'; // set temporary question state variable
1006
- let _hash = 'hash_asnwer'; // set emporary hash state variable
1007
1011
  if (isAsk) { // determine if hte question isAsk
1008
- _state = 'question_ask' // if:isAk then set question state
1009
- _state = 'hash_ask' // if:isAsk then set hash state
1010
1012
  key = t_split[0].substring(1); // if:isAsksplit the agent key and remove first command character
1011
1013
  //if:isAsk use text split index 1 as the parameter block
1012
1014
  params = t_split[1] ? t_split[1].split(':') : false;
@@ -1014,13 +1016,15 @@ class Deva {
1014
1016
  text = t_split.slice(2).join(' ').trim(); // then rejoin the text with spaces.
1015
1017
  }
1016
1018
  else if (isCmd) { // determine if the question is a command
1017
- _state = 'question_command'; // if:isCmd set question state
1018
- _hash = 'hash_command' // if:isCmd set hash state
1019
+ _action = 'question_cmd';
1019
1020
  //if:isCmd use text split index 1 as the parameter block
1020
- params = t_split[1] ? t_split[1].split(':') : false;
1021
+ params = t_split[0] ? t_split[0].split(':').slice(1) : false;
1021
1022
  method = t_split[0].substring(1); // if:isCmd use the 0 index as the command
1022
1023
  text = t_split.slice(1).join(' ').trim(); // if:isCmd rejoin the string on the space after removing first index
1023
1024
  }
1025
+ else {
1026
+ this.action('question_method');
1027
+ }
1024
1028
 
1025
1029
  packet.q = { // build packet.q container
1026
1030
  agent: this._agent || false, // set the agent
@@ -1037,64 +1041,26 @@ class Deva {
1037
1041
  }
1038
1042
 
1039
1043
  // hash the question
1044
+ this.action('question_hash'); // set the has question state
1040
1045
  packet.q.meta.hash = this.hash(JSON.stringify(packet.q));
1041
- this.state('hash_question'); // set the has question state
1042
1046
 
1043
- this.state(_state, packet); // set the state to teh _state variable
1044
1047
 
1045
1048
  if (isAsk) { // isAsk check if the question isAsk and talk
1046
- this.state('question_asking'); // if:isAsk set state to question asking
1049
+ this.action('question_ask');
1047
1050
  this.talk(`${key}:ask`, packet); // if:isAsk talk the event to theother Deva
1048
1051
  // if: isAsk wait for the once event which is key'd to the packet ID for specified responses
1049
1052
  this.once(`${key}:ask:${packet.id}`, answer => {
1053
+ this.action('question_ask_answer');
1050
1054
  return resolve(answer); // if:isAsk resolve the answer from the call
1051
1055
  });
1052
1056
  }
1053
-
1054
1057
  else { // else: answer tue question locally
1055
- this.state('question_answering');
1056
- // check if method exists and is of type function
1057
- if (this.methods[method] && typeof this.methods[method] !== 'function') {
1058
- return resolve(this._methodNotFound(packet)); // resolve method not found if check if check fails
1059
- }
1060
- // Call the local method to process the question based the extracted parameters
1061
- return this.methods[method](packet).then(result => {
1062
- // check the result for the text, html, and data object.
1063
- // this is for when answers are returned from nested Devas.
1064
- const text = typeof result === 'object' ? result.text : result;
1065
- const html = typeof result === 'object' ? result.html : result;
1066
- // if the data passed is NOT an object it will FALSE
1067
- const data = typeof result === 'object' ? result.data : false;
1068
- packet.a = { // setup the packet.a container
1069
- agent: this._agent || false, // set the agent who answered the question
1070
- client: this._client || false, // set the client asking the question
1071
- meta: { // setup the answer meta container
1072
- key: this._agent.key, // set the agent key inot the meta
1073
- method, // set the method into the meta
1074
- params, // set the params into the meta
1075
- },
1076
- text, // set answer text
1077
- html,
1078
- data,
1079
- created: Date.now(),
1080
- };
1081
- // create a hash for the answer and insert into answer meta.
1082
- packet.a.meta.hash = this.hash(JSON.stringify(packet.a));
1083
- this.state(_hash);
1084
- // create a hash for entire packet and insert into packet
1085
- // hash the entire packet.
1086
- packet.hash = this.hash(JSON.stringify(packet));
1087
- this.state('hash_packet');
1088
-
1089
- this.state('question_answer', packet); // set the question answer state
1090
- this.talk(`${this._agent.key}:answer`, packet); // set the question answer state
1091
- return resolve(packet); // resolve the packet to the caller.
1092
- }).catch(err => { // catch any errors in the method
1093
- return this.error(err, packet, reject); // return this.error with err, packet, reject
1094
- });
1058
+ this.action(_action);
1059
+ return this.answer(packet, resolve, reject);
1095
1060
  }
1096
1061
  }
1097
1062
  catch(e) { // try block error trap
1063
+ this.action('error');
1098
1064
  return this.error(e); // if a overall error happens this witll call this.error
1099
1065
  }
1100
1066
  });
@@ -1117,7 +1083,6 @@ class Deva {
1117
1083
  ***************/
1118
1084
  init(client) {
1119
1085
  // set client
1120
- this.Client = client;
1121
1086
  this._active = Date.now();
1122
1087
  return new Promise((resolve, reject) => {
1123
1088
  this.events.setMaxListeners(this.maxListeners);
@@ -1127,10 +1092,15 @@ class Deva {
1127
1092
  return this._assignListeners();
1128
1093
  }).then(() => {
1129
1094
  this.state('init');
1130
- this.Security();
1131
- return this.onInit && typeof this.onInit === 'function' ? this.onInit() : this.start();
1132
- }).then(started => {
1133
- return resolve(started)
1095
+ this.zone('config');
1096
+ return this.Client(client);
1097
+ }).then(() => {
1098
+ this.zone('features');
1099
+ this.action('features');
1100
+ return this.Security();
1101
+ }).then(() => {
1102
+ this.zone('deva');
1103
+ return this.start(this._messages.deva_init);
1134
1104
  }).catch(err => {
1135
1105
  return this.error(err, client, reject);
1136
1106
  });
@@ -1148,7 +1118,6 @@ class Deva {
1148
1118
  usage: this.error(err, data, reject);
1149
1119
  ***************/
1150
1120
  error(err,data=false,reject=false) {
1151
- this.state('error', {err, data}); // set the state to error
1152
1121
  // check fo rthe custom onError function in the agent.
1153
1122
  console.log('\n::BEGIN:ERROR\n');
1154
1123
  console.log(err);
@@ -1176,10 +1145,20 @@ class Deva {
1176
1145
  function or running the system enter function.
1177
1146
  usage: this.start('msg')
1178
1147
  ***************/
1179
- start(msg=false) {
1180
- if (!this._active) return Promise.resolve(this._messages.offline);
1148
+ start(text = false) {
1181
1149
  this.state('start');
1182
- return this.onStart && typeof this.onStart === 'function' ? this.onStart() : this.enter(this._messages.start);
1150
+ if (!this._active) return Promise.resolve(this._messages.states.offline);
1151
+ const _data = {
1152
+ id: this.uid(),
1153
+ key: 'start',
1154
+ value: 'start',
1155
+ agent: this._agent,
1156
+ text: this._messages.states.start,
1157
+ created: Date.now(),
1158
+ }
1159
+ _data.hash = this.hash(JSON.stringify(_data));
1160
+ const hasOnExit = this.onExit && typeof this.onExit === 'function';
1161
+ return hasOnExit ? this.onExit(_data) : Promise.resolve(_data)
1183
1162
  }
1184
1163
 
1185
1164
  /**************
@@ -1195,11 +1174,20 @@ class Deva {
1195
1174
  usage:
1196
1175
  this.stop('msg')
1197
1176
  ***************/
1198
- stop(msg=false) {
1199
- if (!this._active) return Promise.resolve(this._messages.offline);
1177
+ stop(text=false) {
1200
1178
  this.state('stop');
1201
- this._active = false;
1202
- return this.onStop && typeof this.onStop === 'function' ? this.onStop() : this.exit(this._messages.stop);
1179
+ if (!this._active) return Promise.resolve(this._messages.states.offline);
1180
+ const _data = {
1181
+ id: this.uid(),
1182
+ key: 'stop',
1183
+ value: 'stop',
1184
+ agent: this._agent,
1185
+ text: this._messages.states.stop,
1186
+ created: Date.now(),
1187
+ }
1188
+ _data.hash = this.hash(JSON.stringify(_data));
1189
+ const hasOnStop = this.onStop && typeof this.onStop === 'function';
1190
+ return hasOnStop ? this.onStop(_data) : this.exit(_data)
1203
1191
  }
1204
1192
 
1205
1193
  /**************
@@ -1213,10 +1201,20 @@ class Deva {
1213
1201
  If the Deva is offline it will return the offline message.
1214
1202
  usage: this.enter('msg')
1215
1203
  ***************/
1216
- enter(msg=false) {
1217
- if (!this._active) return Promise.resolve(this._messages.offline);
1204
+ enter(text=false) {
1218
1205
  this.state('enter');
1219
- return this.onEnter && typeof this.onEnter === 'function' ? this.onEnter() : this.done(this._messages.enter)
1206
+ if (!this._active) return Promise.resolve(this._messages.states.offline);
1207
+ const _data = {
1208
+ id: this.uid(),
1209
+ type: 'enter',
1210
+ value: 'enter',
1211
+ agent: this._agent,
1212
+ text: this._messages.states.enter,
1213
+ created: Date.now(),
1214
+ }
1215
+ _data.hash = this.hash(JSON.stringify(_data));
1216
+ const hasOnEnter = this.onEnter && typeof this.onEnter === 'function';
1217
+ return hasOnEnter ? this.onExit(_data) : Promise.resolve(_data)
1220
1218
  }
1221
1219
 
1222
1220
  /**************
@@ -1233,11 +1231,21 @@ class Deva {
1233
1231
  If the deva is offline it will return the offline message.
1234
1232
  usage: this.exit('msg')
1235
1233
  ***************/
1236
- exit(msg=false) {
1237
- if (!this._active) return Promise.resolve(this._messages.offline);
1234
+ exit(text=false) {
1238
1235
  this.state('exit');
1236
+ if (!this._active) return Promise.resolve(this._messages.states.offline);
1239
1237
  this._active = false;
1240
- return this.onExit && typeof this.onExit === 'function' ? this.onExit() : Promise.resolve(this._messages.exit)
1238
+ const _data = {
1239
+ id: this.uid(),
1240
+ key: 'exit',
1241
+ value: 'exit',
1242
+ agent: this._agent,
1243
+ text: this._messages.states.exit,
1244
+ created: Date.now(),
1245
+ }
1246
+ _data.hash = this.hash(JSON.stringify(_data));
1247
+ const hasOnExit = this.onExit && typeof this.onExit === 'function';
1248
+ return hasOnExit ? this.onExit(_data) : Promise.resolve(_data)
1241
1249
  }
1242
1250
 
1243
1251
  /**************
@@ -1251,49 +1259,136 @@ class Deva {
1251
1259
  If the deva is offline it will return the offline message.
1252
1260
  usage: this.done('msg')
1253
1261
  ***************/
1254
- done(msg=false) {
1255
- if (!this._active) return Promise.resolve(this._messages.offline);
1262
+ done(text=false) {
1256
1263
  this.state('done');
1257
- msg = msg ? msg : this._state;
1258
- return this.onDone && typeof this.onDone === 'function' ? this.onDone() : Promise.resolve({text:this._messages.done,prompt:this._agent.prompt})
1264
+ if (!this._active) return Promise.resolve(this._messages.states.offline);
1265
+ this._active = false;
1266
+ const _data = {
1267
+ id: this.uid(),
1268
+ key: 'done',
1269
+ value: 'done',
1270
+ agent: this._agent,
1271
+ text: this._messages.states.exit,
1272
+ created: Date.now(),
1273
+ }
1274
+ _data.hash = this.hash(JSON.stringify(_data));
1275
+ const hasOnExit = this.onExit && typeof this.onExit === 'function';
1276
+ return hasOnExit ? this.onExit(_data) : Promise.resolve(_data)
1259
1277
  }
1260
1278
 
1279
+ ////////////////////////////
1280
+
1261
1281
  /**************
1262
- func: status
1282
+ func: state
1263
1283
  params:
1264
- - msg: The msg is any additonal string to append to the end of hte call.
1265
- describe:
1266
- The status function provides an easy way to get the current status of a Deva
1267
- and append custom status messages that may pertain to any custom status call.
1284
+ - st: The state flag to set for the Deva that matches to this._states
1285
+ describe
1286
+ ***************/
1287
+ state(state, data=false) {
1288
+ try {
1289
+ if (!this._states[state]) return;
1290
+ this._state = state;
1291
+ const text = this._messages.states[state];
1292
+ const _data = {
1293
+ id: this.uid(),
1294
+ key: 'state',
1295
+ value: state,
1296
+ agent: this._agent,
1297
+ text,
1298
+ data,
1299
+ created: Date.now(),
1300
+ };
1301
+ _data.hash = this.hash(JSON.stringify(_data));
1302
+ this.talk('state', _data);
1303
+ } catch (e) {
1304
+ return this.error(e);
1305
+ }
1306
+ }
1268
1307
 
1269
- If the deva is offline it will return the offline message.
1270
- usage: this.status('msg')
1308
+ /**************
1309
+ func: zone
1310
+ params:
1311
+ - st: The zone flag to set for the Deva that matches to this._zones
1312
+ describe
1271
1313
  ***************/
1272
- status(msg=false) {
1273
- // check the active status
1274
- if (!this._active) return Promise.resolve(this._messages.offline);
1275
- // format the date since active for output.
1276
- const dateFormat = this.formatDate(this._active, 'long', true);
1277
- // create the text msg string
1278
- let text = `${this._features.systems.label}:STATUS ${this._agent.profile.name} active since ${dateFormat}`;
1279
- if (msg) text = text + `\n${msg}`; // append the msg string if msg true.
1280
- return text; // return final text string
1314
+ zone(zone, data=false) {
1315
+ try {
1316
+ if (!this._zones[zone]) return;
1317
+ this._zone = zone;
1318
+ const text = this._messages.zones[zone];
1319
+ const _data = {
1320
+ id: this.uid(),
1321
+ key: 'zone',
1322
+ value: zone,
1323
+ agent: this._agent,
1324
+ text,
1325
+ data,
1326
+ created: Date.now(),
1327
+ };
1328
+ _data.hash = this.hash(JSON.stringify(_data));
1329
+ this.talk('zone', _data);
1330
+ } catch (e) {
1331
+ return this.error(e);
1332
+ }
1281
1333
  }
1282
1334
 
1283
1335
  /**************
1284
- func: prompt
1336
+ func: action
1285
1337
  params:
1286
- - text: The text string to send to the prompt.
1287
- describe:-
1288
- The prompt function is used to broadcasat a global prompt event with a string. Thsi is handy when passing events between a cli and user interface for example.
1338
+ - st: The state flag to set for the Deva that matches to this._states
1339
+ describe
1340
+ ***************/
1341
+ action(action, data=false) {
1342
+ try {
1343
+ if (!this._actions[action]) return;
1344
+ this._action = action;
1345
+ const text = this._messages.actions[action];
1346
+ const _data = {
1347
+ id: this.uid(),
1348
+ key: 'action',
1349
+ value: action,
1350
+ agent: this._agent,
1351
+ text,
1352
+ data,
1353
+ created: Date.now(),
1354
+ };
1355
+ _data.hash = this.hash(JSON.stringify(_data));
1356
+ this.talk('action', _data);
1357
+ } catch (e) {
1358
+ return this.error(e)
1359
+ }
1360
+ }
1289
1361
 
1290
- usage: this.prompt('text')
1362
+ /**************
1363
+ func: feature
1364
+ params:
1365
+ - st: The state flag to set for the Deva that matches to this._states
1366
+ describe
1291
1367
  ***************/
1292
- prompt(text) {
1293
- // Talk a global prompt event for the client
1294
- return this.talk(`prompt`, {text, agent:this._agent});
1368
+ feature(feature, data=false) {
1369
+ try {
1370
+ if (!this._features[feature]) return;
1371
+ this._feature = feature;
1372
+ const text = this._messages.features[feature] ;
1373
+ const _data = {
1374
+ id: this.uid(),
1375
+ key: 'feature',
1376
+ value: feature,
1377
+ agent: this._agent,
1378
+ text,
1379
+ data,
1380
+ created: Date.now(),
1381
+ };
1382
+ _data.hash = this.hash(JSON.stringify(_data));
1383
+ this.talk('feature', _data);
1384
+ } catch (e) {
1385
+ return this.error(e);
1386
+ }
1295
1387
  }
1296
1388
 
1389
+
1390
+ ///////////////////////////
1391
+
1297
1392
  /**************
1298
1393
  func: client
1299
1394
  params: none
@@ -1303,8 +1398,8 @@ class Deva {
1303
1398
  usage: this.client();
1304
1399
  ***************/
1305
1400
  client() {
1306
- if (!this._active) return this._messages.offline; // check the active status
1307
- this.state('client'); // set the client state
1401
+ this.state('client_data'); // set the client state
1402
+ if (!this._active) return this._messages.states.offline; // check the active status
1308
1403
  return this._client; // return the client feature
1309
1404
  }
1310
1405
 
@@ -1317,11 +1412,12 @@ class Deva {
1317
1412
  usage: this.agent()
1318
1413
  ***************/
1319
1414
  agent() {
1320
- if (!this._active) return this._messages.offline;
1321
- this.state('agent');
1415
+ this.state('agent_data');
1416
+ if (!this._active) return this._messages.states.offline;
1322
1417
  return this._agent;
1323
1418
  }
1324
1419
 
1420
+ // FEATURE FUNCTIONS
1325
1421
  /**************
1326
1422
  func: security
1327
1423
  params: none
@@ -1329,9 +1425,16 @@ class Deva {
1329
1425
  usage: this.security()
1330
1426
  ***************/
1331
1427
  security() {
1332
- if (!this._active) return this._messages.offline; // check the active status
1333
- this.state('security'); // set the security state
1334
- return this._security; // return the security feature
1428
+ this.feature('security'); // set the security state
1429
+ try {
1430
+ if (!this._active) return this._messages.states.offline; // check the active status
1431
+ this.action('security'); // set the security state
1432
+ return this._security; // return the security feature
1433
+ } catch (e) {
1434
+ this.action('error'); // set the security state
1435
+ this.feature('error');
1436
+ return this.error(e);
1437
+ }
1335
1438
  }
1336
1439
 
1337
1440
  /**************
@@ -1341,9 +1444,16 @@ class Deva {
1341
1444
  usage: this.support()
1342
1445
  ***************/
1343
1446
  support() {
1344
- if (!this._active) return this._messages.offline; // check the active status
1345
- this.state('support'); // set the support state
1346
- return this._support; // return the support feature
1447
+ this.feature('support'); // set the support state
1448
+ try {
1449
+ if (!this._active) return this._messages.states.offline; // check the active status
1450
+ this.action('support'); // set the support state
1451
+ return this._support; // return the support feature
1452
+ } catch (e) {
1453
+ this.action('error'); // set the services state
1454
+ this.feature('error');
1455
+ return this.error(e);
1456
+ }
1347
1457
  }
1348
1458
 
1349
1459
  /**************
@@ -1353,9 +1463,16 @@ class Deva {
1353
1463
  usage: this.services()
1354
1464
  ***************/
1355
1465
  services(opts) {
1356
- if (!this._active) return this._messages.offline; // check the active status
1357
- this.state('services'); // set the services state
1358
- return this._services; // return the services feature
1466
+ this.feature('services'); // set the services state
1467
+ try {
1468
+ if (!this._active) return this._messages.states.offline; // check the active status
1469
+ this.action('services'); // set the services state
1470
+ return this._services; // return the services feature
1471
+ } catch (e) {
1472
+ this.action('error'); // set the services state
1473
+ this.feature('error');
1474
+ return this.error(e);
1475
+ }
1359
1476
  }
1360
1477
 
1361
1478
  /**************
@@ -1365,9 +1482,16 @@ class Deva {
1365
1482
  usage: this.systems()
1366
1483
  ***************/
1367
1484
  systems(opts) {
1368
- if (!this._active) return this._messages.offline; // check the active status
1369
- this.state('systems'); // set the systems state
1370
- return this._systems; // return the systems feature
1485
+ this.feature('systems'); // set the systems state
1486
+ try {
1487
+ if (!this._active) return this._messages.states.offline; // check the active status
1488
+ this.action('systems'); // set the systems state
1489
+ return this._systems; // return the systems feature
1490
+ } catch (e) {
1491
+ this.action('error'); // set the systems state
1492
+ this.feature('error');
1493
+ return this.error(e)
1494
+ }
1371
1495
  }
1372
1496
 
1373
1497
  /**************
@@ -1377,9 +1501,16 @@ class Deva {
1377
1501
  usage: this.solutions()
1378
1502
  ***************/
1379
1503
  solutions(opts) {
1380
- if (!this._active) return this._messages.offline; // check the active status
1381
- this.state('solutions'); // set the solutions state
1382
- return this._solutions; // return the solutions feature
1504
+ this.feature('solutions'); // set the solutions state
1505
+ try {
1506
+ if (!this._active) return this._messages.states.offline; // check the active status
1507
+ this.action('solutions'); // set the solutions state
1508
+ return this._solutions; // return the solutions feature
1509
+ } catch (e) {
1510
+ this.action('error'); // set the systems state
1511
+ this.feature('error');
1512
+ return this.error(e);
1513
+ }
1383
1514
  }
1384
1515
 
1385
1516
  /**************
@@ -1388,9 +1519,14 @@ class Deva {
1388
1519
  describe: basic development features available in a Deva.
1389
1520
  ***************/
1390
1521
  development(opts) {
1391
- if (!this._active) return this._messages.offline; // chek the active status
1392
- this.state('development'); // set the development state
1393
- return this._development; // return development feature
1522
+ try {
1523
+ if (!this._active) return this._messages.states.offline; // chek the active status
1524
+ this.feature('development'); // set the development state
1525
+ return this._development; // return development feature
1526
+ } catch (e) {
1527
+ this.feature('error');
1528
+ return this.error(e);
1529
+ }
1394
1530
  }
1395
1531
 
1396
1532
  /**************
@@ -1399,9 +1535,14 @@ class Deva {
1399
1535
  describe: basic assistant features available in a Deva.
1400
1536
  ***************/
1401
1537
  assistant(opts) {
1402
- if (!this._active) return this._messages.offline; // chek the active status
1403
- this.state('assistant'); // set the assistant state
1404
- return this._assistant; // return assistant feature
1538
+ try {
1539
+ if (!this._active) return this._messages.states.offline; // chek the active status
1540
+ this.feature('assistant'); // set the assistant state
1541
+ return this._assistant; // return assistant feature
1542
+ } catch (e) {
1543
+ this.feature('error');
1544
+ return this.error(e);
1545
+ }
1405
1546
  }
1406
1547
 
1407
1548
  /**************
@@ -1410,9 +1551,14 @@ class Deva {
1410
1551
  describe: basic business features available in a Deva.
1411
1552
  ***************/
1412
1553
  business(opts) {
1413
- if (!this._active) return this._messages.offline; // chek the active status
1414
- this.state('business'); // set the business state
1415
- return this._business; // return business feature
1554
+ try {
1555
+ if (!this._active) return this._messages.states.offline; // chek the active status
1556
+ this.feature('business'); // set the business state
1557
+ return this._business; // return business feature
1558
+ } catch (e) {
1559
+ this.feature('error');
1560
+ return this.error('error');
1561
+ }
1416
1562
  }
1417
1563
 
1418
1564
  /**************
@@ -1421,9 +1567,25 @@ class Deva {
1421
1567
  describe: basic legal features available in a Deva.
1422
1568
  ***************/
1423
1569
  legal(opts) {
1424
- if (!this._active) return this._messages.offline; // chek the active status
1425
- this.state('legal'); // set the legal state
1426
- return this._legal; // return legal feature
1570
+ try {
1571
+ if (!this._active) return this._messages.states.offline; // chek the active status
1572
+ this.feature('legal'); // set the legal state
1573
+ return this._legal; // return legal feature
1574
+ } catch (e) {
1575
+ this.feature('error');
1576
+ return this.error(e);
1577
+ }
1578
+ }
1579
+
1580
+ /**************
1581
+ func: story
1582
+ params: opts
1583
+ describe: basic story features available in a Deva.
1584
+ ***************/
1585
+ story(opts) {
1586
+ if (!this._active) return this._messages.states.offline; // chek the active status
1587
+ this.feature('story'); // set the story state
1588
+ return this._story; // return story feature
1427
1589
  }
1428
1590
 
1429
1591
  /**************
@@ -1435,21 +1597,19 @@ class Deva {
1435
1597
  ***************/
1436
1598
  load(key, client) {
1437
1599
  return new Promise((resolve, reject) => {
1438
- this.state('deva_load');
1600
+ this.state('load');
1439
1601
  this.devas[key].init(client).then(loaded => {
1440
- this.state('deva_loaded');
1441
1602
  this.talk(`deva:load`, {
1442
1603
  key,
1443
1604
  created: Date.now(),
1444
1605
  });
1445
- return resolve();
1606
+ return resolve(this._messages.states.load);
1446
1607
  }).catch(err => {
1447
1608
  return this.error(err, deva, reject);
1448
1609
  })
1449
1610
  });
1450
1611
  }
1451
1612
 
1452
-
1453
1613
  /**************
1454
1614
  func: unload
1455
1615
  params:
@@ -1457,67 +1617,180 @@ class Deva {
1457
1617
  describe: Unload a currently loaded Deva.
1458
1618
  ***************/
1459
1619
  unload(key) {
1460
- this.state('deva_unload');
1461
- delete this.devas[key];
1462
- this.state('deva_unloaded');
1463
- this.talk(`deva:unload`, {
1464
- key,
1465
- created: Date.now(),
1620
+ return new Promise((resolve, reject) => {
1621
+ try {
1622
+ this.state('uload');
1623
+ delete this.devas[key];
1624
+ this.talk(`deva:unload`, {
1625
+ key,
1626
+ created: Date.now(),
1627
+ });
1628
+ return resolve(this._messages.states.unload);
1629
+ } catch (e) {
1630
+ return reject(e, this.devas[key], reject)
1631
+ }
1466
1632
  });
1467
- return Promise.resolve(`unload:${deva} `);
1468
1633
  }
1469
1634
 
1635
+
1636
+ // UTILITY FUNCTIONS
1470
1637
  /**************
1471
- func: startDevas
1472
- params: none
1638
+ func: uid
1639
+ params:
1640
+ - guid: This is a true false flag for generating a guid.
1473
1641
  describe:
1474
- Start Devas will initialize the Deva agents inside this curent Deva.
1642
+ The uid function can create two types of id for you.
1643
+ 1. random GUID - this is good for when you need a uinique record id returned
1644
+ 2. transport id - The transport id is a number generated to provide a
1645
+ numerical number used for transporting records to places
1646
+ like social networks, email, other networks where informaton
1647
+ is shared.
1475
1648
  ***************/
1476
- startDevas(client) {
1477
- this.state('devas_start'); // set the devas start state
1478
- return new Promise((resolve, reject) => {
1479
- const loading = [];
1480
- const _client = this.copy(client);
1481
- for (let x in this.devas) {
1482
- loading.push(this.load(x, client));
1483
- }
1484
- Promise.all(loading).then(() => {
1485
- this.state('devas_ready'); // set to ready state
1486
- return resolve({ // return the response
1487
- text:this._messages.devas_started, // include started state message
1488
- prompt:this._agent.prompt, // include agent prompt settings
1489
- });
1490
- }).catch(err => { // catch any errors
1491
- return this.error(err, client, reject); // send to error handler.
1492
- });
1649
+ uid(guid=false) {
1650
+ let id;
1651
+ if (guid) {
1652
+ id = randomUUID()
1653
+ }
1654
+ else {
1655
+ const min = Math.floor(Date.now() - (Date.now() / Math.PI));
1656
+ const max = Math.floor(Date.now() + (Date.now() * Math.PI));
1657
+ id = Math.floor(Math.random() * (max - min)) + min;
1658
+ }
1659
+ return id;
1660
+ }
1661
+
1662
+ /**************
1663
+ func: hash
1664
+ params:
1665
+ - texts: The text string to create a hash value for.
1666
+ - algo: The hashing algorithm to use for hashing. md5, sha256, or sha512
1667
+
1668
+ describe:
1669
+ The hash algorithm will take a string of text and produce a hash.
1670
+ ***************/
1671
+ hash(str, algo=false) {
1672
+ algo = algo || this._security.hash || 'md5';
1673
+ const the_hash = createHash(algo);
1674
+ the_hash.update(str);
1675
+ const _digest = the_hash.digest('base64');
1676
+ return _digest;
1677
+ }
1678
+
1679
+ /**************
1680
+ func: cipher
1681
+ params: str - string to encrypt
1682
+ describe:
1683
+ The encrypt function allows for the internal encryption of data based on the
1684
+ defined client security settings.
1685
+ ***************/
1686
+ cipher(str) {
1687
+ const security = this._security;
1688
+ const {password, algorithm} = security.cipher;
1689
+ const key = createHash('sha256').update(String(password)).digest('base64');
1690
+ const key_in_bytes = Buffer.from(key, 'base64')
1691
+ const iv = randomBytes(16);
1692
+
1693
+ // create a new cipher
1694
+ const _cipher = createCipheriv(algorithm, key_in_bytes, iv);
1695
+ const encrypted = _cipher.update(String(str), 'utf8', 'hex') + _cipher.final('hex');
1696
+
1697
+ this.state('cipher', {
1698
+ id: this.uid(true),
1699
+ iv,
1700
+ key,
1701
+ agent_id: this._agent.id,
1702
+ client_id: this._client.id,
1703
+ created: Date.now()
1704
+ });
1705
+
1706
+ return {
1707
+ iv: iv.toString('base64'),
1708
+ key,
1709
+ encrypted,
1710
+ }
1711
+ }
1712
+ decipher(opt) {
1713
+ const iv = Buffer.from(opt.iv, 'base64');
1714
+ const encrypted = Buffer.from(opt.encrypted, 'hex');
1715
+ const key_in_bytes = Buffer.from(opt.key, 'base64')
1716
+ const security = this._security;
1717
+ const {algorithm} = security.cipher;
1718
+ const decipher = createDecipheriv( algorithm, key_in_bytes, iv);
1719
+ const decrypted = decipher.update(encrypted);
1720
+ const final = Buffer.concat([decrypted, decipher.final()]);
1721
+ this.state('decipher', {
1722
+ id: this.uid(true),
1723
+ iv: opt.iv,
1724
+ key: opt.key,
1725
+ agent_id: this._agent.id,
1726
+ client_id: this._client.id,
1727
+ created: Date.now()
1493
1728
  });
1729
+ return final.toString();
1494
1730
  }
1731
+
1495
1732
  /**************
1496
- func: stpDevas
1497
- params: none
1733
+ func: status
1734
+ params:
1735
+ - msg: The msg is any additonal string to append to the end of hte call.
1498
1736
  describe:
1499
- stopDevas will stop all the devas running in the current Deva.
1737
+ The status function provides an easy way to get the current status of a Deva
1738
+ and append custom status messages that may pertain to any custom status call.
1739
+
1740
+ If the deva is offline it will return the offline message.
1741
+ usage: this.status('msg')
1500
1742
  ***************/
1501
- stopDevas() {
1502
- this.state('devas_stop'); // set the devas stop state
1503
- return new Promise((resolve, reject) => {
1504
- const devas = []; // create empty devas index
1505
- for (let x in this.devas) {
1506
- devas.push(this.devas[x].stop()); // push the deva to devas index
1507
- }
1508
- Promise.all(devas).then(() => {
1509
- this.state('devas_stoped'); // set the deva stopped state
1510
- return resolve({
1511
- text: this._messages.devas_stopped, // include stopped state message
1512
- prompt: this._agent.prompt, // include agent prompt settings
1513
- });
1514
- }).catch(err => { // catch any errors
1515
- return this.error(err, false, reject); // send to error handler
1516
- });
1743
+ status(msg=false) {
1744
+ // check the active status
1745
+ if (!this._active) return Promise.resolve(this._messages.states.offline);
1746
+ // format the date since active for output.
1747
+ const dateFormat = this.formatDate(this._active, 'long', true);
1748
+ // create the text msg string
1749
+ let text = `${this._agent.profile.name} active since ${dateFormat}`;
1750
+ if (msg) text = text + `\n${msg}`; // append the msg string if msg true.
1751
+ return Promise.resolve(text); // return final text string
1752
+ }
1753
+
1754
+ /**************
1755
+ func: prompt
1756
+ params:
1757
+ - text: The text string to send to the prompt.
1758
+ describe:-
1759
+ The prompt function is used to broadcasat a global prompt event with a string. Thsi is handy when passing events between a cli and user interface for example.
1760
+
1761
+ usage: this.prompt('text')
1762
+ ***************/
1763
+ prompt(text) {
1764
+ // console.log('PROMPT', text);
1765
+ // Talk a global prompt event for the client
1766
+ return this.talk('prompt', {
1767
+ id: this.uid(),
1768
+ text,
1769
+ agent:this._agent,
1770
+ created: Date.Now(),
1517
1771
  });
1518
1772
  }
1519
1773
 
1520
- // UTILITY FUNCTIONS
1774
+
1775
+ /**************
1776
+ func: copy
1777
+ params: obj
1778
+ describe:
1779
+ a simple copy object to create a memory clean copy of data to
1780
+ prevent collisions when needed. Handles clean text, array, object copy.
1781
+ it makes the assumption tha the user is submitting either an array or object
1782
+ for copying.
1783
+ ***************/
1784
+ copy(obj) {
1785
+ let v, key;
1786
+ const output = Array.isArray(obj) ? [] : {};
1787
+ for (key in obj) {
1788
+ v = obj[key];
1789
+ output[key] = (typeof v === "object") ? this.copy(v) : v;
1790
+ }
1791
+ return output;
1792
+ }
1793
+
1521
1794
  /**************
1522
1795
  func: formatDate
1523
1796
  params: