@tiledesk/tiledesk-server 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/CHANGELOG.md +37 -0
  3. package/Dockerfile +3 -2
  4. package/Dockerfile-en +3 -2
  5. package/Dockerfile-jobs +3 -2
  6. package/Dockerfile-profiler +3 -2
  7. package/README.md +15 -0
  8. package/app.js +14 -1
  9. package/bin/quota-reset +68 -0
  10. package/bin/quota-restore +86 -0
  11. package/channels/chat21/chat21Handler.js +50 -34
  12. package/channels/chat21/chat21JoinTracker.js +43 -0
  13. package/config/database.js +5 -5
  14. package/config/native_mcp/native_mcp_servers.json +1 -0
  15. package/controllers/chatbot.controller.js +80 -27
  16. package/controllers/intents.controller.js +42 -0
  17. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-chat21-ionic.md +468 -0
  18. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-design-studio.md +1349 -0
  19. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-server.md +1694 -0
  20. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-tybot.md +789 -0
  21. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-server.md +83 -0
  22. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-tybot.md +76 -0
  23. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-chat21-ionic.md +97 -0
  24. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-design-studio.md +72 -0
  25. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-server.md +223 -0
  26. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-tybot.md +121 -0
  27. package/docs/superpowers/plans/2026-09-29-hitl-pre-branch-ports.md +65 -0
  28. package/docs/superpowers/plans/2026-09-30-flow-start-points-design-studio.md +88 -0
  29. package/docs/superpowers/plans/2026-09-30-flow-start-points-server.md +103 -0
  30. package/docs/superpowers/plans/2026-09-30-flow-start-points-tybot.md +43 -0
  31. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-design-studio.md +80 -0
  32. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-runtime.md +39 -0
  33. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-server.md +106 -0
  34. package/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md +715 -0
  35. package/docs/superpowers/specs/2026-09-30-flow-start-points-design.md +156 -0
  36. package/docs/superpowers/specs/2026-10-01-scheduled-start-point-design.md +303 -0
  37. package/docs/superpowers/todo/2026-09-14-webhook-chatbot-shared-code-fixes.md +191 -0
  38. package/event/botEvent.js +12 -1
  39. package/event/emailEvent.js +3 -0
  40. package/event/messageEvent.js +44 -0
  41. package/jobs.js +10 -0
  42. package/jobsManager.js +19 -0
  43. package/middleware/errorHandler.js +6 -2
  44. package/models/agentPrompt.js +104 -0
  45. package/models/analyticResult.js +1 -1
  46. package/models/channelConstants.js +3 -2
  47. package/models/quotaUsage.js +71 -0
  48. package/models/request.js +6 -0
  49. package/models/requestSnapshot.js +12 -0
  50. package/models/routerLogger.js +1 -1
  51. package/models/webhook.js +44 -0
  52. package/package.json +7 -5
  53. package/pubmodules/emailNotification/requestNotification.js +14 -22
  54. package/pubmodules/events/test/eventRoute.js +7 -6
  55. package/pubmodules/rules/conciergeBot.js +9 -4
  56. package/routes/agentPrompts.js +132 -0
  57. package/routes/auth.js +1 -12
  58. package/routes/campaigns.js +0 -13
  59. package/routes/dataTable.js +0 -3
  60. package/routes/faq_kb.js +12 -1
  61. package/routes/messagesRoot.js +0 -1
  62. package/routes/quotes.js +38 -1
  63. package/routes/request.js +81 -5
  64. package/routes/users.js +0 -1
  65. package/routes/webhook.js +160 -8
  66. package/routes/webhooks.js +276 -6
  67. package/routes/widgetLoader.js +6 -3
  68. package/services/QuoteManager.js +77 -16
  69. package/services/aiService.js +1 -1
  70. package/services/chatbotService.js +55 -1
  71. package/services/departmentService.js +60 -43
  72. package/services/dolphinSchedulerClient.js +116 -0
  73. package/services/emailService.js +6 -1
  74. package/services/hitlParticipantListener.js +260 -0
  75. package/services/participantInviteService.js +302 -0
  76. package/services/participantRemoveService.js +79 -0
  77. package/services/quotaSnapshotService.js +1167 -0
  78. package/services/requestService.js +102 -144
  79. package/services/scheduledStart/dolphinBackend.js +246 -0
  80. package/services/scheduledStart/schedulerBackend.js +101 -0
  81. package/services/scheduledStart/schedulerServiceClient.js +41 -0
  82. package/services/scheduledStartPublishListener.js +186 -0
  83. package/services/scheduledStartRules.js +173 -0
  84. package/services/scheduledStartService.js +130 -0
  85. package/services/startPointCleanupListener.js +30 -0
  86. package/services/startPointService.js +158 -0
  87. package/services/testWsService.js +1 -4
  88. package/services/updateRequestSnapshotQueued.js +8 -9
  89. package/services/webhookConversationService.js +185 -0
  90. package/services/webhookSettingsValidator.js +61 -0
  91. package/template/email/checkpointReachedEmail.html +5 -0
  92. package/test/agentPromptsRoute.test.js +154 -0
  93. package/test/agentTokensQuota.js +237 -0
  94. package/test/botEventHitlFallback.js +19 -0
  95. package/test/chat21HandlerJoinWiring.js +59 -0
  96. package/test/dataTablesRoute.test.js +10 -2
  97. package/test/dateUtils.test.js +0 -3
  98. package/test/departmentService.js +8 -12
  99. package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
  100. package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
  101. package/test/faqRoute.js +6 -1
  102. package/test/faqkbRoute.js +14 -10
  103. package/test/faqkbV3Route.js +1 -1
  104. package/test/filepRoute.js +4 -50
  105. package/test/hitlParticipantListener.js +446 -0
  106. package/test/hitlQueueWiring.js +191 -0
  107. package/test/hitlWaitingReset.js +124 -0
  108. package/test/inviteParticipantsRoute.js +492 -0
  109. package/test/kbRoute.js +1 -1
  110. package/test/messageRoute.js +33 -21
  111. package/test/nativemcp.test.js +7 -1
  112. package/test/openaiRoute.js +2 -8
  113. package/test/publishFailLoud.test.js +414 -0
  114. package/test/quotaSnapshots.js +1044 -0
  115. package/test/removeParticipantsRoute.js +186 -0
  116. package/test/requestService.js +24 -16
  117. package/test/requestServiceRemoveParticipant.js +144 -0
  118. package/test/requestVisibility.test.js +186 -0
  119. package/test/scheduledStartFacade.js +184 -0
  120. package/test/scheduledStartHandover.js +95 -0
  121. package/test/scheduledStartPublish.js +320 -0
  122. package/test/scheduledStartRoute.js +163 -0
  123. package/test/scheduledStartRules.js +177 -0
  124. package/test/scheduledStartRun.js +299 -0
  125. package/test/scheduledStartSchedulerBackend.js +116 -0
  126. package/test/scheduledStartService.js +290 -0
  127. package/test/startPointsCleanup.js +69 -0
  128. package/test/startPointsRegression.js +169 -0
  129. package/test/startPointsRoute.js +158 -0
  130. package/test/webhookConversationRoute.js +347 -0
  131. package/test/webhookConversationService.js +261 -0
  132. package/test/webhookConversationTestUtils.js +55 -0
  133. package/utils/aiUtils.js +95 -1
  134. package/utils/intentFieldNames.js +192 -0
  135. package/utils/quotaCliUtil.js +43 -0
  136. package/utils/requestVisibility.js +355 -0
  137. package/websocket/webSocketServer.js +36 -31
  138. /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
