Best Practices & Type Definitions

A summary of recommended patterns and the main TypeScript types for quick reference.

Best Practices

1. Check connection before sending messages

if (client.state.isConnected) {
  client.sendUserTextMessage('Hello');
}

2. Handle errors

try {
  await client.connect(config);
} catch (err) {
  console.error('Connection failed', err);
}

3. Unsubscribe from events when needed

const off = client.on('message', handler);
off();

4. Disconnect on cleanup

5. Keep UI responsive with stateChange


Core Types:

Control Interfaces:

Lipsync Types:

Configuration Interface:


Client State Management

The ConvaiClientState interface provides complete visibility into the conversation state:

Agent State Values:

  • 'disconnected' - Not connected

  • 'connected' - Connected but idle

  • 'listening' - Actively listening to user

  • 'thinking' - Processing user input

  • 'speaking' - Character is responding

Usage:


Event System

The SDK uses an event-driven architecture for state changes and messages:

Available Events:

Event
Parameters
Description

stateChange

(state: ConvaiClientState)

Connection/activity state changed

message

(message: ChatMessage)

New message received

messagesChange

(messages: ChatMessage[])

Message history updated

connect

()

Successfully connected

disconnect

()

Disconnected from character

error

(error: Error)

Error occurred

botReady

()

Bot is ready to receive messages

userTranscriptionChange

(transcription: string)

User speech transcription updated

Usage:

Last updated

Was this helpful?