@tiledesk/tiledesk-tybot-connector 0.2.107 → 0.2.109

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
@@ -17,6 +17,15 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
+ # v0.2.109 -> test
21
+ - Fixed bug: GptTask doesn't work properly with trascript
22
+
23
+ # v0.2.108 -> test
24
+ - Added DirUnassign
25
+ - Added chatbot_id
26
+ - Added support for citations in AskGPTV2
27
+ - Added support Json type in GPT Task
28
+
20
29
  # v0.2.107 -> production
21
30
  - clean logs (more)
22
31
 
package/index.js CHANGED
@@ -467,6 +467,7 @@ router.get('/ext/parameters/requests/:requestid', async (req, res) => {
467
467
  else {
468
468
  const RESERVED = [
469
469
  TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY,
470
+ TiledeskChatbotConst.REQ_CHATBOT_ID_KEY,
470
471
  TiledeskChatbotConst.REQ_CHAT_URL,
471
472
  TiledeskChatbotConst.REQ_CITY_KEY,
472
473
  TiledeskChatbotConst.REQ_COUNTRY_KEY,
@@ -4,6 +4,7 @@ class TiledeskChatbotConst {
4
4
  static REQ_PROJECT_ID_KEY = "project_id";
5
5
  static REQ_REQUEST_ID_KEY = "conversation_id";
6
6
  static REQ_CHATBOT_NAME_KEY = "chatbot_name";
7
+ static REQ_CHATBOT_ID_KEY = "chatbot_id";
7
8
  static REQ_LAST_USER_TEXT_KEY = "last_user_text";
8
9
  static REQ_LAST_USER_TEXT_v2_KEY = "lastUserText";
9
10
  static REQ_LAST_MESSAGE_ID_KEY = "last_message_id";
@@ -31,24 +32,7 @@ class TiledeskChatbotConst {
31
32
  static REQ_USER_COMPANY_KEY = "userCompany";
32
33
  static REQ_TICKET_ID_KEY = "ticketId";
33
34
  static REQ_CHATBOT_TOKEN = "chatbotToken";
34
-
35
-
36
- // static REQ_DEPARTMENT_ID_KEY = "tdDepartmentId";
37
- // static REQ_PROJECT_ID_KEY = "projectId";
38
- // static REQ_REQUEST_ID_KEY = "tdRequestId";
39
- // static REQ_CHATBOT_NAME_KEY = "tdChatbotName";
40
- // static REQ_LAST_USER_TEXT_KEY = "tdLastUserText";
41
- // static REQ_LAST_MESSAGE_ID_KEY = "tdLastMessageId";
42
- // static REQ_COUNTRY_KEY = "tdCountry";
43
- // static REQ_CITY_KEY = "tdCity";
44
- // static REQ_USER_SOURCE_PAGE_KEY = "tdUserSourcePage";
45
- // static REQ_USER_LANGUAGE_KEY = "tdUserLanguage";
46
- // static REQ_USER_AGENT_KEY = "tdUserAgent";
47
- // static REQ_DEPARTMENT_NAME_KEY = "tdDepartmentName";
48
- // static REQ_END_USER_ID_KEY = "tdEndUserId";
49
- // static REQ_END_USER_IP_ADDRESS_KEY = "tdEndUserIpAddress";
50
- // static REQ_CHAT_URL = "chat_url";
51
-
35
+ static REQ_CHATBOT_TOKEN_v2 = "chatbot_jwt_token";
52
36
  }
53
37
 
54
38
  module.exports = { TiledeskChatbotConst };
@@ -511,14 +511,16 @@ class TiledeskChatbotUtil {
511
511
  await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
512
512
  if (chatbot.bot) {
513
513
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
514
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_ID_KEY, chatbot.bot._id);
514
515
  }
515
516
  if (chatbotToken) {
516
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken);
517
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
518
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
517
519
  }
518
520
 
519
521
  if (message.text && message.sender !== "_tdinternal") {
520
522
  // await chatbot.addParameter(TiledeskChatbotConst.USER_INPUT, true); // set userInput
521
- await chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT); // set userInput
523
+ await chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT); // user wrote, delete userInput, replyv2 will not trigger timeout action
522
524
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
523
525
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
524
526
  if (message.channel) {
@@ -888,7 +890,8 @@ class TiledeskChatbotUtil {
888
890
  "lastUserText",
889
891
  TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY,
890
892
  TiledeskChatbotConst.USER_INPUT,
891
- TiledeskChatbotConst.REQ_CHATBOT_TOKEN
893
+ TiledeskChatbotConst.REQ_CHATBOT_TOKEN,
894
+ TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2,
892
895
  ]
893
896
  let userParams = {};
894
897
  if (flowAttributes) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.107",
3
+ "version": "0.2.109",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -51,6 +51,7 @@ const { DirReplyV2 } = require('./directives/DirReplyV2');
51
51
  const { DirIfOnlineAgentsV2 } = require('./directives/DirIfOnlineAgentsV2');
52
52
  const { DirContactUpdate } = require('./directives/DirContactUpdate');
53
53
  const { DirClearTranscript } = require('./directives/DirClearTranscript');
54
+ const { DirMoveToUnassigned } = require('./directives/DirMoveToUnassigned');
54
55
 
55
56
  class DirectivesChatbotPlug {
56
57
 
@@ -698,6 +699,13 @@ class DirectivesChatbotPlug {
698
699
  this.process(next_dir);
699
700
  });
700
701
  }
702
+ else if (directive_name === Directives.MOVE_TO_UNASSIGNED) {
703
+ console.log("...MOVE_TO_UNASSIGNED");
704
+ new DirMoveToUnassigned(context).execute(directive, async () => {
705
+ let next_dir = await this.nextDirective(this.directives);
706
+ this.process(next_dir);
707
+ });
708
+ }
701
709
  else {
702
710
  //console.log("Unhandled Post-message Directive:", directive_name);
703
711
  let next_dir = await this.nextDirective(this.directives);
@@ -66,6 +66,7 @@ class DirAskGPTV2 {
66
66
  let max_tokens;
67
67
  let top_k;
68
68
  let transcript;
69
+ let citations = false;
69
70
  //let default_context = "You are an helpful assistant for question-answering tasks.\nUse ONLY the following pieces of retrieved context to answer the question.\nIf you don't know the answer, just say that you don't know.\nIf none of the retrieved context answer the question, add this word to the end <NOANS>\n\n{context}";
70
71
 
71
72
  let contexts = {
@@ -107,6 +108,10 @@ class DirAskGPTV2 {
107
108
  max_tokens = action.max_tokens;
108
109
  }
109
110
 
111
+ if (action.citations) {
112
+ citations = action.citations;
113
+ }
114
+
110
115
  let requestVariables = null;
111
116
  requestVariables =
112
117
  await TiledeskChatbot.allParametersStatic(
@@ -194,7 +199,8 @@ class DirAskGPTV2 {
194
199
  question: filled_question,
195
200
  gptkey: key,
196
201
  namespace: namespace,
197
- model: model
202
+ model: model,
203
+ citations: citations
198
204
  };
199
205
  if (top_k) {
200
206
  json.top_k = top_k;
@@ -8,15 +8,16 @@ class DirClose {
8
8
  this.context = context;
9
9
  this.tdclient = context.tdclient;
10
10
  this.requestId = context.requestId;
11
+ this.chatbot = context.chatbot;
11
12
  }
12
13
 
13
14
  execute(directive, callback) {
14
- this.tdclient.closeRequest(this.requestId, (err) => {
15
+ this.tdclient.closeRequest(this.requestId, async (err) => {
15
16
  if (err) {
16
17
  console.error("Error in 'close directive':", err);
17
18
  }
18
19
  else {
19
- // console.log("Successfully closed on close()");
20
+ await this.chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT);
20
21
  }
21
22
  callback();
22
23
  });
@@ -145,6 +145,8 @@ class DirGptTask {
145
145
  let keep_going = await this.checkQuoteAvailability(server_base_url);
146
146
  if (keep_going === false) {
147
147
  if (this.log) { console.log("DirGptTask - Quota exceeded for tokens. Skip the action")}
148
+ await this.chatbot.addParameter("flowError", "GPT Error: tokens quota exceeded");
149
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
148
150
  callback();
149
151
  return;
150
152
  }
@@ -153,12 +155,6 @@ class DirGptTask {
153
155
  let json = {
154
156
  model: action.model,
155
157
  messages: [],
156
- // messages: [
157
- // {
158
- // role: "user",
159
- // content: filled_question
160
- // }
161
- // ],
162
158
  max_tokens: action.max_tokens,
163
159
  temperature: action.temperature,
164
160
  }
@@ -175,11 +171,18 @@ class DirGptTask {
175
171
  json.messages.push(message)
176
172
  }
177
173
  })
174
+ json.messages.push({ role: "user", content: filled_question });
178
175
  } else {
179
176
  let message = { role: "user", content: filled_question };
180
177
  json.messages.push(message);
181
178
  }
182
179
 
180
+ if (action.formatType && action.formatType !== 'none') {
181
+ json.response_format = {
182
+ type: action.formatType
183
+ }
184
+ }
185
+
183
186
  if (this.log) { console.log("DirGptTask json: ", json) }
184
187
 
185
188
  const HTTPREQUEST = {
@@ -211,9 +214,12 @@ class DirGptTask {
211
214
  } else {
212
215
  if (this.log) { console.log("DirGptTask resbody: ", JSON.stringify(resbody)); }
213
216
  answer = resbody.choices[0].message.content;
214
- // check if answer is a json
215
- let answer_json = await this.convertToJson(answer);
216
- await this.#assignAttributes(action, answer_json);
217
+
218
+ if (action.formatType === 'json_object' || action.formatType === undefined || action.formatType === null) {
219
+ answer = await this.convertToJson(answer);
220
+ }
221
+
222
+ await this.#assignAttributes(action, answer);
217
223
 
218
224
  if (publicKey === true) {
219
225
  let tokens_usage = {
@@ -471,7 +477,7 @@ class DirGptTask {
471
477
  console.error("(httprequest) DirGptTask Increment tokens quote err: ", err);
472
478
  rejects(false)
473
479
  } else {
474
- console.log("(httprequest) DirGptTask Increment token quote resbody: ", resbody);
480
+ if (this.log) { console.log("(httprequest) DirGptTask Increment token quote resbody: ", resbody); }
475
481
  resolve(true);
476
482
  }
477
483
  }
@@ -10,106 +10,29 @@ class DirMoveToAgent {
10
10
  throw new Error('context object is mandatory.');
11
11
  }
12
12
  this.context = context;
13
- // let context = {
14
- // projectId: projectId,
15
- // token: token,
16
- // supportRequest: supportRequest,
17
- // requestId: supportRequest.request_id,
18
- // TILEDESK_APIURL: API_URL,
19
- // TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
20
- // departmentId: depId,
21
- // tdcache: tdcache,
22
- // log: false
23
- // }
24
- // new TiledeskClient({
25
- // projectId: context.projectId,
26
- // token: context.token,
27
- // APIURL: context.TILEDESK_APIURL,
28
- // APIKEY: "___",
29
- // log: context.log
30
- // });
31
13
  this.tdclient = context.tdclient;
32
14
  this.tdcache = context.tdcache;
33
15
  this.requestId = context.requestId;
34
- // this.depId = context.departmentId;
35
16
  this.log = context.log;
36
17
  }
37
18
 
38
19
  execute(directive, callback) {
39
20
  directive.action = {};
40
- // if (directive.name === Directives.WHEN_ONLINE_MOVE_TO_AGENT) { // TEMP
41
- // directive.action = {
42
- // whenOnlineOnly: true
43
- // }
44
- // }
45
21
  this.go(directive.action, () => {
46
22
  callback();
47
23
  });
48
24
  }
49
25
 
50
26
  async go(action, callback) {
51
- // let depId = null;
52
- // if (this.log) {console.log("DirMoveToAgent this.context.departmentId:", this.context.departmentId);}
53
- // if (this.context.departmentId) {
54
- // depId = this.context.departmentId
55
- // if (this.log) {console.log("DirMoveToAgent depId:", depId);}
56
- // }
57
- // else if (this.tdcache) {
58
- // depId =
59
- // await TiledeskChatbot.getParameterStatic(
60
- // this.tdcache, this.requestId, TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY
61
- // );
62
- // if (this.log) {console.log("DirMoveToAgent depId (cache):", depId);}
63
- // }
64
- // if (this.log) {console.log("DirMoveToAgent anyway depId is:", depId);}
65
- // if (action.whenOnlineOnly === true) {
66
- // this.tdclient.openNow( async (err, result) => {
67
- // if (err) {
68
- // console.error("Agent in DirOfflineHours Error:", err);
69
- // callback();
70
- // }
71
- // else {
72
- // if (result && result.isopen) {
73
- // // if (depId) {
74
- // // this.tdclient.agent(this.requestId, depId, (err) => {
75
- // this.tdclient.moveToAgent(this.requestId, (err) => {
76
- // if (err) {
77
- // console.error("Error moving to agent during online hours:", err);
78
- // }
79
- // else {
80
- // console.log("Successfully moved to agent during online hours");
81
- // }
82
- // callback();
83
- // });
84
- // // }
85
- // // else {
86
- // // callback();
87
- // // }
88
- // }
89
- // else {
90
- // callback();
91
- // }
92
- // }
93
- // });
94
- // }
95
- // else {
96
- // if (depId) {
97
- // this.tdclient.agent(this.requestId, depId, (err) => {
98
- this.tdclient.moveToAgent(this.requestId, (err) => {
99
- if (err) {
100
- console.error("Error moving to agent:", err);
101
- }
102
- else {
103
- // console.log("Successfully moved to agent");
104
- }
105
- callback();
106
- });
107
- // }
108
- // else {
109
- // callback();
110
- // }
111
-
112
- // }
27
+ this.tdclient.moveToAgent(this.requestId, (err) => {
28
+ if (err) {
29
+ console.error("Error moving to agent:", err);
30
+ }
31
+ else {
32
+ // console.log("Successfully moved to agent");
33
+ }
34
+ callback();
35
+ });
113
36
  }
114
37
 
115
38
  }
@@ -0,0 +1,50 @@
1
+ // const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
+ const { Directives } = require('./Directives');
3
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
4
+ const { TiledeskChatbotConst } = require('../../models/TiledeskChatbotConst');
5
+
6
+ class DirMoveToUnassigned {
7
+
8
+ constructor(context) {
9
+ if (!context) {
10
+ throw new Error('context object is mandatory.');
11
+ }
12
+ this.context = context;
13
+ this.tdclient = context.tdclient;
14
+ this.requestId = context.requestId;
15
+ this.log = context.log;
16
+ }
17
+
18
+ execute(directive, callback) {
19
+ directive.action = {};
20
+ this.go(directive.action, () => {
21
+ callback();
22
+ });
23
+ }
24
+
25
+ async go(action, callback) {
26
+ this.moveToUnassigned(this.requestId, (err) => {
27
+ if (err) {
28
+ console.error("Error moving to unassigned:", err);
29
+ }
30
+ else {
31
+ // console.log("Successfully moved to unassigned");
32
+ }
33
+ callback();
34
+ });
35
+ }
36
+
37
+ async moveToUnassigned(requestId, callback) {
38
+ const empty_participants = [] // STATUS WILL MOVE AUTOMATICALLY TO UNASSIGNED (100)
39
+ this.tdclient.updateRequestParticipants(requestId, empty_participants, (err) => {
40
+ if (callback) {
41
+ callback(err);
42
+ }
43
+ });
44
+ }
45
+
46
+
47
+
48
+ }
49
+
50
+ module.exports = { DirMoveToUnassigned };
@@ -51,6 +51,7 @@ class Directives {
51
51
  static IF_ONLINE_AGENTS_V2 = "ifonlineagentsv2";
52
52
  static CONTACT_UPDATE = "leadupdate";
53
53
  static CLEAR_TRANSCRIPT = "clear_transcript";
54
+ static MOVE_TO_UNASSIGNED = "move_to_unassigned";
54
55
 
55
56
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
56
57
  // static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace