manifest
Version:
Self-hosted Manifest LLM router with embedded server, SQLite database, and dashboard
138 lines • 6.6 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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var TierAutoAssignService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TierAutoAssignService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const tier_assignment_entity_1 = require("../../entities/tier-assignment.entity");
const model_discovery_service_1 = require("../../model-discovery/model-discovery.service");
const crypto_1 = require("crypto");
const types_1 = require("../../scoring/types");
function filterSubModels(models) {
const byProvider = new Map();
for (const m of models) {
const key = m.provider.toLowerCase();
const arr = byProvider.get(key) ?? [];
arr.push(m);
byProvider.set(key, arr);
}
const result = [];
for (const providerModels of byProvider.values()) {
const zeroCost = providerModels.filter((m) => m.inputPricePerToken != null &&
m.outputPricePerToken != null &&
m.inputPricePerToken === 0 &&
m.outputPricePerToken === 0);
result.push(...(zeroCost.length > 0 ? zeroCost : providerModels));
}
return result;
}
let TierAutoAssignService = TierAutoAssignService_1 = class TierAutoAssignService {
discoveryService;
tierRepo;
logger = new common_1.Logger(TierAutoAssignService_1.name);
constructor(discoveryService, tierRepo) {
this.discoveryService = discoveryService;
this.tierRepo = tierRepo;
}
async recalculate(agentId) {
const allModels = await this.discoveryService.getModelsForAgent(agentId);
const subModels = filterSubModels(allModels.filter((m) => m.authType === 'subscription'));
const keyModels = allModels.filter((m) => m.authType !== 'subscription');
const allTiers = await this.tierRepo.find({ where: { agent_id: agentId } });
const tierMap = new Map(allTiers.map((t) => [t.tier, t]));
const toSave = [];
const toInsert = [];
for (const tier of types_1.TIERS) {
const best = this.pickBest(subModels, tier) ?? this.pickBest(keyModels, tier);
const existing = tierMap.get(tier);
if (existing) {
existing.auto_assigned_model = best?.model_name ?? null;
existing.updated_at = new Date().toISOString();
toSave.push(existing);
}
else {
toInsert.push({
id: (0, crypto_1.randomUUID)(),
user_id: '',
agent_id: agentId,
tier,
override_model: null,
override_provider: null,
auto_assigned_model: best?.model_name ?? null,
});
}
}
if (toSave.length > 0)
await this.tierRepo.save(toSave);
if (toInsert.length > 0)
await this.tierRepo.insert(toInsert);
this.logger.log(`Recalculated tier assignments for agent ${agentId}`);
}
pickBest(models, tier) {
if (models.length === 0)
return null;
const totalPrice = (m) => {
if (m.inputPricePerToken == null || m.outputPricePerToken == null)
return Infinity;
return Number(m.inputPricePerToken) + Number(m.outputPricePerToken);
};
const quality = (m) => m.qualityScore ?? 3;
const byPrice = [...models].sort((a, b) => totalPrice(a) - totalPrice(b));
let picked = byPrice[0];
switch (tier) {
case 'simple': {
picked = byPrice[0];
break;
}
case 'standard': {
const eligible = byPrice.filter((m) => quality(m) >= 2);
const pool = eligible.length > 0 ? eligible : byPrice;
const toolCapable = pool.filter((m) => m.capabilityCode);
picked = toolCapable.length > 0 ? toolCapable[0] : pool[0];
break;
}
case 'complex': {
const byQuality = [...byPrice].sort((a, b) => quality(b) - quality(a));
const toolCapable = byQuality.filter((m) => m.capabilityCode);
picked = toolCapable.length > 0 ? toolCapable[0] : byQuality[0];
break;
}
case 'reasoning': {
const reasoningModels = byPrice.filter((m) => m.capabilityReasoning);
if (reasoningModels.length > 0) {
const byQuality = [...reasoningModels].sort((a, b) => quality(b) - quality(a));
const toolCapable = byQuality.filter((m) => m.capabilityCode);
picked = toolCapable.length > 0 ? toolCapable[0] : byQuality[0];
}
else {
const byQuality = [...byPrice].sort((a, b) => quality(b) - quality(a));
const toolCapable = byQuality.filter((m) => m.capabilityCode);
picked = toolCapable.length > 0 ? toolCapable[0] : byQuality[0];
}
break;
}
}
return { model_name: picked.id, score: quality(picked) };
}
};
exports.TierAutoAssignService = TierAutoAssignService;
exports.TierAutoAssignService = TierAutoAssignService = TierAutoAssignService_1 = __decorate([
(0, common_1.Injectable)(),
__param(1, (0, typeorm_1.InjectRepository)(tier_assignment_entity_1.TierAssignment)),
__metadata("design:paramtypes", [model_discovery_service_1.ModelDiscoveryService,
typeorm_2.Repository])
], TierAutoAssignService);
//# sourceMappingURL=tier-auto-assign.service.js.map