Skip to main content
Agent Clients

Build a Chat UI with React

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.watch on the root conversation. The returned view has the newest messages and any answer in progress, and from then on the connection receives pi.view frames.
  • Each frame has Chord operations. The hook applies them with applyImmutable from @earendil-works/chord/delta, and reads the messages, the partial answer, and the running tool from the view. See Stream a conversation for what the view holds.
  • Each frame has a seq number. If one is missing, the hook watches again and starts from a new view.
  • 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 view’s pi.live document has a run while 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 Call the agent. 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.