@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
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const axios = require('axios').default;
|
|
2
|
+
|
|
3
|
+
const TIMEOUT_MS = 30000;
|
|
4
|
+
|
|
5
|
+
/** Calls tiledesk-scheduler-service; errors carry `status` (HTTP status, 502 on network failure) and `notFound`. */
|
|
6
|
+
async function call(method, path, { params, data, timeoutMs } = {}) {
|
|
7
|
+
try {
|
|
8
|
+
const response = await axios({
|
|
9
|
+
url: String(process.env.SCHEDULER_SERVICE_URL || '').replace(/\/+$/, '') + path,
|
|
10
|
+
method,
|
|
11
|
+
params,
|
|
12
|
+
data,
|
|
13
|
+
headers: { Authorization: 'Bearer ' + process.env.SCHEDULER_API_TOKEN },
|
|
14
|
+
timeout: timeoutMs || TIMEOUT_MS,
|
|
15
|
+
});
|
|
16
|
+
return response.data;
|
|
17
|
+
} catch (err) {
|
|
18
|
+
const body = err.response && err.response.data;
|
|
19
|
+
const e = new Error('Scheduler service ' + method + ' ' + path + ' failed: ' + ((body && body.error) || err.message));
|
|
20
|
+
e.status = (err.response && err.response.status) || 502;
|
|
21
|
+
e.notFound = e.status === 404;
|
|
22
|
+
throw e;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const slot = (flowId) => '/jobs/' + encodeURIComponent(flowId);
|
|
27
|
+
|
|
28
|
+
/** Missing slots are not an error for idempotent removals and lookups. */
|
|
29
|
+
const ignoreNotFound = (err) => {
|
|
30
|
+
if (err.notFound) return { notFound: true };
|
|
31
|
+
throw err;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
module.exports = {
|
|
35
|
+
upsertCron: (job) => call('POST', '/jobs/cron', { data: job }),
|
|
36
|
+
get: (flowId) => call('GET', slot(flowId)).catch(ignoreNotFound),
|
|
37
|
+
pause: (projectId, flowId) => call('POST', slot(flowId) + '/pause', { params: { projectId } }),
|
|
38
|
+
resume: (projectId, flowId) => call('POST', slot(flowId) + '/resume', { params: { projectId } }),
|
|
39
|
+
remove: (projectId, flowId) => call('DELETE', slot(flowId), { params: { projectId } }).catch(ignoreNotFound),
|
|
40
|
+
preview: (spec, timeoutMs) => call('POST', '/schedules/preview', { data: spec, timeoutMs }),
|
|
41
|
+
};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
const botEvent = require('../event/botEvent');
|
|
2
|
+
const Faq = require('../models/faq');
|
|
3
|
+
const Faq_kb = require('../models/faq_kb');
|
|
4
|
+
const { Webhook } = require('../models/webhook');
|
|
5
|
+
const winston = require('../config/winston');
|
|
6
|
+
const scheduledStartRules = require('./scheduledStartRules');
|
|
7
|
+
const scheduledStartService = require('./scheduledStartService');
|
|
8
|
+
|
|
9
|
+
const plain = (v) => (v && typeof v.toObject === 'function' ? v.toObject() : v);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Brings the live schedule of a chatbot in line with its draft scheduled start point
|
|
13
|
+
* after a publish (spec section 7). Never throws: failures are stored in scheduled_live.error.
|
|
14
|
+
* @returns {Promise<object|null>} the stored scheduled_live, or null
|
|
15
|
+
*/
|
|
16
|
+
async function sync(id_project, chatbot_id, published_bot_id) {
|
|
17
|
+
const filter = { id_project: id_project, chatbot_id: chatbot_id };
|
|
18
|
+
let live = null;
|
|
19
|
+
try {
|
|
20
|
+
if (!scheduledStartRules.isConfigured()) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const webhook = await Webhook.findOne(filter).lean().exec();
|
|
24
|
+
if (!webhook) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const draft = (webhook.start_points || []).find((sp) => sp.type === 'scheduled');
|
|
28
|
+
live = webhook.scheduled_live || null;
|
|
29
|
+
if (!draft && !live) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const clear = async () => {
|
|
34
|
+
await Webhook.updateOne(filter, { $unset: { scheduled_live: 1 } }).exec();
|
|
35
|
+
return null;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
if (!draft) {
|
|
39
|
+
await scheduledStartService.destroy(live);
|
|
40
|
+
return clear();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const box = published_bot_id && await Faq.findOne({ id_faq_kb: published_bot_id, intent_id: draft.block_id }).select('_id').lean().exec();
|
|
44
|
+
if (!box) {
|
|
45
|
+
if (live) {
|
|
46
|
+
await scheduledStartService.destroy(live);
|
|
47
|
+
}
|
|
48
|
+
return clear();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const mapping = draft.mapping || {};
|
|
52
|
+
const snapshot = {
|
|
53
|
+
enabled: draft.enabled !== false,
|
|
54
|
+
block_id: draft.block_id,
|
|
55
|
+
mapping: { source_name: mapping.source_name, payload: mapping.payload || {} },
|
|
56
|
+
schedule: plain(draft.schedule),
|
|
57
|
+
published_bot_id: String(published_bot_id),
|
|
58
|
+
published_at: new Date()
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
let stored;
|
|
62
|
+
if (!snapshot.enabled) {
|
|
63
|
+
if (!live) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
// deactivate returns the ids to keep (the other backend's too while its cleanup is pending)
|
|
67
|
+
const kept = await scheduledStartService.deactivate(live);
|
|
68
|
+
stored = Object.assign({}, snapshot, kept || (scheduledStartService.hasLive(live) ? scheduledStartService.liveIds(live) : {}));
|
|
69
|
+
} else {
|
|
70
|
+
// update also when the ids belong to the other backend: it moves the schedule across
|
|
71
|
+
const hasIds = Object.keys(scheduledStartService.liveIds(live)).length > 0;
|
|
72
|
+
const ids = hasIds
|
|
73
|
+
? await scheduledStartService.update(live, snapshot, chatbot_id, id_project)
|
|
74
|
+
: await scheduledStartService.activate(chatbot_id, snapshot, id_project);
|
|
75
|
+
stored = Object.assign({}, snapshot, ids);
|
|
76
|
+
}
|
|
77
|
+
delete stored.error;
|
|
78
|
+
await Webhook.updateOne(filter, { $set: { scheduled_live: stored } }).exec();
|
|
79
|
+
return stored;
|
|
80
|
+
} catch (err) {
|
|
81
|
+
winston.error('(ScheduledStartPublishListener) sync failed for chatbot ' + chatbot_id, err);
|
|
82
|
+
try {
|
|
83
|
+
const withError = Object.assign({}, live || {}, { error: err.message || String(err) });
|
|
84
|
+
await Webhook.updateOne(filter, { $set: { scheduled_live: withError } }).exec();
|
|
85
|
+
return withError;
|
|
86
|
+
} catch (e) {
|
|
87
|
+
winston.error('(ScheduledStartPublishListener) cannot store sync error', e);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function onPublish(payload) {
|
|
94
|
+
try {
|
|
95
|
+
const chatbot = payload && payload.chatbot;
|
|
96
|
+
if (!chatbot || !payload.publishedBotId) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
await sync(chatbot.id_project, String(chatbot._id || chatbot.id), payload.publishedBotId);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
winston.error('(ScheduledStartPublishListener) publish handler error', err);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Destroys the live schedule of a deleted chatbot and unsets scheduled_live; idempotent. */
|
|
106
|
+
async function destroyOnDelete(faq_kb) {
|
|
107
|
+
try {
|
|
108
|
+
if (!faq_kb || !faq_kb._id || faq_kb.root_id || !scheduledStartRules.isConfigured()) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const chatbot_id = String(faq_kb._id);
|
|
112
|
+
const webhook = await Webhook.findOne({ chatbot_id: chatbot_id }).lean().exec();
|
|
113
|
+
if (webhook && !webhook.scheduled_live) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (!webhook) {
|
|
117
|
+
// Trashing a chatbot already deleted its webhook record (event faqbot.update.virtual.delete)
|
|
118
|
+
// together with the live ids: look the workflow up by name.
|
|
119
|
+
await scheduledStartService.destroyFor(chatbot_id, null);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
// On failure the live snapshot stays, so the run endpoint (410) can still take it offline.
|
|
123
|
+
const ok = await scheduledStartService.destroyFor(chatbot_id, webhook.scheduled_live);
|
|
124
|
+
if (ok) {
|
|
125
|
+
await Webhook.updateOne({ _id: webhook._id }, { $unset: { scheduled_live: 1 } }).exec();
|
|
126
|
+
}
|
|
127
|
+
} catch (err) {
|
|
128
|
+
winston.error('(ScheduledStartPublishListener) error destroying schedule of deleted chatbot', err);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** faqbot.delete (hard DELETE): payload is the chatbot. */
|
|
133
|
+
const onDelete = destroyOnDelete;
|
|
134
|
+
|
|
135
|
+
/** faqbot.deleted (soft trash via PUT and hard delete): payload is { req, chatbot }. */
|
|
136
|
+
async function onDeleted(payload) {
|
|
137
|
+
return destroyOnDelete(payload && payload.chatbot);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Newest published copy (faq_kb with root_id = chatbot) id, or null. */
|
|
141
|
+
const ID_KEYS = ['workflow_code', 'schedule_id', 'backend', 'job_id', 'id_project', 'chatbot_id', 'error'];
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Moves the live schedule of a chatbot to the selected backend, from the stored scheduled_live snapshot.
|
|
145
|
+
* Unlike sync it never reads the draft, so unpublished edits stay unpublished (used for the backend cutover).
|
|
146
|
+
* Returns the new scheduled_live, or null when there is nothing live. Scheduler errors are thrown.
|
|
147
|
+
*/
|
|
148
|
+
async function migrate(id_project, chatbot_id) {
|
|
149
|
+
const filter = { id_project: id_project, chatbot_id: chatbot_id };
|
|
150
|
+
const webhook = await Webhook.findOne(filter).lean().exec();
|
|
151
|
+
const live = webhook && webhook.scheduled_live;
|
|
152
|
+
if (!live || !live.schedule) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const snapshot = Object.keys(live).reduce((acc, key) => {
|
|
156
|
+
if (!ID_KEYS.includes(key)) acc[key] = live[key];
|
|
157
|
+
return acc;
|
|
158
|
+
}, {});
|
|
159
|
+
const ids = live.enabled
|
|
160
|
+
? await scheduledStartService.update(live, snapshot, chatbot_id, id_project)
|
|
161
|
+
: await scheduledStartService.deactivate(live);
|
|
162
|
+
const stored = Object.assign({}, snapshot, ids || {});
|
|
163
|
+
await Webhook.updateOne(filter, { $set: { scheduled_live: stored } }).exec();
|
|
164
|
+
return stored;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function latestPublishedId(id_project, chatbot_id) {
|
|
168
|
+
const copy = await Faq_kb.findOne({ id_project: id_project, root_id: chatbot_id }).sort({ publishedAt: -1 }).select('_id').lean().exec();
|
|
169
|
+
return copy ? String(copy._id) : null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
class ScheduledStartPublishListener {
|
|
173
|
+
listen() {
|
|
174
|
+
botEvent.on('faqbot.publish', onPublish);
|
|
175
|
+
botEvent.on('faqbot.delete', onDelete);
|
|
176
|
+
botEvent.on('faqbot.deleted', onDeleted);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
module.exports = new ScheduledStartPublishListener();
|
|
181
|
+
module.exports.sync = sync;
|
|
182
|
+
module.exports.onPublish = onPublish;
|
|
183
|
+
module.exports.onDelete = onDelete;
|
|
184
|
+
module.exports.onDeleted = onDeleted;
|
|
185
|
+
module.exports.latestPublishedId = latestPublishedId;
|
|
186
|
+
module.exports.migrate = migrate;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
const moment = require('moment-timezone');
|
|
2
|
+
|
|
3
|
+
const FREQUENCIES = ['interval', 'daily', 'weekly', 'monthly'];
|
|
4
|
+
const MINUTE_STEPS = [5, 10, 15, 20, 30];
|
|
5
|
+
const WEEKDAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
|
|
6
|
+
const TIME_REGEX = /^([01]\d|2[0-3]):[0-5]\d$/;
|
|
7
|
+
const FORBIDDEN_KEYS = ['__proto__', 'constructor', 'prototype'];
|
|
8
|
+
const KEY_REGEX = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
9
|
+
const MAX_PAYLOAD_KEYS = 50;
|
|
10
|
+
const MAX_PAYLOAD_BYTES = 8192;
|
|
11
|
+
|
|
12
|
+
const BACKENDS = ['dolphin', 'scheduler'];
|
|
13
|
+
const REQUIRED_ENV = {
|
|
14
|
+
dolphin: ['SCHEDULER_BASEURL', 'SCHEDULER_TOKEN', 'SCHEDULER_PROJECT', 'SCHEDULED_START_TOKEN'],
|
|
15
|
+
scheduler: ['SCHEDULER_SERVICE_URL', 'SCHEDULER_API_TOKEN', 'SCHEDULED_START_TOKEN']
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** Scheduler that runs scheduled starts: SCHEDULED_START_BACKEND, read per call (default dolphin). */
|
|
19
|
+
function backend() {
|
|
20
|
+
const value = process.env.SCHEDULED_START_BACKEND;
|
|
21
|
+
return BACKENDS.includes(value) ? value : 'dolphin';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isConfigured(name) {
|
|
25
|
+
return REQUIRED_ENV[name || backend()].every((env) => !!process.env[env]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isPlainObject(value) {
|
|
29
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @returns {string|null} an error message naming the field, or null when valid */
|
|
33
|
+
function validateSchedule(schedule) {
|
|
34
|
+
if (!isPlainObject(schedule)) {
|
|
35
|
+
return "schedule must be an object";
|
|
36
|
+
}
|
|
37
|
+
if (!FREQUENCIES.includes(schedule.frequency)) {
|
|
38
|
+
return "schedule.frequency must be one of " + FREQUENCIES.join(', ');
|
|
39
|
+
}
|
|
40
|
+
if (schedule.frequency === 'interval') {
|
|
41
|
+
if (schedule.unit === 'minutes') {
|
|
42
|
+
if (!MINUTE_STEPS.includes(schedule.every)) {
|
|
43
|
+
return "schedule.every must be one of " + MINUTE_STEPS.join(', ') + " when unit is minutes";
|
|
44
|
+
}
|
|
45
|
+
} else if (schedule.unit === 'hours') {
|
|
46
|
+
if (!Number.isInteger(schedule.every) || schedule.every < 1 || schedule.every > 12) {
|
|
47
|
+
return "schedule.every must be an integer between 1 and 12 when unit is hours";
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
return "schedule.unit must be minutes or hours";
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
if (typeof schedule.time !== 'string' || !TIME_REGEX.test(schedule.time)) {
|
|
54
|
+
return "schedule.time must be in the format HH:MM";
|
|
55
|
+
}
|
|
56
|
+
if (schedule.frequency === 'weekly') {
|
|
57
|
+
if (!Array.isArray(schedule.weekdays) || schedule.weekdays.length < 1 ||
|
|
58
|
+
!schedule.weekdays.every((d) => WEEKDAYS.includes(d))) {
|
|
59
|
+
return "schedule.weekdays must contain at least one of " + WEEKDAYS.join(', ');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (schedule.frequency === 'monthly') {
|
|
63
|
+
const d = schedule.day_of_month;
|
|
64
|
+
if (d !== 'last' && !(Number.isInteger(d) && d >= 1 && d <= 28)) {
|
|
65
|
+
return "schedule.day_of_month must be an integer between 1 and 28 or 'last'";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (typeof schedule.timezone !== 'string' || !schedule.timezone || !moment.tz.zone(schedule.timezone)) {
|
|
70
|
+
return "schedule.timezone must be a valid IANA time zone";
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @returns {string|null} an error message naming the field, or null when valid */
|
|
76
|
+
function validatePayload(payload) {
|
|
77
|
+
if (!isPlainObject(payload)) {
|
|
78
|
+
return "mapping.payload must be an object";
|
|
79
|
+
}
|
|
80
|
+
const keys = Object.keys(payload);
|
|
81
|
+
if (keys.length > MAX_PAYLOAD_KEYS) {
|
|
82
|
+
return "mapping.payload must have at most " + MAX_PAYLOAD_KEYS + " keys";
|
|
83
|
+
}
|
|
84
|
+
for (const key of keys) {
|
|
85
|
+
if (FORBIDDEN_KEYS.includes(key)) {
|
|
86
|
+
return "mapping.payload key " + key + " is not allowed";
|
|
87
|
+
}
|
|
88
|
+
if (!KEY_REGEX.test(key)) {
|
|
89
|
+
return "mapping.payload key " + key + " is invalid: use letters, digits and underscore, not starting with a digit";
|
|
90
|
+
}
|
|
91
|
+
const value = payload[key];
|
|
92
|
+
const ok = typeof value === 'string' || typeof value === 'boolean' ||
|
|
93
|
+
(typeof value === 'number' && Number.isFinite(value));
|
|
94
|
+
if (!ok) {
|
|
95
|
+
return "mapping.payload value of " + key + " must be a string, a finite number or a boolean";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (Buffer.byteLength(JSON.stringify(payload), 'utf8') > MAX_PAYLOAD_BYTES) {
|
|
99
|
+
return "mapping.payload must be at most " + MAX_PAYLOAD_BYTES + " bytes";
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function sortedWeekdays(weekdays) {
|
|
105
|
+
return WEEKDAYS.filter((d) => weekdays.includes(d));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const WEEKDAY_NUMBERS = { sun: 0, mon: 1, tue: 2, wed: 3, thu: 4, fri: 5, sat: 6 };
|
|
109
|
+
|
|
110
|
+
/** Standard 5-field cron (min hour dom mon dow, sun=0) for a valid schedule. */
|
|
111
|
+
function toCron5(schedule) {
|
|
112
|
+
if (schedule.frequency === 'interval') {
|
|
113
|
+
return schedule.unit === 'minutes'
|
|
114
|
+
? '*/' + schedule.every + ' * * * *'
|
|
115
|
+
: '0 */' + schedule.every + ' * * *';
|
|
116
|
+
}
|
|
117
|
+
const [hh, mm] = schedule.time.split(':').map((n) => parseInt(n, 10));
|
|
118
|
+
const head = mm + ' ' + hh + ' ';
|
|
119
|
+
if (schedule.frequency === 'daily') {
|
|
120
|
+
return head + '* * *';
|
|
121
|
+
}
|
|
122
|
+
if (schedule.frequency === 'weekly') {
|
|
123
|
+
return head + '* * ' + sortedWeekdays(schedule.weekdays).map((d) => WEEKDAY_NUMBERS[d]).join(',');
|
|
124
|
+
}
|
|
125
|
+
return head + (schedule.day_of_month === 'last' ? 'L' : schedule.day_of_month) + ' * *';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Quartz cron (seconds first) for a valid schedule. */
|
|
129
|
+
function toQuartzCron(schedule) {
|
|
130
|
+
if (schedule.frequency === 'interval') {
|
|
131
|
+
return schedule.unit === 'minutes'
|
|
132
|
+
? '0 0/' + schedule.every + ' * * * ? *'
|
|
133
|
+
: '0 0 0/' + schedule.every + ' * * ? *';
|
|
134
|
+
}
|
|
135
|
+
const [hh, mm] = schedule.time.split(':').map((n) => parseInt(n, 10));
|
|
136
|
+
const head = '0 ' + mm + ' ' + hh + ' ';
|
|
137
|
+
if (schedule.frequency === 'daily') {
|
|
138
|
+
return head + '* * ? *';
|
|
139
|
+
}
|
|
140
|
+
if (schedule.frequency === 'weekly') {
|
|
141
|
+
return head + '? * ' + sortedWeekdays(schedule.weekdays).map((d) => d.toUpperCase()).join(',') + ' *';
|
|
142
|
+
}
|
|
143
|
+
return head + (schedule.day_of_month === 'last' ? 'L' : schedule.day_of_month) + ' * ? *';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function capitalize(s) {
|
|
147
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Human readable description built from a valid schedule only. */
|
|
151
|
+
function summarize(schedule) {
|
|
152
|
+
const tz = ' (' + schedule.timezone + ')';
|
|
153
|
+
if (schedule.frequency === 'interval') {
|
|
154
|
+
if (schedule.every === 1 && schedule.unit === 'hours') {
|
|
155
|
+
return 'Every hour' + tz;
|
|
156
|
+
}
|
|
157
|
+
return 'Every ' + schedule.every + ' ' + schedule.unit + tz;
|
|
158
|
+
}
|
|
159
|
+
const at = ' at ' + schedule.time + tz;
|
|
160
|
+
if (schedule.frequency === 'daily') {
|
|
161
|
+
return 'Daily' + at;
|
|
162
|
+
}
|
|
163
|
+
if (schedule.frequency === 'weekly') {
|
|
164
|
+
const days = sortedWeekdays(schedule.weekdays);
|
|
165
|
+
if (days.join(',') === 'mon,tue,wed,thu,fri') {
|
|
166
|
+
return 'Every weekday' + at;
|
|
167
|
+
}
|
|
168
|
+
return 'Every ' + days.map(capitalize).join(', ') + at;
|
|
169
|
+
}
|
|
170
|
+
return 'Monthly on ' + (schedule.day_of_month === 'last' ? 'the last day' : 'day ' + schedule.day_of_month) + at;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
module.exports = { validateSchedule, validatePayload, toQuartzCron, toCron5, summarize, isConfigured, backend, WEEKDAYS };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
const winston = require('../config/winston');
|
|
2
|
+
const rules = require('./scheduledStartRules');
|
|
3
|
+
const dolphin = require('./scheduledStart/dolphinBackend');
|
|
4
|
+
const scheduler = require('./scheduledStart/schedulerBackend');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Scheduled start points run on DolphinScheduler or on tiledesk-scheduler-service, chosen per call
|
|
8
|
+
* by SCHEDULED_START_BACKEND. scheduled_live keeps the ids of the backend that created the schedule:
|
|
9
|
+
* when they belong to the other backend (cutover or rollback), that schedule is removed best effort
|
|
10
|
+
* so a bot is never started twice.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const BACKENDS = { dolphin, scheduler };
|
|
14
|
+
|
|
15
|
+
const hasDolphinIds = (live) => !!(live && live.workflow_code && live.schedule_id);
|
|
16
|
+
const hasSchedulerIds = (live) => !!(live && live.backend === 'scheduler' && live.job_id);
|
|
17
|
+
const OWNS = { dolphin: hasDolphinIds, scheduler: hasSchedulerIds };
|
|
18
|
+
const IDS = {
|
|
19
|
+
dolphin: (live) => ({ workflow_code: live.workflow_code, schedule_id: live.schedule_id }),
|
|
20
|
+
scheduler: (live) => ({ backend: 'scheduler', job_id: live.job_id, id_project: live.id_project, chatbot_id: live.chatbot_id }),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const current = () => rules.backend();
|
|
24
|
+
const other = () => (current() === 'scheduler' ? 'dolphin' : 'scheduler');
|
|
25
|
+
const idsOf = (name, live) => (OWNS[name](live) ? IDS[name](live) : {});
|
|
26
|
+
|
|
27
|
+
/** True when `live` holds ids of the selected backend. */
|
|
28
|
+
function hasLive(live) {
|
|
29
|
+
return OWNS[current()](live);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Every backend id held by `live` (both sets while a cleanup is pending), to store next to the snapshot. */
|
|
33
|
+
function liveIds(live) {
|
|
34
|
+
return Object.assign({}, idsOf('scheduler', live), idsOf('dolphin', live));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Removes the schedule `live` holds on the backend that is no longer selected. Never throws.
|
|
39
|
+
* Returns the ids to keep: those of the other backend when the removal failed, so the next call retries
|
|
40
|
+
* (dropping them would leave that schedule firing with nothing left to remove it). When the other backend
|
|
41
|
+
* is not configured any more nothing can be done and its ids are dropped.
|
|
42
|
+
*/
|
|
43
|
+
async function cleanupOther(live) {
|
|
44
|
+
const name = other();
|
|
45
|
+
if (!OWNS[name](live) || !rules.isConfigured(name)) return {};
|
|
46
|
+
try {
|
|
47
|
+
await BACKENDS[name].destroy(live);
|
|
48
|
+
return {};
|
|
49
|
+
} catch (err) {
|
|
50
|
+
winston.warn('(scheduledStart) cannot remove the ' + name + ' schedule after the backend switch, will retry: ' + err.message);
|
|
51
|
+
return IDS[name](live);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Without stored ids, a chatbot's schedule may still exist on the other backend (e.g. not migrated yet). */
|
|
56
|
+
async function lookupOther(method, chatbotId, live) {
|
|
57
|
+
const name = other();
|
|
58
|
+
if (OWNS[name](live) || !rules.isConfigured(name)) return true;
|
|
59
|
+
return BACKENDS[name][method](chatbotId, null);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const own = (live) => (hasLive(live) ? live : null);
|
|
63
|
+
const isEmpty = (ids) => Object.keys(ids).length === 0;
|
|
64
|
+
|
|
65
|
+
async function activate(chatbotId, snapshot, idProject) {
|
|
66
|
+
return BACKENDS[current()].activate(chatbotId, snapshot, idProject);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Returns the ids to store: the selected backend's, plus the other's while its cleanup is pending. */
|
|
70
|
+
async function update(live, snapshot, chatbotId, idProject) {
|
|
71
|
+
const ids = await BACKENDS[current()].update(own(live), snapshot, chatbotId, idProject);
|
|
72
|
+
return Object.assign({}, ids, await cleanupOther(live));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Returns the ids to store, like update. */
|
|
76
|
+
async function deactivate(live) {
|
|
77
|
+
if (hasLive(live)) await BACKENDS[current()].deactivate(live);
|
|
78
|
+
return Object.assign(idsOf(current(), live), await cleanupOther(live));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function destroy(live) {
|
|
82
|
+
if (hasLive(live)) await BACKENDS[current()].destroy(live);
|
|
83
|
+
const kept = await cleanupOther(live);
|
|
84
|
+
if (!isEmpty(kept)) {
|
|
85
|
+
const err = new Error('the ' + other() + ' schedule could not be removed');
|
|
86
|
+
err.status = 502;
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function nextRuns(live) {
|
|
92
|
+
return BACKENDS[current()].nextRuns(live);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Takes a chatbot's schedule offline: through deactivate when ids are stored, otherwise by looking it up
|
|
97
|
+
* on both backends. Never throws; resolves true on success, false on scheduler failure.
|
|
98
|
+
*/
|
|
99
|
+
async function offlineFor(chatbotId, live) {
|
|
100
|
+
if (!hasLive(live)) {
|
|
101
|
+
const done = await BACKENDS[current()].offlineFor(chatbotId, null);
|
|
102
|
+
const kept = await cleanupOther(live);
|
|
103
|
+
return done && isEmpty(kept) && (await lookupOther('offlineFor', chatbotId, live));
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const kept = await module.exports.deactivate(live);
|
|
107
|
+
return !kept || isEmpty(idsOf(other(), kept));
|
|
108
|
+
} catch (err) {
|
|
109
|
+
winston.warn('(scheduledStart) cannot take schedule of chatbot ' + chatbotId + ' offline: ' + err.message);
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Removes a chatbot's schedule: through destroy when ids are stored, otherwise by lookup on both backends. Never throws. */
|
|
115
|
+
async function destroyFor(chatbotId, live) {
|
|
116
|
+
if (!hasLive(live)) {
|
|
117
|
+
const done = await BACKENDS[current()].destroyFor(chatbotId, null);
|
|
118
|
+
const kept = await cleanupOther(live);
|
|
119
|
+
return done && isEmpty(kept) && (await lookupOther('destroyFor', chatbotId, live));
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
await module.exports.destroy(live);
|
|
123
|
+
return true;
|
|
124
|
+
} catch (err) {
|
|
125
|
+
winston.warn('(scheduledStart) cannot destroy schedule of chatbot ' + chatbotId + ': ' + err.message);
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = { activate, update, deactivate, destroy, nextRuns, offlineFor, destroyFor, hasLive, liveIds };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const faqBotEvent = require('../event/faqBotEvent');
|
|
2
|
+
const { Webhook } = require('../models/webhook');
|
|
3
|
+
const winston = require('../config/winston');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Removes the start points of a chatbot's webhook that point to a box (faq)
|
|
7
|
+
* when that box is deleted.
|
|
8
|
+
*/
|
|
9
|
+
async function onFaqDelete(faq) {
|
|
10
|
+
try {
|
|
11
|
+
if (!faq || !faq.intent_id || !faq.id_faq_kb) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
await Webhook.updateMany(
|
|
15
|
+
{ chatbot_id: faq.id_faq_kb.toString(), 'start_points.block_id': faq.intent_id },
|
|
16
|
+
{ $pull: { start_points: { block_id: faq.intent_id } } }
|
|
17
|
+
).exec();
|
|
18
|
+
} catch (err) {
|
|
19
|
+
winston.error('(StartPointCleanupListener) error removing start points of deleted box', err);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class StartPointCleanupListener {
|
|
24
|
+
listen() {
|
|
25
|
+
faqBotEvent.on('faq.delete', onFaqDelete);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = new StartPointCleanupListener();
|
|
30
|
+
module.exports.onFaqDelete = onFaqDelete;
|