@tiledesk/tiledesk-server 3.0.1 → 3.1.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 (136) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/CHANGELOG.md +34 -0
  3. package/Dockerfile +3 -2
  4. package/Dockerfile-en +3 -2
  5. package/Dockerfile-jobs +3 -2
  6. package/Dockerfile-profiler +3 -2
  7. package/README.md +15 -0
  8. package/app.js +12 -1
  9. package/bin/quota-reset +68 -0
  10. package/bin/quota-restore +86 -0
  11. package/channels/chat21/chat21Handler.js +50 -34
  12. package/channels/chat21/chat21JoinTracker.js +43 -0
  13. package/config/database.js +5 -5
  14. package/config/native_mcp/native_mcp_servers.json +1 -0
  15. package/controllers/chatbot.controller.js +80 -27
  16. package/controllers/intents.controller.js +42 -0
  17. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-chat21-ionic.md +468 -0
  18. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-design-studio.md +1349 -0
  19. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-server.md +1694 -0
  20. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-tybot.md +789 -0
  21. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-server.md +83 -0
  22. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-tybot.md +76 -0
  23. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-chat21-ionic.md +97 -0
  24. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-design-studio.md +72 -0
  25. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-server.md +223 -0
  26. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-tybot.md +121 -0
  27. package/docs/superpowers/plans/2026-09-29-hitl-pre-branch-ports.md +65 -0
  28. package/docs/superpowers/plans/2026-09-30-flow-start-points-design-studio.md +88 -0
  29. package/docs/superpowers/plans/2026-09-30-flow-start-points-server.md +103 -0
  30. package/docs/superpowers/plans/2026-09-30-flow-start-points-tybot.md +43 -0
  31. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-design-studio.md +80 -0
  32. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-runtime.md +39 -0
  33. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-server.md +106 -0
  34. package/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md +715 -0
  35. package/docs/superpowers/specs/2026-09-30-flow-start-points-design.md +156 -0
  36. package/docs/superpowers/specs/2026-10-01-scheduled-start-point-design.md +303 -0
  37. package/docs/superpowers/todo/2026-09-14-webhook-chatbot-shared-code-fixes.md +191 -0
  38. package/event/botEvent.js +12 -1
  39. package/event/emailEvent.js +3 -0
  40. package/event/messageEvent.js +44 -0
  41. package/jobs.js +10 -0
  42. package/jobsManager.js +19 -0
  43. package/middleware/errorHandler.js +6 -2
  44. package/models/analyticResult.js +1 -1
  45. package/models/channelConstants.js +3 -2
  46. package/models/quotaUsage.js +71 -0
  47. package/models/request.js +6 -0
  48. package/models/requestSnapshot.js +12 -0
  49. package/models/routerLogger.js +1 -1
  50. package/models/webhook.js +44 -0
  51. package/package.json +7 -5
  52. package/pubmodules/emailNotification/requestNotification.js +14 -22
  53. package/pubmodules/events/test/eventRoute.js +7 -6
  54. package/pubmodules/rules/conciergeBot.js +9 -4
  55. package/routes/auth.js +1 -12
  56. package/routes/campaigns.js +0 -13
  57. package/routes/dataTable.js +0 -3
  58. package/routes/faq_kb.js +12 -1
  59. package/routes/messagesRoot.js +0 -1
  60. package/routes/quotes.js +38 -1
  61. package/routes/request.js +81 -5
  62. package/routes/users.js +0 -1
  63. package/routes/webhook.js +160 -8
  64. package/routes/webhooks.js +276 -6
  65. package/routes/widgetLoader.js +6 -3
  66. package/services/QuoteManager.js +77 -16
  67. package/services/aiService.js +1 -1
  68. package/services/chatbotService.js +55 -1
  69. package/services/departmentService.js +60 -43
  70. package/services/dolphinSchedulerClient.js +116 -0
  71. package/services/emailService.js +6 -1
  72. package/services/hitlParticipantListener.js +260 -0
  73. package/services/participantInviteService.js +302 -0
  74. package/services/participantRemoveService.js +79 -0
  75. package/services/quotaSnapshotService.js +1167 -0
  76. package/services/requestService.js +102 -144
  77. package/services/scheduledStart/dolphinBackend.js +246 -0
  78. package/services/scheduledStart/schedulerBackend.js +101 -0
  79. package/services/scheduledStart/schedulerServiceClient.js +41 -0
  80. package/services/scheduledStartPublishListener.js +186 -0
  81. package/services/scheduledStartRules.js +173 -0
  82. package/services/scheduledStartService.js +130 -0
  83. package/services/startPointCleanupListener.js +30 -0
  84. package/services/startPointService.js +158 -0
  85. package/services/testWsService.js +1 -4
  86. package/services/updateRequestSnapshotQueued.js +8 -9
  87. package/services/webhookConversationService.js +185 -0
  88. package/services/webhookSettingsValidator.js +61 -0
  89. package/template/email/checkpointReachedEmail.html +5 -0
  90. package/test/agentPromptsRoute.test.js +3 -2
  91. package/test/agentTokensQuota.js +237 -0
  92. package/test/botEventHitlFallback.js +19 -0
  93. package/test/chat21HandlerJoinWiring.js +59 -0
  94. package/test/dataTablesRoute.test.js +10 -2
  95. package/test/dateUtils.test.js +0 -3
  96. package/test/departmentService.js +8 -12
  97. package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
  98. package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
  99. package/test/faqRoute.js +6 -1
  100. package/test/faqkbRoute.js +14 -10
  101. package/test/faqkbV3Route.js +1 -1
  102. package/test/filepRoute.js +4 -50
  103. package/test/hitlParticipantListener.js +446 -0
  104. package/test/hitlQueueWiring.js +191 -0
  105. package/test/hitlWaitingReset.js +124 -0
  106. package/test/inviteParticipantsRoute.js +492 -0
  107. package/test/kbRoute.js +1 -1
  108. package/test/messageRoute.js +33 -21
  109. package/test/nativemcp.test.js +7 -1
  110. package/test/openaiRoute.js +2 -8
  111. package/test/publishFailLoud.test.js +414 -0
  112. package/test/quotaSnapshots.js +1044 -0
  113. package/test/removeParticipantsRoute.js +186 -0
  114. package/test/requestService.js +24 -16
  115. package/test/requestServiceRemoveParticipant.js +144 -0
  116. package/test/requestVisibility.test.js +186 -0
  117. package/test/scheduledStartFacade.js +184 -0
  118. package/test/scheduledStartHandover.js +95 -0
  119. package/test/scheduledStartPublish.js +320 -0
  120. package/test/scheduledStartRoute.js +163 -0
  121. package/test/scheduledStartRules.js +177 -0
  122. package/test/scheduledStartRun.js +299 -0
  123. package/test/scheduledStartSchedulerBackend.js +116 -0
  124. package/test/scheduledStartService.js +290 -0
  125. package/test/startPointsCleanup.js +69 -0
  126. package/test/startPointsRegression.js +169 -0
  127. package/test/startPointsRoute.js +158 -0
  128. package/test/webhookConversationRoute.js +347 -0
  129. package/test/webhookConversationService.js +261 -0
  130. package/test/webhookConversationTestUtils.js +55 -0
  131. package/utils/aiUtils.js +95 -1
  132. package/utils/intentFieldNames.js +192 -0
  133. package/utils/quotaCliUtil.js +43 -0
  134. package/utils/requestVisibility.js +355 -0
  135. package/websocket/webSocketServer.js +36 -31
  136. /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
