@hamsa-ai/voice-agents-sdk 0.4.1-beta.1 → 0.4.1-beta.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hamsa-ai/voice-agents-sdk",
3
- "version": "0.4.1-beta.1",
3
+ "version": "0.4.1-beta.3",
4
4
  "description": "Hamsa AI - Voice Agents JavaScript SDK",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -38,7 +38,7 @@ import { LiveKitAudioManager } from './livekit-audio-manager';
38
38
  import { LiveKitConnection } from './livekit-connection';
39
39
  import { LiveKitToolRegistry } from './livekit-tool-registry';
40
40
  import type { AudioLevelsResult, CallAnalyticsResult, ConnectionStatsResult, ParticipantData, PerformanceMetricsResult, Tool, TrackStatsResult } from './types';
41
- export type { AudioLevelsResult, CallAnalyticsResult, ConnectionStatsResult, ParticipantData, PerformanceMetricsResult, TrackStatsData, TrackStatsResult, } from './types';
41
+ export type { AgentState, AudioLevelsResult, CallAnalyticsResult, ConnectionStatsResult, ParticipantData, PerformanceMetricsResult, TrackStatsData, TrackStatsResult, } from './types';
42
42
  /**
43
43
  * Main LiveKitManager class that orchestrates voice agent communication
44
44
  *
@@ -2,6 +2,11 @@
2
2
  * Shared types and interfaces for LiveKit modules
3
3
  */
4
4
  import type { ConnectionQuality, RemoteTrack, RemoteTrackPublication } from 'livekit-client';
5
+ /**
6
+ * Agent state as defined by LiveKit
7
+ * Represents the current state of the voice agent
8
+ */
9
+ export type AgentState = 'idle' | 'initializing' | 'listening' | 'thinking' | 'speaking';
5
10
  /**
6
11
  * Function signature for client-side tools that can be executed by the agent.
7
12
  * Tools can be synchronous or asynchronous and accept variable arguments.
package/types/main.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { EventEmitter } from 'events';
2
- import type { Room } from 'livekit-client';
3
- import LiveKitManager, { type AudioLevelsResult, type CallAnalyticsResult, type ConnectionStatsResult, type ParticipantData, type PerformanceMetricsResult, type TrackStatsResult } from './classes/livekit-manager';
2
+ import type { ConnectionState, LocalTrack, LocalTrackPublication, Participant, RemoteParticipant, RemoteTrack, RemoteTrackPublication, Room } from 'livekit-client';
3
+ import LiveKitManager, { type AgentState, type AudioLevelsResult, type CallAnalyticsResult, type ConnectionStatsResult, type ParticipantData, type PerformanceMetricsResult, type TrackStatsResult } from './classes/livekit-manager';
4
4
  import ScreenWakeLock from './classes/screen-wake-lock';
5
+ export type { AgentState } from './classes/livekit-manager';
5
6
  /**
6
7
  * Custom error class that includes both human-readable message and machine-readable messageKey
7
8
  * for internationalization and programmatic error handling
@@ -97,6 +98,95 @@ type JobDetails = {
97
98
  /** Additional job properties that may be returned by the API */
98
99
  [key: string]: unknown;
99
100
  };
