n8n
Version:
n8n Workflow Automation Tool
183 lines • 7.5 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.AgentRepository = void 0;
const di_1 = require("@n8n/di");
const typeorm_1 = require("@n8n/typeorm");
const agent_entity_1 = require("../entities/agent.entity");
let AgentRepository = class AgentRepository extends typeorm_1.Repository {
constructor(dataSource) {
super(agent_entity_1.Agent, dataSource.manager);
}
async findByProjectId(projectId) {
return await this.find({
where: { projectId },
relations: { activeVersion: true },
order: { updatedAt: 'DESC' },
});
}
async findSummariesByProjectIds(projectIds, options = {}) {
if (projectIds?.length === 0)
return [];
const query = this.createQueryBuilder('agent')
.select([
'agent.id',
'agent.name',
'agent.projectId',
'agent.activeVersionId',
'agent.availableInMCP',
'agent.updatedAt',
])
.orderBy('agent.updatedAt', 'DESC');
if (projectIds !== null) {
query.where('agent.projectId IN (:...projectIds)', { projectIds });
}
if (options.query) {
query.andWhere('LOWER(agent.name) LIKE LOWER(:query)', { query: `%${options.query}%` });
}
if (options.publishedOnly) {
query.andWhere('agent.activeVersionId IS NOT NULL');
}
if (options.excludeAgentId) {
query.andWhere('agent.id != :excludeAgentId', { excludeAgentId: options.excludeAgentId });
}
if (options.limit !== undefined) {
query.take(options.limit);
}
return await query.getMany();
}
async findByProjectIdsPaginated(projectIds, options, { withProject = false } = {}) {
if (projectIds?.length === 0)
return { count: 0, data: [] };
const query = this.createQueryBuilder('agent').leftJoinAndSelect('agent.activeVersion', 'activeVersion');
if (withProject) {
query.leftJoinAndSelect('agent.project', 'project');
}
if (projectIds !== null) {
query.where('agent.projectId IN (:...projectIds)', { projectIds });
}
this.applyFilters(query, options.filter);
this.applySorting(query, options.sortBy);
query.skip(options.skip).take(options.take);
const [data, count] = await query.getManyAndCount();
return { count, data };
}
applyFilters(query, filter) {
if (filter?.query) {
query.andWhere('LOWER(agent.name) LIKE LOWER(:query)', { query: `%${filter.query}%` });
}
if (filter?.availableInMCP !== undefined) {
query.andWhere('agent.availableInMCP = :availableInMCP', {
availableInMCP: filter.availableInMCP,
});
}
}
applySorting(query, sortBy) {
const [field = 'updatedAt', direction = 'desc'] = sortBy?.split(':') ?? [];
const sortDirection = direction.toLowerCase() === 'asc' ? 'ASC' : 'DESC';
if (field === 'name') {
query
.addSelect('LOWER(agent.name)', 'agent_name_lower')
.orderBy('agent_name_lower', sortDirection);
return;
}
query.orderBy(`agent.${field}`, sortDirection);
}
async findByIdAndProjectId(id, projectId) {
return await this.findOne({
where: { id, projectId },
relations: { activeVersion: true },
});
}
async findById(id) {
return await this.findOne({
where: { id },
relations: { activeVersion: true },
});
}
async findCredentialIndexAgentIdsBatch(afterId, batchSize) {
const query = this.createQueryBuilder('agent')
.select(['agent.id'])
.orderBy('agent.id', 'ASC')
.take(batchSize);
if (afterId !== null) {
query.where('agent.id > :afterId', { afterId });
}
return await query.getMany();
}
async findSummariesByIds(ids) {
if (ids.length === 0)
return [];
return await this.find({
select: ['id', 'name', 'projectId'],
where: { id: (0, typeorm_1.In)(ids) },
});
}
async findByIdInProjects(id, projectIds) {
if (projectIds.length === 0)
return null;
return await this.findOne({
where: { id, projectId: (0, typeorm_1.In)(projectIds) },
relations: { activeVersion: true },
});
}
async existsByIdAndProjectId(id, projectId) {
return await this.exists({ where: { id, projectId } });
}
async findByIdsAndProjectId(ids, projectId) {
if (ids.length === 0)
return [];
return await this.find({
select: ['id', 'activeVersionId'],
where: { id: (0, typeorm_1.In)(ids), projectId },
});
}
async findMcpAvailabilityCandidates(where) {
if ('ids' in where && where.ids.length === 0)
return [];
if ('projectIds' in where && where.projectIds.length === 0)
return [];
const criteria = 'ids' in where
? { id: (0, typeorm_1.In)(where.ids) }
: 'projectIds' in where
? { projectId: (0, typeorm_1.In)(where.projectIds) }
: undefined;
return await this.find({
select: ['id', 'projectId', 'availableInMCP'],
where: criteria,
});
}
async setAvailableInMCP(agentIds, availableInMCP) {
if (agentIds.length === 0)
return;
await this.update({ id: (0, typeorm_1.In)(agentIds) }, { availableInMCP });
}
async claimSetupCompleted(id, completedAt) {
const result = await this.update({ id, setupCompletedAt: (0, typeorm_1.IsNull)() }, { setupCompletedAt: completedAt });
return (result.affected ?? 0) > 0;
}
async findPublished() {
return await this.createQueryBuilder('agent')
.innerJoinAndSelect('agent.activeVersion', 'activeVersion')
.getMany();
}
async findByIntegrationCredential(type, credentialId, projectId, excludeAgentId) {
const agents = await this.find({ where: { projectId } });
return agents.filter((agent) => agent.id !== excludeAgentId &&
(agent.integrations ?? []).some((i) => i.type === type && i.credentialId === credentialId));
}
};
exports.AgentRepository = AgentRepository;
exports.AgentRepository = AgentRepository = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [typeorm_1.DataSource])
], AgentRepository);
//# sourceMappingURL=agent.repository.js.map