@tiledesk/tiledesk-tybot-connector 0.2.36 → 0.2.37

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.
@@ -92,217 +92,88 @@ class DirGptTask {
92
92
  console.log("DirGptTask openai_url ", openai_url);
93
93
  }
94
94
 
95
- const INTEGRATIONS_HTTPREQUEST = {
96
- url: server_base_url + "/" + this.context.projectId + "/integration/name/openai",
95
+ let key = await this.getKeyFromIntegrations(server_base_url);
96
+
97
+ if (!key) {
98
+ if (this.log) { console.log("DirGptTask - Key not found in Integrations. Searching in kb settings..."); }
99
+ key = await this.getKeyFromKbSettings(server_base_url);
100
+ }
101
+
102
+ if (!key) {
103
+ console.error("DirGptTask gptkey is mandatory");
104
+ await this.#assignAttributes(action, answer);
105
+
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
+ model: action.model,
117
+ messages: [
118
+ {
119
+ role: "user",
120
+ content: filled_question
121
+ }
122
+ ],
123
+ max_tokens: action.max_tokens,
124
+ temperature: action.temperature
125
+ }
126
+
127
+ let message = { role: "", content: "" };
128
+ if (action.context) {
129
+ message.role = "system";
130
+ message.content = action.context;
131
+ json.messages.unshift(message);
132
+ }
133
+ if (this.log) { console.log("DirGptTask json: ", json) }
134
+
135
+ const HTTPREQUEST = {
136
+ url: openai_url,
97
137
  headers: {
98
138
  'Content-Type': 'application/json',
99
- 'Authorization': 'JWT ' + this.context.token
139
+ 'Authorization': 'Bearer ' + key
100
140
  },
101
- method: "GET"
141
+ json: json,
142
+ method: 'POST'
102
143
  }
103
- if (this.log) { console.log("DirGptTask INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
104
-
144
+ if (this.log) { console.log("DirGptTask HTTPREQUEST: ", HTTPREQUEST); }
105
145
  this.#myrequest(
106
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
146
+ HTTPREQUEST, async (err, resbody) => {
107
147
  if (err) {
108
- if (callback) {
109
- console.error("(httprequest) DirGptTask get integrations err:", err);
110
- // Don't stop the flow here. Try aniway to retrieve the key from KBs
111
- // callback();
112
- // return;
148
+ if (this.log) {
149
+ console.error("(httprequest) DirGptTask openai err:", err);
150
+ console.error("(httprequest) DirGptTask openai err:", err.response.data);
113
151
  }
114
- } else if (callback) {
115
- if (this.log) { console.log("DirGptTask get integration resbody: ", integration); }
116
-
117
- let key;
118
- if (integration &&
119
- integration.value) {
120
- key = integration.value.apikey;
152
+ await this.#assignAttributes(action, answer);
153
+ if (falseIntent) {
154
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
155
+ callback(true);
156
+ return;
121
157
  }
122
-
123
- // key not present in integrations - for retro compatibility search in kbsettings
124
- if (!key) {
125
-
126
- // if (this.log) { console.log("DirGptTask - Key not found in Integrations. Searching in kb settings...")}
127
- if (this.log) { console.log("DirGptTask - 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("DirGptTask KB_HTTPREQUEST", KB_HTTPREQUEST); }
138
-
139
- this.#myrequest(
140
- KB_HTTPREQUEST, async (err, resbody) => {
141
- if (err) {
142
- if (callback) {
143
- console.error("(httprequest) DirGptTask Get KnowledgeBase err:", err);
144
- await this.#assignAttributes(action, answer);
145
- if (falseIntent) {
146
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
147
- callback(true);
148
- return;
149
- }
150
- callback();
151
- return;
152
- }
153
- } else if (callback) {
154
- if (this.log) { console.log("DirGptTask Get KnowledgeBase settings resbody:", resbody); }
155
-
156
- if (!resbody.gptkey) {
157
- await this.#assignAttributes(action, answer);
158
- if (falseIntent) {
159
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
160
- callback(true);
161
- return;
162
- }
163
- callback();
164
- return;
165
-
166
- } else {
167
-
168
- if (this.log) { console.log("DirGptTask - Key found in KbSettings") };
169
-
170
- key = resbody.gptkey;
171
-
172
- let json = {
173
- "model": action.model,
174
- "messages": [
175
- {
176
- "role": "user",
177
- "content": filled_question
178
- }
179
- ],
180
- "max_tokens": action.max_tokens,
181
- "temperature": action.temperature
182
- }
183
-
184
- let message = { role: "", content: "" };
185
- if (action.context) {
186
- message.role = "system";
187
- message.content = action.context;
188
- json.messages.unshift(message);
189
- }
190
- if (this.log) { console.log("DirGptTask json: ", json) }
191
-
192
- const HTTPREQUEST = {
193
- url: openai_url,
194
- headers: {
195
- 'Content-Type': 'application/json',
196
- 'Authorization': 'Bearer ' + key
197
- },
198
- json: json,
199
- method: 'POST'
200
- }
201
- if (this.log) { console.log("DirGptTask HTTPREQUEST: ", HTTPREQUEST); }
202
- this.#myrequest(
203
- HTTPREQUEST, async (err, resbody) => {
204
- if (err) {
205
- if (this.log) {
206
- console.error("(httprequest) DirGptTask openai err:", err);
207
- console.error("(httprequest) DirGptTask openai err:", err.response.data);
208
- }
209
- await this.#assignAttributes(action, answer);
210
- if (falseIntent) {
211
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
212
- callback(true);
213
- return;
214
- }
215
- callback();
216
- return;
217
- } else {
218
- if (this.log) { console.log("DirGptTask resbody: ", JSON.stringify(resbody)); }
219
- answer = resbody.choices[0].message.content;
220
- let answer_json = await this.convertToJson(answer);
221
- await this.#assignAttributes(action, answer_json);
222
- if (trueIntent) {
223
- await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
224
- callback(true);
225
- return;
226
- }
227
- callback();
228
- return;
229
- }
230
- }
231
- )
232
- }
233
- }
234
- }
235
- )
236
- } else {
237
-
238
- if (this.log) { console.log("DirGptTask - Key found in Integrations") };
239
-
240
- let json = {
241
- "model": action.model,
242
- "messages": [
243
- {
244
- "role": "user",
245
- "content": filled_question
246
- }
247
- ],
248
- "max_tokens": action.max_tokens,
249
- "temperature": action.temperature
250
- }
251
-
252
- let message = { role: "", content: "" };
253
- if (action.context) {
254
- message.role = "system";
255
- message.content = action.context;
256
- json.messages.unshift(message);
257
- }
258
- if (this.log) { console.log("DirGptTask json: ", json) }
259
-
260
- const HTTPREQUEST = {
261
- url: openai_url,
262
- headers: {
263
- 'Content-Type': 'application/json',
264
- 'Authorization': 'Bearer ' + key
265
- },
266
- json: json,
267
- method: 'POST'
268
- }
269
- if (this.log) { console.log("DirGptTask HTTPREQUEST: ", HTTPREQUEST); }
270
- this.#myrequest(
271
- HTTPREQUEST, async (err, resbody) => {
272
- if (err) {
273
- if (this.log) {
274
- console.error("(httprequest) DirGptTask openai err:", err);
275
- console.error("(httprequest) DirGptTask openai err:", err.response.data);
276
- }
277
- await this.#assignAttributes(action, answer);
278
- if (falseIntent) {
279
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
280
- callback(true);
281
- return;
282
- }
283
- callback();
284
- return;
285
- } else {
286
- if (this.log) { console.log("DirGptTask resbody: ", JSON.stringify(resbody)); }
287
- answer = resbody.choices[0].message.content;
288
- // check if answer is a json
289
- let answer_json = await this.convertToJson(answer);
290
- await this.#assignAttributes(action, answer_json);
291
- if (trueIntent) {
292
- await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
293
- callback(true);
294
- return;
295
- }
296
- callback();
297
- return;
298
- }
299
- }
300
- )
301
-
158
+ callback();
159
+ return;
160
+ } else {
161
+ if (this.log) { console.log("DirGptTask resbody: ", JSON.stringify(resbody)); }
162
+ answer = resbody.choices[0].message.content;
163
+ // check if answer is a json
164
+ let answer_json = await this.convertToJson(answer);
165
+ await this.#assignAttributes(action, answer_json);
166
+ if (trueIntent) {
167
+ await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
168
+ callback(true);
169
+ return;
302
170
  }
171
+ callback();
172
+ return;
303
173
  }
304
174
  }
305
175
  )
176
+
306
177
  }
307
178
 
308
179
  async convertToJson(data) {
@@ -312,7 +183,7 @@ class DirGptTask {
312
183
  try {
313
184
  json = JSON.parse(data);
314
185
  resolve(json)
315
- } catch(err) {
186
+ } catch (err) {
316
187
  resolve(data)
317
188
  }
318
189
  })
@@ -423,12 +294,73 @@ class DirGptTask {
423
294
  }
424
295
  })
425
296
  .catch((error) => {
426
- // console.error("An error occurred:", JSON.stringify(error.data));
427
297
  if (callback) {
428
298
  callback(error, null);
429
299
  }
430
300
  });
431
301
  }
302
+
303
+ async getKeyFromIntegrations(server_base_url) {
304
+ return new Promise((resolve) => {
305
+
306
+ const INTEGRATIONS_HTTPREQUEST = {
307
+ url: server_base_url + "/" + this.context.projectId + "/integration/name/openai",
308
+ headers: {
309
+ 'Content-Type': 'application/json',
310
+ 'Authorization': 'JWT ' + this.context.token
311
+ },
312
+ method: "GET"
313
+ }
314
+ if (this.log) { console.log("DirGptTask INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
315
+
316
+ this.#myrequest(
317
+ INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
318
+ if (err) {
319
+ resolve(null);
320
+ } else {
321
+
322
+ if (integration &&
323
+ integration.value) {
324
+ resolve(integration.value.apikey)
325
+ }
326
+ else {
327
+ resolve(null)
328
+ }
329
+ }
330
+ })
331
+ })
332
+ }
333
+
334
+ async getKeyFromKbSettings(server_base_url) {
335
+ return new Promise((resolve) => {
336
+
337
+ const KB_HTTPREQUEST = {
338
+ url: server_base_url + "/" + this.context.projectId + "/kbsettings",
339
+ headers: {
340
+ 'Content-Type': 'application/json',
341
+ 'Authorization': 'JWT ' + this.context.token
342
+ },
343
+ method: "GET"
344
+ }
345
+ if (this.log) { console.log("DirGptTask KB_HTTPREQUEST", KB_HTTPREQUEST); }
346
+
347
+ this.#myrequest(
348
+ KB_HTTPREQUEST, async (err, resbody) => {
349
+ if (err) {
350
+ console.error("(httprequest) DirGptTask Get KnowledgeBase err ", err);
351
+ resolve(null);
352
+ } else {
353
+ if (!resbody.gptkey) {
354
+ resolve(null);
355
+ } else {
356
+ resolve(resbody.gptkey);
357
+ }
358
+ }
359
+ }
360
+ )
361
+ })
362
+ }
363
+
432
364
  }
433
365
 
434
366
  module.exports = { DirGptTask }