@tiledesk/tiledesk-server 2.19.4 → 2.19.5
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/package.json +1 -1
- package/routes/answered.js +243 -264
- package/routes/kb.js +150 -105
- package/routes/unanswered.js +277 -296
- package/services/aiManager.js +28 -19
- package/services/kbQuestionService.js +98 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const { Namespace, AnsweredQuestion, UnansweredQuestion } = require('../models/kb_setting');
|
|
2
|
+
const winston = require('../config/winston');
|
|
3
|
+
|
|
4
|
+
class KbQuestionService {
|
|
5
|
+
|
|
6
|
+
constructor() {}
|
|
7
|
+
|
|
8
|
+
async createAnsweredQuestion(id_project, data) {
|
|
9
|
+
try {
|
|
10
|
+
const { namespace, question, answer, tokens, request_id } = data;
|
|
11
|
+
|
|
12
|
+
if (!namespace || !question || !answer) {
|
|
13
|
+
throw new Error({ status: 422, error: 'Missing required parameters: namespace, question and answer' });
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const isValidNamespace = await this.validateNamespace(id_project, namespace);
|
|
17
|
+
if (!isValidNamespace) {
|
|
18
|
+
throw new Error({ status: 403, error: 'Not allowed. The namespace does not belong to the current project.' });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const answeredQuestion = new AnsweredQuestion({
|
|
22
|
+
id_project,
|
|
23
|
+
namespace,
|
|
24
|
+
question,
|
|
25
|
+
answer,
|
|
26
|
+
tokens,
|
|
27
|
+
request_id,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return await answeredQuestion.save();
|
|
31
|
+
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async createUnansweredQuestion(id_project, data) {
|
|
38
|
+
try {
|
|
39
|
+
const { namespace, question, request_id, sender } = data;
|
|
40
|
+
|
|
41
|
+
if (!namespace || !question) {
|
|
42
|
+
throw new Error({ status: 422, error: 'Missing required parameters: namespace and question' });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const isValidNamespace = await this.validateNamespace(id_project, namespace);
|
|
46
|
+
if (!isValidNamespace) {
|
|
47
|
+
throw new Error({ status: 403, error: 'Not allowed. The namespace does not belong to the current project.' });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const unansweredQuestion = new UnansweredQuestion({
|
|
51
|
+
id_project,
|
|
52
|
+
namespace,
|
|
53
|
+
question,
|
|
54
|
+
request_id,
|
|
55
|
+
sender,
|
|
56
|
+
});
|
|
57
|
+
return await unansweredQuestion.save();
|
|
58
|
+
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async validateNamespace(id_project, namespace_id) {
|
|
65
|
+
try {
|
|
66
|
+
const namespace = await Namespace.findOne({
|
|
67
|
+
id_project: id_project,
|
|
68
|
+
id: namespace_id
|
|
69
|
+
});
|
|
70
|
+
return !!namespace;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async trackKbQaResult(id_project, { namespace, question }, answer, tokens) {
|
|
77
|
+
try {
|
|
78
|
+
if (answer.success === true) {
|
|
79
|
+
await this.createAnsweredQuestion(id_project, {
|
|
80
|
+
namespace,
|
|
81
|
+
question,
|
|
82
|
+
answer: answer.answer,
|
|
83
|
+
tokens,
|
|
84
|
+
});
|
|
85
|
+
} else if (answer.success === false) {
|
|
86
|
+
await this.createUnansweredQuestion(id_project, { namespace, question });
|
|
87
|
+
} else {
|
|
88
|
+
winston.warn('qa answer.success is not true or false: ', answer.success);
|
|
89
|
+
}
|
|
90
|
+
} catch (error) {
|
|
91
|
+
winston.error('qa err: ', error);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const kbQuestionService = new KbQuestionService();
|
|
98
|
+
module.exports = kbQuestionService;
|