Skip to main content

Overview

The useLopusChat hook is a powerful tool for integrating Lopus AI’s generative UI capabilities into your React application. It manages Socket.io connections, message handling, and provides utilities for building AI-powered interfaces.

Message Types

The hook uses a type-safe message system to distinguish between sent and received messages. Understanding these types is crucial for effective message handling.

Received Messages (Server → Client)

These messages are sent from the server to the client and can be of the following types:

Sent Messages (Client → Server)

These messages are sent from the client to the server:

Type Guards

Type guards are essential for safely handling different message types. Use them to ensure type safety in your application.

Best Practices

  1. Type Safety: Always use type guards (isReceivedMessage, isSentMessage) to handle different message types safely.
  2. Error Handling: Wrap sendQuery and sendSubmission calls in try-catch blocks to manage errors effectively.
  3. Message Filtering: Use getMessagesByType with the appropriate message type enum for type-safe filtering.
  4. Component Handling: Remember that component messages contain structured data that needs to be rendered.
  5. Cleanup: The hook handles cleanup automatically, but ensure to clean up any custom message handlers.

Examples

Handling Different Message Types

Error Handling

TypeScript Support

The hook provides full TypeScript support with discriminated unions for message types:
This structure should help you understand and utilize the useLopusChat hook effectively, focusing on message types and best practices.