@tiledesk/tiledesk-tybot-connector 0.2.48 → 0.2.50-rc1

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/CHANGELOG.md CHANGED
@@ -5,6 +5,13 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ # 0.2.50-rc1
9
+ - Added quotes for GPT actions
10
+
11
+ # v0.2.49
12
+ - resplacebotv2, added "/" + blockName (so the blockname will be implicitly executed), removing the need to specify the "/" in the body of the replacebot editor
13
+ - Never add "guest#" as lead userFullname
14
+
8
15
  # v0.2.48
9
16
  - resplacebotv2
10
17
  - fixed jsoncondition expression. added ? in some "static OPERATORS" (i.e. contains) to correctly evaluate undefined operands
@@ -282,11 +282,11 @@ class TiledeskExpression {
282
282
  const applyPattern = operator.applyPattern;
283
283
  let operand = operands[i + 1].isVariable ? TiledeskExpression.variableOperand(operands[i + 1].value) : TiledeskExpression.quotedString(operands[i + 1].value);
284
284
  operand = operands[i + 1].function ? TiledeskExpression.applyFunctionToOperand(operand, operands[i + 1].function) : operand;
285
- console.log("1. expression is:", expression)
286
- console.log("operand is:", operand)
285
+ // console.log("1. expression is:", expression)
286
+ // console.log("operand is:", operand)
287
287
 
288
288
  expression = applyPattern.replace("#1", expression).replace("#2", operand);
289
- console.log("2. expression is:", expression)
289
+ // console.log("2. expression is:", expression)
290
290
 
291
291
  }
292
292
  return expression;
