@tiledesk/tiledesk-tybot-connector 2.0.29-rc3 → 2.0.29-rc4

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
@@ -42,8 +42,6 @@ let TILEBOT_ENDPOINT = null;
42
42
  let staticBots;
43
43
 
44
44
  router.post('/ext/:botid', async (req, res) => {
45
- const ttotal = Date.now()
46
- const t1 = Date.now();
47
45
  const botId = req.params.botid;
48
46
  winston.verbose("(tybotRoute) POST /ext/:botid called: " + botId)
49
47
  if(!botId || botId === "null" || botId === "undefined"){
@@ -175,10 +173,10 @@ router.post('/ext/:botid', async (req, res) => {
175
173
  cache: tdcache
176
174
  }
177
175
  );
178
- const t1e = Date.now();
179
- const t2 = Date.now();
176
+
177
+
180
178
  directivesPlug.processDirectives( () => {
181
- const t2e = Date.now();
179
+
182
180
  winston.verbose("(tybotRoute) Actions - Directives executed.");
183
181
  });
184
182
  }
@@ -201,7 +199,7 @@ router.post('/ext/:botid', async (req, res) => {
201
199
  TILEBOT_ENDPOINT: TILEBOT_ENDPOINT
202
200
  });
203
201
  apiext.sendSupportMessageExt(reply, projectId, requestId, token, () => {
204
- const ttotale = Date.now()
202
+
205
203
  winston.verbose("(tybotRoute) sendSupportMessageExt reply sent: ", reply)
206
204
  });
207
205
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "2.0.29-rc3",
3
+ "version": "2.0.29-rc4",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -57,6 +57,7 @@ const { DirReplaceBotV3 } = require('./directives/DirReplaceBotV3');
57
57
  const { DirAiPrompt } = require('./directives/DirAiPrompt');
58
58
  const { DirWebResponse } = require('./directives/DirWebResponse');
59
59
  const { DirConnectBlock } = require('./directives/DirConnectBlock');
60
+ const { DirAiCondition } = require('./directives/DirAiCondition');
60
61
  const { DirAddKbContent } = require('./directives/DirAddKbContent');
61
62
  const { DirFlowLog } = require('./directives/DirFlowLog');
62
63
 
@@ -593,6 +594,18 @@ class DirectivesChatbotPlug {
593
594
  }
594
595
  });
595
596
  }
