@tiledesk/tiledesk-server 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.circleci/config.yml +1 -1
- package/CHANGELOG.md +37 -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 +14 -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/agentPrompt.js +104 -0
- package/models/analyticResult.js +1 -1
- package/models/channelConstants.js +3 -2
- package/models/quotaUsage.js +71 -0
- package/models/request.js +6 -0
- package/models/requestSnapshot.js +12 -0
- package/models/routerLogger.js +1 -1
- package/models/webhook.js +44 -0
- package/package.json +7 -5
- package/pubmodules/emailNotification/requestNotification.js +14 -22
- package/pubmodules/events/test/eventRoute.js +7 -6
- package/pubmodules/rules/conciergeBot.js +9 -4
- package/routes/agentPrompts.js +132 -0
- package/routes/auth.js +1 -12
- package/routes/campaigns.js +0 -13
- package/routes/dataTable.js +0 -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/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 +154 -0
- package/test/agentTokensQuota.js +237 -0
- package/test/botEventHitlFallback.js +19 -0
- package/test/chat21HandlerJoinWiring.js +59 -0
- package/test/dataTablesRoute.test.js +10 -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/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
package/routes/webhooks.js
CHANGED
|
@@ -6,6 +6,33 @@ const httpUtil = require('../utils/httpUtil');
|
|
|
6
6
|
const { customAlphabet } = require('nanoid');
|
|
7
7
|
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz0123456789', 32);
|
|
8
8
|
var ObjectId = require('mongoose').Types.ObjectId;
|
|
9
|
+
const webhookSettingsValidator = require('../services/webhookSettingsValidator');
|
|
10
|
+
const startPointService = require('../services/startPointService');
|
|
11
|
+
const webhookConversationService = require('../services/webhookConversationService');
|
|
12
|
+
const UIDGenerator = require('../utils/UIDGenerator');
|
|
13
|
+
const scheduledStartRules = require('../services/scheduledStartRules');
|
|
14
|
+
const scheduledStartService = require('../services/scheduledStartService');
|
|
15
|
+
const scheduledStartPublishListener = require('../services/scheduledStartPublishListener');
|
|
16
|
+
|
|
17
|
+
const SCHEDULED_UNAVAILABLE = "Scheduled starts are not available on this installation";
|
|
18
|
+
|
|
19
|
+
const SETTINGS_KEYS = ['mode', 'block_id'];
|
|
20
|
+
|
|
21
|
+
function hasSettings(body) {
|
|
22
|
+
return SETTINGS_KEYS.some((key) => body.hasOwnProperty(key));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Loads the chatbot and validates the settings of body.
|
|
27
|
+
* @returns {Promise<{ok: true, fields: Object} | {ok: false, status: number, message: string}>}
|
|
28
|
+
*/
|
|
29
|
+
async function validateSettings(id_project, chatbot_id, body) {
|
|
30
|
+
const chatbot = await webhookSettingsValidator.findChatbot(id_project, chatbot_id);
|
|
31
|
+
if (!chatbot) {
|
|
32
|
+
return { ok: false, status: 404, message: "Chatbot not found with id " + chatbot_id };
|
|
33
|
+
}
|
|
34
|
+
return webhookSettingsValidator.validate(id_project, chatbot, body);
|
|
35
|
+
}
|
|
9
36
|
// const port = process.env.PORT || '3000';
|
|
10
37
|
// let TILEBOT_ENDPOINT = "http://localhost:" + port + "/modules/tilebot/ext/";;
|
|
11
38
|
// if (process.env.TILEBOT_ENDPOINT) {
|
|
@@ -26,6 +53,17 @@ router.get('/', async (req, res) => {
|
|
|
26
53
|
|
|
27
54
|
})
|
|
28
55
|
|
|
56
|
+
// Removes the scheduler workflow of a webhook about to be deleted. Resolves false when the
|
|
57
|
+
// scheduler cleanup failed (the caller keeps the webhook so the delete can be retried).
|
|
58
|
+
async function destroyScheduledStart(webhook) {
|
|
59
|
+
if (!webhook || !webhook.scheduled_live) return true;
|
|
60
|
+
if (!scheduledStartRules.isConfigured()) {
|
|
61
|
+
winston.warn("Scheduled start is not configured: the workflow of chatbot " + webhook.chatbot_id + " may be orphaned in the scheduler");
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return scheduledStartService.destroyFor(webhook.chatbot_id, webhook.scheduled_live);
|
|
65
|
+
}
|
|
66
|
+
|
|
29
67
|
router.get('/:chatbot_id', async (req, res) => {
|
|
30
68
|
|
|
31
69
|
let id_project = req.projectid;
|
|
@@ -38,10 +76,18 @@ router.get('/:chatbot_id', async (req, res) => {
|
|
|
38
76
|
|
|
39
77
|
if (!webhook) {
|
|
40
78
|
winston.verbose("Webhook not found for chatbot " + chatbot_id);
|
|
41
|
-
|
|
79
|
+
// scheduled_available lets the editor offer the Scheduled start even before any webhook record exists
|
|
80
|
+
return res.status(404).send({ success: false, error: "Webhook not found for chatbot " + chatbot_id, scheduled_available: scheduledStartRules.isConfigured() });
|
|
42
81
|
}
|
|
43
82
|
|
|
44
|
-
|
|
83
|
+
let body = Object.assign(webhook.toObject(), { scheduled_available: scheduledStartRules.isConfigured() });
|
|
84
|
+
if (body.scheduled_available && body.scheduled_live && body.scheduled_live.enabled && body.scheduled_live.schedule) {
|
|
85
|
+
const nextRuns = await scheduledStartService.nextRuns(body.scheduled_live);
|
|
86
|
+
if (Array.isArray(nextRuns)) {
|
|
87
|
+
body.next_runs = nextRuns;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
res.status(200).send(body);
|
|
45
91
|
|
|
46
92
|
})
|
|
47
93
|
|
|
@@ -67,15 +113,29 @@ router.get('/id/:webhook_id', async (req, res) => {
|
|
|
67
113
|
router.post('/', async (req, res) => {
|
|
68
114
|
|
|
69
115
|
let id_project = req.projectid;
|
|
116
|
+
let chatbot_id = req.body.chatbot_id || req.body.id_faq_kb;
|
|
117
|
+
|
|
118
|
+
let validation;
|
|
119
|
+
try {
|
|
120
|
+
validation = await validateSettings(id_project, chatbot_id, req.body);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
winston.error("Error validating new webhook ", err);
|
|
123
|
+
return res.status(500).send({ success: false, error: "Error validating webhook for chatbot " + chatbot_id });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!validation.ok) {
|
|
127
|
+
winston.verbose("Invalid webhook for chatbot " + chatbot_id + ": " + validation.message);
|
|
128
|
+
return res.status(validation.status).send({ success: false, error: validation.message });
|
|
129
|
+
}
|
|
70
130
|
|
|
71
|
-
let webhook = new Webhook({
|
|
131
|
+
let webhook = new Webhook(Object.assign({
|
|
72
132
|
id_project: id_project,
|
|
73
133
|
name: "webhook-" + this.webhook_id,
|
|
74
|
-
chatbot_id:
|
|
134
|
+
chatbot_id: chatbot_id,
|
|
75
135
|
block_id: req.body.block_id,
|
|
76
136
|
copilot: req.body.copilot,
|
|
77
137
|
async: req.body.async
|
|
78
|
-
})
|
|
138
|
+
}, validation.fields));
|
|
79
139
|
|
|
80
140
|
webhook.save((err, savedWebhook) => {
|
|
81
141
|
if (err) {
|
|
@@ -96,9 +156,23 @@ router.post('/preload/:webhook_id', async (req, res) => {
|
|
|
96
156
|
|
|
97
157
|
let id_project = req.projectid;
|
|
98
158
|
let webhook_id = req.params.webhook_id;
|
|
99
|
-
let request_id = "automation-request-" + id_project + "-" + new ObjectId() + "-" + webhook_id;
|
|
100
159
|
let redis_client = req.app.get('redis_client');
|
|
101
160
|
|
|
161
|
+
let webhook;
|
|
162
|
+
try {
|
|
163
|
+
webhook = await Webhook.findOne({ id_project: id_project, webhook_id: webhook_id }).lean().exec();
|
|
164
|
+
} catch (err) {
|
|
165
|
+
winston.error("Error finding webhook for preload ", err);
|
|
166
|
+
return res.status(500).send({ success: false, message: "Unable to start development webhook" });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let request_id;
|
|
170
|
+
if (webhook && webhook.mode === webhookSettingsValidator.CONVERSATION_MODE) {
|
|
171
|
+
request_id = "support-group-" + id_project + "-" + UIDGenerator.generate();
|
|
172
|
+
} else {
|
|
173
|
+
request_id = "automation-request-" + id_project + "-" + new ObjectId() + "-" + webhook_id;
|
|
174
|
+
}
|
|
175
|
+
|
|
102
176
|
try {
|
|
103
177
|
let key = "logs:webhook:" + id_project + ":" + webhook_id;
|
|
104
178
|
let value = JSON.stringify({ request_id: request_id });
|
|
@@ -198,6 +272,20 @@ router.put("/:chatbot_id", async (req, res) => {
|
|
|
198
272
|
update.enabled = req.body.enabled;
|
|
199
273
|
}
|
|
200
274
|
|
|
275
|
+
if (hasSettings(req.body)) {
|
|
276
|
+
let validation;
|
|
277
|
+
try {
|
|
278
|
+
validation = await validateSettings(id_project, chatbot_id, req.body);
|
|
279
|
+
} catch (err) {
|
|
280
|
+
winston.error("Error validating webhook update ", err);
|
|
281
|
+
return res.status(500).send({ success: false, error: "Error validating webhook for chatbot " + chatbot_id });
|
|
282
|
+
}
|
|
283
|
+
if (!validation.ok) {
|
|
284
|
+
return res.status(validation.status).send({ success: false, error: validation.message });
|
|
285
|
+
}
|
|
286
|
+
Object.assign(update, validation.fields);
|
|
287
|
+
}
|
|
288
|
+
|
|
201
289
|
let updatedWebhook = await Webhook.findOneAndUpdate({ id_project: id_project, chatbot_id: chatbot_id }, update, { new: true }).catch((err) => {
|
|
202
290
|
winston.error("Error updating webhook ", err);
|
|
203
291
|
return res.status(500).send({ success: false, error: "Error updating webhook for chatbot " + chatbot_id });
|
|
@@ -211,6 +299,141 @@ router.put("/:chatbot_id", async (req, res) => {
|
|
|
211
299
|
res.status(200).send(updatedWebhook);
|
|
212
300
|
})
|
|
213
301
|
|
|
302
|
+
router.put('/:chatbot_id/start_points/:type', async (req, res) => {
|
|
303
|
+
|
|
304
|
+
let id_project = req.projectid;
|
|
305
|
+
let chatbot_id = req.params.chatbot_id;
|
|
306
|
+
|
|
307
|
+
if (req.params.type === 'scheduled' && !scheduledStartRules.isConfigured()) {
|
|
308
|
+
return res.status(503).send({ success: false, error: SCHEDULED_UNAVAILABLE });
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
try {
|
|
312
|
+
let chatbot = await webhookSettingsValidator.findChatbot(id_project, chatbot_id);
|
|
313
|
+
if (!chatbot) {
|
|
314
|
+
return res.status(404).send({ success: false, error: "Chatbot not found with id " + chatbot_id });
|
|
315
|
+
}
|
|
316
|
+
let webhook = await startPointService.upsert(id_project, chatbot, req.params.type, req.body || {});
|
|
317
|
+
if (req.params.type === 'scheduled') {
|
|
318
|
+
let body = webhook.toObject();
|
|
319
|
+
let draft = startPointService.find(body, 'scheduled');
|
|
320
|
+
body.schedule_summary = scheduledStartRules.summarize(draft.schedule);
|
|
321
|
+
if (body.scheduled_live && body.scheduled_live.schedule) {
|
|
322
|
+
body.live_summary = scheduledStartRules.summarize(body.scheduled_live.schedule);
|
|
323
|
+
}
|
|
324
|
+
return res.status(200).send(body);
|
|
325
|
+
}
|
|
326
|
+
res.status(200).send(webhook);
|
|
327
|
+
} catch (err) {
|
|
328
|
+
if (err instanceof startPointService.StartPointError) {
|
|
329
|
+
return res.status(err.status).send({ success: false, error: err.message });
|
|
330
|
+
}
|
|
331
|
+
winston.error("Error saving start point ", err);
|
|
332
|
+
res.status(500).send({ success: false, error: "Error saving start point for chatbot " + chatbot_id });
|
|
333
|
+
}
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
router.delete('/:chatbot_id/start_points/:type', async (req, res) => {
|
|
337
|
+
|
|
338
|
+
let id_project = req.projectid;
|
|
339
|
+
let chatbot_id = req.params.chatbot_id;
|
|
340
|
+
|
|
341
|
+
if (req.params.type === 'scheduled' && !scheduledStartRules.isConfigured()) {
|
|
342
|
+
return res.status(503).send({ success: false, error: SCHEDULED_UNAVAILABLE });
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
try {
|
|
346
|
+
let webhook = await startPointService.remove(id_project, chatbot_id, req.params.type);
|
|
347
|
+
res.status(200).send(webhook);
|
|
348
|
+
} catch (err) {
|
|
349
|
+
if (err instanceof startPointService.StartPointError) {
|
|
350
|
+
return res.status(err.status).send({ success: false, error: err.message });
|
|
351
|
+
}
|
|
352
|
+
winston.error("Error removing start point ", err);
|
|
353
|
+
res.status(500).send({ success: false, error: "Error removing start point for chatbot " + chatbot_id });
|
|
354
|
+
}
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
router.post('/:chatbot_id/start_points/scheduled/sync', async (req, res) => {
|
|
358
|
+
|
|
359
|
+
let id_project = req.projectid;
|
|
360
|
+
let chatbot_id = req.params.chatbot_id;
|
|
361
|
+
|
|
362
|
+
if (!scheduledStartRules.isConfigured()) {
|
|
363
|
+
return res.status(503).send({ success: false, error: SCHEDULED_UNAVAILABLE });
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
try {
|
|
367
|
+
let chatbot = await webhookSettingsValidator.findChatbot(id_project, chatbot_id);
|
|
368
|
+
if (!chatbot) {
|
|
369
|
+
return res.status(404).send({ success: false, error: "Chatbot not found with id " + chatbot_id });
|
|
370
|
+
}
|
|
371
|
+
let published_bot_id = await scheduledStartPublishListener.latestPublishedId(id_project, chatbot_id);
|
|
372
|
+
let scheduled_live = await scheduledStartPublishListener.sync(id_project, chatbot_id, published_bot_id);
|
|
373
|
+
res.status(200).send({ success: true, scheduled_live: scheduled_live });
|
|
374
|
+
} catch (err) {
|
|
375
|
+
winston.error("Error syncing scheduled start ", err);
|
|
376
|
+
res.status(500).send({ success: false, error: "Error syncing scheduled start for chatbot " + chatbot_id });
|
|
377
|
+
}
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
// Backend cutover: moves the live schedule (not the draft) to the scheduler selected by SCHEDULED_START_BACKEND.
|
|
381
|
+
router.post('/:chatbot_id/start_points/scheduled/migrate', async (req, res) => {
|
|
382
|
+
|
|
383
|
+
let id_project = req.projectid;
|
|
384
|
+
let chatbot_id = req.params.chatbot_id;
|
|
385
|
+
|
|
386
|
+
if (!scheduledStartRules.isConfigured()) {
|
|
387
|
+
return res.status(503).send({ success: false, error: SCHEDULED_UNAVAILABLE });
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
try {
|
|
391
|
+
let scheduled_live = await scheduledStartPublishListener.migrate(id_project, chatbot_id);
|
|
392
|
+
res.status(200).send({ success: true, scheduled_live: scheduled_live });
|
|
393
|
+
} catch (err) {
|
|
394
|
+
winston.error("Error migrating scheduled start ", err);
|
|
395
|
+
res.status(err.status || 500).send({ success: false, error: "Error migrating scheduled start for chatbot " + chatbot_id });
|
|
396
|
+
}
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
router.post('/:chatbot_id/start_points/scheduled/test', async (req, res) => {
|
|
400
|
+
|
|
401
|
+
let id_project = req.projectid;
|
|
402
|
+
let chatbot_id = req.params.chatbot_id;
|
|
403
|
+
|
|
404
|
+
if (!scheduledStartRules.isConfigured()) {
|
|
405
|
+
return res.status(503).send({ success: false, error: SCHEDULED_UNAVAILABLE });
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
try {
|
|
409
|
+
let chatbot = await webhookSettingsValidator.findChatbot(id_project, chatbot_id);
|
|
410
|
+
if (!chatbot) {
|
|
411
|
+
return res.status(404).send({ success: false, error: "Chatbot not found with id " + chatbot_id });
|
|
412
|
+
}
|
|
413
|
+
let webhook = await Webhook.findOne({ id_project: id_project, chatbot_id: chatbot_id });
|
|
414
|
+
let draft = webhook && startPointService.find(webhook, 'scheduled');
|
|
415
|
+
if (!draft) {
|
|
416
|
+
return res.status(404).send({ success: false, error: "Scheduled start point not found for chatbot " + chatbot_id });
|
|
417
|
+
}
|
|
418
|
+
let mapping = draft.mapping || {};
|
|
419
|
+
let result = await webhookConversationService.start(webhook, chatbot, mapping.payload || {}, {
|
|
420
|
+
dev: true,
|
|
421
|
+
startPoint: draft,
|
|
422
|
+
startType: 'scheduled',
|
|
423
|
+
leadId: 'scheduled_' + chatbot_id,
|
|
424
|
+
leadName: mapping.source_name || chatbot.name || 'Scheduled',
|
|
425
|
+
extraAttributes: { scheduled_at: new Date().toISOString() }
|
|
426
|
+
});
|
|
427
|
+
res.status(202).send({ success: true, request_id: result.request_id });
|
|
428
|
+
} catch (err) {
|
|
429
|
+
if (err.status) {
|
|
430
|
+
return res.status(err.status).send({ success: false, error: err.message });
|
|
431
|
+
}
|
|
432
|
+
winston.error("Error starting scheduled test run ", err);
|
|
433
|
+
res.status(500).send({ success: false, error: "Error starting test run for chatbot " + chatbot_id });
|
|
434
|
+
}
|
|
435
|
+
})
|
|
436
|
+
|
|
214
437
|
router.put('/update/:webhook_id', async (req, res) => {
|
|
215
438
|
|
|
216
439
|
let id_project = req.projectid;
|
|
@@ -230,6 +453,30 @@ router.put('/update/:webhook_id', async (req, res) => {
|
|
|
230
453
|
update.enabled = req.body.enabled;
|
|
231
454
|
}
|
|
232
455
|
|
|
456
|
+
if (hasSettings(req.body)) {
|
|
457
|
+
let existing;
|
|
458
|
+
try {
|
|
459
|
+
existing = await Webhook.findOne({ id_project: id_project, webhook_id: webhook_id }).lean().exec();
|
|
460
|
+
} catch (err) {
|
|
461
|
+
winston.error("Error finding webhook for update ", err);
|
|
462
|
+
return res.status(500).send({ success: false, error: "Error updating webhook with id " + webhook_id });
|
|
463
|
+
}
|
|
464
|
+
if (!existing) {
|
|
465
|
+
return res.status(404).send({ success: false, error: "Webhook not found with id " + webhook_id });
|
|
466
|
+
}
|
|
467
|
+
let validation;
|
|
468
|
+
try {
|
|
469
|
+
validation = await validateSettings(id_project, existing.chatbot_id, req.body);
|
|
470
|
+
} catch (err) {
|
|
471
|
+
winston.error("Error validating webhook update ", err);
|
|
472
|
+
return res.status(500).send({ success: false, error: "Error validating webhook with id " + webhook_id });
|
|
473
|
+
}
|
|
474
|
+
if (!validation.ok) {
|
|
475
|
+
return res.status(validation.status).send({ success: false, error: validation.message });
|
|
476
|
+
}
|
|
477
|
+
Object.assign(update, validation.fields);
|
|
478
|
+
}
|
|
479
|
+
|
|
233
480
|
let updatedWebhook = await Webhook.findOneAndUpdate({ id_project: id_project, webhook_id: webhook_id }, update, { new: true }).catch((err) => {
|
|
234
481
|
winston.error("Error updating webhook ", err);
|
|
235
482
|
return res.status(500).send({ success: false, error: "Error updating webhook for chatbot " + chatbot_id });
|
|
@@ -249,10 +496,23 @@ router.delete("/:chatbot_id", async (req, res) => {
|
|
|
249
496
|
let id_project = req.projectid;
|
|
250
497
|
let chatbot_id = req.params.chatbot_id;
|
|
251
498
|
|
|
499
|
+
try {
|
|
500
|
+
let existing = await Webhook.findOne({ id_project: id_project, chatbot_id: chatbot_id }).lean().exec();
|
|
501
|
+
if (!(await destroyScheduledStart(existing))) {
|
|
502
|
+
return res.status(502).send({ success: false, error: "Cannot remove the schedule of chatbot " + chatbot_id });
|
|
503
|
+
}
|
|
504
|
+
} catch (err) {
|
|
505
|
+
winston.error("Error destroying scheduled start ", err);
|
|
506
|
+
return res.status(500).send({ success: false, error: "Error deleting webhook for chatbot " + chatbot_id });
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
let deleted = true;
|
|
252
510
|
await Webhook.deleteOne({ id_project: id_project, chatbot_id: chatbot_id }).catch((err) => {
|
|
253
511
|
winston.error("Error deleting webhook ", err);
|
|
512
|
+
deleted = false;
|
|
254
513
|
return res.status(500).send({ success: false, error: "Error deleting webhook for chatbot " + chatbot_id });
|
|
255
514
|
})
|
|
515
|
+
if (!deleted) return;
|
|
256
516
|
|
|
257
517
|
res.status(200).send({ success: true, message: "Webhook for chatbot " + chatbot_id + " deleted successfully" });
|
|
258
518
|
})
|
|
@@ -262,6 +522,16 @@ router.delete("/delete/:webhook_id", async (req, res) => {
|
|
|
262
522
|
let id_project = req.projectid;
|
|
263
523
|
let webhook_id = req.params.webhook_id;
|
|
264
524
|
|
|
525
|
+
try {
|
|
526
|
+
let existing = await Webhook.findOne({ id_project: id_project, webhook_id: webhook_id }).lean().exec();
|
|
527
|
+
if (!(await destroyScheduledStart(existing))) {
|
|
528
|
+
return res.status(502).send({ success: false, error: "Cannot remove the schedule of webhook " + webhook_id });
|
|
529
|
+
}
|
|
530
|
+
} catch (err) {
|
|
531
|
+
winston.error("Error destroying scheduled start ", err);
|
|
532
|
+
return res.status(500).send({ success: false, error: "Error deleting webhook with id " + webhook_id });
|
|
533
|
+
}
|
|
534
|
+
|
|
265
535
|
await Webhook.deleteOne({ id_project: id_project, webhook_id: webhook_id }).catch((err) => {
|
|
266
536
|
winston.error("Error deleting webhook ", err);
|
|
267
537
|
return res.status(500).send({ success: false, error: "Error deleting webhook with id " + webhook_id });
|
package/routes/widgetLoader.js
CHANGED
|
@@ -5,10 +5,13 @@ var winston = require('../config/winston');
|
|
|
5
5
|
var widgetConfig = require('../config/widget');
|
|
6
6
|
|
|
7
7
|
var widgetLocation = process.env.WIDGET_LOCATION || widgetConfig.location;
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
function rawQuery(requestUrl) {
|
|
10
|
+
return new URL(requestUrl, 'http://localhost').search.slice(1);
|
|
11
|
+
}
|
|
9
12
|
|
|
10
13
|
router.get('/load', function(req, res) {
|
|
11
|
-
var query =
|
|
14
|
+
var query = rawQuery(req.url);
|
|
12
15
|
winston.debug(query);
|
|
13
16
|
// TODO chech if query is null
|
|
14
17
|
res.redirect(widgetLocation+'?'+query);
|
|
@@ -49,7 +52,7 @@ router.get('/v5/:project_id', function(req, res) {
|
|
|
49
52
|
var widgetTestLocation = process.env.WIDGET_TEST_LOCATION || widgetConfig.testLocation;
|
|
50
53
|
|
|
51
54
|
router.get('/test/load', function(req, res) {
|
|
52
|
-
var query =
|
|
55
|
+
var query = rawQuery(req.url);
|
|
53
56
|
res.redirect(widgetTestLocation+'?'+query);
|
|
54
57
|
});
|
|
55
58
|
|
package/services/QuoteManager.js
CHANGED
|
@@ -4,6 +4,7 @@ let winston = require('../config/winston');
|
|
|
4
4
|
const requestEvent = require('../event/requestEvent');
|
|
5
5
|
const messageEvent = require('../event/messageEvent');
|
|
6
6
|
const emailEvent = require('../event/emailEvent');
|
|
7
|
+
const quotaSnapshotService = require('./quotaSnapshotService');
|
|
7
8
|
|
|
8
9
|
// NEW
|
|
9
10
|
// const PLANS_LIST = {
|
|
@@ -16,22 +17,22 @@ const emailEvent = require('../event/emailEvent');
|
|
|
16
17
|
|
|
17
18
|
const PLANS_LIST = {
|
|
18
19
|
//FREE_TRIAL: { requests: 200, messages: 0, tokens: 100000, voice_duration: 0, email: 200, chatbots: 20, namespace: 3, kbs: 50 }, // same as PREMIUM
|
|
19
|
-
SANDBOX: { requests: 200, messages: 0, tokens: 100000, voice_duration: 0, email: 200, chatbots: 2, namespace: 1, kbs: 50 },
|
|
20
|
-
BASIC: { requests: 800, messages: 0, tokens: 2000000, voice_duration: 0, email: 200, chatbots: 5, namespace: 1, kbs: 150 },
|
|
21
|
-
PREMIUM: { requests: 3000, messages: 0, tokens: 5000000, voice_duration: 0, email: 200, chatbots: 20, namespace: 3, kbs: 300 },
|
|
22
|
-
TEAM: { requests: 5000, messages: 0, tokens: 10000000, voice_duration: 0, email: 200, chatbots: 50, namespace: 10, kbs: 1000 },
|
|
20
|
+
SANDBOX: { requests: 200, messages: 0, tokens: 100000, agent_tokens: 100000, voice_duration: 0, email: 200, chatbots: 2, namespace: 1, kbs: 50 },
|
|
21
|
+
BASIC: { requests: 800, messages: 0, tokens: 2000000, agent_tokens: 2000000, voice_duration: 0, email: 200, chatbots: 5, namespace: 1, kbs: 150 },
|
|
22
|
+
PREMIUM: { requests: 3000, messages: 0, tokens: 5000000, agent_tokens: 5000000, voice_duration: 0, email: 200, chatbots: 20, namespace: 3, kbs: 300 },
|
|
23
|
+
TEAM: { requests: 5000, messages: 0, tokens: 10000000, agent_tokens: 10000000, voice_duration: 0, email: 200, chatbots: 50, namespace: 10, kbs: 1000 },
|
|
23
24
|
//CUSTOM: { requests: 5000, messages: 0, tokens: 10000000, voice_duration: 120000, email: 200, chatbots: 50, namespace: 10, kbs: 1000 },
|
|
24
25
|
// FROM MARCH 2025
|
|
25
|
-
FREE_TRIAL: { requests: 3000, messages: 0, tokens: 5000000, voice_duration: 120000, email: 200, chatbots: 5, namespace: 1, kbs: 50 }, // same as PRO
|
|
26
|
-
STARTER: { requests: 800, messages: 0, tokens: 2000000, voice_duration: 0, email: 200, chatbots: 5, namespace: 1, kbs: 150 },
|
|
27
|
-
PRO: { requests: 3000, messages: 0, tokens: 5000000, voice_duration: 0, email: 200, chatbots: 20, namespace: 3, kbs: 300 },
|
|
28
|
-
BUSINESS: { requests: 5000, messages: 0, tokens: 10000000, voice_duration: 0, email: 200, chatbots: 50, namespace: 10, kbs: 1000 },
|
|
29
|
-
CUSTOM: { requests: 5000, messages: 0, tokens: 10000000, voice_duration: 120000, email: 200, chatbots: 50, namespace: 10, kbs: 1000 }
|
|
26
|
+
FREE_TRIAL: { requests: 3000, messages: 0, tokens: 5000000, agent_tokens: 5000000, voice_duration: 120000, email: 200, chatbots: 5, namespace: 1, kbs: 50 }, // same as PRO
|
|
27
|
+
STARTER: { requests: 800, messages: 0, tokens: 2000000, agent_tokens: 2000000, voice_duration: 0, email: 200, chatbots: 5, namespace: 1, kbs: 150 },
|
|
28
|
+
PRO: { requests: 3000, messages: 0, tokens: 5000000, agent_tokens: 5000000, voice_duration: 0, email: 200, chatbots: 20, namespace: 3, kbs: 300 },
|
|
29
|
+
BUSINESS: { requests: 5000, messages: 0, tokens: 10000000, agent_tokens: 10000000, voice_duration: 0, email: 200, chatbots: 50, namespace: 10, kbs: 1000 },
|
|
30
|
+
CUSTOM: { requests: 5000, messages: 0, tokens: 10000000, agent_tokens: 10000000, voice_duration: 120000, email: 200, chatbots: 50, namespace: 10, kbs: 1000 }
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
|
|
33
34
|
|
|
34
|
-
const typesList = ['requests', 'messages', 'email', 'tokens', 'voice_duration', 'chatbots', 'kbs']
|
|
35
|
+
const typesList = ['requests', 'messages', 'email', 'tokens', 'agent_tokens', 'voice_duration', 'chatbots', 'kbs']
|
|
35
36
|
|
|
36
37
|
let quotes_enabled = true;
|
|
37
38
|
|
|
@@ -59,7 +60,8 @@ class QuoteManager {
|
|
|
59
60
|
let key = await this.generateKey(request, 'requests');
|
|
60
61
|
winston.verbose("[QuoteManager] incrementRequestsCount key: " + key);
|
|
61
62
|
|
|
62
|
-
await this.
|
|
63
|
+
await this.restoreKeyIfMissing(key);
|
|
64
|
+
await this.incrementKey(key, 'incr');
|
|
63
65
|
this.sendEmailIfQuotaExceeded(project, request, 'requests', key);
|
|
64
66
|
return key;
|
|
65
67
|
}
|
|
@@ -70,7 +72,8 @@ class QuoteManager {
|
|
|
70
72
|
let key = await this.generateKey(message, 'messages');
|
|
71
73
|
winston.verbose("[QuoteManager] incrementMessagesCount key: " + key);
|
|
72
74
|
|
|
73
|
-
await this.
|
|
75
|
+
await this.restoreKeyIfMissing(key);
|
|
76
|
+
await this.incrementKey(key, 'incr');
|
|
74
77
|
return key;
|
|
75
78
|
}
|
|
76
79
|
|
|
@@ -80,7 +83,8 @@ class QuoteManager {
|
|
|
80
83
|
let key = await this.generateKey(email, 'email');
|
|
81
84
|
winston.verbose("[QuoteManager] incrementEmailCount key: " + key);
|
|
82
85
|
|
|
83
|
-
await this.
|
|
86
|
+
await this.restoreKeyIfMissing(key);
|
|
87
|
+
await this.incrementKey(key, 'incr');
|
|
84
88
|
this.sendEmailIfQuotaExceeded(project, email, 'email', key);
|
|
85
89
|
return key;
|
|
86
90
|
}
|
|
@@ -97,12 +101,35 @@ class QuoteManager {
|
|
|
97
101
|
}
|
|
98
102
|
|
|
99
103
|
let tokens = data.tokens * data.multiplier;
|
|
100
|
-
await this.
|
|
104
|
+
await this.restoreKeyIfMissing(key);
|
|
105
|
+
await this.incrementKey(key, 'incrbyfloat', tokens);
|
|
101
106
|
// await this.tdCache.incrby(key, tokens);
|
|
102
107
|
this.sendEmailIfQuotaExceeded(project, data, 'tokens', key);
|
|
103
108
|
return key;
|
|
104
109
|
}
|
|
105
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Increment the AI agent runtime budget (separate from the chatbot 'tokens' quota).
|
|
113
|
+
* data.counted is the number of agent tokens already converted by the caller
|
|
114
|
+
* (see computeAgentTokens in utils/aiUtils.js).
|
|
115
|
+
*/
|
|
116
|
+
async incrementAgentTokenCount(project, data) {
|
|
117
|
+
|
|
118
|
+
this.project = project;
|
|
119
|
+
let key = await this.generateKey(data, 'agent_tokens');
|
|
120
|
+
winston.verbose("[QuoteManager] incrementAgentTokenCount key: " + key);
|
|
121
|
+
|
|
122
|
+
if (quotes_enabled === false) {
|
|
123
|
+
winston.debug("QUOTES DISABLED - incrementAgentTokenCount")
|
|
124
|
+
return key;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
await this.restoreKeyIfMissing(key);
|
|
128
|
+
await this.incrementKey(key, 'incrbyfloat', data.counted);
|
|
129
|
+
this.sendEmailIfQuotaExceeded(project, data, 'agent_tokens', key);
|
|
130
|
+
return key;
|
|
131
|
+
}
|
|
132
|
+
|
|
106
133
|
async incrementVoiceDurationCount(project, request) {
|
|
107
134
|
|
|
108
135
|
this.project = project;
|
|
@@ -116,13 +143,32 @@ class QuoteManager {
|
|
|
116
143
|
|
|
117
144
|
if (request?.duration) {
|
|
118
145
|
let duration = Math.round(request.duration / 1000); // from ms to s
|
|
119
|
-
await this.
|
|
146
|
+
await this.restoreKeyIfMissing(key);
|
|
147
|
+
await this.incrementKey(key, 'incrby', duration);
|
|
120
148
|
|
|
121
149
|
this.sendEmailIfQuotaExceeded(project, request, 'voice_duration', key);
|
|
122
150
|
}
|
|
123
151
|
}
|
|
124
152
|
// INCREMENT KEY SECTION - END
|
|
125
153
|
|
|
154
|
+
/**
|
|
155
|
+
* INCR / INCRBY / INCRBYFLOAT plus SADD to the dirty set of the quota snapshot (same round trip)
|
|
156
|
+
*/
|
|
157
|
+
async incrementKey(key, command, amount) {
|
|
158
|
+
return quotaSnapshotService.incrementCounter(this.tdCache, key, command, amount);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* If the Redis counter is missing (e.g. Redis was wiped) seed it from the quota_usages snapshot
|
|
163
|
+
* before it is read or incremented. Memoized per key, never throws.
|
|
164
|
+
*/
|
|
165
|
+
async restoreKeyIfMissing(key) {
|
|
166
|
+
if (quotes_enabled === false) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
await quotaSnapshotService.ensureQuotaKey(this.tdCache, key);
|
|
170
|
+
}
|
|
171
|
+
|
|
126
172
|
|
|
127
173
|
async generateKey(object, type) {
|
|
128
174
|
|
|
@@ -211,6 +257,7 @@ class QuoteManager {
|
|
|
211
257
|
let key = await this.generateKey(object, type);
|
|
212
258
|
winston.verbose("[QuoteManager] getCurrentQuote key: " + key);
|
|
213
259
|
|
|
260
|
+
await this.restoreKeyIfMissing(key);
|
|
214
261
|
let quote = await this.tdCache.get(key);
|
|
215
262
|
return Number(quote);
|
|
216
263
|
}
|
|
@@ -225,7 +272,9 @@ class QuoteManager {
|
|
|
225
272
|
let quotes = {}
|
|
226
273
|
for (let type of typesList) {
|
|
227
274
|
|
|
275
|
+
this.project = project; // the awaits below may interleave with calls for other projects
|
|
228
276
|
let key = await this.generateKey(obj, type);
|
|
277
|
+
await this.restoreKeyIfMissing(key);
|
|
229
278
|
let quote = await this.tdCache.get(key);
|
|
230
279
|
|
|
231
280
|
quotes[type] = {
|
|
@@ -319,6 +368,7 @@ class QuoteManager {
|
|
|
319
368
|
|
|
320
369
|
emailEvent.emit('email.send.quote.checkpoint', data);
|
|
321
370
|
await this.tdCache.set(nKey, 'true', { EX: 2592000 }); //seconds in one month = 2592000
|
|
371
|
+
await quotaSnapshotService.markDirty(this.tdCache, key); // so the flag is backed up
|
|
322
372
|
} else {
|
|
323
373
|
winston.verbose("Quota checkpoint reached email already sent.")
|
|
324
374
|
}
|
|
@@ -345,6 +395,10 @@ class QuoteManager {
|
|
|
345
395
|
let requests_key = await this.generateKey(obj, 'requests');
|
|
346
396
|
let tokens_key = await this.generateKey(obj, 'tokens');
|
|
347
397
|
let email_key = await this.generateKey(obj, 'email');
|
|
398
|
+
let agent_tokens_key = await this.generateKey(obj, 'agent_tokens');
|
|
399
|
+
|
|
400
|
+
// forget the notified thresholds in the mongo backup too, or a Redis restore would bring them back
|
|
401
|
+
quotaSnapshotService.clearNotified([requests_key, tokens_key, email_key, agent_tokens_key]);
|
|
348
402
|
|
|
349
403
|
let checkpoints = ['50', '75', '95', '100']
|
|
350
404
|
|
|
@@ -352,14 +406,17 @@ class QuoteManager {
|
|
|
352
406
|
let nrequests_key = requests_key + ":notify:" + checkpoint;
|
|
353
407
|
let ntokens_key = tokens_key + ":notify:" + checkpoint;
|
|
354
408
|
let nemail_key = email_key + ":notify:" + checkpoint;
|
|
409
|
+
let nagent_tokens_key = agent_tokens_key + ":notify:" + checkpoint;
|
|
355
410
|
|
|
356
411
|
winston.verbose("invalidateCheckpointKeys nrequests_key: " + nrequests_key);
|
|
357
412
|
winston.verbose("invalidateCheckpointKeys ntokens_key: " + ntokens_key);
|
|
358
413
|
winston.verbose("invalidateCheckpointKeys nemail_key: " + nemail_key);
|
|
414
|
+
winston.verbose("invalidateCheckpointKeys nagent_tokens_key: " + nagent_tokens_key);
|
|
359
415
|
|
|
360
416
|
this.tdCache.del(nrequests_key);
|
|
361
417
|
this.tdCache.del(ntokens_key);
|
|
362
418
|
this.tdCache.del(nemail_key);
|
|
419
|
+
this.tdCache.del(nagent_tokens_key);
|
|
363
420
|
|
|
364
421
|
return true;
|
|
365
422
|
})
|
|
@@ -379,6 +436,10 @@ class QuoteManager {
|
|
|
379
436
|
email: {
|
|
380
437
|
quote: quotes.email.quote,
|
|
381
438
|
perc: ((quotes.email.quote / limits['email']) * 100).toFixed(1)
|
|
439
|
+
},
|
|
440
|
+
agent_tokens: {
|
|
441
|
+
quote: quotes.agent_tokens.quote,
|
|
442
|
+
perc: ((quotes.agent_tokens.quote / limits['agent_tokens']) * 100).toFixed(1)
|
|
382
443
|
}
|
|
383
444
|
}
|
|
384
445
|
return quotes_obj
|
|
@@ -603,4 +664,4 @@ class QuoteManager {
|
|
|
603
664
|
|
|
604
665
|
}
|
|
605
666
|
|
|
606
|
-
module.exports = { QuoteManager };
|
|
667
|
+
module.exports = { QuoteManager, PLANS_LIST };
|
package/services/aiService.js
CHANGED
|
@@ -272,7 +272,7 @@ class AiService {
|
|
|
272
272
|
}
|
|
273
273
|
|
|
274
274
|
deleteIndex(data) {
|
|
275
|
-
let base_url = kb_endpoint_train;
|
|
275
|
+
let base_url = process.env.KB_ENDPOINT_TRAIN || kb_endpoint_train;
|
|
276
276
|
winston.debug("[OPENAI SERVICE] kb endpoint: " + base_url);
|
|
277
277
|
|
|
278
278
|
return new Promise((resolve, reject) => {
|