@tiledesk/tiledesk-tybot-connector 2.5.0 → 2.6.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/CHANGELOG.md +1 -52
- package/ExtUtil.js +1 -0
- package/TdCache copy.js +242 -0
- package/TiledeskExpression.js +1 -6
- package/TiledeskWhenExpression.js +1 -18
- package/engine/MongodbBotsDataSource.js +1 -3
- package/engine/MongodbIntentsMachine.js +2 -3
- package/engine/TiledeskChatbot.js +4 -76
- package/engine/TiledeskChatbotConst.js +1 -7
- package/engine/mock/MockBotsDataSource.js +6 -11
- package/index.js +36 -132
- package/models/faq_kb.js +0 -11
- package/package.json +1 -1
- package/services/AIController.js +1 -31
- package/services/AIService.js +33 -62
- package/services/IntegrationService.js +1 -4
- package/services/{LLMService.js → KbService.js} +3 -3
- package/services/RequestService.js +2 -23
- package/services/TilebotService.js +3 -19
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +17 -34
- package/tiledeskChatbotPlugs/directives/DirAiCondition.js +51 -18
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +52 -103
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +5 -108
- package/tiledeskChatbotPlugs/directives/DirCallSubagent.js +8 -34
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +11 -36
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +4 -16
- package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -12
- package/tiledeskChatbotPlugs/directives/DirIteration.js +10 -63
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +16 -69
- package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +2 -40
- package/tiledeskChatbotPlugs/directives/DirReply.js +6 -81
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +4 -58
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +0 -8
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +1 -11
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +418 -0
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +2 -5
- package/tiledeskChatbotPlugs/directives/Directives.js +0 -6
- package/utils/HttpUtils.js +17 -99
- package/utils/TiledeskChatbotUtil.js +5 -73
- package/utils/winston.js +2 -80
- package/services/InternalSubAgentService.js +0 -330
- package/tiledeskChatbotPlugs/directives/DirInviteHuman.js +0 -259
- package/tiledeskChatbotPlugs/directives/DirInvokeSubAgent.js +0 -345
- package/tiledeskChatbotPlugs/directives/DirJSONConditionMulti.js +0 -171
- package/tiledeskChatbotPlugs/directives/DirRemoveHuman.js +0 -129
- package/tiledeskChatbotPlugs/directives/DirReturn.js +0 -98
- package/utils/ConnectorsV3.js +0 -292
- package/utils/LlmStream.js +0 -215
- package/utils/SentenceChunker.js +0 -94
- package/utils/StreamSender.js +0 -109
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
const { TiledeskExpression } = require('../TiledeskExpression');
|
|
2
|
-
const { TiledeskWhenExpression } = require('../TiledeskWhenExpression');
|
|
3
2
|
const { Filler } = require('../tiledeskChatbotPlugs/Filler');
|
|
4
3
|
const { TiledeskChatbotConst } = require('../engine/TiledeskChatbotConst');
|
|
5
4
|
const { TiledeskChatbot } = require('../engine/TiledeskChatbot.js');
|
|
@@ -157,53 +156,6 @@ class TiledeskChatbotUtil {
|
|
|
157
156
|
|
|
158
157
|
}
|
|
159
158
|
|
|
160
|
-
/**
|
|
161
|
-
* Decides whether a message guarded by a filter should be shown.
|
|
162
|
-
*
|
|
163
|
-
* The design studio saves two forms of the same filter: the `conditions` tree, and `when`,
|
|
164
|
-
* the same thing written as a formula. The formula is the one to read. It is evaluated by
|
|
165
|
-
* TiledeskWhenExpression, which parses it into a tree and walks it -- no eval, no Function,
|
|
166
|
-
* no vm2, whitelisted functions only -- while the tree is turned back into JavaScript and
|
|
167
|
-
* run in a sandbox, which a security review asked us to move away from.
|
|
168
|
-
*
|
|
169
|
-
* It also happens to be the only form that works: the old evaluator knows 18 operators, the
|
|
170
|
-
* studio offers 38, and the missing ones do not merely fail -- looking one up returns
|
|
171
|
-
* undefined and reading `.applyPattern` off it throws, so the whole filter is abandoned and
|
|
172
|
-
* the message it guarded disappears with nothing said on screen.
|
|
173
|
-
*
|
|
174
|
-
* The fall back to the old path is not politeness: filters saved before `when` was written
|
|
175
|
-
* have no formula, and reading only `when` would switch every one of them off at once.
|
|
176
|
-
*
|
|
177
|
-
* A filter that cannot be evaluated SHOWS its message. `=== false` is what hides it, so both
|
|
178
|
-
* a thrown error and a non-boolean answer leave the content in place -- the same leniency
|
|
179
|
-
* the sandbox had, where a failure returned null. The opposite reading would turn every
|
|
180
|
-
* malformed filter into a message that vanishes, which is the fault being fixed here.
|
|
181
|
-
*/
|
|
182
|
-
static evaluateMessageFilter(jsonCondition, variables) {
|
|
183
|
-
const when = jsonCondition ? jsonCondition.when : null;
|
|
184
|
-
if (typeof when === 'string' && when.trim().length > 0) {
|
|
185
|
-
try {
|
|
186
|
-
return new TiledeskWhenExpression().evaluate(when, variables);
|
|
187
|
-
}
|
|
188
|
-
catch (err) {
|
|
189
|
-
winston.error("(TiledeskChatbotUtil) message filter not evaluated: " + err.message + " - while evaluating the following 'when': '" + when + "'");
|
|
190
|
-
return null;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
// Guarded because building the expression is where the old path breaks: an operator the
|
|
194
|
-
// old evaluator does not know reads `.applyPattern` off undefined and throws, and an
|
|
195
|
-
// unguarded throw here would abandon every remaining message in this reply, not just
|
|
196
|
-
// this one. Filters saved with one of those operators exist in the wild.
|
|
197
|
-
try {
|
|
198
|
-
const expression = TiledeskExpression.JSONGroupToExpression(jsonCondition);
|
|
199
|
-
return new TiledeskExpression().evaluateStaticExpression(expression, variables);
|
|
200
|
-
}
|
|
201
|
-
catch (err) {
|
|
202
|
-
winston.error("(TiledeskChatbotUtil) message filter not evaluated: " + err.message);
|
|
203
|
-
return null;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
159
|
static filterOnVariables(message, variables) {
|
|
208
160
|
if (!variables) {
|
|
209
161
|
return;
|
|
@@ -216,7 +168,8 @@ class TiledeskChatbotUtil {
|
|
|
216
168
|
// if (commands[i].message["lang"] && !(commands[i].message["lang"] === lang)) { // if there is a filter and the filter is false, remove
|
|
217
169
|
const jsonCondition = commands[i].message["_tdJSONCondition"];
|
|
218
170
|
if (jsonCondition) {
|
|
219
|
-
const
|
|
171
|
+
const expression = TiledeskExpression.JSONGroupToExpression(jsonCondition);
|
|
172
|
+
const conditionResult = new TiledeskExpression().evaluateStaticExpression(expression, variables);
|
|
220
173
|
if (conditionResult === false) {
|
|
221
174
|
commands.splice(i, 1);
|
|
222
175
|
if (commands[i-1]) {
|
|
@@ -627,15 +580,14 @@ class TiledeskChatbotUtil {
|
|
|
627
580
|
}
|
|
628
581
|
|
|
629
582
|
const chatbot_name = chatbot.bot.name.trim();
|
|
630
|
-
|
|
631
|
-
if (message && text && message.sender !== "_tdinternal" && !this.isHiddenMessage(message)) {
|
|
583
|
+
if (message && message.text && message.text.trim() !== "" && message.sender !== "_tdinternal" && !this.isHiddenMessage(message)) {
|
|
632
584
|
let transcript = await chatbot.getParameter("transcript");
|
|
633
585
|
const _name_of = name_of(message, chatbot_name);
|
|
634
586
|
if (transcript) {
|
|
635
|
-
transcript = transcript + "\n" + _name_of + text;
|
|
587
|
+
transcript = transcript + "\n" + _name_of + message.text;
|
|
636
588
|
}
|
|
637
589
|
else {
|
|
638
|
-
transcript = _name_of + " " + text;
|
|
590
|
+
transcript = _name_of + " " + message.text;
|
|
639
591
|
}
|
|
640
592
|
await chatbot.addParameter("transcript", transcript);
|
|
641
593
|
}
|
|
@@ -654,22 +606,6 @@ class TiledeskChatbotUtil {
|
|
|
654
606
|
}
|
|
655
607
|
}
|
|
656
608
|
|
|
657
|
-
/**
|
|
658
|
-
* The text a message adds to the transcript: message.text or, when it is empty, the texts of its "message" commands.
|
|
659
|
-
* Voice replies (speech_form & co.) carry what is spoken in attributes.commands with text "": without this the bot's
|
|
660
|
-
* answers never reached the AI Prompt history, and a caller's "sì" to "Vuoi sapere di più?" lost its question.
|
|
661
|
-
*/
|
|
662
|
-
static transcriptText(message) {
|
|
663
|
-
if (message && typeof message.text === "string" && message.text.trim() !== "") {
|
|
664
|
-
return message.text;
|
|
665
|
-
}
|
|
666
|
-
const commands = message && message.attributes && Array.isArray(message.attributes.commands) ? message.attributes.commands : [];
|
|
667
|
-
return commands
|
|
668
|
-
.filter((c) => c && c.type === "message" && c.message && typeof c.message.text === "string" && c.message.text.trim() !== "")
|
|
669
|
-
.map((c) => c.message.text.trim())
|
|
670
|
-
.join(" ");
|
|
671
|
-
}
|
|
672
|
-
|
|
673
609
|
static async clearConversationTranscript(chatbot, callback) {
|
|
674
610
|
await chatbot.addParameter("transcript", "");
|
|
675
611
|
if (callback) {
|
|
@@ -918,10 +854,6 @@ class TiledeskChatbotUtil {
|
|
|
918
854
|
message.attributes.payload = { ...message.attributes.payload, ...message.request.attributes.payload }
|
|
919
855
|
winston.debug("(TiledeskChatbotUtil) Forced Set message.attributes.payload ", message.attributes.payload);
|
|
920
856
|
}
|
|
921
|
-
// --- START TYPE (set by the request creator, e.g. "webhook") ---
|
|
922
|
-
if (message && message.request && message.request.attributes && message.request.attributes.start_type) {
|
|
923
|
-
add("start_type", message.request.attributes.start_type);
|
|
924
|
-
}
|
|
925
857
|
|
|
926
858
|
// --- ATTRIBUTES ---
|
|
927
859
|
if (message.attributes) {
|
package/utils/winston.js
CHANGED
|
@@ -3,83 +3,6 @@ var appRoot = require('app-root-path');
|
|
|
3
3
|
var winston = require('winston');
|
|
4
4
|
var level = process.env.LOG_LEVEL || "info";
|
|
5
5
|
|
|
6
|
-
const SECRET_KEY = /^(authorization|token|x-api-key|apikey|api_key|gptkey|password|secret)$/i;
|
|
7
|
-
const SECRET_TOKEN = /\b(JWT|Bearer)\s+[A-Za-z0-9._~+\/=-]+/g;
|
|
8
|
-
const MAX_DEPTH = 8;
|
|
9
|
-
const MAX_NODES = 5000;
|
|
10
|
-
|
|
11
|
-
function redactString(str) {
|
|
12
|
-
return str.replace(SECRET_TOKEN, '$1 [REDACTED]');
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Returns a redacted COPY of value: the caller's objects are never mutated.
|
|
16
|
-
function redactValue(value, depth, ancestors, budget) {
|
|
17
|
-
if (typeof value === 'string') {
|
|
18
|
-
return redactString(value);
|
|
19
|
-
}
|
|
20
|
-
if (value === null || typeof value !== 'object') {
|
|
21
|
-
return value;
|
|
22
|
-
}
|
|
23
|
-
if (value instanceof Date || Buffer.isBuffer(value)) {
|
|
24
|
-
return value;
|
|
25
|
-
}
|
|
26
|
-
if (ancestors.has(value)) {
|
|
27
|
-
return '[Circular]';
|
|
28
|
-
}
|
|
29
|
-
if (depth >= MAX_DEPTH || budget.nodes-- <= 0) {
|
|
30
|
-
return '[Truncated]';
|
|
31
|
-
}
|
|
32
|
-
ancestors.add(value);
|
|
33
|
-
let out;
|
|
34
|
-
if (Array.isArray(value)) {
|
|
35
|
-
out = value.map((v) => redactValue(v, depth + 1, ancestors, budget));
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
out = {};
|
|
39
|
-
if (value instanceof Error) {
|
|
40
|
-
// message and stack are not enumerable on Error
|
|
41
|
-
out.name = value.name;
|
|
42
|
-
out.message = redactString(String(value.message));
|
|
43
|
-
if (value.stack) {
|
|
44
|
-
out.stack = redactString(String(value.stack));
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
for (const key of Object.keys(value)) {
|
|
48
|
-
if (SECRET_KEY.test(key)) {
|
|
49
|
-
out[key] = '[REDACTED]';
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
let v;
|
|
53
|
-
try { v = value[key]; } catch (e) { v = '[Unreadable]'; }
|
|
54
|
-
out[key] = redactValue(v, depth + 1, ancestors, budget);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
ancestors.delete(value);
|
|
59
|
-
return out;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const redactSecrets = winston.format((info) => {
|
|
63
|
-
const budget = { nodes: MAX_NODES };
|
|
64
|
-
const ancestors = new Set();
|
|
65
|
-
const out = {};
|
|
66
|
-
for (const key of Object.keys(info)) {
|
|
67
|
-
if (SECRET_KEY.test(key)) {
|
|
68
|
-
out[key] = '[REDACTED]';
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
out[key] = redactValue(info[key], 0, ancestors, budget);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// keep winston's internal symbols (LEVEL, ...) and redact the splat args
|
|
75
|
-
for (const sym of Object.getOwnPropertySymbols(info)) {
|
|
76
|
-
out[sym] = sym === Symbol.for('splat')
|
|
77
|
-
? redactValue(info[sym], 0, ancestors, budget)
|
|
78
|
-
: info[sym];
|
|
79
|
-
}
|
|
80
|
-
return out;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
6
|
var options = {
|
|
84
7
|
file: {
|
|
85
8
|
level:level ,
|
|
@@ -89,7 +12,7 @@ var options = {
|
|
|
89
12
|
maxsize: 5242880, // 5MB
|
|
90
13
|
maxFiles: 5,
|
|
91
14
|
colorize: false,
|
|
92
|
-
format: winston.format.
|
|
15
|
+
format: winston.format.simple()
|
|
93
16
|
},
|
|
94
17
|
console: {
|
|
95
18
|
level: level,
|
|
@@ -97,7 +20,7 @@ var options = {
|
|
|
97
20
|
json: true,
|
|
98
21
|
colorize: true,
|
|
99
22
|
// timestamp: true,
|
|
100
|
-
format: winston.format.
|
|
23
|
+
format: winston.format.simple()
|
|
101
24
|
},
|
|
102
25
|
};
|
|
103
26
|
|
|
@@ -117,4 +40,3 @@ logger.stream = {
|
|
|
117
40
|
|
|
118
41
|
|
|
119
42
|
module.exports = logger;
|
|
120
|
-
module.exports.redactSecrets = redactSecrets;
|
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
const { v4: uuidv4 } = require('uuid');
|
|
2
|
-
const { Filler } = require('../tiledeskChatbotPlugs/Filler');
|
|
3
|
-
const winston = require('../utils/winston');
|
|
4
|
-
|
|
5
|
-
const STATUS = {
|
|
6
|
-
STARTED: 'started',
|
|
7
|
-
RUNNING: 'running',
|
|
8
|
-
PROGRESS: 'progress',
|
|
9
|
-
COMPLETED: 'completed',
|
|
10
|
-
FAILED: 'failed',
|
|
11
|
-
TIMEOUT: 'timeout'
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const DEFAULT_TIMEOUT_MS = 120000;
|
|
15
|
-
const DEFAULT_TTL_SECONDS = 86400;
|
|
16
|
-
|
|
17
|
-
class InternalSubAgentService {
|
|
18
|
-
|
|
19
|
-
static agentFromSubBot(subBot, subagentId) {
|
|
20
|
-
if (!subagentId) {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
return {
|
|
24
|
-
key: subagentId,
|
|
25
|
-
id: subBot && subBot._id ? subBot._id : subagentId,
|
|
26
|
-
name: subBot && subBot.name ? subBot.name : subagentId,
|
|
27
|
-
botId: subagentId,
|
|
28
|
-
type: 'internal_flow',
|
|
29
|
-
timeoutMs: subBot && subBot.attributes ? subBot.attributes.subAgentTimeoutMs : undefined
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
static intentCommand(intentName) {
|
|
34
|
-
if (!intentName || typeof intentName !== 'string') {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
const trimmed = intentName.trim();
|
|
38
|
-
if (!trimmed) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
static webhookTopic(requestId) {
|
|
45
|
-
return `/webhooks/${requestId}`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
static runKey(parentRequestId, runId) {
|
|
49
|
-
return `tilebot:requests:${parentRequestId}:subagents:${runId}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
static newRunId() {
|
|
53
|
-
return uuidv4().replace(/-/g, '');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
static buildSubRequestId(projectId, runId) {
|
|
57
|
-
return `automation-request-${projectId}-${runId}`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
static timeoutMs(agent, action) {
|
|
61
|
-
const timeout = action.timeoutMs || action.timeout || (agent && agent.timeoutMs) || DEFAULT_TIMEOUT_MS;
|
|
62
|
-
const asNumber = Number(timeout);
|
|
63
|
-
if (!Number.isFinite(asNumber) || asNumber <= 0) {
|
|
64
|
-
return DEFAULT_TIMEOUT_MS;
|
|
65
|
-
}
|
|
66
|
-
return Math.round(asNumber);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
static ttlSeconds(timeoutMs) {
|
|
70
|
-
return Math.max(DEFAULT_TTL_SECONDS, Math.ceil(timeoutMs / 1000) + 3600);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
static async createRun(tdcache, params) {
|
|
74
|
-
const runId = params.runId || InternalSubAgentService.newRunId();
|
|
75
|
-
const timeoutMs = InternalSubAgentService.timeoutMs(params.agent, params.action || {});
|
|
76
|
-
const now = Date.now();
|
|
77
|
-
const agent = params.agent || {};
|
|
78
|
-
const run = {
|
|
79
|
-
schemaVersion: 1,
|
|
80
|
-
runId: runId,
|
|
81
|
-
parentRequestId: params.parentRequestId,
|
|
82
|
-
subRequestId: InternalSubAgentService.buildSubRequestId(params.projectId, runId),
|
|
83
|
-
projectId: params.projectId,
|
|
84
|
-
parentBotId: params.parentBotId,
|
|
85
|
-
subagentId: params.subagentId || agent.key || agent.id,
|
|
86
|
-
agentKey: agent.key || agent.name || params.subagentId,
|
|
87
|
-
agentName: agent.name,
|
|
88
|
-
type: agent.type || 'internal_flow',
|
|
89
|
-
mode: params.mode || 'fire_and_continue',
|
|
90
|
-
status: STATUS.RUNNING,
|
|
91
|
-
input: params.input || {},
|
|
92
|
-
output: null,
|
|
93
|
-
error: null,
|
|
94
|
-
createdAt: now,
|
|
95
|
-
updatedAt: now,
|
|
96
|
-
expiresAt: now + timeoutMs,
|
|
97
|
-
timeoutMs: timeoutMs
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
await InternalSubAgentService.saveRun(tdcache, run);
|
|
101
|
-
return run;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
static async saveRun(tdcache, run) {
|
|
105
|
-
if (!tdcache) {
|
|
106
|
-
throw new Error('tdcache is mandatory');
|
|
107
|
-
}
|
|
108
|
-
const ttl = InternalSubAgentService.ttlSeconds(run.timeoutMs);
|
|
109
|
-
await tdcache.set(
|
|
110
|
-
InternalSubAgentService.runKey(run.parentRequestId, run.runId),
|
|
111
|
-
JSON.stringify(run),
|
|
112
|
-
{ EX: ttl }
|
|
113
|
-
);
|
|
114
|
-
return run;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
static async getRun(tdcache, parentRequestId, runId) {
|
|
118
|
-
if (!tdcache || !parentRequestId || !runId) {
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const json = await tdcache.get(InternalSubAgentService.runKey(parentRequestId, runId));
|
|
123
|
-
if (!json) {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
try {
|
|
128
|
-
return JSON.parse(json);
|
|
129
|
-
} catch (error) {
|
|
130
|
-
winston.error('(InternalSubAgentService) Error parsing run state: ', error);
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
static async updateRun(tdcache, parentRequestId, runId, patch) {
|
|
136
|
-
const run = await InternalSubAgentService.getRun(tdcache, parentRequestId, runId);
|
|
137
|
-
if (!run) {
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const updated = {
|
|
142
|
-
...run,
|
|
143
|
-
...patch,
|
|
144
|
-
updatedAt: Date.now()
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
await InternalSubAgentService.saveRun(tdcache, updated);
|
|
148
|
-
return updated;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
static isTerminalStatus(status) {
|
|
152
|
-
return status === STATUS.COMPLETED ||
|
|
153
|
-
status === STATUS.FAILED ||
|
|
154
|
-
status === STATUS.TIMEOUT;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
static isWebhookSuccess(status) {
|
|
158
|
-
const code = Number(status);
|
|
159
|
-
return Number.isFinite(code) && code >= 200 && code < 300;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
static fillValue(value, parameters) {
|
|
163
|
-
const filler = new Filler();
|
|
164
|
-
|
|
165
|
-
if (typeof value === 'string') {
|
|
166
|
-
return filler.fill(value, parameters);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (Array.isArray(value)) {
|
|
170
|
-
return value.map((item) => InternalSubAgentService.fillValue(item, parameters));
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (value && typeof value === 'object') {
|
|
174
|
-
return Object.entries(value).reduce((acc, [key, item]) => {
|
|
175
|
-
acc[key] = InternalSubAgentService.fillValue(item, parameters);
|
|
176
|
-
return acc;
|
|
177
|
-
}, {});
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
return value;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
static buildSubAgentMessage(context, run, command) {
|
|
184
|
-
const payload = {
|
|
185
|
-
...(run.input || {}),
|
|
186
|
-
_tdSubAgent: {
|
|
187
|
-
runId: run.runId,
|
|
188
|
-
parentRequestId: run.parentRequestId,
|
|
189
|
-
subRequestId: run.subRequestId,
|
|
190
|
-
agentKey: run.agentKey,
|
|
191
|
-
subagentId: run.subagentId,
|
|
192
|
-
mode: run.mode
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
return {
|
|
197
|
-
payload: {
|
|
198
|
-
_id: uuidv4(),
|
|
199
|
-
senderFullname: '_tdinternal',
|
|
200
|
-
type: 'text',
|
|
201
|
-
sender: '_tdinternal',
|
|
202
|
-
recipient: run.subRequestId,
|
|
203
|
-
text: command,
|
|
204
|
-
id_project: run.projectId,
|
|
205
|
-
attributes: {
|
|
206
|
-
payload: payload
|
|
207
|
-
},
|
|
208
|
-
request: {
|
|
209
|
-
request_id: run.subRequestId,
|
|
210
|
-
id_project: run.projectId,
|
|
211
|
-
draft: context.supportRequest && context.supportRequest.draft,
|
|
212
|
-
attributes: {
|
|
213
|
-
payload: payload
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
},
|
|
217
|
-
token: context.token
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
static buildParentContinuationMessage(context, intentName, event) {
|
|
222
|
-
const intentCommand = InternalSubAgentService.intentCommand(intentName);
|
|
223
|
-
return {
|
|
224
|
-
payload: {
|
|
225
|
-
_id: uuidv4(),
|
|
226
|
-
senderFullname: '_tdinternal',
|
|
227
|
-
type: 'text',
|
|
228
|
-
sender: '_tdinternal',
|
|
229
|
-
recipient: event.parentRequestId,
|
|
230
|
-
text: intentCommand,
|
|
231
|
-
id_project: context.projectId,
|
|
232
|
-
attributes: {
|
|
233
|
-
payload: {
|
|
234
|
-
subAgentRun: event
|
|
235
|
-
}
|
|
236
|
-
},
|
|
237
|
-
request: {
|
|
238
|
-
request_id: event.parentRequestId,
|
|
239
|
-
id_project: context.projectId,
|
|
240
|
-
draft: context.supportRequest && context.supportRequest.draft
|
|
241
|
-
}
|
|
242
|
-
},
|
|
243
|
-
token: context.token
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
static async assignParentResult(tdcache, parentRequestId, action, event) {
|
|
248
|
-
if (!tdcache || !parentRequestId || !action) {
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
const statusTarget = action.assignStatusTo || action.assign_status_to;
|
|
253
|
-
const resultTarget = action.assignResultTo || action.assign_result_to;
|
|
254
|
-
const errorTarget = action.assignErrorTo || action.assign_error_to;
|
|
255
|
-
const runTarget = action.assignRunTo || action.assign_run_to;
|
|
256
|
-
|
|
257
|
-
if (statusTarget) {
|
|
258
|
-
await InternalSubAgentService.addParameter(tdcache, parentRequestId, statusTarget, event.status);
|
|
259
|
-
}
|
|
260
|
-
if (resultTarget && event.output !== undefined) {
|
|
261
|
-
await InternalSubAgentService.addParameter(tdcache, parentRequestId, resultTarget, event.output);
|
|
262
|
-
}
|
|
263
|
-
if (errorTarget && event.error !== undefined) {
|
|
264
|
-
await InternalSubAgentService.addParameter(tdcache, parentRequestId, errorTarget, event.error);
|
|
265
|
-
}
|
|
266
|
-
if (runTarget) {
|
|
267
|
-
await InternalSubAgentService.addParameter(tdcache, parentRequestId, runTarget, event);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
static async addParameter(tdcache, requestId, parameterName, parameterValue) {
|
|
272
|
-
if (!tdcache || parameterName === null || parameterName === undefined) {
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
const parameterKey = `tilebot:requests:${requestId}:parameters`;
|
|
276
|
-
const parameterValueAsString = JSON.stringify(parameterValue);
|
|
277
|
-
if (parameterValueAsString?.length > 20000000) {
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
const ttl = parseInt(process.env.FLOW_ATTRIBUTES_TTL, 10) || (15 * 24 * 60 * 60); // default 15 days
|
|
281
|
-
await tdcache.hset(parameterKey, parameterName, parameterValueAsString, { EX: ttl });
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
static subAgentContext(requestAttributes, context) {
|
|
285
|
-
const fromPayload = requestAttributes?.payload?._tdSubAgent || requestAttributes?._tdSubAgent;
|
|
286
|
-
if (fromPayload?.parentRequestId) {
|
|
287
|
-
return fromPayload;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
const message = context && context.message;
|
|
291
|
-
const supportRequest = context && context.supportRequest;
|
|
292
|
-
const fromMessage = message?.attributes?.payload?._tdSubAgent
|
|
293
|
-
|| message?.request?.attributes?.payload?._tdSubAgent
|
|
294
|
-
|| supportRequest?.attributes?.payload?._tdSubAgent;
|
|
295
|
-
|
|
296
|
-
return fromMessage || null;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
static resolveOutboundRequestId(requestId, requestAttributes, context) {
|
|
300
|
-
const subAgent = InternalSubAgentService.subAgentContext(requestAttributes, context);
|
|
301
|
-
if (subAgent && subAgent.parentRequestId) {
|
|
302
|
-
return subAgent.parentRequestId;
|
|
303
|
-
}
|
|
304
|
-
return requestId;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
static async allParameters(tdcache, requestId) {
|
|
308
|
-
if (!tdcache || !requestId) {
|
|
309
|
-
return {};
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
const parametersKey = `tilebot:requests:${requestId}:parameters`;
|
|
313
|
-
const attributesAsStringMap = await tdcache.hgetall(parametersKey);
|
|
314
|
-
const attributes = {};
|
|
315
|
-
|
|
316
|
-
if (attributesAsStringMap !== null) {
|
|
317
|
-
for (const [key, value] of Object.entries(attributesAsStringMap)) {
|
|
318
|
-
try {
|
|
319
|
-
attributes[key] = JSON.parse(value);
|
|
320
|
-
} catch (error) {
|
|
321
|
-
winston.error('(InternalSubAgentService) Error parsing request parameter: ', error);
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
return attributes;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
module.exports = { InternalSubAgentService, INTERNAL_SUB_AGENT_STATUS: STATUS };
|