Fix: fetchMessageHistory con evento SSE y logs de debug
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m9s

- Agregar logs detallados en /api/debug/history/fetch para diagnosticar
- Emitir evento SSE 'history.synced' cuando llegan mensajes del historial
- Frontend ahora escucha el evento SSE en vez de timeout fijo de 3 segundos
- Agregar script y documentación para referencia de Baileys message history
This commit is contained in:
2025-12-04 12:33:22 -06:00
parent a9e786a8bd
commit 32f66c8fe0
7 changed files with 2004 additions and 10 deletions

View File

@@ -26,13 +26,31 @@ export default defineEventHandler(async (event) => {
throw createError({ statusCode: 400, message: 'Instance not connected' })
}
// Verificar que el método existe
if (typeof socket.fetchMessageHistory !== 'function') {
console.error('[History] fetchMessageHistory method not available on socket')
console.error('[History] Available methods:', Object.keys(socket).filter(k => typeof (socket as any)[k] === 'function').join(', '))
throw createError({ statusCode: 500, message: 'fetchMessageHistory not available on this Baileys version' })
}
// Log de parámetros
console.log('[History] Requesting history with params:', {
instanceId,
count,
oldestMsgKey: oldestMsgKey ? JSON.stringify(oldestMsgKey) : 'undefined',
oldestMsgTimestamp: oldestMsgTimestamp || 'undefined'
})
try {
await socket.fetchMessageHistory(count, oldestMsgKey, oldestMsgTimestamp)
const result = await socket.fetchMessageHistory(count, oldestMsgKey, oldestMsgTimestamp)
console.log('[History] fetchMessageHistory returned:', result)
return {
success: true,
result,
message: `Requested ${count} messages from history. Check messaging-history.set event for results.`
}
} catch (error) {
console.error('[History] fetchMessageHistory error:', error)
throw createError({
statusCode: 500,
message: `Failed to fetch message history: ${(error as Error).message}`