@tiledesk/tiledesk-server 2.29.0 → 3.0.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.
@@ -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/faq.js CHANGED
@@ -655,9 +655,16 @@ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot', 'subscription']), fu
655
655
  // console.log("result: ", result);
656
656
  // })
657
657
 
658
- return Faq.find(query)
659
- .skip(skip).limit(limit)
660
- .populate({ path: 'faq_kb' })
658
+ // Every block carries `faq_kb`, the whole bot read again from the database (virtual of the
659
+ // model). A caller that already has the bot, such as the Design Studio V3, asks for the blocks
660
+ // without it (`populate=false`): a list of blocks then weighs the blocks alone, not the bot
661
+ // repeated in every row. Without the parameter, or with any other value, nothing changes.
662
+ let faqQuery = Faq.find(query)
663
+ .skip(skip).limit(limit);
664
+ if (req.query.populate !== 'false') {
665
+ faqQuery = faqQuery.populate({ path: 'faq_kb' });
666
+ }
667
+ return faqQuery
661
668
  .lean()
662
669
  .exec(function (err, faqs) {
663
670
 
package/routes/faq_kb.js CHANGED
@@ -23,6 +23,7 @@ const chatbotController = require('../controllers/chatbot.controller');
23
23
  const intentsController = require('../controllers/intents.controller');
24
24
  const AppError = require('../utils/AppError');
25
25
  const asyncHandler = require('../utils/asyncHandler');
26
+ const v3FlowService = require('../services/v3FlowService');
26
27
 
27
28
  let chatbot_templates_api_url = process.env.CHATBOT_TEMPLATES_API_URL
28
29
 
@@ -457,12 +458,81 @@ router.put('/:faq_kbid/language/:language', roleChecker.hasRoleOrTypes('admin',
457
458
 
458
459
  })
459
460
 
461
+ /**
462
+ * V3 agents: the connectors list, read and written as a whole.
463
+ * `GET` answers the list (or null for an agent that has none).
464
+ * `PUT` replaces it: the body is the list (`{ version: 1, items: [...] }`), validated in shape; an
465
+ * `If-Match` header (or an `if_match` query parameter) carrying the `updatedAt` the client last saw turns a save on top of a newer
466
+ * list into a 409 that returns the current one, so two editors cannot overwrite each other blindly.
467
+ * The agent is marked as modified (it has to be published again) and the engine's cache is cleared.
468
+ * Only a V3 agent has a list: for any other agent the PUT is refused and nothing changes.
469
+ */
470
+ router.get('/:faq_kbid/connectors', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), asyncHandler(async (req, res) => {
471
+ const faq_kb = await Faq_kb.findOne({ _id: req.params.faq_kbid, id_project: req.projectid }).select('+connectors').exec();
472
+ if (!faq_kb) {
473
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + req.params.faq_kbid + " for project " + req.projectid });
474
+ }
475
+ return res.json(faq_kb.connectors === undefined ? null : faq_kb.connectors);
476
+ }));
477
+
478
+ router.put('/:faq_kbid/connectors', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), asyncHandler(async (req, res) => {
479
+ const chatbot_id = req.params.faq_kbid;
480
+ const id_project = req.projectid;
481
+ const collection = req.body;
482
+
483
+ const errors = v3FlowService.validateConnectors(collection);
484
+ if (errors.length > 0) {
485
+ return res.status(400).send({ success: false, msg: "Invalid connectors list", errors: errors });
486
+ }
487
+
488
+ const faq_kb = await Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }).select('+connectors').exec();
489
+ if (!faq_kb) {
490
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
491
+ }
492
+ if (!v3FlowService.isV3(faq_kb)) {
493
+ return res.status(400).send({ success: false, msg: "The connectors list belongs to V3 agents only (ds_version 'v3')" });
494
+ }
495
+
496
+ // The `updatedAt` the client last saw: `If-Match` header, or `if_match` query parameter for the
497
+ // clients that cannot send a custom header through a CORS gateway.
498
+ const expected = req.get('If-Match') || req.query.if_match;
499
+ const current = faq_kb.connectors && faq_kb.connectors.updatedAt ? new Date(faq_kb.connectors.updatedAt) : null;
500
+ if (expected && current && new Date(expected).getTime() !== current.getTime()) {
501
+ winston.verbose("(faq_kb) connectors of " + chatbot_id + " changed since " + expected + ": conflict");
502
+ return res.status(409).send({ success: false, msg: "The connectors list changed since " + expected, connectors: faq_kb.connectors });
503
+ }
504
+
505
+ faq_kb.connectors = { version: collection.version, updatedAt: new Date(), items: collection.items };
506
+ faq_kb.modified = true;
507
+ faq_kb.markModified('connectors');
508
+ const saved = await faq_kb.save();
509
+ botEvent.emit('faqbot.update', saved);
510
+ winston.debug("(faq_kb) connectors of " + chatbot_id + " saved: " + saved.connectors.items.length + " records");
511
+ return res.json(saved.connectors);
512
+ }));
513
+
460
514
  router.patch('/:faq_kbid/attributes', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), function (req, res) { //TODO add cache_bot_here
461
515
  var data = req.body;
462
516
 
463
517
  const chatbot_id = req.params.faq_kbid;
464
518
  const id_project = req.projectid;
465
519
 
520
+ // The version label and the connectors list are fields of the bot since 15/10/2026, not attributes.
521
+ // A client still sending them the old way (`dsVersion`, `connectors`) gets them promoted: written
522
+ // to the fields, removed from the attributes, exactly as the import does. The Design Studio V3
523
+ // sends them this way on purpose when it repairs an agent saved before that date.
524
+ let promoted = null;
525
+ if (v3FlowService.legacyAttributeKeys(data).length > 0) {
526
+ promoted = { attributes: data };
527
+ v3FlowService.promoteLegacyJson(promoted);
528
+ if (promoted.connectors !== undefined && promoted.connectors !== null) {
529
+ const errors = v3FlowService.validateConnectors(promoted.connectors);
530
+ if (errors.length > 0) {
531
+ return res.status(400).send({ success: false, msg: "Invalid connectors list", errors: errors });
532
+ }
533
+ }
534
+ }
535
+
466
536
  // TODO use service method
467
537
  Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }, function (err, updatedBot) {
468
538
  if (err) {
@@ -487,6 +557,20 @@ router.patch('/:faq_kbid/attributes', roleChecker.hasRoleOrTypes('admin', ['bot'
487
557
  updatedBot.attributes[key] = val;
488
558
  });
489
559
 
560
+ if (promoted) {
561
+ if (promoted.ds_version && !updatedBot.ds_version) {
562
+ updatedBot.ds_version = promoted.ds_version;
563
+ }
564
+ if (promoted.connectors !== undefined && promoted.connectors !== null) {
565
+ updatedBot.connectors = { version: promoted.connectors.version, updatedAt: new Date(), items: promoted.connectors.items };
566
+ updatedBot.markModified('connectors');
567
+ updatedBot.modified = true;
568
+ }
569
+ delete updatedBot.attributes.dsVersion;
570
+ delete updatedBot.attributes.connectors;
571
+ winston.verbose("(faq_kb) attributes of " + chatbot_id + ": dsVersion/connectors promoted to the bot fields");
572
+ }
573
+
490
574
  winston.debug("updatedBot attributes", updatedBot.attributes)
491
575
 
492
576
  // https://stackoverflow.com/questions/24054552/mongoose-not-saving-nested-object
@@ -577,7 +661,8 @@ router.get('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscripti
577
661
  const chatbot_id = req.params.faq_kbid;
578
662
  const id_project = req.projectid;
579
663
 
580
- Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }, function (err, faq_kb) { //TODO add cache_bot_here
664
+ // The single GET is how the Design Studio opens an agent: a V3 agent needs its connectors list here.
665
+ Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }).select('+connectors').exec(function (err, faq_kb) { //TODO add cache_bot_here
581
666
  if (err) {
582
667
  return res.status(500).send({ success: false, msg: 'Error getting object.' });
583
668
  }
@@ -6,6 +6,7 @@ const chatbotTypes = require("../models/chatbotTypes");
6
6
  const chatbotTemplates = require("../models/chatbotTemplates");
7
7
  const templates = require('../template/chatbot');
8
8
  const subagentNotes = require('../template/chatbot/subagent_notes');
9
+ const v3FlowService = require('./v3FlowService');
9
10
 
10
11
  class FaqService {
11
12
 
@@ -79,6 +80,18 @@ class FaqService {
79
80
  throw new Error("parent_id is required for subtype 'subagent'");
80
81
  }
81
82
 
83
+ // A creation body written the old way (`attributes.dsVersion`, `attributes.connectors`) is promoted
84
+ // to the fields, as the import does: a client not yet updated still creates a proper V3 agent.
85
+ v3FlowService.promoteLegacyJson(data);
86
+
87
+ if (data.connectors !== undefined && data.connectors !== null) {
88
+ const errors = v3FlowService.validateConnectors(data.connectors);
89
+ if (errors.length > 0) {
90
+ winston.error('(FaqService) invalid connectors list: ' + errors.join('; '));
91
+ throw new Error('Invalid connectors list: ' + errors.join('; '));
92
+ }
93
+ }
94
+
82
95
  let chatbot = this.createChatbotObject(id_project, id_user, data);
83
96
 
84
97
  let template;
@@ -89,6 +102,22 @@ class FaqService {
89
102
  }
90
103
  }
91
104
 
105
+ let options = {};
106
+ if (data.namespace_id) {
107
+ options.namespace_id = data.namespace_id;
108
+ }
109
+
110
+ // A V3 agent is born in V3 form: the template's blocks lose their V2 destinations and the
111
+ // connectors list is saved with the bot, so nothing has to be repaired on first open.
112
+ // A list given by the caller (an import, a fork) wins: the template is not converted then.
113
+ let templateFaqs = null;
114
+ if (template && v3FlowService.isV3(chatbot) && (data.connectors === undefined || data.connectors === null)) {
115
+ const flow = v3FlowService.toV3Flow(templates[template](options));
116
+ chatbot.connectors = flow.connectors;
117
+ templateFaqs = flow.intents;
118
+ winston.debug('(FaqService) template ' + template + ' in V3 form: ' + flow.connectors.items.length + ' connectors');
119
+ }
120
+
92
121
  let savedChatbot;
93
122
  try {
94
123
  savedChatbot = await chatbot.save();
@@ -98,11 +127,7 @@ class FaqService {
98
127
  }
99
128
 
100
129
  if (template) {
101
- let options = {};
102
- if (data.namespace_id) {
103
- options.namespace_id = data.namespace_id;
104
- }
105
- this.createGreetingsAndOperationalsFaqs(savedChatbot._id, savedChatbot.createdBy, savedChatbot.id_project, template, options);
130
+ this.createGreetingsAndOperationalsFaqs(savedChatbot._id, savedChatbot.createdBy, savedChatbot.id_project, template, options, templateFaqs);
106
131
  }
107
132
 
108
133
  botEvent.emit('faqbot.create', savedChatbot);
@@ -141,7 +166,9 @@ class FaqService {
141
166
  createdBy: id_user,
142
167
  updatedBy: id_user,
143
168
  attributes: data.attributes,
144
- parent_id: data.parent_id
169
+ parent_id: data.parent_id,
170
+ ds_version: v3FlowService.normalizeDsVersion(data.ds_version),
171
+ connectors: data.connectors
145
172
  })
146
173
  }
147
174
 
@@ -162,13 +189,14 @@ class FaqService {
162
189
  return defaultTemplate;
163
190
  }
164
191
 
165
- createGreetingsAndOperationalsFaqs(faq_kb_id, created_by, projectid, template, options) {
192
+ createGreetingsAndOperationalsFaqs(faq_kb_id, created_by, projectid, template, options, faqs) {
166
193
 
167
194
  return new Promise( async (resolve, reject) => {
168
195
 
169
196
  winston.debug('(FaqService) create faqs from template: ' + template);
170
197
 
171
- let faqsArray = templates[template](options);
198
+ // `faqs`: the template's blocks already prepared by the caller (a V3 agent); otherwise the template as it is.
199
+ let faqsArray = faqs || templates[template](options);
172
200
 
173
201
  faqsArray.forEach(faq => {
174
202