|
@@ -1,25 +1,29 @@
|
|
|
import { Message } from '../types'
|
|
import { Message } from '../types'
|
|
|
|
|
|
|
|
-export function generateReportFromMessages(messages: Message[]): string {
|
|
|
|
|
|
|
+export function generateReportFromMessages(messages: Message[], chatType: 'medical' | 'psychological' = 'medical'): string {
|
|
|
if (!messages || messages.length === 0) {
|
|
if (!messages || messages.length === 0) {
|
|
|
return "No hay mensajes para generar un reporte."
|
|
return "No hay mensajes para generar un reporte."
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Generar un resumen médico basado en los mensajes
|
|
|
|
|
- let report = "## REPORTE MÉDICO\n\n"
|
|
|
|
|
|
|
+ const reportTitle = chatType === 'psychological' ? 'REPORTE PSICOLÓGICO' : 'REPORTE MÉDICO';
|
|
|
|
|
+ const userLabel = chatType === 'psychological' ? 'USUARIO' : 'PACIENTE';
|
|
|
|
|
+ const professionalType = chatType === 'psychological' ? 'psicólogo' : 'médico';
|
|
|
|
|
+
|
|
|
|
|
+ // Generar un resumen basado en los mensajes
|
|
|
|
|
+ let report = `## ${reportTitle}\n\n`
|
|
|
report += `**Fecha:** ${new Date().toLocaleDateString('es-ES')}\n`
|
|
report += `**Fecha:** ${new Date().toLocaleDateString('es-ES')}\n`
|
|
|
report += `**Hora:** ${new Date().toLocaleTimeString('es-ES')}\n\n`
|
|
report += `**Hora:** ${new Date().toLocaleTimeString('es-ES')}\n\n`
|
|
|
|
|
|
|
|
// Resumen de la consulta
|
|
// Resumen de la consulta
|
|
|
report += "### RESUMEN DE LA CONSULTA\n"
|
|
report += "### RESUMEN DE LA CONSULTA\n"
|
|
|
- report += `El paciente realizó ${messages.filter(msg => msg.role === "user").length} consulta(s) con el asistente virtual.\n\n`
|
|
|
|
|
|
|
+ report += `El ${userLabel.toLowerCase()} realizó ${messages.filter(msg => msg.role === "user").length} consulta(s) con el asistente virtual.\n\n`
|
|
|
|
|
|
|
|
// Incluir toda la conversación completa
|
|
// Incluir toda la conversación completa
|
|
|
report += "### CONVERSACIÓN COMPLETA\n"
|
|
report += "### CONVERSACIÓN COMPLETA\n"
|
|
|
report += "────────────────────────────────────────────────────────\n\n"
|
|
report += "────────────────────────────────────────────────────────\n\n"
|
|
|
|
|
|
|
|
messages.forEach((message, index) => {
|
|
messages.forEach((message, index) => {
|
|
|
- const role = message.role === "user" ? "**PACIENTE**" : "**ASISTENTE**"
|
|
|
|
|
|
|
+ const role = message.role === "user" ? `**${userLabel}**` : "**ASISTENTE**"
|
|
|
const timestamp = new Date().toLocaleTimeString('es-ES', {
|
|
const timestamp = new Date().toLocaleTimeString('es-ES', {
|
|
|
hour: '2-digit',
|
|
hour: '2-digit',
|
|
|
minute: '2-digit'
|
|
minute: '2-digit'
|
|
@@ -31,25 +35,39 @@ export function generateReportFromMessages(messages: Message[]): string {
|
|
|
|
|
|
|
|
report += "────────────────────────────────────────────────────────\n\n"
|
|
report += "────────────────────────────────────────────────────────\n\n"
|
|
|
|
|
|
|
|
- // Analizar síntomas mencionados
|
|
|
|
|
|
|
+ // Analizar síntomas/temas mencionados
|
|
|
const userMessages = messages.filter(msg => msg.role === "user").map(msg => msg.content)
|
|
const userMessages = messages.filter(msg => msg.role === "user").map(msg => msg.content)
|
|
|
- const symptoms = extractSymptoms(userMessages.join(" "))
|
|
|
|
|
- if (symptoms.length > 0) {
|
|
|
|
|
- report += "### SÍNTOMAS IDENTIFICADOS\n"
|
|
|
|
|
- symptoms.forEach(symptom => {
|
|
|
|
|
- report += `- ${symptom}\n`
|
|
|
|
|
- })
|
|
|
|
|
- report += "\n"
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (chatType === 'psychological') {
|
|
|
|
|
+ // Para reportes psicológicos, extraer temas emocionales
|
|
|
|
|
+ const topics = extractPsychologicalTopics(userMessages.join(" "))
|
|
|
|
|
+ if (topics.length > 0) {
|
|
|
|
|
+ report += "### TEMAS DISCUTIDOS\n"
|
|
|
|
|
+ topics.forEach(topic => {
|
|
|
|
|
+ report += `- ${topic}\n`
|
|
|
|
|
+ })
|
|
|
|
|
+ report += "\n"
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Para reportes médicos, extraer síntomas
|
|
|
|
|
+ const symptoms = extractSymptoms(userMessages.join(" "))
|
|
|
|
|
+ if (symptoms.length > 0) {
|
|
|
|
|
+ report += "### SÍNTOMAS IDENTIFICADOS\n"
|
|
|
|
|
+ symptoms.forEach(symptom => {
|
|
|
|
|
+ report += `- ${symptom}\n`
|
|
|
|
|
+ })
|
|
|
|
|
+ report += "\n"
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // Principales temas discutidos
|
|
|
|
|
- const topics = extractTopics(userMessages.join(" "))
|
|
|
|
|
- if (topics.length > 0) {
|
|
|
|
|
- report += "### TEMAS PRINCIPALES\n"
|
|
|
|
|
- topics.forEach(topic => {
|
|
|
|
|
- report += `- ${topic}\n`
|
|
|
|
|
- })
|
|
|
|
|
- report += "\n"
|
|
|
|
|
|
|
+ // Principales temas discutidos
|
|
|
|
|
+ const topics = extractTopics(userMessages.join(" "))
|
|
|
|
|
+ if (topics.length > 0) {
|
|
|
|
|
+ report += "### TEMAS PRINCIPALES\n"
|
|
|
|
|
+ topics.forEach(topic => {
|
|
|
|
|
+ report += `- ${topic}\n`
|
|
|
|
|
+ })
|
|
|
|
|
+ report += "\n"
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Resumen de recomendaciones del asistente
|
|
// Resumen de recomendaciones del asistente
|
|
@@ -75,14 +93,17 @@ export function generateReportFromMessages(messages: Message[]): string {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Información adicional basada en el contenido
|
|
// Información adicional basada en el contenido
|
|
|
- const additionalInfo = extractAdditionalInfo(userMessages.join(" "))
|
|
|
|
|
|
|
+ const additionalInfo = extractAdditionalInfo(userMessages.join(" "), chatType)
|
|
|
if (additionalInfo) {
|
|
if (additionalInfo) {
|
|
|
report += "### INFORMACIÓN ADICIONAL\n"
|
|
report += "### INFORMACIÓN ADICIONAL\n"
|
|
|
report += `${additionalInfo}\n\n`
|
|
report += `${additionalInfo}\n\n`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- report += "**NOTA:** Este es un reporte generado automáticamente por el asistente virtual. "
|
|
|
|
|
- report += "Se recomienda consultar con un profesional médico para un diagnóstico completo.\n\n"
|
|
|
|
|
|
|
+ const noteText = chatType === 'psychological'
|
|
|
|
|
+ ? "**NOTA:** Este es un reporte generado automáticamente por el asistente virtual de apoyo psicológico. Se recomienda consultar con un profesional de la salud mental para un diagnóstico y tratamiento completo."
|
|
|
|
|
+ : "**NOTA:** Este es un reporte generado automáticamente por el asistente virtual. Se recomienda consultar con un profesional médico para un diagnóstico completo."
|
|
|
|
|
+
|
|
|
|
|
+ report += noteText + "\n\n"
|
|
|
report += "### FIN DEL REPORTE"
|
|
report += "### FIN DEL REPORTE"
|
|
|
|
|
|
|
|
return report
|
|
return report
|
|
@@ -120,6 +141,23 @@ function extractTopics(text: string): string[] {
|
|
|
return foundTopics
|
|
return foundTopics
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function extractPsychologicalTopics(text: string): string[] {
|
|
|
|
|
+ const psychologicalTopics = [
|
|
|
|
|
+ "ansiedad", "estrés", "depresión", "autoestima", "relaciones",
|
|
|
|
|
+ "familia", "trabajo", "estudios", "emociones", "sentimientos",
|
|
|
|
|
+ "tristeza", "miedo", "enojo", "frustración", "soledad",
|
|
|
|
|
+ "confianza", "inseguridad", "motivación", "sueño", "descanso",
|
|
|
|
|
+ "preocupación", "pánico", "fobia", "trauma", "duelo",
|
|
|
|
|
+ "comunicación", "conflicto", "límites", "autocuidado", "bienestar"
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
|
|
+ const foundTopics = psychologicalTopics.filter(topic =>
|
|
|
|
|
+ text.toLowerCase().includes(topic)
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ return foundTopics
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function extractRecommendations(text: string): string[] {
|
|
function extractRecommendations(text: string): string[] {
|
|
|
const recommendations: string[] = []
|
|
const recommendations: string[] = []
|
|
|
const lines = text.split('\n')
|
|
const lines = text.split('\n')
|
|
@@ -171,9 +209,24 @@ function extractRecommendations(text: string): string[] {
|
|
|
return recommendations.slice(0, 5) // Máximo 5 recomendaciones
|
|
return recommendations.slice(0, 5) // Máximo 5 recomendaciones
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function extractAdditionalInfo(text: string): string | null {
|
|
|
|
|
|
|
+function extractAdditionalInfo(text: string, chatType: 'medical' | 'psychological' = 'medical'): string | null {
|
|
|
const lowerText = text.toLowerCase()
|
|
const lowerText = text.toLowerCase()
|
|
|
|
|
|
|
|
|
|
+ if (chatType === 'psychological') {
|
|
|
|
|
+ // Detectar si es una situación de crisis
|
|
|
|
|
+ if (lowerText.includes("suicid") || lowerText.includes("hacerme daño") || lowerText.includes("matarme")) {
|
|
|
|
|
+ return "Se identificó contenido relacionado con crisis emocional. Se recomienda atención profesional inmediata."
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Detectar si es consulta de urgencia emocional
|
|
|
|
|
+ if (lowerText.includes("crisis") || lowerText.includes("urgente") || lowerText.includes("ataque de pánico")) {
|
|
|
|
|
+ return "Consulta de urgencia emocional identificada. Se recomienda apoyo profesional."
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return null
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Para reportes médicos
|
|
|
// Detectar si es una consulta de emergencia
|
|
// Detectar si es una consulta de emergencia
|
|
|
if (lowerText.includes("emergencia") || lowerText.includes("urgente")) {
|
|
if (lowerText.includes("emergencia") || lowerText.includes("urgente")) {
|
|
|
return "Se identificó como consulta de emergencia. Se recomienda atención médica inmediata."
|
|
return "Se identificó como consulta de emergencia. Se recomienda atención médica inmediata."
|