@tiledesk/tiledesk-tybot-connector 2.1.5 → 2.1.8
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 +1 -0
- package/TdCache.js +5 -5
- package/engine/TiledeskChatbot.js +2 -1
- package/logs/app1.log +18 -0
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +2 -1
- package/tiledeskChatbotPlugs/directives/DirAiCondition.js +93 -20
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +309 -24
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
|
|
8
9
|
# this branch
|
|
9
10
|
- added: JSON Condition V2 runtime — new `jsoncondition2` type routed to `DirJSONConditionV2` (evaluates the `when` expression via `TiledeskWhenExpression`, no eval/vm2); V1 `jsoncondition` dispatch left unchanged (`DirJSONCondition`) for full backward-compatibility
|
|
10
11
|
|
package/TdCache.js
CHANGED
|
@@ -82,14 +82,14 @@ class TdCache {
|
|
|
82
82
|
if (!value) {
|
|
83
83
|
return;
|
|
84
84
|
}
|
|
85
|
-
if (!options) {
|
|
86
|
-
options = {EX: 86400}
|
|
87
|
-
}
|
|
88
85
|
await this.client.HSET(
|
|
89
86
|
dict_key,
|
|
90
87
|
key,
|
|
91
|
-
value
|
|
92
|
-
|
|
88
|
+
value);
|
|
89
|
+
// HSET does not support EX; apply TTL via EXPIRE when provided
|
|
90
|
+
if (options && options.EX) {
|
|
91
|
+
await this.expire(dict_key, options.EX);
|
|
92
|
+
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
async hdel(dict_key, key) {
|
|
@@ -537,7 +537,8 @@ class TiledeskChatbot {
|
|
|
537
537
|
if (parameter_value_s?.length > 20000000) {
|
|
538
538
|
return;
|
|
539
539
|
}
|
|
540
|
-
|
|
540
|
+
const ttl = parseInt(process.env.FLOW_ATTRIBUTES_TTL, 10) || (15 * 24 * 60 * 60); // default 15 days
|
|
541
|
+
await _tdcache.hset(parameter_key, parameter_name, parameter_value_s, { EX: ttl });
|
|
541
542
|
}
|
|
542
543
|
|
|
543
544
|
async allParameters() {
|
package/logs/app1.log
CHANGED
|
@@ -4896,3 +4896,21 @@ info: (Tilebot) Redis connected
|
|
|
4896
4896
|
info: (Tilebot) Tilebot started
|
|
4897
4897
|
info: Tilebot route successfully started.
|
|
4898
4898
|
info: Tilebot connector listening on port 10001
|
|
4899
|
+
info: AI_MODELS not defined
|
|
4900
|
+
info: AI Models:
|
|
4901
|
+
info: AI_MODELS not defined
|
|
4902
|
+
info: AI Models:
|
|
4903
|
+
info: Starting tilebot server...
|
|
4904
|
+
info: (Tilebot) Starting Tilebot..
|
|
4905
|
+
info: (Tilebot) settings.API_ENDPOINT:http://127.0.0.1:10002
|
|
4906
|
+
info: (Tilebot) settings.TILEBOT_ENDPOINT:http://127.0.0.1:10001
|
|
4907
|
+
info: (Tilebot) Log Level: info
|
|
4908
|
+
info: (Tilebot) MAX_STEPS: 1000
|
|
4909
|
+
info: (Tilebot) MAX_EXECUTION_TIME: 28800000
|
|
4910
|
+
info: (Tilebot) Starting Tilebot connector v2.1.2
|
|
4911
|
+
info: (Tilebot) Using static bots
|
|
4912
|
+
info: (Tilebot) Connecting Redis...
|
|
4913
|
+
info: (Tilebot) Redis connected
|
|
4914
|
+
info: (Tilebot) Tilebot started
|
|
4915
|
+
info: Tilebot route successfully started.
|
|
4916
|
+
info: Tilebot connector listening on port 10001
|
package/package.json
CHANGED
|
@@ -16,7 +16,8 @@ class TiledeskRequestVariables {
|
|
|
16
16
|
|
|
17
17
|
async set(name, value, callback) {
|
|
18
18
|
const parameter_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":parameters";
|
|
19
|
-
|
|
19
|
+
const ttl = parseInt(process.env.FLOW_ATTRIBUTES_TTL, 10) || (15 * 24 * 60 * 60); // default 15 days
|
|
20
|
+
await this.tdcache.hset(parameter_key, name, value, { EX: ttl });
|
|
20
21
|
if (callback) {
|
|
21
22
|
callback();
|
|
22
23
|
}
|
|
@@ -70,7 +70,7 @@ class DirAiCondition {
|
|
|
70
70
|
let fallbackIntent = action.fallbackIntent; // non condition met block
|
|
71
71
|
let errorIntent = action.errorIntent; // On error block
|
|
72
72
|
await this.checkMandatoryParameters(action).catch( async (missing_param) => {
|
|
73
|
-
const error = "
|
|
73
|
+
const error = "AiCondition Error: '" + missing_param + "' attribute is undefined"
|
|
74
74
|
this.logger.error(error);
|
|
75
75
|
await this.chatbot.addParameter("flowError", error);
|
|
76
76
|
if (errorIntent) {
|
|
@@ -89,7 +89,8 @@ class DirAiCondition {
|
|
|
89
89
|
this.tdcache, this.requestId
|
|
90
90
|
)
|
|
91
91
|
const filler = new Filler();
|
|
92
|
-
|
|
92
|
+
const filled_model = filler.fill(action.model, requestVariables);
|
|
93
|
+
|
|
93
94
|
let conditions = "";
|
|
94
95
|
intents.forEach( function(intent) {
|
|
95
96
|
let filled_prompt = filler.fill(intent.prompt, requestVariables);
|
|
@@ -111,7 +112,7 @@ class DirAiCondition {
|
|
|
111
112
|
// evaluate
|
|
112
113
|
|
|
113
114
|
let AI_endpoint = process.env.KB_ENDPOINT_QA;
|
|
114
|
-
winston.verbose("
|
|
115
|
+
winston.verbose("DirAiCondition AI_endpoint " + AI_endpoint);
|
|
115
116
|
|
|
116
117
|
let headers = {
|
|
117
118
|
'Content-Type': 'application/json'
|
|
@@ -121,11 +122,12 @@ class DirAiCondition {
|
|
|
121
122
|
let key;
|
|
122
123
|
let publicKey = false;
|
|
123
124
|
let ollama_integration;
|
|
125
|
+
let vllm_server_config;
|
|
124
126
|
|
|
125
127
|
if (action.llm === 'ollama') {
|
|
126
128
|
ollama_integration = await integrationService.getIntegration(this.projectId, action.llm, this.token).catch( async (err) => {
|
|
127
129
|
this.logger.error("[AI Condition] Error getting ollama integration.")
|
|
128
|
-
winston.error("
|
|
130
|
+
winston.error("DirAiCondition Error getting ollama integration: ", err);
|
|
129
131
|
await this.chatbot.addParameter("flowError", "Ollama integration not found");
|
|
130
132
|
if (errorIntent) {
|
|
131
133
|
await this.#executeIntent(errorIntent);
|
|
@@ -136,6 +138,66 @@ class DirAiCondition {
|
|
|
136
138
|
return;
|
|
137
139
|
});
|
|
138
140
|
|
|
141
|
+
} else if(action.llm === 'vllm'){
|
|
142
|
+
const vllm_integration = await integrationService.getIntegration(this.projectId, action.llm, this.token);
|
|
143
|
+
|
|
144
|
+
if (!vllm_integration?.value) {
|
|
145
|
+
this.logger.error("[AI Condition] Error getting vllm integration.");
|
|
146
|
+
winston.error("DirAiCondition Error getting vllm integration");
|
|
147
|
+
await this.chatbot.addParameter("flowError", "Vllm integration not found");
|
|
148
|
+
if (falseIntent) {
|
|
149
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
150
|
+
callback(true);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
callback();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const vllm_value = vllm_integration.value;
|
|
158
|
+
if (Array.isArray(vllm_value.servers)) {
|
|
159
|
+
const filled_vllm_server = filler.fill(action.vllmServer, requestVariables);
|
|
160
|
+
if (!filled_vllm_server) {
|
|
161
|
+
this.logger.error("[AI Condition] missing vllmServer for multi-server vllm integration");
|
|
162
|
+
await this.chatbot.addParameter("flowError", "AiCondition Error: 'vllmServer' attribute is undefined");
|
|
163
|
+
if (falseIntent) {
|
|
164
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
165
|
+
callback(true);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
callback();
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
vllm_server_config = vllm_value.servers.find(s => s.name === filled_vllm_server);
|
|
172
|
+
if (!vllm_server_config) {
|
|
173
|
+
this.logger.error("[AI Condition] vllm server not found: ", filled_vllm_server);
|
|
174
|
+
await this.chatbot.addParameter("flowError", "AiCondition Error: vllm server '" + filled_vllm_server + "' not found");
|
|
175
|
+
if (falseIntent) {
|
|
176
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
177
|
+
callback(true);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
callback();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
key = vllm_server_config.apikey;
|
|
184
|
+
} else {
|
|
185
|
+
key = vllm_value.apikey;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!key) {
|
|
189
|
+
this.logger.error("[AI Condition] llm key not found in vllm integration");
|
|
190
|
+
winston.error("Error: DirAiCondition llm key not found in vllm integration");
|
|
191
|
+
await this.chatbot.addParameter("flowError", "AiCondition Error: missing key for llm vllm");
|
|
192
|
+
if (falseIntent) {
|
|
193
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
194
|
+
callback(true);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
callback();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
139
201
|
} else {
|
|
140
202
|
key = await integrationService.getKeyFromIntegrations(this.projectId, action.llm, this.token);
|
|
141
203
|
|
|
@@ -147,8 +209,8 @@ class DirAiCondition {
|
|
|
147
209
|
|
|
148
210
|
if (!key) {
|
|
149
211
|
this.logger.error("[AI Condition] llm key not found");
|
|
150
|
-
winston.error("Error:
|
|
151
|
-
await this.chatbot.addParameter("flowError", "
|
|
212
|
+
winston.error("Error: DirAiCondition llm key not found");
|
|
213
|
+
await this.chatbot.addParameter("flowError", "AiCondition Error: missing key for llm " + action.llm);
|
|
152
214
|
if (errorIntent) {
|
|
153
215
|
await this.#executeIntent(errorIntent);
|
|
154
216
|
callback(true);
|
|
@@ -216,7 +278,18 @@ class DirAiCondition {
|
|
|
216
278
|
|
|
217
279
|
}
|
|
218
280
|
|
|
219
|
-
|
|
281
|
+
if (action.llm === 'vllm' && vllm_server_config) {
|
|
282
|
+
console.log("llm: vllm")
|
|
283
|
+
json.model = {
|
|
284
|
+
name: filled_model,
|
|
285
|
+
url: vllm_server_config.url,
|
|
286
|
+
api_key: vllm_server_config.apikey || vllm_server_config.token || null,
|
|
287
|
+
provider: 'vllm'
|
|
288
|
+
}
|
|
289
|
+
console.log("set json.model to: ", json.model);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
winston.debug("DirAiCondition json: ", json);
|
|
220
293
|
|
|
221
294
|
const HTTPREQUEST = {
|
|
222
295
|
url: AI_endpoint + "/ask",
|
|
@@ -224,12 +297,12 @@ class DirAiCondition {
|
|
|
224
297
|
json: json,
|
|
225
298
|
method: 'POST'
|
|
226
299
|
}
|
|
227
|
-
winston.debug("
|
|
300
|
+
winston.debug("DirAiCondition HttpRequest: ", HTTPREQUEST);
|
|
228
301
|
|
|
229
302
|
httpUtils.request(
|
|
230
303
|
HTTPREQUEST, async (err, resbody) => {
|
|
231
304
|
if (err) {
|
|
232
|
-
winston.error("
|
|
305
|
+
winston.error("DirAiCondition openai err: ", err);
|
|
233
306
|
await this.#assignAttributes(action, answer);
|
|
234
307
|
let error;
|
|
235
308
|
if (err.response?.data?.detail[0]) {
|
|
@@ -250,7 +323,7 @@ class DirAiCondition {
|
|
|
250
323
|
return;
|
|
251
324
|
} else {
|
|
252
325
|
|
|
253
|
-
winston.debug("
|
|
326
|
+
winston.debug("DirAiCondition resbody: ", resbody);
|
|
254
327
|
answer = resbody.answer;
|
|
255
328
|
this.logger.native("[AI Condition] answer: ", answer);
|
|
256
329
|
|
|
@@ -388,7 +461,7 @@ class DirAiCondition {
|
|
|
388
461
|
}
|
|
389
462
|
else {
|
|
390
463
|
this.logger.native("[AI Condition] no block connected to true condition");
|
|
391
|
-
winston.debug("
|
|
464
|
+
winston.debug("DirAiCondition No trueIntentDirective specified");
|
|
392
465
|
if (callback) {
|
|
393
466
|
callback();
|
|
394
467
|
}
|
|
@@ -405,7 +478,7 @@ class DirAiCondition {
|
|
|
405
478
|
}
|
|
406
479
|
else {
|
|
407
480
|
this.logger.native("[AI Condition] no block connected to false condition");
|
|
408
|
-
winston.debug("
|
|
481
|
+
winston.debug("DirAiCondition No falseIntentDirective specified");
|
|
409
482
|
if (callback) {
|
|
410
483
|
callback();
|
|
411
484
|
}
|
|
@@ -414,8 +487,8 @@ class DirAiCondition {
|
|
|
414
487
|
}
|
|
415
488
|
|
|
416
489
|
async #assignAttributes(action, answer) {
|
|
417
|
-
winston.debug("
|
|
418
|
-
winston.debug("
|
|
490
|
+
winston.debug("DirAiCondition assignAttributes action: ", action)
|
|
491
|
+
winston.debug("DirAiCondition assignAttributes answer: " + answer)
|
|
419
492
|
|
|
420
493
|
if (this.context.tdcache) {
|
|
421
494
|
if (action.assignReplyTo && answer) {
|
|
@@ -457,12 +530,12 @@ class DirAiCondition {
|
|
|
457
530
|
},
|
|
458
531
|
method: "GET"
|
|
459
532
|
}
|
|
460
|
-
winston.debug("
|
|
533
|
+
winston.debug("DirAiCondition KB HttpRequest", KB_HTTPREQUEST);
|
|
461
534
|
|
|
462
535
|
httpUtils.request(
|
|
463
536
|
KB_HTTPREQUEST, async (err, resbody) => {
|
|
464
537
|
if (err) {
|
|
465
|
-
winston.error("(httprequest)
|
|
538
|
+
winston.error("(httprequest) DirAiCondition Get KnowledgeBase err: " + err.message);
|
|
466
539
|
resolve(null);
|
|
467
540
|
} else {
|
|
468
541
|
if (!resbody.gptkey) {
|
|
@@ -487,7 +560,7 @@ class DirAiCondition {
|
|
|
487
560
|
},
|
|
488
561
|
method: "GET"
|
|
489
562
|
}
|
|
490
|
-
winston.debug("
|
|
563
|
+
winston.debug("DirAiCondition check quote availability HttpRequest", HTTPREQUEST);
|
|
491
564
|
|
|
492
565
|
httpUtils.request(
|
|
493
566
|
HTTPREQUEST, async (err, resbody) => {
|
|
@@ -517,15 +590,15 @@ class DirAiCondition {
|
|
|
517
590
|
json: tokens_usage,
|
|
518
591
|
method: "POST"
|
|
519
592
|
}
|
|
520
|
-
winston.debug("
|
|
593
|
+
winston.debug("DirAiCondition update quote HttpRequest", HTTPREQUEST);
|
|
521
594
|
|
|
522
595
|
httpUtils.request(
|
|
523
596
|
HTTPREQUEST, async (err, resbody) => {
|
|
524
597
|
if (err) {
|
|
525
|
-
winston.error("(httprequest)
|
|
598
|
+
winston.error("(httprequest) DirAiCondition Increment tokens quote err: ", err);
|
|
526
599
|
reject(false)
|
|
527
600
|
} else {
|
|
528
|
-
winston.debug("(httprequest)
|
|
601
|
+
winston.debug("(httprequest) DirAiCondition Increment token quote resbody: ", resbody);
|
|
529
602
|
resolve(true);
|
|
530
603
|
}
|
|
531
604
|
}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
const axios = require("axios").default;
|
|
2
2
|
const { TiledeskChatbot } = require("../../engine/TiledeskChatbot");
|
|
3
3
|
const { Filler } = require("../Filler");
|
|
4
|
-
let https = require("https");
|
|
5
4
|
const { DirIntent } = require("./DirIntent");
|
|
6
5
|
const { TiledeskChatbotConst } = require("../../engine/TiledeskChatbotConst");
|
|
7
6
|
const { TiledeskChatbotUtil } = require("../../utils/TiledeskChatbotUtil");
|
|
8
7
|
require('dotenv').config();
|
|
9
8
|
const winston = require('../../utils/winston');
|
|
10
|
-
const Utils = require("../../utils/HttpUtils");
|
|
11
|
-
const utils = require("../../utils/HttpUtils");
|
|
12
9
|
const httpUtils = require("../../utils/HttpUtils");
|
|
13
10
|
const integrationService = require("../../services/IntegrationService");
|
|
14
11
|
const { Logger } = require("../../Logger");
|
|
@@ -17,6 +14,9 @@ const quotasService = require("../../services/QuotasService");
|
|
|
17
14
|
const path = require("path");
|
|
18
15
|
const mime = require("mime-types");
|
|
19
16
|
|
|
17
|
+
const NATIVE_MCP_CACHE_KEY = 'native_mcp:servers';
|
|
18
|
+
const reasoningLevels = ['low', 'medium', 'high'];
|
|
19
|
+
|
|
20
20
|
|
|
21
21
|
class DirAiPrompt {
|
|
22
22
|
|
|
@@ -140,6 +140,7 @@ class DirAiPrompt {
|
|
|
140
140
|
|
|
141
141
|
} else if (action.llm === 'vllm') {
|
|
142
142
|
const vllm_integration = await integrationService.getIntegration(this.projectId, action.llm, this.token);
|
|
143
|
+
|
|
143
144
|
if (!vllm_integration?.value) {
|
|
144
145
|
this.logger.error("[AI Prompt] Error getting vllm integration.");
|
|
145
146
|
winston.error("DirAiPrompt Error getting vllm integration");
|
|
@@ -278,12 +279,14 @@ class DirAiPrompt {
|
|
|
278
279
|
}
|
|
279
280
|
|
|
280
281
|
if (action.llm === 'vllm' && vllm_server_config) {
|
|
282
|
+
console.log("llm: vllm")
|
|
281
283
|
json.model = {
|
|
282
284
|
name: filled_model,
|
|
283
285
|
url: vllm_server_config.url,
|
|
284
286
|
api_key: vllm_server_config.apikey || vllm_server_config.token || null,
|
|
285
287
|
provider: 'vllm'
|
|
286
288
|
}
|
|
289
|
+
console.log("set json.model to: ", json.model);
|
|
287
290
|
}
|
|
288
291
|
|
|
289
292
|
if (action.attach) {
|
|
@@ -292,10 +295,11 @@ class DirAiPrompt {
|
|
|
292
295
|
|
|
293
296
|
if (action.servers) {
|
|
294
297
|
|
|
298
|
+
let mcp_integration;
|
|
295
299
|
try {
|
|
296
|
-
|
|
300
|
+
mcp_integration = await integrationService.getIntegration(this.projectId, "mcp", this.token);
|
|
297
301
|
if (mcp_integration?.value?.servers) {
|
|
298
|
-
|
|
302
|
+
this.enrichServersFromIntegration(action.servers, mcp_integration);
|
|
299
303
|
}
|
|
300
304
|
|
|
301
305
|
} catch (err) {
|
|
@@ -311,7 +315,31 @@ class DirAiPrompt {
|
|
|
311
315
|
return;
|
|
312
316
|
}
|
|
313
317
|
|
|
314
|
-
|
|
318
|
+
const nativeUrlError = await this.resolveNativeServerUrls(action.servers);
|
|
319
|
+
if (nativeUrlError) {
|
|
320
|
+
await this.chatbot.addParameter("flowError", nativeUrlError);
|
|
321
|
+
if (falseIntent) {
|
|
322
|
+
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
323
|
+
callback(true);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
callback();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const flowVariables = {
|
|
331
|
+
'x-chatbotToken': requestVariables.chatbotToken,
|
|
332
|
+
'x-project-id': requestVariables.project_id,
|
|
333
|
+
'x-conversation-id': requestVariables.conversation_id,
|
|
334
|
+
'x-department-id': requestVariables.department_id,
|
|
335
|
+
'x-chatbot-name': requestVariables.chatbot_name,
|
|
336
|
+
'x-chatbot-id': this.chatbot.botId,
|
|
337
|
+
'x-user-id': requestVariables.user_id || requestVariables.userLeadId,
|
|
338
|
+
'x-last-user-text': requestVariables.lastUserText,
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
json.servers = this.arrayToObject(action.servers, mcp_integration, flowVariables);
|
|
342
|
+
winston.debug("DirAiPrompt json.servers: ", json.servers);
|
|
315
343
|
if (!json.servers) {
|
|
316
344
|
await this.chatbot.addParameter("flowError", "Can't process MCP Servers");
|
|
317
345
|
if (falseIntent) {
|
|
@@ -322,27 +350,47 @@ class DirAiPrompt {
|
|
|
322
350
|
callback();
|
|
323
351
|
return;
|
|
324
352
|
}
|
|
353
|
+
console.log('json.servers', json.servers);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
// Handle reasoning if enabled
|
|
358
|
+
let apiEndpoint = "/ask";
|
|
359
|
+
|
|
360
|
+
if (action.reasoning === true) {
|
|
361
|
+
let reasoningLevel = 'low';
|
|
362
|
+
if (action.reasoningLevel && reasoningLevels.includes(action.reasoningLevel.toLowerCase())) {
|
|
363
|
+
reasoningLevel = action.reasoningLevel.toLowerCase();
|
|
364
|
+
this.logger.native(`[AI Prompt] Reasoning enabled with level: ${reasoningLevel}`);
|
|
365
|
+
} else {
|
|
366
|
+
this.logger.native(`[AI Prompt] Reasoning enabled with default level: ${reasoningLevel}`);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
apiEndpoint = "/thinking";
|
|
370
|
+
this.logger.native(`[AI Prompt] Reasoning enabled with level: ${reasoningLevel}`);
|
|
371
|
+
winston.debug("DirAiPrompt Reasoning enabled, using /thinking endpoint");
|
|
372
|
+
json.thinking = this.#buildThinkingObject(reasoningLevel, action.max_tokens);
|
|
325
373
|
}
|
|
326
374
|
|
|
327
375
|
winston.debug("DirAiPrompt json: ", json);
|
|
376
|
+
console.log("DirAiPrompt json: ", json);
|
|
328
377
|
|
|
329
378
|
const HTTPREQUEST = {
|
|
330
|
-
url: AI_endpoint +
|
|
379
|
+
url: AI_endpoint + apiEndpoint,
|
|
331
380
|
headers: headers,
|
|
332
381
|
json: json,
|
|
333
382
|
method: 'POST'
|
|
334
383
|
}
|
|
335
384
|
winston.debug("DirAiPrompt HttpRequest: ", HTTPREQUEST);
|
|
336
|
-
|
|
337
385
|
httpUtils.request(
|
|
338
386
|
HTTPREQUEST, async (err, resbody) => {
|
|
339
387
|
if (err) {
|
|
340
388
|
winston.error("DirAiPrompt openai err: ", err.response?.data);
|
|
341
389
|
await this.#assignAttributes(action, answer);
|
|
342
390
|
let error;
|
|
343
|
-
if (err.response?.data?.detail[0]) {
|
|
391
|
+
if (err.response?.data?.detail && err.response?.data?.detail[0]) {
|
|
344
392
|
error = err.response.data.detail[0]?.msg;
|
|
345
|
-
} else if (err.response?.data?.detail?.answer) {
|
|
393
|
+
} else if (err.response?.data?.detail && err.response?.data?.detail?.answer) {
|
|
346
394
|
error = err.response.data.detail.answer;
|
|
347
395
|
} else if (err.response?.data) {
|
|
348
396
|
error = JSON.stringify(err.response.data);
|
|
@@ -365,6 +413,13 @@ class DirAiPrompt {
|
|
|
365
413
|
answer = resbody.answer;
|
|
366
414
|
this.logger.native("[AI Prompt] answer: ", answer);
|
|
367
415
|
|
|
416
|
+
let reasoning_content = null;
|
|
417
|
+
if (action.reasoning === true) {
|
|
418
|
+
reasoning_content = resbody.reasoning_content;
|
|
419
|
+
this.logger.native("[AI Prompt] reasoning_content: ", reasoning_content);
|
|
420
|
+
await this.chatbot.addParameter("reasoning_content", reasoning_content);
|
|
421
|
+
}
|
|
422
|
+
|
|
368
423
|
if (publicKey === true) {
|
|
369
424
|
let tokens_usage = {
|
|
370
425
|
tokens: resbody.prompt_token_info?.total_tokens || 0,
|
|
@@ -373,7 +428,7 @@ class DirAiPrompt {
|
|
|
373
428
|
quotasService.updateQuote(this.projectId, this.token, tokens_usage);
|
|
374
429
|
}
|
|
375
430
|
|
|
376
|
-
await this.#assignAttributes(action, answer);
|
|
431
|
+
await this.#assignAttributes(action, answer, reasoning_content);
|
|
377
432
|
|
|
378
433
|
if (trueIntent) {
|
|
379
434
|
await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
@@ -493,7 +548,7 @@ class DirAiPrompt {
|
|
|
493
548
|
}
|
|
494
549
|
}
|
|
495
550
|
|
|
496
|
-
async #assignAttributes(action, answer) {
|
|
551
|
+
async #assignAttributes(action, answer, reasoning_content) {
|
|
497
552
|
winston.debug("DirAiPrompt assignAttributes action: ", action)
|
|
498
553
|
winston.debug("DirAiPrompt assignAttributes answer: " + answer)
|
|
499
554
|
|
|
@@ -501,7 +556,45 @@ class DirAiPrompt {
|
|
|
501
556
|
if (action.assignReplyTo && answer) {
|
|
502
557
|
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
|
|
503
558
|
}
|
|
559
|
+
if (action.assignReasoningContentTo && reasoning_content) {
|
|
560
|
+
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReasoningContentTo, reasoning_content);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Builds the thinking object for reasoning based on the level and max_tokens
|
|
567
|
+
* @param {string} level - The reasoning level: 'low', 'medium', or 'high'
|
|
568
|
+
* @param {number} max_tokens - Maximum tokens available
|
|
569
|
+
* @returns {object} The thinking configuration object
|
|
570
|
+
*/
|
|
571
|
+
#buildThinkingObject(level, max_tokens) {
|
|
572
|
+
// Calculate budget_tokens based on level
|
|
573
|
+
let budgetPercentage;
|
|
574
|
+
switch (level) {
|
|
575
|
+
case 'high':
|
|
576
|
+
budgetPercentage = 0.60; // 60%
|
|
577
|
+
break;
|
|
578
|
+
case 'medium':
|
|
579
|
+
budgetPercentage = 0.40; // 40%
|
|
580
|
+
break;
|
|
581
|
+
case 'low':
|
|
582
|
+
default:
|
|
583
|
+
budgetPercentage = 0.20; // 20%
|
|
584
|
+
break;
|
|
504
585
|
}
|
|
586
|
+
|
|
587
|
+
const budget_tokens = Math.floor(max_tokens * budgetPercentage);
|
|
588
|
+
|
|
589
|
+
return {
|
|
590
|
+
show_thinking_stream: true,
|
|
591
|
+
reasoning_effort: level,
|
|
592
|
+
reasoning_summary: "auto",
|
|
593
|
+
type: "enabled",
|
|
594
|
+
budget_tokens: budget_tokens,
|
|
595
|
+
thinkingBudget: budget_tokens,
|
|
596
|
+
thinkingLevel: level
|
|
597
|
+
};
|
|
505
598
|
}
|
|
506
599
|
|
|
507
600
|
async getKeyFromKbSettings() {
|
|
@@ -591,34 +684,226 @@ class DirAiPrompt {
|
|
|
591
684
|
})
|
|
592
685
|
}
|
|
593
686
|
|
|
594
|
-
|
|
687
|
+
// Fields accepted by the LLM server schema. Everything else (id, native,
|
|
688
|
+
// customHeaders, oauth, tools, ...) is internal and must not be forwarded.
|
|
689
|
+
static SERVER_ALLOWED_FIELDS = ['transport', 'url', 'command', 'args', 'api_key', 'headers', 'parameters'];
|
|
690
|
+
|
|
691
|
+
arrayToObject(arr, mcp_integration, flowVariables) {
|
|
595
692
|
if (!Array.isArray(arr)) {
|
|
596
693
|
winston.warn("DirAiPrompt Can't process MCP Severs: 'servers' must be an array")
|
|
597
694
|
this.logger.warn("[AI Prompt] Can't process MCP Severs: 'servers' must be an array");
|
|
598
695
|
return null;
|
|
599
696
|
}
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
697
|
+
|
|
698
|
+
const integrationServers = Array.isArray(mcp_integration?.value?.servers)
|
|
699
|
+
? mcp_integration.value.servers
|
|
700
|
+
: [];
|
|
701
|
+
|
|
702
|
+
return arr.reduce((acc, server) => {
|
|
703
|
+
const integrationServer = this.getIntegrationServer(integrationServers, server);
|
|
704
|
+
|
|
705
|
+
const cleanServer = {};
|
|
706
|
+
for (const field of DirAiPrompt.SERVER_ALLOWED_FIELDS) {
|
|
707
|
+
if (field === 'headers') continue;
|
|
708
|
+
if (server[field] !== undefined) {
|
|
709
|
+
cleanServer[field] = server[field];
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
cleanServer.enabled_tools = this.buildEnabledTools(server);
|
|
714
|
+
|
|
715
|
+
const integrationHeaders = this.customHeadersToObject(integrationServer?.customHeaders);
|
|
716
|
+
const serverFlowVariables = server.native === true ? flowVariables : null;
|
|
717
|
+
cleanServer.headers = this.mergeHeadersWithVariables(integrationHeaders, serverFlowVariables);
|
|
718
|
+
|
|
719
|
+
acc[server.name] = cleanServer;
|
|
603
720
|
return acc;
|
|
604
721
|
}, {});
|
|
605
722
|
}
|
|
606
723
|
|
|
607
|
-
|
|
724
|
+
buildEnabledTools(server) {
|
|
725
|
+
if (!Array.isArray(server?.tools) || server.tools.length === 0) {
|
|
726
|
+
return [];
|
|
727
|
+
}
|
|
728
|
+
return server.tools
|
|
729
|
+
.map(t => (typeof t === 'string' ? t : t?.name))
|
|
730
|
+
.filter(name => typeof name === 'string' && name.length > 0);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
getIntegrationServer(integrationServers, server) {
|
|
734
|
+
if (!Array.isArray(integrationServers) || !server) return null;
|
|
735
|
+
if (server.id) {
|
|
736
|
+
const byId = integrationServers.find(s => s.id === server.id);
|
|
737
|
+
if (byId) return byId;
|
|
738
|
+
}
|
|
739
|
+
if (server.name) {
|
|
740
|
+
return integrationServers.find(s => s.name === server.name) || null;
|
|
741
|
+
}
|
|
742
|
+
return null;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
enrichServersFromIntegration(servers, mcp_integration) {
|
|
608
746
|
const integrationServers = mcp_integration?.value?.servers;
|
|
609
747
|
if (!Array.isArray(servers) || !Array.isArray(integrationServers)) return;
|
|
610
|
-
|
|
611
|
-
// Preindex by name
|
|
612
|
-
const map = new Map(integrationServers.map(s => [s.name, s]));
|
|
613
|
-
|
|
748
|
+
|
|
614
749
|
servers.forEach(server => {
|
|
615
|
-
const integrationServer =
|
|
616
|
-
if (integrationServer
|
|
750
|
+
const integrationServer = this.getIntegrationServer(integrationServers, server);
|
|
751
|
+
if (!integrationServer) return;
|
|
752
|
+
|
|
753
|
+
if (server.native) {
|
|
754
|
+
delete server.url;
|
|
755
|
+
} else if (integrationServer.url) {
|
|
756
|
+
server.url = integrationServer.url;
|
|
757
|
+
}
|
|
758
|
+
if (integrationServer.transport) {
|
|
759
|
+
server.transport = integrationServer.transport;
|
|
760
|
+
}
|
|
761
|
+
if (integrationServer.authorization?.key) {
|
|
617
762
|
server.api_key = integrationServer.authorization.key;
|
|
618
763
|
}
|
|
764
|
+
|
|
765
|
+
const integrationHeaders = this.customHeadersToObject(integrationServer.customHeaders);
|
|
766
|
+
if (Object.keys(integrationHeaders).length > 0) {
|
|
767
|
+
const existingHeaders =
|
|
768
|
+
server.headers &&
|
|
769
|
+
typeof server.headers === 'object' &&
|
|
770
|
+
!Array.isArray(server.headers)
|
|
771
|
+
? server.headers
|
|
772
|
+
: {};
|
|
773
|
+
server.headers = { ...existingHeaders, ...integrationHeaders };
|
|
774
|
+
}
|
|
619
775
|
});
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
customHeadersToObject(customHeaders) {
|
|
779
|
+
if (!Array.isArray(customHeaders)) {
|
|
780
|
+
return {};
|
|
781
|
+
}
|
|
782
|
+
return customHeaders.reduce((acc, header) => {
|
|
783
|
+
if (header?.enabled === false || !header?.key) {
|
|
784
|
+
return acc;
|
|
785
|
+
}
|
|
786
|
+
acc[header.key] = header.value != null ? String(header.value) : '';
|
|
787
|
+
return acc;
|
|
788
|
+
}, {});
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
mergeHeadersWithVariables(existingHeaders, variables) {
|
|
792
|
+
const base =
|
|
793
|
+
existingHeaders &&
|
|
794
|
+
typeof existingHeaders === 'object' &&
|
|
795
|
+
!Array.isArray(existingHeaders)
|
|
796
|
+
? { ...existingHeaders }
|
|
797
|
+
: {};
|
|
798
|
+
for (const key of Object.keys(base)) {
|
|
799
|
+
const v = base[key];
|
|
800
|
+
if (v !== undefined && v !== null && typeof v !== 'string') {
|
|
801
|
+
base[key] = typeof v === 'object' ? JSON.stringify(v) : String(v);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
if (!variables || typeof variables !== 'object' || Array.isArray(variables)) {
|
|
805
|
+
return base;
|
|
806
|
+
}
|
|
807
|
+
for (const key of Object.keys(variables)) {
|
|
808
|
+
try {
|
|
809
|
+
const v = variables[key];
|
|
810
|
+
if (v === undefined || typeof v === 'function') {
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
if (v === null) {
|
|
814
|
+
base[key] = '';
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
base[key] = typeof v === 'object' ? JSON.stringify(v) : String(v);
|
|
818
|
+
} catch (err) {
|
|
819
|
+
winston.warn("DirAiPrompt mergeHeadersWithVariables skip key:", key, err);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
return base;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
async resolveNativeServerUrls(servers) {
|
|
826
|
+
if (!Array.isArray(servers)) return null;
|
|
827
|
+
|
|
828
|
+
const nativeServers = servers.filter(server => server.native === true);
|
|
829
|
+
if (nativeServers.length === 0) return null;
|
|
830
|
+
|
|
831
|
+
let nativeMcpCache = await this.getNativeMcpServersFromCache();
|
|
832
|
+
if (!nativeMcpCache) {
|
|
833
|
+
await this.fetchNativeMcpServers();
|
|
834
|
+
nativeMcpCache = await this.getNativeMcpServersFromCache();
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
if (!nativeMcpCache) {
|
|
838
|
+
this.logger.error("[AI Prompt] native MCP servers cache not found");
|
|
839
|
+
winston.error("DirAiPrompt native MCP servers cache not found");
|
|
840
|
+
return "AiPrompt Error: native MCP servers not available";
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
for (const server of nativeServers) {
|
|
844
|
+
const cachedServer = this.findNativeServerInCache(nativeMcpCache, server.id);
|
|
845
|
+
if (cachedServer?.url) {
|
|
846
|
+
server.url = cachedServer.url;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
const unresolved = nativeServers.filter(server => !server.url);
|
|
851
|
+
if (unresolved.length > 0) {
|
|
852
|
+
const names = unresolved.map(server => server.name || server.id).join(", ");
|
|
853
|
+
this.logger.error("[AI Prompt] native MCP server url not found for: ", names);
|
|
854
|
+
winston.error("DirAiPrompt native MCP server url not found for: ", names);
|
|
855
|
+
return "AiPrompt Error: native MCP server url not found for " + names;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
return null;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
async getNativeMcpServersFromCache() {
|
|
862
|
+
try {
|
|
863
|
+
const cached = await this.tdcache.get(NATIVE_MCP_CACHE_KEY);
|
|
864
|
+
if (!cached) return null;
|
|
865
|
+
return JSON.parse(cached);
|
|
866
|
+
} catch (err) {
|
|
867
|
+
this.logger.error("[AI Prompt] Error reading native MCP cache: ", err);
|
|
868
|
+
winston.error("DirAiPrompt Error reading native MCP cache: ", err);
|
|
869
|
+
return null;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
findNativeServerInCache(cache, serverId) {
|
|
874
|
+
if (!cache || !serverId) return null;
|
|
875
|
+
|
|
876
|
+
if (Array.isArray(cache)) {
|
|
877
|
+
return cache.find(server => server.id === serverId);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
if (typeof cache === 'object') {
|
|
881
|
+
return cache[serverId];
|
|
882
|
+
}
|
|
620
883
|
|
|
621
|
-
return
|
|
884
|
+
return null;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
async fetchNativeMcpServers() {
|
|
888
|
+
return new Promise((resolve) => {
|
|
889
|
+
const HTTPREQUEST = {
|
|
890
|
+
url: this.API_ENDPOINT + "/" + this.projectId + "/mcp/native",
|
|
891
|
+
headers: {
|
|
892
|
+
'Content-Type': 'application/json',
|
|
893
|
+
'Authorization': 'JWT ' + this.token
|
|
894
|
+
},
|
|
895
|
+
method: "GET"
|
|
896
|
+
};
|
|
897
|
+
winston.debug("DirAiPrompt fetch native MCP servers HttpRequest", HTTPREQUEST);
|
|
898
|
+
|
|
899
|
+
httpUtils.request(HTTPREQUEST, (err) => {
|
|
900
|
+
if (err) {
|
|
901
|
+
this.logger.error("[AI Prompt] Error fetching native MCP servers: ", err);
|
|
902
|
+
winston.error("DirAiPrompt Error fetching native MCP servers: ", err);
|
|
903
|
+
}
|
|
904
|
+
resolve();
|
|
905
|
+
});
|
|
906
|
+
});
|
|
622
907
|
}
|
|
623
908
|
|
|
624
909
|
async detectAttach(source) {
|
|
@@ -420,6 +420,7 @@ class DirAskGPTV2 {
|
|
|
420
420
|
}
|
|
421
421
|
else if (resbody.success === true) {
|
|
422
422
|
winston.debug("DirAskGPTV2 resbody: ", resbody);
|
|
423
|
+
console.log("DirAskGPTV2 resbody: ", JSON.stringify(resbody));
|
|
423
424
|
if (chunks_only) {
|
|
424
425
|
await this.#assignAttributes(action, resbody.answer, resbody.source, resbody.chunks);
|
|
425
426
|
if (trueIntent) {
|