@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
@@ -6,6 +6,7 @@ const intentsController = require("./intents.controller");
6
6
  const botEvent = require("../event/botEvent");
7
7
  const winston = require("../config/winston");
8
8
  const v3FlowService = require("../services/v3FlowService");
9
+ const intentFieldNames = require("../utils/intentFieldNames");
9
10
 
10
11
  const IMPORT_OVERRIDE_FIELDS = [
11
12
  "webhook_enabled",
@@ -511,11 +512,46 @@ class ChatbotController {
511
512
  }
512
513
  }
513
514
 
515
+ /**
516
+ * Field names MongoDB cannot insert ('.' inside, '$' in front) in the agent's attributes or in any
517
+ * block of the JSON (and of its subagents): refused with a readable 422 before any write.
518
+ */
519
+ _assertImportableBlocks(json, skipSubagents) {
520
+ const errors = [
521
+ ...intentFieldNames.collectAgentErrors(json.name, json.attributes),
522
+ ...intentFieldNames.collectIntentErrors(json.intents)
523
+ ];
524
+ if (!skipSubagents && Array.isArray(json.subagents)) {
525
+ json.subagents.forEach((subagent) => {
526
+ if (subagent) {
527
+ errors.push(...intentFieldNames.collectAgentErrors(subagent.name, subagent.attributes));
528
+ errors.push(...intentFieldNames.collectIntentErrors(subagent.intents));
529
+ }
530
+ });
531
+ }
532
+ if (errors.length > 0) {
533
+ throw intentFieldNames.invalidFlowError(errors);
534
+ }
535
+ }
536
+
537
+ /** Removes a bot created by a failed import (create mode): its blocks, its subagents and their blocks. */
538
+ async _rollbackCreated(chatbot, id_project) {
539
+ try {
540
+ await this.deleteSubagents(chatbot._id, id_project);
541
+ await intentsController.deleteByChatbotId(chatbot._id);
542
+ await Chatbot.deleteOne({ _id: chatbot._id }).exec();
543
+ winston.warn("(ChatbotController) import failed: created chatbot " + chatbot._id + " removed");
544
+ } catch (err) {
545
+ winston.error("(ChatbotController) import rollback error for " + chatbot._id + ": ", err);
546
+ }
547
+ }
548
+
514
549
  async import(json, id_project, chatbot_id, create, replace, overwrite, user_id, skipSubagents) {
515
550
  let chatbot;
516
551
  const sourceParentId = this._sourceId(json);
517
552
  this._stripImportedIdentity(json);
518
553
  this._prepareImportedV3(json);
554
+ this._assertImportableBlocks(json, skipSubagents);
519
555
 
520
556
  if (create) {
521
557
  json.template = "empty";
@@ -526,42 +562,59 @@ class ChatbotController {
526
562
  throw new AppError(400, "Error creating new chatbot");
527
563
  }
528
564
 
529
- if (json.attributes) {
530
- this._rewriteRulesParticipants(json.attributes, chatbot._id);
531
- chatbot = await this.updatedChatbot(chatbot._id, id_project, { attributes: json.attributes });
532
- }
533
- } else {
534
- if (!chatbot_id) {
535
- throw new AppError(400, "With replace or overwrite option a id_faq_kb must be provided");
565
+ // From here on a failure leaves no half-created copy behind (a fork for publish included).
566
+ try {
567
+ if (json.attributes) {
568
+ this._rewriteRulesParticipants(json.attributes, chatbot._id);
569
+ chatbot = await this.updatedChatbot(chatbot._id, id_project, { attributes: json.attributes });
570
+ }
571
+ return await this._importContent(chatbot, json, id_project, sourceParentId, replace, overwrite, user_id, skipSubagents);
572
+ } catch (err) {
573
+ await this._rollbackCreated(chatbot, id_project);
574
+ throw err;
536
575
  }
576
+ }
577
+ chatbot = await this._importInto(json, id_project, chatbot_id);
578
+ return this._importContent(chatbot, json, id_project, sourceParentId, replace, overwrite, user_id, skipSubagents);
579
+ }
537
580
 
538
- chatbot = await this.getChatbot(chatbot_id, id_project);
581
+ /** Replace/overwrite mode: the existing bot takes the JSON's fields. */
582
+ async _importInto(json, id_project, chatbot_id) {
583
+ let chatbot;
584
+ if (!chatbot_id) {
585
+ throw new AppError(400, "With replace or overwrite option a id_faq_kb must be provided");
586
+ }
539
587
 
540
- for (const field of IMPORT_OVERRIDE_FIELDS) {
541
- if (json[field]) {
542
- chatbot[field] = json[field];
543
- }
544
- }
588
+ chatbot = await this.getChatbot(chatbot_id, id_project);
545
589
 
546
- if (json.attributes) {
547
- this._rewriteRulesParticipants(json.attributes, chatbot._id);
548
- chatbot.attributes = json.attributes;
590
+ for (const field of IMPORT_OVERRIDE_FIELDS) {
591
+ if (json[field]) {
592
+ chatbot[field] = json[field];
549
593
  }
550
- this._applyV3Fields(chatbot, json);
594
+ }
551
595
 
552
- try {
553
- chatbot = await Chatbot.findByIdAndUpdate(chatbot._id, chatbot, { new: true });
554
- } catch (err) {
555
- winston.error("(ChatbotController) import update error: ", err);
556
- throw new AppError(400, "Error updating chatbot");
557
- }
558
- if (!chatbot) {
559
- throw new AppError(404, `Chatbot not found with id ${chatbot_id}`);
560
- }
596
+ if (json.attributes) {
597
+ this._rewriteRulesParticipants(json.attributes, chatbot._id);
598
+ chatbot.attributes = json.attributes;
599
+ }
600
+ this._applyV3Fields(chatbot, json);
561
601
 
562
- botEvent.emit("faqbot.update", chatbot);
602
+ try {
603
+ chatbot = await Chatbot.findByIdAndUpdate(chatbot._id, chatbot, { new: true });
604
+ } catch (err) {
605
+ winston.error("(ChatbotController) import update error: ", err);
606
+ throw new AppError(400, "Error updating chatbot");
607
+ }
608
+ if (!chatbot) {
609
+ throw new AppError(404, `Chatbot not found with id ${chatbot_id}`);
563
610
  }
564
611
 
612
+ botEvent.emit("faqbot.update", chatbot);
613
+ return chatbot;
614
+ }
615
+
616
+ /** Blocks and subagents of the JSON into `chatbot`. */
617
+ async _importContent(chatbot, json, id_project, sourceParentId, replace, overwrite, user_id, skipSubagents) {
565
618
  if (replace) {
566
619
  await intentsController.deleteByChatbotId(chatbot._id);
567
620
  }
@@ -2,6 +2,7 @@ const Intent = require("../models/faq");
2
2
  const AppError = require("../utils/AppError");
3
3
  const faqBotEvent = require("../event/faqBotEvent");
4
4
  const winston = require("../config/winston");
5
+ const intentFieldNames = require("../utils/intentFieldNames");
5
6
 
6
7
  const INTENT_FIELDS = [
7
8
  "intent_display_name",
@@ -50,6 +51,9 @@ class IntentsController {
50
51
  }
51
52
  throw new AppError(409, "Duplicate intent_display_name");
52
53
  }
54
+ if (err instanceof AppError) {
55
+ throw err;
56
+ }
53
57
  winston.error("(IntentsController) create error: ", err);
54
58
  throw new AppError(500, "Error creating intent");
55
59
  }
@@ -59,16 +63,49 @@ class IntentsController {
59
63
  if (!intents?.length) {
60
64
  return [];
61
65
  }
66
+ this._assertValidFieldNames(intents);
62
67
  const results = await Promise.allSettled(
63
68
  intents.map((intent) =>
64
69
  this.create(chatbot_id, id_project, user_id, intent, options)
65
70
  )
66
71
  );
72
+ this._assertAllSaved(intents, results);
67
73
  return results
68
74
  .filter((r) => r.status === "fulfilled" && r.value)
69
75
  .map((r) => r.value);
70
76
  }
71
77
 
78
+ /** Refuses the whole list, before any write, when a block holds a field name MongoDB cannot insert. */
79
+ _assertValidFieldNames(intents) {
80
+ const errors = intentFieldNames.collectIntentErrors(intents);
81
+ if (errors.length > 0) {
82
+ throw intentFieldNames.invalidFlowError(errors);
83
+ }
84
+ }
85
+
86
+ /**
87
+ * A block that could not be saved is an error, not a block to drop: the caller (import, fork,
88
+ * publish) must not report success with fewer blocks. Skipped duplicates resolve to null and are fine.
89
+ */
90
+ _assertAllSaved(intents, results) {
91
+ const errors = [];
92
+ results.forEach((r, i) => {
93
+ if (r.status === "rejected") {
94
+ errors.push({
95
+ intent_id: intents[i].intent_id,
96
+ intent_display_name: intents[i].intent_display_name,
97
+ reason: (r.reason && r.reason.message) || "could not be saved"
98
+ });
99
+ }
100
+ });
101
+ if (errors.length > 0) {
102
+ winston.error("(IntentsController) " + errors.length + " of " + intents.length + " blocks not saved: " + JSON.stringify(errors));
103
+ const err = new AppError(500, intentFieldNames.summarizeErrors(errors));
104
+ err.errors = errors;
105
+ throw err;
106
+ }
107
+ }
108
+
72
109
  async upsert(chatbot_id, id_project, user_id, intent) {
73
110
  try {
74
111
  const savingResult = await Intent.findOneAndUpdate(
@@ -91,6 +128,9 @@ class IntentsController {
91
128
 
92
129
  return savingResult.value;
93
130
  } catch (err) {
131
+ if (err instanceof AppError) {
132
+ throw err;
133
+ }
94
134
  winston.error("(IntentsController) upsert error: ", err);
95
135
  throw new AppError(500, "Error upserting intent");
96
136
  }
@@ -100,11 +140,13 @@ class IntentsController {
100
140
  if (!intents?.length) {
101
141
  return [];
102
142
  }
143
+ this._assertValidFieldNames(intents);
103
144
  const results = await Promise.allSettled(
104
145
  intents.map((intent) =>
105
146
  this.upsert(chatbot_id, id_project, user_id, intent)
106
147
  )
107
148
  );
149
+ this._assertAllSaved(intents, results);
108
150
  return results
109
151
  .filter((r) => r.status === "fulfilled" && r.value)
110
152
  .map((r) => r.value);