Files
reformix-hackaton/mvp/image-worker/dist/pipeline/supervisor.service.js
2026-06-07 18:11:44 -04:00

104 lines
4.7 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 SupervisorService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupervisorService = void 0;
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const fs = require("fs");
const path = require("path");
const axios_1 = require("axios");
const OPENROUTER_URL = 'https://openrouter.ai/api/v1/chat/completions';
let SupervisorService = SupervisorService_1 = class SupervisorService {
constructor(config) {
this.config = config;
this.logger = new common_1.Logger(SupervisorService_1.name);
this.systemPrompt = '';
const ruta = path.join(process.cwd(), 'prompts', 'supervisor.txt');
if (fs.existsSync(ruta)) {
this.systemPrompt = fs.readFileSync(ruta, 'utf-8');
}
else {
this.logger.warn('prompts/supervisor.txt no encontrado, usando default');
}
}
async supervisar(tipoReforma, m2Suelo, calidad, notas, fotoAntes, renderDespues) {
const apiKey = this.config.get('OPENROUTER_API_KEY');
const model = this.config.get('OPENROUTER_MODEL_TEXTO', 'anthropic/claude-3.5-haiku-20241022');
const notasTexto = notas.join('; ') || 'sin notas';
try {
const response = await axios_1.default.post(OPENROUTER_URL, {
model,
messages: [
{ role: 'system', content: this.systemPrompt },
{
role: 'user',
content: [
{
type: 'text',
text: `Reforma tipo: ${tipoReforma}\nMetros: ${m2Suelo ?? 'desconocido'}\nCalidad: ${calidad}\nNotas del cliente: ${notasTexto}`,
},
{
type: 'image_url',
image_url: { url: fotoAntes },
},
{
type: 'image_url',
image_url: { url: renderDespues },
},
],
},
],
max_tokens: 256,
temperature: 0.2,
}, {
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://reformix.es',
'X-Title': 'Reformix Image Worker',
},
});
const textContent = response.data.choices?.[0]?.message?.content?.trim();
if (!textContent) {
return { aprobado: false, score: 0, motivo: 'Modelo devolvio respuesta vacia' };
}
return this.parsearRespuesta(textContent);
}
catch (err) {
this.logger.error(`Error en supervisor: ${err.message}`);
return { aprobado: false, score: 0, motivo: `Error del supervisor: ${err.message}` };
}
}
parsearRespuesta(texto) {
const jsonMatch = texto.match(/\{[^{}]*"aprobado"[^{}]*\}/i);
if (!jsonMatch) {
return { aprobado: false, score: 0, motivo: 'Error parseando respuesta del supervisor' };
}
try {
const parsed = JSON.parse(jsonMatch[0]);
return {
aprobado: Boolean(parsed.aprobado),
score: Math.min(100, Math.max(0, Number(parsed.score) || 0)),
motivo: String(parsed.motivo || 'Sin motivo'),
};
}
catch {
return { aprobado: false, score: 0, motivo: 'Error parseando respuesta del supervisor' };
}
}
};
exports.SupervisorService = SupervisorService;
exports.SupervisorService = SupervisorService = SupervisorService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [config_1.ConfigService])
], SupervisorService);
//# sourceMappingURL=supervisor.service.js.map