@@ -14,6 +14,7 @@ var winston = require('../../config/winston');
14
14
  var RoleConstants = require("../../models/roleConstants");
15
15
  var ChannelConstants = require("../../models/channelConstants");
16
16
  var cacheUtil = require('../../utils/cacheUtil');
17
+ var requestVisibility = require('../../utils/requestVisibility');
17
18
  const { TdCache } = require('../../utils/TdCache');
18
19
 
19
20
  const messageEvent = require('../../event/messageEvent');
@@ -572,12 +573,13 @@ sendToAgentEmailChannelEmail(projectid, message) {
572
573
 
573
574
  // winston.info("savedRequest.snapshot.agents", savedRequest.snapshot.agents);
574
575
  // agents è selected false quindi nn va sicuro
575
- if (!snapshotAgents.snapshot.agents) {
576
- return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
576
+ var allAgents = await requestVisibility.loadNotifiableProjectUsers(
577
+ snapshotAgents && snapshotAgents.snapshot,
578
+ savedRequest.id_project
579
+ );
580
+ if (!allAgents || allAgents.length === 0) {
581
+ return winston.warn("RequestNotification snapshot audience is empty. You are closing an old request?", savedRequest);
577
582
  }
578
-
579
- // var allAgents = savedRequest.agents;
580
- var allAgents = snapshotAgents.snapshot.agents;
581
583
  // winston.debug("allAgents", allAgents);
582
584
 
583
585
  allAgents.forEach(project_user => {
@@ -937,30 +939,20 @@ sendAgentEmail(projectid, savedRequest) {
937
939
 
938
940
  var snapshotAgents = savedRequest; //riassegno varibile cosi nn cambio righe successive
939
941
 
940
-
941
-
942
-
943
- // winston.info("savedRequest.snapshot.agents", savedRequest.snapshot.agents);
944
- // agents è selected false quindi nn va sicuro
945
- if (!snapshotAgents.snapshot.agents) {
946
- //return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
947
-
942
+ if (!requestVisibility.hasAudience(snapshotAgents.snapshot)) {
948
943
  // agents già c'è in quanto viene creato con departmentService.getOperator nella request.create ma nn c'è per request.participants.update
949
944
  snapshotAgents = await Request.findById(savedRequest.id).select({"snapshot":1}).exec();
950
945
  winston.debug('load snapshotAgents with Request.findById ');
951
946
  }
952
947
  winston.debug('snapshotAgents', snapshotAgents);
953
948
 
954
- if (!snapshotAgents.snapshot.agents) {
955
- return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
949
+ var allAgents = await requestVisibility.loadNotifiableProjectUsers(
950
+ snapshotAgents && snapshotAgents.snapshot,
951
+ savedRequest.id_project
952
+ );
953
+ if (!allAgents || allAgents.length === 0) {
954
+ return winston.warn("RequestNotification snapshot audience is empty. You are closing an old request?", savedRequest);
956
955
  }
957
-
958
- // var allAgents = savedRequest.agents;
959
- var allAgents = snapshotAgents.snapshot.agents;
960
-
961
- // // var allAgents = savedRequest.agents;
962
- // var allAgents = savedRequest.snapshot.agents;
963
- // // winston.debug("allAgents", allAgents);
964
956
 
965
957
  allAgents.forEach(project_user => {
966
958
  // winston.debug("project_user", project_user); //DON'T UNCOMMENT THIS. OTHERWISE this.agents.filter of models/request.js:availableAgentsCount has .filter not found.
@@ -2,7 +2,7 @@
2
2
  process.env.NODE_ENV = 'test';
3
3
 
4
4
  var User = require('../../../models/user');
5
- var Project_user = require('../../../models/project_user');
5
+ var Event = require('../event');
6
6
 
7
7
  var projectService = require('../../../services/projectService');
8
8
  var userService = require('../../../services/userService');
@@ -51,11 +51,12 @@ describe('EventRoute', () => {
51
51
  expect(res.body.name).to.equal("event1");
52
52
  expect(res.body.attributes.attr1).to.equal("val1");
53
53
 
54
- Project_user.findOne({ id_user: savedUser.id, status: "active"} )
55
- .populate('events')
56
- .exec(function (err, project_user) {
57
- if (log) { console.log("project_user", project_user.toJSON()); }
58
- expect(project_user.events.length).to.equal(1);
54
+ Event.find({ name: "event1", id_project: savedProject._id.toString() })
55
+ .exec(function (err, events) {
56
+ if (err) { console.error("err: ", err); }
57
+ if (log) { console.log("events", events); }
58
+ expect(events).to.have.lengthOf(1);
59
+ expect(events[0].attributes.attr1).to.equal("val1");
59
60
  done();
60
61
  });
61
62
 
@@ -11,6 +11,7 @@ var i8nUtil = require("../../utils/i8nUtil");
11
11
  var BotFromParticipant = require("../../utils/botFromParticipant");
12
12
  var assignmentContextUtil = require('../../utils/assignmentContextUtil');
13
13
  const RequestConstants = require('../../models/requestConstants');
14
+ var ChannelConstants = require("../../models/channelConstants");
14
15
 
15
16
  class ConciergeBot {
16
17
 
@@ -142,18 +143,22 @@ devi mandare un messaggio welcome tu altrimenti il bot inserito successivamente
142
143
  winston.debug("ConciergeBot member: " + member);
143
144
  // botprefix
144
145
  if (member.indexOf("bot_")==-1) {
146
+ if (request.channel && request.channel.name === ChannelConstants.WEBHOOK) {
147
+ winston.debug("ConciergeBot: keeping the bot in the request with request_id: " + request.request_id + " because the conversation channel is webhook");
148
+ } else {
145
149
  var botId = BotFromParticipant.getBotFromParticipants(request.participants);
146
150
  if (botId) {
147
151
  winston.verbose("ConciergeBot: removing bot with id: " + botId + " from the request with request_id: " + request.request_id + " from the project with id: " + request.id_project);
148
-
152
+
149
153
  // botprefix
150
- // removeParticipantByRequestId(request_id, id_project, member)
154
+ // removeParticipantByRequestId(request_id, id_project, member)
151
155
  requestService.removeParticipantByRequestId(request.request_id, request.id_project,"bot_"+botId ).catch((err) => {
152
156
  winston.error("(ConciergeBot) removeParticipantByRequestId error", err)
153
157
  });
154
- }
158
+ }
159
+ }
155
160
  }
156
-
161
+
157
162
 
158
163
  });
159
164
  });
package/routes/auth.js CHANGED
@@ -27,7 +27,6 @@ var UserUtil = require('../utils/userUtil');
27
27
 
28
28
  let configSecret = process.env.GLOBAL_SECRET || config.secret;
29
29
  var pKey = process.env.GLOBAL_SECRET_OR_PRIVATE_KEY;
30
- // console.log("pKey",pKey);
31
30
 
32
31
  if (pKey) {
33
32
  configSecret = pKey.replace(/\\n/g, '\n');
@@ -713,10 +712,6 @@ router.get("/google", function(req,res,next){
713
712
  )(req,res,next);
714
713
  });
715
714
 
716
- // router.get("/google/callbacks", passport.authenticate("google", { session: false }), (req, res) => {
717
- // console.log("callback_signup");
718
- // res.redirect("/google/callback");
719
- // });
720
715
 
721
716
  // Retrieve user data using the access token received</em>
722
717
  router.get("/google/callback", passport.authenticate("google", { session: false }), (req, res) => {
@@ -865,12 +860,6 @@ router.get(
865
860
  );
866
861
 
867
862
 
868
- // profile route after successful sign in</em>
869
- // router.get("/profile", (req, res) => {
870
- // console.log(req);
871
- // res.send("Welcome");
872
- // });
873
-
874
863
  // VERIFY EMAIL
875
864
  router.put('/verifyemail/:userid/:code', async function (req, res) {
876
865
 
@@ -887,7 +876,7 @@ router.put('/verifyemail/:userid/:code', async function (req, res) {
887
876
  let redis_client = req.app.get('redis_client');
888
877
  let key = "emailverify:verify-" + verify_email_code;
889
878
  let value = await redis_client.get(key);
890
- console.log("(Auth) verify value: ", value);
879
+
891
880
  if (!value) {
892
881
  return res.status(401).send({ success: false, error: "Unable to verify email: the verification code is expired or invalid.", error_code: errorCodes.AUTH.ERRORS.VERIFICATION_CODE_EXPIRED})
893
882
  }
@@ -240,19 +240,6 @@ router.post('/directDEPRECATED?', async function (req, res) {
240
240
  //TODO RESTORE IT
241
241
  const cursor = Lead.find(queryLead).cursor();
242
242
 
243
-
244
-
245
- // if (doc!= null) {
246
- // doc = await cursor.next()
247
- // }
248
-
249
- // cursor.next(function(error, doc) {
250
- // console.log(doc);
251
-
252
- // setTimout()
253
- // });
254
-
255
-
256
243
  function sleep(ms) {
257
244
  return new Promise((resolve) => {
258
245
  setTimeout(resolve, ms);
@@ -148,8 +148,6 @@ function getRowsListParams(req) {
148
148
  }
149
149
 
150
150
  router.get('/:id/rows/list', async function (req, res) {
151
-
152
- console.log("List rows")
153
151
  try {
154
152
  const rows = await dataTableService.listRowsWithFilter(
155
153
  req.projectid,
@@ -164,7 +162,6 @@ router.get('/:id/rows/list', async function (req, res) {
164
162
  });
165
163
 
166
164
  router.post('/:id/row/insert', async function (req, res) {
167
- console.log("Insert row")
168
165
  try {
169
166
  const row = await dataTableService.insertRowByBody(req.projectid, req.params.id, req.body);
170
167
  if (!row) return res.status(404).send({ success: false, error: 'Table not found' });
package/routes/faq_kb.js CHANGED
@@ -285,6 +285,13 @@ router.put('/:faq_kbid/publish', roleChecker.hasRole('admin'), async (req, res)
285
285
  return res.status(200).send({ message: "Chatbot published successfully", bot_id: published.bot_id });
286
286
  } catch (e) {
287
287
  winston.error("Error Unable publish chatbot: ", e);
288
+ if (e instanceof AppError) {
289
+ const body = { success: false, message: e.message };
290
+ if (Array.isArray(e.errors)) {
291
+ body.errors = e.errors;
292
+ }
293
+ return res.status(e.status).send(body);
294
+ }
288
295
  return res.status(500).send({ success: false, message: "Unable publish chatbot" });
289
296
  }
290
297
  });
@@ -331,7 +338,11 @@ router.put('/:faq_kbid/publish/multi', roleChecker.hasRole('admin'), asyncHandle
331
338
  } catch (err) {
332
339
  const error = err?.response?.data?.message || err?.response?.data?.error || err?.message || "Publish failed";
333
340
  winston.error("Error publishing chatbot " + id_faq_kb + ": ", err);
334
- results.push({ id: id_faq_kb, success: false, error });
341
+ const result = { id: id_faq_kb, success: false, error };
342
+ if (Array.isArray(err?.errors)) {
343
+ result.errors = err.errors;
344
+ }
345
+ results.push(result);
335
346
  }
336
347
  }
337
348
 
@@ -126,7 +126,6 @@ router.get('/csv', roleChecker.hasRoleOrTypes('owner'), function(req, res) {
126
126
 
127
127
  res.flushHeaders();
128
128
 
129
- console.log("fastCsv",fastCsv)
130
129
  var csvStream = fastCsv.format({headers: true})//.transform(transformer)
131
130
  // var csvStream = fastCsv.createWriteStream({headers: true}).transform(transformer)
132
131
  cursor.stream().pipe(csvStream).pipe(res);
package/routes/quotes.js CHANGED
@@ -2,7 +2,7 @@ var express = require('express');
2
2
  var router = express.Router();
3
3
  const { QuoteManager } = require('../services/QuoteManager');
4
4
  let winston = require('../config/winston');
5
- const { MODELS_MULTIPLIER } = require('../utils/aiUtils');
5
+ const { MODELS_MULTIPLIER, computeAgentTokens } = require('../utils/aiUtils');
6
6
 
7
7
 
8
8
  router.post('/', async (req, res) => {
@@ -44,6 +44,14 @@ router.post('/incr/:type', async (req, res) => {
44
44
 
45
45
  let quoteManager = req.app.get('quote_manager');
46
46
 
47
+ if (type === 'agent_tokens') {
48
+ return incrAgentTokens(req, res, quoteManager);
49
+ }
50
+
51
+ if (type !== 'tokens') {
52
+ return res.status(400).send({ success: false, error: "Unsupported quote type for incr: " + type });
53
+ }
54
+
47
55
  let modelKey;
48
56
  if (typeof data.model === 'string') {
49
57
  modelKey = data.model;
@@ -65,4 +73,33 @@ router.post('/incr/:type', async (req, res) => {
65
73
  res.status(200).send({ message: "value incremented for key " + incremented_key, key: incremented_key, currentQuote: quote });
66
74
  })
67
75
 
76
+ async function incrAgentTokens(req, res, quoteManager) {
77
+
78
+ let data = req.body;
79
+ let result = computeAgentTokens(data);
80
+ if (result.error) {
81
+ winston.warn("(quotes) invalid agent_tokens incr body: " + result.error);
82
+ return res.status(400).send({ success: false, error: result.error });
83
+ }
84
+
85
+ if (!Number.isFinite(result.counted)) {
86
+ return res.status(400).send({ success: false, error: "Invalid usage: counted agent tokens are not finite" });
87
+ }
88
+
89
+ let obj = {
90
+ createdAt: new Date(),
91
+ counted: result.counted,
92
+ tokens: data.tokens,
93
+ model: data.model,
94
+ source: data.source
95
+ };
96
+
97
+ winston.verbose("(quotes) incr agent_tokens mode " + result.mode + " counted " + result.counted + " model " + data.model + " source " + data.source);
98
+
99
+ let incremented_key = await quoteManager.incrementAgentTokenCount(req.project, obj);
100
+ let quote = await quoteManager.getCurrentQuote(req.project, obj, 'agent_tokens');
101
+
102
+ res.status(200).send({ message: "value incremented for key " + incremented_key, key: incremented_key, currentQuote: quote, counted: result.counted });
103
+ }
104
+
68
105
  module.exports = router;
package/routes/request.js CHANGED
@@ -21,6 +21,7 @@ var cacheUtil = require('../utils/cacheUtil');
21
21
  var RequestConstants = require("../models/requestConstants");
22
22
  var cacheEnabler = require("../services/cacheEnabler");
23
23
  var assignmentContextUtil = require('../utils/assignmentContextUtil');
24
+ var requestVisibility = require('../utils/requestVisibility');
24
25
  var Project_user = require("../models/project_user");
25
26
  var Lead = require("../models/lead");
26
27
  var UIDGenerator = require("../utils/UIDGenerator");
@@ -34,6 +35,8 @@ const RoleConstants = require('../models/roleConstants');
34
35
  const eventService = require('../pubmodules/events/eventService');
35
36
  const { Scheduler } = require('../services/Scheduler');
36
37
  const faq_kb = require('../models/faq_kb');
38
+ const participantInviteService = require('../services/participantInviteService');
39
+ const participantRemoveService = require('../services/participantRemoveService');
37
40
 
38
41
  const datesUtil = require('../utils/datesUtil');
39
42
  //const JobManager = require('../utils/jobs-worker-queue-manager-v2/JobManagerV2');
@@ -422,6 +425,57 @@ router.post('/:requestid/participants',
422
425
 
423
426
  });
424
427
 
428
+ router.post('/:requestid/participants/invite', async (req, res) => {
429
+
430
+ const body = req.body || {};
431
+ let members = body.members;
432
+ if (members !== undefined && !Array.isArray(members)) {
433
+ return res.status(422).send({ success: false, error: "members must be an array" });
434
+ }
435
+ if (Array.isArray(members) && members.length > 50) {
436
+ return res.status(422).send({ success: false, error: "Too many members (max 50)" });
437
+ }
438
+ if (body.department_id !== undefined && body.department_id !== null && typeof body.department_id !== 'string') {
439
+ return res.status(422).send({ success: false, error: "department_id must be a string" });
440
+ }
441
+ if (body.fallback_intent !== undefined && body.fallback_intent !== null && typeof body.fallback_intent !== 'string') {
442
+ return res.status(422).send({ success: false, error: "fallback_intent must be a string" });
443
+ }
444
+
445
+ try {
446
+ const result = await participantInviteService.invite(req.projectid, req.params.requestid, {
447
+ department_id: body.department_id,
448
+ members: members,
449
+ fallback_intent: body.fallback_intent
450
+ });
451
+ return res.status(200).send(result);
452
+ } catch (err) {
453
+ if (!err.status) {
454
+ winston.error("Error inviting participants to request " + req.params.requestid, err);
455
+ }
456
+ return res.status(err.status || 500).send({ success: false, error: err.status ? err.message : "Error inviting participants" });
457
+ }
458
+ });
459
+
460
+ router.post('/:requestid/participants/remove', async (req, res) => {
461
+
462
+ const body = req.body || {};
463
+ const scope = body.scope === undefined || body.scope === null ? 'invited' : body.scope;
464
+ if (scope !== 'invited' && scope !== 'all_humans') {
465
+ return res.status(422).send({ success: false, error: 'scope must be "invited" or "all_humans"' });
466
+ }
467
+
468
+ try {
469
+ const result = await participantRemoveService.remove(req.projectid, req.params.requestid, { scope: scope });
470
+ return res.status(200).send(result);
471
+ } catch (err) {
472
+ if (!err.status) {
473
+ winston.error("Error removing participants from request " + req.params.requestid, err);
474
+ }
475
+ return res.status(err.status || 500).send({ success: false, error: err.status ? err.message : "Error removing participants" });
476
+ }
477
+ });
478
+
425
479
  // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.created
426
480
  /*
427
481
  error: uncaughtException: Cannot set property 'participants' of null
@@ -1249,7 +1303,7 @@ router.get('/', function (req, res, next) {
1249
1303
  winston.debug("hasPermissionOrRole All request ");
1250
1304
  } else if (projectuser.hasPermissionOrRole('request_read_group', ["agent"])) {
1251
1305
 
1252
- query["$or"] = [{ "snapshot.agents.id_user": req.user.id }, { "participants": req.user.id }];
1306
+ query["$or"] = requestVisibility.visibilityOr(req.user.id, projectuser.createdAt);
1253
1307
 
1254
1308
  }
1255
1309
  // else if (projectuser.hasPermissionOrRole('request_read_mine', ["????"])) {
@@ -1964,10 +2018,10 @@ router.get('/csv', function (req, res, next) {
1964
2018
  // All request
1965
2019
  } else if (projectuser && (projectuser.role == "owner" || projectuser.role == "admin")) {
1966
2020
  if (req.query.mine) {
1967
- query["$or"] = [{ "snapshot.agents.id_user": req.user.id }, { "participants": req.user.id }];
2021
+ query["$or"] = requestVisibility.visibilityOr(req.user.id, projectuser && projectuser.createdAt);
1968
2022
  }
1969
2023
  } else {
1970
- query["$or"] = [{ "snapshot.agents.id_user": req.user.id }, { "participants": req.user.id }];
2024
+ query["$or"] = requestVisibility.visibilityOr(req.user.id, projectuser && projectuser.createdAt);
1971
2025
  }
1972
2026
 
1973
2027
  if (req.query.dept_id) {
@@ -2442,7 +2496,6 @@ router.get('/:requestid', function (req, res) {
2442
2496
 
2443
2497
 
2444
2498
  let q = Request.findOne({ request_id: requestid, id_project: req.projectid })
2445
- // .select("+snapshot.agents")
2446
2499
  .populate('lead')
2447
2500
  .populate('department')
2448
2501
  .populate('participatingBots')
@@ -2455,7 +2508,7 @@ router.get('/:requestid', function (req, res) {
2455
2508
  // }
2456
2509
  //
2457
2510
  // .populate({path:'requester',populate:{path:'id_user', select:{'firstname':1, 'lastname':1}}})
2458
- q.exec(function (err, request) {
2511
+ q.exec(async function (err, request) {
2459
2512
  if (err) {
2460
2513
  winston.error("error getting request by id ", err);
2461
2514
  return res.status(500).send({ success: false, msg: 'Error getting object.' });
@@ -2463,6 +2516,29 @@ router.get('/:requestid', function (req, res) {
2463
2516
  if (!request) {
2464
2517
  return res.status(404).send({ success: false, msg: 'Object not found.' });
2465
2518
  }
2519
+ if (!requestVisibility.bypassesVisibility(req.user)) {
2520
+ if (!req.projectuser) {
2521
+ return res.status(403).send({ success: false, msg: 'Unauthorized.' });
2522
+ }
2523
+ const scope = requestVisibility.readScope(req.projectuser);
2524
+ let visible = requestVisibility.canSeeRequest(request, req.user.id, req.projectuser.createdAt, scope);
2525
+ // Legacy audience lives in snapshot.agents, which is excluded from the response.
2526
+ if (!visible && scope === 'group') {
2527
+ try {
2528
+ const legacyCount = await Request.countDocuments({
2529
+ _id: request._id,
2530
+ 'snapshot.agents.id_user': req.user.id
2531
+ });
2532
+ visible = legacyCount > 0;
2533
+ } catch (countErr) {
2534
+ winston.error("error checking request visibility ", countErr);
2535
+ return res.status(500).send({ success: false, msg: 'Error getting object.' });
2536
+ }
2537
+ }
2538
+ if (!visible) {
2539
+ return res.status(403).send({ success: false, msg: 'Unauthorized.' });
2540
+ }
2541
+ }
2466
2542
  res.json(request);
2467
2543
  });
2468
2544
  });
package/routes/users.js CHANGED
@@ -171,7 +171,6 @@ router.put('/changepsw', function (req, res) {
171
171
 
172
172
  router.get('/resendverifyemail', function (req, res) {
173
173
  winston.debug('RE-SEND VERIFY EMAIL - LOGGED USER ', req.user);
174
- console.log("resendverifyemail req.user", req.user)
175
174
  let user = req.user;
176
175
  try {
177
176
  // TODO req.user.email is null for bot visitor
package/routes/webhook.js CHANGED
@@ -1,3 +1,4 @@
1
+ const crypto = require('crypto');
1
2
  var express = require('express');
2
3
  var router = express.Router();
3
4
  var { KB, Namespace } = require('../models/kb_setting');
@@ -11,10 +12,68 @@ const errorCodes = require('../errorCodes');
11
12
  const aiManager = require('../services/aiManager');
12
13
  var ObjectId = require('mongoose').Types.ObjectId;
13
14
  const default_embedding = require('../config/kb/embedding');
15
+ const webhookSettingsValidator = require('../services/webhookSettingsValidator');
16
+ const webhookConversationService = require('../services/webhookConversationService');
17
+ const scheduledStartRules = require('../services/scheduledStartRules');
18
+ const scheduledStartService = require('../services/scheduledStartService');
14
19
  const KB_WEBHOOK_TOKEN = process.env.KB_WEBHOOK_TOKEN || 'kbcustomtoken';
15
20
  const AMQP_MANAGER_URL = process.env.AMQP_MANAGER_URL;
16
21
  const JOB_TOPIC_EXCHANGE = process.env.JOB_TOPIC_EXCHANGE_TRAIN || 'tiledesk-trainer';
17
22
 
23
+ // Shared by the prod route and the dev conversation branch. Returns true when the caller may
24
+ // proceed; otherwise it has already sent the (422 turned off / 422 start not enabled / 429 rate
25
+ // limited) response. A conversation webhook starts at its webhook start box, so it needs an
26
+ // enabled `webhook` start point (checked before the rate limit, which it must not consume).
27
+ async function checkEnabledAndRateLimit(req, res, webhook) {
28
+ if (!webhook.enabled) {
29
+ winston.verbose("Webhook " + webhook.webhook_id + " is currently turned off")
30
+ res.status(422).send({ success: false, error: "Webhook " + webhook.webhook_id + " is currently turned off"})
31
+ return false;
32
+ }
33
+
34
+ if (webhook.mode === webhookSettingsValidator.CONVERSATION_MODE && !webhookConversationService.enabledStartPoint(webhook)) {
35
+ winston.verbose("Webhook " + webhook.webhook_id + ": webhook start is not enabled")
36
+ res.status(422).send({ success: false, error: webhookConversationService.START_NOT_ENABLED });
37
+ return false;
38
+ }
39
+
40
+ const rate_manager = req.app.get("rate_manager");
41
+ const allowed = await rate_manager.canExecute(webhook.id_project, null, 'webhook');
42
+ if (!allowed) {
43
+ winston.warn("Webhook rate limit exceeded for project " + webhook.id_project)
44
+ res.status(429).send({ message: "Rate limit exceeded"});
45
+ return false;
46
+ }
47
+
48
+ return true;
49
+ }
50
+
51
+ async function startConversation(res, webhook, payload, options) {
52
+ let chatbot;
53
+ try {
54
+ chatbot = await webhookSettingsValidator.findChatbot(webhook.id_project, webhook.chatbot_id);
55
+ } catch (err) {
56
+ winston.error("Error finding chatbot for webhook conversation ", err);
57
+ return res.status(500).send({ success: false, error: "Error finding chatbot " + webhook.chatbot_id });
58
+ }
59
+ if (!chatbot) {
60
+ return res.status(404).send({ success: false, error: "Chatbot not found with id " + webhook.chatbot_id });
61
+ }
62
+
63
+ try {
64
+ const result = await webhookConversationService.start(webhook, chatbot, payload, options);
65
+ if (options.dev !== true) {
66
+ webhookEvent.emit("webhook.triggered", { webhook: webhook, payload: { preloaded_request_id: result.request_id } });
67
+ }
68
+ return res.status(202).send({ success: true, request_id: result.request_id });
69
+ } catch (err) {
70
+ if (!err.status) {
71
+ winston.error("Error starting webhook conversation for webhook " + webhook.webhook_id, err);
72
+ }
73
+ return res.status(err.status || 500).send({ success: false, error: err.status ? err.message : "Error starting conversation" });
74
+ }
75
+ }
76
+
18
77
  let jobManager = new JobManager(AMQP_MANAGER_URL, {
19
78
  debug: false,
20
79
  topic: JOB_TOPIC_EXCHANGE,
@@ -260,6 +319,74 @@ router.post('/kb/status', async (req, res) => {
260
319
 
261
320
  })
262
321
 
322
+ // Run endpoint called by the DolphinScheduler workflow of a scheduled start point. It ignores
323
+ // webhook.enabled and the webhook start point (they govern the public webhook URL only) and
324
+ // applies just the per-project rate limit.
325
+ router.post('/scheduled-start', async (req, res) => {
326
+ if (!scheduledStartRules.isConfigured()) {
327
+ return res.status(503).send({ success: false, error: "Scheduled starts are not available on this installation" });
328
+ }
329
+ const token = req.headers['x-auth-token'];
330
+ const expected = Buffer.from(String(process.env.SCHEDULED_START_TOKEN));
331
+ const given = Buffer.from(String(token || ''));
332
+ if (!token || given.length !== expected.length || !crypto.timingSafeEqual(given, expected)) {
333
+ return res.status(401).send({ success: false, error: "Unauthorized" });
334
+ }
335
+
336
+ const id_project = req.body && req.body.id_project;
337
+ const chatbot_id = req.body && req.body.chatbot_id;
338
+ if (!id_project || !chatbot_id) {
339
+ return res.status(400).send({ success: false, error: "id_project and chatbot_id are required" });
340
+ }
341
+
342
+ let webhook;
343
+ let chatbot;
344
+ try {
345
+ webhook = await Webhook.findOne({ id_project: String(id_project), chatbot_id: String(chatbot_id) });
346
+ chatbot = await webhookSettingsValidator.findChatbot(String(id_project), String(chatbot_id));
347
+ } catch (err) {
348
+ winston.error("Error loading scheduled start ", err);
349
+ return res.status(500).send({ success: false, error: "Error loading scheduled start" });
350
+ }
351
+
352
+ const live = webhook && webhook.scheduled_live;
353
+ if (!chatbot || !live || live.enabled !== true || !live.block_id) {
354
+ winston.warn("Scheduled start for chatbot " + chatbot_id + " is gone or disabled: taking the workflow offline");
355
+ // Uses the live ids when stored, else finds the workflow by name; never throws.
356
+ await scheduledStartService.offlineFor(String(chatbot_id), live);
357
+ return res.status(410).send({ success: false, error: "Scheduled start is no longer active" });
358
+ }
359
+
360
+ let allowed;
361
+ try {
362
+ allowed = await req.app.get("rate_manager").canExecute(webhook.id_project, null, 'webhook');
363
+ } catch (err) {
364
+ winston.error("Error checking the rate limit of scheduled start ", err);
365
+ return res.status(500).send({ success: false, error: "Error checking the rate limit" });
366
+ }
367
+ if (!allowed) {
368
+ winston.warn("Webhook rate limit exceeded for project " + webhook.id_project);
369
+ return res.status(429).send({ success: false, message: "Rate limit exceeded" });
370
+ }
371
+
372
+ try {
373
+ const mapping = live.mapping || {};
374
+ const result = await webhookConversationService.start(webhook, chatbot, mapping.payload || {}, {
375
+ startPoint: { block_id: live.block_id, mapping: mapping },
376
+ startType: 'scheduled',
377
+ leadId: 'scheduled_' + chatbot_id,
378
+ leadName: mapping.source_name || chatbot.name || 'Scheduled',
379
+ extraAttributes: { scheduled_at: new Date().toISOString() }
380
+ });
381
+ return res.status(202).send({ success: true, request_id: result.request_id });
382
+ } catch (err) {
383
+ if (!err.status) {
384
+ winston.error("Error starting scheduled conversation for chatbot " + chatbot_id, err);
385
+ }
386
+ return res.status(err.status || 500).send({ success: false, error: err.status ? err.message : "Error starting conversation" });
387
+ }
388
+ });
389
+
263
390
  router.all('/:webhook_id', async (req, res) => {
264
391
 
265
392
  let webhook_id = req.params.webhook_id;
@@ -282,16 +409,12 @@ router.all('/:webhook_id', async (req, res) => {
282
409
  return res.status(404).send({ success: false, error: "Webhook not found with id " + webhook_id });
283
410
  }
284
411
 
285
- if (!webhook.enabled) {
286
- winston.verbose("Webhook " + webhook_id + " is currently turned off")
287
- return res.status(422).send({ success: false, error: "Webhook " + webhook_id + " is currently turned off"})
412
+ if (!(await checkEnabledAndRateLimit(req, res, webhook))) {
413
+ return;
288
414
  }
289
415
 
290
- const rate_manager = req.app.get("rate_manager");
291
- const allowed = await rate_manager.canExecute(webhook.id_project, null, 'webhook');
292
- if (!allowed) {
293
- winston.warn("Webhook rate limit exceeded for project " + webhook.id_project)
294
- return res.status(429).send({ message: "Rate limit exceeded"});
416
+ if (webhook.mode === webhookSettingsValidator.CONVERSATION_MODE) {
417
+ return startConversation(res, webhook, payload, { dev: false });
295
418
  }
296
419
 
297
420
  payload.request_id = "automation-request-" + webhook.id_project + "-" + new ObjectId() + "-" + webhook_id;
@@ -342,6 +465,35 @@ router.all('/:webhook_id/dev', async (req, res) => {
342
465
  return res.status(404).send({ success: false, error: "Webhook not found with id " + webhook_id });
343
466
  }
344
467
 
468
+ if (webhook.mode === webhookSettingsValidator.CONVERSATION_MODE) {
469
+ if (!(await checkEnabledAndRateLimit(req, res, webhook))) {
470
+ return;
471
+ }
472
+
473
+ const preloadKey = "logs:webhook:" + webhook.id_project + ":" + webhook.webhook_id;
474
+ let preloaded;
475
+ try {
476
+ preloaded = await req.app.get('redis_client').get(preloadKey);
477
+ } catch (err) {
478
+ winston.error("Error reading preloaded development request ", err);
479
+ return res.status(500).send({ success: false, error: "Unable to read development request" });
480
+ }
481
+ if (!preloaded) {
482
+ return res.status(422).send({ success: false, message: "Development webhook is currently turned off", code: errorCodes.WEBHOOK.ERRORS.NO_PRELOADED_DEV_REQUEST });
483
+ }
484
+
485
+ // The preload is single-use: consume it now so a second dev call (without a fresh preload)
486
+ // gets the normal "no preloaded dev request" 422 instead of hitting the unique request_id
487
+ // index reusing the same request_id for the whole 15-minute Redis TTL.
488
+ try {
489
+ await req.app.get('redis_client').del(preloadKey);
490
+ } catch (err) {
491
+ winston.error("Error deleting consumed preloaded development request ", err);
492
+ }
493
+
494
+ return startConversation(res, webhook, payload, { dev: true, request_id: JSON.parse(preloaded).request_id });
495
+ }
496
+
345
497
  let redis_client = req.app.get('redis_client');
346
498
  webhookService.run(webhook, payload, true, redis_client).then((response) => {
347
499
  return res.status(200).send(response);