42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { Conversacion, RolMensaje } from './conversacion.entity';
|
|
|
|
@Injectable()
|
|
export class ConversacionService {
|
|
constructor(
|
|
@InjectRepository(Conversacion)
|
|
private readonly convRepo: Repository<Conversacion>,
|
|
) {}
|
|
|
|
async guardarMensaje(
|
|
leadId: number,
|
|
rol: RolMensaje,
|
|
mensaje: string,
|
|
): Promise<Conversacion> {
|
|
const entry = this.convRepo.create({ lead_id: leadId, rol, mensaje });
|
|
return this.convRepo.save(entry);
|
|
}
|
|
|
|
async obtenerHistorial(leadId: number): Promise<Conversacion[]> {
|
|
return this.convRepo.find({
|
|
where: { lead_id: leadId },
|
|
order: { created_at: 'ASC' },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Devuelve el historial en formato OpenAI/Claude messages array.
|
|
*/
|
|
async obtenerHistorialComoMessages(
|
|
leadId: number,
|
|
): Promise<Array<{ role: string; content: string }>> {
|
|
const historial = await this.obtenerHistorial(leadId);
|
|
return historial.map((h) => ({
|
|
role: h.rol,
|
|
content: h.mensaje,
|
|
}));
|
|
}
|
|
}
|