@tiledesk/tiledesk-tybot-connector 0.2.109 → 0.2.111

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,11 +17,18 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
+ # v0.2.111 -> test
21
+ - Updated DirAskGPTV2 with engine support
22
+
23
+ # v0.2.110 -> test
24
+ - Added DirMoveToUnassigned
25
+ - Added DirConnectBlock
26
+
20
27
  # v0.2.109 -> test
28
+ - Added DirMoveToUnassigned
21
29
  - Fixed bug: GptTask doesn't work properly with trascript
22
30
 
23
31
  # v0.2.108 -> test
24
- - Added DirUnassign
25
32
  - Added chatbot_id
26
33
  - Added support for citations in AskGPTV2
27
34
  - Added support Json type in GPT Task
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.109",
3
+ "version": "0.2.111",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -706,6 +706,13 @@ class DirectivesChatbotPlug {
706
706
  this.process(next_dir);
707
707
  });
708
708
  }
709
+ else if (directive_name === Directives.CONNECT_BLOCK) {
710
+ // console.log(".....DirIntent")
711
+ new DirConnectBlock(context).execute(directive, async () => {
712
+ let next_dir = await this.nextDirective(this.directives);
713
+ this.process(next_dir);
714
+ });
715
+ }
709
716
  else {
710
717
  //console.log("Unhandled Post-message Directive:", directive_name);
711
718
  let next_dir = await this.nextDirective(this.directives);
@@ -67,6 +67,7 @@ class DirAskGPTV2 {
67
67
  let top_k;
68
68
  let transcript;
69
69
  let citations = false;
70
+ let engine;
70
71
  //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}";
71
72
 
72
73
  let contexts = {
@@ -174,15 +175,23 @@ class DirAskGPTV2 {
174
175
  return;
175
176
  }
176
177
  }
178
+ let ns;
177
179
 
178
-
179
180
  if (action.namespaceAsName) {
180
181
  // Namespace could be an attribute
181
182
  const filled_namespace = filler.fill(action.namespace, requestVariables)
182
-
183
183
  // namespace = await this.getNamespaceIdFromName(server_base_url, action.namespace)
184
- namespace = await this.getNamespaceIdFromName(server_base_url, filled_namespace);
184
+ ns = await this.getNamespace(server_base_url, filled_namespace, null);
185
+ namespace = ns.id;
185
186
  if (this.log) { console.log("DirAskGPT - Retrieved namespace id from name ", namespace); }
187
+ } else {
188
+ ns = await this.getNamespace(server_base_url, null, namespace);
189
+ }
190
+
191
+ if (ns.engine) {
192
+ engine = ns.engine;
193
+ } else {
194
+ engine = await this.setDefaultEngine()
186
195
  }
187
196
 
188
197
  if (!namespace) {
@@ -200,7 +209,8 @@ class DirAskGPTV2 {
200
209
  gptkey: key,
201
210
  namespace: namespace,
202
211
  model: model,
203
- citations: citations
212
+ citations: citations,
213
+ engine: engine
204
214
  };
205
215
  if (top_k) {
206
216
  json.top_k = top_k;
@@ -575,9 +585,8 @@ class DirAskGPTV2 {
575
585
  return objectTranscript;
576
586
  }
577
587
 
578
- async getNamespaceIdFromName(server_base_url, name) {
588
+ async getNamespace(server_base_url, name, id) {
579
589
  return new Promise((resolve) => {
580
-
581
590
  const HTTPREQUEST = {
582
591
  url: server_base_url + "/" + this.context.projectId + "/kb/namespace/all",
583
592
  headers: {
@@ -587,7 +596,6 @@ class DirAskGPTV2 {
587
596
  method: "GET"
588
597
  }
589
598
  if (this.log) { console.log("DirAskGPT get all namespaces HTTPREQUEST", HTTPREQUEST); }
590
-
591
599
  this.#myrequest(
592
600
  HTTPREQUEST, async (err, namespaces) => {
593
601
  if (err) {
@@ -595,18 +603,65 @@ class DirAskGPTV2 {
595
603
  resolve(null)
596
604
  } else {
597
605
  if (this.log) { console.log("(httprequest) DirAskGPT get all namespaces resbody: ", namespaces); }
606
+ if (name) {
607
+ let namespace = namespaces.find(n => n.name === name);
608
+ resolve(namespace);
609
+ } else {
610
+ let namespace = namespaces.find(n => n.id === id);
611
+ resolve(namespace);
612
+ }
598
613
 
599
- let namespace = namespaces.find(n => n.name === name);
600
- let namespace_id = namespace.id;
601
-
602
- resolve(namespace_id);
603
614
  }
604
615
  }
605
616
  )
606
617
  })
618
+ }
607
619
 
620
+ async setDefaultEngine() {
621
+ return new Promise((resolve) => {
622
+ let engine = {
623
+ name: "pinecone",
624
+ type: "pod",
625
+ apikey: "",
626
+ vector_size: 1536,
627
+ index_name: "example-index"
628
+ }
629
+ resolve(engine);
630
+ })
608
631
  }
609
632
 
633
+ // async getNamespaceIdFromName(server_base_url, name) {
634
+ // return new Promise((resolve) => {
635
+
636
+ // const HTTPREQUEST = {
637
+ // url: server_base_url + "/" + this.context.projectId + "/kb/namespace/all",
638
+ // headers: {
639
+ // 'Content-Type': 'application/json',
640
+ // 'Authorization': 'JWT ' + this.context.token
641
+ // },
642
+ // method: "GET"
643
+ // }
644
+ // if (this.log) { console.log("DirAskGPT get all namespaces HTTPREQUEST", HTTPREQUEST); }
645
+
646
+ // this.#myrequest(
647
+ // HTTPREQUEST, async (err, namespaces) => {
648
+ // if (err) {
649
+ // console.error("(httprequest) DirAskGPT get all namespaces err: ", err);
650
+ // resolve(null)
651
+ // } else {
652
+ // if (this.log) { console.log("(httprequest) DirAskGPT get all namespaces resbody: ", namespaces); }
653
+
654
+ // let namespace = namespaces.find(n => n.name === name);
655
+ // let namespace_id = namespace.id;
656
+
657
+ // resolve(namespace_id);
658
+ // }
659
+ // }
660
+ // )
661
+ // })
662
+
663
+ // }
664
+
610
665
 
611
666
  }
612
667
 
@@ -0,0 +1,202 @@
1
+ //const { ExtApi } = require('../../ExtApi.js');
2
+ let axios = require('axios');
3
+ let https = require("https");
4
+ const { v4: uuidv4 } = require('uuid');
5
+ const ms = require('minimist-string');
6
+
7
+ class DirConnectBlock {
8
+ constructor(context) {
9
+ if (!context) {
10
+ throw new Error('context object is mandatory.');
11
+ }
12
+ this.context = context;
13
+ this.API_ENDPOINT = context.TILEDESK_APIURL,
14
+ this.TILEBOT_ENDPOINT = context.TILEBOT_ENDPOINT;
15
+ this.supportRequest = context.supportRequest;
16
+ this.token = context.token;
17
+ this.log = context.log;
18
+ // let context = {
19
+ // projectId: projectId,
20
+ // token: token,
21
+ // supportRequest: supportRequest,
22
+ // requestId: supportRequest.request_id,
23
+ // TILEDESK_APIURL: API_URL,
24
+ // TILEBOT_ENDPOINT: TILEBOT_ENDPOINT,
25
+ // departmentId: depId,
26
+ // tdcache: tdcache,
27
+ // tdclient: tdclient,
28
+ // log: true
29
+ // }
30
+ }
31
+
32
+ execute(directive, callback) {
33
+ let action;
34
+ if (directive.action) {
35
+ action = directive.action;
36
+ }
37
+ else {
38
+ console.error("Incorrect DirConnectBlock directive:", directive);
39
+ callback();
40
+ return;
41
+ }
42
+ this.go(action, () => {
43
+ callback();
44
+ });
45
+ }
46
+
47
+ go(action, callback) {
48
+ const intentName = action.intentName;
49
+ const projectId = this.supportRequest.id_project;
50
+ const requestId = this.supportRequest.request_id;
51
+ const botId = this.supportRequest.bot_id;
52
+ let intent_command;
53
+ if (intentName) {
54
+ intent_command = "/" + intentName;
55
+ }
56
+ else {
57
+ callback();
58
+ return;
59
+ }
60
+
61
+ let intent_command_request = {
62
+ "payload": {
63
+ "_id": uuidv4(),
64
+ "senderFullname": "_tdinternal",
65
+ "type": "text",
66
+ "sender": "_tdinternal",
67
+ "recipient": requestId,
68
+ "text": intent_command,
69
+ "id_project": projectId,
70
+ "request": {
71
+ "request_id": requestId,
72
+ "id_project": projectId
73
+ }
74
+ },
75
+ "token": this.token
76
+ }
77
+ if (this.log) {console.log("move to intent message:", intent_command_request);}
78
+ let TILEBOT_ENDPOINT;
79
+ if (this.TILEBOT_ENDPOINT) {
80
+ TILEBOT_ENDPOINT = this.TILEBOT_ENDPOINT;
81
+ }
82
+ else {
83
+ TILEBOT_ENDPOINT = `${this.API_ENDPOINT}/modules/tilebot`
84
+ }
85
+ this.sendMessageToBot(TILEBOT_ENDPOINT, intent_command_request, botId, () => {
86
+ callback();
87
+ });
88
+ }
89
+
90
+ static intentDirectiveFor(intent, json_params) {
91
+ let string_params = null;
92
+ if (json_params) {
93
+ try {
94
+ string_params = JSON.stringify(json_params);
95
+ }
96
+ catch (error) {
97
+ console.error("Error stringigying JSON PARAMS", json_params);
98
+ }
99
+ }
100
+ if (string_params != null) {
101
+ intent += string_params
102
+ }
103
+ let intentDirective = {
104
+ action: {
105
+ intentName: intent
106
+ }
107
+ }
108
+ return intentDirective;
109
+ }
110
+
111
+ static fullIntentDirectiveFor(intent, json_params) {
112
+ let string_params = JSON.stringify(params);
113
+ let intentDirective = {
114
+ action: {
115
+ intentName: intent
116
+ }
117
+ }
118
+ return intentDirective;
119
+ }
120
+
121
+ /**
122
+ * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
123
+ * /${TILEBOT_ROUTE}/ext/${botId}
124
+ *
125
+ * @param {Object} message. The message to send
126
+ * @param {string} botId. Tiledesk botId
127
+ * @param {string} token. User token
128
+ */
129
+ sendMessageToBot(CHATBOT_ENDPOINT, message, botId, callback) {
130
+ // const jwt_token = this.fixToken(token);
131
+ const url = `${CHATBOT_ENDPOINT}/ext/${botId}`;
132
+ // console.log("sendMessageToBot URL", url);
133
+ const HTTPREQUEST = {
134
+ url: url,
135
+ headers: {
136
+ 'Content-Type' : 'application/json'
137
+ },
138
+ json: message,
139
+ method: 'POST'
140
+ };
141
+ this.myrequest(
142
+ HTTPREQUEST,
143
+ function(err, resbody) {
144
+ if (err) {
145
+ if (callback) {
146
+ callback(err);
147
+ }
148
+ }
149
+ else {
150
+ if (callback) {
151
+ callback(null, resbody);
152
+ }
153
+ }
154
+ }, false
155
+ );
156
+ }
157
+
158
+ myrequest(options, callback, log) {
159
+ if (this.log) {
160
+ console.log("API URL:", options.url);
161
+ console.log("** Options:", JSON.stringify(options));
162
+ }
163
+ let axios_options = {
164
+ url: options.url,
165
+ method: options.method,
166
+ data: options.json,
167
+ params: options.params,
168
+ headers: options.headers
169
+ }
170
+ if (options.url.startsWith("https:")) {
171
+ const httpsAgent = new https.Agent({
172
+ rejectUnauthorized: false,
173
+ });
174
+ axios_options.httpsAgent = httpsAgent;
175
+ }
176
+ axios(axios_options)
177
+ .then((res) => {
178
+ if (this.log) {
179
+ console.log("Response for url:", options.url);
180
+ console.log("Response headers:\n", JSON.stringify(res.headers));
181
+ }
182
+ if (res && res.status == 200 && res.data) {
183
+ if (callback) {
184
+ callback(null, res.data);
185
+ }
186
+ }
187
+ else {
188
+ if (callback) {
189
+ callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
190
+ }
191
+ }
192
+ })
193
+ .catch( (error) => {
194
+ console.error("An error occurred:", error);
195
+ if (callback) {
196
+ callback(error, null, null);
197
+ }
198
+ });
199
+ }
200
+ }
201
+
202
+ module.exports = { DirIntent };
@@ -52,6 +52,7 @@ class Directives {
52
52
  static CONTACT_UPDATE = "leadupdate";
53
53
  static CLEAR_TRANSCRIPT = "clear_transcript";
54
54
  static MOVE_TO_UNASSIGNED = "move_to_unassigned";
55
+ static CONNECT_BLOCK = "connect_block";
55
56
 
56
57
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
57
58
  // static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace