@tiledesk/tiledesk-tybot-connector 0.2.35 → 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,216 +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
- await this.#assignAttributes(action, answer);
221
- if (trueIntent) {
222
- await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
223
- callback(true);
224
- return;
225
- }
226
- callback();
227
- return;
228
- }
229
- }
230
- )
231
- }
232
- }
233
- }
234
- )
235
- } else {
236
-
237
- if (this.log) { console.log("DirGptTask - Key found in Integrations") };
238
-
239
- let json = {
240
- "model": action.model,
241
- "messages": [
242
- {
243
- "role": "user",
244
- "content": filled_question
245
- }
246
- ],
247
- "max_tokens": action.max_tokens,
248
- "temperature": action.temperature
249
- }
250
-
251
- let message = { role: "", content: "" };
252
- if (action.context) {
253
- message.role = "system";
254
- message.content = action.context;
255
- json.messages.unshift(message);
256
- }
257
- if (this.log) { console.log("DirGptTask json: ", json) }
258
-
259
- const HTTPREQUEST = {
260
- url: openai_url,
261
- headers: {
262
- 'Content-Type': 'application/json',
263
- 'Authorization': 'Bearer ' + key
264
- },
265
- json: json,
266
- method: 'POST'
267
- }
268
- if (this.log) { console.log("DirGptTask HTTPREQUEST: ", HTTPREQUEST); }
269
- this.#myrequest(
270
- HTTPREQUEST, async (err, resbody) => {
271
- if (err) {
272
- if (this.log) {
273
- console.error("(httprequest) DirGptTask openai err:", err);
274
- console.error("(httprequest) DirGptTask openai err:", err.response.data);
275
- }
276
- await this.#assignAttributes(action, answer);
277
- if (falseIntent) {
278
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
279
- callback(true);
280
- return;
281
- }
282
- callback();
283
- return;
284
- } else {
285
- if (this.log) { console.log("DirGptTask resbody: ", JSON.stringify(resbody)); }
286
- answer = resbody.choices[0].message.content;
287
- // check if answer is a json
288
- let answer_json = await this.convertToJson(answer);
289
- await this.#assignAttributes(action, answer_json);
290
- if (trueIntent) {
291
- await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
292
- callback(true);
293
- return;
294
- }
295
- callback();
296
- return;
297
- }
298
- }
299
- )
300
-
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;
301
170
  }
171
+ callback();
172
+ return;
302
173
  }
303
174
  }
304
175
  )
176
+
305
177
  }
306
178
 
307
179
  async convertToJson(data) {
@@ -311,7 +183,7 @@ class DirGptTask {
311
183
  try {
312
184
  json = JSON.parse(data);
313
185
  resolve(json)
314
- } catch(err) {
186
+ } catch (err) {
315
187
  resolve(data)
316
188
  }
317
189
  })
@@ -422,12 +294,73 @@ class DirGptTask {
422
294
  }
423
295
  })
424
296
  .catch((error) => {
425
- // console.error("An error occurred:", JSON.stringify(error.data));
426
297
  if (callback) {
427
298
  callback(error, null);
428
299
  }
429
300
  });
430
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
+
431
364
  }
432
365
 
433
366
  module.exports = { DirGptTask }