Frontend React SDK for consuming streaming AI APIs with cancellation and retry safety.
npm install chat-nest-sdk> Frontend SDK for Chat Nest providing a simple React hook to consume streaming AI APIs safely using Server-Side Events (SSE).
This package handles:
- Server-Side Events (SSE) streaming response handling
- Real-time token streaming via SSE protocol
- Cancellation propagation
- Intelligent retry behavior
- Error normalization
- Message state management
Designed for production usage in React applications. Uses SSE for efficient, bidirectional communication with the backend.
---
- Server-Side Events (SSE) streaming protocol
- Real-time token streaming via SSE events
- Abort-safe cancellation
- Retry only on network / server failures
- No retries on client or policy errors
- Message state management
- Lightweight and framework-friendly
- Efficient SSE event parsing (token, done, error, ping, start events)
---
``bash`
npm install chat-nest-sdk
---
`
import { useAiChat } from "chat-nest-sdk";
function App() {
const chat = useAiChat({
endpoint: "http://localhost:3001/api/chat",
initialProfile: "balanced",
dailyTokenLimit: 50_000, // optional: cap daily tokens
maxTokensPerRequest: 4096, // optional: cap per request
});
return (
<>
>
);
}
`
---
| Field | Type | Description |
| ------------------- | ------ | --------------------------------------------------------------------------- |
| endpoint | string | Backend API endpoint |
| initialMessages | Message[] | Optional. Initial messages |
| maxMessages | number | Optional. Max messages to keep in context (default 10) |
| initialProfile | AiUsageProfile | Optional. Profile: constrained, balanced, expanded |
| dailyTokenLimit | number | Optional. Cap daily tokens; server applies min(profile limit, this) |
| maxTokensPerRequest | number | Optional. Cap tokens per request (input + output); server applies min |
| Field | Description |
| ----------------- | ----------------------------- |
| messages | Chat message list |
| sendMessage(text) | Sends user message |
| cancel() | Cancels active request |
| isStreaming | Whether stream is active |
| error | Last error |
| profile | Current profile |
| setProfile(p) | Set profile; persisted to localStorage |
| reset() | Reset messages and error |
---
Only one active request is allowed at a time.
Cancel immediately stops streaming and billing.
4xx errors are never retried.
Network failures retry automatically.
Server-Side Events (SSE): The SDK communicates with the backend using the SSE protocol. The backend must send events in SSE format (event: ). Supported event types: start, token, done, error, ping`.
---
ISC