Compare commits

...

2 Commits

Author SHA1 Message Date
Will Webberley
91823ec63b added gist policy handler and gist policy to core model 2017-07-27 18:17:28 +01:00
Will Webberley
ee4450deb4 added a card handler for gist cards 2017-07-27 18:10:39 +01:00
3 changed files with 43 additions and 2 deletions

View File

@ -18,6 +18,7 @@ module.exports = [
"conceptualise a ~ policy ~ P that is an entity and has the value V as ~ enabled ~ and has the agent A as ~ target ~",
"conceptualise a ~ tell policy ~ P that is a policy",
"conceptualise an ~ ask policy ~ P that is a policy",
"conceptualise a ~ gist policy ~ P that is a policy",
"conceptualise a ~ listen policy ~ P that is a policy",
"conceptualise a ~ listen onbehalfof policy ~ P that is a policy",
"conceptualise a ~ forwardall policy ~ P that is a policy and has the timestamp T as ~ start time ~ and has the value V as ~ all agents ~",

View File

@ -114,6 +114,17 @@ class CardHandler {
data = this.node.addNL(card.content);
return this.node.addSentence(`there is a ${data.response.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${data.response.message.replace(/'/g, "\\'")}' as content and is in reply to the card '${card.name}'.`);
},
'gist card': (card) => {
// Add sentence to any active gist policy queues
for (const policy of this.node.getInstances('gist policy')) {
if (policy.enabled === 'true' && policy.target && policy.target.name) {
const targetName = policy.target.name;
if (!(targetName in this.agent.policyHandler.unsentGistCards)) { this.agent.policyHandler.unsentGistCards[targetName] = []; }
this.agent.policyHandler.unsentGistCards[targetName].push(card);
}
}
},
};
}

View File

@ -86,6 +86,7 @@ class PolicyHandler {
this.node = agent.node;
this.unsentTellCards = {};
this.unsentAskCards = {};
this.unsentGistCards = {};
this.lastSuccessfulRequest = 0;
this.handlers = {
@ -104,8 +105,7 @@ class PolicyHandler {
for (const to of card.is_tos) {
if (to.id === policy.target.id) { inCard = true; break; }
}
if (!inCard) {
card.addRelationship('is to', policy.target);
if (!inCard) { card.addRelationship('is to', policy.target);
}
data += `${card.ce}\n`;
}
@ -159,6 +159,35 @@ class PolicyHandler {
}
},
'gist policy': (policy) => {
// For each gist policy in place, send all currently-untold cards to each target
// multiple cards to be sent to one target line-separated
if (policy.target && policy.target.name && policy.target.address) {
if (!(policy.target.name in this.unsentGistCards)) {
this.unsentGistCards[policy.target.name] = [];
}
let data = '';
for (const card of this.unsentGistCards[policy.target.name]) {
if (card.is_tos && card.is_from.name.toLowerCase() !== policy.target.name.toLowerCase()) { // Don't send back a card sent from target agent
// Make sure target is not already a recipient
let inCard = false;
for (const to of card.is_tos) {
if (to.id === policy.target.id) { inCard = true; break; }
}
if (!inCard) { card.addRelationship('is to', policy.target);
}
data += `${card.ce}\n`;
}
}
if (data.length) {
net.makeRequest('POST', policy.target.address, POST_SENTENCES_ENDPOINT, data, () => {
this.lastSuccessfulRequest = new Date().getTime();
this.unsentGistCards[policy.target.name] = [];
});
}
}
},
'listen policy': (policy) => {
// Make request to target to get cards addressed to THIS agent
if (policy.target && policy.target.address) {