597
+ else if (directive_name === Directives.AI_CONDITION) {
598
+ new DirAiCondition(context).execute(directive, async (stop) => {
599
+ if (stop == true) {
600
+ winston.debug("(DirectivesChatbotPlug) DirAskGPTV2 Stopping Actions on: ", directive);
601
+ this.theend();
602
+ }
603
+ else {
604
+ let next_dir = await this.nextDirective(this.directives);
605
+ this.process(next_dir);
606
+ }
607
+ });
608
+ }
596
609
  else if (directive_name === Directives.WHATSAPP_ATTRIBUTE) {
597
610
  new DirWhatsappByAttribute(context).execute(directive, async (stop) => {
598
611
  let next_dir = await this.nextDirective(this.directives);
@@ -0,0 +1,483 @@
1
+ const axios = require("axios").default;
2
+ const { TiledeskChatbot } = require("../../engine/TiledeskChatbot");
3
+ const { Filler } = require("../Filler");
4
+ let https = require("https");
5
+ const { DirIntent } = require("./DirIntent");
6
+ const { TiledeskChatbotConst } = require("../../engine/TiledeskChatbotConst");
7
+ const { TiledeskChatbotUtil } = require("../../utils/TiledeskChatbotUtil");
8
+ require('dotenv').config();
9
+ const winston = require('../../utils/winston');
10
+ const Utils = require("../../utils/HttpUtils");
11
+ const utils = require("../../utils/HttpUtils");
12
+ const httpUtils = require("../../utils/HttpUtils");
13
+ const integrationService = require("../../services/IntegrationService");
14
+ const { Logger } = require("../../Logger");
15
+ const { randomUUID } = require("crypto");
16
+
17
+
18
+ class DirAiCondition {
19
+
20
+ constructor(context) {
21
+ if (!context) {
22
+ throw new Error('context object is mandatory');
23
+ }
24
+ this.context = context;
25
+ this.chatbot = this.context.chatbot;
26
+ this.tdcache = this.context.tdcache;
27
+ this.requestId = this.context.requestId;
28
+ this.projectId = this.context.projectId;
29
+ this.token = this.context.token;
30
+ this.API_ENDPOINT = this.context.API_ENDPOINT;
31
+
32
+ this.intentDir = new DirIntent(context);
33
+ this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest?.draft, intent_id: this.context.reply?.attributes?.intent_info?.intent_id });
34
+ }
35
+
36
+ execute(directive, callback) {
37
+ winston.verbose("Execute AiAiCondition directive");
38
+ let action;
39
+ if (directive.action) {
40
+ action = directive.action;
41
+ }
42
+ else {
43
+ this.logger.error("Incorrect action for ", directive.name, directive)
44
+ winston.debug("DirAiAiCondition Incorrect directive: ", directive);
45
+ callback();
46
+ return;
47
+ }
48
+ this.go(action, (stop) => {
49
+ this.logger.native("[Ai Condition] Executed");
50
+ callback(stop);
51
+ })
52
+ }
53
+
54
+ async go(action, callback) {
55
+ winston.debug("DirAiCondition action:", action);
56
+ if (!this.tdcache) {
57
+ winston.error("Error: DirAiCondition tdcache is mandatory");
58
+ callback();
59
+ return;
60
+ }
61
+
62
+ let intents = action.intents;
63
+ // intents = [
64
+ // {
65
+ // "label": "26efa629-686e-4a23-a2f8-38c8f5beb408",
66
+ // "prompt": "user asking for medical information",
67
+ // "conditionIntentId": "#9b1c29c1671847dba6db561f771a142e"
68
+ // }
69
+ // ]
70
+ let falllbackIntent = action.falllbackIntent; // non condition met block
71
+ let falseIntent = action.falllbackIntent; // On error block
72
+ await this.checkMandatoryParameters(action).catch( async (missing_param) => {
73
+ const error = "AiPrompt Error: '" + missing_param + "' attribute is undefined"
74
+ this.logger.error(error);
75
+ await this.chatbot.addParameter("flowError", error);
76
+ if (falseIntent) {
77
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
78
+ callback(true);
79
+ return Promise.reject();
80
+ }
81
+ callback();
82
+ return Promise.reject();
83
+ })
84
+
85
+ // fill attributes
86
+ let requestVariables = null;
87
+ requestVariables =
88
+ await TiledeskChatbot.allParametersStatic(
89
+ this.tdcache, this.requestId
90
+ )
91
+ const filler = new Filler();
92
+
93
+ let conditions = "";
94
+ intents.forEach( function(intent) {
95
+ let filled_prompt = filler.fill(intent.prompt, requestVariables);
96
+ conditions += `- label: ${intent.label} when: ${filled_prompt}\n`
97
+ });
98
+
99
+ let instructions = filler.fill(action.instructions, requestVariables);
100
+ let condition_prompt = TiledeskChatbotUtil.AiConditionPromptBuilder(prompt_header, intents, instructions)
101
+
102
+ // let raw_condition_prompt = `Reply with the label satisfying the corresponding condition or with “fallback” if all conditions are false.
103
+ // If more than one condition is true, answer with the first label corresponding to the true condition, following the order from top to bottom.
104
+ // ${conditions}
105
+ // ${instructions}`
106
+
107
+ // const filled_question = condition_prompt; //filler.fill(action.question, requestVariables);
108
+ const filled_context = filler.fill(action.context, requestVariables);
109
+
110
+ // evaluate
111
+
112
+ let AI_endpoint = process.env.AI_ENDPOINT;
113
+ winston.verbose("DirAiPrompt AI_endpoint " + AI_endpoint);
114
+
115
+ let headers = {
116
+ 'Content-Type': 'application/json'
117
+ }
118
+
119
+ let key;
120
+ let ollama_integration;
121
+
122
+ if (action.llm === 'ollama') {
123
+ ollama_integration = await integrationService.getIntegration(this.projectId, action.llm, this.token).catch( async (err) => {
124
+ this.logger.error("[AI Condition] Error getting ollama integration.")
125
+ winston.error("DirAiPrompt Error getting ollama integration: ", err);
126
+ await this.chatbot.addParameter("flowError", "Ollama integration not found");
127
+ if (falseIntent) {
128
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
129
+ callback(true);
130
+ return;
131
+ }
132
+ callback();
133
+ return;
134
+ });
135
+
136
+ } else {
137
+ key = await integrationService.getKeyFromIntegrations(this.projectId, action.llm, this.token);
138
+
139
+ if (!key) {
140
+ this.logger.error("[AI Condition] llm key not found in integrations");
141
+ winston.error("Error: DirAiPrompt llm key not found in integrations");
142
+ await this.chatbot.addParameter("flowError", "AiPrompt Error: missing key for llm " + action.llm);
143
+ if (falseIntent) {
144
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
145
+ callback(true);
146
+ return;
147
+ }
148
+ callback();
149
+ return;
150
+ }
151
+ }
152
+
153
+ let json = {
154
+ question: condition_prompt,
155
+ llm: action.llm,
156
+ model: action.model,
157
+ llm_key: key,
158
+ temperature: action.temperature,
159
+ max_tokens: action.max_tokens
160
+ }
161
+
162
+ if (action.context) {
163
+ json.system_context = filled_context;
164
+ }
165
+ // if (transcript) {
166
+ // json.chat_history_dict = await this.transcriptToLLM(transcript);
167
+ // }
168
+
169
+ if (action.llm === 'ollama') {
170
+ json.llm_key = "";
171
+ json.model = {
172
+ name: action.model,
173
+ url: ollama_integration.value.url,
174
+ token: ollama_integration.value.token
175
+ }
176
+ json.stream = false
177
+
178
+ }
179
+
180
+ winston.debug("DirAiPrompt json: ", json);
181
+
182
+ const HTTPREQUEST = {
183
+ url: AI_endpoint + "/ask",
184
+ headers: headers,
185
+ json: json,
186
+ method: 'POST'
187
+ }
188
+ winston.debug("DirAiPrompt HttpRequest: ", HTTPREQUEST);
189
+
190
+ httpUtils.request(
191
+ HTTPREQUEST, async (err, resbody) => {
192
+ if (err) {
193
+ winston.error("DirAiPrompt openai err: ", err);
194
+ await this.#assignAttributes(action, answer);
195
+ let error;
196
+ if (err.response?.data?.detail[0]) {
197
+ error = err.response.data.detail[0]?.msg;
198
+ } else if (err.response?.data?.detail?.answer) {
199
+ error = err.response.data.detail.answer;
200
+ } else {
201
+ error = JSON.stringify(err.response.data);
202
+ }
203
+ this.logger.error("[AI Condition] error executing action: ", error);
204
+ if (falseIntent) {
205
+ await this.chatbot.addParameter("flowError", "AiPrompt Error: " + error);
206
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
207
+ callback(true);
208
+ return;
209
+ }
210
+ callback();
211
+ return;
212
+ } else {
213
+
214
+ winston.debug("DirAiPrompt resbody: ", resbody);
215
+ answer = resbody.answer;
216
+ this.logger.native("[AI Condition] answer: ", answer);
217
+
218
+ await this.#assignAttributes(action, answer);
219
+
220
+ if (answer === "fallback") {
221
+ if (falllbackIntent) {
222
+ this.#executeIntent(falllbackIntent, () => {
223
+ if (callback) {
224
+ callback(true);
225
+ return;
226
+ }
227
+ });
228
+ }
229
+ }
230
+ else {
231
+ intents.forEach( i => {
232
+ if (i.label === answer) {
233
+ this.#executeIntent(i.conditionIntentId, () => {
234
+ if (callback) {
235
+ callback(true);
236
+ return;
237
+ }
238
+ });
239
+ }
240
+ });
241
+ }
242
+ this.logger.error("[AI Condition] error executing action: condition label not found in intents list");
243
+ if (falseIntent) {
244
+ await this.chatbot.addParameter("flowError", "[AI Condition] error executing action: condition label not found in intents list");
245
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
246
+ callback(true);
247
+ return;
248
+ }
249
+ callback();
250
+ return;
251
+ }
252
+ }
253
+ )
254
+ }
255
+
256
+ async checkMandatoryParameters(action) {
257
+ return new Promise((resolve, reject) => {
258
+ let params = ['question', 'llm', 'model']; // mandatory params
259
+ params.forEach((p) => {
260
+ if (!action[p]) {
261
+ reject(p)
262
+ }
263
+ })
264
+ resolve(true);
265
+ })
266
+ }
267
+
268
+ /**
269
+ * Transforms the transcirpt array in a dictionary like '0': { "question": "xxx", "answer":"xxx"}
270
+ * merging consecutive messages with the same role in a single question or answer.
271
+ * If the first message was sent from assistant, this will be deleted.
272
+ */
273
+ // async transcriptToLLM(transcript) {
274
+
275
+ // let objectTranscript = {};
276
+
277
+ // if (transcript.length === 0) {
278
+ // return objectTranscript;
279
+ // }
280
+
281
+ // let mergedTranscript = [];
282
+ // let current = transcript[0];
283
+
284
+ // for (let i = 1; i < transcript.length; i++) {
285
+ // if (transcript[i].role === current.role) {
286
+ // current.content += '\n' + transcript[i].content;
287
+ // } else {
288
+ // mergedTranscript.push(current);
289
+ // current = transcript[i]
290
+ // }
291
+ // }
292
+ // mergedTranscript.push(current);
293
+
294
+ // if (mergedTranscript[0].role === 'assistant') {
295
+ // mergedTranscript.splice(0, 1)
296
+ // }
297
+
298
+ // let counter = 0;
299
+ // for (let i = 0; i < mergedTranscript.length - 1; i += 2) {
300
+ // // Check if [i] is role user and [i+1] is role assistant??
301
+ // assert(mergedTranscript[i].role === 'user');
302
+ // assert(mergedTranscript[i+1].role === 'assistant');
303
+
304
+ // if (!mergedTranscript[i].content.startsWith('/')) {
305
+ // objectTranscript[counter] = {
306
+ // question: mergedTranscript[i].content,
307
+ // answer: mergedTranscript[i+1].content
308
+ // }
309
+ // counter++;
310
+ // }
311
+ // }
312
+
313
+ // return objectTranscript;
314
+ // }
315
+
316
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
317
+ let trueIntentDirective = null;
318
+ if (trueIntent) {
319
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
320
+ }
321
+ let falseIntentDirective = null;
322
+ if (falseIntent) {
323
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
324
+ }
325
+ if (result === true) {
326
+ if (trueIntentDirective) {
327
+ this.logger.native("[AI Condition] executing true condition");
328
+ this.intentDir.execute(trueIntentDirective, () => {
329
+ if (callback) {
330
+ callback();
331
+ }
332
+ })
333
+ }
334
+ else {
335
+ this.logger.native("[AI Condition] no block connected to true condition");
336
+ winston.debug("DirAiPrompt No trueIntentDirective specified");
337
+ if (callback) {
338
+ callback();
339
+ }
340
+ }
341
+ }
342
+ else {
343
+ if (falseIntentDirective) {
344
+ this.logger.native("[AI Condition] executing false condition");
345
+ this.intentDir.execute(falseIntentDirective, () => {
346
+ if (callback) {
347
+ callback();
348
+ }
349
+ });
350
+ }
351
+ else {
352
+ this.logger.native("[AI Condition] no block connected to false condition");
353
+ winston.debug("DirAiPrompt No falseIntentDirective specified");
354
+ if (callback) {
355
+ callback();
356
+ }
357
+ }
358
+ }
359
+ }
360
+
361
+ async #assignAttributes(action, answer) {
362
+ winston.debug("DirAiPrompt assignAttributes action: ", action)
363
+ winston.debug("DirAiPrompt assignAttributes answer: " + answer)
364
+
365
+ if (this.context.tdcache) {
366
+ if (action.assignReplyTo && answer) {
367
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
368
+ }
369
+ }
370
+ }
371
+
372
+ async #executeIntent(destinationIntentId, callback) {
373
+ let intentDirective = null;
374
+ if (destinationIntentId) {
375
+ intentDirective = DirIntent.intentDirectiveFor(destinationIntentId, null);
376
+ }
377
+ if (intentDirective) {
378
+ this.logger.native("[AI Condition] executing destinationIntentId");
379
+ this.intentDir.execute(intentDirective, () => {
380
+ if (callback) {
381
+ callback();
382
+ }
383
+ })
384
+ }
385
+ else {
386
+ this.logger.native("[AI Condition] no block connected to intentId:", destinationIntentId);
387
+ winston.debug("[AI Condition] no block connected to intentId:" + destinationIntentId);
388
+ if (callback) {
389
+ callback();
390
+ }
391
+ }
392
+ }
393
+
394
+ async getKeyFromKbSettings() {
395
+ return new Promise((resolve) => {
396
+
397
+ const KB_HTTPREQUEST = {
398
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/kbsettings",
399
+ headers: {
400
+ 'Content-Type': 'application/json',
401
+ 'Authorization': 'JWT ' + this.context.token
402
+ },
403
+ method: "GET"
404
+ }
405
+ winston.debug("DirAiPrompt KB HttpRequest", KB_HTTPREQUEST);
406
+
407
+ httpUtils.request(
408
+ KB_HTTPREQUEST, async (err, resbody) => {
409
+ if (err) {
410
+ winston.error("(httprequest) DirAiPrompt Get KnowledgeBase err: " + err.message);
411
+ resolve(null);
412
+ } else {
413
+ if (!resbody.gptkey) {
414
+ resolve(null);
415
+ } else {
416
+ resolve(resbody.gptkey);
417
+ }
418
+ }
419
+ }
420
+ )
421
+ })
422
+ }
423
+
424
+ async checkQuoteAvailability() {
425
+ return new Promise((resolve) => {
426
+
427
+ const HTTPREQUEST = {
428
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/tokens",
429
+ headers: {
430
+ 'Content-Type': 'application/json',
431
+ 'Authorization': 'JWT ' + this.context.token
432
+ },
433
+ method: "GET"
434
+ }
435
+ winston.debug("DirAiPrompt check quote availability HttpRequest", HTTPREQUEST);
436
+
437
+ httpUtils.request(
438
+ HTTPREQUEST, async (err, resbody) => {
439
+ if (err) {
440
+ resolve(true)
441
+ } else {
442
+ if (resbody.isAvailable === true) {
443
+ resolve(true)
444
+ } else {
445
+ resolve(false)
446
+ }
447
+ }
448
+ }
449
+ )
450
+ })
451
+ }
452
+
453
+ async updateQuote(tokens_usage) {
454
+ return new Promise((resolve, reject) => {
455
+
456
+ const HTTPREQUEST = {
457
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/incr/tokens",
458
+ headers: {
459
+ 'Content-Type': 'application/json',
460
+ 'Authorization': 'JWT ' + this.context.token
461
+ },
462
+ json: tokens_usage,
463
+ method: "POST"
464
+ }
465
+ winston.debug("DirAiPrompt update quote HttpRequest", HTTPREQUEST);
466
+
467
+ httpUtils.request(
468
+ HTTPREQUEST, async (err, resbody) => {
469
+ if (err) {
470
+ winston.error("(httprequest) DirAiPrompt Increment tokens quote err: ", err);
471
+ reject(false)
472
+ } else {
473
+ winston.debug("(httprequest) DirAiPrompt Increment token quote resbody: ", resbody);
474
+ resolve(true);
475
+ }
476
+ }
477
+ )
478
+ })
479
+ }
480
+
481
+ }
482
+
483
+ module.exports = { DirAiCondition }
@@ -37,6 +37,7 @@ class Directives {
37
37
  static ASK_GPT_V2 = "askgptv2";
38
38
  static GPT_TASK = "gpt_task";
39
39
  static AI_PROMPT = "ai_prompt";
40
+ static AI_CONDITION = "ai_condition";
40
41
  /**** INTEGRATIONS ****/
41
42
  static QAPLA = 'qapla';
42
43
  static MAKE = 'make';
@@ -1025,6 +1025,20 @@ class TiledeskChatbotUtil {
1025
1025
  return userParams;
1026
1026
  }
1027
1027
 
1028
+ static AiConditionPromptBuilder(prompt_header, intents, instructions) {
1029
+ let conditions = "";
1030
+ intents.forEach( function(intent) {
1031
+ conditions += `- label: ${intent.label} When: ${intent.prompt}\n`
1032
+ });
1033
+
1034
+ instructions = instructions;
1035
+ let raw_condition_prompt = `${prompt_header}
1036
+
1037
+ ${conditions}
1038
+ ${instructions}`
1039
+ return raw_condition_prompt;
1040
+ }
1041
+
1028
1042
  /**
1029
1043
  * A stub to get the request parameters, hosted by tilebot on:
1030
1044
  * /${TILEBOT_ROUTE}/ext/parameters/requests/${requestId}?all
@@ -1093,6 +1107,8 @@ class TiledeskChatbotUtil {
1093
1107
  }
1094
1108
  });
1095
1109
  }
1110
+
1111
+
1096
1112
 
1097
1113
 
1098
1114
  }