@tiledesk/tiledesk-tybot-connector 2.5.0 → 2.6.0

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +1 -52
  2. package/ExtUtil.js +1 -0
  3. package/TdCache copy.js +242 -0
  4. package/TiledeskExpression.js +1 -6
  5. package/TiledeskWhenExpression.js +1 -18
  6. package/engine/MongodbBotsDataSource.js +1 -3
  7. package/engine/MongodbIntentsMachine.js +2 -3
  8. package/engine/TiledeskChatbot.js +4 -76
  9. package/engine/TiledeskChatbotConst.js +1 -7
  10. package/engine/mock/MockBotsDataSource.js +6 -11
  11. package/index.js +36 -132
  12. package/models/faq_kb.js +0 -11
  13. package/package.json +1 -1
  14. package/services/AIController.js +1 -31
  15. package/services/AIService.js +33 -62
  16. package/services/IntegrationService.js +1 -4
  17. package/services/{LLMService.js → KbService.js} +3 -3
  18. package/services/RequestService.js +2 -23
  19. package/services/TilebotService.js +3 -19
  20. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +17 -34
  21. package/tiledeskChatbotPlugs/directives/DirAiCondition.js +51 -18
  22. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +52 -103
  23. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +5 -108
  24. package/tiledeskChatbotPlugs/directives/DirCallSubagent.js +8 -34
  25. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +11 -36
  26. package/tiledeskChatbotPlugs/directives/DirGptTask.js +4 -16
  27. package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -12
  28. package/tiledeskChatbotPlugs/directives/DirIteration.js +10 -63
  29. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +16 -69
  30. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +2 -40
  31. package/tiledeskChatbotPlugs/directives/DirReply.js +6 -81
  32. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +4 -58
  33. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +0 -8
  34. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +1 -11
  35. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +418 -0
  36. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +2 -5
  37. package/tiledeskChatbotPlugs/directives/Directives.js +0 -6
  38. package/utils/HttpUtils.js +17 -99
  39. package/utils/TiledeskChatbotUtil.js +5 -73
  40. package/utils/winston.js +2 -80
  41. package/services/InternalSubAgentService.js +0 -330
  42. package/tiledeskChatbotPlugs/directives/DirInviteHuman.js +0 -259
  43. package/tiledeskChatbotPlugs/directives/DirInvokeSubAgent.js +0 -345
  44. package/tiledeskChatbotPlugs/directives/DirJSONConditionMulti.js +0 -171
  45. package/tiledeskChatbotPlugs/directives/DirRemoveHuman.js +0 -129
  46. package/tiledeskChatbotPlugs/directives/DirReturn.js +0 -98
  47. package/utils/ConnectorsV3.js +0 -292
  48. package/utils/LlmStream.js +0 -215
  49. package/utils/SentenceChunker.js +0 -94
  50. package/utils/StreamSender.js +0 -109
@@ -23,7 +23,6 @@ const { DirAssignFromFunction } = require('./directives/DirAssignFromFunction');
23
23
  const { DirCondition } = require('./directives/DirCondition');
24
24
  const { DirJSONCondition } = require('./directives/DirJSONCondition');
25
25
  const { DirJSONConditionV2 } = require('./directives/DirJSONConditionV2');
26
- const { DirJSONConditionMulti } = require('./directives/DirJSONConditionMulti');
27
26
  const { DirAssign } = require('./directives/DirAssign');
28
27
  const { DirSetAttribute } = require('./directives/DirSetAttribute');
29
28
  const { DirSetAttributeV2 } = require('./directives/DirSetAttributeV2');
@@ -51,8 +50,6 @@ const { DirDocparseExtract } = require('./directives/DirDocparseExtract');
51
50
  const { DirAssistant } = require('./directives/DirAssistant');
52
51
  const { DirReplyV2 } = require('./directives/DirReplyV2');
53
52
  const { DirIfOnlineAgentsV2 } = require('./directives/DirIfOnlineAgentsV2');
54
- const { DirInviteHuman } = require('./directives/DirInviteHuman');
55
- const { DirRemoveHuman } = require('./directives/DirRemoveHuman');
56
53
  const { DirContactUpdate } = require('./directives/DirContactUpdate');
57
54
  const { DirClearTranscript } = require('./directives/DirClearTranscript');
58
55
  const { DirMoveToUnassigned } = require('./directives/DirMoveToUnassigned');
@@ -63,17 +60,15 @@ const { DirAiTask, DirAiPrompt } = require('./directives/DirAiPrompt');
63
60
  const { DirWebResponse } = require('./directives/DirWebResponse');
