Create natural conversation flow with AutoTurn - automatically split long responses into multiple messages with realistic typing delays. Mimic human communication patterns to make AI interactions feel genuinely engaging.
Important: AutoTurn automatically disables streaming mode. When AutoTurn is enabled, the SDK uses non-streaming responses to properly handle response splitting and turn management. Choose either streaming OR conversational turns based on your use case.
import { AnimusClient } from 'animus-client';const client = new AnimusClient({ tokenProviderUrl: 'https://your-backend.com/api/get-animus-token', chat: { model: 'vivian-llama3.1-70b-1.0-fp8', systemMessage: 'You are a helpful assistant.', autoTurn: true // Enable with default settings }});// Listen for message eventsclient.on('messageStart', (data) => { console.log(`Starting ${data.messageType} message: ${data.content}`); if (data.messageType === 'auto' && data.turnIndex !== undefined) { console.log(`Turn ${data.turnIndex + 1}/${data.totalTurns}`); }});client.on('messageComplete', (data) => { console.log(`Completed ${data.messageType} message: ${data.content}`); // Update your UI here displayMessage(data.content);});// Send message - may be split into multiple turns with natural delaysclient.chat.send("I can't decide whether to dine-in or do takeout tonight. What do you think?");
// Example of how the SDK processes a long responseconst longResponse = `Renewable energy is crucial for our future. Solar power harnesses sunlight through photovoltaic cells, converting it directly into electricity.Wind energy captures kinetic energy from moving air using turbines, making it one of the fastest-growing energy sources globally.`;// With autoTurn enabled, this becomes:// Turn 1: "Renewable energy is crucial for our future."// Turn 2: "Solar power harnesses sunlight through photovoltaic cells, converting it directly into electricity."// Turn 3: "Wind energy captures kinetic energy from moving air using turbines, making it one of the fastest-growing energy sources globally."
Message cancellation is automatically handled when users send new messages:
Copy
// Scenario: User interrupts ongoing conversation turnsclient.chat.send("I'm thinking about getting a pet"); // Starts auto-turn sequence// While turns are being processed, user sends another messageclient.chat.send("Actually, tell me about solar panels instead");// Result:// - Any unprocessed turns from first message are canceled// - Already-processed turns remain in history for context// - Second message is processed normally// - No out-of-order responses occur
client.on('messageError', (data) => { if (data.messageType === 'auto' && data.error.includes('Canceled')) { console.log('Auto-turn messages were canceled due to new user input'); // Update UI to reflect cancellation clearPendingMessages(); } if (data.messageType === 'followup' && data.error.includes('Canceled')) { console.log('Follow-up request was canceled due to new user input'); }});
AutoTurn coordinates with automatic image generation:
Copy
const client = new AnimusClient({ tokenProviderUrl: 'your-token-url', chat: { model: 'vivian-llama3.1-70b-1.0-fp8', systemMessage: 'You are a helpful assistant.', autoTurn: true, check_image_generation: true // Enable automatic image generation }});// Image generation events work seamlessly with auto-turnclient.on('imageGenerationStart', (data) => { console.log(`Starting image generation: ${data.prompt}`); showImageGenerationIndicator();});client.on('imageGenerationComplete', (data) => { console.log(`Image generated: ${data.imageUrl}`); displayGeneratedImage(data.imageUrl); hideImageGenerationIndicator();});// Send message that might trigger both auto-turn and image generationclient.chat.send("You have any more hiking pics to share from last week?");
AutoTurn messages are automatically added to chat history with proper metadata:
Copy
// Get chat history including auto-turn messagesconst history = client.chat.getChatHistory();// Auto-turn messages include group metadatahistory.forEach(message => { if (message.groupId) { console.log(`Message ${message.messageIndex + 1}/${message.totalInGroup} in group ${message.groupId}`); }});