UNPKG

manifest

Version:

Self-hosted Manifest LLM router with embedded server, SQLite database, and dashboard

161 lines 7 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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var CustomProviderService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomProviderService = void 0; const common_1 = require("@nestjs/common"); const typeorm_1 = require("@nestjs/typeorm"); const typeorm_2 = require("typeorm"); const crypto_1 = require("crypto"); const custom_provider_entity_1 = require("../../entities/custom-provider.entity"); const provider_service_1 = require("../routing-core/provider.service"); const routing_cache_service_1 = require("../routing-core/routing-cache.service"); const tier_auto_assign_service_1 = require("../routing-core/tier-auto-assign.service"); const url_validation_1 = require("../../common/utils/url-validation"); let CustomProviderService = CustomProviderService_1 = class CustomProviderService { repo; providerService; routingCache; autoAssign; constructor(repo, providerService, routingCache, autoAssign) { this.repo = repo; this.providerService = providerService; this.routingCache = routingCache; this.autoAssign = autoAssign; } static providerKey(id) { return `custom:${id}`; } static modelKey(id, modelName) { return `custom:${id}/${modelName}`; } static rawModelName(prefixedName) { const slash = prefixedName.indexOf('/'); return slash !== -1 ? prefixedName.substring(slash + 1) : prefixedName; } static isCustom(provider) { return provider.startsWith('custom:'); } static extractId(providerKey) { return providerKey.replace('custom:', ''); } async list(agentId) { const cached = this.routingCache.getCustomProviders(agentId); if (cached) return cached; const result = await this.repo.find({ where: { agent_id: agentId } }); this.routingCache.setCustomProviders(agentId, result); return result; } async create(agentId, userId, dto) { const existing = await this.repo.findOne({ where: { agent_id: agentId, name: dto.name }, }); if (existing) { throw new common_1.ConflictException(`Custom provider "${dto.name}" already exists for this agent`); } try { await (0, url_validation_1.validatePublicUrl)(dto.base_url); } catch (err) { throw new common_1.BadRequestException(err.message); } const id = (0, crypto_1.randomUUID)(); const provKey = CustomProviderService_1.providerKey(id); const cp = Object.assign(new custom_provider_entity_1.CustomProvider(), { id, agent_id: agentId, user_id: userId, name: dto.name, base_url: dto.base_url, models: dto.models.map((m) => ({ model_name: m.model_name, input_price_per_million_tokens: m.input_price_per_million_tokens, output_price_per_million_tokens: m.output_price_per_million_tokens, context_window: m.context_window ?? 128000, })), created_at: new Date().toISOString(), }); await this.repo.insert(cp); await this.providerService.upsertProvider(agentId, userId, provKey, dto.apiKey); return cp; } async update(agentId, id, userId, dto) { const cp = await this.repo.findOne({ where: { id, agent_id: agentId } }); if (!cp) { throw new common_1.NotFoundException('Custom provider not found'); } if (dto.name !== undefined && dto.name !== cp.name) { const dup = await this.repo.findOne({ where: { agent_id: agentId, name: dto.name }, }); if (dup) { throw new common_1.ConflictException(`Custom provider "${dto.name}" already exists for this agent`); } cp.name = dto.name; } if (dto.base_url !== undefined) { try { await (0, url_validation_1.validatePublicUrl)(dto.base_url); } catch (err) { throw new common_1.BadRequestException(err.message); } cp.base_url = dto.base_url; } if (dto.models !== undefined) { cp.models = dto.models.map((m) => ({ model_name: m.model_name, input_price_per_million_tokens: m.input_price_per_million_tokens, output_price_per_million_tokens: m.output_price_per_million_tokens, context_window: m.context_window ?? 128000, })); } if ('apiKey' in dto) { await this.providerService.upsertProvider(agentId, userId, CustomProviderService_1.providerKey(id), dto.apiKey); } if (dto.models !== undefined && !('apiKey' in dto)) { await this.autoAssign.recalculate(agentId); } await this.repo.save(cp); this.routingCache.invalidateAgent(agentId); return cp; } async remove(agentId, id) { const cp = await this.repo.findOne({ where: { id, agent_id: agentId } }); if (!cp) { throw new common_1.NotFoundException('Custom provider not found'); } const provKey = CustomProviderService_1.providerKey(id); try { await this.providerService.removeProvider(agentId, provKey); } catch { } await this.repo.remove(cp); } async getById(id) { return this.repo.findOne({ where: { id } }); } }; exports.CustomProviderService = CustomProviderService; exports.CustomProviderService = CustomProviderService = CustomProviderService_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, typeorm_1.InjectRepository)(custom_provider_entity_1.CustomProvider)), __metadata("design:paramtypes", [typeorm_2.Repository, provider_service_1.ProviderService, routing_cache_service_1.RoutingCacheService, tier_auto_assign_service_1.TierAutoAssignService]) ], CustomProviderService); //# sourceMappingURL=custom-provider.service.js.map