@tiledesk/tiledesk-server 3.3.5 → 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 +17 -0
- 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 +1 -1
- 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,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
|
+
};
|