@indra.ai/deva 1.1.22 → 1.1.23
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/examples/client.json +59 -0
- package/examples/hello-world.js +16 -8
- package/index.js +260 -99
- package/package.json +1 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "CLIENT",
|
|
3
|
+
"describe": "CLIENT FILE",
|
|
4
|
+
"copyright": "Copyright ©2023 Quinn Michaels. All rights reserved.",
|
|
5
|
+
"DATA": {
|
|
6
|
+
"id": 3123155399059,
|
|
7
|
+
"key": "test",
|
|
8
|
+
"name": "Test User",
|
|
9
|
+
"eamil": "test@example.com",
|
|
10
|
+
"describe": "The Test User",
|
|
11
|
+
"prompt": {
|
|
12
|
+
"emoji": "🧪",
|
|
13
|
+
"text": "test",
|
|
14
|
+
"colors": {
|
|
15
|
+
"label": {
|
|
16
|
+
"R": 2,
|
|
17
|
+
"G": 229,
|
|
18
|
+
"B": 77
|
|
19
|
+
},
|
|
20
|
+
"text": {
|
|
21
|
+
"R": 2,
|
|
22
|
+
"G": 229,
|
|
23
|
+
"B": 77
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"profile": {
|
|
28
|
+
"emoji": "https://quinnmichaels.com/assets/img/quinn/avatar.png",
|
|
29
|
+
"avatar": "https://quinnmichaels.com/assets/img/quinn/avatar.png",
|
|
30
|
+
"background": "https://quinnmichaels.com/assets/img/quinn/background.png",
|
|
31
|
+
"name": "Test User",
|
|
32
|
+
"describe": "This is the Test user for the Deva Examples in the Repository.",
|
|
33
|
+
"gender": "splendid",
|
|
34
|
+
"lang": "en"
|
|
35
|
+
},
|
|
36
|
+
"states": {},
|
|
37
|
+
"messages": {},
|
|
38
|
+
"services": {
|
|
39
|
+
"concerns": [],
|
|
40
|
+
"global": {
|
|
41
|
+
"indra": "https://indra.ai",
|
|
42
|
+
"world": "https://deva.world",
|
|
43
|
+
"space": "https://deva.space",
|
|
44
|
+
"cloud": "https://deva.cloud"
|
|
45
|
+
},
|
|
46
|
+
"devas": {}
|
|
47
|
+
},
|
|
48
|
+
"security": {
|
|
49
|
+
"concerns": [],
|
|
50
|
+
"global": {},
|
|
51
|
+
"devas": {}
|
|
52
|
+
},
|
|
53
|
+
"support": {
|
|
54
|
+
"concerns": [],
|
|
55
|
+
"global": {},
|
|
56
|
+
"devas": {}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
package/examples/hello-world.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// Copyright (c)2023 Quinn Michaels
|
|
2
2
|
// Distributed under the MIT software license, see the accompanying
|
|
3
3
|
// file LICENSE.md or http://www.opensource.org/licenses/mit-license.php.
|
|
4
|
+
const client = require('./client.json');
|
|
5
|
+
|
|
4
6
|
const Deva = require('../index');
|
|
5
7
|
const HelloWorld = new Deva({
|
|
6
8
|
client: {
|
|
@@ -39,35 +41,41 @@ const HelloWorld = new Deva({
|
|
|
39
41
|
},
|
|
40
42
|
listeners: {
|
|
41
43
|
'hello:state'(packet) {
|
|
42
|
-
console.log(packet.state);
|
|
44
|
+
// console.log(packet.state);
|
|
43
45
|
},
|
|
44
|
-
'prompt'(
|
|
45
|
-
console.log(text);
|
|
46
|
+
'prompt'(packet) {
|
|
47
|
+
console.log(packet.text);
|
|
46
48
|
}
|
|
47
49
|
},
|
|
48
50
|
deva: {},
|
|
49
51
|
modules: {},
|
|
50
52
|
func: {
|
|
53
|
+
getclient(packet) {
|
|
54
|
+
return Promise.resolve(this._client);
|
|
55
|
+
},
|
|
51
56
|
state(packet) {
|
|
52
57
|
const ret = `${this._state} ${this.uid(true)} ${this.uid()} ${this.hash(JSON.stringify(packet), 'sha256')}`;
|
|
53
58
|
return Promise.resolve(ret);
|
|
54
59
|
}
|
|
55
60
|
},
|
|
56
61
|
methods: {
|
|
62
|
+
getclient(packet) {
|
|
63
|
+
return this._client;
|
|
64
|
+
},
|
|
57
65
|
state(packet) {
|
|
58
66
|
return this.func.state(packet);
|
|
59
67
|
}
|
|
60
68
|
},
|
|
61
69
|
onError(e, packet) {
|
|
62
|
-
console.log('ERROR\n\n', e
|
|
70
|
+
console.log('ERROR\n\n', e);
|
|
63
71
|
}
|
|
64
72
|
});
|
|
65
73
|
|
|
66
|
-
HelloWorld.init().then(done => {
|
|
67
|
-
|
|
74
|
+
HelloWorld.init(client.DATA).then(done => {
|
|
75
|
+
// console.log(done);
|
|
76
|
+
return HelloWorld.question('/state')
|
|
68
77
|
}).then(answer => {
|
|
69
|
-
|
|
70
|
-
console.log(answer.a.text);
|
|
78
|
+
console.log('THE ANSWER: ', answer.a.text);
|
|
71
79
|
});
|
|
72
80
|
|
|
73
81
|
|
package/index.js
CHANGED
|
@@ -7,16 +7,18 @@ const { createHash, randomUUID } = require('crypto');
|
|
|
7
7
|
class Deva {
|
|
8
8
|
constructor(opts) {
|
|
9
9
|
opts = opts || {};
|
|
10
|
+
|
|
10
11
|
this._id = randomUUID(); // the unique id assigned to the agent at load
|
|
12
|
+
this._config = opts.config || {}; // local Config Object
|
|
13
|
+
this._agent = opts.agent || false; // Agent profile object
|
|
14
|
+
this._client = {}; // this will be set on init.
|
|
11
15
|
this._state = 'OFFLINE'; // current state of agent.
|
|
12
16
|
this._active = false; // the active/birth date.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
17
|
+
this._security = {}; // inherited Security features.
|
|
18
|
+
this._support = {}; // inherited Support features.
|
|
19
|
+
this._services = {}; // inherited Service features.
|
|
16
20
|
this.events = opts.events || new EventEmitter({}); // Event Bus
|
|
17
21
|
this.lib = opts.lib || {}; // used for loading library functions
|
|
18
|
-
this.agent = opts.agent || false; // Agent profile object
|
|
19
|
-
this.client = opts.client || false; // Client profile object
|
|
20
22
|
this.devas = opts.devas || {}; // Devas which are loaded
|
|
21
23
|
this.vars = opts.vars || {}; // Variables object
|
|
22
24
|
this.listeners = opts.listeners || {}; // local Listeners
|
|
@@ -33,64 +35,141 @@ class Deva {
|
|
|
33
35
|
this.askChr = '#';
|
|
34
36
|
this.inherit = ["events", "config", "lib", "security", "client"];
|
|
35
37
|
this.bind = ["listeners", "methods", "func", "lib", "security", "agent", "client"];
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
set State(opt) {
|
|
42
|
+
this.state(opt);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
set States(opts) {
|
|
36
46
|
this._states = { // The available states to work with.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
47
|
+
uid: `👻 ${this._agent.name} is making a new #uid`,
|
|
48
|
+
offline: `👻 ${this._agent.name} is offline`,
|
|
49
|
+
online: `📡 ${this._agent.name} is online`,
|
|
50
|
+
config: `📀 ${this._agent.name} is checking the config`,
|
|
51
|
+
client: `👨💻 ${this._agent.name} opened the ${this._client.key} profile`,
|
|
52
|
+
agent: `👨💻 ${this._agent.name} is looking at ${this._agent.key} profile`,
|
|
53
|
+
init: `🚀 ${this._agent.name} is initializing`,
|
|
54
|
+
start: `🎬 ${this._agent.name} has started the process`,
|
|
55
|
+
enter: `🎪 ${this._agent.name} is entering the deva.world`,
|
|
56
|
+
stop: `🛑 ${this._agent.name} has stopped`,
|
|
57
|
+
exit: `🚪 ${this._agent.name} found the exit`,
|
|
58
|
+
done: `🤝 ${this._agent.name} is all done`,
|
|
59
|
+
wait: `😵💫 ${this._agent.name} waiting for something to do`,
|
|
60
|
+
data: `📀 ${this._agent.name} is receiving data`,
|
|
61
|
+
ask: `🙋♀️ ${this._agent.name} is asking a question`,
|
|
62
|
+
cmd: `📟 ${this._agent.name} is using a command`,
|
|
63
|
+
question: `🐵 ${this._agent.name} question`,
|
|
64
|
+
ask: `🐵 ${this._agent.name} asking`,
|
|
65
|
+
talk: `🎙️ ${this._agent.name} is talking`,
|
|
66
|
+
listen: `🎧 ${this._agent.name} is listening`,
|
|
67
|
+
error: `❌ ${this._agent.name} had an error`,
|
|
68
|
+
story: `📓 ${this._agent.name} telling a story`,
|
|
69
|
+
development: `👨💻 ${this._agent.name} needs @Development`,
|
|
70
|
+
security: `🚨 ${this._agent.name} needs @Security`,
|
|
71
|
+
support: `🎗️ ${this._agent.name} needs @Support`,
|
|
72
|
+
services: `🎖️ ${this._agent.name} needs @Services`,
|
|
73
|
+
systems: `👽 ${this._agent.name} needs @Systems`,
|
|
74
|
+
solutions: `🔬 ${this._agent.name} needs @Solutions`,
|
|
75
|
+
devas_start: `✨ Starting all the #Devas...`,
|
|
76
|
+
devas_ready: `📸 The #Devas are ready and waiitng`,
|
|
77
|
+
devas_stop: `🙈 The #Devas are stopping`,
|
|
78
|
+
devas_stopped: `🛑 #Devas have stopped`,
|
|
79
|
+
deva_load: `✅ ${this._agent.name} load`,
|
|
80
|
+
deva_loaded: `✅ ${this._agent.name} loaded`,
|
|
81
|
+
deva_unloaded: `✅ ${this._agent.name} unloaded`,
|
|
82
|
+
question_me: `🐵 ${this._client.name} ask ${this._agent.name} a #question`,
|
|
83
|
+
question_default: `🧞♂️ ${this._client.id} sent ${this._agent.name} a #question`,
|
|
84
|
+
question_ask: `🧞 ${this._agent.name} is pondering what ${this._client.name} asked`,
|
|
85
|
+
question_asking: `🧞 ${this._agent.name} is asking another #Deva what ${this._client.name} asked`,
|
|
86
|
+
question_aswering: `🧞 ${this._agent.name} is answering the #question ${this._client.name} asked`,
|
|
87
|
+
question_answer: `🔮 #${this._agent.name} gave #${this._client.name} the answer`,
|
|
88
|
+
question_cmd: `🧞♀️ ${this._agent.name} then ran a #command for #${this._client.name}`,
|
|
89
|
+
hash_question: `🔐 ${this._agent.name} created the #question hash`,
|
|
90
|
+
hash_answer: `🔐 ${this._agent.name} created the #answer hash`,
|
|
91
|
+
hash_answer: `🔐 ${this._agent.name} created the #packet hash`,
|
|
92
|
+
ask_question: `👽 ${this._client.name} asked ${this._agent.name} a #question`,
|
|
93
|
+
ask_answer: `🛸 ${this._agent.name} answered ${this._client.name}`,
|
|
94
|
+
method_not_found: `😩 ${this._agent.name} used a bad #command, and may need help.`,
|
|
95
|
+
security_ready: `🚓 @Security is ready`,
|
|
96
|
+
support_ready: `🚑 @Support is ready`,
|
|
97
|
+
services_ready: `🚚 @Services is ready`,
|
|
98
|
+
security_alert: `🚨 #SECURITY 🚨 SETTTINGS`,
|
|
99
|
+
support_alert: `🚨 #SUPPORT 🚨 SETTTINGS`,
|
|
100
|
+
services_alert: `🚨 #SERVICES 🚨 SETTTINGS`,
|
|
101
|
+
setting_client: `⛄️ Setting the #client for ${this._agent.name}`,
|
|
102
|
+
setting_security: `👮♂️ ${this._agent.name} given #security`,
|
|
103
|
+
setting_support: `👨⚕️ ${this._agent.name} given #support`,
|
|
104
|
+
setting_services: `👷♂️ ${this._agent.name} given #services`,
|
|
75
105
|
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
set Messages(opts) {
|
|
76
109
|
this._messages = {
|
|
77
|
-
offline:
|
|
78
|
-
init:
|
|
79
|
-
start:
|
|
80
|
-
stop:
|
|
81
|
-
enter:
|
|
82
|
-
exit:
|
|
83
|
-
done:
|
|
84
|
-
devas_started: '🤝
|
|
85
|
-
devas_stopped: '🛑
|
|
86
|
-
notext:
|
|
87
|
-
method_not_found:
|
|
110
|
+
offline: `🙅♂️ ${this._agent.name} offline`,
|
|
111
|
+
init: `⚠️ ${this._agent.name} init`,
|
|
112
|
+
start: `✅ ${this._agent.name} start`,
|
|
113
|
+
stop: `💥 ${this._agent.name} stop.`,
|
|
114
|
+
enter: `🖖 ${this._agent.name} enter.`,
|
|
115
|
+
exit: `🚪 ${this._agent.name} exit.`,
|
|
116
|
+
done: `👍 ${this._agent.name} done.`,
|
|
117
|
+
devas_started: '🤝 Devas have started',
|
|
118
|
+
devas_stopped: '🛑 Devas have stopped',
|
|
119
|
+
notext: `❌ ${this._client.name}, please provide with valid input.`,
|
|
120
|
+
method_not_found: `❌ ${this._client.name} you sure messed that up!`,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
set Client(cl) {
|
|
124
|
+
// delete the services key to move and move to services.
|
|
125
|
+
const _client = this.copy(cl);
|
|
126
|
+
if (_client.states) delete _client.states;
|
|
127
|
+
if (_client.messages) delete _client.messages;
|
|
128
|
+
if (_client.security) delete _client.security;
|
|
129
|
+
if (_client.support) delete _client.support;
|
|
130
|
+
if (_client.services) delete _client.services;
|
|
131
|
+
|
|
132
|
+
this._client = cl;
|
|
133
|
+
|
|
134
|
+
this.States = _client.states;
|
|
135
|
+
this.Messages = _client.messages;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
set Security(opt=false) {
|
|
139
|
+
this.state('setting_security');
|
|
140
|
+
if (!opt) this._security = {};
|
|
141
|
+
else {
|
|
142
|
+
this._security = {
|
|
143
|
+
concerns: opt.concerns,
|
|
144
|
+
global: opt.global,
|
|
145
|
+
things: opt.devas[this._agent.key]
|
|
146
|
+
};
|
|
88
147
|
}
|
|
89
148
|
}
|
|
90
149
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
150
|
+
set Support(opt=false) {
|
|
151
|
+
this.state('setting_support');
|
|
152
|
+
if (!opt) this._support = {};
|
|
153
|
+
else {
|
|
154
|
+
this._support = {
|
|
155
|
+
concerns: opt.concerns,
|
|
156
|
+
global: opt.global,
|
|
157
|
+
things: opt.devas[this._agent.key]
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
set Services(opt=false) {
|
|
163
|
+
this.state('setting_services');
|
|
164
|
+
if (!opt) this._servcies = {};
|
|
165
|
+
else {
|
|
166
|
+
this._services = {
|
|
167
|
+
concerns: opt.concerns,
|
|
168
|
+
global: opt.global,
|
|
169
|
+
things: opt.devas[this._agent.key]
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
94
173
|
|
|
95
174
|
/**************
|
|
96
175
|
func: state
|
|
@@ -103,15 +182,15 @@ class Deva {
|
|
|
103
182
|
this._state = this._states[st];
|
|
104
183
|
const _data = {
|
|
105
184
|
id: this.uid(true),
|
|
106
|
-
client: this.
|
|
107
|
-
agent: this.
|
|
185
|
+
client: this._client.id,
|
|
186
|
+
agent: this._agent.id,
|
|
108
187
|
st: st,
|
|
109
188
|
state: this._state,
|
|
110
189
|
data,
|
|
111
190
|
created: Date.now(),
|
|
112
191
|
};
|
|
113
192
|
this.prompt(this._state);
|
|
114
|
-
this.talk(`${this.
|
|
193
|
+
this.talk(`${this._agent.key}:state`, _data);
|
|
115
194
|
return this._state;
|
|
116
195
|
}
|
|
117
196
|
|
|
@@ -140,11 +219,11 @@ class Deva {
|
|
|
140
219
|
}
|
|
141
220
|
});
|
|
142
221
|
// bind translate
|
|
143
|
-
const translate = this.
|
|
144
|
-
if (translate) this.
|
|
222
|
+
const translate = this._agent && this._agent.translate && typeof this._agent.translate === 'function';
|
|
223
|
+
if (translate) this._agent.translate = this._agent.translate.bind(this);
|
|
145
224
|
// bind parser
|
|
146
|
-
const parse = this.
|
|
147
|
-
if (parse) this.
|
|
225
|
+
const parse = this._agent && this._agent.parse && typeof this._agent.parse === 'function';
|
|
226
|
+
if (parse) this._agent.parse = this._agent.parse.bind(this);
|
|
148
227
|
}
|
|
149
228
|
catch (e) {
|
|
150
229
|
return reject(e);
|
|
@@ -165,15 +244,17 @@ class Deva {
|
|
|
165
244
|
_assignListeners() {
|
|
166
245
|
return new Promise((resolve, reject) => {
|
|
167
246
|
try {
|
|
168
|
-
// set the
|
|
169
|
-
for (let
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
this.events.on(`${this.agent.key}:${state}`, packet => {
|
|
247
|
+
// set the default listeners for the states of the agent.
|
|
248
|
+
for (let state in this._states) {
|
|
249
|
+
if (typeof this[state] === 'function') {
|
|
250
|
+
this.events.on(`${this._agent.key}:${state}`, packet => {
|
|
173
251
|
return this[state](packet);
|
|
174
|
-
})
|
|
252
|
+
});
|
|
175
253
|
}
|
|
254
|
+
}
|
|
176
255
|
|
|
256
|
+
// set the assigned listeners for the agent.
|
|
257
|
+
for (let listener in this.listeners) {
|
|
177
258
|
this.events.on(listener, packet => {
|
|
178
259
|
return this.listeners[listener](packet);
|
|
179
260
|
})
|
|
@@ -232,11 +313,11 @@ class Deva {
|
|
|
232
313
|
***************/
|
|
233
314
|
_methodNotFound(packet) {
|
|
234
315
|
packet.a = {
|
|
235
|
-
agent: this.
|
|
236
|
-
client: this.
|
|
316
|
+
agent: this._agent || false,
|
|
317
|
+
client: this._client || false,
|
|
237
318
|
text: `${this._messages.method_not_found} - ${packet.q.meta.method} `,
|
|
238
319
|
meta: {
|
|
239
|
-
key: this.
|
|
320
|
+
key: this._agent.key,
|
|
240
321
|
method: packet.q.meta.method,
|
|
241
322
|
},
|
|
242
323
|
created: Date.now(),
|
|
@@ -245,11 +326,6 @@ class Deva {
|
|
|
245
326
|
return packet;
|
|
246
327
|
}
|
|
247
328
|
|
|
248
|
-
// A simple interface to generate a unique id for the agent. As agents will
|
|
249
|
-
// quite oftne have to key their transactions. This will provide all agents
|
|
250
|
-
// with the same key generator which can also be modified to link into a remote
|
|
251
|
-
// generator or some other solution if needed.
|
|
252
|
-
|
|
253
329
|
/**************
|
|
254
330
|
func: uid
|
|
255
331
|
params:
|
|
@@ -269,6 +345,25 @@ class Deva {
|
|
|
269
345
|
return Math.floor(Math.random() * (max - min)) + min;
|
|
270
346
|
}
|
|
271
347
|
|
|
348
|
+
|
|
349
|
+
/**************
|
|
350
|
+
func: copy
|
|
351
|
+
params: obj
|
|
352
|
+
describe:
|
|
353
|
+
a simple copy object to create a memory clean copy of data to
|
|
354
|
+
prevent collisions when needed. Handles clean text, array, object copy.
|
|
355
|
+
it makes the assumption tha the user is submitting either an array or object
|
|
356
|
+
for copying.
|
|
357
|
+
***************/
|
|
358
|
+
copy(obj) {
|
|
359
|
+
let v, key;
|
|
360
|
+
const output = Array.isArray(obj) ? [] : {};
|
|
361
|
+
for (key in obj) {
|
|
362
|
+
v = obj[key];
|
|
363
|
+
output[key] = (typeof v === "object") ? this.copy(v) : v;
|
|
364
|
+
}
|
|
365
|
+
return output;
|
|
366
|
+
}
|
|
272
367
|
/**************
|
|
273
368
|
func: talk
|
|
274
369
|
params:
|
|
@@ -370,10 +465,10 @@ class Deva {
|
|
|
370
465
|
this.state('ask_question', packet);
|
|
371
466
|
|
|
372
467
|
packet.a = {
|
|
373
|
-
agent: this.
|
|
374
|
-
client: this.
|
|
468
|
+
agent: this._agent || false,
|
|
469
|
+
client: this._client || false,
|
|
375
470
|
meta: {
|
|
376
|
-
key: this.
|
|
471
|
+
key: this._agent.key,
|
|
377
472
|
method: packet.q.meta.method,
|
|
378
473
|
params: packet.q.meta.params,
|
|
379
474
|
},
|
|
@@ -387,7 +482,7 @@ class Deva {
|
|
|
387
482
|
if (this.methods[packet.q.meta.method] === undefined) {
|
|
388
483
|
return setImmediate(() => {
|
|
389
484
|
packet.a.text = `INVALID METHOD (${packet.q.meta.method})`;
|
|
390
|
-
this.talk(`${this.
|
|
485
|
+
this.talk(`${this._agent.key}:ask:${packet.id}`, packet);
|
|
391
486
|
});
|
|
392
487
|
}
|
|
393
488
|
|
|
@@ -404,14 +499,14 @@ class Deva {
|
|
|
404
499
|
}
|
|
405
500
|
|
|
406
501
|
this.state('ask_answer', packet);
|
|
407
|
-
this.talk(`${this.
|
|
502
|
+
this.talk(`${this._agent.key}:ask:${packet.id}`, packet);
|
|
408
503
|
}).catch(err => {
|
|
409
|
-
this.talk(`${this.
|
|
504
|
+
this.talk(`${this._agent.key}:ask:${packet.id}`, {error:err.toString()});
|
|
410
505
|
return this.error(err, packet);
|
|
411
506
|
})
|
|
412
507
|
}
|
|
413
508
|
catch (e) {
|
|
414
|
-
this.talk(`${this.
|
|
509
|
+
this.talk(`${this._agent.key}:ask:${packet.id}`, {error:e.toString()});
|
|
415
510
|
return this.error(e, packet)
|
|
416
511
|
}
|
|
417
512
|
// now when we ask the meta params[0] should be the method
|
|
@@ -428,6 +523,7 @@ class Deva {
|
|
|
428
523
|
***************/
|
|
429
524
|
question(TEXT=false, DATA=false) {
|
|
430
525
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
526
|
+
this.state('question_me', {text:TEXT, data:DATA});
|
|
431
527
|
|
|
432
528
|
const id = this.uid(); // generate a unique transport id for the question.
|
|
433
529
|
const t_split = TEXT.split(' ');
|
|
@@ -451,7 +547,7 @@ class Deva {
|
|
|
451
547
|
let text = TEXT,
|
|
452
548
|
params = false,
|
|
453
549
|
method = 'question',
|
|
454
|
-
key = this.
|
|
550
|
+
key = this._agent.key;
|
|
455
551
|
|
|
456
552
|
return new Promise((resolve, reject) => {
|
|
457
553
|
if (!TEXT) return reject(this._messages.notext);
|
|
@@ -464,13 +560,14 @@ class Deva {
|
|
|
464
560
|
// #agent method:param1:param2 with text strings for proccessing
|
|
465
561
|
// !method param:list:parse for the local agent
|
|
466
562
|
// if is an ask then we format one way
|
|
467
|
-
let _state = '
|
|
563
|
+
let _state = 'question_default';
|
|
468
564
|
if (isAsk) {
|
|
469
565
|
_state = 'question_ask'
|
|
470
566
|
key = t_split[0].substring(1);
|
|
471
567
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
472
568
|
method = params[0];
|
|
473
569
|
text = t_split.slice(2).join(' ').trim();
|
|
570
|
+
|
|
474
571
|
}
|
|
475
572
|
else if (isCmd) {
|
|
476
573
|
_state = 'question_cmd'
|
|
@@ -480,8 +577,8 @@ class Deva {
|
|
|
480
577
|
}
|
|
481
578
|
|
|
482
579
|
packet.q = {
|
|
483
|
-
agent: this.
|
|
484
|
-
client: this.
|
|
580
|
+
agent: this._agent || false,
|
|
581
|
+
client: this._client || false,
|
|
485
582
|
meta: {
|
|
486
583
|
key,
|
|
487
584
|
orig,
|
|
@@ -494,18 +591,24 @@ class Deva {
|
|
|
494
591
|
}
|
|
495
592
|
|
|
496
593
|
// hash the packet and insert the hash into the packet meta object.
|
|
594
|
+
this.state('hash_question');
|
|
497
595
|
packet.q.meta.hash = this.hash(JSON.stringify(packet.q));
|
|
596
|
+
|
|
498
597
|
this.state(_state, packet);
|
|
499
598
|
|
|
500
599
|
// If a question to another Deva with '#' then trigger events
|
|
501
600
|
if (isAsk) {
|
|
601
|
+
this.state('question_asking');
|
|
602
|
+
this.prompt(`sending key ${key}`);
|
|
502
603
|
this.talk(`${key}:ask`, packet);
|
|
503
604
|
this.once(`${key}:ask:${packet.id}`, answer => {
|
|
504
605
|
return resolve(answer);
|
|
505
606
|
});
|
|
506
607
|
}
|
|
608
|
+
|
|
507
609
|
// if the user sends a local command '$' then it will ask of the self.
|
|
508
610
|
else {
|
|
611
|
+
this.state('question_answering');
|
|
509
612
|
if (typeof this.methods[method] !== 'function') {
|
|
510
613
|
return resolve(this._methodNotFound(packet));
|
|
511
614
|
}
|
|
@@ -514,10 +617,10 @@ class Deva {
|
|
|
514
617
|
const html = typeof result === 'object' ? result.html : result;
|
|
515
618
|
const data = typeof result === 'object' ? result.data : false;
|
|
516
619
|
packet.a = {
|
|
517
|
-
agent: this.
|
|
518
|
-
client: this.
|
|
620
|
+
agent: this._agent || false,
|
|
621
|
+
client: this._client || false,
|
|
519
622
|
meta: {
|
|
520
|
-
key: this.
|
|
623
|
+
key: this._agent.key,
|
|
521
624
|
method,
|
|
522
625
|
},
|
|
523
626
|
text,
|
|
@@ -526,10 +629,13 @@ class Deva {
|
|
|
526
629
|
created: Date.now(),
|
|
527
630
|
};
|
|
528
631
|
// create a hash for the answer and insert into answer meta.
|
|
632
|
+
this.state('hash_answer');
|
|
529
633
|
packet.a.meta.hash = this.hash(JSON.stringify(packet.a));
|
|
530
634
|
|
|
531
635
|
// create a hash for entire packet and insert into packet
|
|
636
|
+
this.state('hash_packet');
|
|
532
637
|
packet.hash = this.hash(JSON.stringify(packet));
|
|
638
|
+
|
|
533
639
|
this.state('question_answer', packet);
|
|
534
640
|
|
|
535
641
|
return resolve(packet);
|
|
@@ -546,7 +652,7 @@ class Deva {
|
|
|
546
652
|
|
|
547
653
|
/**************
|
|
548
654
|
func: init
|
|
549
|
-
params:
|
|
655
|
+
params: client - the client data to use that is provided by the clients.
|
|
550
656
|
describe:
|
|
551
657
|
The main init interface where the chain begins. Where the states fire for
|
|
552
658
|
each process of setting:
|
|
@@ -558,9 +664,12 @@ class Deva {
|
|
|
558
664
|
6. The system start function will create a chain reaction of states that load.
|
|
559
665
|
7. If there is an error the init function rejects the call.
|
|
560
666
|
***************/
|
|
561
|
-
init() {
|
|
562
|
-
this._active = Date.now();
|
|
667
|
+
init(client) {
|
|
563
668
|
// set client
|
|
669
|
+
this.Client = client;
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
this._active = Date.now();
|
|
564
673
|
return new Promise((resolve, reject) => {
|
|
565
674
|
this.events.setMaxListeners(this.maxListeners);
|
|
566
675
|
this._assignInherit().then(() => {
|
|
@@ -569,6 +678,11 @@ class Deva {
|
|
|
569
678
|
return this._assignListeners();
|
|
570
679
|
}).then(() => {
|
|
571
680
|
this.state('init');
|
|
681
|
+
|
|
682
|
+
this.Security = client.security;
|
|
683
|
+
this.Support = client.support;
|
|
684
|
+
this.Services = client.services;
|
|
685
|
+
|
|
572
686
|
return this.onInit && typeof this.onInit === 'function' ? this.onInit() : this.start();
|
|
573
687
|
}).then(started => {
|
|
574
688
|
return resolve(started)
|
|
@@ -685,7 +799,7 @@ class Deva {
|
|
|
685
799
|
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
686
800
|
const id = this.uid();
|
|
687
801
|
const dateFormat = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeStyle: 'medium' }).format(this._active);
|
|
688
|
-
let text = `${this.
|
|
802
|
+
let text = `${this._agent.name} active since ${dateFormat}`;
|
|
689
803
|
if (ammend) text = text + `\n${ammend}`;
|
|
690
804
|
return Promise.resolve({text});
|
|
691
805
|
}
|
|
@@ -698,7 +812,7 @@ class Deva {
|
|
|
698
812
|
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.
|
|
699
813
|
***************/
|
|
700
814
|
prompt(text) {
|
|
701
|
-
return this.talk('prompt', {text, agent:this.
|
|
815
|
+
return this.talk('prompt', {text, agent:this._agent});
|
|
702
816
|
}
|
|
703
817
|
|
|
704
818
|
/**************
|
|
@@ -716,18 +830,65 @@ class Deva {
|
|
|
716
830
|
return the_hash.digest('hex');
|
|
717
831
|
}
|
|
718
832
|
|
|
833
|
+
/**************
|
|
834
|
+
func: client
|
|
835
|
+
params: none
|
|
836
|
+
describe:
|
|
837
|
+
this function allows statement management for when client prfioe is
|
|
838
|
+
being accessed.
|
|
839
|
+
***************/
|
|
840
|
+
client() {
|
|
841
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
842
|
+
this.state('client');
|
|
843
|
+
return this._client;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**************
|
|
847
|
+
func: agent
|
|
848
|
+
params: none
|
|
849
|
+
describe:
|
|
850
|
+
this function allows statement management for when client prfioe is
|
|
851
|
+
being accessed.
|
|
852
|
+
***************/
|
|
853
|
+
agent() {
|
|
854
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
855
|
+
this.state('agent');
|
|
856
|
+
return this._agent;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
/**************
|
|
860
|
+
func: security
|
|
861
|
+
params: opts
|
|
862
|
+
describe: basic security features available in a Deva.
|
|
863
|
+
***************/
|
|
864
|
+
security(opts) {}
|
|
865
|
+
|
|
866
|
+
/**************
|
|
867
|
+
func: security
|
|
868
|
+
params: opts
|
|
869
|
+
describe: basic support features available in a Deva.
|
|
870
|
+
***************/
|
|
871
|
+
support(opts) {}
|
|
872
|
+
|
|
873
|
+
/**************
|
|
874
|
+
func: security
|
|
875
|
+
params: opts
|
|
876
|
+
describe: basic services features available in a Deva.
|
|
877
|
+
***************/
|
|
878
|
+
services(opts) {}
|
|
879
|
+
|
|
719
880
|
/**************
|
|
720
881
|
func: startDevas
|
|
721
882
|
params: none
|
|
722
883
|
describe:
|
|
723
884
|
Start Devas will initialize the Deva agents inside this curent Deva.
|
|
724
885
|
***************/
|
|
725
|
-
startDevas() {
|
|
886
|
+
startDevas(client) {
|
|
726
887
|
this.state('devas_start');
|
|
727
888
|
return new Promise((resolve, reject) => {
|
|
728
889
|
const devas = [];
|
|
729
890
|
for (let x in this.devas) {
|
|
730
|
-
devas.push(this.devas[x].init());
|
|
891
|
+
devas.push(this.devas[x].init(client));
|
|
731
892
|
}
|
|
732
893
|
Promise.all(devas).then(() => {
|
|
733
894
|
this.state('devas_ready');
|