const {
    newRequest,
    messages,
    streamPlaceholder,
    generating,
    loading,
    status,
    session,
    changeSession,
    loadingSession
} = useChatState(...);

When initialized, returns a number of properties to check, update, and manage the state.

The main one is newRequest function that’s used instead of agent.run to run the agent… it takes the same arguments and provides better state management.

Props

newRequest
Function

Used to send a new message to the agent or run it. works the same way as agent.run.

Returns AgentResponse or undefined in case of errors. notice that the onError was made to be used with this to capture any errors.

If the agent is working and you call the newRequest before it finishes, it will wait until the previous run is finished before processing the new one.

messages
RunHistory[]

A list of previous messages in the current session.

Reference to RunHistory

streamPlaceholder
AgentResponse | undefined

Used for streaming. while the agent is generating tokens and calling tools, this will contain the agent response while it gets updated in real-time.

When the agent is not working this will be undefined.

Please see this page to understand how it works.

Reference to AgentResponse

generating
boolean

Wether the agent is generating tokens or not (working or idle).

loading
boolean

Wether the agent is loading (started the run but still did not start to generate tokens).

status
string | undefined

The agent status, if the agent is idle it’s undefined, if loading it’s "Thinking", or if it’s calling a tool it’s "Talking with <TOOL_NAME>".

You can use this to display the agent status directly to the end user.

session
string

The current session ID.

changeSession
(id: string) => void

Change the session. just provide another session ID, and it will load its history and update the state.

loadingSession
boolean

When you provide a session_id in initialization or call changeSession, We need to load the session history (messages), and while the session history is loading this will be set to true, otherwise it’s false.

const {
    newRequest,
    messages,
    streamPlaceholder,
    generating,
    loading,
    status,
    session,
    changeSession,
    loadingSession
} = useChatState(...);