Skip to main content

React Hooks

The @baleybots/react package provides React hooks for building chat interfaces and using Baleybots agents in React applications.

Installation

npm install @baleybots/react @baleybots/core

useChat

useChat is the primary hook for building chat interfaces. It supports two modes:

  • Client mode (default) -- runs the agent locally in the browser
  • Server mode -- connects to a backend API via apiUrl and chatId

Client mode

import { useChat } from '@baleybots/react';

function ChatComponent() {
const { segments, sendStreaming, isStreaming } = useChat({
model: 'openai|gpt-4.1-mini',
systemPrompt: 'You are a helpful assistant',
});

const handleSend = async (text: string) => {
await sendStreaming(text);
};

return (
<div>
{segments.map((segment) => {
if (segment.type === 'user') {
return <div key={segment.id}><b>You:</b> {segment.content}</div>;
}
if (segment.type === 'text') {
return <div key={segment.id}><b>Assistant:</b> {segment.content}</div>;
}
return null;
})}
</div>
);
}

Server mode

When apiUrl is set, useChat connects to a backend instead of running the agent locally. Use chatId to persist conversations.

import { useChat } from '@baleybots/react';

function ChatComponent() {
const { segments, sendStreaming, isStreaming } = useChat({
apiUrl: 'https://your-api.com/chat',
chatId: 'conversation-123',
});

// Same rendering code as client mode
}

Return values

The hook returns:

PropertyTypeDescription
segmentsStreamSegment[]All segments in the conversation
isLoadingbooleanWhether a request is in progress
isStreamingbooleanWhether a response is currently streaming
errorError | nullLast error, if any
send(text: string) => PromiseSend a message (no streaming)
sendStreaming(text: string) => PromiseSend a message with streaming
clearHistory() => Promise<void>Clear the conversation
stopStreaming() => voidAbort the current stream

Options

OptionTypeDescription
modelstring | ModelConfigLLM model to use
systemPromptstringSystem prompt for the assistant
toolsRecord<string, ToolDefinition>Tools available to the assistant
outputSchemaZodSchemaSchema for structured responses
apiUrlstringBackend URL (activates server mode)
chatIdstringConversation ID (server mode)
storageHistoryStorageCustom history storage backend
proxyUrlstringProxy URL for API requests
fetchtypeof fetchCustom fetch (e.g., for React Native)

useBaleybot

useBaleybot wraps a Baleybot agent for direct use in React. It manages the agent lifecycle and provides a simple interface.

import { useBaleybot } from '@baleybots/react';
import { z } from 'zod';

function SentimentAnalyzer() {
const { process, result, isProcessing } = useBaleybot({
name: 'sentiment',
goal: 'Analyze the sentiment of text',
outputSchema: z.object({
sentiment: z.enum(['positive', 'negative', 'neutral']),
confidence: z.number(),
}),
});

return (
<div>
<button onClick={() => process('I love this product!')}>
Analyze
</button>
{result && <p>Sentiment: {result.sentiment} ({result.confidence})</p>}
</div>
);
}

Deprecated: useGroupChat

useGroupChat is deprecated. Use useChat with apiUrl and chatId instead:

// Before (deprecated)
import { useGroupChat } from '@baleybots/react';
const { messages } = useGroupChat({ apiUrl: '...', chatId: '...' });

// After
import { useChat } from '@baleybots/react';
const { segments } = useChat({ apiUrl: '...', chatId: '...' });

See the Unified useChat Hook migration guide for details.