Skip to content

Function: useAgentChat()

useAgentChat(__namedParameters): UseAgentChatResult

React hook for chatting with an agent registered via the agents() plugin. Wraps connectSSE (which owns the buffer cap, abort composition, retry/backoff, and frame parsing) with the small amount of stateful glue every chat UI needs: accumulated assistant text, thread id, streaming flag, and an event callback.

The hook is intentionally lower-level than a full chat component — it owns one stream at a time, not a multi-turn message history. The caller composes its own messages array (typically a useState) and appends to it via the onEvent callback for tool calls and via the content field for assistant text.

UseAgentChatOptions

UseAgentChatResult

function Chat({ agent }: { agent: string }) {
const [messages, setMessages] = useState<Message[]>([]);
const { content, threadId, isStreaming, send, reset } = useAgentChat({
agent,
onEvent(ev) {
if (ev.type === "response.output_item.added" && ev.item?.type === "function_call") {
setMessages((m) => [...m, { role: "tool", name: ev.item?.name, args: ev.item?.arguments }]);
}
},
});
// `content` reflects the latest assistant turn; reset() between conversations.
// ...
}