101
+ /**
102
+ * Event handler signatures for HamsaVoiceAgent
103
+ *
104
+ * Defines the type-safe interface for all events emitted by the HamsaVoiceAgent.
105
+ * Each event specifies its exact handler signature, enabling full type safety
106
+ * and IntelliSense support when using the event emitter API.
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * // Fully type-safe event handlers
111
+ * agent.on('transcriptionReceived', (text: string) => {
112
+ * console.log('User said:', text);
113
+ * });
114
+ *
115
+ * agent.on('connectionQualityChanged', ({ quality, metrics }) => {
116
+ * if (quality === 'poor') {
117
+ * showNetworkWarning(metrics);
118
+ * }
119
+ * });
120
+ * ```
121
+ */
122
+ type HamsaVoiceAgentEvents = {
123
+ /** Emitted when connection is established (before call fully starts) */
124
+ start: () => void;
125
+ /** Emitted when call is fully started and ready */
126
+ callStarted: () => void;
127
+ /** Emitted when call ends (user or agent initiated) */
128
+ callEnded: () => void;
129
+ /** Emitted when call is paused */
130
+ callPaused: () => void;
131
+ /** Emitted when call is resumed */
132
+ callResumed: () => void;
133
+ /** Emitted when connection is closed */
134
+ closed: () => void;
135
+ /** Emitted when attempting to reconnect */
136
+ reconnecting: () => void;
137
+ /** Emitted when reconnection succeeds */
138
+ reconnected: () => void;
139
+ /** Emitted when user speech is transcribed */
140
+ transcriptionReceived: (text: string) => void;
141
+ /** Emitted when agent response is received */
142
+ answerReceived: (text: string) => void;
143
+ /** Emitted when agent starts speaking */
144
+ speaking: () => void;
145
+ /** Emitted when agent is listening */
146
+ listening: () => void;
147
+ /** Emitted when agent state changes (idle, initializing, listening, thinking, speaking) */
148
+ agentStateChanged: (state: AgentState) => void;
149
+ /** Emitted when an error occurs */
150
+ error: (error: Error | HamsaApiError) => void;
151
+ /** Emitted when a remote track is subscribed */
152
+ trackSubscribed: (data: {
153
+ track: RemoteTrack;
154
+ publication: RemoteTrackPublication;
155
+ participant: RemoteParticipant;
156
+ }) => void;
157
+ /** Emitted when a remote track is unsubscribed */
158
+ trackUnsubscribed: (data: {
159
+ track: RemoteTrack;
160
+ publication: RemoteTrackPublication;
161
+ participant: RemoteParticipant;
162
+ }) => void;
163
+ /** Emitted when a local track is published */
164
+ localTrackPublished: (data: {
165
+ track?: LocalTrack;
166
+ publication: LocalTrackPublication;
167
+ }) => void;
168
+ /** Emitted when analytics data is updated */
169
+ analyticsUpdated: (analytics: CallAnalyticsResult) => void;
170
+ /** Emitted when connection quality changes */
171
+ connectionQualityChanged: (data: {
172
+ quality: 'excellent' | 'good' | 'poor';
173
+ metrics: ConnectionStatsResult;
174
+ }) => void;
175
+ /** Emitted when connection state changes */
176
+ connectionStateChanged: (state: ConnectionState) => void;
177
+ /** Emitted when audio playback state changes */
178
+ audioPlaybackChanged: (playing: boolean) => void;
179
+ /** Emitted when a participant connects */
180
+ participantConnected: (participant: RemoteParticipant) => void;
181
+ /** Emitted when a participant disconnects */
182
+ participantDisconnected: (participant: RemoteParticipant) => void;
183
+ /** Emitted when data is received */
184
+ dataReceived: (message: Uint8Array, participant: Participant) => void;
185
+ /** Emitted for custom events */
186
+ customEvent: (eventType: string, eventData: unknown, metadata?: Record<string, unknown>) => void;
187
+ /** Emitted for informational messages */
188
+ info: (info: string) => void;
189
+ };
100
190
  /**
101
191
  * HamsaVoiceAgent - Main SDK class for voice agent integration
102
192
  *
@@ -104,6 +194,9 @@ type JobDetails = {
104
194
  * into web applications. It handles authentication, connection management,
105
195
  * conversation lifecycle, analytics, and client-side tool execution.
106
196
  *
197
+ * Note: This class uses declaration merging with an interface (defined below)
198
+ * to provide type-safe event handlers. This is intentional and safe.
199
+ *
107
200
  * Key features:
108
201
  * - Real-time voice communication with AI agents
109
202
  * - Comprehensive analytics and quality monitoring
@@ -784,5 +877,47 @@ declare class HamsaVoiceAgent extends EventEmitter {
784
877
  */
785
878
  getRoom(): Room | null;
786
879
  }
880
+ /**
881
+ * Declaration merging: Add type-safe event methods to HamsaVoiceAgent
882
+ *
883
+ * This interface merges with the HamsaVoiceAgent class to provide fully
884
+ * typed event handler methods without requiring explicit type assertions.
885
+ *
886
+ * @example
887
+ * ```typescript
888
+ * const agent = new HamsaVoiceAgent('api_key');
889
+ *
890
+ * // ✅ Fully type-safe - no casting needed!
891
+ * agent.on('transcriptionReceived', (text) => {
892
+ * console.log(text); // text is inferred as string
893
+ * });
894
+ *
895
+ * // ❌ Type error - wrong event name
896
+ * agent.on('wrongEvent', () => {});
897
+ *
898
+ * // ❌ Type error - wrong handler signature
899
+ * agent.on('transcriptionReceived', () => {}); // Missing parameter
900
+ * ```
901
+ */
902
+ interface HamsaVoiceAgent {
903
+ /**
904
+ * Registers an event listener with type-safe event names and handlers
905
+ */
906
+ on<K extends keyof HamsaVoiceAgentEvents>(event: K, listener: HamsaVoiceAgentEvents[K]): this;
907
+ /**
908
+ * Removes an event listener with type-safe event names and handlers
909
+ */
910
+ off<K extends keyof HamsaVoiceAgentEvents>(event: K, listener: HamsaVoiceAgentEvents[K]): this;
911
+ /**
912
+ * Registers a one-time event listener with type-safe event names and handlers
913
+ */
914
+ once<K extends keyof HamsaVoiceAgentEvents>(event: K, listener: HamsaVoiceAgentEvents[K]): this;
915
+ /**
916
+ * Emits an event with type-safe event names and arguments
917
+ */
918
+ emit<K extends keyof HamsaVoiceAgentEvents>(event: K, ...args: Parameters<HamsaVoiceAgentEvents[K]>): boolean;
919
+ }
787
920
  export { HamsaVoiceAgent, HamsaApiError };
788
921
  export default HamsaVoiceAgent;
922
+ export type { AudioLevelsResult, CallAnalyticsResult, ConnectionStatsResult, ParticipantData, PerformanceMetricsResult, TrackStatsResult, } from './classes/livekit-manager';
923
+ export type { HamsaVoiceAgentEvents, StartOptions, Tool, JobDetails };