The AI Copilots React hooks allow you to fetch, create, and modify AI chats, enabling you to build custom AI interfaces, even beyond our default components. Chats are stored permanently and the infrastructure is handled for you. All hooks work optimistically, meaning they update immediately, before the server has synched.

List a user’s chats

Each authenticated user has their own private set of chats, and the useAiChats hook fetches all AI chats created by the current user. It’s easy to create a list of chats, with links or buttons that take you to each.

Chats are paginated, returning 50 at a time, and you can filter chats by metadata. There are also ways to handle errors.

Create & delete chats

The useCreateAiChat and useDeleteAiChat hooks allow you to create and delete AI chats. When used in combination with the useAiChats hook, you can add “New Chat” and “Delete Chat” buttons to your listing.

When an AI chat is created, a title is automatically generated from the first messages. You can optionally set this title, and add custom metadata to the chat.

Send messages to a chat

The useSendAiMessage hook allows you to send messages directly to a chat, as if from a user. This works really well in combination with buttons in your UI, for example you may have a “Explain with AI” button.

Additionally, when using AiChat you can use the hook to display suggestion buttons in the empty state.

Get a chat’s information

The useAiChat hook allows you to fetch a chat’s title and custom metadata.

Get a chat’s status

The useAiChatStatus hook allows you to fetch the status of an AI chat, indicating whether it’s idle or currently generating content. This is helpful for displaying a loading state or freezing part of your app while content is generating.

Hook types

There are two different ways to use many Liveblocks hooks; with React Suspense, and without it. We recommend using the Suspense versions, as they often result in simpler code.

Suspense hooks

Using Suspense hooks means that any data retrieved, for example chats from useAiChats, will never be undefined, and your component will never see an error.

To catch errors and display a loading screen, you can use ErrorBoundary and ClientSideSuspense.

To use Suspense, make sure you’re exporting your hooks from @liveblocks/react/suspense.

Regular hooks

The regular versions of Liveblocks hooks require you to check for error and isLoading properties. You can then handle these states in the same component.

To use the regular hooks, make sure you’re exporting from @liveblocks/react.