ai.matey.react.core
Core React hooks for AI chat interactions.
Part of the ai.matey monorepo.
Installation
npm install ai.matey.react.core
Quick Start
HTTP Mode (Default)
import { useChat } from 'ai.matey.react.core';
function ChatComponent() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
api: '/api/chat',
});
return (
<div>
{messages.map((m) => (
<div key={m.id}>
<strong>{m.role}:</strong> {m.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit" disabled={isLoading}>Send</button>
</form>
</div>
);
}
Direct Backend Mode
Use any backend adapter directly without HTTP - great for Electron apps, browser extensions, or when you want to skip the server:
import { useChat } from 'ai.matey.react.core';
import { OpenAIBackend } from 'ai.matey.backend/openai';
const backend = new OpenAIBackend({
apiKey: process.env.REACT_APP_OPENAI_API_KEY,
});
function ChatComponent() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
direct: {
backend,
systemPrompt: 'You are a helpful assistant.',
},
});
return (
<div>
{messages.map((m) => (
<div key={m.id}>
<strong>{m.role}:</strong> {m.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit" disabled={isLoading}>Send</button>
</form>
</div>
);
}
Exports
Hooks
useChat - Chat interface with streaming support
useCompletion - Text completion interface
useObject - Structured object generation
Types
Message - Chat message type
ToolCall, ToolInvocation, Tool - Tool calling types
UseChatOptions, UseChatReturn - useChat types
UseCompletionOptions, UseCompletionReturn - useCompletion types
UseObjectOptions, UseObjectReturn - useObject types
DirectBackend, DirectModeOptions, DirectToolCallHandler - Direct mode types
API Reference
useChat
React hook for building chat interfaces with streaming support.
Supports two modes:
- HTTP Mode (default): Uses
api endpoint with fetch
- Direct Mode: Uses
direct.backend for direct backend access
const {
messages,
input,
setInput,
handleInputChange,
handleSubmit,
append,
reload,
stop,
setMessages,
isLoading,
error,
data,
} = useChat(options);
HTTP Mode Options
useChat({
api: '/api/chat',
initialMessages: [],
initialInput: '',
id: 'chat-1',
headers: {},
body: {},
onFinish: (message) => {},
onError: (error) => {},
onResponse: (response) => {},
streamProtocol: 'data',
});
Direct Mode Options
useChat({
direct: {
backend: myBackendAdapter,
systemPrompt: 'You are a helpful assistant.',
defaultParameters: {
temperature: 0.7,
maxTokens: 1000,
},
tools: [
{
name: 'get_weather',
description: 'Get weather for a location',
parameters: { },
},
],
onToolCall: async (name, input, id) => {
return 'Tool result';
},
autoExecuteTools: true,
maxToolRounds: 5,
},
initialMessages: [],
onFinish: (message) => {},
onError: (error) => {},
});
useCompletion
React hook for text completion interfaces.
const {
completion,
input,
setInput,
handleInputChange,
handleSubmit,
complete,
stop,
isLoading,
error,
} = useCompletion({
api: '/api/completion',
});
useObject
React hook for generating structured objects.
const {
object,
submit,
isLoading,
error,
} = useObject<MyType>({
api: '/api/object',
schema: myZodSchema,
});
Direct Mode vs HTTP Mode
| Setup | Requires API endpoint | Just a backend adapter |
| Server needed | Yes | No |
| Use case | Full-stack apps | Electron, browser extensions, testing |
| Streaming | Via SSE | Native async iterators |
| Tool calling | Via API | Built-in with wrapper-ir |
License
MIT - see LICENSE for details.