Skip to main content
Clients

React SDK

Build a chat UI for an agent with React.

The chat UI uses the @rivetkit/react package. The agent logic lives in one hook, and the UI in one component.

Chat.tsxuseAgentChatAgent Actorstatesubmit, abortevents
  • The useActor hook connects to the agent by name and key. Once the connection opens, the hook calls conversation.watchEvents on the root conversation. The returned snapshot has the history and any answer in progress, and from then on the connection receives pi.events.
  • The message_update event streams the reply as text deltas, and message_end adds each finished message, including your own prompt. See Session Lifecycle for every event.
  • Each batch has a seq number. If one is missing, the hook watches again and starts from a new snapshot.
  • The send function calls conversation.submit, which starts a run without waiting for it. A message sent during a run waits as a follow-up.
  • The run_start and run_end events mark when the agent works. The Chat component switches its button from Send to Stop while it does, and Stop calls conversation.abort.

Next steps

  • To steer a run in progress, see Session Lifecycle. To handle errors, see Client SDK.
  • To send a login token, add params to useActor.
  • To switch models from the UI, call conversation.configure(id, { model: { provider, modelId } }) on the connection with one of the agent’s scopedModels.

See React in the Actors docs for everything @rivetkit/react can do.