@micdrop/client 1.9.0 → 2.0.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/README.md CHANGED
@@ -1,6 +1,12 @@
1
+ # 🖐️🎤 Micdrop: Real-Time Voice Conversations with AI
2
+
3
+ Micdrop is a set of open source Typescript packages to build real-time voice conversations with AI agents. It handles all the complexities on the browser and server side (microphone, speaker, VAD, network communication, etc) and provides ready-to-use implementations for various AI providers.
4
+
1
5
  # @micdrop/client
2
6
 
3
- A browser library for handling real-time voice conversations with microphone and speaker management.
7
+ The browser implementation of [Micdrop](../../README.md).
8
+
9
+ It is framework agnostic, you can use it with React, Vue, Angular or any other framework. See [demo-client](../../examples/demo-client/README.md) for a complete example with React.
4
10
 
5
11
  For server implementation, see [@micdrop/server](../server/README.md) package.
6
12
 
@@ -31,58 +37,100 @@ pnpm add @micdrop/client
31
37
 
32
38
  ## Quick Start
33
39
 
40
+ ### Start a call
41
+
42
+ `Micdrop` is a singleton instance of `MicdropClient` that can be used to start, stop and manage a call.
43
+
34
44
  ```typescript
35
- import { CallClient } from '@micdrop/client'
45
+ import { Micdrop } from '@micdrop/client'
36
46
 
37
- // Create a new call handler instance
38
- const call = CallClient.getInstance<YourParamsType>({
39
- // VAD (see docs)
40
- vad: ['volume', 'silero'],
47
+ // Start a call
48
+ await Micdrop.start({
49
+ url: 'wss://your-server.com/ws',
41
50
  })
51
+ ```
42
52
 
43
- // Configure the call
44
- call.url = 'wss://your-server.com/ws'
45
- call.params = {
46
- /* your parameters (to check auth, etc) */
47
- }
53
+ ### Control the call
48
54
 
