@layercode/js-sdk 2.3.0 → 2.3.2

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.
@@ -17,6 +17,16 @@ export interface AgentConfig {
17
17
  frame_samples?: number;
18
18
  };
19
19
  }
20
+ interface AuthorizeSessionRequestParams {
21
+ url: string;
22
+ body: {
23
+ agent_id: string;
24
+ metadata: Record<string, any>;
25
+ sdk_version: string;
26
+ conversation_id?: string | null;
27
+ };
28
+ }
29
+ type AuthorizeSessionRequest = (params: AuthorizeSessionRequestParams) => Promise<Response>;
20
30
  /**
21
31
  * Interface for LayercodeClient public methods
22
32
  */
@@ -27,6 +37,7 @@ interface ILayercodeClient {
27
37
  triggerUserTurnFinished(): Promise<void>;
28
38
  getStream(): MediaStream | null;
29
39
  setInputDevice(deviceId: string): Promise<void>;
40
+ setAudioInput(state: boolean): Promise<void>;
30
41
  listDevices(): Promise<Array<MediaDeviceInfo & {
31
42
  default: boolean;
32
43
  }>>;
@@ -38,6 +49,8 @@ interface ILayercodeClient {
38
49
  readonly agentAudioAmplitude: number;
39
50
  readonly isMuted: boolean;
40
51
  readonly conversationId: string | null;
52
+ readonly userSpeaking: boolean;
53
+ readonly agentSpeaking: boolean;
41
54
  }
42
55
  /**
43
56
  * Interface for LayercodeClient constructor options
@@ -49,9 +62,15 @@ interface LayercodeClientOptions {
49
62
  conversationId?: string | null;
50
63
  /** The endpoint URL for the audio agent API */
51
64
  authorizeSessionEndpoint: string;
65
+ /** Optional custom request handler for authorizing a session */
66
+ authorizeSessionRequest?: AuthorizeSessionRequest;
52
67
  /** Metadata to send with webhooks */
53
68
  metadata?: Record<string, any>;
54
- /** Milliseconds before resuming assistant audio after temporary pause due to user interruption (which was actually a false interruption) */
69
+ /** Whether audio input is enabled. I.e. is the microphone turned on in the browser */
70
+ audioInput?: boolean;
71
+ /** Fired when audio input flag changes */
72
+ audioInputChanged?: (audioInput: boolean) => void;
73
+ /** Milliseconds before resuming agent audio after temporary pause due to user interruption (which was actually a false interruption) */
55
74
  vadResumeDelay?: number;
56
75
  /** Callback when connection is established */
57
76
  onConnect?: ({ conversationId, config }: {
@@ -80,8 +99,12 @@ interface LayercodeClientOptions {
80
99
  onStatusChange?: (status: string) => void;
81
100
  /** Callback when user turn changes */
82
101
  onUserIsSpeakingChange?: (isSpeaking: boolean) => void;
102
+ /** Callback when agent speaking state changes */
103
+ onAgentSpeakingChange?: (isSpeaking: boolean) => void;
83
104
  /** Callback when mute state changes */
84
105
  onMuteStateChange?: (isMuted: boolean) => void;
106
+ /** Whether amplitude monitoring should run for mic and speaker */
107
+ enableAmplitudeMonitoring?: boolean;
85
108
  }
86
109
  /**
87
110
  * @class LayercodeClient
@@ -93,11 +116,13 @@ declare class LayercodeClient implements ILayercodeClient {
93
116
  private wavPlayer;
94
117
  private vad;
95
118
  private ws;
119
+ private audioInput;
96
120
  private AMPLITUDE_MONITORING_SAMPLE_RATE;
97
121
  private pushToTalkActive;
98
122
  private pushToTalkEnabled;
99
123
  private canInterrupt;
100
124
  private userIsSpeaking;
125
+ private agentIsSpeaking;
101
126
  private recorderStarted;
102
127
  private readySent;
103
128
  private currentTurnId;
@@ -128,11 +153,13 @@ declare class LayercodeClient implements ILayercodeClient {
128
153
  * @param {string} status - New status value
129
154
  */
130
155
  private _setStatus;
156
+ private _setAgentSpeaking;
157
+ private _setUserSpeaking;
131
158
  /**
132
159
  * Handles when agent audio finishes playing
133
160
  */
134
161
  private _clientResponseAudioReplayFinished;
135
- private _clientInterruptAssistantReplay;
162
+ private _clientInterruptAgentReplay;
136
163
  triggerUserTurnStarted(): Promise<void>;
137
164
  triggerUserTurnFinished(): Promise<void>;
138
165
  sendClientResponseText(text: string): Promise<void>;
@@ -156,12 +183,25 @@ declare class LayercodeClient implements ILayercodeClient {
156
183
  */
157
184
  private _setupAmplitudeMonitoring;
158
185
  private _stopAmplitudeMonitoring;
186
+ audioInputConnect(): Promise<void>;
187
+ audioInputDisconnect(): Promise<void>;
188
+ setAudioInput(state: boolean): Promise<void>;
189
+ /** Emitters for audio flags */
190
+ private _emitAudioInput;
191
+ get audioInputEnabled(): boolean;
192
+ get userSpeaking(): boolean;
193
+ get agentSpeaking(): boolean;
159
194
  /**
160
195
  * Connects to the Layercode agent using the stored conversation ID and starts the audio conversation
161
196
  * @async
162
197
  * @returns {Promise<void>}
163
198
  */
164
199
  connect(): Promise<void>;
200
+ private bindWebsocketMessageCallbacks;
201
+ private setupVadConfig;
202
+ private authorizeSession;
203
+ private setupAudioOutput;
204
+ private connectToAudioInput;
165
205
  private _resetTurnTracking;
166
206
  disconnect(): Promise<void>;
167
207
  /**
@@ -185,6 +225,10 @@ declare class LayercodeClient implements ILayercodeClient {
185
225
  * Restarts audio recording after a device switch to ensure audio is captured from the new device
186
226
  */
187
227
  private _restartAudioRecording;
228
+ /**
229
+ * Disconnect VAD
230
+ */
231
+ private stopVad;
188
232
  /**
189
233
  * Reinitializes VAD with a new stream (used after device switching)
190
234
  */
@@ -207,4 +251,4 @@ declare class LayercodeClient implements ILayercodeClient {
207
251
  unmute(): void;
208
252
  }
209
253
  export default LayercodeClient;
210
- export type { AgentConfig, ILayercodeClient, LayercodeClientOptions };
254
+ export type { ILayercodeClient, LayercodeClientOptions, AuthorizeSessionRequest, AuthorizeSessionRequestParams };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Layercode",
3
3
  "license": "MIT",
4
4
  "name": "@layercode/js-sdk",
5
- "version": "2.3.0",
5
+ "version": "2.3.2",
6
6
  "description": "Layercode JavaScript SDK for browser usage",
7
7
  "type": "module",
8
8
  "main": "dist/layercode-js-sdk.esm.js",