@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.1
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 +387 -1
- package/ExtApi.js +6 -7
- package/Logger.js +74 -0
- package/TdCache.js +81 -176
- package/TdCache_v3.js +261 -0
- package/TiledeskExpression.js +7 -3
- package/TiledeskServices/AIService.js +43 -0
- package/TiledeskServices/utils.js +99 -0
- package/index.js +305 -50
- package/logs/app.log +279 -0
- package/models/IntentsMachineFactory.js +5 -2
- package/models/MockBotsDataSource.js +19 -11
- package/models/TiledeskChatbot.js +97 -79
- package/models/TiledeskChatbotConst.js +12 -17
- package/models/TiledeskChatbotUtil.js +359 -109
- package/models/TiledeskIntentsMachine.js +1 -1
- package/models/faqKbService.js +1 -1
- package/package.json +8 -6
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
- package/tiledeskChatbotPlugs/Filler.js +13 -2
- package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
- package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
- package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
- package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
- package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
- package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
- package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
- package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
- package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
- package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
- package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +182 -0
- package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
- package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
- package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
- package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
- package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
- package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
- package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
- package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -37
- package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
- package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
- package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +135 -0
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
- package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
- package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
- package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
- package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
- package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
- package/tiledeskChatbotPlugs/directives/DirReply.js +53 -9
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
- package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
- package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
- package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
- package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
- package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
- package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
- /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
const axios = require("axios").default;
|
|
2
|
+
const { TiledeskChatbot } = require("../../models/TiledeskChatbot");
|
|
3
|
+
const { Filler } = require("../Filler");
|
|
4
|
+
let https = require("https");
|
|
5
|
+
const { DirIntent } = require("./DirIntent");
|
|
6
|
+
const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
|
|
7
|
+
const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
|
|
8
|
+
require('dotenv').config();
|
|
9
|
+
|
|
10
|
+
class DirAiPrompt {
|
|
11
|
+
|
|
12
|
+
constructor(context) {
|
|
13
|
+
if (!context) {
|
|
14
|
+
throw new Error('context object is mandatory');
|
|
15
|
+
}
|
|
16
|
+
this.context = context;
|
|
17
|
+
this.chatbot = this.context.chatbot;
|
|
18
|
+
this.tdcache = this.context.tdcache;
|
|
19
|
+
this.requestId = this.context.requestId;
|
|
20
|
+
this.intentDir = new DirIntent(context);
|
|
21
|
+
this.API_ENDPOINT = this.context.API_ENDPOINT;
|
|
22
|
+
this.log = context.log;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
execute(directive, callback) {
|
|
26
|
+
if (this.log) { console.log("AiPrompt directive: ", directive); }
|
|
27
|
+
let action;
|
|
28
|
+
if (directive.action) {
|
|
29
|
+
action = directive.action;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
console.error("Incorrect directive: ", JSON.stringify(directive));
|
|
33
|
+
callback();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
this.go(action, (stop) => {
|
|
37
|
+
callback(stop);
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async go(action, callback) {
|
|
42
|
+
if (this.log) { console.log("DirAiPrompt action:", JSON.stringify(action)); }
|
|
43
|
+
if (!this.tdcache) {
|
|
44
|
+
console.error("Error: DirAiPrompt tdcache is mandatory");
|
|
45
|
+
callback();
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let trueIntent = action.trueIntent;
|
|
50
|
+
let falseIntent = action.falseIntent;
|
|
51
|
+
let trueIntentAttributes = action.trueIntentAttributes;
|
|
52
|
+
let falseIntentAttributes = action.falseIntentAttributes;
|
|
53
|
+
let transcript;
|
|
54
|
+
let answer = "No answer"
|
|
55
|
+
|
|
56
|
+
if (this.log) {
|
|
57
|
+
console.log("DirAiPrompt trueIntent", trueIntent)
|
|
58
|
+
console.log("DirAiPrompt falseIntent", falseIntent)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
await this.checkMandatoryParameters(action).catch( async (missing_param) => {
|
|
62
|
+
await this.chatbot.addParameter("flowError", "AiPrompt Error: '" + missing_param + "' attribute is undefined");
|
|
63
|
+
if (falseIntent) {
|
|
64
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
65
|
+
callback(true);
|
|
66
|
+
return Promise.reject();
|
|
67
|
+
}
|
|
68
|
+
callback();
|
|
69
|
+
return Promise.reject();
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
let requestVariables = null;
|
|
73
|
+
requestVariables =
|
|
74
|
+
await TiledeskChatbot.allParametersStatic(
|
|
75
|
+
this.tdcache, this.requestId
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const filler = new Filler();
|
|
79
|
+
const filled_question = filler.fill(action.question, requestVariables);
|
|
80
|
+
const filled_context = filler.fill(action.context, requestVariables);
|
|
81
|
+
|
|
82
|
+
if (action.history) {
|
|
83
|
+
let transcript_string = await TiledeskChatbot.getParameterStatic(
|
|
84
|
+
this.context.tdcache,
|
|
85
|
+
this.context.requestId,
|
|
86
|
+
TiledeskChatbotConst.REQ_TRANSCRIPT_KEY);
|
|
87
|
+
if (this.log) { console.log("DirAiPrompt transcript string: ", transcript_string) }
|
|
88
|
+
|
|
89
|
+
if (transcript_string) {
|
|
90
|
+
transcript = await TiledeskChatbotUtil.transcriptJSON(transcript_string);
|
|
91
|
+
if (this.log) { console.log("DirAiPrompt transcript: ", transcript) }
|
|
92
|
+
} else {
|
|
93
|
+
if (this.log) { console.log("DirAiPrompt transcript_string is undefined. Skip JSON translation for chat history") }
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const llm_endpoint = process.env.KB_ENDPOINT_QA;
|
|
98
|
+
if (this.log) { console.log("DirAiPrompt llm_endpoint ", llm_endpoint); }
|
|
99
|
+
|
|
100
|
+
let key = await this.getKeyFromIntegrations(action.llm);
|
|
101
|
+
|
|
102
|
+
if (!key) {
|
|
103
|
+
console.error("Error: DirAiPrompt llm key not found in integrations");
|
|
104
|
+
await this.chatbot.addParameter("flowError", "AiPrompt Error: missing key for llm " + action.llm);
|
|
105
|
+
if (falseIntent) {
|
|
106
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
107
|
+
callback(true);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
callback();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let json = {
|
|
115
|
+
question: filled_question,
|
|
116
|
+
llm: action.llm,
|
|
117
|
+
model: action.model,
|
|
118
|
+
llm_key: key,
|
|
119
|
+
temperature: action.temperature,
|
|
120
|
+
max_tokens: action.max_tokens
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (action.context) {
|
|
124
|
+
json.system_context = filled_context;
|
|
125
|
+
}
|
|
126
|
+
if (transcript) {
|
|
127
|
+
json.chat_history_dict = await this.transcriptToLLM(transcript);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (this.log) { console.log("DirAiPrompt json: ", json) }
|
|
131
|
+
|
|
132
|
+
const HTTPREQUEST = {
|
|
133
|
+
url: llm_endpoint + '/ask',
|
|
134
|
+
headers: {
|
|
135
|
+
'Content-Type': 'application/json'
|
|
136
|
+
},
|
|
137
|
+
json: json,
|
|
138
|
+
method: 'POST'
|
|
139
|
+
}
|
|
140
|
+
if (this.log) { console.log("DirAiPrompt HTTPREQUEST: ", HTTPREQUEST); }
|
|
141
|
+
|
|
142
|
+
this.#myrequest(
|
|
143
|
+
HTTPREQUEST, async (err, resbody) => {
|
|
144
|
+
if (err) {
|
|
145
|
+
if (this.log) {
|
|
146
|
+
console.error("(httprequest) DirAiPrompt openai err:", err.response.data);
|
|
147
|
+
}
|
|
148
|
+
await this.#assignAttributes(action, answer);
|
|
149
|
+
let error;
|
|
150
|
+
if (err.response?.data?.detail[0]) {
|
|
151
|
+
error = err.response.data.detail[0]?.msg;
|
|
152
|
+
} else if (err.response?.data?.detail?.answer) {
|
|
153
|
+
error = err.response.data.detail.answer;
|
|
154
|
+
} else {
|
|
155
|
+
error = JSON.stringify(err.response.data);
|
|
156
|
+
}
|
|
157
|
+
if (falseIntent) {
|
|
158
|
+
await this.chatbot.addParameter("flowError", "AiPrompt Error: " + error);
|
|
159
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
160
|
+
callback(true);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
callback();
|
|
164
|
+
return;
|
|
165
|
+
} else {
|
|
166
|
+
|
|
167
|
+
if (this.log) { console.log("DirAiPrompt resbody: ", JSON.stringify(resbody)); }
|
|
168
|
+
answer = resbody.answer;
|
|
169
|
+
|
|
170
|
+
await this.#assignAttributes(action, answer);
|
|
171
|
+
|
|
172
|
+
if (trueIntent) {
|
|
173
|
+
await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
174
|
+
callback(true);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
callback();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async checkMandatoryParameters(action) {
|
|
186
|
+
return new Promise((resolve, reject) => {
|
|
187
|
+
let params = ['question', 'llm', 'model']; // mandatory params
|
|
188
|
+
params.forEach((p) => {
|
|
189
|
+
if (!action[p]) {
|
|
190
|
+
reject(p)
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
resolve(true);
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Transforms the transcirpt array in a dictionary like '0': { "question": "xxx", "answer":"xxx"}
|
|
199
|
+
* merging consecutive messages with the same role in a single question or answer.
|
|
200
|
+
* If the first message was sent from assistant, this will be deleted.
|
|
201
|
+
*/
|
|
202
|
+
async transcriptToLLM(transcript) {
|
|
203
|
+
|
|
204
|
+
let objectTranscript = {};
|
|
205
|
+
|
|
206
|
+
if (transcript.length === 0) {
|
|
207
|
+
return objectTranscript;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let mergedTranscript = [];
|
|
211
|
+
let current = transcript[0];
|
|
212
|
+
|
|
213
|
+
for (let i = 1; i < transcript.length; i++) {
|
|
214
|
+
if (transcript[i].role === current.role) {
|
|
215
|
+
current.content += '\n' + transcript[i].content;
|
|
216
|
+
} else {
|
|
217
|
+
mergedTranscript.push(current);
|
|
218
|
+
current = transcript[i]
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
mergedTranscript.push(current);
|
|
222
|
+
|
|
223
|
+
if (mergedTranscript[0].role === 'assistant') {
|
|
224
|
+
mergedTranscript.splice(0, 1)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let counter = 0;
|
|
228
|
+
for (let i = 0; i < mergedTranscript.length - 1; i += 2) {
|
|
229
|
+
// Check if [i] is role user and [i+1] is role assistant??
|
|
230
|
+
assert(mergedTranscript[i].role === 'user');
|
|
231
|
+
assert(mergedTranscript[i+1].role === 'assistant');
|
|
232
|
+
|
|
233
|
+
if (!mergedTranscript[i].content.startsWith('/')) {
|
|
234
|
+
objectTranscript[counter] = {
|
|
235
|
+
question: mergedTranscript[i].content,
|
|
236
|
+
answer: mergedTranscript[i+1].content
|
|
237
|
+
}
|
|
238
|
+
counter++;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return objectTranscript;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
|
|
246
|
+
let trueIntentDirective = null;
|
|
247
|
+
if (trueIntent) {
|
|
248
|
+
trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
|
|
249
|
+
}
|
|
250
|
+
let falseIntentDirective = null;
|
|
251
|
+
if (falseIntent) {
|
|
252
|
+
falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
|
|
253
|
+
}
|
|
254
|
+
if (result === true) {
|
|
255
|
+
if (trueIntentDirective) {
|
|
256
|
+
this.intentDir.execute(trueIntentDirective, () => {
|
|
257
|
+
if (callback) {
|
|
258
|
+
callback();
|
|
259
|
+
}
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
if (this.log) { console.log("No trueIntentDirective specified"); }
|
|
264
|
+
if (callback) {
|
|
265
|
+
callback();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
if (falseIntentDirective) {
|
|
271
|
+
this.intentDir.execute(falseIntentDirective, () => {
|
|
272
|
+
if (callback) {
|
|
273
|
+
callback();
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
if (this.log) { console.log("No falseIntentDirective specified"); }
|
|
279
|
+
if (callback) {
|
|
280
|
+
callback();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async #assignAttributes(action, answer) {
|
|
287
|
+
if (this.log) {
|
|
288
|
+
console.log("assignAttributes action:", action)
|
|
289
|
+
console.log("assignAttributes answer:", answer)
|
|
290
|
+
}
|
|
291
|
+
if (this.context.tdcache) {
|
|
292
|
+
if (action.assignReplyTo && answer) {
|
|
293
|
+
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
|
|
294
|
+
}
|
|
295
|
+
// Debug log
|
|
296
|
+
if (this.log) {
|
|
297
|
+
const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
|
|
298
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
299
|
+
if (this.log) { console.log("(gpttask) request parameter:", key, "value:", value, "type:", typeof value) }
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
#myrequest(options, callback) {
|
|
306
|
+
if (this.log) {
|
|
307
|
+
console.log("API URL:", options.url);
|
|
308
|
+
console.log("** Options:", JSON.stringify(options));
|
|
309
|
+
}
|
|
310
|
+
let axios_options = {
|
|
311
|
+
url: options.url,
|
|
312
|
+
method: options.method,
|
|
313
|
+
params: options.params,
|
|
314
|
+
headers: options.headers
|
|
315
|
+
}
|
|
316
|
+
if (options.json !== null) {
|
|
317
|
+
axios_options.data = options.json
|
|
318
|
+
}
|
|
319
|
+
if (this.log) {
|
|
320
|
+
console.log("axios_options:", JSON.stringify(axios_options));
|
|
321
|
+
}
|
|
322
|
+
if (options.url.startsWith("https:")) {
|
|
323
|
+
const httpsAgent = new https.Agent({
|
|
324
|
+
rejectUnauthorized: false,
|
|
325
|
+
});
|
|
326
|
+
axios_options.httpsAgent = httpsAgent;
|
|
327
|
+
}
|
|
328
|
+
axios(axios_options)
|
|
329
|
+
.then((res) => {
|
|
330
|
+
if (this.log) {
|
|
331
|
+
console.log("Response for url:", options.url);
|
|
332
|
+
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
333
|
+
}
|
|
334
|
+
if (res && res.status == 200 && res.data) {
|
|
335
|
+
if (callback) {
|
|
336
|
+
callback(null, res.data);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
if (callback) {
|
|
341
|
+
callback(new Error("Response status is not 200"), null);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
})
|
|
345
|
+
.catch((error) => {
|
|
346
|
+
console.error("(DirAiPrompt) Axios error: ", JSON.stringify(error));
|
|
347
|
+
if (callback) {
|
|
348
|
+
callback(error, null);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async getKeyFromIntegrations(model) {
|
|
354
|
+
return new Promise((resolve) => {
|
|
355
|
+
|
|
356
|
+
const INTEGRATIONS_HTTPREQUEST = {
|
|
357
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/" + model,
|
|
358
|
+
headers: {
|
|
359
|
+
'Content-Type': 'application/json',
|
|
360
|
+
'Authorization': 'JWT ' + this.context.token
|
|
361
|
+
},
|
|
362
|
+
method: "GET"
|
|
363
|
+
}
|
|
364
|
+
if (this.log) { console.log("DirAiPrompt INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
|
|
365
|
+
|
|
366
|
+
this.#myrequest(
|
|
367
|
+
INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
|
|
368
|
+
if (err) {
|
|
369
|
+
resolve(null);
|
|
370
|
+
} else {
|
|
371
|
+
|
|
372
|
+
if (integration &&
|
|
373
|
+
integration.value) {
|
|
374
|
+
resolve(integration.value.apikey)
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
resolve(null)
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
})
|
|
381
|
+
})
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
async getKeyFromKbSettings() {
|
|
385
|
+
return new Promise((resolve) => {
|
|
386
|
+
|
|
387
|
+
const KB_HTTPREQUEST = {
|
|
388
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/kbsettings",
|
|
389
|
+
headers: {
|
|
390
|
+
'Content-Type': 'application/json',
|
|
391
|
+
'Authorization': 'JWT ' + this.context.token
|
|
392
|
+
},
|
|
393
|
+
method: "GET"
|
|
394
|
+
}
|
|
395
|
+
if (this.log) { console.log("DirAiPrompt KB_HTTPREQUEST", KB_HTTPREQUEST); }
|
|
396
|
+
|
|
397
|
+
this.#myrequest(
|
|
398
|
+
KB_HTTPREQUEST, async (err, resbody) => {
|
|
399
|
+
if (err) {
|
|
400
|
+
console.error("(httprequest) DirAiPrompt Get KnowledgeBase err:", err.message);
|
|
401
|
+
if (this.log) {
|
|
402
|
+
console.error("(httprequest) DirAiPrompt Get KnowledgeBase full err", err);
|
|
403
|
+
}
|
|
404
|
+
resolve(null);
|
|
405
|
+
} else {
|
|
406
|
+
if (!resbody.gptkey) {
|
|
407
|
+
resolve(null);
|
|
408
|
+
} else {
|
|
409
|
+
resolve(resbody.gptkey);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
)
|
|
414
|
+
})
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async checkQuoteAvailability() {
|
|
418
|
+
return new Promise((resolve) => {
|
|
419
|
+
|
|
420
|
+
const HTTPREQUEST = {
|
|
421
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/tokens",
|
|
422
|
+
headers: {
|
|
423
|
+
'Content-Type': 'application/json',
|
|
424
|
+
'Authorization': 'JWT ' + this.context.token
|
|
425
|
+
},
|
|
426
|
+
method: "GET"
|
|
427
|
+
}
|
|
428
|
+
if (this.log) { console.log("DirAiPrompt check quote availability HTTPREQUEST", HTTPREQUEST); }
|
|
429
|
+
|
|
430
|
+
this.#myrequest(
|
|
431
|
+
HTTPREQUEST, async (err, resbody) => {
|
|
432
|
+
if (err) {
|
|
433
|
+
resolve(true)
|
|
434
|
+
} else {
|
|
435
|
+
if (resbody.isAvailable === true) {
|
|
436
|
+
resolve(true)
|
|
437
|
+
} else {
|
|
438
|
+
resolve(false)
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
)
|
|
443
|
+
})
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async updateQuote(tokens_usage) {
|
|
447
|
+
return new Promise((resolve, reject) => {
|
|
448
|
+
|
|
449
|
+
const HTTPREQUEST = {
|
|
450
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/incr/tokens",
|
|
451
|
+
headers: {
|
|
452
|
+
'Content-Type': 'application/json',
|
|
453
|
+
'Authorization': 'JWT ' + this.context.token
|
|
454
|
+
},
|
|
455
|
+
json: tokens_usage,
|
|
456
|
+
method: "POST"
|
|
457
|
+
}
|
|
458
|
+
if (this.log) { console.log("DirAiPrompt check quote availability HTTPREQUEST", HTTPREQUEST); }
|
|
459
|
+
|
|
460
|
+
this.#myrequest(
|
|
461
|
+
HTTPREQUEST, async (err, resbody) => {
|
|
462
|
+
if (err) {
|
|
463
|
+
console.error("(httprequest) DirAiPrompt Increment tokens quote err: ", err);
|
|
464
|
+
reject(false)
|
|
465
|
+
} else {
|
|
466
|
+
if (this.log) { console.log("(httprequest) DirAiPrompt Increment token quote resbody: ", resbody); }
|
|
467
|
+
resolve(true);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
)
|
|
471
|
+
})
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
module.exports = { DirAiPrompt }
|
|
@@ -15,6 +15,7 @@ class DirAskGPT {
|
|
|
15
15
|
this.tdcache = this.context.tdcache;
|
|
16
16
|
this.requestId = this.context.requestId;
|
|
17
17
|
this.intentDir = new DirIntent(context);
|
|
18
|
+
this.API_ENDPOINT = this.context.API_ENDPOINT;
|
|
18
19
|
this.log = context.log;
|
|
19
20
|
}
|
|
20
21
|
|
|
@@ -88,21 +89,17 @@ class DirAskGPT {
|
|
|
88
89
|
const filler = new Filler();
|
|
89
90
|
const filled_question = filler.fill(action.question, requestVariables);
|
|
90
91
|
|
|
91
|
-
const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
|
|
92
92
|
const kb_endpoint = process.env.KB_ENDPOINT;
|
|
93
|
-
if (this.log) {
|
|
94
|
-
console.log("DirAskGPT ApiEndpoint URL: ", server_base_url);
|
|
95
|
-
console.log("DirAskGPT KbEndpoint URL: ", kb_endpoint);
|
|
96
|
-
}
|
|
93
|
+
if (this.log) { console.log("DirAskGPT KbEndpoint URL: ", kb_endpoint); }
|
|
97
94
|
|
|
98
|
-
let key = await this.getKeyFromIntegrations(
|
|
95
|
+
let key = await this.getKeyFromIntegrations();
|
|
99
96
|
if (!key) {
|
|
100
97
|
if (this.log) { console.log("DirAskGPT - Key not found in Integrations. Searching in kb settings..."); }
|
|
101
|
-
key = await this.getKeyFromKbSettings(
|
|
98
|
+
key = await this.getKeyFromKbSettings();
|
|
102
99
|
}
|
|
103
100
|
|
|
104
101
|
if (!key) {
|
|
105
|
-
if (this.log) { console.log("
|
|
102
|
+
if (this.log) { console.log("DirAskGPT - Retrieve public gptkey")}
|
|
106
103
|
key = process.env.GPTKEY;
|
|
107
104
|
publicKey = true;
|
|
108
105
|
}
|
|
@@ -120,9 +117,9 @@ class DirAskGPT {
|
|
|
120
117
|
}
|
|
121
118
|
|
|
122
119
|
if (publicKey === true) {
|
|
123
|
-
let keep_going = await this.checkQuoteAvailability(
|
|
120
|
+
let keep_going = await this.checkQuoteAvailability();
|
|
124
121
|
if (keep_going === false) {
|
|
125
|
-
if (this.log) { console.log("
|
|
122
|
+
if (this.log) { console.log("DirAskGPT - Quota exceeded for tokens. Skip the action")}
|
|
126
123
|
callback();
|
|
127
124
|
return;
|
|
128
125
|
}
|
|
@@ -167,7 +164,7 @@ class DirAskGPT {
|
|
|
167
164
|
|
|
168
165
|
// if (publicKey === true) {
|
|
169
166
|
// let token_usage = resbody.usage.total_tokens;
|
|
170
|
-
// this.updateQuote(
|
|
167
|
+
// this.updateQuote(token_usage);
|
|
171
168
|
// }
|
|
172
169
|
|
|
173
170
|
if (trueIntent) {
|
|
@@ -298,25 +295,25 @@ class DirAskGPT {
|
|
|
298
295
|
}
|
|
299
296
|
})
|
|
300
297
|
.catch((error) => {
|
|
301
|
-
|
|
298
|
+
console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
|
|
302
299
|
if (callback) {
|
|
303
300
|
callback(error, null);
|
|
304
301
|
}
|
|
305
302
|
});
|
|
306
303
|
}
|
|
307
304
|
|
|
308
|
-
async getKeyFromIntegrations(
|
|
305
|
+
async getKeyFromIntegrations() {
|
|
309
306
|
return new Promise((resolve) => {
|
|
310
307
|
|
|
311
308
|
const INTEGRATIONS_HTTPREQUEST = {
|
|
312
|
-
url:
|
|
309
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/openai",
|
|
313
310
|
headers: {
|
|
314
311
|
'Content-Type': 'application/json',
|
|
315
312
|
'Authorization': 'JWT ' + this.context.token
|
|
316
313
|
},
|
|
317
314
|
method: "GET"
|
|
318
315
|
}
|
|
319
|
-
if (this.log) { console.log("
|
|
316
|
+
if (this.log) { console.log("DirAskGPT INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
|
|
320
317
|
|
|
321
318
|
this.#myrequest(
|
|
322
319
|
INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
|
|
@@ -337,23 +334,23 @@ class DirAskGPT {
|
|
|
337
334
|
})
|
|
338
335
|
}
|
|
339
336
|
|
|
340
|
-
async getKeyFromKbSettings(
|
|
337
|
+
async getKeyFromKbSettings() {
|
|
341
338
|
return new Promise((resolve) => {
|
|
342
339
|
|
|
343
340
|
const KB_HTTPREQUEST = {
|
|
344
|
-
url:
|
|
341
|
+
url: this.API_ENDPOINT + "/" + this.context.projectId + "/kbsettings",
|
|
345
342
|
headers: {
|
|
346
343
|
'Content-Type': 'application/json',
|
|
347
344
|
'Authorization': 'JWT ' + this.context.token
|
|
348
345
|
},
|
|
349
346
|
method: "GET"
|
|
350
347
|
}
|
|
351
|
-
if (this.log) { console.log("
|
|
348
|
+
if (this.log) { console.log("DirAskGPT KB_HTTPREQUEST", KB_HTTPREQUEST); }
|
|
352
349
|
|
|
353
350
|
this.#myrequest(
|
|
354
351
|
KB_HTTPREQUEST, async (err, resbody) => {
|
|
355
352
|
if (err) {
|
|
356
|
-
if (this.log) { console.error("
|
|
353
|
+
if (this.log) { console.error("DirAskGPT Get kb settings error ", err); }
|
|
357
354
|
resolve(null);
|
|
358
355
|
} else {
|
|
359
356
|
if (!resbody.gptkey) {
|