n8n
Version:
n8n Workflow Automation Tool
89 lines • 5.18 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.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 agent_runtime_cache_service_1 = require("./agent-runtime-cache.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_draft_utils_1 = require("./utils/agent-draft.utils");
let AgentIntegrationPersistenceService = class AgentIntegrationPersistenceService {
constructor(agentRepository, chatIntegrationService, runtimeCacheService, chatIntegrationRegistry) {
this.agentRepository = agentRepository;
this.chatIntegrationService = chatIntegrationService;
this.runtimeCacheService = runtimeCacheService;
this.chatIntegrationRegistry = chatIntegrationRegistry;
}
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, options = {}) {
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 existing = (agent.integrations ?? []).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 result = await this.agentRepository.save(agent);
if (options.broadcast !== false) {
await this.chatIntegrationService.broadcastIntegrationChange(agent.id, integration, 'connect');
}
return result;
}
async removeCredentialIntegration(agent, type, credentialId, options = {}) {
if (!agent.integrations?.length)
return agent;
const integration = agent.integrations.find((i) => i.type === type && i.credentialId === credentialId);
if (!integration)
return agent;
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);
if (options.broadcast !== false) {
await this.chatIntegrationService.broadcastIntegrationChange(agent.id, integration, 'disconnect');
}
return result;
}
};
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])
], AgentIntegrationPersistenceService);
//# sourceMappingURL=agent-integration-persistence.service.js.map