fix(chat): melhora confiabilidade da deteccao de novas mensagens
- Implementa deteccao dual: timestamp (lastActivityAt) + contador - Adiciona persistencia de estado em ~/.local/share/Raven/chat-state.json - Corrige race condition no servidor com refetch antes do patch - Adiciona campo lastAgentMessageAt no schema do Convex - Adiciona logs de diagnostico detalhados 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
c4664ab1c7
commit
2293a0275a
5 changed files with 310 additions and 30 deletions
|
|
@ -478,6 +478,7 @@ export default defineSchema({
|
|||
startedAt: v.number(),
|
||||
endedAt: v.optional(v.number()),
|
||||
lastActivityAt: v.number(),
|
||||
lastAgentMessageAt: v.optional(v.number()), // Timestamp da ultima mensagem do agente (para deteccao confiavel)
|
||||
unreadByMachine: v.optional(v.number()),
|
||||
unreadByAgent: v.optional(v.number()),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -3734,6 +3734,8 @@ export const postChatMessage = mutation({
|
|||
await ctx.db.patch(ticketId, { updatedAt: now })
|
||||
|
||||
// Se o autor for um agente (ADMIN, MANAGER, AGENT), incrementar unreadByMachine na sessao de chat ativa
|
||||
// IMPORTANTE: Buscar sessao IMEDIATAMENTE antes do patch para evitar race conditions
|
||||
// O Convex faz retry automatico em caso de OCC conflict
|
||||
const actorRole = participant.role?.toUpperCase() ?? ""
|
||||
if (["ADMIN", "MANAGER", "AGENT"].includes(actorRole)) {
|
||||
const activeSession = await ctx.db
|
||||
|
|
@ -3743,10 +3745,15 @@ export const postChatMessage = mutation({
|
|||
.first()
|
||||
|
||||
if (activeSession) {
|
||||
await ctx.db.patch(activeSession._id, {
|
||||
unreadByMachine: (activeSession.unreadByMachine ?? 0) + 1,
|
||||
lastActivityAt: now,
|
||||
})
|
||||
// Refetch para garantir valor mais recente (OCC protection)
|
||||
const freshSession = await ctx.db.get(activeSession._id)
|
||||
if (freshSession) {
|
||||
await ctx.db.patch(activeSession._id, {
|
||||
unreadByMachine: (freshSession.unreadByMachine ?? 0) + 1,
|
||||
lastActivityAt: now,
|
||||
lastAgentMessageAt: now, // Novo: timestamp da ultima mensagem do agente
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue