UNPKG

n8n

Version:

n8n Workflow Automation Tool

166 lines 7.74 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.AgentSkillsService = void 0; const api_types_1 = require("@n8n/api-types"); const backend_common_1 = require("@n8n/backend-common"); const di_1 = require("@n8n/di"); const n8n_workflow_1 = require("n8n-workflow"); const not_found_error_1 = require("../../errors/response-errors/not-found.error"); const agent_draft_utils_1 = require("./utils/agent-draft.utils"); const agent_repository_1 = require("./repositories/agent.repository"); const agent_resource_id_1 = require("./utils/agent-resource-id"); let AgentSkillsService = class AgentSkillsService { constructor(logger, agentRepository) { this.logger = logger; this.agentRepository = agentRepository; } async listSkills(agentId, projectId) { const entity = await this.agentRepository.findByIdAndProjectId(agentId, projectId); if (!entity) throw new not_found_error_1.NotFoundError('Agent not found'); return entity.skills ?? {}; } async getSkill(agentId, projectId, skillId) { const skills = await this.listSkills(agentId, projectId); const skill = skills[skillId]; if (!skill) throw new not_found_error_1.NotFoundError('Skill not found'); return skill; } async createSkill(agentId, projectId, skill) { const entity = await this.agentRepository.findByIdAndProjectId(agentId, projectId); if (!entity) throw new not_found_error_1.NotFoundError('Agent not found'); this.validateSkill(skill); const skillId = this.addSkill(entity, skill); (0, agent_draft_utils_1.markAgentDraftDirty)(entity); const saved = await this.agentRepository.save(entity); this.logger.debug('Created agent skill', { agentId, projectId, skillId }); return { id: skillId, skill, versionId: saved.versionId }; } async createAndAttachSkill(agentId, projectId, skill) { const entity = await this.agentRepository.findByIdAndProjectId(agentId, projectId); if (!entity) throw new not_found_error_1.NotFoundError('Agent not found'); if (!entity.schema) throw new n8n_workflow_1.UserError('Agent has no JSON config yet.'); this.validateSkill(skill); const skillId = this.addSkill(entity, skill); this.attachSkillRef(entity, skillId); (0, agent_draft_utils_1.markAgentDraftDirty)(entity); const saved = await this.agentRepository.save(entity); this.logger.debug('Created and attached agent skill', { agentId, projectId, skillId }); return { id: skillId, skill, versionId: saved.versionId }; } async updateSkill(agentId, projectId, skillId, updates) { const entity = await this.agentRepository.findByIdAndProjectId(agentId, projectId); if (!entity) throw new not_found_error_1.NotFoundError('Agent not found'); const existing = entity.skills?.[skillId]; if (!existing) throw new not_found_error_1.NotFoundError('Skill not found'); const updated = { ...existing, ...updates }; this.validateSkill(updated); entity.skills = { ...(entity.skills ?? {}), [skillId]: updated, }; (0, agent_draft_utils_1.markAgentDraftDirty)(entity); const saved = await this.agentRepository.save(entity); this.logger.debug('Updated agent skill', { agentId, projectId, skillId }); return { id: skillId, skill: updated, versionId: saved.versionId }; } async deleteSkill(agentId, projectId, skillId) { const entity = await this.agentRepository.findByIdAndProjectId(agentId, projectId); if (!entity) throw new not_found_error_1.NotFoundError('Agent not found'); const skills = { ...(entity.skills ?? {}) }; if (!skills[skillId]) throw new not_found_error_1.NotFoundError('Skill not found'); delete skills[skillId]; entity.skills = skills; if (entity.schema?.skills) { entity.schema.skills = entity.schema.skills.filter((t) => t.id !== skillId); } (0, agent_draft_utils_1.markAgentDraftDirty)(entity); await this.agentRepository.save(entity); this.logger.debug('Deleted agent skill', { agentId, projectId, skillId }); } removeUnreferencedSkills(entity, config) { const referencedSkillIds = new Set((config.skills ?? []).map((t) => t.id)); const orphanSkillIds = Object.keys(entity.skills ?? {}).filter((id) => !referencedSkillIds.has(id)); if (orphanSkillIds.length === 0) return; const skills = { ...(entity.skills ?? {}) }; for (const id of orphanSkillIds) { delete skills[id]; } entity.skills = skills; } getMissingSkillIds(config, skills) { const refs = config?.skills ?? []; const seen = new Set(); const missing = []; for (const ref of refs) { if (seen.has(ref.id)) continue; seen.add(ref.id); if (!skills[ref.id]) missing.push(ref.id); } return missing; } snapshotConfiguredSkills(config, skills) { if (!config) return null; const missing = this.getMissingSkillIds(config, skills); if (missing.length > 0) { throw new n8n_workflow_1.UserError(`Cannot publish agent with missing skill bodies: ${missing.join(', ')}`); } const snapshot = {}; for (const ref of config.skills ?? []) { const skill = skills[ref.id]; if (skill) snapshot[ref.id] = { ...skill }; } return snapshot; } validateSkill(skill) { const result = api_types_1.agentSkillSchema.safeParse(skill); if (!result.success) { throw new n8n_workflow_1.UserError(`Invalid agent skill: ${result.error.issues[0]?.message ?? 'Invalid skill'}`); } } addSkill(entity, skill) { const skillId = (0, agent_resource_id_1.generateAgentResourceId)('skill', Object.keys(entity.skills ?? {})); entity.skills = { ...(entity.skills ?? {}), [skillId]: skill, }; return skillId; } attachSkillRef(entity, skillId) { if (!entity.schema) throw new n8n_workflow_1.UserError('Agent has no JSON config yet.'); entity.schema.skills = [ ...(entity.schema.skills ?? []).filter((ref) => ref.id !== skillId), { type: 'skill', id: skillId }, ]; } }; exports.AgentSkillsService = AgentSkillsService; exports.AgentSkillsService = AgentSkillsService = __decorate([ (0, di_1.Service)(), __metadata("design:paramtypes", [backend_common_1.Logger, agent_repository_1.AgentRepository]) ], AgentSkillsService); //# sourceMappingURL=agent-skills.service.js.map