@tiledesk/tiledesk-server 3.0.1 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/CHANGELOG.md +47 -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/dataTable.js +16 -0
  47. package/models/location.js +6 -12
  48. package/models/quotaUsage.js +71 -0
  49. package/models/request.js +71 -61
  50. package/models/requestSnapshot.js +12 -0
  51. package/models/routerLogger.js +1 -1
  52. package/models/webhook.js +44 -0
  53. package/package.json +9 -7
  54. package/pubmodules/emailNotification/requestNotification.js +14 -22
  55. package/pubmodules/events/test/eventRoute.js +7 -6
  56. package/pubmodules/rules/conciergeBot.js +9 -4
  57. package/routes/auth.js +1 -12
  58. package/routes/campaigns.js +0 -13
  59. package/routes/dataTable.js +23 -3
  60. package/routes/faq_kb.js +12 -1
  61. package/routes/messagesRoot.js +0 -1
  62. package/routes/quotes.js +38 -1
  63. package/routes/request.js +81 -5
  64. package/routes/users.js +0 -1
  65. package/routes/webhook.js +160 -8
  66. package/routes/webhooks.js +276 -6
  67. package/routes/widgetLoader.js +6 -3
  68. package/services/QuoteManager.js +77 -16
  69. package/services/aiService.js +1 -1
  70. package/services/chatbotService.js +55 -1
  71. package/services/dataTableService.js +436 -42
  72. package/services/departmentService.js +60 -43
  73. package/services/dolphinSchedulerClient.js +116 -0
  74. package/services/emailService.js +6 -1
  75. package/services/hitlParticipantListener.js +260 -0
  76. package/services/participantInviteService.js +302 -0
  77. package/services/participantRemoveService.js +79 -0
  78. package/services/quotaSnapshotService.js +1167 -0
  79. package/services/requestService.js +102 -144
  80. package/services/scheduledStart/dolphinBackend.js +246 -0
  81. package/services/scheduledStart/schedulerBackend.js +101 -0
  82. package/services/scheduledStart/schedulerServiceClient.js +41 -0
  83. package/services/scheduledStartPublishListener.js +186 -0
  84. package/services/scheduledStartRules.js +173 -0
  85. package/services/scheduledStartService.js +130 -0
  86. package/services/startPointCleanupListener.js +30 -0
  87. package/services/startPointService.js +158 -0
  88. package/services/testWsService.js +1 -4
  89. package/services/updateRequestSnapshotQueued.js +8 -9
  90. package/services/webhookConversationService.js +185 -0
  91. package/services/webhookSettingsValidator.js +61 -0
  92. package/template/email/checkpointReachedEmail.html +5 -0
  93. package/test/agentPromptsRoute.test.js +3 -2
  94. package/test/agentTokensQuota.js +237 -0
  95. package/test/botEventHitlFallback.js +19 -0
  96. package/test/chat21HandlerJoinWiring.js +59 -0
  97. package/test/dataTablesRoute.test.js +649 -2
  98. package/test/dateUtils.test.js +0 -3
  99. package/test/departmentService.js +8 -12
  100. package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
  101. package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
  102. package/test/faqRoute.js +6 -1
  103. package/test/faqkbRoute.js +14 -10
  104. package/test/faqkbV3Route.js +1 -1
  105. package/test/filepRoute.js +4 -50
  106. package/test/hitlParticipantListener.js +446 -0
  107. package/test/hitlQueueWiring.js +191 -0
  108. package/test/hitlWaitingReset.js +124 -0
  109. package/test/inviteParticipantsRoute.js +492 -0
  110. package/test/kbRoute.js +1 -1
  111. package/test/messageRoute.js +33 -21
  112. package/test/nativemcp.test.js +7 -1
  113. package/test/openaiRoute.js +2 -8
  114. package/test/publishFailLoud.test.js +414 -0
  115. package/test/quotaSnapshots.js +1044 -0
  116. package/test/removeParticipantsRoute.js +186 -0
  117. package/test/requestService.js +24 -16
  118. package/test/requestServiceRemoveParticipant.js +144 -0
  119. package/test/requestVisibility.test.js +186 -0
  120. package/test/scheduledStartFacade.js +184 -0
  121. package/test/scheduledStartHandover.js +95 -0
  122. package/test/scheduledStartPublish.js +320 -0
  123. package/test/scheduledStartRoute.js +163 -0
  124. package/test/scheduledStartRules.js +177 -0
  125. package/test/scheduledStartRun.js +299 -0
  126. package/test/scheduledStartSchedulerBackend.js +116 -0
  127. package/test/scheduledStartService.js +290 -0
  128. package/test/startPointsCleanup.js +69 -0
  129. package/test/startPointsRegression.js +169 -0
  130. package/test/startPointsRoute.js +158 -0
  131. package/test/webhookConversationRoute.js +347 -0
  132. package/test/webhookConversationService.js +261 -0
  133. package/test/webhookConversationTestUtils.js +55 -0
  134. package/utils/aiUtils.js +95 -1
  135. package/utils/dataTableUtils.js +99 -9
  136. package/utils/intentFieldNames.js +192 -0
  137. package/utils/quotaCliUtil.js +43 -0
  138. package/utils/requestVisibility.js +355 -0
  139. package/websocket/webSocketServer.js +36 -31
  140. /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
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);