Skip to content

Commit

Permalink
Actions for questionnaires
Browse files Browse the repository at this point in the history
  • Loading branch information
polterguy committed Aug 9, 2023
1 parent aa2e744 commit 132f675
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

alter table questions add column name varchar(40) null;

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

alter table questionnaires add column action varchar(40) null;

1 change: 1 addition & 0 deletions backend/files/system/magic/questionnaires.get.hl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ data.connect:[generic|magic]
columns
questionnaires.name
questionnaires.type
questionnaires.action
where
and

Expand Down
1 change: 1 addition & 0 deletions backend/files/system/magic/questionnaires.post.hl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.arguments
name:string
type:string
action:string
.description:CRUD create endpoint inserting one record into your questionnaires table in your magic database taking name with authentication and authorisation for root,admin roles
.type:crud-create

Expand Down
1 change: 1 addition & 0 deletions backend/files/system/magic/questionnaires.put.hl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.arguments
name:string
type:string
action:string
.description:CRUD update endpoint updating one record in your questionnaires table in your magic database filtering which item to update with name updating type fields with authentication and authorisation for root roles
.type:crud-update

Expand Down
1 change: 1 addition & 0 deletions backend/files/system/magic/questions.get.hl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ data.connect:[generic|magic]
questions.questionnaire
questions.type
questions.context
questions.name
where
and

Expand Down
2 changes: 2 additions & 0 deletions backend/files/system/magic/questions.post.hl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
.arguments
question:string
questionnaire:string
type:string
name:string
.description:CRUD create endpoint inserting one record into your questions table in your magic database taking question, questionnaire with authentication and authorisation for root,admin roles
.type:crud-create

Expand Down
2 changes: 2 additions & 0 deletions backend/files/system/magic/questions.put.hl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
question_id:long
question:string
questionnaire:string
type:string
name:string
.description:CRUD update endpoint updating one record in your questions table in your magic database filtering which item to update with question_id updating question, questionnaire fields with authentication and authorisation for root,admin roles
.type:crud-update

Expand Down
41 changes: 38 additions & 3 deletions backend/files/system/openai/front.files/chat/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,14 @@ function ensureInitialQuestionnaireIsFetched() {
});
}

// Contains all action values for questionnaire.
const questionnaireActionValues = {};

/*
* Function that loops through questionnaire asking questions until no more
* questions remains in ainiroQuestionnaire array.
*/
function askNextQuestion() {
function askNextQuestion(justAsked = false) {

// Verifying we've got more questions remaining.
if (!ainiroQuestionnaire.questions || ainiroQuestionnaire.questions?.length === 0) {
Expand All @@ -316,6 +319,32 @@ function askNextQuestion() {
// Storing the fact that user has taken this questionnaire in local storage.
localStorage.setItem('ainiro-questionnaire.' + ainiroQuestionnaire.name, JSON.stringify(ainiroQuestionnaireAnswers));
}
if(justAsked === true && ainiroQuestionnaire.action) {

// Done with questionnaire, invoking action on the server
const payload = {
action: ainiroQuestionnaire.action,
values: questionnaireActionValues,
};
let url = `[[url]]/magic/system/openai/questionnaire-action`;
fetch(url, {
method: 'POST',
headers: {
"Content-Type": 'application/json'
},
body: JSON.stringify(payload)
})
.then(res => {
if (res.status >= 200 && res.status <= 299) {
return res.json();
} else {
throw Error(res.statusText);
}
})
.then(() => {
console.log('Successfully invoked action for questionnaire');
});
}
return;
}
const row = window.document.createElement('div');
Expand All @@ -330,7 +359,7 @@ function askNextQuestion() {
// Checking type of question.
if (ainiroQuestionnaire.questions[0].type === 'message') {
ainiroQuestionnaire.questions = ainiroQuestionnaire.questions.slice(1);
askNextQuestion();
askNextQuestion(true);
}
}

Expand Down Expand Up @@ -431,6 +460,11 @@ function aista_invoke_prompt(msg, token, speech) {
// Checking if we're in a questionnaire loop.
if (ainiroQuestionnaire.questions?.length > 0) {

// Checking if this is a "named" question.
if (ainiroQuestionnaire.questions[0].name) {
questionnaireActionValues[ainiroQuestionnaire.questions[0].name] = msg;
}

// Creating our URL.
let url = `[[url]]/magic/system/openai/answer`;

Expand All @@ -453,6 +487,7 @@ function aista_invoke_prompt(msg, token, speech) {
question: ainiroQuestionnaire.questions[0].question,
answer: msg,
context: ainiroQuestionnaire.questions[0].context,
name: ainiroQuestionnaire.questions[0].name,
});
ainiroQuestionnaire.questions = ainiroQuestionnaire.questions.slice(1);
payload.answer = msg;
Expand Down Expand Up @@ -491,7 +526,7 @@ function aista_invoke_prompt(msg, token, speech) {
}

// Asking next question, if we've got more questions in our questionnaire.
askNextQuestion();
askNextQuestion(true);
});

} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

