Skip to main content

React SDK

@kapaai/agent-react provides a complete chat UI with streaming, tool execution, and theming. It depends on @kapaai/agent-core as a peer dependency — install both packages.

Installation

npm install @kapaai/agent-core @kapaai/agent-react

Components

ComponentDescription
AgentProviderWraps your app with chat state and theming. Required.
AgentChatFull chat UI — header, messages, input, footer. Fills its parent container.
AgentPanelSlide-in drawer wrapping AgentChat.
AgentFooterAttribution footer with optional custom content via branding.footerExtra.

Hooks

HookDescription
useAgentChat()Access chat state and actions (messages, sendMessage, approveToolCall, etc.). Required for headless mode.
useAgentColorScheme()Toggle or set the color scheme. See Theming.

Utilities

ExportDescription
createToolHelper<TContext>()Type-safe tool definition factory with Zod inference for execute and render args. See Custom tools.

Headless mode

You can use AgentProvider + useAgentChat() without any SDK UI components to build a fully custom chat interface:

import { AgentProvider, useAgentChat } from '@kapaai/agent-react';

const CustomChat = () => {
const {
messages,
isStreaming,
inputValue,
setInputValue,
sendMessage,
resetConversation,
stopGeneration,
approveToolCall,
rejectToolCall,
} = useAgentChat();

return (
<div>
{messages.map((msg, i) => (
<div key={i}>
<strong>{msg.role}:</strong> {msg.content}
</div>
))}
<input
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage(inputValue);
}
}}
/>
</div>
);
};

function App() {
return (
<AgentProvider
getSessionToken={async () => {
const res = await fetch('/api/session', { method: 'POST' });
return res.json();
}}
projectId="your-project-id"
integrationId="your-integration-id"
>
<CustomChat />
</AgentProvider>
);
}

In headless mode you handle all rendering — message bubbles, tool call cards, approval buttons, streaming indicators. The hook gives you the data and actions, you build the UI.

For the message data structure (ConversationMessage, ContentBlock, ToolCallDisplay, ToolCallStatus), see Message format in the core SDK docs. The types are the same — @kapaai/agent-react re-exports them from @kapaai/agent-core.

useAgentChat() return value

FieldTypeDescription
messagesConversationMessage[]All messages in the conversation.
isStreamingbooleanWhether the agent is currently streaming a response.
threadIdstring | nullCurrent conversation thread ID.
inputValuestringCurrent input field value.
setInputValue(value: string) => voidUpdate the input field value.
sendMessage(text: string) => Promise<void>Send a message and trigger the agent loop.
resetConversation() => voidClear messages and abort any in-progress request.
stopGeneration() => voidAbort the current streaming response.
approveToolCall(id: string) => voidApprove a tool waiting for confirmation.
rejectToolCall(id: string) => voidReject a tool waiting for confirmation.

Lower-level components

These components are also exported for advanced customization. They require AgentProvider for theming context:

ComponentDescription
AgentInputTextarea with send/stop button.
AgentMessageBubbleRenders a single message (user bubble, assistant markdown, or error alert).
ToolCallCardExpandable tool call card with args/response/sources tabs.
ToolCallGroupContainer for grouping tool cards.
SourceTilesSource attribution tiles with favicons.
ExamplePromptsStarter prompt buttons for empty conversations.
ShimmerTextAnimated shimmer text for loading states.