Usage
React Hooks
The @chainlit/react-client
package provides several React hooks to manage various aspects of your chat application seamlessly:
useChatSession
: Manages the chat session’s connection to the WebSocket server.useChatMessages
: Manages retrieval and rendering of chat messages.useChatData
: Accesses chat-related data and states.useChatInteract
: Provides methods to interact with the chat system.useAuth
: Handles authentication processes.useApi
: Simplifies API interactions with built-in support for data fetching and error handling.
useChatSession
Hook
This hook is responsible for managing the chat session’s connection to the WebSocket server.
Methods
connect
: Establishes a connection to the WebSocket server.disconnect
: Disconnects from the WebSocket server.setChatProfile
: Sets the chat profile state.
Example
useChatMessages
Hook
The useChatMessages
hook provides access to the current chat messages, the first user interaction, and the active thread ID within your React application. It leverages Recoil for state management, ensuring that your components reactively update in response to state changes.
Returned Values
threadId
(string | undefined
):
The identifier of the current chat thread.messages
(IStep[]
):
An array of chat messages.firstInteraction
(string | undefined
):
The content of the first user-initiated interaction.
Example
useChatData
Hook
The useChatData
hook offers comprehensive access to various chat-related states and data within your React application.
Returned Properties
actions
(IAction[]
)askUser
(IAsk | undefined
)chatSettingsValue
(any
)connected
(boolean
)disabled
(boolean
)error
(boolean | undefined
)loading
(boolean
)tasklists
(ITasklistElement[]
)
Example
useChatInteract
Hook
The useChatInteract
hook provides a comprehensive set of methods to interact with the chat system within your React application.
Methods
sendMessage
replyMessage
clear
uploadFile
callAction
startAudioStream
sendAudioChunk
stopTask
Example
useAuth
Hook
The useAuth
hook manages authentication within your React application, providing functionalities like user sessions and token management.
Properties & Methods
authConfig
user
accessToken
isLoading
logout
Example
useApi
Hook
The useApi
hook simplifies data fetching and error handling using SWR.
Example
Was this page helpful?