@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
@@ -0,0 +1,158 @@
1
+ const Faq = require('../models/faq');
2
+ const { Webhook } = require('../models/webhook');
3
+ const chatbotTypes = require('../models/chatbotTypes');
4
+ const scheduledStartRules = require('./scheduledStartRules');
5
+
6
+ const TYPES = ['webhook', 'scheduled'];
7
+ const CONVERSATION_MODE = 'conversation';
8
+
9
+ class StartPointError extends Error {
10
+ constructor(status, message) {
11
+ super(message);
12
+ this.status = status;
13
+ }
14
+ }
15
+
16
+ function checkType(type) {
17
+ if (!TYPES.includes(type)) {
18
+ throw new StartPointError(400, "Invalid start point type " + type);
19
+ }
20
+ }
21
+
22
+ class StartPointService {
23
+
24
+ constructor() {
25
+ this.TYPES = TYPES;
26
+ this.StartPointError = StartPointError;
27
+ }
28
+
29
+ find(webhook, type) {
30
+ return ((webhook && webhook.start_points) || []).find((sp) => sp.type === type);
31
+ }
32
+
33
+ /**
34
+ * Creates or updates the start point of the given type on the chatbot's webhook,
35
+ * creating the webhook (conversation mode) when it does not exist.
36
+ */
37
+ async upsert(id_project, chatbot, type, body) {
38
+ checkType(type);
39
+ body = body || {};
40
+
41
+ const subtype = chatbot.subtype || chatbotTypes.CHATBOT;
42
+ if (subtype !== chatbotTypes.CHATBOT) {
43
+ throw new StartPointError(400, "Start points require a chatbot with subtype " + chatbotTypes.CHATBOT);
44
+ }
45
+ if (!body.block_id || typeof body.block_id !== 'string') {
46
+ throw new StartPointError(400, "block_id is required");
47
+ }
48
+ if (body.hasOwnProperty('enabled') && typeof body.enabled !== 'boolean') {
49
+ throw new StartPointError(400, "enabled must be a boolean");
50
+ }
51
+ let mapping;
52
+ if (body.hasOwnProperty('mapping') && body.mapping !== null && body.mapping !== undefined) {
53
+ if (typeof body.mapping !== 'object' || Array.isArray(body.mapping)) {
54
+ throw new StartPointError(400, "mapping must be an object");
55
+ }
56
+ if (body.mapping.hasOwnProperty('source_name') && typeof body.mapping.source_name !== 'string') {
57
+ throw new StartPointError(400, "mapping.source_name must be a string");
58
+ }
59
+ if (type === 'scheduled' && body.mapping.hasOwnProperty('payload')) {
60
+ const payloadError = scheduledStartRules.validatePayload(body.mapping.payload);
61
+ if (payloadError) {
62
+ throw new StartPointError(400, payloadError);
63
+ }
64
+ }
65
+ mapping = body.mapping;
66
+ }
67
+ let schedule;
68
+ if (type === 'scheduled' && body.hasOwnProperty('schedule')) {
69
+ const scheduleError = scheduledStartRules.validateSchedule(body.schedule);
70
+ if (scheduleError) {
71
+ throw new StartPointError(400, scheduleError);
72
+ }
73
+ schedule = body.schedule;
74
+ }
75
+
76
+ const chatbot_id = chatbot._id.toString();
77
+ const block = await Faq.findOne({ id_faq_kb: chatbot_id, intent_id: body.block_id }).lean().exec();
78
+ if (!block) {
79
+ throw new StartPointError(404, "Block not found with id " + body.block_id + " in chatbot " + chatbot_id);
80
+ }
81
+
82
+ const webhook = await Webhook.findOne({ id_project: id_project, chatbot_id: chatbot_id }).exec();
83
+
84
+ if (!webhook) {
85
+ if (type === 'scheduled' && !schedule) {
86
+ throw new StartPointError(400, "schedule is required");
87
+ }
88
+ const startPoint = { type: type, block_id: body.block_id, enabled: body.enabled !== false, mapping: { source_name: mapping && mapping.source_name } };
89
+ if (type === 'scheduled') {
90
+ if (mapping && mapping.hasOwnProperty('payload')) {
91
+ startPoint.mapping.payload = mapping.payload;
92
+ }
93
+ startPoint.schedule = schedule;
94
+ }
95
+ try {
96
+ return await new Webhook({
97
+ id_project: id_project,
98
+ chatbot_id: chatbot_id,
99
+ block_id: body.block_id,
100
+ mode: CONVERSATION_MODE,
101
+ start_points: [startPoint]
102
+ }).save();
103
+ } catch (err) {
104
+ if (err.code === 11000) {
105
+ throw new StartPointError(409, "Webhook already exists for chatbot " + chatbot_id + ", retry");
106
+ }
107
+ throw err;
108
+ }
109
+ }
110
+
111
+ if (webhook.mode !== CONVERSATION_MODE) {
112
+ if (body.confirm !== true) {
113
+ throw new StartPointError(409, "The chatbot has an automation webhook: confirm to convert it to conversation mode");
114
+ }
115
+ webhook.mode = CONVERSATION_MODE;
116
+ }
117
+
118
+ let startPoint = this.find(webhook, type);
119
+ if (!startPoint && type === 'scheduled' && !schedule) {
120
+ throw new StartPointError(400, "schedule is required");
121
+ }
122
+ if (!startPoint) {
123
+ webhook.start_points = (webhook.start_points || []).concat([{ type: type, block_id: body.block_id, enabled: true, mapping: {} }]);
124
+ startPoint = this.find(webhook, type);
125
+ }
126
+ startPoint.block_id = body.block_id;
127
+ if (body.hasOwnProperty('enabled')) {
128
+ startPoint.enabled = body.enabled;
129
+ }
130
+ if (mapping) {
131
+ const merged = Object.assign({}, startPoint.mapping && startPoint.mapping.toObject ? startPoint.mapping.toObject() : startPoint.mapping);
132
+ if (mapping.hasOwnProperty('source_name')) {
133
+ merged.source_name = mapping.source_name;
134
+ }
135
+ if (type === 'scheduled' && mapping.hasOwnProperty('payload')) {
136
+ merged.payload = mapping.payload;
137
+ }
138
+ startPoint.mapping = merged;
139
+ }
140
+ if (schedule) {
141
+ startPoint.schedule = schedule;
142
+ }
143
+ webhook.markModified('start_points');
144
+ return webhook.save();
145
+ }
146
+
147
+ async remove(id_project, chatbot_id, type) {
148
+ checkType(type);
149
+ const webhook = await Webhook.findOne({ id_project: id_project, chatbot_id: chatbot_id }).exec();
150
+ if (!webhook || !this.find(webhook, type)) {
151
+ throw new StartPointError(404, "Start point " + type + " not found for chatbot " + chatbot_id);
152
+ }
153
+ webhook.start_points = webhook.start_points.filter((sp) => sp.type !== type);
154
+ return webhook.save();
155
+ }
156
+ }
157
+
158
+ module.exports = new StartPointService();
@@ -1,6 +1,5 @@
1
1
  var Message = require("../models/message");
