Presence Tracking and Sticky Agent Memory
The assistant module employs continuous presence tracking and persistent persona memory to ensure seamless interactions across multiple platforms and sessions.
Presence Tracking (presence.go)
The system monitors user activity across all connected interfaces (Web, WhatsApp, Discord, IRC, Telegram).
Presence Data Structure
- LastSeen: Timestamp of the most recent interaction.
- Platform: The interface where the user was active (e.g.,
whatsapp). - AgentName: The persona the user last interacted with.
- ChatID: The active session ID.
Thread-Safe Operations
The in-memory presence map uses a sync.RWMutex to guarantee thread-safe updates from concurrent ingress points. The function UpdatePresence(userName, platform, agentName, chatID) ensures the system always knows where the user is.
Sticky Agent Memory (chat_persistence.go)
To avoid forcing the user to constantly mention their preferred agent (e.g., "@Nikki"), the system implements "Sticky Agent Memory."
Mechanism
- Persistence: The last-used agent for every user is saved to
chats/sticky_agents.json. - Resolution: When a new direct message (DM) arrives without an explicit mention, the system looks up the user's sticky agent and routes the message accordingly.
- Transition: The sticky agent is only updated when the user explicitly mentions a different agent in their message.
Welcome Home & Wakeup Routines
Presence detection interfaces with the wakeup.go API.
- UniFi Polling: The system polls the local UniFi network for known MAC addresses (e.g., Dan's phone, Liz's phone).
- Debounced Triggers: When a device connects, a 4-minute debounce prevents rapid flapping.
- Persona Routing: Based on the detected user, a "Welcome Home" routine is triggered, routing the event to their preferred agent (e.g., Dan → Nikki, Liz → Lenore).
Component Diagram: Presence & Sticky Memory
graph LR
U([User sends DM]) --> W[WhatsApp Ingress]
W --> Check{Agent Mentioned?}
Check -- No --> Sticky[Lookup sticky_agents.json]
Check -- Yes --> Update[Update sticky_agents.json]
Sticky --> Route[Route to Last Agent]
Update --> Route[Route to New Agent]
Route --> Session
Session --> Pres[UpdatePresence()]
Pres --> MemMap[(Presence sync.Map)]
Guidance for AI Agents
- Context Continuity: Because of sticky memory, assume the user is continuing the previous thread unless they explicitly change the subject. You are their persistent companion on that platform.
- Presence Awareness: Use the presence data to determine if a user is currently active before sending non-critical, asynchronous notifications.