@tiledesk/tiledesk-server 3.0.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.
- package/CHANGELOG.md +3 -0
- package/app.js +2 -0
- package/models/agentPrompt.js +104 -0
- package/package.json +1 -1
- package/routes/agentPrompts.js +132 -0
- package/test/agentPromptsRoute.test.js +153 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
🚀 IN PRODUCTION 🚀
|
|
6
6
|
(https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
|
|
7
7
|
|
|
8
|
+
# 3.0.1
|
|
9
|
+
- Introduced the agent prompts service (`/:projectid/agent-prompts`), restricted to project admins. This service stores prompts used to create AI agents, including metadata such as the agent, the author (from the token), the source and context within the dashboard or external pages, and attachment links. It also provides an endpoint to list all prompts for a project, with optional filtering by agent, ordered from newest to oldest.
|
|
8
10
|
|
|
9
11
|
# 3.0.0
|
|
10
12
|
- Introduced the `aiGenerations` module for agent history and restoration of versions created or modified via AI, with retention options and cache invalidation per agent.
|
|
@@ -21,6 +23,7 @@
|
|
|
21
23
|
- No changes for previous/non-V3 agents.
|
|
22
24
|
- Added support for returning the blocks list (`GET /:projectid/faq`) without repeating the bot object in each row via the `populate=false` query param. Default behavior remains unchanged.
|
|
23
25
|
|
|
26
|
+
|
|
24
27
|
# 2.29.0
|
|
25
28
|
- Added support for Subagents
|
|
26
29
|
- Bumped tybot-connector version to 2.3.0
|
package/app.js
CHANGED
|
@@ -160,6 +160,7 @@ var webhooks = require('./routes/webhooks');
|
|
|
160
160
|
var roles = require('./routes/roles');
|
|
161
161
|
var copilot = require('./routes/copilot');
|
|
162
162
|
var mcp = require('./routes/mcp');
|
|
163
|
+
var agentPrompts = require('./routes/agentPrompts');
|
|
163
164
|
|
|
164
165
|
var bootDataLoader = require('./services/bootDataLoader');
|
|
165
166
|
var settingDataLoader = require('./services/settingDataLoader');
|
|
@@ -651,6 +652,7 @@ app.use('/:projectid/quotes', [passport.authenticate(['basic', 'jwt'], { session
|
|
|
651
652
|
app.use('/:projectid/integration', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('admin', ['bot','subscription'])], integration )
|
|
652
653
|
|
|
653
654
|
app.use('/:projectid/mcp', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('admin', ['bot','subscription'])], mcp);
|
|
655
|
+
app.use('/:projectid/agent-prompts', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRole('admin')], agentPrompts);
|
|
654
656
|
|
|
655
657
|
// Disabled: superseded by /kb routes. Kept commented with gptkey stripped from responses.
|
|
656
658
|
// app.use('/:projectid/kbsettings', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], kbsettings);
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const mongoose = require('mongoose');
|
|
2
|
+
const Schema = mongoose.Schema;
|
|
3
|
+
const winston = require('../config/winston');
|
|
4
|
+
|
|
5
|
+
// The prompt an AI agent was created from, kept with the agent, the project and its author.
|
|
6
|
+
// Written by the dashboard right after the agent is created (Home, agents list, website landing).
|
|
7
|
+
|
|
8
|
+
// `source`: where in the dashboard the prompt was sent from. `origin`: where it was written, the
|
|
9
|
+
// dashboard itself or a web page (website, landing page, ...) that sent it to the dashboard.
|
|
10
|
+
const AGENT_PROMPT_SOURCES = ['home', 'website', 'bots_list', 'onboarding'];
|
|
11
|
+
const AGENT_PROMPT_ORIGIN_KINDS = ['dashboard', 'web'];
|
|
12
|
+
const AGENT_PROMPT_ORIGIN_URL_MAX_LENGTH = 2048;
|
|
13
|
+
const AGENT_PROMPT_MAX_LENGTH = 2000;
|
|
14
|
+
const AGENT_PROMPT_MAX_ATTACHMENTS = 5;
|
|
15
|
+
|
|
16
|
+
// Only the link of a file is kept here: the file itself lives in the file storage.
|
|
17
|
+
const AttachmentSchema = new Schema({
|
|
18
|
+
name: {
|
|
19
|
+
type: String,
|
|
20
|
+
required: true
|
|
21
|
+
},
|
|
22
|
+
type: {
|
|
23
|
+
type: String
|
|
24
|
+
},
|
|
25
|
+
size: {
|
|
26
|
+
type: Number
|
|
27
|
+
},
|
|
28
|
+
url: {
|
|
29
|
+
type: String,
|
|
30
|
+
required: true
|
|
31
|
+
}
|
|
32
|
+
}, { _id: false });
|
|
33
|
+
|
|
34
|
+
const OriginSchema = new Schema({
|
|
35
|
+
kind: {
|
|
36
|
+
type: String,
|
|
37
|
+
enum: AGENT_PROMPT_ORIGIN_KINDS,
|
|
38
|
+
default: 'dashboard'
|
|
39
|
+
},
|
|
40
|
+
// The address of the web page; only for kind 'web'.
|
|
41
|
+
url: {
|
|
42
|
+
type: String,
|
|
43
|
+
maxlength: AGENT_PROMPT_ORIGIN_URL_MAX_LENGTH
|
|
44
|
+
}
|
|
45
|
+
}, { _id: false });
|
|
46
|
+
|
|
47
|
+
const AgentPromptSchema = new Schema({
|
|
48
|
+
id_project: {
|
|
49
|
+
type: String,
|
|
50
|
+
required: true,
|
|
51
|
+
index: true
|
|
52
|
+
},
|
|
53
|
+
id_faq_kb: {
|
|
54
|
+
type: String,
|
|
55
|
+
required: true
|
|
56
|
+
},
|
|
57
|
+
createdBy: {
|
|
58
|
+
type: String,
|
|
59
|
+
required: true
|
|
60
|
+
},
|
|
61
|
+
text: {
|
|
62
|
+
type: String,
|
|
63
|
+
required: true,
|
|
64
|
+
maxlength: AGENT_PROMPT_MAX_LENGTH
|
|
65
|
+
},
|
|
66
|
+
language: {
|
|
67
|
+
type: String
|
|
68
|
+
},
|
|
69
|
+
source: {
|
|
70
|
+
type: String,
|
|
71
|
+
enum: AGENT_PROMPT_SOURCES,
|
|
72
|
+
required: true
|
|
73
|
+
},
|
|
74
|
+
origin: {
|
|
75
|
+
type: OriginSchema,
|
|
76
|
+
default: () => ({ kind: 'dashboard' })
|
|
77
|
+
},
|
|
78
|
+
attachments: {
|
|
79
|
+
type: [AttachmentSchema],
|
|
80
|
+
default: []
|
|
81
|
+
}
|
|
82
|
+
}, {
|
|
83
|
+
timestamps: true
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
AgentPromptSchema.index({ id_project: 1, createdAt: -1 });
|
|
87
|
+
AgentPromptSchema.index({ id_project: 1, id_faq_kb: 1 });
|
|
88
|
+
// Read across every project by the Super Admin Console: newest first, and by author.
|
|
89
|
+
AgentPromptSchema.index({ createdAt: -1 });
|
|
90
|
+
AgentPromptSchema.index({ createdBy: 1, createdAt: -1 });
|
|
91
|
+
|
|
92
|
+
const AgentPrompt = mongoose.model('agent_prompt', AgentPromptSchema);
|
|
93
|
+
|
|
94
|
+
if (process.env.MONGOOSE_SYNCINDEX) {
|
|
95
|
+
AgentPrompt.syncIndexes();
|
|
96
|
+
winston.verbose('AgentPrompt syncIndexes');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = AgentPrompt;
|
|
100
|
+
module.exports.AGENT_PROMPT_SOURCES = AGENT_PROMPT_SOURCES;
|
|
101
|
+
module.exports.AGENT_PROMPT_MAX_LENGTH = AGENT_PROMPT_MAX_LENGTH;
|
|
102
|
+
module.exports.AGENT_PROMPT_MAX_ATTACHMENTS = AGENT_PROMPT_MAX_ATTACHMENTS;
|
|
103
|
+
module.exports.AGENT_PROMPT_ORIGIN_KINDS = AGENT_PROMPT_ORIGIN_KINDS;
|
|
104
|
+
module.exports.AGENT_PROMPT_ORIGIN_URL_MAX_LENGTH = AGENT_PROMPT_ORIGIN_URL_MAX_LENGTH;
|
package/package.json
CHANGED
|
@@ -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;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
let chai = require('chai');
|
|
3
|
+
let expect = require('chai').expect;
|
|
4
|
+
|
|
5
|
+
let chaiHttp = require('chai-http');
|
|
6
|
+
let server = require('../app');
|
|
7
|
+
const projectService = require('../services/projectService');
|
|
8
|
+
const userService = require('../services/userService');
|
|
9
|
+
const faqService = require('../services/faqService');
|
|
10
|
+
var RoleConstants = require("../models/roleConstants");
|
|
11
|
+
const Project_user = require('../models/project_user');
|
|
12
|
+
|
|
13
|
+
chai.use(chaiHttp);
|
|
14
|
+
|
|
15
|
+
// Resolves with the response whatever its status, so the tests can check 4xx answers too.
|
|
16
|
+
function call(request) {
|
|
17
|
+
return new Promise((resolve) => request.end((err, res) => resolve(res)));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function newOwnerWithAgent(label) {
|
|
21
|
+
const email = "test-signup-" + Date.now() + "-" + Math.round(Math.random() * 1e6) + "@email.com";
|
|
22
|
+
const pwd = "pwd";
|
|
23
|
+
const user = await userService.signup(email, pwd, "Test Firstname", "Test lastname");
|
|
24
|
+
const project = await projectService.create("test-agent-prompts-" + label, user._id);
|
|
25
|
+
const bot = await faqService.create(project._id, user._id, { name: "agent-prompts-bot-" + label });
|
|
26
|
+
return { email, pwd, user, project, bot };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe('Agent Prompts Route', () => {
|
|
30
|
+
|
|
31
|
+
it('saves the prompt with its agent, author and project', async () => {
|
|
32
|
+
const { email, pwd, user, project, bot } = await newOwnerWithAgent('save');
|
|
33
|
+
|
|
34
|
+
const res = await call(chai.request(server)
|
|
35
|
+
.post('/' + project._id + '/agent-prompts')
|
|
36
|
+
.auth(email, pwd)
|
|
37
|
+
.send({
|
|
38
|
+
id_faq_kb: bot._id.toString(),
|
|
39
|
+
text: ' Build an agent that books appointments ',
|
|
40
|
+
language: 'it',
|
|
41
|
+
source: 'home',
|
|
42
|
+
createdBy: 'someone-else',
|
|
43
|
+
id_project: 'another-project'
|
|
44
|
+
}));
|
|
45
|
+
|
|
46
|
+
expect(res.status).to.equal(200);
|
|
47
|
+
expect(res.body.text).to.equal('Build an agent that books appointments');
|
|
48
|
+
expect(res.body.id_faq_kb).to.equal(bot._id.toString());
|
|
49
|
+
expect(res.body.id_project).to.equal(project._id.toString());
|
|
50
|
+
expect(res.body.createdBy).to.equal(user._id.toString());
|
|
51
|
+
expect(res.body.language).to.equal('it');
|
|
52
|
+
expect(res.body.source).to.equal('home');
|
|
53
|
+
expect(res.body.origin).to.deep.equal({ kind: 'dashboard' });
|
|
54
|
+
expect(res.body.attachments).to.be.an('array').that.is.empty;
|
|
55
|
+
expect(res.body.createdAt).to.be.a('string');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('keeps the web page the prompt was written on', async () => {
|
|
59
|
+
const { email, pwd, project, bot } = await newOwnerWithAgent('origin');
|
|
60
|
+
|
|
61
|
+
const res = await call(chai.request(server)
|
|
62
|
+
.post('/' + project._id + '/agent-prompts')
|
|
63
|
+
.auth(email, pwd)
|
|
64
|
+
.send({
|
|
65
|
+
id_faq_kb: bot._id.toString(),
|
|
66
|
+
text: 'A prompt from a landing page',
|
|
67
|
+
source: 'website',
|
|
68
|
+
origin: { kind: 'web', url: 'https://tiledesk.com/ai-agents?utm_source=hero', extra: 'dropped' }
|
|
69
|
+
}));
|
|
70
|
+
|
|
71
|
+
expect(res.status).to.equal(200);
|
|
72
|
+
expect(res.body.origin).to.deep.equal({ kind: 'web', url: 'https://tiledesk.com/ai-agents?utm_source=hero' });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('keeps the links of the attachments', async () => {
|
|
76
|
+
const { email, pwd, project, bot } = await newOwnerWithAgent('attachments');
|
|
77
|
+
|
|
78
|
+
const res = await call(chai.request(server)
|
|
79
|
+
.post('/' + project._id + '/agent-prompts')
|
|
80
|
+
.auth(email, pwd)
|
|
81
|
+
.send({
|
|
82
|
+
id_faq_kb: bot._id.toString(),
|
|
83
|
+
text: 'Answer from this price list',
|
|
84
|
+
source: 'website',
|
|
85
|
+
attachments: [{ name: 'prices.pdf', type: 'application/pdf', size: 1024, url: 'https://files.test/prices.pdf', extra: 'dropped' }]
|
|
86
|
+
}));
|
|
87
|
+
|
|
88
|
+
expect(res.status).to.equal(200);
|
|
89
|
+
expect(res.body.attachments).to.deep.equal([{ name: 'prices.pdf', type: 'application/pdf', size: 1024, url: 'https://files.test/prices.pdf' }]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('refuses a missing text, an unknown source, invalid attachments and an invalid origin', async () => {
|
|
93
|
+
const { email, pwd, project, bot } = await newOwnerWithAgent('invalid');
|
|
94
|
+
const post = (body) => call(chai.request(server).post('/' + project._id + '/agent-prompts').auth(email, pwd).send(body));
|
|
95
|
+
const valid = { id_faq_kb: bot._id.toString(), text: 'A prompt', source: 'home' };
|
|
96
|
+
const file = { name: 'f.txt', url: 'https://files.test/f.txt' };
|
|
97
|
+
|
|
98
|
+
expect((await post({ ...valid, text: ' ' })).status).to.equal(400);
|
|
99
|
+
expect((await post({ ...valid, text: 'x'.repeat(2001) })).status).to.equal(400);
|
|
100
|
+
expect((await post({ ...valid, source: 'somewhere' })).status).to.equal(400);
|
|
101
|
+
expect((await post({ ...valid, id_faq_kb: 'not-an-id' })).status).to.equal(400);
|
|
102
|
+
expect((await post({ ...valid, attachments: [file, file, file, file, file, file] })).status).to.equal(400);
|
|
103
|
+
expect((await post({ ...valid, attachments: [{ name: 'f.txt', url: 'javascript:alert(1)' }] })).status).to.equal(400);
|
|
104
|
+
expect((await post({ ...valid, origin: { kind: 'elsewhere' } })).status).to.equal(400);
|
|
105
|
+
expect((await post({ ...valid, origin: { kind: 'web' } })).status).to.equal(400);
|
|
106
|
+
expect((await post({ ...valid, origin: { kind: 'web', url: 'javascript:alert(1)' } })).status).to.equal(400);
|
|
107
|
+
expect((await post({ ...valid, origin: { kind: 'web', url: 'https://x.test/' + 'a'.repeat(2048) } })).status).to.equal(400);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('refuses an agent of another project', async () => {
|
|
111
|
+
const mine = await newOwnerWithAgent('mine');
|
|
112
|
+
const other = await newOwnerWithAgent('other');
|
|
113
|
+
|
|
114
|
+
const res = await call(chai.request(server)
|
|
115
|
+
.post('/' + mine.project._id + '/agent-prompts')
|
|
116
|
+
.auth(mine.email, mine.pwd)
|
|
117
|
+
.send({ id_faq_kb: other.bot._id.toString(), text: 'A prompt', source: 'home' }));
|
|
118
|
+
|
|
119
|
+
expect(res.status).to.equal(404);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('refuses a user with the agent role and an anonymous request', async () => {
|
|
123
|
+
const { email, pwd, user, project, bot } = await newOwnerWithAgent('roles');
|
|
124
|
+
await Project_user.findOneAndUpdate({ id_project: project._id, id_user: user._id }, { role: RoleConstants.AGENT });
|
|
125
|
+
const body = { id_faq_kb: bot._id.toString(), text: 'A prompt', source: 'home' };
|
|
126
|
+
|
|
127
|
+
const asAgent = await call(chai.request(server).post('/' + project._id + '/agent-prompts').auth(email, pwd).send(body));
|
|
128
|
+
expect(asAgent.status).to.equal(403);
|
|
129
|
+
|
|
130
|
+
const anonymous = await call(chai.request(server).post('/' + project._id + '/agent-prompts').send(body));
|
|
131
|
+
expect(anonymous.status).to.equal(401);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('lists the prompts of the project, newest first, also for one agent', async () => {
|
|
135
|
+
const { email, pwd, user, project, bot } = await newOwnerWithAgent('list');
|
|
136
|
+
const secondBot = await faqService.create(project._id, user._id, { name: "agent-prompts-bot-list-2" });
|
|
137
|
+
const post = (id_faq_kb, text) => call(chai.request(server).post('/' + project._id + '/agent-prompts').auth(email, pwd)
|
|
138
|
+
.send({ id_faq_kb: id_faq_kb.toString(), text: text, source: 'bots_list' }));
|
|
139
|
+
|
|
140
|
+
await post(bot._id, 'first');
|
|
141
|
+
await post(secondBot._id, 'second');
|
|
142
|
+
await post(bot._id, 'third');
|
|
143
|
+
|
|
144
|
+
const all = await call(chai.request(server).get('/' + project._id + '/agent-prompts').auth(email, pwd));
|
|
145
|
+
expect(all.status).to.equal(200);
|
|
146
|
+
expect(all.body.count).to.equal(3);
|
|
147
|
+
expect(all.body.prompts.map(p => p.text)).to.deep.equal(['third', 'second', 'first']);
|
|
148
|
+
|
|
149
|
+
const oneAgent = await call(chai.request(server).get('/' + project._id + '/agent-prompts?id_faq_kb=' + bot._id).auth(email, pwd));
|
|
150
|
+
expect(oneAgent.body.count).to.equal(2);
|
|
151
|
+
expect(oneAgent.body.prompts.map(p => p.text)).to.deep.equal(['third', 'first']);
|
|
152
|
+
});
|
|
153
|
+
});
|