64
61
  const { DirConnectBlock } = require('./directives/DirConnectBlock');
65
62
  const { DirAiCondition } = require('./directives/DirAiCondition');
66
- const { DirAddKbContent } = require('./directives/DirAddKbContent');
63
+
64
+ const winston = require('../utils/winston');
67
65
  const { DirFlowLog } = require('./directives/DirFlowLog');
66
+ const { DirAddKbContent } = require('./directives/DirAddKbContent');
68
67
  const { DirIteration } = require('./directives/DirIteration');
69
68
  const { AnalyticsClient } = require('../AnalyticsClient');
70
- const { DirCallSubagent } = require('./directives/DirCallSubagent');
69
+ const { DirCallSubAgent } = require('./directives/DirCallSubagent');
71
70
  const { DirReturnStack } = require('./directives/DirReturnStack');
72
71
  const { DirDataTables } = require('./directives/DirDataTables');
73
- const { DirInvokeSubAgent } = require('./directives/DirInvokeSubAgent');
74
- const { DirReturn } = require('./directives/DirReturn');
75
-
76
- const winston = require('../utils/winston');
77
72
 
78
73
  class DirectivesChatbotPlug {
79
74
 
@@ -94,8 +89,6 @@ class DirectivesChatbotPlug {
94
89
  this.reply = config.reply;
95
90
  this.chatbot = config.chatbot;
96
91
  this.message = config.message;
97
- // Set only on streaming channels (voice): id of the caller message that started this turn
98
- this.turnId = config.turnId;
99
92
  }
100
93
 
101
94
  exec(pipeline) {
@@ -131,14 +124,15 @@ class DirectivesChatbotPlug {
131
124
  }
132
125
 
133
126
  async processDirectives(theend) {
134
- this.theend = typeof theend === 'function' ? theend : () => {};
127
+ this.theend = theend;
135
128
  const directives = this.directives;
136
129
  if (!directives || directives.length === 0) {
137
130
  winston.verbose("(DirectivesChatbotPlug) No directives to process.");
138
131
  this.theend();
139
132
  return;
140
133
  }
141
- const supportRequest = this.supportRequest;
134
+
135
+ const supportRequest = this.supportRequest;
142
136
  const token = this.token;
143
137
  const API_ENDPOINT = this.API_ENDPOINT;
144
138
  const TILEBOT_ENDPOINT = this.TILEBOT_ENDPOINT;
@@ -160,11 +154,11 @@ class DirectivesChatbotPlug {
160
154
  APIKEY: "___"
161
155
  });
162
156
  }
163
- catch (err) {
157
+ catch(err) {
164
158
  winston.error("(DirectivesChatbotPlug) An error occurred while creating TiledeskClient in DirectivesChatbotPlug: ", err);
165
159
  }
166
160
 
167
- this.context = {
161
+ this.context = {
168
162
  projectId: projectId,
169
163
  chatbot: this.chatbot,
170
164
  message: this.message,
@@ -176,14 +170,13 @@ class DirectivesChatbotPlug {
176
170
  TILEBOT_ENDPOINT: TILEBOT_ENDPOINT,
177
171
  departmentId: depId,
178
172
  tdcache: tdcache,
179
- HELP_CENTER_API_ENDPOINT: this.HELP_CENTER_API_ENDPOINT,
180
- turnId: this.turnId
181
- };
173
+ HELP_CENTER_API_ENDPOINT: this.HELP_CENTER_API_ENDPOINT
174
+ }
182
175
  winston.debug("(DirectivesChatbotPlug) this.context.departmentId: " + this.context.departmentId);
183
-
176
+
184
177
  this.curr_directive_index = -1;
185
178
  winston.verbose("(DirectivesChatbotPlug) processing directives...");
186
-
179
+
187
180
  const next_dir = await this.nextDirective(directives);
188
181
  winston.debug("(DirectivesChatbotPlug) next_dir: ", next_dir);
189
182
  await this.process(next_dir);
@@ -208,11 +201,6 @@ class DirectivesChatbotPlug {
208
201
  winston.debug("(DirectivesChatbotPlug) go_on == false! nextDirective() Stopped!");
209
202
  return this.errorMessage(go_on.error); //"Request error: anomaly detection. MAX ACTIONS exeeded.");
210
203
  }
211
- // Latest message wins: a newer caller message started another turn, stop this one silently
212
- if (this.turnId && await TiledeskChatbot.isTurnSuperseded(this.context.tdcache, this.context.requestId, this.turnId)) {
213
- winston.verbose("(DirectivesChatbotPlug) turn " + this.turnId + " superseded on " + this.context.requestId + ", flow stopped");
214
- return null;
215
- }
216
204
  this.curr_directive_index += 1;
217
205
  if (this.curr_directive_index < directives.length) {
218
206
  let nextd = directives[this.curr_directive_index];
@@ -251,8 +239,8 @@ class DirectivesChatbotPlug {
251
239
 
252
240
  const directive_name = directive.name.toLowerCase();
253
241
 
254
- // Controllo lock action (internal jumps to another block are not subject to it: the lock stays for the user's reply)
255
- if (directive.action && this.message?.sender !== "_tdinternal") {
242
+ // Controllo lock action
243
+ if (directive.action) {
256
244
  const action_id = directive.action["_tdActionId"];
257
245
  const locked_action_id = await this.chatbot.currentLockedAction(this.supportRequest.request_id);
258
246
  if (locked_action_id && locked_action_id !== action_id) {
@@ -282,7 +270,6 @@ class DirectivesChatbotPlug {
282
270
  [Directives.FUNCTION_VALUE]: DirAssignFromFunction,
283
271
  [Directives.JSON_CONDITION]: DirJSONCondition,
284
272
  [Directives.JSON_CONDITION_2]: DirJSONConditionV2, // NEW: JSON Condition V2 (tipo dedicato dal DS); V1 dispatch invariato
285
- [Directives.JSON_CONDITION_MULTI]: DirJSONConditionMulti, // NEW: condizione a piu' casi; V1 e V2 dispatch invariati
286
273
  [Directives.ASSIGN]: DirAssign,
287
274
  [Directives.SET_ATTRIBUTE]: DirSetAttribute,
288
275
  [Directives.SET_ATTRIBUTE_V2]: DirSetAttributeV2,
@@ -292,7 +279,7 @@ class DirectivesChatbotPlug {
292
279
  [Directives.REPLACE_BOT]: DirReplaceBot,
293
280
  [Directives.REPLACE_BOT_V2]: DirReplaceBotV2,
294
281
  [Directives.REPLACE_BOT_V3]: DirReplaceBotV3,
295
- [Directives.CALL_SUBAGENT]: DirCallSubagent,
282
+ [Directives.CALL_SUBAGENT]: DirCallSubAgent,
296
283
  [Directives.RETURN_STACK]: DirReturnStack,
297
284
  [Directives.WAIT]: DirWait,
298
285
  [Directives.LOCK_INTENT]: DirLockIntent,
@@ -329,11 +316,7 @@ class DirectivesChatbotPlug {
329
316
  [Directives.WEB_RESPONSE]: DirWebResponse,
330
317
  [Directives.FLOW_LOG]: DirFlowLog,
331
318
  [Directives.ITERATION]: DirIteration,
332
- [Directives.INVOKE_SUB_AGENT]: DirInvokeSubAgent,
333
- [Directives.RETURN]: DirReturn,
334
319
  [Directives.DATA_TABLES]: DirDataTables,
335
- [Directives.INVITE_HUMAN]: DirInviteHuman,
336
- [Directives.REMOVE_HUMAN]: DirRemoveHuman,
337
320
  };
338
321
 
339
322
  const HandlerClass = handlers[directive_name];
@@ -344,7 +327,7 @@ class DirectivesChatbotPlug {
344
327
 
345
328
  const handler = new HandlerClass(context);
346
329
 
347
- winston.verbose("(DirectivesChatbotPlug) Execut directive: " + directive_name);
330
+ // Esegue l'handler e chiama next se non stop
348
331
 
349
332
  const blockStart = Date.now();
350
333
  handler.execute(directive, async (stop) => {
@@ -11,7 +11,6 @@ const Utils = require("../../utils/HttpUtils");
11
11
  const utils = require("../../utils/HttpUtils");
12
12
  const httpUtils = require("../../utils/HttpUtils");
13
13
  const integrationService = require("../../services/IntegrationService");
14
- const quotasService = require("../../services/QuotasService");
15
14
  const aiController = require("../../services/AIController");
16
15
  const { Logger } = require("../../Logger");
17
16
  const { randomUUID } = require("crypto");
@@ -124,7 +123,7 @@ class DirAiCondition {
124
123
  let key;
125
124
  let publicKey = false;
126
125
  let ollama_integration;
127
- let vllm_model;
126
+ let vllm_server_config;
128
127
  let openrouter_model;
129
128
  let agentplatform_model;
130
129
 
@@ -143,18 +142,14 @@ class DirAiCondition {
143
142
  });
144
143
 
145
144
  } else if(action.llm === 'vllm'){
146
- try {
147
- const filled_vllm_server = filler.fill(action.vllmServer, requestVariables);
148
- vllm_model = await aiController.resolveLLMConfig(
149
- this.projectId, action.llm, filled_model, this.token, filled_vllm_server);
150
- key = vllm_model.api_key;
151
- } catch (err) {
152
- const errorMsg = err?.error || err?.message || "vllm integration not found";
153
- this.logger.error("[AI Condition] vllm integration not resolved: ", errorMsg);
154
- winston.error("Error: DirAiCondition vllm integration not resolved: " + errorMsg);
155
- await this.chatbot.addParameter("flowError", "AiCondition Error: " + errorMsg);
156
- if (errorIntent) {
157
- await this.#executeIntent(errorIntent);
145
+ const vllm_integration = await integrationService.getIntegration(this.projectId, action.llm, this.token);
146
+
147
+ if (!vllm_integration?.value) {
148
+ this.logger.error("[AI Condition] Error getting vllm integration.");
149
+ winston.error("DirAiCondition Error getting vllm integration");
150
+ await this.chatbot.addParameter("flowError", "Vllm integration not found");
151
+ if (falseIntent) {
152
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
158
153
  callback(true);
159
154
  return;
160
155
  }
@@ -162,12 +157,43 @@ class DirAiCondition {
162
157
  return;
163
158
  }
164
159
 
160
+ const vllm_value = vllm_integration.value;
161
+ if (Array.isArray(vllm_value.servers)) {
162
+ const filled_vllm_server = filler.fill(action.vllmServer, requestVariables);
163
+ if (!filled_vllm_server) {
164
+ this.logger.error("[AI Condition] missing vllmServer for multi-server vllm integration");
165
+ await this.chatbot.addParameter("flowError", "AiCondition Error: 'vllmServer' attribute is undefined");
166
+ if (falseIntent) {
167
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
168
+ callback(true);
169
+ return;
170
+ }
171
+ callback();
172
+ return;
173
+ }
174
+ vllm_server_config = vllm_value.servers.find(s => s.name === filled_vllm_server);
175
+ if (!vllm_server_config) {
176
+ this.logger.error("[AI Condition] vllm server not found: ", filled_vllm_server);
177
+ await this.chatbot.addParameter("flowError", "AiCondition Error: vllm server '" + filled_vllm_server + "' not found");
178
+ if (falseIntent) {
179
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
180
+ callback(true);
181
+ return;
182
+ }
183
+ callback();
184
+ return;
185
+ }
186
+ key = vllm_server_config.apikey;
187
+ } else {
188
+ key = vllm_value.apikey;
189
+ }
190
+
165
191
  if (!key) {
166
192
  this.logger.error("[AI Condition] llm key not found in vllm integration");
167
193
  winston.error("Error: DirAiCondition llm key not found in vllm integration");
168
194
  await this.chatbot.addParameter("flowError", "AiCondition Error: missing key for llm vllm");
169
- if (errorIntent) {
170
- await this.#executeIntent(errorIntent);
195
+ if (falseIntent) {
196
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
171
197
  callback(true);
172
198
  return;
173
199
  }
@@ -308,8 +334,15 @@ class DirAiCondition {
308
334
 
309
335
  }
310
336
 
311
- if (action.llm === 'vllm') {
312
- json.model = vllm_model;
337
+ if (action.llm === 'vllm' && vllm_server_config) {
338
+ console.log("llm: vllm")
339
+ json.model = {
340
+ name: filled_model,
341
+ url: vllm_server_config.url,
342
+ api_key: vllm_server_config.apikey || vllm_server_config.token || null,
343
+ provider: 'vllm'
344
+ }
345
+ console.log("set json.model to: ", json.model);
313
346
  }
314
347
 
315
348
  if (action.llm === 'openrouter') {
@@ -14,8 +14,6 @@ const assert = require("assert");
14
14
  const quotasService = require("../../services/QuotasService");
15
15
  const path = require("path");
16
16
  const mime = require("mime-types");
17
- const { TiledeskClient } = require('@tiledesk/tiledesk-client');
18
- const { LlmStream } = require("../../utils/LlmStream");
19
17
 
20
18
  const NATIVE_MCP_CACHE_KEY = 'native_mcp:servers';
21
19
  const reasoningLevels = ['low', 'medium', 'high'];
@@ -125,7 +123,7 @@ class DirAiPrompt {
125
123
  let key;
126
124
  let publicKey = false;
127
125
  let ollama_integration;
128
- let vllm_model;
126
+ let vllm_server_config;
129
127
  let openrouter_model;
130
128
  let agentplatform_model;
131
129
 
@@ -144,16 +142,12 @@ class DirAiPrompt {
144
142
  });
145
143
 
146
144
  } else if (action.llm === 'vllm') {
147
- try {
148
- const filled_vllm_server = filler.fill(action.vllmServer, requestVariables);
149
- vllm_model = await aiController.resolveLLMConfig(
150
- this.projectId, action.llm, filled_model, this.token, filled_vllm_server);
151
- key = vllm_model.api_key;
152
- } catch (err) {
153
- const errorMsg = err?.error || err?.message || "vllm integration not found";
154
- this.logger.error("[AI Prompt] vllm integration not resolved: ", errorMsg);
155
- winston.error("Error: DirAiPrompt vllm integration not resolved: " + errorMsg);
156
- await this.chatbot.addParameter("flowError", "AiPrompt Error: " + errorMsg);
145
+ const vllm_integration = await integrationService.getIntegration(this.projectId, action.llm, this.token);
146
+
147
+ if (!vllm_integration?.value) {
148
+ this.logger.error("[AI Prompt] Error getting vllm integration.");
149
+ winston.error("DirAiPrompt Error getting vllm integration");
150
+ await this.chatbot.addParameter("flowError", "Vllm integration not found");
157
151
  if (falseIntent) {
158
152
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
159
153
  callback(true);
@@ -163,6 +157,37 @@ class DirAiPrompt {
163
157
  return;
164
158
  }
165
159
 
160
+ const vllm_value = vllm_integration.value;
161
+ if (Array.isArray(vllm_value.servers)) {
162
+ const filled_vllm_server = filler.fill(action.vllmServer, requestVariables);
163
+ if (!filled_vllm_server) {
164
+ this.logger.error("[AI Prompt] missing vllmServer for multi-server vllm integration");
165
+ await this.chatbot.addParameter("flowError", "AiPrompt Error: 'vllmServer' attribute is undefined");
166
+ if (falseIntent) {
167
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
168
+ callback(true);
169
+ return;
170
+ }
171
+ callback();
172
+ return;
173
+ }
174
+ vllm_server_config = vllm_value.servers.find(s => s.name === filled_vllm_server);
175
+ if (!vllm_server_config) {
176
+ this.logger.error("[AI Prompt] vllm server not found: ", filled_vllm_server);
177
+ await this.chatbot.addParameter("flowError", "AiPrompt Error: vllm server '" + filled_vllm_server + "' not found");
178
+ if (falseIntent) {
179
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
180
+ callback(true);
181
+ return;
182
+ }
183
+ callback();
184
+ return;
185
+ }
186
+ key = vllm_server_config.apikey;
187
+ } else {
188
+ key = vllm_value.apikey;
189
+ }
190
+
166
191
  if (!key) {
167
192
  this.logger.error("[AI Prompt] llm key not found in vllm integration");
168
193
  winston.error("Error: DirAiPrompt llm key not found in vllm integration");
@@ -309,8 +334,15 @@ class DirAiPrompt {
309
334
 
310
335
  }
311
336
 
312
- if (action.llm === 'vllm') {
313
- json.model = vllm_model;
337
+ if (action.llm === 'vllm' && vllm_server_config) {
338
+ console.log("llm: vllm")
339
+ json.model = {
340
+ name: filled_model,
341
+ url: vllm_server_config.url,
342
+ api_key: vllm_server_config.apikey || vllm_server_config.token || null,
343
+ provider: 'vllm'
344
+ }
345
+ console.log("set json.model to: ", json.model);
314
346
  }
315
347
 
316
348
  if (action.llm === 'openrouter') {
@@ -372,9 +404,7 @@ class DirAiPrompt {
372
404
  };
373
405
 
374
406
  json.servers = this.arrayToObject(action.servers, mcp_integration, flowVariables);
375
- json.tools = [];
376
- // server definitions can carry credentials (headers, tokens): log the names only
377
- winston.info("DirAiPrompt json.servers: " + (json.servers ? Object.keys(json.servers).join(",") : json.servers));
407
+ winston.debug("DirAiPrompt json.servers: ", json.servers);
378
408
  if (!json.servers) {
379
409
  await this.chatbot.addParameter("flowError", "Can't process MCP Servers");
380
410
  if (falseIntent) {
@@ -385,6 +415,7 @@ class DirAiPrompt {
385
415
  callback();
386
416
  return;
387
417
  }
418
+ console.log('json.servers', json.servers);
388
419
  }
389
420
 
390
421
 
@@ -406,16 +437,8 @@ class DirAiPrompt {
406
437
  json.thinking = this.#buildThinkingObject(reasoningLevel, action.max_tokens);
407
438
  }
408
439
 
409
- // never log the provider key
410
- const loggableJson = { ...json, llm_key: json.llm_key ? "***" : json.llm_key };
411
- if (loggableJson.servers) {
412
- loggableJson.servers = Object.keys(loggableJson.servers);
413
- }
414
- if (loggableJson.model && typeof loggableJson.model === 'object') {
415
- loggableJson.model = { ...loggableJson.model };
416
- ['api_key', 'token'].forEach((k) => { if (loggableJson.model[k]) loggableJson.model[k] = "***"; });
417
- }
418
- winston.info("DirAiPrompt json: "+ JSON.stringify(loggableJson));
440
+ winston.debug("DirAiPrompt json: ", json);
441
+ console.log("DirAiPrompt json: ", json);
419
442
 
420
443
  const HTTPREQUEST = {
421
444
  url: AI_endpoint + apiEndpoint,
@@ -423,14 +446,7 @@ class DirAiPrompt {
423
446
  json: json,
424
447
  method: 'POST'
425
448
  }
426
- winston.debug("DirAiPrompt HttpRequest: ", { ...HTTPREQUEST, json: loggableJson });
427
-
428
- // Voice turn: stream the answer to the caller while the LLM generates it (plain /ask only)
429
- if (this.context.turnId && apiEndpoint === "/ask" && !json.servers) {
430
- await this.#goStreaming(action, json, AI_endpoint + apiEndpoint, headers, publicKey,
431
- trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback);
432
- return;
433
- }
449
+ winston.debug("DirAiPrompt HttpRequest: ", HTTPREQUEST);
434
450
  httpUtils.request(
435
451
  HTTPREQUEST, async (err, resbody) => {
436
452
  if (err) {
@@ -552,73 +568,6 @@ class DirAiPrompt {
552
568
  return objectTranscript;
553
569
  }
554
570
 
555
- /**
556
- * Streaming variant of the /ask call for voice turns. tiledesk-llm answers with SSE (chunk events, then a
557
- * "completed" metadata event); deltas are grouped by SentenceChunker and posted to tiledesk-server /stream by
558
- * StreamSender while the LLM is still generating. The flow then continues exactly as the non-streaming path
559
- * (assign reply attribute, true/false intent); the text streamed is saved as "pending" so that the reply that
560
- * speaks it (DirReply) carries attributes.stream_id and the channel does not speak it twice.
561
- * If a newer caller message supersedes the turn, the LLM request is aborted, the part already streamed is posted
562
- * as a message with attributes.interrupted and the flow stops.
563
- */
564
- async #goStreaming(action, json, url, headers, publicKey, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
565
- const stream = new LlmStream(this, { label: "DirAiPrompt" });
566
- this.logger.native("[AI Prompt] streaming answer " + stream.streamId);
567
- const result = await stream.run(url, json, headers);
568
-
569
- if (result.stopped === "superseded") {
570
- this.logger.native("[AI Prompt] turn superseded, answer interrupted");
571
- winston.verbose("DirAiPrompt stream " + stream.streamId + " superseded after " + result.chunks + " chunks");
572
- await stream.sendInterrupted();
573
- callback(true);
574
- return;
575
- }
576
- if (result.stopped === "closed") {
577
- winston.verbose("DirAiPrompt stream " + stream.streamId + ": request closed, flow stopped");
578
- callback(true);
579
- return;
580
- }
581
-
582
- if (result.error) {
583
- const status = result.error.response && result.error.response.status;
584
- const error = status ? "LLM stream error: HTTP " + status : (result.error.message || "General error executing action");
585
- winston.error("DirAiPrompt error executing action: " + error);
586
- this.logger.error("[AI Prompt] error executing action: ", error);
587
- await this.#assignAttributes(action, "No answer");
588
- if (falseIntent) {
589
- await this.chatbot.addParameter("flowError", "AiPrompt Error: " + error);
590
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
591
- callback(true);
592
- return;
593
- }
594
- callback();
595
- return;
596
- }
597
-
598
- const completed = result.completed;
599
- const answer = (completed && typeof completed.full_response === "string") ? completed.full_response : result.text;
600
- this.logger.native("[AI Prompt] answer: ", answer);
601
-
602
- if (publicKey === true) {
603
- const usage = completed && completed.model_used && completed.model_used.prompt_token_info;
604
- quotasService.updateQuote(this.projectId, this.token, { tokens: (usage && usage.total_tokens) || 0, model: json.model });
605
- }
606
-
607
- await stream.savePending(answer);
608
- await this.#assignAttributes(action, answer);
609
- if (trueIntent) {
610
- await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
611
- callback(true);
612
- return;
613
- }
614
- callback();
615
- }
616
-
617
- /** Reads a text/event-stream body (kept for callers of the previous API: see utils/LlmStream). */
618
- static readSSE(stream, onEvent) {
619
- return LlmStream.readSSE(stream, onEvent);
620
- }
621
-
622
571
  async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
623
572
  let trueIntentDirective = null;
624
573
  if (trueIntent) {
@@ -12,6 +12,7 @@ const winston = require('../../utils/winston');
12
12
  const httpUtils = require("../../utils/HttpUtils");
13
13
  const integrationService = require("../../services/IntegrationService");
14
14
  const { Logger } = require("../../Logger");
15
+ const kbService = require("../../services/KbService");
15
16
  const quotasService = require("../../services/QuotasService");
16
17
  const aiController = require("../../services/AIController");
17
18
  const namespaceService = require("../../services/NamespaceService");
@@ -20,8 +21,6 @@ const default_engine_hybrid = require('../../config/kb/engine.hybrid');
20
21
  const default_embedding = require("../../config/kb/embedding");
21
22
  const PromptManager = require('../../config/kb/prompt/rag/PromptManager');
22
23
  const { MODELS_MULTIPLIER } = require("../../utils/aiUtils");
23
- const llmService = require("../../services/LLMService");
24
- const { LlmStream } = require("../../utils/LlmStream");
25
24
 
26
25
  //const ragPromptManager = new PromptManager(path.join(__dirname, '../../config/kb/prompt/rag'));
27
26
  const ragPromptManager = new PromptManager(path.join(__dirname, '../../config/kb/prompt/rag'));
@@ -358,10 +357,6 @@ class DirAskGPTV2 {
358
357
  json.reranking_multiplier = calculatedRerankingMultiplier;
359
358
  }
360
359
  }
361
-
362
- if (ns.embeddings?.embedding_qa) {
363
- json.embedding = ns.embeddings.embedding_qa;
364
- }
365
360
 
366
361
  if (!action.advancedPrompt) {
367
362
  const contextTemplate = getRagContextTemplate(model.name);
@@ -392,6 +387,7 @@ class DirAskGPTV2 {
392
387
  }
393
388
 
394
389
  winston.debug("DirAskGPTV2 json:", json);
390
+ console.log("DirAskGPTV2 json:", JSON.stringify(json));
395
391
 
396
392
  let kb_endpoint = process.env.KB_ENDPOINT_QA;
397
393
  if (ns.hybrid === true) {
@@ -410,13 +406,6 @@ class DirAskGPTV2 {
410
406
  }
411
407
  winston.debug("DirAskGPTV2 HttpRequest: ", HTTPREQUEST);
412
408
 
413
- // Voice turn: speak the answer while tiledesk-llm generates it (chunks_only returns no answer: nothing to stream)
414
- if (this.context.turnId && !chunks_only) {
415
- await this.#goStreaming(action, json, HTTPREQUEST.url, HTTPREQUEST.headers, publicKey, answer, skip_unanswered,
416
- trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback);
417
- return;
418
- }
419
-
420
409
  httpUtils.request(
421
410
  HTTPREQUEST, async (err, resbody) => {
422
411
  if (err) {
@@ -478,7 +467,7 @@ class DirAskGPTV2 {
478
467
  request_id: this.requestId,
479
468
  tokens: tokens
480
469
  }
481
- llmService.addAnsweredQuestion(this.projectId, data, this.token).catch((err) => {
470
+ kbService.addAnsweredQuestion(this.projectId, data, this.token).catch((err) => {
482
471
  winston.error("Error adding answered question: ", err);
483
472
  })
484
473
 
@@ -499,7 +488,8 @@ class DirAskGPTV2 {
499
488
  question: json.question,
500
489
  request_id: this.requestId
501
490
  }
502
- llmService.addUnansweredQuestion(this.projectId, data, this.token).catch((err) => {
491
+
492
+ kbService.addUnansweredQuestion(this.projectId, data, this.token).catch((err) => {
503
493
  winston.error("DirAskGPTV2 - Error adding unanswered question: ", {
504
494
  status: err.response?.status,
505
495
  statusText: err.response?.statusText,
@@ -520,99 +510,6 @@ class DirAskGPTV2 {
520
510
  )
521
511
  }
522
512
 
523
- /**
524
- * Streaming variant of the /qa call for voice turns (see utils/LlmStream): sentences are spoken while the model
525
- * generates them; the "completed" event carries the usual /qa result (model_used), and the flow continues as in the
526
- * non-streaming path (attributes, quota, answered/unanswered question, true/false intent). Differences on voice:
527
- * - citations are not requested (a voice does not read them; the markers would be spoken);
528
- * - a knowledge-base miss is marked by the model with "<NOANS>" at the very end, after the text was spoken: the
529
- * marker is never spoken, the spoken text becomes the answer attribute (so a reply of that attribute closes the
530
- * stream instead of speaking the fallback on top of it), the question is logged as unanswered and the false branch
531
- * runs. If nothing was spoken, the configured fallback is used as in the non-streaming path.
532
- * A newer caller message aborts the request: the part already spoken is posted as interrupted and the flow stops.
533
- */
534
- async #goStreaming(action, json, url, headers, publicKey, noAnswer, skip_unanswered, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
535
- const stream = new LlmStream(this, { label: "DirAskGPTV2", holdMarker: "<NOANS>" });
536
- this.logger.native("[Ask Knowledge Base] streaming answer " + stream.streamId);
537
- const result = await stream.run(url, { ...json, citations: false }, headers);
538
-
539
- if (result.stopped === "superseded") {
540
- this.logger.native("[Ask Knowledge Base] turn superseded, answer interrupted");
541
- winston.verbose("DirAskGPTV2 stream " + stream.streamId + " superseded after " + result.chunks + " chunks");
542
- await stream.sendInterrupted();
543
- callback(true);
544
- return;
545
- }
546
- if (result.stopped === "closed") {
547
- winston.verbose("DirAskGPTV2 stream " + stream.streamId + ": request closed, flow stopped");
548
- callback(true);
549
- return;
550
- }
551
-
552
- const resbody = (result.completed && result.completed.model_used) || null;
553
- if (result.error || (!resbody && !result.text)) {
554
- const status = result.error && result.error.response && result.error.response.status;
555
- winston.error("DirAskGPTV2 stream error: " + (status ? "HTTP " + status : (result.error ? result.error.message : "empty answer")));
556
- this.logger.error(`[Ask Knowledge Base] Error getting answer`);
557
- await this.#assignAttributes(action, noAnswer);
558
- if (falseIntent) {
559
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
560
- callback(true);
561
- return;
562
- }
563
- callback();
564
- return;
565
- }
566
-
567
- const success = result.marker ? false : (resbody && typeof resbody.success === "boolean" ? resbody.success : !!result.text);
568
- if (success) {
569
- const answer = (resbody && typeof resbody.answer === "string" && resbody.answer) || result.text;
570
- this.logger.native("[Ask Knowledge Base] answer: ", answer);
571
- await this.#assignAttributes(action, answer, resbody && resbody.source, resbody && resbody.content_chunks);
572
- let tokens = (resbody && resbody.prompt_token_size) || 0;
573
- if (publicKey === true) {
574
- const multiplier = MODELS_MULTIPLIER[json.model.name] ?? 1;
575
- quotasService.updateQuote(this.projectId, this.token, { tokens: tokens, model: json.model }).catch((err) => {
576
- winston.error("Error updating quota: ", err);
577
- });
578
- tokens = tokens * multiplier;
579
- }
580
- llmService.addAnsweredQuestion(this.projectId, { namespace: json.namespace, question: json.question, answer: answer,
581
- request_id: this.requestId, tokens: tokens }, this.token).catch((err) => {
582
- winston.error("Error adding answered question: ", err);
583
- });
584
- await stream.savePending(answer);
585
- if (trueIntent) {
586
- await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
587
- callback(true);
588
- return;
589
- }
590
- callback();
591
- return;
592
- }
593
-
594
- // knowledge-base miss: what was spoken (if anything) is the answer, otherwise the configured fallback
595
- this.logger.native("[Ask Knowledge Base] no answer in the knowledge base");
596
- if (result.streamed && result.text) {
597
- await this.#assignAttributes(action, result.text);
598
- await stream.savePending(result.text);
599
- } else {
600
- await this.#assignAttributes(action, noAnswer);
601
- }
602
- if (!skip_unanswered) {
603
- llmService.addUnansweredQuestion(this.projectId, { namespace: json.namespace, question: json.question,
604
- request_id: this.requestId }, this.token).catch((err) => {
605
- winston.error("DirAskGPTV2 - Error adding unanswered question: ", err && err.message);
606
- });
607
- }
608
- if (falseIntent) {
609
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
610
- callback(true);
611
- return;
612
- }
613
- callback();
614
- }
615
-
616
513
  async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
617
514
  let trueIntentDirective = null;
618
515
  if (trueIntent) {