@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.
- package/.circleci/config.yml +1 -1
- package/CHANGELOG.md +47 -0
- package/Dockerfile +3 -2
- package/Dockerfile-en +3 -2
- package/Dockerfile-jobs +3 -2
- package/Dockerfile-profiler +3 -2
- package/README.md +15 -0
- package/app.js +12 -1
- package/bin/quota-reset +68 -0
- package/bin/quota-restore +86 -0
- package/channels/chat21/chat21Handler.js +50 -34
- package/channels/chat21/chat21JoinTracker.js +43 -0
- package/config/database.js +5 -5
- package/config/native_mcp/native_mcp_servers.json +1 -0
- package/controllers/chatbot.controller.js +80 -27
- package/controllers/intents.controller.js +42 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-chat21-ionic.md +468 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-design-studio.md +1349 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-server.md +1694 -0
- package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-tybot.md +789 -0
- package/docs/superpowers/plans/2026-09-29-hitl-known-limits-server.md +83 -0
- package/docs/superpowers/plans/2026-09-29-hitl-known-limits-tybot.md +76 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-chat21-ionic.md +97 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-design-studio.md +72 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-server.md +223 -0
- package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-tybot.md +121 -0
- package/docs/superpowers/plans/2026-09-29-hitl-pre-branch-ports.md +65 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-design-studio.md +88 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-server.md +103 -0
- package/docs/superpowers/plans/2026-09-30-flow-start-points-tybot.md +43 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-design-studio.md +80 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-runtime.md +39 -0
- package/docs/superpowers/plans/2026-10-01-scheduled-start-point-server.md +106 -0
- package/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md +715 -0
- package/docs/superpowers/specs/2026-09-30-flow-start-points-design.md +156 -0
- package/docs/superpowers/specs/2026-10-01-scheduled-start-point-design.md +303 -0
- package/docs/superpowers/todo/2026-09-14-webhook-chatbot-shared-code-fixes.md +191 -0
- package/event/botEvent.js +12 -1
- package/event/emailEvent.js +3 -0
- package/event/messageEvent.js +44 -0
- package/jobs.js +10 -0
- package/jobsManager.js +19 -0
- package/middleware/errorHandler.js +6 -2
- package/models/analyticResult.js +1 -1
- package/models/channelConstants.js +3 -2
- package/models/dataTable.js +16 -0
- package/models/location.js +6 -12
- package/models/quotaUsage.js +71 -0
- package/models/request.js +71 -61
- package/models/requestSnapshot.js +12 -0
- package/models/routerLogger.js +1 -1
- package/models/webhook.js +44 -0
- package/package.json +9 -7
- package/pubmodules/emailNotification/requestNotification.js +14 -22
- package/pubmodules/events/test/eventRoute.js +7 -6
- package/pubmodules/rules/conciergeBot.js +9 -4
- package/routes/auth.js +1 -12
- package/routes/campaigns.js +0 -13
- package/routes/dataTable.js +23 -3
- package/routes/faq_kb.js +12 -1
- package/routes/messagesRoot.js +0 -1
- package/routes/quotes.js +38 -1
- package/routes/request.js +81 -5
- package/routes/users.js +0 -1
- package/routes/webhook.js +160 -8
- package/routes/webhooks.js +276 -6
- package/routes/widgetLoader.js +6 -3
- package/services/QuoteManager.js +77 -16
- package/services/aiService.js +1 -1
- package/services/chatbotService.js +55 -1
- package/services/dataTableService.js +436 -42
- package/services/departmentService.js +60 -43
- package/services/dolphinSchedulerClient.js +116 -0
- package/services/emailService.js +6 -1
- package/services/hitlParticipantListener.js +260 -0
- package/services/participantInviteService.js +302 -0
- package/services/participantRemoveService.js +79 -0
- package/services/quotaSnapshotService.js +1167 -0
- package/services/requestService.js +102 -144
- package/services/scheduledStart/dolphinBackend.js +246 -0
- package/services/scheduledStart/schedulerBackend.js +101 -0
- package/services/scheduledStart/schedulerServiceClient.js +41 -0
- package/services/scheduledStartPublishListener.js +186 -0
- package/services/scheduledStartRules.js +173 -0
- package/services/scheduledStartService.js +130 -0
- package/services/startPointCleanupListener.js +30 -0
- package/services/startPointService.js +158 -0
- package/services/testWsService.js +1 -4
- package/services/updateRequestSnapshotQueued.js +8 -9
- package/services/webhookConversationService.js +185 -0
- package/services/webhookSettingsValidator.js +61 -0
- package/template/email/checkpointReachedEmail.html +5 -0
- package/test/agentPromptsRoute.test.js +3 -2
- package/test/agentTokensQuota.js +237 -0
- package/test/botEventHitlFallback.js +19 -0
- package/test/chat21HandlerJoinWiring.js +59 -0
- package/test/dataTablesRoute.test.js +649 -2
- package/test/dateUtils.test.js +0 -3
- package/test/departmentService.js +8 -12
- package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
- package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
- package/test/faqRoute.js +6 -1
- package/test/faqkbRoute.js +14 -10
- package/test/faqkbV3Route.js +1 -1
- package/test/filepRoute.js +4 -50
- package/test/hitlParticipantListener.js +446 -0
- package/test/hitlQueueWiring.js +191 -0
- package/test/hitlWaitingReset.js +124 -0
- package/test/inviteParticipantsRoute.js +492 -0
- package/test/kbRoute.js +1 -1
- package/test/messageRoute.js +33 -21
- package/test/nativemcp.test.js +7 -1
- package/test/openaiRoute.js +2 -8
- package/test/publishFailLoud.test.js +414 -0
- package/test/quotaSnapshots.js +1044 -0
- package/test/removeParticipantsRoute.js +186 -0
- package/test/requestService.js +24 -16
- package/test/requestServiceRemoveParticipant.js +144 -0
- package/test/requestVisibility.test.js +186 -0
- package/test/scheduledStartFacade.js +184 -0
- package/test/scheduledStartHandover.js +95 -0
- package/test/scheduledStartPublish.js +320 -0
- package/test/scheduledStartRoute.js +163 -0
- package/test/scheduledStartRules.js +177 -0
- package/test/scheduledStartRun.js +299 -0
- package/test/scheduledStartSchedulerBackend.js +116 -0
- package/test/scheduledStartService.js +290 -0
- package/test/startPointsCleanup.js +69 -0
- package/test/startPointsRegression.js +169 -0
- package/test/startPointsRoute.js +158 -0
- package/test/webhookConversationRoute.js +347 -0
- package/test/webhookConversationService.js +261 -0
- package/test/webhookConversationTestUtils.js +55 -0
- package/utils/aiUtils.js +95 -1
- package/utils/dataTableUtils.js +99 -9
- package/utils/intentFieldNames.js +192 -0
- package/utils/quotaCliUtil.js +43 -0
- package/utils/requestVisibility.js +355 -0
- package/websocket/webSocketServer.js +36 -31
- /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
|
@@ -10,6 +10,7 @@ const leadEvent = require('../event/leadEvent');
|
|
|
10
10
|
var winston = require('../config/winston');
|
|
11
11
|
var RequestConstants = require("../models/requestConstants");
|
|
12
12
|
var requestUtil = require("../utils/requestUtil");
|
|
13
|
+
var requestVisibility = require("../utils/requestVisibility");
|
|
13
14
|
var assignmentContextUtil = require("../utils/assignmentContextUtil");
|
|
14
15
|
var cacheUtil = require("../utils/cacheUtil");
|
|
15
16
|
var arrayUtil = require("../utils/arrayUtil");
|
|
@@ -246,10 +247,13 @@ class RequestService {
|
|
|
246
247
|
|
|
247
248
|
|
|
248
249
|
request.snapshot.department = result.department;
|
|
249
|
-
request.snapshot.
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
250
|
+
return requestVisibility.applySnapshotVisibility(request.snapshot, result).then(function () {
|
|
251
|
+
request.snapshot.availableAgentsCount = that.getAvailableAgentsCount(result.agents || []);
|
|
252
|
+
if (typeof request.markModified === 'function') {
|
|
253
|
+
request.markModified('snapshot');
|
|
254
|
+
}
|
|
255
|
+
return resolve(request);
|
|
256
|
+
});
|
|
253
257
|
|
|
254
258
|
|
|
255
259
|
}).catch(function (err) {
|
|
@@ -403,6 +407,16 @@ class RequestService {
|
|
|
403
407
|
// Save and populate
|
|
404
408
|
const savedRequest = await routedRequest.save();
|
|
405
409
|
|
|
410
|
+
// Drop the legacy embedded project_user array. applySnapshotVisibility
|
|
411
|
+
// clears it in memory; $unset removes it when it was not selected on load.
|
|
412
|
+
const visibilityUnset = { 'snapshot.agents': 1 };
|
|
413
|
+
if (savedRequest.snapshot && savedRequest.snapshot.visibility === requestVisibility.VISIBILITY_AGENTS) {
|
|
414
|
+
visibilityUnset['snapshot.visibilitySince'] = 1;
|
|
415
|
+
} else if (savedRequest.snapshot && savedRequest.snapshot.visibility === requestVisibility.VISIBILITY_PROJECT) {
|
|
416
|
+
visibilityUnset['snapshot.agentIds'] = 1;
|
|
417
|
+
}
|
|
418
|
+
await Request.updateOne({ _id: savedRequest._id }, { $unset: visibilityUnset });
|
|
419
|
+
|
|
406
420
|
const requestComplete = await savedRequest
|
|
407
421
|
.populate("lead")
|
|
408
422
|
.populate("department")
|
|
@@ -662,6 +676,7 @@ class RequestService {
|
|
|
662
676
|
}
|
|
663
677
|
|
|
664
678
|
snapshot.availableAgentsCount = this.getAvailableAgentsCount(agents);
|
|
679
|
+
await requestVisibility.applySnapshotVisibility(snapshot, result);
|
|
665
680
|
|
|
666
681
|
if (request.requester) {
|
|
667
682
|
snapshot.requester = request.requester;
|
|
@@ -726,7 +741,6 @@ class RequestService {
|
|
|
726
741
|
}
|
|
727
742
|
await q.exec();
|
|
728
743
|
|
|
729
|
-
snapshot.agents = agents;
|
|
730
744
|
requestEvent.emit("request.create.simple", savedRequest, snapshot);
|
|
731
745
|
|
|
732
746
|
if (isStandardConversation) {
|
|
@@ -1899,7 +1913,15 @@ class RequestService {
|
|
|
1899
1913
|
winston.debug("id_project", id_project);
|
|
1900
1914
|
winston.debug("member", member);
|
|
1901
1915
|
|
|
1902
|
-
|
|
1916
|
+
const populateRequest = (req) => req
|
|
1917
|
+
.populate('lead')
|
|
1918
|
+
.populate('department')
|
|
1919
|
+
.populate('participatingBots')
|
|
1920
|
+
.populate('participatingAgents')
|
|
1921
|
+
.populate({ path: 'requester', populate: { path: 'id_user' } })
|
|
1922
|
+
.execPopulate();
|
|
1923
|
+
|
|
1924
|
+
return new Promise(async function (resolve, reject) {
|
|
1903
1925
|
|
|
1904
1926
|
const traceEvent = 'request.participants.leave';
|
|
1905
1927
|
const traceRef = { request_id: request_id, id_project: id_project };
|
|
@@ -1912,158 +1934,94 @@ class RequestService {
|
|
|
1912
1934
|
return reject(err);
|
|
1913
1935
|
}
|
|
1914
1936
|
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
//
|
|
1919
|
-
//
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1937
|
+
// stage reported by traceServiceError if one of the atomic steps below fails
|
|
1938
|
+
var traceStage = 'update';
|
|
1939
|
+
try {
|
|
1940
|
+
// Atomic updates only: a load-modify-save here loses concurrent removals and
|
|
1941
|
+
// overwrites attributes.hitl_* written meanwhile (see spec 12.2.2).
|
|
1942
|
+
var update;
|
|
1943
|
+
if (member.startsWith("bot_")) {
|
|
1944
|
+
// botprefix
|
|
1945
|
+
update = {
|
|
1946
|
+
$pull: { participants: member, participantsBots: member.replace("bot_", "") },
|
|
1947
|
+
$set: { hasBot: false }
|
|
1948
|
+
};
|
|
1949
|
+
} else {
|
|
1950
|
+
update = { $pull: { participants: member, participantsAgents: member } };
|
|
1951
|
+
try {
|
|
1952
|
+
//request.attributes.abandoned_by_project_users start TODO move to routing-queue
|
|
1953
|
+
var pu = await Project_user.findOne({ id_user: member, id_project: id_project }).exec();
|
|
1954
|
+
winston.debug("pu", pu);
|
|
1955
|
+
update.$set = { ['attributes.abandoned_by_project_users.' + pu._id]: new Date().getTime() };
|
|
1956
|
+
} catch (e) {
|
|
1957
|
+
winston.error("Error getting removeParticipantByRequestId pu", e);
|
|
1926
1958
|
}
|
|
1959
|
+
}
|
|
1927
1960
|
|
|
1928
|
-
|
|
1961
|
+
// only the caller that actually removes the member gets a document back
|
|
1962
|
+
traceStage = 'update';
|
|
1963
|
+
var updated = await Request.findOneAndUpdate(
|
|
1964
|
+
{ request_id: request_id, id_project: id_project, participants: member },
|
|
1965
|
+
update,
|
|
1966
|
+
{ new: true }
|
|
1967
|
+
).exec();
|
|
1968
|
+
|
|
1969
|
+
if (!updated) {
|
|
1970
|
+
traceStage = 'find';
|
|
1971
|
+
var existing = await Request.findOne({ request_id: request_id, id_project: id_project }).exec();
|
|
1972
|
+
if (!existing) {
|
|
1929
1973
|
winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
|
|
1930
1974
|
chat21SyncTracer.trace(traceEvent, traceRef, 'service.not_found', 'error');
|
|
1931
1975
|
return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
|
|
1932
1976
|
}
|
|
1977
|
+
winston.verbose('Request member ' + member + ' already not found for request_id ' + request_id + ' and id_project ' + id_project);
|
|
1978
|
+
chat21SyncTracer.trace(traceEvent, traceRef, 'service.unchanged', 'skipped', { member: member, ...chat21SyncTracer.describeRequest(existing) });
|
|
1979
|
+
traceStage = 'populate';
|
|
1980
|
+
return resolve(await populateRequest(existing));
|
|
1981
|
+
}
|
|
1933
1982
|
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
if (
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
// botprefix
|
|
1942
|
-
if (member.startsWith("bot_")) {
|
|
1943
|
-
request.hasBot = false;
|
|
1944
|
-
// botprefix
|
|
1945
|
-
var assigned_operator_idStringBot = member.replace("bot_", "");
|
|
1946
|
-
winston.debug("assigned_operator_idStringBot:" + assigned_operator_idStringBot);
|
|
1947
|
-
|
|
1948
|
-
var indexParticipantsBots = request.participantsBots.indexOf(assigned_operator_idStringBot);
|
|
1949
|
-
request.participantsBots.splice(indexParticipantsBots, 1);
|
|
1950
|
-
} else {
|
|
1951
|
-
var indexParticipantsAgents = request.participantsAgents.indexOf(member);
|
|
1952
|
-
request.participantsAgents.splice(indexParticipantsAgents, 1);
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
try {
|
|
1957
|
-
|
|
1958
|
-
//request.attributes.abandoned_by_project_users start TODO move to routing-queue
|
|
1959
|
-
if (!request.attributes) {
|
|
1960
|
-
winston.debug("removeParticipantByRequestId request.attributes is empty. creating it");
|
|
1961
|
-
request.attributes = {};
|
|
1962
|
-
}
|
|
1963
|
-
if (!request.attributes.abandoned_by_project_users) {
|
|
1964
|
-
winston.debug("removeParticipantByRequestId request.attributes.abandoned_by_project_users is empty. creating it");
|
|
1965
|
-
request.attributes.abandoned_by_project_users = {}
|
|
1966
|
-
}
|
|
1967
|
-
|
|
1968
|
-
/*
|
|
1969
|
-
winston.info("request.participatingAgents",request.participatingAgents);
|
|
1970
|
-
var pu = request.participatingAgents.find(projectUser => {
|
|
1971
|
-
console.log(projectUser);
|
|
1972
|
-
projectUser.id_user.toString() === member
|
|
1973
|
-
});
|
|
1974
|
-
winston.verbose("pu",pu);
|
|
1975
|
-
*/
|
|
1976
|
-
|
|
1977
|
-
var pu = await Project_user.findOne({ id_user: member, id_project: id_project }).exec();
|
|
1978
|
-
winston.debug("pu", pu);
|
|
1979
|
-
|
|
1980
|
-
request.attributes.abandoned_by_project_users[pu._id] = new Date().getTime();
|
|
1981
|
-
winston.debug("removeParticipantByRequestId request.attributes.abandoned_by_project_users", request.attributes.abandoned_by_project_users);
|
|
1982
|
-
//request.attributes.abandoned_by_project_users end
|
|
1983
|
-
|
|
1984
|
-
} catch (e) {
|
|
1985
|
-
winston.error("Error getting removeParticipantByRequestId pu", e);
|
|
1986
|
-
}
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
}
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
if (request.status != RequestConstants.CLOSED) {//don't change the status to 100 or 200 for closed request to resolve this bug. if the agent leave the group and after close the request the status became 100, but if the request is closed the state (1000) must not be changed
|
|
1993
|
-
// qui1000 ????
|
|
1994
|
-
if (request.participants.length > 0) {
|
|
1995
|
-
request.status = RequestConstants.ASSIGNED;
|
|
1996
|
-
// assignet_at?
|
|
1997
|
-
} else {
|
|
1998
|
-
request.status = RequestConstants.UNASSIGNED;
|
|
1999
|
-
}
|
|
2000
|
-
}
|
|
2001
|
-
|
|
2002
|
-
request.markModified('attributes');
|
|
2003
|
-
// winston.debug(" request", request);
|
|
2004
|
-
//cacheinvalidation
|
|
2005
|
-
return request.save(function (err, savedRequest) {
|
|
2006
|
-
if (err) {
|
|
2007
|
-
//winston.error("Error saving removed participant ", err);
|
|
2008
|
-
traceServiceError(traceEvent, traceRef, 'save', err);
|
|
2009
|
-
return reject(err);
|
|
2010
|
-
}
|
|
2011
|
-
|
|
2012
|
-
return savedRequest
|
|
2013
|
-
.populate('lead')
|
|
2014
|
-
.populate('department')
|
|
2015
|
-
.populate('participatingBots')
|
|
2016
|
-
.populate('participatingAgents')
|
|
2017
|
-
.populate({ path: 'requester', populate: { path: 'id_user' } })
|
|
2018
|
-
.execPopulate(function (err, requestComplete) {
|
|
2019
|
-
|
|
2020
|
-
if (err) {
|
|
2021
|
-
winston.error("Error getting removed participant ", err);
|
|
2022
|
-
traceServiceError(traceEvent, traceRef, 'populate', err);
|
|
2023
|
-
return reject(err);
|
|
2024
|
-
}
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
requestEvent.emit('request.update', requestComplete);
|
|
2028
|
-
requestEvent.emit("request.update.comment", { comment: "PARTICIPANT_REMOVE", request: requestComplete });//Deprecated
|
|
2029
|
-
requestEvent.emit("request.updated", { comment: "PARTICIPANT_REMOVE", request: requestComplete, patch: { member: member } });
|
|
2030
|
-
traceEventEmitted(traceEvent, traceRef, requestComplete, { member: member });
|
|
2031
|
-
requestEvent.emit('request.participants.leave', {
|
|
2032
|
-
member: member,
|
|
2033
|
-
request: requestComplete,
|
|
2034
|
-
actor: options && options.actor,
|
|
2035
|
-
source: options && options.source,
|
|
2036
|
-
trackLeave: options && options.trackLeave
|
|
2037
|
-
});
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
return resolve(requestComplete);
|
|
2041
|
-
|
|
2042
|
-
});
|
|
2043
|
-
});
|
|
2044
|
-
|
|
2045
|
-
|
|
1983
|
+
if (updated.status != RequestConstants.CLOSED) {//don't change the status to 100 or 200 for closed request: if the agent leaves and the request is closed the state (1000) must not be changed
|
|
1984
|
+
traceStage = 'status_update';
|
|
1985
|
+
var statusFilter = { request_id: request_id, id_project: id_project, status: { $ne: RequestConstants.CLOSED } };
|
|
1986
|
+
if (updated.participants.length > 0) {
|
|
1987
|
+
statusFilter['participants.0'] = { $exists: true };
|
|
1988
|
+
await Request.updateOne(statusFilter, { $set: { status: RequestConstants.ASSIGNED } }).exec();
|
|
2046
1989
|
} else {
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
return request
|
|
2051
|
-
.populate('lead')
|
|
2052
|
-
.populate('department')
|
|
2053
|
-
.populate('participatingBots')
|
|
2054
|
-
.populate('participatingAgents')
|
|
2055
|
-
.populate({ path: 'requester', populate: { path: 'id_user' } })
|
|
2056
|
-
.execPopulate(function (err, requestComplete) {
|
|
2057
|
-
return resolve(requestComplete);
|
|
2058
|
-
});
|
|
1990
|
+
statusFilter.participants = { $size: 0 };
|
|
1991
|
+
await Request.updateOne(statusFilter, { $set: { status: RequestConstants.UNASSIGNED } }).exec();
|
|
2059
1992
|
}
|
|
1993
|
+
}
|
|
2060
1994
|
|
|
1995
|
+
traceStage = 'reload';
|
|
1996
|
+
var reloaded = await Request.findOne({ request_id: request_id, id_project: id_project }).exec();
|
|
1997
|
+
if (!reloaded) {
|
|
1998
|
+
chat21SyncTracer.trace(traceEvent, traceRef, 'service.not_found', 'error');
|
|
1999
|
+
return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
|
|
2000
|
+
}
|
|
2001
|
+
traceStage = 'populate';
|
|
2002
|
+
var requestComplete = await populateRequest(reloaded);
|
|
2003
|
+
|
|
2004
|
+
requestEvent.emit('request.update', requestComplete);
|
|
2005
|
+
requestEvent.emit("request.update.comment", { comment: "PARTICIPANT_REMOVE", request: requestComplete });//Deprecated
|
|
2006
|
+
requestEvent.emit("request.updated", { comment: "PARTICIPANT_REMOVE", request: requestComplete, patch: { member: member } });
|
|
2007
|
+
traceEventEmitted(traceEvent, traceRef, requestComplete, { member: member });
|
|
2008
|
+
requestEvent.emit('request.participants.leave', {
|
|
2009
|
+
member: member,
|
|
2010
|
+
request: requestComplete,
|
|
2011
|
+
actor: options && options.actor,
|
|
2012
|
+
source: options && options.source,
|
|
2013
|
+
trackLeave: options && options.trackLeave
|
|
2061
2014
|
});
|
|
2015
|
+
|
|
2016
|
+
return resolve(requestComplete);
|
|
2017
|
+
} catch (e) {
|
|
2018
|
+
winston.error("Error removing participant ", e);
|
|
2019
|
+
traceServiceError(traceEvent, traceRef, traceStage, e);
|
|
2020
|
+
return reject(e);
|
|
2021
|
+
}
|
|
2062
2022
|
});
|
|
2063
2023
|
}
|
|
2064
2024
|
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
2025
|
updateAttributesByRequestId(request_id, id_project, attributes) {
|
|
2068
2026
|
var data = attributes;
|
|
2069
2027
|
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
const winston = require('../../config/winston');
|
|
2
|
+
const client = require('../dolphinSchedulerClient');
|
|
3
|
+
const { toQuartzCron } = require('../scheduledStartRules');
|
|
4
|
+
|
|
5
|
+
const workflowName = (chatbotId) => 'auto-start-' + chatbotId;
|
|
6
|
+
|
|
7
|
+
function apiUrl() {
|
|
8
|
+
let url = process.env.API_URL;
|
|
9
|
+
if (!url) {
|
|
10
|
+
try { url = require('../../config/global').apiUrl; } catch (e) { /* ignore */ }
|
|
11
|
+
}
|
|
12
|
+
return String(url || '').replace(/\/+$/, '');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function scheduleSpec(schedule) {
|
|
16
|
+
return { crontab: toQuartzCron(schedule), timezoneId: schedule.timezone };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Workflow-definition query params, HTTP task shape copied from the jobworker getWorkflowObject. */
|
|
20
|
+
function buildDefinition(chatbotId, idProject) {
|
|
21
|
+
const taskCode = Date.now() * 1000 + Math.floor(Math.random() * 1000);
|
|
22
|
+
return {
|
|
23
|
+
globalParams: JSON.stringify([
|
|
24
|
+
{ prop: 'id_project', value: String(idProject || ''), direct: 'OUT', type: 'VARCHAR' },
|
|
25
|
+
{ prop: 'chatbot_id', value: chatbotId, direct: 'OUT', type: 'VARCHAR' },
|
|
26
|
+
{ prop: 'token', value: process.env.SCHEDULED_START_TOKEN, direct: 'OUT', type: 'VARCHAR' },
|
|
27
|
+
]),
|
|
28
|
+
locations: JSON.stringify([{ taskCode, x: 90, y: 140 }]),
|
|
29
|
+
timeout: 0,
|
|
30
|
+
taskRelationJson: JSON.stringify([{
|
|
31
|
+
name: '', preTaskCode: 0, preTaskVersion: 0, postTaskCode: taskCode, postTaskVersion: 0,
|
|
32
|
+
conditionType: 'NONE', conditionParams: {},
|
|
33
|
+
}]),
|
|
34
|
+
taskDefinitionJson: JSON.stringify([{
|
|
35
|
+
code: taskCode, delayTime: '0', description: '', environmentCode: -1,
|
|
36
|
+
failRetryInterval: '1', failRetryTimes: '0', flag: 'YES', isCache: 'NO', name: 'HttpTask',
|
|
37
|
+
taskParams: {
|
|
38
|
+
localParams: [], resourceList: [], httpMethod: 'POST', httpBody: '',
|
|
39
|
+
httpCheckCondition: 'STATUS_CODE_DEFAULT',
|
|
40
|
+
httpParams: [
|
|
41
|
+
{ prop: 'id_project', httpParametersType: 'BODY', value: '${id_project}' },
|
|
42
|
+
{ prop: 'chatbot_id', httpParametersType: 'BODY', value: '${chatbot_id}' },
|
|
43
|
+
{ prop: 'x-auth-token', httpParametersType: 'HEADERS', value: '${token}' },
|
|
44
|
+
],
|
|
45
|
+
url: apiUrl() + '/webhook/scheduled-start',
|
|
46
|
+
condition: '', connectTimeout: 60000, socketTimeout: 60000,
|
|
47
|
+
},
|
|
48
|
+
taskPriority: 'MEDIUM', taskType: 'HTTP', timeout: 0, timeoutFlag: 'CLOSE',
|
|
49
|
+
timeoutNotifyStrategy: '', workerGroup: 'default', cpuQuota: -1, memoryMax: -1,
|
|
50
|
+
taskExecuteType: 'BATCH',
|
|
51
|
+
}]),
|
|
52
|
+
executionType: 'PARALLEL',
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const quiet = (label) => (e) => winston.warn('(scheduledStart) ' + label + ' failed: ' + e.message);
|
|
57
|
+
|
|
58
|
+
/** Takes an existing schedule through offline (best effort) -> update -> online. */
|
|
59
|
+
async function refreshSchedule(id, code, spec) {
|
|
60
|
+
await client.scheduleOffline(id).catch(() => {});
|
|
61
|
+
await client.updateSchedule(id, code, spec);
|
|
62
|
+
await client.scheduleOnline(id);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} chatbotId
|
|
67
|
+
* @param {{schedule: object}} snapshot
|
|
68
|
+
* @param {string} [idProject] project id stored as the workflow global param
|
|
69
|
+
* @returns {Promise<{workflow_code: number, schedule_id: number}>}
|
|
70
|
+
*/
|
|
71
|
+
async function activate(chatbotId, snapshot, idProject) {
|
|
72
|
+
const name = workflowName(chatbotId);
|
|
73
|
+
const spec = scheduleSpec(snapshot.schedule);
|
|
74
|
+
let created = false;
|
|
75
|
+
let code;
|
|
76
|
+
let scheduleId;
|
|
77
|
+
const dropDefinition = async () => {
|
|
78
|
+
if (!created) return;
|
|
79
|
+
await client.releaseWorkflow(code, 'OFFLINE').catch(quiet('rollback offline'));
|
|
80
|
+
await client.deleteWorkflow(code).catch(quiet('rollback delete'));
|
|
81
|
+
};
|
|
82
|
+
try {
|
|
83
|
+
const existing = await client.findWorkflowByName(name);
|
|
84
|
+
if (existing) {
|
|
85
|
+
code = existing.code;
|
|
86
|
+
} else {
|
|
87
|
+
code = (await client.createWorkflow(name, buildDefinition(chatbotId, idProject || snapshot.id_project))).code;
|
|
88
|
+
created = true;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
await client.releaseWorkflow(code, 'ONLINE');
|
|
92
|
+
} catch (err) {
|
|
93
|
+
await dropDefinition();
|
|
94
|
+
throw err;
|
|
95
|
+
}
|
|
96
|
+
// DolphinScheduler allows one schedule per definition: reuse it when present.
|
|
97
|
+
const schedules = created ? [] : await client.listSchedules(code);
|
|
98
|
+
if (schedules.length > 0) {
|
|
99
|
+
scheduleId = schedules[0].id;
|
|
100
|
+
await refreshSchedule(scheduleId, code, spec);
|
|
101
|
+
return { workflow_code: code, schedule_id: scheduleId };
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
scheduleId = (await client.createSchedule(code, spec)).id;
|
|
105
|
+
} catch (err) {
|
|
106
|
+
await dropDefinition();
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
await client.scheduleOnline(scheduleId);
|
|
111
|
+
} catch (err) {
|
|
112
|
+
await client.deleteSchedule(scheduleId).catch(quiet('rollback schedule delete'));
|
|
113
|
+
await dropDefinition();
|
|
114
|
+
throw err;
|
|
115
|
+
}
|
|
116
|
+
return { workflow_code: code, schedule_id: scheduleId };
|
|
117
|
+
} catch (err) {
|
|
118
|
+
err.status = 502;
|
|
119
|
+
throw err;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @param {object} live stored scheduled_live ({workflow_code, schedule_id, ...})
|
|
125
|
+
* @param {{schedule: object}} snapshot
|
|
126
|
+
* @param {string} [chatbotId] used when falling back to activate
|
|
127
|
+
* @param {string} [idProject]
|
|
128
|
+
* @returns {Promise<{workflow_code: number, schedule_id: number}>}
|
|
129
|
+
*/
|
|
130
|
+
async function update(live, snapshot, chatbotId, idProject) {
|
|
131
|
+
const cid = chatbotId || (live && live.chatbot_id) || snapshot.chatbot_id;
|
|
132
|
+
if (!live || !live.workflow_code || !live.schedule_id) {
|
|
133
|
+
return activate(cid, snapshot, idProject);
|
|
134
|
+
}
|
|
135
|
+
const spec = scheduleSpec(snapshot.schedule);
|
|
136
|
+
// Offline is best effort: the schedule may already be offline (deactivated before).
|
|
137
|
+
await client.scheduleOffline(live.schedule_id).catch(() => {});
|
|
138
|
+
try {
|
|
139
|
+
await client.updateSchedule(live.schedule_id, live.workflow_code, spec);
|
|
140
|
+
await client.scheduleOnline(live.schedule_id);
|
|
141
|
+
} catch (err) {
|
|
142
|
+
if (err.notFound) {
|
|
143
|
+
winston.warn('(scheduledStart) live ids not found, falling back to activate: ' + err.message);
|
|
144
|
+
return activate(cid, snapshot, idProject);
|
|
145
|
+
}
|
|
146
|
+
// Do not leave a live bot silently stopped by a failed edit.
|
|
147
|
+
await client.scheduleOnline(live.schedule_id).catch(quiet('restore online'));
|
|
148
|
+
err.status = 502;
|
|
149
|
+
throw err;
|
|
150
|
+
}
|
|
151
|
+
return { workflow_code: live.workflow_code, schedule_id: live.schedule_id };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function deactivate(live) {
|
|
155
|
+
if (!live || !live.schedule_id) return;
|
|
156
|
+
try {
|
|
157
|
+
await client.scheduleOffline(live.schedule_id);
|
|
158
|
+
} catch (err) {
|
|
159
|
+
err.status = 502;
|
|
160
|
+
throw err;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function destroy(live) {
|
|
165
|
+
if (!live) return;
|
|
166
|
+
if (live.schedule_id) {
|
|
167
|
+
await client.scheduleOffline(live.schedule_id).catch((e) => winston.debug('(scheduledStart) schedule offline: ' + e.message));
|
|
168
|
+
}
|
|
169
|
+
if (live.workflow_code) {
|
|
170
|
+
await client.releaseWorkflow(live.workflow_code, 'OFFLINE').catch((e) => winston.debug('(scheduledStart) release offline: ' + e.message));
|
|
171
|
+
try {
|
|
172
|
+
await client.deleteWorkflow(live.workflow_code);
|
|
173
|
+
} catch (err) {
|
|
174
|
+
if (!err.notFound) {
|
|
175
|
+
err.status = 502;
|
|
176
|
+
throw err;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const PREVIEW_TIMEOUT_MS = 3000;
|
|
183
|
+
|
|
184
|
+
/** Next 3 runs, or null when the scheduler is slow/unavailable (callers omit the field). */
|
|
185
|
+
async function nextRuns(live) {
|
|
186
|
+
try {
|
|
187
|
+
const runs = await client.previewSchedule(scheduleSpec(live.schedule), PREVIEW_TIMEOUT_MS);
|
|
188
|
+
return runs.slice(0, 3);
|
|
189
|
+
} catch (err) {
|
|
190
|
+
winston.debug('(scheduledStart) preview failed: ' + err.message);
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const hasIds = (live) => !!(live && (live.workflow_code || live.schedule_id));
|
|
196
|
+
|
|
197
|
+
/** Schedule ids of the workflow named auto-start-<chatbotId>; code null when it does not exist. */
|
|
198
|
+
async function lookupByName(chatbotId) {
|
|
199
|
+
const wf = await client.findWorkflowByName(workflowName(chatbotId));
|
|
200
|
+
if (!wf) return { code: null, scheduleIds: [] };
|
|
201
|
+
const schedules = await client.listSchedules(wf.code);
|
|
202
|
+
return { code: wf.code, scheduleIds: schedules.map((s) => s.id) };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Takes the schedule(s) of a chatbot offline: by the live ids when stored, otherwise by looking
|
|
207
|
+
* the workflow up by name. Never throws; resolves true on success, false on scheduler failure.
|
|
208
|
+
*/
|
|
209
|
+
async function offlineFor(chatbotId, live) {
|
|
210
|
+
try {
|
|
211
|
+
if (live && live.schedule_id) {
|
|
212
|
+
await module.exports.deactivate(live);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
const found = await lookupByName(chatbotId);
|
|
216
|
+
for (const id of found.scheduleIds) {
|
|
217
|
+
await client.scheduleOffline(id);
|
|
218
|
+
}
|
|
219
|
+
return true;
|
|
220
|
+
} catch (err) {
|
|
221
|
+
winston.warn('(scheduledStart) cannot take schedule of chatbot ' + chatbotId + ' offline: ' + err.message);
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Removes the workflow of a chatbot: live ids when stored, otherwise lookup by name. Never throws. */
|
|
227
|
+
async function destroyFor(chatbotId, live) {
|
|
228
|
+
try {
|
|
229
|
+
if (hasIds(live)) {
|
|
230
|
+
await module.exports.destroy(live);
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
const found = await lookupByName(chatbotId);
|
|
234
|
+
if (!found.code) return true;
|
|
235
|
+
for (const id of found.scheduleIds) {
|
|
236
|
+
await client.scheduleOffline(id).catch((e) => winston.debug('(scheduledStart) schedule offline: ' + e.message));
|
|
237
|
+
}
|
|
238
|
+
await module.exports.destroy({ workflow_code: found.code });
|
|
239
|
+
return true;
|
|
240
|
+
} catch (err) {
|
|
241
|
+
winston.warn('(scheduledStart) cannot destroy workflow of chatbot ' + chatbotId + ': ' + err.message);
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
module.exports = { activate, update, deactivate, destroy, nextRuns, offlineFor, destroyFor };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const winston = require('../../config/winston');
|
|
2
|
+
const moment = require('moment-timezone');
|
|
3
|
+
const client = require('./schedulerServiceClient');
|
|
4
|
+
const { toCron5 } = require('../scheduledStartRules');
|
|
5
|
+
|
|
6
|
+
const BACKEND = 'scheduler';
|
|
7
|
+
const PREVIEW_TIMEOUT_MS = 3000;
|
|
8
|
+
|
|
9
|
+
const flowId = (chatbotId) => 'auto-start-' + chatbotId;
|
|
10
|
+
|
|
11
|
+
const asBadGateway = (err) => {
|
|
12
|
+
err.status = 502;
|
|
13
|
+
throw err;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Upserts the auto-start-<chatbotId> slot. The scheduler adds the start token at run time, so it is
|
|
18
|
+
* never sent here. The returned ids are stored in scheduled_live and address the slot later.
|
|
19
|
+
*/
|
|
20
|
+
async function activate(chatbotId, snapshot, idProject) {
|
|
21
|
+
const id_project = String(idProject || snapshot.id_project);
|
|
22
|
+
const chatbot_id = String(chatbotId);
|
|
23
|
+
const result = await client.upsertCron({
|
|
24
|
+
projectId: id_project,
|
|
25
|
+
flowId: flowId(chatbot_id),
|
|
26
|
+
target: 'scheduledStart',
|
|
27
|
+
payload: { id_project: id_project, chatbot_id: chatbot_id },
|
|
28
|
+
cron: toCron5(snapshot.schedule),
|
|
29
|
+
timezone: snapshot.schedule.timezone,
|
|
30
|
+
metadata: { source: 'scheduled-start' },
|
|
31
|
+
}).catch(asBadGateway);
|
|
32
|
+
return { backend: BACKEND, job_id: String(result.job._id), id_project: id_project, chatbot_id: chatbot_id };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The upsert is idempotent and resets a paused slot to pending. */
|
|
36
|
+
async function update(live, snapshot, chatbotId, idProject) {
|
|
37
|
+
return activate(chatbotId || (live && live.chatbot_id), snapshot, idProject || (live && live.id_project));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function deactivate(live) {
|
|
41
|
+
if (!live || !live.chatbot_id) return;
|
|
42
|
+
await client.pause(live.id_project, flowId(live.chatbot_id)).catch((err) => {
|
|
43
|
+
if (!err.notFound) asBadGateway(err);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function destroy(live) {
|
|
48
|
+
if (!live || !live.chatbot_id) return;
|
|
49
|
+
await client.remove(live.id_project, flowId(live.chatbot_id)).catch(asBadGateway);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Next 3 runs as wall-clock times in the schedule timezone, or null when the scheduler is slow/unavailable. */
|
|
53
|
+
async function nextRuns(live) {
|
|
54
|
+
try {
|
|
55
|
+
const { runs } = await client.preview(
|
|
56
|
+
{ cron: toCron5(live.schedule), timezone: live.schedule.timezone, count: 3 },
|
|
57
|
+
PREVIEW_TIMEOUT_MS
|
|
58
|
+
);
|
|
59
|
+
return runs.slice(0, 3).map((iso) => moment.tz(iso, live.schedule.timezone).format('YYYY-MM-DD HH:mm:ss'));
|
|
60
|
+
} catch (err) {
|
|
61
|
+
winston.debug('(scheduledStart) preview failed: ' + err.message);
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Project of the slot: from the live ids, else from the scheduler (chatbot ids are globally unique). */
|
|
67
|
+
async function projectOf(chatbotId, live) {
|
|
68
|
+
if (live && live.id_project) return live.id_project;
|
|
69
|
+
const found = await client.get(flowId(chatbotId));
|
|
70
|
+
return found && found.job ? found.job.projectId : null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Pauses the slot of a chatbot. Never throws; resolves true on success or when there is no slot. */
|
|
74
|
+
async function offlineFor(chatbotId, live) {
|
|
75
|
+
try {
|
|
76
|
+
const projectId = await projectOf(chatbotId, live);
|
|
77
|
+
if (!projectId) return true;
|
|
78
|
+
await client.pause(projectId, flowId(chatbotId)).catch((err) => {
|
|
79
|
+
if (!err.notFound) throw err;
|
|
80
|
+
});
|
|
81
|
+
return true;
|
|
82
|
+
} catch (err) {
|
|
83
|
+
winston.warn('(scheduledStart) cannot pause the schedule of chatbot ' + chatbotId + ': ' + err.message);
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Removes the slot of a chatbot. Never throws; resolves true on success or when there is no slot. */
|
|
89
|
+
async function destroyFor(chatbotId, live) {
|
|
90
|
+
try {
|
|
91
|
+
const projectId = await projectOf(chatbotId, live);
|
|
92
|
+
if (!projectId) return true;
|
|
93
|
+
await client.remove(projectId, flowId(chatbotId));
|
|
94
|
+
return true;
|
|
95
|
+
} catch (err) {
|
|
96
|
+
winston.warn('(scheduledStart) cannot remove the schedule of chatbot ' + chatbotId + ': ' + err.message);
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
module.exports = { activate, update, deactivate, destroy, nextRuns, offlineFor, destroyFor };
|