@convai/web-sdk 2.1.0-beta.0 → 2.1.0-beta.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.
- package/README.md +37 -0
- package/dist/core/BlendshapeQueue.d.ts +17 -0
- package/dist/core/BlendshapeQueue.js +43 -0
- package/dist/core/ConvaiClient.d.ts +11 -0
- package/dist/core/ConvaiClient.js +59 -1
- package/dist/core/DataAudioPlayer.d.ts +131 -0
- package/dist/core/DataAudioPlayer.js +404 -0
- package/dist/core/MessageHandler.d.ts +13 -0
- package/dist/core/MessageHandler.js +57 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.js +2 -0
- package/dist/core/streamController.d.ts +72 -0
- package/dist/core/streamController.js +110 -0
- package/dist/core/types.d.ts +84 -0
- package/dist/react/hooks/useConvaiClient.js +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When to play and pause a reply whose voice arrives over the data channel
|
|
3
|
+
* (`audioRouting: 'data_only'`).
|
|
4
|
+
*
|
|
5
|
+
* Pure decision logic: it is told what arrived and where playback is, and answers
|
|
6
|
+
* play or pause. {@link DataAudioPlayer} drives it every 20 ms.
|
|
7
|
+
*
|
|
8
|
+
* One rule, from the Convai Unreal plugin's ConvaiAudioStreamer, which plays
|
|
9
|
+
* min(audio, lipsync): only SYNCED data is playable, the seconds for which both the
|
|
10
|
+
* audio and its blendshapes are in hand. Playback starts or resumes once `minBuffer`
|
|
11
|
+
* seconds of synced data are ahead of the playhead, and pauses when less than
|
|
12
|
+
* `lowWater` is left, or a little earlier, below `silenceWater`, if the buffer holds
|
|
13
|
+
* a silence to pause on so the stop falls between words. Once the reply has fully arrived, everything left plays out. The mouth is
|
|
14
|
+
* never asked to follow audio it has no frames for.
|
|
15
|
+
*/
|
|
16
|
+
export const STREAM_DEFAULTS = {
|
|
17
|
+
minBuffer: 0.9,
|
|
18
|
+
// Low: the stretch between resume and pause is minBuffer - lowWater. At 0.6 a 0.9 s
|
|
19
|
+
// buffer played 0.3 s at a time (116 pauses on a 71 s story at 0.68x; 32 at 0.15).
|
|
20
|
+
lowWater: 0.15,
|
|
21
|
+
silenceWater: 0.4,
|
|
22
|
+
};
|
|
23
|
+
const fresh = (state) => ({ state, audio: 0, frames: 0, total: null, ended: false, pauses: 0 });
|
|
24
|
+
export class StreamController {
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
this.s = fresh("idle");
|
|
27
|
+
/**
|
|
28
|
+
* Frames that arrived while no reply was open. On SaaS a reply's blendshapes start
|
|
29
|
+
* arriving before its first audio chunk (2.4 s of them, measured); they belong to it.
|
|
30
|
+
*/
|
|
31
|
+
this.early = 0;
|
|
32
|
+
this.o = { ...STREAM_DEFAULTS, ...options };
|
|
33
|
+
}
|
|
34
|
+
get snapshot() {
|
|
35
|
+
return this.s;
|
|
36
|
+
}
|
|
37
|
+
get state() {
|
|
38
|
+
return this.s.state;
|
|
39
|
+
}
|
|
40
|
+
/** Start a fresh reply. */
|
|
41
|
+
newReply() {
|
|
42
|
+
this.s = fresh("buffering");
|
|
43
|
+
this.s.frames = this.early;
|
|
44
|
+
this.early = 0;
|
|
45
|
+
}
|
|
46
|
+
/** `seconds` of audio arrived. Starts a reply if none is open. */
|
|
47
|
+
onAudio(_t, seconds) {
|
|
48
|
+
if (this.s.state === "idle" || this.s.state === "done")
|
|
49
|
+
this.newReply();
|
|
50
|
+
this.s.audio += seconds;
|
|
51
|
+
}
|
|
52
|
+
/** `seconds` of lipsync arrived for the open reply. */
|
|
53
|
+
onFrames(seconds) {
|
|
54
|
+
if (this.s.state !== "idle" && this.s.state !== "done")
|
|
55
|
+
this.s.frames += seconds;
|
|
56
|
+
else
|
|
57
|
+
this.early += seconds;
|
|
58
|
+
}
|
|
59
|
+
/** The turn announced the reply's length. */
|
|
60
|
+
onTotal(seconds) {
|
|
61
|
+
this.s.total = seconds;
|
|
62
|
+
}
|
|
63
|
+
/** All of the reply's audio is in: play it out, no more waiting. */
|
|
64
|
+
onEnded() {
|
|
65
|
+
this.s.ended = true;
|
|
66
|
+
}
|
|
67
|
+
/** Drop the open reply (an interruption). */
|
|
68
|
+
reset() {
|
|
69
|
+
this.s = fresh("idle");
|
|
70
|
+
this.early = 0;
|
|
71
|
+
}
|
|
72
|
+
/** Mark the open reply as played out (the player reached its end). */
|
|
73
|
+
finish() {
|
|
74
|
+
if (this.s.state === "playing" || this.s.state === "paused")
|
|
75
|
+
this.s.state = "done";
|
|
76
|
+
}
|
|
77
|
+
/** Seconds of the reply with both audio and blendshapes in hand. */
|
|
78
|
+
get synced() {
|
|
79
|
+
return Math.min(this.s.audio, this.s.frames);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Decide at wall time `t` (s), with `played` seconds of the reply already played.
|
|
83
|
+
* `silenceAhead` says whether the synced audio left holds a silence to pause on.
|
|
84
|
+
* Returns 'play', 'pause', or null for no change.
|
|
85
|
+
*/
|
|
86
|
+
tick(_t, played, silenceAhead = () => false) {
|
|
87
|
+
const s = this.s;
|
|
88
|
+
const ahead = this.synced - played;
|
|
89
|
+
if (s.state === "buffering" || s.state === "paused") {
|
|
90
|
+
if (s.ended || ahead >= this.o.minBuffer) {
|
|
91
|
+
s.state = "playing";
|
|
92
|
+
return "play";
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
if (s.state === "playing") {
|
|
97
|
+
// 50 ms: the heard position trails what has been played by the output latency.
|
|
98
|
+
if (s.ended && s.audio - played <= 0.05) {
|
|
99
|
+
s.state = "done";
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
if (!s.ended && (ahead < this.o.lowWater || (ahead < this.o.silenceWater && silenceAhead()))) {
|
|
103
|
+
s.state = "paused";
|
|
104
|
+
s.pauses++;
|
|
105
|
+
return "pause";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
package/dist/core/types.d.ts
CHANGED
|
@@ -801,6 +801,30 @@ export interface ConvaiConfig {
|
|
|
801
801
|
iceTransportPolicy?: RTCIceTransportPolicy;
|
|
802
802
|
/** Enable text-to-speech audio generation (default: true for LiveKit, false for SSE). */
|
|
803
803
|
ttsEnabled?: boolean;
|
|
804
|
+
/**
|
|
805
|
+
* How the character's voice reaches the client (LiveKit transport).
|
|
806
|
+
*
|
|
807
|
+
* - `"webrtc"` (default): an audio track, played by the browser as it arrives.
|
|
808
|
+
* - `"data_only"`: PCM chunks over the data channel, next to the lipsync
|
|
809
|
+
* frames. The SDK plays them itself ({@link IConvaiClient.dataAudio}): it
|
|
810
|
+
* holds a reply until enough audio and lipsync are in hand, plays it as one
|
|
811
|
+
* continuous stream, and on a link slower than real time pauses on a
|
|
812
|
+
* silence instead of stuttering. On this path `speakingChange` and
|
|
813
|
+
* `state.isSpeaking` follow what is HEARD, not when the server sent it.
|
|
814
|
+
*
|
|
815
|
+
* @example
|
|
816
|
+
* ```ts
|
|
817
|
+
* const client = new ConvaiClient({ apiKey, characterId, enableLipsync: true, audioRouting: 'data_only' })
|
|
818
|
+
* ```
|
|
819
|
+
*/
|
|
820
|
+
audioRouting?: AudioRouting;
|
|
821
|
+
/**
|
|
822
|
+
* With `audioRouting: 'data_only'`: seconds of synced audio and lipsync to have in hand
|
|
823
|
+
* before a reply plays, and again before it resumes after running low. Larger values
|
|
824
|
+
* mean fewer, longer pauses on a link slower than real time; on a fast link they only
|
|
825
|
+
* delay the start a little. Default 0.9 (the Convai Unreal plugin's MinBufferDuration).
|
|
826
|
+
*/
|
|
827
|
+
dataAudioBufferSeconds?: number;
|
|
804
828
|
/**
|
|
805
829
|
* Enable lipsync/facial animation blendshapes (default: false).
|
|
806
830
|
* When true, sets blendshape_provider to "neurosync".
|
|
@@ -1226,6 +1250,49 @@ export type RawMessageEvent = {
|
|
|
1226
1250
|
/** @deprecated Use {@link SSERequestContext}; retained for error-listener compatibility. */
|
|
1227
1251
|
export type SSERequestErrorContext = SSERequestContext;
|
|
1228
1252
|
/** A streamed signed 16-bit little-endian PCM audio chunk. */
|
|
1253
|
+
/** How the character's voice is delivered: a WebRTC audio track, or PCM chunks over the data channel. */
|
|
1254
|
+
export type AudioRouting = "webrtc" | "data_only";
|
|
1255
|
+
/** The lifecycle of the reply now being played on the data-channel audio path. */
|
|
1256
|
+
export type DataAudioState = "idle" | "buffering" | "playing" | "paused" | "done";
|
|
1257
|
+
/**
|
|
1258
|
+
* The SDK's player for `audioRouting: 'data_only'`. One per client; null on the
|
|
1259
|
+
* WebRTC path.
|
|
1260
|
+
*
|
|
1261
|
+
* Events (`on(name, cb)`, returning an unsubscribe):
|
|
1262
|
+
* - `replyStarted` — the reply's first sample is heard.
|
|
1263
|
+
* - `replyEnded` `{ interrupted }` — the reply has played out, or was interrupted.
|
|
1264
|
+
* - `paused` `{ atSilence }` / `resumed` — a rebuffer on a link slower than real time.
|
|
1265
|
+
* - `underrun` — playback ran out mid-reply (should not happen; counted in `stats`).
|
|
1266
|
+
*/
|
|
1267
|
+
export interface DataAudioPlayback {
|
|
1268
|
+
/** Where the open reply is: buffering before it starts, playing, paused to refill, done. */
|
|
1269
|
+
readonly state: DataAudioState;
|
|
1270
|
+
/** Chunks received, clean pauses taken, and underruns (stutters) since the client was made. */
|
|
1271
|
+
readonly stats: {
|
|
1272
|
+
chunks: number;
|
|
1273
|
+
pauses: number;
|
|
1274
|
+
underruns: number;
|
|
1275
|
+
};
|
|
1276
|
+
/** Seconds of the open reply heard so far; null before it starts. Holds while paused. */
|
|
1277
|
+
position(): number | null;
|
|
1278
|
+
/** RMS of the voice now playing (0–1); null when it cannot be read yet. */
|
|
1279
|
+
level(): number | null;
|
|
1280
|
+
/** Seconds of audio buffered ahead of what is heard. */
|
|
1281
|
+
queuedSeconds(): number;
|
|
1282
|
+
/**
|
|
1283
|
+
* The reply's heard position as an RTCRtpReceiver synchronization source
|
|
1284
|
+
* (48 kHz RTP units), so a lipsync clock written for a WebRTC receiver can
|
|
1285
|
+
* follow this player unchanged. Empty before a reply starts.
|
|
1286
|
+
*/
|
|
1287
|
+
getSynchronizationSources(): Array<{
|
|
1288
|
+
rtpTimestamp: number;
|
|
1289
|
+
timestamp: number;
|
|
1290
|
+
audioLevel?: number;
|
|
1291
|
+
}>;
|
|
1292
|
+
/** Resume audio output; call from a user gesture where the page blocks autoplay. */
|
|
1293
|
+
resume(): Promise<void>;
|
|
1294
|
+
on(event: "replyStarted" | "replyEnded" | "paused" | "resumed" | "underrun", callback: (detail: unknown) => void): () => void;
|
|
1295
|
+
}
|
|
1229
1296
|
export interface AudioDataEvent {
|
|
1230
1297
|
/** Sample rate of this chunk in hertz. */
|
|
1231
1298
|
sample_rate: number;
|
|
@@ -1263,6 +1330,12 @@ export interface ConvaiEventMap {
|
|
|
1263
1330
|
messagesChange: [messages: ChatMessage[]];
|
|
1264
1331
|
userTranscriptionChange: [transcription: string];
|
|
1265
1332
|
speakingChange: [isSpeaking: boolean];
|
|
1333
|
+
/**
|
|
1334
|
+
* The server's own bot-started/stopped-speaking flags, with
|
|
1335
|
+
* `audioRouting: 'data_only'`. They follow when the server SENT the audio;
|
|
1336
|
+
* `speakingChange` follows when it is heard.
|
|
1337
|
+
*/
|
|
1338
|
+
serverSpeakingChange: [isSpeaking: boolean];
|
|
1266
1339
|
listeningChange: [isListening: boolean];
|
|
1267
1340
|
botRespondingChange: [isResponding: boolean];
|
|
1268
1341
|
turnEnd: [turn: {
|
|
@@ -2101,6 +2174,17 @@ export interface IConvaiClient {
|
|
|
2101
2174
|
readonly screenShareControls: ScreenShareControls;
|
|
2102
2175
|
/** Blendshape queue for time-based lipsync synchronization */
|
|
2103
2176
|
readonly blendshapeQueue: BlendshapeQueue;
|
|
2177
|
+
/**
|
|
2178
|
+
* The player for the character's voice when `audioRouting: 'data_only'`;
|
|
2179
|
+
* null on the WebRTC path.
|
|
2180
|
+
*
|
|
2181
|
+
* @example
|
|
2182
|
+
* ```ts
|
|
2183
|
+
* client.dataAudio?.on('paused', () => console.log('waiting for the network'))
|
|
2184
|
+
* const heard = client.dataAudio?.position() // seconds into the reply
|
|
2185
|
+
* ```
|
|
2186
|
+
*/
|
|
2187
|
+
readonly dataAudio: DataAudioPlayback | null;
|
|
2104
2188
|
/** Incremental turn session ID used by conversation events (e.g. conversationStart, turnEnd) */
|
|
2105
2189
|
readonly conversationSessionId: number;
|
|
2106
2190
|
/**
|
|
@@ -201,6 +201,7 @@ export const useConvaiClient = (config) => {
|
|
|
201
201
|
videoControls: client.videoControls,
|
|
202
202
|
screenShareControls: client.screenShareControls,
|
|
203
203
|
blendshapeQueue: client.blendshapeQueue,
|
|
204
|
+
dataAudio: client.dataAudio,
|
|
204
205
|
conversationSessionId: client.conversationSessionId,
|
|
205
206
|
memoryManager: client.memoryManager,
|
|
206
207
|
characterVersions: client.characterVersions,
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "2.1.0-beta.
|
|
1
|
+
export declare const SDK_VERSION = "2.1.0-beta.2";
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@convai/web-sdk",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.2",
|
|
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",
|