n8n
Version:
n8n Workflow Automation Tool
171 lines • 8.5 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatIntegrationActionExecutor = void 0;
const di_1 = require("@n8n/di");
const zod_1 = require("zod");
const agent_chat_integration_1 = require("./agent-chat-integration");
const chat_integration_service_1 = require("./chat-integration.service");
const component_mapper_1 = require("./component-mapper");
const integration_error_codes_1 = require("./integration-error-codes");
const integration_helpers_1 = require("./integration-helpers");
const slack_operations_1 = require("./platforms/slack-operations");
const messageSchema = zod_1.z.object({
text: zod_1.z.string().optional(),
richInteraction: zod_1.z
.object({
awaitResponse: zod_1.z.boolean().optional(),
title: zod_1.z.string().optional(),
message: zod_1.z.string().optional(),
components: zod_1.z.array(zod_1.z.object({ type: zod_1.z.string() }).passthrough()).min(1),
})
.optional(),
});
const respondInputSchema = zod_1.z.object({ message: messageSchema });
const sendDmInputSchema = zod_1.z.object({
userId: zod_1.z.string().min(1),
message: messageSchema,
});
const sendChannelMessageInputSchema = zod_1.z.object({
channelId: zod_1.z.string().min(1),
message: messageSchema,
});
let ChatIntegrationActionExecutor = class ChatIntegrationActionExecutor {
constructor(chatIntegrationService, integrationRegistry) {
this.chatIntegrationService = chatIntegrationService;
this.integrationRegistry = integrationRegistry;
this.componentMapper = new component_mapper_1.ComponentMapper();
}
async execute(params) {
if (!params.descriptor.agentId)
return (0, integration_helpers_1.connectionUnavailable)();
const chat = this.chatIntegrationService.getChatInstance(params.descriptor.agentId, {
type: params.descriptor.integration.type,
credentialId: params.descriptor.integration.credentialId,
});
if (!chat)
return (0, integration_helpers_1.connectionUnavailable)();
try {
if (params.action === 'respond') {
return await this.respondInCurrentThread(chat, params);
}
if (params.action === 'send_dm') {
return await this.sendDirectMessage(chat, params);
}
const integration = this.integrationRegistry.get(params.descriptor.integration.type);
if (integration?.executeAction) {
const result = await integration.executeAction({
chat,
descriptor: params.descriptor,
action: params.action,
input: params.input,
currentMessageContext: params.currentMessageContext,
});
if (result !== undefined)
return result;
}
if (params.action === 'send_channel_message') {
return await this.sendChannelMessage(chat, params);
}
return (0, integration_helpers_1.integrationError)(integration_error_codes_1.INTEGRATION_ERROR_CODES.UNSUPPORTED_ACTION, `The ${params.descriptor.integration.type} integration does not support ${params.action}.`);
}
catch (error) {
return (0, integration_helpers_1.integrationError)(integration_error_codes_1.INTEGRATION_ERROR_CODES.ACTION_FAILED, error instanceof Error ? error.message : String(error));
}
}
async respondInCurrentThread(chat, params) {
const input = respondInputSchema.parse(params.input);
const threadId = params.currentMessageContext?.target.threadId;
if (!threadId) {
return (0, integration_helpers_1.integrationError)(integration_error_codes_1.INTEGRATION_ERROR_CODES.NO_MESSAGE_CONTEXT, 'There is no current message context. Use an explicit send action.');
}
const thread = chat.thread(threadId);
await maybeSubscribeSlackThread(params.descriptor, thread);
const sent = await thread.post(await this.toPostable(params.descriptor, input.message, params));
return {
ok: true,
messageContext: buildMessageContextFromSentMessage({
descriptor: params.descriptor,
sent,
target: params.currentMessageContext.target,
}),
};
}
async sendDirectMessage(chat, params) {
const input = sendDmInputSchema.parse(params.input);
const thread = await chat.openDM(input.userId);
await maybeSubscribeSlackThread(params.descriptor, thread);
const sent = await thread.post(await this.toPostable(params.descriptor, input.message, params));
return {
ok: true,
messageContext: buildMessageContextFromSentMessage({
descriptor: params.descriptor,
sent,
target: { type: 'dm', userId: input.userId, threadId: thread.id },
}),
};
}
async sendChannelMessage(chat, params) {
const input = sendChannelMessageInputSchema.parse(params.input);
const channelId = (0, integration_helpers_1.normalizePlatformId)(params.descriptor.integration.type, input.channelId);
const channel = chat.channel(channelId);
const sent = await channel.post(await this.toPostable(params.descriptor, input.message, params));
await maybeSubscribeSlackSentThread(params.descriptor, chat, sent.threadId);
return {
ok: true,
messageContext: buildMessageContextFromSentMessage({
descriptor: params.descriptor,
sent,
target: { type: 'channel', channelId, threadId: sent.threadId },
}),
};
}
async toPostable(descriptor, message, params) {
const richInteraction = message.richInteraction;
if (!richInteraction)
return message.text ?? '';
if (params.awaitResponse && (!params.runId || !params.toolCallId)) {
throw new Error('Interactive integration actions require runId and toolCallId.');
}
const card = await this.componentMapper.toCard({
title: richInteraction.title ?? message.text,
message: richInteraction.message,
components: richInteraction.components,
}, params.runId ?? '', params.toolCallId ?? '', component_mapper_1.RICH_INTERACTION_RESUME_JSON_SCHEMA, undefined, descriptor.integration.type);
return { card };
}
};
exports.ChatIntegrationActionExecutor = ChatIntegrationActionExecutor;
exports.ChatIntegrationActionExecutor = ChatIntegrationActionExecutor = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [chat_integration_service_1.ChatIntegrationService,
agent_chat_integration_1.ChatIntegrationRegistry])
], ChatIntegrationActionExecutor);
function buildMessageContextFromSentMessage(params) {
return {
integrationConnectionId: params.descriptor.integrationConnectionId,
platform: params.descriptor.integration.type,
target: params.target,
messageId: params.sent.id,
updatedAt: new Date().toISOString(),
};
}
async function maybeSubscribeSlackThread(descriptor, thread) {
if (descriptor.integration.type !== 'slack')
return;
await (0, slack_operations_1.subscribeSlackThread)(thread);
}
async function maybeSubscribeSlackSentThread(descriptor, chat, threadId) {
if (descriptor.integration.type !== 'slack' || !threadId)
return;
await (0, slack_operations_1.subscribeSlackThread)(chat.thread(threadId));
}
//# sourceMappingURL=integration-action-executor.js.map