/*
* Subscribes the specified [name] and [email] to a newsletter using SendGrid account.
*/
slots.create:magic.questionnaires.action.sendgrid-subscribe

// Sanity checking invocation.
validators.mandatory:x:@.arguments/*/name
validators.mandatory:x:@.arguments/*/email
validators.email:x:@.arguments/*/email

// Invoking SendGrid to create recipient.
.auth
set-value:x:@.auth
strings.concat
.:"Bearer "
config.get:"magic:sendgrid:api-key"
config.get:"magic:sendgrid:chatbot-list"
set-name:x:./*/http.put/**/full_name
config.get:"magic:sendgrid:full_name_id"
.:e1_T
unwrap:x:+/**
http.put:"https://api.sendgrid.com/v3/marketing/contacts"
convert:true
headers
Authorization:x:@.auth
Content-Type:application/json
payload
list_ids
.:x:@config.get
contacts
.
email:x:@.arguments/*/email
custom_fields
full_name:x:@.arguments/*/name

// Sanity checking invocation.
if
neq:x:@http.put
.:int:202
.lambda

// Oops ...!!
lambda2hyper:x:@http.put/*/content
log.error:Could not add new contact to list
email:x:@.arguments/*/email
name:x:@.arguments/*/name
stack:x:@lambda2hyper

else

// Doing some basic logging
log.info:Successfully added contact to SendGrid automation
name:x:@.arguments/*/name
email:x:@.arguments/*/email
29 changes: 29 additions & 0 deletions backend/files/system/openai/questionnaire-action.post.hl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/*
* Invokes the specified [action] with the specified [values].
*/
.arguments
action:string
values:*
.description:Invokes the specified [action] with the specified [values]

// Sanity checking invocation.
validators.mandatory:x:@.arguments/*/action

// Figuring out what slot to invoke.
.slot
set-value:x:@.slot
strings.concat
.:magic.questionnaires.action.
get-value:x:@.arguments/*/action

// Parametrising slot.
add:x:./*/signal
get-nodes:x:@.arguments/*/values/*

// Invoking slot.
signal:x:@.slot

// Returning success to caller.
return
result:success
4 changes: 2 additions & 2 deletions backend/files/system/openai/questionnaire.get.hl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data.connect:[generic|magic]

// Retrieving all questions from database.
data.select:@"
select q.question, q.type, q.context
select q.question, q.type, q.context, q.name
from questions q
inner join questionnaires qu on qu.name = q.questionnaire
inner join ml_types t on t.initial_questionnaire = qu.name
Expand All @@ -24,7 +24,7 @@ select q.question, q.type, q.context

// Retrieving main questionnaire from database.
data.select:@"
select q.name, q.type
select q.name, q.type, q.action
from questionnaires q
inner join ml_types t on t.initial_questionnaire = q.name
where t.type = @type"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ <h3 mat-dialog-title class="mb-0">

</div>

<!-- Action -->
<div class="col-12 pe-lg-0">

<mat-form-field class="w-100 standalone-field me-2">
<span
matPrefix
matTooltip="Action"
class="d-flex flex-nowrap align-items-center justify-content-between me-2">
<mat-icon>edit</mat-icon>
<span class="text-muted">|</span>
</span>
<input
matInput
placeholder="Action"
[(ngModel)]="action"
name="action"
autocomplete="off">
</mat-form-field>

</div>

<!-- Type -->
<div class="col-12 pe-lg-0">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class MachineLearningEditQuestionnaireComponent {

name: string = '';
type: string = 'single-shot';
action: string = 'sendgrid-subscribe';

constructor(
private generalService: GeneralService,
Expand All @@ -28,6 +29,7 @@ export class MachineLearningEditQuestionnaireComponent {
if (this.data) {
this.name = this.data.name;
this.type = this.data.type;
this.action = this.data.action;
}
}

Expand All @@ -51,6 +53,7 @@ export class MachineLearningEditQuestionnaireComponent {
const data: any = {
name: this.name,
type: this.type,
action: this.action,
};
if (this.data) {
data.id = this.data.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export class MachineLearningEditQuestionsComponent implements OnInit {
this.questions += 'context=' + idx.context;
addedMeta = true;
}
if (idx.name) {
if (!addedMeta) {
this.questions += ' => ';
} else {
this.questions += ', ';
}
this.questions += 'name=' + idx.name;
addedMeta = true;
}
this.questions += '\r\n'
}
},
Expand Down

0 comments on commit 132f675

Please sign in to comment.