@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
@@ -0,0 +1,446 @@
1
+ //During the test the env variable is set to test
2
+ process.env.NODE_ENV = 'test';
3
+ process.env.LOG_LEVEL = 'error';
4
+
5
+ const chai = require('chai');
6
+ const chaiHttp = require('chai-http');
7
+ const server = require('../app');
8
+ const expect = chai.expect;
9
+ chai.use(chaiHttp);
10
+
11
+ const userService = require('../services/userService');
12
+ const projectService = require('../services/projectService');
13
+ const departmentService = require('../services/departmentService');
14
+ const faqService = require('../services/faqService');
15
+ const requestService = require('../services/requestService');
16
+ const Project_user = require('../models/project_user');
17
+ const Request = require('../models/request');
18
+ const Message = require('../models/message');
19
+ const requestEvent = require('../event/requestEvent');
20
+ const hitlParticipantListener = require('../services/hitlParticipantListener');
21
+ const RoleConstants = require('../models/roleConstants');
22
+ const { uniqueEmail, PWD, waitFor } = require('./webhookConversationTestUtils');
23
+
24
+ const FALLBACK_ACTION = '#noone-id{"flowError":"(Invite human) The invited human left and no replacement is available"}';
25
+
26
+ async function addAgent(project, owner) {
27
+ const email = uniqueEmail('test-hitl-listener-agent');
28
+ const agent = await userService.signup(email, PWD, 'Agent', 'Smith');
29
+ await new Project_user({
30
+ id_project: project._id.toString(),
31
+ id_user: agent._id.toString(),
32
+ role: RoleConstants.AGENT,
33
+ roleType: RoleConstants.TYPE_AGENTS,
34
+ user_available: true,
35
+ createdBy: owner._id.toString(),
36
+ updatedBy: owner._id.toString()
37
+ }).save();
38
+ return { agent, email, id: agent._id.toString() };
39
+ }
40
+
41
+ // The owner is unavailable so routing can only ever pick the agents created by the test.
42
+ // Agents are created before the request so departmentService.getOperators's project_user cache sees them.
43
+ async function setup({ agents = 2, pooled = true } = {}) {
44
+ const ownerEmail = uniqueEmail('test-hitl-listener-owner');
45
+ const owner = await userService.signup(ownerEmail, PWD, 'Owner', 'User');
46
+ const project = await projectService.create('test-hitl-listener', owner._id);
47
+ await Project_user.findOneAndUpdate({ id_project: project._id, id_user: owner._id }, { user_available: false }).exec();
48
+ const chatbot = await faqService.create(project._id, owner._id, { name: 'hitl-bot', type: 'tilebot', language: 'en', template: 'blank' });
49
+ const department = pooled ? await departmentService.create('SRE pooled', project._id, 'pooled', owner._id) : undefined;
50
+ const team = [];
51
+ for (let i = 0; i < agents; i++) {
52
+ team.push(await addAgent(project, owner));
53
+ }
54
+ const request = await requestService.create({
55
+ request_id: 'support-group-' + project._id + '-' + Date.now() + Math.floor(Math.random() * 1000),
56
+ id_project: project._id.toString(),
57
+ first_text: '/start',
58
+ participants: ['bot_' + chatbot._id.toString()],
59
+ channel: { name: 'webhook' },
60
+ preflight: false,
61
+ createdBy: 'system'
62
+ });
63
+ return { owner, ownerEmail, project, chatbot, department, team, request, botMember: 'bot_' + chatbot._id.toString() };
64
+ }
65
+
66
+ const url = (ctx, path) => '/' + ctx.project._id + '/requests/' + ctx.request.request_id + path;
67
+ const invite = (ctx, body) => chai.request(server).post(url(ctx, '/participants/invite')).auth(ctx.ownerEmail, PWD).send(body);
68
+ const leave = (ctx, member) => chai.request(server).delete(url(ctx, '/participants/' + member)).auth(ctx.ownerEmail, PWD).send();
69
+ const load = (ctx) => Request.findOne({ request_id: ctx.request.request_id }).lean().exec();
70
+ const fallbackMessages = (ctx) => Message.find({ recipient: ctx.request.request_id, 'attributes.hitl_fallback': true }).lean().exec();
71
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
72
+
73
+ describe('HitlParticipantListener', function () {
74
+
75
+ this.timeout(30000);
76
+
77
+ it('invites another department agent when the invited human leaves', async () => {
78
+ const ctx = await setup();
79
+ const res = await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
80
+ expect(res).to.have.status(200);
81
+ const x = res.body.invited[0].id_user;
82
+ const other = ctx.team.map((t) => t.id).find((id) => id !== x);
83
+
84
+ expect(await leave(ctx, x)).to.have.status(200);
85
+
86
+ const request = await waitFor(async () => {
87
+ const r = await load(ctx);
88
+ return r.attributes.hitl_invited && r.attributes.hitl_invited[0] === other ? r : null;
89
+ });
90
+ expect(request.participants).to.include(other);
91
+ expect(request.participants).to.not.include(x);
92
+ expect(request.attributes.hitl_invited).to.deep.equal([other]);
93
+ expect(request.attributes.hitl_left).to.deep.equal([x]);
94
+ expect(request.workingStatus).to.equal('pending');
95
+ expect(await fallbackMessages(ctx)).to.have.length(0);
96
+ });
97
+
98
+ it('never re-picks a human who already left', async () => {
99
+ const ctx = await setup();
100
+ const res = await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
101
+ const x = res.body.invited[0].id_user;
102
+ const y = ctx.team.map((t) => t.id).find((id) => id !== x);
103
+
104
+ await leave(ctx, x);
105
+ await waitFor(async () => ((await load(ctx)).attributes.hitl_invited || [])[0] === y);
106
+ await leave(ctx, y);
107
+
108
+ await waitFor(async () => (await fallbackMessages(ctx)).length === 1);
109
+ const request = await load(ctx);
110
+ expect(request.participants).to.deep.equal([ctx.botMember]);
111
+ expect(request.attributes.hitl_left.sort()).to.deep.equal([x, y].sort());
112
+ });
113
+
114
+ it('sends the fallback action when nobody can replace the human', async () => {
115
+ const ctx = await setup({ agents: 1 });
116
+ const res = await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
117
+ expect(res.body.invited[0].id_user).to.equal(ctx.team[0].id);
118
+
119
+ await leave(ctx, ctx.team[0].id);
120
+
121
+ const [message] = await waitFor(async () => {
122
+ const found = await fallbackMessages(ctx);
123
+ return found.length ? found : null;
124
+ });
125
+ expect(message.sender).to.equal('system');
126
+ expect(message.text).to.equal('The invited human left and no replacement is available');
127
+ expect(message.attributes.action).to.equal(FALLBACK_ACTION);
128
+ const request = await load(ctx);
129
+ expect(request.attributes.hitl_invited).to.equal(undefined);
130
+ expect(request.attributes.hitl_fallback).to.equal(undefined);
131
+ expect(request.attributes.hitl_waiting).to.equal(undefined);
132
+ expect(request.workingStatus).to.equal('open');
133
+ });
134
+
135
+ it('does nothing when a human the flow did not invite leaves', async () => {
136
+ const ctx = await setup({ agents: 2 });
137
+ const res = await invite(ctx, { members: [ctx.team[0].email], fallback_intent: '#noone-id' });
138
+ expect(res).to.have.status(200);
139
+ await requestService.addParticipantByRequestId(ctx.request.request_id, ctx.project._id.toString(), ctx.team[1].id);
140
+
141
+ await leave(ctx, ctx.team[1].id);
142
+ await wait(1500);
143
+
144
+ const request = await load(ctx);
145
+ expect(request.participants).to.have.members([ctx.botMember, ctx.team[0].id]);
146
+ expect(request.attributes.hitl_left).to.equal(undefined);
147
+ expect(request.attributes.hitl_invited).to.deep.equal([ctx.team[0].id]);
148
+ expect(await fallbackMessages(ctx)).to.have.length(0);
149
+ });
150
+
151
+ it('does nothing while another invited human remains', async () => {
152
+ const ctx = await setup({ agents: 2 });
153
+ const res = await invite(ctx, { members: [ctx.team[0].id, ctx.team[1].id], fallback_intent: '#noone-id' });
154
+ expect(res).to.have.status(200);
155
+
156
+ await leave(ctx, ctx.team[0].id);
157
+ await waitFor(async () => ((await load(ctx)).attributes.hitl_left || []).length === 1);
158
+ await wait(1500);
159
+
160
+ const request = await load(ctx);
161
+ expect(request.participants).to.have.members([ctx.botMember, ctx.team[1].id]);
162
+ expect(request.attributes.hitl_invited).to.deep.equal([ctx.team[1].id]);
163
+ expect(await fallbackMessages(ctx)).to.have.length(0);
164
+ });
165
+
166
+ it('does not pick or fall back when an admin replaces the invited human', async () => {
167
+ const ctx = await setup({ agents: 2 });
168
+ const res = await invite(ctx, { members: [ctx.team[0].id], fallback_intent: '#noone-id' });
169
+ expect(res).to.have.status(200);
170
+ const c = ctx.team[1].id;
171
+
172
+ const put = await chai.request(server).put(url(ctx, '/participants')).auth(ctx.ownerEmail, PWD).send([ctx.botMember, c]);
173
+ expect(put).to.have.status(200);
174
+
175
+ await waitFor(async () => {
176
+ const r = await load(ctx);
177
+ return (r.attributes.hitl_left || []).includes(ctx.team[0].id) && (r.attributes.hitl_invited || []).includes(c);
178
+ });
179
+ await wait(1500);
180
+ const request = await load(ctx);
181
+ expect(request.participants).to.have.members([ctx.botMember, c]);
182
+ expect(request.attributes.hitl_invited).to.deep.equal([c]);
183
+ expect(request.attributes.hitl_waiting).to.equal(true);
184
+ expect(await fallbackMessages(ctx)).to.have.length(0);
185
+ });
186
+
187
+ it('clears the HITL state when the bot is removed', async () => {
188
+ const ctx = await setup({ agents: 1 });
189
+ await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
190
+ expect((await load(ctx)).workingStatus).to.equal('pending');
191
+
192
+ await leave(ctx, ctx.botMember);
193
+
194
+ await waitFor(async () => (await load(ctx)).workingStatus === 'open');
195
+ const request = await load(ctx);
196
+ expect(request.attributes.hitl_waiting).to.equal(undefined);
197
+ expect(request.attributes.hitl_invited).to.equal(undefined);
198
+ expect(request.attributes.hitl_fallback).to.equal(undefined);
199
+ await wait(1500);
200
+ expect(await fallbackMessages(ctx)).to.have.length(0);
201
+ });
202
+
203
+ it('acts once when two invited humans are removed back to back', async () => {
204
+ const ctx = await setup({ agents: 2, pooled: false });
205
+ const [a, b] = [ctx.team[0].id, ctx.team[1].id];
206
+ const res = await invite(ctx, { members: [a, b], fallback_intent: '#noone-id' });
207
+ expect(res).to.have.status(200);
208
+ // The generic DELETE is a read-modify-save that can lose one of two truly concurrent removals,
209
+ // so the removals are applied directly and the listener is fed both leave events at once.
210
+ await Request.updateOne({ request_id: ctx.request.request_id }, { $pull: { participants: { $in: [a, b] } } }).exec();
211
+ const request = await load(ctx);
212
+
213
+ await Promise.all([
214
+ hitlParticipantListener.onLeave({ member: a, request }),
215
+ hitlParticipantListener.onLeave({ member: b, request })
216
+ ]);
217
+
218
+ await waitFor(async () => (await fallbackMessages(ctx)).length >= 1);
219
+ await wait(1500);
220
+ expect(await fallbackMessages(ctx)).to.have.length(1);
221
+ expect((await load(ctx)).workingStatus).to.equal('open');
222
+ });
223
+
224
+ it('reassigns even when the invite had no fallback block', async () => {
225
+ const ctx = await setup();
226
+ const res = await invite(ctx, { department_id: ctx.department._id.toString() });
227
+ expect(res).to.have.status(200);
228
+ const x = res.body.invited[0].id_user;
229
+ const other = ctx.team.map((t) => t.id).find((id) => id !== x);
230
+
231
+ expect(await leave(ctx, x)).to.have.status(200);
232
+
233
+ const request = await waitFor(async () => {
234
+ const r = await load(ctx);
235
+ return r.attributes.hitl_invited && r.attributes.hitl_invited[0] === other ? r : null;
236
+ });
237
+ expect(request.participants).to.include(other);
238
+ expect(request.workingStatus).to.equal('pending');
239
+ expect(await fallbackMessages(ctx)).to.have.length(0);
240
+ });
241
+
242
+ it('clears the waiting state without a message when nobody can replace and there is no fallback', async () => {
243
+ const ctx = await setup({ agents: 1 });
244
+ const res = await invite(ctx, { department_id: ctx.department._id.toString() });
245
+ expect(res.body.invited[0].id_user).to.equal(ctx.team[0].id);
246
+
247
+ await leave(ctx, ctx.team[0].id);
248
+
249
+ const request = await waitFor(async () => {
250
+ const r = await load(ctx);
251
+ return r.workingStatus === 'open' ? r : null;
252
+ });
253
+ expect(request.attributes.hitl_waiting).to.equal(undefined);
254
+ expect(request.attributes.hitl_invited).to.equal(undefined);
255
+ await wait(1500);
256
+ expect(await fallbackMessages(ctx)).to.have.length(0);
257
+ });
258
+
259
+ it('falls back when the replacement cannot be added', async () => {
260
+ const ctx = await setup();
261
+ const res = await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
262
+ expect(res).to.have.status(200);
263
+ const x = res.body.invited[0].id_user;
264
+ const other = ctx.team.map((t) => t.id).find((id) => id !== x);
265
+
266
+ const original = requestService.addParticipantByRequestId;
267
+ let failed = false;
268
+ requestService.addParticipantByRequestId = function () {
269
+ if (!failed) {
270
+ failed = true;
271
+ return Promise.reject(new Error('cannot add'));
272
+ }
273
+ return original.apply(this, arguments);
274
+ };
275
+ try {
276
+ expect(await leave(ctx, x)).to.have.status(200);
277
+ await waitFor(async () => (await fallbackMessages(ctx)).length === 1);
278
+ await wait(1000);
279
+ } finally {
280
+ requestService.addParticipantByRequestId = original;
281
+ }
282
+
283
+ expect(failed).to.equal(true);
284
+ expect(await fallbackMessages(ctx)).to.have.length(1);
285
+ const request = await load(ctx);
286
+ expect(request.attributes.hitl_left).to.have.members([x, other]);
287
+ expect(request.attributes.hitl_invited).to.equal(undefined);
288
+ expect(request.workingStatus).to.equal('open');
289
+ });
290
+
291
+ it('clears once when an update removes a human and the bot together', async () => {
292
+ const ctx = await setup();
293
+ const res = await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
294
+ expect(res).to.have.status(200);
295
+ const x = res.body.invited[0].id_user;
296
+
297
+ await requestService.setParticipantsByRequestId(ctx.request.request_id, ctx.project._id.toString(), []);
298
+
299
+ const request = await waitFor(async () => {
300
+ const r = await load(ctx);
301
+ return r.workingStatus === 'open' ? r : null;
302
+ });
303
+ await wait(1500);
304
+ const after = await load(ctx);
305
+ expect(after.participants).to.deep.equal([]);
306
+ expect(after.attributes.hitl_waiting).to.equal(undefined);
307
+ expect(after.attributes.hitl_invited).to.equal(undefined);
308
+ expect(after.attributes.hitl_fallback).to.equal(undefined);
309
+ expect(after.attributes.hitl_left).to.equal(undefined);
310
+ expect(request.participants).to.not.include(x);
311
+ expect(await fallbackMessages(ctx)).to.have.length(0);
312
+ });
313
+
314
+ it('releases instead of replacing when the flow is not waiting', async () => {
315
+ const ctx = await setup();
316
+ const res = await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
317
+ expect(res).to.have.status(200);
318
+ const x = res.body.invited[0].id_user;
319
+ const other = ctx.team.map((t) => t.id).find((id) => id !== x);
320
+ await Request.updateOne({ request_id: ctx.request.request_id }, { $unset: { 'attributes.hitl_waiting': '' }, $set: { workingStatus: 'open' } }).exec();
321
+
322
+ expect(await leave(ctx, x)).to.have.status(200);
323
+ await wait(1500);
324
+
325
+ const request = await load(ctx);
326
+ expect(request.participants).to.not.include(other);
327
+ expect(request.attributes.hitl_invited).to.equal(undefined);
328
+ expect(request.attributes.hitl_left || []).to.not.include(x);
329
+ expect(request.attributes.hitl_fallback).to.equal(undefined);
330
+ expect(request.workingStatus).to.equal('open');
331
+ expect(await fallbackMessages(ctx)).to.have.length(0);
332
+ });
333
+
334
+ it('a human who leaves when nothing is pending can be invited again from the department', async () => {
335
+ const ctx = await setup({ agents: 1 });
336
+ const a = ctx.team[0].id;
337
+ let res = await invite(ctx, { department_id: ctx.department._id.toString() });
338
+ expect(res).to.have.status(200);
339
+ expect(res.body.invited[0].id_user).to.equal(a);
340
+ await Request.updateOne({ request_id: ctx.request.request_id }, { $unset: { 'attributes.hitl_waiting': '' }, $set: { workingStatus: 'open' } }).exec();
341
+
342
+ expect(await leave(ctx, a)).to.have.status(200);
343
+ await wait(1500);
344
+ expect((await load(ctx)).attributes.hitl_left || []).to.not.include(a);
345
+
346
+ res = await invite(ctx, { department_id: ctx.department._id.toString() });
347
+ expect(res).to.have.status(200);
348
+ expect(res.body.invited[0].id_user).to.equal(a);
349
+ });
350
+
351
+ it('keeps replacing while the flow waits', async () => {
352
+ const ctx = await setup();
353
+ const res = await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
354
+ expect(res).to.have.status(200);
355
+ const x = res.body.invited[0].id_user;
356
+ const other = ctx.team.map((t) => t.id).find((id) => id !== x);
357
+
358
+ expect(await leave(ctx, x)).to.have.status(200);
359
+
360
+ const request = await waitFor(async () => {
361
+ const r = await load(ctx);
362
+ return (r.attributes.hitl_invited || [])[0] === other ? r : null;
363
+ });
364
+ expect(request.participants).to.include(other);
365
+ });
366
+
367
+ it('emits no workingStatus event when clearing an open conversation', async () => {
368
+ const ctx = await setup({ agents: 1 });
369
+ const res = await invite(ctx, { members: [ctx.team[0].email] });
370
+ expect(res).to.have.status(200);
371
+ await Request.updateOne({ request_id: ctx.request.request_id }, { $set: { workingStatus: 'open' } }).exec();
372
+
373
+ const events = [];
374
+ const onEvent = (data) => {
375
+ if (data && data.request && data.request.request_id === ctx.request.request_id) {
376
+ events.push(data);
377
+ }
378
+ };
379
+ requestEvent.on('request.workingStatus.update', onEvent);
380
+ try {
381
+ expect(await leave(ctx, ctx.team[0].id)).to.have.status(200);
382
+ await waitFor(async () => {
383
+ const r = await load(ctx);
384
+ return r.attributes.hitl_waiting === undefined ? r : null;
385
+ });
386
+ await wait(500);
387
+ } finally {
388
+ requestEvent.removeListener('request.workingStatus.update', onEvent);
389
+ }
390
+
391
+ const request = await load(ctx);
392
+ expect(request.attributes.hitl_invited).to.equal(undefined);
393
+ expect(request.attributes.hitl_fallback).to.equal(undefined);
394
+ expect(request.workingStatus).to.equal('open');
395
+ expect(events).to.have.length(0);
396
+ });
397
+
398
+ it('removes a replacement the flow no longer wants', async () => {
399
+ const ctx = await setup();
400
+ const res = await invite(ctx, { department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
401
+ expect(res).to.have.status(200);
402
+ const x = res.body.invited[0].id_user;
403
+ const other = ctx.team.map((t) => t.id).find((id) => id !== x);
404
+
405
+ const original = requestService.addParticipantByRequestId;
406
+ let raced = false;
407
+ requestService.addParticipantByRequestId = async function (request_id, id_project, member) {
408
+ if (member === other && !raced) {
409
+ raced = true;
410
+ await Request.updateOne({ request_id: request_id }, { $unset: { 'attributes.hitl_invited': '' } }).exec();
411
+ }
412
+ return original.apply(this, arguments);
413
+ };
414
+ try {
415
+ expect(await leave(ctx, x)).to.have.status(200);
416
+ await waitFor(async () => raced);
417
+ await waitFor(async () => {
418
+ const r = await load(ctx);
419
+ return r.participants.includes(other) ? null : r;
420
+ });
421
+ } finally {
422
+ requestService.addParticipantByRequestId = original;
423
+ }
424
+
425
+ expect(raced).to.equal(true);
426
+ await wait(500);
427
+ const request = await load(ctx);
428
+ expect(request.participants).to.not.include(other);
429
+ expect(request.participants).to.not.include(x);
430
+ });
431
+
432
+ it('ignores other channels', async () => {
433
+ const ctx = await setup({ agents: 1 });
434
+ const id = ctx.team[0].id;
435
+ await requestService.addParticipantByRequestId(ctx.request.request_id, ctx.project._id.toString(), id);
436
+ await Request.updateOne({ request_id: ctx.request.request_id }, { $set: { 'channel.name': 'chat21', 'attributes.hitl_invited': [id], 'attributes.hitl_fallback': { intent: '#noone-id' } } }).exec();
437
+
438
+ await leave(ctx, id);
439
+ await wait(1500);
440
+
441
+ const request = await load(ctx);
442
+ expect(request.participants).to.not.include(id);
443
+ expect(request.attributes.hitl_left).to.equal(undefined);
444
+ expect(request.attributes.hitl_invited).to.deep.equal([id]);
445
+ });
446
+ });
@@ -0,0 +1,191 @@
1
+ //During the test the env variable is set to test
2
+ process.env.NODE_ENV = 'test';
3
+ process.env.LOG_LEVEL = 'error';
4
+
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const chai = require('chai');
8
+ const chaiHttp = require('chai-http');
9
+ const server = require('../app');
10
+ const expect = chai.expect;
11
+ chai.use(chaiHttp);
12
+
13
+ const userService = require('../services/userService');
14
+ const projectService = require('../services/projectService');
15
+ const departmentService = require('../services/departmentService');
16
+ const faqService = require('../services/faqService');
17
+ const requestService = require('../services/requestService');
18
+ const Project_user = require('../models/project_user');
19
+ const Request = require('../models/request');
20
+ const Message = require('../models/message');
21
+ const requestEvent = require('../event/requestEvent');
22
+ const hitlParticipantListener = require('../services/hitlParticipantListener');
23
+ const JobsManager = require('../jobsManager');
24
+ const RoleConstants = require('../models/roleConstants');
25
+ const { uniqueEmail, PWD, waitFor } = require('./webhookConversationTestUtils');
26
+
27
+ const QUEUE_KEYS = ['request.participants.leave.queue', 'request.participants.update.queue'];
28
+ const PLAIN_KEYS = ['request.participants.leave', 'request.participants.update'];
29
+ const ALL_KEYS = QUEUE_KEYS.concat(PLAIN_KEYS);
30
+
31
+ // the same constructions as jobs.js (worker) and app.js (server)
32
+ const workerJobsManager = () => new JobsManager(undefined);
33
+ const serverJobsManager = (jobWorkerEnabled) => new JobsManager(jobWorkerEnabled);
34
+
35
+ const snapshot = () => Object.fromEntries(ALL_KEYS.map((k) => [k, requestEvent.listeners(k).slice()]));
36
+
37
+ /** Removes the listeners added since `before` and returns how many were added per key. */
38
+ function restore(before) {
39
+ const added = {};
40
+ ALL_KEYS.forEach((k) => {
41
+ const extra = requestEvent.listeners(k).filter((l) => !before[k].includes(l));
42
+ extra.forEach((l) => requestEvent.removeListener(k, l));
43
+ added[k] = extra.length;
44
+ });
45
+ return added;
46
+ }
47
+
48
+ async function withQueueEnabled(value, fn) {
49
+ const previous = requestEvent.queueEnabled;
50
+ requestEvent.queueEnabled = value;
51
+ try {
52
+ await fn();
53
+ } finally {
54
+ requestEvent.queueEnabled = previous;
55
+ }
56
+ }
57
+
58
+ describe('HITL participant listener wiring', function () {
59
+
60
+ it('the worker listens to the .queue events in queue mode', async () => {
61
+ let before;
62
+ await withQueueEnabled(true, async () => {
63
+ before = snapshot();
64
+ workerJobsManager().listenHitlParticipants(hitlParticipantListener);
65
+ });
66
+ const added = restore(before);
67
+ QUEUE_KEYS.forEach((k) => expect(added[k], k).to.equal(1));
68
+ PLAIN_KEYS.forEach((k) => expect(added[k], k).to.equal(0));
69
+ });
70
+
71
+ it('the server skips it in queue mode with a job worker', async () => {
72
+ let before;
73
+ await withQueueEnabled(true, async () => {
74
+ before = snapshot();
75
+ serverJobsManager(true).listenHitlParticipants(hitlParticipantListener);
76
+ });
77
+ const added = restore(before);
78
+ ALL_KEYS.forEach((k) => expect(added[k], k).to.equal(0));
79
+ });
80
+
81
+ it('the server listens to the .queue events in queue mode without a job worker', async () => {
82
+ let before;
83
+ await withQueueEnabled(true, async () => {
84
+ before = snapshot();
85
+ serverJobsManager(false).listenHitlParticipants(hitlParticipantListener);
86
+ });
87
+ const added = restore(before);
88
+ QUEUE_KEYS.forEach((k) => expect(added[k], k).to.equal(1));
89
+ PLAIN_KEYS.forEach((k) => expect(added[k], k).to.equal(0));
90
+ });
91
+
92
+ it('the server listens to the plain events when the queue is disabled, even with JOB_WORKER_ENABLED', async () => {
93
+ for (const jobWorkerEnabled of [false, true]) {
94
+ let before;
95
+ await withQueueEnabled(false, async () => {
96
+ before = snapshot();
97
+ serverJobsManager(jobWorkerEnabled).listenHitlParticipants(hitlParticipantListener);
98
+ });
99
+ const added = restore(before);
100
+ PLAIN_KEYS.forEach((k) => expect(added[k], k + ' jobWorkerEnabled=' + jobWorkerEnabled).to.equal(1));
101
+ QUEUE_KEYS.forEach((k) => expect(added[k], k + ' jobWorkerEnabled=' + jobWorkerEnabled).to.equal(0));
102
+ }
103
+ });
104
+
105
+ it('both entry points start it through JobsManager, once', () => {
106
+ const read = (f) => fs.readFileSync(path.join(__dirname, '..', f), 'utf8');
107
+ const jobs = read('jobs.js');
108
+ const app = read('app.js');
109
+ expect(jobs.match(/\.listenHitlParticipants\(/g) || [], 'jobs.js').to.have.length(1);
110
+ expect(app.match(/\.listenHitlParticipants\(/g) || [], 'app.js').to.have.length(1);
111
+ // no direct listen() that would bypass jobWorkerEnabled
112
+ expect(app).to.not.match(/hitlParticipantListener\.listen\(/);
113
+ expect(jobs).to.not.match(/hitlParticipantListener\.listen\(/);
114
+ // the worker forwards the fallback message it creates to the bot: botEvent must listen to the plain
115
+ // message.create, i.e. before pubmodules/queue sets queueEnabled
116
+ const botListen = jobs.indexOf('botEvent.listen()');
117
+ const queueRequire = jobs.indexOf("require('./pubmodules/queue')");
118
+ expect(botListen, 'botEvent.listen() in jobs.js').to.be.greaterThan(-1);
119
+ expect(jobs).to.match(/botSubscriptionNotifier\.start\(\)/);
120
+ expect(botListen, 'botEvent.listen() before the queue module').to.be.lessThan(queueRequire);
121
+ });
122
+ });
123
+
124
+ describe('HitlParticipantListener on the .queue events (worker)', function () {
125
+
126
+ this.timeout(30000);
127
+
128
+ async function setup() {
129
+ const ownerEmail = uniqueEmail('test-hitl-queue-owner');
130
+ const owner = await userService.signup(ownerEmail, PWD, 'Owner', 'User');
131
+ const project = await projectService.create('test-hitl-queue', owner._id);
132
+ await Project_user.findOneAndUpdate({ id_project: project._id, id_user: owner._id }, { user_available: false }).exec();
133
+ const chatbot = await faqService.create(project._id, owner._id, { name: 'hitl-bot', type: 'tilebot', language: 'en', template: 'blank' });
134
+ const department = await departmentService.create('SRE pooled', project._id, 'pooled', owner._id);
135
+ const agent = await userService.signup(uniqueEmail('test-hitl-queue-agent'), PWD, 'Agent', 'Smith');
136
+ await new Project_user({
137
+ id_project: project._id.toString(),
138
+ id_user: agent._id.toString(),
139
+ role: RoleConstants.AGENT,
140
+ roleType: RoleConstants.TYPE_AGENTS,
141
+ user_available: true,
142
+ createdBy: owner._id.toString(),
143
+ updatedBy: owner._id.toString()
144
+ }).save();
145
+ const request = await requestService.create({
146
+ request_id: 'support-group-' + project._id + '-' + Date.now() + Math.floor(Math.random() * 1000),
147
+ id_project: project._id.toString(),
148
+ first_text: '/start',
149
+ participants: ['bot_' + chatbot._id.toString()],
150
+ channel: { name: 'webhook' },
151
+ preflight: false,
152
+ createdBy: 'system'
153
+ });
154
+ return { ownerEmail, project, department, agentId: agent._id.toString(), request };
155
+ }
156
+
157
+ it('request.participants.leave.queue triggers the fallback', async () => {
158
+ const ctx = await setup();
159
+ const res = await chai.request(server)
160
+ .post('/' + ctx.project._id + '/requests/' + ctx.request.request_id + '/participants/invite')
161
+ .auth(ctx.ownerEmail, PWD)
162
+ .send({ department_id: ctx.department._id.toString(), fallback_intent: '#noone-id' });
163
+ expect(res).to.have.status(200);
164
+ expect(res.body.invited[0].id_user).to.equal(ctx.agentId);
165
+
166
+ let before;
167
+ await withQueueEnabled(true, async () => {
168
+ before = snapshot();
169
+ workerJobsManager().listenHitlParticipants(hitlParticipantListener);
170
+ });
171
+ try {
172
+ // the payload as reconnect.js re-emits it: JSON from RabbitMQ
173
+ const request = await Request.findOne({ request_id: ctx.request.request_id }).lean().exec();
174
+ const payload = JSON.parse(JSON.stringify({ member: ctx.agentId, request: request }));
175
+ requestEvent.emit('request.participants.leave.queue', payload);
176
+
177
+ const [message] = await waitFor(async () => {
178
+ const found = await Message.find({ recipient: ctx.request.request_id, 'attributes.hitl_fallback': true }).lean().exec();
179
+ return found.length ? found : null;
180
+ });
181
+ expect(message.sender).to.equal('system');
182
+ expect(message.attributes.action).to.equal('#noone-id{"flowError":"(Invite human) The invited human left and no replacement is available"}');
183
+ const updated = await Request.findOne({ request_id: ctx.request.request_id }).lean().exec();
184
+ expect(updated.attributes.hitl_waiting).to.equal(undefined);
185
+ expect(updated.attributes.hitl_left).to.deep.equal([ctx.agentId]);
186
+ expect(updated.workingStatus).to.equal('open');
187
+ } finally {
188
+ restore(before);
189
+ }
190
+ });
191
+ });