Svarga is a lightweight class object that provides events and object management.
$> npm i @indra.ai/svarga
// include the main svarga class
const Svarga = require('@indra.ai/svarga');
// setup the svarga object
const svarga = new Svarga({
me: {
key: '*DEVA KEY*',
name: '*DEVA NAME*',
description: '*DEVA LONG DESCRIPTION*',
prompt: {
emoji: '🐶',
text: '*DEVA*',
color: 'white',
},
voice: {
speech: 'Alex',
speed: 1
},
profile: {
avatar: '*DEVA AVATAR*',
background: '*DEVA BACKGROUND*',
describe: '*DEVA SHORT DESCRIPTION*',
gender: '*DEVA GENDER*',
},
translate(input) {
return input.trim();
},
parse(input) {
return input.trim();
}
},
vars: {},
listeners: {},
deva: {},
modules: {},
func: {},
methods: {},
onStart() {},
onStop() {},
onLoaded() {},
onLogout() {},
onInit() {},
});
// initialize the class
svarga.init();
this.me
The "me" object contains the profile information for the DEVA.
- key: The unique key for the Deva.
- name: The name of the Deva.
- description: A description of what the Deva does.
- prompt: Define how prompt displays.
- emoji: The emoji for use as a prompt indicator.
- text: Short text for prompt display.
- color: The color of the prompt for the Deva.
- voice: Voice properties of the Deva.
- speech: The name of the voice speech to use.
- speed: The speed of the voice.
- profile: Profile Information
- avatar: The avatar for the Deva
- background: The background image for the Deva
- gender: The gender of the Deva
- describe: A short description of the deva 100 characters or less.
this.vars
The vars can be use to set local variables for the deva that need to be used in your program.
There are no default variables, so the scope is for you and your imagination to figure out.
...
vars: {
foo: 'bar',
steps: 10,
strings: 'Some variable string',
adding: 1 + 9 + 11,
objects: {
key: 'key value'
},
arrays: [
'value 1',
'value 2',
]
}
...
Listeners are what you setup that allow your Deva to communicate with other Deva or parts of your application/system.
this.listeners
Each Deva comes with a set of default listeners to provide basic functionality.
The question event is the functionality that exposes the methods to the outside world. When a deva asks a question the string is parsed into a question format so that commands to access various methods can be exposed.
const question_id = this.uid();
this.talk('deva-name:question', {
id: question_id,
key: this.me.key,
q: {
bot: this.me,
text: '*method*:*params* text to send to the event',
data: evt_data,
},
created: Date.now(),
});
this.once(`deva-name:question:${question_id}`, response => {
console.log('response', response);
});
This will trigger an event to start the Deva.
this.talk(`*deva.me.key*:start`);
This will trigger an event to stop the Deva.
this.talk(`*deva.me.key*:stop`);
This will trigger an event to broadcast the Deva status.
this.talk(`*deva.me.key*:status`);
This is a global event listener that fires when all things are loaded.
this.talk(`loaded`);
This is a global event listener that fires when the Deva logsout.
this.deva
The main object for Deva that are bwlow this Deva.
The external modules that your Deva might require to function.
this.modules
The functions that your deva uses to operate. Functions are not exposed through the api to public access.
this.func
this.methods
The methods are exposed publicly through the question event that parses a string and sends a request to the question method that then interacts with functions, modules, and variables.
Provided are a set of state functions that trigger when a Deva is at various states of starting/stopping.
The onStart()
function runs after the start()
function has completed.
this.onStart() {
// some code to run when the deva starts.
}
The onStop()
function runs after the stop()
function has completed.
this.onStop() {
// some code to run when the deva stops
}
The onLoaded()
function runs after the loaded
event has fired.
this.onLoaded() {
// some code to run when the deva is loaded
}
The onLogout()
function runs after the logout
event has fired.
this.onLogout() {
// some code to run when the deva logs out.
}
The onInit()
function runs after the init()
function has completed.
this.onInit() {
// some code to run when the Deva initializes.
}
Generates a unique ID that is used in packet transfer and other various ways.
this.uid() // inside the object
svarga.uid() // outside the object
// example
this.vars.id = this.uid()
The talk()
function is used when your Deva needs to broadcast an event that other Deva
or functions would be listening for.
this.talk('event', resource); // inside the object
svarga.talk('event', resource); // outside the object
// example
const evt_id = this.uid();
const evt_data = {
task_id: 1,
task_name: 'this is blank data',
task_contact: 'joe@schmo.com',
};
this.talk('big-event', {
id: evt_id,
key: this.me.key,
q: {
bot: this.me,
text: 'text to send to the event',
data: evt_data,
},
created: Date.now(),
});
The listen()
function can assign listeners to the Deva and designate which callback
function to run when an event is fired.
Listeners can be set up individually this way or also added to the listeners object independently.
this.listen('some-event', this.func.listener);
this.func.listenter = packet => {
console.log('some-event-fired');
};
The once()
function can assign a one-time listener to a function. This is useful when returning data with an id that one Deva has submitted to another Deva. Also very useful for submit responses that are unique to the request.
this.once(`some-once-event`, this.func.listener)
this.func.listener = packet => {
console.log('some-once-event-fired');
}
The ignore()
function removes a listener from the designated event. This is useful for adding and removing events dynamically or as needed.
this.ignore('ignore-event', this.func.listener);
this.func.listener = packet => {
console.log('ignore-event-fired');
}
To add a Deva dynamically use the addDeva()
function. This can be utilized to add Deva to an existing Deva after the object has already been created.
const opts = {
me: {...},
vars: {...},
listeners: {...},
deva: {...},
func: {...},
onStart() {},
onStop() {},
onLoaded() {},
onLogout() {},
}
this.addDeva('deva-name', opts);
To delete a Deva for any reason use deleteDeva()
. This will delete the Deva and all it's parts from the current Deva.
this.deleteDeva('deva-key');
The question(packet)
function is a default function that allows the system to ask questions of itself or other Deva.
The function checks the beginning of a string for a #
to determine wether to issue a command to run a specific method.
See Question Listener for usage.
#deva method:params message
The status()
function will return the running status of the current Deva.
The start()
function will start the Deva and run the onStart()
state function.
The stop()
function will stop the Deva and run the onStop()
state function.
The init()
function will initialize the Deva and run the onInit()
state function.
The initDeva()
function will initialize the Deva located under the current Deva. This function is mostly used by init(true)
where true will auto initialize the sub-Deva.