Conversations
A conversation is an interactive session between a user and an avatar.
Conversation Lifecycle
- Start – User loads the avatar or sends first message
- Active – Messages are exchanged back and forth
- Idle – No activity (timeout after 30 minutes)
- End – User closes the chat or session expires
Accessing Conversations
In React
import { useConversation } from '@avatarium/react';
function ChatComponent() {
const {
messages, // Array of all messages
isConnected, // WebSocket connection status
send, // Function to send a message
clear // Function to clear history
} = useConversation();
return (
<div>
{messages.map((msg) => (
<div key={msg.id} className={msg.role}>
{msg.content}
</div>
))}
</div>
);
}In JavaScript
const avatar = new Avatarium({ ... });
avatar.on('message', (message) => {
console.log(`${message.role}: ${message.content}`);
});
avatar.on('conversationStart', (conversationId) => {
console.log('Started:', conversationId);
});
avatar.on('conversationEnd', () => {
console.log('Conversation ended');
});Message Structure
Each message contains:
{
"id": "msg_abc123",
"role": "user" | "assistant",
"content": "Hello, how can I help?",
"timestamp": "2024-01-15T10:30:00Z",
"metadata": {
"tokens": 24,
"latency": 187
}
}Conversation Context
Avatars maintain context throughout the conversation:
- Short-term memory – Full context of current session
- System prompt – Your personality configuration
- User messages – Everything the user has said
- Assistant messages – All avatar responses
Context is automatically managed to fit within AI model limits.
Saving Conversations
Enable conversation persistence:
<Avatar
model="scarlett"
persistConversation={true}
conversationId="user-123-session"
/>Retrieve past conversations via API:
GET /api/v1/conversations/{conversationId}Conversation Events
| Event | Trigger | Data |
|---|---|---|
conversationStart | Session begins | { conversationId } |
message | New message | { id, role, content } |
typing | Avatar is thinking | { isTyping: true } |
error | Something went wrong | { error, code } |
conversationEnd | Session ends | { reason } |