UNPKG

n8n

Version:

n8n Workflow Automation Tool

121 lines • 7.79 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.AgentIntegrationPersistenceService = void 0; const api_types_1 = require("@n8n/api-types"); const di_1 = require("@n8n/di"); const n8n_workflow_1 = require("n8n-workflow"); const credentials_service_1 = require("../../credentials/credentials.service"); const event_service_1 = require("../../events/event.service"); const agent_modification_telemetry_service_1 = require("./agent-modification-telemetry.service"); const agent_runtime_cache_service_1 = require("./agent-runtime-cache.service"); const agent_setup_completion_service_1 = require("./agent-setup-completion.service"); const agent_chat_integration_1 = require("./integrations/agent-chat-integration"); const chat_integration_service_1 = require("./integrations/chat-integration.service"); const agent_repository_1 = require("./repositories/agent.repository"); const agent_credential_provider_1 = require("./utils/agent-credential-provider"); const agent_draft_utils_1 = require("./utils/agent-draft.utils"); let AgentIntegrationPersistenceService = class AgentIntegrationPersistenceService { constructor(agentRepository, chatIntegrationService, runtimeCacheService, chatIntegrationRegistry, eventService, modificationTelemetry, credentialsService, setupCompletionService) { this.agentRepository = agentRepository; this.chatIntegrationService = chatIntegrationService; this.runtimeCacheService = runtimeCacheService; this.chatIntegrationRegistry = chatIntegrationRegistry; this.eventService = eventService; this.modificationTelemetry = modificationTelemetry; this.credentialsService = credentialsService; this.setupCompletionService = setupCompletionService; } listChatIntegrations() { return this.chatIntegrationRegistry.listPublic().map((i) => ({ type: i.type, label: i.displayLabel, icon: i.displayIcon, credentialTypes: i.credentialTypes, ...(i.builderGuidance ? { capabilities: i.builderGuidance.capabilities, useIntegrationWhen: i.builderGuidance.useIntegrationWhen, useNodeToolWhen: i.builderGuidance.useNodeToolWhen, } : {}), })); } async saveCredentialIntegration(agent, integration, context) { const parseResult = api_types_1.AgentIntegrationSchema.safeParse(integration); if (!parseResult.success) { throw new n8n_workflow_1.UserError(`Invalid credential integration: ${parseResult.error.message}`); } const validated = parseResult.data; const { type, credentialId } = validated; if ((0, api_types_1.isDraftIntegration)(validated)) { throw new n8n_workflow_1.UserError('Credential integration requires a credential ID.'); } const previousSchema = agent.schema ?? null; const previousIntegrations = agent.integrations ?? []; const wasUnconfigured = (0, agent_modification_telemetry_service_1.isUnconfiguredAgent)(previousSchema, previousIntegrations); const existing = previousIntegrations.filter((i) => !(i.type === type && (0, api_types_1.isDraftIntegration)(i))); const alreadyExists = existing.some((i) => i.type === type && i.credentialId === credentialId); agent.integrations = alreadyExists ? existing.map((existingIntegration) => existingIntegration.type === type && existingIntegration.credentialId === credentialId ? validated : existingIntegration) : [...existing, validated]; (0, agent_draft_utils_1.markAgentDraftDirty)(agent); this.runtimeCacheService.clearRuntimes(agent.id); const credentialProvider = (0, agent_credential_provider_1.createAgentCredentialProvider)(this.credentialsService, agent.projectId, context.user); const emitSetupCompleted = await this.setupCompletionService.recordIfSetupComplete(agent, agent.projectId, credentialProvider, context.user); const result = await this.agentRepository.save(agent); this.eventService.emit('agent-saved', { agentId: agent.id }); await emitSetupCompleted?.(); await this.recordIntegrationMutation(result, previousSchema, previousIntegrations, context, wasUnconfigured); if (context.broadcast !== false) { await this.chatIntegrationService.broadcastIntegrationChange(agent.id, integration, 'connect'); } return result; } async removeCredentialIntegration(agent, type, credentialId, context) { if (!agent.integrations?.length) return agent; const integration = agent.integrations.find((i) => i.type === type && i.credentialId === credentialId); if (!integration) return agent; const previousSchema = agent.schema ?? null; const previousIntegrations = agent.integrations ?? []; const wasUnconfigured = (0, agent_modification_telemetry_service_1.isUnconfiguredAgent)(previousSchema, previousIntegrations); agent.integrations = agent.integrations.filter((i) => i !== integration); (0, agent_draft_utils_1.markAgentDraftDirty)(agent); this.runtimeCacheService.clearRuntimes(agent.id); const result = await this.agentRepository.save(agent); this.eventService.emit('agent-saved', { agentId: agent.id }); await this.recordIntegrationMutation(result, previousSchema, previousIntegrations, context, wasUnconfigured); if (context.broadcast !== false) { await this.chatIntegrationService.broadcastIntegrationChange(agent.id, integration, 'disconnect'); } return result; } async recordIntegrationMutation(agent, previousSchema, previousIntegrations, context, wasUnconfigured) { this.modificationTelemetry.record({ agent, projectId: agent.projectId, user: context.user, by: context.modifiedBy, changedParts: (0, agent_modification_telemetry_service_1.diffAgentConfigParts)(previousSchema, agent.schema, previousIntegrations, agent.integrations ?? []), wasUnconfigured, }); } }; exports.AgentIntegrationPersistenceService = AgentIntegrationPersistenceService; exports.AgentIntegrationPersistenceService = AgentIntegrationPersistenceService = __decorate([ (0, di_1.Service)(), __metadata("design:paramtypes", [agent_repository_1.AgentRepository, chat_integration_service_1.ChatIntegrationService, agent_runtime_cache_service_1.AgentRuntimeCacheService, agent_chat_integration_1.ChatIntegrationRegistry, event_service_1.EventService, agent_modification_telemetry_service_1.AgentModificationTelemetryService, credentials_service_1.CredentialsService, agent_setup_completion_service_1.AgentSetupCompletionService]) ], AgentIntegrationPersistenceService); //# sourceMappingURL=agent-integration-persistence.service.js.map