@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/dist/index.d.ts 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 };