@layercode/js-sdk 1.0.27 → 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
|
@@ -3478,10 +3478,10 @@ function arrayBufferToBase64(arrayBuffer) {
|
|
|
3478
3478
|
|
|
3479
3479
|
/* eslint-env browser */
|
|
3480
3480
|
// SDK version - updated when publishing
|
|
3481
|
-
const SDK_VERSION = '
|
|
3481
|
+
const SDK_VERSION = '2.0.0';
|
|
3482
3482
|
/**
|
|
3483
3483
|
* @class LayercodeClient
|
|
3484
|
-
* @classdesc Core client for Layercode audio
|
|
3484
|
+
* @classdesc Core client for Layercode audio agent that manages audio recording, WebSocket communication, and speech processing.
|
|
3485
3485
|
*/
|
|
3486
3486
|
class LayercodeClient {
|
|
3487
3487
|
/**
|
|
@@ -3491,8 +3491,8 @@ class LayercodeClient {
|
|
|
3491
3491
|
constructor(options) {
|
|
3492
3492
|
this.deviceId = null;
|
|
3493
3493
|
this.options = {
|
|
3494
|
-
|
|
3495
|
-
|
|
3494
|
+
agentId: options.agentId,
|
|
3495
|
+
conversationId: options.conversationId || null,
|
|
3496
3496
|
authorizeSessionEndpoint: options.authorizeSessionEndpoint,
|
|
3497
3497
|
metadata: options.metadata || {},
|
|
3498
3498
|
vadResumeDelay: options.vadResumeDelay || 500,
|
|
@@ -3507,18 +3507,18 @@ class LayercodeClient {
|
|
|
3507
3507
|
onUserIsSpeakingChange: options.onUserIsSpeakingChange || (() => { }),
|
|
3508
3508
|
};
|
|
3509
3509
|
this.AMPLITUDE_MONITORING_SAMPLE_RATE = 10;
|
|
3510
|
-
this._websocketUrl = 'wss://api.layercode.com/v1/
|
|
3511
|
-
this.wavRecorder = new WavRecorder({ sampleRate: 8000 }); // TODO should be set my fetched
|
|
3510
|
+
this._websocketUrl = 'wss://api.layercode.com/v1/agents/websocket';
|
|
3511
|
+
this.wavRecorder = new WavRecorder({ sampleRate: 8000 }); // TODO should be set my fetched agent config
|
|
3512
3512
|
this.wavPlayer = new WavStreamPlayer({
|
|
3513
3513
|
finishedPlayingCallback: this._clientResponseAudioReplayFinished.bind(this),
|
|
3514
|
-
sampleRate: 16000, // TODO should be set my fetched
|
|
3514
|
+
sampleRate: 16000, // TODO should be set my fetched agent config
|
|
3515
3515
|
});
|
|
3516
3516
|
this.vad = null;
|
|
3517
3517
|
this.ws = null;
|
|
3518
3518
|
this.status = 'disconnected';
|
|
3519
3519
|
this.userAudioAmplitude = 0;
|
|
3520
3520
|
this.agentAudioAmplitude = 0;
|
|
3521
|
-
this.
|
|
3521
|
+
this.conversationId = options.conversationId || null;
|
|
3522
3522
|
this.pushToTalkActive = false;
|
|
3523
3523
|
this.pushToTalkEnabled = false;
|
|
3524
3524
|
this.canInterrupt = false;
|
|
@@ -3817,7 +3817,7 @@ class LayercodeClient {
|
|
|
3817
3817
|
}
|
|
3818
3818
|
}
|
|
3819
3819
|
/**
|
|
3820
|
-
* Connects to the Layercode
|
|
3820
|
+
* Connects to the Layercode agent and starts the audio conversation
|
|
3821
3821
|
* @async
|
|
3822
3822
|
* @returns {Promise<void>}
|
|
3823
3823
|
*/
|
|
@@ -3826,15 +3826,15 @@ class LayercodeClient {
|
|
|
3826
3826
|
this._setStatus('connecting');
|
|
3827
3827
|
// Reset turn tracking for clean start
|
|
3828
3828
|
this._resetTurnTracking();
|
|
3829
|
-
// Get
|
|
3829
|
+
// Get conversation key from server
|
|
3830
3830
|
let authorizeSessionRequestBody = {
|
|
3831
|
-
|
|
3831
|
+
agent_id: this.options.agentId,
|
|
3832
3832
|
metadata: this.options.metadata,
|
|
3833
3833
|
sdk_version: SDK_VERSION,
|
|
3834
3834
|
};
|
|
3835
|
-
// If we're reconnecting to a previous
|
|
3836
|
-
if (this.options.
|
|
3837
|
-
authorizeSessionRequestBody.
|
|
3835
|
+
// If we're reconnecting to a previous conversation, we need to include the conversation_id in the request. Otherwise we don't send conversation_id, and a new conversation will be created and the conversation_id will be returned in the response.
|
|
3836
|
+
if (this.options.conversationId) {
|
|
3837
|
+
authorizeSessionRequestBody.conversation_id = this.options.conversationId;
|
|
3838
3838
|
}
|
|
3839
3839
|
const authorizeSessionResponse = await fetch(this.options.authorizeSessionEndpoint, {
|
|
3840
3840
|
method: 'POST',
|
|
@@ -3844,10 +3844,10 @@ class LayercodeClient {
|
|
|
3844
3844
|
body: JSON.stringify(authorizeSessionRequestBody),
|
|
3845
3845
|
});
|
|
3846
3846
|
if (!authorizeSessionResponse.ok) {
|
|
3847
|
-
throw new Error(`Failed to authorize
|
|
3847
|
+
throw new Error(`Failed to authorize conversation: ${authorizeSessionResponse.statusText}`);
|
|
3848
3848
|
}
|
|
3849
3849
|
const authorizeSessionResponseBody = await authorizeSessionResponse.json();
|
|
3850
|
-
this.
|
|
3850
|
+
this.conversationId = authorizeSessionResponseBody.conversation_id; // Save the conversation_id for use in future reconnects
|
|
3851
3851
|
// Connect WebSocket
|
|
3852
3852
|
this.ws = new WebSocket(`${this._websocketUrl}?${new URLSearchParams({
|
|
3853
3853
|
client_session_key: authorizeSessionResponseBody.client_session_key,
|
|
@@ -3871,7 +3871,7 @@ class LayercodeClient {
|
|
|
3871
3871
|
this.ws.onopen = () => {
|
|
3872
3872
|
console.log('WebSocket connection established');
|
|
3873
3873
|
this._setStatus('connected');
|
|
3874
|
-
this.options.onConnect({
|
|
3874
|
+
this.options.onConnect({ conversationId: this.conversationId });
|
|
3875
3875
|
// Attempt to send ready message if recorder already started
|
|
3876
3876
|
this._sendReadyIfNeeded();
|
|
3877
3877
|
};
|
|
@@ -3894,7 +3894,7 @@ class LayercodeClient {
|
|
|
3894
3894
|
// this is to ensure that the device is initialized before the recorder is started
|
|
3895
3895
|
}
|
|
3896
3896
|
catch (error) {
|
|
3897
|
-
console.error('Error connecting to Layercode
|
|
3897
|
+
console.error('Error connecting to Layercode agent:', error);
|
|
3898
3898
|
this._setStatus('error');
|
|
3899
3899
|
this.options.onError(error instanceof Error ? error : new Error(String(error)));
|
|
3900
3900
|
throw error;
|