n8n
Version:
n8n Workflow Automation Tool
80 lines • 5.24 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.AgentIntegrationManagementService = void 0;
const api_types_1 = require("@n8n/api-types");
const di_1 = require("@n8n/di");
const credentials_service_1 = require("../../credentials/credentials.service");
const bad_request_error_1 = require("../../errors/response-errors/bad-request.error");
const not_found_error_1 = require("../../errors/response-errors/not-found.error");
const agent_integration_persistence_service_1 = require("./agent-integration-persistence.service");
const agent_chat_integration_1 = require("./integrations/agent-chat-integration");
const chat_integration_service_1 = require("./integrations/chat-integration.service");
let AgentIntegrationManagementService = class AgentIntegrationManagementService {
constructor(persistenceService, credentialsService, chatService, registry) {
this.persistenceService = persistenceService;
this.credentialsService = credentialsService;
this.chatService = chatService;
this.registry = registry;
}
async validateConfig(input) {
const parsed = await api_types_1.AgentIntegrationSchema.safeParseAsync(input);
if (!parsed.success)
throw new bad_request_error_1.BadRequestError(parsed.error.message);
const integration = parsed.data;
this.registry.require(integration.type).validateConfig?.(integration);
return integration;
}
async connect(options) {
const integration = await this.validateConfig(options.integration);
const implementation = this.registry.require(integration.type);
const usableCredentials = await this.credentialsService.getCredentialsAUserCanUseInAWorkflow(options.user, { projectId: options.agent.projectId });
const credential = usableCredentials.find((item) => item.id === integration.credentialId);
if (!credential) {
throw new not_found_error_1.NotFoundError(`Credential "${integration.credentialId}" not found`);
}
if (!implementation.credentialTypes.includes(credential.type)) {
throw new bad_request_error_1.BadRequestError(`${implementation.displayLabel} integrations do not support ${credential.type} credentials`);
}
await this.chatService.validateBeforeConnect(options.agent.id, integration, options.agent.projectId);
const savedAgent = await this.persistenceService.saveCredentialIntegration(options.agent, integration, { user: options.user, modifiedBy: options.modifiedBy ?? 'user', broadcast: false });
if (savedAgent.activeVersionId === null)
return { integration, savedAgent };
await this.chatService.connect(options.agent.id, integration, options.agent.projectId);
await this.chatService.broadcastIntegrationChange(options.agent.id, integration, 'connect');
return { integration, savedAgent };
}
async disconnect(options) {
const persisted = (options.agent.integrations ?? []).find((item) => item.type === options.type && item.credentialId === options.credentialId);
const parsed = api_types_1.AgentIntegrationSchema.safeParse({
type: options.type,
credentialId: options.credentialId,
});
const integration = persisted ?? (parsed.success ? parsed.data : undefined);
if (integration) {
await this.chatService.disconnectChannel(options.agent.id, integration);
}
else {
await this.chatService.disconnect(options.agent.id, {
type: options.type,
credentialId: options.credentialId,
});
}
const savedAgent = await this.persistenceService.removeCredentialIntegration(options.agent, options.type, options.credentialId, { user: options.user, modifiedBy: options.modifiedBy ?? 'user', broadcast: false });
return { savedAgent };
}
};
exports.AgentIntegrationManagementService = AgentIntegrationManagementService;
exports.AgentIntegrationManagementService = AgentIntegrationManagementService = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [agent_integration_persistence_service_1.AgentIntegrationPersistenceService, credentials_service_1.CredentialsService, chat_integration_service_1.ChatIntegrationService, agent_chat_integration_1.ChatIntegrationRegistry])
], AgentIntegrationManagementService);
//# sourceMappingURL=agent-integration-management.service.js.map