package/models/webhook.js CHANGED
@@ -2,6 +2,36 @@ const mongoose = require('mongoose');
2
2
  const { customAlphabet } = require('nanoid');
3
3
  const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz0123456789', 32);
4
4
 
5
+ const StartPointSchema = mongoose.Schema({
6
+ type: {
7
+ type: String,
8
+ enum: ['webhook', 'scheduled'],
9
+ required: true
10
+ },
11
+ block_id: {
12
+ type: String,
13
+ required: true
14
+ },
15
+ enabled: {
16
+ type: Boolean,
17
+ default: true
18
+ },
19
+ mapping: {
20
+ source_name: {
21
+ type: String,
22
+ required: false
23
+ },
24
+ payload: {
25
+ type: mongoose.Schema.Types.Mixed,
26
+ required: false
27
+ }
28
+ },
29
+ schedule: {
30
+ type: mongoose.Schema.Types.Mixed,
31
+ required: false
32
+ }
33
+ }, { _id: false })
34
+
5
35
  const WebhookSchema = mongoose.Schema({
6
36
  webhook_id: {
7
37
  type: String,
@@ -38,6 +68,20 @@ const WebhookSchema = mongoose.Schema({
38
68
  type: Boolean,
39
69
  required: true,
40
70
  default: true
71
+ },
72
+ mode: {
73
+ type: String,
74
+ enum: ['conversation'],
75
+ required: false
76
+ },
77
+ start_points: {
78
+ type: [StartPointSchema],
79
+ default: undefined
80
+ },
81
+ // Server-owned snapshot of the schedule active in the scheduler; written only by the publish sync.
82
+ scheduled_live: {
83
+ type: mongoose.Schema.Types.Mixed,
84
+ required: false
41
85
  }
42
86
  }, {
43
87
  timestamps: true
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-server",
3
3
  "description": "The Tiledesk server module",
4
- "version": "3.0.0",
4
+ "version": "3.1.0",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
+ "quota-restore": "node ./bin/quota-restore",
8
+ "quota-reset": "node ./bin/quota-reset",
7
9
  "pretest": "mongodb-runner start",
8
10
  "watchenv": "nodemon --watch .env --watch ./confenv/.env ./bin/www",
9
11
  "test": "mocha ./test/*.js ./pubmodules/**/test/*.js ./modules/**/test/*.js ./channels/*/test/*.js ./node_modules/@tiledesk-ent/**/test/*.js --exit",
@@ -22,8 +24,8 @@
22
24
  "url": "git+https://github.com/Tiledesk/tiledesk-server.git"
23
25
  },
24
26
  "engines": {
25
- "node": "16.17.0",
26
- "npm": "8.15.0"
27
+ "node": "24.22.1",
28
+ "npm": "11.19.1"
27
29
  },
28
30
  "bin": {
29
31
  "tiledesk-server": "./bin/www"
@@ -49,7 +51,7 @@
49
51
  "@tiledesk/tiledesk-rasa-connector": "^1.0.10",
50
52
  "@tiledesk/tiledesk-sms-connector": "^0.1.13",
51
53
  "@tiledesk/tiledesk-telegram-connector": "^0.1.14",
52
- "@tiledesk/tiledesk-tybot-connector": "^2.3.0",
54
+ "@tiledesk/tiledesk-tybot-connector": "^3.0.2",
53
55
  "@tiledesk/tiledesk-voice-twilio-connector": "^0.3.2",
54
56
  "@tiledesk/tiledesk-vxml-connector": "^0.1.91",
55
57
  "@tiledesk/tiledesk-whatsapp-connector": "1.0.31",
@@ -114,7 +116,7 @@
114
116
  "request-promise": "^4.2.6",
115
117
  "retry-request": "^4.2.2",
116
118
  "serve-favicon": "~2.5.0",
117
- "sharp": "^0.27.2",
119
+ "sharp": "^0.35.5",
118
120
  "sitemapper": "^3.2.24",
119
121
  "stripe": "^7.2.0",
120
122
  "uniqid": "^5.4.0",
@@ -14,6 +14,7 @@ var winston = require('../../config/winston');
14
14
  var RoleConstants = require("../../models/roleConstants");
15
15
  var ChannelConstants = require("../../models/channelConstants");
16
16
  var cacheUtil = require('../../utils/cacheUtil');
17
+ var requestVisibility = require('../../utils/requestVisibility');
17
18
  const { TdCache } = require('../../utils/TdCache');
18
19
 
19
20
  const messageEvent = require('../../event/messageEvent');
@@ -572,12 +573,13 @@ sendToAgentEmailChannelEmail(projectid, message) {
572
573
 
573
574
  // winston.info("savedRequest.snapshot.agents", savedRequest.snapshot.agents);
574
575
  // agents è selected false quindi nn va sicuro
575
- if (!snapshotAgents.snapshot.agents) {
576
- return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
576
+ var allAgents = await requestVisibility.loadNotifiableProjectUsers(
577
+ snapshotAgents && snapshotAgents.snapshot,
578
+ savedRequest.id_project
579
+ );
580
+ if (!allAgents || allAgents.length === 0) {
581
+ return winston.warn("RequestNotification snapshot audience is empty. You are closing an old request?", savedRequest);
577
582
  }
578
-
579
- // var allAgents = savedRequest.agents;
580
- var allAgents = snapshotAgents.snapshot.agents;
581
583
  // winston.debug("allAgents", allAgents);
582
584
 
583
585
  allAgents.forEach(project_user => {
@@ -937,30 +939,20 @@ sendAgentEmail(projectid, savedRequest) {
937
939
 
938
940
  var snapshotAgents = savedRequest; //riassegno varibile cosi nn cambio righe successive
939
941
 
940
-
941
-
942
-
943
- // winston.info("savedRequest.snapshot.agents", savedRequest.snapshot.agents);
944
- // agents è selected false quindi nn va sicuro
945
- if (!snapshotAgents.snapshot.agents) {
946
- //return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
947
-
942
+ if (!requestVisibility.hasAudience(snapshotAgents.snapshot)) {
948
943
  // agents già c'è in quanto viene creato con departmentService.getOperator nella request.create ma nn c'è per request.participants.update
949
944
  snapshotAgents = await Request.findById(savedRequest.id).select({"snapshot":1}).exec();
950
945
  winston.debug('load snapshotAgents with Request.findById ');
951
946
  }
952
947
  winston.debug('snapshotAgents', snapshotAgents);
953
948
 
954
- if (!snapshotAgents.snapshot.agents) {
955
- return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
949
+ var allAgents = await requestVisibility.loadNotifiableProjectUsers(
950
+ snapshotAgents && snapshotAgents.snapshot,
951
+ savedRequest.id_project
952
+ );
953
+ if (!allAgents || allAgents.length === 0) {
954
+ return winston.warn("RequestNotification snapshot audience is empty. You are closing an old request?", savedRequest);
956
955
  }
957
-
958
- // var allAgents = savedRequest.agents;
959
- var allAgents = snapshotAgents.snapshot.agents;
960
-
961
- // // var allAgents = savedRequest.agents;
962
- // var allAgents = savedRequest.snapshot.agents;
963
- // // winston.debug("allAgents", allAgents);
964
956
 
965
957
  allAgents.forEach(project_user => {
966
958
  // winston.debug("project_user", project_user); //DON'T UNCOMMENT THIS. OTHERWISE this.agents.filter of models/request.js:availableAgentsCount has .filter not found.
@@ -2,7 +2,7 @@
2
2
  process.env.NODE_ENV = 'test';
3
3
 
4
4
  var User = require('../../../models/user');
5
- var Project_user = require('../../../models/project_user');
5
+ var Event = require('../event');
6
6
 
7
7
  var projectService = require('../../../services/projectService');
8
8
  var userService = require('../../../services/userService');
@@ -51,11 +51,12 @@ describe('EventRoute', () => {
51
51
  expect(res.body.name).to.equal("event1");
52
52
  expect(res.body.attributes.attr1).to.equal("val1");
53
53
 
54
- Project_user.findOne({ id_user: savedUser.id, status: "active"} )
55
- .populate('events')
56
- .exec(function (err, project_user) {
57
- if (log) { console.log("project_user", project_user.toJSON()); }
58
- expect(project_user.events.length).to.equal(1);
54
+ Event.find({ name: "event1", id_project: savedProject._id.toString() })
55
+ .exec(function (err, events) {
56
+ if (err) { console.error("err: ", err); }
57
+ if (log) { console.log("events", events); }
58
+ expect(events).to.have.lengthOf(1);
59
+ expect(events[0].attributes.attr1).to.equal("val1");
59
60
  done();
60
61
  });
61
62
 
@@ -11,6 +11,7 @@ var i8nUtil = require("../../utils/i8nUtil");
11
11
  var BotFromParticipant = require("../../utils/botFromParticipant");
12
12
  var assignmentContextUtil = require('../../utils/assignmentContextUtil');
13
13
  const RequestConstants = require('../../models/requestConstants');
14
+ var ChannelConstants = require("../../models/channelConstants");
14
15
 
15
16
  class ConciergeBot {
16
17
 
@@ -142,18 +143,22 @@ devi mandare un messaggio welcome tu altrimenti il bot inserito successivamente
142
143
  winston.debug("ConciergeBot member: " + member);
143
144
  // botprefix
144
145
  if (member.indexOf("bot_")==-1) {
146
+ if (request.channel && request.channel.name === ChannelConstants.WEBHOOK) {
147
+ winston.debug("ConciergeBot: keeping the bot in the request with request_id: " + request.request_id + " because the conversation channel is webhook");
148
+ } else {
145
149
  var botId = BotFromParticipant.getBotFromParticipants(request.participants);
146
150
  if (botId) {
147
151
  winston.verbose("ConciergeBot: removing bot with id: " + botId + " from the request with request_id: " + request.request_id + " from the project with id: " + request.id_project);
148
-
152
+
149
153
  // botprefix
150
- // removeParticipantByRequestId(request_id, id_project, member)
154
+ // removeParticipantByRequestId(request_id, id_project, member)
151
155
  requestService.removeParticipantByRequestId(request.request_id, request.id_project,"bot_"+botId ).catch((err) => {
152
156
  winston.error("(ConciergeBot) removeParticipantByRequestId error", err)
153
157
  });
154
- }
158
+ }
159
+ }
155
160
  }
156
-
161
+
157
162
 
158
163
  });
159
164
  });
@@ -0,0 +1,132 @@
1
+ const express = require('express');
2
+ const router = express.Router();
3
+ const mongoose = require('mongoose');
4
+ const winston = require('../config/winston');
5
+ const AgentPrompt = require('../models/agentPrompt');
6
+ const Faq_kb = require('../models/faq_kb');
7
+
8
+ const {
9
+ AGENT_PROMPT_SOURCES, AGENT_PROMPT_MAX_LENGTH, AGENT_PROMPT_MAX_ATTACHMENTS,
10
+ AGENT_PROMPT_ORIGIN_KINDS, AGENT_PROMPT_ORIGIN_URL_MAX_LENGTH
11
+ } = AgentPrompt;
12
+ const DEFAULT_LIMIT = 50;
13
+ const MAX_LIMIT = 100;
14
+
15
+ // Keeps only the known fields of each attachment; null when the list is not valid.
16
+ function readAttachments(attachments) {
17
+ if (attachments === undefined || attachments === null) {
18
+ return [];
19
+ }
20
+ if (!Array.isArray(attachments) || attachments.length > AGENT_PROMPT_MAX_ATTACHMENTS) {
21
+ return null;
22
+ }
23
+ const result = [];
24
+ for (const a of attachments) {
25
+ if (!a || typeof a.name !== 'string' || !a.name.trim() || !isHttpUrl(a.url)) {
26
+ return null;
27
+ }
28
+ result.push({
29
+ name: a.name.trim(),
30
+ type: typeof a.type === 'string' ? a.type : undefined,
31
+ size: typeof a.size === 'number' ? a.size : undefined,
32
+ url: a.url
33
+ });
34
+ }
35
+ return result;
36
+ }
37
+
38
+ function isHttpUrl(value) {
39
+ return typeof value === 'string' && /^https?:\/\/\S+$/i.test(value);
40
+ }
41
+
42
+ // Where the prompt was written: missing means the dashboard; a web page needs its address.
43
+ // null when not valid.
44
+ function readOrigin(origin) {
45
+ if (origin === undefined || origin === null) {
46
+ return { kind: 'dashboard' };
47
+ }
48
+ if (typeof origin !== 'object' || !AGENT_PROMPT_ORIGIN_KINDS.includes(origin.kind)) {
49
+ return null;
50
+ }
51
+ if (origin.kind === 'dashboard') {
52
+ return { kind: 'dashboard' };
53
+ }
54
+ if (!isHttpUrl(origin.url) || origin.url.length > AGENT_PROMPT_ORIGIN_URL_MAX_LENGTH) {
55
+ return null;
56
+ }
57
+ return { kind: 'web', url: origin.url };
58
+ }
59
+
60
+ // Saves the prompt an agent was created from. Author, project and date come from the request,
61
+ // never from the body.
62
+ router.post('/', async (req, res) => {
63
+ const body = req.body || {};
64
+ const text = typeof body.text === 'string' ? body.text.trim() : '';
65
+ if (!text) {
66
+ return res.status(400).send({ success: false, error: 'text is required' });
67
+ }
68
+ if (text.length > AGENT_PROMPT_MAX_LENGTH) {
69
+ return res.status(400).send({ success: false, error: 'text must be at most ' + AGENT_PROMPT_MAX_LENGTH + ' characters' });
70
+ }
71
+ if (!AGENT_PROMPT_SOURCES.includes(body.source)) {
72
+ return res.status(400).send({ success: false, error: 'source must be one of ' + AGENT_PROMPT_SOURCES.join(', ') });
73
+ }
74
+ const attachments = readAttachments(body.attachments);
75
+ if (!attachments) {
76
+ return res.status(400).send({ success: false, error: 'attachments must be at most ' + AGENT_PROMPT_MAX_ATTACHMENTS + ' items with a name and an http(s) url' });
77
+ }
78
+ const origin = readOrigin(body.origin);
79
+ if (!origin) {
80
+ return res.status(400).send({ success: false, error: 'origin.kind must be one of ' + AGENT_PROMPT_ORIGIN_KINDS.join(', ') + '; a web origin needs an http(s) url of at most ' + AGENT_PROMPT_ORIGIN_URL_MAX_LENGTH + ' characters' });
81
+ }
82
+ if (!body.id_faq_kb || !mongoose.Types.ObjectId.isValid(body.id_faq_kb)) {
83
+ return res.status(400).send({ success: false, error: 'id_faq_kb is required' });
84
+ }
85
+
86
+ try {
87
+ const chatbot = await Faq_kb.findOne({ _id: body.id_faq_kb, id_project: req.projectid }).select('_id').lean();
88
+ if (!chatbot) {
89
+ return res.status(404).send({ success: false, error: 'Chatbot not found in this project' });
90
+ }
91
+
92
+ const saved = await new AgentPrompt({
93
+ id_project: req.projectid,
94
+ id_faq_kb: body.id_faq_kb,
95
+ createdBy: req.user.id || req.user._id,
96
+ text: text,
97
+ language: typeof body.language === 'string' ? body.language : undefined,
98
+ source: body.source,
99
+ origin: origin,
100
+ attachments: attachments
101
+ }).save();
102
+
103
+ winston.debug('AgentPrompt saved ' + saved._id + ' for chatbot ' + body.id_faq_kb);
104
+ res.status(200).send(saved);
105
+ } catch (err) {
106
+ winston.error('Error saving the agent prompt: ', err);
107
+ res.status(500).send({ success: false, error: 'Error saving the agent prompt' });
108
+ }
109
+ });
110
+
111
+ // The prompts of the project, newest first; ?id_faq_kb= narrows them to one agent.
112
+ router.get('/', async (req, res) => {
113
+ const page = Math.max(parseInt(req.query.page, 10) || 0, 0);
114
+ const limit = Math.min(Math.max(parseInt(req.query.limit, 10) || DEFAULT_LIMIT, 1), MAX_LIMIT);
115
+ const query = { id_project: req.projectid };
116
+ if (req.query.id_faq_kb) {
117
+ query.id_faq_kb = String(req.query.id_faq_kb);
118
+ }
119
+
120
+ try {
121
+ const [prompts, count] = await Promise.all([
122
+ AgentPrompt.find(query).sort({ createdAt: -1 }).skip(page * limit).limit(limit).lean(),
123
+ AgentPrompt.countDocuments(query)
124
+ ]);
125
+ res.status(200).send({ count: count, perPage: limit, page: page, prompts: prompts });
126
+ } catch (err) {
127
+ winston.error('Error finding the agent prompts: ', err);
128
+ res.status(500).send({ success: false, error: 'Error finding the agent prompts' });
129
+ }
130
+ });
131
+
132
+ module.exports = router;
package/routes/auth.js CHANGED
@@ -27,7 +27,6 @@ var UserUtil = require('../utils/userUtil');
27
27
 
28
28
  let configSecret = process.env.GLOBAL_SECRET || config.secret;
29
29
  var pKey = process.env.GLOBAL_SECRET_OR_PRIVATE_KEY;
30
- // console.log("pKey",pKey);
31
30
 
32
31
  if (pKey) {
33
32
  configSecret = pKey.replace(/\\n/g, '\n');
@@ -713,10 +712,6 @@ router.get("/google", function(req,res,next){
713
712
  )(req,res,next);
714
713
  });
715
714
 
716
- // router.get("/google/callbacks", passport.authenticate("google", { session: false }), (req, res) => {
717
- // console.log("callback_signup");
718
- // res.redirect("/google/callback");
719
- // });
720
715
 
721
716
  // Retrieve user data using the access token received</em>
722
717
  router.get("/google/callback", passport.authenticate("google", { session: false }), (req, res) => {
@@ -865,12 +860,6 @@ router.get(
865
860
  );
866
861
 
867
862
 
868
- // profile route after successful sign in</em>
869
- // router.get("/profile", (req, res) => {
870
- // console.log(req);
871
- // res.send("Welcome");
872
- // });
873
-
874
863
  // VERIFY EMAIL
875
864
  router.put('/verifyemail/:userid/:code', async function (req, res) {
876
865
 
@@ -887,7 +876,7 @@ router.put('/verifyemail/:userid/:code', async function (req, res) {
887
876
  let redis_client = req.app.get('redis_client');
888
877
  let key = "emailverify:verify-" + verify_email_code;
889
878
  let value = await redis_client.get(key);
890
- console.log("(Auth) verify value: ", value);
879
+
891
880
  if (!value) {
892
881
  return res.status(401).send({ success: false, error: "Unable to verify email: the verification code is expired or invalid.", error_code: errorCodes.AUTH.ERRORS.VERIFICATION_CODE_EXPIRED})
893
882
  }
@@ -240,19 +240,6 @@ router.post('/directDEPRECATED?', async function (req, res) {
240
240
  //TODO RESTORE IT
241
241
  const cursor = Lead.find(queryLead).cursor();
242
242
 
243
-
244
-
245
- // if (doc!= null) {
246
- // doc = await cursor.next()
247
- // }
248
-
249
- // cursor.next(function(error, doc) {
250
- // console.log(doc);
251
-
252
- // setTimout()
253
- // });
254
-
255
-
256
243
  function sleep(ms) {
257
244
  return new Promise((resolve) => {
258
245
  setTimeout(resolve, ms);
@@ -148,8 +148,6 @@ function getRowsListParams(req) {
148
148
  }
149
149
 
150
150
  router.get('/:id/rows/list', async function (req, res) {
151
-
152
- console.log("List rows")
153
151
  try {
154
152
  const rows = await dataTableService.listRowsWithFilter(
155
153
  req.projectid,
@@ -164,7 +162,6 @@ router.get('/:id/rows/list', async function (req, res) {
164
162
  });
165
163
 
166
164
  router.post('/:id/row/insert', async function (req, res) {
167
- console.log("Insert row")
168
165
  try {
169
166
  const row = await dataTableService.insertRowByBody(req.projectid, req.params.id, req.body);
170
167
  if (!row) return res.status(404).send({ success: false, error: 'Table not found' });
package/routes/faq_kb.js CHANGED
@@ -285,6 +285,13 @@ router.put('/:faq_kbid/publish', roleChecker.hasRole('admin'), async (req, res)
285
285
  return res.status(200).send({ message: "Chatbot published successfully", bot_id: published.bot_id });
286
286
  } catch (e) {
287
287
  winston.error("Error Unable publish chatbot: ", e);
288
+ if (e instanceof AppError) {
289
+ const body = { success: false, message: e.message };
290
+ if (Array.isArray(e.errors)) {
291
+ body.errors = e.errors;
292
+ }
293
+ return res.status(e.status).send(body);
294
+ }
288
295
  return res.status(500).send({ success: false, message: "Unable publish chatbot" });
289
296
  }
290
297
  });
@@ -331,7 +338,11 @@ router.put('/:faq_kbid/publish/multi', roleChecker.hasRole('admin'), asyncHandle
331
338
  } catch (err) {
332
339
  const error = err?.response?.data?.message || err?.response?.data?.error || err?.message || "Publish failed";
333
340
  winston.error("Error publishing chatbot " + id_faq_kb + ": ", err);
334
- results.push({ id: id_faq_kb, success: false, error });
341
+ const result = { id: id_faq_kb, success: false, error };
342
+ if (Array.isArray(err?.errors)) {
343
+ result.errors = err.errors;
344
+ }
345
+ results.push(result);
335
346
  }
336
347
  }
337
348
 
@@ -126,7 +126,6 @@ router.get('/csv', roleChecker.hasRoleOrTypes('owner'), function(req, res) {
126
126
 
127
127
  res.flushHeaders();
128
128
 
129
- console.log("fastCsv",fastCsv)
130
129
  var csvStream = fastCsv.format({headers: true})//.transform(transformer)
131
130
  // var csvStream = fastCsv.createWriteStream({headers: true}).transform(transformer)
132
131
  cursor.stream().pipe(csvStream).pipe(res);
package/routes/quotes.js CHANGED
@@ -2,7 +2,7 @@ var express = require('express');
2
2
  var router = express.Router();
3
3
  const { QuoteManager } = require('../services/QuoteManager');
4
4
  let winston = require('../config/winston');
5
- const { MODELS_MULTIPLIER } = require('../utils/aiUtils');
5
+ const { MODELS_MULTIPLIER, computeAgentTokens } = require('../utils/aiUtils');
6
6
 
7
7
 
8
8
  router.post('/', async (req, res) => {
@@ -44,6 +44,14 @@ router.post('/incr/:type', async (req, res) => {
44
44
 
45
45
  let quoteManager = req.app.get('quote_manager');
46
46
 
47
+ if (type === 'agent_tokens') {
48
+ return incrAgentTokens(req, res, quoteManager);
49
+ }
50
+
51
+ if (type !== 'tokens') {
52
+ return res.status(400).send({ success: false, error: "Unsupported quote type for incr: " + type });
53
+ }
54
+
47
55
  let modelKey;
48
56
  if (typeof data.model === 'string') {
49
57
  modelKey = data.model;
@@ -65,4 +73,33 @@ router.post('/incr/:type', async (req, res) => {
65
73
  res.status(200).send({ message: "value incremented for key " + incremented_key, key: incremented_key, currentQuote: quote });
66
74
  })
67
75
 
76
+ async function incrAgentTokens(req, res, quoteManager) {
77
+
78
+ let data = req.body;
79
+ let result = computeAgentTokens(data);
80
+ if (result.error) {
81
+ winston.warn("(quotes) invalid agent_tokens incr body: " + result.error);
82
+ return res.status(400).send({ success: false, error: result.error });
83
+ }
84
+
85
+ if (!Number.isFinite(result.counted)) {
86
+ return res.status(400).send({ success: false, error: "Invalid usage: counted agent tokens are not finite" });
87
+ }
88
+
89
+ let obj = {
90
+ createdAt: new Date(),
91
+ counted: result.counted,
92
+ tokens: data.tokens,
93
+ model: data.model,
94
+ source: data.source
95
+ };
96
+
97
+ winston.verbose("(quotes) incr agent_tokens mode " + result.mode + " counted " + result.counted + " model " + data.model + " source " + data.source);
98
+
99
+ let incremented_key = await quoteManager.incrementAgentTokenCount(req.project, obj);
100
+ let quote = await quoteManager.getCurrentQuote(req.project, obj, 'agent_tokens');
101
+
102
+ res.status(200).send({ message: "value incremented for key " + incremented_key, key: incremented_key, currentQuote: quote, counted: result.counted });
103
+ }
104
+
68
105
  module.exports = router;