n8n
Version:
n8n Workflow Automation Tool
85 lines • 4.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.SubAgentSourceResolver = void 0;
const api_types_1 = require("@n8n/api-types");
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_history_repository_1 = require("../repositories/agent-history.repository");
const agent_repository_1 = require("../repositories/agent.repository");
let SubAgentSourceResolver = class SubAgentSourceResolver {
constructor(agentRepository, agentHistoryRepository) {
this.agentRepository = agentRepository;
this.agentHistoryRepository = agentHistoryRepository;
}
async resolveForRuntime(source, context) {
const agent = await this.agentRepository.findByIdAndProjectId(source.agentId, context.projectId);
if (!agent) {
throw new not_found_error_1.NotFoundError(`Agent "${source.agentId}" not found`);
}
if (source.versionId) {
const version = await this.agentHistoryRepository.findByVersionAndAgentId(source.versionId, source.agentId);
if (!version) {
throw new not_found_error_1.NotFoundError(`Version "${source.versionId}" not found for agent "${source.agentId}"`);
}
if (!version.schema) {
throw new n8n_workflow_1.UserError(`Agent "${source.agentId}" version "${source.versionId}" has no config`);
}
return {
source: {
sourceId: source.agentId,
versionId: source.versionId,
config: this.toRunnableConfig(version.schema),
},
...getAgentRuntimeAssets(version),
};
}
const publishedVersion = agent.activeVersion;
if (!agent.activeVersionId || !publishedVersion?.schema) {
throw new n8n_workflow_1.UserError(`Sub-agent "${source.agentId}" is not published`);
}
return {
source: {
sourceId: source.agentId,
versionId: agent.activeVersionId,
config: this.toRunnableConfig(publishedVersion.schema),
},
...getAgentRuntimeAssets(publishedVersion),
};
}
toRunnableConfig(config) {
const result = api_types_1.RunnableAgentJsonConfigSchema.safeParse(config);
if (!result.success) {
throw new n8n_workflow_1.UserError(`Invalid sub-agent config: ${result.error.issues[0]?.message ?? 'Invalid config'}`);
}
return result.data;
}
};
exports.SubAgentSourceResolver = SubAgentSourceResolver;
exports.SubAgentSourceResolver = SubAgentSourceResolver = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [agent_repository_1.AgentRepository, agent_history_repository_1.AgentHistoryRepository])
], SubAgentSourceResolver);
function getAgentRuntimeAssets(agent) {
const toolDescriptors = {};
const toolCodeByName = {};
for (const [toolId, toolEntry] of Object.entries(agent.tools ?? {})) {
toolDescriptors[toolId] = toolEntry.descriptor;
toolCodeByName[toolEntry.descriptor.name] = toolEntry.code;
}
return {
toolDescriptors,
toolCodeByName,
skills: agent.skills ?? {},
};
}
//# sourceMappingURL=sub-agent-source-resolver.js.map