agent-widget-sdk
Version:
JavaScript SDK for Sarvam Agent Widget APIs and WebSocket connections
409 lines (402 loc) • 10.9 kB
TypeScript
interface WidgetConfig {
appId: string;
orgId: string;
orgName: string;
buttonConfig: ButtonConfig;
widgetConfig: WidgetDisplayConfig;
}
interface ButtonConfig {
text: string;
bgColor: string;
textColor: string;
endText: string;
}
interface WidgetDisplayConfig {
title: string;
logoUrl: string;
}
interface AudioData {
data: string;
encoding: string;
sample_rate: number;
}
interface WebSocketMessage {
request_id: string;
sent_at: number;
start_interaction?: boolean;
end_interaction?: boolean;
content_update?: boolean;
audio?: AudioData;
configs?: {
agentic_variables?: {
web_content?: string;
};
agent_config?: any;
fetched_config?: any;
};
events?: Array<{
event_type: string;
origin: string;
metrics: {
start_time: number;
end_time: number;
};
}>;
}
interface ProxyRequestOptions {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
headers?: Record<string, string>;
body?: any;
}
interface SDKConfig {
apiUrl?: string;
wsUrl?: string;
enableLogging?: boolean;
autoReconnect?: boolean;
reconnectInterval?: number;
maxReconnectAttempts?: number;
}
interface AudioSetup {
stream: MediaStream;
context: AudioContext;
source: MediaStreamAudioSourceNode;
workletNode: AudioWorkletNode;
gainNode: GainNode;
}
interface WebSocketHandlers {
onOpen?: () => void;
onMessage?: (event: MessageEvent) => void;
onClose?: () => void;
onError?: (error: Event) => void;
onAudioResponse?: (audioData: any) => void;
onTextResponse?: (text: string) => void;
}
interface VoiceSessionConfig {
orgId: string;
appId: string;
token: string;
appVersion?: number;
}
declare class SDKError extends Error {
code: string;
details?: any | undefined;
constructor(message: string, code: string, details?: any | undefined);
}
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
interface Logger {
debug: (message: string, ...args: any[]) => void;
info: (message: string, ...args: any[]) => void;
warn: (message: string, ...args: any[]) => void;
error: (message: string, ...args: any[]) => void;
}
/**
* Default logger implementation
*/
declare class DefaultLogger implements Logger {
private enabled;
private level;
constructor(enabled?: boolean, level?: LogLevel);
private shouldLog;
debug(message: string, ...args: any[]): void;
info(message: string, ...args: any[]): void;
warn(message: string, ...args: any[]): void;
error(message: string, ...args: any[]): void;
}
/**
* Converts Float32Array to Int16Array for audio processing
*/
declare function convertFloat32toInt16(buffer: Float32Array): Int16Array;
/**
* Converts ArrayBuffer to base64 string
*/
declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
/**
* Calculates audio level from Float32Array
*/
declare function calculateAudioLevel(buffer: Float32Array): number;
/**
* Generates a UUID v4
*/
declare function generateUUID(): string;
/**
* Validates URL format
*/
declare function isValidUrl(url: string): boolean;
/**
* Extracts clean URL path from full URL
*/
declare function extractCleanUrl(url: string): string;
/**
* Retry function with exponential backoff
*/
declare function retry<T>(fn: () => Promise<T>, maxAttempts?: number, baseDelay?: number): Promise<T>;
/**
* Debounce function
*/
declare function debounce<T extends (...args: any[]) => any>(func: T, wait: number): (...args: Parameters<T>) => void;
/**
* Throttle function
*/
declare function throttle<T extends (...args: any[]) => any>(func: T, limit: number): (...args: Parameters<T>) => void;
/**
* Deep clone object
*/
declare function deepClone<T>(obj: T): T;
/**
* API Client for Sarvam Agent Widget APIs
*/
declare class ApiClient {
private apiUrl;
private logger;
constructor(apiUrl?: string, logger?: Logger);
/**
* Makes a request to the proxy API
*/
proxyRequest(url: string, options?: ProxyRequestOptions): Promise<Response>;
/**
* Gets audio processor script
*/
getAudioProcessor(): Promise<string>;
/**
* Gets widget configuration
*/
getWidgetConfig(appId: string): Promise<WidgetConfig>;
/**
* Creates widget configuration
*/
createWidgetConfig(config: {
appId: string;
orgId: string;
orgName: string;
buttonConfig?: Partial<ButtonConfig>;
widgetConfig?: Partial<WidgetDisplayConfig>;
}): Promise<WidgetConfig>;
/**
* Updates widget configuration
*/
updateWidgetConfig(appId: string, updates: {
buttonConfig?: Partial<ButtonConfig>;
widgetConfig?: Partial<WidgetDisplayConfig>;
}): Promise<WidgetConfig>;
/**
* Fetches app configuration from Sarvam API
*/
fetchAppConfig(appId: string, token: string): Promise<any>;
}
/**
* WebSocket Client for Sarvam Agent Widget
*/
declare class WebSocketClient {
private ws;
private logger;
private handlers;
private config;
private reconnectAttempts;
private maxReconnectAttempts;
private reconnectInterval;
private isManuallyDisconnected;
private webContentSent;
private currentWebContent;
constructor(logger?: Logger);
/**
* Sets event handlers for WebSocket events
*/
setHandlers(handlers: WebSocketHandlers): void;
/**
* Connects to WebSocket
*/
connect(config: VoiceSessionConfig): Promise<void>;
/**
* Disconnects from WebSocket
*/
disconnect(): void;
/**
* Sends a JSON message through WebSocket
*/
private sendMessage;
/**
* Sends start interaction message
*/
sendStartInteraction(webContent?: string): void;
/**
* Sends end interaction message
*/
sendEndInteraction(): void;
/**
* Sends audio chunk
*/
sendAudioChunk(audioBuffer: Float32Array, onAudioLevel?: (level: number) => void): void;
/**
* Sends content update
*/
sendContentUpdate(content: string): void;
/**
* Gets current connection state
*/
getState(): number;
/**
* Checks if WebSocket is connected
*/
isConnected(): boolean;
/**
* Sets up WebSocket event handlers
*/
private setupWebSocketHandlers;
/**
* Attempts to reconnect to WebSocket
*/
private attemptReconnect;
/**
* Creates WebSocket URL
*/
private createWebSocketUrl;
/**
* Updates web content
*/
setWebContent(content: string): void;
/**
* Gets current web content
*/
getWebContent(): string;
/**
* Reset content sent flag
*/
resetContentSent(): void;
}
/**
* Audio Manager for handling audio processing and playback
*/
declare class AudioManager {
private logger;
private audioSetup;
private isRecording;
private onAudioData?;
private onAudioLevel?;
private playbackContext;
constructor(logger?: Logger);
/**
* Sets up audio context and microphone access
*/
setupAudio(processorScript: string): Promise<void>;
/**
* Starts recording audio
*/
startRecording(onAudioData: (audioData: Float32Array) => void, onAudioLevel?: (level: number) => void): void;
/**
* Stops recording audio
*/
stopRecording(): void;
/**
* Plays audio from base64 data
*/
playAudio(audioData: string, onPlaybackStart?: () => void, onPlaybackEnd?: () => void): Promise<void>;
/**
* Gets current audio level
*/
getAudioLevel(): number;
/**
* Checks if audio is recording
*/
isAudioRecording(): boolean;
/**
* Checks if audio is set up
*/
isAudioSetup(): boolean;
/**
* Gets available audio devices
*/
getAudioDevices(): Promise<MediaDeviceInfo[]>;
/**
* Sets the audio input device
*/
setAudioInputDevice(deviceId: string): Promise<void>;
/**
* Cleans up audio resources
*/
cleanup(): Promise<void>;
}
/**
* Main SDK class for Sarvam Agent Widget
*/
declare class SarvamWidgetSDK {
private config;
private logger;
private apiClient;
private wsClient;
private audioManager;
private initialized;
constructor(config?: SDKConfig);
/**
* Initializes the SDK with audio processor
*/
initialize(): Promise<void>;
/**
* Widget Configuration API
*/
getWidgetConfig(appId: string): Promise<WidgetConfig>;
createWidgetConfig(config: {
appId: string;
orgId: string;
orgName: string;
buttonConfig?: Partial<ButtonConfig>;
widgetConfig?: Partial<WidgetDisplayConfig>;
}): Promise<WidgetConfig>;
updateWidgetConfig(appId: string, updates: {
buttonConfig?: Partial<ButtonConfig>;
widgetConfig?: Partial<WidgetDisplayConfig>;
}): Promise<WidgetConfig>;
/**
* Proxy API for making requests
*/
proxyRequest(url: string, options?: {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
headers?: Record<string, string>;
body?: any;
}): Promise<Response>;
/**
* Fetches app configuration
*/
fetchAppConfig(appId: string, token: string): Promise<any>;
/**
* Voice Session Management
*/
startVoiceSession(sessionConfig: VoiceSessionConfig, handlers: WebSocketHandlers): Promise<void>;
/**
* Ends voice session
*/
endVoiceSession(): Promise<void>;
/**
* Starts audio recording and interaction
*/
startInteraction(webContent?: string): void;
/**
* Stops audio recording and interaction
*/
stopInteraction(): void;
/**
* Sends content update
*/
sendContentUpdate(content: string): void;
/**
* Plays audio response
*/
playAudioResponse(audioData: string, onStart?: () => void, onEnd?: () => void): Promise<void>;
/**
* Audio Device Management
*/
getAudioDevices(): Promise<MediaDeviceInfo[]>;
setAudioInputDevice(deviceId: string): Promise<void>;
/**
* Status Methods
*/
isInitialized(): boolean;
isConnected(): boolean;
isRecording(): boolean;
getConnectionState(): number;
/**
* Cleanup resources
*/
cleanup(): Promise<void>;
}
export { ApiClient, AudioManager, DefaultLogger, SDKError, SarvamWidgetSDK, WebSocketClient, arrayBufferToBase64, calculateAudioLevel, convertFloat32toInt16, debounce, deepClone, SarvamWidgetSDK as default, extractCleanUrl, generateUUID, isValidUrl, retry, throttle };
export type { AudioData, AudioSetup, ButtonConfig, LogLevel, Logger, ProxyRequestOptions, SDKConfig, VoiceSessionConfig, WebSocketHandlers, WebSocketMessage, WidgetConfig, WidgetDisplayConfig };