API Reference - Session Initialization
Complete API reference for RKT Chat Client session initialization
API Reference
This document provides complete API reference for the session initialization functionality in RKT Chat Client.
Types
UseChatOptions
Configuration options for the useChat hook.
interface UseChatOptions {
chatbot?: {
id?: string;
prompt?: string;
allowedDomains?: string[];
strictDomain?: boolean;
};
baseUrl?: string;
telemetry?: TelemetryProvider;
errorTracker?: ErrorTracker;
onReady?: (sessionId: string) => void;
}Properties
| Property | Type | Required | Description |
|---|---|---|---|
chatbot | ChatbotConfig | No | Chatbot configuration object |
baseUrl | string | No | Base URL for chat API endpoints |
telemetry | TelemetryProvider | No | Telemetry provider for analytics |
errorTracker | ErrorTracker | No | Error tracking service |
onReady | (sessionId: string) => void | No | Callback when session is ready |
ChatbotConfig
Configuration for the chatbot instance.
interface ChatbotConfig {
id?: string;
prompt?: string;
allowedDomains?: string[];
strictDomain?: boolean;
}Properties
| Property | Type | Required | Description |
|---|---|---|---|
id | string | No | Unique identifier for the chatbot |
prompt | string | No | System prompt for the chatbot |
allowedDomains | string[] | No | List of allowed domains for the chatbot |
strictDomain | boolean | No | Whether to enforce domain restrictions |
TelemetryProvider
Interface for telemetry and analytics providers.
interface TelemetryProvider {
initializeSession: (data: SessionInitData) => void;
trackEvent: (event: string, properties?: Record<string, any>) => void;
trackError: (error: Error, context?: Record<string, any>) => void;
}Methods
| Method | Parameters | Description |
|---|---|---|
initializeSession | data: SessionInitData | Initialize a new session with device and user data |
trackEvent | event: string, properties?: Record<string, any> | Track custom events |
trackError | error: Error, context?: Record<string, any> | Track errors with context |
SessionInitData
Data structure for session initialization.
interface SessionInitData {
chatbotId: string;
sessionId: string;
deviceInfo?: DeviceInfo;
userPreferences?: UserPreferences;
}Properties
| Property | Type | Required | Description |
|---|---|---|---|
chatbotId | string | Yes | ID of the chatbot |
sessionId | string | Yes | Unique session identifier |
deviceInfo | DeviceInfo | No | Device and browser information |
userPreferences | UserPreferences | No | User preference settings |
DeviceInfo
Device and browser information collected during initialization.
interface DeviceInfo {
userAgent: string;
language: string;
languages: string[];
platform: string;
cookieEnabled: boolean;
onLine: boolean;
screenWidth: number;
screenHeight: number;
timezone: string;
timestamp: string;
}UserPreferences
User preference settings.
interface UserPreferences {
theme: "light" | "dark";
language: string;
timezone: string;
}ErrorTracker
Interface for error tracking services.
interface ErrorTracker {
captureException: (error: Error, context?: Record<string, any>) => void;
captureMessage: (message: string, level?: 'info' | 'warning' | 'error', context?: Record<string, any>) => void;
}Methods
| Method | Parameters | Description |
|---|---|---|
captureException | error: Error, context?: Record<string, any> | Capture and report exceptions |
captureMessage | message: string, level?: string, context?: Record<string, any> | Capture and report messages |
Hook Return Values
useChat Return Type
The useChat hook returns an object with the following properties:
interface UseChatReturn {
// Session management
sessionId: string;
isSessionReady: boolean;
telemetryInitialized: boolean;
initializeSession: (sessionId: string) => void;
// Chat functionality
messages: Message[];
sendMessage: (message: string, body?: Record<string, any>) => void;
handleSendMessage: (message: string, body?: Record<string, any>) => void;
clearMessages: () => void;
finishSession: () => void;
// Status and control
status: "idle" | "streaming" | "submitted" | "error";
stop: () => void;
setMessages: (messages: Message[]) => void;
regenerate: () => void;
onCancel: () => void;
// Status flags
isChatLoading: boolean;
isError: boolean;
errorMessage?: string;
}Session Management Properties
| Property | Type | Description |
|---|---|---|
sessionId | string | Unique identifier for the current session |
isSessionReady | boolean | Whether the session is fully initialized |
telemetryInitialized | boolean | Whether telemetry has been initialized |
initializeSession | (sessionId: string) => void | Manually initialize a session |
Chat Functionality Properties
| Property | Type | Description |
|---|---|---|
messages | Message[] | Array of chat messages |
sendMessage | (message: string, body?: Record<string, any>) => void | Send a message to the chat |
handleSendMessage | (message: string, body?: Record<string, any>) => void | Wrapper for sendMessage with telemetry |
clearMessages | () => void | Clear all messages and generate new session |
finishSession | () => void | Finish the current session |
Status and Control Properties
| Property | Type | Description |
|---|---|---|
status | string | Current chat status |
stop | () => void | Stop the current chat operation |
setMessages | (messages: Message[]) => void | Set the messages array |
regenerate | () => void | Regenerate the last response |
onCancel | () => void | Cancel the current operation |
Status Flags
| Property | Type | Description |
|---|---|---|
isChatLoading | boolean | Whether chat is currently loading |
isError | boolean | Whether there's an error |
errorMessage | string? | Error message if there's an error |
Methods
initializeSession
Manually initialize a session with telemetry data.
initializeSession(sessionId: string): voidParameters
| Parameter | Type | Description |
|---|---|---|
sessionId | string | The session ID to initialize |
Example
const { initializeSession, sessionId } = useChat(options);
// Manually initialize session
useEffect(() => {
if (sessionId) {
initializeSession(sessionId);
}
}, [sessionId, initializeSession]);sendMessage
Send a message to the chat with optional body data.
sendMessage(message: string, body?: Record<string, any>): voidParameters
| Parameter | Type | Description |
|---|---|---|
message | string | The message text to send |
body | Record<string, any> | Optional additional data |
Example
const { sendMessage } = useChat(options);
// Send a simple message
sendMessage("Hello, how are you?");
// Send a message with additional data
sendMessage("Hello", {
userId: "123",
context: "support"
});clearMessages
Clear all messages and generate a new session ID.
clearMessages(): voidExample
const { clearMessages } = useChat(options);
// Clear messages and start fresh
const handleNewChat = () => {
clearMessages();
};finishSession
Finish the current session and track completion.
finishSession(): voidExample
const { finishSession } = useChat(options);
// Finish session when component unmounts
useEffect(() => {
return () => {
finishSession();
};
}, [finishSession]);Events
onReady
Callback triggered when the session is fully initialized.
onReady?: (sessionId: string) => voidParameters
| Parameter | Type | Description |
|---|---|---|
sessionId | string | The initialized session ID |
Example
const { sessionId } = useChat({
// ... other options
onReady: (sessionId) => {
console.log(`Session ready: ${sessionId}`);
// Perform any initialization that depends on the session
initializeUserPreferences(sessionId);
trackSessionStart(sessionId);
},
});Error Handling
Error Tracking
The session initialization system includes comprehensive error handling:
// Error tracking integration
const errorTracker = {
captureException: (error: Error, context?: Record<string, any>) => {
// Send to your error tracking service
console.error("Exception:", error, context);
},
captureMessage: (message: string, level?: string, context?: Record<string, any>) => {
// Log important messages
console.log(`[${level}] ${message}`, context);
},
};Common Error Scenarios
- Telemetry Initialization Failure: Gracefully handled with console logging
- Network Errors: Tracked and reported through error tracking
- Browser Compatibility: Automatic fallbacks for unsupported features
- Session ID Generation: Retry logic for failed generation
Browser Support
| Feature | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
requestIdleCallback | ✅ 47+ | ✅ 55+ | ✅ 13+ | ✅ 79+ |
| Session Management | ✅ | ✅ | ✅ | ✅ |
| Telemetry | ✅ | ✅ | ✅ | ✅ |
| Error Handling | ✅ | ✅ | ✅ | ✅ |
Last updated on