@tiledesk/tiledesk-tybot-connector 0.2.36 → 0.2.38

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
+ # v0.2.38
9
+ - improved Qapla' action with condition
10
+
11
+ # v0.2.37
12
+ - improved GptTask action with condition
13
+ - improved AskGPT action with condition
14
+
8
15
  # v0.2.36
9
16
  - Fixes json conversion in GptTask issue
10
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.36",
3
+ "version": "0.2.38",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -619,9 +619,16 @@ class DirectivesChatbotPlug {
619
619
  });
620
620
  }
621
621
  else if (directive_name === Directives.QAPLA) {
622
- new DirQapla(context).execute(directive, async () => {
623
- let next_dir = await this.nextDirective(this.directives);
624
- this.process(next_dir);
622
+ new DirQapla(context).execute(directive, async (stop) => {
623
+ if (context.log) { console.log("DirQapla stop?", stop);}
624
+ if (stop == true) {
625
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
626
+ this.theend();
627
+ }
628
+ else {
629
+ let next_dir = await this.nextDirective(this.directives);
630
+ this.process(next_dir);
631
+ }
625
632
  })
626
633
  }
627
634
  else if (directive_name === Directives.MAKE) {
@@ -94,195 +94,78 @@ class DirAskGPT {
94
94
  console.log("DirAskGPT ApiEndpoint URL: ", pai_url);
95
95
  }
96
96
 
97
- const INTEGRATIONS_HTTPREQUEST = {
98
- url: server_base_url + "/" + this.context.projectId + "/integration/name/openai",
99
- headers: {
100
- 'Content-Type': 'application/json',
101
- 'Authorization': 'JWT ' + this.context.token
102
- },
103
- method: "GET"
97
+ let key = await this.getKeyFromIntegrations(server_base_url);
98
+ if (!key) {
99
+ if (this.log) { console.log("DirAskGPT - Key not found in Integrations. Searching in kb settings..."); }
100
+ key = await this.getKeyFromKbSettings(server_base_url);
104
101
  }
105
- if (this.log) { console.log("DirGptTask INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
102
+
103
+ if (!key) {
104
+ console.error("Error: DirAskGPT gptkey is mandatory");
105
+ await this.#assignAttributes(action, answer);
106
+ if (falseIntent) {
107
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
108
+ callback(true);
109
+ return;
110
+ }
111
+ callback();
112
+ return;
113
+ }
114
+
115
+ let json = {
116
+ question: filled_question,
117
+ kbid: action.kbid,
118
+ gptkey: key
119
+ };
120
+ if (this.log) { console.log("DirAskGPT json:", json); }
121
+
122
+ const HTTPREQUEST = {
123
+ url: pai_url,
124
+ json: json,
125
+ method: "POST"
126
+ }
127
+ if (this.log) { console.log("DirAskGPT HTTPREQUEST", HTTPREQUEST); }
106
128
 
107
129
  this.#myrequest(
108
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
130
+ HTTPREQUEST, async (err, resbody) => {
131
+ if (this.log && err) {
132
+ console.log("DirAskGPT error: ", err);
133
+ }
134
+ if (this.log) { console.log("DirAskGPT resbody:", resbody); }
135
+ let answer = resbody.answer;
136
+ let source = resbody.source_url;
137
+ await this.#assignAttributes(action, answer, source);
138
+
109
139
  if (err) {
110
140
  if (callback) {
111
- console.error("DirAskGPT get integrations error: ", err);
141
+ if (falseIntent) {
142
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
143
+ callback(true);
144
+ return;
145
+ }
112
146
  callback();
113
147
  return;
114
148
  }
115
- } else if (callback) {
116
- if (this.log) { console.log("DirAskGPT get integrations resbody: ", integration) };
117
-
118
- let key;
119
- if (integration &&
120
- integration.value) {
121
- key = integration.value.apikey;
149
+ }
150
+ else if (resbody.success === true) {
151
+ if (trueIntent) {
152
+ await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
153
+ callback(true);
154
+ return;
122
155
  }
123
-
124
- // key not present in integrations - for retro compatibility search in kbsettings
125
- if (!key) {
126
-
127
- if (this.log) { console.log("DirAskGPT - Key not found in Integrations. Searching in kb settings..."); }
128
-
129
- const KB_HTTPREQUEST = {
130
- url: server_base_url + "/" + this.context.projectId + "/kbsettings",
131
- headers: {
132
- 'Content-Type': 'application/json',
133
- 'Authorization': 'JWT ' + this.context.token
134
- },
135
- method: "GET"
136
- }
137
- if (this.log) { console.log("DirAskGPT KB_HTTPREQUEST", KB_HTTPREQUEST); }
138
-
139
- this.#myrequest(
140
- KB_HTTPREQUEST, async (err, resbody) => {
141
- if (this.log) { console.log("DirAskGPT get kbs resbody:", resbody); }
142
-
143
- if (err) {
144
- if (this.log) { console.error("DirAskGPT get kbs error:", err); }
145
- if (callback) {
146
- await this.#assignAttributes(action, answer, source);
147
- console.error("Error: DirAskGPT missing gptkey. Executing condition false...");
148
- if (falseIntent) {
149
- this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
150
- }
151
- callback(true);
152
- return;
153
- }
154
-
155
- } else if (callback) {
156
- if (this.log) { console.log("DirAskGPT gptkey: " + resbody.gptkey); }
157
-
158
- if (!resbody.gptkey) {
159
- await this.#assignAttributes(action, answer, source);
160
- console.error("Error: DirAskGPT missing gptkey. Executing condition false...");
161
- if (falseIntent) {
162
- this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
163
- }
164
- callback(true);
165
- return;
166
-
167
- } else {
168
-
169
- if (this.log) { console.log("DirAskGpt - Key found in KbSettings") };
170
-
171
- key = resbody.gptkey;
172
-
173
- let json = {
174
- question: filled_question,
175
- kbid: action.kbid,
176
- gptkey: key
177
- };
178
- if (this.log) { console.log("DirAskGPT json:", json); }
179
-
180
- const HTTPREQUEST = {
181
- url: pai_url,
182
- json: json,
183
- method: "POST"
184
- }
185
- if (this.log) { console.log("DirAskGPT HTTPREQUEST", HTTPREQUEST); }
186
-
187
- this.#myrequest(
188
- HTTPREQUEST, async (err, resbody) => {
189
- if (this.log && err) {
190
- console.log("DirAskGPT error: ", err);
191
- }
192
- if (this.log) { console.log("DirAskGPT resbody:", resbody); }
193
- let answer = resbody.answer;
194
- let source = resbody.source_url;
195
- await this.#assignAttributes(action, answer, source);
196
-
197
- if (err) {
198
- if (callback) {
199
- if (falseIntent) {
200
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
201
- }
202
- callback(true);
203
- return;
204
- }
205
- }
206
- else if (resbody.success === true) {
207
- if (trueIntent) {
208
- await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
209
- }
210
- callback(); // se la condition è true si deve ritornare true nella callback ugualmente?
211
- return;
212
- } else {
213
- if (falseIntent) {
214
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
215
- }
216
- callback(true);
217
- return;
218
- }
219
- }
220
- )
221
- }
222
- }
223
- }
224
- )
225
-
226
- } else {
227
-
228
- if (this.log) { console.log("DirGptTask - Key found in Integrations") };
229
-
230
- let json = {
231
- question: filled_question,
232
- kbid: action.kbid,
233
- gptkey: key
234
- };
235
- if (this.log) { console.log("DirAskGPT json:", json); }
236
-
237
- const HTTPREQUEST = {
238
- url: pai_url,
239
- json: json,
240
- method: "POST"
241
- }
242
- if (this.log) { console.log("DirAskGPT HTTPREQUEST", HTTPREQUEST); }
243
-
244
- this.#myrequest(
245
- HTTPREQUEST, async (err, resbody) => {
246
- if (this.log && err) {
247
- console.log("DirAskGPT error: ", err);
248
- }
249
- if (this.log) { console.log("DirAskGPT resbody:", resbody); }
250
- let answer = resbody.answer;
251
- let source = resbody.source_url;
252
- await this.#assignAttributes(action, answer, source);
253
-
254
- if (err) {
255
- if (callback) {
256
- if (falseIntent) {
257
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
258
- }
259
- callback(true);
260
- return;
261
- }
262
- }
263
- else if (resbody.success === true) {
264
- if (trueIntent) {
265
- await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
266
- }
267
- callback(); // se la condition è true si deve ritornare true nella callback ugualmente?
268
- return;
269
- } else {
270
- if (falseIntent) {
271
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
272
- }
273
- callback(true);
274
- return;
275
- }
276
- }
277
- )
278
-
156
+ callback();
157
+ return;
158
+ } else {
159
+ if (falseIntent) {
160
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
161
+ callback(true);
162
+ return;
279
163
  }
280
-
164
+ callback();
165
+ return;
281
166
  }
282
167
  }
283
168
  )
284
-
285
-
286
169
  }
287
170
 
288
171
  async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
@@ -400,6 +283,68 @@ class DirAskGPT {
400
283
  });
401
284
  }
402
285
 
286
+ async getKeyFromIntegrations(server_base_url) {
287
+ return new Promise((resolve) => {
288
+
289
+ const INTEGRATIONS_HTTPREQUEST = {
290
+ url: server_base_url + "/" + this.context.projectId + "/integration/name/openai",
291
+ headers: {
292
+ 'Content-Type': 'application/json',
293
+ 'Authorization': 'JWT ' + this.context.token
294
+ },
295
+ method: "GET"
296
+ }
297
+ if (this.log) { console.log("DirGptTask INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
298
+
299
+ this.#myrequest(
300
+ INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
301
+ if (err) {
302
+ console.error("DirAskGPT Get integrations error ", JSON.stringify(err));
303
+ resolve(null);
304
+ } else {
305
+
306
+ if (integration &&
307
+ integration.value) {
308
+ resolve(integration.value.apikey)
309
+ }
310
+ else {
311
+ resolve(null)
312
+ }
313
+ }
314
+ })
315
+ })
316
+ }
317
+
318
+ async getKeyFromKbSettings(server_base_url) {
319
+ return new Promise((resolve) => {
320
+
321
+ const KB_HTTPREQUEST = {
322
+ url: server_base_url + "/" + this.context.projectId + "/kbsettings",
323
+ headers: {
324
+ 'Content-Type': 'application/json',
325
+ 'Authorization': 'JWT ' + this.context.token
326
+ },
327
+ method: "GET"
328
+ }
329
+ if (this.log) { console.log("DirGptTask KB_HTTPREQUEST", KB_HTTPREQUEST); }
330
+
331
+ this.#myrequest(
332
+ KB_HTTPREQUEST, async (err, resbody) => {
333
+ if (err) {
334
+ console.error("DirGptTask Get kb settings error ", JSON.stringify(err));
335
+ resolve(null);
336
+ } else {
337
+ if (!resbody.gptkey) {
338
+ resolve(null);
339
+ } else {
340
+ resolve(resbody.gptkey);
341
+ }
342
+ }
343
+ }
344
+ )
345
+ })
346
+ }
347
+
403
348
  }
404
349
 
405
350
  module.exports = { DirAskGPT }