49
- // Listen for events
50
- call.on('StateChange', () => {
55
+ ```typescript
56
+ // Pause/resume
57
+ Micdrop.pause()
58
+ Micdrop.resume()
59
+
60
+ // Stop the call
61
+ await Micdrop.stop()
62
+ ```
63
+
64
+ ### Listen for events
65
+
66
+ You can listen for events to get the state of the call and handle errors.
67
+
68
+ ```typescript
69
+ Micdrop.on('StateChange', () => {
51
70
  console.log('State changed')
52
71
  })
53
- call.on('EndCall', () => {
72
+ Micdrop.on('EndCall', () => {
54
73
  console.log('Call ended by assistant')
55
74
  })
56
- call.on('Error', (error) => {
75
+ Micdrop.on('Error', (error) => {
57
76
  console.error('Error occurred:', error)
58
77
  })
78
+ ```
59
79
 
60
- // Start the call
61
- await call.start()
80
+ ### Complete Example
81
+
82
+ ```typescript
83
+ import { Micdrop } from '@micdrop/client'
84
+
85
+ // Start a call
86
+ await Micdrop.start({
87
+ // URL of the WebSocket server (using @micdrop/server)
88
+ url: 'wss://your-server.com/ws',
89
+ // Parameters (optional) to check auth or provide other data
90
+ params: {
91
+ authorization: '1234',
92
+ lang: navigator.language,
93
+ },
94
+ // Voice Activity Detection (see docs)
95
+ vad: ['volume', 'silero'],
96
+ // Disable ability for the user to interrupt the assistant when it is speaking
97
+ disableInterruption: true,
98
+ // Enable debug logging
99
+ debugLog: true,
100
+ })
101
+
102
+ // Listen for events
103
+ Micdrop.on('StateChange', (state) => {
104
+ console.log('State:', state)
105
+ })
106
+ Micdrop.on('EndCall', () => {
107
+ console.log('Call ended by assistant')
108
+ })
109
+ Micdrop.on('Error', (error) => {
110
+ console.error('Error occurred:', error)
111
+ })
62
112
 
63
113
  // Pause/resume
64
- call.pause()
65
- call.resume()
114
+ Micdrop.pause()
115
+ Micdrop.resume()
66
116
 
67
117
  // Stop the call
68
- await call.stop()
118
+ await Micdrop.stop()
69
119
  ```
70
120
 
71
121
  ## Demo
72
122
 
73
- Check out the demo implementation in the [@micdrop/demo-client](../demo-client/README.md) package. It shows:
123
+ Check out the demo implementation in the [demo-client example](../../examples/demo-client/README.md) package. It shows:
74
124
 
75
125
  - Setting up a React application with WebSocket communication
76
- - Configuring the CallClient with custom parameters
126
+ - Configuring the MicdropClient with custom parameters
77
127
  - Managing microphone input and audio playback
78
128
  - Handling conversation state and UI updates
79
129
  - Error handling patterns
80
130
 
81
- Here's a simplified version from the demo:
82
-
83
131
  ## Documentation
84
132
 
85
- - **[CallClient](./docs/CallClient.md)** - Manages WebSocket connections, audio streaming, and conversation state
133
+ - **[MicdropClient](./docs/MicdropClient.md)** - Manages WebSocket connections, audio streaming, and conversation state
86
134
 
87
135
  - **[MicRecorder](./docs/MicRecorder.md)** - Records audio from microphone with voice activity detection and speech events
88
136
 
@@ -90,14 +138,19 @@ Here's a simplified version from the demo:
90
138
 
91
139
  - **[Speaker](./docs/Speaker.md)** - Handles audio output devices and playback with analysis capabilities
92
140
 
141
+ - **[VAD](./docs/VAD.md)** - Voice Activity Detection, how to use and customize
142
+
93
143
  ## Browser Support
94
144
 
145
+ Fully supported in Chrome, Firefox, Safari and Edge.
146
+
95
147
  Requires browsers with support for:
96
148
 
97
149
  - WebSocket API
98
- - MediaRecorder API
99
150
  - Web Audio API
100
- - getUserMedia API
151
+ - MediaDevices API
152
+ - MediaRecorder API
153
+ - MediaSource Extensions (MSE)
101
154
 
102
155
  ## License
103
156
 
@@ -105,6 +158,4 @@ MIT
105
158
 
106
159
  ## Author
107
160
 
108
- Originally developed for [Raconte.ai](https://www.raconte.ai)
109
-
110
- by [Lonestone](https://www.lonestone.io) ([GitHub](https://github.com/lonestone))
161
+ Originally developed for [Raconte.ai](https://www.raconte.ai) and open sourced by [Lonestone](https://www.lonestone.io) ([GitHub](https://github.com/lonestone))
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import EventEmitter from 'eventemitter3';
2
- import { VAD as VAD$1, Conversation as Conversation$1 } from '@micdrop/client';
2
+ import { MicdropConversation as MicdropConversation$1, VAD as VAD$1 } from '@micdrop/client';
3
+ import { Readable } from 'stream';
3
4
 
4
5
  interface AudioAnalyserEvents {
5
6
  volume: [number];
@@ -16,13 +17,11 @@ declare class AudioAnalyser extends EventEmitter<AudioAnalyserEvents> {
16
17
  private getMaxVolume;
17
18
  }
18
19
 
19
- declare let mic: Mic;
20
- declare class Mic {
20
+ declare class Mic$1 {
21
21
  analyser: AudioAnalyser;
22
- private currentDeviceId;
22
+ deviceId: string | undefined;
23
23
  private audioStream;
24
24
  private sourceNode;
25
- constructor();
26
25
  private getMicConstraints;
27
26
  /**
28
27
  * Starts the microphone
@@ -35,10 +34,26 @@ declare class Mic {
35
34
  */
36
35
  stop(): void;
37
36
  }
38
- declare global {
39
- interface Window {
40
- micdropMic: Mic;
41
- }
37
+
38
+ interface SpeakerEvents {
39
+ StartPlaying: void;
40
+ StopPlaying: void;
41
+ }
42
+ declare class Speaker$1 extends EventEmitter<SpeakerEvents> {
43
+ isPlaying: boolean;
44
+ analyser: AudioAnalyser;
45
+ deviceId: string | undefined;
46
+ devices: MediaDeviceInfo[];
47
+ private audioStream?;
48
+ private audioSourceNode?;
49
+ private audioElementDestination?;
50
+ start(): Promise<void>;
51
+ playAudio(blob: Blob): Promise<void>;
52
+ stopAudio(): Promise<void>;
53
+ changeDevice(deviceId: string): Promise<void>;
54
+ get isDefaultDevice(): boolean;
55
+ private setIsPlaying;
56
+ private setDevice;
42
57
  }
43
58
 
44
59
  interface VADEvents {
@@ -58,6 +73,7 @@ declare enum VADStatus {
58
73
  * VAD = Voice Activity Detection
59
74
  */
60
75
  declare abstract class VAD extends EventEmitter<VADEvents> {
76
+ readonly name: string;
61
77
  status: VADStatus;
62
78
  delay: number;
63
79
  /**
@@ -105,17 +121,17 @@ interface MicRecorderEvents {
105
121
  }
106
122
  declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
107
123
  state: MicRecorderState;
124
+ vad: VAD;
108
125
  private audioInfo;
109
- private vad;
110
126
  private recorder;
111
127
  private delayedStream;
112
128
  private speakingConfirmed;
113
129
  private queuedChunks;
114
130
  constructor(vadConfig?: VADConfig);
115
- start(stream: MediaStream): Promise<void>;
116
- mute(): void;
117
- unmute(): void;
118
- stop(): void;
131
+ start: (stream: MediaStream) => Promise<void>;
132
+ mute: () => void;
133
+ unmute: () => void;
134
+ stop: () => void;
119
135
  private startSpeakingDetection;
120
136
  private stopSpeakingDetection;
121
137
  private onStartSpeaking;
@@ -127,37 +143,6 @@ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
127
143
  private getAudioInfo;
128
144
  }
129
145
 
130
- declare let speaker: Speaker;
131
- interface SpeakerEvents {
132
- StartPlaying: void;
133
- StopPlaying: void;
134
- }
135
- declare class Speaker extends EventEmitter<SpeakerEvents> {
136
- isPlaying: boolean;
137
- analyser: AudioAnalyser;
138
- private audioElement?;
139
- private player?;
140
- private streamingEnabled;
141
- constructor();
142
- private setupSpeakerPlayer;
143
- get isStreamingEnabled(): boolean;
144
- protected changeIsPlaying(isPlaying: boolean): void;
145
- enableStreaming(): Promise<void>;
146
- disableStreaming(): Promise<void>;
147
- canChangeDevice(): boolean;
148
- private setSinkId;
149
- changeDevice(speakerId: string): Promise<void>;
150
- pauseAudio(): void;
151
- resumeAudio(): void;
152
- playAudio(blob: Blob): Promise<void>;
153
- stopAudio(): void;
154
- }
155
- declare global {
156
- interface Window {
157
- micdropSpeaker: Speaker;
158
- }
159
- }
160
-
161
146
  declare enum LocalStorageKeys {
162
147
  MicDevice = "micdrop.micDevice",
163
148
  SpeakerDevice = "micdrop.speakerDevice",
@@ -225,45 +210,69 @@ declare class VolumeVAD extends VAD {
225
210
  resetOptions(): void;
226
211
  }
227
212
 
228
- declare enum CallClientErrorCode {
213
+ declare const Mic: Mic$1;
214
+ declare const Speaker: Speaker$1;
215
+
216
+ declare enum MicdropClientErrorCode {
217
+ MissingUrl = "MissingUrl",
229
218
  Mic = "Mic",
219
+ Connection = "Connection",
220
+ InternalServer = "InternalServer",
221
+ BadRequest = "BadRequest",
230
222
  Unauthorized = "Unauthorized",
231
- Error = "Error"
223
+ NotFound = "NotFound",
224
+ Unknown = "Unknown"
232
225
  }
233
- declare class CallClientError extends Error {
234
- code: CallClientErrorCode;
235
- constructor(code?: CallClientErrorCode);
226
+ declare class MicdropClientError extends Error {
227
+ code: MicdropClientErrorCode;
228
+ constructor(code?: MicdropClientErrorCode, message?: string);
236
229
  }
230
+ declare function getClientErrorFromWSCloseEvent(event: CloseEvent): MicdropClientError | undefined;
231
+ declare function getClientErrorFromWSCloseEventCode(code: number): MicdropClientErrorCode | undefined;
237
232
 
238
- interface CallClientEvents {
233
+ interface MicdropEvents {
239
234
  EndCall: [];
240
- Error: [CallClientError];
241
- StateChange: void;
235
+ Error: [MicdropClientError];
236
+ StateChange: [MicdropState];
242
237
  }
243
- interface CallClientOptions {
238
+ interface MicdropOptions {
239
+ url?: string;
240
+ params?: Record<string, any>;
244
241
  vad?: VADConfig;
245
242
  disableInterruption?: boolean;
243
+ debugLog?: boolean;
246
244
  }
247
- declare global {
248
- interface Window {
249
- micdropCallClient: any;
250
- }
245
+ interface MicdropState {
246
+ isStarting: boolean;
247
+ isStarted: boolean;
248
+ isPaused: boolean;
249
+ isListening: boolean;
250
+ isProcessing: boolean;
251
+ isUserSpeaking: boolean;
252
+ isAssistantSpeaking: boolean;
253
+ isMicStarted: boolean;
254
+ isMicMuted: boolean;
255
+ micDeviceId: string | undefined;
256
+ speakerDeviceId: string | undefined;
257
+ micDevices: MediaDeviceInfo[];
258
+ speakerDevices: MediaDeviceInfo[];
259
+ conversation: MicdropConversation$1;
260
+ error: MicdropClientError | undefined;
251
261
  }
252
- declare class CallClient<Params extends {}, Options extends CallClientOptions = CallClientOptions> extends EventEmitter<CallClientEvents> {
253
- private options?;
254
- static getInstance<P extends {}, O extends CallClientOptions = CallClientOptions>(options?: O): CallClient<P, O>;
255
- url?: string;
256
- params?: Params;
262
+ declare class MicdropClient extends EventEmitter<MicdropEvents> implements MicdropState {
263
+ options: MicdropOptions;
257
264
  micRecorder?: MicRecorder;
258
- vad: VAD$1;
259
- conversation: Conversation$1;
260
- debug: boolean;
265
+ conversation: MicdropConversation$1;
266
+ error: MicdropClientError | undefined;
267
+ speakerDevices: MediaDeviceInfo[];
268
+ micDevices: MediaDeviceInfo[];
261
269
  private ws?;
262
270
  private micStream?;
263
271
  private startTime;
264
272
  private _isProcessing;
265
273
  private _isPaused;
266
- private constructor();
274
+ constructor(options?: MicdropOptions);
275
+ get vad(): VAD$1 | undefined;
267
276
  get isStarted(): boolean;
268
277
  get isStarting(): boolean;
269
278
  get isPaused(): boolean;
@@ -275,65 +284,139 @@ declare class CallClient<Params extends {}, Options extends CallClientOptions =
275
284
  get isMicMuted(): boolean;
276
285
  get isUserSpeaking(): boolean;
277
286
  get isAssistantSpeaking(): boolean;
278
- start(): Promise<void>;
279
- stop(): Promise<void>;
280
- pause(): void;
281
- resume(): void;
282
- destroy(): Promise<void>;
283
- startMic(deviceId?: string, record?: boolean): Promise<void>;
287
+ get micDeviceId(): string | undefined;
288
+ get speakerDeviceId(): string | undefined;
289
+ get state(): MicdropState;
290
+ start: (options?: MicdropOptions) => Promise<void>;
291
+ stop: () => Promise<void>;
292
+ pause: () => void;
293
+ resume: () => void;
294
+ startMic: ({ vad, deviceId, record, }: {
295
+ vad?: VADConfig;
296
+ deviceId?: string;
297
+ record?: boolean;
298
+ }) => Promise<void>;
299
+ changeMicDevice: (deviceId: string) => Promise<void>;
300
+ changeSpeakerDevice: (deviceId: string) => Promise<void>;
284
301
  private stopMic;
302
+ private updateDevices;
285
303
  private startWS;
304
+ private onWSOpen;
305
+ private onWSMessage;
306
+ private onWSClose;
307
+ private onWSError;
286
308
  private stopWS;
287
309
  private onSpeakerStartPlaying;
288
310
  private onSpeakerStopPlaying;
289
311
  private addMessage;
312
+ private setError;
290
313
  private notifyStateChange;
291
314
  private log;
292
315
  }
293
316
 
294
- declare enum CallClientCommands {
317
+ declare const Micdrop: MicdropClient;
318
+
319
+ declare class Logger {
320
+ private readonly name;
321
+ constructor(name: string);
322
+ log(...message: any[]): void;
323
+ }
324
+
325
+ interface AgentOptions {
326
+ systemPrompt: string;
327
+ }
328
+ interface AgentAnswerReturn {
329
+ message: Promise<string>;
330
+ stream: Readable;
331
+ }
332
+ interface TextPromise {
333
+ promise: Promise<string>;
334
+ resolve: (value: string) => void;
335
+ reject: (reason?: any) => void;
336
+ }
337
+ declare abstract class Agent<Options extends AgentOptions = AgentOptions> extends EventEmitter {
338
+ protected options: Options;
339
+ logger?: Logger;
340
+ conversation: MicdropConversation;
341
+ constructor(options: Options);
342
+ abstract answer(): AgentAnswerReturn;
343
+ abstract cancel(): void;
344
+ addUserMessage(text: string): void;
345
+ addAssistantMessage(text: string): void;
346
+ protected addMessage(role: 'user' | 'assistant' | 'system', text: string): void;
347
+ protected endCall(): void;
348
+ protected cancelLastUserMessage(): void;
349
+ protected cancelLastAssistantMessage(): void;
350
+ protected skipAnswer(): void;
351
+ protected createTextPromise(): TextPromise;
352
+ protected log(...message: any[]): void;
353
+ destroy(): void;
354
+ }
355
+
356
+ declare const MIME_TYPE_TO_EXTENSION: {
357
+ readonly 'audio/wav': "wav";
358
+ readonly 'audio/ogg': "ogg";
359
+ readonly 'audio/mpeg': "mp3";
360
+ readonly 'audio/webm': "webm";
361
+ readonly 'audio/mp4': "mp4";
362
+ readonly 'audio/flac': "flac";
363
+ };
364
+ interface STTEvents {
365
+ Transcript: [string];
366
+ }
367
+ declare abstract class STT extends EventEmitter<STTEvents> {
368
+ protected mimeType?: keyof typeof MIME_TYPE_TO_EXTENSION;
369
+ logger?: Logger;
370
+ transcribe(audioStream: Readable): void;
371
+ protected log(...message: any[]): void;
372
+ destroy(): void;
373
+ protected get extension(): string;
374
+ private detectMimeType;
375
+ }
376
+
377
+ declare abstract class TTS {
378
+ logger?: Logger;
379
+ abstract speak(textStream: Readable): Readable;
380
+ abstract cancel(): void;
381
+ protected log(...message: any[]): void;
382
+ destroy(): void;
383
+ }
384
+
385
+ declare enum MicdropClientCommands {
295
386
  StartSpeaking = "StartSpeaking",
296
387
  StopSpeaking = "StopSpeaking",
297
388
  Mute = "Mute"
298
389
  }
299
- declare enum CallServerCommands {
390
+ declare enum MicdropServerCommands {
300
391
  Message = "Message",
301
392
  CancelLastAssistantMessage = "CancelLastAssistantMessage",
302
393
  CancelLastUserMessage = "CancelLastUserMessage",
303
394
  SkipAnswer = "SkipAnswer",
304
- EnableSpeakerStreaming = "EnableSpeakerStreaming",
305
395
  EndCall = "EndCall"
306
396
  }
307
- interface CallConfig {
308
- systemPrompt: string;
397
+ interface MicdropConfig {
309
398
  firstMessage?: string;
310
- debugLog?: boolean;
311
- debugSaveSpeech?: boolean;
312
- disableTTS?: boolean;
313
- generateAnswer(conversation: Conversation): Promise<string | ConversationMessage>;
314
- speech2Text(audioBlob: Blob, prevMessage?: string): Promise<string>;
315
- text2Speech(text: string): Promise<ArrayBuffer | NodeJS.ReadableStream>;
316
- onMessage?(message: ConversationMessage): void;
317
- onEnd?(call: CallSummary): void;
318
- }
319
- interface CallSummary {
320
- conversation: Conversation;
399
+ generateFirstMessage?: boolean;
400
+ agent: Agent;
401
+ stt: STT;
402
+ tts: TTS;
403
+ onEnd?(call: MicdropCallSummary): void;
404
+ }
405
+ interface MicdropCallSummary {
406
+ conversation: MicdropConversation;
321
407
  duration: number;
322
408
  }
323
- type Conversation = ConversationMessage[];
324
- type AnswerCommands = {
325
- endCall?: boolean;
326
- cancelLastUserMessage?: boolean;
327
- skipAnswer?: boolean;
328
- };
329
- type AnswerMetadata = {
409
+ type MicdropConversation = MicdropConversationMessage[];
410
+ type MicdropAnswerMetadata = {
330
411
  [key: string]: any;
331
412
  };
332
- interface ConversationMessage<Data extends AnswerMetadata = AnswerMetadata> {
413
+ interface MicdropConversationMessage<Data extends MicdropAnswerMetadata = MicdropAnswerMetadata> {
333
414
  role: 'system' | 'user' | 'assistant';
334
415
  content: string;
335
- commands?: AnswerCommands;
336
416
  metadata?: Data;
337
417
  }
418
+ type DeepPartial<T> = T extends object ? {
419
+ [P in keyof T]?: DeepPartial<T[P]>;
420
+ } : T;
338
421
 
339
- export { type AnswerCommands, type AnswerMetadata, CallClient, CallClientCommands, CallClientError, CallClientErrorCode, type CallClientEvents, type CallClientOptions, type CallConfig, CallServerCommands, type CallSummary, type Conversation, type ConversationMessage, LocalStorageKeys, mic as Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, MultipleVAD, SileroVAD, type SileroVADOptions, speaker as Speaker, VAD, type VADEvents, VADStatus, VolumeVAD, type VolumeVADOptions };
422
+ export { type DeepPartial, LocalStorageKeys, Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, Micdrop, type MicdropAnswerMetadata, type MicdropCallSummary, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConfig, type MicdropConversation, type MicdropConversationMessage, type MicdropEvents, type MicdropOptions, MicdropServerCommands, type MicdropState, MultipleVAD, SileroVAD, type SileroVADOptions, Speaker, VAD, type VADEvents, VADStatus, VolumeVAD, type VolumeVADOptions, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode };