@tiledesk/tiledesk-server 3.0.0 → 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 (138) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/CHANGELOG.md +37 -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 +14 -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/agentPrompt.js +104 -0
  45. package/models/analyticResult.js +1 -1
  46. package/models/channelConstants.js +3 -2
  47. package/models/quotaUsage.js +71 -0
  48. package/models/request.js +6 -0
  49. package/models/requestSnapshot.js +12 -0
  50. package/models/routerLogger.js +1 -1
  51. package/models/webhook.js +44 -0
  52. package/package.json +7 -5
  53. package/pubmodules/emailNotification/requestNotification.js +14 -22
  54. package/pubmodules/events/test/eventRoute.js +7 -6
  55. package/pubmodules/rules/conciergeBot.js +9 -4
  56. package/routes/agentPrompts.js +132 -0
  57. package/routes/auth.js +1 -12
  58. package/routes/campaigns.js +0 -13
  59. package/routes/dataTable.js +0 -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/departmentService.js +60 -43
  72. package/services/dolphinSchedulerClient.js +116 -0
  73. package/services/emailService.js +6 -1
  74. package/services/hitlParticipantListener.js +260 -0
  75. package/services/participantInviteService.js +302 -0
  76. package/services/participantRemoveService.js +79 -0
  77. package/services/quotaSnapshotService.js +1167 -0
  78. package/services/requestService.js +102 -144
  79. package/services/scheduledStart/dolphinBackend.js +246 -0
  80. package/services/scheduledStart/schedulerBackend.js +101 -0
  81. package/services/scheduledStart/schedulerServiceClient.js +41 -0
  82. package/services/scheduledStartPublishListener.js +186 -0
  83. package/services/scheduledStartRules.js +173 -0
  84. package/services/scheduledStartService.js +130 -0
  85. package/services/startPointCleanupListener.js +30 -0
  86. package/services/startPointService.js +158 -0
  87. package/services/testWsService.js +1 -4
  88. package/services/updateRequestSnapshotQueued.js +8 -9
  89. package/services/webhookConversationService.js +185 -0
  90. package/services/webhookSettingsValidator.js +61 -0
  91. package/template/email/checkpointReachedEmail.html +5 -0
  92. package/test/agentPromptsRoute.test.js +154 -0
  93. package/test/agentTokensQuota.js +237 -0
  94. package/test/botEventHitlFallback.js +19 -0
  95. package/test/chat21HandlerJoinWiring.js +59 -0
  96. package/test/dataTablesRoute.test.js +10 -2
  97. package/test/dateUtils.test.js +0 -3
  98. package/test/departmentService.js +8 -12
  99. package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
  100. package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
  101. package/test/faqRoute.js +6 -1
  102. package/test/faqkbRoute.js +14 -10
  103. package/test/faqkbV3Route.js +1 -1
  104. package/test/filepRoute.js +4 -50
  105. package/test/hitlParticipantListener.js +446 -0
  106. package/test/hitlQueueWiring.js +191 -0
  107. package/test/hitlWaitingReset.js +124 -0
  108. package/test/inviteParticipantsRoute.js +492 -0
  109. package/test/kbRoute.js +1 -1
  110. package/test/messageRoute.js +33 -21
  111. package/test/nativemcp.test.js +7 -1
  112. package/test/openaiRoute.js +2 -8
  113. package/test/publishFailLoud.test.js +414 -0
  114. package/test/quotaSnapshots.js +1044 -0
  115. package/test/removeParticipantsRoute.js +186 -0
  116. package/test/requestService.js +24 -16
  117. package/test/requestServiceRemoveParticipant.js +144 -0
  118. package/test/requestVisibility.test.js +186 -0
  119. package/test/scheduledStartFacade.js +184 -0
  120. package/test/scheduledStartHandover.js +95 -0
  121. package/test/scheduledStartPublish.js +320 -0
  122. package/test/scheduledStartRoute.js +163 -0
  123. package/test/scheduledStartRules.js +177 -0
  124. package/test/scheduledStartRun.js +299 -0
  125. package/test/scheduledStartSchedulerBackend.js +116 -0
  126. package/test/scheduledStartService.js +290 -0
  127. package/test/startPointsCleanup.js +69 -0
  128. package/test/startPointsRegression.js +169 -0
  129. package/test/startPointsRoute.js +158 -0
  130. package/test/webhookConversationRoute.js +347 -0
  131. package/test/webhookConversationService.js +261 -0
  132. package/test/webhookConversationTestUtils.js +55 -0
  133. package/utils/aiUtils.js +95 -1
  134. package/utils/intentFieldNames.js +192 -0
  135. package/utils/quotaCliUtil.js +43 -0
  136. package/utils/requestVisibility.js +355 -0
  137. package/websocket/webSocketServer.js +36 -31
  138. /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