@@ -458,22 +458,23 @@ class TiledeskChatbotUtil {
458
458
  if (message.request.lead.email) {
459
459
  await chatbot.addParameter("userEmail", message.request.lead.email);
460
460
  }
461
- if (message.request.lead.fullname) {
461
+ if (message.request.lead.fullname && !message.request.lead.fullname.startsWith("guest#")) {
462
+ // worth saving
462
463
  try {
463
- const current_userFullname = await chatbot.getParameter("userFullname");
464
- if (current_userFullname && current_userFullname.startsWith("guest#")) {
464
+ // const current_userFullname = await chatbot.getParameter("userFullname");
465
+ // if (current_userFullname && current_userFullname.startsWith("guest#")) { // replace if exists as guest#
466
+ // await chatbot.addParameter("userFullname", message.request.lead.fullname);
467
+ // }
468
+ // else if (!current_userFullname) {
465
469
  await chatbot.addParameter("userFullname", message.request.lead.fullname);
466
- }
467
- else if (!current_userFullname) {
468
- await chatbot.addParameter("userFullname", message.request.lead.fullname);
469
- }
470
+ // }
470
471
  }
471
472
  catch(error) {
472
473
  console.error("Error on setting userFullname:", error);
473
474
  }
474
475
  }
475
476
  else {
476
- console.log("!lead.fullname");
477
+ // console.log("!lead.fullname");
477
478
  }
478
479
  // console.log("Getting userPhone:", JSON.stringify(message.request));
479
480
  if (message.request.lead.phone) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.48",
3
+ "version": "0.2.50-rc1",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -42,6 +42,7 @@ class DirAskGPT {
42
42
  return;
43
43
  }
44
44
 
45
+ let publicKey = false;
45
46
  let trueIntent = action.trueIntent;
46
47
  let falseIntent = action.falseIntent;
47
48
  let trueIntentAttributes = action.trueIntentAttributes;
@@ -103,6 +104,7 @@ class DirAskGPT {
103
104
  if (!key) {
104
105
  if (this.log) { console.log("DirGptTask - Retrieve public gptkey")}
105
106
  key = process.env.GPTKEY;
107
+ publicKey = true;
106
108
  }
107
109
 
108
110
  if (!key) {
@@ -117,6 +119,15 @@ class DirAskGPT {
117
119
  return;
118
120
  }
119
121
 
122
+ if (publicKey === true) {
123
+ let keep_going = await this.checkQuoteAvailability(server_base_url);
124
+ if (keep_going === false) {
125
+ if (this.log) { console.log("DirGptTask - Quota exceeded for tokens. Skip the action")}
126
+ callback();
127
+ return;
128
+ }
129
+ }
130
+
120
131
  let json = {
121
132
  question: filled_question,
122
133
  kbid: action.kbid,
@@ -153,6 +164,12 @@ class DirAskGPT {
153
164
  }
154
165
  }
155
166
  else if (resbody.success === true) {
167
+
168
+ // if (publicKey === true) {
169
+ // let token_usage = resbody.usage.total_tokens;
170
+ // this.updateQuote(server_base_url, token_usage);
171
+ // }
172
+
156
173
  if (trueIntent) {
157
174
  await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
158
175
  callback(true);
@@ -42,6 +42,7 @@ class DirGptTask {
42
42
  return;
43
43
  }
44
44
 
45
+ let publicKey = false;
45
46
  let trueIntent = action.trueIntent;
46
47
  let falseIntent = action.falseIntent;
47
48
  let trueIntentAttributes = action.trueIntentAttributes;
@@ -93,7 +94,6 @@ class DirGptTask {
93
94
  }
94
95
 
95
96
  let key = await this.getKeyFromIntegrations(server_base_url);
96
-
97
97
  if (!key) {
98
98
  if (this.log) { console.log("DirGptTask - Key not found in Integrations. Searching in kb settings..."); }
99
99
  key = await this.getKeyFromKbSettings(server_base_url);
@@ -102,12 +102,12 @@ class DirGptTask {
102
102
  if (!key) {
103
103
  if (this.log) { console.log("DirGptTask - Retrieve public gptkey")}
104
104
  key = process.env.GPTKEY;
105
+ publicKey = true;
105
106
  }
106
107
 
107
108
  if (!key) {
108
109
  console.error("DirGptTask gptkey is mandatory");
109
110
  await this.#assignAttributes(action, answer);
110
-
111
111
  if (falseIntent) {
112
112
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
113
113
  callback(true);
@@ -117,6 +117,15 @@ class DirGptTask {
117
117
  return;
118
118
  }
119
119
 
120
+ if (publicKey === true) {
121
+ let keep_going = await this.checkQuoteAvailability(server_base_url);
122
+ if (keep_going === false) {
123
+ if (this.log) { console.log("DirGptTask - Quota exceeded for tokens. Skip the action")}
124
+ callback();
125
+ return;
126
+ }
127
+ }
128
+
120
129
  let json = {
121
130
  model: action.model,
122
131
  messages: [
@@ -168,6 +177,12 @@ class DirGptTask {
168
177
  // check if answer is a json
169
178
  let answer_json = await this.convertToJson(answer);
170
179
  await this.#assignAttributes(action, answer_json);
180
+
181
+ if (publicKey === true) {
182
+ let token_usage = resbody.usage.total_tokens;
183
+ this.updateQuote(server_base_url, token_usage);
184
+ }
185
+
171
186
  if (trueIntent) {
172
187
  await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
173
188
  callback(true);
@@ -245,9 +260,6 @@ class DirGptTask {
245
260
  if (action.assignReplyTo && answer) {
246
261
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
247
262
  }
248
- // if (action.assignSourceTo && source) {
249
- // await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignSourceTo, source);
250
- // }
251
263
  // Debug log
252
264
  if (this.log) {
253
265
  const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
@@ -366,6 +378,64 @@ class DirGptTask {
366
378
  })
367
379
  }
368
380
 
381
+ async checkQuoteAvailability(server_base_url) {
382
+ return new Promise((resolve) => {
383
+
384
+ const HTTPREQUEST = {
385
+ url: server_base_url + "/" + this.context.projectId + "/quotes/tokens",
386
+ headers: {
387
+ 'Content-Type': 'application/json',
388
+ 'Authorization': 'JWT ' + this.context.token
389
+ },
390
+ method: "GET"
391
+ }
392
+ if (this.log) { console.log("DirGptTask check quote availability HTTPREQUEST", HTTPREQUEST); }
393
+
394
+ this.#myrequest(
395
+ HTTPREQUEST, async (err, resbody) => {
396
+ if (err) {
397
+ console.error("(httprequest) DirGptTask Check quote availability err: ", err);
398
+ resolve(true)
399
+ } else {
400
+ if (resbody.isAvailable === true) {
401
+ resolve(true)
402
+ } else {
403
+ resolve(false)
404
+ }
405
+ }
406
+ }
407
+ )
408
+ })
409
+ }
410
+
411
+ async updateQuote(server_base_url, tokens) {
412
+ return new Promise((resolve) => {
413
+
414
+ const HTTPREQUEST = {
415
+ url: server_base_url + "/" + this.context.projectId + "/quotes/incr/tokens",
416
+ headers: {
417
+ 'Content-Type': 'application/json',
418
+ 'Authorization': 'JWT ' + this.context.token
419
+ },
420
+ json: { tokens: tokens },
421
+ method: "POST"
422
+ }
423
+ if (this.log) { console.log("DirGptTask check quote availability HTTPREQUEST", HTTPREQUEST); }
424
+
425
+ this.#myrequest(
426
+ HTTPREQUEST, async (err, resbody) => {
427
+ if (err) {
428
+ console.error("(httprequest) DirGptTask Increment tokens quote err: ", err);
429
+ rejects(false)
430
+ } else {
431
+ console.log("(httprequest) DirGptTask Increment token quote resbody: ", resbody);
432
+ resolve(true);
433
+ }
434
+ }
435
+ )
436
+ })
437
+ }
438
+
369
439
  }
370
440
 
371
441
  module.exports = { DirGptTask }
@@ -48,7 +48,7 @@ class DirReplaceBotV2 {
48
48
  if (this.log) {console.log("Sending hidden /start message to bot in dept");}
49
49
  const message = {
50
50
  type: "text",
51
- text: blockName,
51
+ text: "/" + blockName,
52
52
  attributes : {
53
53
  subtype: "info"
54
54
  }