@convai/web-sdk 2.0.0 → 2.0.2-beta.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.
|
@@ -1369,7 +1369,7 @@ export class ConvaiClient extends EventEmitter {
|
|
|
1369
1369
|
// cannot leave a reused client in published-session mode.
|
|
1370
1370
|
this._isPublishedChatSession = false;
|
|
1371
1371
|
const stored = this._storedConfig ?? {};
|
|
1372
|
-
const url = stored.url
|
|
1372
|
+
const url = stored.url || "https://realtime-api.convai.com";
|
|
1373
1373
|
if (!stored.apiKey && !stored.authToken) {
|
|
1374
1374
|
throw new Error("Either apiKey or authToken is required to join a room");
|
|
1375
1375
|
}
|
|
@@ -1441,10 +1441,12 @@ export class ConvaiClient extends EventEmitter {
|
|
|
1441
1441
|
*/
|
|
1442
1442
|
applyConfigDefaults(finalConfig, opts = {}) {
|
|
1443
1443
|
const { requireCredential = true } = opts;
|
|
1444
|
-
//
|
|
1444
|
+
// Default the URL when it is missing, undefined or empty. A spread
|
|
1445
|
+
// default would be overwritten by `url: undefined`, which is what
|
|
1446
|
+
// `url: import.meta.env.X` gives when X is unset.
|
|
1445
1447
|
const configWithDefaults = {
|
|
1446
|
-
url: "https://realtime-api.convai.com",
|
|
1447
1448
|
...finalConfig,
|
|
1449
|
+
url: finalConfig.url || "https://realtime-api.convai.com",
|
|
1448
1450
|
};
|
|
1449
1451
|
this._logRtviMessages = configWithDefaults.logRtviMessages !== false;
|
|
1450
1452
|
this._messageHandler.setRtviMessageLogging(this._logRtviMessages);
|
|
@@ -1764,7 +1766,7 @@ export class ConvaiClient extends EventEmitter {
|
|
|
1764
1766
|
const disconnectOnAbort = () => {
|
|
1765
1767
|
abortDisconnect ?? (abortDisconnect = this.disconnect().catch(() => undefined));
|
|
1766
1768
|
};
|
|
1767
|
-
const apiUrl = (options.url
|
|
1769
|
+
const apiUrl = (options.url || "https://realtime-api.convai.com").replace(/\/+$/, "");
|
|
1768
1770
|
const connectAttemptId = options.connectAttemptId ??
|
|
1769
1771
|
(typeof crypto !== "undefined" && "randomUUID" in crypto
|
|
1770
1772
|
? `published_${crypto.randomUUID().replace(/-/g, "")}`
|
|
@@ -13,6 +13,8 @@ export declare class AudioRenderer {
|
|
|
13
13
|
private boundHandleTrackSubscribed;
|
|
14
14
|
private boundHandleTrackUnsubscribed;
|
|
15
15
|
private boundHandleParticipantDisconnected;
|
|
16
|
+
private boundUnlockAudio;
|
|
17
|
+
private unlockArmed;
|
|
16
18
|
constructor(room: Room, options?: {
|
|
17
19
|
container?: HTMLElement;
|
|
18
20
|
});
|
|
@@ -49,6 +51,15 @@ export declare class AudioRenderer {
|
|
|
49
51
|
* Attach a MediaStreamTrack from WebSocket transport for bot audio playback.
|
|
50
52
|
*/
|
|
51
53
|
attachWebSocketTrack(track: MediaStreamTrack): void;
|
|
54
|
+
/**
|
|
55
|
+
* Browsers block audio that starts before the visitor has clicked, tapped or
|
|
56
|
+
* pressed a key on the page, and an `autoplay` element that was blocked stays
|
|
57
|
+
* paused. When playback is refused, wait for the first gesture and retry.
|
|
58
|
+
*/
|
|
59
|
+
private playOrDefer;
|
|
60
|
+
private armUnlock;
|
|
61
|
+
private disarmUnlock;
|
|
62
|
+
private unlockAudio;
|
|
52
63
|
detachWebSocketTrack(): void;
|
|
53
64
|
/**
|
|
54
65
|
* Cleanup and destroy the audio renderer
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { RoomEvent } from 'livekit-client';
|
|
2
2
|
import { logger } from '../utils/logger.js';
|
|
3
|
+
/** Gestures that let a page start audio. */
|
|
4
|
+
const UNLOCK_EVENTS = ['pointerdown', 'keydown', 'touchstart'];
|
|
3
5
|
/**
|
|
4
6
|
* AudioRenderer - Manages audio playback for remote participants
|
|
5
7
|
* Automatically creates and attaches audio elements for remote audio tracks
|
|
@@ -9,6 +11,8 @@ export class AudioRenderer {
|
|
|
9
11
|
constructor(room, options) {
|
|
10
12
|
this.audioElements = new Map();
|
|
11
13
|
this.livekitTracks = new Map(); // Store track references
|
|
14
|
+
this.boundUnlockAudio = () => this.unlockAudio();
|
|
15
|
+
this.unlockArmed = false;
|
|
12
16
|
this._wsAudioElement = null;
|
|
13
17
|
this.room = room;
|
|
14
18
|
this.rendererId = `renderer-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
@@ -96,6 +100,7 @@ export class AudioRenderer {
|
|
|
96
100
|
this.container.appendChild(audioElement);
|
|
97
101
|
this.audioElements.set(trackSid, audioElement);
|
|
98
102
|
this.livekitTracks.set(trackSid, track);
|
|
103
|
+
this.playOrDefer(audioElement);
|
|
99
104
|
}
|
|
100
105
|
catch (error) {
|
|
101
106
|
logger.error('Failed to attach audio track:', error);
|
|
@@ -130,6 +135,44 @@ export class AudioRenderer {
|
|
|
130
135
|
el.srcObject = new MediaStream([track]);
|
|
131
136
|
this.container.appendChild(el);
|
|
132
137
|
this._wsAudioElement = el;
|
|
138
|
+
this.playOrDefer(el);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Browsers block audio that starts before the visitor has clicked, tapped or
|
|
142
|
+
* pressed a key on the page, and an `autoplay` element that was blocked stays
|
|
143
|
+
* paused. When playback is refused, wait for the first gesture and retry.
|
|
144
|
+
*/
|
|
145
|
+
playOrDefer(element) {
|
|
146
|
+
const playing = element.play?.();
|
|
147
|
+
if (playing && typeof playing.catch === 'function') {
|
|
148
|
+
playing.catch((error) => {
|
|
149
|
+
if (error?.name === 'NotAllowedError')
|
|
150
|
+
this.armUnlock();
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
armUnlock() {
|
|
155
|
+
if (this.unlockArmed || typeof document === 'undefined')
|
|
156
|
+
return;
|
|
157
|
+
this.unlockArmed = true;
|
|
158
|
+
for (const type of UNLOCK_EVENTS)
|
|
159
|
+
document.addEventListener(type, this.boundUnlockAudio, true);
|
|
160
|
+
}
|
|
161
|
+
disarmUnlock() {
|
|
162
|
+
if (!this.unlockArmed)
|
|
163
|
+
return;
|
|
164
|
+
this.unlockArmed = false;
|
|
165
|
+
for (const type of UNLOCK_EVENTS)
|
|
166
|
+
document.removeEventListener(type, this.boundUnlockAudio, true);
|
|
167
|
+
}
|
|
168
|
+
unlockAudio() {
|
|
169
|
+
this.disarmUnlock();
|
|
170
|
+
void this.room.startAudio?.().catch(() => { });
|
|
171
|
+
const elements = [...this.audioElements.values(), ...(this._wsAudioElement ? [this._wsAudioElement] : [])];
|
|
172
|
+
for (const element of elements) {
|
|
173
|
+
if (element.paused)
|
|
174
|
+
this.playOrDefer(element);
|
|
175
|
+
}
|
|
133
176
|
}
|
|
134
177
|
detachWebSocketTrack() {
|
|
135
178
|
if (this._wsAudioElement) {
|
|
@@ -143,6 +186,7 @@ export class AudioRenderer {
|
|
|
143
186
|
* Cleanup and destroy the audio renderer
|
|
144
187
|
*/
|
|
145
188
|
destroy() {
|
|
189
|
+
this.disarmUnlock();
|
|
146
190
|
this.detachWebSocketTrack();
|
|
147
191
|
// Remove all audio elements
|
|
148
192
|
this.audioElements.forEach((element, trackSid) => {
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "2.0.0";
|
|
1
|
+
export declare const SDK_VERSION = "2.0.2-beta.0";
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@convai/web-sdk",
|
|
3
|
-
"version": "2.0.0",
|
|
3
|
+
"version": "2.0.2-beta.0",
|
|
4
4
|
"description": "Build web apps with lifelike AI characters. The Convai Web SDK gives you real-time voice, lipsync, emotions, and dynamic context — with first-class support for React and vanilla TypeScript.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|