@unisphere/models-sdk-js 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.d.ts ADDED
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Kaltura Avatar SDK - Core Types
3
+ *
4
+ * This file contains shared TypeScript types and interfaces used across the SDK.
5
+ */
6
+ /**
7
+ * Configuration options for the Kaltura Avatar SDK
8
+ */
9
+ export interface AvatarConfig {
10
+ /** Base URL for the avatar backend API. Defaults to production URL. */
11
+ baseUrl?: string;
12
+ /** ICE servers for WebRTC connection. Defaults to Google STUN. */
13
+ iceServers?: RTCIceServer[];
14
+ /** ICE transport policy: 'all' allows both STUN and TURN, 'relay' forces TURN only */
15
+ iceTransportPolicy?: 'all' | 'relay';
16
+ /** Retry configuration for API calls and connections */
17
+ retryConfig?: RetryConfig;
18
+ /** Logging level for the SDK */
19
+ logLevel?: 'debug' | 'info' | 'warn' | 'error';
20
+ }
21
+ /**
22
+ * Options for creating an avatar session
23
+ */
24
+ export interface CreateSessionOptions {
25
+ /** ID of the avatar to use */
26
+ avatarId: string;
27
+ /** Optional voice ID for the avatar */
28
+ voiceId?: string;
29
+ }
30
+ /**
31
+ * Configuration for retry logic with exponential backoff
32
+ */
33
+ export interface RetryConfig {
34
+ /** Maximum number of retry attempts. Default: 3 */
35
+ maxAttempts: number;
36
+ /** Initial delay in milliseconds before first retry. Default: 500 */
37
+ initialDelayMs: number;
38
+ /** Maximum delay in milliseconds between retries. Default: 5000 */
39
+ maxDelayMs: number;
40
+ /** Multiplier for exponential backoff. Default: 2 */
41
+ backoffMultiplier: number;
42
+ }
43
+ /**
44
+ * Default retry configuration
45
+ */
46
+ export declare const DEFAULT_RETRY_CONFIG: RetryConfig;
47
+ /**
48
+ * Session state machine states
49
+ */
50
+ export declare enum SessionState {
51
+ /** Session not created yet */
52
+ IDLE = "idle",
53
+ /** Session is being created */
54
+ CREATING = "creating",
55
+ /** Session is ready for commands */
56
+ READY = "ready",
57
+ /** Session has ended */
58
+ ENDED = "ended",
59
+ /** Session encountered an error */
60
+ ERROR = "error"
61
+ }
62
+ /**
63
+ * WebRTC connection state
64
+ */
65
+ export declare enum ConnectionState {
66
+ /** Not connected */
67
+ DISCONNECTED = "disconnected",
68
+ /** Connection in progress */
69
+ CONNECTING = "connecting",
70
+ /** Connection established */
71
+ CONNECTED = "connected",
72
+ /** Connection failed */
73
+ FAILED = "failed",
74
+ /** Connection closed */
75
+ CLOSED = "closed"
76
+ }
77
+ /**
78
+ * Session-level events that can be subscribed to
79
+ */
80
+ export type SessionEvent = 'stateChange' | 'connectionChange' | 'error';
81
+ /**
82
+ * RTC-level events
83
+ */
84
+ export type RTCEvent = 'connected' | 'disconnected' | 'failed' | 'track';
85
+ /**
86
+ * Error codes for different types of failures
87
+ */
88
+ export declare enum AvatarErrorCode {
89
+ INVALID_STATE = "INVALID_STATE",
90
+ SESSION_CREATE_FAILED = "SESSION_CREATE_FAILED",
91
+ SESSION_INIT_FAILED = "SESSION_INIT_FAILED",
92
+ API_REQUEST_FAILED = "API_REQUEST_FAILED",
93
+ API_AUTH_FAILED = "API_AUTH_FAILED",
94
+ API_NETWORK_ERROR = "API_NETWORK_ERROR",
95
+ RTC_CONNECTION_FAILED = "RTC_CONNECTION_FAILED",
96
+ SIGNALING_FAILED = "SIGNALING_FAILED",
97
+ ICE_GATHERING_TIMEOUT = "ICE_GATHERING_TIMEOUT",
98
+ PEER_CONNECTION_FAILED = "PEER_CONNECTION_FAILED",
99
+ VIDEO_ELEMENT_INVALID = "VIDEO_ELEMENT_INVALID",
100
+ MEDIA_PLAYBACK_FAILED = "MEDIA_PLAYBACK_FAILED",
101
+ INVALID_ARGUMENT = "INVALID_ARGUMENT",
102
+ TIMEOUT = "TIMEOUT",
103
+ UNKNOWN_ERROR = "UNKNOWN_ERROR"
104
+ }
105
+ /**
106
+ * Custom error class for avatar SDK errors
107
+ */
108
+ export declare class AvatarError extends Error {
109
+ code: AvatarErrorCode;
110
+ cause?: Error | undefined;
111
+ /**
112
+ * Create a new AvatarError
113
+ * @param message - Human-readable error message
114
+ * @param code - Error code for programmatic handling
115
+ * @param cause - Optional underlying error that caused this error
116
+ */
117
+ constructor(message: string, code: AvatarErrorCode, cause?: Error | undefined);
118
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Error utilities for the Kaltura Avatar SDK
3
+ */
4
+ import { AvatarError, AvatarErrorCode } from '../types';
5
+ /**
6
+ * Create an AvatarError from an unknown error
7
+ * @param error - The error to convert
8
+ * @param code - The error code to use
9
+ * @param message - Optional custom message
10
+ * @returns AvatarError instance
11
+ */
12
+ export declare function createAvatarError(error: unknown, code: AvatarErrorCode, message?: string): AvatarError;
13
+ /**
14
+ * Check if an error is a network error
15
+ * @param error - The error to check
16
+ * @returns true if it's a network error
17
+ */
18
+ export declare function isNetworkError(error: unknown): boolean;
19
+ /**
20
+ * Check if an HTTP status code is retryable (5xx or network errors)
21
+ * @param statusCode - HTTP status code
22
+ * @returns true if the request should be retried
23
+ */
24
+ export declare function isRetryableStatusCode(statusCode: number): boolean;
25
+ /**
26
+ * Check if an error should trigger a retry
27
+ * @param error - The error to check
28
+ * @param statusCode - Optional HTTP status code
29
+ * @returns true if the operation should be retried
30
+ */
31
+ export declare function shouldRetry(error: unknown, statusCode?: number): boolean;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Logging utilities for the Kaltura Avatar SDK
3
+ */
4
+ /**
5
+ * Log levels in order of severity
6
+ */
7
+ export declare enum LogLevel {
8
+ DEBUG = 0,
9
+ INFO = 1,
10
+ WARN = 2,
11
+ ERROR = 3
12
+ }
13
+ /**
14
+ * Simple logger interface
15
+ */
16
+ export interface Logger {
17
+ debug(message: string, ...args: any[]): void;
18
+ info(message: string, ...args: any[]): void;
19
+ warn(message: string, ...args: any[]): void;
20
+ error(message: string, ...args: any[]): void;
21
+ }
22
+ /**
23
+ * Create a logger with a specific prefix and minimum log level
24
+ *
25
+ * @param prefix - Prefix for log messages (e.g., class name)
26
+ * @param minLevel - Minimum log level to output
27
+ * @returns Logger instance
28
+ */
29
+ export declare function createLogger(prefix: string, minLevel?: LogLevel): Logger;
30
+ /**
31
+ * Convert string log level to LogLevel enum
32
+ *
33
+ * @param level - String log level
34
+ * @returns LogLevel enum value
35
+ */
36
+ export declare function parseLogLevel(level?: string): LogLevel;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Retry utilities with exponential backoff for the Kaltura Avatar SDK
3
+ */
4
+ import { RetryConfig } from '../types';
5
+ /**
6
+ * Execute a function with retry logic and exponential backoff
7
+ *
8
+ * @param fn - The async function to execute
9
+ * @param config - Retry configuration
10
+ * @param shouldRetryFn - Optional function to determine if error should trigger retry
11
+ * @returns Promise that resolves with the function result
12
+ * @throws The last error if all retries are exhausted
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const result = await retryWithBackoff(
17
+ * async () => await fetch('/api/endpoint'),
18
+ * { maxAttempts: 3, initialDelayMs: 500, maxDelayMs: 5000, backoffMultiplier: 2 }
19
+ * );
20
+ * ```
21
+ */
22
+ export declare function retryWithBackoff<T>(fn: () => Promise<T>, config: RetryConfig, shouldRetryFn?: (error: unknown, attempt: number) => boolean): Promise<T>;
23
+ /**
24
+ * Wait for a condition to be true with timeout
25
+ *
26
+ * @param condition - Function that returns true when condition is met
27
+ * @param timeoutMs - Maximum time to wait in milliseconds
28
+ * @param checkIntervalMs - How often to check the condition (default: 100ms)
29
+ * @returns Promise that resolves when condition is true
30
+ * @throws Error if timeout is reached
31
+ */
32
+ export declare function waitForCondition(condition: () => boolean, timeoutMs: number, checkIntervalMs?: number): Promise<void>;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Text chunking utilities for the Kaltura Avatar SDK
3
+ *
4
+ * The avatar backend processes text more efficiently when sent in small chunks.
5
+ * This utility splits text into word-based chunks for optimal processing.
6
+ */
7
+ /**
8
+ * Split text into chunks of N words each
9
+ *
10
+ * @param text - The text to chunk
11
+ * @param wordsPerChunk - Number of words per chunk (default: 3)
12
+ * @returns Array of text chunks
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * chunkText('Hello how are you doing today', 3)
17
+ * // Returns: ['Hello how are', 'you doing today']
18
+ *
19
+ * chunkText('Hi there', 3)
20
+ * // Returns: ['Hi there']
21
+ * ```
22
+ */
23
+ export declare function chunkText(text: string, wordsPerChunk?: number): string[];
24
+ /**
25
+ * Generate a unique turn ID for grouping related text chunks
26
+ * Uses UUID v4 format
27
+ *
28
+ * @returns UUID string
29
+ */
30
+ export declare function generateTurnId(): string;