96 lines
5.0 KiB
JavaScript
96 lines
5.0 KiB
JavaScript
"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 PipelineService_1;
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.PipelineService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const config_1 = require("@nestjs/config");
|
|
const prompt_builder_service_1 = require("./prompt-builder.service");
|
|
const image_generator_service_1 = require("./image-generator.service");
|
|
const supervisor_service_1 = require("./supervisor.service");
|
|
const reformix_service_1 = require("../reformix/reformix.service");
|
|
let PipelineService = PipelineService_1 = class PipelineService {
|
|
constructor(config, promptBuilder, imageGenerator, supervisor, reformix) {
|
|
this.config = config;
|
|
this.promptBuilder = promptBuilder;
|
|
this.imageGenerator = imageGenerator;
|
|
this.supervisor = supervisor;
|
|
this.reformix = reformix;
|
|
this.logger = new common_1.Logger(PipelineService_1.name);
|
|
this.maxRetries = this.config.get('MAX_RETRIES', 2);
|
|
this.minScore = this.config.get('SUPERVISOR_MIN_SCORE', 70);
|
|
}
|
|
async procesarLead(dto) {
|
|
const { leadId, reforma, zonas } = dto;
|
|
const zonasConFotos = zonas.filter((z) => z.fotos.antes.length > 0);
|
|
const zonasSaltadas = zonas.filter((z) => z.fotos.antes.length === 0);
|
|
this.logger.log(`[${leadId}] Iniciando pipeline para ${zonasConFotos.length} zonas`);
|
|
for (const z of zonasSaltadas) {
|
|
this.logger.log(`[${leadId}] Zona ${z.zona}: sin fotos "antes", saltando`);
|
|
}
|
|
const renders = [];
|
|
for (const zona of zonasConFotos) {
|
|
try {
|
|
const render = await this.procesarZona(leadId, zona.zona, reforma, zona.notas, zona.fotos.antes[0]);
|
|
renders.push(render);
|
|
}
|
|
catch (err) {
|
|
this.logger.error(`[${leadId}] Zona ${zona.zona}: error fatal: ${err.message}`);
|
|
}
|
|
}
|
|
if (renders.length === 0) {
|
|
this.logger.warn(`[${leadId}] No se generaron renders para ninguna zona`);
|
|
return;
|
|
}
|
|
const items = renders.map((r) => ({
|
|
zona: r.zona,
|
|
imagen: r.imagen,
|
|
}));
|
|
const ok = await this.reformix.entregarRenders(leadId, items);
|
|
if (ok) {
|
|
this.logger.log(`[${leadId}] Renders entregados correctamente (${renders.length} zonas)`);
|
|
}
|
|
else {
|
|
this.logger.error(`[${leadId}] Error entregando renders a la app principal`);
|
|
}
|
|
}
|
|
async procesarZona(leadId, zona, reforma, notas, fotoAntes) {
|
|
const prompt = await this.promptBuilder.generarPrompt(reforma.tipo, reforma.m2Suelo, reforma.calidad, notas);
|
|
this.logger.log(`[${leadId}] Zona ${zona}: prompt generado`);
|
|
let ultimaImagen = null;
|
|
for (let intento = 0; intento <= this.maxRetries; intento++) {
|
|
if (intento > 0) {
|
|
this.logger.log(`[${leadId}] Zona ${zona}: reintento ${intento} de ${this.maxRetries}`);
|
|
}
|
|
const imagen = await this.imageGenerator.generarRender(prompt, fotoAntes);
|
|
ultimaImagen = imagen;
|
|
this.logger.log(`[${leadId}] Zona ${zona}: imagen generada`);
|
|
const resultado = await this.supervisor.supervisar(reforma.tipo, reforma.m2Suelo, reforma.calidad, notas, fotoAntes, imagen);
|
|
const aprobada = resultado.aprobado && resultado.score >= this.minScore;
|
|
this.logger.log(`[${leadId}] Zona ${zona}: ${aprobada ? 'aprobada' : 'rechazada'} (score: ${resultado.score}) - ${resultado.motivo}`);
|
|
if (aprobada) {
|
|
return { zona, imagen, score: resultado.score, aprobada: true };
|
|
}
|
|
}
|
|
this.logger.warn(`[${leadId}] Zona ${zona}: usando ultimo render pese a no superar validacion`);
|
|
return { zona, imagen: ultimaImagen, score: 0, aprobada: false };
|
|
}
|
|
};
|
|
exports.PipelineService = PipelineService;
|
|
exports.PipelineService = PipelineService = PipelineService_1 = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [config_1.ConfigService,
|
|
prompt_builder_service_1.PromptBuilderService,
|
|
image_generator_service_1.ImageGeneratorService,
|
|
supervisor_service_1.SupervisorService,
|
|
reformix_service_1.ReformixService])
|
|
], PipelineService);
|
|
//# sourceMappingURL=pipeline.service.js.map
|