UNPKG

n8n

Version:

n8n Workflow Automation Tool

85 lines 4.58 kB
"use strict"; 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.AgentChatSubscriptionStateService = void 0; const backend_common_1 = require("@n8n/backend-common"); const config_1 = require("@n8n/config"); const decorators_1 = require("@n8n/decorators"); const di_1 = require("@n8n/di"); const publisher_service_1 = require("../../../scaling/pubsub/publisher.service"); const agent_chat_subscription_state_adapter_1 = require("./agent-chat-subscription-state.adapter"); const agent_chat_subscription_repository_1 = require("../repositories/agent-chat-subscription.repository"); function toScope(agentId, integration) { return { agentId, integrationType: integration.type, credentialId: integration.credentialId, }; } let AgentChatSubscriptionStateService = class AgentChatSubscriptionStateService { constructor(logger, repository, publisher, globalConfig) { this.logger = logger; this.repository = repository; this.publisher = publisher; this.globalConfig = globalConfig; this.activeStates = new Map(); } createStateAdapter(options) { const scope = toScope(options.agentId, options.integration); const state = new agent_chat_subscription_state_adapter_1.AgentChatSubscriptionStateAdapter(scope, options.integration, options.delegate, this.repository, async (integration, threadId, action) => await this.publishSubscriptionChanged(options.agentId, integration, threadId, action), (disconnectedState) => this.unregister(disconnectedState)); this.activeStates.set(state.key, state); return state; } async deleteSubscriptionsForIntegration(agentId, integration) { await this.repository.deleteForConnection(toScope(agentId, integration)); } async handleSubscriptionChanged(payload) { const state = this.activeStates.get((0, agent_chat_subscription_state_adapter_1.scopeKey)(toScope(payload.agentId, payload.integration))); await state?.applyRemoteChange(payload.threadId, payload.action); } unregister(state) { if (this.activeStates.get(state.key) === state) { this.activeStates.delete(state.key); } } async publishSubscriptionChanged(agentId, integration, threadId, action) { if (!this.globalConfig.multiMainSetup.enabled) return; try { await this.publisher.publishCommand({ command: 'agent-chat-subscription-changed', payload: { agentId, integration, threadId, action }, }); } catch (error) { this.logger.warn('[AgentChatSubscriptionStateService] Failed to publish subscription change', { agentId, integrationType: integration.type, credentialId: integration.credentialId, threadId, action, error: error instanceof Error ? error.message : String(error), }); } } }; exports.AgentChatSubscriptionStateService = AgentChatSubscriptionStateService; __decorate([ (0, decorators_1.OnPubSubEvent)('agent-chat-subscription-changed', { instanceType: 'main' }), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], AgentChatSubscriptionStateService.prototype, "handleSubscriptionChanged", null); exports.AgentChatSubscriptionStateService = AgentChatSubscriptionStateService = __decorate([ (0, di_1.Service)(), __metadata("design:paramtypes", [backend_common_1.Logger, agent_chat_subscription_repository_1.AgentChatSubscriptionRepository, publisher_service_1.Publisher, config_1.GlobalConfig]) ], AgentChatSubscriptionStateService); //# sourceMappingURL=agent-chat-subscription-state.service.js.map