@tiledesk/tiledesk-server 3.3.4 → 3.5.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 +20 -1
- package/app.js +7 -0
- package/docs/activities-client.md +27 -0
- package/docs/mcp-oauth2-it.md +279 -0
- package/docs/mcp-oauth2.md +279 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server-it.md +155 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server.md +155 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot-it.md +63 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot.md +63 -0
- package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design-it.md +207 -0
- package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design.md +207 -0
- package/jobs.js +15 -0
- package/models/mcpOAuthConnection.js +131 -0
- package/package.json +2 -2
- package/pubmodules/activities/activityMessageUtil.js +137 -0
- package/pubmodules/activities/models/activity.js +2 -0
- package/pubmodules/activities/routes/activity.js +30 -1
- package/pubmodules/activities/test/execution-filter.test.js +133 -0
- package/pubmodules/activities/test/execution-messages.test.js +158 -0
- package/pubmodules/executions-publisher/index.js +457 -0
- package/pubmodules/executions-publisher/test/audit-consumer.test.js +384 -0
- package/pubmodules/executions-publisher/test/executions-publisher.test.js +204 -0
- package/pubmodules/pubModulesManager.js +12 -0
- package/routes/executions-refire.js +148 -0
- package/routes/mcp.js +11 -8
- package/routes/mcpOAuth.js +133 -0
- package/routes/mcpOAuthCallback.js +75 -0
- package/routes/request.js +5 -0
- package/routes/webhook.js +120 -54
- package/services/mcpOAuthDiscovery.js +248 -0
- package/services/mcpOAuthGoogleScopes.js +55 -0
- package/services/mcpOAuthService.js +732 -0
- package/services/mcpService.js +111 -15
- package/services/requestService.js +4 -2
- package/services/webhookService.js +4 -2
- package/test/executions-refire.test.js +362 -0
- package/test/mcpOAuthDiscovery.test.js +331 -0
- package/test/mcpOAuthGoogleScopes.test.js +25 -0
- package/test/mcpOAuthRoute.test.js +455 -0
- package/test/mcpOAuthService.test.js +693 -0
- package/test/mcpOAuthUtils.test.js +348 -0
- package/test/mcpServiceOAuth.test.js +267 -0
- package/test/requestRoute.js +57 -0
- package/test/webhookConversationRoute.js +51 -0
- package/utils/logRedactor.js +4 -2
- package/utils/mcpOAuthCrypto.js +69 -0
- package/utils/mcpOAuthPkce.js +20 -0
- package/utils/outboundUrlGuard.js +131 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
|
|
3
|
+
const { expect } = require('chai');
|
|
4
|
+
const util = require('../activityMessageUtil');
|
|
5
|
+
|
|
6
|
+
function act(verb, reason) {
|
|
7
|
+
return {
|
|
8
|
+
verb: verb,
|
|
9
|
+
actor: { type: 'user', id: 'u1', name: 'Mario' },
|
|
10
|
+
target: { type: 'execution', id: 'abcdef123456', object: { agent_name: 'Support Bot' } },
|
|
11
|
+
actionObj: reason ? { reason: reason } : {}
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('execution activity messages', function () {
|
|
16
|
+
it('sentences en/it for each verb', function () {
|
|
17
|
+
expect(util.buildDefaultActivityMessage(act('EXECUTION_STOP_SOFT')))
|
|
18
|
+
.to.equal('Mario paused execution 123456 of Support Bot after the current action');
|
|
19
|
+
expect(util.buildDefaultActivityMessage(act('EXECUTION_STOP_HARD')))
|
|
20
|
+
.to.equal('Mario paused execution 123456 of Support Bot now');
|
|
21
|
+
expect(util.buildDefaultActivityMessage(act('EXECUTION_DESTROY')))
|
|
22
|
+
.to.equal('Mario destroyed execution 123456 of Support Bot');
|
|
23
|
+
expect(util.buildExecutionMessage(act('EXECUTION_STOP_SOFT'), 'it'))
|
|
24
|
+
.to.equal('Mario ha messo in pausa l\'esecuzione 123456 di Support Bot dopo l\'azione corrente');
|
|
25
|
+
expect(util.buildExecutionMessage(act('EXECUTION_STOP_HARD'), 'it'))
|
|
26
|
+
.to.equal('Mario ha messo in pausa l\'esecuzione 123456 di Support Bot subito');
|
|
27
|
+
expect(util.buildExecutionMessage(act('EXECUTION_DESTROY'), 'it'))
|
|
28
|
+
.to.equal('Mario ha eliminato l\'esecuzione 123456 di Support Bot');
|
|
29
|
+
expect(util.buildExecutionMessage(act('EXECUTION_DESTROY'), 'en'))
|
|
30
|
+
.to.equal('Mario destroyed execution 123456 of Support Bot');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('the short id is the random tail of a uuid v7 (last 6 hex chars without dashes), not its timestamp prefix', function () {
|
|
34
|
+
const a = act('EXECUTION_DESTROY');
|
|
35
|
+
a.target.id = '01a1c193-4e44-73c1-91d5-373a5c5d8888';
|
|
36
|
+
expect(util.buildDefaultActivityMessage(a)).to.equal('Mario destroyed execution 5d8888 of Support Bot');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('appends the reason when present', function () {
|
|
40
|
+
expect(util.buildDefaultActivityMessage(act('EXECUTION_DESTROY', 'cleanup')))
|
|
41
|
+
.to.equal('Mario destroyed execution 123456 of Support Bot · "cleanup"');
|
|
42
|
+
expect(util.buildExecutionMessage(act('EXECUTION_STOP_SOFT', 'x'), 'it'))
|
|
43
|
+
.to.equal('Mario ha messo in pausa l\'esecuzione 123456 di Support Bot dopo l\'azione corrente · "x"');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('a rejected control reads as an attempt, with the reason it was refused (en/it)', function () {
|
|
47
|
+
const rej = (verb, result, reason) => Object.assign(act(verb, reason), { actionObj: Object.assign({ result: result }, reason ? { reason: reason } : {}) });
|
|
48
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_STOP_HARD', 'rejected:stopped')))
|
|
49
|
+
.to.equal('Mario tried to pause execution 123456 of Support Bot now (rejected: paused)');
|
|
50
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_STOP_SOFT', 'rejected:completed')))
|
|
51
|
+
.to.equal('Mario tried to pause execution 123456 of Support Bot after the current action (rejected: completed)');
|
|
52
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_DESTROY', 'rejected:destroyed', 'again')))
|
|
53
|
+
.to.equal('Mario tried to destroy execution 123456 of Support Bot (rejected: destroyed) · "again"');
|
|
54
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_DESTROY', 'rejected')))
|
|
55
|
+
.to.equal('Mario tried to destroy execution 123456 of Support Bot (rejected)');
|
|
56
|
+
expect(util.buildExecutionMessage(rej('EXECUTION_STOP_HARD', 'rejected:stopped'), 'it'))
|
|
57
|
+
.to.equal('Mario ha provato a mettere in pausa l\'esecuzione 123456 di Support Bot subito (rifiutato: paused)');
|
|
58
|
+
expect(util.buildExecutionMessage(rej('EXECUTION_STOP_SOFT', 'rejected:completed'), 'it'))
|
|
59
|
+
.to.equal('Mario ha provato a mettere in pausa l\'esecuzione 123456 di Support Bot dopo l\'azione corrente (rifiutato: completed)');
|
|
60
|
+
expect(util.buildExecutionMessage(rej('EXECUTION_DESTROY', 'rejected:destroyed'), 'it'))
|
|
61
|
+
.to.equal('Mario ha provato a eliminare l\'esecuzione 123456 di Support Bot (rifiutato: destroyed)');
|
|
62
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_DESTROY', 'rejected:stopping')))
|
|
63
|
+
.to.equal('Mario tried to destroy execution 123456 of Support Bot (rejected: pausing)');
|
|
64
|
+
// accepted (or no result, as older rows): the plain sentence
|
|
65
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_STOP_HARD', 'accepted')))
|
|
66
|
+
.to.equal('Mario paused execution 123456 of Support Bot now');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('audit resume/start_new map to the new verbs and sentences en/it', function () {
|
|
70
|
+
expect(util.buildDefaultActivityMessage(act('EXECUTION_RESUME')))
|
|
71
|
+
.to.equal('Mario resumed execution 123456 of Support Bot');
|
|
72
|
+
expect(util.buildDefaultActivityMessage(act('EXECUTION_START_NEW')))
|
|
73
|
+
.to.equal('Mario started a new run of execution 123456 of Support Bot');
|
|
74
|
+
expect(util.buildExecutionMessage(act('EXECUTION_RESUME'), 'it'))
|
|
75
|
+
.to.equal('Mario ha ripreso l\'esecuzione 123456 di Support Bot');
|
|
76
|
+
expect(util.buildExecutionMessage(act('EXECUTION_START_NEW'), 'it'))
|
|
77
|
+
.to.equal('Mario ha avviato una nuova esecuzione da 123456 di Support Bot');
|
|
78
|
+
expect(util.buildExecutionMessage(act('EXECUTION_START_NEW', 'retry'), 'en'))
|
|
79
|
+
.to.equal('Mario started a new run of execution 123456 of Support Bot · "retry"');
|
|
80
|
+
|
|
81
|
+
const rej = (verb, result) => Object.assign(act(verb), { actionObj: { result: result } });
|
|
82
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_RESUME', 'rejected:not_resumable')))
|
|
83
|
+
.to.equal('Mario tried to resume execution 123456 of Support Bot (rejected: not_resumable)');
|
|
84
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_START_NEW', 'rejected:already_started')))
|
|
85
|
+
.to.equal('Mario tried to start a new run of execution 123456 of Support Bot (rejected: already_started)');
|
|
86
|
+
expect(util.buildExecutionMessage(rej('EXECUTION_RESUME', 'rejected:not_resumable'), 'it'))
|
|
87
|
+
.to.equal('Mario ha provato a riprendere l\'esecuzione 123456 di Support Bot (rifiutato: not_resumable)');
|
|
88
|
+
expect(util.buildExecutionMessage(rej('EXECUTION_START_NEW', 'rejected:already_started'), 'it'))
|
|
89
|
+
.to.equal('Mario ha provato ad avviare una nuova esecuzione da 123456 di Support Bot (rifiutato: already_started)');
|
|
90
|
+
|
|
91
|
+
// an unconfirmed refire may have started the run: it reads as started, flagged
|
|
92
|
+
expect(util.buildDefaultActivityMessage(rej('EXECUTION_START_NEW', 'rejected:refire_unconfirmed')))
|
|
93
|
+
.to.equal('Mario started a new run of execution 123456 of Support Bot (unconfirmed)');
|
|
94
|
+
expect(util.buildExecutionMessage(rej('EXECUTION_START_NEW', 'rejected:refire_unconfirmed'), 'it'))
|
|
95
|
+
.to.equal('Mario ha avviato una nuova esecuzione da 123456 di Support Bot (non confermata)');
|
|
96
|
+
// other verbs keep the tried phrasing
|
|
97
|
+
expect(util.buildExecutionMessage(rej('EXECUTION_RESUME', 'rejected:refire_unconfirmed'), 'en'))
|
|
98
|
+
.to.equal('Mario tried to resume execution 123456 of Support Bot (rejected: refire_unconfirmed)');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('data access and retention', function () {
|
|
102
|
+
const da = (verb, actionObj) => Object.assign(act(verb), { actionObj: actionObj });
|
|
103
|
+
const ret = (before, after) => ({
|
|
104
|
+
verb: 'EXECUTION_RETENTION_UPDATE',
|
|
105
|
+
actor: { type: 'user', id: 'u1', name: 'Mario' },
|
|
106
|
+
target: { type: 'project', id: 'p1', object: {} },
|
|
107
|
+
actionObj: { result: 'accepted', retention: { before: before, after: after } }
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('EXECUTION_DATA_ACCESS: reason + one block or whole execution (en/it)', function () {
|
|
111
|
+
const step = da('EXECUTION_DATA_ACCESS', { result: 'accepted', reason: 'debug', scope: { type: 'step', step_seq: 3 } });
|
|
112
|
+
const whole = da('EXECUTION_DATA_ACCESS', { result: 'accepted', reason: 'debug', scope: { type: 'execution' } });
|
|
113
|
+
expect(util.buildDefaultActivityMessage(step))
|
|
114
|
+
.to.equal('Mario viewed the data of execution 123456 of Support Bot · "debug" · 1 block');
|
|
115
|
+
expect(util.buildDefaultActivityMessage(whole))
|
|
116
|
+
.to.equal('Mario viewed the data of execution 123456 of Support Bot · "debug" · whole execution');
|
|
117
|
+
expect(util.buildExecutionMessage(step, 'it'))
|
|
118
|
+
.to.equal('Mario ha visualizzato i dati dell\'esecuzione 123456 di Support Bot · "debug" · 1 blocco');
|
|
119
|
+
expect(util.buildExecutionMessage(whole, 'it'))
|
|
120
|
+
.to.equal('Mario ha visualizzato i dati dell\'esecuzione 123456 di Support Bot · "debug" · intera esecuzione');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('EXECUTION_DATA_ACCESS_DENIED carries the role (en/it)', function () {
|
|
124
|
+
const a = da('EXECUTION_DATA_ACCESS_DENIED', { result: 'rejected:forbidden', role: 'agent' });
|
|
125
|
+
expect(util.buildDefaultActivityMessage(a))
|
|
126
|
+
.to.equal('Mario was denied access to the data of execution 123456 of Support Bot (role agent)');
|
|
127
|
+
expect(util.buildExecutionMessage(a, 'it'))
|
|
128
|
+
.to.equal('Mario non ha ottenuto l\'accesso ai dati dell\'esecuzione 123456 di Support Bot (ruolo agent)');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('EXECUTION_STOP_NOTICE_UPDATE states the setting after the change (en/it)', function () {
|
|
132
|
+
const sn = (after) => ({ verb: 'EXECUTION_STOP_NOTICE_UPDATE', actor: { type: 'user', id: 'u1', name: 'Mario' },
|
|
133
|
+
target: { type: 'project', id: 'p1', object: {} }, actionObj: { result: 'accepted', stop_notice: { before: { enabled: true, text: null }, after } } });
|
|
134
|
+
expect(util.buildDefaultActivityMessage(sn({ enabled: false, text: null })))
|
|
135
|
+
.to.equal('Mario changed the executions pause notice: off');
|
|
136
|
+
expect(util.buildDefaultActivityMessage(sn({ enabled: true, text: 'Back soon' })))
|
|
137
|
+
.to.equal('Mario changed the executions pause notice: on, custom text');
|
|
138
|
+
expect(util.buildExecutionMessage(sn({ enabled: true, text: null }), 'it'))
|
|
139
|
+
.to.equal("Mario ha modificato l'avviso di esecuzione in pausa: attivo, testo predefinito");
|
|
140
|
+
expect(util.buildDefaultActivityMessage(Object.assign(sn({}), { actionObj: {} })))
|
|
141
|
+
.to.equal('Mario changed the executions pause notice');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('EXECUTION_RETENTION_UPDATE shows only the changed parts (en/it)', function () {
|
|
145
|
+
const both = ret({ trace_days: 90, data_days: 30 }, { trace_days: 30, data_days: 7 });
|
|
146
|
+
expect(util.buildDefaultActivityMessage(both))
|
|
147
|
+
.to.equal('Mario changed executions retention: trace 90 → 30 days, data 30 → 7 days');
|
|
148
|
+
expect(util.buildExecutionMessage(both, 'it'))
|
|
149
|
+
.to.equal('Mario ha modificato la conservazione delle esecuzioni: tracce 90 → 30 giorni, dati 30 → 7 giorni');
|
|
150
|
+
const onlyTrace = ret({ trace_days: 90, data_days: 30 }, { trace_days: 60, data_days: 30 });
|
|
151
|
+
expect(util.buildDefaultActivityMessage(onlyTrace))
|
|
152
|
+
.to.equal('Mario changed executions retention: trace 90 → 60 days');
|
|
153
|
+
const onlyData = ret({ trace_days: 90, data_days: 30 }, { trace_days: 90, data_days: 0 });
|
|
154
|
+
expect(util.buildExecutionMessage(onlyData, 'it'))
|
|
155
|
+
.to.equal('Mario ha modificato la conservazione delle esecuzioni: dati 30 → 0 giorni');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* executions-publisher pubmodule
|
|
5
|
+
*
|
|
6
|
+
* Publishes the conversation end events of agent execution traceability
|
|
7
|
+
* (conversation.closed / bot_left / bot_replaced) on the AMQP topic exchange
|
|
8
|
+
* `tiledesk.executions`, routing key `exec.<id_project>.<type>`.
|
|
9
|
+
*
|
|
10
|
+
* Fire-and-forget: nothing is awaited by the event listeners and no error
|
|
11
|
+
* propagates to the emitter. Fully disabled (no listeners registered) when
|
|
12
|
+
* EXECUTIONS_AMQP_URL is unset.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
var requestEvent = require("../../event/requestEvent");
|
|
16
|
+
var winston = require("../../config/winston");
|
|
17
|
+
|
|
18
|
+
var EXCHANGE = "tiledesk.executions";
|
|
19
|
+
var PUBLISH_TIMEOUT_MS = 10000;
|
|
20
|
+
var CONNECT_TIMEOUT_MS = 10000;
|
|
21
|
+
var connectTimeoutOverride = null;
|
|
22
|
+
var BOT_PREFIX = "bot_";
|
|
23
|
+
var SUBAGENT_REASONS = ["subagent_call", "subagent_return"];
|
|
24
|
+
|
|
25
|
+
// ─── transport ──────────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
var transportOverride = null;
|
|
28
|
+
var channelPromise = null;
|
|
29
|
+
var currentConnection = null;
|
|
30
|
+
|
|
31
|
+
var AUDIT_QUEUE = "server.executions.audit";
|
|
32
|
+
var AUDIT_BINDING = "audit.#";
|
|
33
|
+
var AUDIT_PREFETCH = 20;
|
|
34
|
+
var AUDIT_RETRY_MS = 5000;
|
|
35
|
+
var nackDelayMs = 1000;
|
|
36
|
+
var auditChannelFactoryOverride = null;
|
|
37
|
+
var auditRetryTimer = null;
|
|
38
|
+
|
|
39
|
+
function resetChannel() {
|
|
40
|
+
channelPromise = null;
|
|
41
|
+
currentConnection = null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Connects within the timeout (socket and handshake); a connection that arrives later is closed.
|
|
45
|
+
function connectWithTimeout(amqp, url) {
|
|
46
|
+
var ms = connectTimeoutOverride || CONNECT_TIMEOUT_MS;
|
|
47
|
+
return new Promise(function (resolve, reject) {
|
|
48
|
+
var timedOut = false;
|
|
49
|
+
var timer = setTimeout(function () {
|
|
50
|
+
timedOut = true;
|
|
51
|
+
reject(new Error("amqp connect timeout after " + ms + " ms"));
|
|
52
|
+
}, ms);
|
|
53
|
+
Promise.resolve(amqp.connect(url, { timeout: ms })).then(function (conn) {
|
|
54
|
+
if (timedOut) { conn.close().catch(function () {}); return; }
|
|
55
|
+
clearTimeout(timer);
|
|
56
|
+
resolve(conn);
|
|
57
|
+
}, function (err) {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
if (!timedOut) reject(err);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Lazy connect; after an error (or a connect timeout) the next publish reconnects.
|
|
65
|
+
function getChannel() {
|
|
66
|
+
if (channelPromise) return channelPromise;
|
|
67
|
+
var amqp = require("amqplib");
|
|
68
|
+
var connection;
|
|
69
|
+
var p = connectWithTimeout(amqp, process.env.EXECUTIONS_AMQP_URL)
|
|
70
|
+
.then(function (conn) {
|
|
71
|
+
connection = conn;
|
|
72
|
+
currentConnection = conn;
|
|
73
|
+
conn.on("error", function (err) {
|
|
74
|
+
winston.error("executions-publisher AMQP connection error", err);
|
|
75
|
+
if (channelPromise === p) resetChannel();
|
|
76
|
+
});
|
|
77
|
+
conn.on("close", function () {
|
|
78
|
+
if (channelPromise === p) resetChannel();
|
|
79
|
+
});
|
|
80
|
+
return conn.createConfirmChannel();
|
|
81
|
+
})
|
|
82
|
+
.then(function (ch) {
|
|
83
|
+
ch.on("error", function (err) {
|
|
84
|
+
winston.error("executions-publisher AMQP channel error", err);
|
|
85
|
+
if (channelPromise === p) resetChannel();
|
|
86
|
+
});
|
|
87
|
+
return ch.assertExchange(EXCHANGE, "topic", { durable: true }).then(function () {
|
|
88
|
+
return ch;
|
|
89
|
+
});
|
|
90
|
+
})
|
|
91
|
+
.catch(function (err) {
|
|
92
|
+
if (channelPromise === p) resetChannel();
|
|
93
|
+
if (connection) connection.close().catch(function () {});
|
|
94
|
+
throw err;
|
|
95
|
+
});
|
|
96
|
+
channelPromise = p;
|
|
97
|
+
return p;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
var amqpTransport = {
|
|
101
|
+
publish: function (routingKey, buffer) {
|
|
102
|
+
return getChannel().then(function (ch) {
|
|
103
|
+
return new Promise(function (resolve, reject) {
|
|
104
|
+
var settled = false;
|
|
105
|
+
var timer = setTimeout(function () {
|
|
106
|
+
if (settled) return;
|
|
107
|
+
settled = true;
|
|
108
|
+
reject(new Error("publish confirm timeout"));
|
|
109
|
+
}, PUBLISH_TIMEOUT_MS);
|
|
110
|
+
function done(err) {
|
|
111
|
+
if (settled) return;
|
|
112
|
+
settled = true;
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
if (err) reject(err); else resolve();
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
ch.publish(EXCHANGE, routingKey, buffer,
|
|
118
|
+
{ persistent: true, contentType: "application/json" }, done);
|
|
119
|
+
} catch (e) {
|
|
120
|
+
done(e);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
function send(routingKey, envelope) {
|
|
128
|
+
try {
|
|
129
|
+
var transport = transportOverride || amqpTransport;
|
|
130
|
+
var result = transport.publish(routingKey, Buffer.from(JSON.stringify(envelope)));
|
|
131
|
+
if (result && typeof result.catch === "function") {
|
|
132
|
+
result.catch(function (err) {
|
|
133
|
+
winston.error("executions-publisher publish error " + routingKey, err);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
} catch (err) {
|
|
137
|
+
winston.error("executions-publisher publish error " + routingKey, err);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ─── audit consumer ─────────────────────────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
var AUDIT_VERBS = {
|
|
144
|
+
stop_soft: "EXECUTION_STOP_SOFT",
|
|
145
|
+
stop_hard: "EXECUTION_STOP_HARD",
|
|
146
|
+
destroy: "EXECUTION_DESTROY",
|
|
147
|
+
resume: "EXECUTION_RESUME",
|
|
148
|
+
start_new: "EXECUTION_START_NEW",
|
|
149
|
+
data_access_requested: "EXECUTION_DATA_ACCESS",
|
|
150
|
+
data_access_denied: "EXECUTION_DATA_ACCESS_DENIED",
|
|
151
|
+
retention_update: "EXECUTION_RETENTION_UPDATE",
|
|
152
|
+
stop_notice_update: "EXECUTION_STOP_NOTICE_UPDATE"
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// Project-level settings: no execution, no agent; the Activity row targets the project.
|
|
156
|
+
var PROJECT_ACTIONS = ["retention_update", "stop_notice_update"];
|
|
157
|
+
|
|
158
|
+
// Audit entries that never become an Activity row (tiledesk-executions contract 6): the "requested" entry a
|
|
159
|
+
// resume / start new writes before acting (its outcome entry, accepted or rejected:<code>, is the row), and
|
|
160
|
+
// data_viewed (a read of stored values, kept in the executions audit log only). A data_access_requested entry is
|
|
161
|
+
// mirrored only when accepted: every rejected:* (and "requested") one stays in the executions audit log.
|
|
162
|
+
function isSilentAudit(a) {
|
|
163
|
+
if (!a) return false;
|
|
164
|
+
if (a.action === "data_viewed") return true;
|
|
165
|
+
if (a.action === "data_access_requested") return !(a.details && a.details.result === "accepted");
|
|
166
|
+
return !!(a.details && a.details.result === "requested");
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function auditToActivity(a) {
|
|
170
|
+
if (isSilentAudit(a)) return null;
|
|
171
|
+
var verb = AUDIT_VERBS[a.action];
|
|
172
|
+
if (!verb) return null;
|
|
173
|
+
var details = a.details || {};
|
|
174
|
+
var actionObj = {};
|
|
175
|
+
if (details.mode !== undefined) actionObj.mode = details.mode;
|
|
176
|
+
if (a.reason) actionObj.reason = a.reason;
|
|
177
|
+
actionObj.result = details.result;
|
|
178
|
+
actionObj.audit_id = a.audit_id;
|
|
179
|
+
if (details.scope !== undefined) {
|
|
180
|
+
actionObj.scope = { type: details.scope && details.scope.type };
|
|
181
|
+
if (details.scope && details.scope.step_seq !== undefined) actionObj.scope.step_seq = details.scope.step_seq;
|
|
182
|
+
}
|
|
183
|
+
if (details.grant_id !== undefined) actionObj.grant_id = details.grant_id;
|
|
184
|
+
if (details.role !== undefined) actionObj.role = details.role;
|
|
185
|
+
if (details.retention !== undefined) actionObj.retention = details.retention;
|
|
186
|
+
if (details.stop_notice !== undefined) actionObj.stop_notice = details.stop_notice;
|
|
187
|
+
var actor = {
|
|
188
|
+
type: "user",
|
|
189
|
+
id: String((a.actor && a.actor.user_id) || ""),
|
|
190
|
+
name: a.actor_name || (a.actor && (a.actor.name || a.actor.email)) || undefined
|
|
191
|
+
};
|
|
192
|
+
var doc;
|
|
193
|
+
if (PROJECT_ACTIONS.indexOf(a.action) !== -1) {
|
|
194
|
+
// a project-level setting: no execution, no agent; the row targets the project
|
|
195
|
+
doc = { verb: verb, actor: actor, target: { type: "project", id: String(a.id_project), object: {} },
|
|
196
|
+
actionObj: actionObj, id_project: String(a.id_project) };
|
|
197
|
+
var atR = a.at ? new Date(a.at) : null;
|
|
198
|
+
if (atR && !isNaN(atR.getTime())) doc.createdAt = atR;
|
|
199
|
+
return doc;
|
|
200
|
+
}
|
|
201
|
+
doc = {
|
|
202
|
+
verb: verb,
|
|
203
|
+
actor: actor,
|
|
204
|
+
target: {
|
|
205
|
+
type: "execution",
|
|
206
|
+
id: a.execution_id,
|
|
207
|
+
object: {
|
|
208
|
+
agent_id: a.agent_id,
|
|
209
|
+
agent_name: a.agent_name,
|
|
210
|
+
request_id: a.request_id,
|
|
211
|
+
status_after: a.status_after !== undefined ? a.status_after : details.status_after,
|
|
212
|
+
trigger_type: a.trigger_type
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
related: { role: "agent", type: "faq_kb", id: a.agent_id, name: a.agent_name },
|
|
216
|
+
actionObj: actionObj,
|
|
217
|
+
id_project: String(a.id_project)
|
|
218
|
+
};
|
|
219
|
+
var at = a.at ? new Date(a.at) : null;
|
|
220
|
+
if (at && !isNaN(at.getTime())) doc.createdAt = at;
|
|
221
|
+
return doc;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
var PERMANENT_SAVE_ERRORS = ["ValidationError", "CastError", "StrictModeError", "ValidatorError", "DivergentArrayError"];
|
|
225
|
+
|
|
226
|
+
// Only errors that can clear on their own are requeued: the connection or the server. With this server's
|
|
227
|
+
// mongoose 5.13 / mongodb 3.7 a server-side error (not primary during a failover, ...) is a MongoError
|
|
228
|
+
// (MongoServerError in newer drivers), and a save during an outage fails with a MongooseError
|
|
229
|
+
// "Operation ... buffering timed out"; plus network, server selection and timeout errors.
|
|
230
|
+
function isRecoverableSaveError(err) {
|
|
231
|
+
if (!err) return false;
|
|
232
|
+
var name = String(err.name || "");
|
|
233
|
+
if (PERMANENT_SAVE_ERRORS.indexOf(name) >= 0) return false;
|
|
234
|
+
if (name === "MongoError" || name === "MongoServerError") return err.code !== 11000 && err.code !== 11001;
|
|
235
|
+
if (name === "MongooseError") return /buffering timed out/i.test(String(err.message || ""));
|
|
236
|
+
return /Network|Timeout|ServerSelection|NotConnected|PoolCleared|Interrupted|Disconnected|Connection/i.test(name) ||
|
|
237
|
+
/ECONNRESET|ECONNREFUSED|ETIMEDOUT|EPIPE/.test(String(err.code || ""));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function handleAuditMessage(ch, msg) {
|
|
241
|
+
if (!msg) return Promise.resolve();
|
|
242
|
+
var body;
|
|
243
|
+
try {
|
|
244
|
+
body = JSON.parse(msg.content.toString());
|
|
245
|
+
} catch (err) {
|
|
246
|
+
winston.error("executions-publisher audit: invalid JSON, dropped", err);
|
|
247
|
+
ch.ack(msg);
|
|
248
|
+
return Promise.resolve();
|
|
249
|
+
}
|
|
250
|
+
if (isSilentAudit(body)) {
|
|
251
|
+
winston.debug("executions-publisher audit: " + (body.action === "data_viewed" ? "data_viewed" : "requested entry") + ", no Activity " + JSON.stringify({ audit_id: body.audit_id }));
|
|
252
|
+
ch.ack(msg);
|
|
253
|
+
return Promise.resolve();
|
|
254
|
+
}
|
|
255
|
+
var doc = body && auditToActivity(body);
|
|
256
|
+
// a message that can never be saved is dropped up front (a redelivery would loop forever)
|
|
257
|
+
if (!doc || !body.audit_id || !body.id_project || (!body.execution_id && PROJECT_ACTIONS.indexOf(body.action) === -1) || !(body.actor && body.actor.user_id)) {
|
|
258
|
+
winston.error("executions-publisher audit: unusable message (unknown action or missing audit_id, id_project, execution_id or actor.user_id), dropped " +
|
|
259
|
+
JSON.stringify({ audit_id: body && body.audit_id, action: body && body.action }));
|
|
260
|
+
ch.ack(msg);
|
|
261
|
+
return Promise.resolve();
|
|
262
|
+
}
|
|
263
|
+
if (process.env.ACTIVITY_HISTORY_ENABLED !== "true") {
|
|
264
|
+
ch.ack(msg);
|
|
265
|
+
return Promise.resolve();
|
|
266
|
+
}
|
|
267
|
+
var Activity = require("../activities/models/activity");
|
|
268
|
+
return new Activity(doc).save().then(function () {
|
|
269
|
+
ch.ack(msg);
|
|
270
|
+
}).catch(function (err) {
|
|
271
|
+
if (err && (err.code === 11000 || err.code === 11001)) {
|
|
272
|
+
ch.ack(msg);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (!isRecoverableSaveError(err)) {
|
|
276
|
+
// a document error (ValidationError, CastError, StrictModeError) or an unknown one: it would fail
|
|
277
|
+
// the same way on every redelivery, and enough of them stall the queue (prefetch 20)
|
|
278
|
+
winston.error("executions-publisher audit: save error " + (err && err.name) + ", dropped " +
|
|
279
|
+
JSON.stringify({ audit_id: body.audit_id, action: body.action }), err);
|
|
280
|
+
ch.ack(msg);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
winston.error("executions-publisher audit: save error, requeue", err);
|
|
284
|
+
setTimeout(function () {
|
|
285
|
+
try { ch.nack(msg, false, true); } catch (e) { /* channel gone: broker redelivers */ }
|
|
286
|
+
}, nackDelayMs);
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function defaultAuditChannelFactory() {
|
|
291
|
+
return getChannel().then(function () {
|
|
292
|
+
if (!currentConnection) throw new Error("amqp connection not available");
|
|
293
|
+
return currentConnection.createChannel();
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function scheduleAuditRetry() {
|
|
298
|
+
if (auditRetryTimer || !process.env.EXECUTIONS_AMQP_URL) return;
|
|
299
|
+
auditRetryTimer = setTimeout(function () {
|
|
300
|
+
auditRetryTimer = null;
|
|
301
|
+
startAuditConsumer();
|
|
302
|
+
}, AUDIT_RETRY_MS);
|
|
303
|
+
if (auditRetryTimer.unref) auditRetryTimer.unref();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Never throws and never rejects: on failure it retries later (lazy reconnect of the shared connection).
|
|
307
|
+
function startAuditConsumer(opts) {
|
|
308
|
+
var factory = (opts && opts.channelFactory) || auditChannelFactoryOverride || defaultAuditChannelFactory;
|
|
309
|
+
var retry = !(opts && opts.channelFactory) && !auditChannelFactoryOverride;
|
|
310
|
+
var failed = false;
|
|
311
|
+
function onFail(err) {
|
|
312
|
+
if (failed) return;
|
|
313
|
+
failed = true;
|
|
314
|
+
winston.error("executions-publisher audit consumer error", err);
|
|
315
|
+
if (retry) scheduleAuditRetry();
|
|
316
|
+
}
|
|
317
|
+
return Promise.resolve().then(factory).then(function (ch) {
|
|
318
|
+
if (ch.on) {
|
|
319
|
+
ch.on("error", onFail);
|
|
320
|
+
ch.on("close", function () { onFail(new Error("audit channel closed")); });
|
|
321
|
+
}
|
|
322
|
+
return ch.assertQueue(AUDIT_QUEUE, { durable: true })
|
|
323
|
+
.then(function () { return ch.bindQueue(AUDIT_QUEUE, EXCHANGE, AUDIT_BINDING); })
|
|
324
|
+
.then(function () { return ch.prefetch(AUDIT_PREFETCH); })
|
|
325
|
+
.then(function () {
|
|
326
|
+
return ch.consume(AUDIT_QUEUE, function (msg) {
|
|
327
|
+
return handleAuditMessage(ch, msg).catch(function (err) {
|
|
328
|
+
winston.error("executions-publisher audit handler error", err);
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
})
|
|
332
|
+
.then(function () {
|
|
333
|
+
winston.info("executions-publisher audit consumer started");
|
|
334
|
+
});
|
|
335
|
+
}).catch(onFail);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ─── helpers ────────────────────────────────────────────────────────────────
|
|
339
|
+
|
|
340
|
+
function botIds(list) {
|
|
341
|
+
return (list || [])
|
|
342
|
+
.filter(function (p) { return typeof p === "string" && p.indexOf(BOT_PREFIX) === 0; })
|
|
343
|
+
.map(function (p) { return p.substring(BOT_PREFIX.length); });
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function executionIdOf(request) {
|
|
347
|
+
return (request && request.attributes && request.attributes.execution_id) || null;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function publishEvent(type, request, extraData) {
|
|
351
|
+
var idProject = String(request.id_project);
|
|
352
|
+
var executionId = executionIdOf(request);
|
|
353
|
+
var data = Object.assign({
|
|
354
|
+
request_id: request.request_id,
|
|
355
|
+
execution_id: executionId
|
|
356
|
+
}, extraData);
|
|
357
|
+
send("exec." + idProject + "." + type, {
|
|
358
|
+
event_version: 1,
|
|
359
|
+
type: type,
|
|
360
|
+
execution_id: executionId,
|
|
361
|
+
at: new Date().toISOString(),
|
|
362
|
+
id_project: idProject,
|
|
363
|
+
request_id: request.request_id,
|
|
364
|
+
data: data
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ─── listeners ──────────────────────────────────────────────────────────────
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* opts.audit false: publish only, no audit consumer. The jobs worker (jobs.js) closes conversations too (the
|
|
372
|
+
* scheduler's unresponsive-bot close, routing): its request.close must reach the executions service, while the
|
|
373
|
+
* audit queue stays with the API pods.
|
|
374
|
+
*/
|
|
375
|
+
function listen(opts) {
|
|
376
|
+
if (!process.env.EXECUTIONS_AMQP_URL) {
|
|
377
|
+
winston.info("executions-publisher disabled: EXECUTIONS_AMQP_URL not set");
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
requestEvent.on("request.close", function (request) {
|
|
382
|
+
try {
|
|
383
|
+
if (!request) return;
|
|
384
|
+
var bots = botIds(request.participants);
|
|
385
|
+
if (bots.length === 0 && !executionIdOf(request)) return;
|
|
386
|
+
publishEvent("conversation.closed", request, {
|
|
387
|
+
bot_id: bots.length ? bots[0] : null,
|
|
388
|
+
closed_by: request.closed_by || null
|
|
389
|
+
});
|
|
390
|
+
} catch (err) {
|
|
391
|
+
winston.error("executions-publisher request.close error", err);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
requestEvent.on("request.participants.update", function (data) {
|
|
396
|
+
try {
|
|
397
|
+
if (!data || !data.request) return;
|
|
398
|
+
var removed = botIds(data.removedParticipants);
|
|
399
|
+
if (removed.length === 0) return;
|
|
400
|
+
if (SUBAGENT_REASONS.indexOf(data.reason) !== -1) return;
|
|
401
|
+
var request = data.request;
|
|
402
|
+
if (!executionIdOf(request) && data.beforeRequest) {
|
|
403
|
+
request = Object.assign({}, request, { attributes: data.beforeRequest.attributes });
|
|
404
|
+
}
|
|
405
|
+
var added = botIds(data.addedParticipants);
|
|
406
|
+
if (added.length) {
|
|
407
|
+
publishEvent("conversation.bot_replaced", request, { bot_id: removed[0], to_bot_id: added[0] });
|
|
408
|
+
} else {
|
|
409
|
+
publishEvent("conversation.bot_left", request, { bot_id: removed[0] });
|
|
410
|
+
}
|
|
411
|
+
} catch (err) {
|
|
412
|
+
winston.error("executions-publisher request.participants.update error", err);
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
requestEvent.on("request.participants.leave", function (data) {
|
|
417
|
+
try {
|
|
418
|
+
if (!data || !data.request) return;
|
|
419
|
+
var left = botIds([data.member]);
|
|
420
|
+
if (left.length === 0) return;
|
|
421
|
+
publishEvent("conversation.bot_left", data.request, { bot_id: left[0] });
|
|
422
|
+
} catch (err) {
|
|
423
|
+
winston.error("executions-publisher request.participants.leave error", err);
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
if (!opts || opts.audit !== false) startAuditConsumer();
|
|
428
|
+
|
|
429
|
+
winston.info("executions-publisher listening" + (opts && opts.audit === false ? " (publish only)" : ""));
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function _setTransportForTests(t) {
|
|
433
|
+
transportOverride = t;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function _setConnectTimeoutForTests(ms) {
|
|
437
|
+
connectTimeoutOverride = ms || null;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function _setNackDelayForTests(ms) {
|
|
441
|
+
nackDelayMs = ms;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function _setAuditChannelFactoryForTests(f) {
|
|
445
|
+
auditChannelFactoryOverride = f || null;
|
|
446
|
+
if (auditRetryTimer) { clearTimeout(auditRetryTimer); auditRetryTimer = null; }
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
module.exports = {
|
|
450
|
+
listen: listen,
|
|
451
|
+
startAuditConsumer: startAuditConsumer,
|
|
452
|
+
_setNackDelayForTests: _setNackDelayForTests,
|
|
453
|
+
_setAuditChannelFactoryForTests: _setAuditChannelFactoryForTests,
|
|
454
|
+
_setTransportForTests: _setTransportForTests,
|
|
455
|
+
_setConnectTimeoutForTests: _setConnectTimeoutForTests,
|
|
456
|
+
_amqpTransport: amqpTransport
|
|
457
|
+
};
|