2
2
  const WebSocket = require('ws');
3
- var url = require('url');
4
3
 
5
4
  class TestWsService {
6
5
 
@@ -28,9 +27,7 @@ class TestWsService {
28
27
 
29
28
  console.log('ws connection. req.url)', req.url);
30
29
  //console.log('ws connection. req', req);
31
- let urlParsed = url.parse(req.url, true);
32
- console.log('urlParsed', urlParsed);
33
- var queryParameter = urlParsed.query;
30
+ let queryParameter = Object.fromEntries(new URL(req.url, 'http://localhost').searchParams);
34
31
  console.log('queryParameter', queryParameter);
35
32
  console.log('queryParameter.q', queryParameter.q);
36
33
 
@@ -3,6 +3,7 @@
3
3
  var Request = require("../models/request");
4
4
  const requestEvent = require('../event/requestEvent');
5
5
  var winston = require('../config/winston');
6
+ var requestVisibility = require('../utils/requestVisibility');
6
7
 
7
8
  class UpdateRequestSnapshotQueued {
8
9
 
@@ -28,34 +29,32 @@ class UpdateRequestSnapshotQueued {
28
29
 
29
30
  const request = data.request;
30
31
  const snapshot = data.snapshot;
31
- const agentsArray = snapshot?.agents || [];
32
32
 
33
33
  if (!request || !request.request_id || !request.id_project) {
34
34
  winston.error("updateRequestSnapshot: Invalid request data", data);
35
35
  return;
36
36
  }
37
37
 
38
- // If there is no agents array in snapshot or it's not actually an array, skip the update
39
- if (!Array.isArray(agentsArray)) {
40
- winston.error("updateRequestSnapshot: snapshot.agents is not an array.", snapshot);
38
+ const update = requestVisibility.visibilityUpdate(snapshot);
39
+ if (!update) {
40
+ winston.debug("updateRequestSnapshot: snapshot has no visibility, skip", snapshot);
41
41
  return;
42
42
  }
43
43
 
44
44
  const query = { request_id: request.request_id, id_project: request.id_project };
45
45
  winston.debug("updateRequestSnapshot query ", query);
46
46
 
47
- // Update ONLY the snapshot.agents field, presupposing 'snapshot' document already exists
48
47
  Request.findOneAndUpdate(
49
- query,
50
- { "$set": { "snapshot.agents": agentsArray } },
48
+ query,
49
+ update,
51
50
  { new: true },
52
51
  function (err, updatedRequest) {
53
52
  if (err) {
54
- winston.error("Error updating request snapshot.agents in updateRequestSnapshot", err);
53
+ winston.error("Error updating request snapshot visibility in updateRequestSnapshot", err);
55
54
  return;
56
55
  }
57
56
  if (updatedRequest) {
58
- winston.debug("updateRequestSnapshot updated snapshot.agents for request " + updatedRequest.request_id);
57
+ winston.debug("updateRequestSnapshot updated snapshot visibility for request " + updatedRequest.request_id);
59
58
  } else {
60
59
  winston.warn("updateRequestSnapshot: Request not found for " + request.request_id);
61
60
  }
@@ -0,0 +1,185 @@
1
+ const Request = require('../models/request');
2
+ const Department = require('../models/department');
3
+ const Lead = require('../models/lead');
4
+ const leadService = require('./leadService');
5
+ const requestService = require('./requestService');
6
+ const messageService = require('./messageService');
7
+ const requestEvent = require('../event/requestEvent');
8
+ const UIDGenerator = require('../utils/UIDGenerator');
9
+ const ChannelConstants = require('../models/channelConstants');
10
+ const winston = require('../config/winston');
11
+ const startPointService = require('./startPointService');
12
+
13
+ const SOURCE_CHANNEL = ChannelConstants.WEBHOOK;
14
+ const START_TEXT = '/start';
15
+ const START_NOT_ENABLED = 'Webhook start is not enabled';
16
+
17
+ class WebhookConversationError extends Error {
18
+ constructor(status, message) {
19
+ super(message);
20
+ this.status = status;
21
+ }
22
+ }
23
+
24
+ class WebhookConversationService {
25
+
26
+ constructor() {
27
+ this.SOURCE_CHANNEL = SOURCE_CHANNEL;
28
+ }
29
+
30
+ newRequestId(id_project) {
31
+ return 'support-group-' + id_project + '-' + UIDGenerator.generate();
32
+ }
33
+
34
+ /**
35
+ * MongoDB rejects document keys containing '.' and keys starting with '$' (mongoose
36
+ * 5.13.17 / driver 3.7.3: "key <k> must not contain '.'"). Webhook payloads (e.g.
37
+ * Kubernetes/Grafana alert labels such as "app.kubernetes.io/name") routinely have such
38
+ * keys, so they are rewritten recursively before storage: every '.' becomes '_' and a
39
+ * leading '$' becomes '_'. Values are left untouched (recursed into for nested
40
+ * objects/arrays). If two keys collapse onto the same sanitized name, the later one
41
+ * (per Object.keys iteration order) overwrites the earlier one.
42
+ */
43
+ sanitizePayloadKeys(value) {
44
+ if (Array.isArray(value)) {
45
+ return value.map((item) => this.sanitizePayloadKeys(item));
46
+ }
47
+ if (value !== null && typeof value === 'object' && value.constructor === Object) {
48
+ const result = {};
49
+ for (const key of Object.keys(value)) {
50
+ let sanitizedKey = key.replace(/\./g, '_');
51
+ if (sanitizedKey.startsWith('$')) {
52
+ sanitizedKey = '_' + sanitizedKey.slice(1);
53
+ }
54
+ result[sanitizedKey] = this.sanitizePayloadKeys(value[key]);
55
+ }
56
+ return result;
57
+ }
58
+ return value;
59
+ }
60
+
61
+ /**
62
+ * Department of a webhook conversation: the project's default department if its bot is
63
+ * this chatbot; else the oldest department (by createdAt) whose bot is this chatbot; else
64
+ * the project's default department. Only active departments (status > 0; a missing status
65
+ * is the schema default 1) are matched on the bot: status 0 is hidden for the widget and
66
+ * -1 hidden for the dashboard. The final fallback is always the project default.
67
+ */
68
+ async resolveChatbotDepartment(id_project, chatbot_id) {
69
+ const chatbotId = chatbot_id.toString();
70
+ const isActive = (department) => department.status === undefined || department.status === null || department.status > 0;
71
+ const defaultDepartment = await Department.findOne({ id_project: id_project, default: true }).lean().exec();
72
+ if (defaultDepartment && defaultDepartment.id_bot === chatbotId && isActive(defaultDepartment)) {
73
+ return defaultDepartment;
74
+ }
75
+ const attached = await Department.findOne({ id_project: id_project, id_bot: chatbotId, status: { $not: { $lte: 0 } } }).sort({ createdAt: 1 }).lean().exec();
76
+ if (attached) {
77
+ return attached;
78
+ }
79
+ if (!defaultDepartment) {
80
+ throw new WebhookConversationError(422, 'Webhook misconfigured: default department not found for project ' + id_project);
81
+ }
82
+ return defaultDepartment;
83
+ }
84
+
85
+ /**
86
+ * The enabled webhook start point of a conversation webhook, or undefined.
87
+ */
88
+ enabledStartPoint(webhook) {
89
+ const startPoint = startPointService.find(webhook, 'webhook');
90
+ return startPoint && startPoint.enabled !== false ? startPoint : undefined;
91
+ }
92
+
93
+ /**
94
+ * Starts a chatbot conversation the same way the widget does (request with the bot as
95
+ * participant, then the system /start info message that botEvent forwards to the bot), at
96
+ * the webhook start box: the message carries attributes.action = '#<block_id>'.
97
+ */
98
+ async start(webhook, chatbot, payload, options = {}) {
99
+ const startPoint = options.startPoint || this.enabledStartPoint(webhook);
100
+ if (!startPoint) {
101
+ throw new WebhookConversationError(422, START_NOT_ENABLED);
102
+ }
103
+ const id_project = webhook.id_project.toString();
104
+ const request_id = options.request_id || this.newRequestId(id_project);
105
+ const department = await this.resolveChatbotDepartment(id_project, chatbot._id);
106
+
107
+ const startType = options.startType || 'webhook';
108
+ const lead_id = options.leadId || 'webhook_' + webhook.webhook_id;
109
+ const leadName = options.leadName || (startPoint.mapping && startPoint.mapping.source_name) || chatbot.name || webhook.name || 'Webhook';
110
+ let lead = await leadService.createIfNotExistsWithLeadId(
111
+ lead_id,
112
+ leadName,
113
+ undefined,
114
+ id_project,
115
+ 'system',
116
+ startType === 'webhook' ? { webhook_id: webhook.webhook_id } : { chatbot_id: chatbot._id.toString() }
117
+ );
118
+ if (lead.fullname !== leadName) {
119
+ // The webhook lead is shared by all of the webhook's conversations: rename it silently.
120
+ // leadService.updateWitId would emit lead.update, and chat21Handler would then update the
121
+ // chat21 group of every past request of this lead. Only new conversations get the new name.
122
+ await Lead.updateOne({ id_project: id_project, lead_id: lead_id }, { $set: { fullname: leadName } }).exec();
123
+ lead.fullname = leadName;
124
+ }
125
+
126
+ const attributes = { start_type: startType, payload: this.sanitizePayloadKeys(payload || {}) };
127
+ if (startType === 'webhook') {
128
+ attributes.webhook_id = webhook.webhook_id;
129
+ }
130
+ Object.assign(attributes, options.extraAttributes || {});
131
+ let sourcePage;
132
+ if (options.dev === true) {
133
+ // BotSubscriptionNotifier matches "&td_draft=true", requestService.create matches "td_draft=true"
134
+ sourcePage = (startType === 'webhook' ? 'webhook://' + webhook.webhook_id : 'scheduled://' + chatbot._id.toString()) + '?dev=true&td_draft=true';
135
+ attributes.sourcePage = sourcePage;
136
+ }
137
+
138
+ try {
139
+ await requestService.create({
140
+ request_id: request_id,
141
+ lead_id: lead._id,
142
+ lead: lead,
143
+ id_project: id_project,
144
+ first_text: START_TEXT,
145
+ departmentid: department._id.toString(),
146
+ participants: ['bot_' + chatbot._id.toString()],
147
+ channel: { name: SOURCE_CHANNEL },
148
+ sourcePage: sourcePage,
149
+ preflight: false,
150
+ createdBy: 'system',
151
+ attributes: attributes
152
+ });
153
+ } catch (err) {
154
+ if (err && err.message && err.message.startsWith('Requests limits reached')) {
155
+ throw new WebhookConversationError(403, 'Requests quota exceeded for project ' + id_project);
156
+ }
157
+ throw err;
158
+ }
159
+
160
+ // requestService.create drops the department when participants are passed (see Global Constraints)
161
+ const updatedRequest = await Request.findOneAndUpdate(
162
+ { request_id: request_id, id_project: id_project },
163
+ { $set: { department: department._id, 'snapshot.department': department } },
164
+ { new: true }
165
+ )
166
+ .populate('lead')
167
+ .populate('department')
168
+ .populate('participatingBots')
169
+ .populate('participatingAgents')
170
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
171
+ .exec();
172
+ requestEvent.emit('request.update', updatedRequest);
173
+
174
+ await messageService.send('system', 'Bot', request_id, START_TEXT, id_project, null, { subtype: 'info', updateconversation: false, action: '#' + startPoint.block_id });
175
+
176
+ winston.verbose('(WebhookConversationService) conversation ' + request_id + ' started from ' + startType + ' ' + webhook.webhook_id);
177
+ return { request_id: request_id, request: updatedRequest };
178
+ }
179
+ }
180
+
181
+ const webhookConversationService = new WebhookConversationService();
182
+ webhookConversationService.WebhookConversationError = WebhookConversationError;
183
+ webhookConversationService.START_NOT_ENABLED = START_NOT_ENABLED;
184
+
185
+ module.exports = webhookConversationService;
@@ -0,0 +1,61 @@
1
+ const mongoose = require('mongoose');
2
+ const Faq_kb = require('../models/faq_kb');
3
+ const Faq = require('../models/faq');
4
+ const chatbotTypes = require('../models/chatbotTypes');
5
+
6
+ const CONVERSATION_MODE = 'conversation';
7
+ const START_INTENT_NAME = 'start';
8
+
9
+ class WebhookSettingsValidator {
10
+
11
+ constructor() {
12
+ this.CONVERSATION_MODE = CONVERSATION_MODE;
13
+ }
14
+
15
+ async findChatbot(id_project, chatbot_id) {
16
+ if (!chatbot_id || !mongoose.Types.ObjectId.isValid(chatbot_id)) {
17
+ return null;
18
+ }
19
+ return Faq_kb.findOne({ _id: chatbot_id, id_project: id_project, trashed: { $in: [null, false] } }).lean().exec();
20
+ }
21
+
22
+ /**
23
+ * Validates the conversation-related fields of a webhook create/update body.
24
+ * Only the keys present in body are returned in fields.
25
+ * @returns {Promise<{ok: true, fields: Object} | {ok: false, status: number, message: string}>}
26
+ */
27
+ async validate(id_project, chatbot, body) {
28
+ const fields = {};
29
+
30
+ if (body.hasOwnProperty('mode')) {
31
+ if (body.mode !== CONVERSATION_MODE) {
32
+ return { ok: false, status: 422, message: "Invalid webhook mode " + body.mode };
33
+ }
34
+ const subtype = chatbot.subtype || chatbotTypes.CHATBOT;
35
+ if (subtype !== chatbotTypes.CHATBOT) {
36
+ return { ok: false, status: 422, message: "Conversation mode requires a chatbot with subtype " + chatbotTypes.CHATBOT };
37
+ }
38
+ fields.mode = CONVERSATION_MODE;
39
+
40
+ if (body.block_id) {
41
+ fields.block_id = body.block_id;
42
+ } else {
43
+ const startIntent = await Faq.findOne({ id_faq_kb: chatbot._id.toString(), intent_display_name: START_INTENT_NAME }).lean().exec();
44
+ if (!startIntent) {
45
+ return { ok: false, status: 422, message: "Start block not found for chatbot " + chatbot._id };
46
+ }
47
+ fields.block_id = startIntent.intent_id;
48
+ }
49
+ } else if (body.block_id) {
50
+ fields.block_id = body.block_id;
51
+ }
52
+
53
+ if (body.hasOwnProperty('block_id') && body.block_id && typeof body.block_id !== 'string') {
54
+ return { ok: false, status: 422, message: "block_id must be a string" };
55
+ }
56
+
57
+ return { ok: true, fields: fields };
58
+ }
59
+ }
60
+
61
+ module.exports = new WebhookSettingsValidator();
@@ -57,6 +57,11 @@
57
57
  <td style="border: 1px solid #ddd; padding: 8px;">{{email_quote}}</td>
58
58
  <td style="border: 1px solid #ddd; padding: 8px;">{{email_perc}}%</td>
59
59
  </tr>
60
+ <tr>
61
+ <td style="border: 1px solid #ddd; padding: 8px;">AI Agent Tokens</td>
62
+ <td style="border: 1px solid #ddd; padding: 8px;">{{agent_tokens_quote}}</td>
63
+ <td style="border: 1px solid #ddd; padding: 8px;">{{agent_tokens_perc}}%</td>
64
+ </tr>
60
65
  </table>
61
66
  <p style="text-align: center; font-size: 14px; color: #777;">This is an automated message. Please do not reply.</p>
62
67
  <div style="text-align: center; margin-top: 20px; padding: 20px;">
@@ -1,4 +1,5 @@
1
1
  process.env.NODE_ENV = 'test';
2
+ process.env.LOG_LEVEL = 'critical';
2
3
  let chai = require('chai');
3
4
  let expect = require('chai').expect;
4
5
 
@@ -53,7 +54,7 @@ describe('Agent Prompts Route', () => {
53
54
  expect(res.body.origin).to.deep.equal({ kind: 'dashboard' });
54
55
  expect(res.body.attachments).to.be.an('array').that.is.empty;
55
56
  expect(res.body.createdAt).to.be.a('string');
56
- });
57
+ }).timeout(10000);
57
58
 
58
59
  it('keeps the web page the prompt was written on', async () => {
59
60
  const { email, pwd, project, bot } = await newOwnerWithAgent('origin');
@@ -105,7 +106,7 @@ describe('Agent Prompts Route', () => {
105
106
  expect((await post({ ...valid, origin: { kind: 'web' } })).status).to.equal(400);
106
107
  expect((await post({ ...valid, origin: { kind: 'web', url: 'javascript:alert(1)' } })).status).to.equal(400);
107
108
  expect((await post({ ...valid, origin: { kind: 'web', url: 'https://x.test/' + 'a'.repeat(2048) } })).status).to.equal(400);
108
- });
109
+ }).timeout(3000);
109
110
 
110
111
  it('refuses an agent of another project', async () => {
111
112
  const mine = await newOwnerWithAgent('mine');