@@ -1,7 +1,11 @@
1
1
  const axios = require("axios").default;
2
+ const mongoose = require("mongoose");
2
3
  const winston = require('../config/winston');
3
4
  const botEvent = require('../event/botEvent');
4
5
  let Faq_kb = require("../models/faq_kb");
6
+ let Faq = require("../models/faq");
7
+ const AppError = require("../utils/AppError");
8
+ const intentFieldNames = require("../utils/intentFieldNames");
5
9
 
6
10
  class ChatbotService {
7
11
 
@@ -156,13 +160,63 @@ class ChatbotService {
156
160
  return endpoint;
157
161
  }
158
162
 
163
+ /**
164
+ * 422 naming the agent attributes and every block of the source whose field names MongoDB cannot
165
+ * insert. Only the project's own source is read: a source of another project is left to the fork,
166
+ * which refuses it without telling anything about its content.
167
+ */
168
+ async assertPublishable(sourceId, project_id) {
169
+ if (!mongoose.Types.ObjectId.isValid(String(sourceId))) {
170
+ return;
171
+ }
172
+ const source = await Faq_kb.findOne({ _id: sourceId, id_project: String(project_id) }).lean().exec();
173
+ if (!source) {
174
+ return;
175
+ }
176
+ const intents = await Faq.find({ id_faq_kb: String(sourceId), id_project: String(project_id) }).lean().exec();
177
+ const errors = [
178
+ ...intentFieldNames.collectAgentErrors(source.name, source.attributes),
179
+ ...intentFieldNames.collectIntentErrors(intents)
180
+ ];
181
+ if (errors.length > 0) {
182
+ winston.warn("(ChatbotService) publish of " + sourceId + " refused: " + JSON.stringify(errors));
183
+ throw intentFieldNames.publishError(errors);
184
+ }
185
+ }
186
+
187
+ /** The fork (copy) failed: say so in plain words, keeping the copy's own reasons. */
188
+ publishFailure(err) {
189
+ const data = (err && err.response && err.response.data) || {};
190
+ const status = err && err.response && err.response.status;
191
+ if (Array.isArray(data.errors) && data.errors.length > 0 && status >= 400 && status < 500) {
192
+ return intentFieldNames.publishError(data.errors);
193
+ }
194
+ const reason = data.message || data.error || (err && err.message) || "the copy could not be made";
195
+ const failure = new AppError(status >= 400 && status < 500 ? 422 : 500,
196
+ "The flow was not published: " + reason + ". The published version was not changed.");
197
+ if (Array.isArray(data.errors)) {
198
+ failure.errors = data.errors;
199
+ }
200
+ return failure;
201
+ }
202
+
159
203
  async publish({ id_faq_kb, chatbot_id, api_url, token, project_id, user_id, release_note, req }) {
160
204
  const sourceId = chatbot_id || id_faq_kb;
161
205
  winston.debug("(ChatbotService) publish id_faq_kb: " + id_faq_kb + " source: " + sourceId);
162
206
 
207
+ // All or nothing. The copy is a new bot and the draft's url moves to it only at the end, so the
208
+ // published version changes only when every block was copied. A block MongoDB cannot insert is
209
+ // found before anything is written; any other failure of the copy removes the copy (fork rollback).
210
+ await this.assertPublishable(sourceId, project_id);
211
+
163
212
  // Keep callsubagent.botId on the development subagent. Its url points at the published snapshot.
164
213
  // for_publish skips FAQ_KB_CREATE on the trashed snapshot; FAQ_KB_PUBLISH is emitted below.
165
- const forked = await this.fork(sourceId, api_url, token, project_id, { skipSubagents: true, forPublish: true });
214
+ let forked;
215
+ try {
216
+ forked = await this.fork(sourceId, api_url, token, project_id, { skipSubagents: true, forPublish: true });
217
+ } catch (err) {
218
+ throw this.publishFailure(err);
219
+ }
166
220
  const forkedChatBotId = forked.bot_id;
167
221
  winston.debug("(ChatbotService) publish forkedChatBotId: " + forkedChatBotId);
168
222
 
@@ -273,55 +273,72 @@ getOperators(departmentid, projectid, nobot, disableWebHookCall, context) {
273
273
  // console.log('OPERATORS - »»»» BOT IS DEFINED -> ID BOT', department.id_bot);
274
274
  // console.log('OPERATORS - »»»» nobot ', nobot)
275
275
  winston.debug("main_flow_cache_2 departmentService project users");
276
-
277
-
278
- // rolequery
279
- // var role = [RoleConstants.OWNER, RoleConstants.ADMIN, RoleConstants.SUPERVISOR, RoleConstants.AGENT];
280
- // var qpu = Project_user.find({ id_project: projectid, role: { $in : role }, status: "active" });
281
- var qpu = Project_user.find({ id_project: projectid, roleType: RoleConstants.TYPE_AGENTS, status: "active" });
282
-
283
-
284
- // use this. $in doesn't use index very well
285
- // var qpu = Project_user.findOne({ id_project: projectid, $or: [{ "role": RoleConstants.AGENT }, { "role": RoleConstants.SUPERVISOR }, { "role": RoleConstants.ADMIN }, { "role": RoleConstants.OWNER }], status: "active" })
286
-
287
- if (cacheEnabler.project_user) {
288
- qpu.cache(cacheUtil.queryTTL, projectid+":project_users:query:teammates:available") //request_cache
289
- winston.debug('project_user cache enabled');
290
- }
291
-
292
-
293
- return qpu.exec(function (err, project_users) {
294
- if (err) {
295
- winston.error('-- > 2 DEPT FIND BY ID ERR ', err)
296
- return reject(err);
297
- }
298
- // console.log('OPERATORS - BOT IS DEFINED - MEMBERS ', project_users)
299
- // console.log('OPERATORS - BOT IS DEFINED - MEMBERS LENGHT ', project_users.length);
300
276
 
301
- // getAvailableOperatorsWithOperatingHours: IN BASE ALLE 'OPERATING HOURS' DEL PROGETTO ESEGUE
302
- // getAvailableOperator CHE RITORNA I PROJECT USER DISPONIBILI
277
+ const resolveBotOperators = function (project_users, group) {
303
278
  return that.getAvailableOperatorsWithOperatingHours(project_users, projectid).then(function (_available_agents) {
304
-
305
- // console.log("D -> [ OPERATORS - BOT IS DEFINED ] -> AVAILABLE PROJECT-USERS: ", _available_agents);
306
-
307
- // here subscription notifier??
308
- return resolve ({
309
- department: department, available_agents: _available_agents, agents: project_users,
310
- id_bot:department.id_bot, project: project,
311
- context: context,
312
- // botprefix
313
- operators: [{ id_user: 'bot_' + department.id_bot }]
314
- });
279
+ const payload = {
280
+ department: department,
281
+ available_agents: _available_agents,
282
+ agents: project_users,
283
+ id_bot: department.id_bot,
284
+ project: project,
285
+ context: context,
286
+ operators: [{ id_user: 'bot_' + department.id_bot }]
287
+ };
288
+ if (group) {
289
+ payload.group = group;
290
+ }
291
+ return resolve(payload);
315
292
  }).catch(function (error) {
316
-
317
- // winston.error("Write failed: ", error);
318
-
319
293
  winston.error("Error D -> [ OPERATORS - BOT IS DEFINED ] -> AVAILABLE PROJECT-USERS: ", error);
320
-
321
294
  return reject(error);
322
295
  });
323
-
324
- });
296
+ };
297
+
298
+ const loadBotProjectUsers = function (userQuery, group, useCache) {
299
+ var qpu = Project_user.find(userQuery);
300
+ if (useCache && cacheEnabler.project_user) {
301
+ qpu.cache(cacheUtil.queryTTL, projectid+":project_users:query:teammates:available") //request_cache
302
+ winston.debug('project_user cache enabled');
303
+ }
304
+ return qpu.exec(function (err, project_users) {
305
+ if (err) {
306
+ winston.error('-- > 2 DEPT FIND BY ID ERR ', err)
307
+ return reject(err);
308
+ }
309
+ return resolveBotOperators(project_users || [], group);
310
+ });
311
+ };
312
+
313
+ // A department group limits who can see the conversation, including when a bot is assigned.
314
+ if (department.id_group != null) {
315
+ return Group.find({ _id: department.id_group, $or: [ { enabled: true }, { enabled: { $exists: false } } ] }).exec(function (err, group) {
316
+ if (err) {
317
+ winston.error('BOT GROUP -> [ OPERATORS ] -> ERR ', err);
318
+ return reject(err);
319
+ }
320
+ if (!group || !group[0]) {
321
+ return resolve({
322
+ department: department,
323
+ available_agents: [],
324
+ agents: [],
325
+ id_bot: department.id_bot,
326
+ project: project,
327
+ context: context,
328
+ operators: [{ id_user: 'bot_' + department.id_bot }],
329
+ group: [{ _id: department.id_group, members: [] }]
330
+ });
331
+ }
332
+ return loadBotProjectUsers({
333
+ id_project: projectid,
334
+ id_user: { $in: group[0].members },
335
+ roleType: RoleConstants.TYPE_AGENTS,
336
+ status: "active"
337
+ }, group, false);
338
+ });
339
+ }
340
+
341
+ return loadBotProjectUsers({ id_project: projectid, roleType: RoleConstants.TYPE_AGENTS, status: "active" }, null, true);
325
342
  }
326
343
 
327
344
  else { // if (department.routing === 'assigned' || department.routing === 'pooled') {
@@ -0,0 +1,116 @@
1
+ const axios = require('axios').default;
2
+ const moment = require('moment-timezone');
3
+
4
+ const NOT_FOUND_RE = /not exist|does not exist|not found/i;
5
+
6
+ function cfg() {
7
+ return {
8
+ base: process.env.SCHEDULER_BASEURL,
9
+ token: process.env.SCHEDULER_TOKEN,
10
+ project: process.env.SCHEDULER_PROJECT,
11
+ };
12
+ }
13
+
14
+ /** Calls DolphinScheduler; a body with code !== 0 or success === false is an error even on HTTP 200. */
15
+ async function call(method, path, params, timeoutMs) {
16
+ const { base, token, project } = cfg();
17
+ let response;
18
+ try {
19
+ response = await axios({
20
+ url: `${base}/projects/${project}${path}`,
21
+ method,
22
+ headers: { token },
23
+ params,
24
+ timeout: timeoutMs || 30000,
25
+ });
26
+ } catch (err) {
27
+ const body = err.response && err.response.data;
28
+ const e = new Error('DolphinScheduler ' + method + ' ' + path + ' failed: ' +
29
+ ((body && body.msg) || err.message));
30
+ e.status = (err.response && err.response.status) || 502;
31
+ e.notFound = e.status === 404 || NOT_FOUND_RE.test((body && body.msg) || '');
32
+ throw e;
33
+ }
34
+ const body = response.data;
35
+ if (body && typeof body === 'object' && (body.success === false || (body.code !== undefined && body.code !== 0))) {
36
+ const e = new Error('DolphinScheduler ' + method + ' ' + path + ' failed: ' + (body.msg || 'code ' + body.code));
37
+ e.status = 502;
38
+ e.dsCode = body.code;
39
+ e.notFound = NOT_FOUND_RE.test(body.msg || '');
40
+ throw e;
41
+ }
42
+ return body ? body.data : undefined;
43
+ }
44
+
45
+ async function createWorkflow(name, httpTask) {
46
+ const data = await call('POST', '/process-definition', Object.assign({ name }, httpTask));
47
+ return { code: data.code };
48
+ }
49
+
50
+ async function releaseWorkflow(code, state) {
51
+ return call('POST', `/process-definition/${code}/release`, { releaseState: state });
52
+ }
53
+
54
+ async function deleteWorkflow(code) {
55
+ return call('DELETE', `/process-definition/${code}`);
56
+ }
57
+
58
+ async function findWorkflowByName(name) {
59
+ const data = await call('GET', '/process-definition', { searchVal: name, pageNo: 1, pageSize: 100 });
60
+ const list = (data && (data.totalList || data.list)) || (Array.isArray(data) ? data : []);
61
+ const found = list.find((w) => (w.name || (w.processDefinition && w.processDefinition.name)) === name);
62
+ if (!found) return null;
63
+ return { code: found.code || found.processDefinition.code };
64
+ }
65
+
66
+ function scheduleJson({ crontab, timezoneId }) {
67
+ // DolphinScheduler reads startTime in timezoneId: "now" must be that zone's wall clock,
68
+ // or the trigger starts up to the UTC offset late (first run skipped west of UTC).
69
+ return JSON.stringify({
70
+ startTime: moment.tz(timezoneId || 'UTC').format('YYYY-MM-DD HH:mm:ss'),
71
+ endTime: '2099-12-31 23:59:59',
72
+ crontab,
73
+ timezoneId,
74
+ });
75
+ }
76
+
77
+ async function createSchedule(code, spec) {
78
+ const data = await call('POST', '/schedules', {
79
+ processDefinitionCode: code,
80
+ schedule: scheduleJson(spec),
81
+ warningType: 'NONE',
82
+ failureStrategy: 'CONTINUE',
83
+ processInstancePriority: 'MEDIUM',
84
+ });
85
+ return { id: data.id };
86
+ }
87
+
88
+ async function updateSchedule(id, code, spec) {
89
+ return call('PUT', `/schedules/${id}`, {
90
+ processDefinitionCode: code,
91
+ schedule: scheduleJson(spec),
92
+ warningType: 'NONE',
93
+ failureStrategy: 'CONTINUE',
94
+ processInstancePriority: 'MEDIUM',
95
+ });
96
+ }
97
+
98
+ async function listSchedules(code) {
99
+ const data = await call('GET', '/schedules', { processDefinitionCode: code, pageNo: 1, pageSize: 10 });
100
+ const list = (data && (data.totalList || data.list)) || (Array.isArray(data) ? data : []);
101
+ return list.map((x) => ({ id: x.id }));
102
+ }
103
+
104
+ const deleteSchedule = (id) => call('DELETE', `/schedules/${id}`);
105
+ const scheduleOnline = (id) => call('POST', `/schedules/${id}/online`);
106
+ const scheduleOffline = (id) => call('POST', `/schedules/${id}/offline`);
107
+
108
+ async function previewSchedule(spec, timeoutMs) {
109
+ const data = await call('POST', '/schedules/preview', { schedule: scheduleJson(spec) }, timeoutMs);
110
+ return Array.isArray(data) ? data : [];
111
+ }
112
+
113
+ module.exports = {
114
+ createWorkflow, releaseWorkflow, deleteWorkflow, findWorkflowByName,
115
+ createSchedule, updateSchedule, listSchedules, deleteSchedule, scheduleOnline, scheduleOffline, previewSchedule,
116
+ };
@@ -1867,6 +1867,9 @@ class EmailService {
1867
1867
  let email_quote = quotes.email.quote;
1868
1868
  let email_perc = quotes.email.perc;
1869
1869
 
1870
+ let agent_tokens_quote = quotes.agent_tokens ? quotes.agent_tokens.quote : 0;
1871
+ let agent_tokens_perc = quotes.agent_tokens ? quotes.agent_tokens.perc : 0;
1872
+
1870
1873
  let replacements = {
1871
1874
  firstname: firstname,
1872
1875
  project_name: project_name,
@@ -1877,7 +1880,9 @@ class EmailService {
1877
1880
  tokens_quote: tokens_quote,
1878
1881
  tokens_perc: tokens_perc,
1879
1882
  email_quote: email_quote,
1880
- email_perc: email_perc
1883
+ email_perc: email_perc,
1884
+ agent_tokens_quote: agent_tokens_quote,
1885
+ agent_tokens_perc: agent_tokens_perc
1881
1886
  }
1882
1887
 
1883
1888
  html = template(replacements);
@@ -0,0 +1,260 @@
1
+ const Request = require('../models/request');
2
+ const RequestConstants = require('../models/requestConstants');
3
+ const ChannelConstants = require('../models/channelConstants');
4
+ const requestEvent = require('../event/requestEvent');
5
+ const requestService = require('./requestService');
6
+ const participantInviteService = require('./participantInviteService');
7
+ const messageService = require('./messageService');
8
+ const winston = require('../config/winston');
9
+
10
+ const FALLBACK_TEXT = 'The invited human left and no replacement is available';
11
+ const FALLBACK_FLOW_ERROR = '(Invite human) The invited human left and no replacement is available';
12
+
13
+ const HITL_EXISTS = {
14
+ $or: [
15
+ { 'attributes.hitl_waiting': { $exists: true } },
16
+ { 'attributes.hitl_invited': { $exists: true } },
17
+ { 'attributes.hitl_fallback': { $exists: true } }
18
+ ]
19
+ };
20
+
21
+ const isBot = (member) => typeof member === 'string' && member.startsWith('bot_');
22
+ const toId = (value) => value && value.toString();
23
+
24
+ function baseFilter(request) {
25
+ return {
26
+ request_id: request.request_id,
27
+ id_project: request.id_project,
28
+ status: { $ne: RequestConstants.CLOSED },
29
+ 'channel.name': ChannelConstants.WEBHOOK
30
+ };
31
+ }
32
+
33
+ function loadPopulated(request_id, id_project) {
34
+ return Request.findOne({ request_id: request_id, id_project: id_project })
35
+ .populate('lead')
36
+ .populate('department')
37
+ .populate('participatingBots')
38
+ .populate('participatingAgents')
39
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
40
+ .exec();
41
+ }
42
+
43
+ class HitlParticipantListener {
44
+
45
+ listen() {
46
+ let leaveKey = 'request.participants.leave';
47
+ let updateKey = 'request.participants.update';
48
+ if (requestEvent.queueEnabled) {
49
+ leaveKey = 'request.participants.leave.queue';
50
+ updateKey = 'request.participants.update.queue';
51
+ }
52
+ winston.info('Listening ' + leaveKey + ' and ' + updateKey + ' events for HITL participants');
53
+
54
+ requestEvent.on(leaveKey, (data) => {
55
+ this.onLeave(data).catch((err) => winston.error('(HitlParticipantListener) error handling participant leave', err));
56
+ });
57
+ requestEvent.on(updateKey, (data) => {
58
+ this.onUpdate(data).catch((err) => winston.error('(HitlParticipantListener) error handling participants update', err));
59
+ });
60
+ }
61
+
62
+ async onLeave({ member, request } = {}) {
63
+ try {
64
+ if (!request || !member) {
65
+ return;
66
+ }
67
+ await this.handleLeave(toId(member), request, { pick: true });
68
+ } catch (err) {
69
+ winston.error('(HitlParticipantListener) onLeave error', err);
70
+ }
71
+ }
72
+
73
+ async onUpdate({ request, removedParticipants, addedParticipants } = {}) {
74
+ try {
75
+ if (!request) {
76
+ return;
77
+ }
78
+ const added = (addedParticipants || []).map(toId);
79
+ const removed = (removedParticipants || []).map(toId);
80
+ const addedHumans = added.filter((m) => !isBot(m));
81
+
82
+ if (addedHumans.length > 0) {
83
+ // only a conversation where the flow is (or was) waiting for a human tracks them
84
+ await Request.updateOne(
85
+ Object.assign({}, baseFilter(request), {
86
+ $or: [{ 'attributes.hitl_waiting': true }, { 'attributes.hitl_invited': { $exists: true } }]
87
+ }),
88
+ { $addToSet: { 'attributes.hitl_invited': { $each: addedHumans } } }
89
+ ).exec();
90
+ }
91
+ if (removed.some(isBot)) {
92
+ // the flow's bot is gone: nothing left to resume, so clear once and skip the removed humans
93
+ await this.handleLeave(removed.find(isBot), request, { pick: false });
94
+ return;
95
+ }
96
+ for (const member of removed) {
97
+ try {
98
+ await this.handleLeave(member, request, { pick: addedHumans.length === 0 });
99
+ } catch (err) {
100
+ winston.error('(HitlParticipantListener) onUpdate error for member ' + member, err);
101
+ }
102
+ }
103
+ } catch (err) {
104
+ winston.error('(HitlParticipantListener) onUpdate error', err);
105
+ }
106
+ }
107
+
108
+ async handleLeave(member, request, { pick }) {
109
+ if (request.channel && request.channel.name !== ChannelConstants.WEBHOOK) {
110
+ return;
111
+ }
112
+
113
+ if (isBot(member)) {
114
+ await this.clearWaiting(request, {});
115
+ return;
116
+ }
117
+
118
+ // step 1: atomic, so with two humans leaving together only the last one out gets past step 2.
119
+ // attempt W: the flow waits on a human, so the leaver abandoned a pending question
120
+ let updated = await Request.findOneAndUpdate(
121
+ Object.assign({}, baseFilter(request), { 'attributes.hitl_invited': member, 'attributes.hitl_waiting': true }),
122
+ { $pull: { 'attributes.hitl_invited': member }, $addToSet: { 'attributes.hitl_left': member } },
123
+ { new: true }
124
+ ).exec();
125
+ if (!updated) {
126
+ // attempt N: nothing is pending (the human already answered): a plain release, the leaver abandoned nothing
127
+ const released = await Request.findOneAndUpdate(
128
+ Object.assign({}, baseFilter(request), { 'attributes.hitl_invited': member, 'attributes.hitl_waiting': { $ne: true } }),
129
+ { $pull: { 'attributes.hitl_invited': member } },
130
+ { new: true }
131
+ ).exec();
132
+ if (!released) {
133
+ return;
134
+ }
135
+ if ((((released.attributes || {}).hitl_invited) || []).length > 0) {
136
+ return;
137
+ }
138
+ // no human left: release the flow's hold and end the tracking
139
+ const result = await Request.updateOne(
140
+ Object.assign({}, baseFilter(released), { 'attributes.hitl_invited': { $size: 0 } }),
141
+ { $unset: { 'attributes.hitl_invited': '', 'attributes.hitl_fallback': '' } }
142
+ ).exec();
143
+ const changed = (result && (result.nModified || result.modifiedCount || result.n || result.matchedCount)) || 0;
144
+ if (changed > 0) {
145
+ requestEvent.emit('request.update', await loadPopulated(released.request_id, released.id_project));
146
+ }
147
+ return;
148
+ }
149
+ const attrs = updated.attributes || {};
150
+ if ((attrs.hitl_invited || []).length > 0) {
151
+ return;
152
+ }
153
+ if (!pick) {
154
+ return;
155
+ }
156
+
157
+ const id_project = updated.id_project;
158
+ const excluded = (attrs.hitl_left || []).map(toId).concat((updated.participants || []).map(toId));
159
+
160
+ let replacement = null;
161
+ if (attrs.hitl_department_id) {
162
+ try {
163
+ const department = await participantInviteService.resolveDepartment(id_project, attrs.hitl_department_id);
164
+ replacement = await participantInviteService.pickReplacement(id_project, department, updated, excluded);
165
+ } catch (err) {
166
+ winston.verbose('(HitlParticipantListener) no replacement for request ' + updated.request_id + ': ' + err.message);
167
+ replacement = null;
168
+ }
169
+ }
170
+
171
+ if (replacement) {
172
+ // claim atomically: a concurrent leave that also reached this point loses
173
+ const claimed = await Request.findOneAndUpdate(
174
+ Object.assign({}, baseFilter(updated), { 'attributes.hitl_invited': { $size: 0 } }),
175
+ { $addToSet: { 'attributes.hitl_invited': replacement } },
176
+ { new: true }
177
+ ).exec();
178
+ if (!claimed) {
179
+ return;
180
+ }
181
+ let added = true;
182
+ try {
183
+ await requestService.addParticipantByRequestId(updated.request_id, id_project, replacement);
184
+ } catch (err) {
185
+ winston.error('(HitlParticipantListener) could not add replacement ' + replacement + ' to request ' + updated.request_id, err);
186
+ // treat it as unavailable from now on and continue as "nobody found"
187
+ await Request.updateOne(
188
+ { request_id: updated.request_id, id_project: id_project },
189
+ { $pull: { 'attributes.hitl_invited': replacement }, $addToSet: { 'attributes.hitl_left': replacement } }
190
+ ).exec();
191
+ added = false;
192
+ }
193
+ if (added) {
194
+ const stillWanted = await Request.exists({ request_id: updated.request_id, id_project: id_project, 'attributes.hitl_invited': replacement });
195
+ if (!stillWanted) {
196
+ // the flow stopped waiting while the replacement was being added: undo it
197
+ winston.verbose('(HitlParticipantListener) replacement ' + replacement + ' no longer wanted for request ' + updated.request_id + ', removing it');
198
+ // removing through removeParticipantByRequestId also marks the replacement in abandoned_by_project_users
199
+ // (spec-prescribed, narrow race)
200
+ await requestService.removeParticipantByRequestId(updated.request_id, id_project, replacement, {});
201
+ return;
202
+ }
203
+ const populated = await loadPopulated(updated.request_id, id_project);
204
+ participantInviteService.notifyInvited(id_project, populated, [replacement]);
205
+ return;
206
+ }
207
+ }
208
+
209
+ const cleared = await this.clearWaiting(updated, { 'attributes.hitl_invited': { $size: 0 } });
210
+ if (!cleared) {
211
+ return;
212
+ }
213
+ if (attrs.hitl_fallback && attrs.hitl_fallback.intent) {
214
+ await this.sendFallback(updated, attrs.hitl_fallback.intent);
215
+ }
216
+ }
217
+
218
+ /** Clears the HITL state and reopens a pending conversation. Returns false when there was nothing to clear. */
219
+ async clearWaiting(request, extraFilter) {
220
+ const filter = Object.assign({ $and: [HITL_EXISTS] }, baseFilter(request), extraFilter);
221
+ const unsetHitl = { 'attributes.hitl_waiting': '', 'attributes.hitl_invited': '', 'attributes.hitl_fallback': '' };
222
+ // one write per attempt: reopen and clear together when pending, otherwise only clear
223
+ const reopened = await Request.findOneAndUpdate(
224
+ Object.assign({}, filter, { workingStatus: 'pending' }),
225
+ { $unset: unsetHitl, $set: { workingStatus: 'open' } },
226
+ { new: false }
227
+ ).exec();
228
+ if (reopened) {
229
+ const populated = await loadPopulated(request.request_id, request.id_project);
230
+ requestEvent.emit('request.workingStatus.update', { request: populated });
231
+ requestEvent.emit('request.update', populated);
232
+ return true;
233
+ }
234
+ const cleared = await Request.findOneAndUpdate(filter, { $unset: unsetHitl }, { new: false }).exec();
235
+ if (!cleared) {
236
+ return false;
237
+ }
238
+ requestEvent.emit('request.update', await loadPopulated(request.request_id, request.id_project));
239
+ return true;
240
+ }
241
+
242
+ async sendFallback(request, intent) {
243
+ const action = intent + '{"flowError":"' + FALLBACK_FLOW_ERROR + '"}';
244
+ try {
245
+ await messageService.send(
246
+ 'system',
247
+ 'System',
248
+ request.request_id,
249
+ FALLBACK_TEXT,
250
+ request.id_project,
251
+ 'system',
252
+ { subtype: 'info', hitl_fallback: true, updateconversation: false, action: action }
253
+ );
254
+ } catch (err) {
255
+ winston.error('(HitlParticipantListener) error sending the HITL fallback message', err);
256
+ }
257
+ }
258
+ }
259
+
260
+ module.exports = new HitlParticipantListener();