UNPKG

manifest

Version:

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

171 lines 7.96 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 ModelPricingCacheService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModelPricingCacheService = void 0; const common_1 = require("@nestjs/common"); const schedule_1 = require("@nestjs/schedule"); const model_name_normalizer_1 = require("./model-name-normalizer"); const pricing_sync_service_1 = require("../database/pricing-sync.service"); const models_dev_sync_service_1 = require("../database/models-dev-sync.service"); const providers_1 = require("../common/constants/providers"); const provider_model_registry_service_1 = require("../model-discovery/provider-model-registry.service"); let ModelPricingCacheService = ModelPricingCacheService_1 = class ModelPricingCacheService { pricingSync; modelsDevSync; modelRegistry; logger = new common_1.Logger(ModelPricingCacheService_1.name); cache = new Map(); aliasMap = new Map(); constructor(pricingSync, modelsDevSync, modelRegistry) { this.pricingSync = pricingSync; this.modelsDevSync = modelsDevSync; this.modelRegistry = modelRegistry; } async onApplicationBootstrap() { await this.reload(); } async scheduledReload() { await this.reload(); } async reload() { this.cache.clear(); const orCache = this.pricingSync.getAll(); for (const [fullId, entry] of orCache) { const { provider, canonical, providerId } = this.resolveProviderAndName(fullId); const pricingEntry = { model_name: fullId, provider, input_price_per_token: entry.input, output_price_per_token: entry.output, display_name: entry.displayName ?? null, validated: this.resolveValidated(providerId, canonical), source: 'openrouter', }; this.cache.set(fullId, pricingEntry); if (canonical !== fullId && !this.cache.has(canonical)) { this.cache.set(canonical, pricingEntry); } } this.loadModelsDevEntries(); this.aliasMap = (0, model_name_normalizer_1.buildAliasMap)([...this.cache.keys()]); this.logger.log(`Loaded ${this.cache.size} pricing entries`); } getByModel(modelName) { const exact = this.cache.get(modelName); if (exact) return exact; const resolved = (0, model_name_normalizer_1.resolveModelName)(modelName, this.aliasMap); if (resolved) return this.cache.get(resolved); return undefined; } getAll() { const seen = new Set(); const result = []; for (const entry of this.cache.values()) { if (!seen.has(entry.model_name)) { seen.add(entry.model_name); result.push(entry); } } return result; } resolveProviderAndName(openRouterId) { if (openRouterId.startsWith('openrouter/')) { return { provider: 'OpenRouter', canonical: openRouterId, providerId: null }; } const slashIdx = openRouterId.indexOf('/'); if (slashIdx <= 0) { return { provider: 'OpenRouter', canonical: openRouterId, providerId: null }; } const prefix = openRouterId.substring(0, slashIdx); const providerDisplayName = providers_1.OPENROUTER_PREFIX_TO_PROVIDER.get(prefix); if (providerDisplayName) { return { provider: providerDisplayName, canonical: openRouterId.substring(slashIdx + 1), providerId: prefix, }; } return { provider: 'OpenRouter', canonical: openRouterId, providerId: null }; } loadModelsDevEntries() { if (!this.modelsDevSync) return; let count = 0; for (const [providerId, registryEntry] of providers_1.PROVIDER_BY_ID) { const models = this.modelsDevSync.getModelsForProvider(providerId); for (const model of models) { if (model.inputPricePerToken === null) continue; const pricingEntry = { model_name: model.id, provider: registryEntry.displayName, input_price_per_token: model.inputPricePerToken, output_price_per_token: model.outputPricePerToken, display_name: model.name || null, validated: this.resolveValidatedForModelsDev(providerId, model.id), source: 'models.dev', }; const existing = this.cache.get(model.id); const hasRealPricing = existing && (existing.input_price_per_token ?? 0) > 0; const isZeroPricing = (model.inputPricePerToken ?? 0) === 0 && (model.outputPricePerToken ?? 0) === 0; if (!hasRealPricing || !isZeroPricing) { this.cache.set(model.id, pricingEntry); } for (const prefix of registryEntry.openRouterPrefixes) { const prefixedKey = `${prefix}/${model.id}`; if (this.cache.has(prefixedKey)) { this.cache.set(prefixedKey, { ...pricingEntry, model_name: prefixedKey }); } } count++; } } if (count > 0) { this.logger.log(`Overlaid ${count} models.dev pricing entries`); } } resolveValidated(providerId, canonical) { if (!this.modelRegistry || !providerId) return undefined; const entry = providers_1.PROVIDER_BY_ID_OR_ALIAS.get(providerId); const canonicalProviderId = entry?.id ?? providerId; const result = this.modelRegistry.isModelConfirmed(canonicalProviderId, canonical); return result ?? undefined; } resolveValidatedForModelsDev(providerId, modelId) { if (!this.modelRegistry) return undefined; const result = this.modelRegistry.isModelConfirmed(providerId, modelId); return result ?? undefined; } }; exports.ModelPricingCacheService = ModelPricingCacheService; __decorate([ (0, schedule_1.Cron)('0 5 * * *'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], ModelPricingCacheService.prototype, "scheduledReload", null); exports.ModelPricingCacheService = ModelPricingCacheService = ModelPricingCacheService_1 = __decorate([ (0, common_1.Injectable)(), __param(1, (0, common_1.Optional)()), __param(1, (0, common_1.Inject)(models_dev_sync_service_1.ModelsDevSyncService)), __param(2, (0, common_1.Optional)()), __param(2, (0, common_1.Inject)(provider_model_registry_service_1.ProviderModelRegistryService)), __metadata("design:paramtypes", [pricing_sync_service_1.PricingSyncService, Object, Object]) ], ModelPricingCacheService); //# sourceMappingURL=model-pricing-cache.service.js.map